blob: 0b96fa0d64b2f9bf536eed8a0778f36f8aa04508 [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
Arturo Borreroc5598792014-09-02 16:42:23 +0200130static void nf_tables_unregister_hooks(const struct nft_table *table,
131 const struct nft_chain *chain,
132 unsigned int hook_nops)
133{
134 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
135 chain->flags & NFT_BASE_CHAIN)
136 nf_unregister_hooks(nft_base_chain(chain)->ops, hook_nops);
137}
138
Arturo Borreroee01d542014-09-02 16:42:25 +0200139/* Internal table flags */
140#define NFT_TABLE_INACTIVE (1 << 15)
141
142static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
143{
144 struct nft_trans *trans;
145
146 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
147 if (trans == NULL)
148 return -ENOMEM;
149
150 if (msg_type == NFT_MSG_NEWTABLE)
151 ctx->table->flags |= NFT_TABLE_INACTIVE;
152
153 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
154 return 0;
155}
156
157static int nft_deltable(struct nft_ctx *ctx)
158{
159 int err;
160
161 err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
162 if (err < 0)
163 return err;
164
165 list_del_rcu(&ctx->table->list);
166 return err;
167}
168
169static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
170{
171 struct nft_trans *trans;
172
173 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
174 if (trans == NULL)
175 return -ENOMEM;
176
177 if (msg_type == NFT_MSG_NEWCHAIN)
178 ctx->chain->flags |= NFT_CHAIN_INACTIVE;
179
180 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
181 return 0;
182}
183
184static int nft_delchain(struct nft_ctx *ctx)
185{
186 int err;
187
188 err = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
189 if (err < 0)
190 return err;
191
192 ctx->table->use--;
193 list_del_rcu(&ctx->chain->list);
194
195 return err;
196}
197
198static inline bool
199nft_rule_is_active(struct net *net, const struct nft_rule *rule)
200{
Patrick McHardyea4bd992015-03-25 14:08:49 +0000201 return (rule->genmask & nft_genmask_cur(net)) == 0;
Arturo Borreroee01d542014-09-02 16:42:25 +0200202}
203
204static inline int
205nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
206{
Patrick McHardyea4bd992015-03-25 14:08:49 +0000207 return (rule->genmask & nft_genmask_next(net)) == 0;
Arturo Borreroee01d542014-09-02 16:42:25 +0200208}
209
210static inline void
211nft_rule_activate_next(struct net *net, struct nft_rule *rule)
212{
213 /* Now inactive, will be active in the future */
Patrick McHardyea4bd992015-03-25 14:08:49 +0000214 rule->genmask = nft_genmask_cur(net);
Arturo Borreroee01d542014-09-02 16:42:25 +0200215}
216
217static inline void
218nft_rule_deactivate_next(struct net *net, struct nft_rule *rule)
219{
Patrick McHardyea4bd992015-03-25 14:08:49 +0000220 rule->genmask = nft_genmask_next(net);
Arturo Borreroee01d542014-09-02 16:42:25 +0200221}
222
223static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
224{
Patrick McHardyea4bd992015-03-25 14:08:49 +0000225 rule->genmask &= ~nft_genmask_next(net);
Arturo Borreroee01d542014-09-02 16:42:25 +0200226}
227
228static int
229nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
230{
231 /* You cannot delete the same rule twice */
232 if (nft_rule_is_active_next(ctx->net, rule)) {
233 nft_rule_deactivate_next(ctx->net, rule);
234 ctx->chain->use--;
235 return 0;
236 }
237 return -ENOENT;
238}
239
240static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
241 struct nft_rule *rule)
242{
243 struct nft_trans *trans;
244
245 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
246 if (trans == NULL)
247 return NULL;
248
249 nft_trans_rule(trans) = rule;
250 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
251
252 return trans;
253}
254
255static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
256{
257 struct nft_trans *trans;
258 int err;
259
260 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
261 if (trans == NULL)
262 return -ENOMEM;
263
264 err = nf_tables_delrule_deactivate(ctx, rule);
265 if (err < 0) {
266 nft_trans_destroy(trans);
267 return err;
268 }
269
270 return 0;
271}
272
273static int nft_delrule_by_chain(struct nft_ctx *ctx)
274{
275 struct nft_rule *rule;
276 int err;
277
278 list_for_each_entry(rule, &ctx->chain->rules, list) {
279 err = nft_delrule(ctx, rule);
280 if (err < 0)
281 return err;
282 }
283 return 0;
284}
285
286/* Internal set flag */
287#define NFT_SET_INACTIVE (1 << 15)
288
289static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
290 struct nft_set *set)
291{
292 struct nft_trans *trans;
293
294 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
295 if (trans == NULL)
296 return -ENOMEM;
297
298 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
299 nft_trans_set_id(trans) =
300 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
301 set->flags |= NFT_SET_INACTIVE;
302 }
303 nft_trans_set(trans) = set;
304 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
305
306 return 0;
307}
308
309static int nft_delset(struct nft_ctx *ctx, struct nft_set *set)
310{
311 int err;
312
313 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
314 if (err < 0)
315 return err;
316
317 list_del_rcu(&set->list);
318 ctx->table->use--;
319
320 return err;
321}
322
Patrick McHardy96518512013-10-14 11:00:02 +0200323/*
324 * Tables
325 */
326
327static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
328 const struct nlattr *nla)
329{
330 struct nft_table *table;
331
332 list_for_each_entry(table, &afi->tables, list) {
333 if (!nla_strcmp(nla, table->name))
334 return table;
335 }
336 return NULL;
337}
338
339static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200340 const struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +0200341{
342 struct nft_table *table;
343
344 if (nla == NULL)
345 return ERR_PTR(-EINVAL);
346
347 table = nft_table_lookup(afi, nla);
348 if (table != NULL)
349 return table;
350
Patrick McHardy96518512013-10-14 11:00:02 +0200351 return ERR_PTR(-ENOENT);
352}
353
354static inline u64 nf_tables_alloc_handle(struct nft_table *table)
355{
356 return ++table->hgenerator;
357}
358
Patrick McHardy2a37d752014-01-09 18:42:37 +0000359static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200360
Patrick McHardy2a37d752014-01-09 18:42:37 +0000361static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000362__nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200363{
364 int i;
365
Patrick McHardybaae3e62014-01-09 18:42:34 +0000366 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200367 if (chain_type[family][i] != NULL &&
368 !nla_strcmp(nla, chain_type[family][i]->name))
Patrick McHardybaae3e62014-01-09 18:42:34 +0000369 return chain_type[family][i];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200370 }
Patrick McHardybaae3e62014-01-09 18:42:34 +0000371 return NULL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200372}
373
Patrick McHardy2a37d752014-01-09 18:42:37 +0000374static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000375nf_tables_chain_type_lookup(const struct nft_af_info *afi,
376 const struct nlattr *nla,
377 bool autoload)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200378{
Patrick McHardy2a37d752014-01-09 18:42:37 +0000379 const struct nf_chain_type *type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200380
381 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000382 if (type != NULL)
383 return type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200384#ifdef CONFIG_MODULES
Patrick McHardy93b08062014-01-09 18:42:36 +0000385 if (autoload) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200386 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso2fec6bb2014-03-31 12:26:39 +0200387 request_module("nft-chain-%u-%.*s", afi->family,
388 nla_len(nla), (const char *)nla_data(nla));
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200389 nfnl_lock(NFNL_SUBSYS_NFTABLES);
390 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000391 if (type != NULL)
392 return ERR_PTR(-EAGAIN);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200393 }
394#endif
Patrick McHardy93b08062014-01-09 18:42:36 +0000395 return ERR_PTR(-ENOENT);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200396}
397
Patrick McHardy96518512013-10-14 11:00:02 +0200398static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
Pablo Neira Ayuso1cae5652015-03-05 15:05:36 +0100399 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
400 .len = NFT_TABLE_MAXNAMELEN - 1 },
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200401 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
Patrick McHardy96518512013-10-14 11:00:02 +0200402};
403
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200404static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
405 u32 portid, u32 seq, int event, u32 flags,
406 int family, const struct nft_table *table)
Patrick McHardy96518512013-10-14 11:00:02 +0200407{
408 struct nlmsghdr *nlh;
409 struct nfgenmsg *nfmsg;
410
411 event |= NFNL_SUBSYS_NFTABLES << 8;
412 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
413 if (nlh == NULL)
414 goto nla_put_failure;
415
416 nfmsg = nlmsg_data(nlh);
417 nfmsg->nfgen_family = family;
418 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200419 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +0200420
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200421 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
Tomasz Bursztykad8bcc7682013-12-12 15:00:42 +0200422 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
423 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
Patrick McHardy96518512013-10-14 11:00:02 +0200424 goto nla_put_failure;
425
Johannes Berg053c0952015-01-16 22:09:00 +0100426 nlmsg_end(skb, nlh);
427 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200428
429nla_put_failure:
430 nlmsg_trim(skb, nlh);
431 return -1;
432}
433
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200434static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)
Patrick McHardy96518512013-10-14 11:00:02 +0200435{
436 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +0200437 int err;
438
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200439 if (!ctx->report &&
440 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +0200441 return 0;
442
443 err = -ENOBUFS;
444 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
445 if (skb == NULL)
446 goto err;
447
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200448 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
449 event, 0, ctx->afi->family, ctx->table);
Patrick McHardy96518512013-10-14 11:00:02 +0200450 if (err < 0) {
451 kfree_skb(skb);
452 goto err;
453 }
454
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200455 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
456 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +0200457err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200458 if (err < 0) {
459 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
460 err);
461 }
Patrick McHardy96518512013-10-14 11:00:02 +0200462 return err;
463}
464
465static int nf_tables_dump_tables(struct sk_buff *skb,
466 struct netlink_callback *cb)
467{
468 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
469 const struct nft_af_info *afi;
470 const struct nft_table *table;
471 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200472 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200473 int family = nfmsg->nfgen_family;
474
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200475 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +0200476 cb->seq = net->nft.base_seq;
477
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200478 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200479 if (family != NFPROTO_UNSPEC && family != afi->family)
480 continue;
481
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200482 list_for_each_entry_rcu(table, &afi->tables, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200483 if (idx < s_idx)
484 goto cont;
485 if (idx > s_idx)
486 memset(&cb->args[1], 0,
487 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200488 if (nf_tables_fill_table_info(skb, net,
Patrick McHardy96518512013-10-14 11:00:02 +0200489 NETLINK_CB(cb->skb).portid,
490 cb->nlh->nlmsg_seq,
491 NFT_MSG_NEWTABLE,
492 NLM_F_MULTI,
493 afi->family, table) < 0)
494 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +0200495
496 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +0200497cont:
498 idx++;
499 }
500 }
501done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200502 rcu_read_unlock();
Patrick McHardy96518512013-10-14 11:00:02 +0200503 cb->args[0] = idx;
504 return skb->len;
505}
506
507static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
508 const struct nlmsghdr *nlh,
509 const struct nlattr * const nla[])
510{
511 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
512 const struct nft_af_info *afi;
513 const struct nft_table *table;
514 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200515 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200516 int family = nfmsg->nfgen_family;
517 int err;
518
519 if (nlh->nlmsg_flags & NLM_F_DUMP) {
520 struct netlink_dump_control c = {
521 .dump = nf_tables_dump_tables,
522 };
523 return netlink_dump_start(nlsk, skb, nlh, &c);
524 }
525
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200526 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200527 if (IS_ERR(afi))
528 return PTR_ERR(afi);
529
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200530 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200531 if (IS_ERR(table))
532 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200533 if (table->flags & NFT_TABLE_INACTIVE)
534 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200535
536 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
537 if (!skb2)
538 return -ENOMEM;
539
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200540 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +0200541 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
542 family, table);
543 if (err < 0)
544 goto err;
545
546 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
547
548err:
549 kfree_skb(skb2);
550 return err;
551}
552
Patrick McHardy115a60b2014-01-03 12:16:15 +0000553static int nf_tables_table_enable(const struct nft_af_info *afi,
554 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200555{
556 struct nft_chain *chain;
557 int err, i = 0;
558
559 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100560 if (!(chain->flags & NFT_BASE_CHAIN))
561 continue;
562
Patrick McHardy115a60b2014-01-03 12:16:15 +0000563 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200564 if (err < 0)
565 goto err;
566
567 i++;
568 }
569 return 0;
570err:
571 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100572 if (!(chain->flags & NFT_BASE_CHAIN))
573 continue;
574
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200575 if (i-- <= 0)
576 break;
577
Patrick McHardy115a60b2014-01-03 12:16:15 +0000578 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200579 }
580 return err;
581}
582
Pablo Neira Ayusof75edf52014-03-30 14:04:52 +0200583static void nf_tables_table_disable(const struct nft_af_info *afi,
Patrick McHardy115a60b2014-01-03 12:16:15 +0000584 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200585{
586 struct nft_chain *chain;
587
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100588 list_for_each_entry(chain, &table->chains, list) {
589 if (chain->flags & NFT_BASE_CHAIN)
Patrick McHardy115a60b2014-01-03 12:16:15 +0000590 nf_unregister_hooks(nft_base_chain(chain)->ops,
591 afi->nops);
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100592 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200593}
594
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200595static int nf_tables_updtable(struct nft_ctx *ctx)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200596{
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200597 struct nft_trans *trans;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200598 u32 flags;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200599 int ret = 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200600
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200601 if (!ctx->nla[NFTA_TABLE_FLAGS])
602 return 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200603
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200604 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
605 if (flags & ~NFT_TABLE_F_DORMANT)
606 return -EINVAL;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200607
Pablo Neira Ayuso63283dd2014-06-27 18:51:39 +0200608 if (flags == ctx->table->flags)
609 return 0;
610
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200611 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
612 sizeof(struct nft_trans_table));
613 if (trans == NULL)
614 return -ENOMEM;
615
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200616 if ((flags & NFT_TABLE_F_DORMANT) &&
617 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200618 nft_trans_table_enable(trans) = false;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200619 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
620 ctx->table->flags & NFT_TABLE_F_DORMANT) {
621 ret = nf_tables_table_enable(ctx->afi, ctx->table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200622 if (ret >= 0) {
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200623 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200624 nft_trans_table_enable(trans) = true;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200625 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200626 }
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200627 if (ret < 0)
628 goto err;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200629
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200630 nft_trans_table_update(trans) = true;
631 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
632 return 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200633err:
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200634 nft_trans_destroy(trans);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200635 return ret;
636}
637
Patrick McHardy96518512013-10-14 11:00:02 +0200638static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
639 const struct nlmsghdr *nlh,
640 const struct nlattr * const nla[])
641{
642 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
643 const struct nlattr *name;
644 struct nft_af_info *afi;
645 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200646 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200647 int family = nfmsg->nfgen_family;
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000648 u32 flags = 0;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200649 struct nft_ctx ctx;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200650 int err;
Patrick McHardy96518512013-10-14 11:00:02 +0200651
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200652 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200653 if (IS_ERR(afi))
654 return PTR_ERR(afi);
655
656 name = nla[NFTA_TABLE_NAME];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200657 table = nf_tables_table_lookup(afi, name);
Patrick McHardy96518512013-10-14 11:00:02 +0200658 if (IS_ERR(table)) {
659 if (PTR_ERR(table) != -ENOENT)
660 return PTR_ERR(table);
661 table = NULL;
662 }
663
664 if (table != NULL) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200665 if (table->flags & NFT_TABLE_INACTIVE)
666 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200667 if (nlh->nlmsg_flags & NLM_F_EXCL)
668 return -EEXIST;
669 if (nlh->nlmsg_flags & NLM_F_REPLACE)
670 return -EOPNOTSUPP;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200671
672 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
673 return nf_tables_updtable(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +0200674 }
675
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000676 if (nla[NFTA_TABLE_FLAGS]) {
677 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
678 if (flags & ~NFT_TABLE_F_DORMANT)
679 return -EINVAL;
680 }
681
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000682 if (!try_module_get(afi->owner))
683 return -EAFNOSUPPORT;
684
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100685 err = -ENOMEM;
Pablo Neira Ayuso1cae5652015-03-05 15:05:36 +0100686 table = kzalloc(sizeof(*table), GFP_KERNEL);
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100687 if (table == NULL)
688 goto err1;
Patrick McHardy96518512013-10-14 11:00:02 +0200689
Pablo Neira Ayuso1cae5652015-03-05 15:05:36 +0100690 nla_strlcpy(table->name, name, NFT_TABLE_MAXNAMELEN);
Patrick McHardy96518512013-10-14 11:00:02 +0200691 INIT_LIST_HEAD(&table->chains);
Patrick McHardy20a69342013-10-11 12:06:22 +0200692 INIT_LIST_HEAD(&table->sets);
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000693 table->flags = flags;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200694
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200695 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
696 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100697 if (err < 0)
698 goto err2;
699
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200700 list_add_tail_rcu(&table->list, &afi->tables);
Patrick McHardy96518512013-10-14 11:00:02 +0200701 return 0;
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100702err2:
703 kfree(table);
704err1:
705 module_put(afi->owner);
706 return err;
Patrick McHardy96518512013-10-14 11:00:02 +0200707}
708
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200709static int nft_flush_table(struct nft_ctx *ctx)
710{
711 int err;
712 struct nft_chain *chain, *nc;
713 struct nft_set *set, *ns;
714
Pablo Neira Ayusoa2f18db2015-01-04 15:14:22 +0100715 list_for_each_entry(chain, &ctx->table->chains, list) {
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200716 ctx->chain = chain;
717
718 err = nft_delrule_by_chain(ctx);
719 if (err < 0)
720 goto out;
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200721 }
722
723 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
724 if (set->flags & NFT_SET_ANONYMOUS &&
725 !list_empty(&set->bindings))
726 continue;
727
728 err = nft_delset(ctx, set);
729 if (err < 0)
730 goto out;
731 }
732
Pablo Neira Ayusoa2f18db2015-01-04 15:14:22 +0100733 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
734 ctx->chain = chain;
735
736 err = nft_delchain(ctx);
737 if (err < 0)
738 goto out;
739 }
740
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200741 err = nft_deltable(ctx);
742out:
743 return err;
744}
745
746static int nft_flush(struct nft_ctx *ctx, int family)
747{
748 struct nft_af_info *afi;
749 struct nft_table *table, *nt;
750 const struct nlattr * const *nla = ctx->nla;
751 int err = 0;
752
753 list_for_each_entry(afi, &ctx->net->nft.af_info, list) {
754 if (family != AF_UNSPEC && afi->family != family)
755 continue;
756
757 ctx->afi = afi;
758 list_for_each_entry_safe(table, nt, &afi->tables, list) {
759 if (nla[NFTA_TABLE_NAME] &&
760 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
761 continue;
762
763 ctx->table = table;
764
765 err = nft_flush_table(ctx);
766 if (err < 0)
767 goto out;
768 }
769 }
770out:
771 return err;
772}
773
Patrick McHardy96518512013-10-14 11:00:02 +0200774static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
775 const struct nlmsghdr *nlh,
776 const struct nlattr * const nla[])
777{
778 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
779 struct nft_af_info *afi;
780 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200781 struct net *net = sock_net(skb->sk);
Arturo Borreroee01d542014-09-02 16:42:25 +0200782 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200783 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +0200784
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200785 nft_ctx_init(&ctx, skb, nlh, NULL, NULL, NULL, nla);
786 if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL)
787 return nft_flush(&ctx, family);
788
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200789 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200790 if (IS_ERR(afi))
791 return PTR_ERR(afi);
792
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200793 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200794 if (IS_ERR(table))
795 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200796 if (table->flags & NFT_TABLE_INACTIVE)
797 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200798
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200799 ctx.afi = afi;
800 ctx.table = table;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200801
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200802 return nft_flush_table(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +0200803}
804
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200805static void nf_tables_table_destroy(struct nft_ctx *ctx)
806{
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +0200807 BUG_ON(ctx->table->use > 0);
808
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200809 kfree(ctx->table);
810 module_put(ctx->afi->owner);
811}
812
Patrick McHardy2a37d752014-01-09 18:42:37 +0000813int nft_register_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200814{
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200815 int err = 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200816
817 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200818 if (chain_type[ctype->family][ctype->type] != NULL) {
819 err = -EBUSY;
820 goto out;
Patrick McHardy96518512013-10-14 11:00:02 +0200821 }
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200822 chain_type[ctype->family][ctype->type] = ctype;
823out:
Patrick McHardy96518512013-10-14 11:00:02 +0200824 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
825 return err;
826}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200827EXPORT_SYMBOL_GPL(nft_register_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200828
Patrick McHardy2a37d752014-01-09 18:42:37 +0000829void nft_unregister_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200830{
Patrick McHardy96518512013-10-14 11:00:02 +0200831 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200832 chain_type[ctype->family][ctype->type] = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200833 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
834}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200835EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200836
837/*
838 * Chains
839 */
840
841static struct nft_chain *
842nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
843{
844 struct nft_chain *chain;
845
846 list_for_each_entry(chain, &table->chains, list) {
847 if (chain->handle == handle)
848 return chain;
849 }
850
851 return ERR_PTR(-ENOENT);
852}
853
854static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
855 const struct nlattr *nla)
856{
857 struct nft_chain *chain;
858
859 if (nla == NULL)
860 return ERR_PTR(-EINVAL);
861
862 list_for_each_entry(chain, &table->chains, list) {
863 if (!nla_strcmp(nla, chain->name))
864 return chain;
865 }
866
867 return ERR_PTR(-ENOENT);
868}
869
870static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
871 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
872 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
873 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
874 .len = NFT_CHAIN_MAXNAMELEN - 1 },
875 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200876 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
Pablo Neira4c1f7812014-03-31 17:43:47 +0200877 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200878 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
Patrick McHardy96518512013-10-14 11:00:02 +0200879};
880
881static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
882 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
883 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
884};
885
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200886static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
887{
888 struct nft_stats *cpu_stats, total;
889 struct nlattr *nest;
Eric Dumazetce355e22014-07-09 15:14:06 +0200890 unsigned int seq;
891 u64 pkts, bytes;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200892 int cpu;
893
894 memset(&total, 0, sizeof(total));
895 for_each_possible_cpu(cpu) {
896 cpu_stats = per_cpu_ptr(stats, cpu);
Eric Dumazetce355e22014-07-09 15:14:06 +0200897 do {
898 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
899 pkts = cpu_stats->pkts;
900 bytes = cpu_stats->bytes;
901 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
902 total.pkts += pkts;
903 total.bytes += bytes;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200904 }
905 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
906 if (nest == NULL)
907 goto nla_put_failure;
908
909 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
910 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
911 goto nla_put_failure;
912
913 nla_nest_end(skb, nest);
914 return 0;
915
916nla_put_failure:
917 return -ENOSPC;
918}
919
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200920static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
921 u32 portid, u32 seq, int event, u32 flags,
922 int family, const struct nft_table *table,
Patrick McHardy96518512013-10-14 11:00:02 +0200923 const struct nft_chain *chain)
924{
925 struct nlmsghdr *nlh;
926 struct nfgenmsg *nfmsg;
927
928 event |= NFNL_SUBSYS_NFTABLES << 8;
929 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
930 if (nlh == NULL)
931 goto nla_put_failure;
932
933 nfmsg = nlmsg_data(nlh);
934 nfmsg->nfgen_family = family;
935 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200936 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +0200937
938 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
939 goto nla_put_failure;
940 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
941 goto nla_put_failure;
942 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
943 goto nla_put_failure;
944
945 if (chain->flags & NFT_BASE_CHAIN) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200946 const struct nft_base_chain *basechain = nft_base_chain(chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000947 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200948 struct nlattr *nest;
949
950 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
Patrick McHardy96518512013-10-14 11:00:02 +0200951 if (nest == NULL)
952 goto nla_put_failure;
953 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
954 goto nla_put_failure;
955 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
956 goto nla_put_failure;
957 nla_nest_end(skb, nest);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200958
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200959 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
960 htonl(basechain->policy)))
961 goto nla_put_failure;
962
Patrick McHardybaae3e62014-01-09 18:42:34 +0000963 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
964 goto nla_put_failure;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200965
966 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
967 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +0200968 }
969
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200970 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
971 goto nla_put_failure;
972
Johannes Berg053c0952015-01-16 22:09:00 +0100973 nlmsg_end(skb, nlh);
974 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200975
976nla_put_failure:
977 nlmsg_trim(skb, nlh);
978 return -1;
979}
980
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200981static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
Patrick McHardy96518512013-10-14 11:00:02 +0200982{
983 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +0200984 int err;
985
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200986 if (!ctx->report &&
987 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +0200988 return 0;
989
990 err = -ENOBUFS;
991 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
992 if (skb == NULL)
993 goto err;
994
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200995 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
996 event, 0, ctx->afi->family, ctx->table,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200997 ctx->chain);
Patrick McHardy96518512013-10-14 11:00:02 +0200998 if (err < 0) {
999 kfree_skb(skb);
1000 goto err;
1001 }
1002
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001003 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1004 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001005err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001006 if (err < 0) {
1007 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1008 err);
1009 }
Patrick McHardy96518512013-10-14 11:00:02 +02001010 return err;
1011}
1012
1013static int nf_tables_dump_chains(struct sk_buff *skb,
1014 struct netlink_callback *cb)
1015{
1016 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1017 const struct nft_af_info *afi;
1018 const struct nft_table *table;
1019 const struct nft_chain *chain;
1020 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001021 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001022 int family = nfmsg->nfgen_family;
1023
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001024 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001025 cb->seq = net->nft.base_seq;
1026
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001027 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001028 if (family != NFPROTO_UNSPEC && family != afi->family)
1029 continue;
1030
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001031 list_for_each_entry_rcu(table, &afi->tables, list) {
1032 list_for_each_entry_rcu(chain, &table->chains, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001033 if (idx < s_idx)
1034 goto cont;
1035 if (idx > s_idx)
1036 memset(&cb->args[1], 0,
1037 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001038 if (nf_tables_fill_chain_info(skb, net,
1039 NETLINK_CB(cb->skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001040 cb->nlh->nlmsg_seq,
1041 NFT_MSG_NEWCHAIN,
1042 NLM_F_MULTI,
1043 afi->family, table, chain) < 0)
1044 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001045
1046 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +02001047cont:
1048 idx++;
1049 }
1050 }
1051 }
1052done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001053 rcu_read_unlock();
Patrick McHardy96518512013-10-14 11:00:02 +02001054 cb->args[0] = idx;
1055 return skb->len;
1056}
1057
Patrick McHardy96518512013-10-14 11:00:02 +02001058static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
1059 const struct nlmsghdr *nlh,
1060 const struct nlattr * const nla[])
1061{
1062 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1063 const struct nft_af_info *afi;
1064 const struct nft_table *table;
1065 const struct nft_chain *chain;
1066 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001067 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001068 int family = nfmsg->nfgen_family;
1069 int err;
1070
1071 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1072 struct netlink_dump_control c = {
1073 .dump = nf_tables_dump_chains,
1074 };
1075 return netlink_dump_start(nlsk, skb, nlh, &c);
1076 }
1077
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001078 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001079 if (IS_ERR(afi))
1080 return PTR_ERR(afi);
1081
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001082 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001083 if (IS_ERR(table))
1084 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001085 if (table->flags & NFT_TABLE_INACTIVE)
1086 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001087
1088 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1089 if (IS_ERR(chain))
1090 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001091 if (chain->flags & NFT_CHAIN_INACTIVE)
1092 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001093
1094 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1095 if (!skb2)
1096 return -ENOMEM;
1097
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001098 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001099 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
1100 family, table, chain);
1101 if (err < 0)
1102 goto err;
1103
1104 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1105
1106err:
1107 kfree_skb(skb2);
1108 return err;
1109}
1110
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001111static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1112 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
1113 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
1114};
1115
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001116static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001117{
1118 struct nlattr *tb[NFTA_COUNTER_MAX+1];
1119 struct nft_stats __percpu *newstats;
1120 struct nft_stats *stats;
1121 int err;
1122
1123 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
1124 if (err < 0)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001125 return ERR_PTR(err);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001126
1127 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001128 return ERR_PTR(-EINVAL);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001129
Eric Dumazetce355e22014-07-09 15:14:06 +02001130 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001131 if (newstats == NULL)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001132 return ERR_PTR(-ENOMEM);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001133
1134 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1135 * are not exposed to userspace.
1136 */
Pablo Neira Ayusoe8781f72015-01-21 18:04:18 +01001137 preempt_disable();
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001138 stats = this_cpu_ptr(newstats);
1139 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1140 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
Pablo Neira Ayusoe8781f72015-01-21 18:04:18 +01001141 preempt_enable();
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001142
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001143 return newstats;
1144}
1145
1146static void nft_chain_stats_replace(struct nft_base_chain *chain,
1147 struct nft_stats __percpu *newstats)
1148{
Pablo Neira Ayusob88825d2014-08-05 17:25:59 +02001149 if (newstats == NULL)
1150 return;
1151
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001152 if (chain->stats) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001153 struct nft_stats __percpu *oldstats =
Patrick McHardy67a8fc22014-02-18 18:06:49 +00001154 nft_dereference(chain->stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001155
1156 rcu_assign_pointer(chain->stats, newstats);
1157 synchronize_rcu();
1158 free_percpu(oldstats);
1159 } else
1160 rcu_assign_pointer(chain->stats, newstats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001161}
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001162
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001163static void nf_tables_chain_destroy(struct nft_chain *chain)
1164{
1165 BUG_ON(chain->use > 0);
1166
1167 if (chain->flags & NFT_BASE_CHAIN) {
1168 module_put(nft_base_chain(chain)->type->owner);
1169 free_percpu(nft_base_chain(chain)->stats);
1170 kfree(nft_base_chain(chain));
1171 } else {
1172 kfree(chain);
1173 }
1174}
1175
Patrick McHardy96518512013-10-14 11:00:02 +02001176static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
1177 const struct nlmsghdr *nlh,
1178 const struct nlattr * const nla[])
1179{
1180 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1181 const struct nlattr * uninitialized_var(name);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001182 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001183 struct nft_table *table;
1184 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001185 struct nft_base_chain *basechain = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001186 struct nlattr *ha[NFTA_HOOK_MAX + 1];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001187 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001188 int family = nfmsg->nfgen_family;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001189 u8 policy = NF_ACCEPT;
Patrick McHardy96518512013-10-14 11:00:02 +02001190 u64 handle = 0;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001191 unsigned int i;
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001192 struct nft_stats __percpu *stats;
Patrick McHardy96518512013-10-14 11:00:02 +02001193 int err;
1194 bool create;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001195 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001196
1197 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1198
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001199 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +02001200 if (IS_ERR(afi))
1201 return PTR_ERR(afi);
1202
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001203 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001204 if (IS_ERR(table))
1205 return PTR_ERR(table);
1206
Patrick McHardy96518512013-10-14 11:00:02 +02001207 chain = NULL;
1208 name = nla[NFTA_CHAIN_NAME];
1209
1210 if (nla[NFTA_CHAIN_HANDLE]) {
1211 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
1212 chain = nf_tables_chain_lookup_byhandle(table, handle);
1213 if (IS_ERR(chain))
1214 return PTR_ERR(chain);
1215 } else {
1216 chain = nf_tables_chain_lookup(table, name);
1217 if (IS_ERR(chain)) {
1218 if (PTR_ERR(chain) != -ENOENT)
1219 return PTR_ERR(chain);
1220 chain = NULL;
1221 }
1222 }
1223
Patrick McHardy57de2a02014-01-09 18:42:31 +00001224 if (nla[NFTA_CHAIN_POLICY]) {
1225 if ((chain != NULL &&
Pablo Neira Ayusod6b6cb12015-03-17 13:21:42 +01001226 !(chain->flags & NFT_BASE_CHAIN)))
1227 return -EOPNOTSUPP;
1228
1229 if (chain == NULL &&
Patrick McHardy57de2a02014-01-09 18:42:31 +00001230 nla[NFTA_CHAIN_HOOK] == NULL)
1231 return -EOPNOTSUPP;
1232
Pablo Neira Ayuso8f46df12014-01-10 15:11:25 +01001233 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
Patrick McHardy57de2a02014-01-09 18:42:31 +00001234 switch (policy) {
1235 case NF_DROP:
1236 case NF_ACCEPT:
1237 break;
1238 default:
1239 return -EINVAL;
1240 }
1241 }
1242
Patrick McHardy96518512013-10-14 11:00:02 +02001243 if (chain != NULL) {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001244 struct nft_stats *stats = NULL;
1245 struct nft_trans *trans;
1246
1247 if (chain->flags & NFT_CHAIN_INACTIVE)
1248 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001249 if (nlh->nlmsg_flags & NLM_F_EXCL)
1250 return -EEXIST;
1251 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1252 return -EOPNOTSUPP;
1253
1254 if (nla[NFTA_CHAIN_HANDLE] && name &&
1255 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1256 return -EEXIST;
1257
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001258 if (nla[NFTA_CHAIN_COUNTERS]) {
1259 if (!(chain->flags & NFT_BASE_CHAIN))
1260 return -EOPNOTSUPP;
1261
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001262 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1263 if (IS_ERR(stats))
1264 return PTR_ERR(stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001265 }
1266
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001267 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1268 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1269 sizeof(struct nft_trans_chain));
Pablo Neira Ayusof5553c12015-01-29 19:08:09 +01001270 if (trans == NULL) {
1271 free_percpu(stats);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001272 return -ENOMEM;
Pablo Neira Ayusof5553c12015-01-29 19:08:09 +01001273 }
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001274
1275 nft_trans_chain_stats(trans) = stats;
1276 nft_trans_chain_update(trans) = true;
1277
Patrick McHardy4401a862014-01-09 18:42:32 +00001278 if (nla[NFTA_CHAIN_POLICY])
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001279 nft_trans_chain_policy(trans) = policy;
1280 else
1281 nft_trans_chain_policy(trans) = -1;
Patrick McHardy4401a862014-01-09 18:42:32 +00001282
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001283 if (nla[NFTA_CHAIN_HANDLE] && name) {
1284 nla_strlcpy(nft_trans_chain_name(trans), name,
1285 NFT_CHAIN_MAXNAMELEN);
1286 }
1287 list_add_tail(&trans->list, &net->nft.commit_list);
1288 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001289 }
1290
Patrick McHardy75820672014-01-09 18:42:33 +00001291 if (table->use == UINT_MAX)
1292 return -EOVERFLOW;
1293
Patrick McHardy96518512013-10-14 11:00:02 +02001294 if (nla[NFTA_CHAIN_HOOK]) {
Patrick McHardy2a37d752014-01-09 18:42:37 +00001295 const struct nf_chain_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001296 struct nf_hook_ops *ops;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001297 nf_hookfn *hookfn;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001298 u32 hooknum, priority;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001299
Patrick McHardybaae3e62014-01-09 18:42:34 +00001300 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001301 if (nla[NFTA_CHAIN_TYPE]) {
1302 type = nf_tables_chain_type_lookup(afi,
1303 nla[NFTA_CHAIN_TYPE],
1304 create);
Patrick McHardy93b08062014-01-09 18:42:36 +00001305 if (IS_ERR(type))
1306 return PTR_ERR(type);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001307 }
Patrick McHardy96518512013-10-14 11:00:02 +02001308
1309 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1310 nft_hook_policy);
1311 if (err < 0)
1312 return err;
1313 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1314 ha[NFTA_HOOK_PRIORITY] == NULL)
1315 return -EINVAL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001316
1317 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1318 if (hooknum >= afi->nhooks)
Patrick McHardy96518512013-10-14 11:00:02 +02001319 return -EINVAL;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001320 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
Patrick McHardy96518512013-10-14 11:00:02 +02001321
Patrick McHardybaae3e62014-01-09 18:42:34 +00001322 if (!(type->hook_mask & (1 << hooknum)))
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001323 return -EOPNOTSUPP;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001324 if (!try_module_get(type->owner))
Patrick McHardybaae3e62014-01-09 18:42:34 +00001325 return -ENOENT;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001326 hookfn = type->hooks[hooknum];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001327
Patrick McHardy96518512013-10-14 11:00:02 +02001328 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
Pablo Neira Ayusof5553c12015-01-29 19:08:09 +01001329 if (basechain == NULL) {
1330 module_put(type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001331 return -ENOMEM;
Pablo Neira Ayusof5553c12015-01-29 19:08:09 +01001332 }
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001333
Patrick McHardy4401a862014-01-09 18:42:32 +00001334 if (nla[NFTA_CHAIN_COUNTERS]) {
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001335 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1336 if (IS_ERR(stats)) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001337 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +00001338 kfree(basechain);
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001339 return PTR_ERR(stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001340 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001341 basechain->stats = stats;
Patrick McHardy4401a862014-01-09 18:42:32 +00001342 } else {
Eric Dumazetce355e22014-07-09 15:14:06 +02001343 stats = netdev_alloc_pcpu_stats(struct nft_stats);
Sabrina Dubrocac123bb72014-10-21 11:08:21 +02001344 if (stats == NULL) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001345 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +00001346 kfree(basechain);
Sabrina Dubrocac123bb72014-10-21 11:08:21 +02001347 return -ENOMEM;
Patrick McHardy4401a862014-01-09 18:42:32 +00001348 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001349 rcu_assign_pointer(basechain->stats, stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001350 }
1351
Patrick McHardy5ebb3352015-03-21 15:19:15 +00001352 write_pnet(&basechain->pnet, net);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001353 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +02001354 chain = &basechain->chain;
1355
Patrick McHardy115a60b2014-01-03 12:16:15 +00001356 for (i = 0; i < afi->nops; i++) {
1357 ops = &basechain->ops[i];
1358 ops->pf = family;
1359 ops->owner = afi->owner;
1360 ops->hooknum = hooknum;
1361 ops->priority = priority;
1362 ops->priv = chain;
1363 ops->hook = afi->hooks[ops->hooknum];
1364 if (hookfn)
1365 ops->hook = hookfn;
1366 if (afi->hook_ops_init)
1367 afi->hook_ops_init(ops, i);
1368 }
Patrick McHardy96518512013-10-14 11:00:02 +02001369
1370 chain->flags |= NFT_BASE_CHAIN;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001371 basechain->policy = policy;
Patrick McHardy96518512013-10-14 11:00:02 +02001372 } else {
1373 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1374 if (chain == NULL)
1375 return -ENOMEM;
1376 }
1377
1378 INIT_LIST_HEAD(&chain->rules);
1379 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +02001380 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +02001381 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1382
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001383 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1384 chain->flags & NFT_BASE_CHAIN) {
Patrick McHardy115a60b2014-01-03 12:16:15 +00001385 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001386 if (err < 0)
1387 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001388 }
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001389
1390 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1391 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1392 if (err < 0)
1393 goto err2;
1394
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001395 table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001396 list_add_tail_rcu(&chain->list, &table->chains);
Patrick McHardy96518512013-10-14 11:00:02 +02001397 return 0;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001398err2:
Arturo Borreroc5598792014-09-02 16:42:23 +02001399 nf_tables_unregister_hooks(table, chain, afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001400err1:
1401 nf_tables_chain_destroy(chain);
1402 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001403}
1404
1405static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1406 const struct nlmsghdr *nlh,
1407 const struct nlattr * const nla[])
1408{
1409 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001410 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001411 struct nft_table *table;
1412 struct nft_chain *chain;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001413 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001414 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001415 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001416
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001417 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001418 if (IS_ERR(afi))
1419 return PTR_ERR(afi);
1420
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001421 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001422 if (IS_ERR(table))
1423 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001424 if (table->flags & NFT_TABLE_INACTIVE)
1425 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001426
1427 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1428 if (IS_ERR(chain))
1429 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001430 if (chain->flags & NFT_CHAIN_INACTIVE)
1431 return -ENOENT;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001432 if (chain->use > 0)
Patrick McHardy96518512013-10-14 11:00:02 +02001433 return -EBUSY;
1434
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001435 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001436
Arturo Borreroee01d542014-09-02 16:42:25 +02001437 return nft_delchain(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +02001438}
1439
Patrick McHardy96518512013-10-14 11:00:02 +02001440/*
1441 * Expressions
1442 */
1443
1444/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001445 * nft_register_expr - register nf_tables expr type
1446 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001447 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001448 * Registers the expr type for use with nf_tables. Returns zero on
Patrick McHardy96518512013-10-14 11:00:02 +02001449 * success or a negative errno code otherwise.
1450 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001451int nft_register_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001452{
1453 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001454 if (type->family == NFPROTO_UNSPEC)
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001455 list_add_tail_rcu(&type->list, &nf_tables_expressions);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001456 else
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001457 list_add_rcu(&type->list, &nf_tables_expressions);
Patrick McHardy96518512013-10-14 11:00:02 +02001458 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1459 return 0;
1460}
1461EXPORT_SYMBOL_GPL(nft_register_expr);
1462
1463/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001464 * nft_unregister_expr - unregister nf_tables expr type
1465 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001466 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001467 * Unregisters the expr typefor use with nf_tables.
Patrick McHardy96518512013-10-14 11:00:02 +02001468 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001469void nft_unregister_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001470{
1471 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001472 list_del_rcu(&type->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001473 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1474}
1475EXPORT_SYMBOL_GPL(nft_unregister_expr);
1476
Patrick McHardy64d46802014-02-05 15:03:37 +00001477static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1478 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001479{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001480 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001481
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001482 list_for_each_entry(type, &nf_tables_expressions, list) {
Patrick McHardy64d46802014-02-05 15:03:37 +00001483 if (!nla_strcmp(nla, type->name) &&
1484 (!type->family || type->family == family))
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001485 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001486 }
1487 return NULL;
1488}
1489
Patrick McHardy64d46802014-02-05 15:03:37 +00001490static const struct nft_expr_type *nft_expr_type_get(u8 family,
1491 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001492{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001493 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001494
1495 if (nla == NULL)
1496 return ERR_PTR(-EINVAL);
1497
Patrick McHardy64d46802014-02-05 15:03:37 +00001498 type = __nft_expr_type_get(family, nla);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001499 if (type != NULL && try_module_get(type->owner))
1500 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001501
1502#ifdef CONFIG_MODULES
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001503 if (type == NULL) {
Patrick McHardy96518512013-10-14 11:00:02 +02001504 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001505 request_module("nft-expr-%u-%.*s", family,
1506 nla_len(nla), (char *)nla_data(nla));
1507 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1508 if (__nft_expr_type_get(family, nla))
1509 return ERR_PTR(-EAGAIN);
1510
1511 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy96518512013-10-14 11:00:02 +02001512 request_module("nft-expr-%.*s",
1513 nla_len(nla), (char *)nla_data(nla));
1514 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001515 if (__nft_expr_type_get(family, nla))
Patrick McHardy96518512013-10-14 11:00:02 +02001516 return ERR_PTR(-EAGAIN);
1517 }
1518#endif
1519 return ERR_PTR(-ENOENT);
1520}
1521
1522static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1523 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1524 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1525};
1526
1527static int nf_tables_fill_expr_info(struct sk_buff *skb,
1528 const struct nft_expr *expr)
1529{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001530 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
Patrick McHardy96518512013-10-14 11:00:02 +02001531 goto nla_put_failure;
1532
1533 if (expr->ops->dump) {
1534 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1535 if (data == NULL)
1536 goto nla_put_failure;
1537 if (expr->ops->dump(skb, expr) < 0)
1538 goto nla_put_failure;
1539 nla_nest_end(skb, data);
1540 }
1541
1542 return skb->len;
1543
1544nla_put_failure:
1545 return -1;
1546};
1547
1548struct nft_expr_info {
1549 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001550 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001551};
1552
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001553static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1554 const struct nlattr *nla,
Patrick McHardy96518512013-10-14 11:00:02 +02001555 struct nft_expr_info *info)
1556{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001557 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001558 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001559 struct nlattr *tb[NFTA_EXPR_MAX + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001560 int err;
1561
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001562 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
Patrick McHardy96518512013-10-14 11:00:02 +02001563 if (err < 0)
1564 return err;
1565
Patrick McHardy64d46802014-02-05 15:03:37 +00001566 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001567 if (IS_ERR(type))
1568 return PTR_ERR(type);
1569
1570 if (tb[NFTA_EXPR_DATA]) {
1571 err = nla_parse_nested(info->tb, type->maxattr,
1572 tb[NFTA_EXPR_DATA], type->policy);
1573 if (err < 0)
1574 goto err1;
1575 } else
1576 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1577
1578 if (type->select_ops != NULL) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001579 ops = type->select_ops(ctx,
1580 (const struct nlattr * const *)info->tb);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001581 if (IS_ERR(ops)) {
1582 err = PTR_ERR(ops);
1583 goto err1;
1584 }
1585 } else
1586 ops = type->ops;
1587
Patrick McHardy96518512013-10-14 11:00:02 +02001588 info->ops = ops;
1589 return 0;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001590
1591err1:
1592 module_put(type->owner);
1593 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001594}
1595
1596static int nf_tables_newexpr(const struct nft_ctx *ctx,
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001597 const struct nft_expr_info *info,
Patrick McHardy96518512013-10-14 11:00:02 +02001598 struct nft_expr *expr)
1599{
1600 const struct nft_expr_ops *ops = info->ops;
1601 int err;
1602
1603 expr->ops = ops;
1604 if (ops->init) {
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001605 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
Patrick McHardy96518512013-10-14 11:00:02 +02001606 if (err < 0)
1607 goto err1;
1608 }
1609
Patrick McHardy96518512013-10-14 11:00:02 +02001610 return 0;
1611
1612err1:
1613 expr->ops = NULL;
1614 return err;
1615}
1616
Patrick McHardy62472bc2014-03-07 19:08:30 +01001617static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1618 struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +02001619{
1620 if (expr->ops->destroy)
Patrick McHardy62472bc2014-03-07 19:08:30 +01001621 expr->ops->destroy(ctx, expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001622 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001623}
1624
1625/*
1626 * Rules
1627 */
1628
1629static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1630 u64 handle)
1631{
1632 struct nft_rule *rule;
1633
1634 // FIXME: this sucks
1635 list_for_each_entry(rule, &chain->rules, list) {
1636 if (handle == rule->handle)
1637 return rule;
1638 }
1639
1640 return ERR_PTR(-ENOENT);
1641}
1642
1643static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1644 const struct nlattr *nla)
1645{
1646 if (nla == NULL)
1647 return ERR_PTR(-EINVAL);
1648
1649 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1650}
1651
1652static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1653 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1654 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1655 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1656 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1657 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001658 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001659 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001660 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1661 .len = NFT_USERDATA_MAXLEN },
Patrick McHardy96518512013-10-14 11:00:02 +02001662};
1663
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001664static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1665 u32 portid, u32 seq, int event,
1666 u32 flags, int family,
Patrick McHardy96518512013-10-14 11:00:02 +02001667 const struct nft_table *table,
1668 const struct nft_chain *chain,
1669 const struct nft_rule *rule)
1670{
1671 struct nlmsghdr *nlh;
1672 struct nfgenmsg *nfmsg;
1673 const struct nft_expr *expr, *next;
1674 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001675 const struct nft_rule *prule;
1676 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001677
Eric Leblond5e948462013-10-10 13:41:44 +02001678 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001679 flags);
1680 if (nlh == NULL)
1681 goto nla_put_failure;
1682
1683 nfmsg = nlmsg_data(nlh);
1684 nfmsg->nfgen_family = family;
1685 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001686 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +02001687
1688 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1689 goto nla_put_failure;
1690 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1691 goto nla_put_failure;
1692 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1693 goto nla_put_failure;
1694
Eric Leblond5e948462013-10-10 13:41:44 +02001695 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1696 prule = list_entry(rule->list.prev, struct nft_rule, list);
1697 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1698 cpu_to_be64(prule->handle)))
1699 goto nla_put_failure;
1700 }
1701
Patrick McHardy96518512013-10-14 11:00:02 +02001702 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1703 if (list == NULL)
1704 goto nla_put_failure;
1705 nft_rule_for_each_expr(expr, next, rule) {
1706 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1707 if (elem == NULL)
1708 goto nla_put_failure;
1709 if (nf_tables_fill_expr_info(skb, expr) < 0)
1710 goto nla_put_failure;
1711 nla_nest_end(skb, elem);
1712 }
1713 nla_nest_end(skb, list);
1714
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001715 if (rule->udata) {
1716 struct nft_userdata *udata = nft_userdata(rule);
1717 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
1718 udata->data) < 0)
1719 goto nla_put_failure;
1720 }
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001721
Johannes Berg053c0952015-01-16 22:09:00 +01001722 nlmsg_end(skb, nlh);
1723 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001724
1725nla_put_failure:
1726 nlmsg_trim(skb, nlh);
1727 return -1;
1728}
1729
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001730static int nf_tables_rule_notify(const struct nft_ctx *ctx,
Patrick McHardy96518512013-10-14 11:00:02 +02001731 const struct nft_rule *rule,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001732 int event)
Patrick McHardy96518512013-10-14 11:00:02 +02001733{
1734 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +02001735 int err;
1736
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001737 if (!ctx->report &&
1738 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +02001739 return 0;
1740
1741 err = -ENOBUFS;
1742 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1743 if (skb == NULL)
1744 goto err;
1745
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001746 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
1747 event, 0, ctx->afi->family, ctx->table,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001748 ctx->chain, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001749 if (err < 0) {
1750 kfree_skb(skb);
1751 goto err;
1752 }
1753
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001754 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1755 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001756err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001757 if (err < 0) {
1758 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1759 err);
1760 }
Patrick McHardy96518512013-10-14 11:00:02 +02001761 return err;
1762}
1763
1764static int nf_tables_dump_rules(struct sk_buff *skb,
1765 struct netlink_callback *cb)
1766{
1767 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1768 const struct nft_af_info *afi;
1769 const struct nft_table *table;
1770 const struct nft_chain *chain;
1771 const struct nft_rule *rule;
1772 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001773 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001774 int family = nfmsg->nfgen_family;
1775
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001776 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001777 cb->seq = net->nft.base_seq;
1778
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001779 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001780 if (family != NFPROTO_UNSPEC && family != afi->family)
1781 continue;
1782
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001783 list_for_each_entry_rcu(table, &afi->tables, list) {
1784 list_for_each_entry_rcu(chain, &table->chains, list) {
1785 list_for_each_entry_rcu(rule, &chain->rules, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001786 if (!nft_rule_is_active(net, rule))
1787 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001788 if (idx < s_idx)
1789 goto cont;
1790 if (idx > s_idx)
1791 memset(&cb->args[1], 0,
1792 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001793 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001794 cb->nlh->nlmsg_seq,
1795 NFT_MSG_NEWRULE,
1796 NLM_F_MULTI | NLM_F_APPEND,
1797 afi->family, table, chain, rule) < 0)
1798 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001799
1800 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +02001801cont:
1802 idx++;
1803 }
1804 }
1805 }
1806 }
1807done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001808 rcu_read_unlock();
1809
Patrick McHardy96518512013-10-14 11:00:02 +02001810 cb->args[0] = idx;
1811 return skb->len;
1812}
1813
1814static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1815 const struct nlmsghdr *nlh,
1816 const struct nlattr * const nla[])
1817{
1818 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1819 const struct nft_af_info *afi;
1820 const struct nft_table *table;
1821 const struct nft_chain *chain;
1822 const struct nft_rule *rule;
1823 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001824 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001825 int family = nfmsg->nfgen_family;
1826 int err;
1827
1828 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1829 struct netlink_dump_control c = {
1830 .dump = nf_tables_dump_rules,
1831 };
1832 return netlink_dump_start(nlsk, skb, nlh, &c);
1833 }
1834
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001835 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001836 if (IS_ERR(afi))
1837 return PTR_ERR(afi);
1838
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001839 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001840 if (IS_ERR(table))
1841 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001842 if (table->flags & NFT_TABLE_INACTIVE)
1843 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001844
1845 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1846 if (IS_ERR(chain))
1847 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001848 if (chain->flags & NFT_CHAIN_INACTIVE)
1849 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001850
1851 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1852 if (IS_ERR(rule))
1853 return PTR_ERR(rule);
1854
1855 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1856 if (!skb2)
1857 return -ENOMEM;
1858
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001859 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001860 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1861 family, table, chain, rule);
1862 if (err < 0)
1863 goto err;
1864
1865 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1866
1867err:
1868 kfree_skb(skb2);
1869 return err;
1870}
1871
Patrick McHardy62472bc2014-03-07 19:08:30 +01001872static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1873 struct nft_rule *rule)
Patrick McHardy96518512013-10-14 11:00:02 +02001874{
Patrick McHardy96518512013-10-14 11:00:02 +02001875 struct nft_expr *expr;
1876
1877 /*
1878 * Careful: some expressions might not be initialized in case this
1879 * is called on error from nf_tables_newrule().
1880 */
1881 expr = nft_expr_first(rule);
1882 while (expr->ops && expr != nft_expr_last(rule)) {
Patrick McHardy62472bc2014-03-07 19:08:30 +01001883 nf_tables_expr_destroy(ctx, expr);
Patrick McHardy96518512013-10-14 11:00:02 +02001884 expr = nft_expr_next(expr);
1885 }
1886 kfree(rule);
1887}
1888
Patrick McHardy96518512013-10-14 11:00:02 +02001889#define NFT_RULE_MAXEXPRS 128
1890
1891static struct nft_expr_info *info;
1892
1893static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1894 const struct nlmsghdr *nlh,
1895 const struct nlattr * const nla[])
1896{
1897 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001898 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001899 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001900 struct nft_table *table;
1901 struct nft_chain *chain;
1902 struct nft_rule *rule, *old_rule = NULL;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001903 struct nft_userdata *udata;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001904 struct nft_trans *trans = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001905 struct nft_expr *expr;
1906 struct nft_ctx ctx;
1907 struct nlattr *tmp;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001908 unsigned int size, i, n, ulen = 0, usize = 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001909 int err, rem;
1910 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02001911 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02001912
1913 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1914
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001915 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02001916 if (IS_ERR(afi))
1917 return PTR_ERR(afi);
1918
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001919 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001920 if (IS_ERR(table))
1921 return PTR_ERR(table);
1922
1923 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1924 if (IS_ERR(chain))
1925 return PTR_ERR(chain);
1926
1927 if (nla[NFTA_RULE_HANDLE]) {
1928 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1929 rule = __nf_tables_rule_lookup(chain, handle);
1930 if (IS_ERR(rule))
1931 return PTR_ERR(rule);
1932
1933 if (nlh->nlmsg_flags & NLM_F_EXCL)
1934 return -EEXIST;
1935 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1936 old_rule = rule;
1937 else
1938 return -EOPNOTSUPP;
1939 } else {
1940 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1941 return -EINVAL;
1942 handle = nf_tables_alloc_handle(table);
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +02001943
1944 if (chain->use == UINT_MAX)
1945 return -EOVERFLOW;
Patrick McHardy96518512013-10-14 11:00:02 +02001946 }
1947
Eric Leblond5e948462013-10-10 13:41:44 +02001948 if (nla[NFTA_RULE_POSITION]) {
1949 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1950 return -EOPNOTSUPP;
1951
1952 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1953 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1954 if (IS_ERR(old_rule))
1955 return PTR_ERR(old_rule);
1956 }
1957
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001958 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1959
Patrick McHardy96518512013-10-14 11:00:02 +02001960 n = 0;
1961 size = 0;
1962 if (nla[NFTA_RULE_EXPRESSIONS]) {
1963 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1964 err = -EINVAL;
1965 if (nla_type(tmp) != NFTA_LIST_ELEM)
1966 goto err1;
1967 if (n == NFT_RULE_MAXEXPRS)
1968 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001969 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02001970 if (err < 0)
1971 goto err1;
1972 size += info[n].ops->size;
1973 n++;
1974 }
1975 }
Patrick McHardy98898402015-03-03 20:04:19 +00001976 /* Check for overflow of dlen field */
1977 err = -EFBIG;
1978 if (size >= 1 << 12)
1979 goto err1;
Patrick McHardy96518512013-10-14 11:00:02 +02001980
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001981 if (nla[NFTA_RULE_USERDATA]) {
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001982 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001983 if (ulen > 0)
1984 usize = sizeof(struct nft_userdata) + ulen;
1985 }
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001986
Patrick McHardy96518512013-10-14 11:00:02 +02001987 err = -ENOMEM;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001988 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001989 if (rule == NULL)
1990 goto err1;
1991
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001992 nft_rule_activate_next(net, rule);
1993
Patrick McHardy96518512013-10-14 11:00:02 +02001994 rule->handle = handle;
1995 rule->dlen = size;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001996 rule->udata = ulen ? 1 : 0;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001997
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001998 if (ulen) {
1999 udata = nft_userdata(rule);
2000 udata->len = ulen - 1;
2001 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
2002 }
Patrick McHardy96518512013-10-14 11:00:02 +02002003
Patrick McHardy96518512013-10-14 11:00:02 +02002004 expr = nft_expr_first(rule);
2005 for (i = 0; i < n; i++) {
2006 err = nf_tables_newexpr(&ctx, &info[i], expr);
2007 if (err < 0)
2008 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002009 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02002010 expr = nft_expr_next(expr);
2011 }
2012
Patrick McHardy96518512013-10-14 11:00:02 +02002013 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002014 if (nft_rule_is_active_next(net, old_rule)) {
Pablo Neira Ayusoac904ac2014-06-10 10:53:03 +02002015 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02002016 old_rule);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02002017 if (trans == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002018 err = -ENOMEM;
2019 goto err2;
2020 }
Arturo Borreroee01d542014-09-02 16:42:25 +02002021 nft_rule_deactivate_next(net, old_rule);
Pablo Neira Ayusoac34b862014-06-10 10:53:02 +02002022 chain->use--;
Pablo Neira Ayuso5bc5c302014-06-10 10:53:00 +02002023 list_add_tail_rcu(&rule->list, &old_rule->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002024 } else {
2025 err = -ENOENT;
2026 goto err2;
2027 }
Patrick McHardy96518512013-10-14 11:00:02 +02002028 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02002029 if (old_rule)
2030 list_add_rcu(&rule->list, &old_rule->list);
2031 else
2032 list_add_tail_rcu(&rule->list, &chain->rules);
2033 else {
2034 if (old_rule)
2035 list_add_tail_rcu(&rule->list, &old_rule->list);
2036 else
2037 list_add_rcu(&rule->list, &chain->rules);
2038 }
Patrick McHardy96518512013-10-14 11:00:02 +02002039
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02002040 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002041 err = -ENOMEM;
2042 goto err3;
2043 }
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02002044 chain->use++;
Patrick McHardy96518512013-10-14 11:00:02 +02002045 return 0;
2046
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002047err3:
2048 list_del_rcu(&rule->list);
Patrick McHardy96518512013-10-14 11:00:02 +02002049err2:
Patrick McHardy62472bc2014-03-07 19:08:30 +01002050 nf_tables_rule_destroy(&ctx, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02002051err1:
2052 for (i = 0; i < n; i++) {
2053 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002054 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02002055 }
2056 return err;
2057}
2058
2059static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
2060 const struct nlmsghdr *nlh,
2061 const struct nlattr * const nla[])
2062{
2063 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002064 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002065 struct net *net = sock_net(skb->sk);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002066 struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002067 struct nft_chain *chain = NULL;
2068 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002069 int family = nfmsg->nfgen_family, err = 0;
2070 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02002071
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002072 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02002073 if (IS_ERR(afi))
2074 return PTR_ERR(afi);
2075
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002076 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02002077 if (IS_ERR(table))
2078 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002079 if (table->flags & NFT_TABLE_INACTIVE)
2080 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02002081
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002082 if (nla[NFTA_RULE_CHAIN]) {
2083 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
2084 if (IS_ERR(chain))
2085 return PTR_ERR(chain);
2086 }
Patrick McHardy96518512013-10-14 11:00:02 +02002087
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002088 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
2089
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002090 if (chain) {
2091 if (nla[NFTA_RULE_HANDLE]) {
2092 rule = nf_tables_rule_lookup(chain,
2093 nla[NFTA_RULE_HANDLE]);
2094 if (IS_ERR(rule))
2095 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02002096
Arturo Borrero5e266fe2014-09-02 16:42:21 +02002097 err = nft_delrule(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002098 } else {
Arturo Borreroce24b722014-09-02 16:42:24 +02002099 err = nft_delrule_by_chain(&ctx);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002100 }
2101 } else {
2102 list_for_each_entry(chain, &table->chains, list) {
2103 ctx.chain = chain;
Arturo Borreroce24b722014-09-02 16:42:24 +02002104 err = nft_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002105 if (err < 0)
2106 break;
Patrick McHardy96518512013-10-14 11:00:02 +02002107 }
2108 }
2109
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002110 return err;
2111}
2112
Patrick McHardy20a69342013-10-11 12:06:22 +02002113/*
2114 * Sets
2115 */
2116
2117static LIST_HEAD(nf_tables_set_ops);
2118
2119int nft_register_set(struct nft_set_ops *ops)
2120{
2121 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002122 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
Patrick McHardy20a69342013-10-11 12:06:22 +02002123 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2124 return 0;
2125}
2126EXPORT_SYMBOL_GPL(nft_register_set);
2127
2128void nft_unregister_set(struct nft_set_ops *ops)
2129{
2130 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002131 list_del_rcu(&ops->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002132 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2133}
2134EXPORT_SYMBOL_GPL(nft_unregister_set);
2135
Patrick McHardyc50b9602014-03-28 10:19:47 +00002136/*
2137 * Select a set implementation based on the data characteristics and the
2138 * given policy. The total memory use might not be known if no size is
2139 * given, in that case the amount of memory per element is used.
2140 */
2141static const struct nft_set_ops *
2142nft_select_set_ops(const struct nlattr * const nla[],
2143 const struct nft_set_desc *desc,
2144 enum nft_set_policies policy)
Patrick McHardy20a69342013-10-11 12:06:22 +02002145{
Patrick McHardyc50b9602014-03-28 10:19:47 +00002146 const struct nft_set_ops *ops, *bops;
2147 struct nft_set_estimate est, best;
Patrick McHardy20a69342013-10-11 12:06:22 +02002148 u32 features;
2149
2150#ifdef CONFIG_MODULES
2151 if (list_empty(&nf_tables_set_ops)) {
2152 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2153 request_module("nft-set");
2154 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2155 if (!list_empty(&nf_tables_set_ops))
2156 return ERR_PTR(-EAGAIN);
2157 }
2158#endif
2159 features = 0;
2160 if (nla[NFTA_SET_FLAGS] != NULL) {
2161 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
Patrick McHardy4a8678e2015-04-05 14:41:05 +02002162 features &= NFT_SET_INTERVAL | NFT_SET_MAP | NFT_SET_TIMEOUT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002163 }
2164
Patrick McHardyc50b9602014-03-28 10:19:47 +00002165 bops = NULL;
2166 best.size = ~0;
2167 best.class = ~0;
2168
Patrick McHardy20a69342013-10-11 12:06:22 +02002169 list_for_each_entry(ops, &nf_tables_set_ops, list) {
2170 if ((ops->features & features) != features)
2171 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002172 if (!ops->estimate(desc, features, &est))
2173 continue;
2174
2175 switch (policy) {
2176 case NFT_SET_POL_PERFORMANCE:
2177 if (est.class < best.class)
2178 break;
2179 if (est.class == best.class && est.size < best.size)
2180 break;
2181 continue;
2182 case NFT_SET_POL_MEMORY:
2183 if (est.size < best.size)
2184 break;
2185 if (est.size == best.size && est.class < best.class)
2186 break;
2187 continue;
2188 default:
2189 break;
2190 }
2191
Patrick McHardy20a69342013-10-11 12:06:22 +02002192 if (!try_module_get(ops->owner))
2193 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002194 if (bops != NULL)
2195 module_put(bops->owner);
2196
2197 bops = ops;
2198 best = est;
Patrick McHardy20a69342013-10-11 12:06:22 +02002199 }
2200
Patrick McHardyc50b9602014-03-28 10:19:47 +00002201 if (bops != NULL)
2202 return bops;
2203
Patrick McHardy20a69342013-10-11 12:06:22 +02002204 return ERR_PTR(-EOPNOTSUPP);
2205}
2206
2207static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2208 [NFTA_SET_TABLE] = { .type = NLA_STRING },
Pablo Neira Ayusoa9bdd832014-03-24 15:10:37 +01002209 [NFTA_SET_NAME] = { .type = NLA_STRING,
2210 .len = IFNAMSIZ - 1 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002211 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2212 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2213 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2214 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2215 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002216 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2217 [NFTA_SET_DESC] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002218 [NFTA_SET_ID] = { .type = NLA_U32 },
Patrick McHardy761da292015-03-26 12:39:36 +00002219 [NFTA_SET_TIMEOUT] = { .type = NLA_U64 },
2220 [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002221};
2222
2223static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2224 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002225};
2226
2227static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2228 const struct sk_buff *skb,
2229 const struct nlmsghdr *nlh,
2230 const struct nlattr * const nla[])
2231{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002232 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002233 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002234 struct nft_af_info *afi = NULL;
2235 struct nft_table *table = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02002236
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002237 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2238 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2239 if (IS_ERR(afi))
2240 return PTR_ERR(afi);
2241 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002242
2243 if (nla[NFTA_SET_TABLE] != NULL) {
Patrick McHardyec2c9932014-02-05 15:03:35 +00002244 if (afi == NULL)
2245 return -EAFNOSUPPORT;
2246
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002247 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002248 if (IS_ERR(table))
2249 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002250 if (table->flags & NFT_TABLE_INACTIVE)
2251 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002252 }
2253
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002254 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002255 return 0;
2256}
2257
2258struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2259 const struct nlattr *nla)
2260{
2261 struct nft_set *set;
2262
2263 if (nla == NULL)
2264 return ERR_PTR(-EINVAL);
2265
2266 list_for_each_entry(set, &table->sets, list) {
2267 if (!nla_strcmp(nla, set->name))
2268 return set;
2269 }
2270 return ERR_PTR(-ENOENT);
2271}
2272
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002273struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2274 const struct nlattr *nla)
2275{
2276 struct nft_trans *trans;
2277 u32 id = ntohl(nla_get_be32(nla));
2278
2279 list_for_each_entry(trans, &net->nft.commit_list, list) {
2280 if (trans->msg_type == NFT_MSG_NEWSET &&
2281 id == nft_trans_set_id(trans))
2282 return nft_trans_set(trans);
2283 }
2284 return ERR_PTR(-ENOENT);
2285}
2286
Patrick McHardy20a69342013-10-11 12:06:22 +02002287static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2288 const char *name)
2289{
2290 const struct nft_set *i;
2291 const char *p;
2292 unsigned long *inuse;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002293 unsigned int n = 0, min = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002294
2295 p = strnchr(name, IFNAMSIZ, '%');
2296 if (p != NULL) {
2297 if (p[1] != 'd' || strchr(p + 2, '%'))
2298 return -EINVAL;
2299
2300 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2301 if (inuse == NULL)
2302 return -ENOMEM;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002303cont:
Patrick McHardy20a69342013-10-11 12:06:22 +02002304 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01002305 int tmp;
2306
2307 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02002308 continue;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002309 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02002310 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002311
Patrick McHardy60eb1892014-03-07 12:34:05 +01002312 set_bit(tmp - min, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02002313 }
2314
Patrick McHardy53b70282014-02-05 12:26:22 +01002315 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
Patrick McHardy60eb1892014-03-07 12:34:05 +01002316 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2317 min += BITS_PER_BYTE * PAGE_SIZE;
2318 memset(inuse, 0, PAGE_SIZE);
2319 goto cont;
2320 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002321 free_page((unsigned long)inuse);
2322 }
2323
Patrick McHardy60eb1892014-03-07 12:34:05 +01002324 snprintf(set->name, sizeof(set->name), name, min + n);
Patrick McHardy20a69342013-10-11 12:06:22 +02002325 list_for_each_entry(i, &ctx->table->sets, list) {
2326 if (!strcmp(set->name, i->name))
2327 return -ENFILE;
2328 }
2329 return 0;
2330}
2331
2332static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2333 const struct nft_set *set, u16 event, u16 flags)
2334{
2335 struct nfgenmsg *nfmsg;
2336 struct nlmsghdr *nlh;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002337 struct nlattr *desc;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002338 u32 portid = ctx->portid;
2339 u32 seq = ctx->seq;
Patrick McHardy20a69342013-10-11 12:06:22 +02002340
2341 event |= NFNL_SUBSYS_NFTABLES << 8;
2342 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2343 flags);
2344 if (nlh == NULL)
2345 goto nla_put_failure;
2346
2347 nfmsg = nlmsg_data(nlh);
2348 nfmsg->nfgen_family = ctx->afi->family;
2349 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02002350 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
Patrick McHardy20a69342013-10-11 12:06:22 +02002351
2352 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2353 goto nla_put_failure;
2354 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2355 goto nla_put_failure;
2356 if (set->flags != 0)
2357 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2358 goto nla_put_failure;
2359
2360 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2361 goto nla_put_failure;
2362 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2363 goto nla_put_failure;
2364 if (set->flags & NFT_SET_MAP) {
2365 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2366 goto nla_put_failure;
2367 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2368 goto nla_put_failure;
2369 }
2370
Patrick McHardy761da292015-03-26 12:39:36 +00002371 if (set->timeout &&
2372 nla_put_be64(skb, NFTA_SET_TIMEOUT, cpu_to_be64(set->timeout)))
2373 goto nla_put_failure;
2374 if (set->gc_int &&
2375 nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int)))
2376 goto nla_put_failure;
2377
Arturo Borrero9363dc42014-09-23 13:30:41 +02002378 if (set->policy != NFT_SET_POL_PERFORMANCE) {
2379 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
2380 goto nla_put_failure;
2381 }
2382
Patrick McHardyc50b9602014-03-28 10:19:47 +00002383 desc = nla_nest_start(skb, NFTA_SET_DESC);
2384 if (desc == NULL)
2385 goto nla_put_failure;
2386 if (set->size &&
2387 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2388 goto nla_put_failure;
2389 nla_nest_end(skb, desc);
2390
Johannes Berg053c0952015-01-16 22:09:00 +01002391 nlmsg_end(skb, nlh);
2392 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002393
2394nla_put_failure:
2395 nlmsg_trim(skb, nlh);
2396 return -1;
2397}
2398
2399static int nf_tables_set_notify(const struct nft_ctx *ctx,
2400 const struct nft_set *set,
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002401 int event, gfp_t gfp_flags)
Patrick McHardy20a69342013-10-11 12:06:22 +02002402{
2403 struct sk_buff *skb;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002404 u32 portid = ctx->portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002405 int err;
2406
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002407 if (!ctx->report &&
2408 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002409 return 0;
2410
2411 err = -ENOBUFS;
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002412 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
Patrick McHardy20a69342013-10-11 12:06:22 +02002413 if (skb == NULL)
2414 goto err;
2415
2416 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2417 if (err < 0) {
2418 kfree_skb(skb);
2419 goto err;
2420 }
2421
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002422 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002423 ctx->report, gfp_flags);
Patrick McHardy20a69342013-10-11 12:06:22 +02002424err:
2425 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002426 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002427 return err;
2428}
2429
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002430static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002431{
2432 const struct nft_set *set;
2433 unsigned int idx, s_idx = cb->args[0];
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002434 struct nft_af_info *afi;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002435 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2436 struct net *net = sock_net(skb->sk);
2437 int cur_family = cb->args[3];
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002438 struct nft_ctx *ctx = cb->data, ctx_set;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002439
2440 if (cb->args[1])
2441 return skb->len;
2442
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002443 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02002444 cb->seq = net->nft.base_seq;
2445
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002446 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002447 if (ctx->afi && ctx->afi != afi)
2448 continue;
2449
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002450 if (cur_family) {
2451 if (afi->family != cur_family)
2452 continue;
2453
2454 cur_family = 0;
2455 }
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002456 list_for_each_entry_rcu(table, &afi->tables, list) {
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002457 if (ctx->table && ctx->table != table)
2458 continue;
2459
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002460 if (cur_table) {
2461 if (cur_table != table)
2462 continue;
2463
2464 cur_table = NULL;
2465 }
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002466 idx = 0;
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002467 list_for_each_entry_rcu(set, &table->sets, list) {
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002468 if (idx < s_idx)
2469 goto cont;
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002470
2471 ctx_set = *ctx;
2472 ctx_set.table = table;
2473 ctx_set.afi = afi;
2474 if (nf_tables_fill_set(skb, &ctx_set, set,
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002475 NFT_MSG_NEWSET,
2476 NLM_F_MULTI) < 0) {
2477 cb->args[0] = idx;
2478 cb->args[2] = (unsigned long) table;
2479 cb->args[3] = afi->family;
2480 goto done;
2481 }
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02002482 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002483cont:
2484 idx++;
2485 }
2486 if (s_idx)
2487 s_idx = 0;
2488 }
2489 }
2490 cb->args[1] = 1;
2491done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002492 rcu_read_unlock();
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002493 return skb->len;
2494}
2495
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002496static int nf_tables_dump_sets_done(struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002497{
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002498 kfree(cb->data);
2499 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002500}
2501
2502static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2503 const struct nlmsghdr *nlh,
2504 const struct nlattr * const nla[])
2505{
2506 const struct nft_set *set;
2507 struct nft_ctx ctx;
2508 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002509 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002510 int err;
2511
stephen hemminger01cfa0a2014-10-29 22:57:19 -07002512 /* Verify existence before starting dump */
Patrick McHardy20a69342013-10-11 12:06:22 +02002513 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2514 if (err < 0)
2515 return err;
2516
2517 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2518 struct netlink_dump_control c = {
2519 .dump = nf_tables_dump_sets,
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002520 .done = nf_tables_dump_sets_done,
Patrick McHardy20a69342013-10-11 12:06:22 +02002521 };
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002522 struct nft_ctx *ctx_dump;
2523
2524 ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2525 if (ctx_dump == NULL)
2526 return -ENOMEM;
2527
2528 *ctx_dump = ctx;
2529 c.data = ctx_dump;
2530
Patrick McHardy20a69342013-10-11 12:06:22 +02002531 return netlink_dump_start(nlsk, skb, nlh, &c);
2532 }
2533
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002534 /* Only accept unspec with dump */
2535 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2536 return -EAFNOSUPPORT;
2537
Patrick McHardy20a69342013-10-11 12:06:22 +02002538 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2539 if (IS_ERR(set))
2540 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002541 if (set->flags & NFT_SET_INACTIVE)
2542 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002543
2544 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2545 if (skb2 == NULL)
2546 return -ENOMEM;
2547
2548 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2549 if (err < 0)
2550 goto err;
2551
2552 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2553
2554err:
2555 kfree_skb(skb2);
2556 return err;
2557}
2558
Patrick McHardyc50b9602014-03-28 10:19:47 +00002559static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2560 struct nft_set_desc *desc,
2561 const struct nlattr *nla)
2562{
2563 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2564 int err;
2565
2566 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2567 if (err < 0)
2568 return err;
2569
2570 if (da[NFTA_SET_DESC_SIZE] != NULL)
2571 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2572
2573 return 0;
2574}
2575
Patrick McHardy20a69342013-10-11 12:06:22 +02002576static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2577 const struct nlmsghdr *nlh,
2578 const struct nlattr * const nla[])
2579{
2580 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2581 const struct nft_set_ops *ops;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002582 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002583 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002584 struct nft_table *table;
2585 struct nft_set *set;
2586 struct nft_ctx ctx;
2587 char name[IFNAMSIZ];
2588 unsigned int size;
2589 bool create;
Patrick McHardy761da292015-03-26 12:39:36 +00002590 u64 timeout;
2591 u32 ktype, dtype, flags, policy, gc_int;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002592 struct nft_set_desc desc;
Patrick McHardy20a69342013-10-11 12:06:22 +02002593 int err;
2594
2595 if (nla[NFTA_SET_TABLE] == NULL ||
2596 nla[NFTA_SET_NAME] == NULL ||
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002597 nla[NFTA_SET_KEY_LEN] == NULL ||
2598 nla[NFTA_SET_ID] == NULL)
Patrick McHardy20a69342013-10-11 12:06:22 +02002599 return -EINVAL;
2600
Patrick McHardyc50b9602014-03-28 10:19:47 +00002601 memset(&desc, 0, sizeof(desc));
2602
Patrick McHardy20a69342013-10-11 12:06:22 +02002603 ktype = NFT_DATA_VALUE;
2604 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2605 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2606 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2607 return -EINVAL;
2608 }
2609
Patrick McHardyc50b9602014-03-28 10:19:47 +00002610 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2611 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002612 return -EINVAL;
2613
2614 flags = 0;
2615 if (nla[NFTA_SET_FLAGS] != NULL) {
2616 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2617 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
Patrick McHardy761da292015-03-26 12:39:36 +00002618 NFT_SET_INTERVAL | NFT_SET_MAP |
2619 NFT_SET_TIMEOUT))
Patrick McHardy20a69342013-10-11 12:06:22 +02002620 return -EINVAL;
2621 }
2622
2623 dtype = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002624 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2625 if (!(flags & NFT_SET_MAP))
2626 return -EINVAL;
2627
2628 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2629 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2630 dtype != NFT_DATA_VERDICT)
2631 return -EINVAL;
2632
2633 if (dtype != NFT_DATA_VERDICT) {
2634 if (nla[NFTA_SET_DATA_LEN] == NULL)
2635 return -EINVAL;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002636 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2637 if (desc.dlen == 0 ||
2638 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002639 return -EINVAL;
2640 } else
Patrick McHardyc50b9602014-03-28 10:19:47 +00002641 desc.dlen = sizeof(struct nft_data);
Patrick McHardy20a69342013-10-11 12:06:22 +02002642 } else if (flags & NFT_SET_MAP)
2643 return -EINVAL;
2644
Patrick McHardy761da292015-03-26 12:39:36 +00002645 timeout = 0;
2646 if (nla[NFTA_SET_TIMEOUT] != NULL) {
2647 if (!(flags & NFT_SET_TIMEOUT))
2648 return -EINVAL;
2649 timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_TIMEOUT]));
2650 }
2651 gc_int = 0;
2652 if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
2653 if (!(flags & NFT_SET_TIMEOUT))
2654 return -EINVAL;
2655 gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
2656 }
2657
Patrick McHardyc50b9602014-03-28 10:19:47 +00002658 policy = NFT_SET_POL_PERFORMANCE;
2659 if (nla[NFTA_SET_POLICY] != NULL)
2660 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2661
2662 if (nla[NFTA_SET_DESC] != NULL) {
2663 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2664 if (err < 0)
2665 return err;
2666 }
2667
Patrick McHardy20a69342013-10-11 12:06:22 +02002668 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2669
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002670 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002671 if (IS_ERR(afi))
2672 return PTR_ERR(afi);
2673
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002674 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002675 if (IS_ERR(table))
2676 return PTR_ERR(table);
2677
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002678 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002679
2680 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2681 if (IS_ERR(set)) {
2682 if (PTR_ERR(set) != -ENOENT)
2683 return PTR_ERR(set);
2684 set = NULL;
2685 }
2686
2687 if (set != NULL) {
2688 if (nlh->nlmsg_flags & NLM_F_EXCL)
2689 return -EEXIST;
2690 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2691 return -EOPNOTSUPP;
2692 return 0;
2693 }
2694
2695 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2696 return -ENOENT;
2697
Patrick McHardyc50b9602014-03-28 10:19:47 +00002698 ops = nft_select_set_ops(nla, &desc, policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002699 if (IS_ERR(ops))
2700 return PTR_ERR(ops);
2701
2702 size = 0;
2703 if (ops->privsize != NULL)
2704 size = ops->privsize(nla);
2705
2706 err = -ENOMEM;
2707 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2708 if (set == NULL)
2709 goto err1;
2710
2711 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2712 err = nf_tables_set_alloc_name(&ctx, set, name);
2713 if (err < 0)
2714 goto err2;
2715
2716 INIT_LIST_HEAD(&set->bindings);
Patrick McHardycc02e452015-03-25 14:08:50 +00002717 write_pnet(&set->pnet, net);
Patrick McHardy20a69342013-10-11 12:06:22 +02002718 set->ops = ops;
2719 set->ktype = ktype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002720 set->klen = desc.klen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002721 set->dtype = dtype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002722 set->dlen = desc.dlen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002723 set->flags = flags;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002724 set->size = desc.size;
Arturo Borrero9363dc42014-09-23 13:30:41 +02002725 set->policy = policy;
Patrick McHardy761da292015-03-26 12:39:36 +00002726 set->timeout = timeout;
2727 set->gc_int = gc_int;
Patrick McHardy20a69342013-10-11 12:06:22 +02002728
Patrick McHardyc50b9602014-03-28 10:19:47 +00002729 err = ops->init(set, &desc, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002730 if (err < 0)
2731 goto err2;
2732
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002733 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002734 if (err < 0)
2735 goto err2;
2736
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002737 list_add_tail_rcu(&set->list, &table->sets);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02002738 table->use++;
Patrick McHardy20a69342013-10-11 12:06:22 +02002739 return 0;
2740
2741err2:
2742 kfree(set);
2743err1:
2744 module_put(ops->owner);
2745 return err;
2746}
2747
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002748static void nft_set_destroy(struct nft_set *set)
2749{
2750 set->ops->destroy(set);
2751 module_put(set->ops->owner);
2752 kfree(set);
2753}
2754
Patrick McHardy20a69342013-10-11 12:06:22 +02002755static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2756{
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002757 list_del_rcu(&set->list);
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002758 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002759 nft_set_destroy(set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002760}
2761
2762static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2763 const struct nlmsghdr *nlh,
2764 const struct nlattr * const nla[])
2765{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002766 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002767 struct nft_set *set;
2768 struct nft_ctx ctx;
2769 int err;
2770
Patrick McHardyec2c9932014-02-05 15:03:35 +00002771 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2772 return -EAFNOSUPPORT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002773 if (nla[NFTA_SET_TABLE] == NULL)
2774 return -EINVAL;
2775
2776 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2777 if (err < 0)
2778 return err;
2779
2780 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2781 if (IS_ERR(set))
2782 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002783 if (set->flags & NFT_SET_INACTIVE)
2784 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002785 if (!list_empty(&set->bindings))
2786 return -EBUSY;
2787
Arturo Borreroee01d542014-09-02 16:42:25 +02002788 return nft_delset(&ctx, set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002789}
2790
2791static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2792 const struct nft_set *set,
2793 const struct nft_set_iter *iter,
2794 const struct nft_set_elem *elem)
2795{
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002796 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02002797 enum nft_registers dreg;
2798
2799 dreg = nft_type_to_reg(set->dtype);
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002800 return nft_validate_data_load(ctx, dreg, nft_set_ext_data(ext),
Pablo Neira Ayuso2ee0d3c2013-12-28 00:59:38 +01002801 set->dtype == NFT_DATA_VERDICT ?
2802 NFT_DATA_VERDICT : NFT_DATA_VALUE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002803}
2804
2805int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2806 struct nft_set_binding *binding)
2807{
2808 struct nft_set_binding *i;
2809 struct nft_set_iter iter;
2810
2811 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2812 return -EBUSY;
2813
Patrick McHardy11113e12015-04-05 14:41:07 +02002814 if (binding->flags & NFT_SET_MAP) {
Patrick McHardy20a69342013-10-11 12:06:22 +02002815 /* If the set is already bound to the same chain all
2816 * jumps are already validated for that chain.
2817 */
2818 list_for_each_entry(i, &set->bindings, list) {
Patrick McHardy11113e12015-04-05 14:41:07 +02002819 if (binding->flags & NFT_SET_MAP &&
2820 i->chain == binding->chain)
Patrick McHardy20a69342013-10-11 12:06:22 +02002821 goto bind;
2822 }
2823
2824 iter.skip = 0;
2825 iter.count = 0;
2826 iter.err = 0;
2827 iter.fn = nf_tables_bind_check_setelem;
2828
2829 set->ops->walk(ctx, set, &iter);
2830 if (iter.err < 0) {
2831 /* Destroy anonymous sets if binding fails */
2832 if (set->flags & NFT_SET_ANONYMOUS)
2833 nf_tables_set_destroy(ctx, set);
2834
2835 return iter.err;
2836 }
2837 }
2838bind:
2839 binding->chain = ctx->chain;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002840 list_add_tail_rcu(&binding->list, &set->bindings);
Patrick McHardy20a69342013-10-11 12:06:22 +02002841 return 0;
2842}
2843
2844void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2845 struct nft_set_binding *binding)
2846{
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002847 list_del_rcu(&binding->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002848
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002849 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2850 !(set->flags & NFT_SET_INACTIVE))
Patrick McHardy20a69342013-10-11 12:06:22 +02002851 nf_tables_set_destroy(ctx, set);
2852}
2853
Patrick McHardy3ac4c072015-03-25 13:07:49 +00002854const struct nft_set_ext_type nft_set_ext_types[] = {
2855 [NFT_SET_EXT_KEY] = {
2856 .len = sizeof(struct nft_data),
2857 .align = __alignof__(struct nft_data),
2858 },
2859 [NFT_SET_EXT_DATA] = {
2860 .len = sizeof(struct nft_data),
2861 .align = __alignof__(struct nft_data),
2862 },
2863 [NFT_SET_EXT_FLAGS] = {
2864 .len = sizeof(u8),
2865 .align = __alignof__(u8),
2866 },
Patrick McHardyc3e1b002015-03-26 12:39:37 +00002867 [NFT_SET_EXT_TIMEOUT] = {
2868 .len = sizeof(u64),
2869 .align = __alignof__(u64),
2870 },
2871 [NFT_SET_EXT_EXPIRATION] = {
2872 .len = sizeof(unsigned long),
2873 .align = __alignof__(unsigned long),
2874 },
Patrick McHardy68e942e2015-04-05 14:43:38 +02002875 [NFT_SET_EXT_USERDATA] = {
2876 .len = sizeof(struct nft_userdata),
2877 .align = __alignof__(struct nft_userdata),
2878 },
Patrick McHardy3ac4c072015-03-25 13:07:49 +00002879};
2880EXPORT_SYMBOL_GPL(nft_set_ext_types);
2881
Patrick McHardy20a69342013-10-11 12:06:22 +02002882/*
2883 * Set elements
2884 */
2885
2886static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2887 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2888 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2889 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
Patrick McHardyc3e1b002015-03-26 12:39:37 +00002890 [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
Patrick McHardy68e942e2015-04-05 14:43:38 +02002891 [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
2892 .len = NFT_USERDATA_MAXLEN },
Patrick McHardy20a69342013-10-11 12:06:22 +02002893};
2894
2895static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2896 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2897 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2898 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002899 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002900};
2901
2902static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2903 const struct sk_buff *skb,
2904 const struct nlmsghdr *nlh,
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002905 const struct nlattr * const nla[],
2906 bool trans)
Patrick McHardy20a69342013-10-11 12:06:22 +02002907{
2908 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002909 struct nft_af_info *afi;
2910 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002911 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002912
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002913 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002914 if (IS_ERR(afi))
2915 return PTR_ERR(afi);
2916
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002917 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002918 if (IS_ERR(table))
2919 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002920 if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2921 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002922
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002923 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002924 return 0;
2925}
2926
2927static int nf_tables_fill_setelem(struct sk_buff *skb,
2928 const struct nft_set *set,
2929 const struct nft_set_elem *elem)
2930{
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002931 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02002932 unsigned char *b = skb_tail_pointer(skb);
2933 struct nlattr *nest;
2934
2935 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2936 if (nest == NULL)
2937 goto nla_put_failure;
2938
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002939 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
2940 NFT_DATA_VALUE, set->klen) < 0)
Patrick McHardy20a69342013-10-11 12:06:22 +02002941 goto nla_put_failure;
2942
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002943 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
2944 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
Patrick McHardy20a69342013-10-11 12:06:22 +02002945 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2946 set->dlen) < 0)
2947 goto nla_put_failure;
2948
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002949 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
2950 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
2951 htonl(*nft_set_ext_flags(ext))))
2952 goto nla_put_failure;
Patrick McHardy20a69342013-10-11 12:06:22 +02002953
Patrick McHardyc3e1b002015-03-26 12:39:37 +00002954 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
2955 nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
2956 cpu_to_be64(*nft_set_ext_timeout(ext))))
2957 goto nla_put_failure;
2958
2959 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
2960 unsigned long expires, now = jiffies;
2961
2962 expires = *nft_set_ext_expiration(ext);
2963 if (time_before(now, expires))
2964 expires -= now;
2965 else
2966 expires = 0;
2967
2968 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
2969 cpu_to_be64(jiffies_to_msecs(expires))))
2970 goto nla_put_failure;
2971 }
2972
Patrick McHardy68e942e2015-04-05 14:43:38 +02002973 if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
2974 struct nft_userdata *udata;
2975
2976 udata = nft_set_ext_userdata(ext);
2977 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
2978 udata->len + 1, udata->data))
2979 goto nla_put_failure;
2980 }
2981
Patrick McHardy20a69342013-10-11 12:06:22 +02002982 nla_nest_end(skb, nest);
2983 return 0;
2984
2985nla_put_failure:
2986 nlmsg_trim(skb, b);
2987 return -EMSGSIZE;
2988}
2989
2990struct nft_set_dump_args {
2991 const struct netlink_callback *cb;
2992 struct nft_set_iter iter;
2993 struct sk_buff *skb;
2994};
2995
2996static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2997 const struct nft_set *set,
2998 const struct nft_set_iter *iter,
2999 const struct nft_set_elem *elem)
3000{
3001 struct nft_set_dump_args *args;
3002
3003 args = container_of(iter, struct nft_set_dump_args, iter);
3004 return nf_tables_fill_setelem(args->skb, set, elem);
3005}
3006
3007static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
3008{
3009 const struct nft_set *set;
3010 struct nft_set_dump_args args;
3011 struct nft_ctx ctx;
3012 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
3013 struct nfgenmsg *nfmsg;
3014 struct nlmsghdr *nlh;
3015 struct nlattr *nest;
3016 u32 portid, seq;
3017 int event, err;
3018
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01003019 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
3020 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02003021 if (err < 0)
3022 return err;
3023
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003024 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
3025 false);
Patrick McHardy20a69342013-10-11 12:06:22 +02003026 if (err < 0)
3027 return err;
3028
3029 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3030 if (IS_ERR(set))
3031 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003032 if (set->flags & NFT_SET_INACTIVE)
3033 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02003034
3035 event = NFT_MSG_NEWSETELEM;
3036 event |= NFNL_SUBSYS_NFTABLES << 8;
3037 portid = NETLINK_CB(cb->skb).portid;
3038 seq = cb->nlh->nlmsg_seq;
3039
3040 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3041 NLM_F_MULTI);
3042 if (nlh == NULL)
3043 goto nla_put_failure;
3044
3045 nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso6403d962014-06-11 19:05:28 +02003046 nfmsg->nfgen_family = ctx.afi->family;
Patrick McHardy20a69342013-10-11 12:06:22 +02003047 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003048 nfmsg->res_id = htons(ctx.net->nft.base_seq & 0xffff);
Patrick McHardy20a69342013-10-11 12:06:22 +02003049
3050 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
3051 goto nla_put_failure;
3052 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
3053 goto nla_put_failure;
3054
3055 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3056 if (nest == NULL)
3057 goto nla_put_failure;
3058
3059 args.cb = cb;
3060 args.skb = skb;
3061 args.iter.skip = cb->args[0];
3062 args.iter.count = 0;
3063 args.iter.err = 0;
3064 args.iter.fn = nf_tables_dump_setelem;
3065 set->ops->walk(&ctx, set, &args.iter);
3066
3067 nla_nest_end(skb, nest);
3068 nlmsg_end(skb, nlh);
3069
3070 if (args.iter.err && args.iter.err != -EMSGSIZE)
3071 return args.iter.err;
3072 if (args.iter.count == cb->args[0])
3073 return 0;
3074
3075 cb->args[0] = args.iter.count;
3076 return skb->len;
3077
3078nla_put_failure:
3079 return -ENOSPC;
3080}
3081
3082static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
3083 const struct nlmsghdr *nlh,
3084 const struct nlattr * const nla[])
3085{
3086 const struct nft_set *set;
3087 struct nft_ctx ctx;
3088 int err;
3089
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003090 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02003091 if (err < 0)
3092 return err;
3093
3094 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3095 if (IS_ERR(set))
3096 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003097 if (set->flags & NFT_SET_INACTIVE)
3098 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02003099
3100 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3101 struct netlink_dump_control c = {
3102 .dump = nf_tables_dump_set,
3103 };
3104 return netlink_dump_start(nlsk, skb, nlh, &c);
3105 }
3106 return -EOPNOTSUPP;
3107}
3108
Arturo Borrerod60ce622014-04-01 14:06:07 +02003109static int nf_tables_fill_setelem_info(struct sk_buff *skb,
3110 const struct nft_ctx *ctx, u32 seq,
3111 u32 portid, int event, u16 flags,
3112 const struct nft_set *set,
3113 const struct nft_set_elem *elem)
3114{
3115 struct nfgenmsg *nfmsg;
3116 struct nlmsghdr *nlh;
3117 struct nlattr *nest;
3118 int err;
3119
3120 event |= NFNL_SUBSYS_NFTABLES << 8;
3121 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3122 flags);
3123 if (nlh == NULL)
3124 goto nla_put_failure;
3125
3126 nfmsg = nlmsg_data(nlh);
3127 nfmsg->nfgen_family = ctx->afi->family;
3128 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003129 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
Arturo Borrerod60ce622014-04-01 14:06:07 +02003130
3131 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3132 goto nla_put_failure;
3133 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3134 goto nla_put_failure;
3135
3136 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3137 if (nest == NULL)
3138 goto nla_put_failure;
3139
3140 err = nf_tables_fill_setelem(skb, set, elem);
3141 if (err < 0)
3142 goto nla_put_failure;
3143
3144 nla_nest_end(skb, nest);
3145
Johannes Berg053c0952015-01-16 22:09:00 +01003146 nlmsg_end(skb, nlh);
3147 return 0;
Arturo Borrerod60ce622014-04-01 14:06:07 +02003148
3149nla_put_failure:
3150 nlmsg_trim(skb, nlh);
3151 return -1;
3152}
3153
3154static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
3155 const struct nft_set *set,
3156 const struct nft_set_elem *elem,
3157 int event, u16 flags)
3158{
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003159 struct net *net = ctx->net;
3160 u32 portid = ctx->portid;
Arturo Borrerod60ce622014-04-01 14:06:07 +02003161 struct sk_buff *skb;
3162 int err;
3163
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003164 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
Arturo Borrerod60ce622014-04-01 14:06:07 +02003165 return 0;
3166
3167 err = -ENOBUFS;
3168 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3169 if (skb == NULL)
3170 goto err;
3171
3172 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3173 set, elem);
3174 if (err < 0) {
3175 kfree_skb(skb);
3176 goto err;
3177 }
3178
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003179 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
Arturo Borrerod60ce622014-04-01 14:06:07 +02003180 GFP_KERNEL);
3181err:
3182 if (err < 0)
3183 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3184 return err;
3185}
3186
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003187static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3188 int msg_type,
3189 struct nft_set *set)
3190{
3191 struct nft_trans *trans;
3192
3193 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3194 if (trans == NULL)
3195 return NULL;
3196
3197 nft_trans_elem_set(trans) = set;
3198 return trans;
3199}
3200
Patrick McHardy22fe54d2015-04-05 14:41:08 +02003201void *nft_set_elem_init(const struct nft_set *set,
3202 const struct nft_set_ext_tmpl *tmpl,
3203 const struct nft_data *key,
3204 const struct nft_data *data,
3205 u64 timeout, gfp_t gfp)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003206{
3207 struct nft_set_ext *ext;
3208 void *elem;
3209
3210 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
3211 if (elem == NULL)
3212 return NULL;
3213
3214 ext = nft_set_elem_ext(set, elem);
3215 nft_set_ext_init(ext, tmpl);
3216
3217 memcpy(nft_set_ext_key(ext), key, set->klen);
3218 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3219 memcpy(nft_set_ext_data(ext), data, set->dlen);
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003220 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION))
3221 *nft_set_ext_expiration(ext) =
3222 jiffies + msecs_to_jiffies(timeout);
3223 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
3224 *nft_set_ext_timeout(ext) = timeout;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003225
3226 return elem;
3227}
3228
Patrick McHardy61edafb2015-03-25 14:08:47 +00003229void nft_set_elem_destroy(const struct nft_set *set, void *elem)
3230{
3231 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
3232
3233 nft_data_uninit(nft_set_ext_key(ext), NFT_DATA_VALUE);
3234 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3235 nft_data_uninit(nft_set_ext_data(ext), set->dtype);
3236
3237 kfree(elem);
3238}
3239EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
3240
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003241static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +02003242 const struct nlattr *attr)
3243{
3244 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3245 struct nft_data_desc d1, d2;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003246 struct nft_set_ext_tmpl tmpl;
3247 struct nft_set_ext *ext;
Patrick McHardy20a69342013-10-11 12:06:22 +02003248 struct nft_set_elem elem;
3249 struct nft_set_binding *binding;
Patrick McHardy68e942e2015-04-05 14:43:38 +02003250 struct nft_userdata *udata;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003251 struct nft_data data;
Patrick McHardy20a69342013-10-11 12:06:22 +02003252 enum nft_registers dreg;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003253 struct nft_trans *trans;
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003254 u64 timeout;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003255 u32 flags;
Patrick McHardy68e942e2015-04-05 14:43:38 +02003256 u8 ulen;
Patrick McHardy20a69342013-10-11 12:06:22 +02003257 int err;
3258
3259 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3260 nft_set_elem_policy);
3261 if (err < 0)
3262 return err;
3263
3264 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3265 return -EINVAL;
3266
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003267 nft_set_ext_prepare(&tmpl);
3268
3269 flags = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003270 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003271 flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3272 if (flags & ~NFT_SET_ELEM_INTERVAL_END)
Patrick McHardy20a69342013-10-11 12:06:22 +02003273 return -EINVAL;
Patrick McHardy55df35d2015-03-21 15:19:16 +00003274 if (!(set->flags & NFT_SET_INTERVAL) &&
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003275 flags & NFT_SET_ELEM_INTERVAL_END)
Patrick McHardy55df35d2015-03-21 15:19:16 +00003276 return -EINVAL;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003277 if (flags != 0)
3278 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
Patrick McHardy20a69342013-10-11 12:06:22 +02003279 }
3280
3281 if (set->flags & NFT_SET_MAP) {
3282 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003283 !(flags & NFT_SET_ELEM_INTERVAL_END))
Patrick McHardy20a69342013-10-11 12:06:22 +02003284 return -EINVAL;
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01003285 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003286 flags & NFT_SET_ELEM_INTERVAL_END)
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01003287 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003288 } else {
3289 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3290 return -EINVAL;
3291 }
3292
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003293 timeout = 0;
3294 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
3295 if (!(set->flags & NFT_SET_TIMEOUT))
3296 return -EINVAL;
3297 timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_ELEM_TIMEOUT]));
3298 } else if (set->flags & NFT_SET_TIMEOUT) {
3299 timeout = set->timeout;
3300 }
3301
Patrick McHardy20a69342013-10-11 12:06:22 +02003302 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3303 if (err < 0)
3304 goto err1;
3305 err = -EINVAL;
3306 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3307 goto err2;
3308
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003309 nft_set_ext_add(&tmpl, NFT_SET_EXT_KEY);
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003310 if (timeout > 0) {
3311 nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
3312 if (timeout != set->timeout)
3313 nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
3314 }
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003315
Patrick McHardy20a69342013-10-11 12:06:22 +02003316 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003317 err = nft_data_init(ctx, &data, &d2, nla[NFTA_SET_ELEM_DATA]);
Patrick McHardy20a69342013-10-11 12:06:22 +02003318 if (err < 0)
3319 goto err2;
3320
3321 err = -EINVAL;
3322 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3323 goto err3;
3324
3325 dreg = nft_type_to_reg(set->dtype);
3326 list_for_each_entry(binding, &set->bindings, list) {
3327 struct nft_ctx bind_ctx = {
3328 .afi = ctx->afi,
3329 .table = ctx->table,
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02003330 .chain = (struct nft_chain *)binding->chain,
Patrick McHardy20a69342013-10-11 12:06:22 +02003331 };
3332
Patrick McHardy11113e12015-04-05 14:41:07 +02003333 if (!(binding->flags & NFT_SET_MAP))
3334 continue;
3335
Patrick McHardy20a69342013-10-11 12:06:22 +02003336 err = nft_validate_data_load(&bind_ctx, dreg,
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003337 &data, d2.type);
Patrick McHardy20a69342013-10-11 12:06:22 +02003338 if (err < 0)
3339 goto err3;
3340 }
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003341
3342 nft_set_ext_add(&tmpl, NFT_SET_EXT_DATA);
Patrick McHardy20a69342013-10-11 12:06:22 +02003343 }
3344
Patrick McHardy68e942e2015-04-05 14:43:38 +02003345 /* The full maximum length of userdata can exceed the maximum
3346 * offset value (U8_MAX) for following extensions, therefor it
3347 * must be the last extension added.
3348 */
3349 ulen = 0;
3350 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
3351 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
3352 if (ulen > 0)
3353 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
3354 ulen);
3355 }
3356
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003357 err = -ENOMEM;
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003358 elem.priv = nft_set_elem_init(set, &tmpl, &elem.key, &data,
3359 timeout, GFP_KERNEL);
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003360 if (elem.priv == NULL)
3361 goto err3;
3362
3363 ext = nft_set_elem_ext(set, elem.priv);
3364 if (flags)
3365 *nft_set_ext_flags(ext) = flags;
Patrick McHardy68e942e2015-04-05 14:43:38 +02003366 if (ulen > 0) {
3367 udata = nft_set_ext_userdata(ext);
3368 udata->len = ulen - 1;
3369 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
3370 }
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003371
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003372 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3373 if (trans == NULL)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003374 goto err4;
Patrick McHardy20a69342013-10-11 12:06:22 +02003375
Patrick McHardy69086652015-03-26 12:39:39 +00003376 ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
Patrick McHardy20a69342013-10-11 12:06:22 +02003377 err = set->ops->insert(set, &elem);
3378 if (err < 0)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003379 goto err5;
Patrick McHardy20a69342013-10-11 12:06:22 +02003380
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003381 nft_trans_elem(trans) = elem;
Pablo Neira Ayuso46bbafc2014-05-22 12:36:03 +02003382 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Patrick McHardy20a69342013-10-11 12:06:22 +02003383 return 0;
3384
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003385err5:
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003386 kfree(trans);
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003387err4:
3388 kfree(elem.priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02003389err3:
3390 if (nla[NFTA_SET_ELEM_DATA] != NULL)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003391 nft_data_uninit(&data, d2.type);
Patrick McHardy20a69342013-10-11 12:06:22 +02003392err2:
3393 nft_data_uninit(&elem.key, d1.type);
3394err1:
3395 return err;
3396}
3397
3398static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3399 const struct nlmsghdr *nlh,
3400 const struct nlattr * const nla[])
3401{
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003402 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02003403 const struct nlattr *attr;
3404 struct nft_set *set;
3405 struct nft_ctx ctx;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003406 int rem, err = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003407
Pablo Neira Ayuso7d5570c2014-07-25 13:15:36 +02003408 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3409 return -EINVAL;
3410
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003411 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
Patrick McHardy20a69342013-10-11 12:06:22 +02003412 if (err < 0)
3413 return err;
3414
3415 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003416 if (IS_ERR(set)) {
3417 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3418 set = nf_tables_set_lookup_byid(net,
3419 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3420 }
3421 if (IS_ERR(set))
3422 return PTR_ERR(set);
3423 }
3424
Patrick McHardy20a69342013-10-11 12:06:22 +02003425 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3426 return -EBUSY;
3427
3428 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
Patrick McHardy3dd06732015-04-05 14:41:06 +02003429 if (set->size &&
3430 !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact))
3431 return -ENFILE;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003432
Patrick McHardy3dd06732015-04-05 14:41:06 +02003433 err = nft_add_set_elem(&ctx, set, attr);
3434 if (err < 0) {
3435 atomic_dec(&set->nelems);
3436 break;
3437 }
Patrick McHardy20a69342013-10-11 12:06:22 +02003438 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003439 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +02003440}
3441
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003442static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +02003443 const struct nlattr *attr)
3444{
3445 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3446 struct nft_data_desc desc;
3447 struct nft_set_elem elem;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003448 struct nft_trans *trans;
Patrick McHardy20a69342013-10-11 12:06:22 +02003449 int err;
3450
3451 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3452 nft_set_elem_policy);
3453 if (err < 0)
3454 goto err1;
3455
3456 err = -EINVAL;
3457 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3458 goto err1;
3459
3460 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3461 if (err < 0)
3462 goto err1;
3463
3464 err = -EINVAL;
3465 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3466 goto err2;
3467
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003468 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
Julia Lawall609ccf02014-08-07 14:49:08 +02003469 if (trans == NULL) {
3470 err = -ENOMEM;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003471 goto err2;
Julia Lawall609ccf02014-08-07 14:49:08 +02003472 }
Patrick McHardy20a69342013-10-11 12:06:22 +02003473
Patrick McHardycc02e452015-03-25 14:08:50 +00003474 elem.priv = set->ops->deactivate(set, &elem);
3475 if (elem.priv == NULL) {
3476 err = -ENOENT;
3477 goto err3;
3478 }
3479
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003480 nft_trans_elem(trans) = elem;
Pablo Neira Ayuso46bbafc2014-05-22 12:36:03 +02003481 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Thomas Graf0dc13622014-08-01 17:25:38 +02003482 return 0;
Patrick McHardycc02e452015-03-25 14:08:50 +00003483
3484err3:
3485 kfree(trans);
Patrick McHardy20a69342013-10-11 12:06:22 +02003486err2:
3487 nft_data_uninit(&elem.key, desc.type);
3488err1:
3489 return err;
3490}
3491
3492static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3493 const struct nlmsghdr *nlh,
3494 const struct nlattr * const nla[])
3495{
3496 const struct nlattr *attr;
3497 struct nft_set *set;
3498 struct nft_ctx ctx;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003499 int rem, err = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003500
Pablo Neira Ayuso7d5570c2014-07-25 13:15:36 +02003501 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3502 return -EINVAL;
3503
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003504 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02003505 if (err < 0)
3506 return err;
3507
3508 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3509 if (IS_ERR(set))
3510 return PTR_ERR(set);
3511 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3512 return -EBUSY;
3513
3514 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3515 err = nft_del_setelem(&ctx, set, attr);
3516 if (err < 0)
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003517 break;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003518
Patrick McHardy3dd06732015-04-05 14:41:06 +02003519 set->ndeact++;
Patrick McHardy20a69342013-10-11 12:06:22 +02003520 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003521 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +02003522}
3523
Patrick McHardycfed7e12015-03-26 12:39:38 +00003524void nft_set_gc_batch_release(struct rcu_head *rcu)
3525{
3526 struct nft_set_gc_batch *gcb;
3527 unsigned int i;
3528
3529 gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
3530 for (i = 0; i < gcb->head.cnt; i++)
3531 nft_set_elem_destroy(gcb->head.set, gcb->elems[i]);
3532 kfree(gcb);
3533}
3534EXPORT_SYMBOL_GPL(nft_set_gc_batch_release);
3535
3536struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
3537 gfp_t gfp)
3538{
3539 struct nft_set_gc_batch *gcb;
3540
3541 gcb = kzalloc(sizeof(*gcb), gfp);
3542 if (gcb == NULL)
3543 return gcb;
3544 gcb->head.set = set;
3545 return gcb;
3546}
3547EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc);
3548
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003549static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
3550 u32 portid, u32 seq)
3551{
3552 struct nlmsghdr *nlh;
3553 struct nfgenmsg *nfmsg;
3554 int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN;
3555
3556 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
3557 if (nlh == NULL)
3558 goto nla_put_failure;
3559
3560 nfmsg = nlmsg_data(nlh);
3561 nfmsg->nfgen_family = AF_UNSPEC;
3562 nfmsg->version = NFNETLINK_V0;
3563 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
3564
3565 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)))
3566 goto nla_put_failure;
3567
Johannes Berg053c0952015-01-16 22:09:00 +01003568 nlmsg_end(skb, nlh);
3569 return 0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003570
3571nla_put_failure:
3572 nlmsg_trim(skb, nlh);
3573 return -EMSGSIZE;
3574}
3575
3576static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
3577{
3578 struct nlmsghdr *nlh = nlmsg_hdr(skb);
3579 struct sk_buff *skb2;
3580 int err;
3581
3582 if (nlmsg_report(nlh) &&
3583 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3584 return 0;
3585
3586 err = -ENOBUFS;
3587 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3588 if (skb2 == NULL)
3589 goto err;
3590
3591 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3592 nlh->nlmsg_seq);
3593 if (err < 0) {
3594 kfree_skb(skb2);
3595 goto err;
3596 }
3597
3598 err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
3599 NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
3600err:
3601 if (err < 0) {
3602 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
3603 err);
3604 }
3605 return err;
3606}
3607
3608static int nf_tables_getgen(struct sock *nlsk, struct sk_buff *skb,
3609 const struct nlmsghdr *nlh,
3610 const struct nlattr * const nla[])
3611{
3612 struct net *net = sock_net(skb->sk);
3613 struct sk_buff *skb2;
3614 int err;
3615
3616 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3617 if (skb2 == NULL)
3618 return -ENOMEM;
3619
3620 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3621 nlh->nlmsg_seq);
3622 if (err < 0)
3623 goto err;
3624
3625 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3626err:
3627 kfree_skb(skb2);
3628 return err;
3629}
3630
Patrick McHardy96518512013-10-14 11:00:02 +02003631static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3632 [NFT_MSG_NEWTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003633 .call_batch = nf_tables_newtable,
Patrick McHardy96518512013-10-14 11:00:02 +02003634 .attr_count = NFTA_TABLE_MAX,
3635 .policy = nft_table_policy,
3636 },
3637 [NFT_MSG_GETTABLE] = {
3638 .call = nf_tables_gettable,
3639 .attr_count = NFTA_TABLE_MAX,
3640 .policy = nft_table_policy,
3641 },
3642 [NFT_MSG_DELTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003643 .call_batch = nf_tables_deltable,
Patrick McHardy96518512013-10-14 11:00:02 +02003644 .attr_count = NFTA_TABLE_MAX,
3645 .policy = nft_table_policy,
3646 },
3647 [NFT_MSG_NEWCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003648 .call_batch = nf_tables_newchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003649 .attr_count = NFTA_CHAIN_MAX,
3650 .policy = nft_chain_policy,
3651 },
3652 [NFT_MSG_GETCHAIN] = {
3653 .call = nf_tables_getchain,
3654 .attr_count = NFTA_CHAIN_MAX,
3655 .policy = nft_chain_policy,
3656 },
3657 [NFT_MSG_DELCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003658 .call_batch = nf_tables_delchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003659 .attr_count = NFTA_CHAIN_MAX,
3660 .policy = nft_chain_policy,
3661 },
3662 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003663 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003664 .attr_count = NFTA_RULE_MAX,
3665 .policy = nft_rule_policy,
3666 },
3667 [NFT_MSG_GETRULE] = {
3668 .call = nf_tables_getrule,
3669 .attr_count = NFTA_RULE_MAX,
3670 .policy = nft_rule_policy,
3671 },
3672 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003673 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003674 .attr_count = NFTA_RULE_MAX,
3675 .policy = nft_rule_policy,
3676 },
Patrick McHardy20a69342013-10-11 12:06:22 +02003677 [NFT_MSG_NEWSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003678 .call_batch = nf_tables_newset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003679 .attr_count = NFTA_SET_MAX,
3680 .policy = nft_set_policy,
3681 },
3682 [NFT_MSG_GETSET] = {
3683 .call = nf_tables_getset,
3684 .attr_count = NFTA_SET_MAX,
3685 .policy = nft_set_policy,
3686 },
3687 [NFT_MSG_DELSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003688 .call_batch = nf_tables_delset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003689 .attr_count = NFTA_SET_MAX,
3690 .policy = nft_set_policy,
3691 },
3692 [NFT_MSG_NEWSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003693 .call_batch = nf_tables_newsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003694 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3695 .policy = nft_set_elem_list_policy,
3696 },
3697 [NFT_MSG_GETSETELEM] = {
3698 .call = nf_tables_getsetelem,
3699 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3700 .policy = nft_set_elem_list_policy,
3701 },
3702 [NFT_MSG_DELSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003703 .call_batch = nf_tables_delsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003704 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3705 .policy = nft_set_elem_list_policy,
3706 },
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003707 [NFT_MSG_GETGEN] = {
3708 .call = nf_tables_getgen,
3709 },
Patrick McHardy96518512013-10-14 11:00:02 +02003710};
3711
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003712static void nft_chain_commit_update(struct nft_trans *trans)
3713{
3714 struct nft_base_chain *basechain;
3715
3716 if (nft_trans_chain_name(trans)[0])
3717 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3718
3719 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3720 return;
3721
3722 basechain = nft_base_chain(trans->ctx.chain);
3723 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3724
3725 switch (nft_trans_chain_policy(trans)) {
3726 case NF_DROP:
3727 case NF_ACCEPT:
3728 basechain->policy = nft_trans_chain_policy(trans);
3729 break;
3730 }
3731}
3732
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003733static void nf_tables_commit_release(struct nft_trans *trans)
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003734{
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003735 switch (trans->msg_type) {
3736 case NFT_MSG_DELTABLE:
3737 nf_tables_table_destroy(&trans->ctx);
3738 break;
3739 case NFT_MSG_DELCHAIN:
3740 nf_tables_chain_destroy(trans->ctx.chain);
3741 break;
3742 case NFT_MSG_DELRULE:
3743 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3744 break;
3745 case NFT_MSG_DELSET:
3746 nft_set_destroy(nft_trans_set(trans));
3747 break;
Patrick McHardy61edafb2015-03-25 14:08:47 +00003748 case NFT_MSG_DELSETELEM:
3749 nft_set_elem_destroy(nft_trans_elem_set(trans),
3750 nft_trans_elem(trans).priv);
3751 break;
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003752 }
3753 kfree(trans);
3754}
3755
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003756static int nf_tables_commit(struct sk_buff *skb)
3757{
3758 struct net *net = sock_net(skb->sk);
3759 struct nft_trans *trans, *next;
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003760 struct nft_trans_elem *te;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003761
3762 /* Bump generation counter, invalidate any dump in progress */
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02003763 while (++net->nft.base_seq == 0);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003764
3765 /* A new generation has just started */
Patrick McHardyea4bd992015-03-25 14:08:49 +00003766 net->nft.gencursor = nft_gencursor_next(net);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003767
3768 /* Make sure all packets have left the previous generation before
3769 * purging old rules.
3770 */
3771 synchronize_rcu();
3772
3773 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003774 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003775 case NFT_MSG_NEWTABLE:
3776 if (nft_trans_table_update(trans)) {
3777 if (!nft_trans_table_enable(trans)) {
3778 nf_tables_table_disable(trans->ctx.afi,
3779 trans->ctx.table);
3780 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3781 }
3782 } else {
3783 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3784 }
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003785 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003786 nft_trans_destroy(trans);
3787 break;
3788 case NFT_MSG_DELTABLE:
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003789 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003790 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003791 case NFT_MSG_NEWCHAIN:
3792 if (nft_trans_chain_update(trans))
3793 nft_chain_commit_update(trans);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003794 else
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003795 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003796
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003797 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003798 nft_trans_destroy(trans);
3799 break;
3800 case NFT_MSG_DELCHAIN:
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003801 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
Arturo Borreroc5598792014-09-02 16:42:23 +02003802 nf_tables_unregister_hooks(trans->ctx.table,
3803 trans->ctx.chain,
3804 trans->ctx.afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003805 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003806 case NFT_MSG_NEWRULE:
3807 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003808 nf_tables_rule_notify(&trans->ctx,
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003809 nft_trans_rule(trans),
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003810 NFT_MSG_NEWRULE);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003811 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003812 break;
3813 case NFT_MSG_DELRULE:
3814 list_del_rcu(&nft_trans_rule(trans)->list);
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003815 nf_tables_rule_notify(&trans->ctx,
3816 nft_trans_rule(trans),
3817 NFT_MSG_DELRULE);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003818 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003819 case NFT_MSG_NEWSET:
3820 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003821 /* This avoids hitting -EBUSY when deleting the table
3822 * from the transaction.
3823 */
3824 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3825 !list_empty(&nft_trans_set(trans)->bindings))
3826 trans->ctx.table->use--;
3827
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003828 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02003829 NFT_MSG_NEWSET, GFP_KERNEL);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003830 nft_trans_destroy(trans);
3831 break;
3832 case NFT_MSG_DELSET:
3833 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02003834 NFT_MSG_DELSET, GFP_KERNEL);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003835 break;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003836 case NFT_MSG_NEWSETELEM:
Patrick McHardycc02e452015-03-25 14:08:50 +00003837 te = (struct nft_trans_elem *)trans->data;
3838
3839 te->set->ops->activate(te->set, &te->elem);
3840 nf_tables_setelem_notify(&trans->ctx, te->set,
3841 &te->elem,
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003842 NFT_MSG_NEWSETELEM, 0);
3843 nft_trans_destroy(trans);
3844 break;
3845 case NFT_MSG_DELSETELEM:
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003846 te = (struct nft_trans_elem *)trans->data;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003847
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003848 nf_tables_setelem_notify(&trans->ctx, te->set,
3849 &te->elem,
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003850 NFT_MSG_DELSETELEM, 0);
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01003851 te->set->ops->remove(te->set, &te->elem);
Patrick McHardy3dd06732015-04-05 14:41:06 +02003852 atomic_dec(&te->set->nelems);
3853 te->set->ndeact--;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003854 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003855 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003856 }
3857
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003858 synchronize_rcu();
3859
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003860 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003861 list_del(&trans->list);
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003862 nf_tables_commit_release(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003863 }
3864
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003865 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
3866
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003867 return 0;
3868}
3869
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003870static void nf_tables_abort_release(struct nft_trans *trans)
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003871{
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003872 switch (trans->msg_type) {
3873 case NFT_MSG_NEWTABLE:
3874 nf_tables_table_destroy(&trans->ctx);
3875 break;
3876 case NFT_MSG_NEWCHAIN:
3877 nf_tables_chain_destroy(trans->ctx.chain);
3878 break;
3879 case NFT_MSG_NEWRULE:
3880 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3881 break;
3882 case NFT_MSG_NEWSET:
3883 nft_set_destroy(nft_trans_set(trans));
3884 break;
Patrick McHardy61edafb2015-03-25 14:08:47 +00003885 case NFT_MSG_NEWSETELEM:
3886 nft_set_elem_destroy(nft_trans_elem_set(trans),
3887 nft_trans_elem(trans).priv);
3888 break;
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003889 }
3890 kfree(trans);
3891}
3892
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003893static int nf_tables_abort(struct sk_buff *skb)
3894{
3895 struct net *net = sock_net(skb->sk);
3896 struct nft_trans *trans, *next;
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01003897 struct nft_trans_elem *te;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003898
3899 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003900 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003901 case NFT_MSG_NEWTABLE:
3902 if (nft_trans_table_update(trans)) {
3903 if (nft_trans_table_enable(trans)) {
3904 nf_tables_table_disable(trans->ctx.afi,
3905 trans->ctx.table);
3906 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3907 }
3908 nft_trans_destroy(trans);
3909 } else {
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003910 list_del_rcu(&trans->ctx.table->list);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003911 }
3912 break;
3913 case NFT_MSG_DELTABLE:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003914 list_add_tail_rcu(&trans->ctx.table->list,
3915 &trans->ctx.afi->tables);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003916 nft_trans_destroy(trans);
3917 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003918 case NFT_MSG_NEWCHAIN:
3919 if (nft_trans_chain_update(trans)) {
Markus Elfring982f4052014-11-18 20:37:05 +01003920 free_percpu(nft_trans_chain_stats(trans));
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003921
3922 nft_trans_destroy(trans);
3923 } else {
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003924 trans->ctx.table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003925 list_del_rcu(&trans->ctx.chain->list);
Arturo Borreroc5598792014-09-02 16:42:23 +02003926 nf_tables_unregister_hooks(trans->ctx.table,
3927 trans->ctx.chain,
3928 trans->ctx.afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003929 }
3930 break;
3931 case NFT_MSG_DELCHAIN:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003932 trans->ctx.table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003933 list_add_tail_rcu(&trans->ctx.chain->list,
3934 &trans->ctx.table->chains);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003935 nft_trans_destroy(trans);
3936 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003937 case NFT_MSG_NEWRULE:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003938 trans->ctx.chain->use--;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003939 list_del_rcu(&nft_trans_rule(trans)->list);
3940 break;
3941 case NFT_MSG_DELRULE:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003942 trans->ctx.chain->use++;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003943 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003944 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003945 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003946 case NFT_MSG_NEWSET:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003947 trans->ctx.table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003948 list_del_rcu(&nft_trans_set(trans)->list);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003949 break;
3950 case NFT_MSG_DELSET:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003951 trans->ctx.table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003952 list_add_tail_rcu(&nft_trans_set(trans)->list,
3953 &trans->ctx.table->sets);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003954 nft_trans_destroy(trans);
3955 break;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003956 case NFT_MSG_NEWSETELEM:
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01003957 te = (struct nft_trans_elem *)trans->data;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003958
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01003959 te->set->ops->remove(te->set, &te->elem);
Patrick McHardy3dd06732015-04-05 14:41:06 +02003960 atomic_dec(&te->set->nelems);
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003961 break;
3962 case NFT_MSG_DELSETELEM:
Patrick McHardycc02e452015-03-25 14:08:50 +00003963 te = (struct nft_trans_elem *)trans->data;
3964
Patrick McHardycc02e452015-03-25 14:08:50 +00003965 te->set->ops->activate(te->set, &te->elem);
Patrick McHardy3dd06732015-04-05 14:41:06 +02003966 te->set->ndeact--;
Patrick McHardycc02e452015-03-25 14:08:50 +00003967
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003968 nft_trans_destroy(trans);
3969 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003970 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003971 }
3972
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003973 synchronize_rcu();
3974
Pablo Neira Ayusoa1cee072014-05-23 11:09:42 +02003975 list_for_each_entry_safe_reverse(trans, next,
3976 &net->nft.commit_list, list) {
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003977 list_del(&trans->list);
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003978 nf_tables_abort_release(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003979 }
3980
3981 return 0;
3982}
3983
Patrick McHardy96518512013-10-14 11:00:02 +02003984static const struct nfnetlink_subsystem nf_tables_subsys = {
3985 .name = "nf_tables",
3986 .subsys_id = NFNL_SUBSYS_NFTABLES,
3987 .cb_count = NFT_MSG_MAX,
3988 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003989 .commit = nf_tables_commit,
3990 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02003991};
3992
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +02003993int nft_chain_validate_dependency(const struct nft_chain *chain,
3994 enum nft_chain_type type)
3995{
3996 const struct nft_base_chain *basechain;
3997
3998 if (chain->flags & NFT_BASE_CHAIN) {
3999 basechain = nft_base_chain(chain);
4000 if (basechain->type->type != type)
4001 return -EOPNOTSUPP;
4002 }
4003 return 0;
4004}
4005EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
4006
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +01004007int nft_chain_validate_hooks(const struct nft_chain *chain,
4008 unsigned int hook_flags)
4009{
4010 struct nft_base_chain *basechain;
4011
4012 if (chain->flags & NFT_BASE_CHAIN) {
4013 basechain = nft_base_chain(chain);
4014
4015 if ((1 << basechain->ops[0].hooknum) & hook_flags)
4016 return 0;
4017
4018 return -EOPNOTSUPP;
4019 }
4020
4021 return 0;
4022}
4023EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
4024
Patrick McHardy20a69342013-10-11 12:06:22 +02004025/*
4026 * Loop detection - walk through the ruleset beginning at the destination chain
4027 * of a new jump until either the source chain is reached (loop) or all
4028 * reachable chains have been traversed.
4029 *
4030 * The loop check is performed whenever a new jump verdict is added to an
4031 * expression or verdict map or a verdict map is bound to a new chain.
4032 */
4033
4034static int nf_tables_check_loops(const struct nft_ctx *ctx,
4035 const struct nft_chain *chain);
4036
4037static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
4038 const struct nft_set *set,
4039 const struct nft_set_iter *iter,
4040 const struct nft_set_elem *elem)
4041{
Patrick McHardyfe2811e2015-03-25 13:07:50 +00004042 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
4043 const struct nft_data *data;
4044
4045 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
4046 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
Pablo Neira Ayuso62f9c8b2014-02-07 14:45:01 +01004047 return 0;
4048
Patrick McHardyfe2811e2015-03-25 13:07:50 +00004049 data = nft_set_ext_data(ext);
4050 switch (data->verdict) {
Patrick McHardy20a69342013-10-11 12:06:22 +02004051 case NFT_JUMP:
4052 case NFT_GOTO:
Patrick McHardyfe2811e2015-03-25 13:07:50 +00004053 return nf_tables_check_loops(ctx, data->chain);
Patrick McHardy20a69342013-10-11 12:06:22 +02004054 default:
4055 return 0;
4056 }
4057}
4058
4059static int nf_tables_check_loops(const struct nft_ctx *ctx,
4060 const struct nft_chain *chain)
4061{
4062 const struct nft_rule *rule;
4063 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02004064 const struct nft_set *set;
4065 struct nft_set_binding *binding;
4066 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02004067
4068 if (ctx->chain == chain)
4069 return -ELOOP;
4070
4071 list_for_each_entry(rule, &chain->rules, list) {
4072 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02004073 const struct nft_data *data = NULL;
4074 int err;
4075
4076 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02004077 continue;
4078
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02004079 err = expr->ops->validate(ctx, expr, &data);
4080 if (err < 0)
4081 return err;
4082
Patrick McHardy20a69342013-10-11 12:06:22 +02004083 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02004084 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02004085
4086 switch (data->verdict) {
4087 case NFT_JUMP:
4088 case NFT_GOTO:
4089 err = nf_tables_check_loops(ctx, data->chain);
4090 if (err < 0)
4091 return err;
4092 default:
4093 break;
4094 }
4095 }
4096 }
4097
4098 list_for_each_entry(set, &ctx->table->sets, list) {
4099 if (!(set->flags & NFT_SET_MAP) ||
4100 set->dtype != NFT_DATA_VERDICT)
4101 continue;
4102
4103 list_for_each_entry(binding, &set->bindings, list) {
Patrick McHardy11113e12015-04-05 14:41:07 +02004104 if (!(binding->flags & NFT_SET_MAP) ||
4105 binding->chain != chain)
Patrick McHardy20a69342013-10-11 12:06:22 +02004106 continue;
4107
4108 iter.skip = 0;
4109 iter.count = 0;
4110 iter.err = 0;
4111 iter.fn = nf_tables_loop_check_setelem;
4112
4113 set->ops->walk(ctx, set, &iter);
4114 if (iter.err < 0)
4115 return iter.err;
4116 }
4117 }
4118
4119 return 0;
4120}
4121
Patrick McHardy96518512013-10-14 11:00:02 +02004122/**
4123 * nft_validate_input_register - validate an expressions' input register
4124 *
4125 * @reg: the register number
4126 *
4127 * Validate that the input register is one of the general purpose
4128 * registers.
4129 */
4130int nft_validate_input_register(enum nft_registers reg)
4131{
4132 if (reg <= NFT_REG_VERDICT)
4133 return -EINVAL;
4134 if (reg > NFT_REG_MAX)
4135 return -ERANGE;
4136 return 0;
4137}
4138EXPORT_SYMBOL_GPL(nft_validate_input_register);
4139
4140/**
4141 * nft_validate_output_register - validate an expressions' output register
4142 *
4143 * @reg: the register number
4144 *
4145 * Validate that the output register is one of the general purpose
4146 * registers or the verdict register.
4147 */
4148int nft_validate_output_register(enum nft_registers reg)
4149{
4150 if (reg < NFT_REG_VERDICT)
4151 return -EINVAL;
4152 if (reg > NFT_REG_MAX)
4153 return -ERANGE;
4154 return 0;
4155}
4156EXPORT_SYMBOL_GPL(nft_validate_output_register);
4157
4158/**
4159 * nft_validate_data_load - validate an expressions' data load
4160 *
4161 * @ctx: context of the expression performing the load
4162 * @reg: the destination register number
4163 * @data: the data to load
4164 * @type: the data type
4165 *
4166 * Validate that a data load uses the appropriate data type for
4167 * the destination register. A value of NULL for the data means
4168 * that its runtime gathered data, which is always of type
4169 * NFT_DATA_VALUE.
4170 */
4171int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
4172 const struct nft_data *data,
4173 enum nft_data_types type)
4174{
Patrick McHardy20a69342013-10-11 12:06:22 +02004175 int err;
4176
Patrick McHardy96518512013-10-14 11:00:02 +02004177 switch (reg) {
4178 case NFT_REG_VERDICT:
4179 if (data == NULL || type != NFT_DATA_VERDICT)
4180 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02004181
4182 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
4183 err = nf_tables_check_loops(ctx, data->chain);
4184 if (err < 0)
4185 return err;
4186
4187 if (ctx->chain->level + 1 > data->chain->level) {
4188 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
4189 return -EMLINK;
4190 data->chain->level = ctx->chain->level + 1;
4191 }
4192 }
4193
Patrick McHardy96518512013-10-14 11:00:02 +02004194 return 0;
4195 default:
4196 if (data != NULL && type != NFT_DATA_VALUE)
4197 return -EINVAL;
4198 return 0;
4199 }
4200}
4201EXPORT_SYMBOL_GPL(nft_validate_data_load);
4202
4203static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
4204 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
4205 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
4206 .len = NFT_CHAIN_MAXNAMELEN - 1 },
4207};
4208
4209static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
4210 struct nft_data_desc *desc, const struct nlattr *nla)
4211{
4212 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
4213 struct nft_chain *chain;
4214 int err;
4215
4216 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
4217 if (err < 0)
4218 return err;
4219
4220 if (!tb[NFTA_VERDICT_CODE])
4221 return -EINVAL;
4222 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
4223
4224 switch (data->verdict) {
Patrick McHardye0abdad2014-02-18 18:06:50 +00004225 default:
4226 switch (data->verdict & NF_VERDICT_MASK) {
4227 case NF_ACCEPT:
4228 case NF_DROP:
4229 case NF_QUEUE:
4230 break;
4231 default:
4232 return -EINVAL;
4233 }
4234 /* fall through */
Patrick McHardy96518512013-10-14 11:00:02 +02004235 case NFT_CONTINUE:
4236 case NFT_BREAK:
4237 case NFT_RETURN:
4238 desc->len = sizeof(data->verdict);
4239 break;
4240 case NFT_JUMP:
4241 case NFT_GOTO:
4242 if (!tb[NFTA_VERDICT_CHAIN])
4243 return -EINVAL;
4244 chain = nf_tables_chain_lookup(ctx->table,
4245 tb[NFTA_VERDICT_CHAIN]);
4246 if (IS_ERR(chain))
4247 return PTR_ERR(chain);
4248 if (chain->flags & NFT_BASE_CHAIN)
4249 return -EOPNOTSUPP;
4250
Patrick McHardy96518512013-10-14 11:00:02 +02004251 chain->use++;
4252 data->chain = chain;
4253 desc->len = sizeof(data);
4254 break;
Patrick McHardy96518512013-10-14 11:00:02 +02004255 }
4256
4257 desc->type = NFT_DATA_VERDICT;
4258 return 0;
4259}
4260
4261static void nft_verdict_uninit(const struct nft_data *data)
4262{
4263 switch (data->verdict) {
4264 case NFT_JUMP:
4265 case NFT_GOTO:
4266 data->chain->use--;
4267 break;
4268 }
4269}
4270
4271static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
4272{
4273 struct nlattr *nest;
4274
4275 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
4276 if (!nest)
4277 goto nla_put_failure;
4278
4279 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
4280 goto nla_put_failure;
4281
4282 switch (data->verdict) {
4283 case NFT_JUMP:
4284 case NFT_GOTO:
4285 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
4286 goto nla_put_failure;
4287 }
4288 nla_nest_end(skb, nest);
4289 return 0;
4290
4291nla_put_failure:
4292 return -1;
4293}
4294
4295static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
4296 struct nft_data_desc *desc, const struct nlattr *nla)
4297{
4298 unsigned int len;
4299
4300 len = nla_len(nla);
4301 if (len == 0)
4302 return -EINVAL;
4303 if (len > sizeof(data->data))
4304 return -EOVERFLOW;
4305
4306 nla_memcpy(data->data, nla, sizeof(data->data));
4307 desc->type = NFT_DATA_VALUE;
4308 desc->len = len;
4309 return 0;
4310}
4311
4312static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
4313 unsigned int len)
4314{
4315 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
4316}
4317
4318static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
4319 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
4320 .len = FIELD_SIZEOF(struct nft_data, data) },
4321 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
4322};
4323
4324/**
4325 * nft_data_init - parse nf_tables data netlink attributes
4326 *
4327 * @ctx: context of the expression using the data
4328 * @data: destination struct nft_data
4329 * @desc: data description
4330 * @nla: netlink attribute containing data
4331 *
4332 * Parse the netlink data attributes and initialize a struct nft_data.
4333 * The type and length of data are returned in the data description.
4334 *
4335 * The caller can indicate that it only wants to accept data of type
4336 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4337 */
4338int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
4339 struct nft_data_desc *desc, const struct nlattr *nla)
4340{
4341 struct nlattr *tb[NFTA_DATA_MAX + 1];
4342 int err;
4343
4344 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
4345 if (err < 0)
4346 return err;
4347
4348 if (tb[NFTA_DATA_VALUE])
4349 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
4350 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
4351 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
4352 return -EINVAL;
4353}
4354EXPORT_SYMBOL_GPL(nft_data_init);
4355
4356/**
4357 * nft_data_uninit - release a nft_data item
4358 *
4359 * @data: struct nft_data to release
4360 * @type: type of data
4361 *
4362 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4363 * all others need to be released by calling this function.
4364 */
4365void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
4366{
4367 switch (type) {
4368 case NFT_DATA_VALUE:
4369 return;
4370 case NFT_DATA_VERDICT:
4371 return nft_verdict_uninit(data);
4372 default:
4373 WARN_ON(1);
4374 }
4375}
4376EXPORT_SYMBOL_GPL(nft_data_uninit);
4377
4378int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
4379 enum nft_data_types type, unsigned int len)
4380{
4381 struct nlattr *nest;
4382 int err;
4383
4384 nest = nla_nest_start(skb, attr);
4385 if (nest == NULL)
4386 return -1;
4387
4388 switch (type) {
4389 case NFT_DATA_VALUE:
4390 err = nft_value_dump(skb, data, len);
4391 break;
4392 case NFT_DATA_VERDICT:
4393 err = nft_verdict_dump(skb, data);
4394 break;
4395 default:
4396 err = -EINVAL;
4397 WARN_ON(1);
4398 }
4399
4400 nla_nest_end(skb, nest);
4401 return err;
4402}
4403EXPORT_SYMBOL_GPL(nft_data_dump);
4404
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004405static int nf_tables_init_net(struct net *net)
4406{
4407 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02004408 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02004409 net->nft.base_seq = 1;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004410 return 0;
4411}
4412
4413static struct pernet_operations nf_tables_net_ops = {
4414 .init = nf_tables_init_net,
4415};
4416
Patrick McHardy96518512013-10-14 11:00:02 +02004417static int __init nf_tables_module_init(void)
4418{
4419 int err;
4420
4421 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
4422 GFP_KERNEL);
4423 if (info == NULL) {
4424 err = -ENOMEM;
4425 goto err1;
4426 }
4427
4428 err = nf_tables_core_module_init();
4429 if (err < 0)
4430 goto err2;
4431
4432 err = nfnetlink_subsys_register(&nf_tables_subsys);
4433 if (err < 0)
4434 goto err3;
4435
4436 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004437 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02004438err3:
4439 nf_tables_core_module_exit();
4440err2:
4441 kfree(info);
4442err1:
4443 return err;
4444}
4445
4446static void __exit nf_tables_module_exit(void)
4447{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004448 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02004449 nfnetlink_subsys_unregister(&nf_tables_subsys);
Pablo Neira Ayuso1b1bc492014-10-01 13:53:20 +02004450 rcu_barrier();
Patrick McHardy96518512013-10-14 11:00:02 +02004451 nf_tables_core_module_exit();
4452 kfree(info);
4453}
4454
4455module_init(nf_tables_module_init);
4456module_exit(nf_tables_module_exit);
4457
4458MODULE_LICENSE("GPL");
4459MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4460MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);