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