blob: 02d3cc65690ca60aa7fd25db7cd35c2feaceec5e [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
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +020091static void nft_ctx_init(struct nft_ctx *ctx,
92 const struct sk_buff *skb,
93 const struct nlmsghdr *nlh,
94 struct nft_af_info *afi,
95 struct nft_table *table,
96 struct nft_chain *chain,
97 const struct nlattr * const *nla)
98{
99 ctx->net = sock_net(skb->sk);
100 ctx->skb = skb;
101 ctx->nlh = nlh;
102 ctx->afi = afi;
103 ctx->table = table;
104 ctx->chain = chain;
105 ctx->nla = nla;
106}
107
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +0200108static struct nft_trans *nft_trans_alloc(struct nft_ctx *ctx, int msg_type,
109 u32 size)
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +0200110{
111 struct nft_trans *trans;
112
113 trans = kzalloc(sizeof(struct nft_trans) + size, GFP_KERNEL);
114 if (trans == NULL)
115 return NULL;
116
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +0200117 trans->msg_type = msg_type;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +0200118 trans->ctx = *ctx;
119
120 return trans;
121}
122
123static void nft_trans_destroy(struct nft_trans *trans)
124{
125 list_del(&trans->list);
126 kfree(trans);
127}
128
Patrick McHardy96518512013-10-14 11:00:02 +0200129/*
130 * Tables
131 */
132
133static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
134 const struct nlattr *nla)
135{
136 struct nft_table *table;
137
138 list_for_each_entry(table, &afi->tables, list) {
139 if (!nla_strcmp(nla, table->name))
140 return table;
141 }
142 return NULL;
143}
144
145static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200146 const struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +0200147{
148 struct nft_table *table;
149
150 if (nla == NULL)
151 return ERR_PTR(-EINVAL);
152
153 table = nft_table_lookup(afi, nla);
154 if (table != NULL)
155 return table;
156
Patrick McHardy96518512013-10-14 11:00:02 +0200157 return ERR_PTR(-ENOENT);
158}
159
160static inline u64 nf_tables_alloc_handle(struct nft_table *table)
161{
162 return ++table->hgenerator;
163}
164
Patrick McHardy2a37d752014-01-09 18:42:37 +0000165static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200166
Patrick McHardy2a37d752014-01-09 18:42:37 +0000167static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000168__nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200169{
170 int i;
171
Patrick McHardybaae3e62014-01-09 18:42:34 +0000172 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200173 if (chain_type[family][i] != NULL &&
174 !nla_strcmp(nla, chain_type[family][i]->name))
Patrick McHardybaae3e62014-01-09 18:42:34 +0000175 return chain_type[family][i];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200176 }
Patrick McHardybaae3e62014-01-09 18:42:34 +0000177 return NULL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200178}
179
Patrick McHardy2a37d752014-01-09 18:42:37 +0000180static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000181nf_tables_chain_type_lookup(const struct nft_af_info *afi,
182 const struct nlattr *nla,
183 bool autoload)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200184{
Patrick McHardy2a37d752014-01-09 18:42:37 +0000185 const struct nf_chain_type *type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200186
187 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000188 if (type != NULL)
189 return type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200190#ifdef CONFIG_MODULES
Patrick McHardy93b08062014-01-09 18:42:36 +0000191 if (autoload) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200192 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
193 request_module("nft-chain-%u-%*.s", afi->family,
194 nla_len(nla)-1, (const char *)nla_data(nla));
195 nfnl_lock(NFNL_SUBSYS_NFTABLES);
196 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000197 if (type != NULL)
198 return ERR_PTR(-EAGAIN);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200199 }
200#endif
Patrick McHardy93b08062014-01-09 18:42:36 +0000201 return ERR_PTR(-ENOENT);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200202}
203
Patrick McHardy96518512013-10-14 11:00:02 +0200204static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
205 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200206 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
Patrick McHardy96518512013-10-14 11:00:02 +0200207};
208
209static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
210 int event, u32 flags, int family,
211 const struct nft_table *table)
212{
213 struct nlmsghdr *nlh;
214 struct nfgenmsg *nfmsg;
215
216 event |= NFNL_SUBSYS_NFTABLES << 8;
217 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
218 if (nlh == NULL)
219 goto nla_put_failure;
220
221 nfmsg = nlmsg_data(nlh);
222 nfmsg->nfgen_family = family;
223 nfmsg->version = NFNETLINK_V0;
224 nfmsg->res_id = 0;
225
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200226 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
Tomasz Bursztykad8bcc7682013-12-12 15:00:42 +0200227 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
228 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
Patrick McHardy96518512013-10-14 11:00:02 +0200229 goto nla_put_failure;
230
231 return nlmsg_end(skb, nlh);
232
233nla_put_failure:
234 nlmsg_trim(skb, nlh);
235 return -1;
236}
237
238static int nf_tables_table_notify(const struct sk_buff *oskb,
239 const struct nlmsghdr *nlh,
240 const struct nft_table *table,
241 int event, int family)
242{
243 struct sk_buff *skb;
244 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
245 u32 seq = nlh ? nlh->nlmsg_seq : 0;
246 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
247 bool report;
248 int err;
249
250 report = nlh ? nlmsg_report(nlh) : false;
251 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
252 return 0;
253
254 err = -ENOBUFS;
255 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
256 if (skb == NULL)
257 goto err;
258
259 err = nf_tables_fill_table_info(skb, portid, seq, event, 0,
260 family, table);
261 if (err < 0) {
262 kfree_skb(skb);
263 goto err;
264 }
265
266 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
267 GFP_KERNEL);
268err:
269 if (err < 0)
270 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
271 return err;
272}
273
274static int nf_tables_dump_tables(struct sk_buff *skb,
275 struct netlink_callback *cb)
276{
277 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
278 const struct nft_af_info *afi;
279 const struct nft_table *table;
280 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200281 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200282 int family = nfmsg->nfgen_family;
283
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200284 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200285 if (family != NFPROTO_UNSPEC && family != afi->family)
286 continue;
287
288 list_for_each_entry(table, &afi->tables, list) {
289 if (idx < s_idx)
290 goto cont;
291 if (idx > s_idx)
292 memset(&cb->args[1], 0,
293 sizeof(cb->args) - sizeof(cb->args[0]));
294 if (nf_tables_fill_table_info(skb,
295 NETLINK_CB(cb->skb).portid,
296 cb->nlh->nlmsg_seq,
297 NFT_MSG_NEWTABLE,
298 NLM_F_MULTI,
299 afi->family, table) < 0)
300 goto done;
301cont:
302 idx++;
303 }
304 }
305done:
306 cb->args[0] = idx;
307 return skb->len;
308}
309
310static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
311 const struct nlmsghdr *nlh,
312 const struct nlattr * const nla[])
313{
314 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
315 const struct nft_af_info *afi;
316 const struct nft_table *table;
317 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200318 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200319 int family = nfmsg->nfgen_family;
320 int err;
321
322 if (nlh->nlmsg_flags & NLM_F_DUMP) {
323 struct netlink_dump_control c = {
324 .dump = nf_tables_dump_tables,
325 };
326 return netlink_dump_start(nlsk, skb, nlh, &c);
327 }
328
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200329 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200330 if (IS_ERR(afi))
331 return PTR_ERR(afi);
332
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200333 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200334 if (IS_ERR(table))
335 return PTR_ERR(table);
336
337 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
338 if (!skb2)
339 return -ENOMEM;
340
341 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
342 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
343 family, table);
344 if (err < 0)
345 goto err;
346
347 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
348
349err:
350 kfree_skb(skb2);
351 return err;
352}
353
Patrick McHardy115a60b2014-01-03 12:16:15 +0000354static int nf_tables_table_enable(const struct nft_af_info *afi,
355 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200356{
357 struct nft_chain *chain;
358 int err, i = 0;
359
360 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100361 if (!(chain->flags & NFT_BASE_CHAIN))
362 continue;
363
Patrick McHardy115a60b2014-01-03 12:16:15 +0000364 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200365 if (err < 0)
366 goto err;
367
368 i++;
369 }
370 return 0;
371err:
372 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100373 if (!(chain->flags & NFT_BASE_CHAIN))
374 continue;
375
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200376 if (i-- <= 0)
377 break;
378
Patrick McHardy115a60b2014-01-03 12:16:15 +0000379 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200380 }
381 return err;
382}
383
Patrick McHardy115a60b2014-01-03 12:16:15 +0000384static int nf_tables_table_disable(const struct nft_af_info *afi,
385 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200386{
387 struct nft_chain *chain;
388
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100389 list_for_each_entry(chain, &table->chains, list) {
390 if (chain->flags & NFT_BASE_CHAIN)
Patrick McHardy115a60b2014-01-03 12:16:15 +0000391 nf_unregister_hooks(nft_base_chain(chain)->ops,
392 afi->nops);
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100393 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200394
395 return 0;
396}
397
398static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,
399 const struct nlmsghdr *nlh,
400 const struct nlattr * const nla[],
401 struct nft_af_info *afi, struct nft_table *table)
402{
403 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
404 int family = nfmsg->nfgen_family, ret = 0;
405
406 if (nla[NFTA_TABLE_FLAGS]) {
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000407 u32 flags;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200408
409 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
410 if (flags & ~NFT_TABLE_F_DORMANT)
411 return -EINVAL;
412
413 if ((flags & NFT_TABLE_F_DORMANT) &&
414 !(table->flags & NFT_TABLE_F_DORMANT)) {
Patrick McHardy115a60b2014-01-03 12:16:15 +0000415 ret = nf_tables_table_disable(afi, table);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200416 if (ret >= 0)
417 table->flags |= NFT_TABLE_F_DORMANT;
418 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
419 table->flags & NFT_TABLE_F_DORMANT) {
Patrick McHardy115a60b2014-01-03 12:16:15 +0000420 ret = nf_tables_table_enable(afi, table);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200421 if (ret >= 0)
422 table->flags &= ~NFT_TABLE_F_DORMANT;
423 }
424 if (ret < 0)
425 goto err;
426 }
427
428 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
429err:
430 return ret;
431}
432
Patrick McHardy96518512013-10-14 11:00:02 +0200433static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
434 const struct nlmsghdr *nlh,
435 const struct nlattr * const nla[])
436{
437 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
438 const struct nlattr *name;
439 struct nft_af_info *afi;
440 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200441 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200442 int family = nfmsg->nfgen_family;
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000443 u32 flags = 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200444
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200445 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200446 if (IS_ERR(afi))
447 return PTR_ERR(afi);
448
449 name = nla[NFTA_TABLE_NAME];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200450 table = nf_tables_table_lookup(afi, name);
Patrick McHardy96518512013-10-14 11:00:02 +0200451 if (IS_ERR(table)) {
452 if (PTR_ERR(table) != -ENOENT)
453 return PTR_ERR(table);
454 table = NULL;
455 }
456
457 if (table != NULL) {
458 if (nlh->nlmsg_flags & NLM_F_EXCL)
459 return -EEXIST;
460 if (nlh->nlmsg_flags & NLM_F_REPLACE)
461 return -EOPNOTSUPP;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200462 return nf_tables_updtable(nlsk, skb, nlh, nla, afi, table);
Patrick McHardy96518512013-10-14 11:00:02 +0200463 }
464
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000465 if (nla[NFTA_TABLE_FLAGS]) {
466 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
467 if (flags & ~NFT_TABLE_F_DORMANT)
468 return -EINVAL;
469 }
470
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000471 if (!try_module_get(afi->owner))
472 return -EAFNOSUPPORT;
473
Patrick McHardy96518512013-10-14 11:00:02 +0200474 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000475 if (table == NULL) {
476 module_put(afi->owner);
Patrick McHardy96518512013-10-14 11:00:02 +0200477 return -ENOMEM;
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000478 }
Patrick McHardy96518512013-10-14 11:00:02 +0200479
480 nla_strlcpy(table->name, name, nla_len(name));
481 INIT_LIST_HEAD(&table->chains);
Patrick McHardy20a69342013-10-11 12:06:22 +0200482 INIT_LIST_HEAD(&table->sets);
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000483 table->flags = flags;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200484
Patrick McHardy96518512013-10-14 11:00:02 +0200485 list_add_tail(&table->list, &afi->tables);
486 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
487 return 0;
488}
489
490static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
491 const struct nlmsghdr *nlh,
492 const struct nlattr * const nla[])
493{
494 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
495 struct nft_af_info *afi;
496 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200497 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200498 int family = nfmsg->nfgen_family;
499
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200500 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200501 if (IS_ERR(afi))
502 return PTR_ERR(afi);
503
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200504 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200505 if (IS_ERR(table))
506 return PTR_ERR(table);
507
Patrick McHardy44a6f0d2014-01-09 18:42:41 +0000508 if (!list_empty(&table->chains) || !list_empty(&table->sets))
Patrick McHardy96518512013-10-14 11:00:02 +0200509 return -EBUSY;
510
511 list_del(&table->list);
512 nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
513 kfree(table);
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000514 module_put(afi->owner);
Patrick McHardy96518512013-10-14 11:00:02 +0200515 return 0;
516}
517
Patrick McHardy2a37d752014-01-09 18:42:37 +0000518int nft_register_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200519{
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200520 int err = 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200521
522 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200523 if (chain_type[ctype->family][ctype->type] != NULL) {
524 err = -EBUSY;
525 goto out;
Patrick McHardy96518512013-10-14 11:00:02 +0200526 }
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200527 chain_type[ctype->family][ctype->type] = ctype;
528out:
Patrick McHardy96518512013-10-14 11:00:02 +0200529 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
530 return err;
531}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200532EXPORT_SYMBOL_GPL(nft_register_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200533
Patrick McHardy2a37d752014-01-09 18:42:37 +0000534void nft_unregister_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200535{
Patrick McHardy96518512013-10-14 11:00:02 +0200536 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200537 chain_type[ctype->family][ctype->type] = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200538 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
539}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200540EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200541
542/*
543 * Chains
544 */
545
546static struct nft_chain *
547nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
548{
549 struct nft_chain *chain;
550
551 list_for_each_entry(chain, &table->chains, list) {
552 if (chain->handle == handle)
553 return chain;
554 }
555
556 return ERR_PTR(-ENOENT);
557}
558
559static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
560 const struct nlattr *nla)
561{
562 struct nft_chain *chain;
563
564 if (nla == NULL)
565 return ERR_PTR(-EINVAL);
566
567 list_for_each_entry(chain, &table->chains, list) {
568 if (!nla_strcmp(nla, chain->name))
569 return chain;
570 }
571
572 return ERR_PTR(-ENOENT);
573}
574
575static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
576 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
577 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
578 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
579 .len = NFT_CHAIN_MAXNAMELEN - 1 },
580 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200581 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
Pablo Neira4c1f7812014-03-31 17:43:47 +0200582 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200583 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
Patrick McHardy96518512013-10-14 11:00:02 +0200584};
585
586static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
587 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
588 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
589};
590
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200591static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
592{
593 struct nft_stats *cpu_stats, total;
594 struct nlattr *nest;
595 int cpu;
596
597 memset(&total, 0, sizeof(total));
598 for_each_possible_cpu(cpu) {
599 cpu_stats = per_cpu_ptr(stats, cpu);
600 total.pkts += cpu_stats->pkts;
601 total.bytes += cpu_stats->bytes;
602 }
603 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
604 if (nest == NULL)
605 goto nla_put_failure;
606
607 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
608 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
609 goto nla_put_failure;
610
611 nla_nest_end(skb, nest);
612 return 0;
613
614nla_put_failure:
615 return -ENOSPC;
616}
617
Patrick McHardy96518512013-10-14 11:00:02 +0200618static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
619 int event, u32 flags, int family,
620 const struct nft_table *table,
621 const struct nft_chain *chain)
622{
623 struct nlmsghdr *nlh;
624 struct nfgenmsg *nfmsg;
625
626 event |= NFNL_SUBSYS_NFTABLES << 8;
627 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
628 if (nlh == NULL)
629 goto nla_put_failure;
630
631 nfmsg = nlmsg_data(nlh);
632 nfmsg->nfgen_family = family;
633 nfmsg->version = NFNETLINK_V0;
634 nfmsg->res_id = 0;
635
636 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
637 goto nla_put_failure;
638 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
639 goto nla_put_failure;
640 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
641 goto nla_put_failure;
642
643 if (chain->flags & NFT_BASE_CHAIN) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200644 const struct nft_base_chain *basechain = nft_base_chain(chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000645 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200646 struct nlattr *nest;
647
648 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
Patrick McHardy96518512013-10-14 11:00:02 +0200649 if (nest == NULL)
650 goto nla_put_failure;
651 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
652 goto nla_put_failure;
653 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
654 goto nla_put_failure;
655 nla_nest_end(skb, nest);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200656
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200657 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
658 htonl(basechain->policy)))
659 goto nla_put_failure;
660
Patrick McHardybaae3e62014-01-09 18:42:34 +0000661 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
662 goto nla_put_failure;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200663
664 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
665 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +0200666 }
667
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200668 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
669 goto nla_put_failure;
670
Patrick McHardy96518512013-10-14 11:00:02 +0200671 return nlmsg_end(skb, nlh);
672
673nla_put_failure:
674 nlmsg_trim(skb, nlh);
675 return -1;
676}
677
678static int nf_tables_chain_notify(const struct sk_buff *oskb,
679 const struct nlmsghdr *nlh,
680 const struct nft_table *table,
681 const struct nft_chain *chain,
682 int event, int family)
683{
684 struct sk_buff *skb;
685 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
686 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
687 u32 seq = nlh ? nlh->nlmsg_seq : 0;
688 bool report;
689 int err;
690
691 report = nlh ? nlmsg_report(nlh) : false;
692 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
693 return 0;
694
695 err = -ENOBUFS;
696 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
697 if (skb == NULL)
698 goto err;
699
700 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
701 table, chain);
702 if (err < 0) {
703 kfree_skb(skb);
704 goto err;
705 }
706
707 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
708 GFP_KERNEL);
709err:
710 if (err < 0)
711 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
712 return err;
713}
714
715static int nf_tables_dump_chains(struct sk_buff *skb,
716 struct netlink_callback *cb)
717{
718 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
719 const struct nft_af_info *afi;
720 const struct nft_table *table;
721 const struct nft_chain *chain;
722 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200723 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200724 int family = nfmsg->nfgen_family;
725
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200726 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200727 if (family != NFPROTO_UNSPEC && family != afi->family)
728 continue;
729
730 list_for_each_entry(table, &afi->tables, list) {
731 list_for_each_entry(chain, &table->chains, list) {
732 if (idx < s_idx)
733 goto cont;
734 if (idx > s_idx)
735 memset(&cb->args[1], 0,
736 sizeof(cb->args) - sizeof(cb->args[0]));
737 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
738 cb->nlh->nlmsg_seq,
739 NFT_MSG_NEWCHAIN,
740 NLM_F_MULTI,
741 afi->family, table, chain) < 0)
742 goto done;
743cont:
744 idx++;
745 }
746 }
747 }
748done:
749 cb->args[0] = idx;
750 return skb->len;
751}
752
753
754static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
755 const struct nlmsghdr *nlh,
756 const struct nlattr * const nla[])
757{
758 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
759 const struct nft_af_info *afi;
760 const struct nft_table *table;
761 const struct nft_chain *chain;
762 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200763 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200764 int family = nfmsg->nfgen_family;
765 int err;
766
767 if (nlh->nlmsg_flags & NLM_F_DUMP) {
768 struct netlink_dump_control c = {
769 .dump = nf_tables_dump_chains,
770 };
771 return netlink_dump_start(nlsk, skb, nlh, &c);
772 }
773
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200774 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200775 if (IS_ERR(afi))
776 return PTR_ERR(afi);
777
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200778 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200779 if (IS_ERR(table))
780 return PTR_ERR(table);
781
782 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
783 if (IS_ERR(chain))
784 return PTR_ERR(chain);
785
786 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
787 if (!skb2)
788 return -ENOMEM;
789
790 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
791 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
792 family, table, chain);
793 if (err < 0)
794 goto err;
795
796 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
797
798err:
799 kfree_skb(skb2);
800 return err;
801}
802
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200803static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
804 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
805 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
806};
807
808static int
809nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
810{
811 struct nlattr *tb[NFTA_COUNTER_MAX+1];
812 struct nft_stats __percpu *newstats;
813 struct nft_stats *stats;
814 int err;
815
816 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
817 if (err < 0)
818 return err;
819
820 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
821 return -EINVAL;
822
823 newstats = alloc_percpu(struct nft_stats);
824 if (newstats == NULL)
825 return -ENOMEM;
826
827 /* Restore old counters on this cpu, no problem. Per-cpu statistics
828 * are not exposed to userspace.
829 */
830 stats = this_cpu_ptr(newstats);
831 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
832 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
833
834 if (chain->stats) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200835 struct nft_stats __percpu *oldstats =
Patrick McHardy67a8fc22014-02-18 18:06:49 +0000836 nft_dereference(chain->stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200837
838 rcu_assign_pointer(chain->stats, newstats);
839 synchronize_rcu();
840 free_percpu(oldstats);
841 } else
842 rcu_assign_pointer(chain->stats, newstats);
843
844 return 0;
845}
846
Patrick McHardy96518512013-10-14 11:00:02 +0200847static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
848 const struct nlmsghdr *nlh,
849 const struct nlattr * const nla[])
850{
851 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
852 const struct nlattr * uninitialized_var(name);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +0200853 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +0200854 struct nft_table *table;
855 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200856 struct nft_base_chain *basechain = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200857 struct nlattr *ha[NFTA_HOOK_MAX + 1];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200858 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200859 int family = nfmsg->nfgen_family;
Patrick McHardy57de2a02014-01-09 18:42:31 +0000860 u8 policy = NF_ACCEPT;
Patrick McHardy96518512013-10-14 11:00:02 +0200861 u64 handle = 0;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000862 unsigned int i;
Patrick McHardy96518512013-10-14 11:00:02 +0200863 int err;
864 bool create;
865
866 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
867
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200868 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200869 if (IS_ERR(afi))
870 return PTR_ERR(afi);
871
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200872 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200873 if (IS_ERR(table))
874 return PTR_ERR(table);
875
Patrick McHardy96518512013-10-14 11:00:02 +0200876 chain = NULL;
877 name = nla[NFTA_CHAIN_NAME];
878
879 if (nla[NFTA_CHAIN_HANDLE]) {
880 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
881 chain = nf_tables_chain_lookup_byhandle(table, handle);
882 if (IS_ERR(chain))
883 return PTR_ERR(chain);
884 } else {
885 chain = nf_tables_chain_lookup(table, name);
886 if (IS_ERR(chain)) {
887 if (PTR_ERR(chain) != -ENOENT)
888 return PTR_ERR(chain);
889 chain = NULL;
890 }
891 }
892
Patrick McHardy57de2a02014-01-09 18:42:31 +0000893 if (nla[NFTA_CHAIN_POLICY]) {
894 if ((chain != NULL &&
895 !(chain->flags & NFT_BASE_CHAIN)) ||
896 nla[NFTA_CHAIN_HOOK] == NULL)
897 return -EOPNOTSUPP;
898
Pablo Neira Ayuso8f46df12014-01-10 15:11:25 +0100899 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
Patrick McHardy57de2a02014-01-09 18:42:31 +0000900 switch (policy) {
901 case NF_DROP:
902 case NF_ACCEPT:
903 break;
904 default:
905 return -EINVAL;
906 }
907 }
908
Patrick McHardy96518512013-10-14 11:00:02 +0200909 if (chain != NULL) {
910 if (nlh->nlmsg_flags & NLM_F_EXCL)
911 return -EEXIST;
912 if (nlh->nlmsg_flags & NLM_F_REPLACE)
913 return -EOPNOTSUPP;
914
915 if (nla[NFTA_CHAIN_HANDLE] && name &&
916 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
917 return -EEXIST;
918
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200919 if (nla[NFTA_CHAIN_COUNTERS]) {
920 if (!(chain->flags & NFT_BASE_CHAIN))
921 return -EOPNOTSUPP;
922
923 err = nf_tables_counters(nft_base_chain(chain),
924 nla[NFTA_CHAIN_COUNTERS]);
925 if (err < 0)
926 return err;
927 }
928
Patrick McHardy4401a862014-01-09 18:42:32 +0000929 if (nla[NFTA_CHAIN_POLICY])
930 nft_base_chain(chain)->policy = policy;
931
Patrick McHardy96518512013-10-14 11:00:02 +0200932 if (nla[NFTA_CHAIN_HANDLE] && name)
933 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
934
935 goto notify;
936 }
937
Patrick McHardy75820672014-01-09 18:42:33 +0000938 if (table->use == UINT_MAX)
939 return -EOVERFLOW;
940
Patrick McHardy96518512013-10-14 11:00:02 +0200941 if (nla[NFTA_CHAIN_HOOK]) {
Patrick McHardy2a37d752014-01-09 18:42:37 +0000942 const struct nf_chain_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +0200943 struct nf_hook_ops *ops;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200944 nf_hookfn *hookfn;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000945 u32 hooknum, priority;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200946
Patrick McHardybaae3e62014-01-09 18:42:34 +0000947 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200948 if (nla[NFTA_CHAIN_TYPE]) {
949 type = nf_tables_chain_type_lookup(afi,
950 nla[NFTA_CHAIN_TYPE],
951 create);
Patrick McHardy93b08062014-01-09 18:42:36 +0000952 if (IS_ERR(type))
953 return PTR_ERR(type);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200954 }
Patrick McHardy96518512013-10-14 11:00:02 +0200955
956 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
957 nft_hook_policy);
958 if (err < 0)
959 return err;
960 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
961 ha[NFTA_HOOK_PRIORITY] == NULL)
962 return -EINVAL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200963
964 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
965 if (hooknum >= afi->nhooks)
Patrick McHardy96518512013-10-14 11:00:02 +0200966 return -EINVAL;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000967 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
Patrick McHardy96518512013-10-14 11:00:02 +0200968
Patrick McHardybaae3e62014-01-09 18:42:34 +0000969 if (!(type->hook_mask & (1 << hooknum)))
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200970 return -EOPNOTSUPP;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000971 if (!try_module_get(type->owner))
Patrick McHardybaae3e62014-01-09 18:42:34 +0000972 return -ENOENT;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000973 hookfn = type->hooks[hooknum];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200974
Patrick McHardy96518512013-10-14 11:00:02 +0200975 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
976 if (basechain == NULL)
977 return -ENOMEM;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200978
Patrick McHardy4401a862014-01-09 18:42:32 +0000979 if (nla[NFTA_CHAIN_COUNTERS]) {
980 err = nf_tables_counters(basechain,
981 nla[NFTA_CHAIN_COUNTERS]);
982 if (err < 0) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000983 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +0000984 kfree(basechain);
985 return err;
986 }
987 } else {
988 struct nft_stats __percpu *newstats;
989
990 newstats = alloc_percpu(struct nft_stats);
991 if (newstats == NULL) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000992 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +0000993 kfree(basechain);
994 return -ENOMEM;
995 }
996 rcu_assign_pointer(basechain->stats, newstats);
997 }
998
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200999 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +02001000 chain = &basechain->chain;
1001
Patrick McHardy115a60b2014-01-03 12:16:15 +00001002 for (i = 0; i < afi->nops; i++) {
1003 ops = &basechain->ops[i];
1004 ops->pf = family;
1005 ops->owner = afi->owner;
1006 ops->hooknum = hooknum;
1007 ops->priority = priority;
1008 ops->priv = chain;
1009 ops->hook = afi->hooks[ops->hooknum];
1010 if (hookfn)
1011 ops->hook = hookfn;
1012 if (afi->hook_ops_init)
1013 afi->hook_ops_init(ops, i);
1014 }
Patrick McHardy96518512013-10-14 11:00:02 +02001015
1016 chain->flags |= NFT_BASE_CHAIN;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001017 basechain->policy = policy;
Patrick McHardy96518512013-10-14 11:00:02 +02001018 } else {
1019 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1020 if (chain == NULL)
1021 return -ENOMEM;
1022 }
1023
1024 INIT_LIST_HEAD(&chain->rules);
1025 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001026 chain->net = net;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +02001027 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +02001028 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1029
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001030 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1031 chain->flags & NFT_BASE_CHAIN) {
Patrick McHardy115a60b2014-01-03 12:16:15 +00001032 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001033 if (err < 0) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001034 module_put(basechain->type->owner);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001035 free_percpu(basechain->stats);
1036 kfree(basechain);
1037 return err;
1038 }
1039 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001040 list_add_tail(&chain->list, &table->chains);
1041 table->use++;
Patrick McHardy96518512013-10-14 11:00:02 +02001042notify:
1043 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
1044 family);
1045 return 0;
1046}
1047
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001048static void nf_tables_chain_destroy(struct nft_chain *chain)
Patrick McHardy96518512013-10-14 11:00:02 +02001049{
Patrick McHardy96518512013-10-14 11:00:02 +02001050 BUG_ON(chain->use > 0);
1051
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001052 if (chain->flags & NFT_BASE_CHAIN) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001053 module_put(nft_base_chain(chain)->type->owner);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001054 free_percpu(nft_base_chain(chain)->stats);
Patrick McHardy96518512013-10-14 11:00:02 +02001055 kfree(nft_base_chain(chain));
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001056 } else
Patrick McHardy96518512013-10-14 11:00:02 +02001057 kfree(chain);
1058}
1059
1060static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1061 const struct nlmsghdr *nlh,
1062 const struct nlattr * const nla[])
1063{
1064 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001065 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001066 struct nft_table *table;
1067 struct nft_chain *chain;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001068 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001069 int family = nfmsg->nfgen_family;
1070
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001071 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001072 if (IS_ERR(afi))
1073 return PTR_ERR(afi);
1074
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001075 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001076 if (IS_ERR(table))
1077 return PTR_ERR(table);
1078
1079 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1080 if (IS_ERR(chain))
1081 return PTR_ERR(chain);
1082
Patrick McHardy3dd72792014-01-25 08:04:07 +00001083 if (!list_empty(&chain->rules) || chain->use > 0)
Patrick McHardy96518512013-10-14 11:00:02 +02001084 return -EBUSY;
1085
1086 list_del(&chain->list);
1087 table->use--;
1088
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001089 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1090 chain->flags & NFT_BASE_CHAIN)
Patrick McHardy115a60b2014-01-03 12:16:15 +00001091 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
Patrick McHardy96518512013-10-14 11:00:02 +02001092
1093 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1094 family);
1095
1096 /* Make sure all rule references are gone before this is released */
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001097 synchronize_rcu();
1098
1099 nf_tables_chain_destroy(chain);
Patrick McHardy96518512013-10-14 11:00:02 +02001100 return 0;
1101}
1102
Patrick McHardy96518512013-10-14 11:00:02 +02001103/*
1104 * Expressions
1105 */
1106
1107/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001108 * nft_register_expr - register nf_tables expr type
1109 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001110 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001111 * Registers the expr type for use with nf_tables. Returns zero on
Patrick McHardy96518512013-10-14 11:00:02 +02001112 * success or a negative errno code otherwise.
1113 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001114int nft_register_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001115{
1116 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001117 if (type->family == NFPROTO_UNSPEC)
1118 list_add_tail(&type->list, &nf_tables_expressions);
1119 else
1120 list_add(&type->list, &nf_tables_expressions);
Patrick McHardy96518512013-10-14 11:00:02 +02001121 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1122 return 0;
1123}
1124EXPORT_SYMBOL_GPL(nft_register_expr);
1125
1126/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001127 * nft_unregister_expr - unregister nf_tables expr type
1128 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001129 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001130 * Unregisters the expr typefor use with nf_tables.
Patrick McHardy96518512013-10-14 11:00:02 +02001131 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001132void nft_unregister_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001133{
1134 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001135 list_del(&type->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001136 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1137}
1138EXPORT_SYMBOL_GPL(nft_unregister_expr);
1139
Patrick McHardy64d46802014-02-05 15:03:37 +00001140static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1141 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001142{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001143 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001144
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001145 list_for_each_entry(type, &nf_tables_expressions, list) {
Patrick McHardy64d46802014-02-05 15:03:37 +00001146 if (!nla_strcmp(nla, type->name) &&
1147 (!type->family || type->family == family))
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001148 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001149 }
1150 return NULL;
1151}
1152
Patrick McHardy64d46802014-02-05 15:03:37 +00001153static const struct nft_expr_type *nft_expr_type_get(u8 family,
1154 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001155{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001156 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001157
1158 if (nla == NULL)
1159 return ERR_PTR(-EINVAL);
1160
Patrick McHardy64d46802014-02-05 15:03:37 +00001161 type = __nft_expr_type_get(family, nla);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001162 if (type != NULL && try_module_get(type->owner))
1163 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001164
1165#ifdef CONFIG_MODULES
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001166 if (type == NULL) {
Patrick McHardy96518512013-10-14 11:00:02 +02001167 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001168 request_module("nft-expr-%u-%.*s", family,
1169 nla_len(nla), (char *)nla_data(nla));
1170 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1171 if (__nft_expr_type_get(family, nla))
1172 return ERR_PTR(-EAGAIN);
1173
1174 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy96518512013-10-14 11:00:02 +02001175 request_module("nft-expr-%.*s",
1176 nla_len(nla), (char *)nla_data(nla));
1177 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001178 if (__nft_expr_type_get(family, nla))
Patrick McHardy96518512013-10-14 11:00:02 +02001179 return ERR_PTR(-EAGAIN);
1180 }
1181#endif
1182 return ERR_PTR(-ENOENT);
1183}
1184
1185static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1186 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1187 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1188};
1189
1190static int nf_tables_fill_expr_info(struct sk_buff *skb,
1191 const struct nft_expr *expr)
1192{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001193 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
Patrick McHardy96518512013-10-14 11:00:02 +02001194 goto nla_put_failure;
1195
1196 if (expr->ops->dump) {
1197 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1198 if (data == NULL)
1199 goto nla_put_failure;
1200 if (expr->ops->dump(skb, expr) < 0)
1201 goto nla_put_failure;
1202 nla_nest_end(skb, data);
1203 }
1204
1205 return skb->len;
1206
1207nla_put_failure:
1208 return -1;
1209};
1210
1211struct nft_expr_info {
1212 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001213 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001214};
1215
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001216static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1217 const struct nlattr *nla,
Patrick McHardy96518512013-10-14 11:00:02 +02001218 struct nft_expr_info *info)
1219{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001220 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001221 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001222 struct nlattr *tb[NFTA_EXPR_MAX + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001223 int err;
1224
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001225 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
Patrick McHardy96518512013-10-14 11:00:02 +02001226 if (err < 0)
1227 return err;
1228
Patrick McHardy64d46802014-02-05 15:03:37 +00001229 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001230 if (IS_ERR(type))
1231 return PTR_ERR(type);
1232
1233 if (tb[NFTA_EXPR_DATA]) {
1234 err = nla_parse_nested(info->tb, type->maxattr,
1235 tb[NFTA_EXPR_DATA], type->policy);
1236 if (err < 0)
1237 goto err1;
1238 } else
1239 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1240
1241 if (type->select_ops != NULL) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001242 ops = type->select_ops(ctx,
1243 (const struct nlattr * const *)info->tb);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001244 if (IS_ERR(ops)) {
1245 err = PTR_ERR(ops);
1246 goto err1;
1247 }
1248 } else
1249 ops = type->ops;
1250
Patrick McHardy96518512013-10-14 11:00:02 +02001251 info->ops = ops;
1252 return 0;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001253
1254err1:
1255 module_put(type->owner);
1256 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001257}
1258
1259static int nf_tables_newexpr(const struct nft_ctx *ctx,
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001260 const struct nft_expr_info *info,
Patrick McHardy96518512013-10-14 11:00:02 +02001261 struct nft_expr *expr)
1262{
1263 const struct nft_expr_ops *ops = info->ops;
1264 int err;
1265
1266 expr->ops = ops;
1267 if (ops->init) {
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001268 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
Patrick McHardy96518512013-10-14 11:00:02 +02001269 if (err < 0)
1270 goto err1;
1271 }
1272
Patrick McHardy96518512013-10-14 11:00:02 +02001273 return 0;
1274
1275err1:
1276 expr->ops = NULL;
1277 return err;
1278}
1279
Patrick McHardy62472bc2014-03-07 19:08:30 +01001280static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1281 struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +02001282{
1283 if (expr->ops->destroy)
Patrick McHardy62472bc2014-03-07 19:08:30 +01001284 expr->ops->destroy(ctx, expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001285 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001286}
1287
1288/*
1289 * Rules
1290 */
1291
1292static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1293 u64 handle)
1294{
1295 struct nft_rule *rule;
1296
1297 // FIXME: this sucks
1298 list_for_each_entry(rule, &chain->rules, list) {
1299 if (handle == rule->handle)
1300 return rule;
1301 }
1302
1303 return ERR_PTR(-ENOENT);
1304}
1305
1306static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1307 const struct nlattr *nla)
1308{
1309 if (nla == NULL)
1310 return ERR_PTR(-EINVAL);
1311
1312 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1313}
1314
1315static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1316 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1317 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1318 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1319 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1320 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001321 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001322 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001323 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1324 .len = NFT_USERDATA_MAXLEN },
Patrick McHardy96518512013-10-14 11:00:02 +02001325};
1326
1327static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1328 int event, u32 flags, int family,
1329 const struct nft_table *table,
1330 const struct nft_chain *chain,
1331 const struct nft_rule *rule)
1332{
1333 struct nlmsghdr *nlh;
1334 struct nfgenmsg *nfmsg;
1335 const struct nft_expr *expr, *next;
1336 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001337 const struct nft_rule *prule;
1338 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001339
Eric Leblond5e948462013-10-10 13:41:44 +02001340 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001341 flags);
1342 if (nlh == NULL)
1343 goto nla_put_failure;
1344
1345 nfmsg = nlmsg_data(nlh);
1346 nfmsg->nfgen_family = family;
1347 nfmsg->version = NFNETLINK_V0;
1348 nfmsg->res_id = 0;
1349
1350 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1351 goto nla_put_failure;
1352 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1353 goto nla_put_failure;
1354 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1355 goto nla_put_failure;
1356
Eric Leblond5e948462013-10-10 13:41:44 +02001357 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1358 prule = list_entry(rule->list.prev, struct nft_rule, list);
1359 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1360 cpu_to_be64(prule->handle)))
1361 goto nla_put_failure;
1362 }
1363
Patrick McHardy96518512013-10-14 11:00:02 +02001364 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1365 if (list == NULL)
1366 goto nla_put_failure;
1367 nft_rule_for_each_expr(expr, next, rule) {
1368 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1369 if (elem == NULL)
1370 goto nla_put_failure;
1371 if (nf_tables_fill_expr_info(skb, expr) < 0)
1372 goto nla_put_failure;
1373 nla_nest_end(skb, elem);
1374 }
1375 nla_nest_end(skb, list);
1376
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001377 if (rule->ulen &&
1378 nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1379 goto nla_put_failure;
1380
Patrick McHardy96518512013-10-14 11:00:02 +02001381 return nlmsg_end(skb, nlh);
1382
1383nla_put_failure:
1384 nlmsg_trim(skb, nlh);
1385 return -1;
1386}
1387
1388static int nf_tables_rule_notify(const struct sk_buff *oskb,
1389 const struct nlmsghdr *nlh,
1390 const struct nft_table *table,
1391 const struct nft_chain *chain,
1392 const struct nft_rule *rule,
1393 int event, u32 flags, int family)
1394{
1395 struct sk_buff *skb;
1396 u32 portid = NETLINK_CB(oskb).portid;
1397 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1398 u32 seq = nlh->nlmsg_seq;
1399 bool report;
1400 int err;
1401
1402 report = nlmsg_report(nlh);
1403 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1404 return 0;
1405
1406 err = -ENOBUFS;
1407 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1408 if (skb == NULL)
1409 goto err;
1410
1411 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1412 family, table, chain, rule);
1413 if (err < 0) {
1414 kfree_skb(skb);
1415 goto err;
1416 }
1417
1418 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1419 GFP_KERNEL);
1420err:
1421 if (err < 0)
1422 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1423 return err;
1424}
1425
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001426static inline bool
1427nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1428{
1429 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1430}
1431
1432static inline int gencursor_next(struct net *net)
1433{
1434 return net->nft.gencursor+1 == 1 ? 1 : 0;
1435}
1436
1437static inline int
1438nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1439{
1440 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1441}
1442
1443static inline void
1444nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1445{
1446 /* Now inactive, will be active in the future */
1447 rule->genmask = (1 << net->nft.gencursor);
1448}
1449
1450static inline void
1451nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1452{
1453 rule->genmask = (1 << gencursor_next(net));
1454}
1455
1456static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1457{
1458 rule->genmask = 0;
1459}
1460
Patrick McHardy96518512013-10-14 11:00:02 +02001461static int nf_tables_dump_rules(struct sk_buff *skb,
1462 struct netlink_callback *cb)
1463{
1464 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1465 const struct nft_af_info *afi;
1466 const struct nft_table *table;
1467 const struct nft_chain *chain;
1468 const struct nft_rule *rule;
1469 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001470 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001471 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001472 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1473 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
Patrick McHardy96518512013-10-14 11:00:02 +02001474
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001475 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001476 if (family != NFPROTO_UNSPEC && family != afi->family)
1477 continue;
1478
1479 list_for_each_entry(table, &afi->tables, list) {
1480 list_for_each_entry(chain, &table->chains, list) {
1481 list_for_each_entry(rule, &chain->rules, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001482 if (!nft_rule_is_active(net, rule))
1483 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001484 if (idx < s_idx)
1485 goto cont;
1486 if (idx > s_idx)
1487 memset(&cb->args[1], 0,
1488 sizeof(cb->args) - sizeof(cb->args[0]));
1489 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1490 cb->nlh->nlmsg_seq,
1491 NFT_MSG_NEWRULE,
1492 NLM_F_MULTI | NLM_F_APPEND,
1493 afi->family, table, chain, rule) < 0)
1494 goto done;
1495cont:
1496 idx++;
1497 }
1498 }
1499 }
1500 }
1501done:
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001502 /* Invalidate this dump, a transition to the new generation happened */
1503 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1504 return -EBUSY;
1505
Patrick McHardy96518512013-10-14 11:00:02 +02001506 cb->args[0] = idx;
1507 return skb->len;
1508}
1509
1510static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1511 const struct nlmsghdr *nlh,
1512 const struct nlattr * const nla[])
1513{
1514 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1515 const struct nft_af_info *afi;
1516 const struct nft_table *table;
1517 const struct nft_chain *chain;
1518 const struct nft_rule *rule;
1519 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001520 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001521 int family = nfmsg->nfgen_family;
1522 int err;
1523
1524 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1525 struct netlink_dump_control c = {
1526 .dump = nf_tables_dump_rules,
1527 };
1528 return netlink_dump_start(nlsk, skb, nlh, &c);
1529 }
1530
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001531 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001532 if (IS_ERR(afi))
1533 return PTR_ERR(afi);
1534
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001535 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001536 if (IS_ERR(table))
1537 return PTR_ERR(table);
1538
1539 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1540 if (IS_ERR(chain))
1541 return PTR_ERR(chain);
1542
1543 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1544 if (IS_ERR(rule))
1545 return PTR_ERR(rule);
1546
1547 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1548 if (!skb2)
1549 return -ENOMEM;
1550
1551 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1552 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1553 family, table, chain, rule);
1554 if (err < 0)
1555 goto err;
1556
1557 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1558
1559err:
1560 kfree_skb(skb2);
1561 return err;
1562}
1563
Patrick McHardy62472bc2014-03-07 19:08:30 +01001564static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1565 struct nft_rule *rule)
Patrick McHardy96518512013-10-14 11:00:02 +02001566{
Patrick McHardy96518512013-10-14 11:00:02 +02001567 struct nft_expr *expr;
1568
1569 /*
1570 * Careful: some expressions might not be initialized in case this
1571 * is called on error from nf_tables_newrule().
1572 */
1573 expr = nft_expr_first(rule);
1574 while (expr->ops && expr != nft_expr_last(rule)) {
Patrick McHardy62472bc2014-03-07 19:08:30 +01001575 nf_tables_expr_destroy(ctx, expr);
Patrick McHardy96518512013-10-14 11:00:02 +02001576 expr = nft_expr_next(expr);
1577 }
1578 kfree(rule);
1579}
1580
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001581static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001582 struct nft_rule *rule)
1583{
1584 struct nft_trans *trans;
1585
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001586 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001587 if (trans == NULL)
1588 return NULL;
1589
1590 nft_trans_rule(trans) = rule;
1591 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
1592
1593 return trans;
1594}
1595
Patrick McHardy96518512013-10-14 11:00:02 +02001596#define NFT_RULE_MAXEXPRS 128
1597
1598static struct nft_expr_info *info;
1599
1600static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1601 const struct nlmsghdr *nlh,
1602 const struct nlattr * const nla[])
1603{
1604 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001605 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001606 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001607 struct nft_table *table;
1608 struct nft_chain *chain;
1609 struct nft_rule *rule, *old_rule = NULL;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001610 struct nft_trans *trans = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001611 struct nft_expr *expr;
1612 struct nft_ctx ctx;
1613 struct nlattr *tmp;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001614 unsigned int size, i, n, ulen = 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001615 int err, rem;
1616 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02001617 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02001618
1619 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1620
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001621 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02001622 if (IS_ERR(afi))
1623 return PTR_ERR(afi);
1624
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001625 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001626 if (IS_ERR(table))
1627 return PTR_ERR(table);
1628
1629 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1630 if (IS_ERR(chain))
1631 return PTR_ERR(chain);
1632
1633 if (nla[NFTA_RULE_HANDLE]) {
1634 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1635 rule = __nf_tables_rule_lookup(chain, handle);
1636 if (IS_ERR(rule))
1637 return PTR_ERR(rule);
1638
1639 if (nlh->nlmsg_flags & NLM_F_EXCL)
1640 return -EEXIST;
1641 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1642 old_rule = rule;
1643 else
1644 return -EOPNOTSUPP;
1645 } else {
1646 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1647 return -EINVAL;
1648 handle = nf_tables_alloc_handle(table);
1649 }
1650
Eric Leblond5e948462013-10-10 13:41:44 +02001651 if (nla[NFTA_RULE_POSITION]) {
1652 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1653 return -EOPNOTSUPP;
1654
1655 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1656 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1657 if (IS_ERR(old_rule))
1658 return PTR_ERR(old_rule);
1659 }
1660
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001661 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1662
Patrick McHardy96518512013-10-14 11:00:02 +02001663 n = 0;
1664 size = 0;
1665 if (nla[NFTA_RULE_EXPRESSIONS]) {
1666 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1667 err = -EINVAL;
1668 if (nla_type(tmp) != NFTA_LIST_ELEM)
1669 goto err1;
1670 if (n == NFT_RULE_MAXEXPRS)
1671 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001672 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02001673 if (err < 0)
1674 goto err1;
1675 size += info[n].ops->size;
1676 n++;
1677 }
1678 }
1679
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001680 if (nla[NFTA_RULE_USERDATA])
1681 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1682
Patrick McHardy96518512013-10-14 11:00:02 +02001683 err = -ENOMEM;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001684 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001685 if (rule == NULL)
1686 goto err1;
1687
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001688 nft_rule_activate_next(net, rule);
1689
Patrick McHardy96518512013-10-14 11:00:02 +02001690 rule->handle = handle;
1691 rule->dlen = size;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001692 rule->ulen = ulen;
1693
1694 if (ulen)
1695 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
Patrick McHardy96518512013-10-14 11:00:02 +02001696
Patrick McHardy96518512013-10-14 11:00:02 +02001697 expr = nft_expr_first(rule);
1698 for (i = 0; i < n; i++) {
1699 err = nf_tables_newexpr(&ctx, &info[i], expr);
1700 if (err < 0)
1701 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001702 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001703 expr = nft_expr_next(expr);
1704 }
1705
Patrick McHardy96518512013-10-14 11:00:02 +02001706 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001707 if (nft_rule_is_active_next(net, old_rule)) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001708 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE,
1709 old_rule);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001710 if (trans == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001711 err = -ENOMEM;
1712 goto err2;
1713 }
1714 nft_rule_disactivate_next(net, old_rule);
1715 list_add_tail(&rule->list, &old_rule->list);
1716 } else {
1717 err = -ENOENT;
1718 goto err2;
1719 }
Patrick McHardy96518512013-10-14 11:00:02 +02001720 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02001721 if (old_rule)
1722 list_add_rcu(&rule->list, &old_rule->list);
1723 else
1724 list_add_tail_rcu(&rule->list, &chain->rules);
1725 else {
1726 if (old_rule)
1727 list_add_tail_rcu(&rule->list, &old_rule->list);
1728 else
1729 list_add_rcu(&rule->list, &chain->rules);
1730 }
Patrick McHardy96518512013-10-14 11:00:02 +02001731
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001732 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001733 err = -ENOMEM;
1734 goto err3;
1735 }
Patrick McHardy96518512013-10-14 11:00:02 +02001736 return 0;
1737
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001738err3:
1739 list_del_rcu(&rule->list);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001740 if (trans) {
1741 list_del_rcu(&nft_trans_rule(trans)->list);
1742 nft_rule_clear(net, nft_trans_rule(trans));
1743 nft_trans_destroy(trans);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001744 }
Patrick McHardy96518512013-10-14 11:00:02 +02001745err2:
Patrick McHardy62472bc2014-03-07 19:08:30 +01001746 nf_tables_rule_destroy(&ctx, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001747err1:
1748 for (i = 0; i < n; i++) {
1749 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001750 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001751 }
1752 return err;
1753}
1754
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001755static int
1756nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1757{
1758 /* You cannot delete the same rule twice */
1759 if (nft_rule_is_active_next(ctx->net, rule)) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001760 if (nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule) == NULL)
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001761 return -ENOMEM;
1762 nft_rule_disactivate_next(ctx->net, rule);
1763 return 0;
1764 }
1765 return -ENOENT;
1766}
1767
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001768static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1769{
1770 struct nft_rule *rule;
1771 int err;
1772
1773 list_for_each_entry(rule, &ctx->chain->rules, list) {
1774 err = nf_tables_delrule_one(ctx, rule);
1775 if (err < 0)
1776 return err;
1777 }
1778 return 0;
1779}
1780
Patrick McHardy96518512013-10-14 11:00:02 +02001781static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1782 const struct nlmsghdr *nlh,
1783 const struct nlattr * const nla[])
1784{
1785 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001786 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001787 struct net *net = sock_net(skb->sk);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001788 struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001789 struct nft_chain *chain = NULL;
1790 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001791 int family = nfmsg->nfgen_family, err = 0;
1792 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001793
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001794 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001795 if (IS_ERR(afi))
1796 return PTR_ERR(afi);
1797
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001798 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001799 if (IS_ERR(table))
1800 return PTR_ERR(table);
1801
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001802 if (nla[NFTA_RULE_CHAIN]) {
1803 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1804 if (IS_ERR(chain))
1805 return PTR_ERR(chain);
1806 }
Patrick McHardy96518512013-10-14 11:00:02 +02001807
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001808 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1809
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001810 if (chain) {
1811 if (nla[NFTA_RULE_HANDLE]) {
1812 rule = nf_tables_rule_lookup(chain,
1813 nla[NFTA_RULE_HANDLE]);
1814 if (IS_ERR(rule))
1815 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001816
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001817 err = nf_tables_delrule_one(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001818 } else {
1819 err = nf_table_delrule_by_chain(&ctx);
1820 }
1821 } else {
1822 list_for_each_entry(chain, &table->chains, list) {
1823 ctx.chain = chain;
1824 err = nf_table_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001825 if (err < 0)
1826 break;
Patrick McHardy96518512013-10-14 11:00:02 +02001827 }
1828 }
1829
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001830 return err;
1831}
1832
Patrick McHardy20a69342013-10-11 12:06:22 +02001833/*
1834 * Sets
1835 */
1836
1837static LIST_HEAD(nf_tables_set_ops);
1838
1839int nft_register_set(struct nft_set_ops *ops)
1840{
1841 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1842 list_add_tail(&ops->list, &nf_tables_set_ops);
1843 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1844 return 0;
1845}
1846EXPORT_SYMBOL_GPL(nft_register_set);
1847
1848void nft_unregister_set(struct nft_set_ops *ops)
1849{
1850 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1851 list_del(&ops->list);
1852 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1853}
1854EXPORT_SYMBOL_GPL(nft_unregister_set);
1855
Patrick McHardyc50b9602014-03-28 10:19:47 +00001856/*
1857 * Select a set implementation based on the data characteristics and the
1858 * given policy. The total memory use might not be known if no size is
1859 * given, in that case the amount of memory per element is used.
1860 */
1861static const struct nft_set_ops *
1862nft_select_set_ops(const struct nlattr * const nla[],
1863 const struct nft_set_desc *desc,
1864 enum nft_set_policies policy)
Patrick McHardy20a69342013-10-11 12:06:22 +02001865{
Patrick McHardyc50b9602014-03-28 10:19:47 +00001866 const struct nft_set_ops *ops, *bops;
1867 struct nft_set_estimate est, best;
Patrick McHardy20a69342013-10-11 12:06:22 +02001868 u32 features;
1869
1870#ifdef CONFIG_MODULES
1871 if (list_empty(&nf_tables_set_ops)) {
1872 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1873 request_module("nft-set");
1874 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1875 if (!list_empty(&nf_tables_set_ops))
1876 return ERR_PTR(-EAGAIN);
1877 }
1878#endif
1879 features = 0;
1880 if (nla[NFTA_SET_FLAGS] != NULL) {
1881 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1882 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1883 }
1884
Patrick McHardyc50b9602014-03-28 10:19:47 +00001885 bops = NULL;
1886 best.size = ~0;
1887 best.class = ~0;
1888
Patrick McHardy20a69342013-10-11 12:06:22 +02001889 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1890 if ((ops->features & features) != features)
1891 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00001892 if (!ops->estimate(desc, features, &est))
1893 continue;
1894
1895 switch (policy) {
1896 case NFT_SET_POL_PERFORMANCE:
1897 if (est.class < best.class)
1898 break;
1899 if (est.class == best.class && est.size < best.size)
1900 break;
1901 continue;
1902 case NFT_SET_POL_MEMORY:
1903 if (est.size < best.size)
1904 break;
1905 if (est.size == best.size && est.class < best.class)
1906 break;
1907 continue;
1908 default:
1909 break;
1910 }
1911
Patrick McHardy20a69342013-10-11 12:06:22 +02001912 if (!try_module_get(ops->owner))
1913 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00001914 if (bops != NULL)
1915 module_put(bops->owner);
1916
1917 bops = ops;
1918 best = est;
Patrick McHardy20a69342013-10-11 12:06:22 +02001919 }
1920
Patrick McHardyc50b9602014-03-28 10:19:47 +00001921 if (bops != NULL)
1922 return bops;
1923
Patrick McHardy20a69342013-10-11 12:06:22 +02001924 return ERR_PTR(-EOPNOTSUPP);
1925}
1926
1927static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1928 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1929 [NFTA_SET_NAME] = { .type = NLA_STRING },
1930 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1931 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1932 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1933 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1934 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00001935 [NFTA_SET_POLICY] = { .type = NLA_U32 },
1936 [NFTA_SET_DESC] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02001937 [NFTA_SET_ID] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00001938};
1939
1940static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
1941 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02001942};
1943
1944static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1945 const struct sk_buff *skb,
1946 const struct nlmsghdr *nlh,
1947 const struct nlattr * const nla[])
1948{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001949 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02001950 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001951 struct nft_af_info *afi = NULL;
1952 struct nft_table *table = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02001953
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01001954 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
1955 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1956 if (IS_ERR(afi))
1957 return PTR_ERR(afi);
1958 }
Patrick McHardy20a69342013-10-11 12:06:22 +02001959
1960 if (nla[NFTA_SET_TABLE] != NULL) {
Patrick McHardyec2c9932014-02-05 15:03:35 +00001961 if (afi == NULL)
1962 return -EAFNOSUPPORT;
1963
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001964 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02001965 if (IS_ERR(table))
1966 return PTR_ERR(table);
1967 }
1968
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001969 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02001970 return 0;
1971}
1972
1973struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1974 const struct nlattr *nla)
1975{
1976 struct nft_set *set;
1977
1978 if (nla == NULL)
1979 return ERR_PTR(-EINVAL);
1980
1981 list_for_each_entry(set, &table->sets, list) {
1982 if (!nla_strcmp(nla, set->name))
1983 return set;
1984 }
1985 return ERR_PTR(-ENOENT);
1986}
1987
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02001988struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
1989 const struct nlattr *nla)
1990{
1991 struct nft_trans *trans;
1992 u32 id = ntohl(nla_get_be32(nla));
1993
1994 list_for_each_entry(trans, &net->nft.commit_list, list) {
1995 if (trans->msg_type == NFT_MSG_NEWSET &&
1996 id == nft_trans_set_id(trans))
1997 return nft_trans_set(trans);
1998 }
1999 return ERR_PTR(-ENOENT);
2000}
2001
Patrick McHardy20a69342013-10-11 12:06:22 +02002002static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2003 const char *name)
2004{
2005 const struct nft_set *i;
2006 const char *p;
2007 unsigned long *inuse;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002008 unsigned int n = 0, min = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002009
2010 p = strnchr(name, IFNAMSIZ, '%');
2011 if (p != NULL) {
2012 if (p[1] != 'd' || strchr(p + 2, '%'))
2013 return -EINVAL;
2014
2015 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2016 if (inuse == NULL)
2017 return -ENOMEM;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002018cont:
Patrick McHardy20a69342013-10-11 12:06:22 +02002019 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01002020 int tmp;
2021
2022 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02002023 continue;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002024 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02002025 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002026
Patrick McHardy60eb1892014-03-07 12:34:05 +01002027 set_bit(tmp - min, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02002028 }
2029
Patrick McHardy53b70282014-02-05 12:26:22 +01002030 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
Patrick McHardy60eb1892014-03-07 12:34:05 +01002031 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2032 min += BITS_PER_BYTE * PAGE_SIZE;
2033 memset(inuse, 0, PAGE_SIZE);
2034 goto cont;
2035 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002036 free_page((unsigned long)inuse);
2037 }
2038
Patrick McHardy60eb1892014-03-07 12:34:05 +01002039 snprintf(set->name, sizeof(set->name), name, min + n);
Patrick McHardy20a69342013-10-11 12:06:22 +02002040 list_for_each_entry(i, &ctx->table->sets, list) {
2041 if (!strcmp(set->name, i->name))
2042 return -ENFILE;
2043 }
2044 return 0;
2045}
2046
2047static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2048 const struct nft_set *set, u16 event, u16 flags)
2049{
2050 struct nfgenmsg *nfmsg;
2051 struct nlmsghdr *nlh;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002052 struct nlattr *desc;
Patrick McHardy20a69342013-10-11 12:06:22 +02002053 u32 portid = NETLINK_CB(ctx->skb).portid;
2054 u32 seq = ctx->nlh->nlmsg_seq;
2055
2056 event |= NFNL_SUBSYS_NFTABLES << 8;
2057 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2058 flags);
2059 if (nlh == NULL)
2060 goto nla_put_failure;
2061
2062 nfmsg = nlmsg_data(nlh);
2063 nfmsg->nfgen_family = ctx->afi->family;
2064 nfmsg->version = NFNETLINK_V0;
2065 nfmsg->res_id = 0;
2066
2067 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2068 goto nla_put_failure;
2069 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2070 goto nla_put_failure;
2071 if (set->flags != 0)
2072 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2073 goto nla_put_failure;
2074
2075 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2076 goto nla_put_failure;
2077 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2078 goto nla_put_failure;
2079 if (set->flags & NFT_SET_MAP) {
2080 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2081 goto nla_put_failure;
2082 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2083 goto nla_put_failure;
2084 }
2085
Patrick McHardyc50b9602014-03-28 10:19:47 +00002086 desc = nla_nest_start(skb, NFTA_SET_DESC);
2087 if (desc == NULL)
2088 goto nla_put_failure;
2089 if (set->size &&
2090 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2091 goto nla_put_failure;
2092 nla_nest_end(skb, desc);
2093
Patrick McHardy20a69342013-10-11 12:06:22 +02002094 return nlmsg_end(skb, nlh);
2095
2096nla_put_failure:
2097 nlmsg_trim(skb, nlh);
2098 return -1;
2099}
2100
2101static int nf_tables_set_notify(const struct nft_ctx *ctx,
2102 const struct nft_set *set,
2103 int event)
2104{
2105 struct sk_buff *skb;
2106 u32 portid = NETLINK_CB(ctx->skb).portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002107 bool report;
2108 int err;
2109
2110 report = nlmsg_report(ctx->nlh);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002111 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002112 return 0;
2113
2114 err = -ENOBUFS;
2115 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2116 if (skb == NULL)
2117 goto err;
2118
2119 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2120 if (err < 0) {
2121 kfree_skb(skb);
2122 goto err;
2123 }
2124
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002125 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
Patrick McHardy20a69342013-10-11 12:06:22 +02002126 GFP_KERNEL);
2127err:
2128 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002129 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002130 return err;
2131}
2132
2133static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2134 struct netlink_callback *cb)
2135{
2136 const struct nft_set *set;
2137 unsigned int idx = 0, s_idx = cb->args[0];
2138
2139 if (cb->args[1])
2140 return skb->len;
2141
2142 list_for_each_entry(set, &ctx->table->sets, list) {
2143 if (idx < s_idx)
2144 goto cont;
2145 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2146 NLM_F_MULTI) < 0) {
2147 cb->args[0] = idx;
2148 goto done;
2149 }
2150cont:
2151 idx++;
2152 }
2153 cb->args[1] = 1;
2154done:
2155 return skb->len;
2156}
2157
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002158static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2159 struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002160{
2161 const struct nft_set *set;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002162 unsigned int idx, s_idx = cb->args[0];
Patrick McHardy20a69342013-10-11 12:06:22 +02002163 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2164
2165 if (cb->args[1])
2166 return skb->len;
2167
2168 list_for_each_entry(table, &ctx->afi->tables, list) {
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002169 if (cur_table) {
2170 if (cur_table != table)
2171 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02002172
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002173 cur_table = NULL;
2174 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002175 ctx->table = table;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002176 idx = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002177 list_for_each_entry(set, &ctx->table->sets, list) {
2178 if (idx < s_idx)
2179 goto cont;
2180 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2181 NLM_F_MULTI) < 0) {
2182 cb->args[0] = idx;
2183 cb->args[2] = (unsigned long) table;
2184 goto done;
2185 }
2186cont:
2187 idx++;
2188 }
2189 }
2190 cb->args[1] = 1;
2191done:
2192 return skb->len;
2193}
2194
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002195static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2196 struct netlink_callback *cb)
2197{
2198 const struct nft_set *set;
2199 unsigned int idx, s_idx = cb->args[0];
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002200 struct nft_af_info *afi;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002201 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2202 struct net *net = sock_net(skb->sk);
2203 int cur_family = cb->args[3];
2204
2205 if (cb->args[1])
2206 return skb->len;
2207
2208 list_for_each_entry(afi, &net->nft.af_info, list) {
2209 if (cur_family) {
2210 if (afi->family != cur_family)
2211 continue;
2212
2213 cur_family = 0;
2214 }
2215
2216 list_for_each_entry(table, &afi->tables, list) {
2217 if (cur_table) {
2218 if (cur_table != table)
2219 continue;
2220
2221 cur_table = NULL;
2222 }
2223
2224 ctx->table = table;
2225 ctx->afi = afi;
2226 idx = 0;
2227 list_for_each_entry(set, &ctx->table->sets, list) {
2228 if (idx < s_idx)
2229 goto cont;
2230 if (nf_tables_fill_set(skb, ctx, set,
2231 NFT_MSG_NEWSET,
2232 NLM_F_MULTI) < 0) {
2233 cb->args[0] = idx;
2234 cb->args[2] = (unsigned long) table;
2235 cb->args[3] = afi->family;
2236 goto done;
2237 }
2238cont:
2239 idx++;
2240 }
2241 if (s_idx)
2242 s_idx = 0;
2243 }
2244 }
2245 cb->args[1] = 1;
2246done:
2247 return skb->len;
2248}
2249
Patrick McHardy20a69342013-10-11 12:06:22 +02002250static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2251{
2252 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2253 struct nlattr *nla[NFTA_SET_MAX + 1];
2254 struct nft_ctx ctx;
2255 int err, ret;
2256
2257 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2258 nft_set_policy);
2259 if (err < 0)
2260 return err;
2261
2262 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2263 if (err < 0)
2264 return err;
2265
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002266 if (ctx.table == NULL) {
2267 if (ctx.afi == NULL)
2268 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2269 else
2270 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2271 } else
Patrick McHardy20a69342013-10-11 12:06:22 +02002272 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2273
2274 return ret;
2275}
2276
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002277#define NFT_SET_INACTIVE (1 << 15) /* Internal set flag */
2278
Patrick McHardy20a69342013-10-11 12:06:22 +02002279static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2280 const struct nlmsghdr *nlh,
2281 const struct nlattr * const nla[])
2282{
2283 const struct nft_set *set;
2284 struct nft_ctx ctx;
2285 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002286 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002287 int err;
2288
2289 /* Verify existance before starting dump */
2290 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2291 if (err < 0)
2292 return err;
2293
2294 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2295 struct netlink_dump_control c = {
2296 .dump = nf_tables_dump_sets,
2297 };
2298 return netlink_dump_start(nlsk, skb, nlh, &c);
2299 }
2300
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002301 /* Only accept unspec with dump */
2302 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2303 return -EAFNOSUPPORT;
2304
Patrick McHardy20a69342013-10-11 12:06:22 +02002305 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2306 if (IS_ERR(set))
2307 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002308 if (set->flags & NFT_SET_INACTIVE)
2309 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002310
2311 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2312 if (skb2 == NULL)
2313 return -ENOMEM;
2314
2315 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2316 if (err < 0)
2317 goto err;
2318
2319 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2320
2321err:
2322 kfree_skb(skb2);
2323 return err;
2324}
2325
Patrick McHardyc50b9602014-03-28 10:19:47 +00002326static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2327 struct nft_set_desc *desc,
2328 const struct nlattr *nla)
2329{
2330 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2331 int err;
2332
2333 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2334 if (err < 0)
2335 return err;
2336
2337 if (da[NFTA_SET_DESC_SIZE] != NULL)
2338 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2339
2340 return 0;
2341}
2342
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002343static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
2344 struct nft_set *set)
2345{
2346 struct nft_trans *trans;
2347
2348 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
2349 if (trans == NULL)
2350 return -ENOMEM;
2351
2352 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
2353 nft_trans_set_id(trans) =
2354 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
2355 set->flags |= NFT_SET_INACTIVE;
2356 }
2357 nft_trans_set(trans) = set;
2358 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
2359
2360 return 0;
2361}
2362
Patrick McHardy20a69342013-10-11 12:06:22 +02002363static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2364 const struct nlmsghdr *nlh,
2365 const struct nlattr * const nla[])
2366{
2367 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2368 const struct nft_set_ops *ops;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002369 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002370 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002371 struct nft_table *table;
2372 struct nft_set *set;
2373 struct nft_ctx ctx;
2374 char name[IFNAMSIZ];
2375 unsigned int size;
2376 bool create;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002377 u32 ktype, dtype, flags, policy;
2378 struct nft_set_desc desc;
Patrick McHardy20a69342013-10-11 12:06:22 +02002379 int err;
2380
2381 if (nla[NFTA_SET_TABLE] == NULL ||
2382 nla[NFTA_SET_NAME] == NULL ||
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002383 nla[NFTA_SET_KEY_LEN] == NULL ||
2384 nla[NFTA_SET_ID] == NULL)
Patrick McHardy20a69342013-10-11 12:06:22 +02002385 return -EINVAL;
2386
Patrick McHardyc50b9602014-03-28 10:19:47 +00002387 memset(&desc, 0, sizeof(desc));
2388
Patrick McHardy20a69342013-10-11 12:06:22 +02002389 ktype = NFT_DATA_VALUE;
2390 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2391 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2392 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2393 return -EINVAL;
2394 }
2395
Patrick McHardyc50b9602014-03-28 10:19:47 +00002396 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2397 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002398 return -EINVAL;
2399
2400 flags = 0;
2401 if (nla[NFTA_SET_FLAGS] != NULL) {
2402 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2403 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2404 NFT_SET_INTERVAL | NFT_SET_MAP))
2405 return -EINVAL;
2406 }
2407
2408 dtype = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002409 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2410 if (!(flags & NFT_SET_MAP))
2411 return -EINVAL;
2412
2413 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2414 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2415 dtype != NFT_DATA_VERDICT)
2416 return -EINVAL;
2417
2418 if (dtype != NFT_DATA_VERDICT) {
2419 if (nla[NFTA_SET_DATA_LEN] == NULL)
2420 return -EINVAL;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002421 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2422 if (desc.dlen == 0 ||
2423 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002424 return -EINVAL;
2425 } else
Patrick McHardyc50b9602014-03-28 10:19:47 +00002426 desc.dlen = sizeof(struct nft_data);
Patrick McHardy20a69342013-10-11 12:06:22 +02002427 } else if (flags & NFT_SET_MAP)
2428 return -EINVAL;
2429
Patrick McHardyc50b9602014-03-28 10:19:47 +00002430 policy = NFT_SET_POL_PERFORMANCE;
2431 if (nla[NFTA_SET_POLICY] != NULL)
2432 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2433
2434 if (nla[NFTA_SET_DESC] != NULL) {
2435 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2436 if (err < 0)
2437 return err;
2438 }
2439
Patrick McHardy20a69342013-10-11 12:06:22 +02002440 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2441
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002442 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002443 if (IS_ERR(afi))
2444 return PTR_ERR(afi);
2445
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002446 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002447 if (IS_ERR(table))
2448 return PTR_ERR(table);
2449
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002450 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002451
2452 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2453 if (IS_ERR(set)) {
2454 if (PTR_ERR(set) != -ENOENT)
2455 return PTR_ERR(set);
2456 set = NULL;
2457 }
2458
2459 if (set != NULL) {
2460 if (nlh->nlmsg_flags & NLM_F_EXCL)
2461 return -EEXIST;
2462 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2463 return -EOPNOTSUPP;
2464 return 0;
2465 }
2466
2467 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2468 return -ENOENT;
2469
Patrick McHardyc50b9602014-03-28 10:19:47 +00002470 ops = nft_select_set_ops(nla, &desc, policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002471 if (IS_ERR(ops))
2472 return PTR_ERR(ops);
2473
2474 size = 0;
2475 if (ops->privsize != NULL)
2476 size = ops->privsize(nla);
2477
2478 err = -ENOMEM;
2479 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2480 if (set == NULL)
2481 goto err1;
2482
2483 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2484 err = nf_tables_set_alloc_name(&ctx, set, name);
2485 if (err < 0)
2486 goto err2;
2487
2488 INIT_LIST_HEAD(&set->bindings);
2489 set->ops = ops;
2490 set->ktype = ktype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002491 set->klen = desc.klen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002492 set->dtype = dtype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002493 set->dlen = desc.dlen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002494 set->flags = flags;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002495 set->size = desc.size;
Patrick McHardy20a69342013-10-11 12:06:22 +02002496
Patrick McHardyc50b9602014-03-28 10:19:47 +00002497 err = ops->init(set, &desc, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002498 if (err < 0)
2499 goto err2;
2500
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002501 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
2502 if (err < 0)
2503 goto err2;
2504
Patrick McHardy20a69342013-10-11 12:06:22 +02002505 list_add_tail(&set->list, &table->sets);
Patrick McHardy20a69342013-10-11 12:06:22 +02002506 return 0;
2507
2508err2:
2509 kfree(set);
2510err1:
2511 module_put(ops->owner);
2512 return err;
2513}
2514
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002515static void nft_set_destroy(struct nft_set *set)
2516{
2517 set->ops->destroy(set);
2518 module_put(set->ops->owner);
2519 kfree(set);
2520}
2521
Patrick McHardy20a69342013-10-11 12:06:22 +02002522static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2523{
2524 list_del(&set->list);
Patrick McHardyab9da5c12014-03-07 19:08:31 +01002525 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002526 nft_set_destroy(set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002527}
2528
2529static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2530 const struct nlmsghdr *nlh,
2531 const struct nlattr * const nla[])
2532{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002533 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002534 struct nft_set *set;
2535 struct nft_ctx ctx;
2536 int err;
2537
Patrick McHardyec2c9932014-02-05 15:03:35 +00002538 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2539 return -EAFNOSUPPORT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002540 if (nla[NFTA_SET_TABLE] == NULL)
2541 return -EINVAL;
2542
2543 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2544 if (err < 0)
2545 return err;
2546
2547 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2548 if (IS_ERR(set))
2549 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002550 if (set->flags & NFT_SET_INACTIVE)
2551 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002552 if (!list_empty(&set->bindings))
2553 return -EBUSY;
2554
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002555 err = nft_trans_set_add(&ctx, NFT_MSG_DELSET, set);
2556 if (err < 0)
2557 return err;
2558
2559 list_del(&set->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002560 return 0;
2561}
2562
2563static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2564 const struct nft_set *set,
2565 const struct nft_set_iter *iter,
2566 const struct nft_set_elem *elem)
2567{
2568 enum nft_registers dreg;
2569
2570 dreg = nft_type_to_reg(set->dtype);
Pablo Neira Ayuso2ee0d3c2013-12-28 00:59:38 +01002571 return nft_validate_data_load(ctx, dreg, &elem->data,
2572 set->dtype == NFT_DATA_VERDICT ?
2573 NFT_DATA_VERDICT : NFT_DATA_VALUE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002574}
2575
2576int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2577 struct nft_set_binding *binding)
2578{
2579 struct nft_set_binding *i;
2580 struct nft_set_iter iter;
2581
2582 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2583 return -EBUSY;
2584
2585 if (set->flags & NFT_SET_MAP) {
2586 /* If the set is already bound to the same chain all
2587 * jumps are already validated for that chain.
2588 */
2589 list_for_each_entry(i, &set->bindings, list) {
2590 if (i->chain == binding->chain)
2591 goto bind;
2592 }
2593
2594 iter.skip = 0;
2595 iter.count = 0;
2596 iter.err = 0;
2597 iter.fn = nf_tables_bind_check_setelem;
2598
2599 set->ops->walk(ctx, set, &iter);
2600 if (iter.err < 0) {
2601 /* Destroy anonymous sets if binding fails */
2602 if (set->flags & NFT_SET_ANONYMOUS)
2603 nf_tables_set_destroy(ctx, set);
2604
2605 return iter.err;
2606 }
2607 }
2608bind:
2609 binding->chain = ctx->chain;
2610 list_add_tail(&binding->list, &set->bindings);
2611 return 0;
2612}
2613
2614void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2615 struct nft_set_binding *binding)
2616{
2617 list_del(&binding->list);
2618
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002619 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2620 !(set->flags & NFT_SET_INACTIVE))
Patrick McHardy20a69342013-10-11 12:06:22 +02002621 nf_tables_set_destroy(ctx, set);
2622}
2623
2624/*
2625 * Set elements
2626 */
2627
2628static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2629 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2630 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2631 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2632};
2633
2634static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2635 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2636 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2637 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002638 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002639};
2640
2641static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2642 const struct sk_buff *skb,
2643 const struct nlmsghdr *nlh,
2644 const struct nlattr * const nla[])
2645{
2646 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002647 struct nft_af_info *afi;
2648 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002649 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002650
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002651 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002652 if (IS_ERR(afi))
2653 return PTR_ERR(afi);
2654
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002655 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002656 if (IS_ERR(table))
2657 return PTR_ERR(table);
2658
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002659 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002660 return 0;
2661}
2662
2663static int nf_tables_fill_setelem(struct sk_buff *skb,
2664 const struct nft_set *set,
2665 const struct nft_set_elem *elem)
2666{
2667 unsigned char *b = skb_tail_pointer(skb);
2668 struct nlattr *nest;
2669
2670 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2671 if (nest == NULL)
2672 goto nla_put_failure;
2673
2674 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2675 set->klen) < 0)
2676 goto nla_put_failure;
2677
2678 if (set->flags & NFT_SET_MAP &&
2679 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2680 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2681 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2682 set->dlen) < 0)
2683 goto nla_put_failure;
2684
2685 if (elem->flags != 0)
2686 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2687 goto nla_put_failure;
2688
2689 nla_nest_end(skb, nest);
2690 return 0;
2691
2692nla_put_failure:
2693 nlmsg_trim(skb, b);
2694 return -EMSGSIZE;
2695}
2696
2697struct nft_set_dump_args {
2698 const struct netlink_callback *cb;
2699 struct nft_set_iter iter;
2700 struct sk_buff *skb;
2701};
2702
2703static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2704 const struct nft_set *set,
2705 const struct nft_set_iter *iter,
2706 const struct nft_set_elem *elem)
2707{
2708 struct nft_set_dump_args *args;
2709
2710 args = container_of(iter, struct nft_set_dump_args, iter);
2711 return nf_tables_fill_setelem(args->skb, set, elem);
2712}
2713
2714static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2715{
2716 const struct nft_set *set;
2717 struct nft_set_dump_args args;
2718 struct nft_ctx ctx;
2719 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2720 struct nfgenmsg *nfmsg;
2721 struct nlmsghdr *nlh;
2722 struct nlattr *nest;
2723 u32 portid, seq;
2724 int event, err;
2725
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01002726 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2727 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002728 if (err < 0)
2729 return err;
2730
2731 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2732 if (err < 0)
2733 return err;
2734
2735 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2736 if (IS_ERR(set))
2737 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002738 if (set->flags & NFT_SET_INACTIVE)
2739 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002740
2741 event = NFT_MSG_NEWSETELEM;
2742 event |= NFNL_SUBSYS_NFTABLES << 8;
2743 portid = NETLINK_CB(cb->skb).portid;
2744 seq = cb->nlh->nlmsg_seq;
2745
2746 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2747 NLM_F_MULTI);
2748 if (nlh == NULL)
2749 goto nla_put_failure;
2750
2751 nfmsg = nlmsg_data(nlh);
2752 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2753 nfmsg->version = NFNETLINK_V0;
2754 nfmsg->res_id = 0;
2755
2756 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2757 goto nla_put_failure;
2758 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2759 goto nla_put_failure;
2760
2761 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2762 if (nest == NULL)
2763 goto nla_put_failure;
2764
2765 args.cb = cb;
2766 args.skb = skb;
2767 args.iter.skip = cb->args[0];
2768 args.iter.count = 0;
2769 args.iter.err = 0;
2770 args.iter.fn = nf_tables_dump_setelem;
2771 set->ops->walk(&ctx, set, &args.iter);
2772
2773 nla_nest_end(skb, nest);
2774 nlmsg_end(skb, nlh);
2775
2776 if (args.iter.err && args.iter.err != -EMSGSIZE)
2777 return args.iter.err;
2778 if (args.iter.count == cb->args[0])
2779 return 0;
2780
2781 cb->args[0] = args.iter.count;
2782 return skb->len;
2783
2784nla_put_failure:
2785 return -ENOSPC;
2786}
2787
2788static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2789 const struct nlmsghdr *nlh,
2790 const struct nlattr * const nla[])
2791{
2792 const struct nft_set *set;
2793 struct nft_ctx ctx;
2794 int err;
2795
2796 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2797 if (err < 0)
2798 return err;
2799
2800 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2801 if (IS_ERR(set))
2802 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002803 if (set->flags & NFT_SET_INACTIVE)
2804 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002805
2806 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2807 struct netlink_dump_control c = {
2808 .dump = nf_tables_dump_set,
2809 };
2810 return netlink_dump_start(nlsk, skb, nlh, &c);
2811 }
2812 return -EOPNOTSUPP;
2813}
2814
Arturo Borrerod60ce622014-04-01 14:06:07 +02002815static int nf_tables_fill_setelem_info(struct sk_buff *skb,
2816 const struct nft_ctx *ctx, u32 seq,
2817 u32 portid, int event, u16 flags,
2818 const struct nft_set *set,
2819 const struct nft_set_elem *elem)
2820{
2821 struct nfgenmsg *nfmsg;
2822 struct nlmsghdr *nlh;
2823 struct nlattr *nest;
2824 int err;
2825
2826 event |= NFNL_SUBSYS_NFTABLES << 8;
2827 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2828 flags);
2829 if (nlh == NULL)
2830 goto nla_put_failure;
2831
2832 nfmsg = nlmsg_data(nlh);
2833 nfmsg->nfgen_family = ctx->afi->family;
2834 nfmsg->version = NFNETLINK_V0;
2835 nfmsg->res_id = 0;
2836
2837 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2838 goto nla_put_failure;
2839 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2840 goto nla_put_failure;
2841
2842 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2843 if (nest == NULL)
2844 goto nla_put_failure;
2845
2846 err = nf_tables_fill_setelem(skb, set, elem);
2847 if (err < 0)
2848 goto nla_put_failure;
2849
2850 nla_nest_end(skb, nest);
2851
2852 return nlmsg_end(skb, nlh);
2853
2854nla_put_failure:
2855 nlmsg_trim(skb, nlh);
2856 return -1;
2857}
2858
2859static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
2860 const struct nft_set *set,
2861 const struct nft_set_elem *elem,
2862 int event, u16 flags)
2863{
2864 const struct sk_buff *oskb = ctx->skb;
2865 struct net *net = sock_net(oskb->sk);
2866 u32 portid = NETLINK_CB(oskb).portid;
2867 bool report = nlmsg_report(ctx->nlh);
2868 struct sk_buff *skb;
2869 int err;
2870
2871 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
2872 return 0;
2873
2874 err = -ENOBUFS;
2875 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2876 if (skb == NULL)
2877 goto err;
2878
2879 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
2880 set, elem);
2881 if (err < 0) {
2882 kfree_skb(skb);
2883 goto err;
2884 }
2885
2886 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
2887 GFP_KERNEL);
2888err:
2889 if (err < 0)
2890 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
2891 return err;
2892}
2893
Patrick McHardy20a69342013-10-11 12:06:22 +02002894static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2895 const struct nlattr *attr)
2896{
2897 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2898 struct nft_data_desc d1, d2;
2899 struct nft_set_elem elem;
2900 struct nft_set_binding *binding;
2901 enum nft_registers dreg;
2902 int err;
2903
Patrick McHardyc50b9602014-03-28 10:19:47 +00002904 if (set->size && set->nelems == set->size)
2905 return -ENFILE;
2906
Patrick McHardy20a69342013-10-11 12:06:22 +02002907 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2908 nft_set_elem_policy);
2909 if (err < 0)
2910 return err;
2911
2912 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2913 return -EINVAL;
2914
2915 elem.flags = 0;
2916 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2917 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2918 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2919 return -EINVAL;
2920 }
2921
2922 if (set->flags & NFT_SET_MAP) {
2923 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2924 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2925 return -EINVAL;
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01002926 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
2927 elem.flags & NFT_SET_ELEM_INTERVAL_END)
2928 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02002929 } else {
2930 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2931 return -EINVAL;
2932 }
2933
2934 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2935 if (err < 0)
2936 goto err1;
2937 err = -EINVAL;
2938 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2939 goto err2;
2940
2941 err = -EEXIST;
2942 if (set->ops->get(set, &elem) == 0)
2943 goto err2;
2944
2945 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2946 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2947 if (err < 0)
2948 goto err2;
2949
2950 err = -EINVAL;
2951 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2952 goto err3;
2953
2954 dreg = nft_type_to_reg(set->dtype);
2955 list_for_each_entry(binding, &set->bindings, list) {
2956 struct nft_ctx bind_ctx = {
2957 .afi = ctx->afi,
2958 .table = ctx->table,
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002959 .chain = (struct nft_chain *)binding->chain,
Patrick McHardy20a69342013-10-11 12:06:22 +02002960 };
2961
2962 err = nft_validate_data_load(&bind_ctx, dreg,
2963 &elem.data, d2.type);
2964 if (err < 0)
2965 goto err3;
2966 }
2967 }
2968
2969 err = set->ops->insert(set, &elem);
2970 if (err < 0)
2971 goto err3;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002972 set->nelems++;
Patrick McHardy20a69342013-10-11 12:06:22 +02002973
Arturo Borrerod60ce622014-04-01 14:06:07 +02002974 nf_tables_setelem_notify(ctx, set, &elem, NFT_MSG_NEWSETELEM, 0);
Patrick McHardy20a69342013-10-11 12:06:22 +02002975 return 0;
2976
2977err3:
2978 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2979 nft_data_uninit(&elem.data, d2.type);
2980err2:
2981 nft_data_uninit(&elem.key, d1.type);
2982err1:
2983 return err;
2984}
2985
2986static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2987 const struct nlmsghdr *nlh,
2988 const struct nlattr * const nla[])
2989{
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002990 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002991 const struct nlattr *attr;
2992 struct nft_set *set;
2993 struct nft_ctx ctx;
2994 int rem, err;
2995
2996 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2997 if (err < 0)
2998 return err;
2999
3000 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003001 if (IS_ERR(set)) {
3002 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3003 set = nf_tables_set_lookup_byid(net,
3004 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3005 }
3006 if (IS_ERR(set))
3007 return PTR_ERR(set);
3008 }
3009
Patrick McHardy20a69342013-10-11 12:06:22 +02003010 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3011 return -EBUSY;
3012
3013 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3014 err = nft_add_set_elem(&ctx, set, attr);
3015 if (err < 0)
3016 return err;
3017 }
3018 return 0;
3019}
3020
3021static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
3022 const struct nlattr *attr)
3023{
3024 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3025 struct nft_data_desc desc;
3026 struct nft_set_elem elem;
3027 int err;
3028
3029 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3030 nft_set_elem_policy);
3031 if (err < 0)
3032 goto err1;
3033
3034 err = -EINVAL;
3035 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3036 goto err1;
3037
3038 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3039 if (err < 0)
3040 goto err1;
3041
3042 err = -EINVAL;
3043 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3044 goto err2;
3045
3046 err = set->ops->get(set, &elem);
3047 if (err < 0)
3048 goto err2;
3049
3050 set->ops->remove(set, &elem);
Patrick McHardyc50b9602014-03-28 10:19:47 +00003051 set->nelems--;
Patrick McHardy20a69342013-10-11 12:06:22 +02003052
Arturo Borrerod60ce622014-04-01 14:06:07 +02003053 nf_tables_setelem_notify(ctx, set, &elem, NFT_MSG_DELSETELEM, 0);
3054
Patrick McHardy20a69342013-10-11 12:06:22 +02003055 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
3056 if (set->flags & NFT_SET_MAP)
3057 nft_data_uninit(&elem.data, set->dtype);
3058
3059err2:
3060 nft_data_uninit(&elem.key, desc.type);
3061err1:
3062 return err;
3063}
3064
3065static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3066 const struct nlmsghdr *nlh,
3067 const struct nlattr * const nla[])
3068{
3069 const struct nlattr *attr;
3070 struct nft_set *set;
3071 struct nft_ctx ctx;
3072 int rem, err;
3073
3074 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
3075 if (err < 0)
3076 return err;
3077
3078 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3079 if (IS_ERR(set))
3080 return PTR_ERR(set);
3081 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3082 return -EBUSY;
3083
3084 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3085 err = nft_del_setelem(&ctx, set, attr);
3086 if (err < 0)
3087 return err;
3088 }
3089 return 0;
3090}
3091
Patrick McHardy96518512013-10-14 11:00:02 +02003092static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3093 [NFT_MSG_NEWTABLE] = {
3094 .call = nf_tables_newtable,
3095 .attr_count = NFTA_TABLE_MAX,
3096 .policy = nft_table_policy,
3097 },
3098 [NFT_MSG_GETTABLE] = {
3099 .call = nf_tables_gettable,
3100 .attr_count = NFTA_TABLE_MAX,
3101 .policy = nft_table_policy,
3102 },
3103 [NFT_MSG_DELTABLE] = {
3104 .call = nf_tables_deltable,
3105 .attr_count = NFTA_TABLE_MAX,
3106 .policy = nft_table_policy,
3107 },
3108 [NFT_MSG_NEWCHAIN] = {
3109 .call = nf_tables_newchain,
3110 .attr_count = NFTA_CHAIN_MAX,
3111 .policy = nft_chain_policy,
3112 },
3113 [NFT_MSG_GETCHAIN] = {
3114 .call = nf_tables_getchain,
3115 .attr_count = NFTA_CHAIN_MAX,
3116 .policy = nft_chain_policy,
3117 },
3118 [NFT_MSG_DELCHAIN] = {
3119 .call = nf_tables_delchain,
3120 .attr_count = NFTA_CHAIN_MAX,
3121 .policy = nft_chain_policy,
3122 },
3123 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003124 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003125 .attr_count = NFTA_RULE_MAX,
3126 .policy = nft_rule_policy,
3127 },
3128 [NFT_MSG_GETRULE] = {
3129 .call = nf_tables_getrule,
3130 .attr_count = NFTA_RULE_MAX,
3131 .policy = nft_rule_policy,
3132 },
3133 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003134 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003135 .attr_count = NFTA_RULE_MAX,
3136 .policy = nft_rule_policy,
3137 },
Patrick McHardy20a69342013-10-11 12:06:22 +02003138 [NFT_MSG_NEWSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003139 .call_batch = nf_tables_newset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003140 .attr_count = NFTA_SET_MAX,
3141 .policy = nft_set_policy,
3142 },
3143 [NFT_MSG_GETSET] = {
3144 .call = nf_tables_getset,
3145 .attr_count = NFTA_SET_MAX,
3146 .policy = nft_set_policy,
3147 },
3148 [NFT_MSG_DELSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003149 .call_batch = nf_tables_delset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003150 .attr_count = NFTA_SET_MAX,
3151 .policy = nft_set_policy,
3152 },
3153 [NFT_MSG_NEWSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003154 .call_batch = nf_tables_newsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003155 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3156 .policy = nft_set_elem_list_policy,
3157 },
3158 [NFT_MSG_GETSETELEM] = {
3159 .call = nf_tables_getsetelem,
3160 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3161 .policy = nft_set_elem_list_policy,
3162 },
3163 [NFT_MSG_DELSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003164 .call_batch = nf_tables_delsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003165 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3166 .policy = nft_set_elem_list_policy,
3167 },
Patrick McHardy96518512013-10-14 11:00:02 +02003168};
3169
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003170static int nf_tables_commit(struct sk_buff *skb)
3171{
3172 struct net *net = sock_net(skb->sk);
3173 struct nft_trans *trans, *next;
3174
3175 /* Bump generation counter, invalidate any dump in progress */
3176 net->nft.genctr++;
3177
3178 /* A new generation has just started */
3179 net->nft.gencursor = gencursor_next(net);
3180
3181 /* Make sure all packets have left the previous generation before
3182 * purging old rules.
3183 */
3184 synchronize_rcu();
3185
3186 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003187 switch (trans->msg_type) {
3188 case NFT_MSG_NEWRULE:
3189 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3190 nf_tables_rule_notify(trans->ctx.skb, trans->ctx.nlh,
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003191 trans->ctx.table,
3192 trans->ctx.chain,
3193 nft_trans_rule(trans),
3194 NFT_MSG_NEWRULE, 0,
3195 trans->ctx.afi->family);
3196 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003197 break;
3198 case NFT_MSG_DELRULE:
3199 list_del_rcu(&nft_trans_rule(trans)->list);
3200 nf_tables_rule_notify(trans->ctx.skb, trans->ctx.nlh,
3201 trans->ctx.table,
3202 trans->ctx.chain,
3203 nft_trans_rule(trans), NFT_MSG_DELRULE, 0,
3204 trans->ctx.afi->family);
3205 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003206 case NFT_MSG_NEWSET:
3207 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
3208 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3209 NFT_MSG_NEWSET);
3210 nft_trans_destroy(trans);
3211 break;
3212 case NFT_MSG_DELSET:
3213 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3214 NFT_MSG_DELSET);
3215 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003216 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003217 }
3218
3219 /* Make sure we don't see any packet traversing old rules */
3220 synchronize_rcu();
3221
3222 /* Now we can safely release unused old rules */
3223 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003224 switch (trans->msg_type) {
3225 case NFT_MSG_DELRULE:
3226 nf_tables_rule_destroy(&trans->ctx,
3227 nft_trans_rule(trans));
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003228 break;
3229 case NFT_MSG_DELSET:
3230 nft_set_destroy(nft_trans_set(trans));
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003231 break;
3232 }
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003233 nft_trans_destroy(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003234 }
3235
3236 return 0;
3237}
3238
3239static int nf_tables_abort(struct sk_buff *skb)
3240{
3241 struct net *net = sock_net(skb->sk);
3242 struct nft_trans *trans, *next;
3243
3244 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003245 switch (trans->msg_type) {
3246 case NFT_MSG_NEWRULE:
3247 list_del_rcu(&nft_trans_rule(trans)->list);
3248 break;
3249 case NFT_MSG_DELRULE:
3250 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003251 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003252 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003253 case NFT_MSG_NEWSET:
3254 list_del(&nft_trans_set(trans)->list);
3255 break;
3256 case NFT_MSG_DELSET:
3257 list_add_tail(&nft_trans_set(trans)->list,
3258 &trans->ctx.table->sets);
3259 nft_trans_destroy(trans);
3260 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003261 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003262 }
3263
3264 /* Make sure we don't see any packet accessing aborted rules */
3265 synchronize_rcu();
3266
3267 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003268 switch (trans->msg_type) {
3269 case NFT_MSG_NEWRULE:
3270 nf_tables_rule_destroy(&trans->ctx,
3271 nft_trans_rule(trans));
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003272 break;
3273 case NFT_MSG_NEWSET:
3274 nft_set_destroy(nft_trans_set(trans));
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003275 break;
3276 }
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003277 nft_trans_destroy(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003278 }
3279
3280 return 0;
3281}
3282
Patrick McHardy96518512013-10-14 11:00:02 +02003283static const struct nfnetlink_subsystem nf_tables_subsys = {
3284 .name = "nf_tables",
3285 .subsys_id = NFNL_SUBSYS_NFTABLES,
3286 .cb_count = NFT_MSG_MAX,
3287 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003288 .commit = nf_tables_commit,
3289 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02003290};
3291
Patrick McHardy20a69342013-10-11 12:06:22 +02003292/*
3293 * Loop detection - walk through the ruleset beginning at the destination chain
3294 * of a new jump until either the source chain is reached (loop) or all
3295 * reachable chains have been traversed.
3296 *
3297 * The loop check is performed whenever a new jump verdict is added to an
3298 * expression or verdict map or a verdict map is bound to a new chain.
3299 */
3300
3301static int nf_tables_check_loops(const struct nft_ctx *ctx,
3302 const struct nft_chain *chain);
3303
3304static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3305 const struct nft_set *set,
3306 const struct nft_set_iter *iter,
3307 const struct nft_set_elem *elem)
3308{
Pablo Neira Ayuso62f9c8b2014-02-07 14:45:01 +01003309 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3310 return 0;
3311
Patrick McHardy20a69342013-10-11 12:06:22 +02003312 switch (elem->data.verdict) {
3313 case NFT_JUMP:
3314 case NFT_GOTO:
3315 return nf_tables_check_loops(ctx, elem->data.chain);
3316 default:
3317 return 0;
3318 }
3319}
3320
3321static int nf_tables_check_loops(const struct nft_ctx *ctx,
3322 const struct nft_chain *chain)
3323{
3324 const struct nft_rule *rule;
3325 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02003326 const struct nft_set *set;
3327 struct nft_set_binding *binding;
3328 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02003329
3330 if (ctx->chain == chain)
3331 return -ELOOP;
3332
3333 list_for_each_entry(rule, &chain->rules, list) {
3334 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003335 const struct nft_data *data = NULL;
3336 int err;
3337
3338 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02003339 continue;
3340
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003341 err = expr->ops->validate(ctx, expr, &data);
3342 if (err < 0)
3343 return err;
3344
Patrick McHardy20a69342013-10-11 12:06:22 +02003345 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003346 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02003347
3348 switch (data->verdict) {
3349 case NFT_JUMP:
3350 case NFT_GOTO:
3351 err = nf_tables_check_loops(ctx, data->chain);
3352 if (err < 0)
3353 return err;
3354 default:
3355 break;
3356 }
3357 }
3358 }
3359
3360 list_for_each_entry(set, &ctx->table->sets, list) {
3361 if (!(set->flags & NFT_SET_MAP) ||
3362 set->dtype != NFT_DATA_VERDICT)
3363 continue;
3364
3365 list_for_each_entry(binding, &set->bindings, list) {
3366 if (binding->chain != chain)
3367 continue;
3368
3369 iter.skip = 0;
3370 iter.count = 0;
3371 iter.err = 0;
3372 iter.fn = nf_tables_loop_check_setelem;
3373
3374 set->ops->walk(ctx, set, &iter);
3375 if (iter.err < 0)
3376 return iter.err;
3377 }
3378 }
3379
3380 return 0;
3381}
3382
Patrick McHardy96518512013-10-14 11:00:02 +02003383/**
3384 * nft_validate_input_register - validate an expressions' input register
3385 *
3386 * @reg: the register number
3387 *
3388 * Validate that the input register is one of the general purpose
3389 * registers.
3390 */
3391int nft_validate_input_register(enum nft_registers reg)
3392{
3393 if (reg <= NFT_REG_VERDICT)
3394 return -EINVAL;
3395 if (reg > NFT_REG_MAX)
3396 return -ERANGE;
3397 return 0;
3398}
3399EXPORT_SYMBOL_GPL(nft_validate_input_register);
3400
3401/**
3402 * nft_validate_output_register - validate an expressions' output register
3403 *
3404 * @reg: the register number
3405 *
3406 * Validate that the output register is one of the general purpose
3407 * registers or the verdict register.
3408 */
3409int nft_validate_output_register(enum nft_registers reg)
3410{
3411 if (reg < NFT_REG_VERDICT)
3412 return -EINVAL;
3413 if (reg > NFT_REG_MAX)
3414 return -ERANGE;
3415 return 0;
3416}
3417EXPORT_SYMBOL_GPL(nft_validate_output_register);
3418
3419/**
3420 * nft_validate_data_load - validate an expressions' data load
3421 *
3422 * @ctx: context of the expression performing the load
3423 * @reg: the destination register number
3424 * @data: the data to load
3425 * @type: the data type
3426 *
3427 * Validate that a data load uses the appropriate data type for
3428 * the destination register. A value of NULL for the data means
3429 * that its runtime gathered data, which is always of type
3430 * NFT_DATA_VALUE.
3431 */
3432int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3433 const struct nft_data *data,
3434 enum nft_data_types type)
3435{
Patrick McHardy20a69342013-10-11 12:06:22 +02003436 int err;
3437
Patrick McHardy96518512013-10-14 11:00:02 +02003438 switch (reg) {
3439 case NFT_REG_VERDICT:
3440 if (data == NULL || type != NFT_DATA_VERDICT)
3441 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003442
3443 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3444 err = nf_tables_check_loops(ctx, data->chain);
3445 if (err < 0)
3446 return err;
3447
3448 if (ctx->chain->level + 1 > data->chain->level) {
3449 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3450 return -EMLINK;
3451 data->chain->level = ctx->chain->level + 1;
3452 }
3453 }
3454
Patrick McHardy96518512013-10-14 11:00:02 +02003455 return 0;
3456 default:
3457 if (data != NULL && type != NFT_DATA_VALUE)
3458 return -EINVAL;
3459 return 0;
3460 }
3461}
3462EXPORT_SYMBOL_GPL(nft_validate_data_load);
3463
3464static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3465 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3466 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3467 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3468};
3469
3470static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3471 struct nft_data_desc *desc, const struct nlattr *nla)
3472{
3473 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3474 struct nft_chain *chain;
3475 int err;
3476
3477 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3478 if (err < 0)
3479 return err;
3480
3481 if (!tb[NFTA_VERDICT_CODE])
3482 return -EINVAL;
3483 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3484
3485 switch (data->verdict) {
Patrick McHardye0abdad2014-02-18 18:06:50 +00003486 default:
3487 switch (data->verdict & NF_VERDICT_MASK) {
3488 case NF_ACCEPT:
3489 case NF_DROP:
3490 case NF_QUEUE:
3491 break;
3492 default:
3493 return -EINVAL;
3494 }
3495 /* fall through */
Patrick McHardy96518512013-10-14 11:00:02 +02003496 case NFT_CONTINUE:
3497 case NFT_BREAK:
3498 case NFT_RETURN:
3499 desc->len = sizeof(data->verdict);
3500 break;
3501 case NFT_JUMP:
3502 case NFT_GOTO:
3503 if (!tb[NFTA_VERDICT_CHAIN])
3504 return -EINVAL;
3505 chain = nf_tables_chain_lookup(ctx->table,
3506 tb[NFTA_VERDICT_CHAIN]);
3507 if (IS_ERR(chain))
3508 return PTR_ERR(chain);
3509 if (chain->flags & NFT_BASE_CHAIN)
3510 return -EOPNOTSUPP;
3511
Patrick McHardy96518512013-10-14 11:00:02 +02003512 chain->use++;
3513 data->chain = chain;
3514 desc->len = sizeof(data);
3515 break;
Patrick McHardy96518512013-10-14 11:00:02 +02003516 }
3517
3518 desc->type = NFT_DATA_VERDICT;
3519 return 0;
3520}
3521
3522static void nft_verdict_uninit(const struct nft_data *data)
3523{
3524 switch (data->verdict) {
3525 case NFT_JUMP:
3526 case NFT_GOTO:
3527 data->chain->use--;
3528 break;
3529 }
3530}
3531
3532static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3533{
3534 struct nlattr *nest;
3535
3536 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3537 if (!nest)
3538 goto nla_put_failure;
3539
3540 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3541 goto nla_put_failure;
3542
3543 switch (data->verdict) {
3544 case NFT_JUMP:
3545 case NFT_GOTO:
3546 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3547 goto nla_put_failure;
3548 }
3549 nla_nest_end(skb, nest);
3550 return 0;
3551
3552nla_put_failure:
3553 return -1;
3554}
3555
3556static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3557 struct nft_data_desc *desc, const struct nlattr *nla)
3558{
3559 unsigned int len;
3560
3561 len = nla_len(nla);
3562 if (len == 0)
3563 return -EINVAL;
3564 if (len > sizeof(data->data))
3565 return -EOVERFLOW;
3566
3567 nla_memcpy(data->data, nla, sizeof(data->data));
3568 desc->type = NFT_DATA_VALUE;
3569 desc->len = len;
3570 return 0;
3571}
3572
3573static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3574 unsigned int len)
3575{
3576 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3577}
3578
3579static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3580 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3581 .len = FIELD_SIZEOF(struct nft_data, data) },
3582 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3583};
3584
3585/**
3586 * nft_data_init - parse nf_tables data netlink attributes
3587 *
3588 * @ctx: context of the expression using the data
3589 * @data: destination struct nft_data
3590 * @desc: data description
3591 * @nla: netlink attribute containing data
3592 *
3593 * Parse the netlink data attributes and initialize a struct nft_data.
3594 * The type and length of data are returned in the data description.
3595 *
3596 * The caller can indicate that it only wants to accept data of type
3597 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3598 */
3599int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3600 struct nft_data_desc *desc, const struct nlattr *nla)
3601{
3602 struct nlattr *tb[NFTA_DATA_MAX + 1];
3603 int err;
3604
3605 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3606 if (err < 0)
3607 return err;
3608
3609 if (tb[NFTA_DATA_VALUE])
3610 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3611 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3612 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3613 return -EINVAL;
3614}
3615EXPORT_SYMBOL_GPL(nft_data_init);
3616
3617/**
3618 * nft_data_uninit - release a nft_data item
3619 *
3620 * @data: struct nft_data to release
3621 * @type: type of data
3622 *
3623 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3624 * all others need to be released by calling this function.
3625 */
3626void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3627{
3628 switch (type) {
3629 case NFT_DATA_VALUE:
3630 return;
3631 case NFT_DATA_VERDICT:
3632 return nft_verdict_uninit(data);
3633 default:
3634 WARN_ON(1);
3635 }
3636}
3637EXPORT_SYMBOL_GPL(nft_data_uninit);
3638
3639int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3640 enum nft_data_types type, unsigned int len)
3641{
3642 struct nlattr *nest;
3643 int err;
3644
3645 nest = nla_nest_start(skb, attr);
3646 if (nest == NULL)
3647 return -1;
3648
3649 switch (type) {
3650 case NFT_DATA_VALUE:
3651 err = nft_value_dump(skb, data, len);
3652 break;
3653 case NFT_DATA_VERDICT:
3654 err = nft_verdict_dump(skb, data);
3655 break;
3656 default:
3657 err = -EINVAL;
3658 WARN_ON(1);
3659 }
3660
3661 nla_nest_end(skb, nest);
3662 return err;
3663}
3664EXPORT_SYMBOL_GPL(nft_data_dump);
3665
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003666static int nf_tables_init_net(struct net *net)
3667{
3668 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003669 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003670 return 0;
3671}
3672
3673static struct pernet_operations nf_tables_net_ops = {
3674 .init = nf_tables_init_net,
3675};
3676
Patrick McHardy96518512013-10-14 11:00:02 +02003677static int __init nf_tables_module_init(void)
3678{
3679 int err;
3680
3681 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3682 GFP_KERNEL);
3683 if (info == NULL) {
3684 err = -ENOMEM;
3685 goto err1;
3686 }
3687
3688 err = nf_tables_core_module_init();
3689 if (err < 0)
3690 goto err2;
3691
3692 err = nfnetlink_subsys_register(&nf_tables_subsys);
3693 if (err < 0)
3694 goto err3;
3695
3696 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003697 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003698err3:
3699 nf_tables_core_module_exit();
3700err2:
3701 kfree(info);
3702err1:
3703 return err;
3704}
3705
3706static void __exit nf_tables_module_exit(void)
3707{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003708 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003709 nfnetlink_subsys_unregister(&nf_tables_subsys);
3710 nf_tables_core_module_exit();
3711 kfree(info);
3712}
3713
3714module_init(nf_tables_module_init);
3715module_exit(nf_tables_module_exit);
3716
3717MODULE_LICENSE("GPL");
3718MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3719MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);