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