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