blob: d65c80b0e84d7c63bfbdcc3ecf45a6108e4bdea1 [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001/*
Patrick McHardy20a69342013-10-11 12:06:22 +02002 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
Patrick McHardy96518512013-10-14 11:00:02 +02003 *
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>
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020021#include <net/net_namespace.h>
Patrick McHardy96518512013-10-14 11:00:02 +020022#include <net/sock.h>
23
Patrick McHardy96518512013-10-14 11:00:02 +020024static 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 */
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020034int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
Patrick McHardy96518512013-10-14 11:00:02 +020035{
36 INIT_LIST_HEAD(&afi->tables);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020038 list_add_tail(&afi->list, &net->nft.af_info);
Patrick McHardy96518512013-10-14 11:00:02 +020039 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
40 return 0;
41}
42EXPORT_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 */
51void 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}
57EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
58
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020059static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
Patrick McHardy96518512013-10-14 11:00:02 +020060{
61 struct nft_af_info *afi;
62
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020063 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +020064 if (afi->family == family)
65 return afi;
66 }
67 return NULL;
68}
69
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020070static struct nft_af_info *
71nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
Patrick McHardy96518512013-10-14 11:00:02 +020072{
73 struct nft_af_info *afi;
74
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020075 afi = nft_afinfo_lookup(net, family);
Patrick McHardy96518512013-10-14 11:00:02 +020076 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);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020083 afi = nft_afinfo_lookup(net, family);
Patrick McHardy96518512013-10-14 11:00:02 +020084 if (afi != NULL)
85 return ERR_PTR(-EAGAIN);
86 }
87#endif
88 return ERR_PTR(-EAFNOSUPPORT);
89}
90
91/*
92 * Tables
93 */
94
95static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
96 const struct nlattr *nla)
97{
98 struct nft_table *table;
99
100 list_for_each_entry(table, &afi->tables, list) {
101 if (!nla_strcmp(nla, table->name))
102 return table;
103 }
104 return NULL;
105}
106
107static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200108 const struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +0200109{
110 struct nft_table *table;
111
112 if (nla == NULL)
113 return ERR_PTR(-EINVAL);
114
115 table = nft_table_lookup(afi, nla);
116 if (table != NULL)
117 return table;
118
Patrick McHardy96518512013-10-14 11:00:02 +0200119 return ERR_PTR(-ENOENT);
120}
121
122static inline u64 nf_tables_alloc_handle(struct nft_table *table)
123{
124 return ++table->hgenerator;
125}
126
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200127static struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
128
129static int __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
130{
131 int i;
132
133 for (i=0; i<NFT_CHAIN_T_MAX; i++) {
134 if (chain_type[family][i] != NULL &&
135 !nla_strcmp(nla, chain_type[family][i]->name))
136 return i;
137 }
138 return -1;
139}
140
141static int nf_tables_chain_type_lookup(const struct nft_af_info *afi,
142 const struct nlattr *nla,
143 bool autoload)
144{
145 int type;
146
147 type = __nf_tables_chain_type_lookup(afi->family, nla);
148#ifdef CONFIG_MODULES
149 if (type < 0 && autoload) {
150 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
151 request_module("nft-chain-%u-%*.s", afi->family,
152 nla_len(nla)-1, (const char *)nla_data(nla));
153 nfnl_lock(NFNL_SUBSYS_NFTABLES);
154 type = __nf_tables_chain_type_lookup(afi->family, nla);
155 }
156#endif
157 return type;
158}
159
Patrick McHardy96518512013-10-14 11:00:02 +0200160static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
161 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200162 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
Patrick McHardy96518512013-10-14 11:00:02 +0200163};
164
165static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
166 int event, u32 flags, int family,
167 const struct nft_table *table)
168{
169 struct nlmsghdr *nlh;
170 struct nfgenmsg *nfmsg;
171
172 event |= NFNL_SUBSYS_NFTABLES << 8;
173 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
174 if (nlh == NULL)
175 goto nla_put_failure;
176
177 nfmsg = nlmsg_data(nlh);
178 nfmsg->nfgen_family = family;
179 nfmsg->version = NFNETLINK_V0;
180 nfmsg->res_id = 0;
181
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200182 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
183 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)))
Patrick McHardy96518512013-10-14 11:00:02 +0200184 goto nla_put_failure;
185
186 return nlmsg_end(skb, nlh);
187
188nla_put_failure:
189 nlmsg_trim(skb, nlh);
190 return -1;
191}
192
193static int nf_tables_table_notify(const struct sk_buff *oskb,
194 const struct nlmsghdr *nlh,
195 const struct nft_table *table,
196 int event, int family)
197{
198 struct sk_buff *skb;
199 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
200 u32 seq = nlh ? nlh->nlmsg_seq : 0;
201 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
202 bool report;
203 int err;
204
205 report = nlh ? nlmsg_report(nlh) : false;
206 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
207 return 0;
208
209 err = -ENOBUFS;
210 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
211 if (skb == NULL)
212 goto err;
213
214 err = nf_tables_fill_table_info(skb, portid, seq, event, 0,
215 family, table);
216 if (err < 0) {
217 kfree_skb(skb);
218 goto err;
219 }
220
221 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
222 GFP_KERNEL);
223err:
224 if (err < 0)
225 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
226 return err;
227}
228
229static int nf_tables_dump_tables(struct sk_buff *skb,
230 struct netlink_callback *cb)
231{
232 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
233 const struct nft_af_info *afi;
234 const struct nft_table *table;
235 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200236 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200237 int family = nfmsg->nfgen_family;
238
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200239 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200240 if (family != NFPROTO_UNSPEC && family != afi->family)
241 continue;
242
243 list_for_each_entry(table, &afi->tables, list) {
244 if (idx < s_idx)
245 goto cont;
246 if (idx > s_idx)
247 memset(&cb->args[1], 0,
248 sizeof(cb->args) - sizeof(cb->args[0]));
249 if (nf_tables_fill_table_info(skb,
250 NETLINK_CB(cb->skb).portid,
251 cb->nlh->nlmsg_seq,
252 NFT_MSG_NEWTABLE,
253 NLM_F_MULTI,
254 afi->family, table) < 0)
255 goto done;
256cont:
257 idx++;
258 }
259 }
260done:
261 cb->args[0] = idx;
262 return skb->len;
263}
264
265static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
266 const struct nlmsghdr *nlh,
267 const struct nlattr * const nla[])
268{
269 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
270 const struct nft_af_info *afi;
271 const struct nft_table *table;
272 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200273 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200274 int family = nfmsg->nfgen_family;
275 int err;
276
277 if (nlh->nlmsg_flags & NLM_F_DUMP) {
278 struct netlink_dump_control c = {
279 .dump = nf_tables_dump_tables,
280 };
281 return netlink_dump_start(nlsk, skb, nlh, &c);
282 }
283
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200284 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200285 if (IS_ERR(afi))
286 return PTR_ERR(afi);
287
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200288 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200289 if (IS_ERR(table))
290 return PTR_ERR(table);
291
292 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
293 if (!skb2)
294 return -ENOMEM;
295
296 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
297 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
298 family, table);
299 if (err < 0)
300 goto err;
301
302 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
303
304err:
305 kfree_skb(skb2);
306 return err;
307}
308
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200309static int nf_tables_table_enable(struct nft_table *table)
310{
311 struct nft_chain *chain;
312 int err, i = 0;
313
314 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100315 if (!(chain->flags & NFT_BASE_CHAIN))
316 continue;
317
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200318 err = nf_register_hook(&nft_base_chain(chain)->ops);
319 if (err < 0)
320 goto err;
321
322 i++;
323 }
324 return 0;
325err:
326 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100327 if (!(chain->flags & NFT_BASE_CHAIN))
328 continue;
329
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200330 if (i-- <= 0)
331 break;
332
333 nf_unregister_hook(&nft_base_chain(chain)->ops);
334 }
335 return err;
336}
337
338static int nf_tables_table_disable(struct nft_table *table)
339{
340 struct nft_chain *chain;
341
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100342 list_for_each_entry(chain, &table->chains, list) {
343 if (chain->flags & NFT_BASE_CHAIN)
344 nf_unregister_hook(&nft_base_chain(chain)->ops);
345 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200346
347 return 0;
348}
349
350static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,
351 const struct nlmsghdr *nlh,
352 const struct nlattr * const nla[],
353 struct nft_af_info *afi, struct nft_table *table)
354{
355 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
356 int family = nfmsg->nfgen_family, ret = 0;
357
358 if (nla[NFTA_TABLE_FLAGS]) {
359 __be32 flags;
360
361 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
362 if (flags & ~NFT_TABLE_F_DORMANT)
363 return -EINVAL;
364
365 if ((flags & NFT_TABLE_F_DORMANT) &&
366 !(table->flags & NFT_TABLE_F_DORMANT)) {
367 ret = nf_tables_table_disable(table);
368 if (ret >= 0)
369 table->flags |= NFT_TABLE_F_DORMANT;
370 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
371 table->flags & NFT_TABLE_F_DORMANT) {
372 ret = nf_tables_table_enable(table);
373 if (ret >= 0)
374 table->flags &= ~NFT_TABLE_F_DORMANT;
375 }
376 if (ret < 0)
377 goto err;
378 }
379
380 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
381err:
382 return ret;
383}
384
Patrick McHardy96518512013-10-14 11:00:02 +0200385static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
386 const struct nlmsghdr *nlh,
387 const struct nlattr * const nla[])
388{
389 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
390 const struct nlattr *name;
391 struct nft_af_info *afi;
392 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200393 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200394 int family = nfmsg->nfgen_family;
395
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200396 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200397 if (IS_ERR(afi))
398 return PTR_ERR(afi);
399
400 name = nla[NFTA_TABLE_NAME];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200401 table = nf_tables_table_lookup(afi, name);
Patrick McHardy96518512013-10-14 11:00:02 +0200402 if (IS_ERR(table)) {
403 if (PTR_ERR(table) != -ENOENT)
404 return PTR_ERR(table);
405 table = NULL;
406 }
407
408 if (table != NULL) {
409 if (nlh->nlmsg_flags & NLM_F_EXCL)
410 return -EEXIST;
411 if (nlh->nlmsg_flags & NLM_F_REPLACE)
412 return -EOPNOTSUPP;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200413 return nf_tables_updtable(nlsk, skb, nlh, nla, afi, table);
Patrick McHardy96518512013-10-14 11:00:02 +0200414 }
415
416 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
417 if (table == NULL)
418 return -ENOMEM;
419
420 nla_strlcpy(table->name, name, nla_len(name));
421 INIT_LIST_HEAD(&table->chains);
Patrick McHardy20a69342013-10-11 12:06:22 +0200422 INIT_LIST_HEAD(&table->sets);
Patrick McHardy96518512013-10-14 11:00:02 +0200423
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200424 if (nla[NFTA_TABLE_FLAGS]) {
425 __be32 flags;
426
427 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
428 if (flags & ~NFT_TABLE_F_DORMANT) {
429 kfree(table);
430 return -EINVAL;
431 }
432
433 table->flags |= flags;
434 }
435
Patrick McHardy96518512013-10-14 11:00:02 +0200436 list_add_tail(&table->list, &afi->tables);
437 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
438 return 0;
439}
440
441static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
442 const struct nlmsghdr *nlh,
443 const struct nlattr * const nla[])
444{
445 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
446 struct nft_af_info *afi;
447 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200448 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200449 int family = nfmsg->nfgen_family;
450
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200451 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200452 if (IS_ERR(afi))
453 return PTR_ERR(afi);
454
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200455 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200456 if (IS_ERR(table))
457 return PTR_ERR(table);
458
Patrick McHardy96518512013-10-14 11:00:02 +0200459 if (table->use)
460 return -EBUSY;
461
462 list_del(&table->list);
463 nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
464 kfree(table);
465 return 0;
466}
467
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200468int nft_register_chain_type(struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200469{
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200470 int err = 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200471
472 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200473 if (chain_type[ctype->family][ctype->type] != NULL) {
474 err = -EBUSY;
475 goto out;
Patrick McHardy96518512013-10-14 11:00:02 +0200476 }
477
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200478 if (!try_module_get(ctype->me))
479 goto out;
Patrick McHardy96518512013-10-14 11:00:02 +0200480
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200481 chain_type[ctype->family][ctype->type] = ctype;
482out:
Patrick McHardy96518512013-10-14 11:00:02 +0200483 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
484 return err;
485}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200486EXPORT_SYMBOL_GPL(nft_register_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200487
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200488void nft_unregister_chain_type(struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200489{
Patrick McHardy96518512013-10-14 11:00:02 +0200490 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200491 chain_type[ctype->family][ctype->type] = NULL;
492 module_put(ctype->me);
Patrick McHardy96518512013-10-14 11:00:02 +0200493 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
494}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200495EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200496
497/*
498 * Chains
499 */
500
501static struct nft_chain *
502nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
503{
504 struct nft_chain *chain;
505
506 list_for_each_entry(chain, &table->chains, list) {
507 if (chain->handle == handle)
508 return chain;
509 }
510
511 return ERR_PTR(-ENOENT);
512}
513
514static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
515 const struct nlattr *nla)
516{
517 struct nft_chain *chain;
518
519 if (nla == NULL)
520 return ERR_PTR(-EINVAL);
521
522 list_for_each_entry(chain, &table->chains, list) {
523 if (!nla_strcmp(nla, chain->name))
524 return chain;
525 }
526
527 return ERR_PTR(-ENOENT);
528}
529
530static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
531 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
532 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
533 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
534 .len = NFT_CHAIN_MAXNAMELEN - 1 },
535 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200536 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200537 [NFTA_CHAIN_TYPE] = { .type = NLA_NUL_STRING },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200538 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
Patrick McHardy96518512013-10-14 11:00:02 +0200539};
540
541static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
542 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
543 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
544};
545
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200546static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
547{
548 struct nft_stats *cpu_stats, total;
549 struct nlattr *nest;
550 int cpu;
551
552 memset(&total, 0, sizeof(total));
553 for_each_possible_cpu(cpu) {
554 cpu_stats = per_cpu_ptr(stats, cpu);
555 total.pkts += cpu_stats->pkts;
556 total.bytes += cpu_stats->bytes;
557 }
558 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
559 if (nest == NULL)
560 goto nla_put_failure;
561
562 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
563 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
564 goto nla_put_failure;
565
566 nla_nest_end(skb, nest);
567 return 0;
568
569nla_put_failure:
570 return -ENOSPC;
571}
572
Patrick McHardy96518512013-10-14 11:00:02 +0200573static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
574 int event, u32 flags, int family,
575 const struct nft_table *table,
576 const struct nft_chain *chain)
577{
578 struct nlmsghdr *nlh;
579 struct nfgenmsg *nfmsg;
580
581 event |= NFNL_SUBSYS_NFTABLES << 8;
582 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
583 if (nlh == NULL)
584 goto nla_put_failure;
585
586 nfmsg = nlmsg_data(nlh);
587 nfmsg->nfgen_family = family;
588 nfmsg->version = NFNETLINK_V0;
589 nfmsg->res_id = 0;
590
591 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
592 goto nla_put_failure;
593 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
594 goto nla_put_failure;
595 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
596 goto nla_put_failure;
597
598 if (chain->flags & NFT_BASE_CHAIN) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200599 const struct nft_base_chain *basechain = nft_base_chain(chain);
600 const struct nf_hook_ops *ops = &basechain->ops;
601 struct nlattr *nest;
602
603 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
Patrick McHardy96518512013-10-14 11:00:02 +0200604 if (nest == NULL)
605 goto nla_put_failure;
606 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
607 goto nla_put_failure;
608 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
609 goto nla_put_failure;
610 nla_nest_end(skb, nest);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200611
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200612 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
613 htonl(basechain->policy)))
614 goto nla_put_failure;
615
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200616 if (nla_put_string(skb, NFTA_CHAIN_TYPE,
617 chain_type[ops->pf][nft_base_chain(chain)->type]->name))
618 goto nla_put_failure;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200619
620 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
621 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +0200622 }
623
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200624 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
625 goto nla_put_failure;
626
Patrick McHardy96518512013-10-14 11:00:02 +0200627 return nlmsg_end(skb, nlh);
628
629nla_put_failure:
630 nlmsg_trim(skb, nlh);
631 return -1;
632}
633
634static int nf_tables_chain_notify(const struct sk_buff *oskb,
635 const struct nlmsghdr *nlh,
636 const struct nft_table *table,
637 const struct nft_chain *chain,
638 int event, int family)
639{
640 struct sk_buff *skb;
641 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
642 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
643 u32 seq = nlh ? nlh->nlmsg_seq : 0;
644 bool report;
645 int err;
646
647 report = nlh ? nlmsg_report(nlh) : false;
648 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
649 return 0;
650
651 err = -ENOBUFS;
652 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
653 if (skb == NULL)
654 goto err;
655
656 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
657 table, chain);
658 if (err < 0) {
659 kfree_skb(skb);
660 goto err;
661 }
662
663 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
664 GFP_KERNEL);
665err:
666 if (err < 0)
667 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
668 return err;
669}
670
671static int nf_tables_dump_chains(struct sk_buff *skb,
672 struct netlink_callback *cb)
673{
674 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
675 const struct nft_af_info *afi;
676 const struct nft_table *table;
677 const struct nft_chain *chain;
678 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200679 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200680 int family = nfmsg->nfgen_family;
681
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200682 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200683 if (family != NFPROTO_UNSPEC && family != afi->family)
684 continue;
685
686 list_for_each_entry(table, &afi->tables, list) {
687 list_for_each_entry(chain, &table->chains, list) {
688 if (idx < s_idx)
689 goto cont;
690 if (idx > s_idx)
691 memset(&cb->args[1], 0,
692 sizeof(cb->args) - sizeof(cb->args[0]));
693 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
694 cb->nlh->nlmsg_seq,
695 NFT_MSG_NEWCHAIN,
696 NLM_F_MULTI,
697 afi->family, table, chain) < 0)
698 goto done;
699cont:
700 idx++;
701 }
702 }
703 }
704done:
705 cb->args[0] = idx;
706 return skb->len;
707}
708
709
710static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
711 const struct nlmsghdr *nlh,
712 const struct nlattr * const nla[])
713{
714 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
715 const struct nft_af_info *afi;
716 const struct nft_table *table;
717 const struct nft_chain *chain;
718 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200719 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200720 int family = nfmsg->nfgen_family;
721 int err;
722
723 if (nlh->nlmsg_flags & NLM_F_DUMP) {
724 struct netlink_dump_control c = {
725 .dump = nf_tables_dump_chains,
726 };
727 return netlink_dump_start(nlsk, skb, nlh, &c);
728 }
729
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200730 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200731 if (IS_ERR(afi))
732 return PTR_ERR(afi);
733
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200734 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200735 if (IS_ERR(table))
736 return PTR_ERR(table);
737
738 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
739 if (IS_ERR(chain))
740 return PTR_ERR(chain);
741
742 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
743 if (!skb2)
744 return -ENOMEM;
745
746 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
747 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
748 family, table, chain);
749 if (err < 0)
750 goto err;
751
752 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
753
754err:
755 kfree_skb(skb2);
756 return err;
757}
758
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200759static int
760nf_tables_chain_policy(struct nft_base_chain *chain, const struct nlattr *attr)
761{
762 switch (ntohl(nla_get_be32(attr))) {
763 case NF_DROP:
764 chain->policy = NF_DROP;
765 break;
766 case NF_ACCEPT:
767 chain->policy = NF_ACCEPT;
768 break;
769 default:
770 return -EINVAL;
771 }
772 return 0;
773}
774
775static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
776 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
777 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
778};
779
780static int
781nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
782{
783 struct nlattr *tb[NFTA_COUNTER_MAX+1];
784 struct nft_stats __percpu *newstats;
785 struct nft_stats *stats;
786 int err;
787
788 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
789 if (err < 0)
790 return err;
791
792 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
793 return -EINVAL;
794
795 newstats = alloc_percpu(struct nft_stats);
796 if (newstats == NULL)
797 return -ENOMEM;
798
799 /* Restore old counters on this cpu, no problem. Per-cpu statistics
800 * are not exposed to userspace.
801 */
802 stats = this_cpu_ptr(newstats);
803 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
804 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
805
806 if (chain->stats) {
807 /* nfnl_lock is held, add some nfnl function for this, later */
808 struct nft_stats __percpu *oldstats =
809 rcu_dereference_protected(chain->stats, 1);
810
811 rcu_assign_pointer(chain->stats, newstats);
812 synchronize_rcu();
813 free_percpu(oldstats);
814 } else
815 rcu_assign_pointer(chain->stats, newstats);
816
817 return 0;
818}
819
Patrick McHardy96518512013-10-14 11:00:02 +0200820static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
821 const struct nlmsghdr *nlh,
822 const struct nlattr * const nla[])
823{
824 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
825 const struct nlattr * uninitialized_var(name);
826 const struct nft_af_info *afi;
827 struct nft_table *table;
828 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200829 struct nft_base_chain *basechain = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200830 struct nlattr *ha[NFTA_HOOK_MAX + 1];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200831 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200832 int family = nfmsg->nfgen_family;
833 u64 handle = 0;
834 int err;
835 bool create;
836
837 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
838
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200839 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200840 if (IS_ERR(afi))
841 return PTR_ERR(afi);
842
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200843 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200844 if (IS_ERR(table))
845 return PTR_ERR(table);
846
847 if (table->use == UINT_MAX)
848 return -EOVERFLOW;
849
850 chain = NULL;
851 name = nla[NFTA_CHAIN_NAME];
852
853 if (nla[NFTA_CHAIN_HANDLE]) {
854 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
855 chain = nf_tables_chain_lookup_byhandle(table, handle);
856 if (IS_ERR(chain))
857 return PTR_ERR(chain);
858 } else {
859 chain = nf_tables_chain_lookup(table, name);
860 if (IS_ERR(chain)) {
861 if (PTR_ERR(chain) != -ENOENT)
862 return PTR_ERR(chain);
863 chain = NULL;
864 }
865 }
866
867 if (chain != NULL) {
868 if (nlh->nlmsg_flags & NLM_F_EXCL)
869 return -EEXIST;
870 if (nlh->nlmsg_flags & NLM_F_REPLACE)
871 return -EOPNOTSUPP;
872
873 if (nla[NFTA_CHAIN_HANDLE] && name &&
874 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
875 return -EEXIST;
876
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200877 if (nla[NFTA_CHAIN_POLICY]) {
878 if (!(chain->flags & NFT_BASE_CHAIN))
879 return -EOPNOTSUPP;
880
881 err = nf_tables_chain_policy(nft_base_chain(chain),
882 nla[NFTA_CHAIN_POLICY]);
883 if (err < 0)
884 return err;
885 }
886
887 if (nla[NFTA_CHAIN_COUNTERS]) {
888 if (!(chain->flags & NFT_BASE_CHAIN))
889 return -EOPNOTSUPP;
890
891 err = nf_tables_counters(nft_base_chain(chain),
892 nla[NFTA_CHAIN_COUNTERS]);
893 if (err < 0)
894 return err;
895 }
896
Patrick McHardy96518512013-10-14 11:00:02 +0200897 if (nla[NFTA_CHAIN_HANDLE] && name)
898 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
899
900 goto notify;
901 }
902
903 if (nla[NFTA_CHAIN_HOOK]) {
904 struct nf_hook_ops *ops;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200905 nf_hookfn *hookfn;
906 u32 hooknum;
907 int type = NFT_CHAIN_T_DEFAULT;
908
909 if (nla[NFTA_CHAIN_TYPE]) {
910 type = nf_tables_chain_type_lookup(afi,
911 nla[NFTA_CHAIN_TYPE],
912 create);
913 if (type < 0)
914 return -ENOENT;
915 }
Patrick McHardy96518512013-10-14 11:00:02 +0200916
917 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
918 nft_hook_policy);
919 if (err < 0)
920 return err;
921 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
922 ha[NFTA_HOOK_PRIORITY] == NULL)
923 return -EINVAL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200924
925 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
926 if (hooknum >= afi->nhooks)
Patrick McHardy96518512013-10-14 11:00:02 +0200927 return -EINVAL;
928
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200929 hookfn = chain_type[family][type]->fn[hooknum];
930 if (hookfn == NULL)
931 return -EOPNOTSUPP;
932
Patrick McHardy96518512013-10-14 11:00:02 +0200933 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
934 if (basechain == NULL)
935 return -ENOMEM;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200936
937 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +0200938 chain = &basechain->chain;
939
940 ops = &basechain->ops;
941 ops->pf = family;
942 ops->owner = afi->owner;
943 ops->hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
944 ops->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
945 ops->priv = chain;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200946 ops->hook = hookfn;
Patrick McHardy96518512013-10-14 11:00:02 +0200947 if (afi->hooks[ops->hooknum])
948 ops->hook = afi->hooks[ops->hooknum];
949
950 chain->flags |= NFT_BASE_CHAIN;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200951
952 if (nla[NFTA_CHAIN_POLICY]) {
953 err = nf_tables_chain_policy(basechain,
954 nla[NFTA_CHAIN_POLICY]);
955 if (err < 0) {
956 free_percpu(basechain->stats);
957 kfree(basechain);
958 return err;
959 }
960 } else
961 basechain->policy = NF_ACCEPT;
962
963 if (nla[NFTA_CHAIN_COUNTERS]) {
964 err = nf_tables_counters(basechain,
965 nla[NFTA_CHAIN_COUNTERS]);
966 if (err < 0) {
967 free_percpu(basechain->stats);
968 kfree(basechain);
969 return err;
970 }
971 } else {
972 struct nft_stats __percpu *newstats;
973
974 newstats = alloc_percpu(struct nft_stats);
975 if (newstats == NULL)
976 return -ENOMEM;
977
978 rcu_assign_pointer(nft_base_chain(chain)->stats,
979 newstats);
980 }
Patrick McHardy96518512013-10-14 11:00:02 +0200981 } else {
982 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
983 if (chain == NULL)
984 return -ENOMEM;
985 }
986
987 INIT_LIST_HEAD(&chain->rules);
988 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200989 chain->net = net;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +0200990 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +0200991 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
992
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200993 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
994 chain->flags & NFT_BASE_CHAIN) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200995 err = nf_register_hook(&nft_base_chain(chain)->ops);
996 if (err < 0) {
997 free_percpu(basechain->stats);
998 kfree(basechain);
999 return err;
1000 }
1001 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001002 list_add_tail(&chain->list, &table->chains);
1003 table->use++;
Patrick McHardy96518512013-10-14 11:00:02 +02001004notify:
1005 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
1006 family);
1007 return 0;
1008}
1009
1010static void nf_tables_rcu_chain_destroy(struct rcu_head *head)
1011{
1012 struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
1013
1014 BUG_ON(chain->use > 0);
1015
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001016 if (chain->flags & NFT_BASE_CHAIN) {
1017 free_percpu(nft_base_chain(chain)->stats);
Patrick McHardy96518512013-10-14 11:00:02 +02001018 kfree(nft_base_chain(chain));
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001019 } else
Patrick McHardy96518512013-10-14 11:00:02 +02001020 kfree(chain);
1021}
1022
1023static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1024 const struct nlmsghdr *nlh,
1025 const struct nlattr * const nla[])
1026{
1027 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1028 const struct nft_af_info *afi;
1029 struct nft_table *table;
1030 struct nft_chain *chain;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001031 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001032 int family = nfmsg->nfgen_family;
1033
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001034 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001035 if (IS_ERR(afi))
1036 return PTR_ERR(afi);
1037
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001038 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001039 if (IS_ERR(table))
1040 return PTR_ERR(table);
1041
1042 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1043 if (IS_ERR(chain))
1044 return PTR_ERR(chain);
1045
Patrick McHardy96518512013-10-14 11:00:02 +02001046 if (!list_empty(&chain->rules))
1047 return -EBUSY;
1048
1049 list_del(&chain->list);
1050 table->use--;
1051
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001052 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1053 chain->flags & NFT_BASE_CHAIN)
Patrick McHardy96518512013-10-14 11:00:02 +02001054 nf_unregister_hook(&nft_base_chain(chain)->ops);
1055
1056 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1057 family);
1058
1059 /* Make sure all rule references are gone before this is released */
1060 call_rcu(&chain->rcu_head, nf_tables_rcu_chain_destroy);
1061 return 0;
1062}
1063
1064static void nft_ctx_init(struct nft_ctx *ctx,
Patrick McHardy20a69342013-10-11 12:06:22 +02001065 const struct sk_buff *skb,
1066 const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02001067 const struct nft_af_info *afi,
1068 const struct nft_table *table,
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001069 const struct nft_chain *chain,
1070 const struct nlattr * const *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001071{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001072 ctx->net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02001073 ctx->skb = skb;
1074 ctx->nlh = nlh;
Patrick McHardy96518512013-10-14 11:00:02 +02001075 ctx->afi = afi;
1076 ctx->table = table;
1077 ctx->chain = chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001078 ctx->nla = nla;
Patrick McHardy96518512013-10-14 11:00:02 +02001079}
1080
1081/*
1082 * Expressions
1083 */
1084
1085/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001086 * nft_register_expr - register nf_tables expr type
1087 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001088 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001089 * Registers the expr type for use with nf_tables. Returns zero on
Patrick McHardy96518512013-10-14 11:00:02 +02001090 * success or a negative errno code otherwise.
1091 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001092int nft_register_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001093{
1094 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001095 list_add_tail(&type->list, &nf_tables_expressions);
Patrick McHardy96518512013-10-14 11:00:02 +02001096 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1097 return 0;
1098}
1099EXPORT_SYMBOL_GPL(nft_register_expr);
1100
1101/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001102 * nft_unregister_expr - unregister nf_tables expr type
1103 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001104 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001105 * Unregisters the expr typefor use with nf_tables.
Patrick McHardy96518512013-10-14 11:00:02 +02001106 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001107void nft_unregister_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001108{
1109 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001110 list_del(&type->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001111 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1112}
1113EXPORT_SYMBOL_GPL(nft_unregister_expr);
1114
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001115static const struct nft_expr_type *__nft_expr_type_get(struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001116{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001117 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001118
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001119 list_for_each_entry(type, &nf_tables_expressions, list) {
1120 if (!nla_strcmp(nla, type->name))
1121 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001122 }
1123 return NULL;
1124}
1125
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001126static const struct nft_expr_type *nft_expr_type_get(struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001127{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001128 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001129
1130 if (nla == NULL)
1131 return ERR_PTR(-EINVAL);
1132
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001133 type = __nft_expr_type_get(nla);
1134 if (type != NULL && try_module_get(type->owner))
1135 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001136
1137#ifdef CONFIG_MODULES
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001138 if (type == NULL) {
Patrick McHardy96518512013-10-14 11:00:02 +02001139 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1140 request_module("nft-expr-%.*s",
1141 nla_len(nla), (char *)nla_data(nla));
1142 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001143 if (__nft_expr_type_get(nla))
Patrick McHardy96518512013-10-14 11:00:02 +02001144 return ERR_PTR(-EAGAIN);
1145 }
1146#endif
1147 return ERR_PTR(-ENOENT);
1148}
1149
1150static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1151 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1152 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1153};
1154
1155static int nf_tables_fill_expr_info(struct sk_buff *skb,
1156 const struct nft_expr *expr)
1157{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001158 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
Patrick McHardy96518512013-10-14 11:00:02 +02001159 goto nla_put_failure;
1160
1161 if (expr->ops->dump) {
1162 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1163 if (data == NULL)
1164 goto nla_put_failure;
1165 if (expr->ops->dump(skb, expr) < 0)
1166 goto nla_put_failure;
1167 nla_nest_end(skb, data);
1168 }
1169
1170 return skb->len;
1171
1172nla_put_failure:
1173 return -1;
1174};
1175
1176struct nft_expr_info {
1177 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001178 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001179};
1180
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001181static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1182 const struct nlattr *nla,
Patrick McHardy96518512013-10-14 11:00:02 +02001183 struct nft_expr_info *info)
1184{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001185 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001186 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001187 struct nlattr *tb[NFTA_EXPR_MAX + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001188 int err;
1189
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001190 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
Patrick McHardy96518512013-10-14 11:00:02 +02001191 if (err < 0)
1192 return err;
1193
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001194 type = nft_expr_type_get(tb[NFTA_EXPR_NAME]);
1195 if (IS_ERR(type))
1196 return PTR_ERR(type);
1197
1198 if (tb[NFTA_EXPR_DATA]) {
1199 err = nla_parse_nested(info->tb, type->maxattr,
1200 tb[NFTA_EXPR_DATA], type->policy);
1201 if (err < 0)
1202 goto err1;
1203 } else
1204 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1205
1206 if (type->select_ops != NULL) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001207 ops = type->select_ops(ctx,
1208 (const struct nlattr * const *)info->tb);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001209 if (IS_ERR(ops)) {
1210 err = PTR_ERR(ops);
1211 goto err1;
1212 }
1213 } else
1214 ops = type->ops;
1215
Patrick McHardy96518512013-10-14 11:00:02 +02001216 info->ops = ops;
1217 return 0;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001218
1219err1:
1220 module_put(type->owner);
1221 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001222}
1223
1224static int nf_tables_newexpr(const struct nft_ctx *ctx,
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001225 const struct nft_expr_info *info,
Patrick McHardy96518512013-10-14 11:00:02 +02001226 struct nft_expr *expr)
1227{
1228 const struct nft_expr_ops *ops = info->ops;
1229 int err;
1230
1231 expr->ops = ops;
1232 if (ops->init) {
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001233 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
Patrick McHardy96518512013-10-14 11:00:02 +02001234 if (err < 0)
1235 goto err1;
1236 }
1237
Patrick McHardy96518512013-10-14 11:00:02 +02001238 return 0;
1239
1240err1:
1241 expr->ops = NULL;
1242 return err;
1243}
1244
1245static void nf_tables_expr_destroy(struct nft_expr *expr)
1246{
1247 if (expr->ops->destroy)
1248 expr->ops->destroy(expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001249 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001250}
1251
1252/*
1253 * Rules
1254 */
1255
1256static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1257 u64 handle)
1258{
1259 struct nft_rule *rule;
1260
1261 // FIXME: this sucks
1262 list_for_each_entry(rule, &chain->rules, list) {
1263 if (handle == rule->handle)
1264 return rule;
1265 }
1266
1267 return ERR_PTR(-ENOENT);
1268}
1269
1270static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1271 const struct nlattr *nla)
1272{
1273 if (nla == NULL)
1274 return ERR_PTR(-EINVAL);
1275
1276 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1277}
1278
1279static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1280 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1281 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1282 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1283 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1284 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001285 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001286 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Patrick McHardy96518512013-10-14 11:00:02 +02001287};
1288
1289static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1290 int event, u32 flags, int family,
1291 const struct nft_table *table,
1292 const struct nft_chain *chain,
1293 const struct nft_rule *rule)
1294{
1295 struct nlmsghdr *nlh;
1296 struct nfgenmsg *nfmsg;
1297 const struct nft_expr *expr, *next;
1298 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001299 const struct nft_rule *prule;
1300 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001301
Eric Leblond5e948462013-10-10 13:41:44 +02001302 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001303 flags);
1304 if (nlh == NULL)
1305 goto nla_put_failure;
1306
1307 nfmsg = nlmsg_data(nlh);
1308 nfmsg->nfgen_family = family;
1309 nfmsg->version = NFNETLINK_V0;
1310 nfmsg->res_id = 0;
1311
1312 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1313 goto nla_put_failure;
1314 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1315 goto nla_put_failure;
1316 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1317 goto nla_put_failure;
1318
Eric Leblond5e948462013-10-10 13:41:44 +02001319 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1320 prule = list_entry(rule->list.prev, struct nft_rule, list);
1321 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1322 cpu_to_be64(prule->handle)))
1323 goto nla_put_failure;
1324 }
1325
Patrick McHardy96518512013-10-14 11:00:02 +02001326 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1327 if (list == NULL)
1328 goto nla_put_failure;
1329 nft_rule_for_each_expr(expr, next, rule) {
1330 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1331 if (elem == NULL)
1332 goto nla_put_failure;
1333 if (nf_tables_fill_expr_info(skb, expr) < 0)
1334 goto nla_put_failure;
1335 nla_nest_end(skb, elem);
1336 }
1337 nla_nest_end(skb, list);
1338
1339 return nlmsg_end(skb, nlh);
1340
1341nla_put_failure:
1342 nlmsg_trim(skb, nlh);
1343 return -1;
1344}
1345
1346static int nf_tables_rule_notify(const struct sk_buff *oskb,
1347 const struct nlmsghdr *nlh,
1348 const struct nft_table *table,
1349 const struct nft_chain *chain,
1350 const struct nft_rule *rule,
1351 int event, u32 flags, int family)
1352{
1353 struct sk_buff *skb;
1354 u32 portid = NETLINK_CB(oskb).portid;
1355 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1356 u32 seq = nlh->nlmsg_seq;
1357 bool report;
1358 int err;
1359
1360 report = nlmsg_report(nlh);
1361 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1362 return 0;
1363
1364 err = -ENOBUFS;
1365 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1366 if (skb == NULL)
1367 goto err;
1368
1369 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1370 family, table, chain, rule);
1371 if (err < 0) {
1372 kfree_skb(skb);
1373 goto err;
1374 }
1375
1376 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1377 GFP_KERNEL);
1378err:
1379 if (err < 0)
1380 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1381 return err;
1382}
1383
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001384static inline bool
1385nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1386{
1387 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1388}
1389
1390static inline int gencursor_next(struct net *net)
1391{
1392 return net->nft.gencursor+1 == 1 ? 1 : 0;
1393}
1394
1395static inline int
1396nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1397{
1398 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1399}
1400
1401static inline void
1402nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1403{
1404 /* Now inactive, will be active in the future */
1405 rule->genmask = (1 << net->nft.gencursor);
1406}
1407
1408static inline void
1409nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1410{
1411 rule->genmask = (1 << gencursor_next(net));
1412}
1413
1414static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1415{
1416 rule->genmask = 0;
1417}
1418
Patrick McHardy96518512013-10-14 11:00:02 +02001419static int nf_tables_dump_rules(struct sk_buff *skb,
1420 struct netlink_callback *cb)
1421{
1422 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1423 const struct nft_af_info *afi;
1424 const struct nft_table *table;
1425 const struct nft_chain *chain;
1426 const struct nft_rule *rule;
1427 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001428 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001429 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001430 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1431 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
Patrick McHardy96518512013-10-14 11:00:02 +02001432
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001433 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001434 if (family != NFPROTO_UNSPEC && family != afi->family)
1435 continue;
1436
1437 list_for_each_entry(table, &afi->tables, list) {
1438 list_for_each_entry(chain, &table->chains, list) {
1439 list_for_each_entry(rule, &chain->rules, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001440 if (!nft_rule_is_active(net, rule))
1441 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001442 if (idx < s_idx)
1443 goto cont;
1444 if (idx > s_idx)
1445 memset(&cb->args[1], 0,
1446 sizeof(cb->args) - sizeof(cb->args[0]));
1447 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1448 cb->nlh->nlmsg_seq,
1449 NFT_MSG_NEWRULE,
1450 NLM_F_MULTI | NLM_F_APPEND,
1451 afi->family, table, chain, rule) < 0)
1452 goto done;
1453cont:
1454 idx++;
1455 }
1456 }
1457 }
1458 }
1459done:
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001460 /* Invalidate this dump, a transition to the new generation happened */
1461 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1462 return -EBUSY;
1463
Patrick McHardy96518512013-10-14 11:00:02 +02001464 cb->args[0] = idx;
1465 return skb->len;
1466}
1467
1468static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1469 const struct nlmsghdr *nlh,
1470 const struct nlattr * const nla[])
1471{
1472 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1473 const struct nft_af_info *afi;
1474 const struct nft_table *table;
1475 const struct nft_chain *chain;
1476 const struct nft_rule *rule;
1477 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001478 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001479 int family = nfmsg->nfgen_family;
1480 int err;
1481
1482 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1483 struct netlink_dump_control c = {
1484 .dump = nf_tables_dump_rules,
1485 };
1486 return netlink_dump_start(nlsk, skb, nlh, &c);
1487 }
1488
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001489 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001490 if (IS_ERR(afi))
1491 return PTR_ERR(afi);
1492
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001493 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001494 if (IS_ERR(table))
1495 return PTR_ERR(table);
1496
1497 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1498 if (IS_ERR(chain))
1499 return PTR_ERR(chain);
1500
1501 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1502 if (IS_ERR(rule))
1503 return PTR_ERR(rule);
1504
1505 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1506 if (!skb2)
1507 return -ENOMEM;
1508
1509 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1510 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1511 family, table, chain, rule);
1512 if (err < 0)
1513 goto err;
1514
1515 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1516
1517err:
1518 kfree_skb(skb2);
1519 return err;
1520}
1521
1522static void nf_tables_rcu_rule_destroy(struct rcu_head *head)
1523{
1524 struct nft_rule *rule = container_of(head, struct nft_rule, rcu_head);
1525 struct nft_expr *expr;
1526
1527 /*
1528 * Careful: some expressions might not be initialized in case this
1529 * is called on error from nf_tables_newrule().
1530 */
1531 expr = nft_expr_first(rule);
1532 while (expr->ops && expr != nft_expr_last(rule)) {
1533 nf_tables_expr_destroy(expr);
1534 expr = nft_expr_next(expr);
1535 }
1536 kfree(rule);
1537}
1538
1539static void nf_tables_rule_destroy(struct nft_rule *rule)
1540{
1541 call_rcu(&rule->rcu_head, nf_tables_rcu_rule_destroy);
1542}
1543
1544#define NFT_RULE_MAXEXPRS 128
1545
1546static struct nft_expr_info *info;
1547
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001548static struct nft_rule_trans *
1549nf_tables_trans_add(struct nft_rule *rule, const struct nft_ctx *ctx)
1550{
1551 struct nft_rule_trans *rupd;
1552
1553 rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1554 if (rupd == NULL)
1555 return NULL;
1556
1557 rupd->chain = ctx->chain;
1558 rupd->table = ctx->table;
1559 rupd->rule = rule;
1560 rupd->family = ctx->afi->family;
1561 rupd->nlh = ctx->nlh;
1562 list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1563
1564 return rupd;
1565}
1566
Patrick McHardy96518512013-10-14 11:00:02 +02001567static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1568 const struct nlmsghdr *nlh,
1569 const struct nlattr * const nla[])
1570{
1571 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1572 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001573 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001574 struct nft_table *table;
1575 struct nft_chain *chain;
1576 struct nft_rule *rule, *old_rule = NULL;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001577 struct nft_rule_trans *repl = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001578 struct nft_expr *expr;
1579 struct nft_ctx ctx;
1580 struct nlattr *tmp;
1581 unsigned int size, i, n;
1582 int err, rem;
1583 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02001584 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02001585
1586 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1587
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001588 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02001589 if (IS_ERR(afi))
1590 return PTR_ERR(afi);
1591
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001592 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001593 if (IS_ERR(table))
1594 return PTR_ERR(table);
1595
1596 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1597 if (IS_ERR(chain))
1598 return PTR_ERR(chain);
1599
1600 if (nla[NFTA_RULE_HANDLE]) {
1601 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1602 rule = __nf_tables_rule_lookup(chain, handle);
1603 if (IS_ERR(rule))
1604 return PTR_ERR(rule);
1605
1606 if (nlh->nlmsg_flags & NLM_F_EXCL)
1607 return -EEXIST;
1608 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1609 old_rule = rule;
1610 else
1611 return -EOPNOTSUPP;
1612 } else {
1613 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1614 return -EINVAL;
1615 handle = nf_tables_alloc_handle(table);
1616 }
1617
Eric Leblond5e948462013-10-10 13:41:44 +02001618 if (nla[NFTA_RULE_POSITION]) {
1619 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1620 return -EOPNOTSUPP;
1621
1622 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1623 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1624 if (IS_ERR(old_rule))
1625 return PTR_ERR(old_rule);
1626 }
1627
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001628 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1629
Patrick McHardy96518512013-10-14 11:00:02 +02001630 n = 0;
1631 size = 0;
1632 if (nla[NFTA_RULE_EXPRESSIONS]) {
1633 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1634 err = -EINVAL;
1635 if (nla_type(tmp) != NFTA_LIST_ELEM)
1636 goto err1;
1637 if (n == NFT_RULE_MAXEXPRS)
1638 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001639 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02001640 if (err < 0)
1641 goto err1;
1642 size += info[n].ops->size;
1643 n++;
1644 }
1645 }
1646
1647 err = -ENOMEM;
1648 rule = kzalloc(sizeof(*rule) + size, GFP_KERNEL);
1649 if (rule == NULL)
1650 goto err1;
1651
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001652 nft_rule_activate_next(net, rule);
1653
Patrick McHardy96518512013-10-14 11:00:02 +02001654 rule->handle = handle;
1655 rule->dlen = size;
1656
Patrick McHardy96518512013-10-14 11:00:02 +02001657 expr = nft_expr_first(rule);
1658 for (i = 0; i < n; i++) {
1659 err = nf_tables_newexpr(&ctx, &info[i], expr);
1660 if (err < 0)
1661 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001662 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001663 expr = nft_expr_next(expr);
1664 }
1665
Patrick McHardy96518512013-10-14 11:00:02 +02001666 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001667 if (nft_rule_is_active_next(net, old_rule)) {
1668 repl = nf_tables_trans_add(old_rule, &ctx);
1669 if (repl == NULL) {
1670 err = -ENOMEM;
1671 goto err2;
1672 }
1673 nft_rule_disactivate_next(net, old_rule);
1674 list_add_tail(&rule->list, &old_rule->list);
1675 } else {
1676 err = -ENOENT;
1677 goto err2;
1678 }
Patrick McHardy96518512013-10-14 11:00:02 +02001679 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02001680 if (old_rule)
1681 list_add_rcu(&rule->list, &old_rule->list);
1682 else
1683 list_add_tail_rcu(&rule->list, &chain->rules);
1684 else {
1685 if (old_rule)
1686 list_add_tail_rcu(&rule->list, &old_rule->list);
1687 else
1688 list_add_rcu(&rule->list, &chain->rules);
1689 }
Patrick McHardy96518512013-10-14 11:00:02 +02001690
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001691 if (nf_tables_trans_add(rule, &ctx) == NULL) {
1692 err = -ENOMEM;
1693 goto err3;
1694 }
Patrick McHardy96518512013-10-14 11:00:02 +02001695 return 0;
1696
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001697err3:
1698 list_del_rcu(&rule->list);
1699 if (repl) {
1700 list_del_rcu(&repl->rule->list);
1701 list_del(&repl->list);
1702 nft_rule_clear(net, repl->rule);
1703 kfree(repl);
1704 }
Patrick McHardy96518512013-10-14 11:00:02 +02001705err2:
1706 nf_tables_rule_destroy(rule);
1707err1:
1708 for (i = 0; i < n; i++) {
1709 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001710 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001711 }
1712 return err;
1713}
1714
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001715static int
1716nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1717{
1718 /* You cannot delete the same rule twice */
1719 if (nft_rule_is_active_next(ctx->net, rule)) {
1720 if (nf_tables_trans_add(rule, ctx) == NULL)
1721 return -ENOMEM;
1722 nft_rule_disactivate_next(ctx->net, rule);
1723 return 0;
1724 }
1725 return -ENOENT;
1726}
1727
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001728static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1729{
1730 struct nft_rule *rule;
1731 int err;
1732
1733 list_for_each_entry(rule, &ctx->chain->rules, list) {
1734 err = nf_tables_delrule_one(ctx, rule);
1735 if (err < 0)
1736 return err;
1737 }
1738 return 0;
1739}
1740
Patrick McHardy96518512013-10-14 11:00:02 +02001741static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1742 const struct nlmsghdr *nlh,
1743 const struct nlattr * const nla[])
1744{
1745 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1746 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001747 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001748 const struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001749 struct nft_chain *chain = NULL;
1750 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001751 int family = nfmsg->nfgen_family, err = 0;
1752 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001753
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001754 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001755 if (IS_ERR(afi))
1756 return PTR_ERR(afi);
1757
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001758 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001759 if (IS_ERR(table))
1760 return PTR_ERR(table);
1761
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001762 if (nla[NFTA_RULE_CHAIN]) {
1763 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1764 if (IS_ERR(chain))
1765 return PTR_ERR(chain);
1766 }
Patrick McHardy96518512013-10-14 11:00:02 +02001767
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001768 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1769
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001770 if (chain) {
1771 if (nla[NFTA_RULE_HANDLE]) {
1772 rule = nf_tables_rule_lookup(chain,
1773 nla[NFTA_RULE_HANDLE]);
1774 if (IS_ERR(rule))
1775 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001776
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001777 err = nf_tables_delrule_one(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001778 } else {
1779 err = nf_table_delrule_by_chain(&ctx);
1780 }
1781 } else {
1782 list_for_each_entry(chain, &table->chains, list) {
1783 ctx.chain = chain;
1784 err = nf_table_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001785 if (err < 0)
1786 break;
Patrick McHardy96518512013-10-14 11:00:02 +02001787 }
1788 }
1789
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001790 return err;
1791}
1792
1793static int nf_tables_commit(struct sk_buff *skb)
1794{
1795 struct net *net = sock_net(skb->sk);
1796 struct nft_rule_trans *rupd, *tmp;
1797
1798 /* Bump generation counter, invalidate any dump in progress */
1799 net->nft.genctr++;
1800
1801 /* A new generation has just started */
1802 net->nft.gencursor = gencursor_next(net);
1803
1804 /* Make sure all packets have left the previous generation before
1805 * purging old rules.
1806 */
1807 synchronize_rcu();
1808
1809 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1810 /* Delete this rule from the dirty list */
1811 list_del(&rupd->list);
1812
1813 /* This rule was inactive in the past and just became active.
1814 * Clear the next bit of the genmask since its meaning has
1815 * changed, now it is the future.
1816 */
1817 if (nft_rule_is_active(net, rupd->rule)) {
1818 nft_rule_clear(net, rupd->rule);
1819 nf_tables_rule_notify(skb, rupd->nlh, rupd->table,
1820 rupd->chain, rupd->rule,
1821 NFT_MSG_NEWRULE, 0,
1822 rupd->family);
1823 kfree(rupd);
1824 continue;
1825 }
1826
1827 /* This rule is in the past, get rid of it */
1828 list_del_rcu(&rupd->rule->list);
1829 nf_tables_rule_notify(skb, rupd->nlh, rupd->table, rupd->chain,
1830 rupd->rule, NFT_MSG_DELRULE, 0,
1831 rupd->family);
1832 nf_tables_rule_destroy(rupd->rule);
1833 kfree(rupd);
1834 }
1835
1836 return 0;
1837}
1838
1839static int nf_tables_abort(struct sk_buff *skb)
1840{
1841 struct net *net = sock_net(skb->sk);
1842 struct nft_rule_trans *rupd, *tmp;
1843
1844 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1845 /* Delete all rules from the dirty list */
1846 list_del(&rupd->list);
1847
1848 if (!nft_rule_is_active_next(net, rupd->rule)) {
1849 nft_rule_clear(net, rupd->rule);
1850 kfree(rupd);
1851 continue;
1852 }
1853
1854 /* This rule is inactive, get rid of it */
1855 list_del_rcu(&rupd->rule->list);
1856 nf_tables_rule_destroy(rupd->rule);
1857 kfree(rupd);
1858 }
Patrick McHardy96518512013-10-14 11:00:02 +02001859 return 0;
1860}
1861
Patrick McHardy20a69342013-10-11 12:06:22 +02001862/*
1863 * Sets
1864 */
1865
1866static LIST_HEAD(nf_tables_set_ops);
1867
1868int nft_register_set(struct nft_set_ops *ops)
1869{
1870 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1871 list_add_tail(&ops->list, &nf_tables_set_ops);
1872 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1873 return 0;
1874}
1875EXPORT_SYMBOL_GPL(nft_register_set);
1876
1877void nft_unregister_set(struct nft_set_ops *ops)
1878{
1879 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1880 list_del(&ops->list);
1881 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1882}
1883EXPORT_SYMBOL_GPL(nft_unregister_set);
1884
1885static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1886{
1887 const struct nft_set_ops *ops;
1888 u32 features;
1889
1890#ifdef CONFIG_MODULES
1891 if (list_empty(&nf_tables_set_ops)) {
1892 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1893 request_module("nft-set");
1894 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1895 if (!list_empty(&nf_tables_set_ops))
1896 return ERR_PTR(-EAGAIN);
1897 }
1898#endif
1899 features = 0;
1900 if (nla[NFTA_SET_FLAGS] != NULL) {
1901 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1902 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1903 }
1904
1905 // FIXME: implement selection properly
1906 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1907 if ((ops->features & features) != features)
1908 continue;
1909 if (!try_module_get(ops->owner))
1910 continue;
1911 return ops;
1912 }
1913
1914 return ERR_PTR(-EOPNOTSUPP);
1915}
1916
1917static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1918 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1919 [NFTA_SET_NAME] = { .type = NLA_STRING },
1920 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1921 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1922 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1923 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1924 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1925};
1926
1927static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1928 const struct sk_buff *skb,
1929 const struct nlmsghdr *nlh,
1930 const struct nlattr * const nla[])
1931{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001932 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02001933 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1934 const struct nft_af_info *afi;
1935 const struct nft_table *table = NULL;
1936
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001937 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02001938 if (IS_ERR(afi))
1939 return PTR_ERR(afi);
1940
1941 if (nla[NFTA_SET_TABLE] != NULL) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001942 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02001943 if (IS_ERR(table))
1944 return PTR_ERR(table);
1945 }
1946
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001947 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02001948 return 0;
1949}
1950
1951struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1952 const struct nlattr *nla)
1953{
1954 struct nft_set *set;
1955
1956 if (nla == NULL)
1957 return ERR_PTR(-EINVAL);
1958
1959 list_for_each_entry(set, &table->sets, list) {
1960 if (!nla_strcmp(nla, set->name))
1961 return set;
1962 }
1963 return ERR_PTR(-ENOENT);
1964}
1965
1966static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1967 const char *name)
1968{
1969 const struct nft_set *i;
1970 const char *p;
1971 unsigned long *inuse;
1972 unsigned int n = 0;
1973
1974 p = strnchr(name, IFNAMSIZ, '%');
1975 if (p != NULL) {
1976 if (p[1] != 'd' || strchr(p + 2, '%'))
1977 return -EINVAL;
1978
1979 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1980 if (inuse == NULL)
1981 return -ENOMEM;
1982
1983 list_for_each_entry(i, &ctx->table->sets, list) {
1984 if (!sscanf(i->name, name, &n))
1985 continue;
1986 if (n < 0 || n > BITS_PER_LONG * PAGE_SIZE)
1987 continue;
1988 set_bit(n, inuse);
1989 }
1990
1991 n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
1992 free_page((unsigned long)inuse);
1993 }
1994
1995 snprintf(set->name, sizeof(set->name), name, n);
1996 list_for_each_entry(i, &ctx->table->sets, list) {
1997 if (!strcmp(set->name, i->name))
1998 return -ENFILE;
1999 }
2000 return 0;
2001}
2002
2003static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2004 const struct nft_set *set, u16 event, u16 flags)
2005{
2006 struct nfgenmsg *nfmsg;
2007 struct nlmsghdr *nlh;
2008 u32 portid = NETLINK_CB(ctx->skb).portid;
2009 u32 seq = ctx->nlh->nlmsg_seq;
2010
2011 event |= NFNL_SUBSYS_NFTABLES << 8;
2012 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2013 flags);
2014 if (nlh == NULL)
2015 goto nla_put_failure;
2016
2017 nfmsg = nlmsg_data(nlh);
2018 nfmsg->nfgen_family = ctx->afi->family;
2019 nfmsg->version = NFNETLINK_V0;
2020 nfmsg->res_id = 0;
2021
2022 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2023 goto nla_put_failure;
2024 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2025 goto nla_put_failure;
2026 if (set->flags != 0)
2027 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2028 goto nla_put_failure;
2029
2030 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2031 goto nla_put_failure;
2032 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2033 goto nla_put_failure;
2034 if (set->flags & NFT_SET_MAP) {
2035 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2036 goto nla_put_failure;
2037 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2038 goto nla_put_failure;
2039 }
2040
2041 return nlmsg_end(skb, nlh);
2042
2043nla_put_failure:
2044 nlmsg_trim(skb, nlh);
2045 return -1;
2046}
2047
2048static int nf_tables_set_notify(const struct nft_ctx *ctx,
2049 const struct nft_set *set,
2050 int event)
2051{
2052 struct sk_buff *skb;
2053 u32 portid = NETLINK_CB(ctx->skb).portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002054 bool report;
2055 int err;
2056
2057 report = nlmsg_report(ctx->nlh);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002058 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002059 return 0;
2060
2061 err = -ENOBUFS;
2062 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2063 if (skb == NULL)
2064 goto err;
2065
2066 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2067 if (err < 0) {
2068 kfree_skb(skb);
2069 goto err;
2070 }
2071
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002072 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
Patrick McHardy20a69342013-10-11 12:06:22 +02002073 GFP_KERNEL);
2074err:
2075 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002076 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002077 return err;
2078}
2079
2080static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2081 struct netlink_callback *cb)
2082{
2083 const struct nft_set *set;
2084 unsigned int idx = 0, s_idx = cb->args[0];
2085
2086 if (cb->args[1])
2087 return skb->len;
2088
2089 list_for_each_entry(set, &ctx->table->sets, list) {
2090 if (idx < s_idx)
2091 goto cont;
2092 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2093 NLM_F_MULTI) < 0) {
2094 cb->args[0] = idx;
2095 goto done;
2096 }
2097cont:
2098 idx++;
2099 }
2100 cb->args[1] = 1;
2101done:
2102 return skb->len;
2103}
2104
2105static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2106 struct netlink_callback *cb)
2107{
2108 const struct nft_set *set;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002109 unsigned int idx, s_idx = cb->args[0];
Patrick McHardy20a69342013-10-11 12:06:22 +02002110 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2111
2112 if (cb->args[1])
2113 return skb->len;
2114
2115 list_for_each_entry(table, &ctx->afi->tables, list) {
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002116 if (cur_table) {
2117 if (cur_table != table)
2118 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02002119
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002120 cur_table = NULL;
2121 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002122 ctx->table = table;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002123 idx = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002124 list_for_each_entry(set, &ctx->table->sets, list) {
2125 if (idx < s_idx)
2126 goto cont;
2127 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2128 NLM_F_MULTI) < 0) {
2129 cb->args[0] = idx;
2130 cb->args[2] = (unsigned long) table;
2131 goto done;
2132 }
2133cont:
2134 idx++;
2135 }
2136 }
2137 cb->args[1] = 1;
2138done:
2139 return skb->len;
2140}
2141
2142static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2143{
2144 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2145 struct nlattr *nla[NFTA_SET_MAX + 1];
2146 struct nft_ctx ctx;
2147 int err, ret;
2148
2149 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2150 nft_set_policy);
2151 if (err < 0)
2152 return err;
2153
2154 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2155 if (err < 0)
2156 return err;
2157
2158 if (ctx.table == NULL)
2159 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2160 else
2161 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2162
2163 return ret;
2164}
2165
2166static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2167 const struct nlmsghdr *nlh,
2168 const struct nlattr * const nla[])
2169{
2170 const struct nft_set *set;
2171 struct nft_ctx ctx;
2172 struct sk_buff *skb2;
2173 int err;
2174
2175 /* Verify existance before starting dump */
2176 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2177 if (err < 0)
2178 return err;
2179
2180 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2181 struct netlink_dump_control c = {
2182 .dump = nf_tables_dump_sets,
2183 };
2184 return netlink_dump_start(nlsk, skb, nlh, &c);
2185 }
2186
2187 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2188 if (IS_ERR(set))
2189 return PTR_ERR(set);
2190
2191 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2192 if (skb2 == NULL)
2193 return -ENOMEM;
2194
2195 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2196 if (err < 0)
2197 goto err;
2198
2199 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2200
2201err:
2202 kfree_skb(skb2);
2203 return err;
2204}
2205
2206static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2207 const struct nlmsghdr *nlh,
2208 const struct nlattr * const nla[])
2209{
2210 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2211 const struct nft_set_ops *ops;
2212 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002213 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002214 struct nft_table *table;
2215 struct nft_set *set;
2216 struct nft_ctx ctx;
2217 char name[IFNAMSIZ];
2218 unsigned int size;
2219 bool create;
2220 u32 ktype, klen, dlen, dtype, flags;
2221 int err;
2222
2223 if (nla[NFTA_SET_TABLE] == NULL ||
2224 nla[NFTA_SET_NAME] == NULL ||
2225 nla[NFTA_SET_KEY_LEN] == NULL)
2226 return -EINVAL;
2227
2228 ktype = NFT_DATA_VALUE;
2229 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2230 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2231 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2232 return -EINVAL;
2233 }
2234
2235 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2236 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2237 return -EINVAL;
2238
2239 flags = 0;
2240 if (nla[NFTA_SET_FLAGS] != NULL) {
2241 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2242 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2243 NFT_SET_INTERVAL | NFT_SET_MAP))
2244 return -EINVAL;
2245 }
2246
2247 dtype = 0;
2248 dlen = 0;
2249 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2250 if (!(flags & NFT_SET_MAP))
2251 return -EINVAL;
2252
2253 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2254 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2255 dtype != NFT_DATA_VERDICT)
2256 return -EINVAL;
2257
2258 if (dtype != NFT_DATA_VERDICT) {
2259 if (nla[NFTA_SET_DATA_LEN] == NULL)
2260 return -EINVAL;
2261 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2262 if (dlen == 0 ||
2263 dlen > FIELD_SIZEOF(struct nft_data, data))
2264 return -EINVAL;
2265 } else
2266 dlen = sizeof(struct nft_data);
2267 } else if (flags & NFT_SET_MAP)
2268 return -EINVAL;
2269
2270 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2271
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002272 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002273 if (IS_ERR(afi))
2274 return PTR_ERR(afi);
2275
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002276 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002277 if (IS_ERR(table))
2278 return PTR_ERR(table);
2279
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002280 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002281
2282 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2283 if (IS_ERR(set)) {
2284 if (PTR_ERR(set) != -ENOENT)
2285 return PTR_ERR(set);
2286 set = NULL;
2287 }
2288
2289 if (set != NULL) {
2290 if (nlh->nlmsg_flags & NLM_F_EXCL)
2291 return -EEXIST;
2292 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2293 return -EOPNOTSUPP;
2294 return 0;
2295 }
2296
2297 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2298 return -ENOENT;
2299
2300 ops = nft_select_set_ops(nla);
2301 if (IS_ERR(ops))
2302 return PTR_ERR(ops);
2303
2304 size = 0;
2305 if (ops->privsize != NULL)
2306 size = ops->privsize(nla);
2307
2308 err = -ENOMEM;
2309 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2310 if (set == NULL)
2311 goto err1;
2312
2313 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2314 err = nf_tables_set_alloc_name(&ctx, set, name);
2315 if (err < 0)
2316 goto err2;
2317
2318 INIT_LIST_HEAD(&set->bindings);
2319 set->ops = ops;
2320 set->ktype = ktype;
2321 set->klen = klen;
2322 set->dtype = dtype;
2323 set->dlen = dlen;
2324 set->flags = flags;
2325
2326 err = ops->init(set, nla);
2327 if (err < 0)
2328 goto err2;
2329
2330 list_add_tail(&set->list, &table->sets);
2331 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2332 return 0;
2333
2334err2:
2335 kfree(set);
2336err1:
2337 module_put(ops->owner);
2338 return err;
2339}
2340
2341static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2342{
2343 list_del(&set->list);
2344 if (!(set->flags & NFT_SET_ANONYMOUS))
2345 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2346
2347 set->ops->destroy(set);
2348 module_put(set->ops->owner);
2349 kfree(set);
2350}
2351
2352static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2353 const struct nlmsghdr *nlh,
2354 const struct nlattr * const nla[])
2355{
2356 struct nft_set *set;
2357 struct nft_ctx ctx;
2358 int err;
2359
2360 if (nla[NFTA_SET_TABLE] == NULL)
2361 return -EINVAL;
2362
2363 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2364 if (err < 0)
2365 return err;
2366
2367 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2368 if (IS_ERR(set))
2369 return PTR_ERR(set);
2370 if (!list_empty(&set->bindings))
2371 return -EBUSY;
2372
2373 nf_tables_set_destroy(&ctx, set);
2374 return 0;
2375}
2376
2377static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2378 const struct nft_set *set,
2379 const struct nft_set_iter *iter,
2380 const struct nft_set_elem *elem)
2381{
2382 enum nft_registers dreg;
2383
2384 dreg = nft_type_to_reg(set->dtype);
2385 return nft_validate_data_load(ctx, dreg, &elem->data, set->dtype);
2386}
2387
2388int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2389 struct nft_set_binding *binding)
2390{
2391 struct nft_set_binding *i;
2392 struct nft_set_iter iter;
2393
2394 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2395 return -EBUSY;
2396
2397 if (set->flags & NFT_SET_MAP) {
2398 /* If the set is already bound to the same chain all
2399 * jumps are already validated for that chain.
2400 */
2401 list_for_each_entry(i, &set->bindings, list) {
2402 if (i->chain == binding->chain)
2403 goto bind;
2404 }
2405
2406 iter.skip = 0;
2407 iter.count = 0;
2408 iter.err = 0;
2409 iter.fn = nf_tables_bind_check_setelem;
2410
2411 set->ops->walk(ctx, set, &iter);
2412 if (iter.err < 0) {
2413 /* Destroy anonymous sets if binding fails */
2414 if (set->flags & NFT_SET_ANONYMOUS)
2415 nf_tables_set_destroy(ctx, set);
2416
2417 return iter.err;
2418 }
2419 }
2420bind:
2421 binding->chain = ctx->chain;
2422 list_add_tail(&binding->list, &set->bindings);
2423 return 0;
2424}
2425
2426void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2427 struct nft_set_binding *binding)
2428{
2429 list_del(&binding->list);
2430
2431 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2432 nf_tables_set_destroy(ctx, set);
2433}
2434
2435/*
2436 * Set elements
2437 */
2438
2439static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2440 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2441 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2442 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2443};
2444
2445static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2446 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2447 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2448 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2449};
2450
2451static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2452 const struct sk_buff *skb,
2453 const struct nlmsghdr *nlh,
2454 const struct nlattr * const nla[])
2455{
2456 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2457 const struct nft_af_info *afi;
2458 const struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002459 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002460
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002461 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002462 if (IS_ERR(afi))
2463 return PTR_ERR(afi);
2464
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002465 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002466 if (IS_ERR(table))
2467 return PTR_ERR(table);
2468
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002469 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002470 return 0;
2471}
2472
2473static int nf_tables_fill_setelem(struct sk_buff *skb,
2474 const struct nft_set *set,
2475 const struct nft_set_elem *elem)
2476{
2477 unsigned char *b = skb_tail_pointer(skb);
2478 struct nlattr *nest;
2479
2480 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2481 if (nest == NULL)
2482 goto nla_put_failure;
2483
2484 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2485 set->klen) < 0)
2486 goto nla_put_failure;
2487
2488 if (set->flags & NFT_SET_MAP &&
2489 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2490 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2491 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2492 set->dlen) < 0)
2493 goto nla_put_failure;
2494
2495 if (elem->flags != 0)
2496 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2497 goto nla_put_failure;
2498
2499 nla_nest_end(skb, nest);
2500 return 0;
2501
2502nla_put_failure:
2503 nlmsg_trim(skb, b);
2504 return -EMSGSIZE;
2505}
2506
2507struct nft_set_dump_args {
2508 const struct netlink_callback *cb;
2509 struct nft_set_iter iter;
2510 struct sk_buff *skb;
2511};
2512
2513static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2514 const struct nft_set *set,
2515 const struct nft_set_iter *iter,
2516 const struct nft_set_elem *elem)
2517{
2518 struct nft_set_dump_args *args;
2519
2520 args = container_of(iter, struct nft_set_dump_args, iter);
2521 return nf_tables_fill_setelem(args->skb, set, elem);
2522}
2523
2524static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2525{
2526 const struct nft_set *set;
2527 struct nft_set_dump_args args;
2528 struct nft_ctx ctx;
2529 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2530 struct nfgenmsg *nfmsg;
2531 struct nlmsghdr *nlh;
2532 struct nlattr *nest;
2533 u32 portid, seq;
2534 int event, err;
2535
2536 nfmsg = nlmsg_data(cb->nlh);
2537 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_ELEM_LIST_MAX,
2538 nft_set_elem_list_policy);
2539 if (err < 0)
2540 return err;
2541
2542 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2543 if (err < 0)
2544 return err;
2545
2546 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2547 if (IS_ERR(set))
2548 return PTR_ERR(set);
2549
2550 event = NFT_MSG_NEWSETELEM;
2551 event |= NFNL_SUBSYS_NFTABLES << 8;
2552 portid = NETLINK_CB(cb->skb).portid;
2553 seq = cb->nlh->nlmsg_seq;
2554
2555 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2556 NLM_F_MULTI);
2557 if (nlh == NULL)
2558 goto nla_put_failure;
2559
2560 nfmsg = nlmsg_data(nlh);
2561 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2562 nfmsg->version = NFNETLINK_V0;
2563 nfmsg->res_id = 0;
2564
2565 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2566 goto nla_put_failure;
2567 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2568 goto nla_put_failure;
2569
2570 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2571 if (nest == NULL)
2572 goto nla_put_failure;
2573
2574 args.cb = cb;
2575 args.skb = skb;
2576 args.iter.skip = cb->args[0];
2577 args.iter.count = 0;
2578 args.iter.err = 0;
2579 args.iter.fn = nf_tables_dump_setelem;
2580 set->ops->walk(&ctx, set, &args.iter);
2581
2582 nla_nest_end(skb, nest);
2583 nlmsg_end(skb, nlh);
2584
2585 if (args.iter.err && args.iter.err != -EMSGSIZE)
2586 return args.iter.err;
2587 if (args.iter.count == cb->args[0])
2588 return 0;
2589
2590 cb->args[0] = args.iter.count;
2591 return skb->len;
2592
2593nla_put_failure:
2594 return -ENOSPC;
2595}
2596
2597static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2598 const struct nlmsghdr *nlh,
2599 const struct nlattr * const nla[])
2600{
2601 const struct nft_set *set;
2602 struct nft_ctx ctx;
2603 int err;
2604
2605 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2606 if (err < 0)
2607 return err;
2608
2609 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2610 if (IS_ERR(set))
2611 return PTR_ERR(set);
2612
2613 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2614 struct netlink_dump_control c = {
2615 .dump = nf_tables_dump_set,
2616 };
2617 return netlink_dump_start(nlsk, skb, nlh, &c);
2618 }
2619 return -EOPNOTSUPP;
2620}
2621
2622static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2623 const struct nlattr *attr)
2624{
2625 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2626 struct nft_data_desc d1, d2;
2627 struct nft_set_elem elem;
2628 struct nft_set_binding *binding;
2629 enum nft_registers dreg;
2630 int err;
2631
2632 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2633 nft_set_elem_policy);
2634 if (err < 0)
2635 return err;
2636
2637 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2638 return -EINVAL;
2639
2640 elem.flags = 0;
2641 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2642 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2643 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2644 return -EINVAL;
2645 }
2646
2647 if (set->flags & NFT_SET_MAP) {
2648 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2649 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2650 return -EINVAL;
2651 } else {
2652 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2653 return -EINVAL;
2654 }
2655
2656 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2657 if (err < 0)
2658 goto err1;
2659 err = -EINVAL;
2660 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2661 goto err2;
2662
2663 err = -EEXIST;
2664 if (set->ops->get(set, &elem) == 0)
2665 goto err2;
2666
2667 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2668 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2669 if (err < 0)
2670 goto err2;
2671
2672 err = -EINVAL;
2673 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2674 goto err3;
2675
2676 dreg = nft_type_to_reg(set->dtype);
2677 list_for_each_entry(binding, &set->bindings, list) {
2678 struct nft_ctx bind_ctx = {
2679 .afi = ctx->afi,
2680 .table = ctx->table,
2681 .chain = binding->chain,
2682 };
2683
2684 err = nft_validate_data_load(&bind_ctx, dreg,
2685 &elem.data, d2.type);
2686 if (err < 0)
2687 goto err3;
2688 }
2689 }
2690
2691 err = set->ops->insert(set, &elem);
2692 if (err < 0)
2693 goto err3;
2694
2695 return 0;
2696
2697err3:
2698 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2699 nft_data_uninit(&elem.data, d2.type);
2700err2:
2701 nft_data_uninit(&elem.key, d1.type);
2702err1:
2703 return err;
2704}
2705
2706static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2707 const struct nlmsghdr *nlh,
2708 const struct nlattr * const nla[])
2709{
2710 const struct nlattr *attr;
2711 struct nft_set *set;
2712 struct nft_ctx ctx;
2713 int rem, err;
2714
2715 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2716 if (err < 0)
2717 return err;
2718
2719 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2720 if (IS_ERR(set))
2721 return PTR_ERR(set);
2722 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2723 return -EBUSY;
2724
2725 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2726 err = nft_add_set_elem(&ctx, set, attr);
2727 if (err < 0)
2728 return err;
2729 }
2730 return 0;
2731}
2732
2733static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2734 const struct nlattr *attr)
2735{
2736 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2737 struct nft_data_desc desc;
2738 struct nft_set_elem elem;
2739 int err;
2740
2741 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2742 nft_set_elem_policy);
2743 if (err < 0)
2744 goto err1;
2745
2746 err = -EINVAL;
2747 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2748 goto err1;
2749
2750 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2751 if (err < 0)
2752 goto err1;
2753
2754 err = -EINVAL;
2755 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2756 goto err2;
2757
2758 err = set->ops->get(set, &elem);
2759 if (err < 0)
2760 goto err2;
2761
2762 set->ops->remove(set, &elem);
2763
2764 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2765 if (set->flags & NFT_SET_MAP)
2766 nft_data_uninit(&elem.data, set->dtype);
2767
2768err2:
2769 nft_data_uninit(&elem.key, desc.type);
2770err1:
2771 return err;
2772}
2773
2774static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2775 const struct nlmsghdr *nlh,
2776 const struct nlattr * const nla[])
2777{
2778 const struct nlattr *attr;
2779 struct nft_set *set;
2780 struct nft_ctx ctx;
2781 int rem, err;
2782
2783 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2784 if (err < 0)
2785 return err;
2786
2787 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2788 if (IS_ERR(set))
2789 return PTR_ERR(set);
2790 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2791 return -EBUSY;
2792
2793 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2794 err = nft_del_setelem(&ctx, set, attr);
2795 if (err < 0)
2796 return err;
2797 }
2798 return 0;
2799}
2800
Patrick McHardy96518512013-10-14 11:00:02 +02002801static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2802 [NFT_MSG_NEWTABLE] = {
2803 .call = nf_tables_newtable,
2804 .attr_count = NFTA_TABLE_MAX,
2805 .policy = nft_table_policy,
2806 },
2807 [NFT_MSG_GETTABLE] = {
2808 .call = nf_tables_gettable,
2809 .attr_count = NFTA_TABLE_MAX,
2810 .policy = nft_table_policy,
2811 },
2812 [NFT_MSG_DELTABLE] = {
2813 .call = nf_tables_deltable,
2814 .attr_count = NFTA_TABLE_MAX,
2815 .policy = nft_table_policy,
2816 },
2817 [NFT_MSG_NEWCHAIN] = {
2818 .call = nf_tables_newchain,
2819 .attr_count = NFTA_CHAIN_MAX,
2820 .policy = nft_chain_policy,
2821 },
2822 [NFT_MSG_GETCHAIN] = {
2823 .call = nf_tables_getchain,
2824 .attr_count = NFTA_CHAIN_MAX,
2825 .policy = nft_chain_policy,
2826 },
2827 [NFT_MSG_DELCHAIN] = {
2828 .call = nf_tables_delchain,
2829 .attr_count = NFTA_CHAIN_MAX,
2830 .policy = nft_chain_policy,
2831 },
2832 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002833 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02002834 .attr_count = NFTA_RULE_MAX,
2835 .policy = nft_rule_policy,
2836 },
2837 [NFT_MSG_GETRULE] = {
2838 .call = nf_tables_getrule,
2839 .attr_count = NFTA_RULE_MAX,
2840 .policy = nft_rule_policy,
2841 },
2842 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002843 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02002844 .attr_count = NFTA_RULE_MAX,
2845 .policy = nft_rule_policy,
2846 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002847 [NFT_MSG_NEWSET] = {
2848 .call = nf_tables_newset,
2849 .attr_count = NFTA_SET_MAX,
2850 .policy = nft_set_policy,
2851 },
2852 [NFT_MSG_GETSET] = {
2853 .call = nf_tables_getset,
2854 .attr_count = NFTA_SET_MAX,
2855 .policy = nft_set_policy,
2856 },
2857 [NFT_MSG_DELSET] = {
2858 .call = nf_tables_delset,
2859 .attr_count = NFTA_SET_MAX,
2860 .policy = nft_set_policy,
2861 },
2862 [NFT_MSG_NEWSETELEM] = {
2863 .call = nf_tables_newsetelem,
2864 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2865 .policy = nft_set_elem_list_policy,
2866 },
2867 [NFT_MSG_GETSETELEM] = {
2868 .call = nf_tables_getsetelem,
2869 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2870 .policy = nft_set_elem_list_policy,
2871 },
2872 [NFT_MSG_DELSETELEM] = {
2873 .call = nf_tables_delsetelem,
2874 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2875 .policy = nft_set_elem_list_policy,
2876 },
Patrick McHardy96518512013-10-14 11:00:02 +02002877};
2878
2879static const struct nfnetlink_subsystem nf_tables_subsys = {
2880 .name = "nf_tables",
2881 .subsys_id = NFNL_SUBSYS_NFTABLES,
2882 .cb_count = NFT_MSG_MAX,
2883 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002884 .commit = nf_tables_commit,
2885 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02002886};
2887
Patrick McHardy20a69342013-10-11 12:06:22 +02002888/*
2889 * Loop detection - walk through the ruleset beginning at the destination chain
2890 * of a new jump until either the source chain is reached (loop) or all
2891 * reachable chains have been traversed.
2892 *
2893 * The loop check is performed whenever a new jump verdict is added to an
2894 * expression or verdict map or a verdict map is bound to a new chain.
2895 */
2896
2897static int nf_tables_check_loops(const struct nft_ctx *ctx,
2898 const struct nft_chain *chain);
2899
2900static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2901 const struct nft_set *set,
2902 const struct nft_set_iter *iter,
2903 const struct nft_set_elem *elem)
2904{
2905 switch (elem->data.verdict) {
2906 case NFT_JUMP:
2907 case NFT_GOTO:
2908 return nf_tables_check_loops(ctx, elem->data.chain);
2909 default:
2910 return 0;
2911 }
2912}
2913
2914static int nf_tables_check_loops(const struct nft_ctx *ctx,
2915 const struct nft_chain *chain)
2916{
2917 const struct nft_rule *rule;
2918 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02002919 const struct nft_set *set;
2920 struct nft_set_binding *binding;
2921 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02002922
2923 if (ctx->chain == chain)
2924 return -ELOOP;
2925
2926 list_for_each_entry(rule, &chain->rules, list) {
2927 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002928 const struct nft_data *data = NULL;
2929 int err;
2930
2931 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02002932 continue;
2933
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002934 err = expr->ops->validate(ctx, expr, &data);
2935 if (err < 0)
2936 return err;
2937
Patrick McHardy20a69342013-10-11 12:06:22 +02002938 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002939 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02002940
2941 switch (data->verdict) {
2942 case NFT_JUMP:
2943 case NFT_GOTO:
2944 err = nf_tables_check_loops(ctx, data->chain);
2945 if (err < 0)
2946 return err;
2947 default:
2948 break;
2949 }
2950 }
2951 }
2952
2953 list_for_each_entry(set, &ctx->table->sets, list) {
2954 if (!(set->flags & NFT_SET_MAP) ||
2955 set->dtype != NFT_DATA_VERDICT)
2956 continue;
2957
2958 list_for_each_entry(binding, &set->bindings, list) {
2959 if (binding->chain != chain)
2960 continue;
2961
2962 iter.skip = 0;
2963 iter.count = 0;
2964 iter.err = 0;
2965 iter.fn = nf_tables_loop_check_setelem;
2966
2967 set->ops->walk(ctx, set, &iter);
2968 if (iter.err < 0)
2969 return iter.err;
2970 }
2971 }
2972
2973 return 0;
2974}
2975
Patrick McHardy96518512013-10-14 11:00:02 +02002976/**
2977 * nft_validate_input_register - validate an expressions' input register
2978 *
2979 * @reg: the register number
2980 *
2981 * Validate that the input register is one of the general purpose
2982 * registers.
2983 */
2984int nft_validate_input_register(enum nft_registers reg)
2985{
2986 if (reg <= NFT_REG_VERDICT)
2987 return -EINVAL;
2988 if (reg > NFT_REG_MAX)
2989 return -ERANGE;
2990 return 0;
2991}
2992EXPORT_SYMBOL_GPL(nft_validate_input_register);
2993
2994/**
2995 * nft_validate_output_register - validate an expressions' output register
2996 *
2997 * @reg: the register number
2998 *
2999 * Validate that the output register is one of the general purpose
3000 * registers or the verdict register.
3001 */
3002int nft_validate_output_register(enum nft_registers reg)
3003{
3004 if (reg < NFT_REG_VERDICT)
3005 return -EINVAL;
3006 if (reg > NFT_REG_MAX)
3007 return -ERANGE;
3008 return 0;
3009}
3010EXPORT_SYMBOL_GPL(nft_validate_output_register);
3011
3012/**
3013 * nft_validate_data_load - validate an expressions' data load
3014 *
3015 * @ctx: context of the expression performing the load
3016 * @reg: the destination register number
3017 * @data: the data to load
3018 * @type: the data type
3019 *
3020 * Validate that a data load uses the appropriate data type for
3021 * the destination register. A value of NULL for the data means
3022 * that its runtime gathered data, which is always of type
3023 * NFT_DATA_VALUE.
3024 */
3025int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3026 const struct nft_data *data,
3027 enum nft_data_types type)
3028{
Patrick McHardy20a69342013-10-11 12:06:22 +02003029 int err;
3030
Patrick McHardy96518512013-10-14 11:00:02 +02003031 switch (reg) {
3032 case NFT_REG_VERDICT:
3033 if (data == NULL || type != NFT_DATA_VERDICT)
3034 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003035
3036 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3037 err = nf_tables_check_loops(ctx, data->chain);
3038 if (err < 0)
3039 return err;
3040
3041 if (ctx->chain->level + 1 > data->chain->level) {
3042 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3043 return -EMLINK;
3044 data->chain->level = ctx->chain->level + 1;
3045 }
3046 }
3047
Patrick McHardy96518512013-10-14 11:00:02 +02003048 return 0;
3049 default:
3050 if (data != NULL && type != NFT_DATA_VALUE)
3051 return -EINVAL;
3052 return 0;
3053 }
3054}
3055EXPORT_SYMBOL_GPL(nft_validate_data_load);
3056
3057static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3058 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3059 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3060 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3061};
3062
3063static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3064 struct nft_data_desc *desc, const struct nlattr *nla)
3065{
3066 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3067 struct nft_chain *chain;
3068 int err;
3069
3070 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3071 if (err < 0)
3072 return err;
3073
3074 if (!tb[NFTA_VERDICT_CODE])
3075 return -EINVAL;
3076 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3077
3078 switch (data->verdict) {
3079 case NF_ACCEPT:
3080 case NF_DROP:
3081 case NF_QUEUE:
3082 case NFT_CONTINUE:
3083 case NFT_BREAK:
3084 case NFT_RETURN:
3085 desc->len = sizeof(data->verdict);
3086 break;
3087 case NFT_JUMP:
3088 case NFT_GOTO:
3089 if (!tb[NFTA_VERDICT_CHAIN])
3090 return -EINVAL;
3091 chain = nf_tables_chain_lookup(ctx->table,
3092 tb[NFTA_VERDICT_CHAIN]);
3093 if (IS_ERR(chain))
3094 return PTR_ERR(chain);
3095 if (chain->flags & NFT_BASE_CHAIN)
3096 return -EOPNOTSUPP;
3097
Patrick McHardy96518512013-10-14 11:00:02 +02003098 chain->use++;
3099 data->chain = chain;
3100 desc->len = sizeof(data);
3101 break;
3102 default:
3103 return -EINVAL;
3104 }
3105
3106 desc->type = NFT_DATA_VERDICT;
3107 return 0;
3108}
3109
3110static void nft_verdict_uninit(const struct nft_data *data)
3111{
3112 switch (data->verdict) {
3113 case NFT_JUMP:
3114 case NFT_GOTO:
3115 data->chain->use--;
3116 break;
3117 }
3118}
3119
3120static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3121{
3122 struct nlattr *nest;
3123
3124 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3125 if (!nest)
3126 goto nla_put_failure;
3127
3128 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3129 goto nla_put_failure;
3130
3131 switch (data->verdict) {
3132 case NFT_JUMP:
3133 case NFT_GOTO:
3134 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3135 goto nla_put_failure;
3136 }
3137 nla_nest_end(skb, nest);
3138 return 0;
3139
3140nla_put_failure:
3141 return -1;
3142}
3143
3144static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3145 struct nft_data_desc *desc, const struct nlattr *nla)
3146{
3147 unsigned int len;
3148
3149 len = nla_len(nla);
3150 if (len == 0)
3151 return -EINVAL;
3152 if (len > sizeof(data->data))
3153 return -EOVERFLOW;
3154
3155 nla_memcpy(data->data, nla, sizeof(data->data));
3156 desc->type = NFT_DATA_VALUE;
3157 desc->len = len;
3158 return 0;
3159}
3160
3161static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3162 unsigned int len)
3163{
3164 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3165}
3166
3167static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3168 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3169 .len = FIELD_SIZEOF(struct nft_data, data) },
3170 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3171};
3172
3173/**
3174 * nft_data_init - parse nf_tables data netlink attributes
3175 *
3176 * @ctx: context of the expression using the data
3177 * @data: destination struct nft_data
3178 * @desc: data description
3179 * @nla: netlink attribute containing data
3180 *
3181 * Parse the netlink data attributes and initialize a struct nft_data.
3182 * The type and length of data are returned in the data description.
3183 *
3184 * The caller can indicate that it only wants to accept data of type
3185 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3186 */
3187int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3188 struct nft_data_desc *desc, const struct nlattr *nla)
3189{
3190 struct nlattr *tb[NFTA_DATA_MAX + 1];
3191 int err;
3192
3193 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3194 if (err < 0)
3195 return err;
3196
3197 if (tb[NFTA_DATA_VALUE])
3198 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3199 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3200 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3201 return -EINVAL;
3202}
3203EXPORT_SYMBOL_GPL(nft_data_init);
3204
3205/**
3206 * nft_data_uninit - release a nft_data item
3207 *
3208 * @data: struct nft_data to release
3209 * @type: type of data
3210 *
3211 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3212 * all others need to be released by calling this function.
3213 */
3214void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3215{
3216 switch (type) {
3217 case NFT_DATA_VALUE:
3218 return;
3219 case NFT_DATA_VERDICT:
3220 return nft_verdict_uninit(data);
3221 default:
3222 WARN_ON(1);
3223 }
3224}
3225EXPORT_SYMBOL_GPL(nft_data_uninit);
3226
3227int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3228 enum nft_data_types type, unsigned int len)
3229{
3230 struct nlattr *nest;
3231 int err;
3232
3233 nest = nla_nest_start(skb, attr);
3234 if (nest == NULL)
3235 return -1;
3236
3237 switch (type) {
3238 case NFT_DATA_VALUE:
3239 err = nft_value_dump(skb, data, len);
3240 break;
3241 case NFT_DATA_VERDICT:
3242 err = nft_verdict_dump(skb, data);
3243 break;
3244 default:
3245 err = -EINVAL;
3246 WARN_ON(1);
3247 }
3248
3249 nla_nest_end(skb, nest);
3250 return err;
3251}
3252EXPORT_SYMBOL_GPL(nft_data_dump);
3253
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003254static int nf_tables_init_net(struct net *net)
3255{
3256 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003257 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003258 return 0;
3259}
3260
3261static struct pernet_operations nf_tables_net_ops = {
3262 .init = nf_tables_init_net,
3263};
3264
Patrick McHardy96518512013-10-14 11:00:02 +02003265static int __init nf_tables_module_init(void)
3266{
3267 int err;
3268
3269 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3270 GFP_KERNEL);
3271 if (info == NULL) {
3272 err = -ENOMEM;
3273 goto err1;
3274 }
3275
3276 err = nf_tables_core_module_init();
3277 if (err < 0)
3278 goto err2;
3279
3280 err = nfnetlink_subsys_register(&nf_tables_subsys);
3281 if (err < 0)
3282 goto err3;
3283
3284 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003285 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003286err3:
3287 nf_tables_core_module_exit();
3288err2:
3289 kfree(info);
3290err1:
3291 return err;
3292}
3293
3294static void __exit nf_tables_module_exit(void)
3295{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003296 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003297 nfnetlink_subsys_unregister(&nf_tables_subsys);
3298 nf_tables_core_module_exit();
3299 kfree(info);
3300}
3301
3302module_init(nf_tables_module_init);
3303module_exit(nf_tables_module_exit);
3304
3305MODULE_LICENSE("GPL");
3306MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3307MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);