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