expr: add dup expression support
Pablo Neira Ayuso
maintainerPATCH 1/3 expr: add dup expression support
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- include/libnftnl/expr.h | 5 + include/linux/netfilter/nf_tables.h | 14 +++ src/Makefile.am | 1 + src/expr/dup.c | 220 +++++++++++++++++++++++++++++++++++ src/expr_ops.c | 2 + tests/Makefile.am | 4 + tests/nft-expr_dup-test.c | 94 +++++++++++++++ 7 files changed, 340 insertions(+) create mode 100644 src/expr/dup.c create mode 100644 tests/nft-expr_dup-test.c
include/libnftnl/expr.h +5 -0
@@ -173,6 +173,11 @@ enum { NFT_EXPR_REDIR_FLAGS, }; +enum { +NFT_EXPR_DUP_SREG_ADDR =NFT_RULE_EXPR_ATTR_BASE , +NFT_EXPR_DUP_SREG_DEV , +}; + #ifdef __cplusplus }/* extern "C" */ #endif
include/linux/netfilter/nf_tables.h +14 -0
@@ -936,6 +936,20 @@ enum nft_redir_attributes {#define NFTA_REDIR_MAX (__NFTA_REDIR_MAX - 1)/** +* enum nft_tee_attributes - nf_tables tee expression netlink attributes +* +* @NFTA_DUP_SREG_ADDR: source register of destination (NLA_U32: nft_registers) +* @NFTA_DUP_SREG_DEV: output interface name (NLA_U32: nft_register) +*/ +enum nft_tee_attributes { +NFTA_DUP_UNSPEC , +NFTA_DUP_SREG_ADDR , +NFTA_DUP_SREG_DEV , + __NFTA_DUP_MAX +}; +#define NFTA_DUP_MAX (__NFTA_DUP_MAX - 1) + +/** * enum nft_gen_attributes - nf_tables ruleset generation attributes * * @NFTA_GEN_ID: Ruleset generation ID (NLA_U32)
src/Makefile.am +1 -0
@@ -26,6 +26,7 @@ libnftnl_la_SOURCES = utils.c \ expr/counter.c \ expr/ct.c \ expr/data_reg.c \ + expr/dup.c \ expr/exthdr.c \ expr/limit.c \ expr/log.c \
src/expr/dup.c new file +220 -0
220 lines not on this page · raw message
src/expr_ops.c +2 -0
@@ -9,6 +9,7 @@ extern struct expr_ops expr_ops_byteorder;extern struct expr_ops expr_ops_cmp;extern struct expr_ops expr_ops_counter;extern struct expr_ops expr_ops_ct; +extern struct expr_ops expr_ops_dup;extern struct expr_ops expr_ops_exthdr;extern struct expr_ops expr_ops_immediate;extern struct expr_ops expr_ops_limit;
@@ -31,6 +32,7 @@ static struct expr_ops *expr_ops[] = { &expr_ops_cmp, &expr_ops_counter, &expr_ops_ct, + &expr_ops_dup, &expr_ops_exthdr, &expr_ops_immediate, &expr_ops_limit,
tests/Makefile.am +4 -0
@@ -14,6 +14,7 @@ check_PROGRAMS = nft-parsing-test \ nft-expr_counter-test \ nft-expr_cmp-test \ nft-expr_ct-test \ + nft-expr_dup-test \ nft-expr_exthdr-test \ nft-expr_immediate-test \ nft-expr_limit-test \
@@ -62,6 +63,9 @@ nft_expr_exthdr_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} nft_expr_ct_test_SOURCES = nft-expr_ct-test.c nft_expr_ct_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} +nft_expr_dup_test_SOURCES = nft-expr_dup-test.c +nft_expr_dup_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} + nft_expr_immediate_test_SOURCES = nft-expr_counter-test.c nft_expr_immediate_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
tests/nft-expr_dup-test.c new file +94 -0
94 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 2/3 expr: limit: add burst attribute
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- include/buffer.h | 1 + include/libnftnl/expr.h | 1 + include/linux/netfilter/nf_tables.h | 2 ++ src/expr/limit.c | 32 ++++++++++++++++++++++++++++---- tests/nft-expr_limit-test.c | 4 ++++ 5 files changed, 36 insertions(+), 4 deletions(-)
include/buffer.h +1 -0
@@ -38,6 +38,7 @@ int nft_buf_reg(struct nft_buf *b, int type, union nft_data_reg *reg,#define BASE "base"#define BYTES "bytes" +#define BURST "burst"#define CHAIN "chain"#define CODE "code"#define DATA "data"
include/libnftnl/expr.h +1 -0
@@ -150,6 +150,7 @@ enum {enum {NFT_EXPR_LIMIT_RATE =NFT_RULE_EXPR_ATTR_BASE ,NFT_EXPR_LIMIT_UNIT , +NFT_EXPR_LIMIT_BURST , };enum {
include/linux/netfilter/nf_tables.h +2 -0
@@ -761,11 +761,13 @@ enum nft_ct_attributes {* * @NFTA_LIMIT_RATE: refill rate (NLA_U64) * @NFTA_LIMIT_UNIT: refill unit (NLA_U64) +* @NFTA_LIMIT_BURST: burst (NLA_U32) */ enum nft_limit_attributes {NFTA_LIMIT_UNSPEC ,NFTA_LIMIT_RATE ,NFTA_LIMIT_UNIT , +NFTA_LIMIT_BURST , __NFTA_LIMIT_MAX };#define NFTA_LIMIT_MAX (__NFTA_LIMIT_MAX - 1)
src/expr/limit.c +28 -4
@@ -25,6 +25,7 @@struct nft_expr_limit {uint64_t rate;uint64_t unit; +uint32_t burst; }; static int
@@ -40,6 +41,9 @@ nft_rule_expr_limit_set(struct nft_rule_expr *e, uint16_t type,case NFT_EXPR_LIMIT_UNIT : limit->unit = *((uint64_t *)data);break ; +case NFT_EXPR_LIMIT_BURST : + limit->burst = *((uint32_t *)data); +break ;default :return -1 ; }
@@ -59,6 +63,9 @@ nft_rule_expr_limit_get(const struct nft_rule_expr *e, uint16_t type,case NFT_EXPR_LIMIT_UNIT : *data_len =sizeof (uint64_t );return &limit->unit; +case NFT_EXPR_LIMIT_BURST : + *data_len =sizeof (uint32_t ); +return &limit->burst; }return NULL ; }
@@ -77,6 +84,10 @@ static int nft_rule_expr_limit_cb(const struct nlattr *attr, void *data)if (mnl_attr_validate (attr,MNL_TYPE_U64 ) <0 )abi_breakage ();break ; +case NFTA_LIMIT_BURST : +if (mnl_attr_validate (attr,MNL_TYPE_U32 ) <0 ) +abi_breakage (); +break ; } tb[type] = attr;
@@ -92,6 +103,8 @@ nft_rule_expr_limit_build(struct nlmsghdr *nlh, struct nft_rule_expr *e)mnl_attr_put_u64 (nlh,NFTA_LIMIT_RATE ,htobe64 (limit->rate));if (e->flags & (1 <<NFT_EXPR_LIMIT_UNIT ))mnl_attr_put_u64 (nlh,NFTA_LIMIT_UNIT ,htobe64 (limit->unit)); +if (e->flags & (1 <<NFT_EXPR_LIMIT_BURST )) +mnl_attr_put_u32 (nlh,NFTA_LIMIT_BURST ,htonl (limit->burst)); } static int
@@ -111,6 +124,10 @@ nft_rule_expr_limit_parse(struct nft_rule_expr *e, struct nlattr *attr) limit->unit =be64toh (mnl_attr_get_u64 (tb[NFTA_LIMIT_UNIT ])); e->flags |= (1 <<NFT_EXPR_LIMIT_UNIT ); } +if (tb[NFTA_LIMIT_BURST ]) { + limit->burst =ntohl (mnl_attr_get_u32 (tb[NFTA_LIMIT_BURST ])); + e->flags |= (1 <<NFT_EXPR_LIMIT_BURST ); + }return 0 ; }
@@ -120,12 +137,14 @@ static int nft_rule_expr_limit_json_parse(struct nft_rule_expr *e, json_t *root, {#ifdef JSON_PARSING uint64_t uval64; +uint32_t uval32;if (nft_jansson_parse_val (root,"rate" ,NFT_TYPE_U64 , &uval64, err) ==0 )nft_rule_expr_set_u64 (e,NFT_EXPR_LIMIT_RATE , uval64); -if (nft_jansson_parse_val (root,"unit" ,NFT_TYPE_U64 , &uval64, err) ==0 )nft_rule_expr_set_u64 (e,NFT_EXPR_LIMIT_UNIT , uval64); +if (nft_jansson_parse_val (root,"burst" ,NFT_TYPE_U32 , &uval32, err) ==0 ) +nft_rule_expr_set_u32 (e,NFT_EXPR_LIMIT_BURST , uval32);return 0 ;#else
@@ -140,14 +159,17 @@ static int nft_rule_expr_limit_xml_parse(struct nft_rule_expr *e, {#ifdef XML_PARSING uint64_t rate, unit; +uint32_t burst;if (nft_mxml_num_parse (tree,"rate" ,MXML_DESCEND_FIRST ,BASE_DEC , &rate,NFT_TYPE_U64 ,NFT_XML_MAND , err) ==0 )nft_rule_expr_set_u64 (e,NFT_EXPR_LIMIT_RATE , rate); -if (nft_mxml_num_parse (tree,"unit" ,MXML_DESCEND_FIRST ,BASE_DEC , &unit,NFT_TYPE_U64 ,NFT_XML_MAND , err) ==0 )nft_rule_expr_set_u64 (e,NFT_EXPR_LIMIT_UNIT , unit); +if (nft_mxml_num_parse (tree,"burst" ,MXML_DESCEND_FIRST ,BASE_DEC , + &burst,NFT_TYPE_U32 ,NFT_XML_MAND , err) ==0 ) +nft_rule_expr_set_u32 (e,NFT_EXPR_LIMIT_BURST , burst);return 0 ;#else
@@ -178,6 +200,8 @@ static int nft_rule_expr_limit_export(char *buf, size_t size,nft_buf_u64 (&b, type, limit->rate,RATE );if (e->flags & (1 <<NFT_EXPR_LIMIT_UNIT ))nft_buf_u64 (&b, type, limit->unit,UNIT ); +if (e->flags & (1 <<NFT_EXPR_LIMIT_BURST )) +nft_buf_u32 (&b, type, limit->burst,BURST );return nft_buf_done (&b); }
@@ -187,8 +211,8 @@ static int nft_rule_expr_limit_snprintf_default(char *buf, size_t len, {struct nft_expr_limit *limit =nft_expr_data (e); -return snprintf (buf, len,"rate %" PRIu64"/%s " , - limit->rate,get_unit (limit->unit)); +return snprintf (buf, len,"rate %" PRIu64"/%s burst %u " , + limit->rate,get_unit (limit->unit), limit->burst); } static int
tests/nft-expr_limit-test.c +4 -0
@@ -37,6 +37,9 @@ static void cmp_nft_rule_expr(struct nft_rule_expr *rule_a,if (nft_rule_expr_get_u64 (rule_a,NFT_EXPR_LIMIT_UNIT ) !=nft_rule_expr_get_u64 (rule_b,NFT_EXPR_LIMIT_UNIT ))print_err ("Expr CTR_PACKET mismatches" ); +if (nft_rule_expr_get_u64 (rule_a,NFT_EXPR_LIMIT_BURST ) != +nft_rule_expr_get_u64 (rule_b,NFT_EXPR_LIMIT_BURST )) +print_err ("Expr CTR_PACKET mismatches" ); }int main (int argc,char *argv[])
@@ -58,6 +61,7 @@ int main(int argc, char *argv[])nft_rule_expr_set_u64 (ex,NFT_EXPR_LIMIT_RATE ,0x123456789abcdef0 );nft_rule_expr_set_u64 (ex,NFT_EXPR_LIMIT_UNIT ,0x123456789abcdef0 ); +nft_rule_expr_set_u32 (ex,NFT_EXPR_LIMIT_BURST ,0x89123456 );nft_rule_add_expr (a, ex);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 3/3 expr: limit: add per-byte limiting support
In reply to Pablo Neira AyusoSigned-off-by Pablo Neira Ayuso <[email protected]>
--- include/libnftnl/expr.h | 1 + include/linux/netfilter/nf_tables.h | 7 ++++++ src/expr/limit.c | 40 ++++++++++++++++++++++++++++++++--- tests/nft-expr_limit-test.c | 4 ++++ 4 files changed, 49 insertions(+), 3 deletions(-)
include/libnftnl/expr.h +1 -0
@@ -151,6 +151,7 @@ enum {NFT_EXPR_LIMIT_RATE =NFT_RULE_EXPR_ATTR_BASE ,NFT_EXPR_LIMIT_UNIT ,NFT_EXPR_LIMIT_BURST , + NFT_EXPR_LIMIT_TYPE, }; enum {
include/linux/netfilter/nf_tables.h +7 -0
@@ -756,18 +756,25 @@ enum nft_ct_attributes { };#define NFTA_CT_MAX (__NFTA_CT_MAX - 1) +enum nft_limit_type { +NFT_LIMIT_PKTS , +NFT_LIMIT_BYTES +}; +/** * enum nft_limit_attributes - nf_tables limit expression netlink attributes * * @NFTA_LIMIT_RATE: refill rate (NLA_U64) * @NFTA_LIMIT_UNIT: refill unit (NLA_U64) * @NFTA_LIMIT_BURST: burst (NLA_U32) +* @NFTA_LIMIT_TYPE: type of limit (NLA_U32: enum nft_limit_type) */ enum nft_limit_attributes {NFTA_LIMIT_UNSPEC ,NFTA_LIMIT_RATE ,NFTA_LIMIT_UNIT ,NFTA_LIMIT_BURST , +NFTA_LIMIT_TYPE , __NFTA_LIMIT_MAX };#define NFTA_LIMIT_MAX (__NFTA_LIMIT_MAX - 1)
src/expr/limit.c +37 -3
@@ -26,6 +26,7 @@ struct nft_expr_limit {uint64_t rate;uint64_t unit;uint32_t burst; +enum nft_limit_type type; }; static int
@@ -44,6 +45,9 @@ nft_rule_expr_limit_set(struct nft_rule_expr *e, uint16_t type,case NFT_EXPR_LIMIT_BURST : limit->burst = *((uint32_t *)data);break ; +case NFT_EXPR_LIMIT_TYPE : + limit->type = *((uint32_t *)data); +break ;default :return -1 ; }
@@ -66,6 +70,9 @@ nft_rule_expr_limit_get(const struct nft_rule_expr *e, uint16_t type,case NFT_EXPR_LIMIT_BURST : *data_len =sizeof (uint32_t );return &limit->burst; +case NFT_EXPR_LIMIT_TYPE : + *data_len =sizeof (uint32_t ); +return &limit->type; }return NULL ; }
@@ -85,6 +92,7 @@ static int nft_rule_expr_limit_cb(const struct nlattr *attr, void *data)abi_breakage ();break ;case NFTA_LIMIT_BURST : +case NFTA_LIMIT_TYPE :if (mnl_attr_validate (attr,MNL_TYPE_U32 ) <0 )abi_breakage ();break ;
@@ -105,6 +113,8 @@ nft_rule_expr_limit_build(struct nlmsghdr *nlh, struct nft_rule_expr *e)mnl_attr_put_u64 (nlh,NFTA_LIMIT_UNIT ,htobe64 (limit->unit));if (e->flags & (1 <<NFT_EXPR_LIMIT_BURST ))mnl_attr_put_u32 (nlh,NFTA_LIMIT_BURST ,htonl (limit->burst)); +if (e->flags & (1 <<NFT_EXPR_LIMIT_TYPE )) +mnl_attr_put_u32 (nlh,NFTA_LIMIT_TYPE ,htonl (limit->type)); } static int
@@ -128,6 +138,10 @@ nft_rule_expr_limit_parse(struct nft_rule_expr *e, struct nlattr *attr) limit->burst =ntohl (mnl_attr_get_u32 (tb[NFTA_LIMIT_BURST ])); e->flags |= (1 <<NFT_EXPR_LIMIT_BURST ); } +if (tb[NFTA_LIMIT_TYPE ]) { + limit->type =ntohl (mnl_attr_get_u32 (tb[NFTA_LIMIT_TYPE ])); + e->flags |= (1 <<NFT_EXPR_LIMIT_TYPE ); + }return 0 ; }
@@ -145,6 +159,8 @@ static int nft_rule_expr_limit_json_parse(struct nft_rule_expr *e, json_t *root,nft_rule_expr_set_u64 (e,NFT_EXPR_LIMIT_UNIT , uval64);if (nft_jansson_parse_val (root,"burst" ,NFT_TYPE_U32 , &uval32, err) ==0 )nft_rule_expr_set_u32 (e,NFT_EXPR_LIMIT_BURST , uval32); +if (nft_jansson_parse_val (root,"type" ,NFT_TYPE_U32 , &uval32, err) ==0 ) +nft_rule_expr_set_u32 (e,NFT_EXPR_LIMIT_TYPE , uval32);return 0 ;#else
@@ -159,7 +175,7 @@ static int nft_rule_expr_limit_xml_parse(struct nft_rule_expr *e, {#ifdef XML_PARSING uint64_t rate, unit; -uint32_t burst; +uint32_t burst, type;if (nft_mxml_num_parse (tree,"rate" ,MXML_DESCEND_FIRST ,BASE_DEC , &rate,NFT_TYPE_U64 ,NFT_XML_MAND , err) ==0 )
@@ -170,6 +186,9 @@ static int nft_rule_expr_limit_xml_parse(struct nft_rule_expr *e,if (nft_mxml_num_parse (tree,"burst" ,MXML_DESCEND_FIRST ,BASE_DEC , &burst,NFT_TYPE_U32 ,NFT_XML_MAND , err) ==0 )nft_rule_expr_set_u32 (e,NFT_EXPR_LIMIT_BURST , burst); +if (nft_mxml_num_parse (tree,"type" ,MXML_DESCEND_FIRST ,BASE_DEC , + &burst,NFT_TYPE_U32 ,NFT_XML_MAND , err) ==0 ) +nft_rule_expr_set_u32 (e,NFT_EXPR_LIMIT_TYPE , type);return 0 ;#else
@@ -202,17 +221,32 @@ static int nft_rule_expr_limit_export(char *buf, size_t size,nft_buf_u64 (&b, type, limit->unit,UNIT );if (e->flags & (1 <<NFT_EXPR_LIMIT_BURST ))nft_buf_u32 (&b, type, limit->burst,BURST ); +if (e->flags & (1 <<NFT_EXPR_LIMIT_TYPE )) +nft_buf_u32 (&b, type, limit->type,TYPE );return nft_buf_done (&b); } +static const char *limit_to_type (enum nft_limit_type type) +{ +switch (type) { +default : +case NFT_LIMIT_PKTS : +return "packets" ; +case NFT_LIMIT_BYTES : +return "bytes" ; + } +return "unknown" ; +} +static int nft_rule_expr_limit_snprintf_default (char *buf,size_t len,struct nft_rule_expr *e) {struct nft_expr_limit *limit =nft_expr_data (e); -return snprintf (buf, len,"rate %" PRIu64"/%s burst %u " , - limit->rate,get_unit (limit->unit), limit->burst); +return snprintf (buf, len,"rate %" PRIu64"/%s burst %u type %s " , + limit->rate,get_unit (limit->unit), limit->burst, +limit_to_type (limit->type)); } static int
tests/nft-expr_limit-test.c +4 -0
@@ -40,6 +40,9 @@ static void cmp_nft_rule_expr(struct nft_rule_expr *rule_a,if (nft_rule_expr_get_u64 (rule_a,NFT_EXPR_LIMIT_BURST ) !=nft_rule_expr_get_u64 (rule_b,NFT_EXPR_LIMIT_BURST ))print_err ("Expr CTR_PACKET mismatches" ); +if (nft_rule_expr_get_u64 (rule_a,NFT_EXPR_LIMIT_TYPE ) != +nft_rule_expr_get_u64 (rule_b,NFT_EXPR_LIMIT_TYPE )) +print_err ("Expr TYPE mismatches" ); }int main (int argc,char *argv[])
@@ -62,6 +65,7 @@ int main(int argc, char *argv[])nft_rule_expr_set_u64 (ex,NFT_EXPR_LIMIT_RATE ,0x123456789abcdef0 );nft_rule_expr_set_u64 (ex,NFT_EXPR_LIMIT_UNIT ,0x123456789abcdef0 );nft_rule_expr_set_u32 (ex,NFT_EXPR_LIMIT_BURST ,0x89123456 ); +nft_rule_expr_set_u32 (ex,NFT_EXPR_LIMIT_TYPE ,0xdef01234 );nft_rule_add_expr (a, ex);
Signature · 1 line
1.7.10.4
include/linux/netfilter/nf_tables.h enum nft_redir_attributes { · line 944
Quoted 13 lines · Pablo Neira Ayuso
index a99e6a9..cf4a1ce 100644
include/linux/netfilter/nf_tables.h +6
-0@@ -936,6 +936,20 @@ enum nft_redir_attributes {#define NFTA_REDIR_MAX (__NFTA_REDIR_MAX - 1)/** +* enum nft_tee_attributes - nf_tables tee expression netlink attributes +* +* @NFTA_DUP_SREG_ADDR: source register of destination (NLA_U32: nft_registers) +* @NFTA_DUP_SREG_DEV: output interface name (NLA_U32: nft_register) +*/ +enum nft_tee_attributes {
Shouldn't these be called nft_dup_attributes?
Quoted 6 lines
include/linux/netfilter/nf_tables.h +6
-0+NFTA_DUP_UNSPEC , +NFTA_DUP_SREG_ADDR , +NFTA_DUP_SREG_DEV , + __NFTA_DUP_MAX +}; +#define NFTA_DUP_MAX (__NFTA_DUP_MAX - 1)
Pablo Neira Ayuso
maintainerQuoted 7 lines · Patrick McHardy
Quoted 6 lines · Pablo Neira Ayuso
index a99e6a9..cf4a1ce 100644
include/linux/netfilter/nf_tables.h +0
-0@@ -936,6 +936,20 @@ enum nft_redir_attributes {#define NFTA_REDIR_MAX (__NFTA_REDIR_MAX - 1)
Quoted 9 lines
include/linux/netfilter/nf_tables.h enum nft_redir_attributes { · line 944
Quoted 7 lines
include/linux/netfilter/nf_tables.h +6
-0/** +* enum nft_tee_attributes - nf_tables tee expression netlink attributes +* +* @NFTA_DUP_SREG_ADDR: source register of destination (NLA_U32: nft_registers) +* @NFTA_DUP_SREG_DEV: output interface name (NLA_U32: nft_register) +*/ +enum nft_tee_attributes {Shouldn't these be called
nft_dup_attributes?
Yes, this is a leftover from previous kernel patches, will resync this headers and will push this into master after that is fixed. Thanks!