blob: acdd9d68d52f80be372b79d64caefec00370fb27 [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001/*
Patrick McHardy20a69342013-10-11 12:06:22 +02002 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
Patrick McHardy96518512013-10-14 11:00:02 +02003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/skbuff.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nfnetlink.h>
18#include <linux/netfilter/nf_tables.h>
19#include <net/netfilter/nf_tables_core.h>
20#include <net/netfilter/nf_tables.h>
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020021#include <net/net_namespace.h>
Patrick McHardy96518512013-10-14 11:00:02 +020022#include <net/sock.h>
23
Patrick McHardy96518512013-10-14 11:00:02 +020024static LIST_HEAD(nf_tables_expressions);
25
26/**
27 * nft_register_afinfo - register nf_tables address family info
28 *
29 * @afi: address family info to register
30 *
31 * Register the address family for use with nf_tables. Returns zero on
32 * success or a negative errno code otherwise.
33 */
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020034int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
Patrick McHardy96518512013-10-14 11:00:02 +020035{
36 INIT_LIST_HEAD(&afi->tables);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020038 list_add_tail(&afi->list, &net->nft.af_info);
Patrick McHardy96518512013-10-14 11:00:02 +020039 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
40 return 0;
41}
42EXPORT_SYMBOL_GPL(nft_register_afinfo);
43
44/**
45 * nft_unregister_afinfo - unregister nf_tables address family info
46 *
47 * @afi: address family info to unregister
48 *
49 * Unregister the address family for use with nf_tables.
50 */
51void nft_unregister_afinfo(struct nft_af_info *afi)
52{
53 nfnl_lock(NFNL_SUBSYS_NFTABLES);
54 list_del(&afi->list);
55 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
56}
57EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
58
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020059static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
Patrick McHardy96518512013-10-14 11:00:02 +020060{
61 struct nft_af_info *afi;
62
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020063 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +020064 if (afi->family == family)
65 return afi;
66 }
67 return NULL;
68}
69
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020070static struct nft_af_info *
71nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
Patrick McHardy96518512013-10-14 11:00:02 +020072{
73 struct nft_af_info *afi;
74
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020075 afi = nft_afinfo_lookup(net, family);
Patrick McHardy96518512013-10-14 11:00:02 +020076 if (afi != NULL)
77 return afi;
78#ifdef CONFIG_MODULES
79 if (autoload) {
80 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
81 request_module("nft-afinfo-%u", family);
82 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020083 afi = nft_afinfo_lookup(net, family);
Patrick McHardy96518512013-10-14 11:00:02 +020084 if (afi != NULL)
85 return ERR_PTR(-EAGAIN);
86 }
87#endif
88 return ERR_PTR(-EAFNOSUPPORT);
89}
90
91/*
92 * Tables
93 */
94
95static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
96 const struct nlattr *nla)
97{
98 struct nft_table *table;
99
100 list_for_each_entry(table, &afi->tables, list) {
101 if (!nla_strcmp(nla, table->name))
102 return table;
103 }
104 return NULL;
105}
106
107static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200108 const struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +0200109{
110 struct nft_table *table;
111
112 if (nla == NULL)
113 return ERR_PTR(-EINVAL);
114
115 table = nft_table_lookup(afi, nla);
116 if (table != NULL)
117 return table;
118
Patrick McHardy96518512013-10-14 11:00:02 +0200119 return ERR_PTR(-ENOENT);
120}
121
122static inline u64 nf_tables_alloc_handle(struct nft_table *table)
123{
124 return ++table->hgenerator;
125}
126
Patrick McHardy2a37d752014-01-09 18:42:37 +0000127static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200128
Patrick McHardy2a37d752014-01-09 18:42:37 +0000129static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000130__nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200131{
132 int i;
133
Patrick McHardybaae3e62014-01-09 18:42:34 +0000134 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200135 if (chain_type[family][i] != NULL &&
136 !nla_strcmp(nla, chain_type[family][i]->name))
Patrick McHardybaae3e62014-01-09 18:42:34 +0000137 return chain_type[family][i];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200138 }
Patrick McHardybaae3e62014-01-09 18:42:34 +0000139 return NULL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200140}
141
Patrick McHardy2a37d752014-01-09 18:42:37 +0000142static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000143nf_tables_chain_type_lookup(const struct nft_af_info *afi,
144 const struct nlattr *nla,
145 bool autoload)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200146{
Patrick McHardy2a37d752014-01-09 18:42:37 +0000147 const struct nf_chain_type *type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200148
149 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000150 if (type != NULL)
151 return type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200152#ifdef CONFIG_MODULES
Patrick McHardy93b08062014-01-09 18:42:36 +0000153 if (autoload) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200154 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
155 request_module("nft-chain-%u-%*.s", afi->family,
156 nla_len(nla)-1, (const char *)nla_data(nla));
157 nfnl_lock(NFNL_SUBSYS_NFTABLES);
158 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000159 if (type != NULL)
160 return ERR_PTR(-EAGAIN);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200161 }
162#endif
Patrick McHardy93b08062014-01-09 18:42:36 +0000163 return ERR_PTR(-ENOENT);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200164}
165
Patrick McHardy96518512013-10-14 11:00:02 +0200166static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
167 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200168 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
Patrick McHardy96518512013-10-14 11:00:02 +0200169};
170
171static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
172 int event, u32 flags, int family,
173 const struct nft_table *table)
174{
175 struct nlmsghdr *nlh;
176 struct nfgenmsg *nfmsg;
177
178 event |= NFNL_SUBSYS_NFTABLES << 8;
179 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
180 if (nlh == NULL)
181 goto nla_put_failure;
182
183 nfmsg = nlmsg_data(nlh);
184 nfmsg->nfgen_family = family;
185 nfmsg->version = NFNETLINK_V0;
186 nfmsg->res_id = 0;
187
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200188 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
Tomasz Bursztykad8bcc7682013-12-12 15:00:42 +0200189 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
190 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
Patrick McHardy96518512013-10-14 11:00:02 +0200191 goto nla_put_failure;
192
193 return nlmsg_end(skb, nlh);
194
195nla_put_failure:
196 nlmsg_trim(skb, nlh);
197 return -1;
198}
199
200static int nf_tables_table_notify(const struct sk_buff *oskb,
201 const struct nlmsghdr *nlh,
202 const struct nft_table *table,
203 int event, int family)
204{
205 struct sk_buff *skb;
206 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
207 u32 seq = nlh ? nlh->nlmsg_seq : 0;
208 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
209 bool report;
210 int err;
211
212 report = nlh ? nlmsg_report(nlh) : false;
213 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
214 return 0;
215
216 err = -ENOBUFS;
217 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
218 if (skb == NULL)
219 goto err;
220
221 err = nf_tables_fill_table_info(skb, portid, seq, event, 0,
222 family, table);
223 if (err < 0) {
224 kfree_skb(skb);
225 goto err;
226 }
227
228 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
229 GFP_KERNEL);
230err:
231 if (err < 0)
232 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
233 return err;
234}
235
236static int nf_tables_dump_tables(struct sk_buff *skb,
237 struct netlink_callback *cb)
238{
239 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
240 const struct nft_af_info *afi;
241 const struct nft_table *table;
242 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200243 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200244 int family = nfmsg->nfgen_family;
245
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200246 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200247 if (family != NFPROTO_UNSPEC && family != afi->family)
248 continue;
249
250 list_for_each_entry(table, &afi->tables, list) {
251 if (idx < s_idx)
252 goto cont;
253 if (idx > s_idx)
254 memset(&cb->args[1], 0,
255 sizeof(cb->args) - sizeof(cb->args[0]));
256 if (nf_tables_fill_table_info(skb,
257 NETLINK_CB(cb->skb).portid,
258 cb->nlh->nlmsg_seq,
259 NFT_MSG_NEWTABLE,
260 NLM_F_MULTI,
261 afi->family, table) < 0)
262 goto done;
263cont:
264 idx++;
265 }
266 }
267done:
268 cb->args[0] = idx;
269 return skb->len;
270}
271
272static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
273 const struct nlmsghdr *nlh,
274 const struct nlattr * const nla[])
275{
276 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
277 const struct nft_af_info *afi;
278 const struct nft_table *table;
279 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200280 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200281 int family = nfmsg->nfgen_family;
282 int err;
283
284 if (nlh->nlmsg_flags & NLM_F_DUMP) {
285 struct netlink_dump_control c = {
286 .dump = nf_tables_dump_tables,
287 };
288 return netlink_dump_start(nlsk, skb, nlh, &c);
289 }
290
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200291 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200292 if (IS_ERR(afi))
293 return PTR_ERR(afi);
294
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200295 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200296 if (IS_ERR(table))
297 return PTR_ERR(table);
298
299 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
300 if (!skb2)
301 return -ENOMEM;
302
303 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
304 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
305 family, table);
306 if (err < 0)
307 goto err;
308
309 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
310
311err:
312 kfree_skb(skb2);
313 return err;
314}
315
Patrick McHardy115a60b2014-01-03 12:16:15 +0000316static int nf_tables_table_enable(const struct nft_af_info *afi,
317 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200318{
319 struct nft_chain *chain;
320 int err, i = 0;
321
322 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100323 if (!(chain->flags & NFT_BASE_CHAIN))
324 continue;
325
Patrick McHardy115a60b2014-01-03 12:16:15 +0000326 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200327 if (err < 0)
328 goto err;
329
330 i++;
331 }
332 return 0;
333err:
334 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100335 if (!(chain->flags & NFT_BASE_CHAIN))
336 continue;
337
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200338 if (i-- <= 0)
339 break;
340
Patrick McHardy115a60b2014-01-03 12:16:15 +0000341 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200342 }
343 return err;
344}
345
Patrick McHardy115a60b2014-01-03 12:16:15 +0000346static int nf_tables_table_disable(const struct nft_af_info *afi,
347 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200348{
349 struct nft_chain *chain;
350
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100351 list_for_each_entry(chain, &table->chains, list) {
352 if (chain->flags & NFT_BASE_CHAIN)
Patrick McHardy115a60b2014-01-03 12:16:15 +0000353 nf_unregister_hooks(nft_base_chain(chain)->ops,
354 afi->nops);
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100355 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200356
357 return 0;
358}
359
360static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,
361 const struct nlmsghdr *nlh,
362 const struct nlattr * const nla[],
363 struct nft_af_info *afi, struct nft_table *table)
364{
365 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
366 int family = nfmsg->nfgen_family, ret = 0;
367
368 if (nla[NFTA_TABLE_FLAGS]) {
369 __be32 flags;
370
371 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
372 if (flags & ~NFT_TABLE_F_DORMANT)
373 return -EINVAL;
374
375 if ((flags & NFT_TABLE_F_DORMANT) &&
376 !(table->flags & NFT_TABLE_F_DORMANT)) {
Patrick McHardy115a60b2014-01-03 12:16:15 +0000377 ret = nf_tables_table_disable(afi, table);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200378 if (ret >= 0)
379 table->flags |= NFT_TABLE_F_DORMANT;
380 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
381 table->flags & NFT_TABLE_F_DORMANT) {
Patrick McHardy115a60b2014-01-03 12:16:15 +0000382 ret = nf_tables_table_enable(afi, table);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200383 if (ret >= 0)
384 table->flags &= ~NFT_TABLE_F_DORMANT;
385 }
386 if (ret < 0)
387 goto err;
388 }
389
390 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
391err:
392 return ret;
393}
394
Patrick McHardy96518512013-10-14 11:00:02 +0200395static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
396 const struct nlmsghdr *nlh,
397 const struct nlattr * const nla[])
398{
399 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
400 const struct nlattr *name;
401 struct nft_af_info *afi;
402 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200403 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200404 int family = nfmsg->nfgen_family;
405
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200406 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200407 if (IS_ERR(afi))
408 return PTR_ERR(afi);
409
410 name = nla[NFTA_TABLE_NAME];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200411 table = nf_tables_table_lookup(afi, name);
Patrick McHardy96518512013-10-14 11:00:02 +0200412 if (IS_ERR(table)) {
413 if (PTR_ERR(table) != -ENOENT)
414 return PTR_ERR(table);
415 table = NULL;
416 }
417
418 if (table != NULL) {
419 if (nlh->nlmsg_flags & NLM_F_EXCL)
420 return -EEXIST;
421 if (nlh->nlmsg_flags & NLM_F_REPLACE)
422 return -EOPNOTSUPP;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200423 return nf_tables_updtable(nlsk, skb, nlh, nla, afi, table);
Patrick McHardy96518512013-10-14 11:00:02 +0200424 }
425
426 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
427 if (table == NULL)
428 return -ENOMEM;
429
430 nla_strlcpy(table->name, name, nla_len(name));
431 INIT_LIST_HEAD(&table->chains);
Patrick McHardy20a69342013-10-11 12:06:22 +0200432 INIT_LIST_HEAD(&table->sets);
Patrick McHardy96518512013-10-14 11:00:02 +0200433
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200434 if (nla[NFTA_TABLE_FLAGS]) {
435 __be32 flags;
436
437 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
438 if (flags & ~NFT_TABLE_F_DORMANT) {
439 kfree(table);
440 return -EINVAL;
441 }
442
443 table->flags |= flags;
444 }
445
Patrick McHardy96518512013-10-14 11:00:02 +0200446 list_add_tail(&table->list, &afi->tables);
447 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
448 return 0;
449}
450
451static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
452 const struct nlmsghdr *nlh,
453 const struct nlattr * const nla[])
454{
455 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
456 struct nft_af_info *afi;
457 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200458 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200459 int family = nfmsg->nfgen_family;
460
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200461 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200462 if (IS_ERR(afi))
463 return PTR_ERR(afi);
464
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200465 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200466 if (IS_ERR(table))
467 return PTR_ERR(table);
468
Patrick McHardy96518512013-10-14 11:00:02 +0200469 if (table->use)
470 return -EBUSY;
471
472 list_del(&table->list);
473 nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
474 kfree(table);
475 return 0;
476}
477
Patrick McHardy2a37d752014-01-09 18:42:37 +0000478int nft_register_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200479{
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200480 int err = 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200481
482 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200483 if (chain_type[ctype->family][ctype->type] != NULL) {
484 err = -EBUSY;
485 goto out;
Patrick McHardy96518512013-10-14 11:00:02 +0200486 }
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200487 chain_type[ctype->family][ctype->type] = ctype;
488out:
Patrick McHardy96518512013-10-14 11:00:02 +0200489 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
490 return err;
491}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200492EXPORT_SYMBOL_GPL(nft_register_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200493
Patrick McHardy2a37d752014-01-09 18:42:37 +0000494void nft_unregister_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200495{
Patrick McHardy96518512013-10-14 11:00:02 +0200496 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200497 chain_type[ctype->family][ctype->type] = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200498 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
499}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200500EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200501
502/*
503 * Chains
504 */
505
506static struct nft_chain *
507nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
508{
509 struct nft_chain *chain;
510
511 list_for_each_entry(chain, &table->chains, list) {
512 if (chain->handle == handle)
513 return chain;
514 }
515
516 return ERR_PTR(-ENOENT);
517}
518
519static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
520 const struct nlattr *nla)
521{
522 struct nft_chain *chain;
523
524 if (nla == NULL)
525 return ERR_PTR(-EINVAL);
526
527 list_for_each_entry(chain, &table->chains, list) {
528 if (!nla_strcmp(nla, chain->name))
529 return chain;
530 }
531
532 return ERR_PTR(-ENOENT);
533}
534
535static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
536 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
537 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
538 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
539 .len = NFT_CHAIN_MAXNAMELEN - 1 },
540 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200541 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200542 [NFTA_CHAIN_TYPE] = { .type = NLA_NUL_STRING },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200543 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
Patrick McHardy96518512013-10-14 11:00:02 +0200544};
545
546static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
547 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
548 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
549};
550
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200551static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
552{
553 struct nft_stats *cpu_stats, total;
554 struct nlattr *nest;
555 int cpu;
556
557 memset(&total, 0, sizeof(total));
558 for_each_possible_cpu(cpu) {
559 cpu_stats = per_cpu_ptr(stats, cpu);
560 total.pkts += cpu_stats->pkts;
561 total.bytes += cpu_stats->bytes;
562 }
563 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
564 if (nest == NULL)
565 goto nla_put_failure;
566
567 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
568 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
569 goto nla_put_failure;
570
571 nla_nest_end(skb, nest);
572 return 0;
573
574nla_put_failure:
575 return -ENOSPC;
576}
577
Patrick McHardy96518512013-10-14 11:00:02 +0200578static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
579 int event, u32 flags, int family,
580 const struct nft_table *table,
581 const struct nft_chain *chain)
582{
583 struct nlmsghdr *nlh;
584 struct nfgenmsg *nfmsg;
585
586 event |= NFNL_SUBSYS_NFTABLES << 8;
587 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
588 if (nlh == NULL)
589 goto nla_put_failure;
590
591 nfmsg = nlmsg_data(nlh);
592 nfmsg->nfgen_family = family;
593 nfmsg->version = NFNETLINK_V0;
594 nfmsg->res_id = 0;
595
596 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
597 goto nla_put_failure;
598 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
599 goto nla_put_failure;
600 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
601 goto nla_put_failure;
602
603 if (chain->flags & NFT_BASE_CHAIN) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200604 const struct nft_base_chain *basechain = nft_base_chain(chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000605 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200606 struct nlattr *nest;
607
608 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
Patrick McHardy96518512013-10-14 11:00:02 +0200609 if (nest == NULL)
610 goto nla_put_failure;
611 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
612 goto nla_put_failure;
613 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
614 goto nla_put_failure;
615 nla_nest_end(skb, nest);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200616
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200617 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
618 htonl(basechain->policy)))
619 goto nla_put_failure;
620
Patrick McHardybaae3e62014-01-09 18:42:34 +0000621 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
622 goto nla_put_failure;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200623
624 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
625 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +0200626 }
627
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200628 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
629 goto nla_put_failure;
630
Patrick McHardy96518512013-10-14 11:00:02 +0200631 return nlmsg_end(skb, nlh);
632
633nla_put_failure:
634 nlmsg_trim(skb, nlh);
635 return -1;
636}
637
638static int nf_tables_chain_notify(const struct sk_buff *oskb,
639 const struct nlmsghdr *nlh,
640 const struct nft_table *table,
641 const struct nft_chain *chain,
642 int event, int family)
643{
644 struct sk_buff *skb;
645 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
646 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
647 u32 seq = nlh ? nlh->nlmsg_seq : 0;
648 bool report;
649 int err;
650
651 report = nlh ? nlmsg_report(nlh) : false;
652 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
653 return 0;
654
655 err = -ENOBUFS;
656 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
657 if (skb == NULL)
658 goto err;
659
660 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
661 table, chain);
662 if (err < 0) {
663 kfree_skb(skb);
664 goto err;
665 }
666
667 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
668 GFP_KERNEL);
669err:
670 if (err < 0)
671 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
672 return err;
673}
674
675static int nf_tables_dump_chains(struct sk_buff *skb,
676 struct netlink_callback *cb)
677{
678 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
679 const struct nft_af_info *afi;
680 const struct nft_table *table;
681 const struct nft_chain *chain;
682 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200683 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200684 int family = nfmsg->nfgen_family;
685
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200686 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200687 if (family != NFPROTO_UNSPEC && family != afi->family)
688 continue;
689
690 list_for_each_entry(table, &afi->tables, list) {
691 list_for_each_entry(chain, &table->chains, list) {
692 if (idx < s_idx)
693 goto cont;
694 if (idx > s_idx)
695 memset(&cb->args[1], 0,
696 sizeof(cb->args) - sizeof(cb->args[0]));
697 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
698 cb->nlh->nlmsg_seq,
699 NFT_MSG_NEWCHAIN,
700 NLM_F_MULTI,
701 afi->family, table, chain) < 0)
702 goto done;
703cont:
704 idx++;
705 }
706 }
707 }
708done:
709 cb->args[0] = idx;
710 return skb->len;
711}
712
713
714static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
715 const struct nlmsghdr *nlh,
716 const struct nlattr * const nla[])
717{
718 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
719 const struct nft_af_info *afi;
720 const struct nft_table *table;
721 const struct nft_chain *chain;
722 struct sk_buff *skb2;
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 int err;
726
727 if (nlh->nlmsg_flags & NLM_F_DUMP) {
728 struct netlink_dump_control c = {
729 .dump = nf_tables_dump_chains,
730 };
731 return netlink_dump_start(nlsk, skb, nlh, &c);
732 }
733
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200734 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200735 if (IS_ERR(afi))
736 return PTR_ERR(afi);
737
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200738 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200739 if (IS_ERR(table))
740 return PTR_ERR(table);
741
742 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
743 if (IS_ERR(chain))
744 return PTR_ERR(chain);
745
746 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
747 if (!skb2)
748 return -ENOMEM;
749
750 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
751 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
752 family, table, chain);
753 if (err < 0)
754 goto err;
755
756 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
757
758err:
759 kfree_skb(skb2);
760 return err;
761}
762
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200763static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
764 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
765 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
766};
767
768static int
769nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
770{
771 struct nlattr *tb[NFTA_COUNTER_MAX+1];
772 struct nft_stats __percpu *newstats;
773 struct nft_stats *stats;
774 int err;
775
776 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
777 if (err < 0)
778 return err;
779
780 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
781 return -EINVAL;
782
783 newstats = alloc_percpu(struct nft_stats);
784 if (newstats == NULL)
785 return -ENOMEM;
786
787 /* Restore old counters on this cpu, no problem. Per-cpu statistics
788 * are not exposed to userspace.
789 */
790 stats = this_cpu_ptr(newstats);
791 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
792 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
793
794 if (chain->stats) {
795 /* nfnl_lock is held, add some nfnl function for this, later */
796 struct nft_stats __percpu *oldstats =
797 rcu_dereference_protected(chain->stats, 1);
798
799 rcu_assign_pointer(chain->stats, newstats);
800 synchronize_rcu();
801 free_percpu(oldstats);
802 } else
803 rcu_assign_pointer(chain->stats, newstats);
804
805 return 0;
806}
807
Patrick McHardy96518512013-10-14 11:00:02 +0200808static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
809 const struct nlmsghdr *nlh,
810 const struct nlattr * const nla[])
811{
812 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
813 const struct nlattr * uninitialized_var(name);
814 const struct nft_af_info *afi;
815 struct nft_table *table;
816 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200817 struct nft_base_chain *basechain = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200818 struct nlattr *ha[NFTA_HOOK_MAX + 1];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200819 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200820 int family = nfmsg->nfgen_family;
Patrick McHardy57de2a02014-01-09 18:42:31 +0000821 u8 policy = NF_ACCEPT;
Patrick McHardy96518512013-10-14 11:00:02 +0200822 u64 handle = 0;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000823 unsigned int i;
Patrick McHardy96518512013-10-14 11:00:02 +0200824 int err;
825 bool create;
826
827 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
828
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200829 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200830 if (IS_ERR(afi))
831 return PTR_ERR(afi);
832
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200833 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200834 if (IS_ERR(table))
835 return PTR_ERR(table);
836
Patrick McHardy96518512013-10-14 11:00:02 +0200837 chain = NULL;
838 name = nla[NFTA_CHAIN_NAME];
839
840 if (nla[NFTA_CHAIN_HANDLE]) {
841 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
842 chain = nf_tables_chain_lookup_byhandle(table, handle);
843 if (IS_ERR(chain))
844 return PTR_ERR(chain);
845 } else {
846 chain = nf_tables_chain_lookup(table, name);
847 if (IS_ERR(chain)) {
848 if (PTR_ERR(chain) != -ENOENT)
849 return PTR_ERR(chain);
850 chain = NULL;
851 }
852 }
853
Patrick McHardy57de2a02014-01-09 18:42:31 +0000854 if (nla[NFTA_CHAIN_POLICY]) {
855 if ((chain != NULL &&
856 !(chain->flags & NFT_BASE_CHAIN)) ||
857 nla[NFTA_CHAIN_HOOK] == NULL)
858 return -EOPNOTSUPP;
859
860 policy = nla_get_be32(nla[NFTA_CHAIN_POLICY]);
861 switch (policy) {
862 case NF_DROP:
863 case NF_ACCEPT:
864 break;
865 default:
866 return -EINVAL;
867 }
868 }
869
Patrick McHardy96518512013-10-14 11:00:02 +0200870 if (chain != NULL) {
871 if (nlh->nlmsg_flags & NLM_F_EXCL)
872 return -EEXIST;
873 if (nlh->nlmsg_flags & NLM_F_REPLACE)
874 return -EOPNOTSUPP;
875
876 if (nla[NFTA_CHAIN_HANDLE] && name &&
877 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
878 return -EEXIST;
879
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200880 if (nla[NFTA_CHAIN_COUNTERS]) {
881 if (!(chain->flags & NFT_BASE_CHAIN))
882 return -EOPNOTSUPP;
883
884 err = nf_tables_counters(nft_base_chain(chain),
885 nla[NFTA_CHAIN_COUNTERS]);
886 if (err < 0)
887 return err;
888 }
889
Patrick McHardy4401a862014-01-09 18:42:32 +0000890 if (nla[NFTA_CHAIN_POLICY])
891 nft_base_chain(chain)->policy = policy;
892
Patrick McHardy96518512013-10-14 11:00:02 +0200893 if (nla[NFTA_CHAIN_HANDLE] && name)
894 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
895
896 goto notify;
897 }
898
Patrick McHardy75820672014-01-09 18:42:33 +0000899 if (table->use == UINT_MAX)
900 return -EOVERFLOW;
901
Patrick McHardy96518512013-10-14 11:00:02 +0200902 if (nla[NFTA_CHAIN_HOOK]) {
Patrick McHardy2a37d752014-01-09 18:42:37 +0000903 const struct nf_chain_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +0200904 struct nf_hook_ops *ops;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200905 nf_hookfn *hookfn;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000906 u32 hooknum, priority;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200907
Patrick McHardybaae3e62014-01-09 18:42:34 +0000908 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200909 if (nla[NFTA_CHAIN_TYPE]) {
910 type = nf_tables_chain_type_lookup(afi,
911 nla[NFTA_CHAIN_TYPE],
912 create);
Patrick McHardy93b08062014-01-09 18:42:36 +0000913 if (IS_ERR(type))
914 return PTR_ERR(type);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200915 }
Patrick McHardy96518512013-10-14 11:00:02 +0200916
917 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
918 nft_hook_policy);
919 if (err < 0)
920 return err;
921 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
922 ha[NFTA_HOOK_PRIORITY] == NULL)
923 return -EINVAL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200924
925 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
926 if (hooknum >= afi->nhooks)
Patrick McHardy96518512013-10-14 11:00:02 +0200927 return -EINVAL;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000928 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
Patrick McHardy96518512013-10-14 11:00:02 +0200929
Patrick McHardybaae3e62014-01-09 18:42:34 +0000930 if (!(type->hook_mask & (1 << hooknum)))
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200931 return -EOPNOTSUPP;
Patrick McHardybaae3e62014-01-09 18:42:34 +0000932 if (!try_module_get(type->me))
933 return -ENOENT;
934 hookfn = type->fn[hooknum];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200935
Patrick McHardy96518512013-10-14 11:00:02 +0200936 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
937 if (basechain == NULL)
938 return -ENOMEM;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200939
Patrick McHardy4401a862014-01-09 18:42:32 +0000940 if (nla[NFTA_CHAIN_COUNTERS]) {
941 err = nf_tables_counters(basechain,
942 nla[NFTA_CHAIN_COUNTERS]);
943 if (err < 0) {
Patrick McHardybaae3e62014-01-09 18:42:34 +0000944 module_put(type->me);
Patrick McHardy4401a862014-01-09 18:42:32 +0000945 kfree(basechain);
946 return err;
947 }
948 } else {
949 struct nft_stats __percpu *newstats;
950
951 newstats = alloc_percpu(struct nft_stats);
952 if (newstats == NULL) {
Patrick McHardybaae3e62014-01-09 18:42:34 +0000953 module_put(type->me);
Patrick McHardy4401a862014-01-09 18:42:32 +0000954 kfree(basechain);
955 return -ENOMEM;
956 }
957 rcu_assign_pointer(basechain->stats, newstats);
958 }
959
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200960 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +0200961 chain = &basechain->chain;
962
Patrick McHardy115a60b2014-01-03 12:16:15 +0000963 for (i = 0; i < afi->nops; i++) {
964 ops = &basechain->ops[i];
965 ops->pf = family;
966 ops->owner = afi->owner;
967 ops->hooknum = hooknum;
968 ops->priority = priority;
969 ops->priv = chain;
970 ops->hook = afi->hooks[ops->hooknum];
971 if (hookfn)
972 ops->hook = hookfn;
973 if (afi->hook_ops_init)
974 afi->hook_ops_init(ops, i);
975 }
Patrick McHardy96518512013-10-14 11:00:02 +0200976
977 chain->flags |= NFT_BASE_CHAIN;
Patrick McHardy57de2a02014-01-09 18:42:31 +0000978 basechain->policy = policy;
Patrick McHardy96518512013-10-14 11:00:02 +0200979 } else {
980 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
981 if (chain == NULL)
982 return -ENOMEM;
983 }
984
985 INIT_LIST_HEAD(&chain->rules);
986 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200987 chain->net = net;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +0200988 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +0200989 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
990
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200991 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
992 chain->flags & NFT_BASE_CHAIN) {
Patrick McHardy115a60b2014-01-03 12:16:15 +0000993 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200994 if (err < 0) {
Patrick McHardybaae3e62014-01-09 18:42:34 +0000995 module_put(basechain->type->me);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200996 free_percpu(basechain->stats);
997 kfree(basechain);
998 return err;
999 }
1000 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001001 list_add_tail(&chain->list, &table->chains);
1002 table->use++;
Patrick McHardy96518512013-10-14 11:00:02 +02001003notify:
1004 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
1005 family);
1006 return 0;
1007}
1008
1009static void nf_tables_rcu_chain_destroy(struct rcu_head *head)
1010{
1011 struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
1012
1013 BUG_ON(chain->use > 0);
1014
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001015 if (chain->flags & NFT_BASE_CHAIN) {
Patrick McHardybaae3e62014-01-09 18:42:34 +00001016 module_put(nft_base_chain(chain)->type->me);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001017 free_percpu(nft_base_chain(chain)->stats);
Patrick McHardy96518512013-10-14 11:00:02 +02001018 kfree(nft_base_chain(chain));
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001019 } else
Patrick McHardy96518512013-10-14 11:00:02 +02001020 kfree(chain);
1021}
1022
1023static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1024 const struct nlmsghdr *nlh,
1025 const struct nlattr * const nla[])
1026{
1027 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1028 const struct nft_af_info *afi;
1029 struct nft_table *table;
1030 struct nft_chain *chain;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001031 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001032 int family = nfmsg->nfgen_family;
1033
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001034 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001035 if (IS_ERR(afi))
1036 return PTR_ERR(afi);
1037
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001038 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001039 if (IS_ERR(table))
1040 return PTR_ERR(table);
1041
1042 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1043 if (IS_ERR(chain))
1044 return PTR_ERR(chain);
1045
Patrick McHardy96518512013-10-14 11:00:02 +02001046 if (!list_empty(&chain->rules))
1047 return -EBUSY;
1048
1049 list_del(&chain->list);
1050 table->use--;
1051
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001052 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1053 chain->flags & NFT_BASE_CHAIN)
Patrick McHardy115a60b2014-01-03 12:16:15 +00001054 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
Patrick McHardy96518512013-10-14 11:00:02 +02001055
1056 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1057 family);
1058
1059 /* Make sure all rule references are gone before this is released */
1060 call_rcu(&chain->rcu_head, nf_tables_rcu_chain_destroy);
1061 return 0;
1062}
1063
1064static void nft_ctx_init(struct nft_ctx *ctx,
Patrick McHardy20a69342013-10-11 12:06:22 +02001065 const struct sk_buff *skb,
1066 const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02001067 const struct nft_af_info *afi,
1068 const struct nft_table *table,
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001069 const struct nft_chain *chain,
1070 const struct nlattr * const *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001071{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001072 ctx->net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02001073 ctx->skb = skb;
1074 ctx->nlh = nlh;
Patrick McHardy96518512013-10-14 11:00:02 +02001075 ctx->afi = afi;
1076 ctx->table = table;
1077 ctx->chain = chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001078 ctx->nla = nla;
Patrick McHardy96518512013-10-14 11:00:02 +02001079}
1080
1081/*
1082 * Expressions
1083 */
1084
1085/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001086 * nft_register_expr - register nf_tables expr type
1087 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001088 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001089 * Registers the expr type for use with nf_tables. Returns zero on
Patrick McHardy96518512013-10-14 11:00:02 +02001090 * success or a negative errno code otherwise.
1091 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001092int nft_register_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001093{
1094 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001095 list_add_tail(&type->list, &nf_tables_expressions);
Patrick McHardy96518512013-10-14 11:00:02 +02001096 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1097 return 0;
1098}
1099EXPORT_SYMBOL_GPL(nft_register_expr);
1100
1101/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001102 * nft_unregister_expr - unregister nf_tables expr type
1103 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001104 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001105 * Unregisters the expr typefor use with nf_tables.
Patrick McHardy96518512013-10-14 11:00:02 +02001106 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001107void nft_unregister_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001108{
1109 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001110 list_del(&type->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001111 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1112}
1113EXPORT_SYMBOL_GPL(nft_unregister_expr);
1114
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001115static const struct nft_expr_type *__nft_expr_type_get(struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001116{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001117 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001118
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001119 list_for_each_entry(type, &nf_tables_expressions, list) {
1120 if (!nla_strcmp(nla, type->name))
1121 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001122 }
1123 return NULL;
1124}
1125
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001126static const struct nft_expr_type *nft_expr_type_get(struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001127{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001128 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001129
1130 if (nla == NULL)
1131 return ERR_PTR(-EINVAL);
1132
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001133 type = __nft_expr_type_get(nla);
1134 if (type != NULL && try_module_get(type->owner))
1135 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001136
1137#ifdef CONFIG_MODULES
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001138 if (type == NULL) {
Patrick McHardy96518512013-10-14 11:00:02 +02001139 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1140 request_module("nft-expr-%.*s",
1141 nla_len(nla), (char *)nla_data(nla));
1142 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001143 if (__nft_expr_type_get(nla))
Patrick McHardy96518512013-10-14 11:00:02 +02001144 return ERR_PTR(-EAGAIN);
1145 }
1146#endif
1147 return ERR_PTR(-ENOENT);
1148}
1149
1150static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1151 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1152 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1153};
1154
1155static int nf_tables_fill_expr_info(struct sk_buff *skb,
1156 const struct nft_expr *expr)
1157{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001158 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
Patrick McHardy96518512013-10-14 11:00:02 +02001159 goto nla_put_failure;
1160
1161 if (expr->ops->dump) {
1162 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1163 if (data == NULL)
1164 goto nla_put_failure;
1165 if (expr->ops->dump(skb, expr) < 0)
1166 goto nla_put_failure;
1167 nla_nest_end(skb, data);
1168 }
1169
1170 return skb->len;
1171
1172nla_put_failure:
1173 return -1;
1174};
1175
1176struct nft_expr_info {
1177 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001178 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001179};
1180
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001181static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1182 const struct nlattr *nla,
Patrick McHardy96518512013-10-14 11:00:02 +02001183 struct nft_expr_info *info)
1184{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001185 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001186 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001187 struct nlattr *tb[NFTA_EXPR_MAX + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001188 int err;
1189
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001190 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
Patrick McHardy96518512013-10-14 11:00:02 +02001191 if (err < 0)
1192 return err;
1193
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001194 type = nft_expr_type_get(tb[NFTA_EXPR_NAME]);
1195 if (IS_ERR(type))
1196 return PTR_ERR(type);
1197
1198 if (tb[NFTA_EXPR_DATA]) {
1199 err = nla_parse_nested(info->tb, type->maxattr,
1200 tb[NFTA_EXPR_DATA], type->policy);
1201 if (err < 0)
1202 goto err1;
1203 } else
1204 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1205
1206 if (type->select_ops != NULL) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001207 ops = type->select_ops(ctx,
1208 (const struct nlattr * const *)info->tb);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001209 if (IS_ERR(ops)) {
1210 err = PTR_ERR(ops);
1211 goto err1;
1212 }
1213 } else
1214 ops = type->ops;
1215
Patrick McHardy96518512013-10-14 11:00:02 +02001216 info->ops = ops;
1217 return 0;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001218
1219err1:
1220 module_put(type->owner);
1221 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001222}
1223
1224static int nf_tables_newexpr(const struct nft_ctx *ctx,
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001225 const struct nft_expr_info *info,
Patrick McHardy96518512013-10-14 11:00:02 +02001226 struct nft_expr *expr)
1227{
1228 const struct nft_expr_ops *ops = info->ops;
1229 int err;
1230
1231 expr->ops = ops;
1232 if (ops->init) {
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001233 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
Patrick McHardy96518512013-10-14 11:00:02 +02001234 if (err < 0)
1235 goto err1;
1236 }
1237
Patrick McHardy96518512013-10-14 11:00:02 +02001238 return 0;
1239
1240err1:
1241 expr->ops = NULL;
1242 return err;
1243}
1244
1245static void nf_tables_expr_destroy(struct nft_expr *expr)
1246{
1247 if (expr->ops->destroy)
1248 expr->ops->destroy(expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001249 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001250}
1251
1252/*
1253 * Rules
1254 */
1255
1256static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1257 u64 handle)
1258{
1259 struct nft_rule *rule;
1260
1261 // FIXME: this sucks
1262 list_for_each_entry(rule, &chain->rules, list) {
1263 if (handle == rule->handle)
1264 return rule;
1265 }
1266
1267 return ERR_PTR(-ENOENT);
1268}
1269
1270static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1271 const struct nlattr *nla)
1272{
1273 if (nla == NULL)
1274 return ERR_PTR(-EINVAL);
1275
1276 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1277}
1278
1279static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1280 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1281 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1282 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1283 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1284 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001285 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001286 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Patrick McHardy96518512013-10-14 11:00:02 +02001287};
1288
1289static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1290 int event, u32 flags, int family,
1291 const struct nft_table *table,
1292 const struct nft_chain *chain,
1293 const struct nft_rule *rule)
1294{
1295 struct nlmsghdr *nlh;
1296 struct nfgenmsg *nfmsg;
1297 const struct nft_expr *expr, *next;
1298 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001299 const struct nft_rule *prule;
1300 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001301
Eric Leblond5e948462013-10-10 13:41:44 +02001302 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001303 flags);
1304 if (nlh == NULL)
1305 goto nla_put_failure;
1306
1307 nfmsg = nlmsg_data(nlh);
1308 nfmsg->nfgen_family = family;
1309 nfmsg->version = NFNETLINK_V0;
1310 nfmsg->res_id = 0;
1311
1312 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1313 goto nla_put_failure;
1314 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1315 goto nla_put_failure;
1316 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1317 goto nla_put_failure;
1318
Eric Leblond5e948462013-10-10 13:41:44 +02001319 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1320 prule = list_entry(rule->list.prev, struct nft_rule, list);
1321 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1322 cpu_to_be64(prule->handle)))
1323 goto nla_put_failure;
1324 }
1325
Patrick McHardy96518512013-10-14 11:00:02 +02001326 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1327 if (list == NULL)
1328 goto nla_put_failure;
1329 nft_rule_for_each_expr(expr, next, rule) {
1330 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1331 if (elem == NULL)
1332 goto nla_put_failure;
1333 if (nf_tables_fill_expr_info(skb, expr) < 0)
1334 goto nla_put_failure;
1335 nla_nest_end(skb, elem);
1336 }
1337 nla_nest_end(skb, list);
1338
1339 return nlmsg_end(skb, nlh);
1340
1341nla_put_failure:
1342 nlmsg_trim(skb, nlh);
1343 return -1;
1344}
1345
1346static int nf_tables_rule_notify(const struct sk_buff *oskb,
1347 const struct nlmsghdr *nlh,
1348 const struct nft_table *table,
1349 const struct nft_chain *chain,
1350 const struct nft_rule *rule,
1351 int event, u32 flags, int family)
1352{
1353 struct sk_buff *skb;
1354 u32 portid = NETLINK_CB(oskb).portid;
1355 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1356 u32 seq = nlh->nlmsg_seq;
1357 bool report;
1358 int err;
1359
1360 report = nlmsg_report(nlh);
1361 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1362 return 0;
1363
1364 err = -ENOBUFS;
1365 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1366 if (skb == NULL)
1367 goto err;
1368
1369 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1370 family, table, chain, rule);
1371 if (err < 0) {
1372 kfree_skb(skb);
1373 goto err;
1374 }
1375
1376 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1377 GFP_KERNEL);
1378err:
1379 if (err < 0)
1380 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1381 return err;
1382}
1383
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001384static inline bool
1385nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1386{
1387 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1388}
1389
1390static inline int gencursor_next(struct net *net)
1391{
1392 return net->nft.gencursor+1 == 1 ? 1 : 0;
1393}
1394
1395static inline int
1396nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1397{
1398 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1399}
1400
1401static inline void
1402nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1403{
1404 /* Now inactive, will be active in the future */
1405 rule->genmask = (1 << net->nft.gencursor);
1406}
1407
1408static inline void
1409nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1410{
1411 rule->genmask = (1 << gencursor_next(net));
1412}
1413
1414static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1415{
1416 rule->genmask = 0;
1417}
1418
Patrick McHardy96518512013-10-14 11:00:02 +02001419static int nf_tables_dump_rules(struct sk_buff *skb,
1420 struct netlink_callback *cb)
1421{
1422 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1423 const struct nft_af_info *afi;
1424 const struct nft_table *table;
1425 const struct nft_chain *chain;
1426 const struct nft_rule *rule;
1427 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001428 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001429 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001430 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1431 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
Patrick McHardy96518512013-10-14 11:00:02 +02001432
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001433 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001434 if (family != NFPROTO_UNSPEC && family != afi->family)
1435 continue;
1436
1437 list_for_each_entry(table, &afi->tables, list) {
1438 list_for_each_entry(chain, &table->chains, list) {
1439 list_for_each_entry(rule, &chain->rules, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001440 if (!nft_rule_is_active(net, rule))
1441 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001442 if (idx < s_idx)
1443 goto cont;
1444 if (idx > s_idx)
1445 memset(&cb->args[1], 0,
1446 sizeof(cb->args) - sizeof(cb->args[0]));
1447 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1448 cb->nlh->nlmsg_seq,
1449 NFT_MSG_NEWRULE,
1450 NLM_F_MULTI | NLM_F_APPEND,
1451 afi->family, table, chain, rule) < 0)
1452 goto done;
1453cont:
1454 idx++;
1455 }
1456 }
1457 }
1458 }
1459done:
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001460 /* Invalidate this dump, a transition to the new generation happened */
1461 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1462 return -EBUSY;
1463
Patrick McHardy96518512013-10-14 11:00:02 +02001464 cb->args[0] = idx;
1465 return skb->len;
1466}
1467
1468static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1469 const struct nlmsghdr *nlh,
1470 const struct nlattr * const nla[])
1471{
1472 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1473 const struct nft_af_info *afi;
1474 const struct nft_table *table;
1475 const struct nft_chain *chain;
1476 const struct nft_rule *rule;
1477 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001478 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001479 int family = nfmsg->nfgen_family;
1480 int err;
1481
1482 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1483 struct netlink_dump_control c = {
1484 .dump = nf_tables_dump_rules,
1485 };
1486 return netlink_dump_start(nlsk, skb, nlh, &c);
1487 }
1488
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001489 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001490 if (IS_ERR(afi))
1491 return PTR_ERR(afi);
1492
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001493 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001494 if (IS_ERR(table))
1495 return PTR_ERR(table);
1496
1497 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1498 if (IS_ERR(chain))
1499 return PTR_ERR(chain);
1500
1501 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1502 if (IS_ERR(rule))
1503 return PTR_ERR(rule);
1504
1505 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1506 if (!skb2)
1507 return -ENOMEM;
1508
1509 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1510 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1511 family, table, chain, rule);
1512 if (err < 0)
1513 goto err;
1514
1515 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1516
1517err:
1518 kfree_skb(skb2);
1519 return err;
1520}
1521
1522static void nf_tables_rcu_rule_destroy(struct rcu_head *head)
1523{
1524 struct nft_rule *rule = container_of(head, struct nft_rule, rcu_head);
1525 struct nft_expr *expr;
1526
1527 /*
1528 * Careful: some expressions might not be initialized in case this
1529 * is called on error from nf_tables_newrule().
1530 */
1531 expr = nft_expr_first(rule);
1532 while (expr->ops && expr != nft_expr_last(rule)) {
1533 nf_tables_expr_destroy(expr);
1534 expr = nft_expr_next(expr);
1535 }
1536 kfree(rule);
1537}
1538
1539static void nf_tables_rule_destroy(struct nft_rule *rule)
1540{
1541 call_rcu(&rule->rcu_head, nf_tables_rcu_rule_destroy);
1542}
1543
1544#define NFT_RULE_MAXEXPRS 128
1545
1546static struct nft_expr_info *info;
1547
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001548static struct nft_rule_trans *
1549nf_tables_trans_add(struct nft_rule *rule, const struct nft_ctx *ctx)
1550{
1551 struct nft_rule_trans *rupd;
1552
1553 rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1554 if (rupd == NULL)
1555 return NULL;
1556
1557 rupd->chain = ctx->chain;
1558 rupd->table = ctx->table;
1559 rupd->rule = rule;
1560 rupd->family = ctx->afi->family;
1561 rupd->nlh = ctx->nlh;
1562 list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1563
1564 return rupd;
1565}
1566
Patrick McHardy96518512013-10-14 11:00:02 +02001567static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1568 const struct nlmsghdr *nlh,
1569 const struct nlattr * const nla[])
1570{
1571 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1572 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001573 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001574 struct nft_table *table;
1575 struct nft_chain *chain;
1576 struct nft_rule *rule, *old_rule = NULL;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001577 struct nft_rule_trans *repl = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001578 struct nft_expr *expr;
1579 struct nft_ctx ctx;
1580 struct nlattr *tmp;
1581 unsigned int size, i, n;
1582 int err, rem;
1583 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02001584 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02001585
1586 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1587
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001588 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02001589 if (IS_ERR(afi))
1590 return PTR_ERR(afi);
1591
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001592 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001593 if (IS_ERR(table))
1594 return PTR_ERR(table);
1595
1596 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1597 if (IS_ERR(chain))
1598 return PTR_ERR(chain);
1599
1600 if (nla[NFTA_RULE_HANDLE]) {
1601 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1602 rule = __nf_tables_rule_lookup(chain, handle);
1603 if (IS_ERR(rule))
1604 return PTR_ERR(rule);
1605
1606 if (nlh->nlmsg_flags & NLM_F_EXCL)
1607 return -EEXIST;
1608 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1609 old_rule = rule;
1610 else
1611 return -EOPNOTSUPP;
1612 } else {
1613 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1614 return -EINVAL;
1615 handle = nf_tables_alloc_handle(table);
1616 }
1617
Eric Leblond5e948462013-10-10 13:41:44 +02001618 if (nla[NFTA_RULE_POSITION]) {
1619 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1620 return -EOPNOTSUPP;
1621
1622 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1623 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1624 if (IS_ERR(old_rule))
1625 return PTR_ERR(old_rule);
1626 }
1627
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001628 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1629
Patrick McHardy96518512013-10-14 11:00:02 +02001630 n = 0;
1631 size = 0;
1632 if (nla[NFTA_RULE_EXPRESSIONS]) {
1633 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1634 err = -EINVAL;
1635 if (nla_type(tmp) != NFTA_LIST_ELEM)
1636 goto err1;
1637 if (n == NFT_RULE_MAXEXPRS)
1638 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001639 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02001640 if (err < 0)
1641 goto err1;
1642 size += info[n].ops->size;
1643 n++;
1644 }
1645 }
1646
1647 err = -ENOMEM;
1648 rule = kzalloc(sizeof(*rule) + size, GFP_KERNEL);
1649 if (rule == NULL)
1650 goto err1;
1651
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001652 nft_rule_activate_next(net, rule);
1653
Patrick McHardy96518512013-10-14 11:00:02 +02001654 rule->handle = handle;
1655 rule->dlen = size;
1656
Patrick McHardy96518512013-10-14 11:00:02 +02001657 expr = nft_expr_first(rule);
1658 for (i = 0; i < n; i++) {
1659 err = nf_tables_newexpr(&ctx, &info[i], expr);
1660 if (err < 0)
1661 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001662 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001663 expr = nft_expr_next(expr);
1664 }
1665
Patrick McHardy96518512013-10-14 11:00:02 +02001666 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001667 if (nft_rule_is_active_next(net, old_rule)) {
1668 repl = nf_tables_trans_add(old_rule, &ctx);
1669 if (repl == NULL) {
1670 err = -ENOMEM;
1671 goto err2;
1672 }
1673 nft_rule_disactivate_next(net, old_rule);
1674 list_add_tail(&rule->list, &old_rule->list);
1675 } else {
1676 err = -ENOENT;
1677 goto err2;
1678 }
Patrick McHardy96518512013-10-14 11:00:02 +02001679 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02001680 if (old_rule)
1681 list_add_rcu(&rule->list, &old_rule->list);
1682 else
1683 list_add_tail_rcu(&rule->list, &chain->rules);
1684 else {
1685 if (old_rule)
1686 list_add_tail_rcu(&rule->list, &old_rule->list);
1687 else
1688 list_add_rcu(&rule->list, &chain->rules);
1689 }
Patrick McHardy96518512013-10-14 11:00:02 +02001690
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001691 if (nf_tables_trans_add(rule, &ctx) == NULL) {
1692 err = -ENOMEM;
1693 goto err3;
1694 }
Patrick McHardy96518512013-10-14 11:00:02 +02001695 return 0;
1696
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001697err3:
1698 list_del_rcu(&rule->list);
1699 if (repl) {
1700 list_del_rcu(&repl->rule->list);
1701 list_del(&repl->list);
1702 nft_rule_clear(net, repl->rule);
1703 kfree(repl);
1704 }
Patrick McHardy96518512013-10-14 11:00:02 +02001705err2:
1706 nf_tables_rule_destroy(rule);
1707err1:
1708 for (i = 0; i < n; i++) {
1709 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001710 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001711 }
1712 return err;
1713}
1714
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001715static int
1716nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1717{
1718 /* You cannot delete the same rule twice */
1719 if (nft_rule_is_active_next(ctx->net, rule)) {
1720 if (nf_tables_trans_add(rule, ctx) == NULL)
1721 return -ENOMEM;
1722 nft_rule_disactivate_next(ctx->net, rule);
1723 return 0;
1724 }
1725 return -ENOENT;
1726}
1727
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001728static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1729{
1730 struct nft_rule *rule;
1731 int err;
1732
1733 list_for_each_entry(rule, &ctx->chain->rules, list) {
1734 err = nf_tables_delrule_one(ctx, rule);
1735 if (err < 0)
1736 return err;
1737 }
1738 return 0;
1739}
1740
Patrick McHardy96518512013-10-14 11:00:02 +02001741static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1742 const struct nlmsghdr *nlh,
1743 const struct nlattr * const nla[])
1744{
1745 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1746 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001747 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001748 const struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001749 struct nft_chain *chain = NULL;
1750 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001751 int family = nfmsg->nfgen_family, err = 0;
1752 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001753
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001754 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001755 if (IS_ERR(afi))
1756 return PTR_ERR(afi);
1757
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001758 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001759 if (IS_ERR(table))
1760 return PTR_ERR(table);
1761
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001762 if (nla[NFTA_RULE_CHAIN]) {
1763 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1764 if (IS_ERR(chain))
1765 return PTR_ERR(chain);
1766 }
Patrick McHardy96518512013-10-14 11:00:02 +02001767
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001768 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1769
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001770 if (chain) {
1771 if (nla[NFTA_RULE_HANDLE]) {
1772 rule = nf_tables_rule_lookup(chain,
1773 nla[NFTA_RULE_HANDLE]);
1774 if (IS_ERR(rule))
1775 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001776
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001777 err = nf_tables_delrule_one(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001778 } else {
1779 err = nf_table_delrule_by_chain(&ctx);
1780 }
1781 } else {
1782 list_for_each_entry(chain, &table->chains, list) {
1783 ctx.chain = chain;
1784 err = nf_table_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001785 if (err < 0)
1786 break;
Patrick McHardy96518512013-10-14 11:00:02 +02001787 }
1788 }
1789
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001790 return err;
1791}
1792
1793static int nf_tables_commit(struct sk_buff *skb)
1794{
1795 struct net *net = sock_net(skb->sk);
1796 struct nft_rule_trans *rupd, *tmp;
1797
1798 /* Bump generation counter, invalidate any dump in progress */
1799 net->nft.genctr++;
1800
1801 /* A new generation has just started */
1802 net->nft.gencursor = gencursor_next(net);
1803
1804 /* Make sure all packets have left the previous generation before
1805 * purging old rules.
1806 */
1807 synchronize_rcu();
1808
1809 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1810 /* Delete this rule from the dirty list */
1811 list_del(&rupd->list);
1812
1813 /* This rule was inactive in the past and just became active.
1814 * Clear the next bit of the genmask since its meaning has
1815 * changed, now it is the future.
1816 */
1817 if (nft_rule_is_active(net, rupd->rule)) {
1818 nft_rule_clear(net, rupd->rule);
1819 nf_tables_rule_notify(skb, rupd->nlh, rupd->table,
1820 rupd->chain, rupd->rule,
1821 NFT_MSG_NEWRULE, 0,
1822 rupd->family);
1823 kfree(rupd);
1824 continue;
1825 }
1826
1827 /* This rule is in the past, get rid of it */
1828 list_del_rcu(&rupd->rule->list);
1829 nf_tables_rule_notify(skb, rupd->nlh, rupd->table, rupd->chain,
1830 rupd->rule, NFT_MSG_DELRULE, 0,
1831 rupd->family);
1832 nf_tables_rule_destroy(rupd->rule);
1833 kfree(rupd);
1834 }
1835
1836 return 0;
1837}
1838
1839static int nf_tables_abort(struct sk_buff *skb)
1840{
1841 struct net *net = sock_net(skb->sk);
1842 struct nft_rule_trans *rupd, *tmp;
1843
1844 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1845 /* Delete all rules from the dirty list */
1846 list_del(&rupd->list);
1847
1848 if (!nft_rule_is_active_next(net, rupd->rule)) {
1849 nft_rule_clear(net, rupd->rule);
1850 kfree(rupd);
1851 continue;
1852 }
1853
1854 /* This rule is inactive, get rid of it */
1855 list_del_rcu(&rupd->rule->list);
1856 nf_tables_rule_destroy(rupd->rule);
1857 kfree(rupd);
1858 }
Patrick McHardy96518512013-10-14 11:00:02 +02001859 return 0;
1860}
1861
Patrick McHardy20a69342013-10-11 12:06:22 +02001862/*
1863 * Sets
1864 */
1865
1866static LIST_HEAD(nf_tables_set_ops);
1867
1868int nft_register_set(struct nft_set_ops *ops)
1869{
1870 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1871 list_add_tail(&ops->list, &nf_tables_set_ops);
1872 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1873 return 0;
1874}
1875EXPORT_SYMBOL_GPL(nft_register_set);
1876
1877void nft_unregister_set(struct nft_set_ops *ops)
1878{
1879 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1880 list_del(&ops->list);
1881 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1882}
1883EXPORT_SYMBOL_GPL(nft_unregister_set);
1884
1885static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1886{
1887 const struct nft_set_ops *ops;
1888 u32 features;
1889
1890#ifdef CONFIG_MODULES
1891 if (list_empty(&nf_tables_set_ops)) {
1892 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1893 request_module("nft-set");
1894 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1895 if (!list_empty(&nf_tables_set_ops))
1896 return ERR_PTR(-EAGAIN);
1897 }
1898#endif
1899 features = 0;
1900 if (nla[NFTA_SET_FLAGS] != NULL) {
1901 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1902 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1903 }
1904
1905 // FIXME: implement selection properly
1906 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1907 if ((ops->features & features) != features)
1908 continue;
1909 if (!try_module_get(ops->owner))
1910 continue;
1911 return ops;
1912 }
1913
1914 return ERR_PTR(-EOPNOTSUPP);
1915}
1916
1917static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1918 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1919 [NFTA_SET_NAME] = { .type = NLA_STRING },
1920 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1921 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1922 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1923 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1924 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1925};
1926
1927static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1928 const struct sk_buff *skb,
1929 const struct nlmsghdr *nlh,
1930 const struct nlattr * const nla[])
1931{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001932 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02001933 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01001934 const struct nft_af_info *afi = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02001935 const struct nft_table *table = NULL;
1936
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01001937 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
1938 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1939 if (IS_ERR(afi))
1940 return PTR_ERR(afi);
1941 }
Patrick McHardy20a69342013-10-11 12:06:22 +02001942
1943 if (nla[NFTA_SET_TABLE] != NULL) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001944 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02001945 if (IS_ERR(table))
1946 return PTR_ERR(table);
1947 }
1948
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001949 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02001950 return 0;
1951}
1952
1953struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1954 const struct nlattr *nla)
1955{
1956 struct nft_set *set;
1957
1958 if (nla == NULL)
1959 return ERR_PTR(-EINVAL);
1960
1961 list_for_each_entry(set, &table->sets, list) {
1962 if (!nla_strcmp(nla, set->name))
1963 return set;
1964 }
1965 return ERR_PTR(-ENOENT);
1966}
1967
1968static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1969 const char *name)
1970{
1971 const struct nft_set *i;
1972 const char *p;
1973 unsigned long *inuse;
1974 unsigned int n = 0;
1975
1976 p = strnchr(name, IFNAMSIZ, '%');
1977 if (p != NULL) {
1978 if (p[1] != 'd' || strchr(p + 2, '%'))
1979 return -EINVAL;
1980
1981 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1982 if (inuse == NULL)
1983 return -ENOMEM;
1984
1985 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01001986 int tmp;
1987
1988 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02001989 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01001990 if (tmp < 0 || tmp > BITS_PER_LONG * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02001991 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01001992
1993 set_bit(tmp, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02001994 }
1995
1996 n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
1997 free_page((unsigned long)inuse);
1998 }
1999
2000 snprintf(set->name, sizeof(set->name), name, n);
2001 list_for_each_entry(i, &ctx->table->sets, list) {
2002 if (!strcmp(set->name, i->name))
2003 return -ENFILE;
2004 }
2005 return 0;
2006}
2007
2008static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2009 const struct nft_set *set, u16 event, u16 flags)
2010{
2011 struct nfgenmsg *nfmsg;
2012 struct nlmsghdr *nlh;
2013 u32 portid = NETLINK_CB(ctx->skb).portid;
2014 u32 seq = ctx->nlh->nlmsg_seq;
2015
2016 event |= NFNL_SUBSYS_NFTABLES << 8;
2017 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2018 flags);
2019 if (nlh == NULL)
2020 goto nla_put_failure;
2021
2022 nfmsg = nlmsg_data(nlh);
2023 nfmsg->nfgen_family = ctx->afi->family;
2024 nfmsg->version = NFNETLINK_V0;
2025 nfmsg->res_id = 0;
2026
2027 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2028 goto nla_put_failure;
2029 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2030 goto nla_put_failure;
2031 if (set->flags != 0)
2032 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2033 goto nla_put_failure;
2034
2035 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2036 goto nla_put_failure;
2037 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2038 goto nla_put_failure;
2039 if (set->flags & NFT_SET_MAP) {
2040 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2041 goto nla_put_failure;
2042 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2043 goto nla_put_failure;
2044 }
2045
2046 return nlmsg_end(skb, nlh);
2047
2048nla_put_failure:
2049 nlmsg_trim(skb, nlh);
2050 return -1;
2051}
2052
2053static int nf_tables_set_notify(const struct nft_ctx *ctx,
2054 const struct nft_set *set,
2055 int event)
2056{
2057 struct sk_buff *skb;
2058 u32 portid = NETLINK_CB(ctx->skb).portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002059 bool report;
2060 int err;
2061
2062 report = nlmsg_report(ctx->nlh);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002063 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002064 return 0;
2065
2066 err = -ENOBUFS;
2067 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2068 if (skb == NULL)
2069 goto err;
2070
2071 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2072 if (err < 0) {
2073 kfree_skb(skb);
2074 goto err;
2075 }
2076
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002077 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
Patrick McHardy20a69342013-10-11 12:06:22 +02002078 GFP_KERNEL);
2079err:
2080 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002081 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002082 return err;
2083}
2084
2085static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2086 struct netlink_callback *cb)
2087{
2088 const struct nft_set *set;
2089 unsigned int idx = 0, s_idx = cb->args[0];
2090
2091 if (cb->args[1])
2092 return skb->len;
2093
2094 list_for_each_entry(set, &ctx->table->sets, list) {
2095 if (idx < s_idx)
2096 goto cont;
2097 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2098 NLM_F_MULTI) < 0) {
2099 cb->args[0] = idx;
2100 goto done;
2101 }
2102cont:
2103 idx++;
2104 }
2105 cb->args[1] = 1;
2106done:
2107 return skb->len;
2108}
2109
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002110static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2111 struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002112{
2113 const struct nft_set *set;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002114 unsigned int idx, s_idx = cb->args[0];
Patrick McHardy20a69342013-10-11 12:06:22 +02002115 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2116
2117 if (cb->args[1])
2118 return skb->len;
2119
2120 list_for_each_entry(table, &ctx->afi->tables, list) {
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002121 if (cur_table) {
2122 if (cur_table != table)
2123 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02002124
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002125 cur_table = NULL;
2126 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002127 ctx->table = table;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002128 idx = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002129 list_for_each_entry(set, &ctx->table->sets, list) {
2130 if (idx < s_idx)
2131 goto cont;
2132 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2133 NLM_F_MULTI) < 0) {
2134 cb->args[0] = idx;
2135 cb->args[2] = (unsigned long) table;
2136 goto done;
2137 }
2138cont:
2139 idx++;
2140 }
2141 }
2142 cb->args[1] = 1;
2143done:
2144 return skb->len;
2145}
2146
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002147static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2148 struct netlink_callback *cb)
2149{
2150 const struct nft_set *set;
2151 unsigned int idx, s_idx = cb->args[0];
2152 const struct nft_af_info *afi;
2153 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2154 struct net *net = sock_net(skb->sk);
2155 int cur_family = cb->args[3];
2156
2157 if (cb->args[1])
2158 return skb->len;
2159
2160 list_for_each_entry(afi, &net->nft.af_info, list) {
2161 if (cur_family) {
2162 if (afi->family != cur_family)
2163 continue;
2164
2165 cur_family = 0;
2166 }
2167
2168 list_for_each_entry(table, &afi->tables, list) {
2169 if (cur_table) {
2170 if (cur_table != table)
2171 continue;
2172
2173 cur_table = NULL;
2174 }
2175
2176 ctx->table = table;
2177 ctx->afi = afi;
2178 idx = 0;
2179 list_for_each_entry(set, &ctx->table->sets, list) {
2180 if (idx < s_idx)
2181 goto cont;
2182 if (nf_tables_fill_set(skb, ctx, set,
2183 NFT_MSG_NEWSET,
2184 NLM_F_MULTI) < 0) {
2185 cb->args[0] = idx;
2186 cb->args[2] = (unsigned long) table;
2187 cb->args[3] = afi->family;
2188 goto done;
2189 }
2190cont:
2191 idx++;
2192 }
2193 if (s_idx)
2194 s_idx = 0;
2195 }
2196 }
2197 cb->args[1] = 1;
2198done:
2199 return skb->len;
2200}
2201
Patrick McHardy20a69342013-10-11 12:06:22 +02002202static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2203{
2204 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2205 struct nlattr *nla[NFTA_SET_MAX + 1];
2206 struct nft_ctx ctx;
2207 int err, ret;
2208
2209 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2210 nft_set_policy);
2211 if (err < 0)
2212 return err;
2213
2214 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2215 if (err < 0)
2216 return err;
2217
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002218 if (ctx.table == NULL) {
2219 if (ctx.afi == NULL)
2220 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2221 else
2222 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2223 } else
Patrick McHardy20a69342013-10-11 12:06:22 +02002224 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2225
2226 return ret;
2227}
2228
2229static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2230 const struct nlmsghdr *nlh,
2231 const struct nlattr * const nla[])
2232{
2233 const struct nft_set *set;
2234 struct nft_ctx ctx;
2235 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002236 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002237 int err;
2238
2239 /* Verify existance before starting dump */
2240 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2241 if (err < 0)
2242 return err;
2243
2244 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2245 struct netlink_dump_control c = {
2246 .dump = nf_tables_dump_sets,
2247 };
2248 return netlink_dump_start(nlsk, skb, nlh, &c);
2249 }
2250
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002251 /* Only accept unspec with dump */
2252 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2253 return -EAFNOSUPPORT;
2254
Patrick McHardy20a69342013-10-11 12:06:22 +02002255 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2256 if (IS_ERR(set))
2257 return PTR_ERR(set);
2258
2259 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2260 if (skb2 == NULL)
2261 return -ENOMEM;
2262
2263 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2264 if (err < 0)
2265 goto err;
2266
2267 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2268
2269err:
2270 kfree_skb(skb2);
2271 return err;
2272}
2273
2274static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2275 const struct nlmsghdr *nlh,
2276 const struct nlattr * const nla[])
2277{
2278 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2279 const struct nft_set_ops *ops;
2280 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002281 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002282 struct nft_table *table;
2283 struct nft_set *set;
2284 struct nft_ctx ctx;
2285 char name[IFNAMSIZ];
2286 unsigned int size;
2287 bool create;
2288 u32 ktype, klen, dlen, dtype, flags;
2289 int err;
2290
2291 if (nla[NFTA_SET_TABLE] == NULL ||
2292 nla[NFTA_SET_NAME] == NULL ||
2293 nla[NFTA_SET_KEY_LEN] == NULL)
2294 return -EINVAL;
2295
2296 ktype = NFT_DATA_VALUE;
2297 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2298 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2299 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2300 return -EINVAL;
2301 }
2302
2303 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2304 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2305 return -EINVAL;
2306
2307 flags = 0;
2308 if (nla[NFTA_SET_FLAGS] != NULL) {
2309 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2310 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2311 NFT_SET_INTERVAL | NFT_SET_MAP))
2312 return -EINVAL;
2313 }
2314
2315 dtype = 0;
2316 dlen = 0;
2317 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2318 if (!(flags & NFT_SET_MAP))
2319 return -EINVAL;
2320
2321 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2322 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2323 dtype != NFT_DATA_VERDICT)
2324 return -EINVAL;
2325
2326 if (dtype != NFT_DATA_VERDICT) {
2327 if (nla[NFTA_SET_DATA_LEN] == NULL)
2328 return -EINVAL;
2329 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2330 if (dlen == 0 ||
2331 dlen > FIELD_SIZEOF(struct nft_data, data))
2332 return -EINVAL;
2333 } else
2334 dlen = sizeof(struct nft_data);
2335 } else if (flags & NFT_SET_MAP)
2336 return -EINVAL;
2337
2338 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2339
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002340 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002341 if (IS_ERR(afi))
2342 return PTR_ERR(afi);
2343
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002344 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002345 if (IS_ERR(table))
2346 return PTR_ERR(table);
2347
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002348 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002349
2350 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2351 if (IS_ERR(set)) {
2352 if (PTR_ERR(set) != -ENOENT)
2353 return PTR_ERR(set);
2354 set = NULL;
2355 }
2356
2357 if (set != NULL) {
2358 if (nlh->nlmsg_flags & NLM_F_EXCL)
2359 return -EEXIST;
2360 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2361 return -EOPNOTSUPP;
2362 return 0;
2363 }
2364
2365 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2366 return -ENOENT;
2367
2368 ops = nft_select_set_ops(nla);
2369 if (IS_ERR(ops))
2370 return PTR_ERR(ops);
2371
2372 size = 0;
2373 if (ops->privsize != NULL)
2374 size = ops->privsize(nla);
2375
2376 err = -ENOMEM;
2377 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2378 if (set == NULL)
2379 goto err1;
2380
2381 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2382 err = nf_tables_set_alloc_name(&ctx, set, name);
2383 if (err < 0)
2384 goto err2;
2385
2386 INIT_LIST_HEAD(&set->bindings);
2387 set->ops = ops;
2388 set->ktype = ktype;
2389 set->klen = klen;
2390 set->dtype = dtype;
2391 set->dlen = dlen;
2392 set->flags = flags;
2393
2394 err = ops->init(set, nla);
2395 if (err < 0)
2396 goto err2;
2397
2398 list_add_tail(&set->list, &table->sets);
2399 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2400 return 0;
2401
2402err2:
2403 kfree(set);
2404err1:
2405 module_put(ops->owner);
2406 return err;
2407}
2408
2409static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2410{
2411 list_del(&set->list);
2412 if (!(set->flags & NFT_SET_ANONYMOUS))
2413 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2414
2415 set->ops->destroy(set);
2416 module_put(set->ops->owner);
2417 kfree(set);
2418}
2419
2420static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2421 const struct nlmsghdr *nlh,
2422 const struct nlattr * const nla[])
2423{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002424 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002425 struct nft_set *set;
2426 struct nft_ctx ctx;
2427 int err;
2428
2429 if (nla[NFTA_SET_TABLE] == NULL)
2430 return -EINVAL;
2431
2432 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2433 if (err < 0)
2434 return err;
2435
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002436 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2437 return -EAFNOSUPPORT;
2438
Patrick McHardy20a69342013-10-11 12:06:22 +02002439 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2440 if (IS_ERR(set))
2441 return PTR_ERR(set);
2442 if (!list_empty(&set->bindings))
2443 return -EBUSY;
2444
2445 nf_tables_set_destroy(&ctx, set);
2446 return 0;
2447}
2448
2449static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2450 const struct nft_set *set,
2451 const struct nft_set_iter *iter,
2452 const struct nft_set_elem *elem)
2453{
2454 enum nft_registers dreg;
2455
2456 dreg = nft_type_to_reg(set->dtype);
Pablo Neira Ayuso2ee0d3c2013-12-28 00:59:38 +01002457 return nft_validate_data_load(ctx, dreg, &elem->data,
2458 set->dtype == NFT_DATA_VERDICT ?
2459 NFT_DATA_VERDICT : NFT_DATA_VALUE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002460}
2461
2462int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2463 struct nft_set_binding *binding)
2464{
2465 struct nft_set_binding *i;
2466 struct nft_set_iter iter;
2467
2468 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2469 return -EBUSY;
2470
2471 if (set->flags & NFT_SET_MAP) {
2472 /* If the set is already bound to the same chain all
2473 * jumps are already validated for that chain.
2474 */
2475 list_for_each_entry(i, &set->bindings, list) {
2476 if (i->chain == binding->chain)
2477 goto bind;
2478 }
2479
2480 iter.skip = 0;
2481 iter.count = 0;
2482 iter.err = 0;
2483 iter.fn = nf_tables_bind_check_setelem;
2484
2485 set->ops->walk(ctx, set, &iter);
2486 if (iter.err < 0) {
2487 /* Destroy anonymous sets if binding fails */
2488 if (set->flags & NFT_SET_ANONYMOUS)
2489 nf_tables_set_destroy(ctx, set);
2490
2491 return iter.err;
2492 }
2493 }
2494bind:
2495 binding->chain = ctx->chain;
2496 list_add_tail(&binding->list, &set->bindings);
2497 return 0;
2498}
2499
2500void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2501 struct nft_set_binding *binding)
2502{
2503 list_del(&binding->list);
2504
2505 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2506 nf_tables_set_destroy(ctx, set);
2507}
2508
2509/*
2510 * Set elements
2511 */
2512
2513static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2514 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2515 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2516 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2517};
2518
2519static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2520 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2521 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2522 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2523};
2524
2525static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2526 const struct sk_buff *skb,
2527 const struct nlmsghdr *nlh,
2528 const struct nlattr * const nla[])
2529{
2530 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2531 const struct nft_af_info *afi;
2532 const struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002533 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002534
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002535 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002536 if (IS_ERR(afi))
2537 return PTR_ERR(afi);
2538
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002539 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002540 if (IS_ERR(table))
2541 return PTR_ERR(table);
2542
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002543 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002544 return 0;
2545}
2546
2547static int nf_tables_fill_setelem(struct sk_buff *skb,
2548 const struct nft_set *set,
2549 const struct nft_set_elem *elem)
2550{
2551 unsigned char *b = skb_tail_pointer(skb);
2552 struct nlattr *nest;
2553
2554 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2555 if (nest == NULL)
2556 goto nla_put_failure;
2557
2558 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2559 set->klen) < 0)
2560 goto nla_put_failure;
2561
2562 if (set->flags & NFT_SET_MAP &&
2563 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2564 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2565 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2566 set->dlen) < 0)
2567 goto nla_put_failure;
2568
2569 if (elem->flags != 0)
2570 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2571 goto nla_put_failure;
2572
2573 nla_nest_end(skb, nest);
2574 return 0;
2575
2576nla_put_failure:
2577 nlmsg_trim(skb, b);
2578 return -EMSGSIZE;
2579}
2580
2581struct nft_set_dump_args {
2582 const struct netlink_callback *cb;
2583 struct nft_set_iter iter;
2584 struct sk_buff *skb;
2585};
2586
2587static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2588 const struct nft_set *set,
2589 const struct nft_set_iter *iter,
2590 const struct nft_set_elem *elem)
2591{
2592 struct nft_set_dump_args *args;
2593
2594 args = container_of(iter, struct nft_set_dump_args, iter);
2595 return nf_tables_fill_setelem(args->skb, set, elem);
2596}
2597
2598static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2599{
2600 const struct nft_set *set;
2601 struct nft_set_dump_args args;
2602 struct nft_ctx ctx;
2603 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2604 struct nfgenmsg *nfmsg;
2605 struct nlmsghdr *nlh;
2606 struct nlattr *nest;
2607 u32 portid, seq;
2608 int event, err;
2609
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01002610 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2611 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002612 if (err < 0)
2613 return err;
2614
2615 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2616 if (err < 0)
2617 return err;
2618
2619 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2620 if (IS_ERR(set))
2621 return PTR_ERR(set);
2622
2623 event = NFT_MSG_NEWSETELEM;
2624 event |= NFNL_SUBSYS_NFTABLES << 8;
2625 portid = NETLINK_CB(cb->skb).portid;
2626 seq = cb->nlh->nlmsg_seq;
2627
2628 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2629 NLM_F_MULTI);
2630 if (nlh == NULL)
2631 goto nla_put_failure;
2632
2633 nfmsg = nlmsg_data(nlh);
2634 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2635 nfmsg->version = NFNETLINK_V0;
2636 nfmsg->res_id = 0;
2637
2638 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2639 goto nla_put_failure;
2640 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2641 goto nla_put_failure;
2642
2643 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2644 if (nest == NULL)
2645 goto nla_put_failure;
2646
2647 args.cb = cb;
2648 args.skb = skb;
2649 args.iter.skip = cb->args[0];
2650 args.iter.count = 0;
2651 args.iter.err = 0;
2652 args.iter.fn = nf_tables_dump_setelem;
2653 set->ops->walk(&ctx, set, &args.iter);
2654
2655 nla_nest_end(skb, nest);
2656 nlmsg_end(skb, nlh);
2657
2658 if (args.iter.err && args.iter.err != -EMSGSIZE)
2659 return args.iter.err;
2660 if (args.iter.count == cb->args[0])
2661 return 0;
2662
2663 cb->args[0] = args.iter.count;
2664 return skb->len;
2665
2666nla_put_failure:
2667 return -ENOSPC;
2668}
2669
2670static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2671 const struct nlmsghdr *nlh,
2672 const struct nlattr * const nla[])
2673{
2674 const struct nft_set *set;
2675 struct nft_ctx ctx;
2676 int err;
2677
2678 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2679 if (err < 0)
2680 return err;
2681
2682 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2683 if (IS_ERR(set))
2684 return PTR_ERR(set);
2685
2686 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2687 struct netlink_dump_control c = {
2688 .dump = nf_tables_dump_set,
2689 };
2690 return netlink_dump_start(nlsk, skb, nlh, &c);
2691 }
2692 return -EOPNOTSUPP;
2693}
2694
2695static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2696 const struct nlattr *attr)
2697{
2698 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2699 struct nft_data_desc d1, d2;
2700 struct nft_set_elem elem;
2701 struct nft_set_binding *binding;
2702 enum nft_registers dreg;
2703 int err;
2704
2705 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2706 nft_set_elem_policy);
2707 if (err < 0)
2708 return err;
2709
2710 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2711 return -EINVAL;
2712
2713 elem.flags = 0;
2714 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2715 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2716 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2717 return -EINVAL;
2718 }
2719
2720 if (set->flags & NFT_SET_MAP) {
2721 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2722 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2723 return -EINVAL;
2724 } else {
2725 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2726 return -EINVAL;
2727 }
2728
2729 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2730 if (err < 0)
2731 goto err1;
2732 err = -EINVAL;
2733 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2734 goto err2;
2735
2736 err = -EEXIST;
2737 if (set->ops->get(set, &elem) == 0)
2738 goto err2;
2739
2740 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2741 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2742 if (err < 0)
2743 goto err2;
2744
2745 err = -EINVAL;
2746 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2747 goto err3;
2748
2749 dreg = nft_type_to_reg(set->dtype);
2750 list_for_each_entry(binding, &set->bindings, list) {
2751 struct nft_ctx bind_ctx = {
2752 .afi = ctx->afi,
2753 .table = ctx->table,
2754 .chain = binding->chain,
2755 };
2756
2757 err = nft_validate_data_load(&bind_ctx, dreg,
2758 &elem.data, d2.type);
2759 if (err < 0)
2760 goto err3;
2761 }
2762 }
2763
2764 err = set->ops->insert(set, &elem);
2765 if (err < 0)
2766 goto err3;
2767
2768 return 0;
2769
2770err3:
2771 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2772 nft_data_uninit(&elem.data, d2.type);
2773err2:
2774 nft_data_uninit(&elem.key, d1.type);
2775err1:
2776 return err;
2777}
2778
2779static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2780 const struct nlmsghdr *nlh,
2781 const struct nlattr * const nla[])
2782{
2783 const struct nlattr *attr;
2784 struct nft_set *set;
2785 struct nft_ctx ctx;
2786 int rem, err;
2787
2788 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2789 if (err < 0)
2790 return err;
2791
2792 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2793 if (IS_ERR(set))
2794 return PTR_ERR(set);
2795 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2796 return -EBUSY;
2797
2798 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2799 err = nft_add_set_elem(&ctx, set, attr);
2800 if (err < 0)
2801 return err;
2802 }
2803 return 0;
2804}
2805
2806static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2807 const struct nlattr *attr)
2808{
2809 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2810 struct nft_data_desc desc;
2811 struct nft_set_elem elem;
2812 int err;
2813
2814 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2815 nft_set_elem_policy);
2816 if (err < 0)
2817 goto err1;
2818
2819 err = -EINVAL;
2820 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2821 goto err1;
2822
2823 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2824 if (err < 0)
2825 goto err1;
2826
2827 err = -EINVAL;
2828 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2829 goto err2;
2830
2831 err = set->ops->get(set, &elem);
2832 if (err < 0)
2833 goto err2;
2834
2835 set->ops->remove(set, &elem);
2836
2837 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2838 if (set->flags & NFT_SET_MAP)
2839 nft_data_uninit(&elem.data, set->dtype);
2840
2841err2:
2842 nft_data_uninit(&elem.key, desc.type);
2843err1:
2844 return err;
2845}
2846
2847static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2848 const struct nlmsghdr *nlh,
2849 const struct nlattr * const nla[])
2850{
2851 const struct nlattr *attr;
2852 struct nft_set *set;
2853 struct nft_ctx ctx;
2854 int rem, err;
2855
2856 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2857 if (err < 0)
2858 return err;
2859
2860 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2861 if (IS_ERR(set))
2862 return PTR_ERR(set);
2863 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2864 return -EBUSY;
2865
2866 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2867 err = nft_del_setelem(&ctx, set, attr);
2868 if (err < 0)
2869 return err;
2870 }
2871 return 0;
2872}
2873
Patrick McHardy96518512013-10-14 11:00:02 +02002874static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2875 [NFT_MSG_NEWTABLE] = {
2876 .call = nf_tables_newtable,
2877 .attr_count = NFTA_TABLE_MAX,
2878 .policy = nft_table_policy,
2879 },
2880 [NFT_MSG_GETTABLE] = {
2881 .call = nf_tables_gettable,
2882 .attr_count = NFTA_TABLE_MAX,
2883 .policy = nft_table_policy,
2884 },
2885 [NFT_MSG_DELTABLE] = {
2886 .call = nf_tables_deltable,
2887 .attr_count = NFTA_TABLE_MAX,
2888 .policy = nft_table_policy,
2889 },
2890 [NFT_MSG_NEWCHAIN] = {
2891 .call = nf_tables_newchain,
2892 .attr_count = NFTA_CHAIN_MAX,
2893 .policy = nft_chain_policy,
2894 },
2895 [NFT_MSG_GETCHAIN] = {
2896 .call = nf_tables_getchain,
2897 .attr_count = NFTA_CHAIN_MAX,
2898 .policy = nft_chain_policy,
2899 },
2900 [NFT_MSG_DELCHAIN] = {
2901 .call = nf_tables_delchain,
2902 .attr_count = NFTA_CHAIN_MAX,
2903 .policy = nft_chain_policy,
2904 },
2905 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002906 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02002907 .attr_count = NFTA_RULE_MAX,
2908 .policy = nft_rule_policy,
2909 },
2910 [NFT_MSG_GETRULE] = {
2911 .call = nf_tables_getrule,
2912 .attr_count = NFTA_RULE_MAX,
2913 .policy = nft_rule_policy,
2914 },
2915 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002916 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02002917 .attr_count = NFTA_RULE_MAX,
2918 .policy = nft_rule_policy,
2919 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002920 [NFT_MSG_NEWSET] = {
2921 .call = nf_tables_newset,
2922 .attr_count = NFTA_SET_MAX,
2923 .policy = nft_set_policy,
2924 },
2925 [NFT_MSG_GETSET] = {
2926 .call = nf_tables_getset,
2927 .attr_count = NFTA_SET_MAX,
2928 .policy = nft_set_policy,
2929 },
2930 [NFT_MSG_DELSET] = {
2931 .call = nf_tables_delset,
2932 .attr_count = NFTA_SET_MAX,
2933 .policy = nft_set_policy,
2934 },
2935 [NFT_MSG_NEWSETELEM] = {
2936 .call = nf_tables_newsetelem,
2937 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2938 .policy = nft_set_elem_list_policy,
2939 },
2940 [NFT_MSG_GETSETELEM] = {
2941 .call = nf_tables_getsetelem,
2942 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2943 .policy = nft_set_elem_list_policy,
2944 },
2945 [NFT_MSG_DELSETELEM] = {
2946 .call = nf_tables_delsetelem,
2947 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2948 .policy = nft_set_elem_list_policy,
2949 },
Patrick McHardy96518512013-10-14 11:00:02 +02002950};
2951
2952static const struct nfnetlink_subsystem nf_tables_subsys = {
2953 .name = "nf_tables",
2954 .subsys_id = NFNL_SUBSYS_NFTABLES,
2955 .cb_count = NFT_MSG_MAX,
2956 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002957 .commit = nf_tables_commit,
2958 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02002959};
2960
Patrick McHardy20a69342013-10-11 12:06:22 +02002961/*
2962 * Loop detection - walk through the ruleset beginning at the destination chain
2963 * of a new jump until either the source chain is reached (loop) or all
2964 * reachable chains have been traversed.
2965 *
2966 * The loop check is performed whenever a new jump verdict is added to an
2967 * expression or verdict map or a verdict map is bound to a new chain.
2968 */
2969
2970static int nf_tables_check_loops(const struct nft_ctx *ctx,
2971 const struct nft_chain *chain);
2972
2973static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2974 const struct nft_set *set,
2975 const struct nft_set_iter *iter,
2976 const struct nft_set_elem *elem)
2977{
2978 switch (elem->data.verdict) {
2979 case NFT_JUMP:
2980 case NFT_GOTO:
2981 return nf_tables_check_loops(ctx, elem->data.chain);
2982 default:
2983 return 0;
2984 }
2985}
2986
2987static int nf_tables_check_loops(const struct nft_ctx *ctx,
2988 const struct nft_chain *chain)
2989{
2990 const struct nft_rule *rule;
2991 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02002992 const struct nft_set *set;
2993 struct nft_set_binding *binding;
2994 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02002995
2996 if (ctx->chain == chain)
2997 return -ELOOP;
2998
2999 list_for_each_entry(rule, &chain->rules, list) {
3000 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003001 const struct nft_data *data = NULL;
3002 int err;
3003
3004 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02003005 continue;
3006
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003007 err = expr->ops->validate(ctx, expr, &data);
3008 if (err < 0)
3009 return err;
3010
Patrick McHardy20a69342013-10-11 12:06:22 +02003011 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003012 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02003013
3014 switch (data->verdict) {
3015 case NFT_JUMP:
3016 case NFT_GOTO:
3017 err = nf_tables_check_loops(ctx, data->chain);
3018 if (err < 0)
3019 return err;
3020 default:
3021 break;
3022 }
3023 }
3024 }
3025
3026 list_for_each_entry(set, &ctx->table->sets, list) {
3027 if (!(set->flags & NFT_SET_MAP) ||
3028 set->dtype != NFT_DATA_VERDICT)
3029 continue;
3030
3031 list_for_each_entry(binding, &set->bindings, list) {
3032 if (binding->chain != chain)
3033 continue;
3034
3035 iter.skip = 0;
3036 iter.count = 0;
3037 iter.err = 0;
3038 iter.fn = nf_tables_loop_check_setelem;
3039
3040 set->ops->walk(ctx, set, &iter);
3041 if (iter.err < 0)
3042 return iter.err;
3043 }
3044 }
3045
3046 return 0;
3047}
3048
Patrick McHardy96518512013-10-14 11:00:02 +02003049/**
3050 * nft_validate_input_register - validate an expressions' input register
3051 *
3052 * @reg: the register number
3053 *
3054 * Validate that the input register is one of the general purpose
3055 * registers.
3056 */
3057int nft_validate_input_register(enum nft_registers reg)
3058{
3059 if (reg <= NFT_REG_VERDICT)
3060 return -EINVAL;
3061 if (reg > NFT_REG_MAX)
3062 return -ERANGE;
3063 return 0;
3064}
3065EXPORT_SYMBOL_GPL(nft_validate_input_register);
3066
3067/**
3068 * nft_validate_output_register - validate an expressions' output register
3069 *
3070 * @reg: the register number
3071 *
3072 * Validate that the output register is one of the general purpose
3073 * registers or the verdict register.
3074 */
3075int nft_validate_output_register(enum nft_registers reg)
3076{
3077 if (reg < NFT_REG_VERDICT)
3078 return -EINVAL;
3079 if (reg > NFT_REG_MAX)
3080 return -ERANGE;
3081 return 0;
3082}
3083EXPORT_SYMBOL_GPL(nft_validate_output_register);
3084
3085/**
3086 * nft_validate_data_load - validate an expressions' data load
3087 *
3088 * @ctx: context of the expression performing the load
3089 * @reg: the destination register number
3090 * @data: the data to load
3091 * @type: the data type
3092 *
3093 * Validate that a data load uses the appropriate data type for
3094 * the destination register. A value of NULL for the data means
3095 * that its runtime gathered data, which is always of type
3096 * NFT_DATA_VALUE.
3097 */
3098int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3099 const struct nft_data *data,
3100 enum nft_data_types type)
3101{
Patrick McHardy20a69342013-10-11 12:06:22 +02003102 int err;
3103
Patrick McHardy96518512013-10-14 11:00:02 +02003104 switch (reg) {
3105 case NFT_REG_VERDICT:
3106 if (data == NULL || type != NFT_DATA_VERDICT)
3107 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003108
3109 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3110 err = nf_tables_check_loops(ctx, data->chain);
3111 if (err < 0)
3112 return err;
3113
3114 if (ctx->chain->level + 1 > data->chain->level) {
3115 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3116 return -EMLINK;
3117 data->chain->level = ctx->chain->level + 1;
3118 }
3119 }
3120
Patrick McHardy96518512013-10-14 11:00:02 +02003121 return 0;
3122 default:
3123 if (data != NULL && type != NFT_DATA_VALUE)
3124 return -EINVAL;
3125 return 0;
3126 }
3127}
3128EXPORT_SYMBOL_GPL(nft_validate_data_load);
3129
3130static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3131 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3132 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3133 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3134};
3135
3136static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3137 struct nft_data_desc *desc, const struct nlattr *nla)
3138{
3139 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3140 struct nft_chain *chain;
3141 int err;
3142
3143 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3144 if (err < 0)
3145 return err;
3146
3147 if (!tb[NFTA_VERDICT_CODE])
3148 return -EINVAL;
3149 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3150
3151 switch (data->verdict) {
3152 case NF_ACCEPT:
3153 case NF_DROP:
3154 case NF_QUEUE:
3155 case NFT_CONTINUE:
3156 case NFT_BREAK:
3157 case NFT_RETURN:
3158 desc->len = sizeof(data->verdict);
3159 break;
3160 case NFT_JUMP:
3161 case NFT_GOTO:
3162 if (!tb[NFTA_VERDICT_CHAIN])
3163 return -EINVAL;
3164 chain = nf_tables_chain_lookup(ctx->table,
3165 tb[NFTA_VERDICT_CHAIN]);
3166 if (IS_ERR(chain))
3167 return PTR_ERR(chain);
3168 if (chain->flags & NFT_BASE_CHAIN)
3169 return -EOPNOTSUPP;
3170
Patrick McHardy96518512013-10-14 11:00:02 +02003171 chain->use++;
3172 data->chain = chain;
3173 desc->len = sizeof(data);
3174 break;
3175 default:
3176 return -EINVAL;
3177 }
3178
3179 desc->type = NFT_DATA_VERDICT;
3180 return 0;
3181}
3182
3183static void nft_verdict_uninit(const struct nft_data *data)
3184{
3185 switch (data->verdict) {
3186 case NFT_JUMP:
3187 case NFT_GOTO:
3188 data->chain->use--;
3189 break;
3190 }
3191}
3192
3193static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3194{
3195 struct nlattr *nest;
3196
3197 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3198 if (!nest)
3199 goto nla_put_failure;
3200
3201 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3202 goto nla_put_failure;
3203
3204 switch (data->verdict) {
3205 case NFT_JUMP:
3206 case NFT_GOTO:
3207 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3208 goto nla_put_failure;
3209 }
3210 nla_nest_end(skb, nest);
3211 return 0;
3212
3213nla_put_failure:
3214 return -1;
3215}
3216
3217static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3218 struct nft_data_desc *desc, const struct nlattr *nla)
3219{
3220 unsigned int len;
3221
3222 len = nla_len(nla);
3223 if (len == 0)
3224 return -EINVAL;
3225 if (len > sizeof(data->data))
3226 return -EOVERFLOW;
3227
3228 nla_memcpy(data->data, nla, sizeof(data->data));
3229 desc->type = NFT_DATA_VALUE;
3230 desc->len = len;
3231 return 0;
3232}
3233
3234static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3235 unsigned int len)
3236{
3237 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3238}
3239
3240static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3241 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3242 .len = FIELD_SIZEOF(struct nft_data, data) },
3243 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3244};
3245
3246/**
3247 * nft_data_init - parse nf_tables data netlink attributes
3248 *
3249 * @ctx: context of the expression using the data
3250 * @data: destination struct nft_data
3251 * @desc: data description
3252 * @nla: netlink attribute containing data
3253 *
3254 * Parse the netlink data attributes and initialize a struct nft_data.
3255 * The type and length of data are returned in the data description.
3256 *
3257 * The caller can indicate that it only wants to accept data of type
3258 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3259 */
3260int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3261 struct nft_data_desc *desc, const struct nlattr *nla)
3262{
3263 struct nlattr *tb[NFTA_DATA_MAX + 1];
3264 int err;
3265
3266 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3267 if (err < 0)
3268 return err;
3269
3270 if (tb[NFTA_DATA_VALUE])
3271 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3272 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3273 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3274 return -EINVAL;
3275}
3276EXPORT_SYMBOL_GPL(nft_data_init);
3277
3278/**
3279 * nft_data_uninit - release a nft_data item
3280 *
3281 * @data: struct nft_data to release
3282 * @type: type of data
3283 *
3284 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3285 * all others need to be released by calling this function.
3286 */
3287void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3288{
3289 switch (type) {
3290 case NFT_DATA_VALUE:
3291 return;
3292 case NFT_DATA_VERDICT:
3293 return nft_verdict_uninit(data);
3294 default:
3295 WARN_ON(1);
3296 }
3297}
3298EXPORT_SYMBOL_GPL(nft_data_uninit);
3299
3300int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3301 enum nft_data_types type, unsigned int len)
3302{
3303 struct nlattr *nest;
3304 int err;
3305
3306 nest = nla_nest_start(skb, attr);
3307 if (nest == NULL)
3308 return -1;
3309
3310 switch (type) {
3311 case NFT_DATA_VALUE:
3312 err = nft_value_dump(skb, data, len);
3313 break;
3314 case NFT_DATA_VERDICT:
3315 err = nft_verdict_dump(skb, data);
3316 break;
3317 default:
3318 err = -EINVAL;
3319 WARN_ON(1);
3320 }
3321
3322 nla_nest_end(skb, nest);
3323 return err;
3324}
3325EXPORT_SYMBOL_GPL(nft_data_dump);
3326
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003327static int nf_tables_init_net(struct net *net)
3328{
3329 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003330 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003331 return 0;
3332}
3333
3334static struct pernet_operations nf_tables_net_ops = {
3335 .init = nf_tables_init_net,
3336};
3337
Patrick McHardy96518512013-10-14 11:00:02 +02003338static int __init nf_tables_module_init(void)
3339{
3340 int err;
3341
3342 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3343 GFP_KERNEL);
3344 if (info == NULL) {
3345 err = -ENOMEM;
3346 goto err1;
3347 }
3348
3349 err = nf_tables_core_module_init();
3350 if (err < 0)
3351 goto err2;
3352
3353 err = nfnetlink_subsys_register(&nf_tables_subsys);
3354 if (err < 0)
3355 goto err3;
3356
3357 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003358 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003359err3:
3360 nf_tables_core_module_exit();
3361err2:
3362 kfree(info);
3363err1:
3364 return err;
3365}
3366
3367static void __exit nf_tables_module_exit(void)
3368{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003369 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003370 nfnetlink_subsys_unregister(&nf_tables_subsys);
3371 nf_tables_core_module_exit();
3372 kfree(info);
3373}
3374
3375module_init(nf_tables_module_init);
3376module_exit(nf_tables_module_exit);
3377
3378MODULE_LICENSE("GPL");
3379MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3380MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);