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