1 From: Harsha Sharma <harshasharmaiitr@gmail.com>
2 Date: Wed, 27 Dec 2017 00:59:00 +0530
3 Subject: [PATCH] netfilter: nf_tables: allocate handle and delete objects via
6 This patch allows deletion of objects via unique handle which can be
7 listed via '-a' option.
9 Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
10 Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
13 --- a/include/net/netfilter/nf_tables.h
14 +++ b/include/net/netfilter/nf_tables.h
15 @@ -372,6 +372,7 @@ void nft_unregister_set(struct nft_set_t
16 * @list: table set list node
17 * @bindings: list of set bindings
18 * @name: name of the set
19 + * @handle: unique handle of the set
20 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
21 * @dtype: data type (verdict or numeric type defined by userspace)
22 * @objtype: object type (see NFT_OBJECT_* definitions)
23 @@ -394,6 +395,7 @@ struct nft_set {
24 struct list_head list;
25 struct list_head bindings;
31 @@ -944,6 +946,7 @@ unsigned int nft_do_chain(struct nft_pkt
32 * @objects: stateful objects in the table
33 * @flowtables: flow tables in the table
34 * @hgenerator: handle generator state
35 + * @handle: table handle
36 * @use: number of chain references to this table
37 * @flags: table flag (see enum nft_table_flags)
38 * @genmask: generation mask
39 @@ -957,6 +960,7 @@ struct nft_table {
40 struct list_head objects;
41 struct list_head flowtables;
47 @@ -981,9 +985,9 @@ int nft_verdict_dump(struct sk_buff *skb
48 * @name: name of this stateful object
49 * @genmask: generation mask
50 * @use: number of references to this stateful object
51 - * @data: object data, layout depends on type
52 + * @handle: unique object handle
53 * @ops: object operations
54 - * @data: pointer to object data
55 + * @data: object data, layout depends on type
58 struct list_head list;
59 @@ -991,6 +995,7 @@ struct nft_object {
60 struct nft_table *table;
64 /* runtime data below here */
65 const struct nft_object_ops *ops ____cacheline_aligned;
67 @@ -1072,6 +1077,7 @@ void nft_unregister_obj(struct nft_objec
68 * @ops_len: number of hooks in array
69 * @genmask: generation mask
70 * @use: number of references to this flow table
71 + * @handle: unique object handle
72 * @data: rhashtable and garbage collector
73 * @ops: array of hooks
75 @@ -1084,6 +1090,7 @@ struct nft_flowtable {
80 /* runtime data below here */
81 struct nf_hook_ops *ops ____cacheline_aligned;
82 struct nf_flowtable data;
83 --- a/include/uapi/linux/netfilter/nf_tables.h
84 +++ b/include/uapi/linux/netfilter/nf_tables.h
85 @@ -174,6 +174,8 @@ enum nft_table_attributes {
93 #define NFTA_TABLE_MAX (__NFTA_TABLE_MAX - 1)
94 @@ -317,6 +319,7 @@ enum nft_set_desc_attributes {
95 * @NFTA_SET_GC_INTERVAL: garbage collection interval (NLA_U32)
96 * @NFTA_SET_USERDATA: user data (NLA_BINARY)
97 * @NFTA_SET_OBJ_TYPE: stateful object type (NLA_U32: NFT_OBJECT_*)
98 + * @NFTA_SET_HANDLE: set handle (NLA_U64)
100 enum nft_set_attributes {
102 @@ -335,6 +338,7 @@ enum nft_set_attributes {
109 #define NFTA_SET_MAX (__NFTA_SET_MAX - 1)
110 @@ -1314,6 +1318,7 @@ enum nft_ct_helper_attributes {
111 * @NFTA_OBJ_TYPE: stateful object type (NLA_U32)
112 * @NFTA_OBJ_DATA: stateful object data (NLA_NESTED)
113 * @NFTA_OBJ_USE: number of references to this expression (NLA_U32)
114 + * @NFTA_OBJ_HANDLE: object handle (NLA_U64)
116 enum nft_object_attributes {
118 @@ -1322,6 +1327,8 @@ enum nft_object_attributes {
126 #define NFTA_OBJ_MAX (__NFTA_OBJ_MAX - 1)
127 @@ -1333,6 +1340,7 @@ enum nft_object_attributes {
128 * @NFTA_FLOWTABLE_NAME: name of this flow table (NLA_STRING)
129 * @NFTA_FLOWTABLE_HOOK: netfilter hook configuration(NLA_U32)
130 * @NFTA_FLOWTABLE_USE: number of references to this flow table (NLA_U32)
131 + * @NFTA_FLOWTABLE_HANDLE: object handle (NLA_U64)
133 enum nft_flowtable_attributes {
134 NFTA_FLOWTABLE_UNSPEC,
135 @@ -1340,6 +1348,8 @@ enum nft_flowtable_attributes {
139 + NFTA_FLOWTABLE_HANDLE,
140 + NFTA_FLOWTABLE_PAD,
143 #define NFTA_FLOWTABLE_MAX (__NFTA_FLOWTABLE_MAX - 1)
144 --- a/net/netfilter/nf_tables_api.c
145 +++ b/net/netfilter/nf_tables_api.c
147 static LIST_HEAD(nf_tables_expressions);
148 static LIST_HEAD(nf_tables_objects);
149 static LIST_HEAD(nf_tables_flowtables);
150 +static u64 table_handle;
152 static void nft_ctx_init(struct nft_ctx *ctx,
154 @@ -376,6 +377,20 @@ static struct nft_table *nft_table_looku
158 +static struct nft_table *nft_table_lookup_byhandle(const struct net *net,
159 + const struct nlattr *nla,
162 + struct nft_table *table;
164 + list_for_each_entry(table, &net->nft.tables, list) {
165 + if (be64_to_cpu(nla_get_be64(nla)) == table->handle &&
166 + nft_active_genmask(table, genmask))
172 static struct nft_table *nf_tables_table_lookup(const struct net *net,
173 const struct nlattr *nla,
174 u8 family, u8 genmask)
175 @@ -392,6 +407,22 @@ static struct nft_table *nf_tables_table
176 return ERR_PTR(-ENOENT);
179 +static struct nft_table *nf_tables_table_lookup_byhandle(const struct net *net,
180 + const struct nlattr *nla,
183 + struct nft_table *table;
186 + return ERR_PTR(-EINVAL);
188 + table = nft_table_lookup_byhandle(net, nla, genmask);
192 + return ERR_PTR(-ENOENT);
195 static inline u64 nf_tables_alloc_handle(struct nft_table *table)
197 return ++table->hgenerator;
198 @@ -438,6 +469,7 @@ static const struct nla_policy nft_table
199 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
200 .len = NFT_TABLE_MAXNAMELEN - 1 },
201 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
202 + [NFTA_TABLE_HANDLE] = { .type = NLA_U64 },
205 static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
206 @@ -459,7 +491,9 @@ static int nf_tables_fill_table_info(str
208 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
209 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
210 - nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
211 + nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
212 + nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
214 goto nla_put_failure;
217 @@ -718,6 +752,7 @@ static int nf_tables_newtable(struct net
218 INIT_LIST_HEAD(&table->flowtables);
219 table->family = family;
220 table->flags = flags;
221 + table->handle = ++table_handle;
223 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
224 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
225 @@ -835,11 +870,18 @@ static int nf_tables_deltable(struct net
228 nft_ctx_init(&ctx, net, skb, nlh, 0, NULL, NULL, nla);
229 - if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL)
230 + if (family == AF_UNSPEC ||
231 + (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE]))
232 return nft_flush(&ctx, family);
234 - table = nf_tables_table_lookup(net, nla[NFTA_TABLE_NAME], family,
236 + if (nla[NFTA_TABLE_HANDLE])
237 + table = nf_tables_table_lookup_byhandle(net,
238 + nla[NFTA_TABLE_HANDLE],
241 + table = nf_tables_table_lookup(net, nla[NFTA_TABLE_NAME],
245 return PTR_ERR(table);
247 @@ -1596,6 +1638,7 @@ static int nf_tables_delchain(struct net
248 struct nft_rule *rule;
249 int family = nfmsg->nfgen_family;
255 @@ -1604,7 +1647,12 @@ static int nf_tables_delchain(struct net
257 return PTR_ERR(table);
259 - chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask);
260 + if (nla[NFTA_CHAIN_HANDLE]) {
261 + handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
262 + chain = nf_tables_chain_lookup_byhandle(table, handle, genmask);
264 + chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask);
267 return PTR_ERR(chain);
269 @@ -2583,6 +2631,7 @@ static const struct nla_policy nft_set_p
270 [NFTA_SET_USERDATA] = { .type = NLA_BINARY,
271 .len = NFT_USERDATA_MAXLEN },
272 [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 },
273 + [NFTA_SET_HANDLE] = { .type = NLA_U64 },
276 static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
277 @@ -2626,6 +2675,22 @@ static struct nft_set *nf_tables_set_loo
278 return ERR_PTR(-ENOENT);
281 +static struct nft_set *nf_tables_set_lookup_byhandle(const struct nft_table *table,
282 + const struct nlattr *nla, u8 genmask)
284 + struct nft_set *set;
287 + return ERR_PTR(-EINVAL);
289 + list_for_each_entry(set, &table->sets, list) {
290 + if (be64_to_cpu(nla_get_be64(nla)) == set->handle &&
291 + nft_active_genmask(set, genmask))
294 + return ERR_PTR(-ENOENT);
297 static struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
298 const struct nlattr *nla,
300 @@ -2742,6 +2807,9 @@ static int nf_tables_fill_set(struct sk_
301 goto nla_put_failure;
302 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
303 goto nla_put_failure;
304 + if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle),
306 + goto nla_put_failure;
308 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
309 goto nla_put_failure;
310 @@ -3154,6 +3222,7 @@ static int nf_tables_newset(struct net *
312 set->timeout = timeout;
313 set->gc_int = gc_int;
314 + set->handle = nf_tables_alloc_handle(table);
316 err = ops->init(set, &desc, nla);
318 @@ -3213,7 +3282,10 @@ static int nf_tables_delset(struct net *
322 - set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask);
323 + if (nla[NFTA_SET_HANDLE])
324 + set = nf_tables_set_lookup_byhandle(ctx.table, nla[NFTA_SET_HANDLE], genmask);
326 + set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask);
330 @@ -4282,6 +4354,21 @@ struct nft_object *nf_tables_obj_lookup(
332 EXPORT_SYMBOL_GPL(nf_tables_obj_lookup);
334 +struct nft_object *nf_tables_obj_lookup_byhandle(const struct nft_table *table,
335 + const struct nlattr *nla,
336 + u32 objtype, u8 genmask)
338 + struct nft_object *obj;
340 + list_for_each_entry(obj, &table->objects, list) {
341 + if (be64_to_cpu(nla_get_be64(nla)) == obj->handle &&
342 + objtype == obj->ops->type->type &&
343 + nft_active_genmask(obj, genmask))
346 + return ERR_PTR(-ENOENT);
349 static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = {
350 [NFTA_OBJ_TABLE] = { .type = NLA_STRING,
351 .len = NFT_TABLE_MAXNAMELEN - 1 },
352 @@ -4289,6 +4376,7 @@ static const struct nla_policy nft_obj_p
353 .len = NFT_OBJ_MAXNAMELEN - 1 },
354 [NFTA_OBJ_TYPE] = { .type = NLA_U32 },
355 [NFTA_OBJ_DATA] = { .type = NLA_NESTED },
356 + [NFTA_OBJ_HANDLE] = { .type = NLA_U64},
359 static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
360 @@ -4436,6 +4524,8 @@ static int nf_tables_newobj(struct net *
364 + obj->handle = nf_tables_alloc_handle(table);
366 obj->name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL);
369 @@ -4482,7 +4572,9 @@ static int nf_tables_fill_obj_info(struc
370 nla_put_string(skb, NFTA_OBJ_NAME, obj->name) ||
371 nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) ||
372 nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) ||
373 - nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset))
374 + nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) ||
375 + nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle),
377 goto nla_put_failure;
380 @@ -4680,7 +4772,7 @@ static int nf_tables_delobj(struct net *
383 if (!nla[NFTA_OBJ_TYPE] ||
384 - !nla[NFTA_OBJ_NAME])
385 + (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE]))
388 table = nf_tables_table_lookup(net, nla[NFTA_OBJ_TABLE], family,
389 @@ -4689,7 +4781,12 @@ static int nf_tables_delobj(struct net *
390 return PTR_ERR(table);
392 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
393 - obj = nf_tables_obj_lookup(table, nla[NFTA_OBJ_NAME], objtype, genmask);
394 + if (nla[NFTA_OBJ_HANDLE])
395 + obj = nf_tables_obj_lookup_byhandle(table, nla[NFTA_OBJ_HANDLE],
398 + obj = nf_tables_obj_lookup(table, nla[NFTA_OBJ_NAME],
403 @@ -4761,6 +4858,7 @@ static const struct nla_policy nft_flowt
404 [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING,
405 .len = NFT_NAME_MAXLEN - 1 },
406 [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED },
407 + [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 },
410 struct nft_flowtable *nf_tables_flowtable_lookup(const struct nft_table *table,
411 @@ -4778,6 +4876,20 @@ struct nft_flowtable *nf_tables_flowtabl
413 EXPORT_SYMBOL_GPL(nf_tables_flowtable_lookup);
415 +struct nft_flowtable *
416 +nf_tables_flowtable_lookup_byhandle(const struct nft_table *table,
417 + const struct nlattr *nla, u8 genmask)
419 + struct nft_flowtable *flowtable;
421 + list_for_each_entry(flowtable, &table->flowtables, list) {
422 + if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle &&
423 + nft_active_genmask(flowtable, genmask))
426 + return ERR_PTR(-ENOENT);
429 #define NFT_FLOWTABLE_DEVICE_MAX 8
431 static int nf_tables_parse_devices(const struct nft_ctx *ctx,
432 @@ -4986,6 +5098,8 @@ static int nf_tables_newflowtable(struct
435 flowtable->table = table;
436 + flowtable->handle = nf_tables_alloc_handle(table);
438 flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL);
439 if (!flowtable->name) {
441 @@ -5060,8 +5174,14 @@ static int nf_tables_delflowtable(struct
443 return PTR_ERR(table);
445 - flowtable = nf_tables_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
447 + if (nla[NFTA_FLOWTABLE_HANDLE])
448 + flowtable = nf_tables_flowtable_lookup_byhandle(table,
449 + nla[NFTA_FLOWTABLE_HANDLE],
452 + flowtable = nf_tables_flowtable_lookup(table,
453 + nla[NFTA_FLOWTABLE_NAME],
455 if (IS_ERR(flowtable))
456 return PTR_ERR(flowtable);
457 if (flowtable->use > 0)
458 @@ -5094,7 +5214,9 @@ static int nf_tables_fill_flowtable_info
460 if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) ||
461 nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) ||
462 - nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)))
463 + nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) ||
464 + nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle),
465 + NFTA_FLOWTABLE_PAD))
466 goto nla_put_failure;
468 nest = nla_nest_start(skb, NFTA_FLOWTABLE_HOOK);