blob: 92cec68c03ecec606d3f7372bc677b62fcda4e52 [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
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200808static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200809{
810 struct nlattr *tb[NFTA_COUNTER_MAX+1];
811 struct nft_stats __percpu *newstats;
812 struct nft_stats *stats;
813 int err;
814
815 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
816 if (err < 0)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200817 return ERR_PTR(err);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200818
819 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200820 return ERR_PTR(-EINVAL);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200821
822 newstats = alloc_percpu(struct nft_stats);
823 if (newstats == NULL)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200824 return ERR_PTR(-ENOMEM);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200825
826 /* Restore old counters on this cpu, no problem. Per-cpu statistics
827 * are not exposed to userspace.
828 */
829 stats = this_cpu_ptr(newstats);
830 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
831 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
832
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200833 return newstats;
834}
835
836static void nft_chain_stats_replace(struct nft_base_chain *chain,
837 struct nft_stats __percpu *newstats)
838{
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200839 if (chain->stats) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200840 struct nft_stats __percpu *oldstats =
Patrick McHardy67a8fc22014-02-18 18:06:49 +0000841 nft_dereference(chain->stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200842
843 rcu_assign_pointer(chain->stats, newstats);
844 synchronize_rcu();
845 free_percpu(oldstats);
846 } else
847 rcu_assign_pointer(chain->stats, newstats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200848}
849
Patrick McHardy96518512013-10-14 11:00:02 +0200850static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
851 const struct nlmsghdr *nlh,
852 const struct nlattr * const nla[])
853{
854 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
855 const struct nlattr * uninitialized_var(name);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +0200856 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +0200857 struct nft_table *table;
858 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200859 struct nft_base_chain *basechain = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200860 struct nlattr *ha[NFTA_HOOK_MAX + 1];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200861 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200862 int family = nfmsg->nfgen_family;
Patrick McHardy57de2a02014-01-09 18:42:31 +0000863 u8 policy = NF_ACCEPT;
Patrick McHardy96518512013-10-14 11:00:02 +0200864 u64 handle = 0;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000865 unsigned int i;
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200866 struct nft_stats __percpu *stats;
Patrick McHardy96518512013-10-14 11:00:02 +0200867 int err;
868 bool create;
869
870 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
871
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200872 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200873 if (IS_ERR(afi))
874 return PTR_ERR(afi);
875
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200876 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200877 if (IS_ERR(table))
878 return PTR_ERR(table);
879
Patrick McHardy96518512013-10-14 11:00:02 +0200880 chain = NULL;
881 name = nla[NFTA_CHAIN_NAME];
882
883 if (nla[NFTA_CHAIN_HANDLE]) {
884 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
885 chain = nf_tables_chain_lookup_byhandle(table, handle);
886 if (IS_ERR(chain))
887 return PTR_ERR(chain);
888 } else {
889 chain = nf_tables_chain_lookup(table, name);
890 if (IS_ERR(chain)) {
891 if (PTR_ERR(chain) != -ENOENT)
892 return PTR_ERR(chain);
893 chain = NULL;
894 }
895 }
896
Patrick McHardy57de2a02014-01-09 18:42:31 +0000897 if (nla[NFTA_CHAIN_POLICY]) {
898 if ((chain != NULL &&
899 !(chain->flags & NFT_BASE_CHAIN)) ||
900 nla[NFTA_CHAIN_HOOK] == NULL)
901 return -EOPNOTSUPP;
902
Pablo Neira Ayuso8f46df12014-01-10 15:11:25 +0100903 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
Patrick McHardy57de2a02014-01-09 18:42:31 +0000904 switch (policy) {
905 case NF_DROP:
906 case NF_ACCEPT:
907 break;
908 default:
909 return -EINVAL;
910 }
911 }
912
Patrick McHardy96518512013-10-14 11:00:02 +0200913 if (chain != NULL) {
914 if (nlh->nlmsg_flags & NLM_F_EXCL)
915 return -EEXIST;
916 if (nlh->nlmsg_flags & NLM_F_REPLACE)
917 return -EOPNOTSUPP;
918
919 if (nla[NFTA_CHAIN_HANDLE] && name &&
920 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
921 return -EEXIST;
922
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200923 if (nla[NFTA_CHAIN_COUNTERS]) {
924 if (!(chain->flags & NFT_BASE_CHAIN))
925 return -EOPNOTSUPP;
926
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200927 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
928 if (IS_ERR(stats))
929 return PTR_ERR(stats);
930
931 nft_chain_stats_replace(nft_base_chain(chain), stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200932 }
933
Patrick McHardy4401a862014-01-09 18:42:32 +0000934 if (nla[NFTA_CHAIN_POLICY])
935 nft_base_chain(chain)->policy = policy;
936
Patrick McHardy96518512013-10-14 11:00:02 +0200937 if (nla[NFTA_CHAIN_HANDLE] && name)
938 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
939
940 goto notify;
941 }
942
Patrick McHardy75820672014-01-09 18:42:33 +0000943 if (table->use == UINT_MAX)
944 return -EOVERFLOW;
945
Patrick McHardy96518512013-10-14 11:00:02 +0200946 if (nla[NFTA_CHAIN_HOOK]) {
Patrick McHardy2a37d752014-01-09 18:42:37 +0000947 const struct nf_chain_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +0200948 struct nf_hook_ops *ops;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200949 nf_hookfn *hookfn;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000950 u32 hooknum, priority;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200951
Patrick McHardybaae3e62014-01-09 18:42:34 +0000952 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200953 if (nla[NFTA_CHAIN_TYPE]) {
954 type = nf_tables_chain_type_lookup(afi,
955 nla[NFTA_CHAIN_TYPE],
956 create);
Patrick McHardy93b08062014-01-09 18:42:36 +0000957 if (IS_ERR(type))
958 return PTR_ERR(type);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200959 }
Patrick McHardy96518512013-10-14 11:00:02 +0200960
961 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
962 nft_hook_policy);
963 if (err < 0)
964 return err;
965 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
966 ha[NFTA_HOOK_PRIORITY] == NULL)
967 return -EINVAL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200968
969 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
970 if (hooknum >= afi->nhooks)
Patrick McHardy96518512013-10-14 11:00:02 +0200971 return -EINVAL;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000972 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
Patrick McHardy96518512013-10-14 11:00:02 +0200973
Patrick McHardybaae3e62014-01-09 18:42:34 +0000974 if (!(type->hook_mask & (1 << hooknum)))
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200975 return -EOPNOTSUPP;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000976 if (!try_module_get(type->owner))
Patrick McHardybaae3e62014-01-09 18:42:34 +0000977 return -ENOENT;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000978 hookfn = type->hooks[hooknum];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200979
Patrick McHardy96518512013-10-14 11:00:02 +0200980 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
981 if (basechain == NULL)
982 return -ENOMEM;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200983
Patrick McHardy4401a862014-01-09 18:42:32 +0000984 if (nla[NFTA_CHAIN_COUNTERS]) {
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200985 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
986 if (IS_ERR(stats)) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000987 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +0000988 kfree(basechain);
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200989 return PTR_ERR(stats);
Patrick McHardy4401a862014-01-09 18:42:32 +0000990 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200991 basechain->stats = stats;
Patrick McHardy4401a862014-01-09 18:42:32 +0000992 } else {
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200993 stats = alloc_percpu(struct nft_stats);
994 if (IS_ERR(stats)) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000995 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +0000996 kfree(basechain);
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200997 return PTR_ERR(stats);
Patrick McHardy4401a862014-01-09 18:42:32 +0000998 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200999 rcu_assign_pointer(basechain->stats, stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001000 }
1001
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001002 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +02001003 chain = &basechain->chain;
1004
Patrick McHardy115a60b2014-01-03 12:16:15 +00001005 for (i = 0; i < afi->nops; i++) {
1006 ops = &basechain->ops[i];
1007 ops->pf = family;
1008 ops->owner = afi->owner;
1009 ops->hooknum = hooknum;
1010 ops->priority = priority;
1011 ops->priv = chain;
1012 ops->hook = afi->hooks[ops->hooknum];
1013 if (hookfn)
1014 ops->hook = hookfn;
1015 if (afi->hook_ops_init)
1016 afi->hook_ops_init(ops, i);
1017 }
Patrick McHardy96518512013-10-14 11:00:02 +02001018
1019 chain->flags |= NFT_BASE_CHAIN;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001020 basechain->policy = policy;
Patrick McHardy96518512013-10-14 11:00:02 +02001021 } else {
1022 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1023 if (chain == NULL)
1024 return -ENOMEM;
1025 }
1026
1027 INIT_LIST_HEAD(&chain->rules);
1028 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001029 chain->net = net;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +02001030 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +02001031 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1032
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001033 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1034 chain->flags & NFT_BASE_CHAIN) {
Patrick McHardy115a60b2014-01-03 12:16:15 +00001035 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001036 if (err < 0) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001037 module_put(basechain->type->owner);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001038 free_percpu(basechain->stats);
1039 kfree(basechain);
1040 return err;
1041 }
1042 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001043 list_add_tail(&chain->list, &table->chains);
1044 table->use++;
Patrick McHardy96518512013-10-14 11:00:02 +02001045notify:
1046 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
1047 family);
1048 return 0;
1049}
1050
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001051static void nf_tables_chain_destroy(struct nft_chain *chain)
Patrick McHardy96518512013-10-14 11:00:02 +02001052{
Patrick McHardy96518512013-10-14 11:00:02 +02001053 BUG_ON(chain->use > 0);
1054
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001055 if (chain->flags & NFT_BASE_CHAIN) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001056 module_put(nft_base_chain(chain)->type->owner);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001057 free_percpu(nft_base_chain(chain)->stats);
Patrick McHardy96518512013-10-14 11:00:02 +02001058 kfree(nft_base_chain(chain));
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001059 } else
Patrick McHardy96518512013-10-14 11:00:02 +02001060 kfree(chain);
1061}
1062
1063static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1064 const struct nlmsghdr *nlh,
1065 const struct nlattr * const nla[])
1066{
1067 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001068 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001069 struct nft_table *table;
1070 struct nft_chain *chain;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001071 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001072 int family = nfmsg->nfgen_family;
1073
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001074 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001075 if (IS_ERR(afi))
1076 return PTR_ERR(afi);
1077
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001078 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001079 if (IS_ERR(table))
1080 return PTR_ERR(table);
1081
1082 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1083 if (IS_ERR(chain))
1084 return PTR_ERR(chain);
1085
Patrick McHardy3dd72792014-01-25 08:04:07 +00001086 if (!list_empty(&chain->rules) || chain->use > 0)
Patrick McHardy96518512013-10-14 11:00:02 +02001087 return -EBUSY;
1088
1089 list_del(&chain->list);
1090 table->use--;
1091
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001092 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1093 chain->flags & NFT_BASE_CHAIN)
Patrick McHardy115a60b2014-01-03 12:16:15 +00001094 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
Patrick McHardy96518512013-10-14 11:00:02 +02001095
1096 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1097 family);
1098
1099 /* Make sure all rule references are gone before this is released */
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001100 synchronize_rcu();
1101
1102 nf_tables_chain_destroy(chain);
Patrick McHardy96518512013-10-14 11:00:02 +02001103 return 0;
1104}
1105
Patrick McHardy96518512013-10-14 11:00:02 +02001106/*
1107 * Expressions
1108 */
1109
1110/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001111 * nft_register_expr - register nf_tables expr type
1112 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001113 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001114 * Registers the expr type for use with nf_tables. Returns zero on
Patrick McHardy96518512013-10-14 11:00:02 +02001115 * success or a negative errno code otherwise.
1116 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001117int nft_register_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001118{
1119 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001120 if (type->family == NFPROTO_UNSPEC)
1121 list_add_tail(&type->list, &nf_tables_expressions);
1122 else
1123 list_add(&type->list, &nf_tables_expressions);
Patrick McHardy96518512013-10-14 11:00:02 +02001124 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1125 return 0;
1126}
1127EXPORT_SYMBOL_GPL(nft_register_expr);
1128
1129/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001130 * nft_unregister_expr - unregister nf_tables expr type
1131 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001132 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001133 * Unregisters the expr typefor use with nf_tables.
Patrick McHardy96518512013-10-14 11:00:02 +02001134 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001135void nft_unregister_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001136{
1137 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001138 list_del(&type->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001139 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1140}
1141EXPORT_SYMBOL_GPL(nft_unregister_expr);
1142
Patrick McHardy64d46802014-02-05 15:03:37 +00001143static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1144 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001145{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001146 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001147
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001148 list_for_each_entry(type, &nf_tables_expressions, list) {
Patrick McHardy64d46802014-02-05 15:03:37 +00001149 if (!nla_strcmp(nla, type->name) &&
1150 (!type->family || type->family == family))
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001151 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001152 }
1153 return NULL;
1154}
1155
Patrick McHardy64d46802014-02-05 15:03:37 +00001156static const struct nft_expr_type *nft_expr_type_get(u8 family,
1157 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001158{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001159 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001160
1161 if (nla == NULL)
1162 return ERR_PTR(-EINVAL);
1163
Patrick McHardy64d46802014-02-05 15:03:37 +00001164 type = __nft_expr_type_get(family, nla);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001165 if (type != NULL && try_module_get(type->owner))
1166 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001167
1168#ifdef CONFIG_MODULES
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001169 if (type == NULL) {
Patrick McHardy96518512013-10-14 11:00:02 +02001170 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001171 request_module("nft-expr-%u-%.*s", family,
1172 nla_len(nla), (char *)nla_data(nla));
1173 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1174 if (__nft_expr_type_get(family, nla))
1175 return ERR_PTR(-EAGAIN);
1176
1177 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy96518512013-10-14 11:00:02 +02001178 request_module("nft-expr-%.*s",
1179 nla_len(nla), (char *)nla_data(nla));
1180 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001181 if (__nft_expr_type_get(family, nla))
Patrick McHardy96518512013-10-14 11:00:02 +02001182 return ERR_PTR(-EAGAIN);
1183 }
1184#endif
1185 return ERR_PTR(-ENOENT);
1186}
1187
1188static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1189 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1190 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1191};
1192
1193static int nf_tables_fill_expr_info(struct sk_buff *skb,
1194 const struct nft_expr *expr)
1195{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001196 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
Patrick McHardy96518512013-10-14 11:00:02 +02001197 goto nla_put_failure;
1198
1199 if (expr->ops->dump) {
1200 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1201 if (data == NULL)
1202 goto nla_put_failure;
1203 if (expr->ops->dump(skb, expr) < 0)
1204 goto nla_put_failure;
1205 nla_nest_end(skb, data);
1206 }
1207
1208 return skb->len;
1209
1210nla_put_failure:
1211 return -1;
1212};
1213
1214struct nft_expr_info {
1215 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001216 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001217};
1218
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001219static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1220 const struct nlattr *nla,
Patrick McHardy96518512013-10-14 11:00:02 +02001221 struct nft_expr_info *info)
1222{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001223 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001224 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001225 struct nlattr *tb[NFTA_EXPR_MAX + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001226 int err;
1227
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001228 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
Patrick McHardy96518512013-10-14 11:00:02 +02001229 if (err < 0)
1230 return err;
1231
Patrick McHardy64d46802014-02-05 15:03:37 +00001232 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001233 if (IS_ERR(type))
1234 return PTR_ERR(type);
1235
1236 if (tb[NFTA_EXPR_DATA]) {
1237 err = nla_parse_nested(info->tb, type->maxattr,
1238 tb[NFTA_EXPR_DATA], type->policy);
1239 if (err < 0)
1240 goto err1;
1241 } else
1242 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1243
1244 if (type->select_ops != NULL) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001245 ops = type->select_ops(ctx,
1246 (const struct nlattr * const *)info->tb);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001247 if (IS_ERR(ops)) {
1248 err = PTR_ERR(ops);
1249 goto err1;
1250 }
1251 } else
1252 ops = type->ops;
1253
Patrick McHardy96518512013-10-14 11:00:02 +02001254 info->ops = ops;
1255 return 0;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001256
1257err1:
1258 module_put(type->owner);
1259 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001260}
1261
1262static int nf_tables_newexpr(const struct nft_ctx *ctx,
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001263 const struct nft_expr_info *info,
Patrick McHardy96518512013-10-14 11:00:02 +02001264 struct nft_expr *expr)
1265{
1266 const struct nft_expr_ops *ops = info->ops;
1267 int err;
1268
1269 expr->ops = ops;
1270 if (ops->init) {
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001271 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
Patrick McHardy96518512013-10-14 11:00:02 +02001272 if (err < 0)
1273 goto err1;
1274 }
1275
Patrick McHardy96518512013-10-14 11:00:02 +02001276 return 0;
1277
1278err1:
1279 expr->ops = NULL;
1280 return err;
1281}
1282
Patrick McHardy62472bc2014-03-07 19:08:30 +01001283static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1284 struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +02001285{
1286 if (expr->ops->destroy)
Patrick McHardy62472bc2014-03-07 19:08:30 +01001287 expr->ops->destroy(ctx, expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001288 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001289}
1290
1291/*
1292 * Rules
1293 */
1294
1295static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1296 u64 handle)
1297{
1298 struct nft_rule *rule;
1299
1300 // FIXME: this sucks
1301 list_for_each_entry(rule, &chain->rules, list) {
1302 if (handle == rule->handle)
1303 return rule;
1304 }
1305
1306 return ERR_PTR(-ENOENT);
1307}
1308
1309static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1310 const struct nlattr *nla)
1311{
1312 if (nla == NULL)
1313 return ERR_PTR(-EINVAL);
1314
1315 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1316}
1317
1318static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1319 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1320 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1321 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1322 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1323 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001324 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001325 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001326 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1327 .len = NFT_USERDATA_MAXLEN },
Patrick McHardy96518512013-10-14 11:00:02 +02001328};
1329
1330static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1331 int event, u32 flags, int family,
1332 const struct nft_table *table,
1333 const struct nft_chain *chain,
1334 const struct nft_rule *rule)
1335{
1336 struct nlmsghdr *nlh;
1337 struct nfgenmsg *nfmsg;
1338 const struct nft_expr *expr, *next;
1339 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001340 const struct nft_rule *prule;
1341 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001342
Eric Leblond5e948462013-10-10 13:41:44 +02001343 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001344 flags);
1345 if (nlh == NULL)
1346 goto nla_put_failure;
1347
1348 nfmsg = nlmsg_data(nlh);
1349 nfmsg->nfgen_family = family;
1350 nfmsg->version = NFNETLINK_V0;
1351 nfmsg->res_id = 0;
1352
1353 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1354 goto nla_put_failure;
1355 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1356 goto nla_put_failure;
1357 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1358 goto nla_put_failure;
1359
Eric Leblond5e948462013-10-10 13:41:44 +02001360 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1361 prule = list_entry(rule->list.prev, struct nft_rule, list);
1362 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1363 cpu_to_be64(prule->handle)))
1364 goto nla_put_failure;
1365 }
1366
Patrick McHardy96518512013-10-14 11:00:02 +02001367 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1368 if (list == NULL)
1369 goto nla_put_failure;
1370 nft_rule_for_each_expr(expr, next, rule) {
1371 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1372 if (elem == NULL)
1373 goto nla_put_failure;
1374 if (nf_tables_fill_expr_info(skb, expr) < 0)
1375 goto nla_put_failure;
1376 nla_nest_end(skb, elem);
1377 }
1378 nla_nest_end(skb, list);
1379
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001380 if (rule->ulen &&
1381 nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1382 goto nla_put_failure;
1383
Patrick McHardy96518512013-10-14 11:00:02 +02001384 return nlmsg_end(skb, nlh);
1385
1386nla_put_failure:
1387 nlmsg_trim(skb, nlh);
1388 return -1;
1389}
1390
1391static int nf_tables_rule_notify(const struct sk_buff *oskb,
1392 const struct nlmsghdr *nlh,
1393 const struct nft_table *table,
1394 const struct nft_chain *chain,
1395 const struct nft_rule *rule,
1396 int event, u32 flags, int family)
1397{
1398 struct sk_buff *skb;
1399 u32 portid = NETLINK_CB(oskb).portid;
1400 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1401 u32 seq = nlh->nlmsg_seq;
1402 bool report;
1403 int err;
1404
1405 report = nlmsg_report(nlh);
1406 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1407 return 0;
1408
1409 err = -ENOBUFS;
1410 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1411 if (skb == NULL)
1412 goto err;
1413
1414 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1415 family, table, chain, rule);
1416 if (err < 0) {
1417 kfree_skb(skb);
1418 goto err;
1419 }
1420
1421 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1422 GFP_KERNEL);
1423err:
1424 if (err < 0)
1425 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1426 return err;
1427}
1428
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001429static inline bool
1430nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1431{
1432 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1433}
1434
1435static inline int gencursor_next(struct net *net)
1436{
1437 return net->nft.gencursor+1 == 1 ? 1 : 0;
1438}
1439
1440static inline int
1441nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1442{
1443 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1444}
1445
1446static inline void
1447nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1448{
1449 /* Now inactive, will be active in the future */
1450 rule->genmask = (1 << net->nft.gencursor);
1451}
1452
1453static inline void
1454nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1455{
1456 rule->genmask = (1 << gencursor_next(net));
1457}
1458
1459static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1460{
1461 rule->genmask = 0;
1462}
1463
Patrick McHardy96518512013-10-14 11:00:02 +02001464static int nf_tables_dump_rules(struct sk_buff *skb,
1465 struct netlink_callback *cb)
1466{
1467 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1468 const struct nft_af_info *afi;
1469 const struct nft_table *table;
1470 const struct nft_chain *chain;
1471 const struct nft_rule *rule;
1472 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001473 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001474 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001475 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1476 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
Patrick McHardy96518512013-10-14 11:00:02 +02001477
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001478 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001479 if (family != NFPROTO_UNSPEC && family != afi->family)
1480 continue;
1481
1482 list_for_each_entry(table, &afi->tables, list) {
1483 list_for_each_entry(chain, &table->chains, list) {
1484 list_for_each_entry(rule, &chain->rules, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001485 if (!nft_rule_is_active(net, rule))
1486 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001487 if (idx < s_idx)
1488 goto cont;
1489 if (idx > s_idx)
1490 memset(&cb->args[1], 0,
1491 sizeof(cb->args) - sizeof(cb->args[0]));
1492 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1493 cb->nlh->nlmsg_seq,
1494 NFT_MSG_NEWRULE,
1495 NLM_F_MULTI | NLM_F_APPEND,
1496 afi->family, table, chain, rule) < 0)
1497 goto done;
1498cont:
1499 idx++;
1500 }
1501 }
1502 }
1503 }
1504done:
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001505 /* Invalidate this dump, a transition to the new generation happened */
1506 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1507 return -EBUSY;
1508
Patrick McHardy96518512013-10-14 11:00:02 +02001509 cb->args[0] = idx;
1510 return skb->len;
1511}
1512
1513static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1514 const struct nlmsghdr *nlh,
1515 const struct nlattr * const nla[])
1516{
1517 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1518 const struct nft_af_info *afi;
1519 const struct nft_table *table;
1520 const struct nft_chain *chain;
1521 const struct nft_rule *rule;
1522 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001523 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001524 int family = nfmsg->nfgen_family;
1525 int err;
1526
1527 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1528 struct netlink_dump_control c = {
1529 .dump = nf_tables_dump_rules,
1530 };
1531 return netlink_dump_start(nlsk, skb, nlh, &c);
1532 }
1533
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001534 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001535 if (IS_ERR(afi))
1536 return PTR_ERR(afi);
1537
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001538 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001539 if (IS_ERR(table))
1540 return PTR_ERR(table);
1541
1542 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1543 if (IS_ERR(chain))
1544 return PTR_ERR(chain);
1545
1546 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1547 if (IS_ERR(rule))
1548 return PTR_ERR(rule);
1549
1550 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1551 if (!skb2)
1552 return -ENOMEM;
1553
1554 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1555 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1556 family, table, chain, rule);
1557 if (err < 0)
1558 goto err;
1559
1560 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1561
1562err:
1563 kfree_skb(skb2);
1564 return err;
1565}
1566
Patrick McHardy62472bc2014-03-07 19:08:30 +01001567static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1568 struct nft_rule *rule)
Patrick McHardy96518512013-10-14 11:00:02 +02001569{
Patrick McHardy96518512013-10-14 11:00:02 +02001570 struct nft_expr *expr;
1571
1572 /*
1573 * Careful: some expressions might not be initialized in case this
1574 * is called on error from nf_tables_newrule().
1575 */
1576 expr = nft_expr_first(rule);
1577 while (expr->ops && expr != nft_expr_last(rule)) {
Patrick McHardy62472bc2014-03-07 19:08:30 +01001578 nf_tables_expr_destroy(ctx, expr);
Patrick McHardy96518512013-10-14 11:00:02 +02001579 expr = nft_expr_next(expr);
1580 }
1581 kfree(rule);
1582}
1583
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001584static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001585 struct nft_rule *rule)
1586{
1587 struct nft_trans *trans;
1588
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001589 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001590 if (trans == NULL)
1591 return NULL;
1592
1593 nft_trans_rule(trans) = rule;
1594 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
1595
1596 return trans;
1597}
1598
Patrick McHardy96518512013-10-14 11:00:02 +02001599#define NFT_RULE_MAXEXPRS 128
1600
1601static struct nft_expr_info *info;
1602
1603static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1604 const struct nlmsghdr *nlh,
1605 const struct nlattr * const nla[])
1606{
1607 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001608 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001609 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001610 struct nft_table *table;
1611 struct nft_chain *chain;
1612 struct nft_rule *rule, *old_rule = NULL;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001613 struct nft_trans *trans = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001614 struct nft_expr *expr;
1615 struct nft_ctx ctx;
1616 struct nlattr *tmp;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001617 unsigned int size, i, n, ulen = 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001618 int err, rem;
1619 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02001620 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02001621
1622 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1623
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001624 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02001625 if (IS_ERR(afi))
1626 return PTR_ERR(afi);
1627
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001628 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001629 if (IS_ERR(table))
1630 return PTR_ERR(table);
1631
1632 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1633 if (IS_ERR(chain))
1634 return PTR_ERR(chain);
1635
1636 if (nla[NFTA_RULE_HANDLE]) {
1637 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1638 rule = __nf_tables_rule_lookup(chain, handle);
1639 if (IS_ERR(rule))
1640 return PTR_ERR(rule);
1641
1642 if (nlh->nlmsg_flags & NLM_F_EXCL)
1643 return -EEXIST;
1644 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1645 old_rule = rule;
1646 else
1647 return -EOPNOTSUPP;
1648 } else {
1649 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1650 return -EINVAL;
1651 handle = nf_tables_alloc_handle(table);
1652 }
1653
Eric Leblond5e948462013-10-10 13:41:44 +02001654 if (nla[NFTA_RULE_POSITION]) {
1655 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1656 return -EOPNOTSUPP;
1657
1658 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1659 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1660 if (IS_ERR(old_rule))
1661 return PTR_ERR(old_rule);
1662 }
1663
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001664 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1665
Patrick McHardy96518512013-10-14 11:00:02 +02001666 n = 0;
1667 size = 0;
1668 if (nla[NFTA_RULE_EXPRESSIONS]) {
1669 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1670 err = -EINVAL;
1671 if (nla_type(tmp) != NFTA_LIST_ELEM)
1672 goto err1;
1673 if (n == NFT_RULE_MAXEXPRS)
1674 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001675 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02001676 if (err < 0)
1677 goto err1;
1678 size += info[n].ops->size;
1679 n++;
1680 }
1681 }
1682
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001683 if (nla[NFTA_RULE_USERDATA])
1684 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1685
Patrick McHardy96518512013-10-14 11:00:02 +02001686 err = -ENOMEM;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001687 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001688 if (rule == NULL)
1689 goto err1;
1690
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001691 nft_rule_activate_next(net, rule);
1692
Patrick McHardy96518512013-10-14 11:00:02 +02001693 rule->handle = handle;
1694 rule->dlen = size;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001695 rule->ulen = ulen;
1696
1697 if (ulen)
1698 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
Patrick McHardy96518512013-10-14 11:00:02 +02001699
Patrick McHardy96518512013-10-14 11:00:02 +02001700 expr = nft_expr_first(rule);
1701 for (i = 0; i < n; i++) {
1702 err = nf_tables_newexpr(&ctx, &info[i], expr);
1703 if (err < 0)
1704 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001705 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001706 expr = nft_expr_next(expr);
1707 }
1708
Patrick McHardy96518512013-10-14 11:00:02 +02001709 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001710 if (nft_rule_is_active_next(net, old_rule)) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001711 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE,
1712 old_rule);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001713 if (trans == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001714 err = -ENOMEM;
1715 goto err2;
1716 }
1717 nft_rule_disactivate_next(net, old_rule);
1718 list_add_tail(&rule->list, &old_rule->list);
1719 } else {
1720 err = -ENOENT;
1721 goto err2;
1722 }
Patrick McHardy96518512013-10-14 11:00:02 +02001723 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02001724 if (old_rule)
1725 list_add_rcu(&rule->list, &old_rule->list);
1726 else
1727 list_add_tail_rcu(&rule->list, &chain->rules);
1728 else {
1729 if (old_rule)
1730 list_add_tail_rcu(&rule->list, &old_rule->list);
1731 else
1732 list_add_rcu(&rule->list, &chain->rules);
1733 }
Patrick McHardy96518512013-10-14 11:00:02 +02001734
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001735 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001736 err = -ENOMEM;
1737 goto err3;
1738 }
Patrick McHardy96518512013-10-14 11:00:02 +02001739 return 0;
1740
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001741err3:
1742 list_del_rcu(&rule->list);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001743 if (trans) {
1744 list_del_rcu(&nft_trans_rule(trans)->list);
1745 nft_rule_clear(net, nft_trans_rule(trans));
1746 nft_trans_destroy(trans);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001747 }
Patrick McHardy96518512013-10-14 11:00:02 +02001748err2:
Patrick McHardy62472bc2014-03-07 19:08:30 +01001749 nf_tables_rule_destroy(&ctx, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001750err1:
1751 for (i = 0; i < n; i++) {
1752 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001753 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001754 }
1755 return err;
1756}
1757
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001758static int
1759nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1760{
1761 /* You cannot delete the same rule twice */
1762 if (nft_rule_is_active_next(ctx->net, rule)) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001763 if (nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule) == NULL)
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001764 return -ENOMEM;
1765 nft_rule_disactivate_next(ctx->net, rule);
1766 return 0;
1767 }
1768 return -ENOENT;
1769}
1770
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001771static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1772{
1773 struct nft_rule *rule;
1774 int err;
1775
1776 list_for_each_entry(rule, &ctx->chain->rules, list) {
1777 err = nf_tables_delrule_one(ctx, rule);
1778 if (err < 0)
1779 return err;
1780 }
1781 return 0;
1782}
1783
Patrick McHardy96518512013-10-14 11:00:02 +02001784static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1785 const struct nlmsghdr *nlh,
1786 const struct nlattr * const nla[])
1787{
1788 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001789 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001790 struct net *net = sock_net(skb->sk);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001791 struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001792 struct nft_chain *chain = NULL;
1793 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001794 int family = nfmsg->nfgen_family, err = 0;
1795 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001796
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001797 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001798 if (IS_ERR(afi))
1799 return PTR_ERR(afi);
1800
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001801 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001802 if (IS_ERR(table))
1803 return PTR_ERR(table);
1804
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001805 if (nla[NFTA_RULE_CHAIN]) {
1806 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1807 if (IS_ERR(chain))
1808 return PTR_ERR(chain);
1809 }
Patrick McHardy96518512013-10-14 11:00:02 +02001810
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001811 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1812
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001813 if (chain) {
1814 if (nla[NFTA_RULE_HANDLE]) {
1815 rule = nf_tables_rule_lookup(chain,
1816 nla[NFTA_RULE_HANDLE]);
1817 if (IS_ERR(rule))
1818 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001819
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001820 err = nf_tables_delrule_one(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001821 } else {
1822 err = nf_table_delrule_by_chain(&ctx);
1823 }
1824 } else {
1825 list_for_each_entry(chain, &table->chains, list) {
1826 ctx.chain = chain;
1827 err = nf_table_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001828 if (err < 0)
1829 break;
Patrick McHardy96518512013-10-14 11:00:02 +02001830 }
1831 }
1832
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001833 return err;
1834}
1835
Patrick McHardy20a69342013-10-11 12:06:22 +02001836/*
1837 * Sets
1838 */
1839
1840static LIST_HEAD(nf_tables_set_ops);
1841
1842int nft_register_set(struct nft_set_ops *ops)
1843{
1844 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1845 list_add_tail(&ops->list, &nf_tables_set_ops);
1846 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1847 return 0;
1848}
1849EXPORT_SYMBOL_GPL(nft_register_set);
1850
1851void nft_unregister_set(struct nft_set_ops *ops)
1852{
1853 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1854 list_del(&ops->list);
1855 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1856}
1857EXPORT_SYMBOL_GPL(nft_unregister_set);
1858
Patrick McHardyc50b9602014-03-28 10:19:47 +00001859/*
1860 * Select a set implementation based on the data characteristics and the
1861 * given policy. The total memory use might not be known if no size is
1862 * given, in that case the amount of memory per element is used.
1863 */
1864static const struct nft_set_ops *
1865nft_select_set_ops(const struct nlattr * const nla[],
1866 const struct nft_set_desc *desc,
1867 enum nft_set_policies policy)
Patrick McHardy20a69342013-10-11 12:06:22 +02001868{
Patrick McHardyc50b9602014-03-28 10:19:47 +00001869 const struct nft_set_ops *ops, *bops;
1870 struct nft_set_estimate est, best;
Patrick McHardy20a69342013-10-11 12:06:22 +02001871 u32 features;
1872
1873#ifdef CONFIG_MODULES
1874 if (list_empty(&nf_tables_set_ops)) {
1875 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1876 request_module("nft-set");
1877 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1878 if (!list_empty(&nf_tables_set_ops))
1879 return ERR_PTR(-EAGAIN);
1880 }
1881#endif
1882 features = 0;
1883 if (nla[NFTA_SET_FLAGS] != NULL) {
1884 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1885 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1886 }
1887
Patrick McHardyc50b9602014-03-28 10:19:47 +00001888 bops = NULL;
1889 best.size = ~0;
1890 best.class = ~0;
1891
Patrick McHardy20a69342013-10-11 12:06:22 +02001892 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1893 if ((ops->features & features) != features)
1894 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00001895 if (!ops->estimate(desc, features, &est))
1896 continue;
1897
1898 switch (policy) {
1899 case NFT_SET_POL_PERFORMANCE:
1900 if (est.class < best.class)
1901 break;
1902 if (est.class == best.class && est.size < best.size)
1903 break;
1904 continue;
1905 case NFT_SET_POL_MEMORY:
1906 if (est.size < best.size)
1907 break;
1908 if (est.size == best.size && est.class < best.class)
1909 break;
1910 continue;
1911 default:
1912 break;
1913 }
1914
Patrick McHardy20a69342013-10-11 12:06:22 +02001915 if (!try_module_get(ops->owner))
1916 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00001917 if (bops != NULL)
1918 module_put(bops->owner);
1919
1920 bops = ops;
1921 best = est;
Patrick McHardy20a69342013-10-11 12:06:22 +02001922 }
1923
Patrick McHardyc50b9602014-03-28 10:19:47 +00001924 if (bops != NULL)
1925 return bops;
1926
Patrick McHardy20a69342013-10-11 12:06:22 +02001927 return ERR_PTR(-EOPNOTSUPP);
1928}
1929
1930static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1931 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1932 [NFTA_SET_NAME] = { .type = NLA_STRING },
1933 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1934 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1935 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1936 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1937 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00001938 [NFTA_SET_POLICY] = { .type = NLA_U32 },
1939 [NFTA_SET_DESC] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02001940 [NFTA_SET_ID] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00001941};
1942
1943static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
1944 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02001945};
1946
1947static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1948 const struct sk_buff *skb,
1949 const struct nlmsghdr *nlh,
1950 const struct nlattr * const nla[])
1951{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001952 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02001953 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001954 struct nft_af_info *afi = NULL;
1955 struct nft_table *table = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02001956
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01001957 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
1958 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1959 if (IS_ERR(afi))
1960 return PTR_ERR(afi);
1961 }
Patrick McHardy20a69342013-10-11 12:06:22 +02001962
1963 if (nla[NFTA_SET_TABLE] != NULL) {
Patrick McHardyec2c9932014-02-05 15:03:35 +00001964 if (afi == NULL)
1965 return -EAFNOSUPPORT;
1966
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001967 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02001968 if (IS_ERR(table))
1969 return PTR_ERR(table);
1970 }
1971
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001972 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02001973 return 0;
1974}
1975
1976struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1977 const struct nlattr *nla)
1978{
1979 struct nft_set *set;
1980
1981 if (nla == NULL)
1982 return ERR_PTR(-EINVAL);
1983
1984 list_for_each_entry(set, &table->sets, list) {
1985 if (!nla_strcmp(nla, set->name))
1986 return set;
1987 }
1988 return ERR_PTR(-ENOENT);
1989}
1990
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02001991struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
1992 const struct nlattr *nla)
1993{
1994 struct nft_trans *trans;
1995 u32 id = ntohl(nla_get_be32(nla));
1996
1997 list_for_each_entry(trans, &net->nft.commit_list, list) {
1998 if (trans->msg_type == NFT_MSG_NEWSET &&
1999 id == nft_trans_set_id(trans))
2000 return nft_trans_set(trans);
2001 }
2002 return ERR_PTR(-ENOENT);
2003}
2004
Patrick McHardy20a69342013-10-11 12:06:22 +02002005static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2006 const char *name)
2007{
2008 const struct nft_set *i;
2009 const char *p;
2010 unsigned long *inuse;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002011 unsigned int n = 0, min = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002012
2013 p = strnchr(name, IFNAMSIZ, '%');
2014 if (p != NULL) {
2015 if (p[1] != 'd' || strchr(p + 2, '%'))
2016 return -EINVAL;
2017
2018 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2019 if (inuse == NULL)
2020 return -ENOMEM;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002021cont:
Patrick McHardy20a69342013-10-11 12:06:22 +02002022 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01002023 int tmp;
2024
2025 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02002026 continue;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002027 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02002028 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002029
Patrick McHardy60eb1892014-03-07 12:34:05 +01002030 set_bit(tmp - min, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02002031 }
2032
Patrick McHardy53b70282014-02-05 12:26:22 +01002033 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
Patrick McHardy60eb1892014-03-07 12:34:05 +01002034 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2035 min += BITS_PER_BYTE * PAGE_SIZE;
2036 memset(inuse, 0, PAGE_SIZE);
2037 goto cont;
2038 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002039 free_page((unsigned long)inuse);
2040 }
2041
Patrick McHardy60eb1892014-03-07 12:34:05 +01002042 snprintf(set->name, sizeof(set->name), name, min + n);
Patrick McHardy20a69342013-10-11 12:06:22 +02002043 list_for_each_entry(i, &ctx->table->sets, list) {
2044 if (!strcmp(set->name, i->name))
2045 return -ENFILE;
2046 }
2047 return 0;
2048}
2049
2050static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2051 const struct nft_set *set, u16 event, u16 flags)
2052{
2053 struct nfgenmsg *nfmsg;
2054 struct nlmsghdr *nlh;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002055 struct nlattr *desc;
Patrick McHardy20a69342013-10-11 12:06:22 +02002056 u32 portid = NETLINK_CB(ctx->skb).portid;
2057 u32 seq = ctx->nlh->nlmsg_seq;
2058
2059 event |= NFNL_SUBSYS_NFTABLES << 8;
2060 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2061 flags);
2062 if (nlh == NULL)
2063 goto nla_put_failure;
2064
2065 nfmsg = nlmsg_data(nlh);
2066 nfmsg->nfgen_family = ctx->afi->family;
2067 nfmsg->version = NFNETLINK_V0;
2068 nfmsg->res_id = 0;
2069
2070 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2071 goto nla_put_failure;
2072 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2073 goto nla_put_failure;
2074 if (set->flags != 0)
2075 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2076 goto nla_put_failure;
2077
2078 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2079 goto nla_put_failure;
2080 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2081 goto nla_put_failure;
2082 if (set->flags & NFT_SET_MAP) {
2083 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2084 goto nla_put_failure;
2085 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2086 goto nla_put_failure;
2087 }
2088
Patrick McHardyc50b9602014-03-28 10:19:47 +00002089 desc = nla_nest_start(skb, NFTA_SET_DESC);
2090 if (desc == NULL)
2091 goto nla_put_failure;
2092 if (set->size &&
2093 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2094 goto nla_put_failure;
2095 nla_nest_end(skb, desc);
2096
Patrick McHardy20a69342013-10-11 12:06:22 +02002097 return nlmsg_end(skb, nlh);
2098
2099nla_put_failure:
2100 nlmsg_trim(skb, nlh);
2101 return -1;
2102}
2103
2104static int nf_tables_set_notify(const struct nft_ctx *ctx,
2105 const struct nft_set *set,
2106 int event)
2107{
2108 struct sk_buff *skb;
2109 u32 portid = NETLINK_CB(ctx->skb).portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002110 bool report;
2111 int err;
2112
2113 report = nlmsg_report(ctx->nlh);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002114 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002115 return 0;
2116
2117 err = -ENOBUFS;
2118 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2119 if (skb == NULL)
2120 goto err;
2121
2122 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2123 if (err < 0) {
2124 kfree_skb(skb);
2125 goto err;
2126 }
2127
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002128 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
Patrick McHardy20a69342013-10-11 12:06:22 +02002129 GFP_KERNEL);
2130err:
2131 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002132 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002133 return err;
2134}
2135
2136static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2137 struct netlink_callback *cb)
2138{
2139 const struct nft_set *set;
2140 unsigned int idx = 0, s_idx = cb->args[0];
2141
2142 if (cb->args[1])
2143 return skb->len;
2144
2145 list_for_each_entry(set, &ctx->table->sets, list) {
2146 if (idx < s_idx)
2147 goto cont;
2148 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2149 NLM_F_MULTI) < 0) {
2150 cb->args[0] = idx;
2151 goto done;
2152 }
2153cont:
2154 idx++;
2155 }
2156 cb->args[1] = 1;
2157done:
2158 return skb->len;
2159}
2160
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002161static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2162 struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002163{
2164 const struct nft_set *set;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002165 unsigned int idx, s_idx = cb->args[0];
Patrick McHardy20a69342013-10-11 12:06:22 +02002166 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2167
2168 if (cb->args[1])
2169 return skb->len;
2170
2171 list_for_each_entry(table, &ctx->afi->tables, list) {
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002172 if (cur_table) {
2173 if (cur_table != table)
2174 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02002175
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002176 cur_table = NULL;
2177 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002178 ctx->table = table;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002179 idx = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002180 list_for_each_entry(set, &ctx->table->sets, list) {
2181 if (idx < s_idx)
2182 goto cont;
2183 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2184 NLM_F_MULTI) < 0) {
2185 cb->args[0] = idx;
2186 cb->args[2] = (unsigned long) table;
2187 goto done;
2188 }
2189cont:
2190 idx++;
2191 }
2192 }
2193 cb->args[1] = 1;
2194done:
2195 return skb->len;
2196}
2197
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002198static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2199 struct netlink_callback *cb)
2200{
2201 const struct nft_set *set;
2202 unsigned int idx, s_idx = cb->args[0];
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002203 struct nft_af_info *afi;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002204 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2205 struct net *net = sock_net(skb->sk);
2206 int cur_family = cb->args[3];
2207
2208 if (cb->args[1])
2209 return skb->len;
2210
2211 list_for_each_entry(afi, &net->nft.af_info, list) {
2212 if (cur_family) {
2213 if (afi->family != cur_family)
2214 continue;
2215
2216 cur_family = 0;
2217 }
2218
2219 list_for_each_entry(table, &afi->tables, list) {
2220 if (cur_table) {
2221 if (cur_table != table)
2222 continue;
2223
2224 cur_table = NULL;
2225 }
2226
2227 ctx->table = table;
2228 ctx->afi = afi;
2229 idx = 0;
2230 list_for_each_entry(set, &ctx->table->sets, list) {
2231 if (idx < s_idx)
2232 goto cont;
2233 if (nf_tables_fill_set(skb, ctx, set,
2234 NFT_MSG_NEWSET,
2235 NLM_F_MULTI) < 0) {
2236 cb->args[0] = idx;
2237 cb->args[2] = (unsigned long) table;
2238 cb->args[3] = afi->family;
2239 goto done;
2240 }
2241cont:
2242 idx++;
2243 }
2244 if (s_idx)
2245 s_idx = 0;
2246 }
2247 }
2248 cb->args[1] = 1;
2249done:
2250 return skb->len;
2251}
2252
Patrick McHardy20a69342013-10-11 12:06:22 +02002253static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2254{
2255 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2256 struct nlattr *nla[NFTA_SET_MAX + 1];
2257 struct nft_ctx ctx;
2258 int err, ret;
2259
2260 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2261 nft_set_policy);
2262 if (err < 0)
2263 return err;
2264
2265 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2266 if (err < 0)
2267 return err;
2268
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002269 if (ctx.table == NULL) {
2270 if (ctx.afi == NULL)
2271 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2272 else
2273 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2274 } else
Patrick McHardy20a69342013-10-11 12:06:22 +02002275 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2276
2277 return ret;
2278}
2279
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002280#define NFT_SET_INACTIVE (1 << 15) /* Internal set flag */
2281
Patrick McHardy20a69342013-10-11 12:06:22 +02002282static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2283 const struct nlmsghdr *nlh,
2284 const struct nlattr * const nla[])
2285{
2286 const struct nft_set *set;
2287 struct nft_ctx ctx;
2288 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002289 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002290 int err;
2291
2292 /* Verify existance before starting dump */
2293 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2294 if (err < 0)
2295 return err;
2296
2297 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2298 struct netlink_dump_control c = {
2299 .dump = nf_tables_dump_sets,
2300 };
2301 return netlink_dump_start(nlsk, skb, nlh, &c);
2302 }
2303
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002304 /* Only accept unspec with dump */
2305 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2306 return -EAFNOSUPPORT;
2307
Patrick McHardy20a69342013-10-11 12:06:22 +02002308 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2309 if (IS_ERR(set))
2310 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002311 if (set->flags & NFT_SET_INACTIVE)
2312 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002313
2314 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2315 if (skb2 == NULL)
2316 return -ENOMEM;
2317
2318 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2319 if (err < 0)
2320 goto err;
2321
2322 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2323
2324err:
2325 kfree_skb(skb2);
2326 return err;
2327}
2328
Patrick McHardyc50b9602014-03-28 10:19:47 +00002329static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2330 struct nft_set_desc *desc,
2331 const struct nlattr *nla)
2332{
2333 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2334 int err;
2335
2336 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2337 if (err < 0)
2338 return err;
2339
2340 if (da[NFTA_SET_DESC_SIZE] != NULL)
2341 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2342
2343 return 0;
2344}
2345
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002346static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
2347 struct nft_set *set)
2348{
2349 struct nft_trans *trans;
2350
2351 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
2352 if (trans == NULL)
2353 return -ENOMEM;
2354
2355 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
2356 nft_trans_set_id(trans) =
2357 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
2358 set->flags |= NFT_SET_INACTIVE;
2359 }
2360 nft_trans_set(trans) = set;
2361 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
2362
2363 return 0;
2364}
2365
Patrick McHardy20a69342013-10-11 12:06:22 +02002366static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2367 const struct nlmsghdr *nlh,
2368 const struct nlattr * const nla[])
2369{
2370 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2371 const struct nft_set_ops *ops;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002372 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002373 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002374 struct nft_table *table;
2375 struct nft_set *set;
2376 struct nft_ctx ctx;
2377 char name[IFNAMSIZ];
2378 unsigned int size;
2379 bool create;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002380 u32 ktype, dtype, flags, policy;
2381 struct nft_set_desc desc;
Patrick McHardy20a69342013-10-11 12:06:22 +02002382 int err;
2383
2384 if (nla[NFTA_SET_TABLE] == NULL ||
2385 nla[NFTA_SET_NAME] == NULL ||
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002386 nla[NFTA_SET_KEY_LEN] == NULL ||
2387 nla[NFTA_SET_ID] == NULL)
Patrick McHardy20a69342013-10-11 12:06:22 +02002388 return -EINVAL;
2389
Patrick McHardyc50b9602014-03-28 10:19:47 +00002390 memset(&desc, 0, sizeof(desc));
2391
Patrick McHardy20a69342013-10-11 12:06:22 +02002392 ktype = NFT_DATA_VALUE;
2393 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2394 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2395 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2396 return -EINVAL;
2397 }
2398
Patrick McHardyc50b9602014-03-28 10:19:47 +00002399 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2400 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002401 return -EINVAL;
2402
2403 flags = 0;
2404 if (nla[NFTA_SET_FLAGS] != NULL) {
2405 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2406 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2407 NFT_SET_INTERVAL | NFT_SET_MAP))
2408 return -EINVAL;
2409 }
2410
2411 dtype = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002412 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2413 if (!(flags & NFT_SET_MAP))
2414 return -EINVAL;
2415
2416 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2417 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2418 dtype != NFT_DATA_VERDICT)
2419 return -EINVAL;
2420
2421 if (dtype != NFT_DATA_VERDICT) {
2422 if (nla[NFTA_SET_DATA_LEN] == NULL)
2423 return -EINVAL;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002424 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2425 if (desc.dlen == 0 ||
2426 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002427 return -EINVAL;
2428 } else
Patrick McHardyc50b9602014-03-28 10:19:47 +00002429 desc.dlen = sizeof(struct nft_data);
Patrick McHardy20a69342013-10-11 12:06:22 +02002430 } else if (flags & NFT_SET_MAP)
2431 return -EINVAL;
2432
Patrick McHardyc50b9602014-03-28 10:19:47 +00002433 policy = NFT_SET_POL_PERFORMANCE;
2434 if (nla[NFTA_SET_POLICY] != NULL)
2435 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2436
2437 if (nla[NFTA_SET_DESC] != NULL) {
2438 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2439 if (err < 0)
2440 return err;
2441 }
2442
Patrick McHardy20a69342013-10-11 12:06:22 +02002443 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2444
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002445 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002446 if (IS_ERR(afi))
2447 return PTR_ERR(afi);
2448
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002449 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002450 if (IS_ERR(table))
2451 return PTR_ERR(table);
2452
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002453 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002454
2455 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2456 if (IS_ERR(set)) {
2457 if (PTR_ERR(set) != -ENOENT)
2458 return PTR_ERR(set);
2459 set = NULL;
2460 }
2461
2462 if (set != NULL) {
2463 if (nlh->nlmsg_flags & NLM_F_EXCL)
2464 return -EEXIST;
2465 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2466 return -EOPNOTSUPP;
2467 return 0;
2468 }
2469
2470 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2471 return -ENOENT;
2472
Patrick McHardyc50b9602014-03-28 10:19:47 +00002473 ops = nft_select_set_ops(nla, &desc, policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002474 if (IS_ERR(ops))
2475 return PTR_ERR(ops);
2476
2477 size = 0;
2478 if (ops->privsize != NULL)
2479 size = ops->privsize(nla);
2480
2481 err = -ENOMEM;
2482 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2483 if (set == NULL)
2484 goto err1;
2485
2486 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2487 err = nf_tables_set_alloc_name(&ctx, set, name);
2488 if (err < 0)
2489 goto err2;
2490
2491 INIT_LIST_HEAD(&set->bindings);
2492 set->ops = ops;
2493 set->ktype = ktype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002494 set->klen = desc.klen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002495 set->dtype = dtype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002496 set->dlen = desc.dlen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002497 set->flags = flags;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002498 set->size = desc.size;
Patrick McHardy20a69342013-10-11 12:06:22 +02002499
Patrick McHardyc50b9602014-03-28 10:19:47 +00002500 err = ops->init(set, &desc, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002501 if (err < 0)
2502 goto err2;
2503
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002504 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
2505 if (err < 0)
2506 goto err2;
2507
Patrick McHardy20a69342013-10-11 12:06:22 +02002508 list_add_tail(&set->list, &table->sets);
Patrick McHardy20a69342013-10-11 12:06:22 +02002509 return 0;
2510
2511err2:
2512 kfree(set);
2513err1:
2514 module_put(ops->owner);
2515 return err;
2516}
2517
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002518static void nft_set_destroy(struct nft_set *set)
2519{
2520 set->ops->destroy(set);
2521 module_put(set->ops->owner);
2522 kfree(set);
2523}
2524
Patrick McHardy20a69342013-10-11 12:06:22 +02002525static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2526{
2527 list_del(&set->list);
Patrick McHardyab9da5c12014-03-07 19:08:31 +01002528 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002529 nft_set_destroy(set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002530}
2531
2532static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2533 const struct nlmsghdr *nlh,
2534 const struct nlattr * const nla[])
2535{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002536 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002537 struct nft_set *set;
2538 struct nft_ctx ctx;
2539 int err;
2540
Patrick McHardyec2c9932014-02-05 15:03:35 +00002541 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2542 return -EAFNOSUPPORT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002543 if (nla[NFTA_SET_TABLE] == NULL)
2544 return -EINVAL;
2545
2546 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2547 if (err < 0)
2548 return err;
2549
2550 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2551 if (IS_ERR(set))
2552 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002553 if (set->flags & NFT_SET_INACTIVE)
2554 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002555 if (!list_empty(&set->bindings))
2556 return -EBUSY;
2557
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002558 err = nft_trans_set_add(&ctx, NFT_MSG_DELSET, set);
2559 if (err < 0)
2560 return err;
2561
2562 list_del(&set->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002563 return 0;
2564}
2565
2566static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2567 const struct nft_set *set,
2568 const struct nft_set_iter *iter,
2569 const struct nft_set_elem *elem)
2570{
2571 enum nft_registers dreg;
2572
2573 dreg = nft_type_to_reg(set->dtype);
Pablo Neira Ayuso2ee0d3c2013-12-28 00:59:38 +01002574 return nft_validate_data_load(ctx, dreg, &elem->data,
2575 set->dtype == NFT_DATA_VERDICT ?
2576 NFT_DATA_VERDICT : NFT_DATA_VALUE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002577}
2578
2579int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2580 struct nft_set_binding *binding)
2581{
2582 struct nft_set_binding *i;
2583 struct nft_set_iter iter;
2584
2585 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2586 return -EBUSY;
2587
2588 if (set->flags & NFT_SET_MAP) {
2589 /* If the set is already bound to the same chain all
2590 * jumps are already validated for that chain.
2591 */
2592 list_for_each_entry(i, &set->bindings, list) {
2593 if (i->chain == binding->chain)
2594 goto bind;
2595 }
2596
2597 iter.skip = 0;
2598 iter.count = 0;
2599 iter.err = 0;
2600 iter.fn = nf_tables_bind_check_setelem;
2601
2602 set->ops->walk(ctx, set, &iter);
2603 if (iter.err < 0) {
2604 /* Destroy anonymous sets if binding fails */
2605 if (set->flags & NFT_SET_ANONYMOUS)
2606 nf_tables_set_destroy(ctx, set);
2607
2608 return iter.err;
2609 }
2610 }
2611bind:
2612 binding->chain = ctx->chain;
2613 list_add_tail(&binding->list, &set->bindings);
2614 return 0;
2615}
2616
2617void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2618 struct nft_set_binding *binding)
2619{
2620 list_del(&binding->list);
2621
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002622 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2623 !(set->flags & NFT_SET_INACTIVE))
Patrick McHardy20a69342013-10-11 12:06:22 +02002624 nf_tables_set_destroy(ctx, set);
2625}
2626
2627/*
2628 * Set elements
2629 */
2630
2631static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2632 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2633 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2634 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2635};
2636
2637static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2638 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2639 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2640 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002641 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002642};
2643
2644static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2645 const struct sk_buff *skb,
2646 const struct nlmsghdr *nlh,
2647 const struct nlattr * const nla[])
2648{
2649 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002650 struct nft_af_info *afi;
2651 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002652 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002653
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002654 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002655 if (IS_ERR(afi))
2656 return PTR_ERR(afi);
2657
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002658 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002659 if (IS_ERR(table))
2660 return PTR_ERR(table);
2661
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002662 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002663 return 0;
2664}
2665
2666static int nf_tables_fill_setelem(struct sk_buff *skb,
2667 const struct nft_set *set,
2668 const struct nft_set_elem *elem)
2669{
2670 unsigned char *b = skb_tail_pointer(skb);
2671 struct nlattr *nest;
2672
2673 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2674 if (nest == NULL)
2675 goto nla_put_failure;
2676
2677 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2678 set->klen) < 0)
2679 goto nla_put_failure;
2680
2681 if (set->flags & NFT_SET_MAP &&
2682 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2683 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2684 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2685 set->dlen) < 0)
2686 goto nla_put_failure;
2687
2688 if (elem->flags != 0)
2689 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2690 goto nla_put_failure;
2691
2692 nla_nest_end(skb, nest);
2693 return 0;
2694
2695nla_put_failure:
2696 nlmsg_trim(skb, b);
2697 return -EMSGSIZE;
2698}
2699
2700struct nft_set_dump_args {
2701 const struct netlink_callback *cb;
2702 struct nft_set_iter iter;
2703 struct sk_buff *skb;
2704};
2705
2706static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2707 const struct nft_set *set,
2708 const struct nft_set_iter *iter,
2709 const struct nft_set_elem *elem)
2710{
2711 struct nft_set_dump_args *args;
2712
2713 args = container_of(iter, struct nft_set_dump_args, iter);
2714 return nf_tables_fill_setelem(args->skb, set, elem);
2715}
2716
2717static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2718{
2719 const struct nft_set *set;
2720 struct nft_set_dump_args args;
2721 struct nft_ctx ctx;
2722 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2723 struct nfgenmsg *nfmsg;
2724 struct nlmsghdr *nlh;
2725 struct nlattr *nest;
2726 u32 portid, seq;
2727 int event, err;
2728
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01002729 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2730 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002731 if (err < 0)
2732 return err;
2733
2734 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2735 if (err < 0)
2736 return err;
2737
2738 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2739 if (IS_ERR(set))
2740 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002741 if (set->flags & NFT_SET_INACTIVE)
2742 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002743
2744 event = NFT_MSG_NEWSETELEM;
2745 event |= NFNL_SUBSYS_NFTABLES << 8;
2746 portid = NETLINK_CB(cb->skb).portid;
2747 seq = cb->nlh->nlmsg_seq;
2748
2749 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2750 NLM_F_MULTI);
2751 if (nlh == NULL)
2752 goto nla_put_failure;
2753
2754 nfmsg = nlmsg_data(nlh);
2755 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2756 nfmsg->version = NFNETLINK_V0;
2757 nfmsg->res_id = 0;
2758
2759 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2760 goto nla_put_failure;
2761 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2762 goto nla_put_failure;
2763
2764 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2765 if (nest == NULL)
2766 goto nla_put_failure;
2767
2768 args.cb = cb;
2769 args.skb = skb;
2770 args.iter.skip = cb->args[0];
2771 args.iter.count = 0;
2772 args.iter.err = 0;
2773 args.iter.fn = nf_tables_dump_setelem;
2774 set->ops->walk(&ctx, set, &args.iter);
2775
2776 nla_nest_end(skb, nest);
2777 nlmsg_end(skb, nlh);
2778
2779 if (args.iter.err && args.iter.err != -EMSGSIZE)
2780 return args.iter.err;
2781 if (args.iter.count == cb->args[0])
2782 return 0;
2783
2784 cb->args[0] = args.iter.count;
2785 return skb->len;
2786
2787nla_put_failure:
2788 return -ENOSPC;
2789}
2790
2791static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2792 const struct nlmsghdr *nlh,
2793 const struct nlattr * const nla[])
2794{
2795 const struct nft_set *set;
2796 struct nft_ctx ctx;
2797 int err;
2798
2799 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2800 if (err < 0)
2801 return err;
2802
2803 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2804 if (IS_ERR(set))
2805 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002806 if (set->flags & NFT_SET_INACTIVE)
2807 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002808
2809 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2810 struct netlink_dump_control c = {
2811 .dump = nf_tables_dump_set,
2812 };
2813 return netlink_dump_start(nlsk, skb, nlh, &c);
2814 }
2815 return -EOPNOTSUPP;
2816}
2817
Arturo Borrerod60ce622014-04-01 14:06:07 +02002818static int nf_tables_fill_setelem_info(struct sk_buff *skb,
2819 const struct nft_ctx *ctx, u32 seq,
2820 u32 portid, int event, u16 flags,
2821 const struct nft_set *set,
2822 const struct nft_set_elem *elem)
2823{
2824 struct nfgenmsg *nfmsg;
2825 struct nlmsghdr *nlh;
2826 struct nlattr *nest;
2827 int err;
2828
2829 event |= NFNL_SUBSYS_NFTABLES << 8;
2830 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2831 flags);
2832 if (nlh == NULL)
2833 goto nla_put_failure;
2834
2835 nfmsg = nlmsg_data(nlh);
2836 nfmsg->nfgen_family = ctx->afi->family;
2837 nfmsg->version = NFNETLINK_V0;
2838 nfmsg->res_id = 0;
2839
2840 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2841 goto nla_put_failure;
2842 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2843 goto nla_put_failure;
2844
2845 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2846 if (nest == NULL)
2847 goto nla_put_failure;
2848
2849 err = nf_tables_fill_setelem(skb, set, elem);
2850 if (err < 0)
2851 goto nla_put_failure;
2852
2853 nla_nest_end(skb, nest);
2854
2855 return nlmsg_end(skb, nlh);
2856
2857nla_put_failure:
2858 nlmsg_trim(skb, nlh);
2859 return -1;
2860}
2861
2862static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
2863 const struct nft_set *set,
2864 const struct nft_set_elem *elem,
2865 int event, u16 flags)
2866{
2867 const struct sk_buff *oskb = ctx->skb;
2868 struct net *net = sock_net(oskb->sk);
2869 u32 portid = NETLINK_CB(oskb).portid;
2870 bool report = nlmsg_report(ctx->nlh);
2871 struct sk_buff *skb;
2872 int err;
2873
2874 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
2875 return 0;
2876
2877 err = -ENOBUFS;
2878 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2879 if (skb == NULL)
2880 goto err;
2881
2882 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
2883 set, elem);
2884 if (err < 0) {
2885 kfree_skb(skb);
2886 goto err;
2887 }
2888
2889 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
2890 GFP_KERNEL);
2891err:
2892 if (err < 0)
2893 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
2894 return err;
2895}
2896
Patrick McHardy20a69342013-10-11 12:06:22 +02002897static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2898 const struct nlattr *attr)
2899{
2900 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2901 struct nft_data_desc d1, d2;
2902 struct nft_set_elem elem;
2903 struct nft_set_binding *binding;
2904 enum nft_registers dreg;
2905 int err;
2906
Patrick McHardyc50b9602014-03-28 10:19:47 +00002907 if (set->size && set->nelems == set->size)
2908 return -ENFILE;
2909
Patrick McHardy20a69342013-10-11 12:06:22 +02002910 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2911 nft_set_elem_policy);
2912 if (err < 0)
2913 return err;
2914
2915 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2916 return -EINVAL;
2917
2918 elem.flags = 0;
2919 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2920 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2921 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2922 return -EINVAL;
2923 }
2924
2925 if (set->flags & NFT_SET_MAP) {
2926 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2927 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2928 return -EINVAL;
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01002929 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
2930 elem.flags & NFT_SET_ELEM_INTERVAL_END)
2931 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02002932 } else {
2933 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2934 return -EINVAL;
2935 }
2936
2937 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2938 if (err < 0)
2939 goto err1;
2940 err = -EINVAL;
2941 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2942 goto err2;
2943
2944 err = -EEXIST;
2945 if (set->ops->get(set, &elem) == 0)
2946 goto err2;
2947
2948 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2949 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2950 if (err < 0)
2951 goto err2;
2952
2953 err = -EINVAL;
2954 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2955 goto err3;
2956
2957 dreg = nft_type_to_reg(set->dtype);
2958 list_for_each_entry(binding, &set->bindings, list) {
2959 struct nft_ctx bind_ctx = {
2960 .afi = ctx->afi,
2961 .table = ctx->table,
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002962 .chain = (struct nft_chain *)binding->chain,
Patrick McHardy20a69342013-10-11 12:06:22 +02002963 };
2964
2965 err = nft_validate_data_load(&bind_ctx, dreg,
2966 &elem.data, d2.type);
2967 if (err < 0)
2968 goto err3;
2969 }
2970 }
2971
2972 err = set->ops->insert(set, &elem);
2973 if (err < 0)
2974 goto err3;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002975 set->nelems++;
Patrick McHardy20a69342013-10-11 12:06:22 +02002976
Arturo Borrerod60ce622014-04-01 14:06:07 +02002977 nf_tables_setelem_notify(ctx, set, &elem, NFT_MSG_NEWSETELEM, 0);
Patrick McHardy20a69342013-10-11 12:06:22 +02002978 return 0;
2979
2980err3:
2981 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2982 nft_data_uninit(&elem.data, d2.type);
2983err2:
2984 nft_data_uninit(&elem.key, d1.type);
2985err1:
2986 return err;
2987}
2988
2989static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2990 const struct nlmsghdr *nlh,
2991 const struct nlattr * const nla[])
2992{
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002993 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002994 const struct nlattr *attr;
2995 struct nft_set *set;
2996 struct nft_ctx ctx;
2997 int rem, err;
2998
2999 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
3000 if (err < 0)
3001 return err;
3002
3003 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003004 if (IS_ERR(set)) {
3005 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3006 set = nf_tables_set_lookup_byid(net,
3007 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3008 }
3009 if (IS_ERR(set))
3010 return PTR_ERR(set);
3011 }
3012
Patrick McHardy20a69342013-10-11 12:06:22 +02003013 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3014 return -EBUSY;
3015
3016 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3017 err = nft_add_set_elem(&ctx, set, attr);
3018 if (err < 0)
3019 return err;
3020 }
3021 return 0;
3022}
3023
3024static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
3025 const struct nlattr *attr)
3026{
3027 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3028 struct nft_data_desc desc;
3029 struct nft_set_elem elem;
3030 int err;
3031
3032 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3033 nft_set_elem_policy);
3034 if (err < 0)
3035 goto err1;
3036
3037 err = -EINVAL;
3038 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3039 goto err1;
3040
3041 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3042 if (err < 0)
3043 goto err1;
3044
3045 err = -EINVAL;
3046 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3047 goto err2;
3048
3049 err = set->ops->get(set, &elem);
3050 if (err < 0)
3051 goto err2;
3052
3053 set->ops->remove(set, &elem);
Patrick McHardyc50b9602014-03-28 10:19:47 +00003054 set->nelems--;
Patrick McHardy20a69342013-10-11 12:06:22 +02003055
Arturo Borrerod60ce622014-04-01 14:06:07 +02003056 nf_tables_setelem_notify(ctx, set, &elem, NFT_MSG_DELSETELEM, 0);
3057
Patrick McHardy20a69342013-10-11 12:06:22 +02003058 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
3059 if (set->flags & NFT_SET_MAP)
3060 nft_data_uninit(&elem.data, set->dtype);
3061
3062err2:
3063 nft_data_uninit(&elem.key, desc.type);
3064err1:
3065 return err;
3066}
3067
3068static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3069 const struct nlmsghdr *nlh,
3070 const struct nlattr * const nla[])
3071{
3072 const struct nlattr *attr;
3073 struct nft_set *set;
3074 struct nft_ctx ctx;
3075 int rem, err;
3076
3077 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
3078 if (err < 0)
3079 return err;
3080
3081 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3082 if (IS_ERR(set))
3083 return PTR_ERR(set);
3084 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3085 return -EBUSY;
3086
3087 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3088 err = nft_del_setelem(&ctx, set, attr);
3089 if (err < 0)
3090 return err;
3091 }
3092 return 0;
3093}
3094
Patrick McHardy96518512013-10-14 11:00:02 +02003095static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3096 [NFT_MSG_NEWTABLE] = {
3097 .call = nf_tables_newtable,
3098 .attr_count = NFTA_TABLE_MAX,
3099 .policy = nft_table_policy,
3100 },
3101 [NFT_MSG_GETTABLE] = {
3102 .call = nf_tables_gettable,
3103 .attr_count = NFTA_TABLE_MAX,
3104 .policy = nft_table_policy,
3105 },
3106 [NFT_MSG_DELTABLE] = {
3107 .call = nf_tables_deltable,
3108 .attr_count = NFTA_TABLE_MAX,
3109 .policy = nft_table_policy,
3110 },
3111 [NFT_MSG_NEWCHAIN] = {
3112 .call = nf_tables_newchain,
3113 .attr_count = NFTA_CHAIN_MAX,
3114 .policy = nft_chain_policy,
3115 },
3116 [NFT_MSG_GETCHAIN] = {
3117 .call = nf_tables_getchain,
3118 .attr_count = NFTA_CHAIN_MAX,
3119 .policy = nft_chain_policy,
3120 },
3121 [NFT_MSG_DELCHAIN] = {
3122 .call = nf_tables_delchain,
3123 .attr_count = NFTA_CHAIN_MAX,
3124 .policy = nft_chain_policy,
3125 },
3126 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003127 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003128 .attr_count = NFTA_RULE_MAX,
3129 .policy = nft_rule_policy,
3130 },
3131 [NFT_MSG_GETRULE] = {
3132 .call = nf_tables_getrule,
3133 .attr_count = NFTA_RULE_MAX,
3134 .policy = nft_rule_policy,
3135 },
3136 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003137 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003138 .attr_count = NFTA_RULE_MAX,
3139 .policy = nft_rule_policy,
3140 },
Patrick McHardy20a69342013-10-11 12:06:22 +02003141 [NFT_MSG_NEWSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003142 .call_batch = nf_tables_newset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003143 .attr_count = NFTA_SET_MAX,
3144 .policy = nft_set_policy,
3145 },
3146 [NFT_MSG_GETSET] = {
3147 .call = nf_tables_getset,
3148 .attr_count = NFTA_SET_MAX,
3149 .policy = nft_set_policy,
3150 },
3151 [NFT_MSG_DELSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003152 .call_batch = nf_tables_delset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003153 .attr_count = NFTA_SET_MAX,
3154 .policy = nft_set_policy,
3155 },
3156 [NFT_MSG_NEWSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003157 .call_batch = nf_tables_newsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003158 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3159 .policy = nft_set_elem_list_policy,
3160 },
3161 [NFT_MSG_GETSETELEM] = {
3162 .call = nf_tables_getsetelem,
3163 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3164 .policy = nft_set_elem_list_policy,
3165 },
3166 [NFT_MSG_DELSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003167 .call_batch = nf_tables_delsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003168 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3169 .policy = nft_set_elem_list_policy,
3170 },
Patrick McHardy96518512013-10-14 11:00:02 +02003171};
3172
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003173static int nf_tables_commit(struct sk_buff *skb)
3174{
3175 struct net *net = sock_net(skb->sk);
3176 struct nft_trans *trans, *next;
3177
3178 /* Bump generation counter, invalidate any dump in progress */
3179 net->nft.genctr++;
3180
3181 /* A new generation has just started */
3182 net->nft.gencursor = gencursor_next(net);
3183
3184 /* Make sure all packets have left the previous generation before
3185 * purging old rules.
3186 */
3187 synchronize_rcu();
3188
3189 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003190 switch (trans->msg_type) {
3191 case NFT_MSG_NEWRULE:
3192 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3193 nf_tables_rule_notify(trans->ctx.skb, trans->ctx.nlh,
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003194 trans->ctx.table,
3195 trans->ctx.chain,
3196 nft_trans_rule(trans),
3197 NFT_MSG_NEWRULE, 0,
3198 trans->ctx.afi->family);
3199 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003200 break;
3201 case NFT_MSG_DELRULE:
3202 list_del_rcu(&nft_trans_rule(trans)->list);
3203 nf_tables_rule_notify(trans->ctx.skb, trans->ctx.nlh,
3204 trans->ctx.table,
3205 trans->ctx.chain,
3206 nft_trans_rule(trans), NFT_MSG_DELRULE, 0,
3207 trans->ctx.afi->family);
3208 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003209 case NFT_MSG_NEWSET:
3210 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
3211 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3212 NFT_MSG_NEWSET);
3213 nft_trans_destroy(trans);
3214 break;
3215 case NFT_MSG_DELSET:
3216 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3217 NFT_MSG_DELSET);
3218 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003219 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003220 }
3221
3222 /* Make sure we don't see any packet traversing old rules */
3223 synchronize_rcu();
3224
3225 /* Now we can safely release unused old rules */
3226 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003227 switch (trans->msg_type) {
3228 case NFT_MSG_DELRULE:
3229 nf_tables_rule_destroy(&trans->ctx,
3230 nft_trans_rule(trans));
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003231 break;
3232 case NFT_MSG_DELSET:
3233 nft_set_destroy(nft_trans_set(trans));
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003234 break;
3235 }
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003236 nft_trans_destroy(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003237 }
3238
3239 return 0;
3240}
3241
3242static int nf_tables_abort(struct sk_buff *skb)
3243{
3244 struct net *net = sock_net(skb->sk);
3245 struct nft_trans *trans, *next;
3246
3247 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003248 switch (trans->msg_type) {
3249 case NFT_MSG_NEWRULE:
3250 list_del_rcu(&nft_trans_rule(trans)->list);
3251 break;
3252 case NFT_MSG_DELRULE:
3253 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003254 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003255 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003256 case NFT_MSG_NEWSET:
3257 list_del(&nft_trans_set(trans)->list);
3258 break;
3259 case NFT_MSG_DELSET:
3260 list_add_tail(&nft_trans_set(trans)->list,
3261 &trans->ctx.table->sets);
3262 nft_trans_destroy(trans);
3263 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003264 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003265 }
3266
3267 /* Make sure we don't see any packet accessing aborted rules */
3268 synchronize_rcu();
3269
3270 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003271 switch (trans->msg_type) {
3272 case NFT_MSG_NEWRULE:
3273 nf_tables_rule_destroy(&trans->ctx,
3274 nft_trans_rule(trans));
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003275 break;
3276 case NFT_MSG_NEWSET:
3277 nft_set_destroy(nft_trans_set(trans));
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003278 break;
3279 }
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003280 nft_trans_destroy(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003281 }
3282
3283 return 0;
3284}
3285
Patrick McHardy96518512013-10-14 11:00:02 +02003286static const struct nfnetlink_subsystem nf_tables_subsys = {
3287 .name = "nf_tables",
3288 .subsys_id = NFNL_SUBSYS_NFTABLES,
3289 .cb_count = NFT_MSG_MAX,
3290 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003291 .commit = nf_tables_commit,
3292 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02003293};
3294
Patrick McHardy20a69342013-10-11 12:06:22 +02003295/*
3296 * Loop detection - walk through the ruleset beginning at the destination chain
3297 * of a new jump until either the source chain is reached (loop) or all
3298 * reachable chains have been traversed.
3299 *
3300 * The loop check is performed whenever a new jump verdict is added to an
3301 * expression or verdict map or a verdict map is bound to a new chain.
3302 */
3303
3304static int nf_tables_check_loops(const struct nft_ctx *ctx,
3305 const struct nft_chain *chain);
3306
3307static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3308 const struct nft_set *set,
3309 const struct nft_set_iter *iter,
3310 const struct nft_set_elem *elem)
3311{
Pablo Neira Ayuso62f9c8b2014-02-07 14:45:01 +01003312 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3313 return 0;
3314
Patrick McHardy20a69342013-10-11 12:06:22 +02003315 switch (elem->data.verdict) {
3316 case NFT_JUMP:
3317 case NFT_GOTO:
3318 return nf_tables_check_loops(ctx, elem->data.chain);
3319 default:
3320 return 0;
3321 }
3322}
3323
3324static int nf_tables_check_loops(const struct nft_ctx *ctx,
3325 const struct nft_chain *chain)
3326{
3327 const struct nft_rule *rule;
3328 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02003329 const struct nft_set *set;
3330 struct nft_set_binding *binding;
3331 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02003332
3333 if (ctx->chain == chain)
3334 return -ELOOP;
3335
3336 list_for_each_entry(rule, &chain->rules, list) {
3337 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003338 const struct nft_data *data = NULL;
3339 int err;
3340
3341 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02003342 continue;
3343
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003344 err = expr->ops->validate(ctx, expr, &data);
3345 if (err < 0)
3346 return err;
3347
Patrick McHardy20a69342013-10-11 12:06:22 +02003348 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003349 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02003350
3351 switch (data->verdict) {
3352 case NFT_JUMP:
3353 case NFT_GOTO:
3354 err = nf_tables_check_loops(ctx, data->chain);
3355 if (err < 0)
3356 return err;
3357 default:
3358 break;
3359 }
3360 }
3361 }
3362
3363 list_for_each_entry(set, &ctx->table->sets, list) {
3364 if (!(set->flags & NFT_SET_MAP) ||
3365 set->dtype != NFT_DATA_VERDICT)
3366 continue;
3367
3368 list_for_each_entry(binding, &set->bindings, list) {
3369 if (binding->chain != chain)
3370 continue;
3371
3372 iter.skip = 0;
3373 iter.count = 0;
3374 iter.err = 0;
3375 iter.fn = nf_tables_loop_check_setelem;
3376
3377 set->ops->walk(ctx, set, &iter);
3378 if (iter.err < 0)
3379 return iter.err;
3380 }
3381 }
3382
3383 return 0;
3384}
3385
Patrick McHardy96518512013-10-14 11:00:02 +02003386/**
3387 * nft_validate_input_register - validate an expressions' input register
3388 *
3389 * @reg: the register number
3390 *
3391 * Validate that the input register is one of the general purpose
3392 * registers.
3393 */
3394int nft_validate_input_register(enum nft_registers reg)
3395{
3396 if (reg <= NFT_REG_VERDICT)
3397 return -EINVAL;
3398 if (reg > NFT_REG_MAX)
3399 return -ERANGE;
3400 return 0;
3401}
3402EXPORT_SYMBOL_GPL(nft_validate_input_register);
3403
3404/**
3405 * nft_validate_output_register - validate an expressions' output register
3406 *
3407 * @reg: the register number
3408 *
3409 * Validate that the output register is one of the general purpose
3410 * registers or the verdict register.
3411 */
3412int nft_validate_output_register(enum nft_registers reg)
3413{
3414 if (reg < NFT_REG_VERDICT)
3415 return -EINVAL;
3416 if (reg > NFT_REG_MAX)
3417 return -ERANGE;
3418 return 0;
3419}
3420EXPORT_SYMBOL_GPL(nft_validate_output_register);
3421
3422/**
3423 * nft_validate_data_load - validate an expressions' data load
3424 *
3425 * @ctx: context of the expression performing the load
3426 * @reg: the destination register number
3427 * @data: the data to load
3428 * @type: the data type
3429 *
3430 * Validate that a data load uses the appropriate data type for
3431 * the destination register. A value of NULL for the data means
3432 * that its runtime gathered data, which is always of type
3433 * NFT_DATA_VALUE.
3434 */
3435int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3436 const struct nft_data *data,
3437 enum nft_data_types type)
3438{
Patrick McHardy20a69342013-10-11 12:06:22 +02003439 int err;
3440
Patrick McHardy96518512013-10-14 11:00:02 +02003441 switch (reg) {
3442 case NFT_REG_VERDICT:
3443 if (data == NULL || type != NFT_DATA_VERDICT)
3444 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003445
3446 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3447 err = nf_tables_check_loops(ctx, data->chain);
3448 if (err < 0)
3449 return err;
3450
3451 if (ctx->chain->level + 1 > data->chain->level) {
3452 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3453 return -EMLINK;
3454 data->chain->level = ctx->chain->level + 1;
3455 }
3456 }
3457
Patrick McHardy96518512013-10-14 11:00:02 +02003458 return 0;
3459 default:
3460 if (data != NULL && type != NFT_DATA_VALUE)
3461 return -EINVAL;
3462 return 0;
3463 }
3464}
3465EXPORT_SYMBOL_GPL(nft_validate_data_load);
3466
3467static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3468 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3469 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3470 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3471};
3472
3473static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3474 struct nft_data_desc *desc, const struct nlattr *nla)
3475{
3476 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3477 struct nft_chain *chain;
3478 int err;
3479
3480 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3481 if (err < 0)
3482 return err;
3483
3484 if (!tb[NFTA_VERDICT_CODE])
3485 return -EINVAL;
3486 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3487
3488 switch (data->verdict) {
Patrick McHardye0abdad2014-02-18 18:06:50 +00003489 default:
3490 switch (data->verdict & NF_VERDICT_MASK) {
3491 case NF_ACCEPT:
3492 case NF_DROP:
3493 case NF_QUEUE:
3494 break;
3495 default:
3496 return -EINVAL;
3497 }
3498 /* fall through */
Patrick McHardy96518512013-10-14 11:00:02 +02003499 case NFT_CONTINUE:
3500 case NFT_BREAK:
3501 case NFT_RETURN:
3502 desc->len = sizeof(data->verdict);
3503 break;
3504 case NFT_JUMP:
3505 case NFT_GOTO:
3506 if (!tb[NFTA_VERDICT_CHAIN])
3507 return -EINVAL;
3508 chain = nf_tables_chain_lookup(ctx->table,
3509 tb[NFTA_VERDICT_CHAIN]);
3510 if (IS_ERR(chain))
3511 return PTR_ERR(chain);
3512 if (chain->flags & NFT_BASE_CHAIN)
3513 return -EOPNOTSUPP;
3514
Patrick McHardy96518512013-10-14 11:00:02 +02003515 chain->use++;
3516 data->chain = chain;
3517 desc->len = sizeof(data);
3518 break;
Patrick McHardy96518512013-10-14 11:00:02 +02003519 }
3520
3521 desc->type = NFT_DATA_VERDICT;
3522 return 0;
3523}
3524
3525static void nft_verdict_uninit(const struct nft_data *data)
3526{
3527 switch (data->verdict) {
3528 case NFT_JUMP:
3529 case NFT_GOTO:
3530 data->chain->use--;
3531 break;
3532 }
3533}
3534
3535static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3536{
3537 struct nlattr *nest;
3538
3539 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3540 if (!nest)
3541 goto nla_put_failure;
3542
3543 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3544 goto nla_put_failure;
3545
3546 switch (data->verdict) {
3547 case NFT_JUMP:
3548 case NFT_GOTO:
3549 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3550 goto nla_put_failure;
3551 }
3552 nla_nest_end(skb, nest);
3553 return 0;
3554
3555nla_put_failure:
3556 return -1;
3557}
3558
3559static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3560 struct nft_data_desc *desc, const struct nlattr *nla)
3561{
3562 unsigned int len;
3563
3564 len = nla_len(nla);
3565 if (len == 0)
3566 return -EINVAL;
3567 if (len > sizeof(data->data))
3568 return -EOVERFLOW;
3569
3570 nla_memcpy(data->data, nla, sizeof(data->data));
3571 desc->type = NFT_DATA_VALUE;
3572 desc->len = len;
3573 return 0;
3574}
3575
3576static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3577 unsigned int len)
3578{
3579 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3580}
3581
3582static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3583 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3584 .len = FIELD_SIZEOF(struct nft_data, data) },
3585 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3586};
3587
3588/**
3589 * nft_data_init - parse nf_tables data netlink attributes
3590 *
3591 * @ctx: context of the expression using the data
3592 * @data: destination struct nft_data
3593 * @desc: data description
3594 * @nla: netlink attribute containing data
3595 *
3596 * Parse the netlink data attributes and initialize a struct nft_data.
3597 * The type and length of data are returned in the data description.
3598 *
3599 * The caller can indicate that it only wants to accept data of type
3600 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3601 */
3602int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3603 struct nft_data_desc *desc, const struct nlattr *nla)
3604{
3605 struct nlattr *tb[NFTA_DATA_MAX + 1];
3606 int err;
3607
3608 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3609 if (err < 0)
3610 return err;
3611
3612 if (tb[NFTA_DATA_VALUE])
3613 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3614 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3615 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3616 return -EINVAL;
3617}
3618EXPORT_SYMBOL_GPL(nft_data_init);
3619
3620/**
3621 * nft_data_uninit - release a nft_data item
3622 *
3623 * @data: struct nft_data to release
3624 * @type: type of data
3625 *
3626 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3627 * all others need to be released by calling this function.
3628 */
3629void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3630{
3631 switch (type) {
3632 case NFT_DATA_VALUE:
3633 return;
3634 case NFT_DATA_VERDICT:
3635 return nft_verdict_uninit(data);
3636 default:
3637 WARN_ON(1);
3638 }
3639}
3640EXPORT_SYMBOL_GPL(nft_data_uninit);
3641
3642int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3643 enum nft_data_types type, unsigned int len)
3644{
3645 struct nlattr *nest;
3646 int err;
3647
3648 nest = nla_nest_start(skb, attr);
3649 if (nest == NULL)
3650 return -1;
3651
3652 switch (type) {
3653 case NFT_DATA_VALUE:
3654 err = nft_value_dump(skb, data, len);
3655 break;
3656 case NFT_DATA_VERDICT:
3657 err = nft_verdict_dump(skb, data);
3658 break;
3659 default:
3660 err = -EINVAL;
3661 WARN_ON(1);
3662 }
3663
3664 nla_nest_end(skb, nest);
3665 return err;
3666}
3667EXPORT_SYMBOL_GPL(nft_data_dump);
3668
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003669static int nf_tables_init_net(struct net *net)
3670{
3671 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003672 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003673 return 0;
3674}
3675
3676static struct pernet_operations nf_tables_net_ops = {
3677 .init = nf_tables_init_net,
3678};
3679
Patrick McHardy96518512013-10-14 11:00:02 +02003680static int __init nf_tables_module_init(void)
3681{
3682 int err;
3683
3684 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3685 GFP_KERNEL);
3686 if (info == NULL) {
3687 err = -ENOMEM;
3688 goto err1;
3689 }
3690
3691 err = nf_tables_core_module_init();
3692 if (err < 0)
3693 goto err2;
3694
3695 err = nfnetlink_subsys_register(&nf_tables_subsys);
3696 if (err < 0)
3697 goto err3;
3698
3699 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003700 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003701err3:
3702 nf_tables_core_module_exit();
3703err2:
3704 kfree(info);
3705err1:
3706 return err;
3707}
3708
3709static void __exit nf_tables_module_exit(void)
3710{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003711 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003712 nfnetlink_subsys_unregister(&nf_tables_subsys);
3713 nf_tables_core_module_exit();
3714 kfree(info);
3715}
3716
3717module_init(nf_tables_module_init);
3718module_exit(nf_tables_module_exit);
3719
3720MODULE_LICENSE("GPL");
3721MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3722MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);