blob: a27a7c56e7c3f9961b3ba813c9dc115096e8f800 [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 Ayusoe688a7f2014-07-01 11:49:18 +020038 list_add_tail_rcu(&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);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +020054 list_del_rcu(&afi->list);
Patrick McHardy96518512013-10-14 11:00:02 +020055 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{
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +020099 ctx->net = sock_net(skb->sk);
100 ctx->afi = afi;
101 ctx->table = table;
102 ctx->chain = chain;
103 ctx->nla = nla;
104 ctx->portid = NETLINK_CB(skb).portid;
105 ctx->report = nlmsg_report(nlh);
106 ctx->seq = nlh->nlmsg_seq;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +0200107}
108
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +0200109static struct nft_trans *nft_trans_alloc(struct nft_ctx *ctx, int msg_type,
110 u32 size)
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +0200111{
112 struct nft_trans *trans;
113
114 trans = kzalloc(sizeof(struct nft_trans) + size, GFP_KERNEL);
115 if (trans == NULL)
116 return NULL;
117
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +0200118 trans->msg_type = msg_type;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +0200119 trans->ctx = *ctx;
120
121 return trans;
122}
123
124static void nft_trans_destroy(struct nft_trans *trans)
125{
126 list_del(&trans->list);
127 kfree(trans);
128}
129
Patrick McHardy96518512013-10-14 11:00:02 +0200130/*
131 * Tables
132 */
133
134static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
135 const struct nlattr *nla)
136{
137 struct nft_table *table;
138
139 list_for_each_entry(table, &afi->tables, list) {
140 if (!nla_strcmp(nla, table->name))
141 return table;
142 }
143 return NULL;
144}
145
146static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200147 const struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +0200148{
149 struct nft_table *table;
150
151 if (nla == NULL)
152 return ERR_PTR(-EINVAL);
153
154 table = nft_table_lookup(afi, nla);
155 if (table != NULL)
156 return table;
157
Patrick McHardy96518512013-10-14 11:00:02 +0200158 return ERR_PTR(-ENOENT);
159}
160
161static inline u64 nf_tables_alloc_handle(struct nft_table *table)
162{
163 return ++table->hgenerator;
164}
165
Patrick McHardy2a37d752014-01-09 18:42:37 +0000166static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200167
Patrick McHardy2a37d752014-01-09 18:42:37 +0000168static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000169__nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200170{
171 int i;
172
Patrick McHardybaae3e62014-01-09 18:42:34 +0000173 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200174 if (chain_type[family][i] != NULL &&
175 !nla_strcmp(nla, chain_type[family][i]->name))
Patrick McHardybaae3e62014-01-09 18:42:34 +0000176 return chain_type[family][i];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200177 }
Patrick McHardybaae3e62014-01-09 18:42:34 +0000178 return NULL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200179}
180
Patrick McHardy2a37d752014-01-09 18:42:37 +0000181static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000182nf_tables_chain_type_lookup(const struct nft_af_info *afi,
183 const struct nlattr *nla,
184 bool autoload)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200185{
Patrick McHardy2a37d752014-01-09 18:42:37 +0000186 const struct nf_chain_type *type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200187
188 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000189 if (type != NULL)
190 return type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200191#ifdef CONFIG_MODULES
Patrick McHardy93b08062014-01-09 18:42:36 +0000192 if (autoload) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200193 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso2fec6bb2014-03-31 12:26:39 +0200194 request_module("nft-chain-%u-%.*s", afi->family,
195 nla_len(nla), (const char *)nla_data(nla));
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200196 nfnl_lock(NFNL_SUBSYS_NFTABLES);
197 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000198 if (type != NULL)
199 return ERR_PTR(-EAGAIN);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200200 }
201#endif
Patrick McHardy93b08062014-01-09 18:42:36 +0000202 return ERR_PTR(-ENOENT);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200203}
204
Patrick McHardy96518512013-10-14 11:00:02 +0200205static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
206 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200207 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
Patrick McHardy96518512013-10-14 11:00:02 +0200208};
209
210static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
211 int event, u32 flags, int family,
212 const struct nft_table *table)
213{
214 struct nlmsghdr *nlh;
215 struct nfgenmsg *nfmsg;
216
217 event |= NFNL_SUBSYS_NFTABLES << 8;
218 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
219 if (nlh == NULL)
220 goto nla_put_failure;
221
222 nfmsg = nlmsg_data(nlh);
223 nfmsg->nfgen_family = family;
224 nfmsg->version = NFNETLINK_V0;
225 nfmsg->res_id = 0;
226
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200227 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
Tomasz Bursztykad8bcc7682013-12-12 15:00:42 +0200228 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
229 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
Patrick McHardy96518512013-10-14 11:00:02 +0200230 goto nla_put_failure;
231
232 return nlmsg_end(skb, nlh);
233
234nla_put_failure:
235 nlmsg_trim(skb, nlh);
236 return -1;
237}
238
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200239static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)
Patrick McHardy96518512013-10-14 11:00:02 +0200240{
241 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +0200242 int err;
243
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200244 if (!ctx->report &&
245 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +0200246 return 0;
247
248 err = -ENOBUFS;
249 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
250 if (skb == NULL)
251 goto err;
252
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200253 err = nf_tables_fill_table_info(skb, ctx->portid, ctx->seq, event, 0,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200254 ctx->afi->family, ctx->table);
Patrick McHardy96518512013-10-14 11:00:02 +0200255 if (err < 0) {
256 kfree_skb(skb);
257 goto err;
258 }
259
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200260 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
261 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +0200262err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200263 if (err < 0) {
264 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
265 err);
266 }
Patrick McHardy96518512013-10-14 11:00:02 +0200267 return err;
268}
269
270static int nf_tables_dump_tables(struct sk_buff *skb,
271 struct netlink_callback *cb)
272{
273 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
274 const struct nft_af_info *afi;
275 const struct nft_table *table;
276 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200277 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200278 int family = nfmsg->nfgen_family;
279
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200280 rcu_read_lock();
281 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200282 if (family != NFPROTO_UNSPEC && family != afi->family)
283 continue;
284
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200285 list_for_each_entry_rcu(table, &afi->tables, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200286 if (idx < s_idx)
287 goto cont;
288 if (idx > s_idx)
289 memset(&cb->args[1], 0,
290 sizeof(cb->args) - sizeof(cb->args[0]));
291 if (nf_tables_fill_table_info(skb,
292 NETLINK_CB(cb->skb).portid,
293 cb->nlh->nlmsg_seq,
294 NFT_MSG_NEWTABLE,
295 NLM_F_MULTI,
296 afi->family, table) < 0)
297 goto done;
298cont:
299 idx++;
300 }
301 }
302done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200303 rcu_read_unlock();
Patrick McHardy96518512013-10-14 11:00:02 +0200304 cb->args[0] = idx;
305 return skb->len;
306}
307
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200308/* Internal table flags */
309#define NFT_TABLE_INACTIVE (1 << 15)
310
Patrick McHardy96518512013-10-14 11:00:02 +0200311static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
312 const struct nlmsghdr *nlh,
313 const struct nlattr * const nla[])
314{
315 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
316 const struct nft_af_info *afi;
317 const struct nft_table *table;
318 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200319 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200320 int family = nfmsg->nfgen_family;
321 int err;
322
323 if (nlh->nlmsg_flags & NLM_F_DUMP) {
324 struct netlink_dump_control c = {
325 .dump = nf_tables_dump_tables,
326 };
327 return netlink_dump_start(nlsk, skb, nlh, &c);
328 }
329
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200330 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200331 if (IS_ERR(afi))
332 return PTR_ERR(afi);
333
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200334 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200335 if (IS_ERR(table))
336 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200337 if (table->flags & NFT_TABLE_INACTIVE)
338 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200339
340 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
341 if (!skb2)
342 return -ENOMEM;
343
344 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
345 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
346 family, table);
347 if (err < 0)
348 goto err;
349
350 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
351
352err:
353 kfree_skb(skb2);
354 return err;
355}
356
Patrick McHardy115a60b2014-01-03 12:16:15 +0000357static int nf_tables_table_enable(const struct nft_af_info *afi,
358 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200359{
360 struct nft_chain *chain;
361 int err, i = 0;
362
363 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100364 if (!(chain->flags & NFT_BASE_CHAIN))
365 continue;
366
Patrick McHardy115a60b2014-01-03 12:16:15 +0000367 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200368 if (err < 0)
369 goto err;
370
371 i++;
372 }
373 return 0;
374err:
375 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100376 if (!(chain->flags & NFT_BASE_CHAIN))
377 continue;
378
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200379 if (i-- <= 0)
380 break;
381
Patrick McHardy115a60b2014-01-03 12:16:15 +0000382 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200383 }
384 return err;
385}
386
Pablo Neira Ayusof75edf52014-03-30 14:04:52 +0200387static void nf_tables_table_disable(const struct nft_af_info *afi,
Patrick McHardy115a60b2014-01-03 12:16:15 +0000388 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200389{
390 struct nft_chain *chain;
391
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100392 list_for_each_entry(chain, &table->chains, list) {
393 if (chain->flags & NFT_BASE_CHAIN)
Patrick McHardy115a60b2014-01-03 12:16:15 +0000394 nf_unregister_hooks(nft_base_chain(chain)->ops,
395 afi->nops);
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100396 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200397}
398
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200399static int nf_tables_updtable(struct nft_ctx *ctx)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200400{
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200401 struct nft_trans *trans;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200402 u32 flags;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200403 int ret = 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200404
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200405 if (!ctx->nla[NFTA_TABLE_FLAGS])
406 return 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200407
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200408 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
409 if (flags & ~NFT_TABLE_F_DORMANT)
410 return -EINVAL;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200411
Pablo Neira Ayuso63283dd2014-06-27 18:51:39 +0200412 if (flags == ctx->table->flags)
413 return 0;
414
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200415 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
416 sizeof(struct nft_trans_table));
417 if (trans == NULL)
418 return -ENOMEM;
419
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200420 if ((flags & NFT_TABLE_F_DORMANT) &&
421 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200422 nft_trans_table_enable(trans) = false;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200423 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
424 ctx->table->flags & NFT_TABLE_F_DORMANT) {
425 ret = nf_tables_table_enable(ctx->afi, ctx->table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200426 if (ret >= 0) {
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200427 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200428 nft_trans_table_enable(trans) = true;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200429 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200430 }
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200431 if (ret < 0)
432 goto err;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200433
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200434 nft_trans_table_update(trans) = true;
435 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
436 return 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200437err:
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200438 nft_trans_destroy(trans);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200439 return ret;
440}
441
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200442static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
443{
444 struct nft_trans *trans;
445
446 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
447 if (trans == NULL)
448 return -ENOMEM;
449
450 if (msg_type == NFT_MSG_NEWTABLE)
451 ctx->table->flags |= NFT_TABLE_INACTIVE;
452
453 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
454 return 0;
455}
456
Patrick McHardy96518512013-10-14 11:00:02 +0200457static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
458 const struct nlmsghdr *nlh,
459 const struct nlattr * const nla[])
460{
461 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
462 const struct nlattr *name;
463 struct nft_af_info *afi;
464 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200465 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200466 int family = nfmsg->nfgen_family;
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000467 u32 flags = 0;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200468 struct nft_ctx ctx;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200469 int err;
Patrick McHardy96518512013-10-14 11:00:02 +0200470
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200471 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200472 if (IS_ERR(afi))
473 return PTR_ERR(afi);
474
475 name = nla[NFTA_TABLE_NAME];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200476 table = nf_tables_table_lookup(afi, name);
Patrick McHardy96518512013-10-14 11:00:02 +0200477 if (IS_ERR(table)) {
478 if (PTR_ERR(table) != -ENOENT)
479 return PTR_ERR(table);
480 table = NULL;
481 }
482
483 if (table != NULL) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200484 if (table->flags & NFT_TABLE_INACTIVE)
485 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200486 if (nlh->nlmsg_flags & NLM_F_EXCL)
487 return -EEXIST;
488 if (nlh->nlmsg_flags & NLM_F_REPLACE)
489 return -EOPNOTSUPP;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200490
491 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
492 return nf_tables_updtable(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +0200493 }
494
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000495 if (nla[NFTA_TABLE_FLAGS]) {
496 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
497 if (flags & ~NFT_TABLE_F_DORMANT)
498 return -EINVAL;
499 }
500
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000501 if (!try_module_get(afi->owner))
502 return -EAFNOSUPPORT;
503
Patrick McHardy96518512013-10-14 11:00:02 +0200504 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000505 if (table == NULL) {
506 module_put(afi->owner);
Patrick McHardy96518512013-10-14 11:00:02 +0200507 return -ENOMEM;
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000508 }
Patrick McHardy96518512013-10-14 11:00:02 +0200509
510 nla_strlcpy(table->name, name, nla_len(name));
511 INIT_LIST_HEAD(&table->chains);
Patrick McHardy20a69342013-10-11 12:06:22 +0200512 INIT_LIST_HEAD(&table->sets);
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000513 table->flags = flags;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200514
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200515 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
516 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
517 if (err < 0) {
518 kfree(table);
519 module_put(afi->owner);
520 return err;
521 }
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200522 list_add_tail_rcu(&table->list, &afi->tables);
Patrick McHardy96518512013-10-14 11:00:02 +0200523 return 0;
524}
525
526static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
527 const struct nlmsghdr *nlh,
528 const struct nlattr * const nla[])
529{
530 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
531 struct nft_af_info *afi;
532 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200533 struct net *net = sock_net(skb->sk);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200534 int family = nfmsg->nfgen_family, err;
535 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +0200536
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200537 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200538 if (IS_ERR(afi))
539 return PTR_ERR(afi);
540
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200541 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200542 if (IS_ERR(table))
543 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200544 if (table->flags & NFT_TABLE_INACTIVE)
545 return -ENOENT;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +0200546 if (table->use > 0)
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
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200554 list_del_rcu(&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{
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +0200560 BUG_ON(ctx->table->use > 0);
561
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200562 kfree(ctx->table);
563 module_put(ctx->afi->owner);
564}
565
Patrick McHardy2a37d752014-01-09 18:42:37 +0000566int nft_register_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200567{
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200568 int err = 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200569
570 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200571 if (chain_type[ctype->family][ctype->type] != NULL) {
572 err = -EBUSY;
573 goto out;
Patrick McHardy96518512013-10-14 11:00:02 +0200574 }
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200575 chain_type[ctype->family][ctype->type] = ctype;
576out:
Patrick McHardy96518512013-10-14 11:00:02 +0200577 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
578 return err;
579}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200580EXPORT_SYMBOL_GPL(nft_register_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200581
Patrick McHardy2a37d752014-01-09 18:42:37 +0000582void nft_unregister_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200583{
Patrick McHardy96518512013-10-14 11:00:02 +0200584 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200585 chain_type[ctype->family][ctype->type] = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200586 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
587}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200588EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200589
590/*
591 * Chains
592 */
593
594static struct nft_chain *
595nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
596{
597 struct nft_chain *chain;
598
599 list_for_each_entry(chain, &table->chains, list) {
600 if (chain->handle == handle)
601 return chain;
602 }
603
604 return ERR_PTR(-ENOENT);
605}
606
607static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
608 const struct nlattr *nla)
609{
610 struct nft_chain *chain;
611
612 if (nla == NULL)
613 return ERR_PTR(-EINVAL);
614
615 list_for_each_entry(chain, &table->chains, list) {
616 if (!nla_strcmp(nla, chain->name))
617 return chain;
618 }
619
620 return ERR_PTR(-ENOENT);
621}
622
623static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
624 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
625 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
626 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
627 .len = NFT_CHAIN_MAXNAMELEN - 1 },
628 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200629 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
Pablo Neira4c1f7812014-03-31 17:43:47 +0200630 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200631 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
Patrick McHardy96518512013-10-14 11:00:02 +0200632};
633
634static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
635 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
636 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
637};
638
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200639static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
640{
641 struct nft_stats *cpu_stats, total;
642 struct nlattr *nest;
643 int cpu;
644
645 memset(&total, 0, sizeof(total));
646 for_each_possible_cpu(cpu) {
647 cpu_stats = per_cpu_ptr(stats, cpu);
648 total.pkts += cpu_stats->pkts;
649 total.bytes += cpu_stats->bytes;
650 }
651 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
652 if (nest == NULL)
653 goto nla_put_failure;
654
655 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
656 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
657 goto nla_put_failure;
658
659 nla_nest_end(skb, nest);
660 return 0;
661
662nla_put_failure:
663 return -ENOSPC;
664}
665
Patrick McHardy96518512013-10-14 11:00:02 +0200666static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
667 int event, u32 flags, int family,
668 const struct nft_table *table,
669 const struct nft_chain *chain)
670{
671 struct nlmsghdr *nlh;
672 struct nfgenmsg *nfmsg;
673
674 event |= NFNL_SUBSYS_NFTABLES << 8;
675 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
676 if (nlh == NULL)
677 goto nla_put_failure;
678
679 nfmsg = nlmsg_data(nlh);
680 nfmsg->nfgen_family = family;
681 nfmsg->version = NFNETLINK_V0;
682 nfmsg->res_id = 0;
683
684 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
685 goto nla_put_failure;
686 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
687 goto nla_put_failure;
688 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
689 goto nla_put_failure;
690
691 if (chain->flags & NFT_BASE_CHAIN) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200692 const struct nft_base_chain *basechain = nft_base_chain(chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000693 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200694 struct nlattr *nest;
695
696 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
Patrick McHardy96518512013-10-14 11:00:02 +0200697 if (nest == NULL)
698 goto nla_put_failure;
699 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
700 goto nla_put_failure;
701 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
702 goto nla_put_failure;
703 nla_nest_end(skb, nest);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200704
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200705 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
706 htonl(basechain->policy)))
707 goto nla_put_failure;
708
Patrick McHardybaae3e62014-01-09 18:42:34 +0000709 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
710 goto nla_put_failure;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200711
712 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
713 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +0200714 }
715
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200716 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
717 goto nla_put_failure;
718
Patrick McHardy96518512013-10-14 11:00:02 +0200719 return nlmsg_end(skb, nlh);
720
721nla_put_failure:
722 nlmsg_trim(skb, nlh);
723 return -1;
724}
725
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200726static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
Patrick McHardy96518512013-10-14 11:00:02 +0200727{
728 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +0200729 int err;
730
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200731 if (!ctx->report &&
732 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +0200733 return 0;
734
735 err = -ENOBUFS;
736 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
737 if (skb == NULL)
738 goto err;
739
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200740 err = nf_tables_fill_chain_info(skb, ctx->portid, ctx->seq, event, 0,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200741 ctx->afi->family, ctx->table,
742 ctx->chain);
Patrick McHardy96518512013-10-14 11:00:02 +0200743 if (err < 0) {
744 kfree_skb(skb);
745 goto err;
746 }
747
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200748 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
749 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +0200750err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200751 if (err < 0) {
752 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
753 err);
754 }
Patrick McHardy96518512013-10-14 11:00:02 +0200755 return err;
756}
757
758static int nf_tables_dump_chains(struct sk_buff *skb,
759 struct netlink_callback *cb)
760{
761 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
762 const struct nft_af_info *afi;
763 const struct nft_table *table;
764 const struct nft_chain *chain;
765 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200766 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200767 int family = nfmsg->nfgen_family;
768
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200769 rcu_read_lock();
770 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200771 if (family != NFPROTO_UNSPEC && family != afi->family)
772 continue;
773
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200774 list_for_each_entry_rcu(table, &afi->tables, list) {
775 list_for_each_entry_rcu(chain, &table->chains, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200776 if (idx < s_idx)
777 goto cont;
778 if (idx > s_idx)
779 memset(&cb->args[1], 0,
780 sizeof(cb->args) - sizeof(cb->args[0]));
781 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
782 cb->nlh->nlmsg_seq,
783 NFT_MSG_NEWCHAIN,
784 NLM_F_MULTI,
785 afi->family, table, chain) < 0)
786 goto done;
787cont:
788 idx++;
789 }
790 }
791 }
792done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200793 rcu_read_unlock();
Patrick McHardy96518512013-10-14 11:00:02 +0200794 cb->args[0] = idx;
795 return skb->len;
796}
797
Patrick McHardy96518512013-10-14 11:00:02 +0200798static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
799 const struct nlmsghdr *nlh,
800 const struct nlattr * const nla[])
801{
802 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
803 const struct nft_af_info *afi;
804 const struct nft_table *table;
805 const struct nft_chain *chain;
806 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200807 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200808 int family = nfmsg->nfgen_family;
809 int err;
810
811 if (nlh->nlmsg_flags & NLM_F_DUMP) {
812 struct netlink_dump_control c = {
813 .dump = nf_tables_dump_chains,
814 };
815 return netlink_dump_start(nlsk, skb, nlh, &c);
816 }
817
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200818 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200819 if (IS_ERR(afi))
820 return PTR_ERR(afi);
821
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200822 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200823 if (IS_ERR(table))
824 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200825 if (table->flags & NFT_TABLE_INACTIVE)
826 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200827
828 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
829 if (IS_ERR(chain))
830 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +0200831 if (chain->flags & NFT_CHAIN_INACTIVE)
832 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200833
834 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
835 if (!skb2)
836 return -ENOMEM;
837
838 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
839 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
840 family, table, chain);
841 if (err < 0)
842 goto err;
843
844 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
845
846err:
847 kfree_skb(skb2);
848 return err;
849}
850
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200851static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
852 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
853 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
854};
855
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200856static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200857{
858 struct nlattr *tb[NFTA_COUNTER_MAX+1];
859 struct nft_stats __percpu *newstats;
860 struct nft_stats *stats;
861 int err;
862
863 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
864 if (err < 0)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200865 return ERR_PTR(err);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200866
867 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200868 return ERR_PTR(-EINVAL);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200869
870 newstats = alloc_percpu(struct nft_stats);
871 if (newstats == NULL)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200872 return ERR_PTR(-ENOMEM);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200873
874 /* Restore old counters on this cpu, no problem. Per-cpu statistics
875 * are not exposed to userspace.
876 */
877 stats = this_cpu_ptr(newstats);
878 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
879 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
880
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200881 return newstats;
882}
883
884static void nft_chain_stats_replace(struct nft_base_chain *chain,
885 struct nft_stats __percpu *newstats)
886{
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200887 if (chain->stats) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200888 struct nft_stats __percpu *oldstats =
Patrick McHardy67a8fc22014-02-18 18:06:49 +0000889 nft_dereference(chain->stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200890
891 rcu_assign_pointer(chain->stats, newstats);
892 synchronize_rcu();
893 free_percpu(oldstats);
894 } else
895 rcu_assign_pointer(chain->stats, newstats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200896}
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200897
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +0200898static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
899{
900 struct nft_trans *trans;
901
902 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
903 if (trans == NULL)
904 return -ENOMEM;
905
906 if (msg_type == NFT_MSG_NEWCHAIN)
907 ctx->chain->flags |= NFT_CHAIN_INACTIVE;
908
909 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200910 return 0;
911}
912
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +0200913static void nf_tables_chain_destroy(struct nft_chain *chain)
914{
915 BUG_ON(chain->use > 0);
916
917 if (chain->flags & NFT_BASE_CHAIN) {
918 module_put(nft_base_chain(chain)->type->owner);
919 free_percpu(nft_base_chain(chain)->stats);
920 kfree(nft_base_chain(chain));
921 } else {
922 kfree(chain);
923 }
924}
925
Patrick McHardy96518512013-10-14 11:00:02 +0200926static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
927 const struct nlmsghdr *nlh,
928 const struct nlattr * const nla[])
929{
930 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
931 const struct nlattr * uninitialized_var(name);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +0200932 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +0200933 struct nft_table *table;
934 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200935 struct nft_base_chain *basechain = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200936 struct nlattr *ha[NFTA_HOOK_MAX + 1];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200937 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200938 int family = nfmsg->nfgen_family;
Patrick McHardy57de2a02014-01-09 18:42:31 +0000939 u8 policy = NF_ACCEPT;
Patrick McHardy96518512013-10-14 11:00:02 +0200940 u64 handle = 0;
Patrick McHardy115a60b2014-01-03 12:16:15 +0000941 unsigned int i;
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +0200942 struct nft_stats __percpu *stats;
Patrick McHardy96518512013-10-14 11:00:02 +0200943 int err;
944 bool create;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +0200945 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +0200946
947 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
948
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200949 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200950 if (IS_ERR(afi))
951 return PTR_ERR(afi);
952
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200953 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +0200954 if (IS_ERR(table))
955 return PTR_ERR(table);
956
Patrick McHardy96518512013-10-14 11:00:02 +0200957 chain = NULL;
958 name = nla[NFTA_CHAIN_NAME];
959
960 if (nla[NFTA_CHAIN_HANDLE]) {
961 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
962 chain = nf_tables_chain_lookup_byhandle(table, handle);
963 if (IS_ERR(chain))
964 return PTR_ERR(chain);
965 } else {
966 chain = nf_tables_chain_lookup(table, name);
967 if (IS_ERR(chain)) {
968 if (PTR_ERR(chain) != -ENOENT)
969 return PTR_ERR(chain);
970 chain = NULL;
971 }
972 }
973
Patrick McHardy57de2a02014-01-09 18:42:31 +0000974 if (nla[NFTA_CHAIN_POLICY]) {
975 if ((chain != NULL &&
976 !(chain->flags & NFT_BASE_CHAIN)) ||
977 nla[NFTA_CHAIN_HOOK] == NULL)
978 return -EOPNOTSUPP;
979
Pablo Neira Ayuso8f46df12014-01-10 15:11:25 +0100980 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
Patrick McHardy57de2a02014-01-09 18:42:31 +0000981 switch (policy) {
982 case NF_DROP:
983 case NF_ACCEPT:
984 break;
985 default:
986 return -EINVAL;
987 }
988 }
989
Patrick McHardy96518512013-10-14 11:00:02 +0200990 if (chain != NULL) {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +0200991 struct nft_stats *stats = NULL;
992 struct nft_trans *trans;
993
994 if (chain->flags & NFT_CHAIN_INACTIVE)
995 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200996 if (nlh->nlmsg_flags & NLM_F_EXCL)
997 return -EEXIST;
998 if (nlh->nlmsg_flags & NLM_F_REPLACE)
999 return -EOPNOTSUPP;
1000
1001 if (nla[NFTA_CHAIN_HANDLE] && name &&
1002 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1003 return -EEXIST;
1004
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001005 if (nla[NFTA_CHAIN_COUNTERS]) {
1006 if (!(chain->flags & NFT_BASE_CHAIN))
1007 return -EOPNOTSUPP;
1008
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001009 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1010 if (IS_ERR(stats))
1011 return PTR_ERR(stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001012 }
1013
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001014 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1015 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1016 sizeof(struct nft_trans_chain));
1017 if (trans == NULL)
1018 return -ENOMEM;
1019
1020 nft_trans_chain_stats(trans) = stats;
1021 nft_trans_chain_update(trans) = true;
1022
Patrick McHardy4401a862014-01-09 18:42:32 +00001023 if (nla[NFTA_CHAIN_POLICY])
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001024 nft_trans_chain_policy(trans) = policy;
1025 else
1026 nft_trans_chain_policy(trans) = -1;
Patrick McHardy4401a862014-01-09 18:42:32 +00001027
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001028 if (nla[NFTA_CHAIN_HANDLE] && name) {
1029 nla_strlcpy(nft_trans_chain_name(trans), name,
1030 NFT_CHAIN_MAXNAMELEN);
1031 }
1032 list_add_tail(&trans->list, &net->nft.commit_list);
1033 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001034 }
1035
Patrick McHardy75820672014-01-09 18:42:33 +00001036 if (table->use == UINT_MAX)
1037 return -EOVERFLOW;
1038
Patrick McHardy96518512013-10-14 11:00:02 +02001039 if (nla[NFTA_CHAIN_HOOK]) {
Patrick McHardy2a37d752014-01-09 18:42:37 +00001040 const struct nf_chain_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001041 struct nf_hook_ops *ops;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001042 nf_hookfn *hookfn;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001043 u32 hooknum, priority;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001044
Patrick McHardybaae3e62014-01-09 18:42:34 +00001045 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001046 if (nla[NFTA_CHAIN_TYPE]) {
1047 type = nf_tables_chain_type_lookup(afi,
1048 nla[NFTA_CHAIN_TYPE],
1049 create);
Patrick McHardy93b08062014-01-09 18:42:36 +00001050 if (IS_ERR(type))
1051 return PTR_ERR(type);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001052 }
Patrick McHardy96518512013-10-14 11:00:02 +02001053
1054 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1055 nft_hook_policy);
1056 if (err < 0)
1057 return err;
1058 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1059 ha[NFTA_HOOK_PRIORITY] == NULL)
1060 return -EINVAL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001061
1062 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1063 if (hooknum >= afi->nhooks)
Patrick McHardy96518512013-10-14 11:00:02 +02001064 return -EINVAL;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001065 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
Patrick McHardy96518512013-10-14 11:00:02 +02001066
Patrick McHardybaae3e62014-01-09 18:42:34 +00001067 if (!(type->hook_mask & (1 << hooknum)))
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001068 return -EOPNOTSUPP;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001069 if (!try_module_get(type->owner))
Patrick McHardybaae3e62014-01-09 18:42:34 +00001070 return -ENOENT;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001071 hookfn = type->hooks[hooknum];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001072
Patrick McHardy96518512013-10-14 11:00:02 +02001073 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1074 if (basechain == NULL)
1075 return -ENOMEM;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001076
Patrick McHardy4401a862014-01-09 18:42:32 +00001077 if (nla[NFTA_CHAIN_COUNTERS]) {
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001078 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1079 if (IS_ERR(stats)) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001080 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +00001081 kfree(basechain);
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001082 return PTR_ERR(stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001083 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001084 basechain->stats = stats;
Patrick McHardy4401a862014-01-09 18:42:32 +00001085 } else {
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001086 stats = alloc_percpu(struct nft_stats);
1087 if (IS_ERR(stats)) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001088 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +00001089 kfree(basechain);
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001090 return PTR_ERR(stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001091 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001092 rcu_assign_pointer(basechain->stats, stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001093 }
1094
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001095 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +02001096 chain = &basechain->chain;
1097
Patrick McHardy115a60b2014-01-03 12:16:15 +00001098 for (i = 0; i < afi->nops; i++) {
1099 ops = &basechain->ops[i];
1100 ops->pf = family;
1101 ops->owner = afi->owner;
1102 ops->hooknum = hooknum;
1103 ops->priority = priority;
1104 ops->priv = chain;
1105 ops->hook = afi->hooks[ops->hooknum];
1106 if (hookfn)
1107 ops->hook = hookfn;
1108 if (afi->hook_ops_init)
1109 afi->hook_ops_init(ops, i);
1110 }
Patrick McHardy96518512013-10-14 11:00:02 +02001111
1112 chain->flags |= NFT_BASE_CHAIN;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001113 basechain->policy = policy;
Patrick McHardy96518512013-10-14 11:00:02 +02001114 } else {
1115 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1116 if (chain == NULL)
1117 return -ENOMEM;
1118 }
1119
1120 INIT_LIST_HEAD(&chain->rules);
1121 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001122 chain->net = net;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +02001123 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +02001124 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1125
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001126 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1127 chain->flags & NFT_BASE_CHAIN) {
Patrick McHardy115a60b2014-01-03 12:16:15 +00001128 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001129 if (err < 0)
1130 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001131 }
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001132
1133 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1134 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1135 if (err < 0)
1136 goto err2;
1137
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001138 table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001139 list_add_tail_rcu(&chain->list, &table->chains);
Patrick McHardy96518512013-10-14 11:00:02 +02001140 return 0;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001141err2:
1142 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1143 chain->flags & NFT_BASE_CHAIN) {
1144 nf_unregister_hooks(nft_base_chain(chain)->ops,
1145 afi->nops);
1146 }
1147err1:
1148 nf_tables_chain_destroy(chain);
1149 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001150}
1151
1152static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1153 const struct nlmsghdr *nlh,
1154 const struct nlattr * const nla[])
1155{
1156 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001157 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001158 struct nft_table *table;
1159 struct nft_chain *chain;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001160 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001161 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001162 struct nft_ctx ctx;
1163 int err;
Patrick McHardy96518512013-10-14 11:00:02 +02001164
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001165 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001166 if (IS_ERR(afi))
1167 return PTR_ERR(afi);
1168
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001169 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001170 if (IS_ERR(table))
1171 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001172 if (table->flags & NFT_TABLE_INACTIVE)
1173 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001174
1175 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1176 if (IS_ERR(chain))
1177 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001178 if (chain->flags & NFT_CHAIN_INACTIVE)
1179 return -ENOENT;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001180 if (chain->use > 0)
Patrick McHardy96518512013-10-14 11:00:02 +02001181 return -EBUSY;
1182
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001183 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1184 err = nft_trans_chain_add(&ctx, NFT_MSG_DELCHAIN);
1185 if (err < 0)
1186 return err;
1187
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001188 table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001189 list_del_rcu(&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)
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001208 list_add_tail_rcu(&type->list, &nf_tables_expressions);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001209 else
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001210 list_add_rcu(&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);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001225 list_del_rcu(&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
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001478static int nf_tables_rule_notify(const struct nft_ctx *ctx,
Patrick McHardy96518512013-10-14 11:00:02 +02001479 const struct nft_rule *rule,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001480 int event)
Patrick McHardy96518512013-10-14 11:00:02 +02001481{
1482 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +02001483 int err;
1484
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001485 if (!ctx->report &&
1486 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +02001487 return 0;
1488
1489 err = -ENOBUFS;
1490 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1491 if (skb == NULL)
1492 goto err;
1493
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001494 err = nf_tables_fill_rule_info(skb, ctx->portid, ctx->seq, event, 0,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001495 ctx->afi->family, ctx->table,
1496 ctx->chain, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001497 if (err < 0) {
1498 kfree_skb(skb);
1499 goto err;
1500 }
1501
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001502 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1503 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001504err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001505 if (err < 0) {
1506 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1507 err);
1508 }
Patrick McHardy96518512013-10-14 11:00:02 +02001509 return err;
1510}
1511
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001512static inline bool
1513nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1514{
1515 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1516}
1517
1518static inline int gencursor_next(struct net *net)
1519{
1520 return net->nft.gencursor+1 == 1 ? 1 : 0;
1521}
1522
1523static inline int
1524nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1525{
1526 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1527}
1528
1529static inline void
1530nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1531{
1532 /* Now inactive, will be active in the future */
1533 rule->genmask = (1 << net->nft.gencursor);
1534}
1535
1536static inline void
1537nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1538{
1539 rule->genmask = (1 << gencursor_next(net));
1540}
1541
1542static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1543{
1544 rule->genmask = 0;
1545}
1546
Patrick McHardy96518512013-10-14 11:00:02 +02001547static int nf_tables_dump_rules(struct sk_buff *skb,
1548 struct netlink_callback *cb)
1549{
1550 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1551 const struct nft_af_info *afi;
1552 const struct nft_table *table;
1553 const struct nft_chain *chain;
1554 const struct nft_rule *rule;
1555 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001556 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001557 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001558 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1559 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
Patrick McHardy96518512013-10-14 11:00:02 +02001560
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001561 rcu_read_lock();
1562 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001563 if (family != NFPROTO_UNSPEC && family != afi->family)
1564 continue;
1565
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001566 list_for_each_entry_rcu(table, &afi->tables, list) {
1567 list_for_each_entry_rcu(chain, &table->chains, list) {
1568 list_for_each_entry_rcu(rule, &chain->rules, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001569 if (!nft_rule_is_active(net, rule))
1570 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001571 if (idx < s_idx)
1572 goto cont;
1573 if (idx > s_idx)
1574 memset(&cb->args[1], 0,
1575 sizeof(cb->args) - sizeof(cb->args[0]));
1576 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1577 cb->nlh->nlmsg_seq,
1578 NFT_MSG_NEWRULE,
1579 NLM_F_MULTI | NLM_F_APPEND,
1580 afi->family, table, chain, rule) < 0)
1581 goto done;
1582cont:
1583 idx++;
1584 }
1585 }
1586 }
1587 }
1588done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001589 rcu_read_unlock();
1590
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001591 /* Invalidate this dump, a transition to the new generation happened */
1592 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1593 return -EBUSY;
1594
Patrick McHardy96518512013-10-14 11:00:02 +02001595 cb->args[0] = idx;
1596 return skb->len;
1597}
1598
1599static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1600 const struct nlmsghdr *nlh,
1601 const struct nlattr * const nla[])
1602{
1603 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1604 const struct nft_af_info *afi;
1605 const struct nft_table *table;
1606 const struct nft_chain *chain;
1607 const struct nft_rule *rule;
1608 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001609 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001610 int family = nfmsg->nfgen_family;
1611 int err;
1612
1613 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1614 struct netlink_dump_control c = {
1615 .dump = nf_tables_dump_rules,
1616 };
1617 return netlink_dump_start(nlsk, skb, nlh, &c);
1618 }
1619
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001620 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001621 if (IS_ERR(afi))
1622 return PTR_ERR(afi);
1623
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001624 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001625 if (IS_ERR(table))
1626 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001627 if (table->flags & NFT_TABLE_INACTIVE)
1628 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001629
1630 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1631 if (IS_ERR(chain))
1632 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001633 if (chain->flags & NFT_CHAIN_INACTIVE)
1634 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001635
1636 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1637 if (IS_ERR(rule))
1638 return PTR_ERR(rule);
1639
1640 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1641 if (!skb2)
1642 return -ENOMEM;
1643
1644 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1645 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1646 family, table, chain, rule);
1647 if (err < 0)
1648 goto err;
1649
1650 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1651
1652err:
1653 kfree_skb(skb2);
1654 return err;
1655}
1656
Patrick McHardy62472bc2014-03-07 19:08:30 +01001657static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1658 struct nft_rule *rule)
Patrick McHardy96518512013-10-14 11:00:02 +02001659{
Patrick McHardy96518512013-10-14 11:00:02 +02001660 struct nft_expr *expr;
1661
1662 /*
1663 * Careful: some expressions might not be initialized in case this
1664 * is called on error from nf_tables_newrule().
1665 */
1666 expr = nft_expr_first(rule);
1667 while (expr->ops && expr != nft_expr_last(rule)) {
Patrick McHardy62472bc2014-03-07 19:08:30 +01001668 nf_tables_expr_destroy(ctx, expr);
Patrick McHardy96518512013-10-14 11:00:02 +02001669 expr = nft_expr_next(expr);
1670 }
1671 kfree(rule);
1672}
1673
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001674static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001675 struct nft_rule *rule)
1676{
1677 struct nft_trans *trans;
1678
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001679 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001680 if (trans == NULL)
1681 return NULL;
1682
1683 nft_trans_rule(trans) = rule;
1684 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
1685
1686 return trans;
1687}
1688
Patrick McHardy96518512013-10-14 11:00:02 +02001689#define NFT_RULE_MAXEXPRS 128
1690
1691static struct nft_expr_info *info;
1692
1693static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1694 const struct nlmsghdr *nlh,
1695 const struct nlattr * const nla[])
1696{
1697 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001698 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001699 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001700 struct nft_table *table;
1701 struct nft_chain *chain;
1702 struct nft_rule *rule, *old_rule = NULL;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001703 struct nft_trans *trans = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001704 struct nft_expr *expr;
1705 struct nft_ctx ctx;
1706 struct nlattr *tmp;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001707 unsigned int size, i, n, ulen = 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001708 int err, rem;
1709 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02001710 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02001711
1712 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1713
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001714 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02001715 if (IS_ERR(afi))
1716 return PTR_ERR(afi);
1717
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001718 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001719 if (IS_ERR(table))
1720 return PTR_ERR(table);
1721
1722 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1723 if (IS_ERR(chain))
1724 return PTR_ERR(chain);
1725
1726 if (nla[NFTA_RULE_HANDLE]) {
1727 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1728 rule = __nf_tables_rule_lookup(chain, handle);
1729 if (IS_ERR(rule))
1730 return PTR_ERR(rule);
1731
1732 if (nlh->nlmsg_flags & NLM_F_EXCL)
1733 return -EEXIST;
1734 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1735 old_rule = rule;
1736 else
1737 return -EOPNOTSUPP;
1738 } else {
1739 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1740 return -EINVAL;
1741 handle = nf_tables_alloc_handle(table);
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +02001742
1743 if (chain->use == UINT_MAX)
1744 return -EOVERFLOW;
Patrick McHardy96518512013-10-14 11:00:02 +02001745 }
1746
Eric Leblond5e948462013-10-10 13:41:44 +02001747 if (nla[NFTA_RULE_POSITION]) {
1748 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1749 return -EOPNOTSUPP;
1750
1751 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1752 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1753 if (IS_ERR(old_rule))
1754 return PTR_ERR(old_rule);
1755 }
1756
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001757 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1758
Patrick McHardy96518512013-10-14 11:00:02 +02001759 n = 0;
1760 size = 0;
1761 if (nla[NFTA_RULE_EXPRESSIONS]) {
1762 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1763 err = -EINVAL;
1764 if (nla_type(tmp) != NFTA_LIST_ELEM)
1765 goto err1;
1766 if (n == NFT_RULE_MAXEXPRS)
1767 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001768 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02001769 if (err < 0)
1770 goto err1;
1771 size += info[n].ops->size;
1772 n++;
1773 }
1774 }
1775
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001776 if (nla[NFTA_RULE_USERDATA])
1777 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1778
Patrick McHardy96518512013-10-14 11:00:02 +02001779 err = -ENOMEM;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001780 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001781 if (rule == NULL)
1782 goto err1;
1783
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001784 nft_rule_activate_next(net, rule);
1785
Patrick McHardy96518512013-10-14 11:00:02 +02001786 rule->handle = handle;
1787 rule->dlen = size;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001788 rule->ulen = ulen;
1789
1790 if (ulen)
1791 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
Patrick McHardy96518512013-10-14 11:00:02 +02001792
Patrick McHardy96518512013-10-14 11:00:02 +02001793 expr = nft_expr_first(rule);
1794 for (i = 0; i < n; i++) {
1795 err = nf_tables_newexpr(&ctx, &info[i], expr);
1796 if (err < 0)
1797 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001798 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001799 expr = nft_expr_next(expr);
1800 }
1801
Patrick McHardy96518512013-10-14 11:00:02 +02001802 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001803 if (nft_rule_is_active_next(net, old_rule)) {
Pablo Neira Ayusoac904ac2014-06-10 10:53:03 +02001804 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001805 old_rule);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001806 if (trans == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001807 err = -ENOMEM;
1808 goto err2;
1809 }
1810 nft_rule_disactivate_next(net, old_rule);
Pablo Neira Ayusoac34b862014-06-10 10:53:02 +02001811 chain->use--;
Pablo Neira Ayuso5bc5c302014-06-10 10:53:00 +02001812 list_add_tail_rcu(&rule->list, &old_rule->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001813 } else {
1814 err = -ENOENT;
1815 goto err2;
1816 }
Patrick McHardy96518512013-10-14 11:00:02 +02001817 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02001818 if (old_rule)
1819 list_add_rcu(&rule->list, &old_rule->list);
1820 else
1821 list_add_tail_rcu(&rule->list, &chain->rules);
1822 else {
1823 if (old_rule)
1824 list_add_tail_rcu(&rule->list, &old_rule->list);
1825 else
1826 list_add_rcu(&rule->list, &chain->rules);
1827 }
Patrick McHardy96518512013-10-14 11:00:02 +02001828
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001829 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001830 err = -ENOMEM;
1831 goto err3;
1832 }
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001833 chain->use++;
Patrick McHardy96518512013-10-14 11:00:02 +02001834 return 0;
1835
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001836err3:
1837 list_del_rcu(&rule->list);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001838 if (trans) {
1839 list_del_rcu(&nft_trans_rule(trans)->list);
1840 nft_rule_clear(net, nft_trans_rule(trans));
1841 nft_trans_destroy(trans);
Pablo Neira Ayusoac34b862014-06-10 10:53:02 +02001842 chain->use++;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001843 }
Patrick McHardy96518512013-10-14 11:00:02 +02001844err2:
Patrick McHardy62472bc2014-03-07 19:08:30 +01001845 nf_tables_rule_destroy(&ctx, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001846err1:
1847 for (i = 0; i < n; i++) {
1848 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001849 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001850 }
1851 return err;
1852}
1853
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001854static int
1855nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1856{
1857 /* You cannot delete the same rule twice */
1858 if (nft_rule_is_active_next(ctx->net, rule)) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001859 if (nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule) == NULL)
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001860 return -ENOMEM;
1861 nft_rule_disactivate_next(ctx->net, rule);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001862 ctx->chain->use--;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001863 return 0;
1864 }
1865 return -ENOENT;
1866}
1867
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001868static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1869{
1870 struct nft_rule *rule;
1871 int err;
1872
1873 list_for_each_entry(rule, &ctx->chain->rules, list) {
1874 err = nf_tables_delrule_one(ctx, rule);
1875 if (err < 0)
1876 return err;
1877 }
1878 return 0;
1879}
1880
Patrick McHardy96518512013-10-14 11:00:02 +02001881static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1882 const struct nlmsghdr *nlh,
1883 const struct nlattr * const nla[])
1884{
1885 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001886 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001887 struct net *net = sock_net(skb->sk);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001888 struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001889 struct nft_chain *chain = NULL;
1890 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001891 int family = nfmsg->nfgen_family, err = 0;
1892 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001893
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001894 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001895 if (IS_ERR(afi))
1896 return PTR_ERR(afi);
1897
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001898 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001899 if (IS_ERR(table))
1900 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001901 if (table->flags & NFT_TABLE_INACTIVE)
1902 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001903
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001904 if (nla[NFTA_RULE_CHAIN]) {
1905 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1906 if (IS_ERR(chain))
1907 return PTR_ERR(chain);
1908 }
Patrick McHardy96518512013-10-14 11:00:02 +02001909
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001910 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1911
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001912 if (chain) {
1913 if (nla[NFTA_RULE_HANDLE]) {
1914 rule = nf_tables_rule_lookup(chain,
1915 nla[NFTA_RULE_HANDLE]);
1916 if (IS_ERR(rule))
1917 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001918
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001919 err = nf_tables_delrule_one(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01001920 } else {
1921 err = nf_table_delrule_by_chain(&ctx);
1922 }
1923 } else {
1924 list_for_each_entry(chain, &table->chains, list) {
1925 ctx.chain = chain;
1926 err = nf_table_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001927 if (err < 0)
1928 break;
Patrick McHardy96518512013-10-14 11:00:02 +02001929 }
1930 }
1931
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001932 return err;
1933}
1934
Patrick McHardy20a69342013-10-11 12:06:22 +02001935/*
1936 * Sets
1937 */
1938
1939static LIST_HEAD(nf_tables_set_ops);
1940
1941int nft_register_set(struct nft_set_ops *ops)
1942{
1943 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001944 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
Patrick McHardy20a69342013-10-11 12:06:22 +02001945 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1946 return 0;
1947}
1948EXPORT_SYMBOL_GPL(nft_register_set);
1949
1950void nft_unregister_set(struct nft_set_ops *ops)
1951{
1952 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001953 list_del_rcu(&ops->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02001954 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1955}
1956EXPORT_SYMBOL_GPL(nft_unregister_set);
1957
Patrick McHardyc50b9602014-03-28 10:19:47 +00001958/*
1959 * Select a set implementation based on the data characteristics and the
1960 * given policy. The total memory use might not be known if no size is
1961 * given, in that case the amount of memory per element is used.
1962 */
1963static const struct nft_set_ops *
1964nft_select_set_ops(const struct nlattr * const nla[],
1965 const struct nft_set_desc *desc,
1966 enum nft_set_policies policy)
Patrick McHardy20a69342013-10-11 12:06:22 +02001967{
Patrick McHardyc50b9602014-03-28 10:19:47 +00001968 const struct nft_set_ops *ops, *bops;
1969 struct nft_set_estimate est, best;
Patrick McHardy20a69342013-10-11 12:06:22 +02001970 u32 features;
1971
1972#ifdef CONFIG_MODULES
1973 if (list_empty(&nf_tables_set_ops)) {
1974 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1975 request_module("nft-set");
1976 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1977 if (!list_empty(&nf_tables_set_ops))
1978 return ERR_PTR(-EAGAIN);
1979 }
1980#endif
1981 features = 0;
1982 if (nla[NFTA_SET_FLAGS] != NULL) {
1983 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1984 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1985 }
1986
Patrick McHardyc50b9602014-03-28 10:19:47 +00001987 bops = NULL;
1988 best.size = ~0;
1989 best.class = ~0;
1990
Patrick McHardy20a69342013-10-11 12:06:22 +02001991 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1992 if ((ops->features & features) != features)
1993 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00001994 if (!ops->estimate(desc, features, &est))
1995 continue;
1996
1997 switch (policy) {
1998 case NFT_SET_POL_PERFORMANCE:
1999 if (est.class < best.class)
2000 break;
2001 if (est.class == best.class && est.size < best.size)
2002 break;
2003 continue;
2004 case NFT_SET_POL_MEMORY:
2005 if (est.size < best.size)
2006 break;
2007 if (est.size == best.size && est.class < best.class)
2008 break;
2009 continue;
2010 default:
2011 break;
2012 }
2013
Patrick McHardy20a69342013-10-11 12:06:22 +02002014 if (!try_module_get(ops->owner))
2015 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002016 if (bops != NULL)
2017 module_put(bops->owner);
2018
2019 bops = ops;
2020 best = est;
Patrick McHardy20a69342013-10-11 12:06:22 +02002021 }
2022
Patrick McHardyc50b9602014-03-28 10:19:47 +00002023 if (bops != NULL)
2024 return bops;
2025
Patrick McHardy20a69342013-10-11 12:06:22 +02002026 return ERR_PTR(-EOPNOTSUPP);
2027}
2028
2029static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2030 [NFTA_SET_TABLE] = { .type = NLA_STRING },
Pablo Neira Ayusoa9bdd832014-03-24 15:10:37 +01002031 [NFTA_SET_NAME] = { .type = NLA_STRING,
2032 .len = IFNAMSIZ - 1 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002033 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2034 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2035 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2036 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2037 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002038 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2039 [NFTA_SET_DESC] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002040 [NFTA_SET_ID] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002041};
2042
2043static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2044 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002045};
2046
2047static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2048 const struct sk_buff *skb,
2049 const struct nlmsghdr *nlh,
2050 const struct nlattr * const nla[])
2051{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002052 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002053 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002054 struct nft_af_info *afi = NULL;
2055 struct nft_table *table = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02002056
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002057 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2058 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2059 if (IS_ERR(afi))
2060 return PTR_ERR(afi);
2061 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002062
2063 if (nla[NFTA_SET_TABLE] != NULL) {
Patrick McHardyec2c9932014-02-05 15:03:35 +00002064 if (afi == NULL)
2065 return -EAFNOSUPPORT;
2066
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002067 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002068 if (IS_ERR(table))
2069 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002070 if (table->flags & NFT_TABLE_INACTIVE)
2071 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002072 }
2073
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002074 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002075 return 0;
2076}
2077
2078struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2079 const struct nlattr *nla)
2080{
2081 struct nft_set *set;
2082
2083 if (nla == NULL)
2084 return ERR_PTR(-EINVAL);
2085
2086 list_for_each_entry(set, &table->sets, list) {
2087 if (!nla_strcmp(nla, set->name))
2088 return set;
2089 }
2090 return ERR_PTR(-ENOENT);
2091}
2092
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002093struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2094 const struct nlattr *nla)
2095{
2096 struct nft_trans *trans;
2097 u32 id = ntohl(nla_get_be32(nla));
2098
2099 list_for_each_entry(trans, &net->nft.commit_list, list) {
2100 if (trans->msg_type == NFT_MSG_NEWSET &&
2101 id == nft_trans_set_id(trans))
2102 return nft_trans_set(trans);
2103 }
2104 return ERR_PTR(-ENOENT);
2105}
2106
Patrick McHardy20a69342013-10-11 12:06:22 +02002107static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2108 const char *name)
2109{
2110 const struct nft_set *i;
2111 const char *p;
2112 unsigned long *inuse;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002113 unsigned int n = 0, min = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002114
2115 p = strnchr(name, IFNAMSIZ, '%');
2116 if (p != NULL) {
2117 if (p[1] != 'd' || strchr(p + 2, '%'))
2118 return -EINVAL;
2119
2120 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2121 if (inuse == NULL)
2122 return -ENOMEM;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002123cont:
Patrick McHardy20a69342013-10-11 12:06:22 +02002124 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01002125 int tmp;
2126
2127 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02002128 continue;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002129 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02002130 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002131
Patrick McHardy60eb1892014-03-07 12:34:05 +01002132 set_bit(tmp - min, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02002133 }
2134
Patrick McHardy53b70282014-02-05 12:26:22 +01002135 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
Patrick McHardy60eb1892014-03-07 12:34:05 +01002136 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2137 min += BITS_PER_BYTE * PAGE_SIZE;
2138 memset(inuse, 0, PAGE_SIZE);
2139 goto cont;
2140 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002141 free_page((unsigned long)inuse);
2142 }
2143
Patrick McHardy60eb1892014-03-07 12:34:05 +01002144 snprintf(set->name, sizeof(set->name), name, min + n);
Patrick McHardy20a69342013-10-11 12:06:22 +02002145 list_for_each_entry(i, &ctx->table->sets, list) {
2146 if (!strcmp(set->name, i->name))
2147 return -ENFILE;
2148 }
2149 return 0;
2150}
2151
2152static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2153 const struct nft_set *set, u16 event, u16 flags)
2154{
2155 struct nfgenmsg *nfmsg;
2156 struct nlmsghdr *nlh;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002157 struct nlattr *desc;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002158 u32 portid = ctx->portid;
2159 u32 seq = ctx->seq;
Patrick McHardy20a69342013-10-11 12:06:22 +02002160
2161 event |= NFNL_SUBSYS_NFTABLES << 8;
2162 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2163 flags);
2164 if (nlh == NULL)
2165 goto nla_put_failure;
2166
2167 nfmsg = nlmsg_data(nlh);
2168 nfmsg->nfgen_family = ctx->afi->family;
2169 nfmsg->version = NFNETLINK_V0;
2170 nfmsg->res_id = 0;
2171
2172 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2173 goto nla_put_failure;
2174 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2175 goto nla_put_failure;
2176 if (set->flags != 0)
2177 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2178 goto nla_put_failure;
2179
2180 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2181 goto nla_put_failure;
2182 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2183 goto nla_put_failure;
2184 if (set->flags & NFT_SET_MAP) {
2185 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2186 goto nla_put_failure;
2187 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2188 goto nla_put_failure;
2189 }
2190
Patrick McHardyc50b9602014-03-28 10:19:47 +00002191 desc = nla_nest_start(skb, NFTA_SET_DESC);
2192 if (desc == NULL)
2193 goto nla_put_failure;
2194 if (set->size &&
2195 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2196 goto nla_put_failure;
2197 nla_nest_end(skb, desc);
2198
Patrick McHardy20a69342013-10-11 12:06:22 +02002199 return nlmsg_end(skb, nlh);
2200
2201nla_put_failure:
2202 nlmsg_trim(skb, nlh);
2203 return -1;
2204}
2205
2206static int nf_tables_set_notify(const struct nft_ctx *ctx,
2207 const struct nft_set *set,
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002208 int event, gfp_t gfp_flags)
Patrick McHardy20a69342013-10-11 12:06:22 +02002209{
2210 struct sk_buff *skb;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002211 u32 portid = ctx->portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002212 int err;
2213
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002214 if (!ctx->report &&
2215 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002216 return 0;
2217
2218 err = -ENOBUFS;
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002219 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
Patrick McHardy20a69342013-10-11 12:06:22 +02002220 if (skb == NULL)
2221 goto err;
2222
2223 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2224 if (err < 0) {
2225 kfree_skb(skb);
2226 goto err;
2227 }
2228
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002229 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002230 ctx->report, gfp_flags);
Patrick McHardy20a69342013-10-11 12:06:22 +02002231err:
2232 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002233 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002234 return err;
2235}
2236
2237static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2238 struct netlink_callback *cb)
2239{
2240 const struct nft_set *set;
2241 unsigned int idx = 0, s_idx = cb->args[0];
2242
2243 if (cb->args[1])
2244 return skb->len;
2245
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002246 rcu_read_lock();
2247 list_for_each_entry_rcu(set, &ctx->table->sets, list) {
Patrick McHardy20a69342013-10-11 12:06:22 +02002248 if (idx < s_idx)
2249 goto cont;
2250 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2251 NLM_F_MULTI) < 0) {
2252 cb->args[0] = idx;
2253 goto done;
2254 }
2255cont:
2256 idx++;
2257 }
2258 cb->args[1] = 1;
2259done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002260 rcu_read_unlock();
Patrick McHardy20a69342013-10-11 12:06:22 +02002261 return skb->len;
2262}
2263
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002264static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2265 struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002266{
2267 const struct nft_set *set;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002268 unsigned int idx, s_idx = cb->args[0];
Patrick McHardy20a69342013-10-11 12:06:22 +02002269 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2270
2271 if (cb->args[1])
2272 return skb->len;
2273
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002274 rcu_read_lock();
2275 list_for_each_entry_rcu(table, &ctx->afi->tables, list) {
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002276 if (cur_table) {
2277 if (cur_table != table)
2278 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02002279
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002280 cur_table = NULL;
2281 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002282 ctx->table = table;
Pablo Neira Ayusoe38195b2013-12-24 18:32:35 +01002283 idx = 0;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002284 list_for_each_entry_rcu(set, &ctx->table->sets, list) {
Patrick McHardy20a69342013-10-11 12:06:22 +02002285 if (idx < s_idx)
2286 goto cont;
2287 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2288 NLM_F_MULTI) < 0) {
2289 cb->args[0] = idx;
2290 cb->args[2] = (unsigned long) table;
2291 goto done;
2292 }
2293cont:
2294 idx++;
2295 }
2296 }
2297 cb->args[1] = 1;
2298done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002299 rcu_read_unlock();
Patrick McHardy20a69342013-10-11 12:06:22 +02002300 return skb->len;
2301}
2302
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002303static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2304 struct netlink_callback *cb)
2305{
2306 const struct nft_set *set;
2307 unsigned int idx, s_idx = cb->args[0];
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002308 struct nft_af_info *afi;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002309 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2310 struct net *net = sock_net(skb->sk);
2311 int cur_family = cb->args[3];
2312
2313 if (cb->args[1])
2314 return skb->len;
2315
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002316 rcu_read_lock();
2317 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002318 if (cur_family) {
2319 if (afi->family != cur_family)
2320 continue;
2321
2322 cur_family = 0;
2323 }
2324
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002325 list_for_each_entry_rcu(table, &afi->tables, list) {
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002326 if (cur_table) {
2327 if (cur_table != table)
2328 continue;
2329
2330 cur_table = NULL;
2331 }
2332
2333 ctx->table = table;
2334 ctx->afi = afi;
2335 idx = 0;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002336 list_for_each_entry_rcu(set, &ctx->table->sets, list) {
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002337 if (idx < s_idx)
2338 goto cont;
2339 if (nf_tables_fill_set(skb, ctx, set,
2340 NFT_MSG_NEWSET,
2341 NLM_F_MULTI) < 0) {
2342 cb->args[0] = idx;
2343 cb->args[2] = (unsigned long) table;
2344 cb->args[3] = afi->family;
2345 goto done;
2346 }
2347cont:
2348 idx++;
2349 }
2350 if (s_idx)
2351 s_idx = 0;
2352 }
2353 }
2354 cb->args[1] = 1;
2355done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002356 rcu_read_unlock();
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002357 return skb->len;
2358}
2359
Patrick McHardy20a69342013-10-11 12:06:22 +02002360static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2361{
2362 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2363 struct nlattr *nla[NFTA_SET_MAX + 1];
2364 struct nft_ctx ctx;
2365 int err, ret;
2366
2367 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2368 nft_set_policy);
2369 if (err < 0)
2370 return err;
2371
2372 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2373 if (err < 0)
2374 return err;
2375
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002376 if (ctx.table == NULL) {
2377 if (ctx.afi == NULL)
2378 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2379 else
2380 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2381 } else
Patrick McHardy20a69342013-10-11 12:06:22 +02002382 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2383
2384 return ret;
2385}
2386
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002387#define NFT_SET_INACTIVE (1 << 15) /* Internal set flag */
2388
Patrick McHardy20a69342013-10-11 12:06:22 +02002389static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2390 const struct nlmsghdr *nlh,
2391 const struct nlattr * const nla[])
2392{
2393 const struct nft_set *set;
2394 struct nft_ctx ctx;
2395 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002396 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002397 int err;
2398
2399 /* Verify existance before starting dump */
2400 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2401 if (err < 0)
2402 return err;
2403
2404 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2405 struct netlink_dump_control c = {
2406 .dump = nf_tables_dump_sets,
2407 };
2408 return netlink_dump_start(nlsk, skb, nlh, &c);
2409 }
2410
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002411 /* Only accept unspec with dump */
2412 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2413 return -EAFNOSUPPORT;
2414
Patrick McHardy20a69342013-10-11 12:06:22 +02002415 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2416 if (IS_ERR(set))
2417 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002418 if (set->flags & NFT_SET_INACTIVE)
2419 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002420
2421 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2422 if (skb2 == NULL)
2423 return -ENOMEM;
2424
2425 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2426 if (err < 0)
2427 goto err;
2428
2429 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2430
2431err:
2432 kfree_skb(skb2);
2433 return err;
2434}
2435
Patrick McHardyc50b9602014-03-28 10:19:47 +00002436static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2437 struct nft_set_desc *desc,
2438 const struct nlattr *nla)
2439{
2440 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2441 int err;
2442
2443 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2444 if (err < 0)
2445 return err;
2446
2447 if (da[NFTA_SET_DESC_SIZE] != NULL)
2448 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2449
2450 return 0;
2451}
2452
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002453static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
2454 struct nft_set *set)
2455{
2456 struct nft_trans *trans;
2457
2458 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
2459 if (trans == NULL)
2460 return -ENOMEM;
2461
2462 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
2463 nft_trans_set_id(trans) =
2464 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
2465 set->flags |= NFT_SET_INACTIVE;
2466 }
2467 nft_trans_set(trans) = set;
2468 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
2469
2470 return 0;
2471}
2472
Patrick McHardy20a69342013-10-11 12:06:22 +02002473static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2474 const struct nlmsghdr *nlh,
2475 const struct nlattr * const nla[])
2476{
2477 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2478 const struct nft_set_ops *ops;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002479 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002480 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002481 struct nft_table *table;
2482 struct nft_set *set;
2483 struct nft_ctx ctx;
2484 char name[IFNAMSIZ];
2485 unsigned int size;
2486 bool create;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002487 u32 ktype, dtype, flags, policy;
2488 struct nft_set_desc desc;
Patrick McHardy20a69342013-10-11 12:06:22 +02002489 int err;
2490
2491 if (nla[NFTA_SET_TABLE] == NULL ||
2492 nla[NFTA_SET_NAME] == NULL ||
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002493 nla[NFTA_SET_KEY_LEN] == NULL ||
2494 nla[NFTA_SET_ID] == NULL)
Patrick McHardy20a69342013-10-11 12:06:22 +02002495 return -EINVAL;
2496
Patrick McHardyc50b9602014-03-28 10:19:47 +00002497 memset(&desc, 0, sizeof(desc));
2498
Patrick McHardy20a69342013-10-11 12:06:22 +02002499 ktype = NFT_DATA_VALUE;
2500 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2501 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2502 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2503 return -EINVAL;
2504 }
2505
Patrick McHardyc50b9602014-03-28 10:19:47 +00002506 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2507 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002508 return -EINVAL;
2509
2510 flags = 0;
2511 if (nla[NFTA_SET_FLAGS] != NULL) {
2512 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2513 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2514 NFT_SET_INTERVAL | NFT_SET_MAP))
2515 return -EINVAL;
2516 }
2517
2518 dtype = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002519 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2520 if (!(flags & NFT_SET_MAP))
2521 return -EINVAL;
2522
2523 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2524 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2525 dtype != NFT_DATA_VERDICT)
2526 return -EINVAL;
2527
2528 if (dtype != NFT_DATA_VERDICT) {
2529 if (nla[NFTA_SET_DATA_LEN] == NULL)
2530 return -EINVAL;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002531 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2532 if (desc.dlen == 0 ||
2533 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002534 return -EINVAL;
2535 } else
Patrick McHardyc50b9602014-03-28 10:19:47 +00002536 desc.dlen = sizeof(struct nft_data);
Patrick McHardy20a69342013-10-11 12:06:22 +02002537 } else if (flags & NFT_SET_MAP)
2538 return -EINVAL;
2539
Patrick McHardyc50b9602014-03-28 10:19:47 +00002540 policy = NFT_SET_POL_PERFORMANCE;
2541 if (nla[NFTA_SET_POLICY] != NULL)
2542 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2543
2544 if (nla[NFTA_SET_DESC] != NULL) {
2545 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2546 if (err < 0)
2547 return err;
2548 }
2549
Patrick McHardy20a69342013-10-11 12:06:22 +02002550 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2551
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002552 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002553 if (IS_ERR(afi))
2554 return PTR_ERR(afi);
2555
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002556 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002557 if (IS_ERR(table))
2558 return PTR_ERR(table);
2559
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002560 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002561
2562 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2563 if (IS_ERR(set)) {
2564 if (PTR_ERR(set) != -ENOENT)
2565 return PTR_ERR(set);
2566 set = NULL;
2567 }
2568
2569 if (set != NULL) {
2570 if (nlh->nlmsg_flags & NLM_F_EXCL)
2571 return -EEXIST;
2572 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2573 return -EOPNOTSUPP;
2574 return 0;
2575 }
2576
2577 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2578 return -ENOENT;
2579
Patrick McHardyc50b9602014-03-28 10:19:47 +00002580 ops = nft_select_set_ops(nla, &desc, policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002581 if (IS_ERR(ops))
2582 return PTR_ERR(ops);
2583
2584 size = 0;
2585 if (ops->privsize != NULL)
2586 size = ops->privsize(nla);
2587
2588 err = -ENOMEM;
2589 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2590 if (set == NULL)
2591 goto err1;
2592
2593 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2594 err = nf_tables_set_alloc_name(&ctx, set, name);
2595 if (err < 0)
2596 goto err2;
2597
2598 INIT_LIST_HEAD(&set->bindings);
2599 set->ops = ops;
2600 set->ktype = ktype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002601 set->klen = desc.klen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002602 set->dtype = dtype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002603 set->dlen = desc.dlen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002604 set->flags = flags;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002605 set->size = desc.size;
Patrick McHardy20a69342013-10-11 12:06:22 +02002606
Patrick McHardyc50b9602014-03-28 10:19:47 +00002607 err = ops->init(set, &desc, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002608 if (err < 0)
2609 goto err2;
2610
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002611 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002612 if (err < 0)
2613 goto err2;
2614
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002615 list_add_tail_rcu(&set->list, &table->sets);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02002616 table->use++;
Patrick McHardy20a69342013-10-11 12:06:22 +02002617 return 0;
2618
2619err2:
2620 kfree(set);
2621err1:
2622 module_put(ops->owner);
2623 return err;
2624}
2625
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002626static void nft_set_destroy(struct nft_set *set)
2627{
2628 set->ops->destroy(set);
2629 module_put(set->ops->owner);
2630 kfree(set);
2631}
2632
Patrick McHardy20a69342013-10-11 12:06:22 +02002633static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2634{
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002635 list_del_rcu(&set->list);
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002636 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002637 nft_set_destroy(set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002638}
2639
2640static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2641 const struct nlmsghdr *nlh,
2642 const struct nlattr * const nla[])
2643{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002644 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002645 struct nft_set *set;
2646 struct nft_ctx ctx;
2647 int err;
2648
Patrick McHardyec2c9932014-02-05 15:03:35 +00002649 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2650 return -EAFNOSUPPORT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002651 if (nla[NFTA_SET_TABLE] == NULL)
2652 return -EINVAL;
2653
2654 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2655 if (err < 0)
2656 return err;
2657
2658 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2659 if (IS_ERR(set))
2660 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002661 if (set->flags & NFT_SET_INACTIVE)
2662 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002663 if (!list_empty(&set->bindings))
2664 return -EBUSY;
2665
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002666 err = nft_trans_set_add(&ctx, NFT_MSG_DELSET, set);
2667 if (err < 0)
2668 return err;
2669
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002670 list_del_rcu(&set->list);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02002671 ctx.table->use--;
Patrick McHardy20a69342013-10-11 12:06:22 +02002672 return 0;
2673}
2674
2675static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2676 const struct nft_set *set,
2677 const struct nft_set_iter *iter,
2678 const struct nft_set_elem *elem)
2679{
2680 enum nft_registers dreg;
2681
2682 dreg = nft_type_to_reg(set->dtype);
Pablo Neira Ayuso2ee0d3c2013-12-28 00:59:38 +01002683 return nft_validate_data_load(ctx, dreg, &elem->data,
2684 set->dtype == NFT_DATA_VERDICT ?
2685 NFT_DATA_VERDICT : NFT_DATA_VALUE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002686}
2687
2688int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2689 struct nft_set_binding *binding)
2690{
2691 struct nft_set_binding *i;
2692 struct nft_set_iter iter;
2693
2694 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2695 return -EBUSY;
2696
2697 if (set->flags & NFT_SET_MAP) {
2698 /* If the set is already bound to the same chain all
2699 * jumps are already validated for that chain.
2700 */
2701 list_for_each_entry(i, &set->bindings, list) {
2702 if (i->chain == binding->chain)
2703 goto bind;
2704 }
2705
2706 iter.skip = 0;
2707 iter.count = 0;
2708 iter.err = 0;
2709 iter.fn = nf_tables_bind_check_setelem;
2710
2711 set->ops->walk(ctx, set, &iter);
2712 if (iter.err < 0) {
2713 /* Destroy anonymous sets if binding fails */
2714 if (set->flags & NFT_SET_ANONYMOUS)
2715 nf_tables_set_destroy(ctx, set);
2716
2717 return iter.err;
2718 }
2719 }
2720bind:
2721 binding->chain = ctx->chain;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002722 list_add_tail_rcu(&binding->list, &set->bindings);
Patrick McHardy20a69342013-10-11 12:06:22 +02002723 return 0;
2724}
2725
2726void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2727 struct nft_set_binding *binding)
2728{
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002729 list_del_rcu(&binding->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002730
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002731 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2732 !(set->flags & NFT_SET_INACTIVE))
Patrick McHardy20a69342013-10-11 12:06:22 +02002733 nf_tables_set_destroy(ctx, set);
2734}
2735
2736/*
2737 * Set elements
2738 */
2739
2740static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2741 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2742 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2743 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2744};
2745
2746static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2747 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2748 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2749 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002750 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002751};
2752
2753static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2754 const struct sk_buff *skb,
2755 const struct nlmsghdr *nlh,
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002756 const struct nlattr * const nla[],
2757 bool trans)
Patrick McHardy20a69342013-10-11 12:06:22 +02002758{
2759 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002760 struct nft_af_info *afi;
2761 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002762 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002763
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002764 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002765 if (IS_ERR(afi))
2766 return PTR_ERR(afi);
2767
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002768 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002769 if (IS_ERR(table))
2770 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002771 if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2772 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002773
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002774 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002775 return 0;
2776}
2777
2778static int nf_tables_fill_setelem(struct sk_buff *skb,
2779 const struct nft_set *set,
2780 const struct nft_set_elem *elem)
2781{
2782 unsigned char *b = skb_tail_pointer(skb);
2783 struct nlattr *nest;
2784
2785 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2786 if (nest == NULL)
2787 goto nla_put_failure;
2788
2789 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2790 set->klen) < 0)
2791 goto nla_put_failure;
2792
2793 if (set->flags & NFT_SET_MAP &&
2794 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2795 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2796 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2797 set->dlen) < 0)
2798 goto nla_put_failure;
2799
2800 if (elem->flags != 0)
2801 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2802 goto nla_put_failure;
2803
2804 nla_nest_end(skb, nest);
2805 return 0;
2806
2807nla_put_failure:
2808 nlmsg_trim(skb, b);
2809 return -EMSGSIZE;
2810}
2811
2812struct nft_set_dump_args {
2813 const struct netlink_callback *cb;
2814 struct nft_set_iter iter;
2815 struct sk_buff *skb;
2816};
2817
2818static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2819 const struct nft_set *set,
2820 const struct nft_set_iter *iter,
2821 const struct nft_set_elem *elem)
2822{
2823 struct nft_set_dump_args *args;
2824
2825 args = container_of(iter, struct nft_set_dump_args, iter);
2826 return nf_tables_fill_setelem(args->skb, set, elem);
2827}
2828
2829static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2830{
2831 const struct nft_set *set;
2832 struct nft_set_dump_args args;
2833 struct nft_ctx ctx;
2834 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2835 struct nfgenmsg *nfmsg;
2836 struct nlmsghdr *nlh;
2837 struct nlattr *nest;
2838 u32 portid, seq;
2839 int event, err;
2840
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01002841 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2842 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002843 if (err < 0)
2844 return err;
2845
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002846 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2847 false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002848 if (err < 0)
2849 return err;
2850
2851 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2852 if (IS_ERR(set))
2853 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002854 if (set->flags & NFT_SET_INACTIVE)
2855 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002856
2857 event = NFT_MSG_NEWSETELEM;
2858 event |= NFNL_SUBSYS_NFTABLES << 8;
2859 portid = NETLINK_CB(cb->skb).portid;
2860 seq = cb->nlh->nlmsg_seq;
2861
2862 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2863 NLM_F_MULTI);
2864 if (nlh == NULL)
2865 goto nla_put_failure;
2866
2867 nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso6403d962014-06-11 19:05:28 +02002868 nfmsg->nfgen_family = ctx.afi->family;
Patrick McHardy20a69342013-10-11 12:06:22 +02002869 nfmsg->version = NFNETLINK_V0;
2870 nfmsg->res_id = 0;
2871
2872 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2873 goto nla_put_failure;
2874 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2875 goto nla_put_failure;
2876
2877 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2878 if (nest == NULL)
2879 goto nla_put_failure;
2880
2881 args.cb = cb;
2882 args.skb = skb;
2883 args.iter.skip = cb->args[0];
2884 args.iter.count = 0;
2885 args.iter.err = 0;
2886 args.iter.fn = nf_tables_dump_setelem;
2887 set->ops->walk(&ctx, set, &args.iter);
2888
2889 nla_nest_end(skb, nest);
2890 nlmsg_end(skb, nlh);
2891
2892 if (args.iter.err && args.iter.err != -EMSGSIZE)
2893 return args.iter.err;
2894 if (args.iter.count == cb->args[0])
2895 return 0;
2896
2897 cb->args[0] = args.iter.count;
2898 return skb->len;
2899
2900nla_put_failure:
2901 return -ENOSPC;
2902}
2903
2904static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2905 const struct nlmsghdr *nlh,
2906 const struct nlattr * const nla[])
2907{
2908 const struct nft_set *set;
2909 struct nft_ctx ctx;
2910 int err;
2911
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002912 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002913 if (err < 0)
2914 return err;
2915
2916 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2917 if (IS_ERR(set))
2918 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002919 if (set->flags & NFT_SET_INACTIVE)
2920 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002921
2922 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2923 struct netlink_dump_control c = {
2924 .dump = nf_tables_dump_set,
2925 };
2926 return netlink_dump_start(nlsk, skb, nlh, &c);
2927 }
2928 return -EOPNOTSUPP;
2929}
2930
Arturo Borrerod60ce622014-04-01 14:06:07 +02002931static int nf_tables_fill_setelem_info(struct sk_buff *skb,
2932 const struct nft_ctx *ctx, u32 seq,
2933 u32 portid, int event, u16 flags,
2934 const struct nft_set *set,
2935 const struct nft_set_elem *elem)
2936{
2937 struct nfgenmsg *nfmsg;
2938 struct nlmsghdr *nlh;
2939 struct nlattr *nest;
2940 int err;
2941
2942 event |= NFNL_SUBSYS_NFTABLES << 8;
2943 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2944 flags);
2945 if (nlh == NULL)
2946 goto nla_put_failure;
2947
2948 nfmsg = nlmsg_data(nlh);
2949 nfmsg->nfgen_family = ctx->afi->family;
2950 nfmsg->version = NFNETLINK_V0;
2951 nfmsg->res_id = 0;
2952
2953 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2954 goto nla_put_failure;
2955 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2956 goto nla_put_failure;
2957
2958 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2959 if (nest == NULL)
2960 goto nla_put_failure;
2961
2962 err = nf_tables_fill_setelem(skb, set, elem);
2963 if (err < 0)
2964 goto nla_put_failure;
2965
2966 nla_nest_end(skb, nest);
2967
2968 return nlmsg_end(skb, nlh);
2969
2970nla_put_failure:
2971 nlmsg_trim(skb, nlh);
2972 return -1;
2973}
2974
2975static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
2976 const struct nft_set *set,
2977 const struct nft_set_elem *elem,
2978 int event, u16 flags)
2979{
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002980 struct net *net = ctx->net;
2981 u32 portid = ctx->portid;
Arturo Borrerod60ce622014-04-01 14:06:07 +02002982 struct sk_buff *skb;
2983 int err;
2984
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002985 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
Arturo Borrerod60ce622014-04-01 14:06:07 +02002986 return 0;
2987
2988 err = -ENOBUFS;
2989 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2990 if (skb == NULL)
2991 goto err;
2992
2993 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
2994 set, elem);
2995 if (err < 0) {
2996 kfree_skb(skb);
2997 goto err;
2998 }
2999
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003000 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
Arturo Borrerod60ce622014-04-01 14:06:07 +02003001 GFP_KERNEL);
3002err:
3003 if (err < 0)
3004 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3005 return err;
3006}
3007
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003008static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3009 int msg_type,
3010 struct nft_set *set)
3011{
3012 struct nft_trans *trans;
3013
3014 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3015 if (trans == NULL)
3016 return NULL;
3017
3018 nft_trans_elem_set(trans) = set;
3019 return trans;
3020}
3021
3022static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +02003023 const struct nlattr *attr)
3024{
3025 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3026 struct nft_data_desc d1, d2;
3027 struct nft_set_elem elem;
3028 struct nft_set_binding *binding;
3029 enum nft_registers dreg;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003030 struct nft_trans *trans;
Patrick McHardy20a69342013-10-11 12:06:22 +02003031 int err;
3032
Patrick McHardyc50b9602014-03-28 10:19:47 +00003033 if (set->size && set->nelems == set->size)
3034 return -ENFILE;
3035
Patrick McHardy20a69342013-10-11 12:06:22 +02003036 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3037 nft_set_elem_policy);
3038 if (err < 0)
3039 return err;
3040
3041 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3042 return -EINVAL;
3043
3044 elem.flags = 0;
3045 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3046 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3047 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
3048 return -EINVAL;
3049 }
3050
3051 if (set->flags & NFT_SET_MAP) {
3052 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3053 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
3054 return -EINVAL;
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01003055 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3056 elem.flags & NFT_SET_ELEM_INTERVAL_END)
3057 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003058 } else {
3059 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3060 return -EINVAL;
3061 }
3062
3063 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3064 if (err < 0)
3065 goto err1;
3066 err = -EINVAL;
3067 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3068 goto err2;
3069
3070 err = -EEXIST;
3071 if (set->ops->get(set, &elem) == 0)
3072 goto err2;
3073
3074 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3075 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
3076 if (err < 0)
3077 goto err2;
3078
3079 err = -EINVAL;
3080 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3081 goto err3;
3082
3083 dreg = nft_type_to_reg(set->dtype);
3084 list_for_each_entry(binding, &set->bindings, list) {
3085 struct nft_ctx bind_ctx = {
3086 .afi = ctx->afi,
3087 .table = ctx->table,
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02003088 .chain = (struct nft_chain *)binding->chain,
Patrick McHardy20a69342013-10-11 12:06:22 +02003089 };
3090
3091 err = nft_validate_data_load(&bind_ctx, dreg,
3092 &elem.data, d2.type);
3093 if (err < 0)
3094 goto err3;
3095 }
3096 }
3097
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003098 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3099 if (trans == NULL)
Patrick McHardy20a69342013-10-11 12:06:22 +02003100 goto err3;
3101
Patrick McHardy20a69342013-10-11 12:06:22 +02003102 err = set->ops->insert(set, &elem);
3103 if (err < 0)
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003104 goto err4;
Patrick McHardy20a69342013-10-11 12:06:22 +02003105
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003106 nft_trans_elem(trans) = elem;
Pablo Neira Ayuso46bbafc2014-05-22 12:36:03 +02003107 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Patrick McHardy20a69342013-10-11 12:06:22 +02003108 return 0;
3109
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003110err4:
3111 kfree(trans);
Patrick McHardy20a69342013-10-11 12:06:22 +02003112err3:
3113 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3114 nft_data_uninit(&elem.data, d2.type);
3115err2:
3116 nft_data_uninit(&elem.key, d1.type);
3117err1:
3118 return err;
3119}
3120
3121static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3122 const struct nlmsghdr *nlh,
3123 const struct nlattr * const nla[])
3124{
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003125 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02003126 const struct nlattr *attr;
3127 struct nft_set *set;
3128 struct nft_ctx ctx;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003129 int rem, err = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003130
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003131 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
Patrick McHardy20a69342013-10-11 12:06:22 +02003132 if (err < 0)
3133 return err;
3134
3135 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003136 if (IS_ERR(set)) {
3137 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3138 set = nf_tables_set_lookup_byid(net,
3139 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3140 }
3141 if (IS_ERR(set))
3142 return PTR_ERR(set);
3143 }
3144
Patrick McHardy20a69342013-10-11 12:06:22 +02003145 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3146 return -EBUSY;
3147
3148 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3149 err = nft_add_set_elem(&ctx, set, attr);
3150 if (err < 0)
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003151 break;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003152
3153 set->nelems++;
Patrick McHardy20a69342013-10-11 12:06:22 +02003154 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003155 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +02003156}
3157
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003158static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +02003159 const struct nlattr *attr)
3160{
3161 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3162 struct nft_data_desc desc;
3163 struct nft_set_elem elem;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003164 struct nft_trans *trans;
Patrick McHardy20a69342013-10-11 12:06:22 +02003165 int err;
3166
3167 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3168 nft_set_elem_policy);
3169 if (err < 0)
3170 goto err1;
3171
3172 err = -EINVAL;
3173 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3174 goto err1;
3175
3176 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3177 if (err < 0)
3178 goto err1;
3179
3180 err = -EINVAL;
3181 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3182 goto err2;
3183
3184 err = set->ops->get(set, &elem);
3185 if (err < 0)
3186 goto err2;
3187
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003188 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
3189 if (trans == NULL)
3190 goto err2;
Patrick McHardy20a69342013-10-11 12:06:22 +02003191
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003192 nft_trans_elem(trans) = elem;
Pablo Neira Ayuso46bbafc2014-05-22 12:36:03 +02003193 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Patrick McHardy20a69342013-10-11 12:06:22 +02003194
3195 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
3196 if (set->flags & NFT_SET_MAP)
3197 nft_data_uninit(&elem.data, set->dtype);
3198
3199err2:
3200 nft_data_uninit(&elem.key, desc.type);
3201err1:
3202 return err;
3203}
3204
3205static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3206 const struct nlmsghdr *nlh,
3207 const struct nlattr * const nla[])
3208{
3209 const struct nlattr *attr;
3210 struct nft_set *set;
3211 struct nft_ctx ctx;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003212 int rem, err = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003213
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003214 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02003215 if (err < 0)
3216 return err;
3217
3218 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3219 if (IS_ERR(set))
3220 return PTR_ERR(set);
3221 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3222 return -EBUSY;
3223
3224 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3225 err = nft_del_setelem(&ctx, set, attr);
3226 if (err < 0)
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003227 break;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003228
3229 set->nelems--;
Patrick McHardy20a69342013-10-11 12:06:22 +02003230 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003231 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +02003232}
3233
Patrick McHardy96518512013-10-14 11:00:02 +02003234static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3235 [NFT_MSG_NEWTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003236 .call_batch = nf_tables_newtable,
Patrick McHardy96518512013-10-14 11:00:02 +02003237 .attr_count = NFTA_TABLE_MAX,
3238 .policy = nft_table_policy,
3239 },
3240 [NFT_MSG_GETTABLE] = {
3241 .call = nf_tables_gettable,
3242 .attr_count = NFTA_TABLE_MAX,
3243 .policy = nft_table_policy,
3244 },
3245 [NFT_MSG_DELTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003246 .call_batch = nf_tables_deltable,
Patrick McHardy96518512013-10-14 11:00:02 +02003247 .attr_count = NFTA_TABLE_MAX,
3248 .policy = nft_table_policy,
3249 },
3250 [NFT_MSG_NEWCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003251 .call_batch = nf_tables_newchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003252 .attr_count = NFTA_CHAIN_MAX,
3253 .policy = nft_chain_policy,
3254 },
3255 [NFT_MSG_GETCHAIN] = {
3256 .call = nf_tables_getchain,
3257 .attr_count = NFTA_CHAIN_MAX,
3258 .policy = nft_chain_policy,
3259 },
3260 [NFT_MSG_DELCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003261 .call_batch = nf_tables_delchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003262 .attr_count = NFTA_CHAIN_MAX,
3263 .policy = nft_chain_policy,
3264 },
3265 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003266 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003267 .attr_count = NFTA_RULE_MAX,
3268 .policy = nft_rule_policy,
3269 },
3270 [NFT_MSG_GETRULE] = {
3271 .call = nf_tables_getrule,
3272 .attr_count = NFTA_RULE_MAX,
3273 .policy = nft_rule_policy,
3274 },
3275 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003276 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003277 .attr_count = NFTA_RULE_MAX,
3278 .policy = nft_rule_policy,
3279 },
Patrick McHardy20a69342013-10-11 12:06:22 +02003280 [NFT_MSG_NEWSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003281 .call_batch = nf_tables_newset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003282 .attr_count = NFTA_SET_MAX,
3283 .policy = nft_set_policy,
3284 },
3285 [NFT_MSG_GETSET] = {
3286 .call = nf_tables_getset,
3287 .attr_count = NFTA_SET_MAX,
3288 .policy = nft_set_policy,
3289 },
3290 [NFT_MSG_DELSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003291 .call_batch = nf_tables_delset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003292 .attr_count = NFTA_SET_MAX,
3293 .policy = nft_set_policy,
3294 },
3295 [NFT_MSG_NEWSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003296 .call_batch = nf_tables_newsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003297 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3298 .policy = nft_set_elem_list_policy,
3299 },
3300 [NFT_MSG_GETSETELEM] = {
3301 .call = nf_tables_getsetelem,
3302 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3303 .policy = nft_set_elem_list_policy,
3304 },
3305 [NFT_MSG_DELSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003306 .call_batch = nf_tables_delsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003307 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3308 .policy = nft_set_elem_list_policy,
3309 },
Patrick McHardy96518512013-10-14 11:00:02 +02003310};
3311
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003312static void nft_chain_commit_update(struct nft_trans *trans)
3313{
3314 struct nft_base_chain *basechain;
3315
3316 if (nft_trans_chain_name(trans)[0])
3317 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3318
3319 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3320 return;
3321
3322 basechain = nft_base_chain(trans->ctx.chain);
3323 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3324
3325 switch (nft_trans_chain_policy(trans)) {
3326 case NF_DROP:
3327 case NF_ACCEPT:
3328 basechain->policy = nft_trans_chain_policy(trans);
3329 break;
3330 }
3331}
3332
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003333/* Schedule objects for release via rcu to make sure no packets are accesing
3334 * removed rules.
3335 */
3336static void nf_tables_commit_release_rcu(struct rcu_head *rt)
3337{
3338 struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3339
3340 switch (trans->msg_type) {
3341 case NFT_MSG_DELTABLE:
3342 nf_tables_table_destroy(&trans->ctx);
3343 break;
3344 case NFT_MSG_DELCHAIN:
3345 nf_tables_chain_destroy(trans->ctx.chain);
3346 break;
3347 case NFT_MSG_DELRULE:
3348 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3349 break;
3350 case NFT_MSG_DELSET:
3351 nft_set_destroy(nft_trans_set(trans));
3352 break;
3353 }
3354 kfree(trans);
3355}
3356
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003357static int nf_tables_commit(struct sk_buff *skb)
3358{
3359 struct net *net = sock_net(skb->sk);
3360 struct nft_trans *trans, *next;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003361 struct nft_set *set;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003362
3363 /* Bump generation counter, invalidate any dump in progress */
3364 net->nft.genctr++;
3365
3366 /* A new generation has just started */
3367 net->nft.gencursor = gencursor_next(net);
3368
3369 /* Make sure all packets have left the previous generation before
3370 * purging old rules.
3371 */
3372 synchronize_rcu();
3373
3374 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003375 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003376 case NFT_MSG_NEWTABLE:
3377 if (nft_trans_table_update(trans)) {
3378 if (!nft_trans_table_enable(trans)) {
3379 nf_tables_table_disable(trans->ctx.afi,
3380 trans->ctx.table);
3381 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3382 }
3383 } else {
3384 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3385 }
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003386 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003387 nft_trans_destroy(trans);
3388 break;
3389 case NFT_MSG_DELTABLE:
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003390 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003391 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003392 case NFT_MSG_NEWCHAIN:
3393 if (nft_trans_chain_update(trans))
3394 nft_chain_commit_update(trans);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003395 else
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003396 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003397
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003398 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003399 nft_trans_destroy(trans);
3400 break;
3401 case NFT_MSG_DELCHAIN:
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003402 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003403 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3404 trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3405 nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3406 trans->ctx.afi->nops);
3407 }
3408 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003409 case NFT_MSG_NEWRULE:
3410 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003411 nf_tables_rule_notify(&trans->ctx,
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003412 nft_trans_rule(trans),
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003413 NFT_MSG_NEWRULE);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003414 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003415 break;
3416 case NFT_MSG_DELRULE:
3417 list_del_rcu(&nft_trans_rule(trans)->list);
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003418 nf_tables_rule_notify(&trans->ctx,
3419 nft_trans_rule(trans),
3420 NFT_MSG_DELRULE);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003421 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003422 case NFT_MSG_NEWSET:
3423 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003424 /* This avoids hitting -EBUSY when deleting the table
3425 * from the transaction.
3426 */
3427 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3428 !list_empty(&nft_trans_set(trans)->bindings))
3429 trans->ctx.table->use--;
3430
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003431 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02003432 NFT_MSG_NEWSET, GFP_KERNEL);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003433 nft_trans_destroy(trans);
3434 break;
3435 case NFT_MSG_DELSET:
3436 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02003437 NFT_MSG_DELSET, GFP_KERNEL);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003438 break;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003439 case NFT_MSG_NEWSETELEM:
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003440 nf_tables_setelem_notify(&trans->ctx,
3441 nft_trans_elem_set(trans),
3442 &nft_trans_elem(trans),
3443 NFT_MSG_NEWSETELEM, 0);
3444 nft_trans_destroy(trans);
3445 break;
3446 case NFT_MSG_DELSETELEM:
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003447 nf_tables_setelem_notify(&trans->ctx,
3448 nft_trans_elem_set(trans),
3449 &nft_trans_elem(trans),
3450 NFT_MSG_DELSETELEM, 0);
3451 set = nft_trans_elem_set(trans);
3452 set->ops->get(set, &nft_trans_elem(trans));
3453 set->ops->remove(set, &nft_trans_elem(trans));
3454 nft_trans_destroy(trans);
3455 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003456 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003457 }
3458
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003459 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003460 list_del(&trans->list);
3461 trans->ctx.nla = NULL;
3462 call_rcu(&trans->rcu_head, nf_tables_commit_release_rcu);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003463 }
3464
3465 return 0;
3466}
3467
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003468/* Schedule objects for release via rcu to make sure no packets are accesing
3469 * aborted rules.
3470 */
3471static void nf_tables_abort_release_rcu(struct rcu_head *rt)
3472{
3473 struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3474
3475 switch (trans->msg_type) {
3476 case NFT_MSG_NEWTABLE:
3477 nf_tables_table_destroy(&trans->ctx);
3478 break;
3479 case NFT_MSG_NEWCHAIN:
3480 nf_tables_chain_destroy(trans->ctx.chain);
3481 break;
3482 case NFT_MSG_NEWRULE:
3483 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3484 break;
3485 case NFT_MSG_NEWSET:
3486 nft_set_destroy(nft_trans_set(trans));
3487 break;
3488 }
3489 kfree(trans);
3490}
3491
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003492static int nf_tables_abort(struct sk_buff *skb)
3493{
3494 struct net *net = sock_net(skb->sk);
3495 struct nft_trans *trans, *next;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003496 struct nft_set *set;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003497
3498 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003499 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003500 case NFT_MSG_NEWTABLE:
3501 if (nft_trans_table_update(trans)) {
3502 if (nft_trans_table_enable(trans)) {
3503 nf_tables_table_disable(trans->ctx.afi,
3504 trans->ctx.table);
3505 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3506 }
3507 nft_trans_destroy(trans);
3508 } else {
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003509 list_del_rcu(&trans->ctx.table->list);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003510 }
3511 break;
3512 case NFT_MSG_DELTABLE:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003513 list_add_tail_rcu(&trans->ctx.table->list,
3514 &trans->ctx.afi->tables);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003515 nft_trans_destroy(trans);
3516 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003517 case NFT_MSG_NEWCHAIN:
3518 if (nft_trans_chain_update(trans)) {
3519 if (nft_trans_chain_stats(trans))
3520 free_percpu(nft_trans_chain_stats(trans));
3521
3522 nft_trans_destroy(trans);
3523 } else {
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003524 trans->ctx.table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003525 list_del_rcu(&trans->ctx.chain->list);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003526 if (!(trans->ctx.table->flags & NFT_TABLE_F_DORMANT) &&
3527 trans->ctx.chain->flags & NFT_BASE_CHAIN) {
3528 nf_unregister_hooks(nft_base_chain(trans->ctx.chain)->ops,
3529 trans->ctx.afi->nops);
3530 }
3531 }
3532 break;
3533 case NFT_MSG_DELCHAIN:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003534 trans->ctx.table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003535 list_add_tail_rcu(&trans->ctx.chain->list,
3536 &trans->ctx.table->chains);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003537 nft_trans_destroy(trans);
3538 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003539 case NFT_MSG_NEWRULE:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003540 trans->ctx.chain->use--;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003541 list_del_rcu(&nft_trans_rule(trans)->list);
3542 break;
3543 case NFT_MSG_DELRULE:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003544 trans->ctx.chain->use++;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003545 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003546 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003547 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003548 case NFT_MSG_NEWSET:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003549 trans->ctx.table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003550 list_del_rcu(&nft_trans_set(trans)->list);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003551 break;
3552 case NFT_MSG_DELSET:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003553 trans->ctx.table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003554 list_add_tail_rcu(&nft_trans_set(trans)->list,
3555 &trans->ctx.table->sets);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003556 nft_trans_destroy(trans);
3557 break;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003558 case NFT_MSG_NEWSETELEM:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003559 nft_trans_elem_set(trans)->nelems--;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003560 set = nft_trans_elem_set(trans);
3561 set->ops->get(set, &nft_trans_elem(trans));
3562 set->ops->remove(set, &nft_trans_elem(trans));
3563 nft_trans_destroy(trans);
3564 break;
3565 case NFT_MSG_DELSETELEM:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003566 nft_trans_elem_set(trans)->nelems++;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003567 nft_trans_destroy(trans);
3568 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003569 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003570 }
3571
Pablo Neira Ayusoa1cee072014-05-23 11:09:42 +02003572 list_for_each_entry_safe_reverse(trans, next,
3573 &net->nft.commit_list, list) {
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003574 list_del(&trans->list);
3575 trans->ctx.nla = NULL;
3576 call_rcu(&trans->rcu_head, nf_tables_abort_release_rcu);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003577 }
3578
3579 return 0;
3580}
3581
Patrick McHardy96518512013-10-14 11:00:02 +02003582static const struct nfnetlink_subsystem nf_tables_subsys = {
3583 .name = "nf_tables",
3584 .subsys_id = NFNL_SUBSYS_NFTABLES,
3585 .cb_count = NFT_MSG_MAX,
3586 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003587 .commit = nf_tables_commit,
3588 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02003589};
3590
Patrick McHardy20a69342013-10-11 12:06:22 +02003591/*
3592 * Loop detection - walk through the ruleset beginning at the destination chain
3593 * of a new jump until either the source chain is reached (loop) or all
3594 * reachable chains have been traversed.
3595 *
3596 * The loop check is performed whenever a new jump verdict is added to an
3597 * expression or verdict map or a verdict map is bound to a new chain.
3598 */
3599
3600static int nf_tables_check_loops(const struct nft_ctx *ctx,
3601 const struct nft_chain *chain);
3602
3603static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3604 const struct nft_set *set,
3605 const struct nft_set_iter *iter,
3606 const struct nft_set_elem *elem)
3607{
Pablo Neira Ayuso62f9c8b2014-02-07 14:45:01 +01003608 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3609 return 0;
3610
Patrick McHardy20a69342013-10-11 12:06:22 +02003611 switch (elem->data.verdict) {
3612 case NFT_JUMP:
3613 case NFT_GOTO:
3614 return nf_tables_check_loops(ctx, elem->data.chain);
3615 default:
3616 return 0;
3617 }
3618}
3619
3620static int nf_tables_check_loops(const struct nft_ctx *ctx,
3621 const struct nft_chain *chain)
3622{
3623 const struct nft_rule *rule;
3624 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02003625 const struct nft_set *set;
3626 struct nft_set_binding *binding;
3627 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02003628
3629 if (ctx->chain == chain)
3630 return -ELOOP;
3631
3632 list_for_each_entry(rule, &chain->rules, list) {
3633 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003634 const struct nft_data *data = NULL;
3635 int err;
3636
3637 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02003638 continue;
3639
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003640 err = expr->ops->validate(ctx, expr, &data);
3641 if (err < 0)
3642 return err;
3643
Patrick McHardy20a69342013-10-11 12:06:22 +02003644 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003645 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02003646
3647 switch (data->verdict) {
3648 case NFT_JUMP:
3649 case NFT_GOTO:
3650 err = nf_tables_check_loops(ctx, data->chain);
3651 if (err < 0)
3652 return err;
3653 default:
3654 break;
3655 }
3656 }
3657 }
3658
3659 list_for_each_entry(set, &ctx->table->sets, list) {
3660 if (!(set->flags & NFT_SET_MAP) ||
3661 set->dtype != NFT_DATA_VERDICT)
3662 continue;
3663
3664 list_for_each_entry(binding, &set->bindings, list) {
3665 if (binding->chain != chain)
3666 continue;
3667
3668 iter.skip = 0;
3669 iter.count = 0;
3670 iter.err = 0;
3671 iter.fn = nf_tables_loop_check_setelem;
3672
3673 set->ops->walk(ctx, set, &iter);
3674 if (iter.err < 0)
3675 return iter.err;
3676 }
3677 }
3678
3679 return 0;
3680}
3681
Patrick McHardy96518512013-10-14 11:00:02 +02003682/**
3683 * nft_validate_input_register - validate an expressions' input register
3684 *
3685 * @reg: the register number
3686 *
3687 * Validate that the input register is one of the general purpose
3688 * registers.
3689 */
3690int nft_validate_input_register(enum nft_registers reg)
3691{
3692 if (reg <= NFT_REG_VERDICT)
3693 return -EINVAL;
3694 if (reg > NFT_REG_MAX)
3695 return -ERANGE;
3696 return 0;
3697}
3698EXPORT_SYMBOL_GPL(nft_validate_input_register);
3699
3700/**
3701 * nft_validate_output_register - validate an expressions' output register
3702 *
3703 * @reg: the register number
3704 *
3705 * Validate that the output register is one of the general purpose
3706 * registers or the verdict register.
3707 */
3708int nft_validate_output_register(enum nft_registers reg)
3709{
3710 if (reg < NFT_REG_VERDICT)
3711 return -EINVAL;
3712 if (reg > NFT_REG_MAX)
3713 return -ERANGE;
3714 return 0;
3715}
3716EXPORT_SYMBOL_GPL(nft_validate_output_register);
3717
3718/**
3719 * nft_validate_data_load - validate an expressions' data load
3720 *
3721 * @ctx: context of the expression performing the load
3722 * @reg: the destination register number
3723 * @data: the data to load
3724 * @type: the data type
3725 *
3726 * Validate that a data load uses the appropriate data type for
3727 * the destination register. A value of NULL for the data means
3728 * that its runtime gathered data, which is always of type
3729 * NFT_DATA_VALUE.
3730 */
3731int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3732 const struct nft_data *data,
3733 enum nft_data_types type)
3734{
Patrick McHardy20a69342013-10-11 12:06:22 +02003735 int err;
3736
Patrick McHardy96518512013-10-14 11:00:02 +02003737 switch (reg) {
3738 case NFT_REG_VERDICT:
3739 if (data == NULL || type != NFT_DATA_VERDICT)
3740 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003741
3742 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3743 err = nf_tables_check_loops(ctx, data->chain);
3744 if (err < 0)
3745 return err;
3746
3747 if (ctx->chain->level + 1 > data->chain->level) {
3748 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3749 return -EMLINK;
3750 data->chain->level = ctx->chain->level + 1;
3751 }
3752 }
3753
Patrick McHardy96518512013-10-14 11:00:02 +02003754 return 0;
3755 default:
3756 if (data != NULL && type != NFT_DATA_VALUE)
3757 return -EINVAL;
3758 return 0;
3759 }
3760}
3761EXPORT_SYMBOL_GPL(nft_validate_data_load);
3762
3763static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3764 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3765 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3766 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3767};
3768
3769static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3770 struct nft_data_desc *desc, const struct nlattr *nla)
3771{
3772 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3773 struct nft_chain *chain;
3774 int err;
3775
3776 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3777 if (err < 0)
3778 return err;
3779
3780 if (!tb[NFTA_VERDICT_CODE])
3781 return -EINVAL;
3782 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3783
3784 switch (data->verdict) {
Patrick McHardye0abdad2014-02-18 18:06:50 +00003785 default:
3786 switch (data->verdict & NF_VERDICT_MASK) {
3787 case NF_ACCEPT:
3788 case NF_DROP:
3789 case NF_QUEUE:
3790 break;
3791 default:
3792 return -EINVAL;
3793 }
3794 /* fall through */
Patrick McHardy96518512013-10-14 11:00:02 +02003795 case NFT_CONTINUE:
3796 case NFT_BREAK:
3797 case NFT_RETURN:
3798 desc->len = sizeof(data->verdict);
3799 break;
3800 case NFT_JUMP:
3801 case NFT_GOTO:
3802 if (!tb[NFTA_VERDICT_CHAIN])
3803 return -EINVAL;
3804 chain = nf_tables_chain_lookup(ctx->table,
3805 tb[NFTA_VERDICT_CHAIN]);
3806 if (IS_ERR(chain))
3807 return PTR_ERR(chain);
3808 if (chain->flags & NFT_BASE_CHAIN)
3809 return -EOPNOTSUPP;
3810
Patrick McHardy96518512013-10-14 11:00:02 +02003811 chain->use++;
3812 data->chain = chain;
3813 desc->len = sizeof(data);
3814 break;
Patrick McHardy96518512013-10-14 11:00:02 +02003815 }
3816
3817 desc->type = NFT_DATA_VERDICT;
3818 return 0;
3819}
3820
3821static void nft_verdict_uninit(const struct nft_data *data)
3822{
3823 switch (data->verdict) {
3824 case NFT_JUMP:
3825 case NFT_GOTO:
3826 data->chain->use--;
3827 break;
3828 }
3829}
3830
3831static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3832{
3833 struct nlattr *nest;
3834
3835 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3836 if (!nest)
3837 goto nla_put_failure;
3838
3839 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3840 goto nla_put_failure;
3841
3842 switch (data->verdict) {
3843 case NFT_JUMP:
3844 case NFT_GOTO:
3845 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3846 goto nla_put_failure;
3847 }
3848 nla_nest_end(skb, nest);
3849 return 0;
3850
3851nla_put_failure:
3852 return -1;
3853}
3854
3855static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3856 struct nft_data_desc *desc, const struct nlattr *nla)
3857{
3858 unsigned int len;
3859
3860 len = nla_len(nla);
3861 if (len == 0)
3862 return -EINVAL;
3863 if (len > sizeof(data->data))
3864 return -EOVERFLOW;
3865
3866 nla_memcpy(data->data, nla, sizeof(data->data));
3867 desc->type = NFT_DATA_VALUE;
3868 desc->len = len;
3869 return 0;
3870}
3871
3872static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3873 unsigned int len)
3874{
3875 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3876}
3877
3878static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3879 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3880 .len = FIELD_SIZEOF(struct nft_data, data) },
3881 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3882};
3883
3884/**
3885 * nft_data_init - parse nf_tables data netlink attributes
3886 *
3887 * @ctx: context of the expression using the data
3888 * @data: destination struct nft_data
3889 * @desc: data description
3890 * @nla: netlink attribute containing data
3891 *
3892 * Parse the netlink data attributes and initialize a struct nft_data.
3893 * The type and length of data are returned in the data description.
3894 *
3895 * The caller can indicate that it only wants to accept data of type
3896 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3897 */
3898int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3899 struct nft_data_desc *desc, const struct nlattr *nla)
3900{
3901 struct nlattr *tb[NFTA_DATA_MAX + 1];
3902 int err;
3903
3904 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3905 if (err < 0)
3906 return err;
3907
3908 if (tb[NFTA_DATA_VALUE])
3909 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3910 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3911 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3912 return -EINVAL;
3913}
3914EXPORT_SYMBOL_GPL(nft_data_init);
3915
3916/**
3917 * nft_data_uninit - release a nft_data item
3918 *
3919 * @data: struct nft_data to release
3920 * @type: type of data
3921 *
3922 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3923 * all others need to be released by calling this function.
3924 */
3925void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3926{
3927 switch (type) {
3928 case NFT_DATA_VALUE:
3929 return;
3930 case NFT_DATA_VERDICT:
3931 return nft_verdict_uninit(data);
3932 default:
3933 WARN_ON(1);
3934 }
3935}
3936EXPORT_SYMBOL_GPL(nft_data_uninit);
3937
3938int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3939 enum nft_data_types type, unsigned int len)
3940{
3941 struct nlattr *nest;
3942 int err;
3943
3944 nest = nla_nest_start(skb, attr);
3945 if (nest == NULL)
3946 return -1;
3947
3948 switch (type) {
3949 case NFT_DATA_VALUE:
3950 err = nft_value_dump(skb, data, len);
3951 break;
3952 case NFT_DATA_VERDICT:
3953 err = nft_verdict_dump(skb, data);
3954 break;
3955 default:
3956 err = -EINVAL;
3957 WARN_ON(1);
3958 }
3959
3960 nla_nest_end(skb, nest);
3961 return err;
3962}
3963EXPORT_SYMBOL_GPL(nft_data_dump);
3964
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003965static int nf_tables_init_net(struct net *net)
3966{
3967 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003968 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003969 return 0;
3970}
3971
3972static struct pernet_operations nf_tables_net_ops = {
3973 .init = nf_tables_init_net,
3974};
3975
Patrick McHardy96518512013-10-14 11:00:02 +02003976static int __init nf_tables_module_init(void)
3977{
3978 int err;
3979
3980 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3981 GFP_KERNEL);
3982 if (info == NULL) {
3983 err = -ENOMEM;
3984 goto err1;
3985 }
3986
3987 err = nf_tables_core_module_init();
3988 if (err < 0)
3989 goto err2;
3990
3991 err = nfnetlink_subsys_register(&nf_tables_subsys);
3992 if (err < 0)
3993 goto err3;
3994
3995 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003996 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02003997err3:
3998 nf_tables_core_module_exit();
3999err2:
4000 kfree(info);
4001err1:
4002 return err;
4003}
4004
4005static void __exit nf_tables_module_exit(void)
4006{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004007 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02004008 nfnetlink_subsys_unregister(&nf_tables_subsys);
4009 nf_tables_core_module_exit();
4010 kfree(info);
4011}
4012
4013module_init(nf_tables_module_init);
4014module_exit(nf_tables_module_exit);
4015
4016MODULE_LICENSE("GPL");
4017MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4018MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);