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