]>
Commit | Line | Data |
---|---|---|
96518518 | 1 | /* |
20a69341 | 2 | * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net> |
96518518 PM |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | |
9 | */ | |
10 | ||
11 | #include <linux/module.h> | |
12 | #include <linux/init.h> | |
13 | #include <linux/list.h> | |
14 | #include <linux/skbuff.h> | |
15 | #include <linux/netlink.h> | |
16 | #include <linux/netfilter.h> | |
17 | #include <linux/netfilter/nfnetlink.h> | |
18 | #include <linux/netfilter/nf_tables.h> | |
19 | #include <net/netfilter/nf_tables_core.h> | |
20 | #include <net/netfilter/nf_tables.h> | |
99633ab2 | 21 | #include <net/net_namespace.h> |
96518518 PM |
22 | #include <net/sock.h> |
23 | ||
96518518 | 24 | static LIST_HEAD(nf_tables_expressions); |
e5009240 | 25 | static LIST_HEAD(nf_tables_objects); |
96518518 PM |
26 | |
27 | /** | |
28 | * nft_register_afinfo - register nf_tables address family info | |
29 | * | |
30 | * @afi: address family info to register | |
31 | * | |
32 | * Register the address family for use with nf_tables. Returns zero on | |
33 | * success or a negative errno code otherwise. | |
34 | */ | |
99633ab2 | 35 | int nft_register_afinfo(struct net *net, struct nft_af_info *afi) |
96518518 PM |
36 | { |
37 | INIT_LIST_HEAD(&afi->tables); | |
38 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 39 | list_add_tail_rcu(&afi->list, &net->nft.af_info); |
96518518 PM |
40 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
41 | return 0; | |
42 | } | |
43 | EXPORT_SYMBOL_GPL(nft_register_afinfo); | |
44 | ||
df05ef87 PNA |
45 | static void __nft_release_afinfo(struct net *net, struct nft_af_info *afi); |
46 | ||
96518518 PM |
47 | /** |
48 | * nft_unregister_afinfo - unregister nf_tables address family info | |
49 | * | |
50 | * @afi: address family info to unregister | |
51 | * | |
52 | * Unregister the address family for use with nf_tables. | |
53 | */ | |
df05ef87 | 54 | void nft_unregister_afinfo(struct net *net, struct nft_af_info *afi) |
96518518 PM |
55 | { |
56 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
df05ef87 | 57 | __nft_release_afinfo(net, afi); |
e688a7f8 | 58 | list_del_rcu(&afi->list); |
96518518 PM |
59 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
60 | } | |
61 | EXPORT_SYMBOL_GPL(nft_unregister_afinfo); | |
62 | ||
99633ab2 | 63 | static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family) |
96518518 PM |
64 | { |
65 | struct nft_af_info *afi; | |
66 | ||
99633ab2 | 67 | list_for_each_entry(afi, &net->nft.af_info, list) { |
96518518 PM |
68 | if (afi->family == family) |
69 | return afi; | |
70 | } | |
71 | return NULL; | |
72 | } | |
73 | ||
99633ab2 PNA |
74 | static struct nft_af_info * |
75 | nf_tables_afinfo_lookup(struct net *net, int family, bool autoload) | |
96518518 PM |
76 | { |
77 | struct nft_af_info *afi; | |
78 | ||
99633ab2 | 79 | afi = nft_afinfo_lookup(net, family); |
96518518 PM |
80 | if (afi != NULL) |
81 | return afi; | |
82 | #ifdef CONFIG_MODULES | |
83 | if (autoload) { | |
84 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
85 | request_module("nft-afinfo-%u", family); | |
86 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
99633ab2 | 87 | afi = nft_afinfo_lookup(net, family); |
96518518 PM |
88 | if (afi != NULL) |
89 | return ERR_PTR(-EAGAIN); | |
90 | } | |
91 | #endif | |
92 | return ERR_PTR(-EAFNOSUPPORT); | |
93 | } | |
94 | ||
7c95f6d8 | 95 | static void nft_ctx_init(struct nft_ctx *ctx, |
633c9a84 | 96 | struct net *net, |
7c95f6d8 PNA |
97 | const struct sk_buff *skb, |
98 | const struct nlmsghdr *nlh, | |
99 | struct nft_af_info *afi, | |
100 | struct nft_table *table, | |
101 | struct nft_chain *chain, | |
102 | const struct nlattr * const *nla) | |
103 | { | |
633c9a84 | 104 | ctx->net = net; |
128ad332 PNA |
105 | ctx->afi = afi; |
106 | ctx->table = table; | |
107 | ctx->chain = chain; | |
108 | ctx->nla = nla; | |
109 | ctx->portid = NETLINK_CB(skb).portid; | |
110 | ctx->report = nlmsg_report(nlh); | |
111 | ctx->seq = nlh->nlmsg_seq; | |
7c95f6d8 PNA |
112 | } |
113 | ||
8411b644 PNA |
114 | static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx, |
115 | int msg_type, u32 size, gfp_t gfp) | |
1081d11b PNA |
116 | { |
117 | struct nft_trans *trans; | |
118 | ||
8411b644 | 119 | trans = kzalloc(sizeof(struct nft_trans) + size, gfp); |
1081d11b PNA |
120 | if (trans == NULL) |
121 | return NULL; | |
122 | ||
b380e5c7 | 123 | trans->msg_type = msg_type; |
1081d11b PNA |
124 | trans->ctx = *ctx; |
125 | ||
126 | return trans; | |
127 | } | |
128 | ||
8411b644 PNA |
129 | static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx, |
130 | int msg_type, u32 size) | |
131 | { | |
132 | return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL); | |
133 | } | |
134 | ||
1081d11b PNA |
135 | static void nft_trans_destroy(struct nft_trans *trans) |
136 | { | |
137 | list_del(&trans->list); | |
138 | kfree(trans); | |
139 | } | |
140 | ||
82bec71d PNA |
141 | static int nf_tables_register_hooks(struct net *net, |
142 | const struct nft_table *table, | |
d8ee8f7c PNA |
143 | struct nft_chain *chain, |
144 | unsigned int hook_nops) | |
145 | { | |
146 | if (table->flags & NFT_TABLE_F_DORMANT || | |
147 | !(chain->flags & NFT_BASE_CHAIN)) | |
148 | return 0; | |
149 | ||
82bec71d PNA |
150 | return nf_register_net_hooks(net, nft_base_chain(chain)->ops, |
151 | hook_nops); | |
d8ee8f7c PNA |
152 | } |
153 | ||
82bec71d PNA |
154 | static void nf_tables_unregister_hooks(struct net *net, |
155 | const struct nft_table *table, | |
d8ee8f7c | 156 | struct nft_chain *chain, |
c5598794 AB |
157 | unsigned int hook_nops) |
158 | { | |
d8ee8f7c PNA |
159 | if (table->flags & NFT_TABLE_F_DORMANT || |
160 | !(chain->flags & NFT_BASE_CHAIN)) | |
161 | return; | |
162 | ||
82bec71d | 163 | nf_unregister_net_hooks(net, nft_base_chain(chain)->ops, hook_nops); |
c5598794 AB |
164 | } |
165 | ||
ee01d542 AB |
166 | static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type) |
167 | { | |
168 | struct nft_trans *trans; | |
169 | ||
170 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table)); | |
171 | if (trans == NULL) | |
172 | return -ENOMEM; | |
173 | ||
174 | if (msg_type == NFT_MSG_NEWTABLE) | |
f2a6d766 | 175 | nft_activate_next(ctx->net, ctx->table); |
ee01d542 AB |
176 | |
177 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
178 | return 0; | |
179 | } | |
180 | ||
181 | static int nft_deltable(struct nft_ctx *ctx) | |
182 | { | |
183 | int err; | |
184 | ||
185 | err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE); | |
186 | if (err < 0) | |
187 | return err; | |
188 | ||
f2a6d766 | 189 | nft_deactivate_next(ctx->net, ctx->table); |
ee01d542 AB |
190 | return err; |
191 | } | |
192 | ||
193 | static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type) | |
194 | { | |
195 | struct nft_trans *trans; | |
196 | ||
197 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain)); | |
198 | if (trans == NULL) | |
199 | return -ENOMEM; | |
200 | ||
201 | if (msg_type == NFT_MSG_NEWCHAIN) | |
664b0f8c | 202 | nft_activate_next(ctx->net, ctx->chain); |
ee01d542 AB |
203 | |
204 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
205 | return 0; | |
206 | } | |
207 | ||
208 | static int nft_delchain(struct nft_ctx *ctx) | |
209 | { | |
210 | int err; | |
211 | ||
212 | err = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN); | |
213 | if (err < 0) | |
214 | return err; | |
215 | ||
216 | ctx->table->use--; | |
664b0f8c | 217 | nft_deactivate_next(ctx->net, ctx->chain); |
ee01d542 AB |
218 | |
219 | return err; | |
220 | } | |
221 | ||
ee01d542 AB |
222 | static int |
223 | nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule) | |
224 | { | |
225 | /* You cannot delete the same rule twice */ | |
889f7ee7 PNA |
226 | if (nft_is_active_next(ctx->net, rule)) { |
227 | nft_deactivate_next(ctx->net, rule); | |
ee01d542 AB |
228 | ctx->chain->use--; |
229 | return 0; | |
230 | } | |
231 | return -ENOENT; | |
232 | } | |
233 | ||
234 | static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type, | |
235 | struct nft_rule *rule) | |
236 | { | |
237 | struct nft_trans *trans; | |
238 | ||
239 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule)); | |
240 | if (trans == NULL) | |
241 | return NULL; | |
242 | ||
1a94e38d PNA |
243 | if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) { |
244 | nft_trans_rule_id(trans) = | |
245 | ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID])); | |
246 | } | |
ee01d542 AB |
247 | nft_trans_rule(trans) = rule; |
248 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
249 | ||
250 | return trans; | |
251 | } | |
252 | ||
253 | static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule) | |
254 | { | |
255 | struct nft_trans *trans; | |
256 | int err; | |
257 | ||
258 | trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule); | |
259 | if (trans == NULL) | |
260 | return -ENOMEM; | |
261 | ||
262 | err = nf_tables_delrule_deactivate(ctx, rule); | |
263 | if (err < 0) { | |
264 | nft_trans_destroy(trans); | |
265 | return err; | |
266 | } | |
267 | ||
268 | return 0; | |
269 | } | |
270 | ||
271 | static int nft_delrule_by_chain(struct nft_ctx *ctx) | |
272 | { | |
273 | struct nft_rule *rule; | |
274 | int err; | |
275 | ||
276 | list_for_each_entry(rule, &ctx->chain->rules, list) { | |
277 | err = nft_delrule(ctx, rule); | |
278 | if (err < 0) | |
279 | return err; | |
280 | } | |
281 | return 0; | |
282 | } | |
283 | ||
ee01d542 AB |
284 | static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type, |
285 | struct nft_set *set) | |
286 | { | |
287 | struct nft_trans *trans; | |
288 | ||
289 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set)); | |
290 | if (trans == NULL) | |
291 | return -ENOMEM; | |
292 | ||
293 | if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) { | |
294 | nft_trans_set_id(trans) = | |
295 | ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID])); | |
37a9cc52 | 296 | nft_activate_next(ctx->net, set); |
ee01d542 AB |
297 | } |
298 | nft_trans_set(trans) = set; | |
299 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
304 | static int nft_delset(struct nft_ctx *ctx, struct nft_set *set) | |
305 | { | |
306 | int err; | |
307 | ||
308 | err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set); | |
309 | if (err < 0) | |
310 | return err; | |
311 | ||
37a9cc52 | 312 | nft_deactivate_next(ctx->net, set); |
ee01d542 AB |
313 | ctx->table->use--; |
314 | ||
315 | return err; | |
316 | } | |
317 | ||
e5009240 PNA |
318 | static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type, |
319 | struct nft_object *obj) | |
320 | { | |
321 | struct nft_trans *trans; | |
322 | ||
323 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj)); | |
324 | if (trans == NULL) | |
325 | return -ENOMEM; | |
326 | ||
327 | if (msg_type == NFT_MSG_NEWOBJ) | |
328 | nft_activate_next(ctx->net, obj); | |
329 | ||
330 | nft_trans_obj(trans) = obj; | |
331 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
332 | ||
333 | return 0; | |
334 | } | |
335 | ||
336 | static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj) | |
337 | { | |
338 | int err; | |
339 | ||
340 | err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj); | |
341 | if (err < 0) | |
342 | return err; | |
343 | ||
344 | nft_deactivate_next(ctx->net, obj); | |
345 | ctx->table->use--; | |
346 | ||
347 | return err; | |
348 | } | |
349 | ||
96518518 PM |
350 | /* |
351 | * Tables | |
352 | */ | |
353 | ||
354 | static struct nft_table *nft_table_lookup(const struct nft_af_info *afi, | |
f2a6d766 PNA |
355 | const struct nlattr *nla, |
356 | u8 genmask) | |
96518518 PM |
357 | { |
358 | struct nft_table *table; | |
359 | ||
360 | list_for_each_entry(table, &afi->tables, list) { | |
f2a6d766 PNA |
361 | if (!nla_strcmp(nla, table->name) && |
362 | nft_active_genmask(table, genmask)) | |
96518518 PM |
363 | return table; |
364 | } | |
365 | return NULL; | |
366 | } | |
367 | ||
368 | static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi, | |
f2a6d766 PNA |
369 | const struct nlattr *nla, |
370 | u8 genmask) | |
96518518 PM |
371 | { |
372 | struct nft_table *table; | |
373 | ||
374 | if (nla == NULL) | |
375 | return ERR_PTR(-EINVAL); | |
376 | ||
f2a6d766 | 377 | table = nft_table_lookup(afi, nla, genmask); |
96518518 PM |
378 | if (table != NULL) |
379 | return table; | |
380 | ||
96518518 PM |
381 | return ERR_PTR(-ENOENT); |
382 | } | |
383 | ||
384 | static inline u64 nf_tables_alloc_handle(struct nft_table *table) | |
385 | { | |
386 | return ++table->hgenerator; | |
387 | } | |
388 | ||
2a37d755 | 389 | static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX]; |
9370761c | 390 | |
2a37d755 | 391 | static const struct nf_chain_type * |
baae3e62 | 392 | __nf_tables_chain_type_lookup(int family, const struct nlattr *nla) |
9370761c PNA |
393 | { |
394 | int i; | |
395 | ||
baae3e62 | 396 | for (i = 0; i < NFT_CHAIN_T_MAX; i++) { |
9370761c PNA |
397 | if (chain_type[family][i] != NULL && |
398 | !nla_strcmp(nla, chain_type[family][i]->name)) | |
baae3e62 | 399 | return chain_type[family][i]; |
9370761c | 400 | } |
baae3e62 | 401 | return NULL; |
9370761c PNA |
402 | } |
403 | ||
2a37d755 | 404 | static const struct nf_chain_type * |
baae3e62 PM |
405 | nf_tables_chain_type_lookup(const struct nft_af_info *afi, |
406 | const struct nlattr *nla, | |
407 | bool autoload) | |
9370761c | 408 | { |
2a37d755 | 409 | const struct nf_chain_type *type; |
9370761c PNA |
410 | |
411 | type = __nf_tables_chain_type_lookup(afi->family, nla); | |
93b0806f PM |
412 | if (type != NULL) |
413 | return type; | |
9370761c | 414 | #ifdef CONFIG_MODULES |
93b0806f | 415 | if (autoload) { |
9370761c | 416 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2fec6bb6 PNA |
417 | request_module("nft-chain-%u-%.*s", afi->family, |
418 | nla_len(nla), (const char *)nla_data(nla)); | |
9370761c PNA |
419 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
420 | type = __nf_tables_chain_type_lookup(afi->family, nla); | |
93b0806f PM |
421 | if (type != NULL) |
422 | return ERR_PTR(-EAGAIN); | |
9370761c PNA |
423 | } |
424 | #endif | |
93b0806f | 425 | return ERR_PTR(-ENOENT); |
9370761c PNA |
426 | } |
427 | ||
96518518 | 428 | static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { |
1cae565e PNA |
429 | [NFTA_TABLE_NAME] = { .type = NLA_STRING, |
430 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
9ddf6323 | 431 | [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, |
96518518 PM |
432 | }; |
433 | ||
84d7fce6 PNA |
434 | static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, |
435 | u32 portid, u32 seq, int event, u32 flags, | |
436 | int family, const struct nft_table *table) | |
96518518 PM |
437 | { |
438 | struct nlmsghdr *nlh; | |
439 | struct nfgenmsg *nfmsg; | |
440 | ||
441 | event |= NFNL_SUBSYS_NFTABLES << 8; | |
442 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); | |
443 | if (nlh == NULL) | |
444 | goto nla_put_failure; | |
445 | ||
446 | nfmsg = nlmsg_data(nlh); | |
447 | nfmsg->nfgen_family = family; | |
448 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 449 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
96518518 | 450 | |
9ddf6323 | 451 | if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || |
d8bcc768 TB |
452 | nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) || |
453 | nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use))) | |
96518518 PM |
454 | goto nla_put_failure; |
455 | ||
053c095a JB |
456 | nlmsg_end(skb, nlh); |
457 | return 0; | |
96518518 PM |
458 | |
459 | nla_put_failure: | |
460 | nlmsg_trim(skb, nlh); | |
461 | return -1; | |
462 | } | |
463 | ||
35151d84 | 464 | static int nf_tables_table_notify(const struct nft_ctx *ctx, int event) |
96518518 PM |
465 | { |
466 | struct sk_buff *skb; | |
96518518 PM |
467 | int err; |
468 | ||
128ad332 PNA |
469 | if (!ctx->report && |
470 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
96518518 PM |
471 | return 0; |
472 | ||
473 | err = -ENOBUFS; | |
474 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
475 | if (skb == NULL) | |
476 | goto err; | |
477 | ||
84d7fce6 PNA |
478 | err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq, |
479 | event, 0, ctx->afi->family, ctx->table); | |
96518518 PM |
480 | if (err < 0) { |
481 | kfree_skb(skb); | |
482 | goto err; | |
483 | } | |
484 | ||
128ad332 PNA |
485 | err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
486 | ctx->report, GFP_KERNEL); | |
96518518 | 487 | err: |
128ad332 PNA |
488 | if (err < 0) { |
489 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, | |
490 | err); | |
491 | } | |
96518518 PM |
492 | return err; |
493 | } | |
494 | ||
495 | static int nf_tables_dump_tables(struct sk_buff *skb, | |
496 | struct netlink_callback *cb) | |
497 | { | |
498 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
499 | const struct nft_af_info *afi; | |
500 | const struct nft_table *table; | |
501 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 502 | struct net *net = sock_net(skb->sk); |
96518518 PM |
503 | int family = nfmsg->nfgen_family; |
504 | ||
e688a7f8 | 505 | rcu_read_lock(); |
38e029f1 PNA |
506 | cb->seq = net->nft.base_seq; |
507 | ||
e688a7f8 | 508 | list_for_each_entry_rcu(afi, &net->nft.af_info, list) { |
96518518 PM |
509 | if (family != NFPROTO_UNSPEC && family != afi->family) |
510 | continue; | |
511 | ||
e688a7f8 | 512 | list_for_each_entry_rcu(table, &afi->tables, list) { |
96518518 PM |
513 | if (idx < s_idx) |
514 | goto cont; | |
515 | if (idx > s_idx) | |
516 | memset(&cb->args[1], 0, | |
517 | sizeof(cb->args) - sizeof(cb->args[0])); | |
f2a6d766 PNA |
518 | if (!nft_is_active(net, table)) |
519 | continue; | |
84d7fce6 | 520 | if (nf_tables_fill_table_info(skb, net, |
96518518 PM |
521 | NETLINK_CB(cb->skb).portid, |
522 | cb->nlh->nlmsg_seq, | |
523 | NFT_MSG_NEWTABLE, | |
524 | NLM_F_MULTI, | |
525 | afi->family, table) < 0) | |
526 | goto done; | |
38e029f1 PNA |
527 | |
528 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 PM |
529 | cont: |
530 | idx++; | |
531 | } | |
532 | } | |
533 | done: | |
e688a7f8 | 534 | rcu_read_unlock(); |
96518518 PM |
535 | cb->args[0] = idx; |
536 | return skb->len; | |
537 | } | |
538 | ||
7b8002a1 PNA |
539 | static int nf_tables_gettable(struct net *net, struct sock *nlsk, |
540 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
96518518 PM |
541 | const struct nlattr * const nla[]) |
542 | { | |
543 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 544 | u8 genmask = nft_genmask_cur(net); |
96518518 PM |
545 | const struct nft_af_info *afi; |
546 | const struct nft_table *table; | |
547 | struct sk_buff *skb2; | |
548 | int family = nfmsg->nfgen_family; | |
549 | int err; | |
550 | ||
551 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
552 | struct netlink_dump_control c = { | |
553 | .dump = nf_tables_dump_tables, | |
554 | }; | |
555 | return netlink_dump_start(nlsk, skb, nlh, &c); | |
556 | } | |
557 | ||
99633ab2 | 558 | afi = nf_tables_afinfo_lookup(net, family, false); |
96518518 PM |
559 | if (IS_ERR(afi)) |
560 | return PTR_ERR(afi); | |
561 | ||
f2a6d766 | 562 | table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME], genmask); |
96518518 PM |
563 | if (IS_ERR(table)) |
564 | return PTR_ERR(table); | |
565 | ||
566 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
567 | if (!skb2) | |
568 | return -ENOMEM; | |
569 | ||
84d7fce6 | 570 | err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid, |
96518518 PM |
571 | nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0, |
572 | family, table); | |
573 | if (err < 0) | |
574 | goto err; | |
575 | ||
576 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
577 | ||
578 | err: | |
579 | kfree_skb(skb2); | |
580 | return err; | |
581 | } | |
582 | ||
10435c11 F |
583 | static void _nf_tables_table_disable(struct net *net, |
584 | const struct nft_af_info *afi, | |
585 | struct nft_table *table, | |
586 | u32 cnt) | |
587 | { | |
588 | struct nft_chain *chain; | |
589 | u32 i = 0; | |
590 | ||
591 | list_for_each_entry(chain, &table->chains, list) { | |
592 | if (!nft_is_active_next(net, chain)) | |
593 | continue; | |
594 | if (!(chain->flags & NFT_BASE_CHAIN)) | |
595 | continue; | |
596 | ||
597 | if (cnt && i++ == cnt) | |
598 | break; | |
599 | ||
600 | nf_unregister_net_hooks(net, nft_base_chain(chain)->ops, | |
601 | afi->nops); | |
602 | } | |
603 | } | |
604 | ||
664b0f8c PNA |
605 | static int nf_tables_table_enable(struct net *net, |
606 | const struct nft_af_info *afi, | |
115a60b1 | 607 | struct nft_table *table) |
9ddf6323 PNA |
608 | { |
609 | struct nft_chain *chain; | |
610 | int err, i = 0; | |
611 | ||
612 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
613 | if (!nft_is_active_next(net, chain)) |
614 | continue; | |
d2012975 PNA |
615 | if (!(chain->flags & NFT_BASE_CHAIN)) |
616 | continue; | |
617 | ||
82bec71d PNA |
618 | err = nf_register_net_hooks(net, nft_base_chain(chain)->ops, |
619 | afi->nops); | |
9ddf6323 PNA |
620 | if (err < 0) |
621 | goto err; | |
622 | ||
623 | i++; | |
624 | } | |
625 | return 0; | |
626 | err: | |
10435c11 F |
627 | if (i) |
628 | _nf_tables_table_disable(net, afi, table, i); | |
9ddf6323 PNA |
629 | return err; |
630 | } | |
631 | ||
664b0f8c PNA |
632 | static void nf_tables_table_disable(struct net *net, |
633 | const struct nft_af_info *afi, | |
d8ee8f7c | 634 | struct nft_table *table) |
9ddf6323 | 635 | { |
10435c11 | 636 | _nf_tables_table_disable(net, afi, table, 0); |
9ddf6323 PNA |
637 | } |
638 | ||
e1aaca93 | 639 | static int nf_tables_updtable(struct nft_ctx *ctx) |
9ddf6323 | 640 | { |
55dd6f93 | 641 | struct nft_trans *trans; |
e1aaca93 | 642 | u32 flags; |
55dd6f93 | 643 | int ret = 0; |
9ddf6323 | 644 | |
e1aaca93 PNA |
645 | if (!ctx->nla[NFTA_TABLE_FLAGS]) |
646 | return 0; | |
9ddf6323 | 647 | |
e1aaca93 PNA |
648 | flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS])); |
649 | if (flags & ~NFT_TABLE_F_DORMANT) | |
650 | return -EINVAL; | |
651 | ||
63283dd2 PNA |
652 | if (flags == ctx->table->flags) |
653 | return 0; | |
654 | ||
55dd6f93 PNA |
655 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE, |
656 | sizeof(struct nft_trans_table)); | |
657 | if (trans == NULL) | |
658 | return -ENOMEM; | |
9ddf6323 | 659 | |
e1aaca93 PNA |
660 | if ((flags & NFT_TABLE_F_DORMANT) && |
661 | !(ctx->table->flags & NFT_TABLE_F_DORMANT)) { | |
55dd6f93 | 662 | nft_trans_table_enable(trans) = false; |
e1aaca93 PNA |
663 | } else if (!(flags & NFT_TABLE_F_DORMANT) && |
664 | ctx->table->flags & NFT_TABLE_F_DORMANT) { | |
664b0f8c | 665 | ret = nf_tables_table_enable(ctx->net, ctx->afi, ctx->table); |
55dd6f93 | 666 | if (ret >= 0) { |
e1aaca93 | 667 | ctx->table->flags &= ~NFT_TABLE_F_DORMANT; |
55dd6f93 | 668 | nft_trans_table_enable(trans) = true; |
9ddf6323 | 669 | } |
9ddf6323 | 670 | } |
e1aaca93 PNA |
671 | if (ret < 0) |
672 | goto err; | |
9ddf6323 | 673 | |
55dd6f93 PNA |
674 | nft_trans_table_update(trans) = true; |
675 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
676 | return 0; | |
9ddf6323 | 677 | err: |
55dd6f93 | 678 | nft_trans_destroy(trans); |
9ddf6323 PNA |
679 | return ret; |
680 | } | |
681 | ||
633c9a84 PNA |
682 | static int nf_tables_newtable(struct net *net, struct sock *nlsk, |
683 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
96518518 PM |
684 | const struct nlattr * const nla[]) |
685 | { | |
686 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 687 | u8 genmask = nft_genmask_next(net); |
96518518 PM |
688 | const struct nlattr *name; |
689 | struct nft_af_info *afi; | |
690 | struct nft_table *table; | |
691 | int family = nfmsg->nfgen_family; | |
c5c1f975 | 692 | u32 flags = 0; |
e1aaca93 | 693 | struct nft_ctx ctx; |
55dd6f93 | 694 | int err; |
96518518 | 695 | |
99633ab2 | 696 | afi = nf_tables_afinfo_lookup(net, family, true); |
96518518 PM |
697 | if (IS_ERR(afi)) |
698 | return PTR_ERR(afi); | |
699 | ||
700 | name = nla[NFTA_TABLE_NAME]; | |
f2a6d766 | 701 | table = nf_tables_table_lookup(afi, name, genmask); |
96518518 PM |
702 | if (IS_ERR(table)) { |
703 | if (PTR_ERR(table) != -ENOENT) | |
704 | return PTR_ERR(table); | |
1a28ad74 | 705 | } else { |
96518518 PM |
706 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
707 | return -EEXIST; | |
708 | if (nlh->nlmsg_flags & NLM_F_REPLACE) | |
709 | return -EOPNOTSUPP; | |
e1aaca93 | 710 | |
633c9a84 | 711 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla); |
e1aaca93 | 712 | return nf_tables_updtable(&ctx); |
96518518 PM |
713 | } |
714 | ||
c5c1f975 PM |
715 | if (nla[NFTA_TABLE_FLAGS]) { |
716 | flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS])); | |
717 | if (flags & ~NFT_TABLE_F_DORMANT) | |
718 | return -EINVAL; | |
719 | } | |
720 | ||
ebddf1a8 | 721 | err = -EAFNOSUPPORT; |
7047f9d0 | 722 | if (!try_module_get(afi->owner)) |
ebddf1a8 | 723 | goto err1; |
7047f9d0 | 724 | |
ffdb210e | 725 | err = -ENOMEM; |
1cae565e | 726 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
ffdb210e | 727 | if (table == NULL) |
ebddf1a8 | 728 | goto err2; |
96518518 | 729 | |
1cae565e | 730 | nla_strlcpy(table->name, name, NFT_TABLE_MAXNAMELEN); |
96518518 | 731 | INIT_LIST_HEAD(&table->chains); |
20a69341 | 732 | INIT_LIST_HEAD(&table->sets); |
e5009240 | 733 | INIT_LIST_HEAD(&table->objects); |
c5c1f975 | 734 | table->flags = flags; |
9ddf6323 | 735 | |
633c9a84 | 736 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla); |
55dd6f93 | 737 | err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); |
ffdb210e | 738 | if (err < 0) |
ebddf1a8 | 739 | goto err3; |
ffdb210e | 740 | |
e688a7f8 | 741 | list_add_tail_rcu(&table->list, &afi->tables); |
96518518 | 742 | return 0; |
ebddf1a8 | 743 | err3: |
ffdb210e | 744 | kfree(table); |
ebddf1a8 | 745 | err2: |
ffdb210e | 746 | module_put(afi->owner); |
ebddf1a8 | 747 | err1: |
ffdb210e | 748 | return err; |
96518518 PM |
749 | } |
750 | ||
b9ac12ef AB |
751 | static int nft_flush_table(struct nft_ctx *ctx) |
752 | { | |
753 | int err; | |
754 | struct nft_chain *chain, *nc; | |
e5009240 | 755 | struct nft_object *obj, *ne; |
b9ac12ef AB |
756 | struct nft_set *set, *ns; |
757 | ||
a2f18db0 | 758 | list_for_each_entry(chain, &ctx->table->chains, list) { |
664b0f8c PNA |
759 | if (!nft_is_active_next(ctx->net, chain)) |
760 | continue; | |
761 | ||
b9ac12ef AB |
762 | ctx->chain = chain; |
763 | ||
764 | err = nft_delrule_by_chain(ctx); | |
765 | if (err < 0) | |
766 | goto out; | |
b9ac12ef AB |
767 | } |
768 | ||
769 | list_for_each_entry_safe(set, ns, &ctx->table->sets, list) { | |
37a9cc52 PNA |
770 | if (!nft_is_active_next(ctx->net, set)) |
771 | continue; | |
772 | ||
b9ac12ef AB |
773 | if (set->flags & NFT_SET_ANONYMOUS && |
774 | !list_empty(&set->bindings)) | |
775 | continue; | |
776 | ||
777 | err = nft_delset(ctx, set); | |
778 | if (err < 0) | |
779 | goto out; | |
780 | } | |
781 | ||
e5009240 PNA |
782 | list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) { |
783 | err = nft_delobj(ctx, obj); | |
784 | if (err < 0) | |
785 | goto out; | |
786 | } | |
787 | ||
a2f18db0 | 788 | list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) { |
664b0f8c PNA |
789 | if (!nft_is_active_next(ctx->net, chain)) |
790 | continue; | |
791 | ||
a2f18db0 PNA |
792 | ctx->chain = chain; |
793 | ||
794 | err = nft_delchain(ctx); | |
795 | if (err < 0) | |
796 | goto out; | |
797 | } | |
798 | ||
b9ac12ef AB |
799 | err = nft_deltable(ctx); |
800 | out: | |
801 | return err; | |
802 | } | |
803 | ||
804 | static int nft_flush(struct nft_ctx *ctx, int family) | |
805 | { | |
806 | struct nft_af_info *afi; | |
807 | struct nft_table *table, *nt; | |
808 | const struct nlattr * const *nla = ctx->nla; | |
809 | int err = 0; | |
810 | ||
811 | list_for_each_entry(afi, &ctx->net->nft.af_info, list) { | |
812 | if (family != AF_UNSPEC && afi->family != family) | |
813 | continue; | |
814 | ||
815 | ctx->afi = afi; | |
816 | list_for_each_entry_safe(table, nt, &afi->tables, list) { | |
f2a6d766 PNA |
817 | if (!nft_is_active_next(ctx->net, table)) |
818 | continue; | |
819 | ||
b9ac12ef AB |
820 | if (nla[NFTA_TABLE_NAME] && |
821 | nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0) | |
822 | continue; | |
823 | ||
824 | ctx->table = table; | |
825 | ||
826 | err = nft_flush_table(ctx); | |
827 | if (err < 0) | |
828 | goto out; | |
829 | } | |
830 | } | |
831 | out: | |
832 | return err; | |
833 | } | |
834 | ||
633c9a84 PNA |
835 | static int nf_tables_deltable(struct net *net, struct sock *nlsk, |
836 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
96518518 PM |
837 | const struct nlattr * const nla[]) |
838 | { | |
839 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 840 | u8 genmask = nft_genmask_next(net); |
96518518 PM |
841 | struct nft_af_info *afi; |
842 | struct nft_table *table; | |
ee01d542 | 843 | int family = nfmsg->nfgen_family; |
55dd6f93 | 844 | struct nft_ctx ctx; |
96518518 | 845 | |
633c9a84 | 846 | nft_ctx_init(&ctx, net, skb, nlh, NULL, NULL, NULL, nla); |
b9ac12ef AB |
847 | if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL) |
848 | return nft_flush(&ctx, family); | |
849 | ||
99633ab2 | 850 | afi = nf_tables_afinfo_lookup(net, family, false); |
96518518 PM |
851 | if (IS_ERR(afi)) |
852 | return PTR_ERR(afi); | |
853 | ||
f2a6d766 | 854 | table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME], genmask); |
96518518 PM |
855 | if (IS_ERR(table)) |
856 | return PTR_ERR(table); | |
96518518 | 857 | |
b9ac12ef AB |
858 | ctx.afi = afi; |
859 | ctx.table = table; | |
55dd6f93 | 860 | |
b9ac12ef | 861 | return nft_flush_table(&ctx); |
96518518 PM |
862 | } |
863 | ||
55dd6f93 PNA |
864 | static void nf_tables_table_destroy(struct nft_ctx *ctx) |
865 | { | |
4fefee57 PNA |
866 | BUG_ON(ctx->table->use > 0); |
867 | ||
55dd6f93 PNA |
868 | kfree(ctx->table); |
869 | module_put(ctx->afi->owner); | |
870 | } | |
871 | ||
2a37d755 | 872 | int nft_register_chain_type(const struct nf_chain_type *ctype) |
96518518 | 873 | { |
9370761c | 874 | int err = 0; |
96518518 PM |
875 | |
876 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
9370761c PNA |
877 | if (chain_type[ctype->family][ctype->type] != NULL) { |
878 | err = -EBUSY; | |
879 | goto out; | |
96518518 | 880 | } |
9370761c PNA |
881 | chain_type[ctype->family][ctype->type] = ctype; |
882 | out: | |
96518518 PM |
883 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
884 | return err; | |
885 | } | |
9370761c | 886 | EXPORT_SYMBOL_GPL(nft_register_chain_type); |
96518518 | 887 | |
2a37d755 | 888 | void nft_unregister_chain_type(const struct nf_chain_type *ctype) |
96518518 | 889 | { |
96518518 | 890 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
9370761c | 891 | chain_type[ctype->family][ctype->type] = NULL; |
96518518 PM |
892 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
893 | } | |
9370761c | 894 | EXPORT_SYMBOL_GPL(nft_unregister_chain_type); |
96518518 PM |
895 | |
896 | /* | |
897 | * Chains | |
898 | */ | |
899 | ||
900 | static struct nft_chain * | |
664b0f8c PNA |
901 | nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle, |
902 | u8 genmask) | |
96518518 PM |
903 | { |
904 | struct nft_chain *chain; | |
905 | ||
906 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
907 | if (chain->handle == handle && |
908 | nft_active_genmask(chain, genmask)) | |
96518518 PM |
909 | return chain; |
910 | } | |
911 | ||
912 | return ERR_PTR(-ENOENT); | |
913 | } | |
914 | ||
915 | static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table, | |
664b0f8c PNA |
916 | const struct nlattr *nla, |
917 | u8 genmask) | |
96518518 PM |
918 | { |
919 | struct nft_chain *chain; | |
920 | ||
921 | if (nla == NULL) | |
922 | return ERR_PTR(-EINVAL); | |
923 | ||
924 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
925 | if (!nla_strcmp(nla, chain->name) && |
926 | nft_active_genmask(chain, genmask)) | |
96518518 PM |
927 | return chain; |
928 | } | |
929 | ||
930 | return ERR_PTR(-ENOENT); | |
931 | } | |
932 | ||
933 | static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { | |
b2fbd044 LZ |
934 | [NFTA_CHAIN_TABLE] = { .type = NLA_STRING, |
935 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
936 | [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 }, |
937 | [NFTA_CHAIN_NAME] = { .type = NLA_STRING, | |
938 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
939 | [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, | |
0ca743a5 | 940 | [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, |
4c1f7818 | 941 | [NFTA_CHAIN_TYPE] = { .type = NLA_STRING }, |
0ca743a5 | 942 | [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, |
96518518 PM |
943 | }; |
944 | ||
945 | static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = { | |
946 | [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 }, | |
947 | [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
2cbce139 PNA |
948 | [NFTA_HOOK_DEV] = { .type = NLA_STRING, |
949 | .len = IFNAMSIZ - 1 }, | |
96518518 PM |
950 | }; |
951 | ||
0ca743a5 PNA |
952 | static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats) |
953 | { | |
954 | struct nft_stats *cpu_stats, total; | |
955 | struct nlattr *nest; | |
ce355e20 ED |
956 | unsigned int seq; |
957 | u64 pkts, bytes; | |
0ca743a5 PNA |
958 | int cpu; |
959 | ||
960 | memset(&total, 0, sizeof(total)); | |
961 | for_each_possible_cpu(cpu) { | |
962 | cpu_stats = per_cpu_ptr(stats, cpu); | |
ce355e20 ED |
963 | do { |
964 | seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp); | |
965 | pkts = cpu_stats->pkts; | |
966 | bytes = cpu_stats->bytes; | |
967 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq)); | |
968 | total.pkts += pkts; | |
969 | total.bytes += bytes; | |
0ca743a5 PNA |
970 | } |
971 | nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS); | |
972 | if (nest == NULL) | |
973 | goto nla_put_failure; | |
974 | ||
b46f6ded ND |
975 | if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts), |
976 | NFTA_COUNTER_PAD) || | |
977 | nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes), | |
978 | NFTA_COUNTER_PAD)) | |
0ca743a5 PNA |
979 | goto nla_put_failure; |
980 | ||
981 | nla_nest_end(skb, nest); | |
982 | return 0; | |
983 | ||
984 | nla_put_failure: | |
985 | return -ENOSPC; | |
986 | } | |
987 | ||
84d7fce6 PNA |
988 | static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, |
989 | u32 portid, u32 seq, int event, u32 flags, | |
990 | int family, const struct nft_table *table, | |
96518518 PM |
991 | const struct nft_chain *chain) |
992 | { | |
993 | struct nlmsghdr *nlh; | |
994 | struct nfgenmsg *nfmsg; | |
995 | ||
996 | event |= NFNL_SUBSYS_NFTABLES << 8; | |
997 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); | |
998 | if (nlh == NULL) | |
999 | goto nla_put_failure; | |
1000 | ||
1001 | nfmsg = nlmsg_data(nlh); | |
1002 | nfmsg->nfgen_family = family; | |
1003 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 1004 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
96518518 PM |
1005 | |
1006 | if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name)) | |
1007 | goto nla_put_failure; | |
b46f6ded ND |
1008 | if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle), |
1009 | NFTA_CHAIN_PAD)) | |
96518518 PM |
1010 | goto nla_put_failure; |
1011 | if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name)) | |
1012 | goto nla_put_failure; | |
1013 | ||
1014 | if (chain->flags & NFT_BASE_CHAIN) { | |
0ca743a5 | 1015 | const struct nft_base_chain *basechain = nft_base_chain(chain); |
115a60b1 | 1016 | const struct nf_hook_ops *ops = &basechain->ops[0]; |
0ca743a5 PNA |
1017 | struct nlattr *nest; |
1018 | ||
1019 | nest = nla_nest_start(skb, NFTA_CHAIN_HOOK); | |
96518518 PM |
1020 | if (nest == NULL) |
1021 | goto nla_put_failure; | |
1022 | if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum))) | |
1023 | goto nla_put_failure; | |
1024 | if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority))) | |
1025 | goto nla_put_failure; | |
2cbce139 PNA |
1026 | if (basechain->dev_name[0] && |
1027 | nla_put_string(skb, NFTA_HOOK_DEV, basechain->dev_name)) | |
1028 | goto nla_put_failure; | |
96518518 | 1029 | nla_nest_end(skb, nest); |
9370761c | 1030 | |
0ca743a5 PNA |
1031 | if (nla_put_be32(skb, NFTA_CHAIN_POLICY, |
1032 | htonl(basechain->policy))) | |
1033 | goto nla_put_failure; | |
1034 | ||
baae3e62 PM |
1035 | if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name)) |
1036 | goto nla_put_failure; | |
0ca743a5 PNA |
1037 | |
1038 | if (nft_dump_stats(skb, nft_base_chain(chain)->stats)) | |
1039 | goto nla_put_failure; | |
96518518 PM |
1040 | } |
1041 | ||
0ca743a5 PNA |
1042 | if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use))) |
1043 | goto nla_put_failure; | |
1044 | ||
053c095a JB |
1045 | nlmsg_end(skb, nlh); |
1046 | return 0; | |
96518518 PM |
1047 | |
1048 | nla_put_failure: | |
1049 | nlmsg_trim(skb, nlh); | |
1050 | return -1; | |
1051 | } | |
1052 | ||
35151d84 | 1053 | static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event) |
96518518 PM |
1054 | { |
1055 | struct sk_buff *skb; | |
96518518 PM |
1056 | int err; |
1057 | ||
128ad332 PNA |
1058 | if (!ctx->report && |
1059 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
96518518 PM |
1060 | return 0; |
1061 | ||
1062 | err = -ENOBUFS; | |
1063 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1064 | if (skb == NULL) | |
1065 | goto err; | |
1066 | ||
84d7fce6 PNA |
1067 | err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq, |
1068 | event, 0, ctx->afi->family, ctx->table, | |
35151d84 | 1069 | ctx->chain); |
96518518 PM |
1070 | if (err < 0) { |
1071 | kfree_skb(skb); | |
1072 | goto err; | |
1073 | } | |
1074 | ||
128ad332 PNA |
1075 | err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
1076 | ctx->report, GFP_KERNEL); | |
96518518 | 1077 | err: |
128ad332 PNA |
1078 | if (err < 0) { |
1079 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, | |
1080 | err); | |
1081 | } | |
96518518 PM |
1082 | return err; |
1083 | } | |
1084 | ||
1085 | static int nf_tables_dump_chains(struct sk_buff *skb, | |
1086 | struct netlink_callback *cb) | |
1087 | { | |
1088 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
1089 | const struct nft_af_info *afi; | |
1090 | const struct nft_table *table; | |
1091 | const struct nft_chain *chain; | |
1092 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 1093 | struct net *net = sock_net(skb->sk); |
96518518 PM |
1094 | int family = nfmsg->nfgen_family; |
1095 | ||
e688a7f8 | 1096 | rcu_read_lock(); |
38e029f1 PNA |
1097 | cb->seq = net->nft.base_seq; |
1098 | ||
e688a7f8 | 1099 | list_for_each_entry_rcu(afi, &net->nft.af_info, list) { |
96518518 PM |
1100 | if (family != NFPROTO_UNSPEC && family != afi->family) |
1101 | continue; | |
1102 | ||
e688a7f8 PNA |
1103 | list_for_each_entry_rcu(table, &afi->tables, list) { |
1104 | list_for_each_entry_rcu(chain, &table->chains, list) { | |
96518518 PM |
1105 | if (idx < s_idx) |
1106 | goto cont; | |
1107 | if (idx > s_idx) | |
1108 | memset(&cb->args[1], 0, | |
1109 | sizeof(cb->args) - sizeof(cb->args[0])); | |
664b0f8c PNA |
1110 | if (!nft_is_active(net, chain)) |
1111 | continue; | |
84d7fce6 PNA |
1112 | if (nf_tables_fill_chain_info(skb, net, |
1113 | NETLINK_CB(cb->skb).portid, | |
96518518 PM |
1114 | cb->nlh->nlmsg_seq, |
1115 | NFT_MSG_NEWCHAIN, | |
1116 | NLM_F_MULTI, | |
1117 | afi->family, table, chain) < 0) | |
1118 | goto done; | |
38e029f1 PNA |
1119 | |
1120 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 PM |
1121 | cont: |
1122 | idx++; | |
1123 | } | |
1124 | } | |
1125 | } | |
1126 | done: | |
e688a7f8 | 1127 | rcu_read_unlock(); |
96518518 PM |
1128 | cb->args[0] = idx; |
1129 | return skb->len; | |
1130 | } | |
1131 | ||
7b8002a1 PNA |
1132 | static int nf_tables_getchain(struct net *net, struct sock *nlsk, |
1133 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
96518518 PM |
1134 | const struct nlattr * const nla[]) |
1135 | { | |
1136 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 1137 | u8 genmask = nft_genmask_cur(net); |
96518518 PM |
1138 | const struct nft_af_info *afi; |
1139 | const struct nft_table *table; | |
1140 | const struct nft_chain *chain; | |
1141 | struct sk_buff *skb2; | |
1142 | int family = nfmsg->nfgen_family; | |
1143 | int err; | |
1144 | ||
1145 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1146 | struct netlink_dump_control c = { | |
1147 | .dump = nf_tables_dump_chains, | |
1148 | }; | |
1149 | return netlink_dump_start(nlsk, skb, nlh, &c); | |
1150 | } | |
1151 | ||
99633ab2 | 1152 | afi = nf_tables_afinfo_lookup(net, family, false); |
96518518 PM |
1153 | if (IS_ERR(afi)) |
1154 | return PTR_ERR(afi); | |
1155 | ||
f2a6d766 | 1156 | table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask); |
96518518 PM |
1157 | if (IS_ERR(table)) |
1158 | return PTR_ERR(table); | |
1159 | ||
664b0f8c | 1160 | chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask); |
96518518 PM |
1161 | if (IS_ERR(chain)) |
1162 | return PTR_ERR(chain); | |
1163 | ||
1164 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
1165 | if (!skb2) | |
1166 | return -ENOMEM; | |
1167 | ||
84d7fce6 | 1168 | err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid, |
96518518 PM |
1169 | nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0, |
1170 | family, table, chain); | |
1171 | if (err < 0) | |
1172 | goto err; | |
1173 | ||
1174 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
1175 | ||
1176 | err: | |
1177 | kfree_skb(skb2); | |
1178 | return err; | |
1179 | } | |
1180 | ||
0ca743a5 PNA |
1181 | static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { |
1182 | [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, | |
1183 | [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, | |
1184 | }; | |
1185 | ||
ff3cd7b3 | 1186 | static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr) |
0ca743a5 PNA |
1187 | { |
1188 | struct nlattr *tb[NFTA_COUNTER_MAX+1]; | |
1189 | struct nft_stats __percpu *newstats; | |
1190 | struct nft_stats *stats; | |
1191 | int err; | |
1192 | ||
1193 | err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy); | |
1194 | if (err < 0) | |
ff3cd7b3 | 1195 | return ERR_PTR(err); |
0ca743a5 PNA |
1196 | |
1197 | if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS]) | |
ff3cd7b3 | 1198 | return ERR_PTR(-EINVAL); |
0ca743a5 | 1199 | |
ce355e20 | 1200 | newstats = netdev_alloc_pcpu_stats(struct nft_stats); |
0ca743a5 | 1201 | if (newstats == NULL) |
ff3cd7b3 | 1202 | return ERR_PTR(-ENOMEM); |
0ca743a5 PNA |
1203 | |
1204 | /* Restore old counters on this cpu, no problem. Per-cpu statistics | |
1205 | * are not exposed to userspace. | |
1206 | */ | |
e8781f70 | 1207 | preempt_disable(); |
0ca743a5 PNA |
1208 | stats = this_cpu_ptr(newstats); |
1209 | stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); | |
1210 | stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); | |
e8781f70 | 1211 | preempt_enable(); |
0ca743a5 | 1212 | |
ff3cd7b3 PNA |
1213 | return newstats; |
1214 | } | |
1215 | ||
1216 | static void nft_chain_stats_replace(struct nft_base_chain *chain, | |
1217 | struct nft_stats __percpu *newstats) | |
1218 | { | |
b88825de PNA |
1219 | if (newstats == NULL) |
1220 | return; | |
1221 | ||
0ca743a5 | 1222 | if (chain->stats) { |
0ca743a5 | 1223 | struct nft_stats __percpu *oldstats = |
67a8fc27 | 1224 | nft_dereference(chain->stats); |
0ca743a5 PNA |
1225 | |
1226 | rcu_assign_pointer(chain->stats, newstats); | |
1227 | synchronize_rcu(); | |
1228 | free_percpu(oldstats); | |
1229 | } else | |
1230 | rcu_assign_pointer(chain->stats, newstats); | |
0ca743a5 PNA |
1231 | } |
1232 | ||
91c7b38d PNA |
1233 | static void nf_tables_chain_destroy(struct nft_chain *chain) |
1234 | { | |
1235 | BUG_ON(chain->use > 0); | |
1236 | ||
1237 | if (chain->flags & NFT_BASE_CHAIN) { | |
2cbce139 PNA |
1238 | struct nft_base_chain *basechain = nft_base_chain(chain); |
1239 | ||
1240 | module_put(basechain->type->owner); | |
1241 | free_percpu(basechain->stats); | |
1242 | if (basechain->ops[0].dev != NULL) | |
1243 | dev_put(basechain->ops[0].dev); | |
1244 | kfree(basechain); | |
91c7b38d PNA |
1245 | } else { |
1246 | kfree(chain); | |
1247 | } | |
1248 | } | |
1249 | ||
508f8ccd PNA |
1250 | struct nft_chain_hook { |
1251 | u32 num; | |
1252 | u32 priority; | |
1253 | const struct nf_chain_type *type; | |
1254 | struct net_device *dev; | |
1255 | }; | |
1256 | ||
1257 | static int nft_chain_parse_hook(struct net *net, | |
1258 | const struct nlattr * const nla[], | |
1259 | struct nft_af_info *afi, | |
1260 | struct nft_chain_hook *hook, bool create) | |
1261 | { | |
1262 | struct nlattr *ha[NFTA_HOOK_MAX + 1]; | |
1263 | const struct nf_chain_type *type; | |
1264 | struct net_device *dev; | |
1265 | int err; | |
1266 | ||
1267 | err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK], | |
1268 | nft_hook_policy); | |
1269 | if (err < 0) | |
1270 | return err; | |
1271 | ||
1272 | if (ha[NFTA_HOOK_HOOKNUM] == NULL || | |
1273 | ha[NFTA_HOOK_PRIORITY] == NULL) | |
1274 | return -EINVAL; | |
1275 | ||
1276 | hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); | |
1277 | if (hook->num >= afi->nhooks) | |
1278 | return -EINVAL; | |
1279 | ||
1280 | hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); | |
1281 | ||
1282 | type = chain_type[afi->family][NFT_CHAIN_T_DEFAULT]; | |
1283 | if (nla[NFTA_CHAIN_TYPE]) { | |
1284 | type = nf_tables_chain_type_lookup(afi, nla[NFTA_CHAIN_TYPE], | |
1285 | create); | |
1286 | if (IS_ERR(type)) | |
1287 | return PTR_ERR(type); | |
1288 | } | |
1289 | if (!(type->hook_mask & (1 << hook->num))) | |
1290 | return -EOPNOTSUPP; | |
1291 | if (!try_module_get(type->owner)) | |
1292 | return -ENOENT; | |
1293 | ||
1294 | hook->type = type; | |
1295 | ||
1296 | hook->dev = NULL; | |
1297 | if (afi->flags & NFT_AF_NEEDS_DEV) { | |
1298 | char ifname[IFNAMSIZ]; | |
1299 | ||
1300 | if (!ha[NFTA_HOOK_DEV]) { | |
1301 | module_put(type->owner); | |
1302 | return -EOPNOTSUPP; | |
1303 | } | |
1304 | ||
1305 | nla_strlcpy(ifname, ha[NFTA_HOOK_DEV], IFNAMSIZ); | |
1306 | dev = dev_get_by_name(net, ifname); | |
1307 | if (!dev) { | |
1308 | module_put(type->owner); | |
1309 | return -ENOENT; | |
1310 | } | |
1311 | hook->dev = dev; | |
1312 | } else if (ha[NFTA_HOOK_DEV]) { | |
1313 | module_put(type->owner); | |
1314 | return -EOPNOTSUPP; | |
1315 | } | |
1316 | ||
1317 | return 0; | |
1318 | } | |
1319 | ||
1320 | static void nft_chain_release_hook(struct nft_chain_hook *hook) | |
1321 | { | |
1322 | module_put(hook->type->owner); | |
1323 | if (hook->dev != NULL) | |
1324 | dev_put(hook->dev); | |
1325 | } | |
1326 | ||
633c9a84 PNA |
1327 | static int nf_tables_newchain(struct net *net, struct sock *nlsk, |
1328 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
96518518 PM |
1329 | const struct nlattr * const nla[]) |
1330 | { | |
1331 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
1332 | const struct nlattr * uninitialized_var(name); | |
7c95f6d8 | 1333 | struct nft_af_info *afi; |
96518518 PM |
1334 | struct nft_table *table; |
1335 | struct nft_chain *chain; | |
0ca743a5 | 1336 | struct nft_base_chain *basechain = NULL; |
f2a6d766 | 1337 | u8 genmask = nft_genmask_next(net); |
96518518 | 1338 | int family = nfmsg->nfgen_family; |
57de2a0c | 1339 | u8 policy = NF_ACCEPT; |
96518518 | 1340 | u64 handle = 0; |
115a60b1 | 1341 | unsigned int i; |
ff3cd7b3 | 1342 | struct nft_stats __percpu *stats; |
96518518 PM |
1343 | int err; |
1344 | bool create; | |
91c7b38d | 1345 | struct nft_ctx ctx; |
96518518 PM |
1346 | |
1347 | create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false; | |
1348 | ||
99633ab2 | 1349 | afi = nf_tables_afinfo_lookup(net, family, true); |
96518518 PM |
1350 | if (IS_ERR(afi)) |
1351 | return PTR_ERR(afi); | |
1352 | ||
f2a6d766 | 1353 | table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask); |
96518518 PM |
1354 | if (IS_ERR(table)) |
1355 | return PTR_ERR(table); | |
1356 | ||
96518518 PM |
1357 | chain = NULL; |
1358 | name = nla[NFTA_CHAIN_NAME]; | |
1359 | ||
1360 | if (nla[NFTA_CHAIN_HANDLE]) { | |
1361 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); | |
664b0f8c | 1362 | chain = nf_tables_chain_lookup_byhandle(table, handle, genmask); |
96518518 PM |
1363 | if (IS_ERR(chain)) |
1364 | return PTR_ERR(chain); | |
1365 | } else { | |
664b0f8c | 1366 | chain = nf_tables_chain_lookup(table, name, genmask); |
96518518 PM |
1367 | if (IS_ERR(chain)) { |
1368 | if (PTR_ERR(chain) != -ENOENT) | |
1369 | return PTR_ERR(chain); | |
1370 | chain = NULL; | |
1371 | } | |
1372 | } | |
1373 | ||
57de2a0c PM |
1374 | if (nla[NFTA_CHAIN_POLICY]) { |
1375 | if ((chain != NULL && | |
d6b6cb1d PNA |
1376 | !(chain->flags & NFT_BASE_CHAIN))) |
1377 | return -EOPNOTSUPP; | |
1378 | ||
1379 | if (chain == NULL && | |
57de2a0c PM |
1380 | nla[NFTA_CHAIN_HOOK] == NULL) |
1381 | return -EOPNOTSUPP; | |
1382 | ||
8f46df18 | 1383 | policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
57de2a0c PM |
1384 | switch (policy) { |
1385 | case NF_DROP: | |
1386 | case NF_ACCEPT: | |
1387 | break; | |
1388 | default: | |
1389 | return -EINVAL; | |
1390 | } | |
1391 | } | |
1392 | ||
96518518 | 1393 | if (chain != NULL) { |
91c7b38d PNA |
1394 | struct nft_stats *stats = NULL; |
1395 | struct nft_trans *trans; | |
1396 | ||
96518518 PM |
1397 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
1398 | return -EEXIST; | |
1399 | if (nlh->nlmsg_flags & NLM_F_REPLACE) | |
1400 | return -EOPNOTSUPP; | |
1401 | ||
6133740d PNA |
1402 | if (nla[NFTA_CHAIN_HOOK]) { |
1403 | struct nft_base_chain *basechain; | |
1404 | struct nft_chain_hook hook; | |
1405 | struct nf_hook_ops *ops; | |
1406 | ||
1407 | if (!(chain->flags & NFT_BASE_CHAIN)) | |
1408 | return -EBUSY; | |
1409 | ||
1410 | err = nft_chain_parse_hook(net, nla, afi, &hook, | |
1411 | create); | |
1412 | if (err < 0) | |
1413 | return err; | |
1414 | ||
1415 | basechain = nft_base_chain(chain); | |
1416 | if (basechain->type != hook.type) { | |
1417 | nft_chain_release_hook(&hook); | |
1418 | return -EBUSY; | |
1419 | } | |
1420 | ||
1421 | for (i = 0; i < afi->nops; i++) { | |
1422 | ops = &basechain->ops[i]; | |
1423 | if (ops->hooknum != hook.num || | |
1424 | ops->priority != hook.priority || | |
1425 | ops->dev != hook.dev) { | |
1426 | nft_chain_release_hook(&hook); | |
1427 | return -EBUSY; | |
1428 | } | |
1429 | } | |
1430 | nft_chain_release_hook(&hook); | |
1431 | } | |
1432 | ||
664b0f8c PNA |
1433 | if (nla[NFTA_CHAIN_HANDLE] && name) { |
1434 | struct nft_chain *chain2; | |
1435 | ||
1436 | chain2 = nf_tables_chain_lookup(table, | |
1437 | nla[NFTA_CHAIN_NAME], | |
1438 | genmask); | |
1439 | if (IS_ERR(chain2)) | |
1440 | return PTR_ERR(chain2); | |
1441 | } | |
96518518 | 1442 | |
0ca743a5 PNA |
1443 | if (nla[NFTA_CHAIN_COUNTERS]) { |
1444 | if (!(chain->flags & NFT_BASE_CHAIN)) | |
1445 | return -EOPNOTSUPP; | |
1446 | ||
ff3cd7b3 PNA |
1447 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); |
1448 | if (IS_ERR(stats)) | |
1449 | return PTR_ERR(stats); | |
0ca743a5 PNA |
1450 | } |
1451 | ||
633c9a84 | 1452 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
91c7b38d PNA |
1453 | trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN, |
1454 | sizeof(struct nft_trans_chain)); | |
f5553c19 PNA |
1455 | if (trans == NULL) { |
1456 | free_percpu(stats); | |
91c7b38d | 1457 | return -ENOMEM; |
f5553c19 | 1458 | } |
4401a862 | 1459 | |
91c7b38d PNA |
1460 | nft_trans_chain_stats(trans) = stats; |
1461 | nft_trans_chain_update(trans) = true; | |
4401a862 | 1462 | |
91c7b38d PNA |
1463 | if (nla[NFTA_CHAIN_POLICY]) |
1464 | nft_trans_chain_policy(trans) = policy; | |
1465 | else | |
1466 | nft_trans_chain_policy(trans) = -1; | |
96518518 | 1467 | |
91c7b38d PNA |
1468 | if (nla[NFTA_CHAIN_HANDLE] && name) { |
1469 | nla_strlcpy(nft_trans_chain_name(trans), name, | |
1470 | NFT_CHAIN_MAXNAMELEN); | |
1471 | } | |
1472 | list_add_tail(&trans->list, &net->nft.commit_list); | |
1473 | return 0; | |
96518518 PM |
1474 | } |
1475 | ||
75820676 PM |
1476 | if (table->use == UINT_MAX) |
1477 | return -EOVERFLOW; | |
1478 | ||
96518518 | 1479 | if (nla[NFTA_CHAIN_HOOK]) { |
508f8ccd | 1480 | struct nft_chain_hook hook; |
96518518 | 1481 | struct nf_hook_ops *ops; |
9370761c | 1482 | nf_hookfn *hookfn; |
96518518 | 1483 | |
508f8ccd | 1484 | err = nft_chain_parse_hook(net, nla, afi, &hook, create); |
96518518 PM |
1485 | if (err < 0) |
1486 | return err; | |
2cbce139 | 1487 | |
96518518 | 1488 | basechain = kzalloc(sizeof(*basechain), GFP_KERNEL); |
f5553c19 | 1489 | if (basechain == NULL) { |
508f8ccd | 1490 | nft_chain_release_hook(&hook); |
96518518 | 1491 | return -ENOMEM; |
f5553c19 | 1492 | } |
9370761c | 1493 | |
508f8ccd PNA |
1494 | if (hook.dev != NULL) |
1495 | strncpy(basechain->dev_name, hook.dev->name, IFNAMSIZ); | |
2cbce139 | 1496 | |
4401a862 | 1497 | if (nla[NFTA_CHAIN_COUNTERS]) { |
ff3cd7b3 PNA |
1498 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); |
1499 | if (IS_ERR(stats)) { | |
508f8ccd | 1500 | nft_chain_release_hook(&hook); |
4401a862 | 1501 | kfree(basechain); |
ff3cd7b3 | 1502 | return PTR_ERR(stats); |
4401a862 | 1503 | } |
ff3cd7b3 | 1504 | basechain->stats = stats; |
4401a862 | 1505 | } else { |
ce355e20 | 1506 | stats = netdev_alloc_pcpu_stats(struct nft_stats); |
c123bb71 | 1507 | if (stats == NULL) { |
508f8ccd | 1508 | nft_chain_release_hook(&hook); |
4401a862 | 1509 | kfree(basechain); |
c123bb71 | 1510 | return -ENOMEM; |
4401a862 | 1511 | } |
ff3cd7b3 | 1512 | rcu_assign_pointer(basechain->stats, stats); |
4401a862 PM |
1513 | } |
1514 | ||
508f8ccd PNA |
1515 | hookfn = hook.type->hooks[hook.num]; |
1516 | basechain->type = hook.type; | |
96518518 PM |
1517 | chain = &basechain->chain; |
1518 | ||
115a60b1 PM |
1519 | for (i = 0; i < afi->nops; i++) { |
1520 | ops = &basechain->ops[i]; | |
1521 | ops->pf = family; | |
508f8ccd PNA |
1522 | ops->hooknum = hook.num; |
1523 | ops->priority = hook.priority; | |
115a60b1 PM |
1524 | ops->priv = chain; |
1525 | ops->hook = afi->hooks[ops->hooknum]; | |
508f8ccd | 1526 | ops->dev = hook.dev; |
115a60b1 PM |
1527 | if (hookfn) |
1528 | ops->hook = hookfn; | |
1529 | if (afi->hook_ops_init) | |
1530 | afi->hook_ops_init(ops, i); | |
1531 | } | |
96518518 PM |
1532 | |
1533 | chain->flags |= NFT_BASE_CHAIN; | |
57de2a0c | 1534 | basechain->policy = policy; |
96518518 PM |
1535 | } else { |
1536 | chain = kzalloc(sizeof(*chain), GFP_KERNEL); | |
1537 | if (chain == NULL) | |
1538 | return -ENOMEM; | |
1539 | } | |
1540 | ||
1541 | INIT_LIST_HEAD(&chain->rules); | |
1542 | chain->handle = nf_tables_alloc_handle(table); | |
b5bc89bf | 1543 | chain->table = table; |
96518518 PM |
1544 | nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN); |
1545 | ||
82bec71d | 1546 | err = nf_tables_register_hooks(net, table, chain, afi->nops); |
d8ee8f7c PNA |
1547 | if (err < 0) |
1548 | goto err1; | |
96518518 | 1549 | |
633c9a84 | 1550 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
91c7b38d PNA |
1551 | err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN); |
1552 | if (err < 0) | |
1553 | goto err2; | |
96518518 | 1554 | |
4fefee57 | 1555 | table->use++; |
e688a7f8 | 1556 | list_add_tail_rcu(&chain->list, &table->chains); |
91c7b38d PNA |
1557 | return 0; |
1558 | err2: | |
82bec71d | 1559 | nf_tables_unregister_hooks(net, table, chain, afi->nops); |
91c7b38d PNA |
1560 | err1: |
1561 | nf_tables_chain_destroy(chain); | |
1562 | return err; | |
96518518 PM |
1563 | } |
1564 | ||
633c9a84 PNA |
1565 | static int nf_tables_delchain(struct net *net, struct sock *nlsk, |
1566 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
96518518 PM |
1567 | const struct nlattr * const nla[]) |
1568 | { | |
1569 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 1570 | u8 genmask = nft_genmask_next(net); |
7c95f6d8 | 1571 | struct nft_af_info *afi; |
96518518 PM |
1572 | struct nft_table *table; |
1573 | struct nft_chain *chain; | |
1574 | int family = nfmsg->nfgen_family; | |
91c7b38d | 1575 | struct nft_ctx ctx; |
96518518 | 1576 | |
99633ab2 | 1577 | afi = nf_tables_afinfo_lookup(net, family, false); |
96518518 PM |
1578 | if (IS_ERR(afi)) |
1579 | return PTR_ERR(afi); | |
1580 | ||
f2a6d766 | 1581 | table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask); |
96518518 PM |
1582 | if (IS_ERR(table)) |
1583 | return PTR_ERR(table); | |
1584 | ||
664b0f8c | 1585 | chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask); |
96518518 PM |
1586 | if (IS_ERR(chain)) |
1587 | return PTR_ERR(chain); | |
4fefee57 | 1588 | if (chain->use > 0) |
96518518 PM |
1589 | return -EBUSY; |
1590 | ||
633c9a84 | 1591 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
0165d932 | 1592 | |
ee01d542 | 1593 | return nft_delchain(&ctx); |
96518518 PM |
1594 | } |
1595 | ||
96518518 PM |
1596 | /* |
1597 | * Expressions | |
1598 | */ | |
1599 | ||
1600 | /** | |
ef1f7df9 PM |
1601 | * nft_register_expr - register nf_tables expr type |
1602 | * @ops: expr type | |
96518518 | 1603 | * |
ef1f7df9 | 1604 | * Registers the expr type for use with nf_tables. Returns zero on |
96518518 PM |
1605 | * success or a negative errno code otherwise. |
1606 | */ | |
ef1f7df9 | 1607 | int nft_register_expr(struct nft_expr_type *type) |
96518518 PM |
1608 | { |
1609 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
758dbcec | 1610 | if (type->family == NFPROTO_UNSPEC) |
e688a7f8 | 1611 | list_add_tail_rcu(&type->list, &nf_tables_expressions); |
758dbcec | 1612 | else |
e688a7f8 | 1613 | list_add_rcu(&type->list, &nf_tables_expressions); |
96518518 PM |
1614 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1615 | return 0; | |
1616 | } | |
1617 | EXPORT_SYMBOL_GPL(nft_register_expr); | |
1618 | ||
1619 | /** | |
ef1f7df9 PM |
1620 | * nft_unregister_expr - unregister nf_tables expr type |
1621 | * @ops: expr type | |
96518518 | 1622 | * |
ef1f7df9 | 1623 | * Unregisters the expr typefor use with nf_tables. |
96518518 | 1624 | */ |
ef1f7df9 | 1625 | void nft_unregister_expr(struct nft_expr_type *type) |
96518518 PM |
1626 | { |
1627 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 1628 | list_del_rcu(&type->list); |
96518518 PM |
1629 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1630 | } | |
1631 | EXPORT_SYMBOL_GPL(nft_unregister_expr); | |
1632 | ||
64d46806 PM |
1633 | static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
1634 | struct nlattr *nla) | |
96518518 | 1635 | { |
ef1f7df9 | 1636 | const struct nft_expr_type *type; |
96518518 | 1637 | |
ef1f7df9 | 1638 | list_for_each_entry(type, &nf_tables_expressions, list) { |
64d46806 PM |
1639 | if (!nla_strcmp(nla, type->name) && |
1640 | (!type->family || type->family == family)) | |
ef1f7df9 | 1641 | return type; |
96518518 PM |
1642 | } |
1643 | return NULL; | |
1644 | } | |
1645 | ||
64d46806 PM |
1646 | static const struct nft_expr_type *nft_expr_type_get(u8 family, |
1647 | struct nlattr *nla) | |
96518518 | 1648 | { |
ef1f7df9 | 1649 | const struct nft_expr_type *type; |
96518518 PM |
1650 | |
1651 | if (nla == NULL) | |
1652 | return ERR_PTR(-EINVAL); | |
1653 | ||
64d46806 | 1654 | type = __nft_expr_type_get(family, nla); |
ef1f7df9 PM |
1655 | if (type != NULL && try_module_get(type->owner)) |
1656 | return type; | |
96518518 PM |
1657 | |
1658 | #ifdef CONFIG_MODULES | |
ef1f7df9 | 1659 | if (type == NULL) { |
64d46806 PM |
1660 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1661 | request_module("nft-expr-%u-%.*s", family, | |
1662 | nla_len(nla), (char *)nla_data(nla)); | |
1663 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
1664 | if (__nft_expr_type_get(family, nla)) | |
1665 | return ERR_PTR(-EAGAIN); | |
1666 | ||
96518518 PM |
1667 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1668 | request_module("nft-expr-%.*s", | |
1669 | nla_len(nla), (char *)nla_data(nla)); | |
1670 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
64d46806 | 1671 | if (__nft_expr_type_get(family, nla)) |
96518518 PM |
1672 | return ERR_PTR(-EAGAIN); |
1673 | } | |
1674 | #endif | |
1675 | return ERR_PTR(-ENOENT); | |
1676 | } | |
1677 | ||
1678 | static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { | |
1679 | [NFTA_EXPR_NAME] = { .type = NLA_STRING }, | |
1680 | [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, | |
1681 | }; | |
1682 | ||
1683 | static int nf_tables_fill_expr_info(struct sk_buff *skb, | |
1684 | const struct nft_expr *expr) | |
1685 | { | |
ef1f7df9 | 1686 | if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
96518518 PM |
1687 | goto nla_put_failure; |
1688 | ||
1689 | if (expr->ops->dump) { | |
1690 | struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA); | |
1691 | if (data == NULL) | |
1692 | goto nla_put_failure; | |
1693 | if (expr->ops->dump(skb, expr) < 0) | |
1694 | goto nla_put_failure; | |
1695 | nla_nest_end(skb, data); | |
1696 | } | |
1697 | ||
1698 | return skb->len; | |
1699 | ||
1700 | nla_put_failure: | |
1701 | return -1; | |
1702 | }; | |
1703 | ||
0b2d8a7b PM |
1704 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
1705 | const struct nft_expr *expr) | |
1706 | { | |
1707 | struct nlattr *nest; | |
1708 | ||
1709 | nest = nla_nest_start(skb, attr); | |
1710 | if (!nest) | |
1711 | goto nla_put_failure; | |
1712 | if (nf_tables_fill_expr_info(skb, expr) < 0) | |
1713 | goto nla_put_failure; | |
1714 | nla_nest_end(skb, nest); | |
1715 | return 0; | |
1716 | ||
1717 | nla_put_failure: | |
1718 | return -1; | |
1719 | } | |
1720 | ||
96518518 PM |
1721 | struct nft_expr_info { |
1722 | const struct nft_expr_ops *ops; | |
ef1f7df9 | 1723 | struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
96518518 PM |
1724 | }; |
1725 | ||
0ca743a5 PNA |
1726 | static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
1727 | const struct nlattr *nla, | |
96518518 PM |
1728 | struct nft_expr_info *info) |
1729 | { | |
ef1f7df9 | 1730 | const struct nft_expr_type *type; |
96518518 | 1731 | const struct nft_expr_ops *ops; |
ef1f7df9 | 1732 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
96518518 PM |
1733 | int err; |
1734 | ||
ef1f7df9 | 1735 | err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy); |
96518518 PM |
1736 | if (err < 0) |
1737 | return err; | |
1738 | ||
64d46806 | 1739 | type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]); |
ef1f7df9 PM |
1740 | if (IS_ERR(type)) |
1741 | return PTR_ERR(type); | |
1742 | ||
1743 | if (tb[NFTA_EXPR_DATA]) { | |
1744 | err = nla_parse_nested(info->tb, type->maxattr, | |
1745 | tb[NFTA_EXPR_DATA], type->policy); | |
1746 | if (err < 0) | |
1747 | goto err1; | |
1748 | } else | |
1749 | memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); | |
1750 | ||
1751 | if (type->select_ops != NULL) { | |
0ca743a5 PNA |
1752 | ops = type->select_ops(ctx, |
1753 | (const struct nlattr * const *)info->tb); | |
ef1f7df9 PM |
1754 | if (IS_ERR(ops)) { |
1755 | err = PTR_ERR(ops); | |
1756 | goto err1; | |
1757 | } | |
1758 | } else | |
1759 | ops = type->ops; | |
1760 | ||
96518518 PM |
1761 | info->ops = ops; |
1762 | return 0; | |
ef1f7df9 PM |
1763 | |
1764 | err1: | |
1765 | module_put(type->owner); | |
1766 | return err; | |
96518518 PM |
1767 | } |
1768 | ||
1769 | static int nf_tables_newexpr(const struct nft_ctx *ctx, | |
ef1f7df9 | 1770 | const struct nft_expr_info *info, |
96518518 PM |
1771 | struct nft_expr *expr) |
1772 | { | |
1773 | const struct nft_expr_ops *ops = info->ops; | |
1774 | int err; | |
1775 | ||
1776 | expr->ops = ops; | |
1777 | if (ops->init) { | |
ef1f7df9 | 1778 | err = ops->init(ctx, expr, (const struct nlattr **)info->tb); |
96518518 PM |
1779 | if (err < 0) |
1780 | goto err1; | |
1781 | } | |
1782 | ||
96518518 PM |
1783 | return 0; |
1784 | ||
1785 | err1: | |
1786 | expr->ops = NULL; | |
1787 | return err; | |
1788 | } | |
1789 | ||
62472bce PM |
1790 | static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
1791 | struct nft_expr *expr) | |
96518518 PM |
1792 | { |
1793 | if (expr->ops->destroy) | |
62472bce | 1794 | expr->ops->destroy(ctx, expr); |
ef1f7df9 | 1795 | module_put(expr->ops->type->owner); |
96518518 PM |
1796 | } |
1797 | ||
0b2d8a7b PM |
1798 | struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
1799 | const struct nlattr *nla) | |
1800 | { | |
1801 | struct nft_expr_info info; | |
1802 | struct nft_expr *expr; | |
1803 | int err; | |
1804 | ||
1805 | err = nf_tables_expr_parse(ctx, nla, &info); | |
1806 | if (err < 0) | |
1807 | goto err1; | |
1808 | ||
1809 | err = -ENOMEM; | |
1810 | expr = kzalloc(info.ops->size, GFP_KERNEL); | |
1811 | if (expr == NULL) | |
1812 | goto err2; | |
1813 | ||
1814 | err = nf_tables_newexpr(ctx, &info, expr); | |
1815 | if (err < 0) | |
6cafaf47 | 1816 | goto err3; |
0b2d8a7b PM |
1817 | |
1818 | return expr; | |
6cafaf47 LZ |
1819 | err3: |
1820 | kfree(expr); | |
0b2d8a7b PM |
1821 | err2: |
1822 | module_put(info.ops->type->owner); | |
1823 | err1: | |
1824 | return ERR_PTR(err); | |
1825 | } | |
1826 | ||
1827 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) | |
1828 | { | |
1829 | nf_tables_expr_destroy(ctx, expr); | |
1830 | kfree(expr); | |
1831 | } | |
1832 | ||
96518518 PM |
1833 | /* |
1834 | * Rules | |
1835 | */ | |
1836 | ||
1837 | static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain, | |
1838 | u64 handle) | |
1839 | { | |
1840 | struct nft_rule *rule; | |
1841 | ||
1842 | // FIXME: this sucks | |
1843 | list_for_each_entry(rule, &chain->rules, list) { | |
1844 | if (handle == rule->handle) | |
1845 | return rule; | |
1846 | } | |
1847 | ||
1848 | return ERR_PTR(-ENOENT); | |
1849 | } | |
1850 | ||
1851 | static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain, | |
1852 | const struct nlattr *nla) | |
1853 | { | |
1854 | if (nla == NULL) | |
1855 | return ERR_PTR(-EINVAL); | |
1856 | ||
1857 | return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); | |
1858 | } | |
1859 | ||
1860 | static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { | |
b2fbd044 LZ |
1861 | [NFTA_RULE_TABLE] = { .type = NLA_STRING, |
1862 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
1863 | [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
1864 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
1865 | [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, | |
1866 | [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, | |
0ca743a5 | 1867 | [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
5e948466 | 1868 | [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
0768b3b3 PNA |
1869 | [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
1870 | .len = NFT_USERDATA_MAXLEN }, | |
96518518 PM |
1871 | }; |
1872 | ||
84d7fce6 PNA |
1873 | static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
1874 | u32 portid, u32 seq, int event, | |
1875 | u32 flags, int family, | |
96518518 PM |
1876 | const struct nft_table *table, |
1877 | const struct nft_chain *chain, | |
1878 | const struct nft_rule *rule) | |
1879 | { | |
1880 | struct nlmsghdr *nlh; | |
1881 | struct nfgenmsg *nfmsg; | |
1882 | const struct nft_expr *expr, *next; | |
1883 | struct nlattr *list; | |
5e948466 EL |
1884 | const struct nft_rule *prule; |
1885 | int type = event | NFNL_SUBSYS_NFTABLES << 8; | |
96518518 | 1886 | |
5e948466 | 1887 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), |
96518518 PM |
1888 | flags); |
1889 | if (nlh == NULL) | |
1890 | goto nla_put_failure; | |
1891 | ||
1892 | nfmsg = nlmsg_data(nlh); | |
1893 | nfmsg->nfgen_family = family; | |
1894 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 1895 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
96518518 PM |
1896 | |
1897 | if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) | |
1898 | goto nla_put_failure; | |
1899 | if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) | |
1900 | goto nla_put_failure; | |
b46f6ded ND |
1901 | if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle), |
1902 | NFTA_RULE_PAD)) | |
96518518 PM |
1903 | goto nla_put_failure; |
1904 | ||
5e948466 EL |
1905 | if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) { |
1906 | prule = list_entry(rule->list.prev, struct nft_rule, list); | |
1907 | if (nla_put_be64(skb, NFTA_RULE_POSITION, | |
b46f6ded ND |
1908 | cpu_to_be64(prule->handle), |
1909 | NFTA_RULE_PAD)) | |
5e948466 EL |
1910 | goto nla_put_failure; |
1911 | } | |
1912 | ||
96518518 PM |
1913 | list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS); |
1914 | if (list == NULL) | |
1915 | goto nla_put_failure; | |
1916 | nft_rule_for_each_expr(expr, next, rule) { | |
0b2d8a7b | 1917 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0) |
96518518 | 1918 | goto nla_put_failure; |
96518518 PM |
1919 | } |
1920 | nla_nest_end(skb, list); | |
1921 | ||
86f1ec32 PM |
1922 | if (rule->udata) { |
1923 | struct nft_userdata *udata = nft_userdata(rule); | |
1924 | if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, | |
1925 | udata->data) < 0) | |
1926 | goto nla_put_failure; | |
1927 | } | |
0768b3b3 | 1928 | |
053c095a JB |
1929 | nlmsg_end(skb, nlh); |
1930 | return 0; | |
96518518 PM |
1931 | |
1932 | nla_put_failure: | |
1933 | nlmsg_trim(skb, nlh); | |
1934 | return -1; | |
1935 | } | |
1936 | ||
35151d84 | 1937 | static int nf_tables_rule_notify(const struct nft_ctx *ctx, |
96518518 | 1938 | const struct nft_rule *rule, |
35151d84 | 1939 | int event) |
96518518 PM |
1940 | { |
1941 | struct sk_buff *skb; | |
96518518 PM |
1942 | int err; |
1943 | ||
128ad332 PNA |
1944 | if (!ctx->report && |
1945 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
96518518 PM |
1946 | return 0; |
1947 | ||
1948 | err = -ENOBUFS; | |
1949 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1950 | if (skb == NULL) | |
1951 | goto err; | |
1952 | ||
84d7fce6 PNA |
1953 | err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
1954 | event, 0, ctx->afi->family, ctx->table, | |
35151d84 | 1955 | ctx->chain, rule); |
96518518 PM |
1956 | if (err < 0) { |
1957 | kfree_skb(skb); | |
1958 | goto err; | |
1959 | } | |
1960 | ||
128ad332 PNA |
1961 | err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
1962 | ctx->report, GFP_KERNEL); | |
96518518 | 1963 | err: |
128ad332 PNA |
1964 | if (err < 0) { |
1965 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, | |
1966 | err); | |
1967 | } | |
96518518 PM |
1968 | return err; |
1969 | } | |
1970 | ||
6e1f760e PNA |
1971 | struct nft_rule_dump_ctx { |
1972 | char table[NFT_TABLE_MAXNAMELEN]; | |
1973 | char chain[NFT_CHAIN_MAXNAMELEN]; | |
1974 | }; | |
1975 | ||
96518518 PM |
1976 | static int nf_tables_dump_rules(struct sk_buff *skb, |
1977 | struct netlink_callback *cb) | |
1978 | { | |
1979 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6e1f760e | 1980 | const struct nft_rule_dump_ctx *ctx = cb->data; |
96518518 PM |
1981 | const struct nft_af_info *afi; |
1982 | const struct nft_table *table; | |
1983 | const struct nft_chain *chain; | |
1984 | const struct nft_rule *rule; | |
1985 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 1986 | struct net *net = sock_net(skb->sk); |
96518518 PM |
1987 | int family = nfmsg->nfgen_family; |
1988 | ||
e688a7f8 | 1989 | rcu_read_lock(); |
38e029f1 PNA |
1990 | cb->seq = net->nft.base_seq; |
1991 | ||
e688a7f8 | 1992 | list_for_each_entry_rcu(afi, &net->nft.af_info, list) { |
96518518 PM |
1993 | if (family != NFPROTO_UNSPEC && family != afi->family) |
1994 | continue; | |
1995 | ||
e688a7f8 | 1996 | list_for_each_entry_rcu(table, &afi->tables, list) { |
6e1f760e PNA |
1997 | if (ctx && ctx->table[0] && |
1998 | strcmp(ctx->table, table->name) != 0) | |
1999 | continue; | |
2000 | ||
e688a7f8 | 2001 | list_for_each_entry_rcu(chain, &table->chains, list) { |
6e1f760e PNA |
2002 | if (ctx && ctx->chain[0] && |
2003 | strcmp(ctx->chain, chain->name) != 0) | |
2004 | continue; | |
2005 | ||
e688a7f8 | 2006 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
889f7ee7 | 2007 | if (!nft_is_active(net, rule)) |
0628b123 | 2008 | goto cont; |
96518518 PM |
2009 | if (idx < s_idx) |
2010 | goto cont; | |
2011 | if (idx > s_idx) | |
2012 | memset(&cb->args[1], 0, | |
2013 | sizeof(cb->args) - sizeof(cb->args[0])); | |
84d7fce6 | 2014 | if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, |
96518518 PM |
2015 | cb->nlh->nlmsg_seq, |
2016 | NFT_MSG_NEWRULE, | |
2017 | NLM_F_MULTI | NLM_F_APPEND, | |
2018 | afi->family, table, chain, rule) < 0) | |
2019 | goto done; | |
38e029f1 PNA |
2020 | |
2021 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 PM |
2022 | cont: |
2023 | idx++; | |
2024 | } | |
2025 | } | |
2026 | } | |
2027 | } | |
2028 | done: | |
e688a7f8 PNA |
2029 | rcu_read_unlock(); |
2030 | ||
96518518 PM |
2031 | cb->args[0] = idx; |
2032 | return skb->len; | |
2033 | } | |
2034 | ||
6e1f760e PNA |
2035 | static int nf_tables_dump_rules_done(struct netlink_callback *cb) |
2036 | { | |
2037 | kfree(cb->data); | |
2038 | return 0; | |
2039 | } | |
2040 | ||
7b8002a1 PNA |
2041 | static int nf_tables_getrule(struct net *net, struct sock *nlsk, |
2042 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
96518518 PM |
2043 | const struct nlattr * const nla[]) |
2044 | { | |
2045 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 2046 | u8 genmask = nft_genmask_cur(net); |
96518518 PM |
2047 | const struct nft_af_info *afi; |
2048 | const struct nft_table *table; | |
2049 | const struct nft_chain *chain; | |
2050 | const struct nft_rule *rule; | |
2051 | struct sk_buff *skb2; | |
2052 | int family = nfmsg->nfgen_family; | |
2053 | int err; | |
2054 | ||
2055 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
2056 | struct netlink_dump_control c = { | |
2057 | .dump = nf_tables_dump_rules, | |
6e1f760e | 2058 | .done = nf_tables_dump_rules_done, |
96518518 | 2059 | }; |
6e1f760e PNA |
2060 | |
2061 | if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) { | |
2062 | struct nft_rule_dump_ctx *ctx; | |
2063 | ||
2064 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
2065 | if (!ctx) | |
2066 | return -ENOMEM; | |
2067 | ||
2068 | if (nla[NFTA_RULE_TABLE]) | |
2069 | nla_strlcpy(ctx->table, nla[NFTA_RULE_TABLE], | |
2070 | sizeof(ctx->table)); | |
2071 | if (nla[NFTA_RULE_CHAIN]) | |
2072 | nla_strlcpy(ctx->chain, nla[NFTA_RULE_CHAIN], | |
2073 | sizeof(ctx->chain)); | |
2074 | c.data = ctx; | |
2075 | } | |
2076 | ||
96518518 PM |
2077 | return netlink_dump_start(nlsk, skb, nlh, &c); |
2078 | } | |
2079 | ||
99633ab2 | 2080 | afi = nf_tables_afinfo_lookup(net, family, false); |
96518518 PM |
2081 | if (IS_ERR(afi)) |
2082 | return PTR_ERR(afi); | |
2083 | ||
f2a6d766 | 2084 | table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask); |
96518518 PM |
2085 | if (IS_ERR(table)) |
2086 | return PTR_ERR(table); | |
2087 | ||
664b0f8c | 2088 | chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask); |
96518518 PM |
2089 | if (IS_ERR(chain)) |
2090 | return PTR_ERR(chain); | |
2091 | ||
2092 | rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); | |
2093 | if (IS_ERR(rule)) | |
2094 | return PTR_ERR(rule); | |
2095 | ||
2096 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
2097 | if (!skb2) | |
2098 | return -ENOMEM; | |
2099 | ||
84d7fce6 | 2100 | err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
96518518 PM |
2101 | nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
2102 | family, table, chain, rule); | |
2103 | if (err < 0) | |
2104 | goto err; | |
2105 | ||
2106 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
2107 | ||
2108 | err: | |
2109 | kfree_skb(skb2); | |
2110 | return err; | |
2111 | } | |
2112 | ||
62472bce PM |
2113 | static void nf_tables_rule_destroy(const struct nft_ctx *ctx, |
2114 | struct nft_rule *rule) | |
96518518 | 2115 | { |
96518518 PM |
2116 | struct nft_expr *expr; |
2117 | ||
2118 | /* | |
2119 | * Careful: some expressions might not be initialized in case this | |
2120 | * is called on error from nf_tables_newrule(). | |
2121 | */ | |
2122 | expr = nft_expr_first(rule); | |
3e38df13 | 2123 | while (expr != nft_expr_last(rule) && expr->ops) { |
62472bce | 2124 | nf_tables_expr_destroy(ctx, expr); |
96518518 PM |
2125 | expr = nft_expr_next(expr); |
2126 | } | |
2127 | kfree(rule); | |
2128 | } | |
2129 | ||
1081d11b PNA |
2130 | #define NFT_RULE_MAXEXPRS 128 |
2131 | ||
2132 | static struct nft_expr_info *info; | |
2133 | ||
633c9a84 PNA |
2134 | static int nf_tables_newrule(struct net *net, struct sock *nlsk, |
2135 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
96518518 PM |
2136 | const struct nlattr * const nla[]) |
2137 | { | |
2138 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 2139 | u8 genmask = nft_genmask_next(net); |
7c95f6d8 | 2140 | struct nft_af_info *afi; |
96518518 PM |
2141 | struct nft_table *table; |
2142 | struct nft_chain *chain; | |
2143 | struct nft_rule *rule, *old_rule = NULL; | |
86f1ec32 | 2144 | struct nft_userdata *udata; |
1081d11b | 2145 | struct nft_trans *trans = NULL; |
96518518 PM |
2146 | struct nft_expr *expr; |
2147 | struct nft_ctx ctx; | |
2148 | struct nlattr *tmp; | |
86f1ec32 | 2149 | unsigned int size, i, n, ulen = 0, usize = 0; |
96518518 PM |
2150 | int err, rem; |
2151 | bool create; | |
5e948466 | 2152 | u64 handle, pos_handle; |
96518518 PM |
2153 | |
2154 | create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false; | |
2155 | ||
99633ab2 | 2156 | afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create); |
96518518 PM |
2157 | if (IS_ERR(afi)) |
2158 | return PTR_ERR(afi); | |
2159 | ||
f2a6d766 | 2160 | table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask); |
96518518 PM |
2161 | if (IS_ERR(table)) |
2162 | return PTR_ERR(table); | |
2163 | ||
664b0f8c | 2164 | chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask); |
96518518 PM |
2165 | if (IS_ERR(chain)) |
2166 | return PTR_ERR(chain); | |
2167 | ||
2168 | if (nla[NFTA_RULE_HANDLE]) { | |
2169 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); | |
2170 | rule = __nf_tables_rule_lookup(chain, handle); | |
2171 | if (IS_ERR(rule)) | |
2172 | return PTR_ERR(rule); | |
2173 | ||
2174 | if (nlh->nlmsg_flags & NLM_F_EXCL) | |
2175 | return -EEXIST; | |
2176 | if (nlh->nlmsg_flags & NLM_F_REPLACE) | |
2177 | old_rule = rule; | |
2178 | else | |
2179 | return -EOPNOTSUPP; | |
2180 | } else { | |
2181 | if (!create || nlh->nlmsg_flags & NLM_F_REPLACE) | |
2182 | return -EINVAL; | |
2183 | handle = nf_tables_alloc_handle(table); | |
a0a7379e PNA |
2184 | |
2185 | if (chain->use == UINT_MAX) | |
2186 | return -EOVERFLOW; | |
96518518 PM |
2187 | } |
2188 | ||
5e948466 EL |
2189 | if (nla[NFTA_RULE_POSITION]) { |
2190 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) | |
2191 | return -EOPNOTSUPP; | |
2192 | ||
2193 | pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); | |
2194 | old_rule = __nf_tables_rule_lookup(chain, pos_handle); | |
2195 | if (IS_ERR(old_rule)) | |
2196 | return PTR_ERR(old_rule); | |
2197 | } | |
2198 | ||
633c9a84 | 2199 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
0ca743a5 | 2200 | |
96518518 PM |
2201 | n = 0; |
2202 | size = 0; | |
2203 | if (nla[NFTA_RULE_EXPRESSIONS]) { | |
2204 | nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { | |
2205 | err = -EINVAL; | |
2206 | if (nla_type(tmp) != NFTA_LIST_ELEM) | |
2207 | goto err1; | |
2208 | if (n == NFT_RULE_MAXEXPRS) | |
2209 | goto err1; | |
0ca743a5 | 2210 | err = nf_tables_expr_parse(&ctx, tmp, &info[n]); |
96518518 PM |
2211 | if (err < 0) |
2212 | goto err1; | |
2213 | size += info[n].ops->size; | |
2214 | n++; | |
2215 | } | |
2216 | } | |
9889840f PM |
2217 | /* Check for overflow of dlen field */ |
2218 | err = -EFBIG; | |
2219 | if (size >= 1 << 12) | |
2220 | goto err1; | |
96518518 | 2221 | |
86f1ec32 | 2222 | if (nla[NFTA_RULE_USERDATA]) { |
0768b3b3 | 2223 | ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
86f1ec32 PM |
2224 | if (ulen > 0) |
2225 | usize = sizeof(struct nft_userdata) + ulen; | |
2226 | } | |
0768b3b3 | 2227 | |
96518518 | 2228 | err = -ENOMEM; |
86f1ec32 | 2229 | rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL); |
96518518 PM |
2230 | if (rule == NULL) |
2231 | goto err1; | |
2232 | ||
889f7ee7 | 2233 | nft_activate_next(net, rule); |
0628b123 | 2234 | |
96518518 PM |
2235 | rule->handle = handle; |
2236 | rule->dlen = size; | |
86f1ec32 | 2237 | rule->udata = ulen ? 1 : 0; |
0768b3b3 | 2238 | |
86f1ec32 PM |
2239 | if (ulen) { |
2240 | udata = nft_userdata(rule); | |
2241 | udata->len = ulen - 1; | |
2242 | nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); | |
2243 | } | |
96518518 | 2244 | |
96518518 PM |
2245 | expr = nft_expr_first(rule); |
2246 | for (i = 0; i < n; i++) { | |
2247 | err = nf_tables_newexpr(&ctx, &info[i], expr); | |
2248 | if (err < 0) | |
2249 | goto err2; | |
ef1f7df9 | 2250 | info[i].ops = NULL; |
96518518 PM |
2251 | expr = nft_expr_next(expr); |
2252 | } | |
2253 | ||
96518518 | 2254 | if (nlh->nlmsg_flags & NLM_F_REPLACE) { |
889f7ee7 | 2255 | if (nft_is_active_next(net, old_rule)) { |
ac904ac8 | 2256 | trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE, |
b380e5c7 | 2257 | old_rule); |
1081d11b | 2258 | if (trans == NULL) { |
0628b123 PNA |
2259 | err = -ENOMEM; |
2260 | goto err2; | |
2261 | } | |
889f7ee7 | 2262 | nft_deactivate_next(net, old_rule); |
ac34b861 | 2263 | chain->use--; |
5bc5c307 | 2264 | list_add_tail_rcu(&rule->list, &old_rule->list); |
0628b123 PNA |
2265 | } else { |
2266 | err = -ENOENT; | |
2267 | goto err2; | |
2268 | } | |
96518518 | 2269 | } else if (nlh->nlmsg_flags & NLM_F_APPEND) |
5e948466 EL |
2270 | if (old_rule) |
2271 | list_add_rcu(&rule->list, &old_rule->list); | |
2272 | else | |
2273 | list_add_tail_rcu(&rule->list, &chain->rules); | |
2274 | else { | |
2275 | if (old_rule) | |
2276 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
2277 | else | |
2278 | list_add_rcu(&rule->list, &chain->rules); | |
2279 | } | |
96518518 | 2280 | |
b380e5c7 | 2281 | if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) { |
0628b123 PNA |
2282 | err = -ENOMEM; |
2283 | goto err3; | |
2284 | } | |
4fefee57 | 2285 | chain->use++; |
96518518 PM |
2286 | return 0; |
2287 | ||
0628b123 PNA |
2288 | err3: |
2289 | list_del_rcu(&rule->list); | |
96518518 | 2290 | err2: |
62472bce | 2291 | nf_tables_rule_destroy(&ctx, rule); |
96518518 PM |
2292 | err1: |
2293 | for (i = 0; i < n; i++) { | |
2294 | if (info[i].ops != NULL) | |
ef1f7df9 | 2295 | module_put(info[i].ops->type->owner); |
96518518 PM |
2296 | } |
2297 | return err; | |
2298 | } | |
2299 | ||
1a94e38d PNA |
2300 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
2301 | const struct nlattr *nla) | |
2302 | { | |
2303 | u32 id = ntohl(nla_get_be32(nla)); | |
2304 | struct nft_trans *trans; | |
2305 | ||
2306 | list_for_each_entry(trans, &net->nft.commit_list, list) { | |
2307 | struct nft_rule *rule = nft_trans_rule(trans); | |
2308 | ||
2309 | if (trans->msg_type == NFT_MSG_NEWRULE && | |
2310 | id == nft_trans_rule_id(trans)) | |
2311 | return rule; | |
2312 | } | |
2313 | return ERR_PTR(-ENOENT); | |
2314 | } | |
2315 | ||
633c9a84 PNA |
2316 | static int nf_tables_delrule(struct net *net, struct sock *nlsk, |
2317 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
96518518 PM |
2318 | const struct nlattr * const nla[]) |
2319 | { | |
2320 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 2321 | u8 genmask = nft_genmask_next(net); |
7c95f6d8 | 2322 | struct nft_af_info *afi; |
7c95f6d8 | 2323 | struct nft_table *table; |
cf9dc09d PNA |
2324 | struct nft_chain *chain = NULL; |
2325 | struct nft_rule *rule; | |
0628b123 PNA |
2326 | int family = nfmsg->nfgen_family, err = 0; |
2327 | struct nft_ctx ctx; | |
96518518 | 2328 | |
99633ab2 | 2329 | afi = nf_tables_afinfo_lookup(net, family, false); |
96518518 PM |
2330 | if (IS_ERR(afi)) |
2331 | return PTR_ERR(afi); | |
2332 | ||
f2a6d766 | 2333 | table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask); |
96518518 PM |
2334 | if (IS_ERR(table)) |
2335 | return PTR_ERR(table); | |
2336 | ||
cf9dc09d | 2337 | if (nla[NFTA_RULE_CHAIN]) { |
664b0f8c PNA |
2338 | chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN], |
2339 | genmask); | |
cf9dc09d PNA |
2340 | if (IS_ERR(chain)) |
2341 | return PTR_ERR(chain); | |
2342 | } | |
96518518 | 2343 | |
633c9a84 | 2344 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla); |
0628b123 | 2345 | |
cf9dc09d PNA |
2346 | if (chain) { |
2347 | if (nla[NFTA_RULE_HANDLE]) { | |
2348 | rule = nf_tables_rule_lookup(chain, | |
2349 | nla[NFTA_RULE_HANDLE]); | |
2350 | if (IS_ERR(rule)) | |
2351 | return PTR_ERR(rule); | |
96518518 | 2352 | |
1a94e38d PNA |
2353 | err = nft_delrule(&ctx, rule); |
2354 | } else if (nla[NFTA_RULE_ID]) { | |
2355 | rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_ID]); | |
2356 | if (IS_ERR(rule)) | |
2357 | return PTR_ERR(rule); | |
2358 | ||
5e266fe7 | 2359 | err = nft_delrule(&ctx, rule); |
cf9dc09d | 2360 | } else { |
ce24b721 | 2361 | err = nft_delrule_by_chain(&ctx); |
cf9dc09d PNA |
2362 | } |
2363 | } else { | |
2364 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
2365 | if (!nft_is_active_next(net, chain)) |
2366 | continue; | |
2367 | ||
cf9dc09d | 2368 | ctx.chain = chain; |
ce24b721 | 2369 | err = nft_delrule_by_chain(&ctx); |
0628b123 PNA |
2370 | if (err < 0) |
2371 | break; | |
2372 | } | |
2373 | } | |
2374 | ||
2375 | return err; | |
2376 | } | |
2377 | ||
20a69341 PM |
2378 | /* |
2379 | * Sets | |
2380 | */ | |
2381 | ||
2382 | static LIST_HEAD(nf_tables_set_ops); | |
2383 | ||
2384 | int nft_register_set(struct nft_set_ops *ops) | |
2385 | { | |
2386 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 2387 | list_add_tail_rcu(&ops->list, &nf_tables_set_ops); |
20a69341 PM |
2388 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2389 | return 0; | |
2390 | } | |
2391 | EXPORT_SYMBOL_GPL(nft_register_set); | |
2392 | ||
2393 | void nft_unregister_set(struct nft_set_ops *ops) | |
2394 | { | |
2395 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 2396 | list_del_rcu(&ops->list); |
20a69341 PM |
2397 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2398 | } | |
2399 | EXPORT_SYMBOL_GPL(nft_unregister_set); | |
2400 | ||
c50b960c PM |
2401 | /* |
2402 | * Select a set implementation based on the data characteristics and the | |
2403 | * given policy. The total memory use might not be known if no size is | |
2404 | * given, in that case the amount of memory per element is used. | |
2405 | */ | |
2406 | static const struct nft_set_ops * | |
2407 | nft_select_set_ops(const struct nlattr * const nla[], | |
2408 | const struct nft_set_desc *desc, | |
2409 | enum nft_set_policies policy) | |
20a69341 | 2410 | { |
c50b960c PM |
2411 | const struct nft_set_ops *ops, *bops; |
2412 | struct nft_set_estimate est, best; | |
20a69341 PM |
2413 | u32 features; |
2414 | ||
2415 | #ifdef CONFIG_MODULES | |
2416 | if (list_empty(&nf_tables_set_ops)) { | |
2417 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
2418 | request_module("nft-set"); | |
2419 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
2420 | if (!list_empty(&nf_tables_set_ops)) | |
2421 | return ERR_PTR(-EAGAIN); | |
2422 | } | |
2423 | #endif | |
2424 | features = 0; | |
2425 | if (nla[NFTA_SET_FLAGS] != NULL) { | |
2426 | features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
7286ff7f PNA |
2427 | features &= NFT_SET_INTERVAL | NFT_SET_MAP | NFT_SET_TIMEOUT | |
2428 | NFT_SET_OBJECT; | |
20a69341 PM |
2429 | } |
2430 | ||
55af753c PNA |
2431 | bops = NULL; |
2432 | best.size = ~0; | |
2433 | best.lookup = ~0; | |
0b5a7874 | 2434 | best.space = ~0; |
c50b960c | 2435 | |
20a69341 PM |
2436 | list_for_each_entry(ops, &nf_tables_set_ops, list) { |
2437 | if ((ops->features & features) != features) | |
2438 | continue; | |
c50b960c PM |
2439 | if (!ops->estimate(desc, features, &est)) |
2440 | continue; | |
2441 | ||
2442 | switch (policy) { | |
2443 | case NFT_SET_POL_PERFORMANCE: | |
55af753c | 2444 | if (est.lookup < best.lookup) |
c50b960c | 2445 | break; |
0b5a7874 PNA |
2446 | if (est.lookup == best.lookup) { |
2447 | if (!desc->size) { | |
2448 | if (est.space < best.space) | |
2449 | break; | |
2450 | } else if (est.size < best.size) { | |
2451 | break; | |
2452 | } | |
2453 | } | |
c50b960c PM |
2454 | continue; |
2455 | case NFT_SET_POL_MEMORY: | |
0b5a7874 PNA |
2456 | if (!desc->size) { |
2457 | if (est.space < best.space) | |
2458 | break; | |
2459 | if (est.space == best.space && | |
2460 | est.lookup < best.lookup) | |
2461 | break; | |
2462 | } else if (est.size < best.size) { | |
c50b960c | 2463 | break; |
0b5a7874 | 2464 | } |
c50b960c PM |
2465 | continue; |
2466 | default: | |
2467 | break; | |
2468 | } | |
2469 | ||
20a69341 PM |
2470 | if (!try_module_get(ops->owner)) |
2471 | continue; | |
c50b960c PM |
2472 | if (bops != NULL) |
2473 | module_put(bops->owner); | |
2474 | ||
2475 | bops = ops; | |
2476 | best = est; | |
20a69341 PM |
2477 | } |
2478 | ||
c50b960c PM |
2479 | if (bops != NULL) |
2480 | return bops; | |
2481 | ||
20a69341 PM |
2482 | return ERR_PTR(-EOPNOTSUPP); |
2483 | } | |
2484 | ||
2485 | static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { | |
b2fbd044 LZ |
2486 | [NFTA_SET_TABLE] = { .type = NLA_STRING, |
2487 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
a9bdd836 | 2488 | [NFTA_SET_NAME] = { .type = NLA_STRING, |
cb39ad8b | 2489 | .len = NFT_SET_MAXNAMELEN - 1 }, |
20a69341 PM |
2490 | [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
2491 | [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, | |
2492 | [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, | |
2493 | [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, | |
2494 | [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, | |
c50b960c PM |
2495 | [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
2496 | [NFTA_SET_DESC] = { .type = NLA_NESTED }, | |
958bee14 | 2497 | [NFTA_SET_ID] = { .type = NLA_U32 }, |
761da293 PM |
2498 | [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
2499 | [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, | |
e6d8ecac CFG |
2500 | [NFTA_SET_USERDATA] = { .type = NLA_BINARY, |
2501 | .len = NFT_USERDATA_MAXLEN }, | |
8aeff920 | 2502 | [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 }, |
c50b960c PM |
2503 | }; |
2504 | ||
2505 | static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { | |
2506 | [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, | |
20a69341 PM |
2507 | }; |
2508 | ||
633c9a84 | 2509 | static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net, |
20a69341 PM |
2510 | const struct sk_buff *skb, |
2511 | const struct nlmsghdr *nlh, | |
f2a6d766 PNA |
2512 | const struct nlattr * const nla[], |
2513 | u8 genmask) | |
20a69341 PM |
2514 | { |
2515 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
7c95f6d8 PNA |
2516 | struct nft_af_info *afi = NULL; |
2517 | struct nft_table *table = NULL; | |
20a69341 | 2518 | |
c9c8e485 PNA |
2519 | if (nfmsg->nfgen_family != NFPROTO_UNSPEC) { |
2520 | afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false); | |
2521 | if (IS_ERR(afi)) | |
2522 | return PTR_ERR(afi); | |
2523 | } | |
20a69341 PM |
2524 | |
2525 | if (nla[NFTA_SET_TABLE] != NULL) { | |
ec2c9935 PM |
2526 | if (afi == NULL) |
2527 | return -EAFNOSUPPORT; | |
2528 | ||
f2a6d766 PNA |
2529 | table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE], |
2530 | genmask); | |
20a69341 PM |
2531 | if (IS_ERR(table)) |
2532 | return PTR_ERR(table); | |
2533 | } | |
2534 | ||
633c9a84 | 2535 | nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla); |
20a69341 PM |
2536 | return 0; |
2537 | } | |
2538 | ||
2539 | struct nft_set *nf_tables_set_lookup(const struct nft_table *table, | |
37a9cc52 | 2540 | const struct nlattr *nla, u8 genmask) |
20a69341 PM |
2541 | { |
2542 | struct nft_set *set; | |
2543 | ||
2544 | if (nla == NULL) | |
2545 | return ERR_PTR(-EINVAL); | |
2546 | ||
2547 | list_for_each_entry(set, &table->sets, list) { | |
37a9cc52 PNA |
2548 | if (!nla_strcmp(nla, set->name) && |
2549 | nft_active_genmask(set, genmask)) | |
20a69341 PM |
2550 | return set; |
2551 | } | |
2552 | return ERR_PTR(-ENOENT); | |
2553 | } | |
63aea290 | 2554 | EXPORT_SYMBOL_GPL(nf_tables_set_lookup); |
20a69341 | 2555 | |
958bee14 | 2556 | struct nft_set *nf_tables_set_lookup_byid(const struct net *net, |
37a9cc52 PNA |
2557 | const struct nlattr *nla, |
2558 | u8 genmask) | |
958bee14 PNA |
2559 | { |
2560 | struct nft_trans *trans; | |
2561 | u32 id = ntohl(nla_get_be32(nla)); | |
2562 | ||
2563 | list_for_each_entry(trans, &net->nft.commit_list, list) { | |
37a9cc52 PNA |
2564 | struct nft_set *set = nft_trans_set(trans); |
2565 | ||
958bee14 | 2566 | if (trans->msg_type == NFT_MSG_NEWSET && |
37a9cc52 PNA |
2567 | id == nft_trans_set_id(trans) && |
2568 | nft_active_genmask(set, genmask)) | |
2569 | return set; | |
958bee14 PNA |
2570 | } |
2571 | return ERR_PTR(-ENOENT); | |
2572 | } | |
63aea290 | 2573 | EXPORT_SYMBOL_GPL(nf_tables_set_lookup_byid); |
958bee14 | 2574 | |
20a69341 PM |
2575 | static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
2576 | const char *name) | |
2577 | { | |
2578 | const struct nft_set *i; | |
2579 | const char *p; | |
2580 | unsigned long *inuse; | |
60eb1894 | 2581 | unsigned int n = 0, min = 0; |
20a69341 | 2582 | |
cb39ad8b | 2583 | p = strnchr(name, NFT_SET_MAXNAMELEN, '%'); |
20a69341 PM |
2584 | if (p != NULL) { |
2585 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
2586 | return -EINVAL; | |
2587 | ||
2588 | inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); | |
2589 | if (inuse == NULL) | |
2590 | return -ENOMEM; | |
60eb1894 | 2591 | cont: |
20a69341 | 2592 | list_for_each_entry(i, &ctx->table->sets, list) { |
14662917 DB |
2593 | int tmp; |
2594 | ||
37a9cc52 PNA |
2595 | if (!nft_is_active_next(ctx->net, set)) |
2596 | continue; | |
14662917 | 2597 | if (!sscanf(i->name, name, &tmp)) |
20a69341 | 2598 | continue; |
60eb1894 | 2599 | if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
20a69341 | 2600 | continue; |
14662917 | 2601 | |
60eb1894 | 2602 | set_bit(tmp - min, inuse); |
20a69341 PM |
2603 | } |
2604 | ||
53b70287 | 2605 | n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
60eb1894 PM |
2606 | if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
2607 | min += BITS_PER_BYTE * PAGE_SIZE; | |
2608 | memset(inuse, 0, PAGE_SIZE); | |
2609 | goto cont; | |
2610 | } | |
20a69341 PM |
2611 | free_page((unsigned long)inuse); |
2612 | } | |
2613 | ||
60eb1894 | 2614 | snprintf(set->name, sizeof(set->name), name, min + n); |
20a69341 | 2615 | list_for_each_entry(i, &ctx->table->sets, list) { |
37a9cc52 PNA |
2616 | if (!nft_is_active_next(ctx->net, i)) |
2617 | continue; | |
20a69341 PM |
2618 | if (!strcmp(set->name, i->name)) |
2619 | return -ENFILE; | |
2620 | } | |
2621 | return 0; | |
2622 | } | |
2623 | ||
2624 | static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, | |
2625 | const struct nft_set *set, u16 event, u16 flags) | |
2626 | { | |
2627 | struct nfgenmsg *nfmsg; | |
2628 | struct nlmsghdr *nlh; | |
c50b960c | 2629 | struct nlattr *desc; |
128ad332 PNA |
2630 | u32 portid = ctx->portid; |
2631 | u32 seq = ctx->seq; | |
20a69341 PM |
2632 | |
2633 | event |= NFNL_SUBSYS_NFTABLES << 8; | |
2634 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), | |
2635 | flags); | |
2636 | if (nlh == NULL) | |
2637 | goto nla_put_failure; | |
2638 | ||
2639 | nfmsg = nlmsg_data(nlh); | |
2640 | nfmsg->nfgen_family = ctx->afi->family; | |
2641 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 2642 | nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff); |
20a69341 PM |
2643 | |
2644 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) | |
2645 | goto nla_put_failure; | |
2646 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
2647 | goto nla_put_failure; | |
2648 | if (set->flags != 0) | |
2649 | if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) | |
2650 | goto nla_put_failure; | |
2651 | ||
2652 | if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) | |
2653 | goto nla_put_failure; | |
2654 | if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) | |
2655 | goto nla_put_failure; | |
2656 | if (set->flags & NFT_SET_MAP) { | |
2657 | if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) | |
2658 | goto nla_put_failure; | |
2659 | if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) | |
2660 | goto nla_put_failure; | |
2661 | } | |
8aeff920 PNA |
2662 | if (set->flags & NFT_SET_OBJECT && |
2663 | nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype))) | |
2664 | goto nla_put_failure; | |
20a69341 | 2665 | |
761da293 | 2666 | if (set->timeout && |
d3e2a111 AP |
2667 | nla_put_be64(skb, NFTA_SET_TIMEOUT, |
2668 | cpu_to_be64(jiffies_to_msecs(set->timeout)), | |
b46f6ded | 2669 | NFTA_SET_PAD)) |
761da293 PM |
2670 | goto nla_put_failure; |
2671 | if (set->gc_int && | |
2672 | nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int))) | |
2673 | goto nla_put_failure; | |
2674 | ||
9363dc4b AB |
2675 | if (set->policy != NFT_SET_POL_PERFORMANCE) { |
2676 | if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) | |
2677 | goto nla_put_failure; | |
2678 | } | |
2679 | ||
e6d8ecac CFG |
2680 | if (nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata)) |
2681 | goto nla_put_failure; | |
2682 | ||
c50b960c PM |
2683 | desc = nla_nest_start(skb, NFTA_SET_DESC); |
2684 | if (desc == NULL) | |
2685 | goto nla_put_failure; | |
2686 | if (set->size && | |
2687 | nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) | |
2688 | goto nla_put_failure; | |
2689 | nla_nest_end(skb, desc); | |
2690 | ||
053c095a JB |
2691 | nlmsg_end(skb, nlh); |
2692 | return 0; | |
20a69341 PM |
2693 | |
2694 | nla_put_failure: | |
2695 | nlmsg_trim(skb, nlh); | |
2696 | return -1; | |
2697 | } | |
2698 | ||
2699 | static int nf_tables_set_notify(const struct nft_ctx *ctx, | |
2700 | const struct nft_set *set, | |
31f8441c | 2701 | int event, gfp_t gfp_flags) |
20a69341 PM |
2702 | { |
2703 | struct sk_buff *skb; | |
128ad332 | 2704 | u32 portid = ctx->portid; |
20a69341 PM |
2705 | int err; |
2706 | ||
128ad332 PNA |
2707 | if (!ctx->report && |
2708 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
20a69341 PM |
2709 | return 0; |
2710 | ||
2711 | err = -ENOBUFS; | |
31f8441c | 2712 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
20a69341 PM |
2713 | if (skb == NULL) |
2714 | goto err; | |
2715 | ||
2716 | err = nf_tables_fill_set(skb, ctx, set, event, 0); | |
2717 | if (err < 0) { | |
2718 | kfree_skb(skb); | |
2719 | goto err; | |
2720 | } | |
2721 | ||
128ad332 | 2722 | err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, |
31f8441c | 2723 | ctx->report, gfp_flags); |
20a69341 PM |
2724 | err: |
2725 | if (err < 0) | |
99633ab2 | 2726 | nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err); |
20a69341 PM |
2727 | return err; |
2728 | } | |
2729 | ||
5b96af77 | 2730 | static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
c9c8e485 PNA |
2731 | { |
2732 | const struct nft_set *set; | |
2733 | unsigned int idx, s_idx = cb->args[0]; | |
7c95f6d8 | 2734 | struct nft_af_info *afi; |
c9c8e485 PNA |
2735 | struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
2736 | struct net *net = sock_net(skb->sk); | |
2737 | int cur_family = cb->args[3]; | |
5b96af77 | 2738 | struct nft_ctx *ctx = cb->data, ctx_set; |
c9c8e485 PNA |
2739 | |
2740 | if (cb->args[1]) | |
2741 | return skb->len; | |
2742 | ||
e688a7f8 | 2743 | rcu_read_lock(); |
38e029f1 PNA |
2744 | cb->seq = net->nft.base_seq; |
2745 | ||
e688a7f8 | 2746 | list_for_each_entry_rcu(afi, &net->nft.af_info, list) { |
5b96af77 PNA |
2747 | if (ctx->afi && ctx->afi != afi) |
2748 | continue; | |
2749 | ||
c9c8e485 PNA |
2750 | if (cur_family) { |
2751 | if (afi->family != cur_family) | |
2752 | continue; | |
2753 | ||
2754 | cur_family = 0; | |
2755 | } | |
e688a7f8 | 2756 | list_for_each_entry_rcu(table, &afi->tables, list) { |
5b96af77 PNA |
2757 | if (ctx->table && ctx->table != table) |
2758 | continue; | |
2759 | ||
c9c8e485 PNA |
2760 | if (cur_table) { |
2761 | if (cur_table != table) | |
2762 | continue; | |
2763 | ||
2764 | cur_table = NULL; | |
2765 | } | |
c9c8e485 | 2766 | idx = 0; |
5b96af77 | 2767 | list_for_each_entry_rcu(set, &table->sets, list) { |
c9c8e485 PNA |
2768 | if (idx < s_idx) |
2769 | goto cont; | |
37a9cc52 PNA |
2770 | if (!nft_is_active(net, set)) |
2771 | goto cont; | |
5b96af77 PNA |
2772 | |
2773 | ctx_set = *ctx; | |
2774 | ctx_set.table = table; | |
2775 | ctx_set.afi = afi; | |
2776 | if (nf_tables_fill_set(skb, &ctx_set, set, | |
c9c8e485 PNA |
2777 | NFT_MSG_NEWSET, |
2778 | NLM_F_MULTI) < 0) { | |
2779 | cb->args[0] = idx; | |
2780 | cb->args[2] = (unsigned long) table; | |
2781 | cb->args[3] = afi->family; | |
2782 | goto done; | |
2783 | } | |
38e029f1 | 2784 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
c9c8e485 PNA |
2785 | cont: |
2786 | idx++; | |
2787 | } | |
2788 | if (s_idx) | |
2789 | s_idx = 0; | |
2790 | } | |
2791 | } | |
2792 | cb->args[1] = 1; | |
2793 | done: | |
e688a7f8 | 2794 | rcu_read_unlock(); |
c9c8e485 PNA |
2795 | return skb->len; |
2796 | } | |
2797 | ||
5b96af77 | 2798 | static int nf_tables_dump_sets_done(struct netlink_callback *cb) |
20a69341 | 2799 | { |
5b96af77 PNA |
2800 | kfree(cb->data); |
2801 | return 0; | |
20a69341 PM |
2802 | } |
2803 | ||
7b8002a1 PNA |
2804 | static int nf_tables_getset(struct net *net, struct sock *nlsk, |
2805 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
20a69341 PM |
2806 | const struct nlattr * const nla[]) |
2807 | { | |
f2a6d766 | 2808 | u8 genmask = nft_genmask_cur(net); |
20a69341 PM |
2809 | const struct nft_set *set; |
2810 | struct nft_ctx ctx; | |
2811 | struct sk_buff *skb2; | |
c9c8e485 | 2812 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
20a69341 PM |
2813 | int err; |
2814 | ||
01cfa0a4 | 2815 | /* Verify existence before starting dump */ |
f2a6d766 | 2816 | err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, genmask); |
20a69341 PM |
2817 | if (err < 0) |
2818 | return err; | |
2819 | ||
2820 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
2821 | struct netlink_dump_control c = { | |
2822 | .dump = nf_tables_dump_sets, | |
5b96af77 | 2823 | .done = nf_tables_dump_sets_done, |
20a69341 | 2824 | }; |
5b96af77 PNA |
2825 | struct nft_ctx *ctx_dump; |
2826 | ||
2827 | ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL); | |
2828 | if (ctx_dump == NULL) | |
2829 | return -ENOMEM; | |
2830 | ||
2831 | *ctx_dump = ctx; | |
2832 | c.data = ctx_dump; | |
2833 | ||
20a69341 PM |
2834 | return netlink_dump_start(nlsk, skb, nlh, &c); |
2835 | } | |
2836 | ||
c9c8e485 PNA |
2837 | /* Only accept unspec with dump */ |
2838 | if (nfmsg->nfgen_family == NFPROTO_UNSPEC) | |
2839 | return -EAFNOSUPPORT; | |
eaa2bcd6 PT |
2840 | if (!nla[NFTA_SET_TABLE]) |
2841 | return -EINVAL; | |
c9c8e485 | 2842 | |
37a9cc52 | 2843 | set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
2844 | if (IS_ERR(set)) |
2845 | return PTR_ERR(set); | |
2846 | ||
2847 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
2848 | if (skb2 == NULL) | |
2849 | return -ENOMEM; | |
2850 | ||
2851 | err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0); | |
2852 | if (err < 0) | |
2853 | goto err; | |
2854 | ||
2855 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
2856 | ||
2857 | err: | |
2858 | kfree_skb(skb2); | |
2859 | return err; | |
2860 | } | |
2861 | ||
c50b960c PM |
2862 | static int nf_tables_set_desc_parse(const struct nft_ctx *ctx, |
2863 | struct nft_set_desc *desc, | |
2864 | const struct nlattr *nla) | |
2865 | { | |
2866 | struct nlattr *da[NFTA_SET_DESC_MAX + 1]; | |
2867 | int err; | |
2868 | ||
2869 | err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy); | |
2870 | if (err < 0) | |
2871 | return err; | |
2872 | ||
2873 | if (da[NFTA_SET_DESC_SIZE] != NULL) | |
2874 | desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE])); | |
2875 | ||
2876 | return 0; | |
2877 | } | |
2878 | ||
633c9a84 PNA |
2879 | static int nf_tables_newset(struct net *net, struct sock *nlsk, |
2880 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
20a69341 PM |
2881 | const struct nlattr * const nla[]) |
2882 | { | |
2883 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 2884 | u8 genmask = nft_genmask_next(net); |
20a69341 | 2885 | const struct nft_set_ops *ops; |
7c95f6d8 | 2886 | struct nft_af_info *afi; |
20a69341 PM |
2887 | struct nft_table *table; |
2888 | struct nft_set *set; | |
2889 | struct nft_ctx ctx; | |
cb39ad8b | 2890 | char name[NFT_SET_MAXNAMELEN]; |
20a69341 PM |
2891 | unsigned int size; |
2892 | bool create; | |
761da293 | 2893 | u64 timeout; |
8aeff920 | 2894 | u32 ktype, dtype, flags, policy, gc_int, objtype; |
c50b960c | 2895 | struct nft_set_desc desc; |
e6d8ecac CFG |
2896 | unsigned char *udata; |
2897 | u16 udlen; | |
20a69341 PM |
2898 | int err; |
2899 | ||
2900 | if (nla[NFTA_SET_TABLE] == NULL || | |
2901 | nla[NFTA_SET_NAME] == NULL || | |
958bee14 PNA |
2902 | nla[NFTA_SET_KEY_LEN] == NULL || |
2903 | nla[NFTA_SET_ID] == NULL) | |
20a69341 PM |
2904 | return -EINVAL; |
2905 | ||
c50b960c PM |
2906 | memset(&desc, 0, sizeof(desc)); |
2907 | ||
20a69341 PM |
2908 | ktype = NFT_DATA_VALUE; |
2909 | if (nla[NFTA_SET_KEY_TYPE] != NULL) { | |
2910 | ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE])); | |
2911 | if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK) | |
2912 | return -EINVAL; | |
2913 | } | |
2914 | ||
c50b960c | 2915 | desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); |
7d740264 | 2916 | if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
2917 | return -EINVAL; |
2918 | ||
2919 | flags = 0; | |
2920 | if (nla[NFTA_SET_FLAGS] != NULL) { | |
2921 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
2922 | if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | | |
7c6c6e95 | 2923 | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | |
8aeff920 PNA |
2924 | NFT_SET_MAP | NFT_SET_EVAL | |
2925 | NFT_SET_OBJECT)) | |
20a69341 | 2926 | return -EINVAL; |
8aeff920 PNA |
2927 | /* Only one of these operations is supported */ |
2928 | if ((flags & (NFT_SET_MAP | NFT_SET_EVAL | NFT_SET_OBJECT)) == | |
2929 | (NFT_SET_MAP | NFT_SET_EVAL | NFT_SET_OBJECT)) | |
7c6c6e95 | 2930 | return -EOPNOTSUPP; |
20a69341 PM |
2931 | } |
2932 | ||
2933 | dtype = 0; | |
20a69341 PM |
2934 | if (nla[NFTA_SET_DATA_TYPE] != NULL) { |
2935 | if (!(flags & NFT_SET_MAP)) | |
2936 | return -EINVAL; | |
2937 | ||
2938 | dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE])); | |
2939 | if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK && | |
2940 | dtype != NFT_DATA_VERDICT) | |
2941 | return -EINVAL; | |
2942 | ||
2943 | if (dtype != NFT_DATA_VERDICT) { | |
2944 | if (nla[NFTA_SET_DATA_LEN] == NULL) | |
2945 | return -EINVAL; | |
c50b960c | 2946 | desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); |
7d740264 | 2947 | if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
2948 | return -EINVAL; |
2949 | } else | |
7d740264 | 2950 | desc.dlen = sizeof(struct nft_verdict); |
20a69341 PM |
2951 | } else if (flags & NFT_SET_MAP) |
2952 | return -EINVAL; | |
2953 | ||
8aeff920 PNA |
2954 | if (nla[NFTA_SET_OBJ_TYPE] != NULL) { |
2955 | if (!(flags & NFT_SET_OBJECT)) | |
2956 | return -EINVAL; | |
2957 | ||
2958 | objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE])); | |
2959 | if (objtype == NFT_OBJECT_UNSPEC || | |
2960 | objtype > NFT_OBJECT_MAX) | |
2961 | return -EINVAL; | |
2962 | } else if (flags & NFT_SET_OBJECT) | |
2963 | return -EINVAL; | |
2964 | else | |
2965 | objtype = NFT_OBJECT_UNSPEC; | |
2966 | ||
761da293 PM |
2967 | timeout = 0; |
2968 | if (nla[NFTA_SET_TIMEOUT] != NULL) { | |
2969 | if (!(flags & NFT_SET_TIMEOUT)) | |
2970 | return -EINVAL; | |
d3e2a111 AP |
2971 | timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64( |
2972 | nla[NFTA_SET_TIMEOUT]))); | |
761da293 PM |
2973 | } |
2974 | gc_int = 0; | |
2975 | if (nla[NFTA_SET_GC_INTERVAL] != NULL) { | |
2976 | if (!(flags & NFT_SET_TIMEOUT)) | |
2977 | return -EINVAL; | |
2978 | gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL])); | |
2979 | } | |
2980 | ||
c50b960c PM |
2981 | policy = NFT_SET_POL_PERFORMANCE; |
2982 | if (nla[NFTA_SET_POLICY] != NULL) | |
2983 | policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); | |
2984 | ||
2985 | if (nla[NFTA_SET_DESC] != NULL) { | |
2986 | err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]); | |
2987 | if (err < 0) | |
2988 | return err; | |
2989 | } | |
2990 | ||
20a69341 PM |
2991 | create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false; |
2992 | ||
99633ab2 | 2993 | afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create); |
20a69341 PM |
2994 | if (IS_ERR(afi)) |
2995 | return PTR_ERR(afi); | |
2996 | ||
f2a6d766 | 2997 | table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE], genmask); |
20a69341 PM |
2998 | if (IS_ERR(table)) |
2999 | return PTR_ERR(table); | |
3000 | ||
633c9a84 | 3001 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla); |
20a69341 | 3002 | |
37a9cc52 | 3003 | set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
3004 | if (IS_ERR(set)) { |
3005 | if (PTR_ERR(set) != -ENOENT) | |
3006 | return PTR_ERR(set); | |
1a28ad74 | 3007 | } else { |
20a69341 PM |
3008 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
3009 | return -EEXIST; | |
3010 | if (nlh->nlmsg_flags & NLM_F_REPLACE) | |
3011 | return -EOPNOTSUPP; | |
3012 | return 0; | |
3013 | } | |
3014 | ||
3015 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) | |
3016 | return -ENOENT; | |
3017 | ||
c50b960c | 3018 | ops = nft_select_set_ops(nla, &desc, policy); |
20a69341 PM |
3019 | if (IS_ERR(ops)) |
3020 | return PTR_ERR(ops); | |
3021 | ||
e6d8ecac CFG |
3022 | udlen = 0; |
3023 | if (nla[NFTA_SET_USERDATA]) | |
3024 | udlen = nla_len(nla[NFTA_SET_USERDATA]); | |
3025 | ||
20a69341 PM |
3026 | size = 0; |
3027 | if (ops->privsize != NULL) | |
3028 | size = ops->privsize(nla); | |
3029 | ||
3030 | err = -ENOMEM; | |
e6d8ecac | 3031 | set = kzalloc(sizeof(*set) + size + udlen, GFP_KERNEL); |
20a69341 PM |
3032 | if (set == NULL) |
3033 | goto err1; | |
3034 | ||
3035 | nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name)); | |
3036 | err = nf_tables_set_alloc_name(&ctx, set, name); | |
3037 | if (err < 0) | |
3038 | goto err2; | |
3039 | ||
e6d8ecac CFG |
3040 | udata = NULL; |
3041 | if (udlen) { | |
3042 | udata = set->data + size; | |
3043 | nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen); | |
3044 | } | |
3045 | ||
20a69341 PM |
3046 | INIT_LIST_HEAD(&set->bindings); |
3047 | set->ops = ops; | |
3048 | set->ktype = ktype; | |
c50b960c | 3049 | set->klen = desc.klen; |
20a69341 | 3050 | set->dtype = dtype; |
8aeff920 | 3051 | set->objtype = objtype; |
c50b960c | 3052 | set->dlen = desc.dlen; |
20a69341 | 3053 | set->flags = flags; |
c50b960c | 3054 | set->size = desc.size; |
9363dc4b | 3055 | set->policy = policy; |
e6d8ecac CFG |
3056 | set->udlen = udlen; |
3057 | set->udata = udata; | |
761da293 PM |
3058 | set->timeout = timeout; |
3059 | set->gc_int = gc_int; | |
20a69341 | 3060 | |
c50b960c | 3061 | err = ops->init(set, &desc, nla); |
20a69341 PM |
3062 | if (err < 0) |
3063 | goto err2; | |
3064 | ||
958bee14 | 3065 | err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set); |
20a69341 | 3066 | if (err < 0) |
c17c3cdf | 3067 | goto err3; |
20a69341 | 3068 | |
e688a7f8 | 3069 | list_add_tail_rcu(&set->list, &table->sets); |
4fefee57 | 3070 | table->use++; |
20a69341 PM |
3071 | return 0; |
3072 | ||
c17c3cdf LZ |
3073 | err3: |
3074 | ops->destroy(set); | |
20a69341 PM |
3075 | err2: |
3076 | kfree(set); | |
3077 | err1: | |
3078 | module_put(ops->owner); | |
3079 | return err; | |
3080 | } | |
3081 | ||
958bee14 | 3082 | static void nft_set_destroy(struct nft_set *set) |
20a69341 | 3083 | { |
20a69341 PM |
3084 | set->ops->destroy(set); |
3085 | module_put(set->ops->owner); | |
3086 | kfree(set); | |
3087 | } | |
3088 | ||
958bee14 PNA |
3089 | static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) |
3090 | { | |
e688a7f8 | 3091 | list_del_rcu(&set->list); |
31f8441c | 3092 | nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC); |
958bee14 PNA |
3093 | nft_set_destroy(set); |
3094 | } | |
3095 | ||
633c9a84 PNA |
3096 | static int nf_tables_delset(struct net *net, struct sock *nlsk, |
3097 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
20a69341 PM |
3098 | const struct nlattr * const nla[]) |
3099 | { | |
c9c8e485 | 3100 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
f2a6d766 | 3101 | u8 genmask = nft_genmask_next(net); |
20a69341 PM |
3102 | struct nft_set *set; |
3103 | struct nft_ctx ctx; | |
3104 | int err; | |
3105 | ||
ec2c9935 PM |
3106 | if (nfmsg->nfgen_family == NFPROTO_UNSPEC) |
3107 | return -EAFNOSUPPORT; | |
20a69341 PM |
3108 | if (nla[NFTA_SET_TABLE] == NULL) |
3109 | return -EINVAL; | |
3110 | ||
f2a6d766 | 3111 | err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, genmask); |
20a69341 PM |
3112 | if (err < 0) |
3113 | return err; | |
3114 | ||
37a9cc52 | 3115 | set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
3116 | if (IS_ERR(set)) |
3117 | return PTR_ERR(set); | |
3118 | if (!list_empty(&set->bindings)) | |
3119 | return -EBUSY; | |
3120 | ||
ee01d542 | 3121 | return nft_delset(&ctx, set); |
20a69341 PM |
3122 | } |
3123 | ||
3124 | static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, | |
de70185d | 3125 | struct nft_set *set, |
20a69341 | 3126 | const struct nft_set_iter *iter, |
de70185d | 3127 | struct nft_set_elem *elem) |
20a69341 | 3128 | { |
fe2811eb | 3129 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
3130 | enum nft_registers dreg; |
3131 | ||
3132 | dreg = nft_type_to_reg(set->dtype); | |
1ec10212 PM |
3133 | return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext), |
3134 | set->dtype == NFT_DATA_VERDICT ? | |
3135 | NFT_DATA_VERDICT : NFT_DATA_VALUE, | |
3136 | set->dlen); | |
20a69341 PM |
3137 | } |
3138 | ||
3139 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, | |
3140 | struct nft_set_binding *binding) | |
3141 | { | |
3142 | struct nft_set_binding *i; | |
3143 | struct nft_set_iter iter; | |
3144 | ||
3145 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS) | |
3146 | return -EBUSY; | |
3147 | ||
11113e19 | 3148 | if (binding->flags & NFT_SET_MAP) { |
20a69341 PM |
3149 | /* If the set is already bound to the same chain all |
3150 | * jumps are already validated for that chain. | |
3151 | */ | |
3152 | list_for_each_entry(i, &set->bindings, list) { | |
a4684402 | 3153 | if (i->flags & NFT_SET_MAP && |
11113e19 | 3154 | i->chain == binding->chain) |
20a69341 PM |
3155 | goto bind; |
3156 | } | |
3157 | ||
8588ac09 | 3158 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
3159 | iter.skip = 0; |
3160 | iter.count = 0; | |
3161 | iter.err = 0; | |
3162 | iter.fn = nf_tables_bind_check_setelem; | |
1f48ff6c | 3163 | iter.flush = false; |
20a69341 PM |
3164 | |
3165 | set->ops->walk(ctx, set, &iter); | |
a02f4248 | 3166 | if (iter.err < 0) |
20a69341 | 3167 | return iter.err; |
20a69341 PM |
3168 | } |
3169 | bind: | |
3170 | binding->chain = ctx->chain; | |
e688a7f8 | 3171 | list_add_tail_rcu(&binding->list, &set->bindings); |
20a69341 PM |
3172 | return 0; |
3173 | } | |
63aea290 | 3174 | EXPORT_SYMBOL_GPL(nf_tables_bind_set); |
20a69341 PM |
3175 | |
3176 | void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, | |
3177 | struct nft_set_binding *binding) | |
3178 | { | |
e688a7f8 | 3179 | list_del_rcu(&binding->list); |
20a69341 | 3180 | |
958bee14 | 3181 | if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS && |
37a9cc52 | 3182 | nft_is_active(ctx->net, set)) |
20a69341 PM |
3183 | nf_tables_set_destroy(ctx, set); |
3184 | } | |
63aea290 | 3185 | EXPORT_SYMBOL_GPL(nf_tables_unbind_set); |
20a69341 | 3186 | |
3ac4c07a PM |
3187 | const struct nft_set_ext_type nft_set_ext_types[] = { |
3188 | [NFT_SET_EXT_KEY] = { | |
7d740264 | 3189 | .align = __alignof__(u32), |
3ac4c07a PM |
3190 | }, |
3191 | [NFT_SET_EXT_DATA] = { | |
7d740264 | 3192 | .align = __alignof__(u32), |
3ac4c07a | 3193 | }, |
f25ad2e9 PM |
3194 | [NFT_SET_EXT_EXPR] = { |
3195 | .align = __alignof__(struct nft_expr), | |
3196 | }, | |
8aeff920 PNA |
3197 | [NFT_SET_EXT_OBJREF] = { |
3198 | .len = sizeof(struct nft_object *), | |
3199 | .align = __alignof__(struct nft_object *), | |
3200 | }, | |
3ac4c07a PM |
3201 | [NFT_SET_EXT_FLAGS] = { |
3202 | .len = sizeof(u8), | |
3203 | .align = __alignof__(u8), | |
3204 | }, | |
c3e1b005 PM |
3205 | [NFT_SET_EXT_TIMEOUT] = { |
3206 | .len = sizeof(u64), | |
3207 | .align = __alignof__(u64), | |
3208 | }, | |
3209 | [NFT_SET_EXT_EXPIRATION] = { | |
3210 | .len = sizeof(unsigned long), | |
3211 | .align = __alignof__(unsigned long), | |
3212 | }, | |
68e942e8 PM |
3213 | [NFT_SET_EXT_USERDATA] = { |
3214 | .len = sizeof(struct nft_userdata), | |
3215 | .align = __alignof__(struct nft_userdata), | |
3216 | }, | |
3ac4c07a PM |
3217 | }; |
3218 | EXPORT_SYMBOL_GPL(nft_set_ext_types); | |
3219 | ||
20a69341 PM |
3220 | /* |
3221 | * Set elements | |
3222 | */ | |
3223 | ||
3224 | static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { | |
3225 | [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, | |
3226 | [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, | |
3227 | [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, | |
c3e1b005 | 3228 | [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 }, |
68e942e8 PM |
3229 | [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY, |
3230 | .len = NFT_USERDATA_MAXLEN }, | |
20a69341 PM |
3231 | }; |
3232 | ||
3233 | static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { | |
b2fbd044 LZ |
3234 | [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING, |
3235 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
3236 | [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING, | |
3237 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
20a69341 | 3238 | [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED }, |
958bee14 | 3239 | [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
3240 | }; |
3241 | ||
633c9a84 | 3242 | static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net, |
20a69341 PM |
3243 | const struct sk_buff *skb, |
3244 | const struct nlmsghdr *nlh, | |
f2a6d766 PNA |
3245 | const struct nlattr * const nla[], |
3246 | u8 genmask) | |
20a69341 PM |
3247 | { |
3248 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
7c95f6d8 PNA |
3249 | struct nft_af_info *afi; |
3250 | struct nft_table *table; | |
20a69341 | 3251 | |
99633ab2 | 3252 | afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false); |
20a69341 PM |
3253 | if (IS_ERR(afi)) |
3254 | return PTR_ERR(afi); | |
3255 | ||
f2a6d766 PNA |
3256 | table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE], |
3257 | genmask); | |
20a69341 PM |
3258 | if (IS_ERR(table)) |
3259 | return PTR_ERR(table); | |
3260 | ||
633c9a84 | 3261 | nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla); |
20a69341 PM |
3262 | return 0; |
3263 | } | |
3264 | ||
3265 | static int nf_tables_fill_setelem(struct sk_buff *skb, | |
3266 | const struct nft_set *set, | |
3267 | const struct nft_set_elem *elem) | |
3268 | { | |
fe2811eb | 3269 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
3270 | unsigned char *b = skb_tail_pointer(skb); |
3271 | struct nlattr *nest; | |
3272 | ||
3273 | nest = nla_nest_start(skb, NFTA_LIST_ELEM); | |
3274 | if (nest == NULL) | |
3275 | goto nla_put_failure; | |
3276 | ||
fe2811eb PM |
3277 | if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), |
3278 | NFT_DATA_VALUE, set->klen) < 0) | |
20a69341 PM |
3279 | goto nla_put_failure; |
3280 | ||
fe2811eb PM |
3281 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
3282 | nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext), | |
20a69341 PM |
3283 | set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, |
3284 | set->dlen) < 0) | |
3285 | goto nla_put_failure; | |
3286 | ||
f25ad2e9 PM |
3287 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) && |
3288 | nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0) | |
3289 | goto nla_put_failure; | |
3290 | ||
8aeff920 PNA |
3291 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && |
3292 | nla_put_string(skb, NFTA_SET_ELEM_OBJREF, | |
3293 | (*nft_set_ext_obj(ext))->name) < 0) | |
3294 | goto nla_put_failure; | |
3295 | ||
fe2811eb PM |
3296 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
3297 | nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, | |
3298 | htonl(*nft_set_ext_flags(ext)))) | |
3299 | goto nla_put_failure; | |
20a69341 | 3300 | |
c3e1b005 PM |
3301 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) && |
3302 | nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT, | |
d3e2a111 AP |
3303 | cpu_to_be64(jiffies_to_msecs( |
3304 | *nft_set_ext_timeout(ext))), | |
b46f6ded | 3305 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
3306 | goto nla_put_failure; |
3307 | ||
3308 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { | |
3309 | unsigned long expires, now = jiffies; | |
3310 | ||
3311 | expires = *nft_set_ext_expiration(ext); | |
3312 | if (time_before(now, expires)) | |
3313 | expires -= now; | |
3314 | else | |
3315 | expires = 0; | |
3316 | ||
3317 | if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION, | |
b46f6ded ND |
3318 | cpu_to_be64(jiffies_to_msecs(expires)), |
3319 | NFTA_SET_ELEM_PAD)) | |
c3e1b005 PM |
3320 | goto nla_put_failure; |
3321 | } | |
3322 | ||
68e942e8 PM |
3323 | if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) { |
3324 | struct nft_userdata *udata; | |
3325 | ||
3326 | udata = nft_set_ext_userdata(ext); | |
3327 | if (nla_put(skb, NFTA_SET_ELEM_USERDATA, | |
3328 | udata->len + 1, udata->data)) | |
3329 | goto nla_put_failure; | |
3330 | } | |
3331 | ||
20a69341 PM |
3332 | nla_nest_end(skb, nest); |
3333 | return 0; | |
3334 | ||
3335 | nla_put_failure: | |
3336 | nlmsg_trim(skb, b); | |
3337 | return -EMSGSIZE; | |
3338 | } | |
3339 | ||
3340 | struct nft_set_dump_args { | |
3341 | const struct netlink_callback *cb; | |
3342 | struct nft_set_iter iter; | |
3343 | struct sk_buff *skb; | |
3344 | }; | |
3345 | ||
3346 | static int nf_tables_dump_setelem(const struct nft_ctx *ctx, | |
de70185d | 3347 | struct nft_set *set, |
20a69341 | 3348 | const struct nft_set_iter *iter, |
de70185d | 3349 | struct nft_set_elem *elem) |
20a69341 PM |
3350 | { |
3351 | struct nft_set_dump_args *args; | |
3352 | ||
3353 | args = container_of(iter, struct nft_set_dump_args, iter); | |
3354 | return nf_tables_fill_setelem(args->skb, set, elem); | |
3355 | } | |
3356 | ||
3357 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) | |
3358 | { | |
633c9a84 | 3359 | struct net *net = sock_net(skb->sk); |
f2a6d766 | 3360 | u8 genmask = nft_genmask_cur(net); |
de70185d | 3361 | struct nft_set *set; |
20a69341 PM |
3362 | struct nft_set_dump_args args; |
3363 | struct nft_ctx ctx; | |
3364 | struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1]; | |
3365 | struct nfgenmsg *nfmsg; | |
3366 | struct nlmsghdr *nlh; | |
3367 | struct nlattr *nest; | |
3368 | u32 portid, seq; | |
3369 | int event, err; | |
3370 | ||
720e0dfa MN |
3371 | err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla, |
3372 | NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy); | |
20a69341 PM |
3373 | if (err < 0) |
3374 | return err; | |
3375 | ||
633c9a84 | 3376 | err = nft_ctx_init_from_elemattr(&ctx, net, cb->skb, cb->nlh, |
f2a6d766 | 3377 | (void *)nla, genmask); |
20a69341 PM |
3378 | if (err < 0) |
3379 | return err; | |
3380 | ||
37a9cc52 PNA |
3381 | set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], |
3382 | genmask); | |
20a69341 PM |
3383 | if (IS_ERR(set)) |
3384 | return PTR_ERR(set); | |
3385 | ||
3386 | event = NFT_MSG_NEWSETELEM; | |
3387 | event |= NFNL_SUBSYS_NFTABLES << 8; | |
3388 | portid = NETLINK_CB(cb->skb).portid; | |
3389 | seq = cb->nlh->nlmsg_seq; | |
3390 | ||
3391 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), | |
3392 | NLM_F_MULTI); | |
3393 | if (nlh == NULL) | |
3394 | goto nla_put_failure; | |
3395 | ||
3396 | nfmsg = nlmsg_data(nlh); | |
6403d962 | 3397 | nfmsg->nfgen_family = ctx.afi->family; |
20a69341 | 3398 | nfmsg->version = NFNETLINK_V0; |
84d7fce6 | 3399 | nfmsg->res_id = htons(ctx.net->nft.base_seq & 0xffff); |
20a69341 PM |
3400 | |
3401 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name)) | |
3402 | goto nla_put_failure; | |
3403 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name)) | |
3404 | goto nla_put_failure; | |
3405 | ||
3406 | nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS); | |
3407 | if (nest == NULL) | |
3408 | goto nla_put_failure; | |
3409 | ||
8588ac09 PNA |
3410 | args.cb = cb; |
3411 | args.skb = skb; | |
3412 | args.iter.genmask = nft_genmask_cur(ctx.net); | |
3413 | args.iter.skip = cb->args[0]; | |
3414 | args.iter.count = 0; | |
3415 | args.iter.err = 0; | |
3416 | args.iter.fn = nf_tables_dump_setelem; | |
1f48ff6c | 3417 | args.iter.flush = false; |
20a69341 PM |
3418 | set->ops->walk(&ctx, set, &args.iter); |
3419 | ||
3420 | nla_nest_end(skb, nest); | |
3421 | nlmsg_end(skb, nlh); | |
3422 | ||
3423 | if (args.iter.err && args.iter.err != -EMSGSIZE) | |
3424 | return args.iter.err; | |
3425 | if (args.iter.count == cb->args[0]) | |
3426 | return 0; | |
3427 | ||
3428 | cb->args[0] = args.iter.count; | |
3429 | return skb->len; | |
3430 | ||
3431 | nla_put_failure: | |
3432 | return -ENOSPC; | |
3433 | } | |
3434 | ||
7b8002a1 PNA |
3435 | static int nf_tables_getsetelem(struct net *net, struct sock *nlsk, |
3436 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
20a69341 PM |
3437 | const struct nlattr * const nla[]) |
3438 | { | |
f2a6d766 | 3439 | u8 genmask = nft_genmask_cur(net); |
20a69341 PM |
3440 | const struct nft_set *set; |
3441 | struct nft_ctx ctx; | |
3442 | int err; | |
3443 | ||
f2a6d766 | 3444 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, genmask); |
20a69341 PM |
3445 | if (err < 0) |
3446 | return err; | |
3447 | ||
37a9cc52 PNA |
3448 | set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], |
3449 | genmask); | |
20a69341 PM |
3450 | if (IS_ERR(set)) |
3451 | return PTR_ERR(set); | |
3452 | ||
3453 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
3454 | struct netlink_dump_control c = { | |
3455 | .dump = nf_tables_dump_set, | |
3456 | }; | |
3457 | return netlink_dump_start(nlsk, skb, nlh, &c); | |
3458 | } | |
3459 | return -EOPNOTSUPP; | |
3460 | } | |
3461 | ||
d60ce62f AB |
3462 | static int nf_tables_fill_setelem_info(struct sk_buff *skb, |
3463 | const struct nft_ctx *ctx, u32 seq, | |
3464 | u32 portid, int event, u16 flags, | |
3465 | const struct nft_set *set, | |
3466 | const struct nft_set_elem *elem) | |
3467 | { | |
3468 | struct nfgenmsg *nfmsg; | |
3469 | struct nlmsghdr *nlh; | |
3470 | struct nlattr *nest; | |
3471 | int err; | |
3472 | ||
3473 | event |= NFNL_SUBSYS_NFTABLES << 8; | |
3474 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), | |
3475 | flags); | |
3476 | if (nlh == NULL) | |
3477 | goto nla_put_failure; | |
3478 | ||
3479 | nfmsg = nlmsg_data(nlh); | |
3480 | nfmsg->nfgen_family = ctx->afi->family; | |
3481 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 3482 | nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff); |
d60ce62f AB |
3483 | |
3484 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) | |
3485 | goto nla_put_failure; | |
3486 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
3487 | goto nla_put_failure; | |
3488 | ||
3489 | nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS); | |
3490 | if (nest == NULL) | |
3491 | goto nla_put_failure; | |
3492 | ||
3493 | err = nf_tables_fill_setelem(skb, set, elem); | |
3494 | if (err < 0) | |
3495 | goto nla_put_failure; | |
3496 | ||
3497 | nla_nest_end(skb, nest); | |
3498 | ||
053c095a JB |
3499 | nlmsg_end(skb, nlh); |
3500 | return 0; | |
d60ce62f AB |
3501 | |
3502 | nla_put_failure: | |
3503 | nlmsg_trim(skb, nlh); | |
3504 | return -1; | |
3505 | } | |
3506 | ||
3507 | static int nf_tables_setelem_notify(const struct nft_ctx *ctx, | |
3508 | const struct nft_set *set, | |
3509 | const struct nft_set_elem *elem, | |
3510 | int event, u16 flags) | |
3511 | { | |
128ad332 PNA |
3512 | struct net *net = ctx->net; |
3513 | u32 portid = ctx->portid; | |
d60ce62f AB |
3514 | struct sk_buff *skb; |
3515 | int err; | |
3516 | ||
128ad332 | 3517 | if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
d60ce62f AB |
3518 | return 0; |
3519 | ||
3520 | err = -ENOBUFS; | |
3521 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
3522 | if (skb == NULL) | |
3523 | goto err; | |
3524 | ||
3525 | err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags, | |
3526 | set, elem); | |
3527 | if (err < 0) { | |
3528 | kfree_skb(skb); | |
3529 | goto err; | |
3530 | } | |
3531 | ||
128ad332 | 3532 | err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report, |
d60ce62f AB |
3533 | GFP_KERNEL); |
3534 | err: | |
3535 | if (err < 0) | |
3536 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err); | |
3537 | return err; | |
3538 | } | |
3539 | ||
60319eb1 PNA |
3540 | static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx, |
3541 | int msg_type, | |
3542 | struct nft_set *set) | |
3543 | { | |
3544 | struct nft_trans *trans; | |
3545 | ||
3546 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem)); | |
3547 | if (trans == NULL) | |
3548 | return NULL; | |
3549 | ||
3550 | nft_trans_elem_set(trans) = set; | |
3551 | return trans; | |
3552 | } | |
3553 | ||
22fe54d5 PM |
3554 | void *nft_set_elem_init(const struct nft_set *set, |
3555 | const struct nft_set_ext_tmpl *tmpl, | |
49499c3e | 3556 | const u32 *key, const u32 *data, |
22fe54d5 | 3557 | u64 timeout, gfp_t gfp) |
fe2811eb PM |
3558 | { |
3559 | struct nft_set_ext *ext; | |
3560 | void *elem; | |
3561 | ||
3562 | elem = kzalloc(set->ops->elemsize + tmpl->len, gfp); | |
3563 | if (elem == NULL) | |
3564 | return NULL; | |
3565 | ||
3566 | ext = nft_set_elem_ext(set, elem); | |
3567 | nft_set_ext_init(ext, tmpl); | |
3568 | ||
3569 | memcpy(nft_set_ext_key(ext), key, set->klen); | |
3570 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
3571 | memcpy(nft_set_ext_data(ext), data, set->dlen); | |
c3e1b005 PM |
3572 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) |
3573 | *nft_set_ext_expiration(ext) = | |
d3e2a111 | 3574 | jiffies + timeout; |
c3e1b005 PM |
3575 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) |
3576 | *nft_set_ext_timeout(ext) = timeout; | |
fe2811eb PM |
3577 | |
3578 | return elem; | |
3579 | } | |
3580 | ||
61f9e292 LZ |
3581 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
3582 | bool destroy_expr) | |
61edafbb PM |
3583 | { |
3584 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
3585 | ||
3586 | nft_data_uninit(nft_set_ext_key(ext), NFT_DATA_VALUE); | |
3587 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
3588 | nft_data_uninit(nft_set_ext_data(ext), set->dtype); | |
61f9e292 | 3589 | if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) |
f25ad2e9 | 3590 | nf_tables_expr_destroy(NULL, nft_set_ext_expr(ext)); |
8aeff920 PNA |
3591 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) |
3592 | (*nft_set_ext_obj(ext))->use--; | |
61edafbb PM |
3593 | kfree(elem); |
3594 | } | |
3595 | EXPORT_SYMBOL_GPL(nft_set_elem_destroy); | |
3596 | ||
0e9091d6 PNA |
3597 | static int nft_setelem_parse_flags(const struct nft_set *set, |
3598 | const struct nlattr *attr, u32 *flags) | |
3599 | { | |
3600 | if (attr == NULL) | |
3601 | return 0; | |
3602 | ||
3603 | *flags = ntohl(nla_get_be32(attr)); | |
3604 | if (*flags & ~NFT_SET_ELEM_INTERVAL_END) | |
3605 | return -EINVAL; | |
3606 | if (!(set->flags & NFT_SET_INTERVAL) && | |
3607 | *flags & NFT_SET_ELEM_INTERVAL_END) | |
3608 | return -EINVAL; | |
3609 | ||
3610 | return 0; | |
3611 | } | |
3612 | ||
60319eb1 | 3613 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
c016c7e4 | 3614 | const struct nlattr *attr, u32 nlmsg_flags) |
20a69341 PM |
3615 | { |
3616 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
8aeff920 | 3617 | u8 genmask = nft_genmask_next(ctx->net); |
20a69341 | 3618 | struct nft_data_desc d1, d2; |
fe2811eb | 3619 | struct nft_set_ext_tmpl tmpl; |
c016c7e4 | 3620 | struct nft_set_ext *ext, *ext2; |
20a69341 PM |
3621 | struct nft_set_elem elem; |
3622 | struct nft_set_binding *binding; | |
8aeff920 | 3623 | struct nft_object *obj = NULL; |
68e942e8 | 3624 | struct nft_userdata *udata; |
fe2811eb | 3625 | struct nft_data data; |
20a69341 | 3626 | enum nft_registers dreg; |
60319eb1 | 3627 | struct nft_trans *trans; |
0e9091d6 | 3628 | u32 flags = 0; |
c3e1b005 | 3629 | u64 timeout; |
68e942e8 | 3630 | u8 ulen; |
20a69341 PM |
3631 | int err; |
3632 | ||
3633 | err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr, | |
3634 | nft_set_elem_policy); | |
3635 | if (err < 0) | |
3636 | return err; | |
3637 | ||
3638 | if (nla[NFTA_SET_ELEM_KEY] == NULL) | |
3639 | return -EINVAL; | |
3640 | ||
fe2811eb PM |
3641 | nft_set_ext_prepare(&tmpl); |
3642 | ||
0e9091d6 PNA |
3643 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
3644 | if (err < 0) | |
3645 | return err; | |
3646 | if (flags != 0) | |
3647 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
20a69341 PM |
3648 | |
3649 | if (set->flags & NFT_SET_MAP) { | |
3650 | if (nla[NFTA_SET_ELEM_DATA] == NULL && | |
fe2811eb | 3651 | !(flags & NFT_SET_ELEM_INTERVAL_END)) |
20a69341 | 3652 | return -EINVAL; |
bd7fc645 | 3653 | if (nla[NFTA_SET_ELEM_DATA] != NULL && |
fe2811eb | 3654 | flags & NFT_SET_ELEM_INTERVAL_END) |
bd7fc645 | 3655 | return -EINVAL; |
20a69341 PM |
3656 | } else { |
3657 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
3658 | return -EINVAL; | |
3659 | } | |
3660 | ||
c3e1b005 PM |
3661 | timeout = 0; |
3662 | if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) { | |
3663 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
3664 | return -EINVAL; | |
d3e2a111 AP |
3665 | timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64( |
3666 | nla[NFTA_SET_ELEM_TIMEOUT]))); | |
c3e1b005 PM |
3667 | } else if (set->flags & NFT_SET_TIMEOUT) { |
3668 | timeout = set->timeout; | |
3669 | } | |
3670 | ||
7d740264 | 3671 | err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1, |
d0a11fc3 | 3672 | nla[NFTA_SET_ELEM_KEY]); |
20a69341 PM |
3673 | if (err < 0) |
3674 | goto err1; | |
3675 | err = -EINVAL; | |
3676 | if (d1.type != NFT_DATA_VALUE || d1.len != set->klen) | |
3677 | goto err2; | |
3678 | ||
7d740264 | 3679 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, d1.len); |
c3e1b005 PM |
3680 | if (timeout > 0) { |
3681 | nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); | |
3682 | if (timeout != set->timeout) | |
3683 | nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT); | |
3684 | } | |
fe2811eb | 3685 | |
8aeff920 PNA |
3686 | if (nla[NFTA_SET_ELEM_OBJREF] != NULL) { |
3687 | if (!(set->flags & NFT_SET_OBJECT)) { | |
3688 | err = -EINVAL; | |
3689 | goto err2; | |
3690 | } | |
3691 | obj = nf_tables_obj_lookup(ctx->table, nla[NFTA_SET_ELEM_OBJREF], | |
3692 | set->objtype, genmask); | |
3693 | if (IS_ERR(obj)) { | |
3694 | err = PTR_ERR(obj); | |
3695 | goto err2; | |
3696 | } | |
3697 | nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF); | |
3698 | } | |
3699 | ||
20a69341 | 3700 | if (nla[NFTA_SET_ELEM_DATA] != NULL) { |
d0a11fc3 PM |
3701 | err = nft_data_init(ctx, &data, sizeof(data), &d2, |
3702 | nla[NFTA_SET_ELEM_DATA]); | |
20a69341 PM |
3703 | if (err < 0) |
3704 | goto err2; | |
3705 | ||
3706 | err = -EINVAL; | |
3707 | if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen) | |
3708 | goto err3; | |
3709 | ||
3710 | dreg = nft_type_to_reg(set->dtype); | |
3711 | list_for_each_entry(binding, &set->bindings, list) { | |
3712 | struct nft_ctx bind_ctx = { | |
58c78e10 | 3713 | .net = ctx->net, |
20a69341 PM |
3714 | .afi = ctx->afi, |
3715 | .table = ctx->table, | |
7c95f6d8 | 3716 | .chain = (struct nft_chain *)binding->chain, |
20a69341 PM |
3717 | }; |
3718 | ||
11113e19 PM |
3719 | if (!(binding->flags & NFT_SET_MAP)) |
3720 | continue; | |
3721 | ||
1ec10212 PM |
3722 | err = nft_validate_register_store(&bind_ctx, dreg, |
3723 | &data, | |
3724 | d2.type, d2.len); | |
20a69341 PM |
3725 | if (err < 0) |
3726 | goto err3; | |
3727 | } | |
fe2811eb | 3728 | |
7d740264 | 3729 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, d2.len); |
20a69341 PM |
3730 | } |
3731 | ||
68e942e8 PM |
3732 | /* The full maximum length of userdata can exceed the maximum |
3733 | * offset value (U8_MAX) for following extensions, therefor it | |
3734 | * must be the last extension added. | |
3735 | */ | |
3736 | ulen = 0; | |
3737 | if (nla[NFTA_SET_ELEM_USERDATA] != NULL) { | |
3738 | ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]); | |
3739 | if (ulen > 0) | |
3740 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA, | |
3741 | ulen); | |
3742 | } | |
3743 | ||
fe2811eb | 3744 | err = -ENOMEM; |
7d740264 | 3745 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, data.data, |
c3e1b005 | 3746 | timeout, GFP_KERNEL); |
fe2811eb PM |
3747 | if (elem.priv == NULL) |
3748 | goto err3; | |
3749 | ||
3750 | ext = nft_set_elem_ext(set, elem.priv); | |
3751 | if (flags) | |
3752 | *nft_set_ext_flags(ext) = flags; | |
68e942e8 PM |
3753 | if (ulen > 0) { |
3754 | udata = nft_set_ext_userdata(ext); | |
3755 | udata->len = ulen - 1; | |
3756 | nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen); | |
3757 | } | |
8aeff920 PNA |
3758 | if (obj) { |
3759 | *nft_set_ext_obj(ext) = obj; | |
3760 | obj->use++; | |
3761 | } | |
fe2811eb | 3762 | |
60319eb1 PNA |
3763 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); |
3764 | if (trans == NULL) | |
fe2811eb | 3765 | goto err4; |
60319eb1 | 3766 | |
69086658 | 3767 | ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK; |
c016c7e4 PNA |
3768 | err = set->ops->insert(ctx->net, set, &elem, &ext2); |
3769 | if (err) { | |
3770 | if (err == -EEXIST) { | |
8aeff920 PNA |
3771 | if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
3772 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) && | |
3773 | memcmp(nft_set_ext_data(ext), | |
3774 | nft_set_ext_data(ext2), set->dlen) != 0) || | |
3775 | (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && | |
3776 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) && | |
3777 | *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2))) | |
c016c7e4 PNA |
3778 | err = -EBUSY; |
3779 | else if (!(nlmsg_flags & NLM_F_EXCL)) | |
3780 | err = 0; | |
3781 | } | |
fe2811eb | 3782 | goto err5; |
c016c7e4 | 3783 | } |
20a69341 | 3784 | |
35d0ac90 PNA |
3785 | if (set->size && |
3786 | !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) { | |
3787 | err = -ENFILE; | |
3788 | goto err6; | |
3789 | } | |
3790 | ||
60319eb1 | 3791 | nft_trans_elem(trans) = elem; |
46bbafce | 3792 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
20a69341 PM |
3793 | return 0; |
3794 | ||
35d0ac90 | 3795 | err6: |
5cb82a38 | 3796 | set->ops->remove(ctx->net, set, &elem); |
fe2811eb | 3797 | err5: |
60319eb1 | 3798 | kfree(trans); |
fe2811eb PM |
3799 | err4: |
3800 | kfree(elem.priv); | |
20a69341 PM |
3801 | err3: |
3802 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
fe2811eb | 3803 | nft_data_uninit(&data, d2.type); |
20a69341 | 3804 | err2: |
7d740264 | 3805 | nft_data_uninit(&elem.key.val, d1.type); |
20a69341 PM |
3806 | err1: |
3807 | return err; | |
3808 | } | |
3809 | ||
633c9a84 PNA |
3810 | static int nf_tables_newsetelem(struct net *net, struct sock *nlsk, |
3811 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
20a69341 PM |
3812 | const struct nlattr * const nla[]) |
3813 | { | |
f2a6d766 | 3814 | u8 genmask = nft_genmask_next(net); |
20a69341 PM |
3815 | const struct nlattr *attr; |
3816 | struct nft_set *set; | |
3817 | struct nft_ctx ctx; | |
60319eb1 | 3818 | int rem, err = 0; |
20a69341 | 3819 | |
7d5570ca PNA |
3820 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
3821 | return -EINVAL; | |
3822 | ||
f2a6d766 | 3823 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, genmask); |
20a69341 PM |
3824 | if (err < 0) |
3825 | return err; | |
3826 | ||
37a9cc52 PNA |
3827 | set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], |
3828 | genmask); | |
958bee14 PNA |
3829 | if (IS_ERR(set)) { |
3830 | if (nla[NFTA_SET_ELEM_LIST_SET_ID]) { | |
3831 | set = nf_tables_set_lookup_byid(net, | |
37a9cc52 PNA |
3832 | nla[NFTA_SET_ELEM_LIST_SET_ID], |
3833 | genmask); | |
958bee14 PNA |
3834 | } |
3835 | if (IS_ERR(set)) | |
3836 | return PTR_ERR(set); | |
3837 | } | |
3838 | ||
20a69341 PM |
3839 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) |
3840 | return -EBUSY; | |
3841 | ||
3842 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
c016c7e4 | 3843 | err = nft_add_set_elem(&ctx, set, attr, nlh->nlmsg_flags); |
35d0ac90 | 3844 | if (err < 0) |
60319eb1 | 3845 | break; |
20a69341 | 3846 | } |
60319eb1 | 3847 | return err; |
20a69341 PM |
3848 | } |
3849 | ||
60319eb1 | 3850 | static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, |
20a69341 PM |
3851 | const struct nlattr *attr) |
3852 | { | |
3853 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
3971ca14 | 3854 | struct nft_set_ext_tmpl tmpl; |
20a69341 PM |
3855 | struct nft_data_desc desc; |
3856 | struct nft_set_elem elem; | |
3971ca14 | 3857 | struct nft_set_ext *ext; |
60319eb1 | 3858 | struct nft_trans *trans; |
3971ca14 PNA |
3859 | u32 flags = 0; |
3860 | void *priv; | |
20a69341 PM |
3861 | int err; |
3862 | ||
3863 | err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr, | |
3864 | nft_set_elem_policy); | |
3865 | if (err < 0) | |
3866 | goto err1; | |
3867 | ||
3868 | err = -EINVAL; | |
3869 | if (nla[NFTA_SET_ELEM_KEY] == NULL) | |
3870 | goto err1; | |
3871 | ||
3971ca14 PNA |
3872 | nft_set_ext_prepare(&tmpl); |
3873 | ||
3874 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); | |
3875 | if (err < 0) | |
3876 | return err; | |
3877 | if (flags != 0) | |
3878 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
3879 | ||
7d740264 | 3880 | err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc, |
d0a11fc3 | 3881 | nla[NFTA_SET_ELEM_KEY]); |
20a69341 PM |
3882 | if (err < 0) |
3883 | goto err1; | |
3884 | ||
3885 | err = -EINVAL; | |
3886 | if (desc.type != NFT_DATA_VALUE || desc.len != set->klen) | |
3887 | goto err2; | |
3888 | ||
3971ca14 PNA |
3889 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, desc.len); |
3890 | ||
3891 | err = -ENOMEM; | |
3892 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, NULL, 0, | |
3893 | GFP_KERNEL); | |
3894 | if (elem.priv == NULL) | |
3895 | goto err2; | |
3896 | ||
3897 | ext = nft_set_elem_ext(set, elem.priv); | |
3898 | if (flags) | |
3899 | *nft_set_ext_flags(ext) = flags; | |
3900 | ||
60319eb1 | 3901 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); |
609ccf08 JL |
3902 | if (trans == NULL) { |
3903 | err = -ENOMEM; | |
3971ca14 | 3904 | goto err3; |
609ccf08 | 3905 | } |
20a69341 | 3906 | |
42a55769 | 3907 | priv = set->ops->deactivate(ctx->net, set, &elem); |
3971ca14 | 3908 | if (priv == NULL) { |
cc02e457 | 3909 | err = -ENOENT; |
3971ca14 | 3910 | goto err4; |
cc02e457 | 3911 | } |
3971ca14 PNA |
3912 | kfree(elem.priv); |
3913 | elem.priv = priv; | |
cc02e457 | 3914 | |
60319eb1 | 3915 | nft_trans_elem(trans) = elem; |
46bbafce | 3916 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
0dc13625 | 3917 | return 0; |
cc02e457 | 3918 | |
3971ca14 | 3919 | err4: |
cc02e457 | 3920 | kfree(trans); |
3971ca14 PNA |
3921 | err3: |
3922 | kfree(elem.priv); | |
20a69341 | 3923 | err2: |
7d740264 | 3924 | nft_data_uninit(&elem.key.val, desc.type); |
20a69341 PM |
3925 | err1: |
3926 | return err; | |
3927 | } | |
3928 | ||
8411b644 | 3929 | static int nft_flush_set(const struct nft_ctx *ctx, |
de70185d | 3930 | struct nft_set *set, |
8411b644 | 3931 | const struct nft_set_iter *iter, |
de70185d | 3932 | struct nft_set_elem *elem) |
8411b644 PNA |
3933 | { |
3934 | struct nft_trans *trans; | |
3935 | int err; | |
3936 | ||
3937 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
3938 | sizeof(struct nft_trans_elem), GFP_ATOMIC); | |
3939 | if (!trans) | |
3940 | return -ENOMEM; | |
3941 | ||
1ba1c414 | 3942 | if (!set->ops->flush(ctx->net, set, elem->priv)) { |
8411b644 PNA |
3943 | err = -ENOENT; |
3944 | goto err1; | |
3945 | } | |
b2c11e4b | 3946 | set->ndeact++; |
8411b644 | 3947 | |
de70185d PNA |
3948 | nft_trans_elem_set(trans) = set; |
3949 | nft_trans_elem(trans) = *elem; | |
8411b644 PNA |
3950 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
3951 | ||
3952 | return 0; | |
3953 | err1: | |
3954 | kfree(trans); | |
3955 | return err; | |
3956 | } | |
3957 | ||
633c9a84 PNA |
3958 | static int nf_tables_delsetelem(struct net *net, struct sock *nlsk, |
3959 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
20a69341 PM |
3960 | const struct nlattr * const nla[]) |
3961 | { | |
f2a6d766 | 3962 | u8 genmask = nft_genmask_next(net); |
20a69341 PM |
3963 | const struct nlattr *attr; |
3964 | struct nft_set *set; | |
3965 | struct nft_ctx ctx; | |
60319eb1 | 3966 | int rem, err = 0; |
20a69341 | 3967 | |
f2a6d766 | 3968 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, genmask); |
20a69341 PM |
3969 | if (err < 0) |
3970 | return err; | |
3971 | ||
37a9cc52 PNA |
3972 | set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], |
3973 | genmask); | |
20a69341 PM |
3974 | if (IS_ERR(set)) |
3975 | return PTR_ERR(set); | |
3976 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) | |
3977 | return -EBUSY; | |
3978 | ||
8411b644 | 3979 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) { |
baa2d42c PNA |
3980 | struct nft_set_iter iter = { |
3981 | .genmask = genmask, | |
3982 | .fn = nft_flush_set, | |
1f48ff6c | 3983 | .flush = true, |
8411b644 | 3984 | }; |
baa2d42c | 3985 | set->ops->walk(&ctx, set, &iter); |
8411b644 | 3986 | |
baa2d42c | 3987 | return iter.err; |
8411b644 PNA |
3988 | } |
3989 | ||
20a69341 PM |
3990 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
3991 | err = nft_del_setelem(&ctx, set, attr); | |
3992 | if (err < 0) | |
60319eb1 | 3993 | break; |
4fefee57 | 3994 | |
3dd0673a | 3995 | set->ndeact++; |
20a69341 | 3996 | } |
60319eb1 | 3997 | return err; |
20a69341 PM |
3998 | } |
3999 | ||
cfed7e1b PM |
4000 | void nft_set_gc_batch_release(struct rcu_head *rcu) |
4001 | { | |
4002 | struct nft_set_gc_batch *gcb; | |
4003 | unsigned int i; | |
4004 | ||
4005 | gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu); | |
4006 | for (i = 0; i < gcb->head.cnt; i++) | |
61f9e292 | 4007 | nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true); |
cfed7e1b PM |
4008 | kfree(gcb); |
4009 | } | |
4010 | EXPORT_SYMBOL_GPL(nft_set_gc_batch_release); | |
4011 | ||
4012 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
4013 | gfp_t gfp) | |
4014 | { | |
4015 | struct nft_set_gc_batch *gcb; | |
4016 | ||
4017 | gcb = kzalloc(sizeof(*gcb), gfp); | |
4018 | if (gcb == NULL) | |
4019 | return gcb; | |
4020 | gcb->head.set = set; | |
4021 | return gcb; | |
4022 | } | |
4023 | EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc); | |
4024 | ||
e5009240 PNA |
4025 | /* |
4026 | * Stateful objects | |
4027 | */ | |
4028 | ||
4029 | /** | |
4030 | * nft_register_obj- register nf_tables stateful object type | |
4031 | * @obj: object type | |
4032 | * | |
4033 | * Registers the object type for use with nf_tables. Returns zero on | |
4034 | * success or a negative errno code otherwise. | |
4035 | */ | |
4036 | int nft_register_obj(struct nft_object_type *obj_type) | |
4037 | { | |
4038 | if (obj_type->type == NFT_OBJECT_UNSPEC) | |
4039 | return -EINVAL; | |
4040 | ||
4041 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
4042 | list_add_rcu(&obj_type->list, &nf_tables_objects); | |
4043 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
4044 | return 0; | |
4045 | } | |
4046 | EXPORT_SYMBOL_GPL(nft_register_obj); | |
4047 | ||
4048 | /** | |
4049 | * nft_unregister_obj - unregister nf_tables object type | |
4050 | * @obj: object type | |
4051 | * | |
4052 | * Unregisters the object type for use with nf_tables. | |
4053 | */ | |
4054 | void nft_unregister_obj(struct nft_object_type *obj_type) | |
4055 | { | |
4056 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
4057 | list_del_rcu(&obj_type->list); | |
4058 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
4059 | } | |
4060 | EXPORT_SYMBOL_GPL(nft_unregister_obj); | |
4061 | ||
4062 | struct nft_object *nf_tables_obj_lookup(const struct nft_table *table, | |
4063 | const struct nlattr *nla, | |
4064 | u32 objtype, u8 genmask) | |
4065 | { | |
4066 | struct nft_object *obj; | |
4067 | ||
4068 | list_for_each_entry(obj, &table->objects, list) { | |
4069 | if (!nla_strcmp(nla, obj->name) && | |
4070 | objtype == obj->type->type && | |
4071 | nft_active_genmask(obj, genmask)) | |
4072 | return obj; | |
4073 | } | |
4074 | return ERR_PTR(-ENOENT); | |
4075 | } | |
4076 | EXPORT_SYMBOL_GPL(nf_tables_obj_lookup); | |
4077 | ||
4078 | static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = { | |
b2fbd044 LZ |
4079 | [NFTA_OBJ_TABLE] = { .type = NLA_STRING, |
4080 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
4081 | [NFTA_OBJ_NAME] = { .type = NLA_STRING, | |
4082 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
e5009240 PNA |
4083 | [NFTA_OBJ_TYPE] = { .type = NLA_U32 }, |
4084 | [NFTA_OBJ_DATA] = { .type = NLA_NESTED }, | |
4085 | }; | |
4086 | ||
4087 | static struct nft_object *nft_obj_init(const struct nft_object_type *type, | |
4088 | const struct nlattr *attr) | |
4089 | { | |
4090 | struct nlattr *tb[type->maxattr + 1]; | |
4091 | struct nft_object *obj; | |
4092 | int err; | |
4093 | ||
4094 | if (attr) { | |
4095 | err = nla_parse_nested(tb, type->maxattr, attr, type->policy); | |
4096 | if (err < 0) | |
4097 | goto err1; | |
4098 | } else { | |
4099 | memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1)); | |
4100 | } | |
4101 | ||
4102 | err = -ENOMEM; | |
4103 | obj = kzalloc(sizeof(struct nft_object) + type->size, GFP_KERNEL); | |
4104 | if (obj == NULL) | |
4105 | goto err1; | |
4106 | ||
4107 | err = type->init((const struct nlattr * const *)tb, obj); | |
4108 | if (err < 0) | |
4109 | goto err2; | |
4110 | ||
4111 | obj->type = type; | |
4112 | return obj; | |
4113 | err2: | |
4114 | kfree(obj); | |
4115 | err1: | |
4116 | return ERR_PTR(err); | |
4117 | } | |
4118 | ||
4119 | static int nft_object_dump(struct sk_buff *skb, unsigned int attr, | |
43da04a5 | 4120 | struct nft_object *obj, bool reset) |
e5009240 PNA |
4121 | { |
4122 | struct nlattr *nest; | |
4123 | ||
4124 | nest = nla_nest_start(skb, attr); | |
4125 | if (!nest) | |
4126 | goto nla_put_failure; | |
43da04a5 | 4127 | if (obj->type->dump(skb, obj, reset) < 0) |
e5009240 PNA |
4128 | goto nla_put_failure; |
4129 | nla_nest_end(skb, nest); | |
4130 | return 0; | |
4131 | ||
4132 | nla_put_failure: | |
4133 | return -1; | |
4134 | } | |
4135 | ||
4136 | static const struct nft_object_type *__nft_obj_type_get(u32 objtype) | |
4137 | { | |
4138 | const struct nft_object_type *type; | |
4139 | ||
4140 | list_for_each_entry(type, &nf_tables_objects, list) { | |
4141 | if (objtype == type->type) | |
4142 | return type; | |
4143 | } | |
4144 | return NULL; | |
4145 | } | |
4146 | ||
4147 | static const struct nft_object_type *nft_obj_type_get(u32 objtype) | |
4148 | { | |
4149 | const struct nft_object_type *type; | |
4150 | ||
4151 | type = __nft_obj_type_get(objtype); | |
4152 | if (type != NULL && try_module_get(type->owner)) | |
4153 | return type; | |
4154 | ||
4155 | #ifdef CONFIG_MODULES | |
4156 | if (type == NULL) { | |
4157 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
4158 | request_module("nft-obj-%u", objtype); | |
4159 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
4160 | if (__nft_obj_type_get(objtype)) | |
4161 | return ERR_PTR(-EAGAIN); | |
4162 | } | |
4163 | #endif | |
4164 | return ERR_PTR(-ENOENT); | |
4165 | } | |
4166 | ||
4167 | static int nf_tables_newobj(struct net *net, struct sock *nlsk, | |
4168 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
4169 | const struct nlattr * const nla[]) | |
4170 | { | |
4171 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
4172 | const struct nft_object_type *type; | |
4173 | u8 genmask = nft_genmask_next(net); | |
4174 | int family = nfmsg->nfgen_family; | |
4175 | struct nft_af_info *afi; | |
4176 | struct nft_table *table; | |
4177 | struct nft_object *obj; | |
4178 | struct nft_ctx ctx; | |
4179 | u32 objtype; | |
4180 | int err; | |
4181 | ||
4182 | if (!nla[NFTA_OBJ_TYPE] || | |
4183 | !nla[NFTA_OBJ_NAME] || | |
4184 | !nla[NFTA_OBJ_DATA]) | |
4185 | return -EINVAL; | |
4186 | ||
4187 | afi = nf_tables_afinfo_lookup(net, family, true); | |
4188 | if (IS_ERR(afi)) | |
4189 | return PTR_ERR(afi); | |
4190 | ||
4191 | table = nf_tables_table_lookup(afi, nla[NFTA_OBJ_TABLE], genmask); | |
4192 | if (IS_ERR(table)) | |
4193 | return PTR_ERR(table); | |
4194 | ||
4195 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4196 | obj = nf_tables_obj_lookup(table, nla[NFTA_OBJ_NAME], objtype, genmask); | |
4197 | if (IS_ERR(obj)) { | |
4198 | err = PTR_ERR(obj); | |
4199 | if (err != -ENOENT) | |
4200 | return err; | |
4201 | ||
1a28ad74 | 4202 | } else { |
e5009240 PNA |
4203 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
4204 | return -EEXIST; | |
4205 | ||
4206 | return 0; | |
4207 | } | |
4208 | ||
4209 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla); | |
4210 | ||
4211 | type = nft_obj_type_get(objtype); | |
4212 | if (IS_ERR(type)) | |
4213 | return PTR_ERR(type); | |
4214 | ||
4215 | obj = nft_obj_init(type, nla[NFTA_OBJ_DATA]); | |
4216 | if (IS_ERR(obj)) { | |
4217 | err = PTR_ERR(obj); | |
4218 | goto err1; | |
4219 | } | |
18965317 | 4220 | obj->table = table; |
e5009240 PNA |
4221 | nla_strlcpy(obj->name, nla[NFTA_OBJ_NAME], NFT_OBJ_MAXNAMELEN); |
4222 | ||
4223 | err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj); | |
4224 | if (err < 0) | |
4225 | goto err2; | |
4226 | ||
4227 | list_add_tail_rcu(&obj->list, &table->objects); | |
4228 | table->use++; | |
4229 | return 0; | |
4230 | err2: | |
4231 | if (obj->type->destroy) | |
4232 | obj->type->destroy(obj); | |
4233 | kfree(obj); | |
4234 | err1: | |
4235 | module_put(type->owner); | |
4236 | return err; | |
4237 | } | |
4238 | ||
4239 | static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net, | |
4240 | u32 portid, u32 seq, int event, u32 flags, | |
4241 | int family, const struct nft_table *table, | |
43da04a5 | 4242 | struct nft_object *obj, bool reset) |
e5009240 PNA |
4243 | { |
4244 | struct nfgenmsg *nfmsg; | |
4245 | struct nlmsghdr *nlh; | |
4246 | ||
4247 | event |= NFNL_SUBSYS_NFTABLES << 8; | |
4248 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); | |
4249 | if (nlh == NULL) | |
4250 | goto nla_put_failure; | |
4251 | ||
4252 | nfmsg = nlmsg_data(nlh); | |
4253 | nfmsg->nfgen_family = family; | |
4254 | nfmsg->version = NFNETLINK_V0; | |
4255 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); | |
4256 | ||
4257 | if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) || | |
4258 | nla_put_string(skb, NFTA_OBJ_NAME, obj->name) || | |
4259 | nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->type->type)) || | |
4260 | nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) || | |
43da04a5 | 4261 | nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset)) |
e5009240 PNA |
4262 | goto nla_put_failure; |
4263 | ||
4264 | nlmsg_end(skb, nlh); | |
4265 | return 0; | |
4266 | ||
4267 | nla_put_failure: | |
4268 | nlmsg_trim(skb, nlh); | |
4269 | return -1; | |
4270 | } | |
4271 | ||
a9fea2a3 PNA |
4272 | struct nft_obj_filter { |
4273 | char table[NFT_OBJ_MAXNAMELEN]; | |
4274 | u32 type; | |
4275 | }; | |
4276 | ||
e5009240 PNA |
4277 | static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) |
4278 | { | |
4279 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
4280 | const struct nft_af_info *afi; | |
4281 | const struct nft_table *table; | |
e5009240 | 4282 | unsigned int idx = 0, s_idx = cb->args[0]; |
a9fea2a3 | 4283 | struct nft_obj_filter *filter = cb->data; |
e5009240 PNA |
4284 | struct net *net = sock_net(skb->sk); |
4285 | int family = nfmsg->nfgen_family; | |
43da04a5 PNA |
4286 | struct nft_object *obj; |
4287 | bool reset = false; | |
4288 | ||
4289 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) | |
4290 | reset = true; | |
e5009240 PNA |
4291 | |
4292 | rcu_read_lock(); | |
4293 | cb->seq = net->nft.base_seq; | |
4294 | ||
4295 | list_for_each_entry_rcu(afi, &net->nft.af_info, list) { | |
4296 | if (family != NFPROTO_UNSPEC && family != afi->family) | |
4297 | continue; | |
4298 | ||
4299 | list_for_each_entry_rcu(table, &afi->tables, list) { | |
4300 | list_for_each_entry_rcu(obj, &table->objects, list) { | |
4301 | if (!nft_is_active(net, obj)) | |
4302 | goto cont; | |
4303 | if (idx < s_idx) | |
4304 | goto cont; | |
4305 | if (idx > s_idx) | |
4306 | memset(&cb->args[1], 0, | |
4307 | sizeof(cb->args) - sizeof(cb->args[0])); | |
d21e540b | 4308 | if (filter && filter->table[0] && |
a9fea2a3 PNA |
4309 | strcmp(filter->table, table->name)) |
4310 | goto cont; | |
d21e540b LZ |
4311 | if (filter && |
4312 | filter->type != NFT_OBJECT_UNSPEC && | |
a9fea2a3 PNA |
4313 | obj->type->type != filter->type) |
4314 | goto cont; | |
4315 | ||
e5009240 PNA |
4316 | if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid, |
4317 | cb->nlh->nlmsg_seq, | |
4318 | NFT_MSG_NEWOBJ, | |
4319 | NLM_F_MULTI | NLM_F_APPEND, | |
43da04a5 | 4320 | afi->family, table, obj, reset) < 0) |
e5009240 PNA |
4321 | goto done; |
4322 | ||
4323 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
4324 | cont: | |
4325 | idx++; | |
4326 | } | |
4327 | } | |
4328 | } | |
4329 | done: | |
4330 | rcu_read_unlock(); | |
4331 | ||
4332 | cb->args[0] = idx; | |
4333 | return skb->len; | |
4334 | } | |
4335 | ||
a9fea2a3 PNA |
4336 | static int nf_tables_dump_obj_done(struct netlink_callback *cb) |
4337 | { | |
4338 | kfree(cb->data); | |
4339 | ||
4340 | return 0; | |
4341 | } | |
4342 | ||
4343 | static struct nft_obj_filter * | |
4344 | nft_obj_filter_alloc(const struct nlattr * const nla[]) | |
4345 | { | |
4346 | struct nft_obj_filter *filter; | |
4347 | ||
4348 | filter = kzalloc(sizeof(*filter), GFP_KERNEL); | |
4349 | if (!filter) | |
4350 | return ERR_PTR(-ENOMEM); | |
4351 | ||
4352 | if (nla[NFTA_OBJ_TABLE]) | |
4353 | nla_strlcpy(filter->table, nla[NFTA_OBJ_TABLE], | |
4354 | NFT_TABLE_MAXNAMELEN); | |
4355 | if (nla[NFTA_OBJ_TYPE]) | |
4356 | filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4357 | ||
4358 | return filter; | |
4359 | } | |
4360 | ||
e5009240 PNA |
4361 | static int nf_tables_getobj(struct net *net, struct sock *nlsk, |
4362 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
4363 | const struct nlattr * const nla[]) | |
4364 | { | |
4365 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
4366 | u8 genmask = nft_genmask_cur(net); | |
4367 | int family = nfmsg->nfgen_family; | |
4368 | const struct nft_af_info *afi; | |
4369 | const struct nft_table *table; | |
4370 | struct nft_object *obj; | |
4371 | struct sk_buff *skb2; | |
43da04a5 | 4372 | bool reset = false; |
e5009240 PNA |
4373 | u32 objtype; |
4374 | int err; | |
4375 | ||
4376 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
4377 | struct netlink_dump_control c = { | |
4378 | .dump = nf_tables_dump_obj, | |
a9fea2a3 | 4379 | .done = nf_tables_dump_obj_done, |
e5009240 | 4380 | }; |
a9fea2a3 PNA |
4381 | |
4382 | if (nla[NFTA_OBJ_TABLE] || | |
4383 | nla[NFTA_OBJ_TYPE]) { | |
4384 | struct nft_obj_filter *filter; | |
4385 | ||
4386 | filter = nft_obj_filter_alloc(nla); | |
4387 | if (IS_ERR(filter)) | |
4388 | return -ENOMEM; | |
4389 | ||
4390 | c.data = filter; | |
4391 | } | |
e5009240 PNA |
4392 | return netlink_dump_start(nlsk, skb, nlh, &c); |
4393 | } | |
4394 | ||
4395 | if (!nla[NFTA_OBJ_NAME] || | |
4396 | !nla[NFTA_OBJ_TYPE]) | |
4397 | return -EINVAL; | |
4398 | ||
4399 | afi = nf_tables_afinfo_lookup(net, family, false); | |
4400 | if (IS_ERR(afi)) | |
4401 | return PTR_ERR(afi); | |
4402 | ||
4403 | table = nf_tables_table_lookup(afi, nla[NFTA_OBJ_TABLE], genmask); | |
4404 | if (IS_ERR(table)) | |
4405 | return PTR_ERR(table); | |
4406 | ||
4407 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4408 | obj = nf_tables_obj_lookup(table, nla[NFTA_OBJ_NAME], objtype, genmask); | |
4409 | if (IS_ERR(obj)) | |
4410 | return PTR_ERR(obj); | |
4411 | ||
4412 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
4413 | if (!skb2) | |
4414 | return -ENOMEM; | |
4415 | ||
43da04a5 PNA |
4416 | if (NFNL_MSG_TYPE(nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) |
4417 | reset = true; | |
4418 | ||
e5009240 PNA |
4419 | err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid, |
4420 | nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0, | |
43da04a5 | 4421 | family, table, obj, reset); |
e5009240 PNA |
4422 | if (err < 0) |
4423 | goto err; | |
4424 | ||
4425 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
4426 | err: | |
4427 | kfree_skb(skb2); | |
4428 | return err; | |
4429 | ||
4430 | return 0; | |
4431 | } | |
4432 | ||
4433 | static void nft_obj_destroy(struct nft_object *obj) | |
4434 | { | |
4435 | if (obj->type->destroy) | |
4436 | obj->type->destroy(obj); | |
4437 | ||
4438 | module_put(obj->type->owner); | |
4439 | kfree(obj); | |
4440 | } | |
4441 | ||
4442 | static int nf_tables_delobj(struct net *net, struct sock *nlsk, | |
4443 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
4444 | const struct nlattr * const nla[]) | |
4445 | { | |
4446 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
4447 | u8 genmask = nft_genmask_next(net); | |
4448 | int family = nfmsg->nfgen_family; | |
4449 | struct nft_af_info *afi; | |
4450 | struct nft_table *table; | |
4451 | struct nft_object *obj; | |
4452 | struct nft_ctx ctx; | |
4453 | u32 objtype; | |
4454 | ||
4455 | if (!nla[NFTA_OBJ_TYPE] || | |
4456 | !nla[NFTA_OBJ_NAME]) | |
4457 | return -EINVAL; | |
4458 | ||
4459 | afi = nf_tables_afinfo_lookup(net, family, true); | |
4460 | if (IS_ERR(afi)) | |
4461 | return PTR_ERR(afi); | |
4462 | ||
4463 | table = nf_tables_table_lookup(afi, nla[NFTA_OBJ_TABLE], genmask); | |
4464 | if (IS_ERR(table)) | |
4465 | return PTR_ERR(table); | |
4466 | ||
4467 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4468 | obj = nf_tables_obj_lookup(table, nla[NFTA_OBJ_NAME], objtype, genmask); | |
4469 | if (IS_ERR(obj)) | |
4470 | return PTR_ERR(obj); | |
4471 | if (obj->use > 0) | |
4472 | return -EBUSY; | |
4473 | ||
4474 | nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla); | |
4475 | ||
4476 | return nft_delobj(&ctx, obj); | |
4477 | } | |
4478 | ||
2599e989 PNA |
4479 | int nft_obj_notify(struct net *net, struct nft_table *table, |
4480 | struct nft_object *obj, u32 portid, u32 seq, int event, | |
4481 | int family, int report, gfp_t gfp) | |
e5009240 PNA |
4482 | { |
4483 | struct sk_buff *skb; | |
4484 | int err; | |
4485 | ||
2599e989 PNA |
4486 | if (!report && |
4487 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
e5009240 PNA |
4488 | return 0; |
4489 | ||
4490 | err = -ENOBUFS; | |
2599e989 | 4491 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp); |
e5009240 PNA |
4492 | if (skb == NULL) |
4493 | goto err; | |
4494 | ||
2599e989 PNA |
4495 | err = nf_tables_fill_obj_info(skb, net, portid, seq, event, 0, family, |
4496 | table, obj, false); | |
e5009240 PNA |
4497 | if (err < 0) { |
4498 | kfree_skb(skb); | |
4499 | goto err; | |
4500 | } | |
4501 | ||
2599e989 | 4502 | err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report, gfp); |
e5009240 PNA |
4503 | err: |
4504 | if (err < 0) { | |
2599e989 | 4505 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err); |
e5009240 PNA |
4506 | } |
4507 | return err; | |
4508 | } | |
2599e989 PNA |
4509 | EXPORT_SYMBOL_GPL(nft_obj_notify); |
4510 | ||
4511 | static int nf_tables_obj_notify(const struct nft_ctx *ctx, | |
4512 | struct nft_object *obj, int event) | |
4513 | { | |
4514 | return nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, | |
4515 | ctx->seq, event, ctx->afi->family, ctx->report, | |
4516 | GFP_KERNEL); | |
4517 | } | |
e5009240 | 4518 | |
84d7fce6 PNA |
4519 | static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, |
4520 | u32 portid, u32 seq) | |
4521 | { | |
4522 | struct nlmsghdr *nlh; | |
4523 | struct nfgenmsg *nfmsg; | |
4524 | int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN; | |
4525 | ||
4526 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0); | |
4527 | if (nlh == NULL) | |
4528 | goto nla_put_failure; | |
4529 | ||
4530 | nfmsg = nlmsg_data(nlh); | |
4531 | nfmsg->nfgen_family = AF_UNSPEC; | |
4532 | nfmsg->version = NFNETLINK_V0; | |
4533 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); | |
4534 | ||
4535 | if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq))) | |
4536 | goto nla_put_failure; | |
4537 | ||
053c095a JB |
4538 | nlmsg_end(skb, nlh); |
4539 | return 0; | |
84d7fce6 PNA |
4540 | |
4541 | nla_put_failure: | |
4542 | nlmsg_trim(skb, nlh); | |
4543 | return -EMSGSIZE; | |
4544 | } | |
4545 | ||
4546 | static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event) | |
4547 | { | |
4548 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
4549 | struct sk_buff *skb2; | |
4550 | int err; | |
4551 | ||
4552 | if (nlmsg_report(nlh) && | |
4553 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
4554 | return 0; | |
4555 | ||
4556 | err = -ENOBUFS; | |
4557 | skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
4558 | if (skb2 == NULL) | |
4559 | goto err; | |
4560 | ||
4561 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
4562 | nlh->nlmsg_seq); | |
4563 | if (err < 0) { | |
4564 | kfree_skb(skb2); | |
4565 | goto err; | |
4566 | } | |
4567 | ||
4568 | err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, | |
4569 | NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL); | |
4570 | err: | |
4571 | if (err < 0) { | |
4572 | nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, | |
4573 | err); | |
4574 | } | |
4575 | return err; | |
4576 | } | |
4577 | ||
7b8002a1 PNA |
4578 | static int nf_tables_getgen(struct net *net, struct sock *nlsk, |
4579 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
84d7fce6 PNA |
4580 | const struct nlattr * const nla[]) |
4581 | { | |
84d7fce6 PNA |
4582 | struct sk_buff *skb2; |
4583 | int err; | |
4584 | ||
4585 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
4586 | if (skb2 == NULL) | |
4587 | return -ENOMEM; | |
4588 | ||
4589 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
4590 | nlh->nlmsg_seq); | |
4591 | if (err < 0) | |
4592 | goto err; | |
4593 | ||
4594 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
4595 | err: | |
4596 | kfree_skb(skb2); | |
4597 | return err; | |
4598 | } | |
4599 | ||
96518518 PM |
4600 | static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { |
4601 | [NFT_MSG_NEWTABLE] = { | |
55dd6f93 | 4602 | .call_batch = nf_tables_newtable, |
96518518 PM |
4603 | .attr_count = NFTA_TABLE_MAX, |
4604 | .policy = nft_table_policy, | |
4605 | }, | |
4606 | [NFT_MSG_GETTABLE] = { | |
4607 | .call = nf_tables_gettable, | |
4608 | .attr_count = NFTA_TABLE_MAX, | |
4609 | .policy = nft_table_policy, | |
4610 | }, | |
4611 | [NFT_MSG_DELTABLE] = { | |
55dd6f93 | 4612 | .call_batch = nf_tables_deltable, |
96518518 PM |
4613 | .attr_count = NFTA_TABLE_MAX, |
4614 | .policy = nft_table_policy, | |
4615 | }, | |
4616 | [NFT_MSG_NEWCHAIN] = { | |
91c7b38d | 4617 | .call_batch = nf_tables_newchain, |
96518518 PM |
4618 | .attr_count = NFTA_CHAIN_MAX, |
4619 | .policy = nft_chain_policy, | |
4620 | }, | |
4621 | [NFT_MSG_GETCHAIN] = { | |
4622 | .call = nf_tables_getchain, | |
4623 | .attr_count = NFTA_CHAIN_MAX, | |
4624 | .policy = nft_chain_policy, | |
4625 | }, | |
4626 | [NFT_MSG_DELCHAIN] = { | |
91c7b38d | 4627 | .call_batch = nf_tables_delchain, |
96518518 PM |
4628 | .attr_count = NFTA_CHAIN_MAX, |
4629 | .policy = nft_chain_policy, | |
4630 | }, | |
4631 | [NFT_MSG_NEWRULE] = { | |
0628b123 | 4632 | .call_batch = nf_tables_newrule, |
96518518 PM |
4633 | .attr_count = NFTA_RULE_MAX, |
4634 | .policy = nft_rule_policy, | |
4635 | }, | |
4636 | [NFT_MSG_GETRULE] = { | |
4637 | .call = nf_tables_getrule, | |
4638 | .attr_count = NFTA_RULE_MAX, | |
4639 | .policy = nft_rule_policy, | |
4640 | }, | |
4641 | [NFT_MSG_DELRULE] = { | |
0628b123 | 4642 | .call_batch = nf_tables_delrule, |
96518518 PM |
4643 | .attr_count = NFTA_RULE_MAX, |
4644 | .policy = nft_rule_policy, | |
4645 | }, | |
20a69341 | 4646 | [NFT_MSG_NEWSET] = { |
958bee14 | 4647 | .call_batch = nf_tables_newset, |
20a69341 PM |
4648 | .attr_count = NFTA_SET_MAX, |
4649 | .policy = nft_set_policy, | |
4650 | }, | |
4651 | [NFT_MSG_GETSET] = { | |
4652 | .call = nf_tables_getset, | |
4653 | .attr_count = NFTA_SET_MAX, | |
4654 | .policy = nft_set_policy, | |
4655 | }, | |
4656 | [NFT_MSG_DELSET] = { | |
958bee14 | 4657 | .call_batch = nf_tables_delset, |
20a69341 PM |
4658 | .attr_count = NFTA_SET_MAX, |
4659 | .policy = nft_set_policy, | |
4660 | }, | |
4661 | [NFT_MSG_NEWSETELEM] = { | |
958bee14 | 4662 | .call_batch = nf_tables_newsetelem, |
20a69341 PM |
4663 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
4664 | .policy = nft_set_elem_list_policy, | |
4665 | }, | |
4666 | [NFT_MSG_GETSETELEM] = { | |
4667 | .call = nf_tables_getsetelem, | |
4668 | .attr_count = NFTA_SET_ELEM_LIST_MAX, | |
4669 | .policy = nft_set_elem_list_policy, | |
4670 | }, | |
4671 | [NFT_MSG_DELSETELEM] = { | |
958bee14 | 4672 | .call_batch = nf_tables_delsetelem, |
20a69341 PM |
4673 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
4674 | .policy = nft_set_elem_list_policy, | |
4675 | }, | |
84d7fce6 PNA |
4676 | [NFT_MSG_GETGEN] = { |
4677 | .call = nf_tables_getgen, | |
4678 | }, | |
e5009240 PNA |
4679 | [NFT_MSG_NEWOBJ] = { |
4680 | .call_batch = nf_tables_newobj, | |
4681 | .attr_count = NFTA_OBJ_MAX, | |
4682 | .policy = nft_obj_policy, | |
4683 | }, | |
4684 | [NFT_MSG_GETOBJ] = { | |
4685 | .call = nf_tables_getobj, | |
4686 | .attr_count = NFTA_OBJ_MAX, | |
4687 | .policy = nft_obj_policy, | |
4688 | }, | |
4689 | [NFT_MSG_DELOBJ] = { | |
4690 | .call_batch = nf_tables_delobj, | |
4691 | .attr_count = NFTA_OBJ_MAX, | |
4692 | .policy = nft_obj_policy, | |
4693 | }, | |
43da04a5 PNA |
4694 | [NFT_MSG_GETOBJ_RESET] = { |
4695 | .call = nf_tables_getobj, | |
4696 | .attr_count = NFTA_OBJ_MAX, | |
4697 | .policy = nft_obj_policy, | |
4698 | }, | |
96518518 PM |
4699 | }; |
4700 | ||
91c7b38d PNA |
4701 | static void nft_chain_commit_update(struct nft_trans *trans) |
4702 | { | |
4703 | struct nft_base_chain *basechain; | |
4704 | ||
4705 | if (nft_trans_chain_name(trans)[0]) | |
4706 | strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans)); | |
4707 | ||
4708 | if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN)) | |
4709 | return; | |
4710 | ||
4711 | basechain = nft_base_chain(trans->ctx.chain); | |
4712 | nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans)); | |
4713 | ||
4714 | switch (nft_trans_chain_policy(trans)) { | |
4715 | case NF_DROP: | |
4716 | case NF_ACCEPT: | |
4717 | basechain->policy = nft_trans_chain_policy(trans); | |
4718 | break; | |
4719 | } | |
4720 | } | |
4721 | ||
b326dd37 | 4722 | static void nf_tables_commit_release(struct nft_trans *trans) |
c7c32e72 | 4723 | { |
c7c32e72 PNA |
4724 | switch (trans->msg_type) { |
4725 | case NFT_MSG_DELTABLE: | |
4726 | nf_tables_table_destroy(&trans->ctx); | |
4727 | break; | |
4728 | case NFT_MSG_DELCHAIN: | |
4729 | nf_tables_chain_destroy(trans->ctx.chain); | |
4730 | break; | |
4731 | case NFT_MSG_DELRULE: | |
4732 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
4733 | break; | |
4734 | case NFT_MSG_DELSET: | |
4735 | nft_set_destroy(nft_trans_set(trans)); | |
4736 | break; | |
61edafbb PM |
4737 | case NFT_MSG_DELSETELEM: |
4738 | nft_set_elem_destroy(nft_trans_elem_set(trans), | |
61f9e292 | 4739 | nft_trans_elem(trans).priv, true); |
61edafbb | 4740 | break; |
e5009240 PNA |
4741 | case NFT_MSG_DELOBJ: |
4742 | nft_obj_destroy(nft_trans_obj(trans)); | |
4743 | break; | |
c7c32e72 PNA |
4744 | } |
4745 | kfree(trans); | |
4746 | } | |
4747 | ||
5913beaf | 4748 | static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
37082f93 | 4749 | { |
37082f93 | 4750 | struct nft_trans *trans, *next; |
a3716e70 | 4751 | struct nft_trans_elem *te; |
37082f93 PNA |
4752 | |
4753 | /* Bump generation counter, invalidate any dump in progress */ | |
38e029f1 | 4754 | while (++net->nft.base_seq == 0); |
37082f93 PNA |
4755 | |
4756 | /* A new generation has just started */ | |
ea4bd995 | 4757 | net->nft.gencursor = nft_gencursor_next(net); |
37082f93 PNA |
4758 | |
4759 | /* Make sure all packets have left the previous generation before | |
4760 | * purging old rules. | |
4761 | */ | |
4762 | synchronize_rcu(); | |
4763 | ||
4764 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { | |
b380e5c7 | 4765 | switch (trans->msg_type) { |
55dd6f93 PNA |
4766 | case NFT_MSG_NEWTABLE: |
4767 | if (nft_trans_table_update(trans)) { | |
4768 | if (!nft_trans_table_enable(trans)) { | |
664b0f8c PNA |
4769 | nf_tables_table_disable(net, |
4770 | trans->ctx.afi, | |
55dd6f93 PNA |
4771 | trans->ctx.table); |
4772 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; | |
4773 | } | |
4774 | } else { | |
f2a6d766 | 4775 | nft_clear(net, trans->ctx.table); |
55dd6f93 | 4776 | } |
35151d84 | 4777 | nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE); |
55dd6f93 PNA |
4778 | nft_trans_destroy(trans); |
4779 | break; | |
4780 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 4781 | list_del_rcu(&trans->ctx.table->list); |
35151d84 | 4782 | nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE); |
55dd6f93 | 4783 | break; |
91c7b38d PNA |
4784 | case NFT_MSG_NEWCHAIN: |
4785 | if (nft_trans_chain_update(trans)) | |
4786 | nft_chain_commit_update(trans); | |
4fefee57 | 4787 | else |
664b0f8c | 4788 | nft_clear(net, trans->ctx.chain); |
4fefee57 | 4789 | |
35151d84 | 4790 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
91c7b38d PNA |
4791 | nft_trans_destroy(trans); |
4792 | break; | |
4793 | case NFT_MSG_DELCHAIN: | |
664b0f8c | 4794 | list_del_rcu(&trans->ctx.chain->list); |
35151d84 | 4795 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN); |
82bec71d PNA |
4796 | nf_tables_unregister_hooks(trans->ctx.net, |
4797 | trans->ctx.table, | |
c5598794 AB |
4798 | trans->ctx.chain, |
4799 | trans->ctx.afi->nops); | |
91c7b38d | 4800 | break; |
b380e5c7 | 4801 | case NFT_MSG_NEWRULE: |
889f7ee7 | 4802 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
35151d84 | 4803 | nf_tables_rule_notify(&trans->ctx, |
37082f93 | 4804 | nft_trans_rule(trans), |
35151d84 | 4805 | NFT_MSG_NEWRULE); |
37082f93 | 4806 | nft_trans_destroy(trans); |
b380e5c7 PNA |
4807 | break; |
4808 | case NFT_MSG_DELRULE: | |
4809 | list_del_rcu(&nft_trans_rule(trans)->list); | |
35151d84 PNA |
4810 | nf_tables_rule_notify(&trans->ctx, |
4811 | nft_trans_rule(trans), | |
4812 | NFT_MSG_DELRULE); | |
b380e5c7 | 4813 | break; |
958bee14 | 4814 | case NFT_MSG_NEWSET: |
37a9cc52 | 4815 | nft_clear(net, nft_trans_set(trans)); |
4fefee57 PNA |
4816 | /* This avoids hitting -EBUSY when deleting the table |
4817 | * from the transaction. | |
4818 | */ | |
4819 | if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS && | |
4820 | !list_empty(&nft_trans_set(trans)->bindings)) | |
4821 | trans->ctx.table->use--; | |
4822 | ||
958bee14 | 4823 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 4824 | NFT_MSG_NEWSET, GFP_KERNEL); |
958bee14 PNA |
4825 | nft_trans_destroy(trans); |
4826 | break; | |
4827 | case NFT_MSG_DELSET: | |
37a9cc52 | 4828 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 | 4829 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 4830 | NFT_MSG_DELSET, GFP_KERNEL); |
958bee14 | 4831 | break; |
60319eb1 | 4832 | case NFT_MSG_NEWSETELEM: |
cc02e457 PM |
4833 | te = (struct nft_trans_elem *)trans->data; |
4834 | ||
42a55769 | 4835 | te->set->ops->activate(net, te->set, &te->elem); |
cc02e457 PM |
4836 | nf_tables_setelem_notify(&trans->ctx, te->set, |
4837 | &te->elem, | |
60319eb1 PNA |
4838 | NFT_MSG_NEWSETELEM, 0); |
4839 | nft_trans_destroy(trans); | |
4840 | break; | |
4841 | case NFT_MSG_DELSETELEM: | |
a3716e70 | 4842 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 4843 | |
a3716e70 PNA |
4844 | nf_tables_setelem_notify(&trans->ctx, te->set, |
4845 | &te->elem, | |
60319eb1 | 4846 | NFT_MSG_DELSETELEM, 0); |
5cb82a38 | 4847 | te->set->ops->remove(net, te->set, &te->elem); |
3dd0673a PM |
4848 | atomic_dec(&te->set->nelems); |
4849 | te->set->ndeact--; | |
60319eb1 | 4850 | break; |
e5009240 PNA |
4851 | case NFT_MSG_NEWOBJ: |
4852 | nft_clear(net, nft_trans_obj(trans)); | |
4853 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), | |
4854 | NFT_MSG_NEWOBJ); | |
4855 | nft_trans_destroy(trans); | |
4856 | break; | |
4857 | case NFT_MSG_DELOBJ: | |
4858 | list_del_rcu(&nft_trans_obj(trans)->list); | |
4859 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), | |
4860 | NFT_MSG_DELOBJ); | |
4861 | break; | |
37082f93 | 4862 | } |
37082f93 PNA |
4863 | } |
4864 | ||
b326dd37 PNA |
4865 | synchronize_rcu(); |
4866 | ||
37082f93 | 4867 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { |
c7c32e72 | 4868 | list_del(&trans->list); |
b326dd37 | 4869 | nf_tables_commit_release(trans); |
37082f93 | 4870 | } |
84d7fce6 PNA |
4871 | |
4872 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); | |
37082f93 PNA |
4873 | |
4874 | return 0; | |
4875 | } | |
4876 | ||
b326dd37 | 4877 | static void nf_tables_abort_release(struct nft_trans *trans) |
c7c32e72 | 4878 | { |
c7c32e72 PNA |
4879 | switch (trans->msg_type) { |
4880 | case NFT_MSG_NEWTABLE: | |
4881 | nf_tables_table_destroy(&trans->ctx); | |
4882 | break; | |
4883 | case NFT_MSG_NEWCHAIN: | |
4884 | nf_tables_chain_destroy(trans->ctx.chain); | |
4885 | break; | |
4886 | case NFT_MSG_NEWRULE: | |
4887 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
4888 | break; | |
4889 | case NFT_MSG_NEWSET: | |
4890 | nft_set_destroy(nft_trans_set(trans)); | |
4891 | break; | |
61edafbb PM |
4892 | case NFT_MSG_NEWSETELEM: |
4893 | nft_set_elem_destroy(nft_trans_elem_set(trans), | |
61f9e292 | 4894 | nft_trans_elem(trans).priv, true); |
61edafbb | 4895 | break; |
e5009240 PNA |
4896 | case NFT_MSG_NEWOBJ: |
4897 | nft_obj_destroy(nft_trans_obj(trans)); | |
4898 | break; | |
c7c32e72 PNA |
4899 | } |
4900 | kfree(trans); | |
4901 | } | |
4902 | ||
5913beaf | 4903 | static int nf_tables_abort(struct net *net, struct sk_buff *skb) |
37082f93 | 4904 | { |
37082f93 | 4905 | struct nft_trans *trans, *next; |
02263db0 | 4906 | struct nft_trans_elem *te; |
37082f93 | 4907 | |
a907e36d XL |
4908 | list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list, |
4909 | list) { | |
b380e5c7 | 4910 | switch (trans->msg_type) { |
55dd6f93 PNA |
4911 | case NFT_MSG_NEWTABLE: |
4912 | if (nft_trans_table_update(trans)) { | |
4913 | if (nft_trans_table_enable(trans)) { | |
664b0f8c PNA |
4914 | nf_tables_table_disable(net, |
4915 | trans->ctx.afi, | |
55dd6f93 PNA |
4916 | trans->ctx.table); |
4917 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; | |
4918 | } | |
4919 | nft_trans_destroy(trans); | |
4920 | } else { | |
e688a7f8 | 4921 | list_del_rcu(&trans->ctx.table->list); |
55dd6f93 PNA |
4922 | } |
4923 | break; | |
4924 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 4925 | nft_clear(trans->ctx.net, trans->ctx.table); |
55dd6f93 PNA |
4926 | nft_trans_destroy(trans); |
4927 | break; | |
91c7b38d PNA |
4928 | case NFT_MSG_NEWCHAIN: |
4929 | if (nft_trans_chain_update(trans)) { | |
982f4051 | 4930 | free_percpu(nft_trans_chain_stats(trans)); |
91c7b38d PNA |
4931 | |
4932 | nft_trans_destroy(trans); | |
4933 | } else { | |
4fefee57 | 4934 | trans->ctx.table->use--; |
e688a7f8 | 4935 | list_del_rcu(&trans->ctx.chain->list); |
82bec71d PNA |
4936 | nf_tables_unregister_hooks(trans->ctx.net, |
4937 | trans->ctx.table, | |
c5598794 AB |
4938 | trans->ctx.chain, |
4939 | trans->ctx.afi->nops); | |
91c7b38d PNA |
4940 | } |
4941 | break; | |
4942 | case NFT_MSG_DELCHAIN: | |
4fefee57 | 4943 | trans->ctx.table->use++; |
664b0f8c | 4944 | nft_clear(trans->ctx.net, trans->ctx.chain); |
91c7b38d PNA |
4945 | nft_trans_destroy(trans); |
4946 | break; | |
b380e5c7 | 4947 | case NFT_MSG_NEWRULE: |
4fefee57 | 4948 | trans->ctx.chain->use--; |
b380e5c7 PNA |
4949 | list_del_rcu(&nft_trans_rule(trans)->list); |
4950 | break; | |
4951 | case NFT_MSG_DELRULE: | |
4fefee57 | 4952 | trans->ctx.chain->use++; |
889f7ee7 | 4953 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
37082f93 | 4954 | nft_trans_destroy(trans); |
b380e5c7 | 4955 | break; |
958bee14 | 4956 | case NFT_MSG_NEWSET: |
4fefee57 | 4957 | trans->ctx.table->use--; |
e688a7f8 | 4958 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 PNA |
4959 | break; |
4960 | case NFT_MSG_DELSET: | |
4fefee57 | 4961 | trans->ctx.table->use++; |
37a9cc52 | 4962 | nft_clear(trans->ctx.net, nft_trans_set(trans)); |
958bee14 PNA |
4963 | nft_trans_destroy(trans); |
4964 | break; | |
60319eb1 | 4965 | case NFT_MSG_NEWSETELEM: |
02263db0 | 4966 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 4967 | |
5cb82a38 | 4968 | te->set->ops->remove(net, te->set, &te->elem); |
3dd0673a | 4969 | atomic_dec(&te->set->nelems); |
60319eb1 PNA |
4970 | break; |
4971 | case NFT_MSG_DELSETELEM: | |
cc02e457 PM |
4972 | te = (struct nft_trans_elem *)trans->data; |
4973 | ||
42a55769 | 4974 | te->set->ops->activate(net, te->set, &te->elem); |
3dd0673a | 4975 | te->set->ndeact--; |
cc02e457 | 4976 | |
e5009240 PNA |
4977 | nft_trans_destroy(trans); |
4978 | break; | |
4979 | case NFT_MSG_NEWOBJ: | |
4980 | trans->ctx.table->use--; | |
4981 | list_del_rcu(&nft_trans_obj(trans)->list); | |
4982 | break; | |
4983 | case NFT_MSG_DELOBJ: | |
4984 | trans->ctx.table->use++; | |
4985 | nft_clear(trans->ctx.net, nft_trans_obj(trans)); | |
60319eb1 PNA |
4986 | nft_trans_destroy(trans); |
4987 | break; | |
37082f93 | 4988 | } |
37082f93 PNA |
4989 | } |
4990 | ||
b326dd37 PNA |
4991 | synchronize_rcu(); |
4992 | ||
a1cee076 PNA |
4993 | list_for_each_entry_safe_reverse(trans, next, |
4994 | &net->nft.commit_list, list) { | |
c7c32e72 | 4995 | list_del(&trans->list); |
b326dd37 | 4996 | nf_tables_abort_release(trans); |
37082f93 PNA |
4997 | } |
4998 | ||
4999 | return 0; | |
5000 | } | |
5001 | ||
74e8bcd2 PNA |
5002 | static bool nf_tables_valid_genid(struct net *net, u32 genid) |
5003 | { | |
5004 | return net->nft.base_seq == genid; | |
5005 | } | |
5006 | ||
96518518 PM |
5007 | static const struct nfnetlink_subsystem nf_tables_subsys = { |
5008 | .name = "nf_tables", | |
5009 | .subsys_id = NFNL_SUBSYS_NFTABLES, | |
5010 | .cb_count = NFT_MSG_MAX, | |
5011 | .cb = nf_tables_cb, | |
0628b123 PNA |
5012 | .commit = nf_tables_commit, |
5013 | .abort = nf_tables_abort, | |
74e8bcd2 | 5014 | .valid_genid = nf_tables_valid_genid, |
96518518 PM |
5015 | }; |
5016 | ||
7210e4e3 PNA |
5017 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
5018 | enum nft_chain_type type) | |
5019 | { | |
5020 | const struct nft_base_chain *basechain; | |
5021 | ||
5022 | if (chain->flags & NFT_BASE_CHAIN) { | |
5023 | basechain = nft_base_chain(chain); | |
5024 | if (basechain->type->type != type) | |
5025 | return -EOPNOTSUPP; | |
5026 | } | |
5027 | return 0; | |
5028 | } | |
5029 | EXPORT_SYMBOL_GPL(nft_chain_validate_dependency); | |
5030 | ||
75e8d06d PNA |
5031 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
5032 | unsigned int hook_flags) | |
5033 | { | |
5034 | struct nft_base_chain *basechain; | |
5035 | ||
5036 | if (chain->flags & NFT_BASE_CHAIN) { | |
5037 | basechain = nft_base_chain(chain); | |
5038 | ||
5039 | if ((1 << basechain->ops[0].hooknum) & hook_flags) | |
5040 | return 0; | |
5041 | ||
5042 | return -EOPNOTSUPP; | |
5043 | } | |
5044 | ||
5045 | return 0; | |
5046 | } | |
5047 | EXPORT_SYMBOL_GPL(nft_chain_validate_hooks); | |
5048 | ||
20a69341 PM |
5049 | /* |
5050 | * Loop detection - walk through the ruleset beginning at the destination chain | |
5051 | * of a new jump until either the source chain is reached (loop) or all | |
5052 | * reachable chains have been traversed. | |
5053 | * | |
5054 | * The loop check is performed whenever a new jump verdict is added to an | |
5055 | * expression or verdict map or a verdict map is bound to a new chain. | |
5056 | */ | |
5057 | ||
5058 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
5059 | const struct nft_chain *chain); | |
5060 | ||
5061 | static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, | |
de70185d | 5062 | struct nft_set *set, |
20a69341 | 5063 | const struct nft_set_iter *iter, |
de70185d | 5064 | struct nft_set_elem *elem) |
20a69341 | 5065 | { |
fe2811eb PM |
5066 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
5067 | const struct nft_data *data; | |
5068 | ||
5069 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
5070 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
62f9c8b4 PNA |
5071 | return 0; |
5072 | ||
fe2811eb | 5073 | data = nft_set_ext_data(ext); |
1ca2e170 | 5074 | switch (data->verdict.code) { |
20a69341 PM |
5075 | case NFT_JUMP: |
5076 | case NFT_GOTO: | |
1ca2e170 | 5077 | return nf_tables_check_loops(ctx, data->verdict.chain); |
20a69341 PM |
5078 | default: |
5079 | return 0; | |
5080 | } | |
5081 | } | |
5082 | ||
5083 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
5084 | const struct nft_chain *chain) | |
5085 | { | |
5086 | const struct nft_rule *rule; | |
5087 | const struct nft_expr *expr, *last; | |
de70185d | 5088 | struct nft_set *set; |
20a69341 PM |
5089 | struct nft_set_binding *binding; |
5090 | struct nft_set_iter iter; | |
20a69341 PM |
5091 | |
5092 | if (ctx->chain == chain) | |
5093 | return -ELOOP; | |
5094 | ||
5095 | list_for_each_entry(rule, &chain->rules, list) { | |
5096 | nft_rule_for_each_expr(expr, last, rule) { | |
0ca743a5 PNA |
5097 | const struct nft_data *data = NULL; |
5098 | int err; | |
5099 | ||
5100 | if (!expr->ops->validate) | |
20a69341 PM |
5101 | continue; |
5102 | ||
0ca743a5 PNA |
5103 | err = expr->ops->validate(ctx, expr, &data); |
5104 | if (err < 0) | |
5105 | return err; | |
5106 | ||
20a69341 | 5107 | if (data == NULL) |
0ca743a5 | 5108 | continue; |
20a69341 | 5109 | |
1ca2e170 | 5110 | switch (data->verdict.code) { |
20a69341 PM |
5111 | case NFT_JUMP: |
5112 | case NFT_GOTO: | |
1ca2e170 PM |
5113 | err = nf_tables_check_loops(ctx, |
5114 | data->verdict.chain); | |
20a69341 PM |
5115 | if (err < 0) |
5116 | return err; | |
5117 | default: | |
5118 | break; | |
5119 | } | |
5120 | } | |
5121 | } | |
5122 | ||
5123 | list_for_each_entry(set, &ctx->table->sets, list) { | |
37a9cc52 PNA |
5124 | if (!nft_is_active_next(ctx->net, set)) |
5125 | continue; | |
20a69341 PM |
5126 | if (!(set->flags & NFT_SET_MAP) || |
5127 | set->dtype != NFT_DATA_VERDICT) | |
5128 | continue; | |
5129 | ||
5130 | list_for_each_entry(binding, &set->bindings, list) { | |
11113e19 PM |
5131 | if (!(binding->flags & NFT_SET_MAP) || |
5132 | binding->chain != chain) | |
20a69341 PM |
5133 | continue; |
5134 | ||
8588ac09 | 5135 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
5136 | iter.skip = 0; |
5137 | iter.count = 0; | |
5138 | iter.err = 0; | |
5139 | iter.fn = nf_tables_loop_check_setelem; | |
1f48ff6c | 5140 | iter.flush = false; |
20a69341 PM |
5141 | |
5142 | set->ops->walk(ctx, set, &iter); | |
5143 | if (iter.err < 0) | |
5144 | return iter.err; | |
5145 | } | |
5146 | } | |
5147 | ||
5148 | return 0; | |
5149 | } | |
5150 | ||
36b701fa LGL |
5151 | /** |
5152 | * nft_parse_u32_check - fetch u32 attribute and check for maximum value | |
5153 | * | |
5154 | * @attr: netlink attribute to fetch value from | |
5155 | * @max: maximum value to be stored in dest | |
5156 | * @dest: pointer to the variable | |
5157 | * | |
5158 | * Parse, check and store a given u32 netlink attribute into variable. | |
5159 | * This function returns -ERANGE if the value goes over maximum value. | |
5160 | * Otherwise a 0 is returned and the attribute value is stored in the | |
5161 | * destination variable. | |
5162 | */ | |
f1d505bb | 5163 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest) |
36b701fa | 5164 | { |
09525a09 | 5165 | u32 val; |
36b701fa LGL |
5166 | |
5167 | val = ntohl(nla_get_be32(attr)); | |
5168 | if (val > max) | |
5169 | return -ERANGE; | |
5170 | ||
5171 | *dest = val; | |
5172 | return 0; | |
5173 | } | |
5174 | EXPORT_SYMBOL_GPL(nft_parse_u32_check); | |
5175 | ||
49499c3e PM |
5176 | /** |
5177 | * nft_parse_register - parse a register value from a netlink attribute | |
5178 | * | |
5179 | * @attr: netlink attribute | |
5180 | * | |
5181 | * Parse and translate a register value from a netlink attribute. | |
5182 | * Registers used to be 128 bit wide, these register numbers will be | |
5183 | * mapped to the corresponding 32 bit register numbers. | |
5184 | */ | |
b1c96ed3 PM |
5185 | unsigned int nft_parse_register(const struct nlattr *attr) |
5186 | { | |
49499c3e PM |
5187 | unsigned int reg; |
5188 | ||
5189 | reg = ntohl(nla_get_be32(attr)); | |
5190 | switch (reg) { | |
5191 | case NFT_REG_VERDICT...NFT_REG_4: | |
5192 | return reg * NFT_REG_SIZE / NFT_REG32_SIZE; | |
5193 | default: | |
5194 | return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00; | |
5195 | } | |
b1c96ed3 PM |
5196 | } |
5197 | EXPORT_SYMBOL_GPL(nft_parse_register); | |
5198 | ||
49499c3e PM |
5199 | /** |
5200 | * nft_dump_register - dump a register value to a netlink attribute | |
5201 | * | |
5202 | * @skb: socket buffer | |
5203 | * @attr: attribute number | |
5204 | * @reg: register number | |
5205 | * | |
5206 | * Construct a netlink attribute containing the register number. For | |
5207 | * compatibility reasons, register numbers being a multiple of 4 are | |
5208 | * translated to the corresponding 128 bit register numbers. | |
5209 | */ | |
b1c96ed3 PM |
5210 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg) |
5211 | { | |
49499c3e PM |
5212 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
5213 | reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE); | |
5214 | else | |
5215 | reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00; | |
5216 | ||
b1c96ed3 PM |
5217 | return nla_put_be32(skb, attr, htonl(reg)); |
5218 | } | |
5219 | EXPORT_SYMBOL_GPL(nft_dump_register); | |
5220 | ||
96518518 | 5221 | /** |
d07db988 | 5222 | * nft_validate_register_load - validate a load from a register |
96518518 PM |
5223 | * |
5224 | * @reg: the register number | |
d07db988 | 5225 | * @len: the length of the data |
96518518 PM |
5226 | * |
5227 | * Validate that the input register is one of the general purpose | |
d07db988 | 5228 | * registers and that the length of the load is within the bounds. |
96518518 | 5229 | */ |
d07db988 | 5230 | int nft_validate_register_load(enum nft_registers reg, unsigned int len) |
96518518 | 5231 | { |
49499c3e | 5232 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
96518518 | 5233 | return -EINVAL; |
d07db988 PM |
5234 | if (len == 0) |
5235 | return -EINVAL; | |
49499c3e | 5236 | if (reg * NFT_REG32_SIZE + len > FIELD_SIZEOF(struct nft_regs, data)) |
d07db988 | 5237 | return -ERANGE; |
49499c3e | 5238 | |
96518518 PM |
5239 | return 0; |
5240 | } | |
d07db988 | 5241 | EXPORT_SYMBOL_GPL(nft_validate_register_load); |
96518518 | 5242 | |
96518518 | 5243 | /** |
1ec10212 | 5244 | * nft_validate_register_store - validate an expressions' register store |
96518518 PM |
5245 | * |
5246 | * @ctx: context of the expression performing the load | |
5247 | * @reg: the destination register number | |
5248 | * @data: the data to load | |
5249 | * @type: the data type | |
45d9bcda | 5250 | * @len: the length of the data |
96518518 PM |
5251 | * |
5252 | * Validate that a data load uses the appropriate data type for | |
45d9bcda PM |
5253 | * the destination register and the length is within the bounds. |
5254 | * A value of NULL for the data means that its runtime gathered | |
58f40ab6 | 5255 | * data. |
96518518 | 5256 | */ |
1ec10212 PM |
5257 | int nft_validate_register_store(const struct nft_ctx *ctx, |
5258 | enum nft_registers reg, | |
5259 | const struct nft_data *data, | |
5260 | enum nft_data_types type, unsigned int len) | |
96518518 | 5261 | { |
20a69341 PM |
5262 | int err; |
5263 | ||
96518518 PM |
5264 | switch (reg) { |
5265 | case NFT_REG_VERDICT: | |
58f40ab6 | 5266 | if (type != NFT_DATA_VERDICT) |
96518518 | 5267 | return -EINVAL; |
20a69341 | 5268 | |
58f40ab6 | 5269 | if (data != NULL && |
1ca2e170 PM |
5270 | (data->verdict.code == NFT_GOTO || |
5271 | data->verdict.code == NFT_JUMP)) { | |
5272 | err = nf_tables_check_loops(ctx, data->verdict.chain); | |
20a69341 PM |
5273 | if (err < 0) |
5274 | return err; | |
5275 | ||
1ca2e170 PM |
5276 | if (ctx->chain->level + 1 > |
5277 | data->verdict.chain->level) { | |
20a69341 PM |
5278 | if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE) |
5279 | return -EMLINK; | |
1ca2e170 | 5280 | data->verdict.chain->level = ctx->chain->level + 1; |
20a69341 PM |
5281 | } |
5282 | } | |
5283 | ||
96518518 PM |
5284 | return 0; |
5285 | default: | |
49499c3e | 5286 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
27e6d201 | 5287 | return -EINVAL; |
45d9bcda PM |
5288 | if (len == 0) |
5289 | return -EINVAL; | |
49499c3e PM |
5290 | if (reg * NFT_REG32_SIZE + len > |
5291 | FIELD_SIZEOF(struct nft_regs, data)) | |
45d9bcda | 5292 | return -ERANGE; |
27e6d201 | 5293 | |
96518518 PM |
5294 | if (data != NULL && type != NFT_DATA_VALUE) |
5295 | return -EINVAL; | |
5296 | return 0; | |
5297 | } | |
5298 | } | |
1ec10212 | 5299 | EXPORT_SYMBOL_GPL(nft_validate_register_store); |
96518518 PM |
5300 | |
5301 | static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { | |
5302 | [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, | |
5303 | [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING, | |
5304 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
5305 | }; | |
5306 | ||
5307 | static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data, | |
5308 | struct nft_data_desc *desc, const struct nlattr *nla) | |
5309 | { | |
664b0f8c | 5310 | u8 genmask = nft_genmask_next(ctx->net); |
96518518 PM |
5311 | struct nlattr *tb[NFTA_VERDICT_MAX + 1]; |
5312 | struct nft_chain *chain; | |
5313 | int err; | |
5314 | ||
5315 | err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy); | |
5316 | if (err < 0) | |
5317 | return err; | |
5318 | ||
5319 | if (!tb[NFTA_VERDICT_CODE]) | |
5320 | return -EINVAL; | |
1ca2e170 | 5321 | data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); |
96518518 | 5322 | |
1ca2e170 | 5323 | switch (data->verdict.code) { |
e0abdadc | 5324 | default: |
1ca2e170 | 5325 | switch (data->verdict.code & NF_VERDICT_MASK) { |
e0abdadc PM |
5326 | case NF_ACCEPT: |
5327 | case NF_DROP: | |
5328 | case NF_QUEUE: | |
5329 | break; | |
5330 | default: | |
5331 | return -EINVAL; | |
5332 | } | |
5333 | /* fall through */ | |
96518518 PM |
5334 | case NFT_CONTINUE: |
5335 | case NFT_BREAK: | |
5336 | case NFT_RETURN: | |
96518518 PM |
5337 | break; |
5338 | case NFT_JUMP: | |
5339 | case NFT_GOTO: | |
5340 | if (!tb[NFTA_VERDICT_CHAIN]) | |
5341 | return -EINVAL; | |
5342 | chain = nf_tables_chain_lookup(ctx->table, | |
664b0f8c | 5343 | tb[NFTA_VERDICT_CHAIN], genmask); |
96518518 PM |
5344 | if (IS_ERR(chain)) |
5345 | return PTR_ERR(chain); | |
5346 | if (chain->flags & NFT_BASE_CHAIN) | |
5347 | return -EOPNOTSUPP; | |
5348 | ||
96518518 | 5349 | chain->use++; |
1ca2e170 | 5350 | data->verdict.chain = chain; |
96518518 | 5351 | break; |
96518518 PM |
5352 | } |
5353 | ||
4c4ed074 | 5354 | desc->len = sizeof(data->verdict); |
96518518 PM |
5355 | desc->type = NFT_DATA_VERDICT; |
5356 | return 0; | |
5357 | } | |
5358 | ||
5359 | static void nft_verdict_uninit(const struct nft_data *data) | |
5360 | { | |
1ca2e170 | 5361 | switch (data->verdict.code) { |
96518518 PM |
5362 | case NFT_JUMP: |
5363 | case NFT_GOTO: | |
1ca2e170 | 5364 | data->verdict.chain->use--; |
96518518 PM |
5365 | break; |
5366 | } | |
5367 | } | |
5368 | ||
33d5a7b1 | 5369 | int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v) |
96518518 PM |
5370 | { |
5371 | struct nlattr *nest; | |
5372 | ||
33d5a7b1 | 5373 | nest = nla_nest_start(skb, type); |
96518518 PM |
5374 | if (!nest) |
5375 | goto nla_put_failure; | |
5376 | ||
33d5a7b1 | 5377 | if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code))) |
96518518 PM |
5378 | goto nla_put_failure; |
5379 | ||
33d5a7b1 | 5380 | switch (v->code) { |
96518518 PM |
5381 | case NFT_JUMP: |
5382 | case NFT_GOTO: | |
1ca2e170 | 5383 | if (nla_put_string(skb, NFTA_VERDICT_CHAIN, |
33d5a7b1 | 5384 | v->chain->name)) |
96518518 PM |
5385 | goto nla_put_failure; |
5386 | } | |
5387 | nla_nest_end(skb, nest); | |
5388 | return 0; | |
5389 | ||
5390 | nla_put_failure: | |
5391 | return -1; | |
5392 | } | |
5393 | ||
d0a11fc3 PM |
5394 | static int nft_value_init(const struct nft_ctx *ctx, |
5395 | struct nft_data *data, unsigned int size, | |
96518518 PM |
5396 | struct nft_data_desc *desc, const struct nlattr *nla) |
5397 | { | |
5398 | unsigned int len; | |
5399 | ||
5400 | len = nla_len(nla); | |
5401 | if (len == 0) | |
5402 | return -EINVAL; | |
d0a11fc3 | 5403 | if (len > size) |
96518518 PM |
5404 | return -EOVERFLOW; |
5405 | ||
d0a11fc3 | 5406 | nla_memcpy(data->data, nla, len); |
96518518 PM |
5407 | desc->type = NFT_DATA_VALUE; |
5408 | desc->len = len; | |
5409 | return 0; | |
5410 | } | |
5411 | ||
5412 | static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data, | |
5413 | unsigned int len) | |
5414 | { | |
5415 | return nla_put(skb, NFTA_DATA_VALUE, len, data->data); | |
5416 | } | |
5417 | ||
5418 | static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { | |
d0a11fc3 | 5419 | [NFTA_DATA_VALUE] = { .type = NLA_BINARY }, |
96518518 PM |
5420 | [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, |
5421 | }; | |
5422 | ||
5423 | /** | |
5424 | * nft_data_init - parse nf_tables data netlink attributes | |
5425 | * | |
5426 | * @ctx: context of the expression using the data | |
5427 | * @data: destination struct nft_data | |
d0a11fc3 | 5428 | * @size: maximum data length |
96518518 PM |
5429 | * @desc: data description |
5430 | * @nla: netlink attribute containing data | |
5431 | * | |
5432 | * Parse the netlink data attributes and initialize a struct nft_data. | |
5433 | * The type and length of data are returned in the data description. | |
5434 | * | |
5435 | * The caller can indicate that it only wants to accept data of type | |
5436 | * NFT_DATA_VALUE by passing NULL for the ctx argument. | |
5437 | */ | |
d0a11fc3 PM |
5438 | int nft_data_init(const struct nft_ctx *ctx, |
5439 | struct nft_data *data, unsigned int size, | |
96518518 PM |
5440 | struct nft_data_desc *desc, const struct nlattr *nla) |
5441 | { | |
5442 | struct nlattr *tb[NFTA_DATA_MAX + 1]; | |
5443 | int err; | |
5444 | ||
5445 | err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy); | |
5446 | if (err < 0) | |
5447 | return err; | |
5448 | ||
5449 | if (tb[NFTA_DATA_VALUE]) | |
d0a11fc3 PM |
5450 | return nft_value_init(ctx, data, size, desc, |
5451 | tb[NFTA_DATA_VALUE]); | |
96518518 PM |
5452 | if (tb[NFTA_DATA_VERDICT] && ctx != NULL) |
5453 | return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); | |
5454 | return -EINVAL; | |
5455 | } | |
5456 | EXPORT_SYMBOL_GPL(nft_data_init); | |
5457 | ||
5458 | /** | |
5459 | * nft_data_uninit - release a nft_data item | |
5460 | * | |
5461 | * @data: struct nft_data to release | |
5462 | * @type: type of data | |
5463 | * | |
5464 | * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
5465 | * all others need to be released by calling this function. | |
5466 | */ | |
5467 | void nft_data_uninit(const struct nft_data *data, enum nft_data_types type) | |
5468 | { | |
960bd2c2 | 5469 | if (type < NFT_DATA_VERDICT) |
96518518 | 5470 | return; |
960bd2c2 | 5471 | switch (type) { |
96518518 PM |
5472 | case NFT_DATA_VERDICT: |
5473 | return nft_verdict_uninit(data); | |
5474 | default: | |
5475 | WARN_ON(1); | |
5476 | } | |
5477 | } | |
5478 | EXPORT_SYMBOL_GPL(nft_data_uninit); | |
5479 | ||
5480 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, | |
5481 | enum nft_data_types type, unsigned int len) | |
5482 | { | |
5483 | struct nlattr *nest; | |
5484 | int err; | |
5485 | ||
5486 | nest = nla_nest_start(skb, attr); | |
5487 | if (nest == NULL) | |
5488 | return -1; | |
5489 | ||
5490 | switch (type) { | |
5491 | case NFT_DATA_VALUE: | |
5492 | err = nft_value_dump(skb, data, len); | |
5493 | break; | |
5494 | case NFT_DATA_VERDICT: | |
33d5a7b1 | 5495 | err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict); |
96518518 PM |
5496 | break; |
5497 | default: | |
5498 | err = -EINVAL; | |
5499 | WARN_ON(1); | |
5500 | } | |
5501 | ||
5502 | nla_nest_end(skb, nest); | |
5503 | return err; | |
5504 | } | |
5505 | EXPORT_SYMBOL_GPL(nft_data_dump); | |
5506 | ||
df05ef87 | 5507 | static int __net_init nf_tables_init_net(struct net *net) |
99633ab2 PNA |
5508 | { |
5509 | INIT_LIST_HEAD(&net->nft.af_info); | |
0628b123 | 5510 | INIT_LIST_HEAD(&net->nft.commit_list); |
38e029f1 | 5511 | net->nft.base_seq = 1; |
99633ab2 PNA |
5512 | return 0; |
5513 | } | |
5514 | ||
5ebe0b0e PNA |
5515 | int __nft_release_basechain(struct nft_ctx *ctx) |
5516 | { | |
5517 | struct nft_rule *rule, *nr; | |
5518 | ||
5519 | BUG_ON(!(ctx->chain->flags & NFT_BASE_CHAIN)); | |
5520 | ||
82bec71d | 5521 | nf_tables_unregister_hooks(ctx->net, ctx->chain->table, ctx->chain, |
5ebe0b0e PNA |
5522 | ctx->afi->nops); |
5523 | list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) { | |
5524 | list_del(&rule->list); | |
5525 | ctx->chain->use--; | |
5526 | nf_tables_rule_destroy(ctx, rule); | |
5527 | } | |
5528 | list_del(&ctx->chain->list); | |
5529 | ctx->table->use--; | |
5530 | nf_tables_chain_destroy(ctx->chain); | |
5531 | ||
5532 | return 0; | |
5533 | } | |
5534 | EXPORT_SYMBOL_GPL(__nft_release_basechain); | |
5535 | ||
df05ef87 PNA |
5536 | /* Called by nft_unregister_afinfo() from __net_exit path, nfnl_lock is held. */ |
5537 | static void __nft_release_afinfo(struct net *net, struct nft_af_info *afi) | |
5538 | { | |
5539 | struct nft_table *table, *nt; | |
5540 | struct nft_chain *chain, *nc; | |
e5009240 | 5541 | struct nft_object *obj, *ne; |
df05ef87 PNA |
5542 | struct nft_rule *rule, *nr; |
5543 | struct nft_set *set, *ns; | |
5544 | struct nft_ctx ctx = { | |
5545 | .net = net, | |
5546 | .afi = afi, | |
5547 | }; | |
5548 | ||
5549 | list_for_each_entry_safe(table, nt, &afi->tables, list) { | |
5550 | list_for_each_entry(chain, &table->chains, list) | |
82bec71d PNA |
5551 | nf_tables_unregister_hooks(net, table, chain, |
5552 | afi->nops); | |
df05ef87 PNA |
5553 | /* No packets are walking on these chains anymore. */ |
5554 | ctx.table = table; | |
5555 | list_for_each_entry(chain, &table->chains, list) { | |
5556 | ctx.chain = chain; | |
5557 | list_for_each_entry_safe(rule, nr, &chain->rules, list) { | |
5558 | list_del(&rule->list); | |
5559 | chain->use--; | |
5560 | nf_tables_rule_destroy(&ctx, rule); | |
5561 | } | |
5562 | } | |
5563 | list_for_each_entry_safe(set, ns, &table->sets, list) { | |
5564 | list_del(&set->list); | |
5565 | table->use--; | |
5566 | nft_set_destroy(set); | |
5567 | } | |
e5009240 PNA |
5568 | list_for_each_entry_safe(obj, ne, &table->objects, list) { |
5569 | list_del(&obj->list); | |
5570 | table->use--; | |
5571 | nft_obj_destroy(obj); | |
5572 | } | |
df05ef87 PNA |
5573 | list_for_each_entry_safe(chain, nc, &table->chains, list) { |
5574 | list_del(&chain->list); | |
5575 | table->use--; | |
5576 | nf_tables_chain_destroy(chain); | |
5577 | } | |
5578 | list_del(&table->list); | |
5579 | nf_tables_table_destroy(&ctx); | |
5580 | } | |
5581 | } | |
5582 | ||
99633ab2 PNA |
5583 | static struct pernet_operations nf_tables_net_ops = { |
5584 | .init = nf_tables_init_net, | |
5585 | }; | |
5586 | ||
96518518 PM |
5587 | static int __init nf_tables_module_init(void) |
5588 | { | |
5589 | int err; | |
5590 | ||
5591 | info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS, | |
5592 | GFP_KERNEL); | |
5593 | if (info == NULL) { | |
5594 | err = -ENOMEM; | |
5595 | goto err1; | |
5596 | } | |
5597 | ||
5598 | err = nf_tables_core_module_init(); | |
5599 | if (err < 0) | |
5600 | goto err2; | |
5601 | ||
5602 | err = nfnetlink_subsys_register(&nf_tables_subsys); | |
5603 | if (err < 0) | |
5604 | goto err3; | |
5605 | ||
5606 | pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n"); | |
99633ab2 | 5607 | return register_pernet_subsys(&nf_tables_net_ops); |
96518518 PM |
5608 | err3: |
5609 | nf_tables_core_module_exit(); | |
5610 | err2: | |
5611 | kfree(info); | |
5612 | err1: | |
5613 | return err; | |
5614 | } | |
5615 | ||
5616 | static void __exit nf_tables_module_exit(void) | |
5617 | { | |
99633ab2 | 5618 | unregister_pernet_subsys(&nf_tables_net_ops); |
96518518 | 5619 | nfnetlink_subsys_unregister(&nf_tables_subsys); |
1b1bc49c | 5620 | rcu_barrier(); |
96518518 PM |
5621 | nf_tables_core_module_exit(); |
5622 | kfree(info); | |
5623 | } | |
5624 | ||
5625 | module_init(nf_tables_module_init); | |
5626 | module_exit(nf_tables_module_exit); | |
5627 | ||
5628 | MODULE_LICENSE("GPL"); | |
5629 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
5630 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES); |