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