blob: 43ae48721254e42e68939ea704ab7fc490550130 [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
Patrick McHardy62472bc2014-03-07 19:08:30 +01001256static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1257 struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +02001258{
1259 if (expr->ops->destroy)
Patrick McHardy62472bc2014-03-07 19:08:30 +01001260 expr->ops->destroy(ctx, expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001261 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001262}
1263
1264/*
1265 * Rules
1266 */
1267
1268static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1269 u64 handle)
1270{
1271 struct nft_rule *rule;
1272
1273 // FIXME: this sucks
1274 list_for_each_entry(rule, &chain->rules, list) {
1275 if (handle == rule->handle)
1276 return rule;
1277 }
1278
1279 return ERR_PTR(-ENOENT);
1280}
1281
1282static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1283 const struct nlattr *nla)
1284{
1285 if (nla == NULL)
1286 return ERR_PTR(-EINVAL);
1287
1288 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1289}
1290
1291static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1292 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1293 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1294 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1295 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1296 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001297 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001298 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001299 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1300 .len = NFT_USERDATA_MAXLEN },
Patrick McHardy96518512013-10-14 11:00:02 +02001301};
1302
1303static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1304 int event, u32 flags, int family,
1305 const struct nft_table *table,
1306 const struct nft_chain *chain,
1307 const struct nft_rule *rule)
1308{
1309 struct nlmsghdr *nlh;
1310 struct nfgenmsg *nfmsg;
1311 const struct nft_expr *expr, *next;
1312 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001313 const struct nft_rule *prule;
1314 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001315
Eric Leblond5e948462013-10-10 13:41:44 +02001316 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001317 flags);
1318 if (nlh == NULL)
1319 goto nla_put_failure;
1320
1321 nfmsg = nlmsg_data(nlh);
1322 nfmsg->nfgen_family = family;
1323 nfmsg->version = NFNETLINK_V0;
1324 nfmsg->res_id = 0;
1325
1326 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1327 goto nla_put_failure;
1328 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1329 goto nla_put_failure;
1330 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1331 goto nla_put_failure;
1332
Eric Leblond5e948462013-10-10 13:41:44 +02001333 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1334 prule = list_entry(rule->list.prev, struct nft_rule, list);
1335 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1336 cpu_to_be64(prule->handle)))
1337 goto nla_put_failure;
1338 }
1339
Patrick McHardy96518512013-10-14 11:00:02 +02001340 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1341 if (list == NULL)
1342 goto nla_put_failure;
1343 nft_rule_for_each_expr(expr, next, rule) {
1344 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1345 if (elem == NULL)
1346 goto nla_put_failure;
1347 if (nf_tables_fill_expr_info(skb, expr) < 0)
1348 goto nla_put_failure;
1349 nla_nest_end(skb, elem);
1350 }
1351 nla_nest_end(skb, list);
1352
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001353 if (rule->ulen &&
1354 nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1355 goto nla_put_failure;
1356
Patrick McHardy96518512013-10-14 11:00:02 +02001357 return nlmsg_end(skb, nlh);
1358
1359nla_put_failure:
1360 nlmsg_trim(skb, nlh);
1361 return -1;
1362}
1363
1364static int nf_tables_rule_notify(const struct sk_buff *oskb,
1365 const struct nlmsghdr *nlh,
1366 const struct nft_table *table,
1367 const struct nft_chain *chain,
1368 const struct nft_rule *rule,
1369 int event, u32 flags, int family)
1370{
1371 struct sk_buff *skb;
1372 u32 portid = NETLINK_CB(oskb).portid;
1373 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1374 u32 seq = nlh->nlmsg_seq;
1375 bool report;
1376 int err;
1377
1378 report = nlmsg_report(nlh);
1379 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1380 return 0;
1381
1382 err = -ENOBUFS;
1383 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1384 if (skb == NULL)
1385 goto err;
1386
1387 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1388 family, table, chain, rule);
1389 if (err < 0) {
1390 kfree_skb(skb);
1391 goto err;
1392 }
1393
1394 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1395 GFP_KERNEL);
1396err:
1397 if (err < 0)
1398 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1399 return err;
1400}
1401
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001402static inline bool
1403nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1404{
1405 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1406}
1407
1408static inline int gencursor_next(struct net *net)
1409{
1410 return net->nft.gencursor+1 == 1 ? 1 : 0;
1411}
1412
1413static inline int
1414nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1415{
1416 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1417}
1418
1419static inline void
1420nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1421{
1422 /* Now inactive, will be active in the future */
1423 rule->genmask = (1 << net->nft.gencursor);
1424}
1425
1426static inline void
1427nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1428{
1429 rule->genmask = (1 << gencursor_next(net));
1430}
1431
1432static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1433{
1434 rule->genmask = 0;
1435}
1436
Patrick McHardy96518512013-10-14 11:00:02 +02001437static int nf_tables_dump_rules(struct sk_buff *skb,
1438 struct netlink_callback *cb)
1439{
1440 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1441 const struct nft_af_info *afi;
1442 const struct nft_table *table;
1443 const struct nft_chain *chain;
1444 const struct nft_rule *rule;
1445 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001446 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001447 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001448 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1449 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
Patrick McHardy96518512013-10-14 11:00:02 +02001450
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001451 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001452 if (family != NFPROTO_UNSPEC && family != afi->family)
1453 continue;
1454
1455 list_for_each_entry(table, &afi->tables, list) {
1456 list_for_each_entry(chain, &table->chains, list) {
1457 list_for_each_entry(rule, &chain->rules, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001458 if (!nft_rule_is_active(net, rule))
1459 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001460 if (idx < s_idx)
1461 goto cont;
1462 if (idx > s_idx)
1463 memset(&cb->args[1], 0,
1464 sizeof(cb->args) - sizeof(cb->args[0]));
1465 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1466 cb->nlh->nlmsg_seq,
1467 NFT_MSG_NEWRULE,
1468 NLM_F_MULTI | NLM_F_APPEND,
1469 afi->family, table, chain, rule) < 0)
1470 goto done;
1471cont:
1472 idx++;
1473 }
1474 }
1475 }
1476 }
1477done:
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001478 /* Invalidate this dump, a transition to the new generation happened */
1479 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1480 return -EBUSY;
1481
Patrick McHardy96518512013-10-14 11:00:02 +02001482 cb->args[0] = idx;
1483 return skb->len;
1484}
1485
1486static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1487 const struct nlmsghdr *nlh,
1488 const struct nlattr * const nla[])
1489{
1490 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1491 const struct nft_af_info *afi;
1492 const struct nft_table *table;
1493 const struct nft_chain *chain;
1494 const struct nft_rule *rule;
1495 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001496 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001497 int family = nfmsg->nfgen_family;
1498 int err;
1499
1500 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1501 struct netlink_dump_control c = {
1502 .dump = nf_tables_dump_rules,
1503 };
1504 return netlink_dump_start(nlsk, skb, nlh, &c);
1505 }
1506
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001507 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001508 if (IS_ERR(afi))
1509 return PTR_ERR(afi);
1510
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001511 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001512 if (IS_ERR(table))
1513 return PTR_ERR(table);
1514
1515 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1516 if (IS_ERR(chain))
1517 return PTR_ERR(chain);
1518
1519 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1520 if (IS_ERR(rule))
1521 return PTR_ERR(rule);
1522
1523 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1524 if (!skb2)
1525 return -ENOMEM;
1526
1527 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1528 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1529 family, table, chain, rule);
1530 if (err < 0)
1531 goto err;
1532
1533 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1534
1535err:
1536 kfree_skb(skb2);
1537 return err;
1538}
1539
Patrick McHardy62472bc2014-03-07 19:08:30 +01001540static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1541 struct nft_rule *rule)
Patrick McHardy96518512013-10-14 11:00:02 +02001542{
Patrick McHardy96518512013-10-14 11:00:02 +02001543 struct nft_expr *expr;
1544
1545 /*
1546 * Careful: some expressions might not be initialized in case this
1547 * is called on error from nf_tables_newrule().
1548 */
1549 expr = nft_expr_first(rule);
1550 while (expr->ops && expr != nft_expr_last(rule)) {
Patrick McHardy62472bc2014-03-07 19:08:30 +01001551 nf_tables_expr_destroy(ctx, expr);
Patrick McHardy96518512013-10-14 11:00:02 +02001552 expr = nft_expr_next(expr);
1553 }
1554 kfree(rule);
1555}
1556
Patrick McHardy96518512013-10-14 11:00:02 +02001557#define NFT_RULE_MAXEXPRS 128
1558
1559static struct nft_expr_info *info;
1560
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001561static struct nft_rule_trans *
Patrick McHardya36e901c2014-03-07 19:08:29 +01001562nf_tables_trans_add(struct nft_ctx *ctx, struct nft_rule *rule)
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001563{
1564 struct nft_rule_trans *rupd;
1565
1566 rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1567 if (rupd == NULL)
1568 return NULL;
1569
Patrick McHardy62472bc2014-03-07 19:08:30 +01001570 rupd->ctx = *ctx;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001571 rupd->rule = rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001572 list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1573
1574 return rupd;
1575}
1576
Patrick McHardy96518512013-10-14 11:00:02 +02001577static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1578 const struct nlmsghdr *nlh,
1579 const struct nlattr * const nla[])
1580{
1581 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1582 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001583 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001584 struct nft_table *table;
1585 struct nft_chain *chain;
1586 struct nft_rule *rule, *old_rule = NULL;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001587 struct nft_rule_trans *repl = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001588 struct nft_expr *expr;
1589 struct nft_ctx ctx;
1590 struct nlattr *tmp;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001591 unsigned int size, i, n, ulen = 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001592 int err, rem;
1593 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02001594 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02001595
1596 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1597
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001598 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02001599 if (IS_ERR(afi))
1600 return PTR_ERR(afi);
1601
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001602 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001603 if (IS_ERR(table))
1604 return PTR_ERR(table);
1605
1606 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1607 if (IS_ERR(chain))
1608 return PTR_ERR(chain);
1609
1610 if (nla[NFTA_RULE_HANDLE]) {
1611 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1612 rule = __nf_tables_rule_lookup(chain, handle);
1613 if (IS_ERR(rule))
1614 return PTR_ERR(rule);
1615
1616 if (nlh->nlmsg_flags & NLM_F_EXCL)
1617 return -EEXIST;
1618 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1619 old_rule = rule;
1620 else
1621 return -EOPNOTSUPP;
1622 } else {
1623 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1624 return -EINVAL;
1625 handle = nf_tables_alloc_handle(table);
1626 }
1627
Eric Leblond5e948462013-10-10 13:41:44 +02001628 if (nla[NFTA_RULE_POSITION]) {
1629 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1630 return -EOPNOTSUPP;
1631
1632 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1633 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1634 if (IS_ERR(old_rule))
1635 return PTR_ERR(old_rule);
1636 }
1637
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001638 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1639
Patrick McHardy96518512013-10-14 11:00:02 +02001640 n = 0;
1641 size = 0;
1642 if (nla[NFTA_RULE_EXPRESSIONS]) {
1643 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1644 err = -EINVAL;
1645 if (nla_type(tmp) != NFTA_LIST_ELEM)
1646 goto err1;
1647 if (n == NFT_RULE_MAXEXPRS)
1648 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001649 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02001650 if (err < 0)
1651 goto err1;
1652 size += info[n].ops->size;
1653 n++;
1654 }
1655 }
1656
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001657 if (nla[NFTA_RULE_USERDATA])
1658 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1659
Patrick McHardy96518512013-10-14 11:00:02 +02001660 err = -ENOMEM;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001661 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001662 if (rule == NULL)
1663 goto err1;
1664
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001665 nft_rule_activate_next(net, rule);
1666
Patrick McHardy96518512013-10-14 11:00:02 +02001667 rule->handle = handle;
1668 rule->dlen = size;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001669 rule->ulen = ulen;
1670
1671 if (ulen)
1672 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
Patrick McHardy96518512013-10-14 11:00:02 +02001673
Patrick McHardy96518512013-10-14 11:00:02 +02001674 expr = nft_expr_first(rule);
1675 for (i = 0; i < n; i++) {
1676 err = nf_tables_newexpr(&ctx, &info[i], expr);
1677 if (err < 0)
1678 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001679 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001680 expr = nft_expr_next(expr);
1681 }
1682
Patrick McHardy96518512013-10-14 11:00:02 +02001683 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001684 if (nft_rule_is_active_next(net, old_rule)) {
Patrick McHardya36e901c2014-03-07 19:08:29 +01001685 repl = nf_tables_trans_add(&ctx, old_rule);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001686 if (repl == NULL) {
1687 err = -ENOMEM;
1688 goto err2;
1689 }
1690 nft_rule_disactivate_next(net, old_rule);
1691 list_add_tail(&rule->list, &old_rule->list);
1692 } else {
1693 err = -ENOENT;
1694 goto err2;
1695 }
Patrick McHardy96518512013-10-14 11:00:02 +02001696 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02001697 if (old_rule)
1698 list_add_rcu(&rule->list, &old_rule->list);
1699 else
1700 list_add_tail_rcu(&rule->list, &chain->rules);
1701 else {
1702 if (old_rule)
1703 list_add_tail_rcu(&rule->list, &old_rule->list);
1704 else
1705 list_add_rcu(&rule->list, &chain->rules);
1706 }
Patrick McHardy96518512013-10-14 11:00:02 +02001707
Patrick McHardya36e901c2014-03-07 19:08:29 +01001708 if (nf_tables_trans_add(&ctx, rule) == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001709 err = -ENOMEM;
1710 goto err3;
1711 }
Patrick McHardy96518512013-10-14 11:00:02 +02001712 return 0;
1713
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001714err3:
1715 list_del_rcu(&rule->list);
1716 if (repl) {
1717 list_del_rcu(&repl->rule->list);
1718 list_del(&repl->list);
1719 nft_rule_clear(net, repl->rule);
1720 kfree(repl);
1721 }
Patrick McHardy96518512013-10-14 11:00:02 +02001722err2:
Patrick McHardy62472bc2014-03-07 19:08:30 +01001723 nf_tables_rule_destroy(&ctx, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001724err1:
1725 for (i = 0; i < n; i++) {
1726 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001727 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001728 }
1729 return err;
1730}
1731
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001732static int
1733nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1734{
1735 /* You cannot delete the same rule twice */
1736 if (nft_rule_is_active_next(ctx->net, rule)) {
Patrick McHardya36e901c2014-03-07 19:08:29 +01001737 if (nf_tables_trans_add(ctx, rule) == NULL)
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001738 return -ENOMEM;
1739 nft_rule_disactivate_next(ctx->net, rule);
1740 return 0;
1741 }
1742 return -ENOENT;
1743}
1744
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001745static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1746{
1747 struct nft_rule *rule;
1748 int err;
1749
1750 list_for_each_entry(rule, &ctx->chain->rules, list) {
1751 err = nf_tables_delrule_one(ctx, rule);
1752 if (err < 0)
1753 return err;
1754 }
1755 return 0;
1756}
1757
Patrick McHardy96518512013-10-14 11:00:02 +02001758static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1759 const struct nlmsghdr *nlh,
1760 const struct nlattr * const nla[])
1761{
1762 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1763 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001764 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001765 const struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001766 struct nft_chain *chain = NULL;
1767 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001768 int family = nfmsg->nfgen_family, err = 0;
1769 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001770
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001771 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001772 if (IS_ERR(afi))
1773 return PTR_ERR(afi);
1774
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001775 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001776 if (IS_ERR(table))
1777 return PTR_ERR(table);
1778
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001779 if (nla[NFTA_RULE_CHAIN]) {
1780 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1781 if (IS_ERR(chain))
1782 return PTR_ERR(chain);
1783 }
Patrick McHardy96518512013-10-14 11:00:02 +02001784
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001785 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1786
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001787 if (chain) {
1788 if (nla[NFTA_RULE_HANDLE]) {
1789 rule = nf_tables_rule_lookup(chain,
1790 nla[NFTA_RULE_HANDLE]);
1791 if (IS_ERR(rule))
1792 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001793
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001794 err = nf_tables_delrule_one(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001795 } else {
1796 err = nf_table_delrule_by_chain(&ctx);
1797 }
1798 } else {
1799 list_for_each_entry(chain, &table->chains, list) {
1800 ctx.chain = chain;
1801 err = nf_table_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001802 if (err < 0)
1803 break;
Patrick McHardy96518512013-10-14 11:00:02 +02001804 }
1805 }
1806
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001807 return err;
1808}
1809
1810static int nf_tables_commit(struct sk_buff *skb)
1811{
1812 struct net *net = sock_net(skb->sk);
1813 struct nft_rule_trans *rupd, *tmp;
1814
1815 /* Bump generation counter, invalidate any dump in progress */
1816 net->nft.genctr++;
1817
1818 /* A new generation has just started */
1819 net->nft.gencursor = gencursor_next(net);
1820
1821 /* Make sure all packets have left the previous generation before
1822 * purging old rules.
1823 */
1824 synchronize_rcu();
1825
1826 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001827 /* This rule was inactive in the past and just became active.
1828 * Clear the next bit of the genmask since its meaning has
1829 * changed, now it is the future.
1830 */
1831 if (nft_rule_is_active(net, rupd->rule)) {
1832 nft_rule_clear(net, rupd->rule);
Patrick McHardy62472bc2014-03-07 19:08:30 +01001833 nf_tables_rule_notify(skb, rupd->ctx.nlh,
1834 rupd->ctx.table, rupd->ctx.chain,
1835 rupd->rule, NFT_MSG_NEWRULE, 0,
1836 rupd->ctx.afi->family);
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001837 list_del(&rupd->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001838 kfree(rupd);
1839 continue;
1840 }
1841
1842 /* This rule is in the past, get rid of it */
1843 list_del_rcu(&rupd->rule->list);
Patrick McHardy62472bc2014-03-07 19:08:30 +01001844 nf_tables_rule_notify(skb, rupd->ctx.nlh,
1845 rupd->ctx.table, rupd->ctx.chain,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001846 rupd->rule, NFT_MSG_DELRULE, 0,
Patrick McHardy62472bc2014-03-07 19:08:30 +01001847 rupd->ctx.afi->family);
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001848 }
1849
1850 /* Make sure we don't see any packet traversing old rules */
1851 synchronize_rcu();
1852
1853 /* Now we can safely release unused old rules */
1854 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
Patrick McHardy62472bc2014-03-07 19:08:30 +01001855 nf_tables_rule_destroy(&rupd->ctx, rupd->rule);
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001856 list_del(&rupd->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001857 kfree(rupd);
1858 }
1859
1860 return 0;
1861}
1862
1863static int nf_tables_abort(struct sk_buff *skb)
1864{
1865 struct net *net = sock_net(skb->sk);
1866 struct nft_rule_trans *rupd, *tmp;
1867
1868 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001869 if (!nft_rule_is_active_next(net, rupd->rule)) {
1870 nft_rule_clear(net, rupd->rule);
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001871 list_del(&rupd->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001872 kfree(rupd);
1873 continue;
1874 }
1875
1876 /* This rule is inactive, get rid of it */
1877 list_del_rcu(&rupd->rule->list);
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001878 }
1879
1880 /* Make sure we don't see any packet accessing aborted rules */
1881 synchronize_rcu();
1882
1883 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
Patrick McHardy62472bc2014-03-07 19:08:30 +01001884 nf_tables_rule_destroy(&rupd->ctx, rupd->rule);
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001885 list_del(&rupd->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001886 kfree(rupd);
1887 }
Pablo Neira Ayuso0165d932014-01-25 14:03:51 +01001888
Patrick McHardy96518512013-10-14 11:00:02 +02001889 return 0;
1890}
1891
Patrick McHardy20a69342013-10-11 12:06:22 +02001892/*
1893 * Sets
1894 */
1895
1896static LIST_HEAD(nf_tables_set_ops);
1897
1898int nft_register_set(struct nft_set_ops *ops)
1899{
1900 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1901 list_add_tail(&ops->list, &nf_tables_set_ops);
1902 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1903 return 0;
1904}
1905EXPORT_SYMBOL_GPL(nft_register_set);
1906
1907void nft_unregister_set(struct nft_set_ops *ops)
1908{
1909 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1910 list_del(&ops->list);
1911 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1912}
1913EXPORT_SYMBOL_GPL(nft_unregister_set);
1914
1915static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1916{
1917 const struct nft_set_ops *ops;
1918 u32 features;
1919
1920#ifdef CONFIG_MODULES
1921 if (list_empty(&nf_tables_set_ops)) {
1922 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1923 request_module("nft-set");
1924 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1925 if (!list_empty(&nf_tables_set_ops))
1926 return ERR_PTR(-EAGAIN);
1927 }
1928#endif
1929 features = 0;
1930 if (nla[NFTA_SET_FLAGS] != NULL) {
1931 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1932 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1933 }
1934
1935 // FIXME: implement selection properly
1936 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1937 if ((ops->features & features) != features)
1938 continue;
1939 if (!try_module_get(ops->owner))
1940 continue;
1941 return ops;
1942 }
1943
1944 return ERR_PTR(-EOPNOTSUPP);
1945}
1946
1947static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1948 [NFTA_SET_TABLE] = { .type = NLA_STRING },
Pablo Neira Ayusoa9bdd832014-03-24 15:10:37 +01001949 [NFTA_SET_NAME] = { .type = NLA_STRING,
1950 .len = IFNAMSIZ - 1 },
Patrick McHardy20a69342013-10-11 12:06:22 +02001951 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1952 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1953 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1954 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1955 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1956};
1957
1958static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1959 const struct sk_buff *skb,
1960 const struct nlmsghdr *nlh,
1961 const struct nlattr * const nla[])
1962{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001963 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02001964 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01001965 const struct nft_af_info *afi = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02001966 const struct nft_table *table = NULL;
1967
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01001968 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
1969 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1970 if (IS_ERR(afi))
1971 return PTR_ERR(afi);
1972 }
Patrick McHardy20a69342013-10-11 12:06:22 +02001973
1974 if (nla[NFTA_SET_TABLE] != NULL) {
Patrick McHardyec2c9932014-02-05 15:03:35 +00001975 if (afi == NULL)
1976 return -EAFNOSUPPORT;
1977
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001978 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02001979 if (IS_ERR(table))
1980 return PTR_ERR(table);
1981 }
1982
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001983 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02001984 return 0;
1985}
1986
1987struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1988 const struct nlattr *nla)
1989{
1990 struct nft_set *set;
1991
1992 if (nla == NULL)
1993 return ERR_PTR(-EINVAL);
1994
1995 list_for_each_entry(set, &table->sets, list) {
1996 if (!nla_strcmp(nla, set->name))
1997 return set;
1998 }
1999 return ERR_PTR(-ENOENT);
2000}
2001
2002static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2003 const char *name)
2004{
2005 const struct nft_set *i;
2006 const char *p;
2007 unsigned long *inuse;
2008 unsigned int n = 0;
2009
2010 p = strnchr(name, IFNAMSIZ, '%');
2011 if (p != NULL) {
2012 if (p[1] != 'd' || strchr(p + 2, '%'))
2013 return -EINVAL;
2014
2015 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2016 if (inuse == NULL)
2017 return -ENOMEM;
2018
2019 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01002020 int tmp;
2021
2022 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02002023 continue;
Patrick McHardy53b70282014-02-05 12:26:22 +01002024 if (tmp < 0 || tmp >= BITS_PER_BYTE * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02002025 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002026
2027 set_bit(tmp, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02002028 }
2029
Patrick McHardy53b70282014-02-05 12:26:22 +01002030 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002031 free_page((unsigned long)inuse);
2032 }
2033
2034 snprintf(set->name, sizeof(set->name), name, n);
2035 list_for_each_entry(i, &ctx->table->sets, list) {
2036 if (!strcmp(set->name, i->name))
2037 return -ENFILE;
2038 }
2039 return 0;
2040}
2041
2042static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2043 const struct nft_set *set, u16 event, u16 flags)
2044{
2045 struct nfgenmsg *nfmsg;
2046 struct nlmsghdr *nlh;
2047 u32 portid = NETLINK_CB(ctx->skb).portid;
2048 u32 seq = ctx->nlh->nlmsg_seq;
2049
2050 event |= NFNL_SUBSYS_NFTABLES << 8;
2051 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2052 flags);
2053 if (nlh == NULL)
2054 goto nla_put_failure;
2055
2056 nfmsg = nlmsg_data(nlh);
2057 nfmsg->nfgen_family = ctx->afi->family;
2058 nfmsg->version = NFNETLINK_V0;
2059 nfmsg->res_id = 0;
2060
2061 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2062 goto nla_put_failure;
2063 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2064 goto nla_put_failure;
2065 if (set->flags != 0)
2066 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2067 goto nla_put_failure;
2068
2069 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2070 goto nla_put_failure;
2071 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2072 goto nla_put_failure;
2073 if (set->flags & NFT_SET_MAP) {
2074 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2075 goto nla_put_failure;
2076 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2077 goto nla_put_failure;
2078 }
2079
2080 return nlmsg_end(skb, nlh);
2081
2082nla_put_failure:
2083 nlmsg_trim(skb, nlh);
2084 return -1;
2085}
2086
2087static int nf_tables_set_notify(const struct nft_ctx *ctx,
2088 const struct nft_set *set,
2089 int event)
2090{
2091 struct sk_buff *skb;
2092 u32 portid = NETLINK_CB(ctx->skb).portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002093 bool report;
2094 int err;
2095
2096 report = nlmsg_report(ctx->nlh);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002097 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002098 return 0;
2099
2100 err = -ENOBUFS;
2101 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2102 if (skb == NULL)
2103 goto err;
2104
2105 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2106 if (err < 0) {
2107 kfree_skb(skb);
2108 goto err;
2109 }
2110
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002111 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
Patrick McHardy20a69342013-10-11 12:06:22 +02002112 GFP_KERNEL);
2113err:
2114 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002115 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002116 return err;
2117}
2118
2119static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2120 struct netlink_callback *cb)
2121{
2122 const struct nft_set *set;
2123 unsigned int idx = 0, s_idx = cb->args[0];
2124
2125 if (cb->args[1])
2126 return skb->len;
2127
2128 list_for_each_entry(set, &ctx->table->sets, list) {
2129 if (idx < s_idx)
2130 goto cont;
2131 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2132 NLM_F_MULTI) < 0) {
2133 cb->args[0] = idx;
2134 goto done;
2135 }
2136cont:
2137 idx++;
2138 }
2139 cb->args[1] = 1;
2140done:
2141 return skb->len;
2142}
2143
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002144static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2145 struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002146{
2147 const struct nft_set *set;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002148 unsigned int idx, s_idx = cb->args[0];
Patrick McHardy20a69342013-10-11 12:06:22 +02002149 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2150
2151 if (cb->args[1])
2152 return skb->len;
2153
2154 list_for_each_entry(table, &ctx->afi->tables, list) {
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002155 if (cur_table) {
2156 if (cur_table != table)
2157 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02002158
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002159 cur_table = NULL;
2160 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002161 ctx->table = table;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002162 idx = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002163 list_for_each_entry(set, &ctx->table->sets, list) {
2164 if (idx < s_idx)
2165 goto cont;
2166 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2167 NLM_F_MULTI) < 0) {
2168 cb->args[0] = idx;
2169 cb->args[2] = (unsigned long) table;
2170 goto done;
2171 }
2172cont:
2173 idx++;
2174 }
2175 }
2176 cb->args[1] = 1;
2177done:
2178 return skb->len;
2179}
2180
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002181static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2182 struct netlink_callback *cb)
2183{
2184 const struct nft_set *set;
2185 unsigned int idx, s_idx = cb->args[0];
2186 const struct nft_af_info *afi;
2187 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2188 struct net *net = sock_net(skb->sk);
2189 int cur_family = cb->args[3];
2190
2191 if (cb->args[1])
2192 return skb->len;
2193
2194 list_for_each_entry(afi, &net->nft.af_info, list) {
2195 if (cur_family) {
2196 if (afi->family != cur_family)
2197 continue;
2198
2199 cur_family = 0;
2200 }
2201
2202 list_for_each_entry(table, &afi->tables, list) {
2203 if (cur_table) {
2204 if (cur_table != table)
2205 continue;
2206
2207 cur_table = NULL;
2208 }
2209
2210 ctx->table = table;
2211 ctx->afi = afi;
2212 idx = 0;
2213 list_for_each_entry(set, &ctx->table->sets, list) {
2214 if (idx < s_idx)
2215 goto cont;
2216 if (nf_tables_fill_set(skb, ctx, set,
2217 NFT_MSG_NEWSET,
2218 NLM_F_MULTI) < 0) {
2219 cb->args[0] = idx;
2220 cb->args[2] = (unsigned long) table;
2221 cb->args[3] = afi->family;
2222 goto done;
2223 }
2224cont:
2225 idx++;
2226 }
2227 if (s_idx)
2228 s_idx = 0;
2229 }
2230 }
2231 cb->args[1] = 1;
2232done:
2233 return skb->len;
2234}
2235
Patrick McHardy20a69342013-10-11 12:06:22 +02002236static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2237{
2238 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2239 struct nlattr *nla[NFTA_SET_MAX + 1];
2240 struct nft_ctx ctx;
2241 int err, ret;
2242
2243 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2244 nft_set_policy);
2245 if (err < 0)
2246 return err;
2247
2248 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2249 if (err < 0)
2250 return err;
2251
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002252 if (ctx.table == NULL) {
2253 if (ctx.afi == NULL)
2254 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2255 else
2256 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2257 } else
Patrick McHardy20a69342013-10-11 12:06:22 +02002258 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2259
2260 return ret;
2261}
2262
2263static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2264 const struct nlmsghdr *nlh,
2265 const struct nlattr * const nla[])
2266{
2267 const struct nft_set *set;
2268 struct nft_ctx ctx;
2269 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002270 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002271 int err;
2272
2273 /* Verify existance before starting dump */
2274 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2275 if (err < 0)
2276 return err;
2277
2278 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2279 struct netlink_dump_control c = {
2280 .dump = nf_tables_dump_sets,
2281 };
2282 return netlink_dump_start(nlsk, skb, nlh, &c);
2283 }
2284
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002285 /* Only accept unspec with dump */
2286 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2287 return -EAFNOSUPPORT;
2288
Patrick McHardy20a69342013-10-11 12:06:22 +02002289 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2290 if (IS_ERR(set))
2291 return PTR_ERR(set);
2292
2293 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2294 if (skb2 == NULL)
2295 return -ENOMEM;
2296
2297 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2298 if (err < 0)
2299 goto err;
2300
2301 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2302
2303err:
2304 kfree_skb(skb2);
2305 return err;
2306}
2307
2308static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2309 const struct nlmsghdr *nlh,
2310 const struct nlattr * const nla[])
2311{
2312 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2313 const struct nft_set_ops *ops;
2314 const struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002315 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002316 struct nft_table *table;
2317 struct nft_set *set;
2318 struct nft_ctx ctx;
2319 char name[IFNAMSIZ];
2320 unsigned int size;
2321 bool create;
2322 u32 ktype, klen, dlen, dtype, flags;
2323 int err;
2324
2325 if (nla[NFTA_SET_TABLE] == NULL ||
2326 nla[NFTA_SET_NAME] == NULL ||
2327 nla[NFTA_SET_KEY_LEN] == NULL)
2328 return -EINVAL;
2329
2330 ktype = NFT_DATA_VALUE;
2331 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2332 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2333 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2334 return -EINVAL;
2335 }
2336
2337 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2338 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2339 return -EINVAL;
2340
2341 flags = 0;
2342 if (nla[NFTA_SET_FLAGS] != NULL) {
2343 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2344 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2345 NFT_SET_INTERVAL | NFT_SET_MAP))
2346 return -EINVAL;
2347 }
2348
2349 dtype = 0;
2350 dlen = 0;
2351 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2352 if (!(flags & NFT_SET_MAP))
2353 return -EINVAL;
2354
2355 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2356 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2357 dtype != NFT_DATA_VERDICT)
2358 return -EINVAL;
2359
2360 if (dtype != NFT_DATA_VERDICT) {
2361 if (nla[NFTA_SET_DATA_LEN] == NULL)
2362 return -EINVAL;
2363 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2364 if (dlen == 0 ||
2365 dlen > FIELD_SIZEOF(struct nft_data, data))
2366 return -EINVAL;
2367 } else
2368 dlen = sizeof(struct nft_data);
2369 } else if (flags & NFT_SET_MAP)
2370 return -EINVAL;
2371
2372 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2373
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002374 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002375 if (IS_ERR(afi))
2376 return PTR_ERR(afi);
2377
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002378 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002379 if (IS_ERR(table))
2380 return PTR_ERR(table);
2381
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002382 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002383
2384 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2385 if (IS_ERR(set)) {
2386 if (PTR_ERR(set) != -ENOENT)
2387 return PTR_ERR(set);
2388 set = NULL;
2389 }
2390
2391 if (set != NULL) {
2392 if (nlh->nlmsg_flags & NLM_F_EXCL)
2393 return -EEXIST;
2394 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2395 return -EOPNOTSUPP;
2396 return 0;
2397 }
2398
2399 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2400 return -ENOENT;
2401
2402 ops = nft_select_set_ops(nla);
2403 if (IS_ERR(ops))
2404 return PTR_ERR(ops);
2405
2406 size = 0;
2407 if (ops->privsize != NULL)
2408 size = ops->privsize(nla);
2409
2410 err = -ENOMEM;
2411 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2412 if (set == NULL)
2413 goto err1;
2414
2415 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2416 err = nf_tables_set_alloc_name(&ctx, set, name);
2417 if (err < 0)
2418 goto err2;
2419
2420 INIT_LIST_HEAD(&set->bindings);
2421 set->ops = ops;
2422 set->ktype = ktype;
2423 set->klen = klen;
2424 set->dtype = dtype;
2425 set->dlen = dlen;
2426 set->flags = flags;
2427
2428 err = ops->init(set, nla);
2429 if (err < 0)
2430 goto err2;
2431
2432 list_add_tail(&set->list, &table->sets);
2433 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2434 return 0;
2435
2436err2:
2437 kfree(set);
2438err1:
2439 module_put(ops->owner);
2440 return err;
2441}
2442
2443static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2444{
2445 list_del(&set->list);
Patrick McHardyab9da5c12014-03-07 19:08:31 +01002446 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
Patrick McHardy20a69342013-10-11 12:06:22 +02002447
2448 set->ops->destroy(set);
2449 module_put(set->ops->owner);
2450 kfree(set);
2451}
2452
2453static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2454 const struct nlmsghdr *nlh,
2455 const struct nlattr * const nla[])
2456{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002457 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002458 struct nft_set *set;
2459 struct nft_ctx ctx;
2460 int err;
2461
Patrick McHardyec2c9932014-02-05 15:03:35 +00002462 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2463 return -EAFNOSUPPORT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002464 if (nla[NFTA_SET_TABLE] == NULL)
2465 return -EINVAL;
2466
2467 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2468 if (err < 0)
2469 return err;
2470
2471 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2472 if (IS_ERR(set))
2473 return PTR_ERR(set);
2474 if (!list_empty(&set->bindings))
2475 return -EBUSY;
2476
2477 nf_tables_set_destroy(&ctx, set);
2478 return 0;
2479}
2480
2481static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2482 const struct nft_set *set,
2483 const struct nft_set_iter *iter,
2484 const struct nft_set_elem *elem)
2485{
2486 enum nft_registers dreg;
2487
2488 dreg = nft_type_to_reg(set->dtype);
Pablo Neira Ayuso2ee0d3c2013-12-28 00:59:38 +01002489 return nft_validate_data_load(ctx, dreg, &elem->data,
2490 set->dtype == NFT_DATA_VERDICT ?
2491 NFT_DATA_VERDICT : NFT_DATA_VALUE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002492}
2493
2494int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2495 struct nft_set_binding *binding)
2496{
2497 struct nft_set_binding *i;
2498 struct nft_set_iter iter;
2499
2500 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2501 return -EBUSY;
2502
2503 if (set->flags & NFT_SET_MAP) {
2504 /* If the set is already bound to the same chain all
2505 * jumps are already validated for that chain.
2506 */
2507 list_for_each_entry(i, &set->bindings, list) {
2508 if (i->chain == binding->chain)
2509 goto bind;
2510 }
2511
2512 iter.skip = 0;
2513 iter.count = 0;
2514 iter.err = 0;
2515 iter.fn = nf_tables_bind_check_setelem;
2516
2517 set->ops->walk(ctx, set, &iter);
2518 if (iter.err < 0) {
2519 /* Destroy anonymous sets if binding fails */
2520 if (set->flags & NFT_SET_ANONYMOUS)
2521 nf_tables_set_destroy(ctx, set);
2522
2523 return iter.err;
2524 }
2525 }
2526bind:
2527 binding->chain = ctx->chain;
2528 list_add_tail(&binding->list, &set->bindings);
2529 return 0;
2530}
2531
2532void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2533 struct nft_set_binding *binding)
2534{
2535 list_del(&binding->list);
2536
2537 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2538 nf_tables_set_destroy(ctx, set);
2539}
2540
2541/*
2542 * Set elements
2543 */
2544
2545static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2546 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2547 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2548 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2549};
2550
2551static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2552 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2553 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2554 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2555};
2556
2557static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2558 const struct sk_buff *skb,
2559 const struct nlmsghdr *nlh,
2560 const struct nlattr * const nla[])
2561{
2562 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2563 const struct nft_af_info *afi;
2564 const struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002565 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002566
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002567 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002568 if (IS_ERR(afi))
2569 return PTR_ERR(afi);
2570
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002571 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002572 if (IS_ERR(table))
2573 return PTR_ERR(table);
2574
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002575 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002576 return 0;
2577}
2578
2579static int nf_tables_fill_setelem(struct sk_buff *skb,
2580 const struct nft_set *set,
2581 const struct nft_set_elem *elem)
2582{
2583 unsigned char *b = skb_tail_pointer(skb);
2584 struct nlattr *nest;
2585
2586 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2587 if (nest == NULL)
2588 goto nla_put_failure;
2589
2590 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2591 set->klen) < 0)
2592 goto nla_put_failure;
2593
2594 if (set->flags & NFT_SET_MAP &&
2595 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2596 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2597 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2598 set->dlen) < 0)
2599 goto nla_put_failure;
2600
2601 if (elem->flags != 0)
2602 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2603 goto nla_put_failure;
2604
2605 nla_nest_end(skb, nest);
2606 return 0;
2607
2608nla_put_failure:
2609 nlmsg_trim(skb, b);
2610 return -EMSGSIZE;
2611}
2612
2613struct nft_set_dump_args {
2614 const struct netlink_callback *cb;
2615 struct nft_set_iter iter;
2616 struct sk_buff *skb;
2617};
2618
2619static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2620 const struct nft_set *set,
2621 const struct nft_set_iter *iter,
2622 const struct nft_set_elem *elem)
2623{
2624 struct nft_set_dump_args *args;
2625
2626 args = container_of(iter, struct nft_set_dump_args, iter);
2627 return nf_tables_fill_setelem(args->skb, set, elem);
2628}
2629
2630static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2631{
2632 const struct nft_set *set;
2633 struct nft_set_dump_args args;
2634 struct nft_ctx ctx;
2635 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2636 struct nfgenmsg *nfmsg;
2637 struct nlmsghdr *nlh;
2638 struct nlattr *nest;
2639 u32 portid, seq;
2640 int event, err;
2641
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01002642 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2643 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002644 if (err < 0)
2645 return err;
2646
2647 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2648 if (err < 0)
2649 return err;
2650
2651 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2652 if (IS_ERR(set))
2653 return PTR_ERR(set);
2654
2655 event = NFT_MSG_NEWSETELEM;
2656 event |= NFNL_SUBSYS_NFTABLES << 8;
2657 portid = NETLINK_CB(cb->skb).portid;
2658 seq = cb->nlh->nlmsg_seq;
2659
2660 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2661 NLM_F_MULTI);
2662 if (nlh == NULL)
2663 goto nla_put_failure;
2664
2665 nfmsg = nlmsg_data(nlh);
2666 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2667 nfmsg->version = NFNETLINK_V0;
2668 nfmsg->res_id = 0;
2669
2670 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2671 goto nla_put_failure;
2672 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2673 goto nla_put_failure;
2674
2675 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2676 if (nest == NULL)
2677 goto nla_put_failure;
2678
2679 args.cb = cb;
2680 args.skb = skb;
2681 args.iter.skip = cb->args[0];
2682 args.iter.count = 0;
2683 args.iter.err = 0;
2684 args.iter.fn = nf_tables_dump_setelem;
2685 set->ops->walk(&ctx, set, &args.iter);
2686
2687 nla_nest_end(skb, nest);
2688 nlmsg_end(skb, nlh);
2689
2690 if (args.iter.err && args.iter.err != -EMSGSIZE)
2691 return args.iter.err;
2692 if (args.iter.count == cb->args[0])
2693 return 0;
2694
2695 cb->args[0] = args.iter.count;
2696 return skb->len;
2697
2698nla_put_failure:
2699 return -ENOSPC;
2700}
2701
2702static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2703 const struct nlmsghdr *nlh,
2704 const struct nlattr * const nla[])
2705{
2706 const struct nft_set *set;
2707 struct nft_ctx ctx;
2708 int err;
2709
2710 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2711 if (err < 0)
2712 return err;
2713
2714 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2715 if (IS_ERR(set))
2716 return PTR_ERR(set);
2717
2718 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2719 struct netlink_dump_control c = {
2720 .dump = nf_tables_dump_set,
2721 };
2722 return netlink_dump_start(nlsk, skb, nlh, &c);
2723 }
2724 return -EOPNOTSUPP;
2725}
2726
2727static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2728 const struct nlattr *attr)
2729{
2730 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2731 struct nft_data_desc d1, d2;
2732 struct nft_set_elem elem;
2733 struct nft_set_binding *binding;
2734 enum nft_registers dreg;
2735 int err;
2736
2737 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2738 nft_set_elem_policy);
2739 if (err < 0)
2740 return err;
2741
2742 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2743 return -EINVAL;
2744
2745 elem.flags = 0;
2746 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2747 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2748 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2749 return -EINVAL;
2750 }
2751
2752 if (set->flags & NFT_SET_MAP) {
2753 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2754 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2755 return -EINVAL;
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01002756 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
2757 elem.flags & NFT_SET_ELEM_INTERVAL_END)
2758 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02002759 } else {
2760 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2761 return -EINVAL;
2762 }
2763
2764 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2765 if (err < 0)
2766 goto err1;
2767 err = -EINVAL;
2768 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2769 goto err2;
2770
2771 err = -EEXIST;
2772 if (set->ops->get(set, &elem) == 0)
2773 goto err2;
2774
2775 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2776 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2777 if (err < 0)
2778 goto err2;
2779
2780 err = -EINVAL;
2781 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2782 goto err3;
2783
2784 dreg = nft_type_to_reg(set->dtype);
2785 list_for_each_entry(binding, &set->bindings, list) {
2786 struct nft_ctx bind_ctx = {
2787 .afi = ctx->afi,
2788 .table = ctx->table,
2789 .chain = binding->chain,
2790 };
2791
2792 err = nft_validate_data_load(&bind_ctx, dreg,
2793 &elem.data, d2.type);
2794 if (err < 0)
2795 goto err3;
2796 }
2797 }
2798
2799 err = set->ops->insert(set, &elem);
2800 if (err < 0)
2801 goto err3;
2802
2803 return 0;
2804
2805err3:
2806 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2807 nft_data_uninit(&elem.data, d2.type);
2808err2:
2809 nft_data_uninit(&elem.key, d1.type);
2810err1:
2811 return err;
2812}
2813
2814static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2815 const struct nlmsghdr *nlh,
2816 const struct nlattr * const nla[])
2817{
2818 const struct nlattr *attr;
2819 struct nft_set *set;
2820 struct nft_ctx ctx;
2821 int rem, err;
2822
2823 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2824 if (err < 0)
2825 return err;
2826
2827 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2828 if (IS_ERR(set))
2829 return PTR_ERR(set);
2830 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2831 return -EBUSY;
2832
2833 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2834 err = nft_add_set_elem(&ctx, set, attr);
2835 if (err < 0)
2836 return err;
2837 }
2838 return 0;
2839}
2840
2841static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2842 const struct nlattr *attr)
2843{
2844 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2845 struct nft_data_desc desc;
2846 struct nft_set_elem elem;
2847 int err;
2848
2849 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2850 nft_set_elem_policy);
2851 if (err < 0)
2852 goto err1;
2853
2854 err = -EINVAL;
2855 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2856 goto err1;
2857
2858 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2859 if (err < 0)
2860 goto err1;
2861
2862 err = -EINVAL;
2863 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2864 goto err2;
2865
2866 err = set->ops->get(set, &elem);
2867 if (err < 0)
2868 goto err2;
2869
2870 set->ops->remove(set, &elem);
2871
2872 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2873 if (set->flags & NFT_SET_MAP)
2874 nft_data_uninit(&elem.data, set->dtype);
2875
2876err2:
2877 nft_data_uninit(&elem.key, desc.type);
2878err1:
2879 return err;
2880}
2881
2882static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2883 const struct nlmsghdr *nlh,
2884 const struct nlattr * const nla[])
2885{
2886 const struct nlattr *attr;
2887 struct nft_set *set;
2888 struct nft_ctx ctx;
2889 int rem, err;
2890
2891 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2892 if (err < 0)
2893 return err;
2894
2895 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2896 if (IS_ERR(set))
2897 return PTR_ERR(set);
2898 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2899 return -EBUSY;
2900
2901 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2902 err = nft_del_setelem(&ctx, set, attr);
2903 if (err < 0)
2904 return err;
2905 }
2906 return 0;
2907}
2908
Patrick McHardy96518512013-10-14 11:00:02 +02002909static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2910 [NFT_MSG_NEWTABLE] = {
2911 .call = nf_tables_newtable,
2912 .attr_count = NFTA_TABLE_MAX,
2913 .policy = nft_table_policy,
2914 },
2915 [NFT_MSG_GETTABLE] = {
2916 .call = nf_tables_gettable,
2917 .attr_count = NFTA_TABLE_MAX,
2918 .policy = nft_table_policy,
2919 },
2920 [NFT_MSG_DELTABLE] = {
2921 .call = nf_tables_deltable,
2922 .attr_count = NFTA_TABLE_MAX,
2923 .policy = nft_table_policy,
2924 },
2925 [NFT_MSG_NEWCHAIN] = {
2926 .call = nf_tables_newchain,
2927 .attr_count = NFTA_CHAIN_MAX,
2928 .policy = nft_chain_policy,
2929 },
2930 [NFT_MSG_GETCHAIN] = {
2931 .call = nf_tables_getchain,
2932 .attr_count = NFTA_CHAIN_MAX,
2933 .policy = nft_chain_policy,
2934 },
2935 [NFT_MSG_DELCHAIN] = {
2936 .call = nf_tables_delchain,
2937 .attr_count = NFTA_CHAIN_MAX,
2938 .policy = nft_chain_policy,
2939 },
2940 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002941 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02002942 .attr_count = NFTA_RULE_MAX,
2943 .policy = nft_rule_policy,
2944 },
2945 [NFT_MSG_GETRULE] = {
2946 .call = nf_tables_getrule,
2947 .attr_count = NFTA_RULE_MAX,
2948 .policy = nft_rule_policy,
2949 },
2950 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002951 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02002952 .attr_count = NFTA_RULE_MAX,
2953 .policy = nft_rule_policy,
2954 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002955 [NFT_MSG_NEWSET] = {
2956 .call = nf_tables_newset,
2957 .attr_count = NFTA_SET_MAX,
2958 .policy = nft_set_policy,
2959 },
2960 [NFT_MSG_GETSET] = {
2961 .call = nf_tables_getset,
2962 .attr_count = NFTA_SET_MAX,
2963 .policy = nft_set_policy,
2964 },
2965 [NFT_MSG_DELSET] = {
2966 .call = nf_tables_delset,
2967 .attr_count = NFTA_SET_MAX,
2968 .policy = nft_set_policy,
2969 },
2970 [NFT_MSG_NEWSETELEM] = {
2971 .call = nf_tables_newsetelem,
2972 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2973 .policy = nft_set_elem_list_policy,
2974 },
2975 [NFT_MSG_GETSETELEM] = {
2976 .call = nf_tables_getsetelem,
2977 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2978 .policy = nft_set_elem_list_policy,
2979 },
2980 [NFT_MSG_DELSETELEM] = {
2981 .call = nf_tables_delsetelem,
2982 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2983 .policy = nft_set_elem_list_policy,
2984 },
Patrick McHardy96518512013-10-14 11:00:02 +02002985};
2986
2987static const struct nfnetlink_subsystem nf_tables_subsys = {
2988 .name = "nf_tables",
2989 .subsys_id = NFNL_SUBSYS_NFTABLES,
2990 .cb_count = NFT_MSG_MAX,
2991 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002992 .commit = nf_tables_commit,
2993 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02002994};
2995
Patrick McHardy20a69342013-10-11 12:06:22 +02002996/*
2997 * Loop detection - walk through the ruleset beginning at the destination chain
2998 * of a new jump until either the source chain is reached (loop) or all
2999 * reachable chains have been traversed.
3000 *
3001 * The loop check is performed whenever a new jump verdict is added to an
3002 * expression or verdict map or a verdict map is bound to a new chain.
3003 */
3004
3005static int nf_tables_check_loops(const struct nft_ctx *ctx,
3006 const struct nft_chain *chain);
3007
3008static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3009 const struct nft_set *set,
3010 const struct nft_set_iter *iter,
3011 const struct nft_set_elem *elem)
3012{
Pablo Neira Ayuso62f9c8b2014-02-07 14:45:01 +01003013 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3014 return 0;
3015
Patrick McHardy20a69342013-10-11 12:06:22 +02003016 switch (elem->data.verdict) {
3017 case NFT_JUMP:
3018 case NFT_GOTO:
3019 return nf_tables_check_loops(ctx, elem->data.chain);
3020 default:
3021 return 0;
3022 }
3023}
3024
3025static int nf_tables_check_loops(const struct nft_ctx *ctx,
3026 const struct nft_chain *chain)
3027{
3028 const struct nft_rule *rule;
3029 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02003030 const struct nft_set *set;
3031 struct nft_set_binding *binding;
3032 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02003033
3034 if (ctx->chain == chain)
3035 return -ELOOP;
3036
3037 list_for_each_entry(rule, &chain->rules, list) {
3038 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003039 const struct nft_data *data = NULL;
3040 int err;
3041
3042 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02003043 continue;
3044
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003045 err = expr->ops->validate(ctx, expr, &data);
3046 if (err < 0)
3047 return err;
3048
Patrick McHardy20a69342013-10-11 12:06:22 +02003049 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003050 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02003051
3052 switch (data->verdict) {
3053 case NFT_JUMP:
3054 case NFT_GOTO:
3055 err = nf_tables_check_loops(ctx, data->chain);
3056 if (err < 0)
3057 return err;
3058 default:
3059 break;
3060 }
3061 }
3062 }
3063
3064 list_for_each_entry(set, &ctx->table->sets, list) {
3065 if (!(set->flags & NFT_SET_MAP) ||
3066 set->dtype != NFT_DATA_VERDICT)
3067 continue;
3068
3069 list_for_each_entry(binding, &set->bindings, list) {
3070 if (binding->chain != chain)
3071 continue;
3072
3073 iter.skip = 0;
3074 iter.count = 0;
3075 iter.err = 0;
3076 iter.fn = nf_tables_loop_check_setelem;
3077
3078 set->ops->walk(ctx, set, &iter);
3079 if (iter.err < 0)
3080 return iter.err;
3081 }
3082 }
3083
3084 return 0;
3085}
3086
Patrick McHardy96518512013-10-14 11:00:02 +02003087/**
3088 * nft_validate_input_register - validate an expressions' input register
3089 *
3090 * @reg: the register number
3091 *
3092 * Validate that the input register is one of the general purpose
3093 * registers.
3094 */
3095int nft_validate_input_register(enum nft_registers reg)
3096{
3097 if (reg <= NFT_REG_VERDICT)
3098 return -EINVAL;
3099 if (reg > NFT_REG_MAX)
3100 return -ERANGE;
3101 return 0;
3102}
3103EXPORT_SYMBOL_GPL(nft_validate_input_register);
3104
3105/**
3106 * nft_validate_output_register - validate an expressions' output register
3107 *
3108 * @reg: the register number
3109 *
3110 * Validate that the output register is one of the general purpose
3111 * registers or the verdict register.
3112 */
3113int nft_validate_output_register(enum nft_registers reg)
3114{
3115 if (reg < NFT_REG_VERDICT)
3116 return -EINVAL;
3117 if (reg > NFT_REG_MAX)
3118 return -ERANGE;
3119 return 0;
3120}
3121EXPORT_SYMBOL_GPL(nft_validate_output_register);
3122
3123/**
3124 * nft_validate_data_load - validate an expressions' data load
3125 *
3126 * @ctx: context of the expression performing the load
3127 * @reg: the destination register number
3128 * @data: the data to load
3129 * @type: the data type
3130 *
3131 * Validate that a data load uses the appropriate data type for
3132 * the destination register. A value of NULL for the data means
3133 * that its runtime gathered data, which is always of type
3134 * NFT_DATA_VALUE.
3135 */
3136int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3137 const struct nft_data *data,
3138 enum nft_data_types type)
3139{
Patrick McHardy20a69342013-10-11 12:06:22 +02003140 int err;
3141
Patrick McHardy96518512013-10-14 11:00:02 +02003142 switch (reg) {
3143 case NFT_REG_VERDICT:
3144 if (data == NULL || type != NFT_DATA_VERDICT)
3145 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003146
3147 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3148 err = nf_tables_check_loops(ctx, data->chain);
3149 if (err < 0)
3150 return err;
3151
3152 if (ctx->chain->level + 1 > data->chain->level) {
3153 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3154 return -EMLINK;
3155 data->chain->level = ctx->chain->level + 1;
3156 }
3157 }
3158
Patrick McHardy96518512013-10-14 11:00:02 +02003159 return 0;
3160 default:
3161 if (data != NULL && type != NFT_DATA_VALUE)
3162 return -EINVAL;
3163 return 0;
3164 }
3165}
3166EXPORT_SYMBOL_GPL(nft_validate_data_load);
3167
3168static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3169 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3170 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3171 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3172};
3173
3174static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3175 struct nft_data_desc *desc, const struct nlattr *nla)
3176{
3177 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3178 struct nft_chain *chain;
3179 int err;
3180
3181 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3182 if (err < 0)
3183 return err;
3184
3185 if (!tb[NFTA_VERDICT_CODE])
3186 return -EINVAL;
3187 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3188
3189 switch (data->verdict) {
Patrick McHardye0abdad2014-02-18 18:06:50 +00003190 default:
3191 switch (data->verdict & NF_VERDICT_MASK) {
3192 case NF_ACCEPT:
3193 case NF_DROP:
3194 case NF_QUEUE:
3195 break;
3196 default:
3197 return -EINVAL;
3198 }
3199 /* fall through */
Patrick McHardy96518512013-10-14 11:00:02 +02003200 case NFT_CONTINUE:
3201 case NFT_BREAK:
3202 case NFT_RETURN:
3203 desc->len = sizeof(data->verdict);
3204 break;
3205 case NFT_JUMP:
3206 case NFT_GOTO:
3207 if (!tb[NFTA_VERDICT_CHAIN])
3208 return -EINVAL;
3209 chain = nf_tables_chain_lookup(ctx->table,
3210 tb[NFTA_VERDICT_CHAIN]);
3211 if (IS_ERR(chain))
3212 return PTR_ERR(chain);
3213 if (chain->flags & NFT_BASE_CHAIN)
3214 return -EOPNOTSUPP;
3215
Patrick McHardy96518512013-10-14 11:00:02 +02003216 chain->use++;
3217 data->chain = chain;
3218 desc->len = sizeof(data);
3219 break;
Patrick McHardy96518512013-10-14 11:00:02 +02003220 }
3221
3222 desc->type = NFT_DATA_VERDICT;
3223 return 0;
3224}
3225
3226static void nft_verdict_uninit(const struct nft_data *data)
3227{
3228 switch (data->verdict) {
3229 case NFT_JUMP:
3230 case NFT_GOTO:
3231 data->chain->use--;
3232 break;
3233 }
3234}
3235
3236static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3237{
3238 struct nlattr *nest;
3239
3240 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3241 if (!nest)
3242 goto nla_put_failure;
3243
3244 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3245 goto nla_put_failure;
3246
3247 switch (data->verdict) {
3248 case NFT_JUMP:
3249 case NFT_GOTO:
3250 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3251 goto nla_put_failure;
3252 }
3253 nla_nest_end(skb, nest);
3254 return 0;
3255
3256nla_put_failure:
3257 return -1;
3258}
3259
3260static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3261 struct nft_data_desc *desc, const struct nlattr *nla)
3262{
3263 unsigned int len;
3264
3265 len = nla_len(nla);
3266 if (len == 0)
3267 return -EINVAL;
3268 if (len > sizeof(data->data))
3269 return -EOVERFLOW;
3270
3271 nla_memcpy(data->data, nla, sizeof(data->data));
3272 desc->type = NFT_DATA_VALUE;
3273 desc->len = len;
3274 return 0;
3275}
3276
3277static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3278 unsigned int len)
3279{
3280 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3281}
3282
3283static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3284 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3285 .len = FIELD_SIZEOF(struct nft_data, data) },
3286 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3287};
3288
3289/**
3290 * nft_data_init - parse nf_tables data netlink attributes
3291 *
3292 * @ctx: context of the expression using the data
3293 * @data: destination struct nft_data
3294 * @desc: data description
3295 * @nla: netlink attribute containing data
3296 *
3297 * Parse the netlink data attributes and initialize a struct nft_data.
3298 * The type and length of data are returned in the data description.
3299 *
3300 * The caller can indicate that it only wants to accept data of type
3301 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3302 */
3303int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3304 struct nft_data_desc *desc, const struct nlattr *nla)
3305{
3306 struct nlattr *tb[NFTA_DATA_MAX + 1];
3307 int err;
3308
3309 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3310 if (err < 0)
3311 return err;
3312
3313 if (tb[NFTA_DATA_VALUE])
3314 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3315 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3316 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3317 return -EINVAL;
3318}
3319EXPORT_SYMBOL_GPL(nft_data_init);
3320
3321/**
3322 * nft_data_uninit - release a nft_data item
3323 *
3324 * @data: struct nft_data to release
3325 * @type: type of data
3326 *
3327 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3328 * all others need to be released by calling this function.
3329 */
3330void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3331{
3332 switch (type) {
3333 case NFT_DATA_VALUE:
3334 return;
3335 case NFT_DATA_VERDICT:
3336 return nft_verdict_uninit(data);
3337 default:
3338 WARN_ON(1);
3339 }
3340}
3341EXPORT_SYMBOL_GPL(nft_data_uninit);
3342
3343int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3344 enum nft_data_types type, unsigned int len)
3345{
3346 struct nlattr *nest;
3347 int err;
3348
3349 nest = nla_nest_start(skb, attr);
3350 if (nest == NULL)
3351 return -1;
3352
3353 switch (type) {
3354 case NFT_DATA_VALUE:
3355 err = nft_value_dump(skb, data, len);
3356 break;
3357 case NFT_DATA_VERDICT:
3358 err = nft_verdict_dump(skb, data);
3359 break;
3360 default:
3361 err = -EINVAL;
3362 WARN_ON(1);
3363 }
3364
3365 nla_nest_end(skb, nest);
3366 return err;
3367}
3368EXPORT_SYMBOL_GPL(nft_data_dump);
3369
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003370static int nf_tables_init_net(struct net *net)
3371{
3372 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003373 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003374 return 0;
3375}
3376
3377static struct pernet_operations nf_tables_net_ops = {
3378 .init = nf_tables_init_net,
3379};
3380
Patrick McHardy96518512013-10-14 11:00:02 +02003381static int __init nf_tables_module_init(void)
3382{
3383 int err;
3384
3385 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3386 GFP_KERNEL);
3387 if (info == NULL) {
3388 err = -ENOMEM;
3389 goto err1;
3390 }
3391
3392 err = nf_tables_core_module_init();
3393 if (err < 0)
3394 goto err2;
3395
3396 err = nfnetlink_subsys_register(&nf_tables_subsys);
3397 if (err < 0)
3398 goto err3;
3399
3400 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003401 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003402err3:
3403 nf_tables_core_module_exit();
3404err2:
3405 kfree(info);
3406err1:
3407 return err;
3408}
3409
3410static void __exit nf_tables_module_exit(void)
3411{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003412 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003413 nfnetlink_subsys_unregister(&nf_tables_subsys);
3414 nf_tables_core_module_exit();
3415 kfree(info);
3416}
3417
3418module_init(nf_tables_module_init);
3419module_exit(nf_tables_module_exit);
3420
3421MODULE_LICENSE("GPL");
3422MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3423MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);