Netfilter updates for net-next
Subsystems (3)
Pablo Neira Ayuso
maintainercover Netfilter updates for net-next
Hi David,
The following patchset contains Netfilter updates for your net-next tree, most relevantly they are:
1) Extend nft_exthdr to allow to match TCP options bitfields, from Manuel Messner.
2) Allow to check if IPv6 extension header is present in nf_tables, from Phil Sutter.
3) Allow to set and match conntrack zone in nf_tables, patches from Florian Westphal.
4) Several patches for the nf_tables set infrastructure, this includes cleanup and preparatory patches to add the new bitmap set type.
5) Add optional ruleset generation ID check to nf_tables and allow to delete rules that got no public handle yet via NFTA_RULE_ID. These patches add the missing kernel infrastructure to support rule deletion by description from userspace.
6) Missing NFT_SET_OBJECT flag to select the right backend when sets stores an object map.
7) A couple of cleanups for the expectation and SIP helper, from Gao feng.
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Thanks!
----------------------------------------------------------------
The following changes since commit 6e7bc478c9a006c701c14476ec9d389a484b4864:
net: skb_needs_check() accepts CHECKSUM_NONE for tx (2017-02-03 17:33:01 -0500)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git HEAD
for you to fetch changes up to 7286ff7fde9f963736c7e575572899d8e16b06b7:
netfilter: nf_tables: honor NFT_SET_OBJECT in set backend selection (2017-02-12 14:45:14 +0100)
----------------------------------------------------------------
Florian Westphal (3):
netfilter: nft_ct: add zone id get support
netfilter: nft_ct: prepare for key-dependent error unwind
netfilter: nft_ct: add zone id set support
Gao Feng (2):
netfilter: nf_ct_sip: Use mod_timer_pending()
netfilter: nf_ct_expect: nf_ct_expect_insert() returns void
Manuel Messner (1):
netfilter: nft_exthdr: add TCP option matching
Pablo Neira Ayuso (14):
netfilter: nf_tables: pass netns to set->ops->remove()
netfilter: nf_tables: use struct nft_set_iter in set element flush
netfilter: nf_tables: rename deactivate_one() to flush()
netfilter: nf_tables: add flush field to struct nft_set_iter
netfilter: nf_tables: rename struct nft_set_estimate class field
netfilter: nf_tables: add space notation to sets
netfilter: nf_tables: add bitmap set type
netfilter: nfnetlink: get rid of u_intX_t types
netfilter: nfnetlink: add nfnetlink_rcv_skb_batch()
netfilter: nfnetlink: allow to check for generation ID
netfilter: nf_tables: add check_genid to the nfnetlink subsystem
netfilter: nf_tables: add NFTA_RULE_ID attribute
netfilter: update MAINTAINERS
netfilter: nf_tables: honor NFT_SET_OBJECT in set backend selection
Phil Sutter (1):
netfilter: nft_exthdr: Add support for existence check
MAINTAINERS | 3 +- include/linux/netfilter/nfnetlink.h | 1 + include/net/netfilter/nf_tables.h | 21 ++- include/uapi/linux/netfilter/nf_tables.h | 27 ++- include/uapi/linux/netfilter/nfnetlink.h | 12 ++ net/netfilter/Kconfig | 10 +- net/netfilter/Makefile | 1 + net/netfilter/nf_conntrack_expect.c | 8 +- net/netfilter/nf_conntrack_sip.c | 12 +- net/netfilter/nf_tables_api.c | 89 ++++++--- net/netfilter/nfnetlink.c | 90 ++++++--- net/netfilter/nft_ct.c | 195 +++++++++++++++++-- net/netfilter/nft_exthdr.c | 139 ++++++++++++-- net/netfilter/nft_set_bitmap.c | 314 +++++++++++++++++++++++++++++++ net/netfilter/nft_set_hash.c | 16 +- net/netfilter/nft_set_rbtree.c | 16 +- 16 files changed, 832 insertions(+), 122 deletions(-) create mode 100644 net/netfilter/nft_set_bitmap.c
Pablo Neira Ayuso
maintainerPATCH 1/21 netfilter: nft_exthdr: Add support for existence check
From: Phil Sutter <[email protected]>
If NFT_EXTHDR_F_PRESENT is set, exthdr will not copy any header field data into *dest, but instead set it to 1 if the header is found and 0 otherwise.
--- include/uapi/linux/netfilter/nf_tables.h | 6 ++++++ net/netfilter/nft_exthdr.c | 22 ++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +6 -0
@@ -704,6 +704,10 @@ enum nft_payload_attributes { };#define NFTA_PAYLOAD_MAX (__NFTA_PAYLOAD_MAX - 1) +enum nft_exthdr_flags { +NFT_EXTHDR_F_PRESENT = (1 <<0 ), +}; +/** * enum nft_exthdr_attributes - nf_tables IPv6 extension header expression netlink attributes *
@@ -711,6 +715,7 @@ enum nft_payload_attributes {* @NFTA_EXTHDR_TYPE: extension header type (NLA_U8) * @NFTA_EXTHDR_OFFSET: extension header offset (NLA_U32) * @NFTA_EXTHDR_LEN: extension header length (NLA_U32) +* @NFTA_EXTHDR_FLAGS: extension header flags (NLA_U32) */ enum nft_exthdr_attributes {NFTA_EXTHDR_UNSPEC ,
@@ -718,6 +723,7 @@ enum nft_exthdr_attributes {NFTA_EXTHDR_TYPE ,NFTA_EXTHDR_OFFSET ,NFTA_EXTHDR_LEN , +NFTA_EXTHDR_FLAGS , __NFTA_EXTHDR_MAX };#define NFTA_EXTHDR_MAX (__NFTA_EXTHDR_MAX - 1)
net/netfilter/nft_exthdr.c +20 -2
@@ -23,6 +23,7 @@ struct nft_exthdr {u8 offset;u8 len; enum nft_registers dreg:8; +u8 flags; }; static void nft_exthdr_eval(const struct nft_expr *expr,
@@ -35,8 +36,12 @@ static void nft_exthdr_eval(const struct nft_expr *expr,int err; err =ipv6_find_hdr (pkt->skb, &offset, priv->type,NULL ,NULL ); -if (err <0 ) +if (priv->flags &NFT_EXTHDR_F_PRESENT ) { + *dest = (err >=0 ); +return ; + }else if (err <0 ) { goto err; + } offset += priv->offset; dest[priv->len /NFT_REG32_SIZE ] =0 ;
@@ -52,6 +57,7 @@ static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = { [NFTA_EXTHDR_TYPE] = { .type = NLA_U8 }, [NFTA_EXTHDR_OFFSET] = { .type = NLA_U32 }, [NFTA_EXTHDR_LEN] = { .type = NLA_U32 }, + [NFTA_EXTHDR_FLAGS] = { .type = NLA_U32 }, }; static int nft_exthdr_init(const struct nft_ctx *ctx,
@@ -59,7 +65,7 @@ static int nft_exthdr_init(const struct nft_ctx *ctx, const struct nlattr * const tb[]) {struct nft_exthdr *priv =nft_expr_priv (expr); -u32 offset, len; +u32 offset, len, flags =0 ;int err; if (tb[NFTA_EXTHDR_DREG] == NULL ||
@@ -76,10 +82,20 @@ static int nft_exthdr_init(const struct nft_ctx *ctx,if (err <0 )return err; +if (tb[NFTA_EXTHDR_FLAGS ]) { + err =nft_parse_u32_check (tb[NFTA_EXTHDR_FLAGS ],U8_MAX , &flags); +if (err <0 ) +return err; + +if (flags & ~NFT_EXTHDR_F_PRESENT ) +return -EINVAL ; + } + priv->type =nla_get_u8 (tb[NFTA_EXTHDR_TYPE ]); priv->offset = offset; priv->len = len; priv->dreg =nft_parse_register (tb[NFTA_EXTHDR_DREG ]); + priv->flags = flags;return nft_validate_register_store (ctx, priv->dreg,NULL ,NFT_DATA_VALUE , priv->len);
@@ -97,6 +113,8 @@ static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr) goto nla_put_failure;if (nla_put_be32 (skb,NFTA_EXTHDR_LEN ,htonl (priv->len))) goto nla_put_failure; +if (nla_put_be32 (skb,NFTA_EXTHDR_FLAGS ,htonl (priv->flags))) + goto nla_put_failure;return 0 ; nla_put_failure:
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 2/21 netfilter: nf_tables: pass netns to set->ops->remove()
In reply to Pablo Neira AyusoThis new parameter is required by the new bitmap set type that comes in a follow up patch.
--- include/net/netfilter/nf_tables.h | 3 ++- net/netfilter/nf_tables_api.c | 6 +++--- net/netfilter/nft_set_hash.c | 3 ++- net/netfilter/nft_set_rbtree.c | 3 ++- 4 files changed, 9 insertions(+), 6 deletions(-)
include/net/netfilter/nf_tables.h +2 -1
@@ -298,7 +298,8 @@ struct nft_set_ops {bool (*deactivate_one)(const struct net *net,const struct nft_set *set,void *priv); -void (*remove)(const struct nft_set *set, +void (*remove)(const struct net *net, +const struct nft_set *set,const struct nft_set_elem *elem); void (*walk)(const struct nft_ctx *ctx, struct nft_set *set,
net/netfilter/nf_tables_api.c +3 -3
@@ -3752,7 +3752,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,return 0 ; err6: - set->ops->remove (set, &elem); + set->ops->remove (ctx->net, set, &elem); err5:kfree (trans); err4:
@@ -4804,7 +4804,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)nf_tables_setelem_notify (&trans->ctx, te->set, &te->elem,NFT_MSG_DELSETELEM ,0 ); - te->set->ops->remove (te->set, &te->elem); + te->set->ops->remove (net, te->set, &te->elem);atomic_dec (&te->set->nelems); te->set->ndeact--;break ;
@@ -4925,7 +4925,7 @@ static int nf_tables_abort(struct net *net, struct sk_buff *skb)case NFT_MSG_NEWSETELEM : te = (struct nft_trans_elem *)trans->data; - te->set->ops->remove (te->set, &te->elem); + te->set->ops->remove (net, te->set, &te->elem);atomic_dec (&te->set->nelems);break ;case NFT_MSG_DELSETELEM :
net/netfilter/nft_set_hash.c +2 -1
@@ -203,7 +203,8 @@ static void *nft_hash_deactivate(const struct net *net,return he; } -static void nft_hash_remove (const struct nft_set *set, +static void nft_hash_remove (const struct net *net, +const struct nft_set *set,const struct nft_set_elem *elem) {struct nft_hash *priv =nft_set_priv (set);
net/netfilter/nft_set_rbtree.c +2 -1
@@ -151,7 +151,8 @@ static int nft_rbtree_insert(const struct net *net, const struct nft_set *set,return err; } -static void nft_rbtree_remove (const struct nft_set *set, +static void nft_rbtree_remove (const struct net *net, +const struct nft_set *set,const struct nft_set_elem *elem) {struct nft_rbtree *priv =nft_set_priv (set);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 3/21 netfilter: nf_tables: use struct nft_set_iter in set element flush
In reply to Pablo Neira AyusoInstead of struct nft_set_dump_args, remove unnecessary wrapper structure.
--- net/netfilter/nf_tables_api.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
net/netfilter/nf_tables_api.c +5 -7
@@ -3936,15 +3936,13 @@ static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,return -EBUSY ;if (nla[NFTA_SET_ELEM_LIST_ELEMENTS ] ==NULL ) { -struct nft_set_dump_args args = { - .iter = { - .genmask = genmask, - .fn = nft_flush_set, - }, +struct nft_set_iter iter = { + .genmask = genmask, + .fn = nft_flush_set, }; - set->ops->walk (&ctx, set, &args.iter); + set->ops->walk (&ctx, set, &iter); -return args.iter.err; +return iter.err; }nla_for_each_nested (attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS ], rem) {
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 4/21 netfilter: nf_tables: rename deactivate_one() to flush()
In reply to Pablo Neira AyusoAlthough semantics are similar to deactivate() with no implicit element lookup, this is only called from the set flush path, so better rename this to flush().
--- include/net/netfilter/nf_tables.h | 8 ++++---- net/netfilter/nf_tables_api.c | 2 +- net/netfilter/nft_set_hash.c | 8 ++++---- net/netfilter/nft_set_rbtree.c | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-)
include/net/netfilter/nf_tables.h +4 -4
@@ -260,7 +260,7 @@ struct nft_expr;* @insert: insert new element into set * @activate: activate new element in the next generation * @deactivate: lookup for element and deactivate it in the next generation -* @deactivate_one: deactivate element in the next generation +* @flush: deactivate element in the next generation * @remove: remove element from set * @walk: iterate over all set elemeennts * @privsize: function to return size of set private data
@@ -295,9 +295,9 @@ struct nft_set_ops {void * (*deactivate)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem); -bool (*deactivate_one)(const struct net *net, -const struct nft_set *set, -void *priv); +bool (*flush)(const struct net *net, +const struct nft_set *set, +void *priv);void (*remove)(const struct net *net,const struct nft_set *set,const struct nft_set_elem *elem);
net/netfilter/nf_tables_api.c +1 -1
@@ -3898,7 +3898,7 @@ static int nft_flush_set(const struct nft_ctx *ctx,if (!trans)return -ENOMEM ; -if (!set->ops->deactivate_one (ctx->net, set, elem->priv)) { +if (!set->ops->flush (ctx->net, set, elem->priv)) { err = -ENOENT ; goto err1; }
net/netfilter/nft_set_hash.c +4 -4
@@ -167,8 +167,8 @@ static void nft_hash_activate(const struct net *net, const struct nft_set *set,nft_set_elem_clear_busy (&he->ext); } -static bool nft_hash_deactivate_one (const struct net *net, -const struct nft_set *set,void *priv) +static bool nft_hash_flush (const struct net *net, +const struct nft_set *set,void *priv) {struct nft_hash_elem *he = priv;
@@ -195,7 +195,7 @@ static void *nft_hash_deactivate(const struct net *net,rcu_read_lock (); he =rhashtable_lookup_fast (&priv->ht, &arg, nft_hash_params);if (he !=NULL && - !nft_hash_deactivate_one (net, set, he)) + !nft_hash_flush (net, set, he)) he =NULL ;rcu_read_unlock ();
@@ -398,7 +398,7 @@ static struct nft_set_ops nft_hash_ops __read_mostly = { .insert = nft_hash_insert, .activate = nft_hash_activate, .deactivate = nft_hash_deactivate, - .deactivate_one = nft_hash_deactivate_one, + .flush = nft_hash_flush, .remove = nft_hash_remove, .lookup = nft_hash_lookup, .update = nft_hash_update,
net/netfilter/nft_set_rbtree.c +4 -4
@@ -172,8 +172,8 @@ static void nft_rbtree_activate(const struct net *net,nft_set_elem_change_active (net, set, &rbe->ext); } -static bool nft_rbtree_deactivate_one (const struct net *net, -const struct nft_set *set,void *priv) +static bool nft_rbtree_flush (const struct net *net, +const struct nft_set *set,void *priv) {struct nft_rbtree_elem *rbe = priv;
@@ -214,7 +214,7 @@ static void *nft_rbtree_deactivate(const struct net *net, parent = parent->rb_right;continue ; } -nft_rbtree_deactivate_one (net, set, rbe); +nft_rbtree_flush (net, set, rbe);return rbe; } }
@@ -305,7 +305,7 @@ static struct nft_set_ops nft_rbtree_ops __read_mostly = { .insert = nft_rbtree_insert, .remove = nft_rbtree_remove, .deactivate = nft_rbtree_deactivate, - .deactivate_one = nft_rbtree_deactivate_one, + .flush = nft_rbtree_flush, .activate = nft_rbtree_activate, .lookup = nft_rbtree_lookup, .walk = nft_rbtree_walk,
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 5/21 netfilter: nf_tables: add flush field to struct nft_set_iter
In reply to Pablo Neira AyusoThis provides context to walk callback iterator, thus, we know if the walk happens from the set flush path. This is required by the new bitmap set type coming in a follow up patch which has no real struct nft_set_ext, so it has to allocate it based on the two bit compact element representation.
--- include/net/netfilter/nf_tables.h | 1 + net/netfilter/nf_tables_api.c | 4 ++++ 2 files changed, 5 insertions(+)
include/net/netfilter/nf_tables.h +1 -0
@@ -203,6 +203,7 @@ struct nft_set_elem {struct nft_set ;struct nft_set_iter {u8 genmask; +bool flush;unsigned int count;unsigned int skip;int err;
net/netfilter/nf_tables_api.c +4 -0
@@ -3121,6 +3121,7 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, iter.count =0 ; iter.err =0 ; iter.fn = nf_tables_bind_check_setelem; + iter.flush = false; set->ops->walk (ctx, set, &iter);if (iter.err <0 )
@@ -3374,6 +3375,7 @@ static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) args.iter.count =0 ; args.iter.err =0 ; args.iter.fn = nf_tables_dump_setelem; + args.iter.flush = false; set->ops->walk (&ctx, set, &args.iter);nla_nest_end (skb, nest);
@@ -3939,6 +3941,7 @@ static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,struct nft_set_iter iter = { .genmask = genmask, .fn = nft_flush_set, + .flush = true, }; set->ops->walk (&ctx, set, &iter);
@@ -5089,6 +5092,7 @@ static int nf_tables_check_loops(const struct nft_ctx *ctx, iter.count =0 ; iter.err =0 ; iter.fn = nf_tables_loop_check_setelem; + iter.flush = false; set->ops->walk (ctx, set, &iter);if (iter.err <0 )
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 6/21 netfilter: nf_tables: rename struct nft_set_estimate class field
In reply to Pablo Neira AyusoUse lookup as field name instead, to prepare the introduction of the memory class in a follow up patch.
--- include/net/netfilter/nf_tables.h | 4 ++-- net/netfilter/nf_tables_api.c | 12 ++++++------ net/netfilter/nft_set_hash.c | 2 +- net/netfilter/nft_set_rbtree.c | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-)
include/net/netfilter/nf_tables.h +2 -2
@@ -244,11 +244,11 @@ enum nft_set_class {* characteristics * * @size: required memory -* @class: lookup performance class +* @lookup: lookup performance class */ struct nft_set_estimate {unsigned int size; -enum nft_set_class class; +enum nft_set_class lookup; };struct nft_set_ext ;
net/netfilter/nf_tables_api.c +6 -6
@@ -2401,9 +2401,9 @@ nft_select_set_ops(const struct nlattr * const nla[], features &=NFT_SET_INTERVAL |NFT_SET_MAP |NFT_SET_TIMEOUT ; } - bops =NULL ; - best.size = ~0 ; - best.class = ~0 ; + bops =NULL ; + best.size = ~0 ; + best.lookup = ~0 ;list_for_each_entry (ops, &nf_tables_set_ops, list) {if ((ops->features & features) != features)
@@ -2413,15 +2413,15 @@ nft_select_set_ops(const struct nlattr * const nla[],switch (policy) {case NFT_SET_POL_PERFORMANCE : -if (est.class < best.class) +if (est.lookup < best.lookup)break ; -if (est.class == best.class && est.size < best.size) +if (est.lookup == best.lookup && est.size < best.size)break ;continue ;case NFT_SET_POL_MEMORY :if (est.size < best.size)break ; -if (est.size == best.size && est.class < best.class) +if (est.size == best.size && est.lookup < best.lookup)break ;continue ;default :
net/netfilter/nft_set_hash.c +1 -1
@@ -384,7 +384,7 @@ static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, est->size = esize +2 *sizeof (struct nft_hash_elem *); } - est->class =NFT_SET_CLASS_O_1 ; + est->lookup =NFT_SET_CLASS_O_1 ;return true; }
net/netfilter/nft_set_rbtree.c +1 -1
@@ -291,7 +291,7 @@ static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features,else est->size = nsize; - est->class =NFT_SET_CLASS_O_LOG_N ; + est->lookup =NFT_SET_CLASS_O_LOG_N ;return true; }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 7/21 netfilter: nf_tables: add space notation to sets
In reply to Pablo Neira AyusoThe space notation allows us to classify the set backend implementation based on the amount of required memory. This provides an order of the set representation scalability in terms of memory. The size field is still left in place so use this if the userspace provides no explicit number of elements, so we cannot calculate the real memory that this set needs. This also helps us break ties in the set backend selection routine, eg. two backend implementations provide the same performance.
--- include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 22 +++++++++++++++++----- net/netfilter/nft_set_hash.c | 1 + net/netfilter/nft_set_rbtree.c | 1 + 4 files changed, 21 insertions(+), 5 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
@@ -245,10 +245,12 @@ enum nft_set_class {* * @size: required memory * @lookup: lookup performance class +* @space: memory class */ struct nft_set_estimate {unsigned int size;enum nft_set_class lookup; +enum nft_set_class space; };struct nft_set_ext ;
net/netfilter/nf_tables_api.c +17 -5
@@ -2404,6 +2404,7 @@ nft_select_set_ops(const struct nlattr * const nla[], bops =NULL ; best.size = ~0 ; best.lookup = ~0 ; + best.space = ~0 ;list_for_each_entry (ops, &nf_tables_set_ops, list) {if ((ops->features & features) != features)
@@ -2415,14 +2416,25 @@ nft_select_set_ops(const struct nlattr * const nla[],case NFT_SET_POL_PERFORMANCE :if (est.lookup < best.lookup)break ; -if (est.lookup == best.lookup && est.size < best.size) -break ; +if (est.lookup == best.lookup) { +if (!desc->size) { +if (est.space < best.space) +break ; + }else if (est.size < best.size) { +break ; + } + }continue ;case NFT_SET_POL_MEMORY : -if (est.size < best.size) -break ; -if (est.size == best.size && est.lookup < best.lookup) +if (!desc->size) { +if (est.space < best.space) +break ; +if (est.space == best.space && + est.lookup < best.lookup) +break ; + }else if (est.size < best.size) {break ; + }continue ;default :break ;
net/netfilter/nft_set_hash.c +1 -0
@@ -385,6 +385,7 @@ static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, } est->lookup =NFT_SET_CLASS_O_1 ; + est->space =NFT_SET_CLASS_O_N ;return true; }
net/netfilter/nft_set_rbtree.c +1 -0
@@ -292,6 +292,7 @@ static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features, est->size = nsize; est->lookup =NFT_SET_CLASS_O_LOG_N ; + est->space =NFT_SET_CLASS_O_N ;return true; }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 8/21 netfilter: nf_tables: add bitmap set type
In reply to Pablo Neira AyusoThis patch adds a new bitmap set type. This bitmap uses two bits to represent one element. These two bits determine the element state in the current and the future generation that fits into the nf_tables commit protocol. When dumping elements back to userspace, the two bits are expanded into a struct nft_set_ext object.
If no NFTA_SET_DESC_SIZE is specified, the existing automatic set backend selection prefers bitmap over hash in case of keys whose size is <= 16 bit. If the set size is know, the bitmap set type is selected if with 16 bit kets and more than 390 elements in the set, otherwise the hash table set implementation is used.
For 8 bit keys, the bitmap consumes 66 bytes. For 16 bit keys, the bitmap takes 16388 bytes.
--- net/netfilter/Kconfig | 6 + net/netfilter/Makefile | 1 + net/netfilter/nft_set_bitmap.c | 314 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 321 insertions(+) create mode 100644 net/netfilter/nft_set_bitmap.c
net/netfilter/Kconfig +6 -0
@@ -509,6 +509,12 @@ config NFT_SET_HASH This option adds the "hash" set type that is used to build one-way mappings between matchings and actions. +config NFT_SET_BITMAP + tristate "Netfilter nf_tables bitmap set module" + help + This option adds the "bitmap" set type that is used to build sets + whose keys are smaller or equal to 16 bits. + config NFT_COUNTER tristate "Netfilter nf_tables counter module" help
net/netfilter/Makefile +1 -0
@@ -93,6 +93,7 @@ obj-$(CONFIG_NFT_REJECT) += nft_reject.o obj-$(CONFIG_NFT_REJECT_INET) += nft_reject_inet.o obj-$(CONFIG_NFT_SET_RBTREE) += nft_set_rbtree.o obj-$(CONFIG_NFT_SET_HASH) += nft_set_hash.o +obj-$(CONFIG_NFT_SET_BITMAP) += nft_set_bitmap.o obj-$(CONFIG_NFT_COUNTER) += nft_counter.o obj-$(CONFIG_NFT_LOG) += nft_log.o obj-$(CONFIG_NFT_MASQ) += nft_masq.o
net/netfilter/nft_set_bitmap.c new file +314 -0
314 lines not on this page · raw message
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 9/21 netfilter: nft_ct: add zone id get support
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Just like with counters the direction attribute is optional.
We set priv->dir to MAX unconditionally to avoid duplicating the assignment for all keys with optional direction.
For keys where direction is mandatory, existing code already returns an error.
--- include/uapi/linux/netfilter/nf_tables.h | 2 ++ net/netfilter/nft_ct.c | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -870,6 +870,7 @@ enum nft_rt_attributes {* @NFT_CT_PKTS: conntrack packets * @NFT_CT_BYTES: conntrack bytes * @NFT_CT_AVGPKT: conntrack average bytes per packet +* @NFT_CT_ZONE: conntrack zone */ enum nft_ct_keys {NFT_CT_STATE ,
@@ -889,6 +890,7 @@ enum nft_ct_keys {NFT_CT_PKTS ,NFT_CT_BYTES ,NFT_CT_AVGPKT , + NFT_CT_ZONE, };/**
net/netfilter/nft_ct.c +19 -3
@@ -151,6 +151,18 @@ static void nft_ct_get_eval(const struct nft_expr *expr,case NFT_CT_PROTOCOL : *dest =nf_ct_protonum (ct);return ; +#ifdef CONFIG_NF_CONNTRACK_ZONES +case NFT_CT_ZONE : { +const struct nf_conntrack_zone *zone =nf_ct_zone (ct); + +if (priv->dir <IP_CT_DIR_MAX ) + *dest =nf_ct_zone_id (zone, priv->dir); +else + *dest = zone->id; + +return ; + } +#endif default :break ; }
@@ -266,6 +278,7 @@ static int nft_ct_get_init(const struct nft_ctx *ctx,int err; priv->key =ntohl (nla_get_be32 (tb[NFTA_CT_KEY ])); + priv->dir =IP_CT_DIR_MAX ;switch (priv->key) {case NFT_CT_DIRECTION :if (tb[NFTA_CT_DIRECTION ] !=NULL )
@@ -333,11 +346,13 @@ static int nft_ct_get_init(const struct nft_ctx *ctx,case NFT_CT_BYTES :case NFT_CT_PKTS :case NFT_CT_AVGPKT : -/* no direction? return sum of original + reply */ -if (tb[NFTA_CT_DIRECTION ] ==NULL ) - priv->dir =IP_CT_DIR_MAX ; len =sizeof (u64);break ; +#ifdef CONFIG_NF_CONNTRACK_ZONES +case NFT_CT_ZONE : + len =sizeof (u16); +break ; +#endif default :return -EOPNOTSUPP ; }
@@ -465,6 +480,7 @@ static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr)case NFT_CT_BYTES :case NFT_CT_PKTS :case NFT_CT_AVGPKT : +case NFT_CT_ZONE :if (priv->dir <IP_CT_DIR_MAX &&nla_put_u8 (skb,NFTA_CT_DIRECTION , priv->dir)) goto nla_put_failure;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 10/21 netfilter: nft_ct: prepare for key-dependent error unwind
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Next patch will add ZONE_ID set support which will need similar error unwind (put operation) as conntrack labels.
Prepare for this: remove the 'label_got' boolean in favor of a switch statement that can be extended in next patch.
As we already have that in the set_destroy function place that in a separate function and call it from the set init function.
--- net/netfilter/nft_ct.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-)
net/netfilter/nft_ct.c +15 -14
@@ -386,12 +386,24 @@ static int nft_ct_get_init(const struct nft_ctx *ctx,return 0 ; } +static void __nft_ct_set_destroy (const struct nft_ctx *ctx,struct nft_ct *priv) +{ +switch (priv->key) { +#ifdef CONFIG_NF_CONNTRACK_LABELS +case NFT_CT_LABELS : +nf_connlabels_put (ctx->net); +break ; +#endif +default : +break ; + } +} +static int nft_ct_set_init (const struct nft_ctx *ctx,const struct nft_expr *expr,const struct nlattr *const tb[]) {struct nft_ct *priv =nft_expr_priv (expr); -bool label_got = false;unsigned int len;int err;
@@ -412,7 +424,6 @@ static int nft_ct_set_init(const struct nft_ctx *ctx, err =nf_connlabels_get (ctx->net, (len *BITS_PER_BYTE ) -1 );if (err)return err; - label_got = true;break ;#endif default :
@@ -431,8 +442,7 @@ static int nft_ct_set_init(const struct nft_ctx *ctx,return 0 ; err1: -if (label_got) -nf_connlabels_put (ctx->net); +__nft_ct_set_destroy (ctx, priv);return err; }
@@ -447,16 +457,7 @@ static void nft_ct_set_destroy(const struct nft_ctx *ctx, {struct nft_ct *priv =nft_expr_priv (expr); -switch (priv->key) { -#ifdef CONFIG_NF_CONNTRACK_LABELS -case NFT_CT_LABELS : -nf_connlabels_put (ctx->net); -break ; -#endif -default : -break ; - } - +__nft_ct_set_destroy (ctx, priv);nft_ct_netns_put (ctx->net, ctx->afi->family); }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 11/21 netfilter: nft_ct: add zone id set support
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
zones allow tracking multiple connections sharing identical tuples, this is needed e.g. when tracking distinct vlans with overlapping ip addresses (conntrack is l2 agnostic).
Thus the zone has to be set before the packet is picked up by the connection tracker. This is done by means of 'conntrack templates' which are conntrack structures used solely to pass this info from one netfilter hook to the next.
The iptables CT target instantiates these connection tracking templates once per rule, i.e. the template is fixed/tied to particular zone, can be read-only and therefore be re-used by as many skbs simultaneously as needed.
We can't follow this model because we want to take the zone id from an sreg at rule eval time so we could e.g. fill in the zone id from the packets vlan id or a e.g. nftables key : value maps.
To avoid cost of per packet alloc/free of the template, use a percpu template 'scratch' object and use the refcount to detect the (unlikely) case where the template is still attached to another skb (i.e., previous skb was nfqueued ...).
--- net/netfilter/nft_ct.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 143 insertions(+), 1 deletion(-)
net/netfilter/nft_ct.c +143 -1
@@ -32,6 +32,11 @@ struct nft_ct { }; }; +#ifdef CONFIG_NF_CONNTRACK_ZONES +static DEFINE_PER_CPU(struct nf_conn *, nft_ct_pcpu_template); +static unsigned int nft_ct_pcpu_template_refcnt__read_mostly ; +#endif +static u64 nft_ct_get_eval_counter (const struct nf_conn_counter *c,enum nft_ct_keys k,enum ip_conntrack_dir d)
@@ -191,6 +196,53 @@ static void nft_ct_get_eval(const struct nft_expr *expr, regs->verdict.code =NFT_BREAK ; } +#ifdef CONFIG_NF_CONNTRACK_ZONES +static void nft_ct_set_zone_eval (const struct nft_expr *expr, +struct nft_regs *regs, +const struct nft_pktinfo *pkt) +{ +struct nf_conntrack_zone zone = { .dir =NF_CT_DEFAULT_ZONE_DIR }; +const struct nft_ct *priv =nft_expr_priv (expr); +struct sk_buff *skb = pkt->skb; +enum ip_conntrack_info ctinfo; +u16 value = regs->data[priv->sreg]; +struct nf_conn *ct; + + ct =nf_ct_get (skb, &ctinfo); +if (ct)/* already tracked */ +return ; + + zone.id = value; + +switch (priv->dir) { +case IP_CT_DIR_ORIGINAL : + zone.dir =NF_CT_ZONE_DIR_ORIG ; +break ; +case IP_CT_DIR_REPLY : + zone.dir =NF_CT_ZONE_DIR_REPL ; +break ; +default : +break ; + } + + ct =this_cpu_read (nft_ct_pcpu_template); + +if (likely (atomic_read (&ct->ct_general.use) ==1 )) { +nf_ct_zone_add (ct, &zone); + }else { +/* previous skb got queued to userspace */ + ct =nf_ct_tmpl_alloc (nft_net (pkt), &zone,GFP_ATOMIC ); +if (!ct) { + regs->verdict.code =NF_DROP ; +return ; + } + } + +atomic_inc (&ct->ct_general.use); +nf_ct_set (skb, ct,IP_CT_NEW ); +} +#endif +static void nft_ct_set_eval (const struct nft_expr *expr,struct nft_regs *regs,const struct nft_pktinfo *pkt)
@@ -269,6 +321,45 @@ static void nft_ct_netns_put(struct net *net, uint8_t family)nf_ct_netns_put (net, family); } +#ifdef CONFIG_NF_CONNTRACK_ZONES +static void nft_ct_tmpl_put_pcpu (void ) +{ +struct nf_conn *ct; +int cpu; + +for_each_possible_cpu (cpu) { + ct =per_cpu (nft_ct_pcpu_template, cpu); +if (!ct) +break ; +nf_ct_put (ct); +per_cpu (nft_ct_pcpu_template, cpu) =NULL ; + } +} + +static bool nft_ct_tmpl_alloc_pcpu (void ) +{ +struct nf_conntrack_zone zone = { .id =0 }; +struct nf_conn *tmp; +int cpu; + +if (nft_ct_pcpu_template_refcnt) +return true; + +for_each_possible_cpu (cpu) { + tmp =nf_ct_tmpl_alloc (&init_net, &zone,GFP_KERNEL ); +if (!tmp) { +nft_ct_tmpl_put_pcpu (); +return false; + } + +atomic_set (&tmp->ct_general.use,1 ); +per_cpu (nft_ct_pcpu_template, cpu) = tmp; + } + +return true; +} +#endif +static int nft_ct_get_init (const struct nft_ctx *ctx,const struct nft_expr *expr,const struct nlattr *const tb[])
@@ -394,6 +485,11 @@ static void __nft_ct_set_destroy(const struct nft_ctx *ctx, struct nft_ct *priv)nf_connlabels_put (ctx->net);break ;#endif +#ifdef CONFIG_NF_CONNTRACK_ZONES +case NFT_CT_ZONE : +if (--nft_ct_pcpu_template_refcnt ==0 ) +nft_ct_tmpl_put_pcpu (); +#endif default :break ; }
@@ -407,6 +503,7 @@ static int nft_ct_set_init(const struct nft_ctx *ctx,unsigned int len;int err; + priv->dir =IP_CT_DIR_MAX ; priv->key =ntohl (nla_get_be32 (tb[NFTA_CT_KEY ]));switch (priv->key) {#ifdef CONFIG_NF_CONNTRACK_MARK
@@ -426,10 +523,28 @@ static int nft_ct_set_init(const struct nft_ctx *ctx,return err;break ;#endif +#ifdef CONFIG_NF_CONNTRACK_ZONES +case NFT_CT_ZONE : +if (!nft_ct_tmpl_alloc_pcpu ()) +return -ENOMEM ; + nft_ct_pcpu_template_refcnt++; +break ; +#endif default :return -EOPNOTSUPP ; } +if (tb[NFTA_CT_DIRECTION ]) { + priv->dir =nla_get_u8 (tb[NFTA_CT_DIRECTION ]); +switch (priv->dir) { +case IP_CT_DIR_ORIGINAL : +case IP_CT_DIR_REPLY : +break ; +default : +return -EINVAL ; + } + } + priv->sreg =nft_parse_register (tb[NFTA_CT_SREG ]); err =nft_validate_register_load (priv->sreg, len);if (err <0 )
@@ -504,6 +619,17 @@ static int nft_ct_set_dump(struct sk_buff *skb, const struct nft_expr *expr) goto nla_put_failure;if (nla_put_be32 (skb,NFTA_CT_KEY ,htonl (priv->key))) goto nla_put_failure; + +switch (priv->key) { +case NFT_CT_ZONE : +if (priv->dir <IP_CT_DIR_MAX && +nla_put_u8 (skb,NFTA_CT_DIRECTION , priv->dir)) + goto nla_put_failure; +break ; +default : +break ; + } +return 0 ; nla_put_failure:
@@ -529,6 +655,17 @@ static const struct nft_expr_ops nft_ct_set_ops = { .dump = nft_ct_set_dump, }; +#ifdef CONFIG_NF_CONNTRACK_ZONES +static const struct nft_expr_ops nft_ct_set_zone_ops = { + .type = &nft_ct_type, + .size =NFT_EXPR_SIZE (sizeof (struct nft_ct )), + .eval = nft_ct_set_zone_eval, + .init = nft_ct_set_init, + .destroy = nft_ct_set_destroy, + .dump = nft_ct_set_dump, +}; +#endif +static const struct nft_expr_ops *nft_ct_select_ops (const struct nft_ctx *ctx,const struct nlattr *const tb[])
@@ -542,8 +679,13 @@ nft_ct_select_ops(const struct nft_ctx *ctx,if (tb[NFTA_CT_DREG ])return &nft_ct_get_ops; -if (tb[NFTA_CT_SREG ]) +if (tb[NFTA_CT_SREG ]) { +#ifdef CONFIG_NF_CONNTRACK_ZONES +if (nla_get_be32 (tb[NFTA_CT_KEY ]) ==htonl (NFT_CT_ZONE )) +return &nft_ct_set_zone_ops; +#endif return &nft_ct_set_ops; + }return ERR_PTR (-EINVAL ); }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 12/21 netfilter: nft_exthdr: add TCP option matching
In reply to Pablo Neira AyusoFrom: Manuel Messner <[email protected]>
This patch implements the kernel side of the TCP option patch.
--- include/uapi/linux/netfilter/nf_tables.h | 17 ++++- net/netfilter/Kconfig | 4 +- net/netfilter/nft_exthdr.c | 119 +++++++++++++++++++++++++++---- 3 files changed, 124 insertions(+), 16 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +16 -1
@@ -709,13 +709,27 @@ enum nft_exthdr_flags { };/** -* enum nft_exthdr_attributes - nf_tables IPv6 extension header expression netlink attributes +* enum nft_exthdr_op - nf_tables match options +* +* @NFT_EXTHDR_OP_IPV6: match against ipv6 extension headers +* @NFT_EXTHDR_OP_TCP: match against tcp options +*/ +enum nft_exthdr_op { +NFT_EXTHDR_OP_IPV6 , +NFT_EXTHDR_OP_TCPOPT , + __NFT_EXTHDR_OP_MAX +}; +#define NFT_EXTHDR_OP_MAX (__NFT_EXTHDR_OP_MAX - 1) + +/** +* enum nft_exthdr_attributes - nf_tables extension header expression netlink attributes * * @NFTA_EXTHDR_DREG: destination register (NLA_U32: nft_registers) * @NFTA_EXTHDR_TYPE: extension header type (NLA_U8) * @NFTA_EXTHDR_OFFSET: extension header offset (NLA_U32) * @NFTA_EXTHDR_LEN: extension header length (NLA_U32) * @NFTA_EXTHDR_FLAGS: extension header flags (NLA_U32) +* @NFTA_EXTHDR_OP: option match type (NLA_U8) */ enum nft_exthdr_attributes {NFTA_EXTHDR_UNSPEC ,
@@ -724,6 +738,7 @@ enum nft_exthdr_attributes {NFTA_EXTHDR_OFFSET ,NFTA_EXTHDR_LEN ,NFTA_EXTHDR_FLAGS , +NFTA_EXTHDR_OP , __NFTA_EXTHDR_MAX };#define NFTA_EXTHDR_MAX (__NFTA_EXTHDR_MAX - 1)
net/netfilter/Kconfig +2 -2
@@ -467,10 +467,10 @@ config NF_TABLES_NETDEV This option enables support for the "netdev" table. config NFT_EXTHDR - tristate "Netfilter nf_tables IPv6 exthdr module" + tristate "Netfilter nf_tables exthdr module" help This option adds the "exthdr" expression that you can use to match - IPv6 extension headers. + IPv6 extension headers and tcp options. config NFT_META tristate "Netfilter nf_tables meta module"
net/netfilter/nft_exthdr.c +106 -13
179 lines not on this page · raw message
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 13/21 netfilter: nf_ct_sip: Use mod_timer_pending()
In reply to Pablo Neira AyusoFrom: Gao Feng <[email protected]>
timer_del() followed by timer_add() can be replaced by mod_timer_pending().
--- net/netfilter/nf_conntrack_sip.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
net/netfilter/nf_conntrack_sip.c +5 -7
@@ -809,13 +809,11 @@ static int refresh_signalling_expectation(struct nf_conn *ct, exp->tuple.dst.protonum != proto || exp->tuple.dst.u.udp.port != port) continue; -if (!del_timer (&exp->timeout)) -continue ; - exp->flags &= ~NF_CT_EXPECT_INACTIVE ; - exp->timeout.expires = jiffies + expires *HZ ; -add_timer (&exp->timeout); - found =1 ; -break ; +if (mod_timer_pending (&exp->timeout, jiffies + expires *HZ )) { + exp->flags &= ~NF_CT_EXPECT_INACTIVE ; + found =1 ; +break ; + } }spin_unlock_bh (&nf_conntrack_expect_lock);return found;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 14/21 netfilter: nf_ct_expect: nf_ct_expect_insert() returns void
In reply to Pablo Neira AyusoFrom: Gao Feng <[email protected]>
Because nf_ct_expect_insert() always succeeds now, its return value can be just void instead of int. And remove code that checks for its return value.
--- net/netfilter/nf_conntrack_expect.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_expect.c +3 -5
@@ -353,7 +353,7 @@ void nf_ct_expect_put(struct nf_conntrack_expect *exp) }EXPORT_SYMBOL_GPL (nf_ct_expect_put); -static int nf_ct_expect_insert (struct nf_conntrack_expect *exp) +static void nf_ct_expect_insert (struct nf_conntrack_expect *exp) {struct nf_conn_help *master_help =nfct_help (exp->master);struct nf_conntrack_helper *helper;
@@ -380,7 +380,6 @@ static int nf_ct_expect_insert(struct nf_conntrack_expect *exp)add_timer (&exp->timeout);NF_CT_STAT_INC (net, expect_create); -return 0 ; }/* Race with expectations being used means we could have none to find; OK. */
@@ -464,9 +463,8 @@ int nf_ct_expect_related_report(struct nf_conntrack_expect *expect,if (ret <=0 ) goto out; - ret =nf_ct_expect_insert (expect); -if (ret <0 ) - goto out; +nf_ct_expect_insert (expect); +spin_unlock_bh (&nf_conntrack_expect_lock);nf_ct_expect_event_report (IPEXP_NEW , expect, portid, report);return ret;
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 15/21 netfilter: nfnetlink: get rid of u_intX_t types
In reply to Pablo Neira AyusoUse uX types instead.
--- net/netfilter/nfnetlink.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
net/netfilter/nfnetlink.c +8 -8
@@ -100,9 +100,9 @@ int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n) }EXPORT_SYMBOL_GPL (nfnetlink_subsys_unregister); -static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys (u_int16_t type) +static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys (u16 type) { -u_int8_t subsys_id =NFNL_SUBSYS_ID (type); +u8 subsys_id =NFNL_SUBSYS_ID (type);if (subsys_id >=NFNL_SUBSYS_COUNT )return NULL ;
@@ -111,9 +111,9 @@ static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t t }static inline const struct nfnl_callback * -nfnetlink_find_client (u_int16_t type,const struct nfnetlink_subsystem *ss) +nfnetlink_find_client (u16 type,const struct nfnetlink_subsystem *ss) { -u_int8_t cb_id =NFNL_MSG_TYPE (type); +u8 cb_id =NFNL_MSG_TYPE (type);if (cb_id >= ss->cb_count)return NULL ;
@@ -185,7 +185,7 @@ static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) {int min_len =nlmsg_total_size (sizeof (struct nfgenmsg )); -u_int8_t cb_id =NFNL_MSG_TYPE (nlh->nlmsg_type); +u8 cb_id =NFNL_MSG_TYPE (nlh->nlmsg_type);struct nlattr *cda[ss->cb[cb_id].attr_count +1 ];struct nlattr *attr = (void *)nlh + min_len;int attrlen = nlh->nlmsg_len - min_len;
@@ -273,7 +273,7 @@ enum { };static void nfnetlink_rcv_batch (struct sk_buff *skb,struct nlmsghdr *nlh, -u_int16_t subsys_id) +u16 subsys_id) {struct sk_buff *oskb = skb;struct net *net =sock_net (skb->sk);
@@ -365,7 +365,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, {int min_len =nlmsg_total_size (sizeof (struct nfgenmsg )); -u_int8_t cb_id =NFNL_MSG_TYPE (nlh->nlmsg_type); +u8 cb_id =NFNL_MSG_TYPE (nlh->nlmsg_type);struct nlattr *cda[ss->cb[cb_id].attr_count +1 ];struct nlattr *attr = (void *)nlh + min_len;int attrlen = nlh->nlmsg_len - min_len;
@@ -439,7 +439,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,static void nfnetlink_rcv (struct sk_buff *skb) {struct nlmsghdr *nlh =nlmsg_hdr (skb); -u_int16_t res_id; +u16 res_id;int msglen; if (nlh->nlmsg_len < NLMSG_HDRLEN ||
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 16/21 netfilter: nfnetlink: add nfnetlink_rcv_skb_batch()
In reply to Pablo Neira AyusoAdd new nfnetlink_rcv_skb_batch() to wrap initial nfnetlink batch handling.
--- net/netfilter/nfnetlink.c | 51 ++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 23 deletions(-)
net/netfilter/nfnetlink.c +28 -23
@@ -436,12 +436,35 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,kfree_skb (skb); } -static void nfnetlink_rcv (struct sk_buff *skb) +static void nfnetlink_rcv_skb_batch (struct sk_buff *skb,struct nlmsghdr *nlh) { -struct nlmsghdr *nlh =nlmsg_hdr (skb); +struct nfgenmsg *nfgenmsg;u16 res_id;int msglen; + msglen =NLMSG_ALIGN (nlh->nlmsg_len); +if (msglen > skb->len) + msglen = skb->len; + +if (nlh->nlmsg_len <NLMSG_HDRLEN || + skb->len <NLMSG_HDRLEN +sizeof (struct nfgenmsg )) +return ; + + nfgenmsg =nlmsg_data (nlh); +skb_pull (skb, msglen); +/* Work around old nft using host byte order */ +if (nfgenmsg->res_id ==NFNL_SUBSYS_NFTABLES ) + res_id =NFNL_SUBSYS_NFTABLES ; +else + res_id =ntohs (nfgenmsg->res_id); + +nfnetlink_rcv_batch (skb, nlh, res_id); +} + +static void nfnetlink_rcv (struct sk_buff *skb) +{ +struct nlmsghdr *nlh =nlmsg_hdr (skb); +if (nlh->nlmsg_len <NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)return ;
@@ -451,28 +474,10 @@ static void nfnetlink_rcv(struct sk_buff *skb)return ; } -if (nlh->nlmsg_type ==NFNL_MSG_BATCH_BEGIN ) { -struct nfgenmsg *nfgenmsg; - - msglen =NLMSG_ALIGN (nlh->nlmsg_len); -if (msglen > skb->len) - msglen = skb->len; - -if (nlh->nlmsg_len <NLMSG_HDRLEN || - skb->len <NLMSG_HDRLEN +sizeof (struct nfgenmsg )) -return ; - - nfgenmsg =nlmsg_data (nlh); -skb_pull (skb, msglen); -/* Work around old nft using host byte order */ -if (nfgenmsg->res_id ==NFNL_SUBSYS_NFTABLES ) - res_id =NFNL_SUBSYS_NFTABLES ; -else - res_id =ntohs (nfgenmsg->res_id); -nfnetlink_rcv_batch (skb, nlh, res_id); - }else { +if (nlh->nlmsg_type ==NFNL_MSG_BATCH_BEGIN ) +nfnetlink_rcv_skb_batch (skb, nlh); +else netlink_rcv_skb (skb, &nfnetlink_rcv_msg); - } }#ifdef CONFIG_MODULES
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 17/21 netfilter: nfnetlink: allow to check for generation ID
In reply to Pablo Neira AyusoThis patch allows userspace to specify the generation ID that has been used to build an incremental batch update.
If userspace specifies the generation ID in the batch message as attribute, then nfnetlink compares it to the current generation ID so you make sure that you work against the right baseline. Otherwise, bail out with ERESTART so userspace knows that its changeset is stale and needs to respin. Userspace can do this transparently at the cost of taking slightly more time to refresh caches and rework the changeset.
This check is optional, if there is no NFNL_BATCH_GENID attribute in the batch begin message, then no check is performed.
--- include/linux/netfilter/nfnetlink.h | 1 + include/uapi/linux/netfilter/nfnetlink.h | 12 ++++++++++++ net/netfilter/nfnetlink.c | 31 +++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 4 deletions(-)
include/linux/netfilter/nfnetlink.h +1 -0
@@ -28,6 +28,7 @@ struct nfnetlink_subsystem {const struct nfnl_callback *cb;/* callback for individual types */ int (*commit)(struct net *net,struct sk_buff *skb);int (*abort)(struct net *net,struct sk_buff *skb); +bool (*valid_genid)(struct net *net,u32 genid); };int nfnetlink_subsys_register (const struct nfnetlink_subsystem *n);
include/uapi/linux/netfilter/nfnetlink.h +12 -0
@@ -65,4 +65,16 @@ struct nfgenmsg {#define NFNL_MSG_BATCH_BEGIN NLMSG_MIN_TYPE#define NFNL_MSG_BATCH_END NLMSG_MIN_TYPE+1 +/** +* enum nfnl_batch_attributes - nfnetlink batch netlink attributes +* +* @NFNL_BATCH_GENID: generation ID for this changeset (NLA_U32) +*/ +enum nfnl_batch_attributes { +NFNL_BATCH_UNSPEC , +NFNL_BATCH_GENID , + __NFNL_BATCH_MAX +}; +#define NFNL_BATCH_MAX (__NFNL_BATCH_MAX - 1) +#endif /* _UAPI_NFNETLINK_H */
net/netfilter/nfnetlink.c +27 -4
@@ -3,7 +3,7 @@* * (C) 2001 by Jay Schulist <[email protected]>, * (C) 2002-2005 by Harald Welte <[email protected]> -* (C) 2005,2007 by Pablo Neira Ayuso <[email protected]> +* (C) 2005-2017 by Pablo Neira Ayuso <[email protected]> * * Initial netfilter messages via netlink development funded and * generally made possible by Network Robots, Inc. (www.networkrobots.com)
@@ -273,7 +273,7 @@ enum { };static void nfnetlink_rcv_batch (struct sk_buff *skb,struct nlmsghdr *nlh, -u16 subsys_id) +u16 subsys_id,u32 genid) {struct sk_buff *oskb = skb;struct net *net =sock_net (skb->sk);
@@ -315,6 +315,12 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,return kfree_skb (skb); } +if (genid && ss->valid_genid && !ss->valid_genid (net, genid)) { +nfnl_unlock (subsys_id); +netlink_ack (oskb, nlh, -ERESTART ); +return kfree_skb (skb); + } +while (skb->len >=nlmsg_total_size (0 )) {int msglen, type;
@@ -436,11 +442,20 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,kfree_skb (skb); } +static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX +1 ] = { + [NFNL_BATCH_GENID ] = { .type =NLA_U32 }, +}; +static void nfnetlink_rcv_skb_batch (struct sk_buff *skb,struct nlmsghdr *nlh) { +int min_len =nlmsg_total_size (sizeof (struct nfgenmsg )); +struct nlattr *attr = (void *)nlh + min_len; +struct nlattr *cda[NFNL_BATCH_MAX +1 ]; +int attrlen = nlh->nlmsg_len - min_len;struct nfgenmsg *nfgenmsg; +int msglen, err; +u32 gen_id =0 ;u16 res_id; -int msglen; msglen =NLMSG_ALIGN (nlh->nlmsg_len);if (msglen > skb->len)
@@ -450,6 +465,14 @@ static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh) skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))return ; + err =nla_parse (cda,NFNL_BATCH_MAX , attr, attrlen, nfnl_batch_policy); +if (err <0 ) { +netlink_ack (skb, nlh, err); +return ; + } +if (cda[NFNL_BATCH_GENID ]) + gen_id =ntohl (nla_get_be32 (cda[NFNL_BATCH_GENID ])); + nfgenmsg =nlmsg_data (nlh);skb_pull (skb, msglen);/* Work around old nft using host byte order */
@@ -458,7 +481,7 @@ static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)else res_id =ntohs (nfgenmsg->res_id); -nfnetlink_rcv_batch (skb, nlh, res_id); +nfnetlink_rcv_batch (skb, nlh, res_id, gen_id); }static void nfnetlink_rcv (struct sk_buff *skb)
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 18/21 netfilter: nf_tables: add check_genid to the nfnetlink subsystem
In reply to Pablo Neira AyusoThis patch implements the check generation id as provided by nfnetlink. This allows us to reject ruleset updates against stale baseline, so userspace can retry update with a fresh ruleset cache.
--- net/netfilter/nf_tables_api.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/netfilter/nf_tables_api.c +6 -0
@@ -4972,6 +4972,11 @@ static int nf_tables_abort(struct net *net, struct sk_buff *skb)return 0 ; } +static bool nf_tables_valid_genid (struct net *net,u32 genid) +{ +return net->nft.base_seq == genid; +} +static const struct nfnetlink_subsystem nf_tables_subsys = { .name ="nf_tables" , .subsys_id =NFNL_SUBSYS_NFTABLES ,
@@ -4979,6 +4984,7 @@ static const struct nfnetlink_subsystem nf_tables_subsys = { .cb = nf_tables_cb, .commit = nf_tables_commit, .abort = nf_tables_abort, + .valid_genid = nf_tables_valid_genid, }; int nft_chain_validate_dependency(const struct nft_chain *chain,
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 19/21 netfilter: nf_tables: add NFTA_RULE_ID attribute
In reply to Pablo Neira AyusoThis new attribute allows us to uniquely identify a rule in transaction. Robots may trigger an insertion followed by deletion in a batch, in that scenario we still don't have a public rule handle that we can use to delete the rule. This is similar to the NFTA_SET_ID attribute that allows us to refer to an anonymous set from a batch.
--- include/net/netfilter/nf_tables.h | 3 +++ include/uapi/linux/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+)
include/net/netfilter/nf_tables.h +3 -0
@@ -1202,10 +1202,13 @@ struct nft_trans {struct nft_trans_rule {struct nft_rule *rule; +u32 rule_id; };#define nft_trans_rule (trans) \ (((struct nft_trans_rule *)trans->data)->rule) +#define nft_trans_rule_id (trans) \ + (((struct nft_trans_rule *)trans->data)->rule_id)struct nft_trans_set {struct nft_set *set;
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -207,6 +207,7 @@ enum nft_chain_attributes {* @NFTA_RULE_COMPAT: compatibility specifications of the rule (NLA_NESTED: nft_rule_compat_attributes) * @NFTA_RULE_POSITION: numeric handle of the previous rule (NLA_U64) * @NFTA_RULE_USERDATA: user data (NLA_BINARY, NFT_USERDATA_MAXLEN) +* @NFTA_RULE_ID: uniquely identifies a rule in a transaction (NLA_U32) */ enum nft_rule_attributes {NFTA_RULE_UNSPEC ,
@@ -218,6 +219,7 @@ enum nft_rule_attributes {NFTA_RULE_POSITION ,NFTA_RULE_USERDATA ,NFTA_RULE_PAD , +NFTA_RULE_ID , __NFTA_RULE_MAX };#define NFTA_RULE_MAX (__NFTA_RULE_MAX - 1)
net/netfilter/nf_tables_api.c +26 -0
@@ -240,6 +240,10 @@ static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,if (trans ==NULL )return NULL ; +if (msg_type ==NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID ] !=NULL ) { +nft_trans_rule_id (trans) = +ntohl (nla_get_be32 (ctx->nla[NFTA_RULE_ID ])); + }nft_trans_rule (trans) = rule;list_add_tail (&trans->list, &ctx->net->nft.commit_list);
@@ -2293,6 +2297,22 @@ static int nf_tables_newrule(struct net *net, struct sock *nlsk,return err; } +static struct nft_rule *nft_rule_lookup_byid (const struct net *net, +const struct nlattr *nla) +{ +u32 id =ntohl (nla_get_be32 (nla)); +struct nft_trans *trans; + +list_for_each_entry (trans, &net->nft.commit_list, list) { +struct nft_rule *rule =nft_trans_rule (trans); + +if (trans->msg_type ==NFT_MSG_NEWRULE && + id ==nft_trans_rule_id (trans)) +return rule; + } +return ERR_PTR (-ENOENT ); +} +static int nf_tables_delrule (struct net *net,struct sock *nlsk,struct sk_buff *skb,const struct nlmsghdr *nlh,const struct nlattr *const nla[])
@@ -2331,6 +2351,12 @@ static int nf_tables_delrule(struct net *net, struct sock *nlsk,return PTR_ERR (rule); err =nft_delrule (&ctx, rule); + }else if (nla[NFTA_RULE_ID ]) { + rule =nft_rule_lookup_byid (net, nla[NFTA_RULE_ID ]); +if (IS_ERR (rule)) +return PTR_ERR (rule); + + err =nft_delrule (&ctx, rule); }else { err =nft_delrule_by_chain (&ctx); }
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 20/21 netfilter: update MAINTAINERS
In reply to Pablo Neira AyusoIt's been a while since Patrick has been suspended as coreteam member [1]. Update this file to remove him.
While at this, remove references to all foo-tables variants, given the project hosts more than just that, eg. ipset, conntrack, ...
[1] https://marc.info/?l=netfilter-devel&m=146887464512702
--- MAINTAINERS | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
MAINTAINERS +1 -2
@@ -8579,9 +8579,8 @@ F: Documentation/networking/s2io.txt F: Documentation/networking/vxge.txt F: drivers/net/ethernet/neterion/ -NETFILTER ({IP,IP6,ARP,EB,NF}TABLES) +NETFILTER M: Pablo Neira Ayuso <[email protected]> -M: Patrick McHardy <[email protected]> M: Jozsef Kadlecsik <[email protected]> L: [email protected] L: [email protected]
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 21/21 netfilter: nf_tables: honor NFT_SET_OBJECT in set backend selection
In reply to Pablo Neira AyusoCheck for NFT_SET_OBJECT feature flag, otherwise we may end up selecting the wrong set backend.
--- net/netfilter/nf_tables_api.c | 3 ++- net/netfilter/nft_set_hash.c | 2 +- net/netfilter/nft_set_rbtree.c | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-)
net/netfilter/nf_tables_api.c +2 -1
@@ -2424,7 +2424,8 @@ nft_select_set_ops(const struct nlattr * const nla[], features =0 ;if (nla[NFTA_SET_FLAGS ] !=NULL ) { features =ntohl (nla_get_be32 (nla[NFTA_SET_FLAGS ])); - features &=NFT_SET_INTERVAL |NFT_SET_MAP |NFT_SET_TIMEOUT ; + features &=NFT_SET_INTERVAL |NFT_SET_MAP |NFT_SET_TIMEOUT | +NFT_SET_OBJECT ; } bops =NULL ;
net/netfilter/nft_set_hash.c +1 -1
@@ -404,7 +404,7 @@ static struct nft_set_ops nft_hash_ops __read_mostly = { .lookup = nft_hash_lookup, .update = nft_hash_update, .walk = nft_hash_walk, - .features = NFT_SET_MAP | NFT_SET_TIMEOUT, + .features = NFT_SET_MAP | NFT_SET_OBJECT | NFT_SET_TIMEOUT, .owner = THIS_MODULE, };
net/netfilter/nft_set_rbtree.c +1 -1
@@ -310,7 +310,7 @@ static struct nft_set_ops nft_rbtree_ops __read_mostly = { .activate = nft_rbtree_activate, .lookup = nft_rbtree_lookup, .walk = nft_rbtree_walk, - .features = NFT_SET_INTERVAL | NFT_SET_MAP, + .features = NFT_SET_INTERVAL | NFT_SET_MAP | NFT_SET_OBJECT, .owner = THIS_MODULE, };
Signature · 1 line
2.1.4
David Miller
maintainerFrom: Pablo Neira Ayuso <[email protected]>
Date: Sun, 12 Feb 2017 20:42:32 +0100
Quoted 2 lines
The following patchset contains Netfilter updates for your net-next tree, most relevantly they are:
..
Quoted 3 lines
You can pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Pulled, I really like the RULE_ID generation count stuff for userspace.
Thanks.