blob: 4b7e14ddd2b2aff42c469774ed6b43123f7668a1 [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) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200797 struct nft_stats __percpu *oldstats =
Patrick McHardy67a8fc22014-02-18 18:06:49 +0000798 nft_dereference(chain->stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200799
800 rcu_assign_pointer(chain->stats, newstats);
801 synchronize_rcu();
802 free_percpu(oldstats);
803 } else
804 rcu_assign_pointer(chain->stats, newstats);
805
806 return 0;
807}
808
Patrick McHardy96518512013-10-14 11:00:02 +0200809static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
810 const struct nlmsghdr *nlh,
811 const struct nlattr * const nla[])
812{
813 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
814 const struct nlattr * uninitialized_var(name);
815 const struct nft_af_info *afi;
816 struct nft_table *table;
817 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200818 struct nft_base_chain *basechain = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200819 struct nlattr *ha[NFTA_HOOK_MAX + 1];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200820 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200821 int family = nfmsg->nfgen_family;
Patrick McHardy57de2a02014-01-09 18:42:31 +0000822 u8 policy = NF_ACCEPT;
Patrick McHardy96518512013-10-14 11:00:02 +0200823 u64 handle = 0;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000824 unsigned int i;
Patrick McHardy96518512013-10-14 11:00:02 +0200825 int err;
826 bool create;
827
828 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
829
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200830 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200831 if (IS_ERR(afi))
832 return PTR_ERR(afi);
833
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200834 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200835 if (IS_ERR(table))
836 return PTR_ERR(table);
837
Patrick McHardy96518512013-10-14 11:00:02 +0200838 chain = NULL;
839 name = nla[NFTA_CHAIN_NAME];
840
841 if (nla[NFTA_CHAIN_HANDLE]) {
842 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
843 chain = nf_tables_chain_lookup_byhandle(table, handle);
844 if (IS_ERR(chain))
845 return PTR_ERR(chain);
846 } else {
847 chain = nf_tables_chain_lookup(table, name);
848 if (IS_ERR(chain)) {
849 if (PTR_ERR(chain) != -ENOENT)
850 return PTR_ERR(chain);
851 chain = NULL;
852 }
853 }
854
Patrick McHardy57de2a02014-01-09 18:42:31 +0000855 if (nla[NFTA_CHAIN_POLICY]) {
856 if ((chain != NULL &&
857 !(chain->flags & NFT_BASE_CHAIN)) ||
858 nla[NFTA_CHAIN_HOOK] == NULL)
859 return -EOPNOTSUPP;
860
Pablo Neira Ayuso8f46df12014-01-10 15:11:25 +0100861 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
Patrick McHardy57de2a02014-01-09 18:42:31 +0000862 switch (policy) {
863 case NF_DROP:
864 case NF_ACCEPT:
865 break;
866 default:
867 return -EINVAL;
868 }
869 }
870
Patrick McHardy96518512013-10-14 11:00:02 +0200871 if (chain != NULL) {
872 if (nlh->nlmsg_flags & NLM_F_EXCL)
873 return -EEXIST;
874 if (nlh->nlmsg_flags & NLM_F_REPLACE)
875 return -EOPNOTSUPP;
876
877 if (nla[NFTA_CHAIN_HANDLE] && name &&
878 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
879 return -EEXIST;
880
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200881 if (nla[NFTA_CHAIN_COUNTERS]) {
882 if (!(chain->flags & NFT_BASE_CHAIN))
883 return -EOPNOTSUPP;
884
885 err = nf_tables_counters(nft_base_chain(chain),
886 nla[NFTA_CHAIN_COUNTERS]);
887 if (err < 0)
888 return err;
889 }
890
Patrick McHardy4401a862014-01-09 18:42:32 +0000891 if (nla[NFTA_CHAIN_POLICY])
892 nft_base_chain(chain)->policy = policy;
893
Patrick McHardy96518512013-10-14 11:00:02 +0200894 if (nla[NFTA_CHAIN_HANDLE] && name)
895 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
896
897 goto notify;
898 }
899
Patrick McHardy75820672014-01-09 18:42:33 +0000900 if (table->use == UINT_MAX)
901 return -EOVERFLOW;
902
Patrick McHardy96518512013-10-14 11:00:02 +0200903 if (nla[NFTA_CHAIN_HOOK]) {
Patrick McHardy2a37d752014-01-09 18:42:37 +0000904 const struct nf_chain_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +0200905 struct nf_hook_ops *ops;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200906 nf_hookfn *hookfn;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000907 u32 hooknum, priority;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200908
Patrick McHardybaae3e62014-01-09 18:42:34 +0000909 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200910 if (nla[NFTA_CHAIN_TYPE]) {
911 type = nf_tables_chain_type_lookup(afi,
912 nla[NFTA_CHAIN_TYPE],
913 create);
Patrick McHardy93b08062014-01-09 18:42:36 +0000914 if (IS_ERR(type))
915 return PTR_ERR(type);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200916 }
Patrick McHardy96518512013-10-14 11:00:02 +0200917
918 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
919 nft_hook_policy);
920 if (err < 0)
921 return err;
922 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
923 ha[NFTA_HOOK_PRIORITY] == NULL)
924 return -EINVAL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200925
926 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
927 if (hooknum >= afi->nhooks)
Patrick McHardy96518512013-10-14 11:00:02 +0200928 return -EINVAL;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000929 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
Patrick McHardy96518512013-10-14 11:00:02 +0200930
Patrick McHardybaae3e62014-01-09 18:42:34 +0000931 if (!(type->hook_mask & (1 << hooknum)))
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200932 return -EOPNOTSUPP;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000933 if (!try_module_get(type->owner))
Patrick McHardybaae3e62014-01-09 18:42:34 +0000934 return -ENOENT;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000935 hookfn = type->hooks[hooknum];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200936
Patrick McHardy96518512013-10-14 11:00:02 +0200937 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
938 if (basechain == NULL)
939 return -ENOMEM;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200940
Patrick McHardy4401a862014-01-09 18:42:32 +0000941 if (nla[NFTA_CHAIN_COUNTERS]) {
942 err = nf_tables_counters(basechain,
943 nla[NFTA_CHAIN_COUNTERS]);
944 if (err < 0) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000945 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +0000946 kfree(basechain);
947 return err;
948 }
949 } else {
950 struct nft_stats __percpu *newstats;
951
952 newstats = alloc_percpu(struct nft_stats);
953 if (newstats == NULL) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000954 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +0000955 kfree(basechain);
956 return -ENOMEM;
957 }
958 rcu_assign_pointer(basechain->stats, newstats);
959 }
960
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200961 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +0200962 chain = &basechain->chain;
963
Patrick McHardy115a60b2014-01-03 12:16:15 +0000964 for (i = 0; i < afi->nops; i++) {
965 ops = &basechain->ops[i];
966 ops->pf = family;
967 ops->owner = afi->owner;
968 ops->hooknum = hooknum;
969 ops->priority = priority;
970 ops->priv = chain;
971 ops->hook = afi->hooks[ops->hooknum];
972 if (hookfn)
973 ops->hook = hookfn;
974 if (afi->hook_ops_init)
975 afi->hook_ops_init(ops, i);
976 }
Patrick McHardy96518512013-10-14 11:00:02 +0200977
978 chain->flags |= NFT_BASE_CHAIN;
Patrick McHardy57de2a02014-01-09 18:42:31 +0000979 basechain->policy = policy;
Patrick McHardy96518512013-10-14 11:00:02 +0200980 } else {
981 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
982 if (chain == NULL)
983 return -ENOMEM;
984 }
985
986 INIT_LIST_HEAD(&chain->rules);
987 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +0200988 chain->net = net;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +0200989 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +0200990 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
991
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200992 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
993 chain->flags & NFT_BASE_CHAIN) {
Patrick McHardy115a60b2014-01-03 12:16:15 +0000994 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200995 if (err < 0) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +0000996 module_put(basechain->type->owner);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200997 free_percpu(basechain->stats);
998 kfree(basechain);
999 return err;
1000 }
1001 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001002 list_add_tail(&chain->list, &table->chains);
1003 table->use++;
Patrick McHardy96518512013-10-14 11:00:02 +02001004notify:
1005 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
1006 family);
1007 return 0;
1008}
1009
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001010static void nf_tables_chain_destroy(struct nft_chain *chain)
Patrick McHardy96518512013-10-14 11:00:02 +02001011{
Patrick McHardy96518512013-10-14 11:00:02 +02001012 BUG_ON(chain->use > 0);
1013
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001014 if (chain->flags & NFT_BASE_CHAIN) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001015 module_put(nft_base_chain(chain)->type->owner);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001016 free_percpu(nft_base_chain(chain)->stats);
Patrick McHardy96518512013-10-14 11:00:02 +02001017 kfree(nft_base_chain(chain));
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001018 } else
Patrick McHardy96518512013-10-14 11:00:02 +02001019 kfree(chain);
1020}
1021
1022static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1023 const struct nlmsghdr *nlh,
1024 const struct nlattr * const nla[])
1025{
1026 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1027 const struct nft_af_info *afi;
1028 struct nft_table *table;
1029 struct nft_chain *chain;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001030 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001031 int family = nfmsg->nfgen_family;
1032
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001033 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001034 if (IS_ERR(afi))
1035 return PTR_ERR(afi);
1036
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001037 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001038 if (IS_ERR(table))
1039 return PTR_ERR(table);
1040
1041 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1042 if (IS_ERR(chain))
1043 return PTR_ERR(chain);
1044
Patrick McHardy3dd72792014-01-25 08:04:07 +00001045 if (!list_empty(&chain->rules) || chain->use > 0)
Patrick McHardy96518512013-10-14 11:00:02 +02001046 return -EBUSY;
1047
1048 list_del(&chain->list);
1049 table->use--;
1050
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001051 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1052 chain->flags & NFT_BASE_CHAIN)
Patrick McHardy115a60b2014-01-03 12:16:15 +00001053 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
Patrick McHardy96518512013-10-14 11:00:02 +02001054
1055 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1056 family);
1057
1058 /* Make sure all rule references are gone before this is released */
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001059 synchronize_rcu();
1060
1061 nf_tables_chain_destroy(chain);
Patrick McHardy96518512013-10-14 11:00:02 +02001062 return 0;
1063}
1064
1065static void nft_ctx_init(struct nft_ctx *ctx,
Patrick McHardy20a69342013-10-11 12:06:22 +02001066 const struct sk_buff *skb,
1067 const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02001068 const struct nft_af_info *afi,
1069 const struct nft_table *table,
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001070 const struct nft_chain *chain,
1071 const struct nlattr * const *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001072{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001073 ctx->net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02001074 ctx->skb = skb;
1075 ctx->nlh = nlh;
Patrick McHardy96518512013-10-14 11:00:02 +02001076 ctx->afi = afi;
1077 ctx->table = table;
1078 ctx->chain = chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001079 ctx->nla = nla;
Patrick McHardy96518512013-10-14 11:00:02 +02001080}
1081
1082/*
1083 * Expressions
1084 */
1085
1086/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001087 * nft_register_expr - register nf_tables expr type
1088 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001089 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001090 * Registers the expr type for use with nf_tables. Returns zero on
Patrick McHardy96518512013-10-14 11:00:02 +02001091 * success or a negative errno code otherwise.
1092 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001093int nft_register_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001094{
1095 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001096 list_add_tail(&type->list, &nf_tables_expressions);
Patrick McHardy96518512013-10-14 11:00:02 +02001097 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1098 return 0;
1099}
1100EXPORT_SYMBOL_GPL(nft_register_expr);
1101
1102/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001103 * nft_unregister_expr - unregister nf_tables expr type
1104 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001105 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001106 * Unregisters the expr typefor use with nf_tables.
Patrick McHardy96518512013-10-14 11:00:02 +02001107 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001108void nft_unregister_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001109{
1110 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001111 list_del(&type->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001112 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1113}
1114EXPORT_SYMBOL_GPL(nft_unregister_expr);
1115
Patrick McHardy64d46802014-02-05 15:03:37 +00001116static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1117 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) {
Patrick McHardy64d46802014-02-05 15:03:37 +00001122 if (!nla_strcmp(nla, type->name) &&
1123 (!type->family || type->family == family))
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001124 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001125 }
1126 return NULL;
1127}
1128
Patrick McHardy64d46802014-02-05 15:03:37 +00001129static const struct nft_expr_type *nft_expr_type_get(u8 family,
1130 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001131{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001132 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001133
1134 if (nla == NULL)
1135 return ERR_PTR(-EINVAL);
1136
Patrick McHardy64d46802014-02-05 15:03:37 +00001137 type = __nft_expr_type_get(family, nla);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001138 if (type != NULL && try_module_get(type->owner))
1139 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001140
1141#ifdef CONFIG_MODULES
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001142 if (type == NULL) {
Patrick McHardy96518512013-10-14 11:00:02 +02001143 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001144 request_module("nft-expr-%u-%.*s", family,
1145 nla_len(nla), (char *)nla_data(nla));
1146 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1147 if (__nft_expr_type_get(family, nla))
1148 return ERR_PTR(-EAGAIN);
1149
1150 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy96518512013-10-14 11:00:02 +02001151 request_module("nft-expr-%.*s",
1152 nla_len(nla), (char *)nla_data(nla));
1153 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001154 if (__nft_expr_type_get(family, nla))
Patrick McHardy96518512013-10-14 11:00:02 +02001155 return ERR_PTR(-EAGAIN);
1156 }
1157#endif
1158 return ERR_PTR(-ENOENT);
1159}
1160
1161static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1162 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1163 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1164};
1165
1166static int nf_tables_fill_expr_info(struct sk_buff *skb,
1167 const struct nft_expr *expr)
1168{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001169 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
Patrick McHardy96518512013-10-14 11:00:02 +02001170 goto nla_put_failure;
1171
1172 if (expr->ops->dump) {
1173 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1174 if (data == NULL)
1175 goto nla_put_failure;
1176 if (expr->ops->dump(skb, expr) < 0)
1177 goto nla_put_failure;
1178 nla_nest_end(skb, data);
1179 }
1180
1181 return skb->len;
1182
1183nla_put_failure:
1184 return -1;
1185};
1186
1187struct nft_expr_info {
1188 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001189 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001190};
1191
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001192static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1193 const struct nlattr *nla,
Patrick McHardy96518512013-10-14 11:00:02 +02001194 struct nft_expr_info *info)
1195{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001196 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001197 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001198 struct nlattr *tb[NFTA_EXPR_MAX + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001199 int err;
1200
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001201 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
Patrick McHardy96518512013-10-14 11:00:02 +02001202 if (err < 0)
1203 return err;
1204
Patrick McHardy64d46802014-02-05 15:03:37 +00001205 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001206 if (IS_ERR(type))
1207 return PTR_ERR(type);
1208
1209 if (tb[NFTA_EXPR_DATA]) {
1210 err = nla_parse_nested(info->tb, type->maxattr,
1211 tb[NFTA_EXPR_DATA], type->policy);
1212 if (err < 0)
1213 goto err1;
1214 } else
1215 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1216
1217 if (type->select_ops != NULL) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001218 ops = type->select_ops(ctx,
1219 (const struct nlattr * const *)info->tb);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001220 if (IS_ERR(ops)) {
1221 err = PTR_ERR(ops);
1222 goto err1;
1223 }
1224 } else
1225 ops = type->ops;
1226
Patrick McHardy96518512013-10-14 11:00:02 +02001227 info->ops = ops;
1228 return 0;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001229
1230err1:
1231 module_put(type->owner);
1232 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001233}
1234
1235static int nf_tables_newexpr(const struct nft_ctx *ctx,
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001236 const struct nft_expr_info *info,
Patrick McHardy96518512013-10-14 11:00:02 +02001237 struct nft_expr *expr)
1238{
1239 const struct nft_expr_ops *ops = info->ops;
1240 int err;
1241
1242 expr->ops = ops;
1243 if (ops->init) {
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001244 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
Patrick McHardy96518512013-10-14 11:00:02 +02001245 if (err < 0)
1246 goto err1;
1247 }
1248
Patrick McHardy96518512013-10-14 11:00:02 +02001249 return 0;
1250
1251err1:
1252 expr->ops = NULL;
1253 return err;
1254}
1255
1256static void nf_tables_expr_destroy(struct nft_expr *expr)
1257{
1258 if (expr->ops->destroy)
1259 expr->ops->destroy(expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001260 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001261}
1262
1263/*
1264 * Rules
1265 */
1266
1267static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1268 u64 handle)
1269{
1270 struct nft_rule *rule;
1271
1272 // FIXME: this sucks
1273 list_for_each_entry(rule, &chain->rules, list) {
1274 if (handle == rule->handle)
1275 return rule;
1276 }
1277
1278 return ERR_PTR(-ENOENT);
1279}
1280
1281static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1282 const struct nlattr *nla)
1283{
1284 if (nla == NULL)
1285 return ERR_PTR(-EINVAL);
1286
1287 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1288}
1289
1290static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1291 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1292 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1293 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1294 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1295 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001296 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001297 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Patrick McHardy96518512013-10-14 11:00:02 +02001298};
1299
1300static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1301 int event, u32 flags, int family,
1302 const struct nft_table *table,
1303 const struct nft_chain *chain,
1304 const struct nft_rule *rule)
1305{
1306 struct nlmsghdr *nlh;
1307 struct nfgenmsg *nfmsg;
1308 const struct nft_expr *expr, *next;
1309 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001310 const struct nft_rule *prule;
1311 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001312
Eric Leblond5e948462013-10-10 13:41:44 +02001313 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001314 flags);
1315 if (nlh == NULL)
1316 goto nla_put_failure;
1317
1318 nfmsg = nlmsg_data(nlh);
1319 nfmsg->nfgen_family = family;
1320 nfmsg->version = NFNETLINK_V0;
1321 nfmsg->res_id = 0;
1322
1323 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1324 goto nla_put_failure;
1325 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1326 goto nla_put_failure;
1327 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1328 goto nla_put_failure;
1329
Eric Leblond5e948462013-10-10 13:41:44 +02001330 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1331 prule = list_entry(rule->list.prev, struct nft_rule, list);
1332 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1333 cpu_to_be64(prule->handle)))
1334 goto nla_put_failure;
1335 }
1336
Patrick McHardy96518512013-10-14 11:00:02 +02001337 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1338 if (list == NULL)
1339 goto nla_put_failure;
1340 nft_rule_for_each_expr(expr, next, rule) {
1341 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1342 if (elem == NULL)
1343 goto nla_put_failure;
1344 if (nf_tables_fill_expr_info(skb, expr) < 0)
1345 goto nla_put_failure;
1346 nla_nest_end(skb, elem);
1347 }
1348 nla_nest_end(skb, list);
1349
1350 return nlmsg_end(skb, nlh);
1351
1352nla_put_failure:
1353 nlmsg_trim(skb, nlh);
1354 return -1;
1355}
1356
1357static int nf_tables_rule_notify(const struct sk_buff *oskb,
1358 const struct nlmsghdr *nlh,
1359 const struct nft_table *table,
1360 const struct nft_chain *chain,
1361 const struct nft_rule *rule,
1362 int event, u32 flags, int family)
1363{
1364 struct sk_buff *skb;
1365 u32 portid = NETLINK_CB(oskb).portid;
1366 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1367 u32 seq = nlh->nlmsg_seq;
1368 bool report;
1369 int err;
1370
1371 report = nlmsg_report(nlh);
1372 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1373 return 0;
1374
1375 err = -ENOBUFS;
1376 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1377 if (skb == NULL)
1378 goto err;
1379
1380 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1381 family, table, chain, rule);
1382 if (err < 0) {
1383 kfree_skb(skb);
1384 goto err;
1385 }
1386
1387 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1388 GFP_KERNEL);
1389err:
1390 if (err < 0)
1391 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1392 return err;
1393}
1394
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001395static inline bool
1396nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1397{
1398 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1399}
1400
1401static inline int gencursor_next(struct net *net)
1402{
1403 return net->nft.gencursor+1 == 1 ? 1 : 0;
1404}
1405
1406static inline int
1407nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1408{
1409 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1410}
1411
1412static inline void
1413nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1414{
1415 /* Now inactive, will be active in the future */
1416 rule->genmask = (1 << net->nft.gencursor);
1417}
1418
1419static inline void
1420nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1421{
1422 rule->genmask = (1 << gencursor_next(net));
1423}
1424
1425static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1426{
1427 rule->genmask = 0;
1428}
1429
Patrick McHardy96518512013-10-14 11:00:02 +02001430static int nf_tables_dump_rules(struct sk_buff *skb,
1431 struct netlink_callback *cb)
1432{
1433 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1434 const struct nft_af_info *afi;
1435 const struct nft_table *table;
1436 const struct nft_chain *chain;
1437 const struct nft_rule *rule;
1438 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001439 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001440 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001441 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1442 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
Patrick McHardy96518512013-10-14 11:00:02 +02001443
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001444 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001445 if (family != NFPROTO_UNSPEC && family != afi->family)
1446 continue;
1447
1448 list_for_each_entry(table, &afi->tables, list) {
1449 list_for_each_entry(chain, &table->chains, list) {
1450 list_for_each_entry(rule, &chain->rules, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001451 if (!nft_rule_is_active(net, rule))
1452 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001453 if (idx < s_idx)
1454 goto cont;
1455 if (idx > s_idx)
1456 memset(&cb->args[1], 0,
1457 sizeof(cb->args) - sizeof(cb->args[0]));
1458 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1459 cb->nlh->nlmsg_seq,
1460 NFT_MSG_NEWRULE,
1461 NLM_F_MULTI | NLM_F_APPEND,
1462 afi->family, table, chain, rule) < 0)
1463 goto done;
1464cont:
1465 idx++;
1466 }
1467 }
1468 }
1469 }
1470done:
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001471 /* Invalidate this dump, a transition to the new generation happened */
1472 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1473 return -EBUSY;
1474
Patrick McHardy96518512013-10-14 11:00:02 +02001475 cb->args[0] = idx;
1476 return skb->len;
1477}
1478
1479static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1480 const struct nlmsghdr *nlh,
1481 const struct nlattr * const nla[])
1482{
1483 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1484 const struct nft_af_info *afi;
1485 const struct nft_table *table;
1486 const struct nft_chain *chain;
1487 const struct nft_rule *rule;
1488 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001489 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001490 int family = nfmsg->nfgen_family;
1491 int err;
1492
1493 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1494 struct netlink_dump_control c = {
1495 .dump = nf_tables_dump_rules,
1496 };
1497 return netlink_dump_start(nlsk, skb, nlh, &c);
1498 }
1499
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001500 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001501 if (IS_ERR(afi))
1502 return PTR_ERR(afi);
1503
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001504 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001505 if (IS_ERR(table))
1506 return PTR_ERR(table);
1507
1508 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1509 if (IS_ERR(chain))
1510 return PTR_ERR(chain);
1511
1512 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1513 if (IS_ERR(rule))
1514 return PTR_ERR(rule);
1515
1516 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1517 if (!skb2)
1518 return -ENOMEM;
1519
1520 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1521 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1522 family, table, chain, rule);
1523 if (err < 0)
1524 goto err;
1525
1526 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1527
1528err:
1529 kfree_skb(skb2);
1530 return err;
1531}
1532
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001533static void nf_tables_rule_destroy(struct nft_rule *rule)
Patrick McHardy96518512013-10-14 11:00:02 +02001534{
Patrick McHardy96518512013-10-14 11:00:02 +02001535 struct nft_expr *expr;
1536
1537 /*
1538 * Careful: some expressions might not be initialized in case this
1539 * is called on error from nf_tables_newrule().
1540 */
1541 expr = nft_expr_first(rule);
1542 while (expr->ops && expr != nft_expr_last(rule)) {
1543 nf_tables_expr_destroy(expr);
1544 expr = nft_expr_next(expr);
1545 }
1546 kfree(rule);
1547}
1548
Patrick McHardy96518512013-10-14 11:00:02 +02001549#define NFT_RULE_MAXEXPRS 128
1550
1551static struct nft_expr_info *info;
1552
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001553static struct nft_rule_trans *
1554nf_tables_trans_add(struct nft_rule *rule, const struct nft_ctx *ctx)
1555{
1556 struct nft_rule_trans *rupd;
1557
1558 rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1559 if (rupd == NULL)
1560 return NULL;
1561
1562 rupd->chain = ctx->chain;
1563 rupd->table = ctx->table;
1564 rupd->rule = rule;
1565 rupd->family = ctx->afi->family;
1566 rupd->nlh = ctx->nlh;
1567 list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1568
1569 return rupd;
1570}
1571
Patrick McHardy96518512013-10-14 11:00:02 +02001572static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1573 const struct nlmsghdr *nlh,
1574 const struct nlattr * const nla[])
1575{
1576 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1577 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001578 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001579 struct nft_table *table;
1580 struct nft_chain *chain;
1581 struct nft_rule *rule, *old_rule = NULL;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001582 struct nft_rule_trans *repl = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001583 struct nft_expr *expr;
1584 struct nft_ctx ctx;
1585 struct nlattr *tmp;
1586 unsigned int size, i, n;
1587 int err, rem;
1588 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02001589 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02001590
1591 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1592
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001593 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02001594 if (IS_ERR(afi))
1595 return PTR_ERR(afi);
1596
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001597 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001598 if (IS_ERR(table))
1599 return PTR_ERR(table);
1600
1601 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1602 if (IS_ERR(chain))
1603 return PTR_ERR(chain);
1604
1605 if (nla[NFTA_RULE_HANDLE]) {
1606 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1607 rule = __nf_tables_rule_lookup(chain, handle);
1608 if (IS_ERR(rule))
1609 return PTR_ERR(rule);
1610
1611 if (nlh->nlmsg_flags & NLM_F_EXCL)
1612 return -EEXIST;
1613 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1614 old_rule = rule;
1615 else
1616 return -EOPNOTSUPP;
1617 } else {
1618 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1619 return -EINVAL;
1620 handle = nf_tables_alloc_handle(table);
1621 }
1622
Eric Leblond5e948462013-10-10 13:41:44 +02001623 if (nla[NFTA_RULE_POSITION]) {
1624 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1625 return -EOPNOTSUPP;
1626
1627 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1628 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1629 if (IS_ERR(old_rule))
1630 return PTR_ERR(old_rule);
1631 }
1632
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001633 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1634
Patrick McHardy96518512013-10-14 11:00:02 +02001635 n = 0;
1636 size = 0;
1637 if (nla[NFTA_RULE_EXPRESSIONS]) {
1638 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1639 err = -EINVAL;
1640 if (nla_type(tmp) != NFTA_LIST_ELEM)
1641 goto err1;
1642 if (n == NFT_RULE_MAXEXPRS)
1643 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001644 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02001645 if (err < 0)
1646 goto err1;
1647 size += info[n].ops->size;
1648 n++;
1649 }
1650 }
1651
1652 err = -ENOMEM;
1653 rule = kzalloc(sizeof(*rule) + size, GFP_KERNEL);
1654 if (rule == NULL)
1655 goto err1;
1656
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001657 nft_rule_activate_next(net, rule);
1658
Patrick McHardy96518512013-10-14 11:00:02 +02001659 rule->handle = handle;
1660 rule->dlen = size;
1661
Patrick McHardy96518512013-10-14 11:00:02 +02001662 expr = nft_expr_first(rule);
1663 for (i = 0; i < n; i++) {
1664 err = nf_tables_newexpr(&ctx, &info[i], expr);
1665 if (err < 0)
1666 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001667 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001668 expr = nft_expr_next(expr);
1669 }
1670
Patrick McHardy96518512013-10-14 11:00:02 +02001671 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001672 if (nft_rule_is_active_next(net, old_rule)) {
1673 repl = nf_tables_trans_add(old_rule, &ctx);
1674 if (repl == NULL) {
1675 err = -ENOMEM;
1676 goto err2;
1677 }
1678 nft_rule_disactivate_next(net, old_rule);
1679 list_add_tail(&rule->list, &old_rule->list);
1680 } else {
1681 err = -ENOENT;
1682 goto err2;
1683 }
Patrick McHardy96518512013-10-14 11:00:02 +02001684 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02001685 if (old_rule)
1686 list_add_rcu(&rule->list, &old_rule->list);
1687 else
1688 list_add_tail_rcu(&rule->list, &chain->rules);
1689 else {
1690 if (old_rule)
1691 list_add_tail_rcu(&rule->list, &old_rule->list);
1692 else
1693 list_add_rcu(&rule->list, &chain->rules);
1694 }
Patrick McHardy96518512013-10-14 11:00:02 +02001695
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001696 if (nf_tables_trans_add(rule, &ctx) == NULL) {
1697 err = -ENOMEM;
1698 goto err3;
1699 }
Patrick McHardy96518512013-10-14 11:00:02 +02001700 return 0;
1701
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001702err3:
1703 list_del_rcu(&rule->list);
1704 if (repl) {
1705 list_del_rcu(&repl->rule->list);
1706 list_del(&repl->list);
1707 nft_rule_clear(net, repl->rule);
1708 kfree(repl);
1709 }
Patrick McHardy96518512013-10-14 11:00:02 +02001710err2:
1711 nf_tables_rule_destroy(rule);
1712err1:
1713 for (i = 0; i < n; i++) {
1714 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001715 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001716 }
1717 return err;
1718}
1719
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001720static int
1721nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1722{
1723 /* You cannot delete the same rule twice */
1724 if (nft_rule_is_active_next(ctx->net, rule)) {
1725 if (nf_tables_trans_add(rule, ctx) == NULL)
1726 return -ENOMEM;
1727 nft_rule_disactivate_next(ctx->net, rule);
1728 return 0;
1729 }
1730 return -ENOENT;
1731}
1732
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001733static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1734{
1735 struct nft_rule *rule;
1736 int err;
1737
1738 list_for_each_entry(rule, &ctx->chain->rules, list) {
1739 err = nf_tables_delrule_one(ctx, rule);
1740 if (err < 0)
1741 return err;
1742 }
1743 return 0;
1744}
1745
Patrick McHardy96518512013-10-14 11:00:02 +02001746static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1747 const struct nlmsghdr *nlh,
1748 const struct nlattr * const nla[])
1749{
1750 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1751 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001752 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001753 const struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001754 struct nft_chain *chain = NULL;
1755 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001756 int family = nfmsg->nfgen_family, err = 0;
1757 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001758
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001759 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001760 if (IS_ERR(afi))
1761 return PTR_ERR(afi);
1762
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001763 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001764 if (IS_ERR(table))
1765 return PTR_ERR(table);
1766
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001767 if (nla[NFTA_RULE_CHAIN]) {
1768 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1769 if (IS_ERR(chain))
1770 return PTR_ERR(chain);
1771 }
Patrick McHardy96518512013-10-14 11:00:02 +02001772
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001773 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1774
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001775 if (chain) {
1776 if (nla[NFTA_RULE_HANDLE]) {
1777 rule = nf_tables_rule_lookup(chain,
1778 nla[NFTA_RULE_HANDLE]);
1779 if (IS_ERR(rule))
1780 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001781
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001782 err = nf_tables_delrule_one(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001783 } else {
1784 err = nf_table_delrule_by_chain(&ctx);
1785 }
1786 } else {
1787 list_for_each_entry(chain, &table->chains, list) {
1788 ctx.chain = chain;
1789 err = nf_table_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001790 if (err < 0)
1791 break;
Patrick McHardy96518512013-10-14 11:00:02 +02001792 }
1793 }
1794
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001795 return err;
1796}
1797
1798static int nf_tables_commit(struct sk_buff *skb)
1799{
1800 struct net *net = sock_net(skb->sk);
1801 struct nft_rule_trans *rupd, *tmp;
1802
1803 /* Bump generation counter, invalidate any dump in progress */
1804 net->nft.genctr++;
1805
1806 /* A new generation has just started */
1807 net->nft.gencursor = gencursor_next(net);
1808
1809 /* Make sure all packets have left the previous generation before
1810 * purging old rules.
1811 */
1812 synchronize_rcu();
1813
1814 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001815 /* 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);
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001825 list_del(&rupd->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001826 kfree(rupd);
1827 continue;
1828 }
1829
1830 /* This rule is in the past, get rid of it */
1831 list_del_rcu(&rupd->rule->list);
1832 nf_tables_rule_notify(skb, rupd->nlh, rupd->table, rupd->chain,
1833 rupd->rule, NFT_MSG_DELRULE, 0,
1834 rupd->family);
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001835 }
1836
1837 /* Make sure we don't see any packet traversing old rules */
1838 synchronize_rcu();
1839
1840 /* Now we can safely release unused old rules */
1841 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001842 nf_tables_rule_destroy(rupd->rule);
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001843 list_del(&rupd->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001844 kfree(rupd);
1845 }
1846
1847 return 0;
1848}
1849
1850static int nf_tables_abort(struct sk_buff *skb)
1851{
1852 struct net *net = sock_net(skb->sk);
1853 struct nft_rule_trans *rupd, *tmp;
1854
1855 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001856 if (!nft_rule_is_active_next(net, rupd->rule)) {
1857 nft_rule_clear(net, rupd->rule);
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001858 list_del(&rupd->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001859 kfree(rupd);
1860 continue;
1861 }
1862
1863 /* This rule is inactive, get rid of it */
1864 list_del_rcu(&rupd->rule->list);
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001865 }
1866
1867 /* Make sure we don't see any packet accessing aborted rules */
1868 synchronize_rcu();
1869
1870 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001871 nf_tables_rule_destroy(rupd->rule);
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001872 list_del(&rupd->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001873 kfree(rupd);
1874 }
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001875
Patrick McHardy96518512013-10-14 11:00:02 +02001876 return 0;
1877}
1878
Patrick McHardy20a69342013-10-11 12:06:22 +02001879/*
1880 * Sets
1881 */
1882
1883static LIST_HEAD(nf_tables_set_ops);
1884
1885int nft_register_set(struct nft_set_ops *ops)
1886{
1887 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1888 list_add_tail(&ops->list, &nf_tables_set_ops);
1889 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1890 return 0;
1891}
1892EXPORT_SYMBOL_GPL(nft_register_set);
1893
1894void nft_unregister_set(struct nft_set_ops *ops)
1895{
1896 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1897 list_del(&ops->list);
1898 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1899}
1900EXPORT_SYMBOL_GPL(nft_unregister_set);
1901
1902static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1903{
1904 const struct nft_set_ops *ops;
1905 u32 features;
1906
1907#ifdef CONFIG_MODULES
1908 if (list_empty(&nf_tables_set_ops)) {
1909 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1910 request_module("nft-set");
1911 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1912 if (!list_empty(&nf_tables_set_ops))
1913 return ERR_PTR(-EAGAIN);
1914 }
1915#endif
1916 features = 0;
1917 if (nla[NFTA_SET_FLAGS] != NULL) {
1918 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1919 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1920 }
1921
1922 // FIXME: implement selection properly
1923 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1924 if ((ops->features & features) != features)
1925 continue;
1926 if (!try_module_get(ops->owner))
1927 continue;
1928 return ops;
1929 }
1930
1931 return ERR_PTR(-EOPNOTSUPP);
1932}
1933
1934static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1935 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1936 [NFTA_SET_NAME] = { .type = NLA_STRING },
1937 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1938 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1939 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1940 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1941 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1942};
1943
1944static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1945 const struct sk_buff *skb,
1946 const struct nlmsghdr *nlh,
1947 const struct nlattr * const nla[])
1948{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001949 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02001950 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01001951 const struct nft_af_info *afi = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02001952 const struct nft_table *table = NULL;
1953
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01001954 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
1955 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1956 if (IS_ERR(afi))
1957 return PTR_ERR(afi);
1958 }
Patrick McHardy20a69342013-10-11 12:06:22 +02001959
1960 if (nla[NFTA_SET_TABLE] != NULL) {
Patrick McHardyec2c9932014-02-05 15:03:35 +00001961 if (afi == NULL)
1962 return -EAFNOSUPPORT;
1963
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001964 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02001965 if (IS_ERR(table))
1966 return PTR_ERR(table);
1967 }
1968
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001969 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02001970 return 0;
1971}
1972
1973struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1974 const struct nlattr *nla)
1975{
1976 struct nft_set *set;
1977
1978 if (nla == NULL)
1979 return ERR_PTR(-EINVAL);
1980
1981 list_for_each_entry(set, &table->sets, list) {
1982 if (!nla_strcmp(nla, set->name))
1983 return set;
1984 }
1985 return ERR_PTR(-ENOENT);
1986}
1987
1988static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1989 const char *name)
1990{
1991 const struct nft_set *i;
1992 const char *p;
1993 unsigned long *inuse;
1994 unsigned int n = 0;
1995
1996 p = strnchr(name, IFNAMSIZ, '%');
1997 if (p != NULL) {
1998 if (p[1] != 'd' || strchr(p + 2, '%'))
1999 return -EINVAL;
2000
2001 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2002 if (inuse == NULL)
2003 return -ENOMEM;
2004
2005 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01002006 int tmp;
2007
2008 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02002009 continue;
Patrick McHardy53b70282014-02-05 12:26:22 +01002010 if (tmp < 0 || tmp >= BITS_PER_BYTE * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02002011 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002012
2013 set_bit(tmp, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02002014 }
2015
Patrick McHardy53b70282014-02-05 12:26:22 +01002016 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002017 free_page((unsigned long)inuse);
2018 }
2019
2020 snprintf(set->name, sizeof(set->name), name, n);
2021 list_for_each_entry(i, &ctx->table->sets, list) {
2022 if (!strcmp(set->name, i->name))
2023 return -ENFILE;
2024 }
2025 return 0;
2026}
2027
2028static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2029 const struct nft_set *set, u16 event, u16 flags)
2030{
2031 struct nfgenmsg *nfmsg;
2032 struct nlmsghdr *nlh;
2033 u32 portid = NETLINK_CB(ctx->skb).portid;
2034 u32 seq = ctx->nlh->nlmsg_seq;
2035
2036 event |= NFNL_SUBSYS_NFTABLES << 8;
2037 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2038 flags);
2039 if (nlh == NULL)
2040 goto nla_put_failure;
2041
2042 nfmsg = nlmsg_data(nlh);
2043 nfmsg->nfgen_family = ctx->afi->family;
2044 nfmsg->version = NFNETLINK_V0;
2045 nfmsg->res_id = 0;
2046
2047 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2048 goto nla_put_failure;
2049 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2050 goto nla_put_failure;
2051 if (set->flags != 0)
2052 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2053 goto nla_put_failure;
2054
2055 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2056 goto nla_put_failure;
2057 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2058 goto nla_put_failure;
2059 if (set->flags & NFT_SET_MAP) {
2060 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2061 goto nla_put_failure;
2062 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2063 goto nla_put_failure;
2064 }
2065
2066 return nlmsg_end(skb, nlh);
2067
2068nla_put_failure:
2069 nlmsg_trim(skb, nlh);
2070 return -1;
2071}
2072
2073static int nf_tables_set_notify(const struct nft_ctx *ctx,
2074 const struct nft_set *set,
2075 int event)
2076{
2077 struct sk_buff *skb;
2078 u32 portid = NETLINK_CB(ctx->skb).portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002079 bool report;
2080 int err;
2081
2082 report = nlmsg_report(ctx->nlh);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002083 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002084 return 0;
2085
2086 err = -ENOBUFS;
2087 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2088 if (skb == NULL)
2089 goto err;
2090
2091 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2092 if (err < 0) {
2093 kfree_skb(skb);
2094 goto err;
2095 }
2096
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002097 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
Patrick McHardy20a69342013-10-11 12:06:22 +02002098 GFP_KERNEL);
2099err:
2100 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002101 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002102 return err;
2103}
2104
2105static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2106 struct netlink_callback *cb)
2107{
2108 const struct nft_set *set;
2109 unsigned int idx = 0, s_idx = cb->args[0];
2110
2111 if (cb->args[1])
2112 return skb->len;
2113
2114 list_for_each_entry(set, &ctx->table->sets, list) {
2115 if (idx < s_idx)
2116 goto cont;
2117 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2118 NLM_F_MULTI) < 0) {
2119 cb->args[0] = idx;
2120 goto done;
2121 }
2122cont:
2123 idx++;
2124 }
2125 cb->args[1] = 1;
2126done:
2127 return skb->len;
2128}
2129
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002130static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2131 struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002132{
2133 const struct nft_set *set;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002134 unsigned int idx, s_idx = cb->args[0];
Patrick McHardy20a69342013-10-11 12:06:22 +02002135 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2136
2137 if (cb->args[1])
2138 return skb->len;
2139
2140 list_for_each_entry(table, &ctx->afi->tables, list) {
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002141 if (cur_table) {
2142 if (cur_table != table)
2143 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02002144
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002145 cur_table = NULL;
2146 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002147 ctx->table = table;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002148 idx = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002149 list_for_each_entry(set, &ctx->table->sets, list) {
2150 if (idx < s_idx)
2151 goto cont;
2152 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2153 NLM_F_MULTI) < 0) {
2154 cb->args[0] = idx;
2155 cb->args[2] = (unsigned long) table;
2156 goto done;
2157 }
2158cont:
2159 idx++;
2160 }
2161 }
2162 cb->args[1] = 1;
2163done:
2164 return skb->len;
2165}
2166
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002167static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2168 struct netlink_callback *cb)
2169{
2170 const struct nft_set *set;
2171 unsigned int idx, s_idx = cb->args[0];
2172 const struct nft_af_info *afi;
2173 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2174 struct net *net = sock_net(skb->sk);
2175 int cur_family = cb->args[3];
2176
2177 if (cb->args[1])
2178 return skb->len;
2179
2180 list_for_each_entry(afi, &net->nft.af_info, list) {
2181 if (cur_family) {
2182 if (afi->family != cur_family)
2183 continue;
2184
2185 cur_family = 0;
2186 }
2187
2188 list_for_each_entry(table, &afi->tables, list) {
2189 if (cur_table) {
2190 if (cur_table != table)
2191 continue;
2192
2193 cur_table = NULL;
2194 }
2195
2196 ctx->table = table;
2197 ctx->afi = afi;
2198 idx = 0;
2199 list_for_each_entry(set, &ctx->table->sets, list) {
2200 if (idx < s_idx)
2201 goto cont;
2202 if (nf_tables_fill_set(skb, ctx, set,
2203 NFT_MSG_NEWSET,
2204 NLM_F_MULTI) < 0) {
2205 cb->args[0] = idx;
2206 cb->args[2] = (unsigned long) table;
2207 cb->args[3] = afi->family;
2208 goto done;
2209 }
2210cont:
2211 idx++;
2212 }
2213 if (s_idx)
2214 s_idx = 0;
2215 }
2216 }
2217 cb->args[1] = 1;
2218done:
2219 return skb->len;
2220}
2221
Patrick McHardy20a69342013-10-11 12:06:22 +02002222static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2223{
2224 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2225 struct nlattr *nla[NFTA_SET_MAX + 1];
2226 struct nft_ctx ctx;
2227 int err, ret;
2228
2229 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2230 nft_set_policy);
2231 if (err < 0)
2232 return err;
2233
2234 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2235 if (err < 0)
2236 return err;
2237
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002238 if (ctx.table == NULL) {
2239 if (ctx.afi == NULL)
2240 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2241 else
2242 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2243 } else
Patrick McHardy20a69342013-10-11 12:06:22 +02002244 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2245
2246 return ret;
2247}
2248
2249static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2250 const struct nlmsghdr *nlh,
2251 const struct nlattr * const nla[])
2252{
2253 const struct nft_set *set;
2254 struct nft_ctx ctx;
2255 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002256 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002257 int err;
2258
2259 /* Verify existance before starting dump */
2260 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2261 if (err < 0)
2262 return err;
2263
2264 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2265 struct netlink_dump_control c = {
2266 .dump = nf_tables_dump_sets,
2267 };
2268 return netlink_dump_start(nlsk, skb, nlh, &c);
2269 }
2270
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002271 /* Only accept unspec with dump */
2272 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2273 return -EAFNOSUPPORT;
2274
Patrick McHardy20a69342013-10-11 12:06:22 +02002275 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2276 if (IS_ERR(set))
2277 return PTR_ERR(set);
2278
2279 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2280 if (skb2 == NULL)
2281 return -ENOMEM;
2282
2283 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2284 if (err < 0)
2285 goto err;
2286
2287 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2288
2289err:
2290 kfree_skb(skb2);
2291 return err;
2292}
2293
2294static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2295 const struct nlmsghdr *nlh,
2296 const struct nlattr * const nla[])
2297{
2298 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2299 const struct nft_set_ops *ops;
2300 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002301 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002302 struct nft_table *table;
2303 struct nft_set *set;
2304 struct nft_ctx ctx;
2305 char name[IFNAMSIZ];
2306 unsigned int size;
2307 bool create;
2308 u32 ktype, klen, dlen, dtype, flags;
2309 int err;
2310
2311 if (nla[NFTA_SET_TABLE] == NULL ||
2312 nla[NFTA_SET_NAME] == NULL ||
2313 nla[NFTA_SET_KEY_LEN] == NULL)
2314 return -EINVAL;
2315
2316 ktype = NFT_DATA_VALUE;
2317 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2318 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2319 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2320 return -EINVAL;
2321 }
2322
2323 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2324 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2325 return -EINVAL;
2326
2327 flags = 0;
2328 if (nla[NFTA_SET_FLAGS] != NULL) {
2329 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2330 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2331 NFT_SET_INTERVAL | NFT_SET_MAP))
2332 return -EINVAL;
2333 }
2334
2335 dtype = 0;
2336 dlen = 0;
2337 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2338 if (!(flags & NFT_SET_MAP))
2339 return -EINVAL;
2340
2341 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2342 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2343 dtype != NFT_DATA_VERDICT)
2344 return -EINVAL;
2345
2346 if (dtype != NFT_DATA_VERDICT) {
2347 if (nla[NFTA_SET_DATA_LEN] == NULL)
2348 return -EINVAL;
2349 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2350 if (dlen == 0 ||
2351 dlen > FIELD_SIZEOF(struct nft_data, data))
2352 return -EINVAL;
2353 } else
2354 dlen = sizeof(struct nft_data);
2355 } else if (flags & NFT_SET_MAP)
2356 return -EINVAL;
2357
2358 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2359
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002360 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002361 if (IS_ERR(afi))
2362 return PTR_ERR(afi);
2363
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002364 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002365 if (IS_ERR(table))
2366 return PTR_ERR(table);
2367
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002368 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002369
2370 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2371 if (IS_ERR(set)) {
2372 if (PTR_ERR(set) != -ENOENT)
2373 return PTR_ERR(set);
2374 set = NULL;
2375 }
2376
2377 if (set != NULL) {
2378 if (nlh->nlmsg_flags & NLM_F_EXCL)
2379 return -EEXIST;
2380 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2381 return -EOPNOTSUPP;
2382 return 0;
2383 }
2384
2385 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2386 return -ENOENT;
2387
2388 ops = nft_select_set_ops(nla);
2389 if (IS_ERR(ops))
2390 return PTR_ERR(ops);
2391
2392 size = 0;
2393 if (ops->privsize != NULL)
2394 size = ops->privsize(nla);
2395
2396 err = -ENOMEM;
2397 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2398 if (set == NULL)
2399 goto err1;
2400
2401 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2402 err = nf_tables_set_alloc_name(&ctx, set, name);
2403 if (err < 0)
2404 goto err2;
2405
2406 INIT_LIST_HEAD(&set->bindings);
2407 set->ops = ops;
2408 set->ktype = ktype;
2409 set->klen = klen;
2410 set->dtype = dtype;
2411 set->dlen = dlen;
2412 set->flags = flags;
2413
2414 err = ops->init(set, nla);
2415 if (err < 0)
2416 goto err2;
2417
2418 list_add_tail(&set->list, &table->sets);
2419 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2420 return 0;
2421
2422err2:
2423 kfree(set);
2424err1:
2425 module_put(ops->owner);
2426 return err;
2427}
2428
2429static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2430{
2431 list_del(&set->list);
2432 if (!(set->flags & NFT_SET_ANONYMOUS))
2433 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2434
2435 set->ops->destroy(set);
2436 module_put(set->ops->owner);
2437 kfree(set);
2438}
2439
2440static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2441 const struct nlmsghdr *nlh,
2442 const struct nlattr * const nla[])
2443{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002444 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002445 struct nft_set *set;
2446 struct nft_ctx ctx;
2447 int err;
2448
Patrick McHardyec2c9932014-02-05 15:03:35 +00002449 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2450 return -EAFNOSUPPORT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002451 if (nla[NFTA_SET_TABLE] == NULL)
2452 return -EINVAL;
2453
2454 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2455 if (err < 0)
2456 return err;
2457
2458 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2459 if (IS_ERR(set))
2460 return PTR_ERR(set);
2461 if (!list_empty(&set->bindings))
2462 return -EBUSY;
2463
2464 nf_tables_set_destroy(&ctx, set);
2465 return 0;
2466}
2467
2468static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2469 const struct nft_set *set,
2470 const struct nft_set_iter *iter,
2471 const struct nft_set_elem *elem)
2472{
2473 enum nft_registers dreg;
2474
2475 dreg = nft_type_to_reg(set->dtype);
Pablo Neira Ayuso2ee0d3c2013-12-28 00:59:38 +01002476 return nft_validate_data_load(ctx, dreg, &elem->data,
2477 set->dtype == NFT_DATA_VERDICT ?
2478 NFT_DATA_VERDICT : NFT_DATA_VALUE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002479}
2480
2481int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2482 struct nft_set_binding *binding)
2483{
2484 struct nft_set_binding *i;
2485 struct nft_set_iter iter;
2486
2487 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2488 return -EBUSY;
2489
2490 if (set->flags & NFT_SET_MAP) {
2491 /* If the set is already bound to the same chain all
2492 * jumps are already validated for that chain.
2493 */
2494 list_for_each_entry(i, &set->bindings, list) {
2495 if (i->chain == binding->chain)
2496 goto bind;
2497 }
2498
2499 iter.skip = 0;
2500 iter.count = 0;
2501 iter.err = 0;
2502 iter.fn = nf_tables_bind_check_setelem;
2503
2504 set->ops->walk(ctx, set, &iter);
2505 if (iter.err < 0) {
2506 /* Destroy anonymous sets if binding fails */
2507 if (set->flags & NFT_SET_ANONYMOUS)
2508 nf_tables_set_destroy(ctx, set);
2509
2510 return iter.err;
2511 }
2512 }
2513bind:
2514 binding->chain = ctx->chain;
2515 list_add_tail(&binding->list, &set->bindings);
2516 return 0;
2517}
2518
2519void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2520 struct nft_set_binding *binding)
2521{
2522 list_del(&binding->list);
2523
2524 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2525 nf_tables_set_destroy(ctx, set);
2526}
2527
2528/*
2529 * Set elements
2530 */
2531
2532static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2533 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2534 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2535 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2536};
2537
2538static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2539 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2540 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2541 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2542};
2543
2544static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2545 const struct sk_buff *skb,
2546 const struct nlmsghdr *nlh,
2547 const struct nlattr * const nla[])
2548{
2549 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2550 const struct nft_af_info *afi;
2551 const struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002552 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002553
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002554 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002555 if (IS_ERR(afi))
2556 return PTR_ERR(afi);
2557
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002558 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002559 if (IS_ERR(table))
2560 return PTR_ERR(table);
2561
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002562 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002563 return 0;
2564}
2565
2566static int nf_tables_fill_setelem(struct sk_buff *skb,
2567 const struct nft_set *set,
2568 const struct nft_set_elem *elem)
2569{
2570 unsigned char *b = skb_tail_pointer(skb);
2571 struct nlattr *nest;
2572
2573 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2574 if (nest == NULL)
2575 goto nla_put_failure;
2576
2577 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2578 set->klen) < 0)
2579 goto nla_put_failure;
2580
2581 if (set->flags & NFT_SET_MAP &&
2582 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2583 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2584 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2585 set->dlen) < 0)
2586 goto nla_put_failure;
2587
2588 if (elem->flags != 0)
2589 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2590 goto nla_put_failure;
2591
2592 nla_nest_end(skb, nest);
2593 return 0;
2594
2595nla_put_failure:
2596 nlmsg_trim(skb, b);
2597 return -EMSGSIZE;
2598}
2599
2600struct nft_set_dump_args {
2601 const struct netlink_callback *cb;
2602 struct nft_set_iter iter;
2603 struct sk_buff *skb;
2604};
2605
2606static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2607 const struct nft_set *set,
2608 const struct nft_set_iter *iter,
2609 const struct nft_set_elem *elem)
2610{
2611 struct nft_set_dump_args *args;
2612
2613 args = container_of(iter, struct nft_set_dump_args, iter);
2614 return nf_tables_fill_setelem(args->skb, set, elem);
2615}
2616
2617static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2618{
2619 const struct nft_set *set;
2620 struct nft_set_dump_args args;
2621 struct nft_ctx ctx;
2622 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2623 struct nfgenmsg *nfmsg;
2624 struct nlmsghdr *nlh;
2625 struct nlattr *nest;
2626 u32 portid, seq;
2627 int event, err;
2628
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01002629 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2630 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002631 if (err < 0)
2632 return err;
2633
2634 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2635 if (err < 0)
2636 return err;
2637
2638 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2639 if (IS_ERR(set))
2640 return PTR_ERR(set);
2641
2642 event = NFT_MSG_NEWSETELEM;
2643 event |= NFNL_SUBSYS_NFTABLES << 8;
2644 portid = NETLINK_CB(cb->skb).portid;
2645 seq = cb->nlh->nlmsg_seq;
2646
2647 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2648 NLM_F_MULTI);
2649 if (nlh == NULL)
2650 goto nla_put_failure;
2651
2652 nfmsg = nlmsg_data(nlh);
2653 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2654 nfmsg->version = NFNETLINK_V0;
2655 nfmsg->res_id = 0;
2656
2657 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2658 goto nla_put_failure;
2659 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2660 goto nla_put_failure;
2661
2662 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2663 if (nest == NULL)
2664 goto nla_put_failure;
2665
2666 args.cb = cb;
2667 args.skb = skb;
2668 args.iter.skip = cb->args[0];
2669 args.iter.count = 0;
2670 args.iter.err = 0;
2671 args.iter.fn = nf_tables_dump_setelem;
2672 set->ops->walk(&ctx, set, &args.iter);
2673
2674 nla_nest_end(skb, nest);
2675 nlmsg_end(skb, nlh);
2676
2677 if (args.iter.err && args.iter.err != -EMSGSIZE)
2678 return args.iter.err;
2679 if (args.iter.count == cb->args[0])
2680 return 0;
2681
2682 cb->args[0] = args.iter.count;
2683 return skb->len;
2684
2685nla_put_failure:
2686 return -ENOSPC;
2687}
2688
2689static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2690 const struct nlmsghdr *nlh,
2691 const struct nlattr * const nla[])
2692{
2693 const struct nft_set *set;
2694 struct nft_ctx ctx;
2695 int err;
2696
2697 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2698 if (err < 0)
2699 return err;
2700
2701 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2702 if (IS_ERR(set))
2703 return PTR_ERR(set);
2704
2705 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2706 struct netlink_dump_control c = {
2707 .dump = nf_tables_dump_set,
2708 };
2709 return netlink_dump_start(nlsk, skb, nlh, &c);
2710 }
2711 return -EOPNOTSUPP;
2712}
2713
2714static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2715 const struct nlattr *attr)
2716{
2717 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2718 struct nft_data_desc d1, d2;
2719 struct nft_set_elem elem;
2720 struct nft_set_binding *binding;
2721 enum nft_registers dreg;
2722 int err;
2723
2724 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2725 nft_set_elem_policy);
2726 if (err < 0)
2727 return err;
2728
2729 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2730 return -EINVAL;
2731
2732 elem.flags = 0;
2733 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2734 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2735 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2736 return -EINVAL;
2737 }
2738
2739 if (set->flags & NFT_SET_MAP) {
2740 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2741 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2742 return -EINVAL;
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01002743 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
2744 elem.flags & NFT_SET_ELEM_INTERVAL_END)
2745 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02002746 } else {
2747 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2748 return -EINVAL;
2749 }
2750
2751 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2752 if (err < 0)
2753 goto err1;
2754 err = -EINVAL;
2755 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2756 goto err2;
2757
2758 err = -EEXIST;
2759 if (set->ops->get(set, &elem) == 0)
2760 goto err2;
2761
2762 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2763 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2764 if (err < 0)
2765 goto err2;
2766
2767 err = -EINVAL;
2768 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2769 goto err3;
2770
2771 dreg = nft_type_to_reg(set->dtype);
2772 list_for_each_entry(binding, &set->bindings, list) {
2773 struct nft_ctx bind_ctx = {
2774 .afi = ctx->afi,
2775 .table = ctx->table,
2776 .chain = binding->chain,
2777 };
2778
2779 err = nft_validate_data_load(&bind_ctx, dreg,
2780 &elem.data, d2.type);
2781 if (err < 0)
2782 goto err3;
2783 }
2784 }
2785
2786 err = set->ops->insert(set, &elem);
2787 if (err < 0)
2788 goto err3;
2789
2790 return 0;
2791
2792err3:
2793 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2794 nft_data_uninit(&elem.data, d2.type);
2795err2:
2796 nft_data_uninit(&elem.key, d1.type);
2797err1:
2798 return err;
2799}
2800
2801static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2802 const struct nlmsghdr *nlh,
2803 const struct nlattr * const nla[])
2804{
2805 const struct nlattr *attr;
2806 struct nft_set *set;
2807 struct nft_ctx ctx;
2808 int rem, err;
2809
2810 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2811 if (err < 0)
2812 return err;
2813
2814 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2815 if (IS_ERR(set))
2816 return PTR_ERR(set);
2817 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2818 return -EBUSY;
2819
2820 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2821 err = nft_add_set_elem(&ctx, set, attr);
2822 if (err < 0)
2823 return err;
2824 }
2825 return 0;
2826}
2827
2828static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2829 const struct nlattr *attr)
2830{
2831 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2832 struct nft_data_desc desc;
2833 struct nft_set_elem elem;
2834 int err;
2835
2836 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2837 nft_set_elem_policy);
2838 if (err < 0)
2839 goto err1;
2840
2841 err = -EINVAL;
2842 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2843 goto err1;
2844
2845 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2846 if (err < 0)
2847 goto err1;
2848
2849 err = -EINVAL;
2850 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2851 goto err2;
2852
2853 err = set->ops->get(set, &elem);
2854 if (err < 0)
2855 goto err2;
2856
2857 set->ops->remove(set, &elem);
2858
2859 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2860 if (set->flags & NFT_SET_MAP)
2861 nft_data_uninit(&elem.data, set->dtype);
2862
2863err2:
2864 nft_data_uninit(&elem.key, desc.type);
2865err1:
2866 return err;
2867}
2868
2869static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2870 const struct nlmsghdr *nlh,
2871 const struct nlattr * const nla[])
2872{
2873 const struct nlattr *attr;
2874 struct nft_set *set;
2875 struct nft_ctx ctx;
2876 int rem, err;
2877
2878 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2879 if (err < 0)
2880 return err;
2881
2882 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2883 if (IS_ERR(set))
2884 return PTR_ERR(set);
2885 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2886 return -EBUSY;
2887
2888 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2889 err = nft_del_setelem(&ctx, set, attr);
2890 if (err < 0)
2891 return err;
2892 }
2893 return 0;
2894}
2895
Patrick McHardy96518512013-10-14 11:00:02 +02002896static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2897 [NFT_MSG_NEWTABLE] = {
2898 .call = nf_tables_newtable,
2899 .attr_count = NFTA_TABLE_MAX,
2900 .policy = nft_table_policy,
2901 },
2902 [NFT_MSG_GETTABLE] = {
2903 .call = nf_tables_gettable,
2904 .attr_count = NFTA_TABLE_MAX,
2905 .policy = nft_table_policy,
2906 },
2907 [NFT_MSG_DELTABLE] = {
2908 .call = nf_tables_deltable,
2909 .attr_count = NFTA_TABLE_MAX,
2910 .policy = nft_table_policy,
2911 },
2912 [NFT_MSG_NEWCHAIN] = {
2913 .call = nf_tables_newchain,
2914 .attr_count = NFTA_CHAIN_MAX,
2915 .policy = nft_chain_policy,
2916 },
2917 [NFT_MSG_GETCHAIN] = {
2918 .call = nf_tables_getchain,
2919 .attr_count = NFTA_CHAIN_MAX,
2920 .policy = nft_chain_policy,
2921 },
2922 [NFT_MSG_DELCHAIN] = {
2923 .call = nf_tables_delchain,
2924 .attr_count = NFTA_CHAIN_MAX,
2925 .policy = nft_chain_policy,
2926 },
2927 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002928 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02002929 .attr_count = NFTA_RULE_MAX,
2930 .policy = nft_rule_policy,
2931 },
2932 [NFT_MSG_GETRULE] = {
2933 .call = nf_tables_getrule,
2934 .attr_count = NFTA_RULE_MAX,
2935 .policy = nft_rule_policy,
2936 },
2937 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002938 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02002939 .attr_count = NFTA_RULE_MAX,
2940 .policy = nft_rule_policy,
2941 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002942 [NFT_MSG_NEWSET] = {
2943 .call = nf_tables_newset,
2944 .attr_count = NFTA_SET_MAX,
2945 .policy = nft_set_policy,
2946 },
2947 [NFT_MSG_GETSET] = {
2948 .call = nf_tables_getset,
2949 .attr_count = NFTA_SET_MAX,
2950 .policy = nft_set_policy,
2951 },
2952 [NFT_MSG_DELSET] = {
2953 .call = nf_tables_delset,
2954 .attr_count = NFTA_SET_MAX,
2955 .policy = nft_set_policy,
2956 },
2957 [NFT_MSG_NEWSETELEM] = {
2958 .call = nf_tables_newsetelem,
2959 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2960 .policy = nft_set_elem_list_policy,
2961 },
2962 [NFT_MSG_GETSETELEM] = {
2963 .call = nf_tables_getsetelem,
2964 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2965 .policy = nft_set_elem_list_policy,
2966 },
2967 [NFT_MSG_DELSETELEM] = {
2968 .call = nf_tables_delsetelem,
2969 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2970 .policy = nft_set_elem_list_policy,
2971 },
Patrick McHardy96518512013-10-14 11:00:02 +02002972};
2973
2974static const struct nfnetlink_subsystem nf_tables_subsys = {
2975 .name = "nf_tables",
2976 .subsys_id = NFNL_SUBSYS_NFTABLES,
2977 .cb_count = NFT_MSG_MAX,
2978 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002979 .commit = nf_tables_commit,
2980 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02002981};
2982
Patrick McHardy20a69342013-10-11 12:06:22 +02002983/*
2984 * Loop detection - walk through the ruleset beginning at the destination chain
2985 * of a new jump until either the source chain is reached (loop) or all
2986 * reachable chains have been traversed.
2987 *
2988 * The loop check is performed whenever a new jump verdict is added to an
2989 * expression or verdict map or a verdict map is bound to a new chain.
2990 */
2991
2992static int nf_tables_check_loops(const struct nft_ctx *ctx,
2993 const struct nft_chain *chain);
2994
2995static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2996 const struct nft_set *set,
2997 const struct nft_set_iter *iter,
2998 const struct nft_set_elem *elem)
2999{
Pablo Neira Ayuso62f9c8b2014-02-07 14:45:01 +01003000 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3001 return 0;
3002
Patrick McHardy20a69342013-10-11 12:06:22 +02003003 switch (elem->data.verdict) {
3004 case NFT_JUMP:
3005 case NFT_GOTO:
3006 return nf_tables_check_loops(ctx, elem->data.chain);
3007 default:
3008 return 0;
3009 }
3010}
3011
3012static int nf_tables_check_loops(const struct nft_ctx *ctx,
3013 const struct nft_chain *chain)
3014{
3015 const struct nft_rule *rule;
3016 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02003017 const struct nft_set *set;
3018 struct nft_set_binding *binding;
3019 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02003020
3021 if (ctx->chain == chain)
3022 return -ELOOP;
3023
3024 list_for_each_entry(rule, &chain->rules, list) {
3025 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003026 const struct nft_data *data = NULL;
3027 int err;
3028
3029 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02003030 continue;
3031
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003032 err = expr->ops->validate(ctx, expr, &data);
3033 if (err < 0)
3034 return err;
3035
Patrick McHardy20a69342013-10-11 12:06:22 +02003036 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003037 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02003038
3039 switch (data->verdict) {
3040 case NFT_JUMP:
3041 case NFT_GOTO:
3042 err = nf_tables_check_loops(ctx, data->chain);
3043 if (err < 0)
3044 return err;
3045 default:
3046 break;
3047 }
3048 }
3049 }
3050
3051 list_for_each_entry(set, &ctx->table->sets, list) {
3052 if (!(set->flags & NFT_SET_MAP) ||
3053 set->dtype != NFT_DATA_VERDICT)
3054 continue;
3055
3056 list_for_each_entry(binding, &set->bindings, list) {
3057 if (binding->chain != chain)
3058 continue;
3059
3060 iter.skip = 0;
3061 iter.count = 0;
3062 iter.err = 0;
3063 iter.fn = nf_tables_loop_check_setelem;
3064
3065 set->ops->walk(ctx, set, &iter);
3066 if (iter.err < 0)
3067 return iter.err;
3068 }
3069 }
3070
3071 return 0;
3072}
3073
Patrick McHardy96518512013-10-14 11:00:02 +02003074/**
3075 * nft_validate_input_register - validate an expressions' input register
3076 *
3077 * @reg: the register number
3078 *
3079 * Validate that the input register is one of the general purpose
3080 * registers.
3081 */
3082int nft_validate_input_register(enum nft_registers reg)
3083{
3084 if (reg <= NFT_REG_VERDICT)
3085 return -EINVAL;
3086 if (reg > NFT_REG_MAX)
3087 return -ERANGE;
3088 return 0;
3089}
3090EXPORT_SYMBOL_GPL(nft_validate_input_register);
3091
3092/**
3093 * nft_validate_output_register - validate an expressions' output register
3094 *
3095 * @reg: the register number
3096 *
3097 * Validate that the output register is one of the general purpose
3098 * registers or the verdict register.
3099 */
3100int nft_validate_output_register(enum nft_registers reg)
3101{
3102 if (reg < NFT_REG_VERDICT)
3103 return -EINVAL;
3104 if (reg > NFT_REG_MAX)
3105 return -ERANGE;
3106 return 0;
3107}
3108EXPORT_SYMBOL_GPL(nft_validate_output_register);
3109
3110/**
3111 * nft_validate_data_load - validate an expressions' data load
3112 *
3113 * @ctx: context of the expression performing the load
3114 * @reg: the destination register number
3115 * @data: the data to load
3116 * @type: the data type
3117 *
3118 * Validate that a data load uses the appropriate data type for
3119 * the destination register. A value of NULL for the data means
3120 * that its runtime gathered data, which is always of type
3121 * NFT_DATA_VALUE.
3122 */
3123int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3124 const struct nft_data *data,
3125 enum nft_data_types type)
3126{
Patrick McHardy20a69342013-10-11 12:06:22 +02003127 int err;
3128
Patrick McHardy96518512013-10-14 11:00:02 +02003129 switch (reg) {
3130 case NFT_REG_VERDICT:
3131 if (data == NULL || type != NFT_DATA_VERDICT)
3132 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003133
3134 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3135 err = nf_tables_check_loops(ctx, data->chain);
3136 if (err < 0)
3137 return err;
3138
3139 if (ctx->chain->level + 1 > data->chain->level) {
3140 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3141 return -EMLINK;
3142 data->chain->level = ctx->chain->level + 1;
3143 }
3144 }
3145
Patrick McHardy96518512013-10-14 11:00:02 +02003146 return 0;
3147 default:
3148 if (data != NULL && type != NFT_DATA_VALUE)
3149 return -EINVAL;
3150 return 0;
3151 }
3152}
3153EXPORT_SYMBOL_GPL(nft_validate_data_load);
3154
3155static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3156 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3157 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3158 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3159};
3160
3161static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3162 struct nft_data_desc *desc, const struct nlattr *nla)
3163{
3164 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3165 struct nft_chain *chain;
3166 int err;
3167
3168 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3169 if (err < 0)
3170 return err;
3171
3172 if (!tb[NFTA_VERDICT_CODE])
3173 return -EINVAL;
3174 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3175
3176 switch (data->verdict) {
3177 case NF_ACCEPT:
3178 case NF_DROP:
3179 case NF_QUEUE:
3180 case NFT_CONTINUE:
3181 case NFT_BREAK:
3182 case NFT_RETURN:
3183 desc->len = sizeof(data->verdict);
3184 break;
3185 case NFT_JUMP:
3186 case NFT_GOTO:
3187 if (!tb[NFTA_VERDICT_CHAIN])
3188 return -EINVAL;
3189 chain = nf_tables_chain_lookup(ctx->table,
3190 tb[NFTA_VERDICT_CHAIN]);
3191 if (IS_ERR(chain))
3192 return PTR_ERR(chain);
3193 if (chain->flags & NFT_BASE_CHAIN)
3194 return -EOPNOTSUPP;
3195
Patrick McHardy96518512013-10-14 11:00:02 +02003196 chain->use++;
3197 data->chain = chain;
3198 desc->len = sizeof(data);
3199 break;
3200 default:
3201 return -EINVAL;
3202 }
3203
3204 desc->type = NFT_DATA_VERDICT;
3205 return 0;
3206}
3207
3208static void nft_verdict_uninit(const struct nft_data *data)
3209{
3210 switch (data->verdict) {
3211 case NFT_JUMP:
3212 case NFT_GOTO:
3213 data->chain->use--;
3214 break;
3215 }
3216}
3217
3218static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3219{
3220 struct nlattr *nest;
3221
3222 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3223 if (!nest)
3224 goto nla_put_failure;
3225
3226 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3227 goto nla_put_failure;
3228
3229 switch (data->verdict) {
3230 case NFT_JUMP:
3231 case NFT_GOTO:
3232 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3233 goto nla_put_failure;
3234 }
3235 nla_nest_end(skb, nest);
3236 return 0;
3237
3238nla_put_failure:
3239 return -1;
3240}
3241
3242static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3243 struct nft_data_desc *desc, const struct nlattr *nla)
3244{
3245 unsigned int len;
3246
3247 len = nla_len(nla);
3248 if (len == 0)
3249 return -EINVAL;
3250 if (len > sizeof(data->data))
3251 return -EOVERFLOW;
3252
3253 nla_memcpy(data->data, nla, sizeof(data->data));
3254 desc->type = NFT_DATA_VALUE;
3255 desc->len = len;
3256 return 0;
3257}
3258
3259static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3260 unsigned int len)
3261{
3262 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3263}
3264
3265static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3266 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3267 .len = FIELD_SIZEOF(struct nft_data, data) },
3268 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3269};
3270
3271/**
3272 * nft_data_init - parse nf_tables data netlink attributes
3273 *
3274 * @ctx: context of the expression using the data
3275 * @data: destination struct nft_data
3276 * @desc: data description
3277 * @nla: netlink attribute containing data
3278 *
3279 * Parse the netlink data attributes and initialize a struct nft_data.
3280 * The type and length of data are returned in the data description.
3281 *
3282 * The caller can indicate that it only wants to accept data of type
3283 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3284 */
3285int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3286 struct nft_data_desc *desc, const struct nlattr *nla)
3287{
3288 struct nlattr *tb[NFTA_DATA_MAX + 1];
3289 int err;
3290
3291 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3292 if (err < 0)
3293 return err;
3294
3295 if (tb[NFTA_DATA_VALUE])
3296 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3297 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3298 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3299 return -EINVAL;
3300}
3301EXPORT_SYMBOL_GPL(nft_data_init);
3302
3303/**
3304 * nft_data_uninit - release a nft_data item
3305 *
3306 * @data: struct nft_data to release
3307 * @type: type of data
3308 *
3309 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3310 * all others need to be released by calling this function.
3311 */
3312void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3313{
3314 switch (type) {
3315 case NFT_DATA_VALUE:
3316 return;
3317 case NFT_DATA_VERDICT:
3318 return nft_verdict_uninit(data);
3319 default:
3320 WARN_ON(1);
3321 }
3322}
3323EXPORT_SYMBOL_GPL(nft_data_uninit);
3324
3325int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3326 enum nft_data_types type, unsigned int len)
3327{
3328 struct nlattr *nest;
3329 int err;
3330
3331 nest = nla_nest_start(skb, attr);
3332 if (nest == NULL)
3333 return -1;
3334
3335 switch (type) {
3336 case NFT_DATA_VALUE:
3337 err = nft_value_dump(skb, data, len);
3338 break;
3339 case NFT_DATA_VERDICT:
3340 err = nft_verdict_dump(skb, data);
3341 break;
3342 default:
3343 err = -EINVAL;
3344 WARN_ON(1);
3345 }
3346
3347 nla_nest_end(skb, nest);
3348 return err;
3349}
3350EXPORT_SYMBOL_GPL(nft_data_dump);
3351
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003352static int nf_tables_init_net(struct net *net)
3353{
3354 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003355 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003356 return 0;
3357}
3358
3359static struct pernet_operations nf_tables_net_ops = {
3360 .init = nf_tables_init_net,
3361};
3362
Patrick McHardy96518512013-10-14 11:00:02 +02003363static int __init nf_tables_module_init(void)
3364{
3365 int err;
3366
3367 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3368 GFP_KERNEL);
3369 if (info == NULL) {
3370 err = -ENOMEM;
3371 goto err1;
3372 }
3373
3374 err = nf_tables_core_module_init();
3375 if (err < 0)
3376 goto err2;
3377
3378 err = nfnetlink_subsys_register(&nf_tables_subsys);
3379 if (err < 0)
3380 goto err3;
3381
3382 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003383 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003384err3:
3385 nf_tables_core_module_exit();
3386err2:
3387 kfree(info);
3388err1:
3389 return err;
3390}
3391
3392static void __exit nf_tables_module_exit(void)
3393{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003394 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003395 nfnetlink_subsys_unregister(&nf_tables_subsys);
3396 nf_tables_core_module_exit();
3397 kfree(info);
3398}
3399
3400module_init(nf_tables_module_init);
3401module_exit(nf_tables_module_exit);
3402
3403MODULE_LICENSE("GPL");
3404MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3405MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);