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