blob: 117bbaaddde636a7b5cbf754012ab2f696898e71 [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]) {
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000369 u32 flags;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200370
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;
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000405 u32 flags = 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200406
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200407 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200408 if (IS_ERR(afi))
409 return PTR_ERR(afi);
410
411 name = nla[NFTA_TABLE_NAME];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200412 table = nf_tables_table_lookup(afi, name);
Patrick McHardy96518512013-10-14 11:00:02 +0200413 if (IS_ERR(table)) {
414 if (PTR_ERR(table) != -ENOENT)
415 return PTR_ERR(table);
416 table = NULL;
417 }
418
419 if (table != NULL) {
420 if (nlh->nlmsg_flags & NLM_F_EXCL)
421 return -EEXIST;
422 if (nlh->nlmsg_flags & NLM_F_REPLACE)
423 return -EOPNOTSUPP;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200424 return nf_tables_updtable(nlsk, skb, nlh, nla, afi, table);
Patrick McHardy96518512013-10-14 11:00:02 +0200425 }
426
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000427 if (nla[NFTA_TABLE_FLAGS]) {
428 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
429 if (flags & ~NFT_TABLE_F_DORMANT)
430 return -EINVAL;
431 }
432
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000433 if (!try_module_get(afi->owner))
434 return -EAFNOSUPPORT;
435
Patrick McHardy96518512013-10-14 11:00:02 +0200436 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000437 if (table == NULL) {
438 module_put(afi->owner);
Patrick McHardy96518512013-10-14 11:00:02 +0200439 return -ENOMEM;
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000440 }
Patrick McHardy96518512013-10-14 11:00:02 +0200441
442 nla_strlcpy(table->name, name, nla_len(name));
443 INIT_LIST_HEAD(&table->chains);
Patrick McHardy20a69342013-10-11 12:06:22 +0200444 INIT_LIST_HEAD(&table->sets);
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000445 table->flags = flags;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200446
Patrick McHardy96518512013-10-14 11:00:02 +0200447 list_add_tail(&table->list, &afi->tables);
448 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
449 return 0;
450}
451
452static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
453 const struct nlmsghdr *nlh,
454 const struct nlattr * const nla[])
455{
456 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
457 struct nft_af_info *afi;
458 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200459 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200460 int family = nfmsg->nfgen_family;
461
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200462 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200463 if (IS_ERR(afi))
464 return PTR_ERR(afi);
465
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200466 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200467 if (IS_ERR(table))
468 return PTR_ERR(table);
469
Patrick McHardy44a6f0d2014-01-09 18:42:41 +0000470 if (!list_empty(&table->chains) || !list_empty(&table->sets))
Patrick McHardy96518512013-10-14 11:00:02 +0200471 return -EBUSY;
472
473 list_del(&table->list);
474 nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
475 kfree(table);
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000476 module_put(afi->owner);
Patrick McHardy96518512013-10-14 11:00:02 +0200477 return 0;
478}
479
Patrick McHardy2a37d752014-01-09 18:42:37 +0000480int nft_register_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200481{
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200482 int err = 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200483
484 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200485 if (chain_type[ctype->family][ctype->type] != NULL) {
486 err = -EBUSY;
487 goto out;
Patrick McHardy96518512013-10-14 11:00:02 +0200488 }
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200489 chain_type[ctype->family][ctype->type] = ctype;
490out:
Patrick McHardy96518512013-10-14 11:00:02 +0200491 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
492 return err;
493}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200494EXPORT_SYMBOL_GPL(nft_register_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200495
Patrick McHardy2a37d752014-01-09 18:42:37 +0000496void nft_unregister_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200497{
Patrick McHardy96518512013-10-14 11:00:02 +0200498 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200499 chain_type[ctype->family][ctype->type] = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200500 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
501}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200502EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200503
504/*
505 * Chains
506 */
507
508static struct nft_chain *
509nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
510{
511 struct nft_chain *chain;
512
513 list_for_each_entry(chain, &table->chains, list) {
514 if (chain->handle == handle)
515 return chain;
516 }
517
518 return ERR_PTR(-ENOENT);
519}
520
521static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
522 const struct nlattr *nla)
523{
524 struct nft_chain *chain;
525
526 if (nla == NULL)
527 return ERR_PTR(-EINVAL);
528
529 list_for_each_entry(chain, &table->chains, list) {
530 if (!nla_strcmp(nla, chain->name))
531 return chain;
532 }
533
534 return ERR_PTR(-ENOENT);
535}
536
537static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
538 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
539 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
540 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
541 .len = NFT_CHAIN_MAXNAMELEN - 1 },
542 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200543 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200544 [NFTA_CHAIN_TYPE] = { .type = NLA_NUL_STRING },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200545 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
Patrick McHardy96518512013-10-14 11:00:02 +0200546};
547
548static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
549 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
550 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
551};
552
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200553static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
554{
555 struct nft_stats *cpu_stats, total;
556 struct nlattr *nest;
557 int cpu;
558
559 memset(&total, 0, sizeof(total));
560 for_each_possible_cpu(cpu) {
561 cpu_stats = per_cpu_ptr(stats, cpu);
562 total.pkts += cpu_stats->pkts;
563 total.bytes += cpu_stats->bytes;
564 }
565 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
566 if (nest == NULL)
567 goto nla_put_failure;
568
569 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
570 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
571 goto nla_put_failure;
572
573 nla_nest_end(skb, nest);
574 return 0;
575
576nla_put_failure:
577 return -ENOSPC;
578}
579
Patrick McHardy96518512013-10-14 11:00:02 +0200580static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
581 int event, u32 flags, int family,
582 const struct nft_table *table,
583 const struct nft_chain *chain)
584{
585 struct nlmsghdr *nlh;
586 struct nfgenmsg *nfmsg;
587
588 event |= NFNL_SUBSYS_NFTABLES << 8;
589 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
590 if (nlh == NULL)
591 goto nla_put_failure;
592
593 nfmsg = nlmsg_data(nlh);
594 nfmsg->nfgen_family = family;
595 nfmsg->version = NFNETLINK_V0;
596 nfmsg->res_id = 0;
597
598 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
599 goto nla_put_failure;
600 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
601 goto nla_put_failure;
602 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
603 goto nla_put_failure;
604
605 if (chain->flags & NFT_BASE_CHAIN) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200606 const struct nft_base_chain *basechain = nft_base_chain(chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000607 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200608 struct nlattr *nest;
609
610 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
Patrick McHardy96518512013-10-14 11:00:02 +0200611 if (nest == NULL)
612 goto nla_put_failure;
613 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
614 goto nla_put_failure;
615 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
616 goto nla_put_failure;
617 nla_nest_end(skb, nest);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200618
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200619 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
620 htonl(basechain->policy)))
621 goto nla_put_failure;
622
Patrick McHardybaae3e62014-01-09 18:42:34 +0000623 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
624 goto nla_put_failure;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200625
626 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
627 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +0200628 }
629
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200630 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
631 goto nla_put_failure;
632
Patrick McHardy96518512013-10-14 11:00:02 +0200633 return nlmsg_end(skb, nlh);
634
635nla_put_failure:
636 nlmsg_trim(skb, nlh);
637 return -1;
638}
639
640static int nf_tables_chain_notify(const struct sk_buff *oskb,
641 const struct nlmsghdr *nlh,
642 const struct nft_table *table,
643 const struct nft_chain *chain,
644 int event, int family)
645{
646 struct sk_buff *skb;
647 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
648 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
649 u32 seq = nlh ? nlh->nlmsg_seq : 0;
650 bool report;
651 int err;
652
653 report = nlh ? nlmsg_report(nlh) : false;
654 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
655 return 0;
656
657 err = -ENOBUFS;
658 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
659 if (skb == NULL)
660 goto err;
661
662 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
663 table, chain);
664 if (err < 0) {
665 kfree_skb(skb);
666 goto err;
667 }
668
669 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
670 GFP_KERNEL);
671err:
672 if (err < 0)
673 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
674 return err;
675}
676
677static int nf_tables_dump_chains(struct sk_buff *skb,
678 struct netlink_callback *cb)
679{
680 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
681 const struct nft_af_info *afi;
682 const struct nft_table *table;
683 const struct nft_chain *chain;
684 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200685 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200686 int family = nfmsg->nfgen_family;
687
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200688 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200689 if (family != NFPROTO_UNSPEC && family != afi->family)
690 continue;
691
692 list_for_each_entry(table, &afi->tables, list) {
693 list_for_each_entry(chain, &table->chains, list) {
694 if (idx < s_idx)
695 goto cont;
696 if (idx > s_idx)
697 memset(&cb->args[1], 0,
698 sizeof(cb->args) - sizeof(cb->args[0]));
699 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
700 cb->nlh->nlmsg_seq,
701 NFT_MSG_NEWCHAIN,
702 NLM_F_MULTI,
703 afi->family, table, chain) < 0)
704 goto done;
705cont:
706 idx++;
707 }
708 }
709 }
710done:
711 cb->args[0] = idx;
712 return skb->len;
713}
714
715
716static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
717 const struct nlmsghdr *nlh,
718 const struct nlattr * const nla[])
719{
720 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
721 const struct nft_af_info *afi;
722 const struct nft_table *table;
723 const struct nft_chain *chain;
724 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200725 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200726 int family = nfmsg->nfgen_family;
727 int err;
728
729 if (nlh->nlmsg_flags & NLM_F_DUMP) {
730 struct netlink_dump_control c = {
731 .dump = nf_tables_dump_chains,
732 };
733 return netlink_dump_start(nlsk, skb, nlh, &c);
734 }
735
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200736 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200737 if (IS_ERR(afi))
738 return PTR_ERR(afi);
739
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200740 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200741 if (IS_ERR(table))
742 return PTR_ERR(table);
743
744 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
745 if (IS_ERR(chain))
746 return PTR_ERR(chain);
747
748 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
749 if (!skb2)
750 return -ENOMEM;
751
752 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
753 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
754 family, table, chain);
755 if (err < 0)
756 goto err;
757
758 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
759
760err:
761 kfree_skb(skb2);
762 return err;
763}
764
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200765static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
766 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
767 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
768};
769
770static int
771nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
772{
773 struct nlattr *tb[NFTA_COUNTER_MAX+1];
774 struct nft_stats __percpu *newstats;
775 struct nft_stats *stats;
776 int err;
777
778 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
779 if (err < 0)
780 return err;
781
782 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
783 return -EINVAL;
784
785 newstats = alloc_percpu(struct nft_stats);
786 if (newstats == NULL)
787 return -ENOMEM;
788
789 /* Restore old counters on this cpu, no problem. Per-cpu statistics
790 * are not exposed to userspace.
791 */
792 stats = this_cpu_ptr(newstats);
793 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
794 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
795
796 if (chain->stats) {
797 /* nfnl_lock is held, add some nfnl function for this, later */
798 struct nft_stats __percpu *oldstats =
799 rcu_dereference_protected(chain->stats, 1);
800
801 rcu_assign_pointer(chain->stats, newstats);
802 synchronize_rcu();
803 free_percpu(oldstats);
804 } else
805 rcu_assign_pointer(chain->stats, newstats);
806
807 return 0;
808}
809
Patrick McHardy96518512013-10-14 11:00:02 +0200810static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
811 const struct nlmsghdr *nlh,
812 const struct nlattr * const nla[])
813{
814 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
815 const struct nlattr * uninitialized_var(name);
816 const struct nft_af_info *afi;
817 struct nft_table *table;
818 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200819 struct nft_base_chain *basechain = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200820 struct nlattr *ha[NFTA_HOOK_MAX + 1];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200821 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200822 int family = nfmsg->nfgen_family;
Patrick McHardy57de2a02014-01-09 18:42:31 +0000823 u8 policy = NF_ACCEPT;
Patrick McHardy96518512013-10-14 11:00:02 +0200824 u64 handle = 0;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000825 unsigned int i;
Patrick McHardy96518512013-10-14 11:00:02 +0200826 int err;
827 bool create;
828
829 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
830
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200831 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200832 if (IS_ERR(afi))
833 return PTR_ERR(afi);
834
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200835 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200836 if (IS_ERR(table))
837 return PTR_ERR(table);
838
Patrick McHardy96518512013-10-14 11:00:02 +0200839 chain = NULL;
840 name = nla[NFTA_CHAIN_NAME];
841
842 if (nla[NFTA_CHAIN_HANDLE]) {
843 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
844 chain = nf_tables_chain_lookup_byhandle(table, handle);
845 if (IS_ERR(chain))
846 return PTR_ERR(chain);
847 } else {
848 chain = nf_tables_chain_lookup(table, name);
849 if (IS_ERR(chain)) {
850 if (PTR_ERR(chain) != -ENOENT)
851 return PTR_ERR(chain);
852 chain = NULL;
853 }
854 }
855
Patrick McHardy57de2a02014-01-09 18:42:31 +0000856 if (nla[NFTA_CHAIN_POLICY]) {
857 if ((chain != NULL &&
858 !(chain->flags & NFT_BASE_CHAIN)) ||
859 nla[NFTA_CHAIN_HOOK] == NULL)
860 return -EOPNOTSUPP;
861
Pablo Neira Ayuso8f46df12014-01-10 15:11:25 +0100862 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
Patrick McHardy57de2a02014-01-09 18:42:31 +0000863 switch (policy) {
864 case NF_DROP:
865 case NF_ACCEPT:
866 break;
867 default:
868 return -EINVAL;
869 }
870 }
871
Patrick McHardy96518512013-10-14 11:00:02 +0200872 if (chain != NULL) {
873 if (nlh->nlmsg_flags & NLM_F_EXCL)
874 return -EEXIST;
875 if (nlh->nlmsg_flags & NLM_F_REPLACE)
876 return -EOPNOTSUPP;
877
878 if (nla[NFTA_CHAIN_HANDLE] && name &&
879 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
880 return -EEXIST;
881
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200882 if (nla[NFTA_CHAIN_COUNTERS]) {
883 if (!(chain->flags & NFT_BASE_CHAIN))
884 return -EOPNOTSUPP;
885
886 err = nf_tables_counters(nft_base_chain(chain),
887 nla[NFTA_CHAIN_COUNTERS]);
888 if (err < 0)
889 return err;
890 }
891
Patrick McHardy4401a862014-01-09 18:42:32 +0000892 if (nla[NFTA_CHAIN_POLICY])
893 nft_base_chain(chain)->policy = policy;
894
Patrick McHardy96518512013-10-14 11:00:02 +0200895 if (nla[NFTA_CHAIN_HANDLE] && name)
896 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
897
898 goto notify;
899 }
900
Patrick McHardy75820672014-01-09 18:42:33 +0000901 if (table->use == UINT_MAX)
902 return -EOVERFLOW;
903
Patrick McHardy96518512013-10-14 11:00:02 +0200904 if (nla[NFTA_CHAIN_HOOK]) {
Patrick McHardy2a37d752014-01-09 18:42:37 +0000905 const struct nf_chain_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +0200906 struct nf_hook_ops *ops;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200907 nf_hookfn *hookfn;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000908 u32 hooknum, priority;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200909
Patrick McHardybaae3e62014-01-09 18:42:34 +0000910 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200911 if (nla[NFTA_CHAIN_TYPE]) {
912 type = nf_tables_chain_type_lookup(afi,
913 nla[NFTA_CHAIN_TYPE],
914 create);
Patrick McHardy93b08062014-01-09 18:42:36 +0000915 if (IS_ERR(type))
916 return PTR_ERR(type);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200917 }
Patrick McHardy96518512013-10-14 11:00:02 +0200918
919 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
920 nft_hook_policy);
921 if (err < 0)
922 return err;
923 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
924 ha[NFTA_HOOK_PRIORITY] == NULL)
925 return -EINVAL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200926
927 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
928 if (hooknum >= afi->nhooks)
Patrick McHardy96518512013-10-14 11:00:02 +0200929 return -EINVAL;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000930 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
Patrick McHardy96518512013-10-14 11:00:02 +0200931
Patrick McHardybaae3e62014-01-09 18:42:34 +0000932 if (!(type->hook_mask & (1 << hooknum)))
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200933 return -EOPNOTSUPP;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000934 if (!try_module_get(type->owner))
Patrick McHardybaae3e62014-01-09 18:42:34 +0000935 return -ENOENT;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000936 hookfn = type->hooks[hooknum];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200937
Patrick McHardy96518512013-10-14 11:00:02 +0200938 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
939 if (basechain == NULL)
940 return -ENOMEM;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200941
Patrick McHardy4401a862014-01-09 18:42:32 +0000942 if (nla[NFTA_CHAIN_COUNTERS]) {
943 err = nf_tables_counters(basechain,
944 nla[NFTA_CHAIN_COUNTERS]);
945 if (err < 0) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000946 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +0000947 kfree(basechain);
948 return err;
949 }
950 } else {
951 struct nft_stats __percpu *newstats;
952
953 newstats = alloc_percpu(struct nft_stats);
954 if (newstats == NULL) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000955 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +0000956 kfree(basechain);
957 return -ENOMEM;
958 }
959 rcu_assign_pointer(basechain->stats, newstats);
960 }
961
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200962 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +0200963 chain = &basechain->chain;
964
Patrick McHardy115a60b2014-01-03 12:16:15 +0000965 for (i = 0; i < afi->nops; i++) {
966 ops = &basechain->ops[i];
967 ops->pf = family;
968 ops->owner = afi->owner;
969 ops->hooknum = hooknum;
970 ops->priority = priority;
971 ops->priv = chain;
972 ops->hook = afi->hooks[ops->hooknum];
973 if (hookfn)
974 ops->hook = hookfn;
975 if (afi->hook_ops_init)
976 afi->hook_ops_init(ops, i);
977 }
Patrick McHardy96518512013-10-14 11:00:02 +0200978
979 chain->flags |= NFT_BASE_CHAIN;
Patrick McHardy57de2a02014-01-09 18:42:31 +0000980 basechain->policy = policy;
Patrick McHardy96518512013-10-14 11:00:02 +0200981 } else {
982 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
983 if (chain == NULL)
984 return -ENOMEM;
985 }
986
987 INIT_LIST_HEAD(&chain->rules);
988 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200989 chain->net = net;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +0200990 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +0200991 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
992
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200993 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
994 chain->flags & NFT_BASE_CHAIN) {
Patrick McHardy115a60b2014-01-03 12:16:15 +0000995 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200996 if (err < 0) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000997 module_put(basechain->type->owner);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200998 free_percpu(basechain->stats);
999 kfree(basechain);
1000 return err;
1001 }
1002 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001003 list_add_tail(&chain->list, &table->chains);
1004 table->use++;
Patrick McHardy96518512013-10-14 11:00:02 +02001005notify:
1006 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
1007 family);
1008 return 0;
1009}
1010
1011static void nf_tables_rcu_chain_destroy(struct rcu_head *head)
1012{
1013 struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
1014
1015 BUG_ON(chain->use > 0);
1016
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001017 if (chain->flags & NFT_BASE_CHAIN) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001018 module_put(nft_base_chain(chain)->type->owner);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001019 free_percpu(nft_base_chain(chain)->stats);
Patrick McHardy96518512013-10-14 11:00:02 +02001020 kfree(nft_base_chain(chain));
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001021 } else
Patrick McHardy96518512013-10-14 11:00:02 +02001022 kfree(chain);
1023}
1024
1025static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1026 const struct nlmsghdr *nlh,
1027 const struct nlattr * const nla[])
1028{
1029 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1030 const struct nft_af_info *afi;
1031 struct nft_table *table;
1032 struct nft_chain *chain;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001033 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001034 int family = nfmsg->nfgen_family;
1035
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001036 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001037 if (IS_ERR(afi))
1038 return PTR_ERR(afi);
1039
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001040 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001041 if (IS_ERR(table))
1042 return PTR_ERR(table);
1043
1044 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1045 if (IS_ERR(chain))
1046 return PTR_ERR(chain);
1047
Patrick McHardy96518512013-10-14 11:00:02 +02001048 if (!list_empty(&chain->rules))
1049 return -EBUSY;
1050
1051 list_del(&chain->list);
1052 table->use--;
1053
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001054 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1055 chain->flags & NFT_BASE_CHAIN)
Patrick McHardy115a60b2014-01-03 12:16:15 +00001056 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
Patrick McHardy96518512013-10-14 11:00:02 +02001057
1058 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1059 family);
1060
1061 /* Make sure all rule references are gone before this is released */
1062 call_rcu(&chain->rcu_head, nf_tables_rcu_chain_destroy);
1063 return 0;
1064}
1065
1066static void nft_ctx_init(struct nft_ctx *ctx,
Patrick McHardy20a69342013-10-11 12:06:22 +02001067 const struct sk_buff *skb,
1068 const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02001069 const struct nft_af_info *afi,
1070 const struct nft_table *table,
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001071 const struct nft_chain *chain,
1072 const struct nlattr * const *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001073{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001074 ctx->net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02001075 ctx->skb = skb;
1076 ctx->nlh = nlh;
Patrick McHardy96518512013-10-14 11:00:02 +02001077 ctx->afi = afi;
1078 ctx->table = table;
1079 ctx->chain = chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001080 ctx->nla = nla;
Patrick McHardy96518512013-10-14 11:00:02 +02001081}
1082
1083/*
1084 * Expressions
1085 */
1086
1087/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001088 * nft_register_expr - register nf_tables expr type
1089 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001090 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001091 * Registers the expr type for use with nf_tables. Returns zero on
Patrick McHardy96518512013-10-14 11:00:02 +02001092 * success or a negative errno code otherwise.
1093 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001094int nft_register_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001095{
1096 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001097 list_add_tail(&type->list, &nf_tables_expressions);
Patrick McHardy96518512013-10-14 11:00:02 +02001098 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1099 return 0;
1100}
1101EXPORT_SYMBOL_GPL(nft_register_expr);
1102
1103/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001104 * nft_unregister_expr - unregister nf_tables expr type
1105 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001106 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001107 * Unregisters the expr typefor use with nf_tables.
Patrick McHardy96518512013-10-14 11:00:02 +02001108 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001109void nft_unregister_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001110{
1111 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001112 list_del(&type->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001113 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1114}
1115EXPORT_SYMBOL_GPL(nft_unregister_expr);
1116
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001117static const struct nft_expr_type *__nft_expr_type_get(struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001118{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001119 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001120
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001121 list_for_each_entry(type, &nf_tables_expressions, list) {
1122 if (!nla_strcmp(nla, type->name))
1123 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001124 }
1125 return NULL;
1126}
1127
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001128static const struct nft_expr_type *nft_expr_type_get(struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001129{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001130 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001131
1132 if (nla == NULL)
1133 return ERR_PTR(-EINVAL);
1134
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001135 type = __nft_expr_type_get(nla);
1136 if (type != NULL && try_module_get(type->owner))
1137 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001138
1139#ifdef CONFIG_MODULES
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001140 if (type == NULL) {
Patrick McHardy96518512013-10-14 11:00:02 +02001141 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1142 request_module("nft-expr-%.*s",
1143 nla_len(nla), (char *)nla_data(nla));
1144 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001145 if (__nft_expr_type_get(nla))
Patrick McHardy96518512013-10-14 11:00:02 +02001146 return ERR_PTR(-EAGAIN);
1147 }
1148#endif
1149 return ERR_PTR(-ENOENT);
1150}
1151
1152static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1153 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1154 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1155};
1156
1157static int nf_tables_fill_expr_info(struct sk_buff *skb,
1158 const struct nft_expr *expr)
1159{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001160 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
Patrick McHardy96518512013-10-14 11:00:02 +02001161 goto nla_put_failure;
1162
1163 if (expr->ops->dump) {
1164 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1165 if (data == NULL)
1166 goto nla_put_failure;
1167 if (expr->ops->dump(skb, expr) < 0)
1168 goto nla_put_failure;
1169 nla_nest_end(skb, data);
1170 }
1171
1172 return skb->len;
1173
1174nla_put_failure:
1175 return -1;
1176};
1177
1178struct nft_expr_info {
1179 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001180 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001181};
1182
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001183static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1184 const struct nlattr *nla,
Patrick McHardy96518512013-10-14 11:00:02 +02001185 struct nft_expr_info *info)
1186{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001187 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001188 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001189 struct nlattr *tb[NFTA_EXPR_MAX + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001190 int err;
1191
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001192 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
Patrick McHardy96518512013-10-14 11:00:02 +02001193 if (err < 0)
1194 return err;
1195
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001196 type = nft_expr_type_get(tb[NFTA_EXPR_NAME]);
1197 if (IS_ERR(type))
1198 return PTR_ERR(type);
1199
1200 if (tb[NFTA_EXPR_DATA]) {
1201 err = nla_parse_nested(info->tb, type->maxattr,
1202 tb[NFTA_EXPR_DATA], type->policy);
1203 if (err < 0)
1204 goto err1;
1205 } else
1206 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1207
1208 if (type->select_ops != NULL) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001209 ops = type->select_ops(ctx,
1210 (const struct nlattr * const *)info->tb);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001211 if (IS_ERR(ops)) {
1212 err = PTR_ERR(ops);
1213 goto err1;
1214 }
1215 } else
1216 ops = type->ops;
1217
Patrick McHardy96518512013-10-14 11:00:02 +02001218 info->ops = ops;
1219 return 0;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001220
1221err1:
1222 module_put(type->owner);
1223 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001224}
1225
1226static int nf_tables_newexpr(const struct nft_ctx *ctx,
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001227 const struct nft_expr_info *info,
Patrick McHardy96518512013-10-14 11:00:02 +02001228 struct nft_expr *expr)
1229{
1230 const struct nft_expr_ops *ops = info->ops;
1231 int err;
1232
1233 expr->ops = ops;
1234 if (ops->init) {
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001235 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
Patrick McHardy96518512013-10-14 11:00:02 +02001236 if (err < 0)
1237 goto err1;
1238 }
1239
Patrick McHardy96518512013-10-14 11:00:02 +02001240 return 0;
1241
1242err1:
1243 expr->ops = NULL;
1244 return err;
1245}
1246
1247static void nf_tables_expr_destroy(struct nft_expr *expr)
1248{
1249 if (expr->ops->destroy)
1250 expr->ops->destroy(expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001251 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001252}
1253
1254/*
1255 * Rules
1256 */
1257
1258static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1259 u64 handle)
1260{
1261 struct nft_rule *rule;
1262
1263 // FIXME: this sucks
1264 list_for_each_entry(rule, &chain->rules, list) {
1265 if (handle == rule->handle)
1266 return rule;
1267 }
1268
1269 return ERR_PTR(-ENOENT);
1270}
1271
1272static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1273 const struct nlattr *nla)
1274{
1275 if (nla == NULL)
1276 return ERR_PTR(-EINVAL);
1277
1278 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1279}
1280
1281static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1282 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1283 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1284 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1285 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1286 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001287 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001288 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Patrick McHardy96518512013-10-14 11:00:02 +02001289};
1290
1291static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1292 int event, u32 flags, int family,
1293 const struct nft_table *table,
1294 const struct nft_chain *chain,
1295 const struct nft_rule *rule)
1296{
1297 struct nlmsghdr *nlh;
1298 struct nfgenmsg *nfmsg;
1299 const struct nft_expr *expr, *next;
1300 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001301 const struct nft_rule *prule;
1302 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001303
Eric Leblond5e948462013-10-10 13:41:44 +02001304 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001305 flags);
1306 if (nlh == NULL)
1307 goto nla_put_failure;
1308
1309 nfmsg = nlmsg_data(nlh);
1310 nfmsg->nfgen_family = family;
1311 nfmsg->version = NFNETLINK_V0;
1312 nfmsg->res_id = 0;
1313
1314 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1315 goto nla_put_failure;
1316 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1317 goto nla_put_failure;
1318 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1319 goto nla_put_failure;
1320
Eric Leblond5e948462013-10-10 13:41:44 +02001321 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1322 prule = list_entry(rule->list.prev, struct nft_rule, list);
1323 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1324 cpu_to_be64(prule->handle)))
1325 goto nla_put_failure;
1326 }
1327
Patrick McHardy96518512013-10-14 11:00:02 +02001328 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1329 if (list == NULL)
1330 goto nla_put_failure;
1331 nft_rule_for_each_expr(expr, next, rule) {
1332 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1333 if (elem == NULL)
1334 goto nla_put_failure;
1335 if (nf_tables_fill_expr_info(skb, expr) < 0)
1336 goto nla_put_failure;
1337 nla_nest_end(skb, elem);
1338 }
1339 nla_nest_end(skb, list);
1340
1341 return nlmsg_end(skb, nlh);
1342
1343nla_put_failure:
1344 nlmsg_trim(skb, nlh);
1345 return -1;
1346}
1347
1348static int nf_tables_rule_notify(const struct sk_buff *oskb,
1349 const struct nlmsghdr *nlh,
1350 const struct nft_table *table,
1351 const struct nft_chain *chain,
1352 const struct nft_rule *rule,
1353 int event, u32 flags, int family)
1354{
1355 struct sk_buff *skb;
1356 u32 portid = NETLINK_CB(oskb).portid;
1357 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1358 u32 seq = nlh->nlmsg_seq;
1359 bool report;
1360 int err;
1361
1362 report = nlmsg_report(nlh);
1363 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1364 return 0;
1365
1366 err = -ENOBUFS;
1367 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1368 if (skb == NULL)
1369 goto err;
1370
1371 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1372 family, table, chain, rule);
1373 if (err < 0) {
1374 kfree_skb(skb);
1375 goto err;
1376 }
1377
1378 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1379 GFP_KERNEL);
1380err:
1381 if (err < 0)
1382 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1383 return err;
1384}
1385
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001386static inline bool
1387nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1388{
1389 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1390}
1391
1392static inline int gencursor_next(struct net *net)
1393{
1394 return net->nft.gencursor+1 == 1 ? 1 : 0;
1395}
1396
1397static inline int
1398nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1399{
1400 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1401}
1402
1403static inline void
1404nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1405{
1406 /* Now inactive, will be active in the future */
1407 rule->genmask = (1 << net->nft.gencursor);
1408}
1409
1410static inline void
1411nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1412{
1413 rule->genmask = (1 << gencursor_next(net));
1414}
1415
1416static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1417{
1418 rule->genmask = 0;
1419}
1420
Patrick McHardy96518512013-10-14 11:00:02 +02001421static int nf_tables_dump_rules(struct sk_buff *skb,
1422 struct netlink_callback *cb)
1423{
1424 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1425 const struct nft_af_info *afi;
1426 const struct nft_table *table;
1427 const struct nft_chain *chain;
1428 const struct nft_rule *rule;
1429 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001430 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001431 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001432 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1433 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
Patrick McHardy96518512013-10-14 11:00:02 +02001434
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001435 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001436 if (family != NFPROTO_UNSPEC && family != afi->family)
1437 continue;
1438
1439 list_for_each_entry(table, &afi->tables, list) {
1440 list_for_each_entry(chain, &table->chains, list) {
1441 list_for_each_entry(rule, &chain->rules, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001442 if (!nft_rule_is_active(net, rule))
1443 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001444 if (idx < s_idx)
1445 goto cont;
1446 if (idx > s_idx)
1447 memset(&cb->args[1], 0,
1448 sizeof(cb->args) - sizeof(cb->args[0]));
1449 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1450 cb->nlh->nlmsg_seq,
1451 NFT_MSG_NEWRULE,
1452 NLM_F_MULTI | NLM_F_APPEND,
1453 afi->family, table, chain, rule) < 0)
1454 goto done;
1455cont:
1456 idx++;
1457 }
1458 }
1459 }
1460 }
1461done:
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001462 /* Invalidate this dump, a transition to the new generation happened */
1463 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1464 return -EBUSY;
1465
Patrick McHardy96518512013-10-14 11:00:02 +02001466 cb->args[0] = idx;
1467 return skb->len;
1468}
1469
1470static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1471 const struct nlmsghdr *nlh,
1472 const struct nlattr * const nla[])
1473{
1474 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1475 const struct nft_af_info *afi;
1476 const struct nft_table *table;
1477 const struct nft_chain *chain;
1478 const struct nft_rule *rule;
1479 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001480 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001481 int family = nfmsg->nfgen_family;
1482 int err;
1483
1484 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1485 struct netlink_dump_control c = {
1486 .dump = nf_tables_dump_rules,
1487 };
1488 return netlink_dump_start(nlsk, skb, nlh, &c);
1489 }
1490
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001491 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001492 if (IS_ERR(afi))
1493 return PTR_ERR(afi);
1494
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001495 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001496 if (IS_ERR(table))
1497 return PTR_ERR(table);
1498
1499 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1500 if (IS_ERR(chain))
1501 return PTR_ERR(chain);
1502
1503 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1504 if (IS_ERR(rule))
1505 return PTR_ERR(rule);
1506
1507 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1508 if (!skb2)
1509 return -ENOMEM;
1510
1511 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1512 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1513 family, table, chain, rule);
1514 if (err < 0)
1515 goto err;
1516
1517 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1518
1519err:
1520 kfree_skb(skb2);
1521 return err;
1522}
1523
1524static void nf_tables_rcu_rule_destroy(struct rcu_head *head)
1525{
1526 struct nft_rule *rule = container_of(head, struct nft_rule, rcu_head);
1527 struct nft_expr *expr;
1528
1529 /*
1530 * Careful: some expressions might not be initialized in case this
1531 * is called on error from nf_tables_newrule().
1532 */
1533 expr = nft_expr_first(rule);
1534 while (expr->ops && expr != nft_expr_last(rule)) {
1535 nf_tables_expr_destroy(expr);
1536 expr = nft_expr_next(expr);
1537 }
1538 kfree(rule);
1539}
1540
1541static void nf_tables_rule_destroy(struct nft_rule *rule)
1542{
1543 call_rcu(&rule->rcu_head, nf_tables_rcu_rule_destroy);
1544}
1545
1546#define NFT_RULE_MAXEXPRS 128
1547
1548static struct nft_expr_info *info;
1549
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001550static struct nft_rule_trans *
1551nf_tables_trans_add(struct nft_rule *rule, const struct nft_ctx *ctx)
1552{
1553 struct nft_rule_trans *rupd;
1554
1555 rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1556 if (rupd == NULL)
1557 return NULL;
1558
1559 rupd->chain = ctx->chain;
1560 rupd->table = ctx->table;
1561 rupd->rule = rule;
1562 rupd->family = ctx->afi->family;
1563 rupd->nlh = ctx->nlh;
1564 list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1565
1566 return rupd;
1567}
1568
Patrick McHardy96518512013-10-14 11:00:02 +02001569static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1570 const struct nlmsghdr *nlh,
1571 const struct nlattr * const nla[])
1572{
1573 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1574 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001575 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001576 struct nft_table *table;
1577 struct nft_chain *chain;
1578 struct nft_rule *rule, *old_rule = NULL;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001579 struct nft_rule_trans *repl = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001580 struct nft_expr *expr;
1581 struct nft_ctx ctx;
1582 struct nlattr *tmp;
1583 unsigned int size, i, n;
1584 int err, rem;
1585 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02001586 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02001587
1588 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1589
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001590 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02001591 if (IS_ERR(afi))
1592 return PTR_ERR(afi);
1593
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001594 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001595 if (IS_ERR(table))
1596 return PTR_ERR(table);
1597
1598 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1599 if (IS_ERR(chain))
1600 return PTR_ERR(chain);
1601
1602 if (nla[NFTA_RULE_HANDLE]) {
1603 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1604 rule = __nf_tables_rule_lookup(chain, handle);
1605 if (IS_ERR(rule))
1606 return PTR_ERR(rule);
1607
1608 if (nlh->nlmsg_flags & NLM_F_EXCL)
1609 return -EEXIST;
1610 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1611 old_rule = rule;
1612 else
1613 return -EOPNOTSUPP;
1614 } else {
1615 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1616 return -EINVAL;
1617 handle = nf_tables_alloc_handle(table);
1618 }
1619
Eric Leblond5e948462013-10-10 13:41:44 +02001620 if (nla[NFTA_RULE_POSITION]) {
1621 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1622 return -EOPNOTSUPP;
1623
1624 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1625 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1626 if (IS_ERR(old_rule))
1627 return PTR_ERR(old_rule);
1628 }
1629
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001630 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1631
Patrick McHardy96518512013-10-14 11:00:02 +02001632 n = 0;
1633 size = 0;
1634 if (nla[NFTA_RULE_EXPRESSIONS]) {
1635 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1636 err = -EINVAL;
1637 if (nla_type(tmp) != NFTA_LIST_ELEM)
1638 goto err1;
1639 if (n == NFT_RULE_MAXEXPRS)
1640 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001641 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02001642 if (err < 0)
1643 goto err1;
1644 size += info[n].ops->size;
1645 n++;
1646 }
1647 }
1648
1649 err = -ENOMEM;
1650 rule = kzalloc(sizeof(*rule) + size, GFP_KERNEL);
1651 if (rule == NULL)
1652 goto err1;
1653
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001654 nft_rule_activate_next(net, rule);
1655
Patrick McHardy96518512013-10-14 11:00:02 +02001656 rule->handle = handle;
1657 rule->dlen = size;
1658
Patrick McHardy96518512013-10-14 11:00:02 +02001659 expr = nft_expr_first(rule);
1660 for (i = 0; i < n; i++) {
1661 err = nf_tables_newexpr(&ctx, &info[i], expr);
1662 if (err < 0)
1663 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001664 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001665 expr = nft_expr_next(expr);
1666 }
1667
Patrick McHardy96518512013-10-14 11:00:02 +02001668 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001669 if (nft_rule_is_active_next(net, old_rule)) {
1670 repl = nf_tables_trans_add(old_rule, &ctx);
1671 if (repl == NULL) {
1672 err = -ENOMEM;
1673 goto err2;
1674 }
1675 nft_rule_disactivate_next(net, old_rule);
1676 list_add_tail(&rule->list, &old_rule->list);
1677 } else {
1678 err = -ENOENT;
1679 goto err2;
1680 }
Patrick McHardy96518512013-10-14 11:00:02 +02001681 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02001682 if (old_rule)
1683 list_add_rcu(&rule->list, &old_rule->list);
1684 else
1685 list_add_tail_rcu(&rule->list, &chain->rules);
1686 else {
1687 if (old_rule)
1688 list_add_tail_rcu(&rule->list, &old_rule->list);
1689 else
1690 list_add_rcu(&rule->list, &chain->rules);
1691 }
Patrick McHardy96518512013-10-14 11:00:02 +02001692
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001693 if (nf_tables_trans_add(rule, &ctx) == NULL) {
1694 err = -ENOMEM;
1695 goto err3;
1696 }
Patrick McHardy96518512013-10-14 11:00:02 +02001697 return 0;
1698
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001699err3:
1700 list_del_rcu(&rule->list);
1701 if (repl) {
1702 list_del_rcu(&repl->rule->list);
1703 list_del(&repl->list);
1704 nft_rule_clear(net, repl->rule);
1705 kfree(repl);
1706 }
Patrick McHardy96518512013-10-14 11:00:02 +02001707err2:
1708 nf_tables_rule_destroy(rule);
1709err1:
1710 for (i = 0; i < n; i++) {
1711 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001712 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001713 }
1714 return err;
1715}
1716
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001717static int
1718nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1719{
1720 /* You cannot delete the same rule twice */
1721 if (nft_rule_is_active_next(ctx->net, rule)) {
1722 if (nf_tables_trans_add(rule, ctx) == NULL)
1723 return -ENOMEM;
1724 nft_rule_disactivate_next(ctx->net, rule);
1725 return 0;
1726 }
1727 return -ENOENT;
1728}
1729
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001730static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1731{
1732 struct nft_rule *rule;
1733 int err;
1734
1735 list_for_each_entry(rule, &ctx->chain->rules, list) {
1736 err = nf_tables_delrule_one(ctx, rule);
1737 if (err < 0)
1738 return err;
1739 }
1740 return 0;
1741}
1742
Patrick McHardy96518512013-10-14 11:00:02 +02001743static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1744 const struct nlmsghdr *nlh,
1745 const struct nlattr * const nla[])
1746{
1747 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1748 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001749 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001750 const struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001751 struct nft_chain *chain = NULL;
1752 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001753 int family = nfmsg->nfgen_family, err = 0;
1754 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001755
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001756 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001757 if (IS_ERR(afi))
1758 return PTR_ERR(afi);
1759
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001760 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001761 if (IS_ERR(table))
1762 return PTR_ERR(table);
1763
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001764 if (nla[NFTA_RULE_CHAIN]) {
1765 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1766 if (IS_ERR(chain))
1767 return PTR_ERR(chain);
1768 }
Patrick McHardy96518512013-10-14 11:00:02 +02001769
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001770 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1771
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001772 if (chain) {
1773 if (nla[NFTA_RULE_HANDLE]) {
1774 rule = nf_tables_rule_lookup(chain,
1775 nla[NFTA_RULE_HANDLE]);
1776 if (IS_ERR(rule))
1777 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001778
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001779 err = nf_tables_delrule_one(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001780 } else {
1781 err = nf_table_delrule_by_chain(&ctx);
1782 }
1783 } else {
1784 list_for_each_entry(chain, &table->chains, list) {
1785 ctx.chain = chain;
1786 err = nf_table_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001787 if (err < 0)
1788 break;
Patrick McHardy96518512013-10-14 11:00:02 +02001789 }
1790 }
1791
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001792 return err;
1793}
1794
1795static int nf_tables_commit(struct sk_buff *skb)
1796{
1797 struct net *net = sock_net(skb->sk);
1798 struct nft_rule_trans *rupd, *tmp;
1799
1800 /* Bump generation counter, invalidate any dump in progress */
1801 net->nft.genctr++;
1802
1803 /* A new generation has just started */
1804 net->nft.gencursor = gencursor_next(net);
1805
1806 /* Make sure all packets have left the previous generation before
1807 * purging old rules.
1808 */
1809 synchronize_rcu();
1810
1811 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1812 /* Delete this rule from the dirty list */
1813 list_del(&rupd->list);
1814
1815 /* This rule was inactive in the past and just became active.
1816 * Clear the next bit of the genmask since its meaning has
1817 * changed, now it is the future.
1818 */
1819 if (nft_rule_is_active(net, rupd->rule)) {
1820 nft_rule_clear(net, rupd->rule);
1821 nf_tables_rule_notify(skb, rupd->nlh, rupd->table,
1822 rupd->chain, rupd->rule,
1823 NFT_MSG_NEWRULE, 0,
1824 rupd->family);
1825 kfree(rupd);
1826 continue;
1827 }
1828
1829 /* This rule is in the past, get rid of it */
1830 list_del_rcu(&rupd->rule->list);
1831 nf_tables_rule_notify(skb, rupd->nlh, rupd->table, rupd->chain,
1832 rupd->rule, NFT_MSG_DELRULE, 0,
1833 rupd->family);
1834 nf_tables_rule_destroy(rupd->rule);
1835 kfree(rupd);
1836 }
1837
1838 return 0;
1839}
1840
1841static int nf_tables_abort(struct sk_buff *skb)
1842{
1843 struct net *net = sock_net(skb->sk);
1844 struct nft_rule_trans *rupd, *tmp;
1845
1846 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1847 /* Delete all rules from the dirty list */
1848 list_del(&rupd->list);
1849
1850 if (!nft_rule_is_active_next(net, rupd->rule)) {
1851 nft_rule_clear(net, rupd->rule);
1852 kfree(rupd);
1853 continue;
1854 }
1855
1856 /* This rule is inactive, get rid of it */
1857 list_del_rcu(&rupd->rule->list);
1858 nf_tables_rule_destroy(rupd->rule);
1859 kfree(rupd);
1860 }
Patrick McHardy96518512013-10-14 11:00:02 +02001861 return 0;
1862}
1863
Patrick McHardy20a69342013-10-11 12:06:22 +02001864/*
1865 * Sets
1866 */
1867
1868static LIST_HEAD(nf_tables_set_ops);
1869
1870int nft_register_set(struct nft_set_ops *ops)
1871{
1872 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1873 list_add_tail(&ops->list, &nf_tables_set_ops);
1874 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1875 return 0;
1876}
1877EXPORT_SYMBOL_GPL(nft_register_set);
1878
1879void nft_unregister_set(struct nft_set_ops *ops)
1880{
1881 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1882 list_del(&ops->list);
1883 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1884}
1885EXPORT_SYMBOL_GPL(nft_unregister_set);
1886
1887static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1888{
1889 const struct nft_set_ops *ops;
1890 u32 features;
1891
1892#ifdef CONFIG_MODULES
1893 if (list_empty(&nf_tables_set_ops)) {
1894 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1895 request_module("nft-set");
1896 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1897 if (!list_empty(&nf_tables_set_ops))
1898 return ERR_PTR(-EAGAIN);
1899 }
1900#endif
1901 features = 0;
1902 if (nla[NFTA_SET_FLAGS] != NULL) {
1903 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1904 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1905 }
1906
1907 // FIXME: implement selection properly
1908 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1909 if ((ops->features & features) != features)
1910 continue;
1911 if (!try_module_get(ops->owner))
1912 continue;
1913 return ops;
1914 }
1915
1916 return ERR_PTR(-EOPNOTSUPP);
1917}
1918
1919static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1920 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1921 [NFTA_SET_NAME] = { .type = NLA_STRING },
1922 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1923 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1924 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1925 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1926 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1927};
1928
1929static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1930 const struct sk_buff *skb,
1931 const struct nlmsghdr *nlh,
1932 const struct nlattr * const nla[])
1933{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001934 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02001935 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01001936 const struct nft_af_info *afi = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02001937 const struct nft_table *table = NULL;
1938
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01001939 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
1940 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1941 if (IS_ERR(afi))
1942 return PTR_ERR(afi);
1943 }
Patrick McHardy20a69342013-10-11 12:06:22 +02001944
1945 if (nla[NFTA_SET_TABLE] != NULL) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001946 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02001947 if (IS_ERR(table))
1948 return PTR_ERR(table);
1949 }
1950
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001951 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02001952 return 0;
1953}
1954
1955struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1956 const struct nlattr *nla)
1957{
1958 struct nft_set *set;
1959
1960 if (nla == NULL)
1961 return ERR_PTR(-EINVAL);
1962
1963 list_for_each_entry(set, &table->sets, list) {
1964 if (!nla_strcmp(nla, set->name))
1965 return set;
1966 }
1967 return ERR_PTR(-ENOENT);
1968}
1969
1970static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1971 const char *name)
1972{
1973 const struct nft_set *i;
1974 const char *p;
1975 unsigned long *inuse;
1976 unsigned int n = 0;
1977
1978 p = strnchr(name, IFNAMSIZ, '%');
1979 if (p != NULL) {
1980 if (p[1] != 'd' || strchr(p + 2, '%'))
1981 return -EINVAL;
1982
1983 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1984 if (inuse == NULL)
1985 return -ENOMEM;
1986
1987 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01001988 int tmp;
1989
1990 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02001991 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01001992 if (tmp < 0 || tmp > BITS_PER_LONG * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02001993 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01001994
1995 set_bit(tmp, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02001996 }
1997
1998 n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
1999 free_page((unsigned long)inuse);
2000 }
2001
2002 snprintf(set->name, sizeof(set->name), name, n);
2003 list_for_each_entry(i, &ctx->table->sets, list) {
2004 if (!strcmp(set->name, i->name))
2005 return -ENFILE;
2006 }
2007 return 0;
2008}
2009
2010static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2011 const struct nft_set *set, u16 event, u16 flags)
2012{
2013 struct nfgenmsg *nfmsg;
2014 struct nlmsghdr *nlh;
2015 u32 portid = NETLINK_CB(ctx->skb).portid;
2016 u32 seq = ctx->nlh->nlmsg_seq;
2017
2018 event |= NFNL_SUBSYS_NFTABLES << 8;
2019 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2020 flags);
2021 if (nlh == NULL)
2022 goto nla_put_failure;
2023
2024 nfmsg = nlmsg_data(nlh);
2025 nfmsg->nfgen_family = ctx->afi->family;
2026 nfmsg->version = NFNETLINK_V0;
2027 nfmsg->res_id = 0;
2028
2029 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2030 goto nla_put_failure;
2031 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2032 goto nla_put_failure;
2033 if (set->flags != 0)
2034 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2035 goto nla_put_failure;
2036
2037 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2038 goto nla_put_failure;
2039 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2040 goto nla_put_failure;
2041 if (set->flags & NFT_SET_MAP) {
2042 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2043 goto nla_put_failure;
2044 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2045 goto nla_put_failure;
2046 }
2047
2048 return nlmsg_end(skb, nlh);
2049
2050nla_put_failure:
2051 nlmsg_trim(skb, nlh);
2052 return -1;
2053}
2054
2055static int nf_tables_set_notify(const struct nft_ctx *ctx,
2056 const struct nft_set *set,
2057 int event)
2058{
2059 struct sk_buff *skb;
2060 u32 portid = NETLINK_CB(ctx->skb).portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002061 bool report;
2062 int err;
2063
2064 report = nlmsg_report(ctx->nlh);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002065 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002066 return 0;
2067
2068 err = -ENOBUFS;
2069 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2070 if (skb == NULL)
2071 goto err;
2072
2073 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2074 if (err < 0) {
2075 kfree_skb(skb);
2076 goto err;
2077 }
2078
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002079 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
Patrick McHardy20a69342013-10-11 12:06:22 +02002080 GFP_KERNEL);
2081err:
2082 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002083 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002084 return err;
2085}
2086
2087static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2088 struct netlink_callback *cb)
2089{
2090 const struct nft_set *set;
2091 unsigned int idx = 0, s_idx = cb->args[0];
2092
2093 if (cb->args[1])
2094 return skb->len;
2095
2096 list_for_each_entry(set, &ctx->table->sets, list) {
2097 if (idx < s_idx)
2098 goto cont;
2099 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2100 NLM_F_MULTI) < 0) {
2101 cb->args[0] = idx;
2102 goto done;
2103 }
2104cont:
2105 idx++;
2106 }
2107 cb->args[1] = 1;
2108done:
2109 return skb->len;
2110}
2111
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002112static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2113 struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002114{
2115 const struct nft_set *set;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002116 unsigned int idx, s_idx = cb->args[0];
Patrick McHardy20a69342013-10-11 12:06:22 +02002117 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2118
2119 if (cb->args[1])
2120 return skb->len;
2121
2122 list_for_each_entry(table, &ctx->afi->tables, list) {
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002123 if (cur_table) {
2124 if (cur_table != table)
2125 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02002126
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002127 cur_table = NULL;
2128 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002129 ctx->table = table;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002130 idx = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002131 list_for_each_entry(set, &ctx->table->sets, list) {
2132 if (idx < s_idx)
2133 goto cont;
2134 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2135 NLM_F_MULTI) < 0) {
2136 cb->args[0] = idx;
2137 cb->args[2] = (unsigned long) table;
2138 goto done;
2139 }
2140cont:
2141 idx++;
2142 }
2143 }
2144 cb->args[1] = 1;
2145done:
2146 return skb->len;
2147}
2148
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002149static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2150 struct netlink_callback *cb)
2151{
2152 const struct nft_set *set;
2153 unsigned int idx, s_idx = cb->args[0];
2154 const struct nft_af_info *afi;
2155 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2156 struct net *net = sock_net(skb->sk);
2157 int cur_family = cb->args[3];
2158
2159 if (cb->args[1])
2160 return skb->len;
2161
2162 list_for_each_entry(afi, &net->nft.af_info, list) {
2163 if (cur_family) {
2164 if (afi->family != cur_family)
2165 continue;
2166
2167 cur_family = 0;
2168 }
2169
2170 list_for_each_entry(table, &afi->tables, list) {
2171 if (cur_table) {
2172 if (cur_table != table)
2173 continue;
2174
2175 cur_table = NULL;
2176 }
2177
2178 ctx->table = table;
2179 ctx->afi = afi;
2180 idx = 0;
2181 list_for_each_entry(set, &ctx->table->sets, list) {
2182 if (idx < s_idx)
2183 goto cont;
2184 if (nf_tables_fill_set(skb, ctx, set,
2185 NFT_MSG_NEWSET,
2186 NLM_F_MULTI) < 0) {
2187 cb->args[0] = idx;
2188 cb->args[2] = (unsigned long) table;
2189 cb->args[3] = afi->family;
2190 goto done;
2191 }
2192cont:
2193 idx++;
2194 }
2195 if (s_idx)
2196 s_idx = 0;
2197 }
2198 }
2199 cb->args[1] = 1;
2200done:
2201 return skb->len;
2202}
2203
Patrick McHardy20a69342013-10-11 12:06:22 +02002204static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2205{
2206 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2207 struct nlattr *nla[NFTA_SET_MAX + 1];
2208 struct nft_ctx ctx;
2209 int err, ret;
2210
2211 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2212 nft_set_policy);
2213 if (err < 0)
2214 return err;
2215
2216 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2217 if (err < 0)
2218 return err;
2219
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002220 if (ctx.table == NULL) {
2221 if (ctx.afi == NULL)
2222 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2223 else
2224 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2225 } else
Patrick McHardy20a69342013-10-11 12:06:22 +02002226 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2227
2228 return ret;
2229}
2230
2231static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2232 const struct nlmsghdr *nlh,
2233 const struct nlattr * const nla[])
2234{
2235 const struct nft_set *set;
2236 struct nft_ctx ctx;
2237 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002238 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002239 int err;
2240
2241 /* Verify existance before starting dump */
2242 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2243 if (err < 0)
2244 return err;
2245
2246 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2247 struct netlink_dump_control c = {
2248 .dump = nf_tables_dump_sets,
2249 };
2250 return netlink_dump_start(nlsk, skb, nlh, &c);
2251 }
2252
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002253 /* Only accept unspec with dump */
2254 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2255 return -EAFNOSUPPORT;
2256
Patrick McHardy20a69342013-10-11 12:06:22 +02002257 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2258 if (IS_ERR(set))
2259 return PTR_ERR(set);
2260
2261 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2262 if (skb2 == NULL)
2263 return -ENOMEM;
2264
2265 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2266 if (err < 0)
2267 goto err;
2268
2269 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2270
2271err:
2272 kfree_skb(skb2);
2273 return err;
2274}
2275
2276static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2277 const struct nlmsghdr *nlh,
2278 const struct nlattr * const nla[])
2279{
2280 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2281 const struct nft_set_ops *ops;
2282 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002283 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002284 struct nft_table *table;
2285 struct nft_set *set;
2286 struct nft_ctx ctx;
2287 char name[IFNAMSIZ];
2288 unsigned int size;
2289 bool create;
2290 u32 ktype, klen, dlen, dtype, flags;
2291 int err;
2292
2293 if (nla[NFTA_SET_TABLE] == NULL ||
2294 nla[NFTA_SET_NAME] == NULL ||
2295 nla[NFTA_SET_KEY_LEN] == NULL)
2296 return -EINVAL;
2297
2298 ktype = NFT_DATA_VALUE;
2299 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2300 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2301 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2302 return -EINVAL;
2303 }
2304
2305 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2306 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2307 return -EINVAL;
2308
2309 flags = 0;
2310 if (nla[NFTA_SET_FLAGS] != NULL) {
2311 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2312 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2313 NFT_SET_INTERVAL | NFT_SET_MAP))
2314 return -EINVAL;
2315 }
2316
2317 dtype = 0;
2318 dlen = 0;
2319 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2320 if (!(flags & NFT_SET_MAP))
2321 return -EINVAL;
2322
2323 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2324 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2325 dtype != NFT_DATA_VERDICT)
2326 return -EINVAL;
2327
2328 if (dtype != NFT_DATA_VERDICT) {
2329 if (nla[NFTA_SET_DATA_LEN] == NULL)
2330 return -EINVAL;
2331 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2332 if (dlen == 0 ||
2333 dlen > FIELD_SIZEOF(struct nft_data, data))
2334 return -EINVAL;
2335 } else
2336 dlen = sizeof(struct nft_data);
2337 } else if (flags & NFT_SET_MAP)
2338 return -EINVAL;
2339
2340 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2341
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002342 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002343 if (IS_ERR(afi))
2344 return PTR_ERR(afi);
2345
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002346 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002347 if (IS_ERR(table))
2348 return PTR_ERR(table);
2349
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002350 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002351
2352 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2353 if (IS_ERR(set)) {
2354 if (PTR_ERR(set) != -ENOENT)
2355 return PTR_ERR(set);
2356 set = NULL;
2357 }
2358
2359 if (set != NULL) {
2360 if (nlh->nlmsg_flags & NLM_F_EXCL)
2361 return -EEXIST;
2362 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2363 return -EOPNOTSUPP;
2364 return 0;
2365 }
2366
2367 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2368 return -ENOENT;
2369
2370 ops = nft_select_set_ops(nla);
2371 if (IS_ERR(ops))
2372 return PTR_ERR(ops);
2373
2374 size = 0;
2375 if (ops->privsize != NULL)
2376 size = ops->privsize(nla);
2377
2378 err = -ENOMEM;
2379 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2380 if (set == NULL)
2381 goto err1;
2382
2383 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2384 err = nf_tables_set_alloc_name(&ctx, set, name);
2385 if (err < 0)
2386 goto err2;
2387
2388 INIT_LIST_HEAD(&set->bindings);
2389 set->ops = ops;
2390 set->ktype = ktype;
2391 set->klen = klen;
2392 set->dtype = dtype;
2393 set->dlen = dlen;
2394 set->flags = flags;
2395
2396 err = ops->init(set, nla);
2397 if (err < 0)
2398 goto err2;
2399
2400 list_add_tail(&set->list, &table->sets);
2401 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2402 return 0;
2403
2404err2:
2405 kfree(set);
2406err1:
2407 module_put(ops->owner);
2408 return err;
2409}
2410
2411static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2412{
2413 list_del(&set->list);
2414 if (!(set->flags & NFT_SET_ANONYMOUS))
2415 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2416
2417 set->ops->destroy(set);
2418 module_put(set->ops->owner);
2419 kfree(set);
2420}
2421
2422static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2423 const struct nlmsghdr *nlh,
2424 const struct nlattr * const nla[])
2425{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002426 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002427 struct nft_set *set;
2428 struct nft_ctx ctx;
2429 int err;
2430
2431 if (nla[NFTA_SET_TABLE] == NULL)
2432 return -EINVAL;
2433
2434 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2435 if (err < 0)
2436 return err;
2437
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002438 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2439 return -EAFNOSUPPORT;
2440
Patrick McHardy20a69342013-10-11 12:06:22 +02002441 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2442 if (IS_ERR(set))
2443 return PTR_ERR(set);
2444 if (!list_empty(&set->bindings))
2445 return -EBUSY;
2446
2447 nf_tables_set_destroy(&ctx, set);
2448 return 0;
2449}
2450
2451static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2452 const struct nft_set *set,
2453 const struct nft_set_iter *iter,
2454 const struct nft_set_elem *elem)
2455{
2456 enum nft_registers dreg;
2457
2458 dreg = nft_type_to_reg(set->dtype);
Pablo Neira Ayuso2ee0d3c2013-12-28 00:59:38 +01002459 return nft_validate_data_load(ctx, dreg, &elem->data,
2460 set->dtype == NFT_DATA_VERDICT ?
2461 NFT_DATA_VERDICT : NFT_DATA_VALUE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002462}
2463
2464int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2465 struct nft_set_binding *binding)
2466{
2467 struct nft_set_binding *i;
2468 struct nft_set_iter iter;
2469
2470 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2471 return -EBUSY;
2472
2473 if (set->flags & NFT_SET_MAP) {
2474 /* If the set is already bound to the same chain all
2475 * jumps are already validated for that chain.
2476 */
2477 list_for_each_entry(i, &set->bindings, list) {
2478 if (i->chain == binding->chain)
2479 goto bind;
2480 }
2481
2482 iter.skip = 0;
2483 iter.count = 0;
2484 iter.err = 0;
2485 iter.fn = nf_tables_bind_check_setelem;
2486
2487 set->ops->walk(ctx, set, &iter);
2488 if (iter.err < 0) {
2489 /* Destroy anonymous sets if binding fails */
2490 if (set->flags & NFT_SET_ANONYMOUS)
2491 nf_tables_set_destroy(ctx, set);
2492
2493 return iter.err;
2494 }
2495 }
2496bind:
2497 binding->chain = ctx->chain;
2498 list_add_tail(&binding->list, &set->bindings);
2499 return 0;
2500}
2501
2502void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2503 struct nft_set_binding *binding)
2504{
2505 list_del(&binding->list);
2506
2507 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2508 nf_tables_set_destroy(ctx, set);
2509}
2510
2511/*
2512 * Set elements
2513 */
2514
2515static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2516 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2517 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2518 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2519};
2520
2521static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2522 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2523 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2524 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2525};
2526
2527static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2528 const struct sk_buff *skb,
2529 const struct nlmsghdr *nlh,
2530 const struct nlattr * const nla[])
2531{
2532 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2533 const struct nft_af_info *afi;
2534 const struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002535 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002536
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002537 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002538 if (IS_ERR(afi))
2539 return PTR_ERR(afi);
2540
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002541 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002542 if (IS_ERR(table))
2543 return PTR_ERR(table);
2544
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002545 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002546 return 0;
2547}
2548
2549static int nf_tables_fill_setelem(struct sk_buff *skb,
2550 const struct nft_set *set,
2551 const struct nft_set_elem *elem)
2552{
2553 unsigned char *b = skb_tail_pointer(skb);
2554 struct nlattr *nest;
2555
2556 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2557 if (nest == NULL)
2558 goto nla_put_failure;
2559
2560 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2561 set->klen) < 0)
2562 goto nla_put_failure;
2563
2564 if (set->flags & NFT_SET_MAP &&
2565 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2566 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2567 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2568 set->dlen) < 0)
2569 goto nla_put_failure;
2570
2571 if (elem->flags != 0)
2572 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2573 goto nla_put_failure;
2574
2575 nla_nest_end(skb, nest);
2576 return 0;
2577
2578nla_put_failure:
2579 nlmsg_trim(skb, b);
2580 return -EMSGSIZE;
2581}
2582
2583struct nft_set_dump_args {
2584 const struct netlink_callback *cb;
2585 struct nft_set_iter iter;
2586 struct sk_buff *skb;
2587};
2588
2589static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2590 const struct nft_set *set,
2591 const struct nft_set_iter *iter,
2592 const struct nft_set_elem *elem)
2593{
2594 struct nft_set_dump_args *args;
2595
2596 args = container_of(iter, struct nft_set_dump_args, iter);
2597 return nf_tables_fill_setelem(args->skb, set, elem);
2598}
2599
2600static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2601{
2602 const struct nft_set *set;
2603 struct nft_set_dump_args args;
2604 struct nft_ctx ctx;
2605 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2606 struct nfgenmsg *nfmsg;
2607 struct nlmsghdr *nlh;
2608 struct nlattr *nest;
2609 u32 portid, seq;
2610 int event, err;
2611
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01002612 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2613 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002614 if (err < 0)
2615 return err;
2616
2617 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2618 if (err < 0)
2619 return err;
2620
2621 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2622 if (IS_ERR(set))
2623 return PTR_ERR(set);
2624
2625 event = NFT_MSG_NEWSETELEM;
2626 event |= NFNL_SUBSYS_NFTABLES << 8;
2627 portid = NETLINK_CB(cb->skb).portid;
2628 seq = cb->nlh->nlmsg_seq;
2629
2630 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2631 NLM_F_MULTI);
2632 if (nlh == NULL)
2633 goto nla_put_failure;
2634
2635 nfmsg = nlmsg_data(nlh);
2636 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2637 nfmsg->version = NFNETLINK_V0;
2638 nfmsg->res_id = 0;
2639
2640 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2641 goto nla_put_failure;
2642 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2643 goto nla_put_failure;
2644
2645 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2646 if (nest == NULL)
2647 goto nla_put_failure;
2648
2649 args.cb = cb;
2650 args.skb = skb;
2651 args.iter.skip = cb->args[0];
2652 args.iter.count = 0;
2653 args.iter.err = 0;
2654 args.iter.fn = nf_tables_dump_setelem;
2655 set->ops->walk(&ctx, set, &args.iter);
2656
2657 nla_nest_end(skb, nest);
2658 nlmsg_end(skb, nlh);
2659
2660 if (args.iter.err && args.iter.err != -EMSGSIZE)
2661 return args.iter.err;
2662 if (args.iter.count == cb->args[0])
2663 return 0;
2664
2665 cb->args[0] = args.iter.count;
2666 return skb->len;
2667
2668nla_put_failure:
2669 return -ENOSPC;
2670}
2671
2672static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2673 const struct nlmsghdr *nlh,
2674 const struct nlattr * const nla[])
2675{
2676 const struct nft_set *set;
2677 struct nft_ctx ctx;
2678 int err;
2679
2680 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2681 if (err < 0)
2682 return err;
2683
2684 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2685 if (IS_ERR(set))
2686 return PTR_ERR(set);
2687
2688 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2689 struct netlink_dump_control c = {
2690 .dump = nf_tables_dump_set,
2691 };
2692 return netlink_dump_start(nlsk, skb, nlh, &c);
2693 }
2694 return -EOPNOTSUPP;
2695}
2696
2697static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2698 const struct nlattr *attr)
2699{
2700 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2701 struct nft_data_desc d1, d2;
2702 struct nft_set_elem elem;
2703 struct nft_set_binding *binding;
2704 enum nft_registers dreg;
2705 int err;
2706
2707 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2708 nft_set_elem_policy);
2709 if (err < 0)
2710 return err;
2711
2712 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2713 return -EINVAL;
2714
2715 elem.flags = 0;
2716 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2717 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2718 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2719 return -EINVAL;
2720 }
2721
2722 if (set->flags & NFT_SET_MAP) {
2723 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2724 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2725 return -EINVAL;
2726 } else {
2727 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2728 return -EINVAL;
2729 }
2730
2731 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2732 if (err < 0)
2733 goto err1;
2734 err = -EINVAL;
2735 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2736 goto err2;
2737
2738 err = -EEXIST;
2739 if (set->ops->get(set, &elem) == 0)
2740 goto err2;
2741
2742 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2743 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2744 if (err < 0)
2745 goto err2;
2746
2747 err = -EINVAL;
2748 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2749 goto err3;
2750
2751 dreg = nft_type_to_reg(set->dtype);
2752 list_for_each_entry(binding, &set->bindings, list) {
2753 struct nft_ctx bind_ctx = {
2754 .afi = ctx->afi,
2755 .table = ctx->table,
2756 .chain = binding->chain,
2757 };
2758
2759 err = nft_validate_data_load(&bind_ctx, dreg,
2760 &elem.data, d2.type);
2761 if (err < 0)
2762 goto err3;
2763 }
2764 }
2765
2766 err = set->ops->insert(set, &elem);
2767 if (err < 0)
2768 goto err3;
2769
2770 return 0;
2771
2772err3:
2773 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2774 nft_data_uninit(&elem.data, d2.type);
2775err2:
2776 nft_data_uninit(&elem.key, d1.type);
2777err1:
2778 return err;
2779}
2780
2781static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2782 const struct nlmsghdr *nlh,
2783 const struct nlattr * const nla[])
2784{
2785 const struct nlattr *attr;
2786 struct nft_set *set;
2787 struct nft_ctx ctx;
2788 int rem, err;
2789
2790 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2791 if (err < 0)
2792 return err;
2793
2794 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2795 if (IS_ERR(set))
2796 return PTR_ERR(set);
2797 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2798 return -EBUSY;
2799
2800 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2801 err = nft_add_set_elem(&ctx, set, attr);
2802 if (err < 0)
2803 return err;
2804 }
2805 return 0;
2806}
2807
2808static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2809 const struct nlattr *attr)
2810{
2811 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2812 struct nft_data_desc desc;
2813 struct nft_set_elem elem;
2814 int err;
2815
2816 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2817 nft_set_elem_policy);
2818 if (err < 0)
2819 goto err1;
2820
2821 err = -EINVAL;
2822 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2823 goto err1;
2824
2825 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2826 if (err < 0)
2827 goto err1;
2828
2829 err = -EINVAL;
2830 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2831 goto err2;
2832
2833 err = set->ops->get(set, &elem);
2834 if (err < 0)
2835 goto err2;
2836
2837 set->ops->remove(set, &elem);
2838
2839 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2840 if (set->flags & NFT_SET_MAP)
2841 nft_data_uninit(&elem.data, set->dtype);
2842
2843err2:
2844 nft_data_uninit(&elem.key, desc.type);
2845err1:
2846 return err;
2847}
2848
2849static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2850 const struct nlmsghdr *nlh,
2851 const struct nlattr * const nla[])
2852{
2853 const struct nlattr *attr;
2854 struct nft_set *set;
2855 struct nft_ctx ctx;
2856 int rem, err;
2857
2858 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2859 if (err < 0)
2860 return err;
2861
2862 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2863 if (IS_ERR(set))
2864 return PTR_ERR(set);
2865 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2866 return -EBUSY;
2867
2868 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2869 err = nft_del_setelem(&ctx, set, attr);
2870 if (err < 0)
2871 return err;
2872 }
2873 return 0;
2874}
2875
Patrick McHardy96518512013-10-14 11:00:02 +02002876static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2877 [NFT_MSG_NEWTABLE] = {
2878 .call = nf_tables_newtable,
2879 .attr_count = NFTA_TABLE_MAX,
2880 .policy = nft_table_policy,
2881 },
2882 [NFT_MSG_GETTABLE] = {
2883 .call = nf_tables_gettable,
2884 .attr_count = NFTA_TABLE_MAX,
2885 .policy = nft_table_policy,
2886 },
2887 [NFT_MSG_DELTABLE] = {
2888 .call = nf_tables_deltable,
2889 .attr_count = NFTA_TABLE_MAX,
2890 .policy = nft_table_policy,
2891 },
2892 [NFT_MSG_NEWCHAIN] = {
2893 .call = nf_tables_newchain,
2894 .attr_count = NFTA_CHAIN_MAX,
2895 .policy = nft_chain_policy,
2896 },
2897 [NFT_MSG_GETCHAIN] = {
2898 .call = nf_tables_getchain,
2899 .attr_count = NFTA_CHAIN_MAX,
2900 .policy = nft_chain_policy,
2901 },
2902 [NFT_MSG_DELCHAIN] = {
2903 .call = nf_tables_delchain,
2904 .attr_count = NFTA_CHAIN_MAX,
2905 .policy = nft_chain_policy,
2906 },
2907 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002908 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02002909 .attr_count = NFTA_RULE_MAX,
2910 .policy = nft_rule_policy,
2911 },
2912 [NFT_MSG_GETRULE] = {
2913 .call = nf_tables_getrule,
2914 .attr_count = NFTA_RULE_MAX,
2915 .policy = nft_rule_policy,
2916 },
2917 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002918 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02002919 .attr_count = NFTA_RULE_MAX,
2920 .policy = nft_rule_policy,
2921 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002922 [NFT_MSG_NEWSET] = {
2923 .call = nf_tables_newset,
2924 .attr_count = NFTA_SET_MAX,
2925 .policy = nft_set_policy,
2926 },
2927 [NFT_MSG_GETSET] = {
2928 .call = nf_tables_getset,
2929 .attr_count = NFTA_SET_MAX,
2930 .policy = nft_set_policy,
2931 },
2932 [NFT_MSG_DELSET] = {
2933 .call = nf_tables_delset,
2934 .attr_count = NFTA_SET_MAX,
2935 .policy = nft_set_policy,
2936 },
2937 [NFT_MSG_NEWSETELEM] = {
2938 .call = nf_tables_newsetelem,
2939 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2940 .policy = nft_set_elem_list_policy,
2941 },
2942 [NFT_MSG_GETSETELEM] = {
2943 .call = nf_tables_getsetelem,
2944 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2945 .policy = nft_set_elem_list_policy,
2946 },
2947 [NFT_MSG_DELSETELEM] = {
2948 .call = nf_tables_delsetelem,
2949 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2950 .policy = nft_set_elem_list_policy,
2951 },
Patrick McHardy96518512013-10-14 11:00:02 +02002952};
2953
2954static const struct nfnetlink_subsystem nf_tables_subsys = {
2955 .name = "nf_tables",
2956 .subsys_id = NFNL_SUBSYS_NFTABLES,
2957 .cb_count = NFT_MSG_MAX,
2958 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002959 .commit = nf_tables_commit,
2960 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02002961};
2962
Patrick McHardy20a69342013-10-11 12:06:22 +02002963/*
2964 * Loop detection - walk through the ruleset beginning at the destination chain
2965 * of a new jump until either the source chain is reached (loop) or all
2966 * reachable chains have been traversed.
2967 *
2968 * The loop check is performed whenever a new jump verdict is added to an
2969 * expression or verdict map or a verdict map is bound to a new chain.
2970 */
2971
2972static int nf_tables_check_loops(const struct nft_ctx *ctx,
2973 const struct nft_chain *chain);
2974
2975static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2976 const struct nft_set *set,
2977 const struct nft_set_iter *iter,
2978 const struct nft_set_elem *elem)
2979{
2980 switch (elem->data.verdict) {
2981 case NFT_JUMP:
2982 case NFT_GOTO:
2983 return nf_tables_check_loops(ctx, elem->data.chain);
2984 default:
2985 return 0;
2986 }
2987}
2988
2989static int nf_tables_check_loops(const struct nft_ctx *ctx,
2990 const struct nft_chain *chain)
2991{
2992 const struct nft_rule *rule;
2993 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02002994 const struct nft_set *set;
2995 struct nft_set_binding *binding;
2996 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02002997
2998 if (ctx->chain == chain)
2999 return -ELOOP;
3000
3001 list_for_each_entry(rule, &chain->rules, list) {
3002 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003003 const struct nft_data *data = NULL;
3004 int err;
3005
3006 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02003007 continue;
3008
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003009 err = expr->ops->validate(ctx, expr, &data);
3010 if (err < 0)
3011 return err;
3012
Patrick McHardy20a69342013-10-11 12:06:22 +02003013 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003014 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02003015
3016 switch (data->verdict) {
3017 case NFT_JUMP:
3018 case NFT_GOTO:
3019 err = nf_tables_check_loops(ctx, data->chain);
3020 if (err < 0)
3021 return err;
3022 default:
3023 break;
3024 }
3025 }
3026 }
3027
3028 list_for_each_entry(set, &ctx->table->sets, list) {
3029 if (!(set->flags & NFT_SET_MAP) ||
3030 set->dtype != NFT_DATA_VERDICT)
3031 continue;
3032
3033 list_for_each_entry(binding, &set->bindings, list) {
3034 if (binding->chain != chain)
3035 continue;
3036
3037 iter.skip = 0;
3038 iter.count = 0;
3039 iter.err = 0;
3040 iter.fn = nf_tables_loop_check_setelem;
3041
3042 set->ops->walk(ctx, set, &iter);
3043 if (iter.err < 0)
3044 return iter.err;
3045 }
3046 }
3047
3048 return 0;
3049}
3050
Patrick McHardy96518512013-10-14 11:00:02 +02003051/**
3052 * nft_validate_input_register - validate an expressions' input register
3053 *
3054 * @reg: the register number
3055 *
3056 * Validate that the input register is one of the general purpose
3057 * registers.
3058 */
3059int nft_validate_input_register(enum nft_registers reg)
3060{
3061 if (reg <= NFT_REG_VERDICT)
3062 return -EINVAL;
3063 if (reg > NFT_REG_MAX)
3064 return -ERANGE;
3065 return 0;
3066}
3067EXPORT_SYMBOL_GPL(nft_validate_input_register);
3068
3069/**
3070 * nft_validate_output_register - validate an expressions' output register
3071 *
3072 * @reg: the register number
3073 *
3074 * Validate that the output register is one of the general purpose
3075 * registers or the verdict register.
3076 */
3077int nft_validate_output_register(enum nft_registers reg)
3078{
3079 if (reg < NFT_REG_VERDICT)
3080 return -EINVAL;
3081 if (reg > NFT_REG_MAX)
3082 return -ERANGE;
3083 return 0;
3084}
3085EXPORT_SYMBOL_GPL(nft_validate_output_register);
3086
3087/**
3088 * nft_validate_data_load - validate an expressions' data load
3089 *
3090 * @ctx: context of the expression performing the load
3091 * @reg: the destination register number
3092 * @data: the data to load
3093 * @type: the data type
3094 *
3095 * Validate that a data load uses the appropriate data type for
3096 * the destination register. A value of NULL for the data means
3097 * that its runtime gathered data, which is always of type
3098 * NFT_DATA_VALUE.
3099 */
3100int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3101 const struct nft_data *data,
3102 enum nft_data_types type)
3103{
Patrick McHardy20a69342013-10-11 12:06:22 +02003104 int err;
3105
Patrick McHardy96518512013-10-14 11:00:02 +02003106 switch (reg) {
3107 case NFT_REG_VERDICT:
3108 if (data == NULL || type != NFT_DATA_VERDICT)
3109 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003110
3111 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3112 err = nf_tables_check_loops(ctx, data->chain);
3113 if (err < 0)
3114 return err;
3115
3116 if (ctx->chain->level + 1 > data->chain->level) {
3117 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3118 return -EMLINK;
3119 data->chain->level = ctx->chain->level + 1;
3120 }
3121 }
3122
Patrick McHardy96518512013-10-14 11:00:02 +02003123 return 0;
3124 default:
3125 if (data != NULL && type != NFT_DATA_VALUE)
3126 return -EINVAL;
3127 return 0;
3128 }
3129}
3130EXPORT_SYMBOL_GPL(nft_validate_data_load);
3131
3132static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3133 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3134 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3135 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3136};
3137
3138static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3139 struct nft_data_desc *desc, const struct nlattr *nla)
3140{
3141 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3142 struct nft_chain *chain;
3143 int err;
3144
3145 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3146 if (err < 0)
3147 return err;
3148
3149 if (!tb[NFTA_VERDICT_CODE])
3150 return -EINVAL;
3151 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3152
3153 switch (data->verdict) {
3154 case NF_ACCEPT:
3155 case NF_DROP:
3156 case NF_QUEUE:
3157 case NFT_CONTINUE:
3158 case NFT_BREAK:
3159 case NFT_RETURN:
3160 desc->len = sizeof(data->verdict);
3161 break;
3162 case NFT_JUMP:
3163 case NFT_GOTO:
3164 if (!tb[NFTA_VERDICT_CHAIN])
3165 return -EINVAL;
3166 chain = nf_tables_chain_lookup(ctx->table,
3167 tb[NFTA_VERDICT_CHAIN]);
3168 if (IS_ERR(chain))
3169 return PTR_ERR(chain);
3170 if (chain->flags & NFT_BASE_CHAIN)
3171 return -EOPNOTSUPP;
3172
Patrick McHardy96518512013-10-14 11:00:02 +02003173 chain->use++;
3174 data->chain = chain;
3175 desc->len = sizeof(data);
3176 break;
3177 default:
3178 return -EINVAL;
3179 }
3180
3181 desc->type = NFT_DATA_VERDICT;
3182 return 0;
3183}
3184
3185static void nft_verdict_uninit(const struct nft_data *data)
3186{
3187 switch (data->verdict) {
3188 case NFT_JUMP:
3189 case NFT_GOTO:
3190 data->chain->use--;
3191 break;
3192 }
3193}
3194
3195static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3196{
3197 struct nlattr *nest;
3198
3199 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3200 if (!nest)
3201 goto nla_put_failure;
3202
3203 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3204 goto nla_put_failure;
3205
3206 switch (data->verdict) {
3207 case NFT_JUMP:
3208 case NFT_GOTO:
3209 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3210 goto nla_put_failure;
3211 }
3212 nla_nest_end(skb, nest);
3213 return 0;
3214
3215nla_put_failure:
3216 return -1;
3217}
3218
3219static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3220 struct nft_data_desc *desc, const struct nlattr *nla)
3221{
3222 unsigned int len;
3223
3224 len = nla_len(nla);
3225 if (len == 0)
3226 return -EINVAL;
3227 if (len > sizeof(data->data))
3228 return -EOVERFLOW;
3229
3230 nla_memcpy(data->data, nla, sizeof(data->data));
3231 desc->type = NFT_DATA_VALUE;
3232 desc->len = len;
3233 return 0;
3234}
3235
3236static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3237 unsigned int len)
3238{
3239 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3240}
3241
3242static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3243 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3244 .len = FIELD_SIZEOF(struct nft_data, data) },
3245 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3246};
3247
3248/**
3249 * nft_data_init - parse nf_tables data netlink attributes
3250 *
3251 * @ctx: context of the expression using the data
3252 * @data: destination struct nft_data
3253 * @desc: data description
3254 * @nla: netlink attribute containing data
3255 *
3256 * Parse the netlink data attributes and initialize a struct nft_data.
3257 * The type and length of data are returned in the data description.
3258 *
3259 * The caller can indicate that it only wants to accept data of type
3260 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3261 */
3262int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3263 struct nft_data_desc *desc, const struct nlattr *nla)
3264{
3265 struct nlattr *tb[NFTA_DATA_MAX + 1];
3266 int err;
3267
3268 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3269 if (err < 0)
3270 return err;
3271
3272 if (tb[NFTA_DATA_VALUE])
3273 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3274 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3275 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3276 return -EINVAL;
3277}
3278EXPORT_SYMBOL_GPL(nft_data_init);
3279
3280/**
3281 * nft_data_uninit - release a nft_data item
3282 *
3283 * @data: struct nft_data to release
3284 * @type: type of data
3285 *
3286 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3287 * all others need to be released by calling this function.
3288 */
3289void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3290{
3291 switch (type) {
3292 case NFT_DATA_VALUE:
3293 return;
3294 case NFT_DATA_VERDICT:
3295 return nft_verdict_uninit(data);
3296 default:
3297 WARN_ON(1);
3298 }
3299}
3300EXPORT_SYMBOL_GPL(nft_data_uninit);
3301
3302int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3303 enum nft_data_types type, unsigned int len)
3304{
3305 struct nlattr *nest;
3306 int err;
3307
3308 nest = nla_nest_start(skb, attr);
3309 if (nest == NULL)
3310 return -1;
3311
3312 switch (type) {
3313 case NFT_DATA_VALUE:
3314 err = nft_value_dump(skb, data, len);
3315 break;
3316 case NFT_DATA_VERDICT:
3317 err = nft_verdict_dump(skb, data);
3318 break;
3319 default:
3320 err = -EINVAL;
3321 WARN_ON(1);
3322 }
3323
3324 nla_nest_end(skb, nest);
3325 return err;
3326}
3327EXPORT_SYMBOL_GPL(nft_data_dump);
3328
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003329static int nf_tables_init_net(struct net *net)
3330{
3331 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003332 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003333 return 0;
3334}
3335
3336static struct pernet_operations nf_tables_net_ops = {
3337 .init = nf_tables_init_net,
3338};
3339
Patrick McHardy96518512013-10-14 11:00:02 +02003340static int __init nf_tables_module_init(void)
3341{
3342 int err;
3343
3344 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3345 GFP_KERNEL);
3346 if (info == NULL) {
3347 err = -ENOMEM;
3348 goto err1;
3349 }
3350
3351 err = nf_tables_core_module_init();
3352 if (err < 0)
3353 goto err2;
3354
3355 err = nfnetlink_subsys_register(&nf_tables_subsys);
3356 if (err < 0)
3357 goto err3;
3358
3359 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003360 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003361err3:
3362 nf_tables_core_module_exit();
3363err2:
3364 kfree(info);
3365err1:
3366 return err;
3367}
3368
3369static void __exit nf_tables_module_exit(void)
3370{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003371 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003372 nfnetlink_subsys_unregister(&nf_tables_subsys);
3373 nf_tables_core_module_exit();
3374 kfree(info);
3375}
3376
3377module_init(nf_tables_module_init);
3378module_exit(nf_tables_module_exit);
3379
3380MODULE_LICENSE("GPL");
3381MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3382MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);