blob: 3e685dbb2921e62270239cc1ecc8250f6a0fdda9 [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001/*
Patrick McHardy20a69342013-10-11 12:06:22 +02002 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
Patrick McHardy96518512013-10-14 11:00:02 +02003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/skbuff.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nfnetlink.h>
18#include <linux/netfilter/nf_tables.h>
19#include <net/netfilter/nf_tables_core.h>
20#include <net/netfilter/nf_tables.h>
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020021#include <net/net_namespace.h>
Patrick McHardy96518512013-10-14 11:00:02 +020022#include <net/sock.h>
23
Patrick McHardy96518512013-10-14 11:00:02 +020024static LIST_HEAD(nf_tables_expressions);
25
26/**
27 * nft_register_afinfo - register nf_tables address family info
28 *
29 * @afi: address family info to register
30 *
31 * Register the address family for use with nf_tables. Returns zero on
32 * success or a negative errno code otherwise.
33 */
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020034int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
Patrick McHardy96518512013-10-14 11:00:02 +020035{
36 INIT_LIST_HEAD(&afi->tables);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020038 list_add_tail(&afi->list, &net->nft.af_info);
Patrick McHardy96518512013-10-14 11:00:02 +020039 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
40 return 0;
41}
42EXPORT_SYMBOL_GPL(nft_register_afinfo);
43
44/**
45 * nft_unregister_afinfo - unregister nf_tables address family info
46 *
47 * @afi: address family info to unregister
48 *
49 * Unregister the address family for use with nf_tables.
50 */
51void nft_unregister_afinfo(struct nft_af_info *afi)
52{
53 nfnl_lock(NFNL_SUBSYS_NFTABLES);
54 list_del(&afi->list);
55 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
56}
57EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
58
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020059static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
Patrick McHardy96518512013-10-14 11:00:02 +020060{
61 struct nft_af_info *afi;
62
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020063 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +020064 if (afi->family == family)
65 return afi;
66 }
67 return NULL;
68}
69
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020070static struct nft_af_info *
71nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
Patrick McHardy96518512013-10-14 11:00:02 +020072{
73 struct nft_af_info *afi;
74
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020075 afi = nft_afinfo_lookup(net, family);
Patrick McHardy96518512013-10-14 11:00:02 +020076 if (afi != NULL)
77 return afi;
78#ifdef CONFIG_MODULES
79 if (autoload) {
80 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
81 request_module("nft-afinfo-%u", family);
82 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020083 afi = nft_afinfo_lookup(net, family);
Patrick McHardy96518512013-10-14 11:00:02 +020084 if (afi != NULL)
85 return ERR_PTR(-EAGAIN);
86 }
87#endif
88 return ERR_PTR(-EAFNOSUPPORT);
89}
90
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +020091static void nft_ctx_init(struct nft_ctx *ctx,
92 const struct sk_buff *skb,
93 const struct nlmsghdr *nlh,
94 struct nft_af_info *afi,
95 struct nft_table *table,
96 struct nft_chain *chain,
97 const struct nlattr * const *nla)
98{
99 ctx->net = sock_net(skb->sk);
100 ctx->skb = skb;
101 ctx->nlh = nlh;
102 ctx->afi = afi;
103 ctx->table = table;
104 ctx->chain = chain;
105 ctx->nla = nla;
106}
107
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +0200108static struct nft_trans *nft_trans_alloc(struct nft_ctx *ctx, int msg_type,
109 u32 size)
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +0200110{
111 struct nft_trans *trans;
112
113 trans = kzalloc(sizeof(struct nft_trans) + size, GFP_KERNEL);
114 if (trans == NULL)
115 return NULL;
116
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +0200117 trans->msg_type = msg_type;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +0200118 trans->ctx = *ctx;
119
120 return trans;
121}
122
123static void nft_trans_destroy(struct nft_trans *trans)
124{
125 list_del(&trans->list);
126 kfree(trans);
127}
128
Patrick McHardy96518512013-10-14 11:00:02 +0200129/*
130 * Tables
131 */
132
133static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
134 const struct nlattr *nla)
135{
136 struct nft_table *table;
137
138 list_for_each_entry(table, &afi->tables, list) {
139 if (!nla_strcmp(nla, table->name))
140 return table;
141 }
142 return NULL;
143}
144
145static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200146 const struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +0200147{
148 struct nft_table *table;
149
150 if (nla == NULL)
151 return ERR_PTR(-EINVAL);
152
153 table = nft_table_lookup(afi, nla);
154 if (table != NULL)
155 return table;
156
Patrick McHardy96518512013-10-14 11:00:02 +0200157 return ERR_PTR(-ENOENT);
158}
159
160static inline u64 nf_tables_alloc_handle(struct nft_table *table)
161{
162 return ++table->hgenerator;
163}
164
Patrick McHardy2a37d752014-01-09 18:42:37 +0000165static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200166
Patrick McHardy2a37d752014-01-09 18:42:37 +0000167static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000168__nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200169{
170 int i;
171
Patrick McHardybaae3e62014-01-09 18:42:34 +0000172 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200173 if (chain_type[family][i] != NULL &&
174 !nla_strcmp(nla, chain_type[family][i]->name))
Patrick McHardybaae3e62014-01-09 18:42:34 +0000175 return chain_type[family][i];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200176 }
Patrick McHardybaae3e62014-01-09 18:42:34 +0000177 return NULL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200178}
179
Patrick McHardy2a37d752014-01-09 18:42:37 +0000180static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000181nf_tables_chain_type_lookup(const struct nft_af_info *afi,
182 const struct nlattr *nla,
183 bool autoload)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200184{
Patrick McHardy2a37d752014-01-09 18:42:37 +0000185 const struct nf_chain_type *type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200186
187 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000188 if (type != NULL)
189 return type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200190#ifdef CONFIG_MODULES
Patrick McHardy93b08062014-01-09 18:42:36 +0000191 if (autoload) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200192 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
193 request_module("nft-chain-%u-%*.s", afi->family,
194 nla_len(nla)-1, (const char *)nla_data(nla));
195 nfnl_lock(NFNL_SUBSYS_NFTABLES);
196 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000197 if (type != NULL)
198 return ERR_PTR(-EAGAIN);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200199 }
200#endif
Patrick McHardy93b08062014-01-09 18:42:36 +0000201 return ERR_PTR(-ENOENT);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200202}
203
Patrick McHardy96518512013-10-14 11:00:02 +0200204static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
205 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200206 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
Patrick McHardy96518512013-10-14 11:00:02 +0200207};
208
209static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
210 int event, u32 flags, int family,
211 const struct nft_table *table)
212{
213 struct nlmsghdr *nlh;
214 struct nfgenmsg *nfmsg;
215
216 event |= NFNL_SUBSYS_NFTABLES << 8;
217 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
218 if (nlh == NULL)
219 goto nla_put_failure;
220
221 nfmsg = nlmsg_data(nlh);
222 nfmsg->nfgen_family = family;
223 nfmsg->version = NFNETLINK_V0;
224 nfmsg->res_id = 0;
225
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200226 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
Tomasz Bursztykad8bcc7682013-12-12 15:00:42 +0200227 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
228 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
Patrick McHardy96518512013-10-14 11:00:02 +0200229 goto nla_put_failure;
230
231 return nlmsg_end(skb, nlh);
232
233nla_put_failure:
234 nlmsg_trim(skb, nlh);
235 return -1;
236}
237
238static int nf_tables_table_notify(const struct sk_buff *oskb,
239 const struct nlmsghdr *nlh,
240 const struct nft_table *table,
241 int event, int family)
242{
243 struct sk_buff *skb;
244 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
245 u32 seq = nlh ? nlh->nlmsg_seq : 0;
246 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
247 bool report;
248 int err;
249
250 report = nlh ? nlmsg_report(nlh) : false;
251 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
252 return 0;
253
254 err = -ENOBUFS;
255 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
256 if (skb == NULL)
257 goto err;
258
259 err = nf_tables_fill_table_info(skb, portid, seq, event, 0,
260 family, table);
261 if (err < 0) {
262 kfree_skb(skb);
263 goto err;
264 }
265
266 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
267 GFP_KERNEL);
268err:
269 if (err < 0)
270 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
271 return err;
272}
273
274static int nf_tables_dump_tables(struct sk_buff *skb,
275 struct netlink_callback *cb)
276{
277 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
278 const struct nft_af_info *afi;
279 const struct nft_table *table;
280 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200281 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200282 int family = nfmsg->nfgen_family;
283
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200284 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200285 if (family != NFPROTO_UNSPEC && family != afi->family)
286 continue;
287
288 list_for_each_entry(table, &afi->tables, list) {
289 if (idx < s_idx)
290 goto cont;
291 if (idx > s_idx)
292 memset(&cb->args[1], 0,
293 sizeof(cb->args) - sizeof(cb->args[0]));
294 if (nf_tables_fill_table_info(skb,
295 NETLINK_CB(cb->skb).portid,
296 cb->nlh->nlmsg_seq,
297 NFT_MSG_NEWTABLE,
298 NLM_F_MULTI,
299 afi->family, table) < 0)
300 goto done;
301cont:
302 idx++;
303 }
304 }
305done:
306 cb->args[0] = idx;
307 return skb->len;
308}
309
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200310/* Internal table flags */
311#define NFT_TABLE_INACTIVE (1 << 15)
312
Patrick McHardy96518512013-10-14 11:00:02 +0200313static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
314 const struct nlmsghdr *nlh,
315 const struct nlattr * const nla[])
316{
317 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
318 const struct nft_af_info *afi;
319 const struct nft_table *table;
320 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200321 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200322 int family = nfmsg->nfgen_family;
323 int err;
324
325 if (nlh->nlmsg_flags & NLM_F_DUMP) {
326 struct netlink_dump_control c = {
327 .dump = nf_tables_dump_tables,
328 };
329 return netlink_dump_start(nlsk, skb, nlh, &c);
330 }
331
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200332 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200333 if (IS_ERR(afi))
334 return PTR_ERR(afi);
335
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200336 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200337 if (IS_ERR(table))
338 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200339 if (table->flags & NFT_TABLE_INACTIVE)
340 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200341
342 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
343 if (!skb2)
344 return -ENOMEM;
345
346 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
347 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
348 family, table);
349 if (err < 0)
350 goto err;
351
352 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
353
354err:
355 kfree_skb(skb2);
356 return err;
357}
358
Patrick McHardy115a60b2014-01-03 12:16:15 +0000359static int nf_tables_table_enable(const struct nft_af_info *afi,
360 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200361{
362 struct nft_chain *chain;
363 int err, i = 0;
364
365 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100366 if (!(chain->flags & NFT_BASE_CHAIN))
367 continue;
368
Patrick McHardy115a60b2014-01-03 12:16:15 +0000369 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200370 if (err < 0)
371 goto err;
372
373 i++;
374 }
375 return 0;
376err:
377 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100378 if (!(chain->flags & NFT_BASE_CHAIN))
379 continue;
380
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200381 if (i-- <= 0)
382 break;
383
Patrick McHardy115a60b2014-01-03 12:16:15 +0000384 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200385 }
386 return err;
387}
388
Pablo Neira Ayusof75edf52014-03-30 14:04:52 +0200389static void nf_tables_table_disable(const struct nft_af_info *afi,
Patrick McHardy115a60b2014-01-03 12:16:15 +0000390 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200391{
392 struct nft_chain *chain;
393
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100394 list_for_each_entry(chain, &table->chains, list) {
395 if (chain->flags & NFT_BASE_CHAIN)
Patrick McHardy115a60b2014-01-03 12:16:15 +0000396 nf_unregister_hooks(nft_base_chain(chain)->ops,
397 afi->nops);
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100398 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200399}
400
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200401static int nf_tables_updtable(struct nft_ctx *ctx)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200402{
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200403 struct nft_trans *trans;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200404 u32 flags;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200405 int ret = 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200406
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200407 if (!ctx->nla[NFTA_TABLE_FLAGS])
408 return 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200409
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200410 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
411 if (flags & ~NFT_TABLE_F_DORMANT)
412 return -EINVAL;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200413
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200414 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
415 sizeof(struct nft_trans_table));
416 if (trans == NULL)
417 return -ENOMEM;
418
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200419 if ((flags & NFT_TABLE_F_DORMANT) &&
420 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200421 nft_trans_table_enable(trans) = false;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200422 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
423 ctx->table->flags & NFT_TABLE_F_DORMANT) {
424 ret = nf_tables_table_enable(ctx->afi, ctx->table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200425 if (ret >= 0) {
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200426 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200427 nft_trans_table_enable(trans) = true;
428 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200429 }
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200430 if (ret < 0)
431 goto err;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200432
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200433 nft_trans_table_update(trans) = true;
434 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
435 return 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200436err:
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200437 nft_trans_destroy(trans);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200438 return ret;
439}
440
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200441static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
442{
443 struct nft_trans *trans;
444
445 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
446 if (trans == NULL)
447 return -ENOMEM;
448
449 if (msg_type == NFT_MSG_NEWTABLE)
450 ctx->table->flags |= NFT_TABLE_INACTIVE;
451
452 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
453 return 0;
454}
455
Patrick McHardy96518512013-10-14 11:00:02 +0200456static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
457 const struct nlmsghdr *nlh,
458 const struct nlattr * const nla[])
459{
460 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
461 const struct nlattr *name;
462 struct nft_af_info *afi;
463 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200464 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200465 int family = nfmsg->nfgen_family;
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000466 u32 flags = 0;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200467 struct nft_ctx ctx;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200468 int err;
Patrick McHardy96518512013-10-14 11:00:02 +0200469
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200470 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200471 if (IS_ERR(afi))
472 return PTR_ERR(afi);
473
474 name = nla[NFTA_TABLE_NAME];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200475 table = nf_tables_table_lookup(afi, name);
Patrick McHardy96518512013-10-14 11:00:02 +0200476 if (IS_ERR(table)) {
477 if (PTR_ERR(table) != -ENOENT)
478 return PTR_ERR(table);
479 table = NULL;
480 }
481
482 if (table != NULL) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200483 if (table->flags & NFT_TABLE_INACTIVE)
484 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200485 if (nlh->nlmsg_flags & NLM_F_EXCL)
486 return -EEXIST;
487 if (nlh->nlmsg_flags & NLM_F_REPLACE)
488 return -EOPNOTSUPP;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200489
490 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
491 return nf_tables_updtable(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +0200492 }
493
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000494 if (nla[NFTA_TABLE_FLAGS]) {
495 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
496 if (flags & ~NFT_TABLE_F_DORMANT)
497 return -EINVAL;
498 }
499
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000500 if (!try_module_get(afi->owner))
501 return -EAFNOSUPPORT;
502
Patrick McHardy96518512013-10-14 11:00:02 +0200503 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000504 if (table == NULL) {
505 module_put(afi->owner);
Patrick McHardy96518512013-10-14 11:00:02 +0200506 return -ENOMEM;
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000507 }
Patrick McHardy96518512013-10-14 11:00:02 +0200508
509 nla_strlcpy(table->name, name, nla_len(name));
510 INIT_LIST_HEAD(&table->chains);
Patrick McHardy20a69342013-10-11 12:06:22 +0200511 INIT_LIST_HEAD(&table->sets);
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000512 table->flags = flags;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200513
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200514 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
515 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
516 if (err < 0) {
517 kfree(table);
518 module_put(afi->owner);
519 return err;
520 }
Patrick McHardy96518512013-10-14 11:00:02 +0200521 list_add_tail(&table->list, &afi->tables);
Patrick McHardy96518512013-10-14 11:00:02 +0200522 return 0;
523}
524
525static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
526 const struct nlmsghdr *nlh,
527 const struct nlattr * const nla[])
528{
529 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
530 struct nft_af_info *afi;
531 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200532 struct net *net = sock_net(skb->sk);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200533 int family = nfmsg->nfgen_family, err;
534 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +0200535
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200536 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200537 if (IS_ERR(afi))
538 return PTR_ERR(afi);
539
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200540 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200541 if (IS_ERR(table))
542 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200543 if (table->flags & NFT_TABLE_INACTIVE)
544 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200545
Patrick McHardy44a6f0d2014-01-09 18:42:41 +0000546 if (!list_empty(&table->chains) || !list_empty(&table->sets))
Patrick McHardy96518512013-10-14 11:00:02 +0200547 return -EBUSY;
548
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200549 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
550 err = nft_trans_table_add(&ctx, NFT_MSG_DELTABLE);
551 if (err < 0)
552 return err;
553
Patrick McHardy96518512013-10-14 11:00:02 +0200554 list_del(&table->list);
Patrick McHardy96518512013-10-14 11:00:02 +0200555 return 0;
556}
557
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200558static void nf_tables_table_destroy(struct nft_ctx *ctx)
559{
560 kfree(ctx->table);
561 module_put(ctx->afi->owner);
562}
563
Patrick McHardy2a37d752014-01-09 18:42:37 +0000564int nft_register_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200565{
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200566 int err = 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200567
568 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200569 if (chain_type[ctype->family][ctype->type] != NULL) {
570 err = -EBUSY;
571 goto out;
Patrick McHardy96518512013-10-14 11:00:02 +0200572 }
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200573 chain_type[ctype->family][ctype->type] = ctype;
574out:
Patrick McHardy96518512013-10-14 11:00:02 +0200575 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
576 return err;
577}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200578EXPORT_SYMBOL_GPL(nft_register_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200579
Patrick McHardy2a37d752014-01-09 18:42:37 +0000580void nft_unregister_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200581{
Patrick McHardy96518512013-10-14 11:00:02 +0200582 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200583 chain_type[ctype->family][ctype->type] = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200584 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
585}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200586EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200587
588/*
589 * Chains
590 */
591
592static struct nft_chain *
593nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
594{
595 struct nft_chain *chain;
596
597 list_for_each_entry(chain, &table->chains, list) {
598 if (chain->handle == handle)
599 return chain;
600 }
601
602 return ERR_PTR(-ENOENT);
603}
604
605static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
606 const struct nlattr *nla)
607{
608 struct nft_chain *chain;
609
610 if (nla == NULL)
611 return ERR_PTR(-EINVAL);
612
613 list_for_each_entry(chain, &table->chains, list) {
614 if (!nla_strcmp(nla, chain->name))
615 return chain;
616 }
617
618 return ERR_PTR(-ENOENT);
619}
620
621static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
622 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
623 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
624 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
625 .len = NFT_CHAIN_MAXNAMELEN - 1 },
626 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200627 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
Pablo Neira4c1f7812014-03-31 17:43:47 +0200628 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200629 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
Patrick McHardy96518512013-10-14 11:00:02 +0200630};
631
632static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
633 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
634 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
635};
636
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200637static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
638{
639 struct nft_stats *cpu_stats, total;
640 struct nlattr *nest;
641 int cpu;
642
643 memset(&total, 0, sizeof(total));
644 for_each_possible_cpu(cpu) {
645 cpu_stats = per_cpu_ptr(stats, cpu);
646 total.pkts += cpu_stats->pkts;
647 total.bytes += cpu_stats->bytes;
648 }
649 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
650 if (nest == NULL)
651 goto nla_put_failure;
652
653 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
654 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
655 goto nla_put_failure;
656
657 nla_nest_end(skb, nest);
658 return 0;
659
660nla_put_failure:
661 return -ENOSPC;
662}
663
Patrick McHardy96518512013-10-14 11:00:02 +0200664static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
665 int event, u32 flags, int family,
666 const struct nft_table *table,
667 const struct nft_chain *chain)
668{
669 struct nlmsghdr *nlh;
670 struct nfgenmsg *nfmsg;
671
672 event |= NFNL_SUBSYS_NFTABLES << 8;
673 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
674 if (nlh == NULL)
675 goto nla_put_failure;
676
677 nfmsg = nlmsg_data(nlh);
678 nfmsg->nfgen_family = family;
679 nfmsg->version = NFNETLINK_V0;
680 nfmsg->res_id = 0;
681
682 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
683 goto nla_put_failure;
684 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
685 goto nla_put_failure;
686 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
687 goto nla_put_failure;
688
689 if (chain->flags & NFT_BASE_CHAIN) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200690 const struct nft_base_chain *basechain = nft_base_chain(chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000691 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200692 struct nlattr *nest;
693
694 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
Patrick McHardy96518512013-10-14 11:00:02 +0200695 if (nest == NULL)
696 goto nla_put_failure;
697 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
698 goto nla_put_failure;
699 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
700 goto nla_put_failure;
701 nla_nest_end(skb, nest);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200702
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200703 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
704 htonl(basechain->policy)))
705 goto nla_put_failure;
706
Patrick McHardybaae3e62014-01-09 18:42:34 +0000707 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
708 goto nla_put_failure;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200709
710 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
711 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +0200712 }
713
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200714 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
715 goto nla_put_failure;
716
Patrick McHardy96518512013-10-14 11:00:02 +0200717 return nlmsg_end(skb, nlh);
718
719nla_put_failure:
720 nlmsg_trim(skb, nlh);
721 return -1;
722}
723
724static int nf_tables_chain_notify(const struct sk_buff *oskb,
725 const struct nlmsghdr *nlh,
726 const struct nft_table *table,
727 const struct nft_chain *chain,
728 int event, int family)
729{
730 struct sk_buff *skb;
731 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
732 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
733 u32 seq = nlh ? nlh->nlmsg_seq : 0;
734 bool report;
735 int err;
736
737 report = nlh ? nlmsg_report(nlh) : false;
738 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
739 return 0;
740
741 err = -ENOBUFS;
742 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
743 if (skb == NULL)
744 goto err;
745
746 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
747 table, chain);
748 if (err < 0) {
749 kfree_skb(skb);
750 goto err;
751 }
752
753 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
754 GFP_KERNEL);
755err:
756 if (err < 0)
757 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
758 return err;
759}
760
761static int nf_tables_dump_chains(struct sk_buff *skb,
762 struct netlink_callback *cb)
763{
764 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
765 const struct nft_af_info *afi;
766 const struct nft_table *table;
767 const struct nft_chain *chain;
768 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200769 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200770 int family = nfmsg->nfgen_family;
771
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200772 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200773 if (family != NFPROTO_UNSPEC && family != afi->family)
774 continue;
775
776 list_for_each_entry(table, &afi->tables, list) {
777 list_for_each_entry(chain, &table->chains, list) {
778 if (idx < s_idx)
779 goto cont;
780 if (idx > s_idx)
781 memset(&cb->args[1], 0,
782 sizeof(cb->args) - sizeof(cb->args[0]));
783 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
784 cb->nlh->nlmsg_seq,
785 NFT_MSG_NEWCHAIN,
786 NLM_F_MULTI,
787 afi->family, table, chain) < 0)
788 goto done;
789cont:
790 idx++;
791 }
792 }
793 }
794done:
795 cb->args[0] = idx;
796 return skb->len;
797}
798
799
800static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
801 const struct nlmsghdr *nlh,
802 const struct nlattr * const nla[])
803{
804 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
805 const struct nft_af_info *afi;
806 const struct nft_table *table;
807 const struct nft_chain *chain;
808 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200809 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200810 int family = nfmsg->nfgen_family;
811 int err;
812
813 if (nlh->nlmsg_flags & NLM_F_DUMP) {
814 struct netlink_dump_control c = {
815 .dump = nf_tables_dump_chains,
816 };
817 return netlink_dump_start(nlsk, skb, nlh, &c);
818 }
819
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200820 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200821 if (IS_ERR(afi))
822 return PTR_ERR(afi);
823
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200824 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200825 if (IS_ERR(table))
826 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200827 if (table->flags & NFT_TABLE_INACTIVE)
828 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200829
830 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
831 if (IS_ERR(chain))
832 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +0200833 if (chain->flags & NFT_CHAIN_INACTIVE)
834 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200835
836 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
837 if (!skb2)
838 return -ENOMEM;
839
840 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
841 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
842 family, table, chain);
843 if (err < 0)
844 goto err;
845
846 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
847
848err:
849 kfree_skb(skb2);
850 return err;
851}
852
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200853static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
854 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
855 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
856};
857
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200858static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200859{
860 struct nlattr *tb[NFTA_COUNTER_MAX+1];
861 struct nft_stats __percpu *newstats;
862 struct nft_stats *stats;
863 int err;
864
865 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
866 if (err < 0)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200867 return ERR_PTR(err);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200868
869 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200870 return ERR_PTR(-EINVAL);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200871
872 newstats = alloc_percpu(struct nft_stats);
873 if (newstats == NULL)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200874 return ERR_PTR(-ENOMEM);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200875
876 /* Restore old counters on this cpu, no problem. Per-cpu statistics
877 * are not exposed to userspace.
878 */
879 stats = this_cpu_ptr(newstats);
880 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
881 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
882
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200883 return newstats;
884}
885
886static void nft_chain_stats_replace(struct nft_base_chain *chain,
887 struct nft_stats __percpu *newstats)
888{
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200889 if (chain->stats) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200890 struct nft_stats __percpu *oldstats =
Patrick McHardy67a8fc22014-02-18 18:06:49 +0000891 nft_dereference(chain->stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200892
893 rcu_assign_pointer(chain->stats, newstats);
894 synchronize_rcu();
895 free_percpu(oldstats);
896 } else
897 rcu_assign_pointer(chain->stats, newstats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200898}
899
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +0200900static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
901{
902 struct nft_trans *trans;
903
904 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
905 if (trans == NULL)
906 return -ENOMEM;
907
908 if (msg_type == NFT_MSG_NEWCHAIN)
909 ctx->chain->flags |= NFT_CHAIN_INACTIVE;
910
911 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
912 return 0;
913}
914
915static void nf_tables_chain_destroy(struct nft_chain *chain)
916{
917 BUG_ON(chain->use > 0);
918
919 if (chain->flags & NFT_BASE_CHAIN) {
920 module_put(nft_base_chain(chain)->type->owner);
921 free_percpu(nft_base_chain(chain)->stats);
922 kfree(nft_base_chain(chain));
923 } else {
924 kfree(chain);
925 }
926}
927
Patrick McHardy96518512013-10-14 11:00:02 +0200928static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
929 const struct nlmsghdr *nlh,
930 const struct nlattr * const nla[])
931{
932 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
933 const struct nlattr * uninitialized_var(name);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +0200934 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +0200935 struct nft_table *table;
936 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200937 struct nft_base_chain *basechain = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200938 struct nlattr *ha[NFTA_HOOK_MAX + 1];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200939 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200940 int family = nfmsg->nfgen_family;
Patrick McHardy57de2a02014-01-09 18:42:31 +0000941 u8 policy = NF_ACCEPT;
Patrick McHardy96518512013-10-14 11:00:02 +0200942 u64 handle = 0;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000943 unsigned int i;
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200944 struct nft_stats __percpu *stats;
Patrick McHardy96518512013-10-14 11:00:02 +0200945 int err;
946 bool create;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +0200947 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +0200948
949 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
950
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200951 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200952 if (IS_ERR(afi))
953 return PTR_ERR(afi);
954
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200955 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200956 if (IS_ERR(table))
957 return PTR_ERR(table);
958
Patrick McHardy96518512013-10-14 11:00:02 +0200959 chain = NULL;
960 name = nla[NFTA_CHAIN_NAME];
961
962 if (nla[NFTA_CHAIN_HANDLE]) {
963 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
964 chain = nf_tables_chain_lookup_byhandle(table, handle);
965 if (IS_ERR(chain))
966 return PTR_ERR(chain);
967 } else {
968 chain = nf_tables_chain_lookup(table, name);
969 if (IS_ERR(chain)) {
970 if (PTR_ERR(chain) != -ENOENT)
971 return PTR_ERR(chain);
972 chain = NULL;
973 }
974 }
975
Patrick McHardy57de2a02014-01-09 18:42:31 +0000976 if (nla[NFTA_CHAIN_POLICY]) {
977 if ((chain != NULL &&
978 !(chain->flags & NFT_BASE_CHAIN)) ||
979 nla[NFTA_CHAIN_HOOK] == NULL)
980 return -EOPNOTSUPP;
981
Pablo Neira Ayuso8f46df12014-01-10 15:11:25 +0100982 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
Patrick McHardy57de2a02014-01-09 18:42:31 +0000983 switch (policy) {
984 case NF_DROP:
985 case NF_ACCEPT:
986 break;
987 default:
988 return -EINVAL;
989 }
990 }
991
Patrick McHardy96518512013-10-14 11:00:02 +0200992 if (chain != NULL) {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +0200993 struct nft_stats *stats = NULL;
994 struct nft_trans *trans;
995
996 if (chain->flags & NFT_CHAIN_INACTIVE)
997 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200998 if (nlh->nlmsg_flags & NLM_F_EXCL)
999 return -EEXIST;
1000 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1001 return -EOPNOTSUPP;
1002
1003 if (nla[NFTA_CHAIN_HANDLE] && name &&
1004 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1005 return -EEXIST;
1006
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001007 if (nla[NFTA_CHAIN_COUNTERS]) {
1008 if (!(chain->flags & NFT_BASE_CHAIN))
1009 return -EOPNOTSUPP;
1010
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001011 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1012 if (IS_ERR(stats))
1013 return PTR_ERR(stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001014 }
1015
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001016 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1017 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1018 sizeof(struct nft_trans_chain));
1019 if (trans == NULL)
1020 return -ENOMEM;
1021
1022 nft_trans_chain_stats(trans) = stats;
1023 nft_trans_chain_update(trans) = true;
1024
Patrick McHardy4401a862014-01-09 18:42:32 +00001025 if (nla[NFTA_CHAIN_POLICY])
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001026 nft_trans_chain_policy(trans) = policy;
1027 else
1028 nft_trans_chain_policy(trans) = -1;
Patrick McHardy4401a862014-01-09 18:42:32 +00001029
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001030 if (nla[NFTA_CHAIN_HANDLE] && name) {
1031 nla_strlcpy(nft_trans_chain_name(trans), name,
1032 NFT_CHAIN_MAXNAMELEN);
1033 }
1034 list_add_tail(&trans->list, &net->nft.commit_list);
1035 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001036 }
1037
Patrick McHardy75820672014-01-09 18:42:33 +00001038 if (table->use == UINT_MAX)
1039 return -EOVERFLOW;
1040
Patrick McHardy96518512013-10-14 11:00:02 +02001041 if (nla[NFTA_CHAIN_HOOK]) {
Patrick McHardy2a37d752014-01-09 18:42:37 +00001042 const struct nf_chain_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001043 struct nf_hook_ops *ops;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001044 nf_hookfn *hookfn;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001045 u32 hooknum, priority;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001046
Patrick McHardybaae3e62014-01-09 18:42:34 +00001047 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001048 if (nla[NFTA_CHAIN_TYPE]) {
1049 type = nf_tables_chain_type_lookup(afi,
1050 nla[NFTA_CHAIN_TYPE],
1051 create);
Patrick McHardy93b08062014-01-09 18:42:36 +00001052 if (IS_ERR(type))
1053 return PTR_ERR(type);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001054 }
Patrick McHardy96518512013-10-14 11:00:02 +02001055
1056 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1057 nft_hook_policy);
1058 if (err < 0)
1059 return err;
1060 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1061 ha[NFTA_HOOK_PRIORITY] == NULL)
1062 return -EINVAL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001063
1064 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1065 if (hooknum >= afi->nhooks)
Patrick McHardy96518512013-10-14 11:00:02 +02001066 return -EINVAL;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001067 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
Patrick McHardy96518512013-10-14 11:00:02 +02001068
Patrick McHardybaae3e62014-01-09 18:42:34 +00001069 if (!(type->hook_mask & (1 << hooknum)))
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001070 return -EOPNOTSUPP;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001071 if (!try_module_get(type->owner))
Patrick McHardybaae3e62014-01-09 18:42:34 +00001072 return -ENOENT;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001073 hookfn = type->hooks[hooknum];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001074
Patrick McHardy96518512013-10-14 11:00:02 +02001075 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1076 if (basechain == NULL)
1077 return -ENOMEM;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001078
Patrick McHardy4401a862014-01-09 18:42:32 +00001079 if (nla[NFTA_CHAIN_COUNTERS]) {
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001080 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1081 if (IS_ERR(stats)) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001082 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +00001083 kfree(basechain);
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001084 return PTR_ERR(stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001085 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001086 basechain->stats = stats;
Patrick McHardy4401a862014-01-09 18:42:32 +00001087 } else {
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001088 stats = alloc_percpu(struct nft_stats);
1089 if (IS_ERR(stats)) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001090 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +00001091 kfree(basechain);
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001092 return PTR_ERR(stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001093 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001094 rcu_assign_pointer(basechain->stats, stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001095 }
1096
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001097 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +02001098 chain = &basechain->chain;
1099
Patrick McHardy115a60b2014-01-03 12:16:15 +00001100 for (i = 0; i < afi->nops; i++) {
1101 ops = &basechain->ops[i];
1102 ops->pf = family;
1103 ops->owner = afi->owner;
1104 ops->hooknum = hooknum;
1105 ops->priority = priority;
1106 ops->priv = chain;
1107 ops->hook = afi->hooks[ops->hooknum];
1108 if (hookfn)
1109 ops->hook = hookfn;
1110 if (afi->hook_ops_init)
1111 afi->hook_ops_init(ops, i);
1112 }
Patrick McHardy96518512013-10-14 11:00:02 +02001113
1114 chain->flags |= NFT_BASE_CHAIN;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001115 basechain->policy = policy;
Patrick McHardy96518512013-10-14 11:00:02 +02001116 } else {
1117 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1118 if (chain == NULL)
1119 return -ENOMEM;
1120 }
1121
1122 INIT_LIST_HEAD(&chain->rules);
1123 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001124 chain->net = net;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +02001125 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +02001126 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1127
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001128 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1129 chain->flags & NFT_BASE_CHAIN) {
Patrick McHardy115a60b2014-01-03 12:16:15 +00001130 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001131 if (err < 0)
1132 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001133 }
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001134
1135 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1136 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1137 if (err < 0)
1138 goto err2;
1139
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001140 list_add_tail(&chain->list, &table->chains);
Patrick McHardy96518512013-10-14 11:00:02 +02001141 return 0;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001142err2:
1143 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1144 chain->flags & NFT_BASE_CHAIN) {
1145 nf_unregister_hooks(nft_base_chain(chain)->ops,
1146 afi->nops);
1147 }
1148err1:
1149 nf_tables_chain_destroy(chain);
1150 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001151}
1152
1153static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1154 const struct nlmsghdr *nlh,
1155 const struct nlattr * const nla[])
1156{
1157 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001158 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001159 struct nft_table *table;
1160 struct nft_chain *chain;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001161 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001162 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001163 struct nft_ctx ctx;
1164 int err;
Patrick McHardy96518512013-10-14 11:00:02 +02001165
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001166 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001167 if (IS_ERR(afi))
1168 return PTR_ERR(afi);
1169
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001170 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001171 if (IS_ERR(table))
1172 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001173 if (table->flags & NFT_TABLE_INACTIVE)
1174 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001175
1176 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1177 if (IS_ERR(chain))
1178 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001179 if (chain->flags & NFT_CHAIN_INACTIVE)
1180 return -ENOENT;
Patrick McHardy3dd72792014-01-25 08:04:07 +00001181 if (!list_empty(&chain->rules) || chain->use > 0)
Patrick McHardy96518512013-10-14 11:00:02 +02001182 return -EBUSY;
1183
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001184 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1185 err = nft_trans_chain_add(&ctx, NFT_MSG_DELCHAIN);
1186 if (err < 0)
1187 return err;
1188
Patrick McHardy96518512013-10-14 11:00:02 +02001189 list_del(&chain->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001190 return 0;
1191}
1192
Patrick McHardy96518512013-10-14 11:00:02 +02001193/*
1194 * Expressions
1195 */
1196
1197/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001198 * nft_register_expr - register nf_tables expr type
1199 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001200 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001201 * Registers the expr type for use with nf_tables. Returns zero on
Patrick McHardy96518512013-10-14 11:00:02 +02001202 * success or a negative errno code otherwise.
1203 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001204int nft_register_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001205{
1206 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001207 if (type->family == NFPROTO_UNSPEC)
1208 list_add_tail(&type->list, &nf_tables_expressions);
1209 else
1210 list_add(&type->list, &nf_tables_expressions);
Patrick McHardy96518512013-10-14 11:00:02 +02001211 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1212 return 0;
1213}
1214EXPORT_SYMBOL_GPL(nft_register_expr);
1215
1216/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001217 * nft_unregister_expr - unregister nf_tables expr type
1218 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001219 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001220 * Unregisters the expr typefor use with nf_tables.
Patrick McHardy96518512013-10-14 11:00:02 +02001221 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001222void nft_unregister_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001223{
1224 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001225 list_del(&type->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001226 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1227}
1228EXPORT_SYMBOL_GPL(nft_unregister_expr);
1229
Patrick McHardy64d46802014-02-05 15:03:37 +00001230static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1231 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001232{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001233 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001234
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001235 list_for_each_entry(type, &nf_tables_expressions, list) {
Patrick McHardy64d46802014-02-05 15:03:37 +00001236 if (!nla_strcmp(nla, type->name) &&
1237 (!type->family || type->family == family))
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001238 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001239 }
1240 return NULL;
1241}
1242
Patrick McHardy64d46802014-02-05 15:03:37 +00001243static const struct nft_expr_type *nft_expr_type_get(u8 family,
1244 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001245{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001246 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001247
1248 if (nla == NULL)
1249 return ERR_PTR(-EINVAL);
1250
Patrick McHardy64d46802014-02-05 15:03:37 +00001251 type = __nft_expr_type_get(family, nla);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001252 if (type != NULL && try_module_get(type->owner))
1253 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001254
1255#ifdef CONFIG_MODULES
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001256 if (type == NULL) {
Patrick McHardy96518512013-10-14 11:00:02 +02001257 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001258 request_module("nft-expr-%u-%.*s", family,
1259 nla_len(nla), (char *)nla_data(nla));
1260 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1261 if (__nft_expr_type_get(family, nla))
1262 return ERR_PTR(-EAGAIN);
1263
1264 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy96518512013-10-14 11:00:02 +02001265 request_module("nft-expr-%.*s",
1266 nla_len(nla), (char *)nla_data(nla));
1267 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001268 if (__nft_expr_type_get(family, nla))
Patrick McHardy96518512013-10-14 11:00:02 +02001269 return ERR_PTR(-EAGAIN);
1270 }
1271#endif
1272 return ERR_PTR(-ENOENT);
1273}
1274
1275static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1276 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1277 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1278};
1279
1280static int nf_tables_fill_expr_info(struct sk_buff *skb,
1281 const struct nft_expr *expr)
1282{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001283 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
Patrick McHardy96518512013-10-14 11:00:02 +02001284 goto nla_put_failure;
1285
1286 if (expr->ops->dump) {
1287 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1288 if (data == NULL)
1289 goto nla_put_failure;
1290 if (expr->ops->dump(skb, expr) < 0)
1291 goto nla_put_failure;
1292 nla_nest_end(skb, data);
1293 }
1294
1295 return skb->len;
1296
1297nla_put_failure:
1298 return -1;
1299};
1300
1301struct nft_expr_info {
1302 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001303 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001304};
1305
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001306static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1307 const struct nlattr *nla,
Patrick McHardy96518512013-10-14 11:00:02 +02001308 struct nft_expr_info *info)
1309{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001310 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001311 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001312 struct nlattr *tb[NFTA_EXPR_MAX + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001313 int err;
1314
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001315 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
Patrick McHardy96518512013-10-14 11:00:02 +02001316 if (err < 0)
1317 return err;
1318
Patrick McHardy64d46802014-02-05 15:03:37 +00001319 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001320 if (IS_ERR(type))
1321 return PTR_ERR(type);
1322
1323 if (tb[NFTA_EXPR_DATA]) {
1324 err = nla_parse_nested(info->tb, type->maxattr,
1325 tb[NFTA_EXPR_DATA], type->policy);
1326 if (err < 0)
1327 goto err1;
1328 } else
1329 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1330
1331 if (type->select_ops != NULL) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001332 ops = type->select_ops(ctx,
1333 (const struct nlattr * const *)info->tb);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001334 if (IS_ERR(ops)) {
1335 err = PTR_ERR(ops);
1336 goto err1;
1337 }
1338 } else
1339 ops = type->ops;
1340
Patrick McHardy96518512013-10-14 11:00:02 +02001341 info->ops = ops;
1342 return 0;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001343
1344err1:
1345 module_put(type->owner);
1346 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001347}
1348
1349static int nf_tables_newexpr(const struct nft_ctx *ctx,
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001350 const struct nft_expr_info *info,
Patrick McHardy96518512013-10-14 11:00:02 +02001351 struct nft_expr *expr)
1352{
1353 const struct nft_expr_ops *ops = info->ops;
1354 int err;
1355
1356 expr->ops = ops;
1357 if (ops->init) {
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001358 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
Patrick McHardy96518512013-10-14 11:00:02 +02001359 if (err < 0)
1360 goto err1;
1361 }
1362
Patrick McHardy96518512013-10-14 11:00:02 +02001363 return 0;
1364
1365err1:
1366 expr->ops = NULL;
1367 return err;
1368}
1369
Patrick McHardy62472bc2014-03-07 19:08:30 +01001370static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1371 struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +02001372{
1373 if (expr->ops->destroy)
Patrick McHardy62472bc2014-03-07 19:08:30 +01001374 expr->ops->destroy(ctx, expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001375 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001376}
1377
1378/*
1379 * Rules
1380 */
1381
1382static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1383 u64 handle)
1384{
1385 struct nft_rule *rule;
1386
1387 // FIXME: this sucks
1388 list_for_each_entry(rule, &chain->rules, list) {
1389 if (handle == rule->handle)
1390 return rule;
1391 }
1392
1393 return ERR_PTR(-ENOENT);
1394}
1395
1396static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1397 const struct nlattr *nla)
1398{
1399 if (nla == NULL)
1400 return ERR_PTR(-EINVAL);
1401
1402 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1403}
1404
1405static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1406 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1407 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1408 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1409 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1410 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001411 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001412 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001413 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1414 .len = NFT_USERDATA_MAXLEN },
Patrick McHardy96518512013-10-14 11:00:02 +02001415};
1416
1417static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1418 int event, u32 flags, int family,
1419 const struct nft_table *table,
1420 const struct nft_chain *chain,
1421 const struct nft_rule *rule)
1422{
1423 struct nlmsghdr *nlh;
1424 struct nfgenmsg *nfmsg;
1425 const struct nft_expr *expr, *next;
1426 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001427 const struct nft_rule *prule;
1428 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001429
Eric Leblond5e948462013-10-10 13:41:44 +02001430 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001431 flags);
1432 if (nlh == NULL)
1433 goto nla_put_failure;
1434
1435 nfmsg = nlmsg_data(nlh);
1436 nfmsg->nfgen_family = family;
1437 nfmsg->version = NFNETLINK_V0;
1438 nfmsg->res_id = 0;
1439
1440 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1441 goto nla_put_failure;
1442 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1443 goto nla_put_failure;
1444 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1445 goto nla_put_failure;
1446
Eric Leblond5e948462013-10-10 13:41:44 +02001447 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1448 prule = list_entry(rule->list.prev, struct nft_rule, list);
1449 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1450 cpu_to_be64(prule->handle)))
1451 goto nla_put_failure;
1452 }
1453
Patrick McHardy96518512013-10-14 11:00:02 +02001454 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1455 if (list == NULL)
1456 goto nla_put_failure;
1457 nft_rule_for_each_expr(expr, next, rule) {
1458 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1459 if (elem == NULL)
1460 goto nla_put_failure;
1461 if (nf_tables_fill_expr_info(skb, expr) < 0)
1462 goto nla_put_failure;
1463 nla_nest_end(skb, elem);
1464 }
1465 nla_nest_end(skb, list);
1466
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001467 if (rule->ulen &&
1468 nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1469 goto nla_put_failure;
1470
Patrick McHardy96518512013-10-14 11:00:02 +02001471 return nlmsg_end(skb, nlh);
1472
1473nla_put_failure:
1474 nlmsg_trim(skb, nlh);
1475 return -1;
1476}
1477
1478static int nf_tables_rule_notify(const struct sk_buff *oskb,
1479 const struct nlmsghdr *nlh,
1480 const struct nft_table *table,
1481 const struct nft_chain *chain,
1482 const struct nft_rule *rule,
1483 int event, u32 flags, int family)
1484{
1485 struct sk_buff *skb;
1486 u32 portid = NETLINK_CB(oskb).portid;
1487 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1488 u32 seq = nlh->nlmsg_seq;
1489 bool report;
1490 int err;
1491
1492 report = nlmsg_report(nlh);
1493 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1494 return 0;
1495
1496 err = -ENOBUFS;
1497 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1498 if (skb == NULL)
1499 goto err;
1500
1501 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1502 family, table, chain, rule);
1503 if (err < 0) {
1504 kfree_skb(skb);
1505 goto err;
1506 }
1507
1508 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1509 GFP_KERNEL);
1510err:
1511 if (err < 0)
1512 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1513 return err;
1514}
1515
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001516static inline bool
1517nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1518{
1519 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1520}
1521
1522static inline int gencursor_next(struct net *net)
1523{
1524 return net->nft.gencursor+1 == 1 ? 1 : 0;
1525}
1526
1527static inline int
1528nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1529{
1530 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1531}
1532
1533static inline void
1534nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1535{
1536 /* Now inactive, will be active in the future */
1537 rule->genmask = (1 << net->nft.gencursor);
1538}
1539
1540static inline void
1541nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1542{
1543 rule->genmask = (1 << gencursor_next(net));
1544}
1545
1546static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1547{
1548 rule->genmask = 0;
1549}
1550
Patrick McHardy96518512013-10-14 11:00:02 +02001551static int nf_tables_dump_rules(struct sk_buff *skb,
1552 struct netlink_callback *cb)
1553{
1554 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1555 const struct nft_af_info *afi;
1556 const struct nft_table *table;
1557 const struct nft_chain *chain;
1558 const struct nft_rule *rule;
1559 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001560 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001561 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001562 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1563 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
Patrick McHardy96518512013-10-14 11:00:02 +02001564
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001565 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001566 if (family != NFPROTO_UNSPEC && family != afi->family)
1567 continue;
1568
1569 list_for_each_entry(table, &afi->tables, list) {
1570 list_for_each_entry(chain, &table->chains, list) {
1571 list_for_each_entry(rule, &chain->rules, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001572 if (!nft_rule_is_active(net, rule))
1573 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001574 if (idx < s_idx)
1575 goto cont;
1576 if (idx > s_idx)
1577 memset(&cb->args[1], 0,
1578 sizeof(cb->args) - sizeof(cb->args[0]));
1579 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1580 cb->nlh->nlmsg_seq,
1581 NFT_MSG_NEWRULE,
1582 NLM_F_MULTI | NLM_F_APPEND,
1583 afi->family, table, chain, rule) < 0)
1584 goto done;
1585cont:
1586 idx++;
1587 }
1588 }
1589 }
1590 }
1591done:
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001592 /* Invalidate this dump, a transition to the new generation happened */
1593 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1594 return -EBUSY;
1595
Patrick McHardy96518512013-10-14 11:00:02 +02001596 cb->args[0] = idx;
1597 return skb->len;
1598}
1599
1600static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1601 const struct nlmsghdr *nlh,
1602 const struct nlattr * const nla[])
1603{
1604 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1605 const struct nft_af_info *afi;
1606 const struct nft_table *table;
1607 const struct nft_chain *chain;
1608 const struct nft_rule *rule;
1609 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001610 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001611 int family = nfmsg->nfgen_family;
1612 int err;
1613
1614 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1615 struct netlink_dump_control c = {
1616 .dump = nf_tables_dump_rules,
1617 };
1618 return netlink_dump_start(nlsk, skb, nlh, &c);
1619 }
1620
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001621 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001622 if (IS_ERR(afi))
1623 return PTR_ERR(afi);
1624
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001625 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001626 if (IS_ERR(table))
1627 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001628 if (table->flags & NFT_TABLE_INACTIVE)
1629 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001630
1631 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1632 if (IS_ERR(chain))
1633 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001634 if (chain->flags & NFT_CHAIN_INACTIVE)
1635 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001636
1637 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1638 if (IS_ERR(rule))
1639 return PTR_ERR(rule);
1640
1641 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1642 if (!skb2)
1643 return -ENOMEM;
1644
1645 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1646 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1647 family, table, chain, rule);
1648 if (err < 0)
1649 goto err;
1650
1651 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1652
1653err:
1654 kfree_skb(skb2);
1655 return err;
1656}
1657
Patrick McHardy62472bc2014-03-07 19:08:30 +01001658static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1659 struct nft_rule *rule)
Patrick McHardy96518512013-10-14 11:00:02 +02001660{
Patrick McHardy96518512013-10-14 11:00:02 +02001661 struct nft_expr *expr;
1662
1663 /*
1664 * Careful: some expressions might not be initialized in case this
1665 * is called on error from nf_tables_newrule().
1666 */
1667 expr = nft_expr_first(rule);
1668 while (expr->ops && expr != nft_expr_last(rule)) {
Patrick McHardy62472bc2014-03-07 19:08:30 +01001669 nf_tables_expr_destroy(ctx, expr);
Patrick McHardy96518512013-10-14 11:00:02 +02001670 expr = nft_expr_next(expr);
1671 }
1672 kfree(rule);
1673}
1674
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001675static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001676 struct nft_rule *rule)
1677{
1678 struct nft_trans *trans;
1679
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001680 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001681 if (trans == NULL)
1682 return NULL;
1683
1684 nft_trans_rule(trans) = rule;
1685 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
1686
1687 return trans;
1688}
1689
Patrick McHardy96518512013-10-14 11:00:02 +02001690#define NFT_RULE_MAXEXPRS 128
1691
1692static struct nft_expr_info *info;
1693
1694static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1695 const struct nlmsghdr *nlh,
1696 const struct nlattr * const nla[])
1697{
1698 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001699 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001700 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001701 struct nft_table *table;
1702 struct nft_chain *chain;
1703 struct nft_rule *rule, *old_rule = NULL;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001704 struct nft_trans *trans = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001705 struct nft_expr *expr;
1706 struct nft_ctx ctx;
1707 struct nlattr *tmp;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001708 unsigned int size, i, n, ulen = 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001709 int err, rem;
1710 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02001711 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02001712
1713 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1714
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001715 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02001716 if (IS_ERR(afi))
1717 return PTR_ERR(afi);
1718
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001719 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001720 if (IS_ERR(table))
1721 return PTR_ERR(table);
1722
1723 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1724 if (IS_ERR(chain))
1725 return PTR_ERR(chain);
1726
1727 if (nla[NFTA_RULE_HANDLE]) {
1728 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1729 rule = __nf_tables_rule_lookup(chain, handle);
1730 if (IS_ERR(rule))
1731 return PTR_ERR(rule);
1732
1733 if (nlh->nlmsg_flags & NLM_F_EXCL)
1734 return -EEXIST;
1735 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1736 old_rule = rule;
1737 else
1738 return -EOPNOTSUPP;
1739 } else {
1740 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1741 return -EINVAL;
1742 handle = nf_tables_alloc_handle(table);
1743 }
1744
Eric Leblond5e948462013-10-10 13:41:44 +02001745 if (nla[NFTA_RULE_POSITION]) {
1746 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1747 return -EOPNOTSUPP;
1748
1749 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1750 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1751 if (IS_ERR(old_rule))
1752 return PTR_ERR(old_rule);
1753 }
1754
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001755 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1756
Patrick McHardy96518512013-10-14 11:00:02 +02001757 n = 0;
1758 size = 0;
1759 if (nla[NFTA_RULE_EXPRESSIONS]) {
1760 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1761 err = -EINVAL;
1762 if (nla_type(tmp) != NFTA_LIST_ELEM)
1763 goto err1;
1764 if (n == NFT_RULE_MAXEXPRS)
1765 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001766 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02001767 if (err < 0)
1768 goto err1;
1769 size += info[n].ops->size;
1770 n++;
1771 }
1772 }
1773
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001774 if (nla[NFTA_RULE_USERDATA])
1775 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1776
Patrick McHardy96518512013-10-14 11:00:02 +02001777 err = -ENOMEM;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001778 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001779 if (rule == NULL)
1780 goto err1;
1781
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001782 nft_rule_activate_next(net, rule);
1783
Patrick McHardy96518512013-10-14 11:00:02 +02001784 rule->handle = handle;
1785 rule->dlen = size;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001786 rule->ulen = ulen;
1787
1788 if (ulen)
1789 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
Patrick McHardy96518512013-10-14 11:00:02 +02001790
Patrick McHardy96518512013-10-14 11:00:02 +02001791 expr = nft_expr_first(rule);
1792 for (i = 0; i < n; i++) {
1793 err = nf_tables_newexpr(&ctx, &info[i], expr);
1794 if (err < 0)
1795 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001796 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001797 expr = nft_expr_next(expr);
1798 }
1799
Patrick McHardy96518512013-10-14 11:00:02 +02001800 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001801 if (nft_rule_is_active_next(net, old_rule)) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001802 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE,
1803 old_rule);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001804 if (trans == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001805 err = -ENOMEM;
1806 goto err2;
1807 }
1808 nft_rule_disactivate_next(net, old_rule);
1809 list_add_tail(&rule->list, &old_rule->list);
1810 } else {
1811 err = -ENOENT;
1812 goto err2;
1813 }
Patrick McHardy96518512013-10-14 11:00:02 +02001814 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02001815 if (old_rule)
1816 list_add_rcu(&rule->list, &old_rule->list);
1817 else
1818 list_add_tail_rcu(&rule->list, &chain->rules);
1819 else {
1820 if (old_rule)
1821 list_add_tail_rcu(&rule->list, &old_rule->list);
1822 else
1823 list_add_rcu(&rule->list, &chain->rules);
1824 }
Patrick McHardy96518512013-10-14 11:00:02 +02001825
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001826 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001827 err = -ENOMEM;
1828 goto err3;
1829 }
Patrick McHardy96518512013-10-14 11:00:02 +02001830 return 0;
1831
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001832err3:
1833 list_del_rcu(&rule->list);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001834 if (trans) {
1835 list_del_rcu(&nft_trans_rule(trans)->list);
1836 nft_rule_clear(net, nft_trans_rule(trans));
1837 nft_trans_destroy(trans);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001838 }
Patrick McHardy96518512013-10-14 11:00:02 +02001839err2:
Patrick McHardy62472bc2014-03-07 19:08:30 +01001840 nf_tables_rule_destroy(&ctx, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001841err1:
1842 for (i = 0; i < n; i++) {
1843 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001844 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001845 }
1846 return err;
1847}
1848
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001849static int
1850nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1851{
1852 /* You cannot delete the same rule twice */
1853 if (nft_rule_is_active_next(ctx->net, rule)) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001854 if (nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule) == NULL)
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001855 return -ENOMEM;
1856 nft_rule_disactivate_next(ctx->net, rule);
1857 return 0;
1858 }
1859 return -ENOENT;
1860}
1861
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001862static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1863{
1864 struct nft_rule *rule;
1865 int err;
1866
1867 list_for_each_entry(rule, &ctx->chain->rules, list) {
1868 err = nf_tables_delrule_one(ctx, rule);
1869 if (err < 0)
1870 return err;
1871 }
1872 return 0;
1873}
1874
Patrick McHardy96518512013-10-14 11:00:02 +02001875static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1876 const struct nlmsghdr *nlh,
1877 const struct nlattr * const nla[])
1878{
1879 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001880 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001881 struct net *net = sock_net(skb->sk);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001882 struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001883 struct nft_chain *chain = NULL;
1884 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001885 int family = nfmsg->nfgen_family, err = 0;
1886 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001887
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001888 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001889 if (IS_ERR(afi))
1890 return PTR_ERR(afi);
1891
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001892 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001893 if (IS_ERR(table))
1894 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001895 if (table->flags & NFT_TABLE_INACTIVE)
1896 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001897
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001898 if (nla[NFTA_RULE_CHAIN]) {
1899 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1900 if (IS_ERR(chain))
1901 return PTR_ERR(chain);
1902 }
Patrick McHardy96518512013-10-14 11:00:02 +02001903
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001904 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1905
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001906 if (chain) {
1907 if (nla[NFTA_RULE_HANDLE]) {
1908 rule = nf_tables_rule_lookup(chain,
1909 nla[NFTA_RULE_HANDLE]);
1910 if (IS_ERR(rule))
1911 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001912
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001913 err = nf_tables_delrule_one(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001914 } else {
1915 err = nf_table_delrule_by_chain(&ctx);
1916 }
1917 } else {
1918 list_for_each_entry(chain, &table->chains, list) {
1919 ctx.chain = chain;
1920 err = nf_table_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001921 if (err < 0)
1922 break;
Patrick McHardy96518512013-10-14 11:00:02 +02001923 }
1924 }
1925
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001926 return err;
1927}
1928
Patrick McHardy20a69342013-10-11 12:06:22 +02001929/*
1930 * Sets
1931 */
1932
1933static LIST_HEAD(nf_tables_set_ops);
1934
1935int nft_register_set(struct nft_set_ops *ops)
1936{
1937 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1938 list_add_tail(&ops->list, &nf_tables_set_ops);
1939 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1940 return 0;
1941}
1942EXPORT_SYMBOL_GPL(nft_register_set);
1943
1944void nft_unregister_set(struct nft_set_ops *ops)
1945{
1946 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1947 list_del(&ops->list);
1948 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1949}
1950EXPORT_SYMBOL_GPL(nft_unregister_set);
1951
Patrick McHardyc50b9602014-03-28 10:19:47 +00001952/*
1953 * Select a set implementation based on the data characteristics and the
1954 * given policy. The total memory use might not be known if no size is
1955 * given, in that case the amount of memory per element is used.
1956 */
1957static const struct nft_set_ops *
1958nft_select_set_ops(const struct nlattr * const nla[],
1959 const struct nft_set_desc *desc,
1960 enum nft_set_policies policy)
Patrick McHardy20a69342013-10-11 12:06:22 +02001961{
Patrick McHardyc50b9602014-03-28 10:19:47 +00001962 const struct nft_set_ops *ops, *bops;
1963 struct nft_set_estimate est, best;
Patrick McHardy20a69342013-10-11 12:06:22 +02001964 u32 features;
1965
1966#ifdef CONFIG_MODULES
1967 if (list_empty(&nf_tables_set_ops)) {
1968 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1969 request_module("nft-set");
1970 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1971 if (!list_empty(&nf_tables_set_ops))
1972 return ERR_PTR(-EAGAIN);
1973 }
1974#endif
1975 features = 0;
1976 if (nla[NFTA_SET_FLAGS] != NULL) {
1977 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1978 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1979 }
1980
Patrick McHardyc50b9602014-03-28 10:19:47 +00001981 bops = NULL;
1982 best.size = ~0;
1983 best.class = ~0;
1984
Patrick McHardy20a69342013-10-11 12:06:22 +02001985 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1986 if ((ops->features & features) != features)
1987 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00001988 if (!ops->estimate(desc, features, &est))
1989 continue;
1990
1991 switch (policy) {
1992 case NFT_SET_POL_PERFORMANCE:
1993 if (est.class < best.class)
1994 break;
1995 if (est.class == best.class && est.size < best.size)
1996 break;
1997 continue;
1998 case NFT_SET_POL_MEMORY:
1999 if (est.size < best.size)
2000 break;
2001 if (est.size == best.size && est.class < best.class)
2002 break;
2003 continue;
2004 default:
2005 break;
2006 }
2007
Patrick McHardy20a69342013-10-11 12:06:22 +02002008 if (!try_module_get(ops->owner))
2009 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002010 if (bops != NULL)
2011 module_put(bops->owner);
2012
2013 bops = ops;
2014 best = est;
Patrick McHardy20a69342013-10-11 12:06:22 +02002015 }
2016
Patrick McHardyc50b9602014-03-28 10:19:47 +00002017 if (bops != NULL)
2018 return bops;
2019
Patrick McHardy20a69342013-10-11 12:06:22 +02002020 return ERR_PTR(-EOPNOTSUPP);
2021}
2022
2023static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2024 [NFTA_SET_TABLE] = { .type = NLA_STRING },
2025 [NFTA_SET_NAME] = { .type = NLA_STRING },
2026 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2027 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2028 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2029 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2030 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002031 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2032 [NFTA_SET_DESC] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002033 [NFTA_SET_ID] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002034};
2035
2036static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2037 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002038};
2039
2040static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2041 const struct sk_buff *skb,
2042 const struct nlmsghdr *nlh,
2043 const struct nlattr * const nla[])
2044{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002045 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002046 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002047 struct nft_af_info *afi = NULL;
2048 struct nft_table *table = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02002049
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002050 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2051 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2052 if (IS_ERR(afi))
2053 return PTR_ERR(afi);
2054 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002055
2056 if (nla[NFTA_SET_TABLE] != NULL) {
Patrick McHardyec2c9932014-02-05 15:03:35 +00002057 if (afi == NULL)
2058 return -EAFNOSUPPORT;
2059
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002060 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002061 if (IS_ERR(table))
2062 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002063 if (table->flags & NFT_TABLE_INACTIVE)
2064 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002065 }
2066
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002067 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002068 return 0;
2069}
2070
2071struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2072 const struct nlattr *nla)
2073{
2074 struct nft_set *set;
2075
2076 if (nla == NULL)
2077 return ERR_PTR(-EINVAL);
2078
2079 list_for_each_entry(set, &table->sets, list) {
2080 if (!nla_strcmp(nla, set->name))
2081 return set;
2082 }
2083 return ERR_PTR(-ENOENT);
2084}
2085
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002086struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2087 const struct nlattr *nla)
2088{
2089 struct nft_trans *trans;
2090 u32 id = ntohl(nla_get_be32(nla));
2091
2092 list_for_each_entry(trans, &net->nft.commit_list, list) {
2093 if (trans->msg_type == NFT_MSG_NEWSET &&
2094 id == nft_trans_set_id(trans))
2095 return nft_trans_set(trans);
2096 }
2097 return ERR_PTR(-ENOENT);
2098}
2099
Patrick McHardy20a69342013-10-11 12:06:22 +02002100static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2101 const char *name)
2102{
2103 const struct nft_set *i;
2104 const char *p;
2105 unsigned long *inuse;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002106 unsigned int n = 0, min = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002107
2108 p = strnchr(name, IFNAMSIZ, '%');
2109 if (p != NULL) {
2110 if (p[1] != 'd' || strchr(p + 2, '%'))
2111 return -EINVAL;
2112
2113 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2114 if (inuse == NULL)
2115 return -ENOMEM;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002116cont:
Patrick McHardy20a69342013-10-11 12:06:22 +02002117 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01002118 int tmp;
2119
2120 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02002121 continue;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002122 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02002123 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002124
Patrick McHardy60eb1892014-03-07 12:34:05 +01002125 set_bit(tmp - min, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02002126 }
2127
Patrick McHardy53b70282014-02-05 12:26:22 +01002128 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
Patrick McHardy60eb1892014-03-07 12:34:05 +01002129 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2130 min += BITS_PER_BYTE * PAGE_SIZE;
2131 memset(inuse, 0, PAGE_SIZE);
2132 goto cont;
2133 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002134 free_page((unsigned long)inuse);
2135 }
2136
Patrick McHardy60eb1892014-03-07 12:34:05 +01002137 snprintf(set->name, sizeof(set->name), name, min + n);
Patrick McHardy20a69342013-10-11 12:06:22 +02002138 list_for_each_entry(i, &ctx->table->sets, list) {
2139 if (!strcmp(set->name, i->name))
2140 return -ENFILE;
2141 }
2142 return 0;
2143}
2144
2145static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2146 const struct nft_set *set, u16 event, u16 flags)
2147{
2148 struct nfgenmsg *nfmsg;
2149 struct nlmsghdr *nlh;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002150 struct nlattr *desc;
Patrick McHardy20a69342013-10-11 12:06:22 +02002151 u32 portid = NETLINK_CB(ctx->skb).portid;
2152 u32 seq = ctx->nlh->nlmsg_seq;
2153
2154 event |= NFNL_SUBSYS_NFTABLES << 8;
2155 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2156 flags);
2157 if (nlh == NULL)
2158 goto nla_put_failure;
2159
2160 nfmsg = nlmsg_data(nlh);
2161 nfmsg->nfgen_family = ctx->afi->family;
2162 nfmsg->version = NFNETLINK_V0;
2163 nfmsg->res_id = 0;
2164
2165 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2166 goto nla_put_failure;
2167 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2168 goto nla_put_failure;
2169 if (set->flags != 0)
2170 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2171 goto nla_put_failure;
2172
2173 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2174 goto nla_put_failure;
2175 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2176 goto nla_put_failure;
2177 if (set->flags & NFT_SET_MAP) {
2178 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2179 goto nla_put_failure;
2180 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2181 goto nla_put_failure;
2182 }
2183
Patrick McHardyc50b9602014-03-28 10:19:47 +00002184 desc = nla_nest_start(skb, NFTA_SET_DESC);
2185 if (desc == NULL)
2186 goto nla_put_failure;
2187 if (set->size &&
2188 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2189 goto nla_put_failure;
2190 nla_nest_end(skb, desc);
2191
Patrick McHardy20a69342013-10-11 12:06:22 +02002192 return nlmsg_end(skb, nlh);
2193
2194nla_put_failure:
2195 nlmsg_trim(skb, nlh);
2196 return -1;
2197}
2198
2199static int nf_tables_set_notify(const struct nft_ctx *ctx,
2200 const struct nft_set *set,
2201 int event)
2202{
2203 struct sk_buff *skb;
2204 u32 portid = NETLINK_CB(ctx->skb).portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002205 bool report;
2206 int err;
2207
2208 report = nlmsg_report(ctx->nlh);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002209 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002210 return 0;
2211
2212 err = -ENOBUFS;
2213 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2214 if (skb == NULL)
2215 goto err;
2216
2217 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2218 if (err < 0) {
2219 kfree_skb(skb);
2220 goto err;
2221 }
2222
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002223 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
Patrick McHardy20a69342013-10-11 12:06:22 +02002224 GFP_KERNEL);
2225err:
2226 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002227 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002228 return err;
2229}
2230
2231static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2232 struct netlink_callback *cb)
2233{
2234 const struct nft_set *set;
2235 unsigned int idx = 0, s_idx = cb->args[0];
2236
2237 if (cb->args[1])
2238 return skb->len;
2239
2240 list_for_each_entry(set, &ctx->table->sets, list) {
2241 if (idx < s_idx)
2242 goto cont;
2243 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2244 NLM_F_MULTI) < 0) {
2245 cb->args[0] = idx;
2246 goto done;
2247 }
2248cont:
2249 idx++;
2250 }
2251 cb->args[1] = 1;
2252done:
2253 return skb->len;
2254}
2255
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002256static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2257 struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002258{
2259 const struct nft_set *set;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002260 unsigned int idx, s_idx = cb->args[0];
Patrick McHardy20a69342013-10-11 12:06:22 +02002261 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2262
2263 if (cb->args[1])
2264 return skb->len;
2265
2266 list_for_each_entry(table, &ctx->afi->tables, list) {
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002267 if (cur_table) {
2268 if (cur_table != table)
2269 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02002270
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002271 cur_table = NULL;
2272 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002273 ctx->table = table;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002274 idx = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002275 list_for_each_entry(set, &ctx->table->sets, list) {
2276 if (idx < s_idx)
2277 goto cont;
2278 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2279 NLM_F_MULTI) < 0) {
2280 cb->args[0] = idx;
2281 cb->args[2] = (unsigned long) table;
2282 goto done;
2283 }
2284cont:
2285 idx++;
2286 }
2287 }
2288 cb->args[1] = 1;
2289done:
2290 return skb->len;
2291}
2292
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002293static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2294 struct netlink_callback *cb)
2295{
2296 const struct nft_set *set;
2297 unsigned int idx, s_idx = cb->args[0];
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002298 struct nft_af_info *afi;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002299 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2300 struct net *net = sock_net(skb->sk);
2301 int cur_family = cb->args[3];
2302
2303 if (cb->args[1])
2304 return skb->len;
2305
2306 list_for_each_entry(afi, &net->nft.af_info, list) {
2307 if (cur_family) {
2308 if (afi->family != cur_family)
2309 continue;
2310
2311 cur_family = 0;
2312 }
2313
2314 list_for_each_entry(table, &afi->tables, list) {
2315 if (cur_table) {
2316 if (cur_table != table)
2317 continue;
2318
2319 cur_table = NULL;
2320 }
2321
2322 ctx->table = table;
2323 ctx->afi = afi;
2324 idx = 0;
2325 list_for_each_entry(set, &ctx->table->sets, list) {
2326 if (idx < s_idx)
2327 goto cont;
2328 if (nf_tables_fill_set(skb, ctx, set,
2329 NFT_MSG_NEWSET,
2330 NLM_F_MULTI) < 0) {
2331 cb->args[0] = idx;
2332 cb->args[2] = (unsigned long) table;
2333 cb->args[3] = afi->family;
2334 goto done;
2335 }
2336cont:
2337 idx++;
2338 }
2339 if (s_idx)
2340 s_idx = 0;
2341 }
2342 }
2343 cb->args[1] = 1;
2344done:
2345 return skb->len;
2346}
2347
Patrick McHardy20a69342013-10-11 12:06:22 +02002348static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2349{
2350 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2351 struct nlattr *nla[NFTA_SET_MAX + 1];
2352 struct nft_ctx ctx;
2353 int err, ret;
2354
2355 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2356 nft_set_policy);
2357 if (err < 0)
2358 return err;
2359
2360 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2361 if (err < 0)
2362 return err;
2363
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002364 if (ctx.table == NULL) {
2365 if (ctx.afi == NULL)
2366 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2367 else
2368 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2369 } else
Patrick McHardy20a69342013-10-11 12:06:22 +02002370 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2371
2372 return ret;
2373}
2374
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002375#define NFT_SET_INACTIVE (1 << 15) /* Internal set flag */
2376
Patrick McHardy20a69342013-10-11 12:06:22 +02002377static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2378 const struct nlmsghdr *nlh,
2379 const struct nlattr * const nla[])
2380{
2381 const struct nft_set *set;
2382 struct nft_ctx ctx;
2383 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002384 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002385 int err;
2386
2387 /* Verify existance before starting dump */
2388 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2389 if (err < 0)
2390 return err;
2391
2392 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2393 struct netlink_dump_control c = {
2394 .dump = nf_tables_dump_sets,
2395 };
2396 return netlink_dump_start(nlsk, skb, nlh, &c);
2397 }
2398
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002399 /* Only accept unspec with dump */
2400 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2401 return -EAFNOSUPPORT;
2402
Patrick McHardy20a69342013-10-11 12:06:22 +02002403 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2404 if (IS_ERR(set))
2405 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002406 if (set->flags & NFT_SET_INACTIVE)
2407 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002408
2409 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2410 if (skb2 == NULL)
2411 return -ENOMEM;
2412
2413 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2414 if (err < 0)
2415 goto err;
2416
2417 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2418
2419err:
2420 kfree_skb(skb2);
2421 return err;
2422}
2423
Patrick McHardyc50b9602014-03-28 10:19:47 +00002424static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2425 struct nft_set_desc *desc,
2426 const struct nlattr *nla)
2427{
2428 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2429 int err;
2430
2431 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2432 if (err < 0)
2433 return err;
2434
2435 if (da[NFTA_SET_DESC_SIZE] != NULL)
2436 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2437
2438 return 0;
2439}
2440
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002441static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
2442 struct nft_set *set)
2443{
2444 struct nft_trans *trans;
2445
2446 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
2447 if (trans == NULL)
2448 return -ENOMEM;
2449
2450 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
2451 nft_trans_set_id(trans) =
2452 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
2453 set->flags |= NFT_SET_INACTIVE;
2454 }
2455 nft_trans_set(trans) = set;
2456 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
2457
2458 return 0;
2459}
2460
Patrick McHardy20a69342013-10-11 12:06:22 +02002461static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2462 const struct nlmsghdr *nlh,
2463 const struct nlattr * const nla[])
2464{
2465 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2466 const struct nft_set_ops *ops;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002467 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002468 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002469 struct nft_table *table;
2470 struct nft_set *set;
2471 struct nft_ctx ctx;
2472 char name[IFNAMSIZ];
2473 unsigned int size;
2474 bool create;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002475 u32 ktype, dtype, flags, policy;
2476 struct nft_set_desc desc;
Patrick McHardy20a69342013-10-11 12:06:22 +02002477 int err;
2478
2479 if (nla[NFTA_SET_TABLE] == NULL ||
2480 nla[NFTA_SET_NAME] == NULL ||
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002481 nla[NFTA_SET_KEY_LEN] == NULL ||
2482 nla[NFTA_SET_ID] == NULL)
Patrick McHardy20a69342013-10-11 12:06:22 +02002483 return -EINVAL;
2484
Patrick McHardyc50b9602014-03-28 10:19:47 +00002485 memset(&desc, 0, sizeof(desc));
2486
Patrick McHardy20a69342013-10-11 12:06:22 +02002487 ktype = NFT_DATA_VALUE;
2488 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2489 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2490 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2491 return -EINVAL;
2492 }
2493
Patrick McHardyc50b9602014-03-28 10:19:47 +00002494 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2495 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002496 return -EINVAL;
2497
2498 flags = 0;
2499 if (nla[NFTA_SET_FLAGS] != NULL) {
2500 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2501 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2502 NFT_SET_INTERVAL | NFT_SET_MAP))
2503 return -EINVAL;
2504 }
2505
2506 dtype = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002507 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2508 if (!(flags & NFT_SET_MAP))
2509 return -EINVAL;
2510
2511 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2512 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2513 dtype != NFT_DATA_VERDICT)
2514 return -EINVAL;
2515
2516 if (dtype != NFT_DATA_VERDICT) {
2517 if (nla[NFTA_SET_DATA_LEN] == NULL)
2518 return -EINVAL;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002519 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2520 if (desc.dlen == 0 ||
2521 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002522 return -EINVAL;
2523 } else
Patrick McHardyc50b9602014-03-28 10:19:47 +00002524 desc.dlen = sizeof(struct nft_data);
Patrick McHardy20a69342013-10-11 12:06:22 +02002525 } else if (flags & NFT_SET_MAP)
2526 return -EINVAL;
2527
Patrick McHardyc50b9602014-03-28 10:19:47 +00002528 policy = NFT_SET_POL_PERFORMANCE;
2529 if (nla[NFTA_SET_POLICY] != NULL)
2530 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2531
2532 if (nla[NFTA_SET_DESC] != NULL) {
2533 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2534 if (err < 0)
2535 return err;
2536 }
2537
Patrick McHardy20a69342013-10-11 12:06:22 +02002538 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2539
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002540 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002541 if (IS_ERR(afi))
2542 return PTR_ERR(afi);
2543
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002544 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002545 if (IS_ERR(table))
2546 return PTR_ERR(table);
2547
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002548 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002549
2550 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2551 if (IS_ERR(set)) {
2552 if (PTR_ERR(set) != -ENOENT)
2553 return PTR_ERR(set);
2554 set = NULL;
2555 }
2556
2557 if (set != NULL) {
2558 if (nlh->nlmsg_flags & NLM_F_EXCL)
2559 return -EEXIST;
2560 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2561 return -EOPNOTSUPP;
2562 return 0;
2563 }
2564
2565 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2566 return -ENOENT;
2567
Patrick McHardyc50b9602014-03-28 10:19:47 +00002568 ops = nft_select_set_ops(nla, &desc, policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002569 if (IS_ERR(ops))
2570 return PTR_ERR(ops);
2571
2572 size = 0;
2573 if (ops->privsize != NULL)
2574 size = ops->privsize(nla);
2575
2576 err = -ENOMEM;
2577 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2578 if (set == NULL)
2579 goto err1;
2580
2581 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2582 err = nf_tables_set_alloc_name(&ctx, set, name);
2583 if (err < 0)
2584 goto err2;
2585
2586 INIT_LIST_HEAD(&set->bindings);
2587 set->ops = ops;
2588 set->ktype = ktype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002589 set->klen = desc.klen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002590 set->dtype = dtype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002591 set->dlen = desc.dlen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002592 set->flags = flags;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002593 set->size = desc.size;
Patrick McHardy20a69342013-10-11 12:06:22 +02002594
Patrick McHardyc50b9602014-03-28 10:19:47 +00002595 err = ops->init(set, &desc, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002596 if (err < 0)
2597 goto err2;
2598
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002599 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
2600 if (err < 0)
2601 goto err2;
2602
Patrick McHardy20a69342013-10-11 12:06:22 +02002603 list_add_tail(&set->list, &table->sets);
Patrick McHardy20a69342013-10-11 12:06:22 +02002604 return 0;
2605
2606err2:
2607 kfree(set);
2608err1:
2609 module_put(ops->owner);
2610 return err;
2611}
2612
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002613static void nft_set_destroy(struct nft_set *set)
2614{
2615 set->ops->destroy(set);
2616 module_put(set->ops->owner);
2617 kfree(set);
2618}
2619
Patrick McHardy20a69342013-10-11 12:06:22 +02002620static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2621{
2622 list_del(&set->list);
Patrick McHardyab9da5c12014-03-07 19:08:31 +01002623 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002624 nft_set_destroy(set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002625}
2626
2627static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2628 const struct nlmsghdr *nlh,
2629 const struct nlattr * const nla[])
2630{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002631 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002632 struct nft_set *set;
2633 struct nft_ctx ctx;
2634 int err;
2635
Patrick McHardyec2c9932014-02-05 15:03:35 +00002636 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2637 return -EAFNOSUPPORT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002638 if (nla[NFTA_SET_TABLE] == NULL)
2639 return -EINVAL;
2640
2641 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2642 if (err < 0)
2643 return err;
2644
2645 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2646 if (IS_ERR(set))
2647 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002648 if (set->flags & NFT_SET_INACTIVE)
2649 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002650 if (!list_empty(&set->bindings))
2651 return -EBUSY;
2652
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002653 err = nft_trans_set_add(&ctx, NFT_MSG_DELSET, set);
2654 if (err < 0)
2655 return err;
2656
2657 list_del(&set->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002658 return 0;
2659}
2660
2661static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2662 const struct nft_set *set,
2663 const struct nft_set_iter *iter,
2664 const struct nft_set_elem *elem)
2665{
2666 enum nft_registers dreg;
2667
2668 dreg = nft_type_to_reg(set->dtype);
Pablo Neira Ayuso2ee0d3c2013-12-28 00:59:38 +01002669 return nft_validate_data_load(ctx, dreg, &elem->data,
2670 set->dtype == NFT_DATA_VERDICT ?
2671 NFT_DATA_VERDICT : NFT_DATA_VALUE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002672}
2673
2674int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2675 struct nft_set_binding *binding)
2676{
2677 struct nft_set_binding *i;
2678 struct nft_set_iter iter;
2679
2680 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2681 return -EBUSY;
2682
2683 if (set->flags & NFT_SET_MAP) {
2684 /* If the set is already bound to the same chain all
2685 * jumps are already validated for that chain.
2686 */
2687 list_for_each_entry(i, &set->bindings, list) {
2688 if (i->chain == binding->chain)
2689 goto bind;
2690 }
2691
2692 iter.skip = 0;
2693 iter.count = 0;
2694 iter.err = 0;
2695 iter.fn = nf_tables_bind_check_setelem;
2696
2697 set->ops->walk(ctx, set, &iter);
2698 if (iter.err < 0) {
2699 /* Destroy anonymous sets if binding fails */
2700 if (set->flags & NFT_SET_ANONYMOUS)
2701 nf_tables_set_destroy(ctx, set);
2702
2703 return iter.err;
2704 }
2705 }
2706bind:
2707 binding->chain = ctx->chain;
2708 list_add_tail(&binding->list, &set->bindings);
2709 return 0;
2710}
2711
2712void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2713 struct nft_set_binding *binding)
2714{
2715 list_del(&binding->list);
2716
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002717 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2718 !(set->flags & NFT_SET_INACTIVE))
Patrick McHardy20a69342013-10-11 12:06:22 +02002719 nf_tables_set_destroy(ctx, set);
2720}
2721
2722/*
2723 * Set elements
2724 */
2725
2726static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2727 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2728 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2729 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2730};
2731
2732static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2733 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2734 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2735 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002736 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002737};
2738
2739static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2740 const struct sk_buff *skb,
2741 const struct nlmsghdr *nlh,
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002742 const struct nlattr * const nla[],
2743 bool trans)
Patrick McHardy20a69342013-10-11 12:06:22 +02002744{
2745 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002746 struct nft_af_info *afi;
2747 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002748 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002749
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002750 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002751 if (IS_ERR(afi))
2752 return PTR_ERR(afi);
2753
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002754 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002755 if (IS_ERR(table))
2756 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002757 if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2758 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002759
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002760 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002761 return 0;
2762}
2763
2764static int nf_tables_fill_setelem(struct sk_buff *skb,
2765 const struct nft_set *set,
2766 const struct nft_set_elem *elem)
2767{
2768 unsigned char *b = skb_tail_pointer(skb);
2769 struct nlattr *nest;
2770
2771 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2772 if (nest == NULL)
2773 goto nla_put_failure;
2774
2775 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2776 set->klen) < 0)
2777 goto nla_put_failure;
2778
2779 if (set->flags & NFT_SET_MAP &&
2780 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2781 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2782 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2783 set->dlen) < 0)
2784 goto nla_put_failure;
2785
2786 if (elem->flags != 0)
2787 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2788 goto nla_put_failure;
2789
2790 nla_nest_end(skb, nest);
2791 return 0;
2792
2793nla_put_failure:
2794 nlmsg_trim(skb, b);
2795 return -EMSGSIZE;
2796}
2797
2798struct nft_set_dump_args {
2799 const struct netlink_callback *cb;
2800 struct nft_set_iter iter;
2801 struct sk_buff *skb;
2802};
2803
2804static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2805 const struct nft_set *set,
2806 const struct nft_set_iter *iter,
2807 const struct nft_set_elem *elem)
2808{
2809 struct nft_set_dump_args *args;
2810
2811 args = container_of(iter, struct nft_set_dump_args, iter);
2812 return nf_tables_fill_setelem(args->skb, set, elem);
2813}
2814
2815static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2816{
2817 const struct nft_set *set;
2818 struct nft_set_dump_args args;
2819 struct nft_ctx ctx;
2820 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2821 struct nfgenmsg *nfmsg;
2822 struct nlmsghdr *nlh;
2823 struct nlattr *nest;
2824 u32 portid, seq;
2825 int event, err;
2826
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01002827 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2828 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002829 if (err < 0)
2830 return err;
2831
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002832 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2833 false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002834 if (err < 0)
2835 return err;
2836
2837 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2838 if (IS_ERR(set))
2839 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002840 if (set->flags & NFT_SET_INACTIVE)
2841 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002842
2843 event = NFT_MSG_NEWSETELEM;
2844 event |= NFNL_SUBSYS_NFTABLES << 8;
2845 portid = NETLINK_CB(cb->skb).portid;
2846 seq = cb->nlh->nlmsg_seq;
2847
2848 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2849 NLM_F_MULTI);
2850 if (nlh == NULL)
2851 goto nla_put_failure;
2852
2853 nfmsg = nlmsg_data(nlh);
2854 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2855 nfmsg->version = NFNETLINK_V0;
2856 nfmsg->res_id = 0;
2857
2858 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2859 goto nla_put_failure;
2860 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2861 goto nla_put_failure;
2862
2863 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2864 if (nest == NULL)
2865 goto nla_put_failure;
2866
2867 args.cb = cb;
2868 args.skb = skb;
2869 args.iter.skip = cb->args[0];
2870 args.iter.count = 0;
2871 args.iter.err = 0;
2872 args.iter.fn = nf_tables_dump_setelem;
2873 set->ops->walk(&ctx, set, &args.iter);
2874
2875 nla_nest_end(skb, nest);
2876 nlmsg_end(skb, nlh);
2877
2878 if (args.iter.err && args.iter.err != -EMSGSIZE)
2879 return args.iter.err;
2880 if (args.iter.count == cb->args[0])
2881 return 0;
2882
2883 cb->args[0] = args.iter.count;
2884 return skb->len;
2885
2886nla_put_failure:
2887 return -ENOSPC;
2888}
2889
2890static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2891 const struct nlmsghdr *nlh,
2892 const struct nlattr * const nla[])
2893{
2894 const struct nft_set *set;
2895 struct nft_ctx ctx;
2896 int err;
2897
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002898 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002899 if (err < 0)
2900 return err;
2901
2902 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2903 if (IS_ERR(set))
2904 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002905 if (set->flags & NFT_SET_INACTIVE)
2906 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002907
2908 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2909 struct netlink_dump_control c = {
2910 .dump = nf_tables_dump_set,
2911 };
2912 return netlink_dump_start(nlsk, skb, nlh, &c);
2913 }
2914 return -EOPNOTSUPP;
2915}
2916
Arturo Borrerod60ce622014-04-01 14:06:07 +02002917static int nf_tables_fill_setelem_info(struct sk_buff *skb,
2918 const struct nft_ctx *ctx, u32 seq,
2919 u32 portid, int event, u16 flags,
2920 const struct nft_set *set,
2921 const struct nft_set_elem *elem)
2922{
2923 struct nfgenmsg *nfmsg;
2924 struct nlmsghdr *nlh;
2925 struct nlattr *nest;
2926 int err;
2927
2928 event |= NFNL_SUBSYS_NFTABLES << 8;
2929 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2930 flags);
2931 if (nlh == NULL)
2932 goto nla_put_failure;
2933
2934 nfmsg = nlmsg_data(nlh);
2935 nfmsg->nfgen_family = ctx->afi->family;
2936 nfmsg->version = NFNETLINK_V0;
2937 nfmsg->res_id = 0;
2938
2939 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2940 goto nla_put_failure;
2941 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2942 goto nla_put_failure;
2943
2944 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2945 if (nest == NULL)
2946 goto nla_put_failure;
2947
2948 err = nf_tables_fill_setelem(skb, set, elem);
2949 if (err < 0)
2950 goto nla_put_failure;
2951
2952 nla_nest_end(skb, nest);
2953
2954 return nlmsg_end(skb, nlh);
2955
2956nla_put_failure:
2957 nlmsg_trim(skb, nlh);
2958 return -1;
2959}
2960
2961static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
2962 const struct nft_set *set,
2963 const struct nft_set_elem *elem,
2964 int event, u16 flags)
2965{
2966 const struct sk_buff *oskb = ctx->skb;
2967 struct net *net = sock_net(oskb->sk);
2968 u32 portid = NETLINK_CB(oskb).portid;
2969 bool report = nlmsg_report(ctx->nlh);
2970 struct sk_buff *skb;
2971 int err;
2972
2973 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
2974 return 0;
2975
2976 err = -ENOBUFS;
2977 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2978 if (skb == NULL)
2979 goto err;
2980
2981 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
2982 set, elem);
2983 if (err < 0) {
2984 kfree_skb(skb);
2985 goto err;
2986 }
2987
2988 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
2989 GFP_KERNEL);
2990err:
2991 if (err < 0)
2992 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
2993 return err;
2994}
2995
Patrick McHardy20a69342013-10-11 12:06:22 +02002996static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2997 const struct nlattr *attr)
2998{
2999 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3000 struct nft_data_desc d1, d2;
3001 struct nft_set_elem elem;
3002 struct nft_set_binding *binding;
3003 enum nft_registers dreg;
3004 int err;
3005
Patrick McHardyc50b9602014-03-28 10:19:47 +00003006 if (set->size && set->nelems == set->size)
3007 return -ENFILE;
3008
Patrick McHardy20a69342013-10-11 12:06:22 +02003009 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3010 nft_set_elem_policy);
3011 if (err < 0)
3012 return err;
3013
3014 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3015 return -EINVAL;
3016
3017 elem.flags = 0;
3018 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3019 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3020 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
3021 return -EINVAL;
3022 }
3023
3024 if (set->flags & NFT_SET_MAP) {
3025 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3026 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
3027 return -EINVAL;
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01003028 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3029 elem.flags & NFT_SET_ELEM_INTERVAL_END)
3030 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003031 } else {
3032 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3033 return -EINVAL;
3034 }
3035
3036 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3037 if (err < 0)
3038 goto err1;
3039 err = -EINVAL;
3040 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3041 goto err2;
3042
3043 err = -EEXIST;
3044 if (set->ops->get(set, &elem) == 0)
3045 goto err2;
3046
3047 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3048 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
3049 if (err < 0)
3050 goto err2;
3051
3052 err = -EINVAL;
3053 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3054 goto err3;
3055
3056 dreg = nft_type_to_reg(set->dtype);
3057 list_for_each_entry(binding, &set->bindings, list) {
3058 struct nft_ctx bind_ctx = {
3059 .afi = ctx->afi,
3060 .table = ctx->table,
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02003061 .chain = (struct nft_chain *)binding->chain,
Patrick McHardy20a69342013-10-11 12:06:22 +02003062 };
3063
3064 err = nft_validate_data_load(&bind_ctx, dreg,
3065 &elem.data, d2.type);
3066 if (err < 0)
3067 goto err3;
3068 }
3069 }
3070
3071 err = set->ops->insert(set, &elem);
3072 if (err < 0)
3073 goto err3;
Patrick McHardyc50b9602014-03-28 10:19:47 +00003074 set->nelems++;
Patrick McHardy20a69342013-10-11 12:06:22 +02003075
Arturo Borrerod60ce622014-04-01 14:06:07 +02003076 nf_tables_setelem_notify(ctx, set, &elem, NFT_MSG_NEWSETELEM, 0);
Patrick McHardy20a69342013-10-11 12:06:22 +02003077 return 0;
3078
3079err3:
3080 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3081 nft_data_uninit(&elem.data, d2.type);
3082err2:
3083 nft_data_uninit(&elem.key, d1.type);
3084err1:
3085 return err;
3086}
3087
3088static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3089 const struct nlmsghdr *nlh,
3090 const struct nlattr * const nla[])
3091{
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003092 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02003093 const struct nlattr *attr;
3094 struct nft_set *set;
3095 struct nft_ctx ctx;
3096 int rem, err;
3097
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003098 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
Patrick McHardy20a69342013-10-11 12:06:22 +02003099 if (err < 0)
3100 return err;
3101
3102 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003103 if (IS_ERR(set)) {
3104 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3105 set = nf_tables_set_lookup_byid(net,
3106 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3107 }
3108 if (IS_ERR(set))
3109 return PTR_ERR(set);
3110 }
3111
Patrick McHardy20a69342013-10-11 12:06:22 +02003112 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3113 return -EBUSY;
3114
3115 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3116 err = nft_add_set_elem(&ctx, set, attr);
3117 if (err < 0)
3118 return err;
3119 }
3120 return 0;
3121}
3122
3123static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
3124 const struct nlattr *attr)
3125{
3126 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3127 struct nft_data_desc desc;
3128 struct nft_set_elem elem;
3129 int err;
3130
3131 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3132 nft_set_elem_policy);
3133 if (err < 0)
3134 goto err1;
3135
3136 err = -EINVAL;
3137 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3138 goto err1;
3139
3140 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3141 if (err < 0)
3142 goto err1;
3143
3144 err = -EINVAL;
3145 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3146 goto err2;
3147
3148 err = set->ops->get(set, &elem);
3149 if (err < 0)
3150 goto err2;
3151
3152 set->ops->remove(set, &elem);
Patrick McHardyc50b9602014-03-28 10:19:47 +00003153 set->nelems--;
Patrick McHardy20a69342013-10-11 12:06:22 +02003154
Arturo Borrerod60ce622014-04-01 14:06:07 +02003155 nf_tables_setelem_notify(ctx, set, &elem, NFT_MSG_DELSETELEM, 0);
3156
Patrick McHardy20a69342013-10-11 12:06:22 +02003157 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
3158 if (set->flags & NFT_SET_MAP)
3159 nft_data_uninit(&elem.data, set->dtype);
3160
3161err2:
3162 nft_data_uninit(&elem.key, desc.type);
3163err1:
3164 return err;
3165}
3166
3167static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3168 const struct nlmsghdr *nlh,
3169 const struct nlattr * const nla[])
3170{
3171 const struct nlattr *attr;
3172 struct nft_set *set;
3173 struct nft_ctx ctx;
3174 int rem, err;
3175
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003176 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02003177 if (err < 0)
3178 return err;
3179
3180 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3181 if (IS_ERR(set))
3182 return PTR_ERR(set);
3183 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3184 return -EBUSY;
3185
3186 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3187 err = nft_del_setelem(&ctx, set, attr);
3188 if (err < 0)
3189 return err;
3190 }
3191 return 0;
3192}
3193
Patrick McHardy96518512013-10-14 11:00:02 +02003194static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3195 [NFT_MSG_NEWTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003196 .call_batch = nf_tables_newtable,
Patrick McHardy96518512013-10-14 11:00:02 +02003197 .attr_count = NFTA_TABLE_MAX,
3198 .policy = nft_table_policy,
3199 },
3200 [NFT_MSG_GETTABLE] = {
3201 .call = nf_tables_gettable,
3202 .attr_count = NFTA_TABLE_MAX,
3203 .policy = nft_table_policy,
3204 },
3205 [NFT_MSG_DELTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003206 .call_batch = nf_tables_deltable,
Patrick McHardy96518512013-10-14 11:00:02 +02003207 .attr_count = NFTA_TABLE_MAX,
3208 .policy = nft_table_policy,
3209 },
3210 [NFT_MSG_NEWCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003211 .call_batch = nf_tables_newchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003212 .attr_count = NFTA_CHAIN_MAX,
3213 .policy = nft_chain_policy,
3214 },
3215 [NFT_MSG_GETCHAIN] = {
3216 .call = nf_tables_getchain,
3217 .attr_count = NFTA_CHAIN_MAX,
3218 .policy = nft_chain_policy,
3219 },
3220 [NFT_MSG_DELCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003221 .call_batch = nf_tables_delchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003222 .attr_count = NFTA_CHAIN_MAX,
3223 .policy = nft_chain_policy,
3224 },
3225 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003226 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003227 .attr_count = NFTA_RULE_MAX,
3228 .policy = nft_rule_policy,
3229 },
3230 [NFT_MSG_GETRULE] = {
3231 .call = nf_tables_getrule,
3232 .attr_count = NFTA_RULE_MAX,
3233 .policy = nft_rule_policy,
3234 },
3235 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003236 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003237 .attr_count = NFTA_RULE_MAX,
3238 .policy = nft_rule_policy,
3239 },
Patrick McHardy20a69342013-10-11 12:06:22 +02003240 [NFT_MSG_NEWSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003241 .call_batch = nf_tables_newset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003242 .attr_count = NFTA_SET_MAX,
3243 .policy = nft_set_policy,
3244 },
3245 [NFT_MSG_GETSET] = {
3246 .call = nf_tables_getset,
3247 .attr_count = NFTA_SET_MAX,
3248 .policy = nft_set_policy,
3249 },
3250 [NFT_MSG_DELSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003251 .call_batch = nf_tables_delset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003252 .attr_count = NFTA_SET_MAX,
3253 .policy = nft_set_policy,
3254 },
3255 [NFT_MSG_NEWSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003256 .call_batch = nf_tables_newsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003257 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3258 .policy = nft_set_elem_list_policy,
3259 },
3260 [NFT_MSG_GETSETELEM] = {
3261 .call = nf_tables_getsetelem,
3262 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3263 .policy = nft_set_elem_list_policy,
3264 },
3265 [NFT_MSG_DELSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003266 .call_batch = nf_tables_delsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003267 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3268 .policy = nft_set_elem_list_policy,
3269 },
Patrick McHardy96518512013-10-14 11:00:02 +02003270};
3271
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003272static void nft_chain_commit_update(struct nft_trans *trans)
3273{
3274 struct nft_base_chain *basechain;
3275
3276 if (nft_trans_chain_name(trans)[0])
3277 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3278
3279 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3280 return;
3281
3282 basechain = nft_base_chain(trans->ctx.chain);
3283 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3284
3285 switch (nft_trans_chain_policy(trans)) {
3286 case NF_DROP:
3287 case NF_ACCEPT:
3288 basechain->policy = nft_trans_chain_policy(trans);
3289 break;
3290 }
3291}
3292
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003293static int nf_tables_commit(struct sk_buff *skb)
3294{
3295 struct net *net = sock_net(skb->sk);
3296 struct nft_trans *trans, *next;
3297
3298 /* Bump generation counter, invalidate any dump in progress */
3299 net->nft.genctr++;
3300
3301 /* A new generation has just started */
3302 net->nft.gencursor = gencursor_next(net);
3303
3304 /* Make sure all packets have left the previous generation before
3305 * purging old rules.
3306 */
3307 synchronize_rcu();
3308
3309 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003310 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003311 case NFT_MSG_NEWTABLE:
3312 if (nft_trans_table_update(trans)) {
3313 if (!nft_trans_table_enable(trans)) {
3314 nf_tables_table_disable(trans->ctx.afi,
3315 trans->ctx.table);
3316 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3317 }
3318 } else {
3319 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3320 }
3321 nf_tables_table_notify(trans->ctx.skb, trans->ctx.nlh,
3322 trans->ctx.table,
3323 NFT_MSG_NEWTABLE,
3324 trans->ctx.afi->family);
3325 nft_trans_destroy(trans);
3326 break;
3327 case NFT_MSG_DELTABLE:
3328 nf_tables_table_notify(trans->ctx.skb, trans->ctx.nlh,
3329 trans->ctx.table,
3330 NFT_MSG_DELTABLE,
3331 trans->ctx.afi->family);
3332 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003333 case NFT_MSG_NEWCHAIN:
3334 if (nft_trans_chain_update(trans))
3335 nft_chain_commit_update(trans);
3336 else {
3337 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
3338 trans->ctx.table->use++;
3339 }
3340 nf_tables_chain_notify(trans->ctx.skb, trans->ctx.nlh,
3341 trans->ctx.table,
3342 trans->ctx.chain,
3343 NFT_MSG_NEWCHAIN,
3344 trans->ctx.afi->family);
3345 nft_trans_destroy(trans);
3346 break;
3347 case NFT_MSG_DELCHAIN:
3348 trans->ctx.table->use--;
3349 nf_tables_chain_notify(trans->ctx.skb, trans->ctx.nlh,
3350 trans->ctx.table,
3351 trans->ctx.chain,
3352 NFT_MSG_DELCHAIN,
3353 trans->ctx.afi->family);
3354 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3355 trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3356 nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3357 trans->ctx.afi->nops);
3358 }
3359 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003360 case NFT_MSG_NEWRULE:
3361 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
3362 nf_tables_rule_notify(trans->ctx.skb, trans->ctx.nlh,
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003363 trans->ctx.table,
3364 trans->ctx.chain,
3365 nft_trans_rule(trans),
3366 NFT_MSG_NEWRULE, 0,
3367 trans->ctx.afi->family);
3368 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003369 break;
3370 case NFT_MSG_DELRULE:
3371 list_del_rcu(&nft_trans_rule(trans)->list);
3372 nf_tables_rule_notify(trans->ctx.skb, trans->ctx.nlh,
3373 trans->ctx.table,
3374 trans->ctx.chain,
3375 nft_trans_rule(trans), NFT_MSG_DELRULE, 0,
3376 trans->ctx.afi->family);
3377 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003378 case NFT_MSG_NEWSET:
3379 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
3380 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3381 NFT_MSG_NEWSET);
3382 nft_trans_destroy(trans);
3383 break;
3384 case NFT_MSG_DELSET:
3385 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
3386 NFT_MSG_DELSET);
3387 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003388 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003389 }
3390
3391 /* Make sure we don't see any packet traversing old rules */
3392 synchronize_rcu();
3393
3394 /* Now we can safely release unused old rules */
3395 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003396 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003397 case NFT_MSG_DELTABLE:
3398 nf_tables_table_destroy(&trans->ctx);
3399 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003400 case NFT_MSG_DELCHAIN:
3401 nf_tables_chain_destroy(trans->ctx.chain);
3402 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003403 case NFT_MSG_DELRULE:
3404 nf_tables_rule_destroy(&trans->ctx,
3405 nft_trans_rule(trans));
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003406 break;
3407 case NFT_MSG_DELSET:
3408 nft_set_destroy(nft_trans_set(trans));
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003409 break;
3410 }
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003411 nft_trans_destroy(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003412 }
3413
3414 return 0;
3415}
3416
3417static int nf_tables_abort(struct sk_buff *skb)
3418{
3419 struct net *net = sock_net(skb->sk);
3420 struct nft_trans *trans, *next;
3421
3422 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003423 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003424 case NFT_MSG_NEWTABLE:
3425 if (nft_trans_table_update(trans)) {
3426 if (nft_trans_table_enable(trans)) {
3427 nf_tables_table_disable(trans->ctx.afi,
3428 trans->ctx.table);
3429 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3430 }
3431 nft_trans_destroy(trans);
3432 } else {
3433 list_del(&trans->ctx.table->list);
3434 }
3435 break;
3436 case NFT_MSG_DELTABLE:
3437 list_add_tail(&trans->ctx.table->list,
3438 &trans->ctx.afi->tables);
3439 nft_trans_destroy(trans);
3440 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003441 case NFT_MSG_NEWCHAIN:
3442 if (nft_trans_chain_update(trans)) {
3443 if (nft_trans_chain_stats(trans))
3444 free_percpu(nft_trans_chain_stats(trans));
3445
3446 nft_trans_destroy(trans);
3447 } else {
3448 list_del(&trans->ctx.chain->list);
3449 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3450 trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3451 nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3452 trans->ctx.afi->nops);
3453 }
3454 }
3455 break;
3456 case NFT_MSG_DELCHAIN:
3457 list_add_tail(&trans->ctx.chain->list,
3458 &trans->ctx.table->chains);
3459 nft_trans_destroy(trans);
3460 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003461 case NFT_MSG_NEWRULE:
3462 list_del_rcu(&nft_trans_rule(trans)->list);
3463 break;
3464 case NFT_MSG_DELRULE:
3465 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003466 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003467 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003468 case NFT_MSG_NEWSET:
3469 list_del(&nft_trans_set(trans)->list);
3470 break;
3471 case NFT_MSG_DELSET:
3472 list_add_tail(&nft_trans_set(trans)->list,
3473 &trans->ctx.table->sets);
3474 nft_trans_destroy(trans);
3475 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003476 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003477 }
3478
3479 /* Make sure we don't see any packet accessing aborted rules */
3480 synchronize_rcu();
3481
3482 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003483 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003484 case NFT_MSG_NEWTABLE:
3485 nf_tables_table_destroy(&trans->ctx);
3486 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003487 case NFT_MSG_NEWCHAIN:
3488 nf_tables_chain_destroy(trans->ctx.chain);
3489 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003490 case NFT_MSG_NEWRULE:
3491 nf_tables_rule_destroy(&trans->ctx,
3492 nft_trans_rule(trans));
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003493 break;
3494 case NFT_MSG_NEWSET:
3495 nft_set_destroy(nft_trans_set(trans));
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003496 break;
3497 }
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003498 nft_trans_destroy(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003499 }
3500
3501 return 0;
3502}
3503
Patrick McHardy96518512013-10-14 11:00:02 +02003504static const struct nfnetlink_subsystem nf_tables_subsys = {
3505 .name = "nf_tables",
3506 .subsys_id = NFNL_SUBSYS_NFTABLES,
3507 .cb_count = NFT_MSG_MAX,
3508 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003509 .commit = nf_tables_commit,
3510 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02003511};
3512
Patrick McHardy20a69342013-10-11 12:06:22 +02003513/*
3514 * Loop detection - walk through the ruleset beginning at the destination chain
3515 * of a new jump until either the source chain is reached (loop) or all
3516 * reachable chains have been traversed.
3517 *
3518 * The loop check is performed whenever a new jump verdict is added to an
3519 * expression or verdict map or a verdict map is bound to a new chain.
3520 */
3521
3522static int nf_tables_check_loops(const struct nft_ctx *ctx,
3523 const struct nft_chain *chain);
3524
3525static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3526 const struct nft_set *set,
3527 const struct nft_set_iter *iter,
3528 const struct nft_set_elem *elem)
3529{
Pablo Neira Ayuso62f9c8b2014-02-07 14:45:01 +01003530 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3531 return 0;
3532
Patrick McHardy20a69342013-10-11 12:06:22 +02003533 switch (elem->data.verdict) {
3534 case NFT_JUMP:
3535 case NFT_GOTO:
3536 return nf_tables_check_loops(ctx, elem->data.chain);
3537 default:
3538 return 0;
3539 }
3540}
3541
3542static int nf_tables_check_loops(const struct nft_ctx *ctx,
3543 const struct nft_chain *chain)
3544{
3545 const struct nft_rule *rule;
3546 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02003547 const struct nft_set *set;
3548 struct nft_set_binding *binding;
3549 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02003550
3551 if (ctx->chain == chain)
3552 return -ELOOP;
3553
3554 list_for_each_entry(rule, &chain->rules, list) {
3555 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003556 const struct nft_data *data = NULL;
3557 int err;
3558
3559 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02003560 continue;
3561
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003562 err = expr->ops->validate(ctx, expr, &data);
3563 if (err < 0)
3564 return err;
3565
Patrick McHardy20a69342013-10-11 12:06:22 +02003566 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003567 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02003568
3569 switch (data->verdict) {
3570 case NFT_JUMP:
3571 case NFT_GOTO:
3572 err = nf_tables_check_loops(ctx, data->chain);
3573 if (err < 0)
3574 return err;
3575 default:
3576 break;
3577 }
3578 }
3579 }
3580
3581 list_for_each_entry(set, &ctx->table->sets, list) {
3582 if (!(set->flags & NFT_SET_MAP) ||
3583 set->dtype != NFT_DATA_VERDICT)
3584 continue;
3585
3586 list_for_each_entry(binding, &set->bindings, list) {
3587 if (binding->chain != chain)
3588 continue;
3589
3590 iter.skip = 0;
3591 iter.count = 0;
3592 iter.err = 0;
3593 iter.fn = nf_tables_loop_check_setelem;
3594
3595 set->ops->walk(ctx, set, &iter);
3596 if (iter.err < 0)
3597 return iter.err;
3598 }
3599 }
3600
3601 return 0;
3602}
3603
Patrick McHardy96518512013-10-14 11:00:02 +02003604/**
3605 * nft_validate_input_register - validate an expressions' input register
3606 *
3607 * @reg: the register number
3608 *
3609 * Validate that the input register is one of the general purpose
3610 * registers.
3611 */
3612int nft_validate_input_register(enum nft_registers reg)
3613{
3614 if (reg <= NFT_REG_VERDICT)
3615 return -EINVAL;
3616 if (reg > NFT_REG_MAX)
3617 return -ERANGE;
3618 return 0;
3619}
3620EXPORT_SYMBOL_GPL(nft_validate_input_register);
3621
3622/**
3623 * nft_validate_output_register - validate an expressions' output register
3624 *
3625 * @reg: the register number
3626 *
3627 * Validate that the output register is one of the general purpose
3628 * registers or the verdict register.
3629 */
3630int nft_validate_output_register(enum nft_registers reg)
3631{
3632 if (reg < NFT_REG_VERDICT)
3633 return -EINVAL;
3634 if (reg > NFT_REG_MAX)
3635 return -ERANGE;
3636 return 0;
3637}
3638EXPORT_SYMBOL_GPL(nft_validate_output_register);
3639
3640/**
3641 * nft_validate_data_load - validate an expressions' data load
3642 *
3643 * @ctx: context of the expression performing the load
3644 * @reg: the destination register number
3645 * @data: the data to load
3646 * @type: the data type
3647 *
3648 * Validate that a data load uses the appropriate data type for
3649 * the destination register. A value of NULL for the data means
3650 * that its runtime gathered data, which is always of type
3651 * NFT_DATA_VALUE.
3652 */
3653int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3654 const struct nft_data *data,
3655 enum nft_data_types type)
3656{
Patrick McHardy20a69342013-10-11 12:06:22 +02003657 int err;
3658
Patrick McHardy96518512013-10-14 11:00:02 +02003659 switch (reg) {
3660 case NFT_REG_VERDICT:
3661 if (data == NULL || type != NFT_DATA_VERDICT)
3662 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003663
3664 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3665 err = nf_tables_check_loops(ctx, data->chain);
3666 if (err < 0)
3667 return err;
3668
3669 if (ctx->chain->level + 1 > data->chain->level) {
3670 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3671 return -EMLINK;
3672 data->chain->level = ctx->chain->level + 1;
3673 }
3674 }
3675
Patrick McHardy96518512013-10-14 11:00:02 +02003676 return 0;
3677 default:
3678 if (data != NULL && type != NFT_DATA_VALUE)
3679 return -EINVAL;
3680 return 0;
3681 }
3682}
3683EXPORT_SYMBOL_GPL(nft_validate_data_load);
3684
3685static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3686 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3687 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3688 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3689};
3690
3691static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3692 struct nft_data_desc *desc, const struct nlattr *nla)
3693{
3694 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3695 struct nft_chain *chain;
3696 int err;
3697
3698 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3699 if (err < 0)
3700 return err;
3701
3702 if (!tb[NFTA_VERDICT_CODE])
3703 return -EINVAL;
3704 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3705
3706 switch (data->verdict) {
Patrick McHardye0abdad2014-02-18 18:06:50 +00003707 default:
3708 switch (data->verdict & NF_VERDICT_MASK) {
3709 case NF_ACCEPT:
3710 case NF_DROP:
3711 case NF_QUEUE:
3712 break;
3713 default:
3714 return -EINVAL;
3715 }
3716 /* fall through */
Patrick McHardy96518512013-10-14 11:00:02 +02003717 case NFT_CONTINUE:
3718 case NFT_BREAK:
3719 case NFT_RETURN:
3720 desc->len = sizeof(data->verdict);
3721 break;
3722 case NFT_JUMP:
3723 case NFT_GOTO:
3724 if (!tb[NFTA_VERDICT_CHAIN])
3725 return -EINVAL;
3726 chain = nf_tables_chain_lookup(ctx->table,
3727 tb[NFTA_VERDICT_CHAIN]);
3728 if (IS_ERR(chain))
3729 return PTR_ERR(chain);
3730 if (chain->flags & NFT_BASE_CHAIN)
3731 return -EOPNOTSUPP;
3732
Patrick McHardy96518512013-10-14 11:00:02 +02003733 chain->use++;
3734 data->chain = chain;
3735 desc->len = sizeof(data);
3736 break;
Patrick McHardy96518512013-10-14 11:00:02 +02003737 }
3738
3739 desc->type = NFT_DATA_VERDICT;
3740 return 0;
3741}
3742
3743static void nft_verdict_uninit(const struct nft_data *data)
3744{
3745 switch (data->verdict) {
3746 case NFT_JUMP:
3747 case NFT_GOTO:
3748 data->chain->use--;
3749 break;
3750 }
3751}
3752
3753static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3754{
3755 struct nlattr *nest;
3756
3757 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3758 if (!nest)
3759 goto nla_put_failure;
3760
3761 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3762 goto nla_put_failure;
3763
3764 switch (data->verdict) {
3765 case NFT_JUMP:
3766 case NFT_GOTO:
3767 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3768 goto nla_put_failure;
3769 }
3770 nla_nest_end(skb, nest);
3771 return 0;
3772
3773nla_put_failure:
3774 return -1;
3775}
3776
3777static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3778 struct nft_data_desc *desc, const struct nlattr *nla)
3779{
3780 unsigned int len;
3781
3782 len = nla_len(nla);
3783 if (len == 0)
3784 return -EINVAL;
3785 if (len > sizeof(data->data))
3786 return -EOVERFLOW;
3787
3788 nla_memcpy(data->data, nla, sizeof(data->data));
3789 desc->type = NFT_DATA_VALUE;
3790 desc->len = len;
3791 return 0;
3792}
3793
3794static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3795 unsigned int len)
3796{
3797 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3798}
3799
3800static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3801 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3802 .len = FIELD_SIZEOF(struct nft_data, data) },
3803 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3804};
3805
3806/**
3807 * nft_data_init - parse nf_tables data netlink attributes
3808 *
3809 * @ctx: context of the expression using the data
3810 * @data: destination struct nft_data
3811 * @desc: data description
3812 * @nla: netlink attribute containing data
3813 *
3814 * Parse the netlink data attributes and initialize a struct nft_data.
3815 * The type and length of data are returned in the data description.
3816 *
3817 * The caller can indicate that it only wants to accept data of type
3818 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3819 */
3820int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3821 struct nft_data_desc *desc, const struct nlattr *nla)
3822{
3823 struct nlattr *tb[NFTA_DATA_MAX + 1];
3824 int err;
3825
3826 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3827 if (err < 0)
3828 return err;
3829
3830 if (tb[NFTA_DATA_VALUE])
3831 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3832 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3833 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3834 return -EINVAL;
3835}
3836EXPORT_SYMBOL_GPL(nft_data_init);
3837
3838/**
3839 * nft_data_uninit - release a nft_data item
3840 *
3841 * @data: struct nft_data to release
3842 * @type: type of data
3843 *
3844 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3845 * all others need to be released by calling this function.
3846 */
3847void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3848{
3849 switch (type) {
3850 case NFT_DATA_VALUE:
3851 return;
3852 case NFT_DATA_VERDICT:
3853 return nft_verdict_uninit(data);
3854 default:
3855 WARN_ON(1);
3856 }
3857}
3858EXPORT_SYMBOL_GPL(nft_data_uninit);
3859
3860int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3861 enum nft_data_types type, unsigned int len)
3862{
3863 struct nlattr *nest;
3864 int err;
3865
3866 nest = nla_nest_start(skb, attr);
3867 if (nest == NULL)
3868 return -1;
3869
3870 switch (type) {
3871 case NFT_DATA_VALUE:
3872 err = nft_value_dump(skb, data, len);
3873 break;
3874 case NFT_DATA_VERDICT:
3875 err = nft_verdict_dump(skb, data);
3876 break;
3877 default:
3878 err = -EINVAL;
3879 WARN_ON(1);
3880 }
3881
3882 nla_nest_end(skb, nest);
3883 return err;
3884}
3885EXPORT_SYMBOL_GPL(nft_data_dump);
3886
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003887static int nf_tables_init_net(struct net *net)
3888{
3889 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003890 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003891 return 0;
3892}
3893
3894static struct pernet_operations nf_tables_net_ops = {
3895 .init = nf_tables_init_net,
3896};
3897
Patrick McHardy96518512013-10-14 11:00:02 +02003898static int __init nf_tables_module_init(void)
3899{
3900 int err;
3901
3902 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3903 GFP_KERNEL);
3904 if (info == NULL) {
3905 err = -ENOMEM;
3906 goto err1;
3907 }
3908
3909 err = nf_tables_core_module_init();
3910 if (err < 0)
3911 goto err2;
3912
3913 err = nfnetlink_subsys_register(&nf_tables_subsys);
3914 if (err < 0)
3915 goto err3;
3916
3917 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003918 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003919err3:
3920 nf_tables_core_module_exit();
3921err2:
3922 kfree(info);
3923err1:
3924 return err;
3925}
3926
3927static void __exit nf_tables_module_exit(void)
3928{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003929 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003930 nfnetlink_subsys_unregister(&nf_tables_subsys);
3931 nf_tables_core_module_exit();
3932 kfree(info);
3933}
3934
3935module_init(nf_tables_module_init);
3936module_exit(nf_tables_module_exit);
3937
3938MODULE_LICENSE("GPL");
3939MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3940MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);