blob: a476b99621551e8bb27aa06b9d67878c3ab4a43f [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{
201 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
202}
203
204static inline int gencursor_next(struct net *net)
205{
206 return net->nft.gencursor+1 == 1 ? 1 : 0;
207}
208
209static inline int
210nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
211{
212 return (rule->genmask & (1 << gencursor_next(net))) == 0;
213}
214
215static inline void
216nft_rule_activate_next(struct net *net, struct nft_rule *rule)
217{
218 /* Now inactive, will be active in the future */
219 rule->genmask = (1 << net->nft.gencursor);
220}
221
222static inline void
223nft_rule_deactivate_next(struct net *net, struct nft_rule *rule)
224{
225 rule->genmask = (1 << gencursor_next(net));
226}
227
228static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
229{
230 rule->genmask = 0;
231}
232
233static int
234nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
235{
236 /* You cannot delete the same rule twice */
237 if (nft_rule_is_active_next(ctx->net, rule)) {
238 nft_rule_deactivate_next(ctx->net, rule);
239 ctx->chain->use--;
240 return 0;
241 }
242 return -ENOENT;
243}
244
245static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
246 struct nft_rule *rule)
247{
248 struct nft_trans *trans;
249
250 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
251 if (trans == NULL)
252 return NULL;
253
254 nft_trans_rule(trans) = rule;
255 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
256
257 return trans;
258}
259
260static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
261{
262 struct nft_trans *trans;
263 int err;
264
265 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
266 if (trans == NULL)
267 return -ENOMEM;
268
269 err = nf_tables_delrule_deactivate(ctx, rule);
270 if (err < 0) {
271 nft_trans_destroy(trans);
272 return err;
273 }
274
275 return 0;
276}
277
278static int nft_delrule_by_chain(struct nft_ctx *ctx)
279{
280 struct nft_rule *rule;
281 int err;
282
283 list_for_each_entry(rule, &ctx->chain->rules, list) {
284 err = nft_delrule(ctx, rule);
285 if (err < 0)
286 return err;
287 }
288 return 0;
289}
290
291/* Internal set flag */
292#define NFT_SET_INACTIVE (1 << 15)
293
294static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
295 struct nft_set *set)
296{
297 struct nft_trans *trans;
298
299 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
300 if (trans == NULL)
301 return -ENOMEM;
302
303 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
304 nft_trans_set_id(trans) =
305 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
306 set->flags |= NFT_SET_INACTIVE;
307 }
308 nft_trans_set(trans) = set;
309 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
310
311 return 0;
312}
313
314static int nft_delset(struct nft_ctx *ctx, struct nft_set *set)
315{
316 int err;
317
318 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
319 if (err < 0)
320 return err;
321
322 list_del_rcu(&set->list);
323 ctx->table->use--;
324
325 return err;
326}
327
Patrick McHardy96518512013-10-14 11:00:02 +0200328/*
329 * Tables
330 */
331
332static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
333 const struct nlattr *nla)
334{
335 struct nft_table *table;
336
337 list_for_each_entry(table, &afi->tables, list) {
338 if (!nla_strcmp(nla, table->name))
339 return table;
340 }
341 return NULL;
342}
343
344static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200345 const struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +0200346{
347 struct nft_table *table;
348
349 if (nla == NULL)
350 return ERR_PTR(-EINVAL);
351
352 table = nft_table_lookup(afi, nla);
353 if (table != NULL)
354 return table;
355
Patrick McHardy96518512013-10-14 11:00:02 +0200356 return ERR_PTR(-ENOENT);
357}
358
359static inline u64 nf_tables_alloc_handle(struct nft_table *table)
360{
361 return ++table->hgenerator;
362}
363
Patrick McHardy2a37d752014-01-09 18:42:37 +0000364static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200365
Patrick McHardy2a37d752014-01-09 18:42:37 +0000366static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000367__nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200368{
369 int i;
370
Patrick McHardybaae3e62014-01-09 18:42:34 +0000371 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200372 if (chain_type[family][i] != NULL &&
373 !nla_strcmp(nla, chain_type[family][i]->name))
Patrick McHardybaae3e62014-01-09 18:42:34 +0000374 return chain_type[family][i];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200375 }
Patrick McHardybaae3e62014-01-09 18:42:34 +0000376 return NULL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200377}
378
Patrick McHardy2a37d752014-01-09 18:42:37 +0000379static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000380nf_tables_chain_type_lookup(const struct nft_af_info *afi,
381 const struct nlattr *nla,
382 bool autoload)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200383{
Patrick McHardy2a37d752014-01-09 18:42:37 +0000384 const struct nf_chain_type *type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200385
386 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000387 if (type != NULL)
388 return type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200389#ifdef CONFIG_MODULES
Patrick McHardy93b08062014-01-09 18:42:36 +0000390 if (autoload) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200391 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso2fec6bb2014-03-31 12:26:39 +0200392 request_module("nft-chain-%u-%.*s", afi->family,
393 nla_len(nla), (const char *)nla_data(nla));
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200394 nfnl_lock(NFNL_SUBSYS_NFTABLES);
395 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000396 if (type != NULL)
397 return ERR_PTR(-EAGAIN);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200398 }
399#endif
Patrick McHardy93b08062014-01-09 18:42:36 +0000400 return ERR_PTR(-ENOENT);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200401}
402
Patrick McHardy96518512013-10-14 11:00:02 +0200403static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
404 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200405 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
Patrick McHardy96518512013-10-14 11:00:02 +0200406};
407
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200408static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
409 u32 portid, u32 seq, int event, u32 flags,
410 int family, const struct nft_table *table)
Patrick McHardy96518512013-10-14 11:00:02 +0200411{
412 struct nlmsghdr *nlh;
413 struct nfgenmsg *nfmsg;
414
415 event |= NFNL_SUBSYS_NFTABLES << 8;
416 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
417 if (nlh == NULL)
418 goto nla_put_failure;
419
420 nfmsg = nlmsg_data(nlh);
421 nfmsg->nfgen_family = family;
422 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200423 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +0200424
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200425 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
Tomasz Bursztykad8bcc7682013-12-12 15:00:42 +0200426 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
427 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
Patrick McHardy96518512013-10-14 11:00:02 +0200428 goto nla_put_failure;
429
430 return nlmsg_end(skb, nlh);
431
432nla_put_failure:
433 nlmsg_trim(skb, nlh);
434 return -1;
435}
436
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200437static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)
Patrick McHardy96518512013-10-14 11:00:02 +0200438{
439 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +0200440 int err;
441
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200442 if (!ctx->report &&
443 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +0200444 return 0;
445
446 err = -ENOBUFS;
447 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
448 if (skb == NULL)
449 goto err;
450
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200451 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
452 event, 0, ctx->afi->family, ctx->table);
Patrick McHardy96518512013-10-14 11:00:02 +0200453 if (err < 0) {
454 kfree_skb(skb);
455 goto err;
456 }
457
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200458 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
459 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +0200460err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200461 if (err < 0) {
462 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
463 err);
464 }
Patrick McHardy96518512013-10-14 11:00:02 +0200465 return err;
466}
467
468static int nf_tables_dump_tables(struct sk_buff *skb,
469 struct netlink_callback *cb)
470{
471 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
472 const struct nft_af_info *afi;
473 const struct nft_table *table;
474 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200475 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200476 int family = nfmsg->nfgen_family;
477
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200478 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +0200479 cb->seq = net->nft.base_seq;
480
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200481 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200482 if (family != NFPROTO_UNSPEC && family != afi->family)
483 continue;
484
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200485 list_for_each_entry_rcu(table, &afi->tables, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200486 if (idx < s_idx)
487 goto cont;
488 if (idx > s_idx)
489 memset(&cb->args[1], 0,
490 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200491 if (nf_tables_fill_table_info(skb, net,
Patrick McHardy96518512013-10-14 11:00:02 +0200492 NETLINK_CB(cb->skb).portid,
493 cb->nlh->nlmsg_seq,
494 NFT_MSG_NEWTABLE,
495 NLM_F_MULTI,
496 afi->family, table) < 0)
497 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +0200498
499 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +0200500cont:
501 idx++;
502 }
503 }
504done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200505 rcu_read_unlock();
Patrick McHardy96518512013-10-14 11:00:02 +0200506 cb->args[0] = idx;
507 return skb->len;
508}
509
510static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
511 const struct nlmsghdr *nlh,
512 const struct nlattr * const nla[])
513{
514 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
515 const struct nft_af_info *afi;
516 const struct nft_table *table;
517 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200518 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200519 int family = nfmsg->nfgen_family;
520 int err;
521
522 if (nlh->nlmsg_flags & NLM_F_DUMP) {
523 struct netlink_dump_control c = {
524 .dump = nf_tables_dump_tables,
525 };
526 return netlink_dump_start(nlsk, skb, nlh, &c);
527 }
528
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200529 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200530 if (IS_ERR(afi))
531 return PTR_ERR(afi);
532
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200533 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200534 if (IS_ERR(table))
535 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200536 if (table->flags & NFT_TABLE_INACTIVE)
537 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200538
539 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
540 if (!skb2)
541 return -ENOMEM;
542
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200543 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +0200544 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
545 family, table);
546 if (err < 0)
547 goto err;
548
549 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
550
551err:
552 kfree_skb(skb2);
553 return err;
554}
555
Patrick McHardy115a60b2014-01-03 12:16:15 +0000556static int nf_tables_table_enable(const struct nft_af_info *afi,
557 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200558{
559 struct nft_chain *chain;
560 int err, i = 0;
561
562 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100563 if (!(chain->flags & NFT_BASE_CHAIN))
564 continue;
565
Patrick McHardy115a60b2014-01-03 12:16:15 +0000566 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200567 if (err < 0)
568 goto err;
569
570 i++;
571 }
572 return 0;
573err:
574 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100575 if (!(chain->flags & NFT_BASE_CHAIN))
576 continue;
577
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200578 if (i-- <= 0)
579 break;
580
Patrick McHardy115a60b2014-01-03 12:16:15 +0000581 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200582 }
583 return err;
584}
585
Pablo Neira Ayusof75edf52014-03-30 14:04:52 +0200586static void nf_tables_table_disable(const struct nft_af_info *afi,
Patrick McHardy115a60b2014-01-03 12:16:15 +0000587 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200588{
589 struct nft_chain *chain;
590
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100591 list_for_each_entry(chain, &table->chains, list) {
592 if (chain->flags & NFT_BASE_CHAIN)
Patrick McHardy115a60b2014-01-03 12:16:15 +0000593 nf_unregister_hooks(nft_base_chain(chain)->ops,
594 afi->nops);
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100595 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200596}
597
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200598static int nf_tables_updtable(struct nft_ctx *ctx)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200599{
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200600 struct nft_trans *trans;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200601 u32 flags;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200602 int ret = 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200603
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200604 if (!ctx->nla[NFTA_TABLE_FLAGS])
605 return 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200606
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200607 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
608 if (flags & ~NFT_TABLE_F_DORMANT)
609 return -EINVAL;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200610
Pablo Neira Ayuso63283dd2014-06-27 18:51:39 +0200611 if (flags == ctx->table->flags)
612 return 0;
613
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200614 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
615 sizeof(struct nft_trans_table));
616 if (trans == NULL)
617 return -ENOMEM;
618
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200619 if ((flags & NFT_TABLE_F_DORMANT) &&
620 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200621 nft_trans_table_enable(trans) = false;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200622 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
623 ctx->table->flags & NFT_TABLE_F_DORMANT) {
624 ret = nf_tables_table_enable(ctx->afi, ctx->table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200625 if (ret >= 0) {
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200626 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200627 nft_trans_table_enable(trans) = true;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200628 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200629 }
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200630 if (ret < 0)
631 goto err;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200632
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200633 nft_trans_table_update(trans) = true;
634 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
635 return 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200636err:
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200637 nft_trans_destroy(trans);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200638 return ret;
639}
640
Patrick McHardy96518512013-10-14 11:00:02 +0200641static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
642 const struct nlmsghdr *nlh,
643 const struct nlattr * const nla[])
644{
645 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
646 const struct nlattr *name;
647 struct nft_af_info *afi;
648 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200649 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200650 int family = nfmsg->nfgen_family;
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000651 u32 flags = 0;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200652 struct nft_ctx ctx;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200653 int err;
Patrick McHardy96518512013-10-14 11:00:02 +0200654
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200655 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200656 if (IS_ERR(afi))
657 return PTR_ERR(afi);
658
659 name = nla[NFTA_TABLE_NAME];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200660 table = nf_tables_table_lookup(afi, name);
Patrick McHardy96518512013-10-14 11:00:02 +0200661 if (IS_ERR(table)) {
662 if (PTR_ERR(table) != -ENOENT)
663 return PTR_ERR(table);
664 table = NULL;
665 }
666
667 if (table != NULL) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200668 if (table->flags & NFT_TABLE_INACTIVE)
669 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200670 if (nlh->nlmsg_flags & NLM_F_EXCL)
671 return -EEXIST;
672 if (nlh->nlmsg_flags & NLM_F_REPLACE)
673 return -EOPNOTSUPP;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200674
675 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
676 return nf_tables_updtable(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +0200677 }
678
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000679 if (nla[NFTA_TABLE_FLAGS]) {
680 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
681 if (flags & ~NFT_TABLE_F_DORMANT)
682 return -EINVAL;
683 }
684
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000685 if (!try_module_get(afi->owner))
686 return -EAFNOSUPPORT;
687
Patrick McHardy96518512013-10-14 11:00:02 +0200688 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000689 if (table == NULL) {
690 module_put(afi->owner);
Patrick McHardy96518512013-10-14 11:00:02 +0200691 return -ENOMEM;
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000692 }
Patrick McHardy96518512013-10-14 11:00:02 +0200693
694 nla_strlcpy(table->name, name, nla_len(name));
695 INIT_LIST_HEAD(&table->chains);
Patrick McHardy20a69342013-10-11 12:06:22 +0200696 INIT_LIST_HEAD(&table->sets);
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000697 table->flags = flags;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200698
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200699 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
700 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
701 if (err < 0) {
702 kfree(table);
703 module_put(afi->owner);
704 return err;
705 }
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200706 list_add_tail_rcu(&table->list, &afi->tables);
Patrick McHardy96518512013-10-14 11:00:02 +0200707 return 0;
708}
709
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200710static int nft_flush_table(struct nft_ctx *ctx)
711{
712 int err;
713 struct nft_chain *chain, *nc;
714 struct nft_set *set, *ns;
715
716 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
717 ctx->chain = chain;
718
719 err = nft_delrule_by_chain(ctx);
720 if (err < 0)
721 goto out;
722
723 err = nft_delchain(ctx);
724 if (err < 0)
725 goto out;
726 }
727
728 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
729 if (set->flags & NFT_SET_ANONYMOUS &&
730 !list_empty(&set->bindings))
731 continue;
732
733 err = nft_delset(ctx, set);
734 if (err < 0)
735 goto out;
736 }
737
738 err = nft_deltable(ctx);
739out:
740 return err;
741}
742
743static int nft_flush(struct nft_ctx *ctx, int family)
744{
745 struct nft_af_info *afi;
746 struct nft_table *table, *nt;
747 const struct nlattr * const *nla = ctx->nla;
748 int err = 0;
749
750 list_for_each_entry(afi, &ctx->net->nft.af_info, list) {
751 if (family != AF_UNSPEC && afi->family != family)
752 continue;
753
754 ctx->afi = afi;
755 list_for_each_entry_safe(table, nt, &afi->tables, list) {
756 if (nla[NFTA_TABLE_NAME] &&
757 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
758 continue;
759
760 ctx->table = table;
761
762 err = nft_flush_table(ctx);
763 if (err < 0)
764 goto out;
765 }
766 }
767out:
768 return err;
769}
770
Patrick McHardy96518512013-10-14 11:00:02 +0200771static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
772 const struct nlmsghdr *nlh,
773 const struct nlattr * const nla[])
774{
775 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
776 struct nft_af_info *afi;
777 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200778 struct net *net = sock_net(skb->sk);
Arturo Borreroee01d542014-09-02 16:42:25 +0200779 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200780 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +0200781
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200782 nft_ctx_init(&ctx, skb, nlh, NULL, NULL, NULL, nla);
783 if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL)
784 return nft_flush(&ctx, family);
785
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200786 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200787 if (IS_ERR(afi))
788 return PTR_ERR(afi);
789
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200790 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200791 if (IS_ERR(table))
792 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200793 if (table->flags & NFT_TABLE_INACTIVE)
794 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200795
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200796 ctx.afi = afi;
797 ctx.table = table;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200798
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200799 return nft_flush_table(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +0200800}
801
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200802static void nf_tables_table_destroy(struct nft_ctx *ctx)
803{
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +0200804 BUG_ON(ctx->table->use > 0);
805
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200806 kfree(ctx->table);
807 module_put(ctx->afi->owner);
808}
809
Patrick McHardy2a37d752014-01-09 18:42:37 +0000810int nft_register_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200811{
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200812 int err = 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200813
814 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200815 if (chain_type[ctype->family][ctype->type] != NULL) {
816 err = -EBUSY;
817 goto out;
Patrick McHardy96518512013-10-14 11:00:02 +0200818 }
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200819 chain_type[ctype->family][ctype->type] = ctype;
820out:
Patrick McHardy96518512013-10-14 11:00:02 +0200821 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
822 return err;
823}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200824EXPORT_SYMBOL_GPL(nft_register_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200825
Patrick McHardy2a37d752014-01-09 18:42:37 +0000826void nft_unregister_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200827{
Patrick McHardy96518512013-10-14 11:00:02 +0200828 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200829 chain_type[ctype->family][ctype->type] = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200830 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
831}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200832EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200833
834/*
835 * Chains
836 */
837
838static struct nft_chain *
839nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
840{
841 struct nft_chain *chain;
842
843 list_for_each_entry(chain, &table->chains, list) {
844 if (chain->handle == handle)
845 return chain;
846 }
847
848 return ERR_PTR(-ENOENT);
849}
850
851static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
852 const struct nlattr *nla)
853{
854 struct nft_chain *chain;
855
856 if (nla == NULL)
857 return ERR_PTR(-EINVAL);
858
859 list_for_each_entry(chain, &table->chains, list) {
860 if (!nla_strcmp(nla, chain->name))
861 return chain;
862 }
863
864 return ERR_PTR(-ENOENT);
865}
866
867static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
868 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
869 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
870 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
871 .len = NFT_CHAIN_MAXNAMELEN - 1 },
872 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200873 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
Pablo Neira4c1f7812014-03-31 17:43:47 +0200874 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200875 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
Patrick McHardy96518512013-10-14 11:00:02 +0200876};
877
878static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
879 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
880 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
881};
882
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200883static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
884{
885 struct nft_stats *cpu_stats, total;
886 struct nlattr *nest;
Eric Dumazetce355e22014-07-09 15:14:06 +0200887 unsigned int seq;
888 u64 pkts, bytes;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200889 int cpu;
890
891 memset(&total, 0, sizeof(total));
892 for_each_possible_cpu(cpu) {
893 cpu_stats = per_cpu_ptr(stats, cpu);
Eric Dumazetce355e22014-07-09 15:14:06 +0200894 do {
895 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
896 pkts = cpu_stats->pkts;
897 bytes = cpu_stats->bytes;
898 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
899 total.pkts += pkts;
900 total.bytes += bytes;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200901 }
902 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
903 if (nest == NULL)
904 goto nla_put_failure;
905
906 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
907 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
908 goto nla_put_failure;
909
910 nla_nest_end(skb, nest);
911 return 0;
912
913nla_put_failure:
914 return -ENOSPC;
915}
916
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200917static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
918 u32 portid, u32 seq, int event, u32 flags,
919 int family, const struct nft_table *table,
Patrick McHardy96518512013-10-14 11:00:02 +0200920 const struct nft_chain *chain)
921{
922 struct nlmsghdr *nlh;
923 struct nfgenmsg *nfmsg;
924
925 event |= NFNL_SUBSYS_NFTABLES << 8;
926 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
927 if (nlh == NULL)
928 goto nla_put_failure;
929
930 nfmsg = nlmsg_data(nlh);
931 nfmsg->nfgen_family = family;
932 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200933 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +0200934
935 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
936 goto nla_put_failure;
937 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
938 goto nla_put_failure;
939 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
940 goto nla_put_failure;
941
942 if (chain->flags & NFT_BASE_CHAIN) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200943 const struct nft_base_chain *basechain = nft_base_chain(chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000944 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200945 struct nlattr *nest;
946
947 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
Patrick McHardy96518512013-10-14 11:00:02 +0200948 if (nest == NULL)
949 goto nla_put_failure;
950 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
951 goto nla_put_failure;
952 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
953 goto nla_put_failure;
954 nla_nest_end(skb, nest);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200955
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200956 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
957 htonl(basechain->policy)))
958 goto nla_put_failure;
959
Patrick McHardybaae3e62014-01-09 18:42:34 +0000960 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
961 goto nla_put_failure;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200962
963 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
964 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +0200965 }
966
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200967 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
968 goto nla_put_failure;
969
Patrick McHardy96518512013-10-14 11:00:02 +0200970 return nlmsg_end(skb, nlh);
971
972nla_put_failure:
973 nlmsg_trim(skb, nlh);
974 return -1;
975}
976
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200977static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
Patrick McHardy96518512013-10-14 11:00:02 +0200978{
979 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +0200980 int err;
981
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200982 if (!ctx->report &&
983 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +0200984 return 0;
985
986 err = -ENOBUFS;
987 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
988 if (skb == NULL)
989 goto err;
990
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200991 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
992 event, 0, ctx->afi->family, ctx->table,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200993 ctx->chain);
Patrick McHardy96518512013-10-14 11:00:02 +0200994 if (err < 0) {
995 kfree_skb(skb);
996 goto err;
997 }
998
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200999 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1000 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001001err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001002 if (err < 0) {
1003 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1004 err);
1005 }
Patrick McHardy96518512013-10-14 11:00:02 +02001006 return err;
1007}
1008
1009static int nf_tables_dump_chains(struct sk_buff *skb,
1010 struct netlink_callback *cb)
1011{
1012 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1013 const struct nft_af_info *afi;
1014 const struct nft_table *table;
1015 const struct nft_chain *chain;
1016 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001017 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001018 int family = nfmsg->nfgen_family;
1019
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001020 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001021 cb->seq = net->nft.base_seq;
1022
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001023 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001024 if (family != NFPROTO_UNSPEC && family != afi->family)
1025 continue;
1026
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001027 list_for_each_entry_rcu(table, &afi->tables, list) {
1028 list_for_each_entry_rcu(chain, &table->chains, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001029 if (idx < s_idx)
1030 goto cont;
1031 if (idx > s_idx)
1032 memset(&cb->args[1], 0,
1033 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001034 if (nf_tables_fill_chain_info(skb, net,
1035 NETLINK_CB(cb->skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001036 cb->nlh->nlmsg_seq,
1037 NFT_MSG_NEWCHAIN,
1038 NLM_F_MULTI,
1039 afi->family, table, chain) < 0)
1040 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001041
1042 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +02001043cont:
1044 idx++;
1045 }
1046 }
1047 }
1048done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001049 rcu_read_unlock();
Patrick McHardy96518512013-10-14 11:00:02 +02001050 cb->args[0] = idx;
1051 return skb->len;
1052}
1053
Patrick McHardy96518512013-10-14 11:00:02 +02001054static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
1055 const struct nlmsghdr *nlh,
1056 const struct nlattr * const nla[])
1057{
1058 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1059 const struct nft_af_info *afi;
1060 const struct nft_table *table;
1061 const struct nft_chain *chain;
1062 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001063 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001064 int family = nfmsg->nfgen_family;
1065 int err;
1066
1067 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1068 struct netlink_dump_control c = {
1069 .dump = nf_tables_dump_chains,
1070 };
1071 return netlink_dump_start(nlsk, skb, nlh, &c);
1072 }
1073
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001074 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001075 if (IS_ERR(afi))
1076 return PTR_ERR(afi);
1077
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001078 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001079 if (IS_ERR(table))
1080 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001081 if (table->flags & NFT_TABLE_INACTIVE)
1082 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001083
1084 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1085 if (IS_ERR(chain))
1086 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001087 if (chain->flags & NFT_CHAIN_INACTIVE)
1088 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001089
1090 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1091 if (!skb2)
1092 return -ENOMEM;
1093
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001094 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001095 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
1096 family, table, chain);
1097 if (err < 0)
1098 goto err;
1099
1100 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1101
1102err:
1103 kfree_skb(skb2);
1104 return err;
1105}
1106
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001107static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1108 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
1109 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
1110};
1111
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001112static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001113{
1114 struct nlattr *tb[NFTA_COUNTER_MAX+1];
1115 struct nft_stats __percpu *newstats;
1116 struct nft_stats *stats;
1117 int err;
1118
1119 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
1120 if (err < 0)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001121 return ERR_PTR(err);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001122
1123 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001124 return ERR_PTR(-EINVAL);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001125
Eric Dumazetce355e22014-07-09 15:14:06 +02001126 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001127 if (newstats == NULL)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001128 return ERR_PTR(-ENOMEM);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001129
1130 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1131 * are not exposed to userspace.
1132 */
1133 stats = this_cpu_ptr(newstats);
1134 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1135 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
1136
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001137 return newstats;
1138}
1139
1140static void nft_chain_stats_replace(struct nft_base_chain *chain,
1141 struct nft_stats __percpu *newstats)
1142{
Pablo Neira Ayusob88825d2014-08-05 17:25:59 +02001143 if (newstats == NULL)
1144 return;
1145
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001146 if (chain->stats) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001147 struct nft_stats __percpu *oldstats =
Patrick McHardy67a8fc22014-02-18 18:06:49 +00001148 nft_dereference(chain->stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001149
1150 rcu_assign_pointer(chain->stats, newstats);
1151 synchronize_rcu();
1152 free_percpu(oldstats);
1153 } else
1154 rcu_assign_pointer(chain->stats, newstats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001155}
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001156
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001157static void nf_tables_chain_destroy(struct nft_chain *chain)
1158{
1159 BUG_ON(chain->use > 0);
1160
1161 if (chain->flags & NFT_BASE_CHAIN) {
1162 module_put(nft_base_chain(chain)->type->owner);
1163 free_percpu(nft_base_chain(chain)->stats);
1164 kfree(nft_base_chain(chain));
1165 } else {
1166 kfree(chain);
1167 }
1168}
1169
Patrick McHardy96518512013-10-14 11:00:02 +02001170static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
1171 const struct nlmsghdr *nlh,
1172 const struct nlattr * const nla[])
1173{
1174 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1175 const struct nlattr * uninitialized_var(name);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001176 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001177 struct nft_table *table;
1178 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001179 struct nft_base_chain *basechain = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001180 struct nlattr *ha[NFTA_HOOK_MAX + 1];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001181 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001182 int family = nfmsg->nfgen_family;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001183 u8 policy = NF_ACCEPT;
Patrick McHardy96518512013-10-14 11:00:02 +02001184 u64 handle = 0;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001185 unsigned int i;
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001186 struct nft_stats __percpu *stats;
Patrick McHardy96518512013-10-14 11:00:02 +02001187 int err;
1188 bool create;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001189 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001190
1191 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1192
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001193 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +02001194 if (IS_ERR(afi))
1195 return PTR_ERR(afi);
1196
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001197 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001198 if (IS_ERR(table))
1199 return PTR_ERR(table);
1200
Patrick McHardy96518512013-10-14 11:00:02 +02001201 chain = NULL;
1202 name = nla[NFTA_CHAIN_NAME];
1203
1204 if (nla[NFTA_CHAIN_HANDLE]) {
1205 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
1206 chain = nf_tables_chain_lookup_byhandle(table, handle);
1207 if (IS_ERR(chain))
1208 return PTR_ERR(chain);
1209 } else {
1210 chain = nf_tables_chain_lookup(table, name);
1211 if (IS_ERR(chain)) {
1212 if (PTR_ERR(chain) != -ENOENT)
1213 return PTR_ERR(chain);
1214 chain = NULL;
1215 }
1216 }
1217
Patrick McHardy57de2a02014-01-09 18:42:31 +00001218 if (nla[NFTA_CHAIN_POLICY]) {
1219 if ((chain != NULL &&
1220 !(chain->flags & NFT_BASE_CHAIN)) ||
1221 nla[NFTA_CHAIN_HOOK] == NULL)
1222 return -EOPNOTSUPP;
1223
Pablo Neira Ayuso8f46df12014-01-10 15:11:25 +01001224 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
Patrick McHardy57de2a02014-01-09 18:42:31 +00001225 switch (policy) {
1226 case NF_DROP:
1227 case NF_ACCEPT:
1228 break;
1229 default:
1230 return -EINVAL;
1231 }
1232 }
1233
Patrick McHardy96518512013-10-14 11:00:02 +02001234 if (chain != NULL) {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001235 struct nft_stats *stats = NULL;
1236 struct nft_trans *trans;
1237
1238 if (chain->flags & NFT_CHAIN_INACTIVE)
1239 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001240 if (nlh->nlmsg_flags & NLM_F_EXCL)
1241 return -EEXIST;
1242 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1243 return -EOPNOTSUPP;
1244
1245 if (nla[NFTA_CHAIN_HANDLE] && name &&
1246 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1247 return -EEXIST;
1248
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001249 if (nla[NFTA_CHAIN_COUNTERS]) {
1250 if (!(chain->flags & NFT_BASE_CHAIN))
1251 return -EOPNOTSUPP;
1252
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001253 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1254 if (IS_ERR(stats))
1255 return PTR_ERR(stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001256 }
1257
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001258 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1259 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1260 sizeof(struct nft_trans_chain));
1261 if (trans == NULL)
1262 return -ENOMEM;
1263
1264 nft_trans_chain_stats(trans) = stats;
1265 nft_trans_chain_update(trans) = true;
1266
Patrick McHardy4401a862014-01-09 18:42:32 +00001267 if (nla[NFTA_CHAIN_POLICY])
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001268 nft_trans_chain_policy(trans) = policy;
1269 else
1270 nft_trans_chain_policy(trans) = -1;
Patrick McHardy4401a862014-01-09 18:42:32 +00001271
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001272 if (nla[NFTA_CHAIN_HANDLE] && name) {
1273 nla_strlcpy(nft_trans_chain_name(trans), name,
1274 NFT_CHAIN_MAXNAMELEN);
1275 }
1276 list_add_tail(&trans->list, &net->nft.commit_list);
1277 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001278 }
1279
Patrick McHardy75820672014-01-09 18:42:33 +00001280 if (table->use == UINT_MAX)
1281 return -EOVERFLOW;
1282
Patrick McHardy96518512013-10-14 11:00:02 +02001283 if (nla[NFTA_CHAIN_HOOK]) {
Patrick McHardy2a37d752014-01-09 18:42:37 +00001284 const struct nf_chain_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001285 struct nf_hook_ops *ops;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001286 nf_hookfn *hookfn;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001287 u32 hooknum, priority;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001288
Patrick McHardybaae3e62014-01-09 18:42:34 +00001289 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001290 if (nla[NFTA_CHAIN_TYPE]) {
1291 type = nf_tables_chain_type_lookup(afi,
1292 nla[NFTA_CHAIN_TYPE],
1293 create);
Patrick McHardy93b08062014-01-09 18:42:36 +00001294 if (IS_ERR(type))
1295 return PTR_ERR(type);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001296 }
Patrick McHardy96518512013-10-14 11:00:02 +02001297
1298 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1299 nft_hook_policy);
1300 if (err < 0)
1301 return err;
1302 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1303 ha[NFTA_HOOK_PRIORITY] == NULL)
1304 return -EINVAL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001305
1306 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1307 if (hooknum >= afi->nhooks)
Patrick McHardy96518512013-10-14 11:00:02 +02001308 return -EINVAL;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001309 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
Patrick McHardy96518512013-10-14 11:00:02 +02001310
Patrick McHardybaae3e62014-01-09 18:42:34 +00001311 if (!(type->hook_mask & (1 << hooknum)))
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001312 return -EOPNOTSUPP;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001313 if (!try_module_get(type->owner))
Patrick McHardybaae3e62014-01-09 18:42:34 +00001314 return -ENOENT;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001315 hookfn = type->hooks[hooknum];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001316
Patrick McHardy96518512013-10-14 11:00:02 +02001317 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1318 if (basechain == NULL)
1319 return -ENOMEM;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001320
Patrick McHardy4401a862014-01-09 18:42:32 +00001321 if (nla[NFTA_CHAIN_COUNTERS]) {
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001322 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1323 if (IS_ERR(stats)) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001324 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +00001325 kfree(basechain);
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001326 return PTR_ERR(stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001327 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001328 basechain->stats = stats;
Patrick McHardy4401a862014-01-09 18:42:32 +00001329 } else {
Eric Dumazetce355e22014-07-09 15:14:06 +02001330 stats = netdev_alloc_pcpu_stats(struct nft_stats);
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001331 if (IS_ERR(stats)) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001332 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +00001333 kfree(basechain);
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001334 return PTR_ERR(stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001335 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001336 rcu_assign_pointer(basechain->stats, stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001337 }
1338
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001339 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +02001340 chain = &basechain->chain;
1341
Patrick McHardy115a60b2014-01-03 12:16:15 +00001342 for (i = 0; i < afi->nops; i++) {
1343 ops = &basechain->ops[i];
1344 ops->pf = family;
1345 ops->owner = afi->owner;
1346 ops->hooknum = hooknum;
1347 ops->priority = priority;
1348 ops->priv = chain;
1349 ops->hook = afi->hooks[ops->hooknum];
1350 if (hookfn)
1351 ops->hook = hookfn;
1352 if (afi->hook_ops_init)
1353 afi->hook_ops_init(ops, i);
1354 }
Patrick McHardy96518512013-10-14 11:00:02 +02001355
1356 chain->flags |= NFT_BASE_CHAIN;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001357 basechain->policy = policy;
Patrick McHardy96518512013-10-14 11:00:02 +02001358 } else {
1359 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1360 if (chain == NULL)
1361 return -ENOMEM;
1362 }
1363
1364 INIT_LIST_HEAD(&chain->rules);
1365 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001366 chain->net = net;
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +02001367 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +02001368 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1369
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001370 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1371 chain->flags & NFT_BASE_CHAIN) {
Patrick McHardy115a60b2014-01-03 12:16:15 +00001372 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001373 if (err < 0)
1374 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001375 }
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001376
1377 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1378 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1379 if (err < 0)
1380 goto err2;
1381
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001382 table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001383 list_add_tail_rcu(&chain->list, &table->chains);
Patrick McHardy96518512013-10-14 11:00:02 +02001384 return 0;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001385err2:
Arturo Borreroc5598792014-09-02 16:42:23 +02001386 nf_tables_unregister_hooks(table, chain, afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001387err1:
1388 nf_tables_chain_destroy(chain);
1389 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001390}
1391
1392static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1393 const struct nlmsghdr *nlh,
1394 const struct nlattr * const nla[])
1395{
1396 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001397 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001398 struct nft_table *table;
1399 struct nft_chain *chain;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001400 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001401 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001402 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001403
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001404 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001405 if (IS_ERR(afi))
1406 return PTR_ERR(afi);
1407
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001408 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001409 if (IS_ERR(table))
1410 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001411 if (table->flags & NFT_TABLE_INACTIVE)
1412 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001413
1414 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1415 if (IS_ERR(chain))
1416 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001417 if (chain->flags & NFT_CHAIN_INACTIVE)
1418 return -ENOENT;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001419 if (chain->use > 0)
Patrick McHardy96518512013-10-14 11:00:02 +02001420 return -EBUSY;
1421
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001422 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001423
Arturo Borreroee01d542014-09-02 16:42:25 +02001424 return nft_delchain(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +02001425}
1426
Patrick McHardy96518512013-10-14 11:00:02 +02001427/*
1428 * Expressions
1429 */
1430
1431/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001432 * nft_register_expr - register nf_tables expr type
1433 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001434 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001435 * Registers the expr type for use with nf_tables. Returns zero on
Patrick McHardy96518512013-10-14 11:00:02 +02001436 * success or a negative errno code otherwise.
1437 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001438int nft_register_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001439{
1440 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001441 if (type->family == NFPROTO_UNSPEC)
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001442 list_add_tail_rcu(&type->list, &nf_tables_expressions);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001443 else
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001444 list_add_rcu(&type->list, &nf_tables_expressions);
Patrick McHardy96518512013-10-14 11:00:02 +02001445 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1446 return 0;
1447}
1448EXPORT_SYMBOL_GPL(nft_register_expr);
1449
1450/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001451 * nft_unregister_expr - unregister nf_tables expr type
1452 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001453 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001454 * Unregisters the expr typefor use with nf_tables.
Patrick McHardy96518512013-10-14 11:00:02 +02001455 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001456void nft_unregister_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001457{
1458 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001459 list_del_rcu(&type->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001460 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1461}
1462EXPORT_SYMBOL_GPL(nft_unregister_expr);
1463
Patrick McHardy64d46802014-02-05 15:03:37 +00001464static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1465 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001466{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001467 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001468
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001469 list_for_each_entry(type, &nf_tables_expressions, list) {
Patrick McHardy64d46802014-02-05 15:03:37 +00001470 if (!nla_strcmp(nla, type->name) &&
1471 (!type->family || type->family == family))
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001472 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001473 }
1474 return NULL;
1475}
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
1482 if (nla == NULL)
1483 return ERR_PTR(-EINVAL);
1484
Patrick McHardy64d46802014-02-05 15:03:37 +00001485 type = __nft_expr_type_get(family, nla);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001486 if (type != NULL && try_module_get(type->owner))
1487 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001488
1489#ifdef CONFIG_MODULES
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001490 if (type == NULL) {
Patrick McHardy96518512013-10-14 11:00:02 +02001491 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001492 request_module("nft-expr-%u-%.*s", family,
1493 nla_len(nla), (char *)nla_data(nla));
1494 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1495 if (__nft_expr_type_get(family, nla))
1496 return ERR_PTR(-EAGAIN);
1497
1498 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy96518512013-10-14 11:00:02 +02001499 request_module("nft-expr-%.*s",
1500 nla_len(nla), (char *)nla_data(nla));
1501 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001502 if (__nft_expr_type_get(family, nla))
Patrick McHardy96518512013-10-14 11:00:02 +02001503 return ERR_PTR(-EAGAIN);
1504 }
1505#endif
1506 return ERR_PTR(-ENOENT);
1507}
1508
1509static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1510 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1511 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1512};
1513
1514static int nf_tables_fill_expr_info(struct sk_buff *skb,
1515 const struct nft_expr *expr)
1516{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001517 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
Patrick McHardy96518512013-10-14 11:00:02 +02001518 goto nla_put_failure;
1519
1520 if (expr->ops->dump) {
1521 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1522 if (data == NULL)
1523 goto nla_put_failure;
1524 if (expr->ops->dump(skb, expr) < 0)
1525 goto nla_put_failure;
1526 nla_nest_end(skb, data);
1527 }
1528
1529 return skb->len;
1530
1531nla_put_failure:
1532 return -1;
1533};
1534
1535struct nft_expr_info {
1536 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001537 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001538};
1539
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001540static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1541 const struct nlattr *nla,
Patrick McHardy96518512013-10-14 11:00:02 +02001542 struct nft_expr_info *info)
1543{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001544 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001545 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001546 struct nlattr *tb[NFTA_EXPR_MAX + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001547 int err;
1548
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001549 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
Patrick McHardy96518512013-10-14 11:00:02 +02001550 if (err < 0)
1551 return err;
1552
Patrick McHardy64d46802014-02-05 15:03:37 +00001553 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001554 if (IS_ERR(type))
1555 return PTR_ERR(type);
1556
1557 if (tb[NFTA_EXPR_DATA]) {
1558 err = nla_parse_nested(info->tb, type->maxattr,
1559 tb[NFTA_EXPR_DATA], type->policy);
1560 if (err < 0)
1561 goto err1;
1562 } else
1563 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1564
1565 if (type->select_ops != NULL) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001566 ops = type->select_ops(ctx,
1567 (const struct nlattr * const *)info->tb);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001568 if (IS_ERR(ops)) {
1569 err = PTR_ERR(ops);
1570 goto err1;
1571 }
1572 } else
1573 ops = type->ops;
1574
Patrick McHardy96518512013-10-14 11:00:02 +02001575 info->ops = ops;
1576 return 0;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001577
1578err1:
1579 module_put(type->owner);
1580 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001581}
1582
1583static int nf_tables_newexpr(const struct nft_ctx *ctx,
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001584 const struct nft_expr_info *info,
Patrick McHardy96518512013-10-14 11:00:02 +02001585 struct nft_expr *expr)
1586{
1587 const struct nft_expr_ops *ops = info->ops;
1588 int err;
1589
1590 expr->ops = ops;
1591 if (ops->init) {
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001592 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
Patrick McHardy96518512013-10-14 11:00:02 +02001593 if (err < 0)
1594 goto err1;
1595 }
1596
Patrick McHardy96518512013-10-14 11:00:02 +02001597 return 0;
1598
1599err1:
1600 expr->ops = NULL;
1601 return err;
1602}
1603
Patrick McHardy62472bc2014-03-07 19:08:30 +01001604static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1605 struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +02001606{
1607 if (expr->ops->destroy)
Patrick McHardy62472bc2014-03-07 19:08:30 +01001608 expr->ops->destroy(ctx, expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001609 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001610}
1611
1612/*
1613 * Rules
1614 */
1615
1616static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1617 u64 handle)
1618{
1619 struct nft_rule *rule;
1620
1621 // FIXME: this sucks
1622 list_for_each_entry(rule, &chain->rules, list) {
1623 if (handle == rule->handle)
1624 return rule;
1625 }
1626
1627 return ERR_PTR(-ENOENT);
1628}
1629
1630static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1631 const struct nlattr *nla)
1632{
1633 if (nla == NULL)
1634 return ERR_PTR(-EINVAL);
1635
1636 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1637}
1638
1639static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1640 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1641 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1642 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1643 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1644 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001645 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001646 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001647 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1648 .len = NFT_USERDATA_MAXLEN },
Patrick McHardy96518512013-10-14 11:00:02 +02001649};
1650
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001651static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1652 u32 portid, u32 seq, int event,
1653 u32 flags, int family,
Patrick McHardy96518512013-10-14 11:00:02 +02001654 const struct nft_table *table,
1655 const struct nft_chain *chain,
1656 const struct nft_rule *rule)
1657{
1658 struct nlmsghdr *nlh;
1659 struct nfgenmsg *nfmsg;
1660 const struct nft_expr *expr, *next;
1661 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001662 const struct nft_rule *prule;
1663 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001664
Eric Leblond5e948462013-10-10 13:41:44 +02001665 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001666 flags);
1667 if (nlh == NULL)
1668 goto nla_put_failure;
1669
1670 nfmsg = nlmsg_data(nlh);
1671 nfmsg->nfgen_family = family;
1672 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001673 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +02001674
1675 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1676 goto nla_put_failure;
1677 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1678 goto nla_put_failure;
1679 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1680 goto nla_put_failure;
1681
Eric Leblond5e948462013-10-10 13:41:44 +02001682 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1683 prule = list_entry(rule->list.prev, struct nft_rule, list);
1684 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1685 cpu_to_be64(prule->handle)))
1686 goto nla_put_failure;
1687 }
1688
Patrick McHardy96518512013-10-14 11:00:02 +02001689 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1690 if (list == NULL)
1691 goto nla_put_failure;
1692 nft_rule_for_each_expr(expr, next, rule) {
1693 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1694 if (elem == NULL)
1695 goto nla_put_failure;
1696 if (nf_tables_fill_expr_info(skb, expr) < 0)
1697 goto nla_put_failure;
1698 nla_nest_end(skb, elem);
1699 }
1700 nla_nest_end(skb, list);
1701
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001702 if (rule->ulen &&
1703 nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1704 goto nla_put_failure;
1705
Patrick McHardy96518512013-10-14 11:00:02 +02001706 return nlmsg_end(skb, nlh);
1707
1708nla_put_failure:
1709 nlmsg_trim(skb, nlh);
1710 return -1;
1711}
1712
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001713static int nf_tables_rule_notify(const struct nft_ctx *ctx,
Patrick McHardy96518512013-10-14 11:00:02 +02001714 const struct nft_rule *rule,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001715 int event)
Patrick McHardy96518512013-10-14 11:00:02 +02001716{
1717 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +02001718 int err;
1719
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001720 if (!ctx->report &&
1721 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +02001722 return 0;
1723
1724 err = -ENOBUFS;
1725 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1726 if (skb == NULL)
1727 goto err;
1728
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001729 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
1730 event, 0, ctx->afi->family, ctx->table,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001731 ctx->chain, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001732 if (err < 0) {
1733 kfree_skb(skb);
1734 goto err;
1735 }
1736
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001737 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1738 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001739err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001740 if (err < 0) {
1741 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1742 err);
1743 }
Patrick McHardy96518512013-10-14 11:00:02 +02001744 return err;
1745}
1746
1747static int nf_tables_dump_rules(struct sk_buff *skb,
1748 struct netlink_callback *cb)
1749{
1750 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1751 const struct nft_af_info *afi;
1752 const struct nft_table *table;
1753 const struct nft_chain *chain;
1754 const struct nft_rule *rule;
1755 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001756 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001757 int family = nfmsg->nfgen_family;
1758
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001759 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001760 cb->seq = net->nft.base_seq;
1761
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001762 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001763 if (family != NFPROTO_UNSPEC && family != afi->family)
1764 continue;
1765
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001766 list_for_each_entry_rcu(table, &afi->tables, list) {
1767 list_for_each_entry_rcu(chain, &table->chains, list) {
1768 list_for_each_entry_rcu(rule, &chain->rules, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001769 if (!nft_rule_is_active(net, rule))
1770 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001771 if (idx < s_idx)
1772 goto cont;
1773 if (idx > s_idx)
1774 memset(&cb->args[1], 0,
1775 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001776 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001777 cb->nlh->nlmsg_seq,
1778 NFT_MSG_NEWRULE,
1779 NLM_F_MULTI | NLM_F_APPEND,
1780 afi->family, table, chain, rule) < 0)
1781 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001782
1783 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +02001784cont:
1785 idx++;
1786 }
1787 }
1788 }
1789 }
1790done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001791 rcu_read_unlock();
1792
Patrick McHardy96518512013-10-14 11:00:02 +02001793 cb->args[0] = idx;
1794 return skb->len;
1795}
1796
1797static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1798 const struct nlmsghdr *nlh,
1799 const struct nlattr * const nla[])
1800{
1801 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1802 const struct nft_af_info *afi;
1803 const struct nft_table *table;
1804 const struct nft_chain *chain;
1805 const struct nft_rule *rule;
1806 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001807 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001808 int family = nfmsg->nfgen_family;
1809 int err;
1810
1811 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1812 struct netlink_dump_control c = {
1813 .dump = nf_tables_dump_rules,
1814 };
1815 return netlink_dump_start(nlsk, skb, nlh, &c);
1816 }
1817
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001818 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001819 if (IS_ERR(afi))
1820 return PTR_ERR(afi);
1821
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001822 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001823 if (IS_ERR(table))
1824 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001825 if (table->flags & NFT_TABLE_INACTIVE)
1826 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001827
1828 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1829 if (IS_ERR(chain))
1830 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001831 if (chain->flags & NFT_CHAIN_INACTIVE)
1832 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001833
1834 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1835 if (IS_ERR(rule))
1836 return PTR_ERR(rule);
1837
1838 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1839 if (!skb2)
1840 return -ENOMEM;
1841
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001842 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001843 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1844 family, table, chain, rule);
1845 if (err < 0)
1846 goto err;
1847
1848 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1849
1850err:
1851 kfree_skb(skb2);
1852 return err;
1853}
1854
Patrick McHardy62472bc2014-03-07 19:08:30 +01001855static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1856 struct nft_rule *rule)
Patrick McHardy96518512013-10-14 11:00:02 +02001857{
Patrick McHardy96518512013-10-14 11:00:02 +02001858 struct nft_expr *expr;
1859
1860 /*
1861 * Careful: some expressions might not be initialized in case this
1862 * is called on error from nf_tables_newrule().
1863 */
1864 expr = nft_expr_first(rule);
1865 while (expr->ops && expr != nft_expr_last(rule)) {
Patrick McHardy62472bc2014-03-07 19:08:30 +01001866 nf_tables_expr_destroy(ctx, expr);
Patrick McHardy96518512013-10-14 11:00:02 +02001867 expr = nft_expr_next(expr);
1868 }
1869 kfree(rule);
1870}
1871
Patrick McHardy96518512013-10-14 11:00:02 +02001872#define NFT_RULE_MAXEXPRS 128
1873
1874static struct nft_expr_info *info;
1875
1876static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1877 const struct nlmsghdr *nlh,
1878 const struct nlattr * const nla[])
1879{
1880 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001881 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001882 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001883 struct nft_table *table;
1884 struct nft_chain *chain;
1885 struct nft_rule *rule, *old_rule = NULL;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001886 struct nft_trans *trans = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001887 struct nft_expr *expr;
1888 struct nft_ctx ctx;
1889 struct nlattr *tmp;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001890 unsigned int size, i, n, ulen = 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001891 int err, rem;
1892 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02001893 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02001894
1895 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1896
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001897 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02001898 if (IS_ERR(afi))
1899 return PTR_ERR(afi);
1900
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001901 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001902 if (IS_ERR(table))
1903 return PTR_ERR(table);
1904
1905 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1906 if (IS_ERR(chain))
1907 return PTR_ERR(chain);
1908
1909 if (nla[NFTA_RULE_HANDLE]) {
1910 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1911 rule = __nf_tables_rule_lookup(chain, handle);
1912 if (IS_ERR(rule))
1913 return PTR_ERR(rule);
1914
1915 if (nlh->nlmsg_flags & NLM_F_EXCL)
1916 return -EEXIST;
1917 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1918 old_rule = rule;
1919 else
1920 return -EOPNOTSUPP;
1921 } else {
1922 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1923 return -EINVAL;
1924 handle = nf_tables_alloc_handle(table);
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +02001925
1926 if (chain->use == UINT_MAX)
1927 return -EOVERFLOW;
Patrick McHardy96518512013-10-14 11:00:02 +02001928 }
1929
Eric Leblond5e948462013-10-10 13:41:44 +02001930 if (nla[NFTA_RULE_POSITION]) {
1931 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1932 return -EOPNOTSUPP;
1933
1934 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1935 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1936 if (IS_ERR(old_rule))
1937 return PTR_ERR(old_rule);
1938 }
1939
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001940 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1941
Patrick McHardy96518512013-10-14 11:00:02 +02001942 n = 0;
1943 size = 0;
1944 if (nla[NFTA_RULE_EXPRESSIONS]) {
1945 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1946 err = -EINVAL;
1947 if (nla_type(tmp) != NFTA_LIST_ELEM)
1948 goto err1;
1949 if (n == NFT_RULE_MAXEXPRS)
1950 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001951 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02001952 if (err < 0)
1953 goto err1;
1954 size += info[n].ops->size;
1955 n++;
1956 }
1957 }
1958
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001959 if (nla[NFTA_RULE_USERDATA])
1960 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1961
Patrick McHardy96518512013-10-14 11:00:02 +02001962 err = -ENOMEM;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001963 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001964 if (rule == NULL)
1965 goto err1;
1966
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001967 nft_rule_activate_next(net, rule);
1968
Patrick McHardy96518512013-10-14 11:00:02 +02001969 rule->handle = handle;
1970 rule->dlen = size;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001971 rule->ulen = ulen;
1972
1973 if (ulen)
1974 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
Patrick McHardy96518512013-10-14 11:00:02 +02001975
Patrick McHardy96518512013-10-14 11:00:02 +02001976 expr = nft_expr_first(rule);
1977 for (i = 0; i < n; i++) {
1978 err = nf_tables_newexpr(&ctx, &info[i], expr);
1979 if (err < 0)
1980 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001981 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001982 expr = nft_expr_next(expr);
1983 }
1984
Patrick McHardy96518512013-10-14 11:00:02 +02001985 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001986 if (nft_rule_is_active_next(net, old_rule)) {
Pablo Neira Ayusoac904ac2014-06-10 10:53:03 +02001987 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02001988 old_rule);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001989 if (trans == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001990 err = -ENOMEM;
1991 goto err2;
1992 }
Arturo Borreroee01d542014-09-02 16:42:25 +02001993 nft_rule_deactivate_next(net, old_rule);
Pablo Neira Ayusoac34b862014-06-10 10:53:02 +02001994 chain->use--;
Pablo Neira Ayuso5bc5c302014-06-10 10:53:00 +02001995 list_add_tail_rcu(&rule->list, &old_rule->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001996 } else {
1997 err = -ENOENT;
1998 goto err2;
1999 }
Patrick McHardy96518512013-10-14 11:00:02 +02002000 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02002001 if (old_rule)
2002 list_add_rcu(&rule->list, &old_rule->list);
2003 else
2004 list_add_tail_rcu(&rule->list, &chain->rules);
2005 else {
2006 if (old_rule)
2007 list_add_tail_rcu(&rule->list, &old_rule->list);
2008 else
2009 list_add_rcu(&rule->list, &chain->rules);
2010 }
Patrick McHardy96518512013-10-14 11:00:02 +02002011
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02002012 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002013 err = -ENOMEM;
2014 goto err3;
2015 }
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02002016 chain->use++;
Patrick McHardy96518512013-10-14 11:00:02 +02002017 return 0;
2018
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002019err3:
2020 list_del_rcu(&rule->list);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02002021 if (trans) {
2022 list_del_rcu(&nft_trans_rule(trans)->list);
2023 nft_rule_clear(net, nft_trans_rule(trans));
2024 nft_trans_destroy(trans);
Pablo Neira Ayusoac34b862014-06-10 10:53:02 +02002025 chain->use++;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002026 }
Patrick McHardy96518512013-10-14 11:00:02 +02002027err2:
Patrick McHardy62472bc2014-03-07 19:08:30 +01002028 nf_tables_rule_destroy(&ctx, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02002029err1:
2030 for (i = 0; i < n; i++) {
2031 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002032 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02002033 }
2034 return err;
2035}
2036
2037static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
2038 const struct nlmsghdr *nlh,
2039 const struct nlattr * const nla[])
2040{
2041 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002042 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002043 struct net *net = sock_net(skb->sk);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002044 struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002045 struct nft_chain *chain = NULL;
2046 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002047 int family = nfmsg->nfgen_family, err = 0;
2048 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02002049
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002050 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02002051 if (IS_ERR(afi))
2052 return PTR_ERR(afi);
2053
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002054 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02002055 if (IS_ERR(table))
2056 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002057 if (table->flags & NFT_TABLE_INACTIVE)
2058 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02002059
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002060 if (nla[NFTA_RULE_CHAIN]) {
2061 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
2062 if (IS_ERR(chain))
2063 return PTR_ERR(chain);
2064 }
Patrick McHardy96518512013-10-14 11:00:02 +02002065
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002066 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
2067
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002068 if (chain) {
2069 if (nla[NFTA_RULE_HANDLE]) {
2070 rule = nf_tables_rule_lookup(chain,
2071 nla[NFTA_RULE_HANDLE]);
2072 if (IS_ERR(rule))
2073 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02002074
Arturo Borrero5e266fe2014-09-02 16:42:21 +02002075 err = nft_delrule(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002076 } else {
Arturo Borreroce24b722014-09-02 16:42:24 +02002077 err = nft_delrule_by_chain(&ctx);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002078 }
2079 } else {
2080 list_for_each_entry(chain, &table->chains, list) {
2081 ctx.chain = chain;
Arturo Borreroce24b722014-09-02 16:42:24 +02002082 err = nft_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002083 if (err < 0)
2084 break;
Patrick McHardy96518512013-10-14 11:00:02 +02002085 }
2086 }
2087
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002088 return err;
2089}
2090
Patrick McHardy20a69342013-10-11 12:06:22 +02002091/*
2092 * Sets
2093 */
2094
2095static LIST_HEAD(nf_tables_set_ops);
2096
2097int nft_register_set(struct nft_set_ops *ops)
2098{
2099 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002100 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
Patrick McHardy20a69342013-10-11 12:06:22 +02002101 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2102 return 0;
2103}
2104EXPORT_SYMBOL_GPL(nft_register_set);
2105
2106void nft_unregister_set(struct nft_set_ops *ops)
2107{
2108 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002109 list_del_rcu(&ops->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002110 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2111}
2112EXPORT_SYMBOL_GPL(nft_unregister_set);
2113
Patrick McHardyc50b9602014-03-28 10:19:47 +00002114/*
2115 * Select a set implementation based on the data characteristics and the
2116 * given policy. The total memory use might not be known if no size is
2117 * given, in that case the amount of memory per element is used.
2118 */
2119static const struct nft_set_ops *
2120nft_select_set_ops(const struct nlattr * const nla[],
2121 const struct nft_set_desc *desc,
2122 enum nft_set_policies policy)
Patrick McHardy20a69342013-10-11 12:06:22 +02002123{
Patrick McHardyc50b9602014-03-28 10:19:47 +00002124 const struct nft_set_ops *ops, *bops;
2125 struct nft_set_estimate est, best;
Patrick McHardy20a69342013-10-11 12:06:22 +02002126 u32 features;
2127
2128#ifdef CONFIG_MODULES
2129 if (list_empty(&nf_tables_set_ops)) {
2130 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2131 request_module("nft-set");
2132 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2133 if (!list_empty(&nf_tables_set_ops))
2134 return ERR_PTR(-EAGAIN);
2135 }
2136#endif
2137 features = 0;
2138 if (nla[NFTA_SET_FLAGS] != NULL) {
2139 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2140 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
2141 }
2142
Patrick McHardyc50b9602014-03-28 10:19:47 +00002143 bops = NULL;
2144 best.size = ~0;
2145 best.class = ~0;
2146
Patrick McHardy20a69342013-10-11 12:06:22 +02002147 list_for_each_entry(ops, &nf_tables_set_ops, list) {
2148 if ((ops->features & features) != features)
2149 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002150 if (!ops->estimate(desc, features, &est))
2151 continue;
2152
2153 switch (policy) {
2154 case NFT_SET_POL_PERFORMANCE:
2155 if (est.class < best.class)
2156 break;
2157 if (est.class == best.class && est.size < best.size)
2158 break;
2159 continue;
2160 case NFT_SET_POL_MEMORY:
2161 if (est.size < best.size)
2162 break;
2163 if (est.size == best.size && est.class < best.class)
2164 break;
2165 continue;
2166 default:
2167 break;
2168 }
2169
Patrick McHardy20a69342013-10-11 12:06:22 +02002170 if (!try_module_get(ops->owner))
2171 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002172 if (bops != NULL)
2173 module_put(bops->owner);
2174
2175 bops = ops;
2176 best = est;
Patrick McHardy20a69342013-10-11 12:06:22 +02002177 }
2178
Patrick McHardyc50b9602014-03-28 10:19:47 +00002179 if (bops != NULL)
2180 return bops;
2181
Patrick McHardy20a69342013-10-11 12:06:22 +02002182 return ERR_PTR(-EOPNOTSUPP);
2183}
2184
2185static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2186 [NFTA_SET_TABLE] = { .type = NLA_STRING },
Pablo Neira Ayusoa9bdd832014-03-24 15:10:37 +01002187 [NFTA_SET_NAME] = { .type = NLA_STRING,
2188 .len = IFNAMSIZ - 1 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002189 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2190 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2191 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2192 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2193 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002194 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2195 [NFTA_SET_DESC] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002196 [NFTA_SET_ID] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002197};
2198
2199static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2200 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002201};
2202
2203static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2204 const struct sk_buff *skb,
2205 const struct nlmsghdr *nlh,
2206 const struct nlattr * const nla[])
2207{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002208 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002209 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002210 struct nft_af_info *afi = NULL;
2211 struct nft_table *table = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02002212
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002213 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2214 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2215 if (IS_ERR(afi))
2216 return PTR_ERR(afi);
2217 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002218
2219 if (nla[NFTA_SET_TABLE] != NULL) {
Patrick McHardyec2c9932014-02-05 15:03:35 +00002220 if (afi == NULL)
2221 return -EAFNOSUPPORT;
2222
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002223 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002224 if (IS_ERR(table))
2225 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002226 if (table->flags & NFT_TABLE_INACTIVE)
2227 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002228 }
2229
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002230 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002231 return 0;
2232}
2233
2234struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2235 const struct nlattr *nla)
2236{
2237 struct nft_set *set;
2238
2239 if (nla == NULL)
2240 return ERR_PTR(-EINVAL);
2241
2242 list_for_each_entry(set, &table->sets, list) {
2243 if (!nla_strcmp(nla, set->name))
2244 return set;
2245 }
2246 return ERR_PTR(-ENOENT);
2247}
2248
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002249struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2250 const struct nlattr *nla)
2251{
2252 struct nft_trans *trans;
2253 u32 id = ntohl(nla_get_be32(nla));
2254
2255 list_for_each_entry(trans, &net->nft.commit_list, list) {
2256 if (trans->msg_type == NFT_MSG_NEWSET &&
2257 id == nft_trans_set_id(trans))
2258 return nft_trans_set(trans);
2259 }
2260 return ERR_PTR(-ENOENT);
2261}
2262
Patrick McHardy20a69342013-10-11 12:06:22 +02002263static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2264 const char *name)
2265{
2266 const struct nft_set *i;
2267 const char *p;
2268 unsigned long *inuse;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002269 unsigned int n = 0, min = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002270
2271 p = strnchr(name, IFNAMSIZ, '%');
2272 if (p != NULL) {
2273 if (p[1] != 'd' || strchr(p + 2, '%'))
2274 return -EINVAL;
2275
2276 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2277 if (inuse == NULL)
2278 return -ENOMEM;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002279cont:
Patrick McHardy20a69342013-10-11 12:06:22 +02002280 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01002281 int tmp;
2282
2283 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02002284 continue;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002285 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02002286 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002287
Patrick McHardy60eb1892014-03-07 12:34:05 +01002288 set_bit(tmp - min, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02002289 }
2290
Patrick McHardy53b70282014-02-05 12:26:22 +01002291 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
Patrick McHardy60eb1892014-03-07 12:34:05 +01002292 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2293 min += BITS_PER_BYTE * PAGE_SIZE;
2294 memset(inuse, 0, PAGE_SIZE);
2295 goto cont;
2296 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002297 free_page((unsigned long)inuse);
2298 }
2299
Patrick McHardy60eb1892014-03-07 12:34:05 +01002300 snprintf(set->name, sizeof(set->name), name, min + n);
Patrick McHardy20a69342013-10-11 12:06:22 +02002301 list_for_each_entry(i, &ctx->table->sets, list) {
2302 if (!strcmp(set->name, i->name))
2303 return -ENFILE;
2304 }
2305 return 0;
2306}
2307
2308static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2309 const struct nft_set *set, u16 event, u16 flags)
2310{
2311 struct nfgenmsg *nfmsg;
2312 struct nlmsghdr *nlh;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002313 struct nlattr *desc;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002314 u32 portid = ctx->portid;
2315 u32 seq = ctx->seq;
Patrick McHardy20a69342013-10-11 12:06:22 +02002316
2317 event |= NFNL_SUBSYS_NFTABLES << 8;
2318 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2319 flags);
2320 if (nlh == NULL)
2321 goto nla_put_failure;
2322
2323 nfmsg = nlmsg_data(nlh);
2324 nfmsg->nfgen_family = ctx->afi->family;
2325 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02002326 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
Patrick McHardy20a69342013-10-11 12:06:22 +02002327
2328 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2329 goto nla_put_failure;
2330 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2331 goto nla_put_failure;
2332 if (set->flags != 0)
2333 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2334 goto nla_put_failure;
2335
2336 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2337 goto nla_put_failure;
2338 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2339 goto nla_put_failure;
2340 if (set->flags & NFT_SET_MAP) {
2341 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2342 goto nla_put_failure;
2343 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2344 goto nla_put_failure;
2345 }
2346
Patrick McHardyc50b9602014-03-28 10:19:47 +00002347 desc = nla_nest_start(skb, NFTA_SET_DESC);
2348 if (desc == NULL)
2349 goto nla_put_failure;
2350 if (set->size &&
2351 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2352 goto nla_put_failure;
2353 nla_nest_end(skb, desc);
2354
Patrick McHardy20a69342013-10-11 12:06:22 +02002355 return nlmsg_end(skb, nlh);
2356
2357nla_put_failure:
2358 nlmsg_trim(skb, nlh);
2359 return -1;
2360}
2361
2362static int nf_tables_set_notify(const struct nft_ctx *ctx,
2363 const struct nft_set *set,
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002364 int event, gfp_t gfp_flags)
Patrick McHardy20a69342013-10-11 12:06:22 +02002365{
2366 struct sk_buff *skb;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002367 u32 portid = ctx->portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002368 int err;
2369
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002370 if (!ctx->report &&
2371 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002372 return 0;
2373
2374 err = -ENOBUFS;
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002375 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
Patrick McHardy20a69342013-10-11 12:06:22 +02002376 if (skb == NULL)
2377 goto err;
2378
2379 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2380 if (err < 0) {
2381 kfree_skb(skb);
2382 goto err;
2383 }
2384
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002385 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002386 ctx->report, gfp_flags);
Patrick McHardy20a69342013-10-11 12:06:22 +02002387err:
2388 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002389 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002390 return err;
2391}
2392
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002393static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002394{
2395 const struct nft_set *set;
2396 unsigned int idx, s_idx = cb->args[0];
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002397 struct nft_af_info *afi;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002398 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2399 struct net *net = sock_net(skb->sk);
2400 int cur_family = cb->args[3];
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002401 struct nft_ctx *ctx = cb->data, ctx_set;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002402
2403 if (cb->args[1])
2404 return skb->len;
2405
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002406 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02002407 cb->seq = net->nft.base_seq;
2408
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002409 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002410 if (ctx->afi && ctx->afi != afi)
2411 continue;
2412
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002413 if (cur_family) {
2414 if (afi->family != cur_family)
2415 continue;
2416
2417 cur_family = 0;
2418 }
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002419 list_for_each_entry_rcu(table, &afi->tables, list) {
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002420 if (ctx->table && ctx->table != table)
2421 continue;
2422
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002423 if (cur_table) {
2424 if (cur_table != table)
2425 continue;
2426
2427 cur_table = NULL;
2428 }
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002429 idx = 0;
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002430 list_for_each_entry_rcu(set, &table->sets, list) {
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002431 if (idx < s_idx)
2432 goto cont;
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002433
2434 ctx_set = *ctx;
2435 ctx_set.table = table;
2436 ctx_set.afi = afi;
2437 if (nf_tables_fill_set(skb, &ctx_set, set,
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002438 NFT_MSG_NEWSET,
2439 NLM_F_MULTI) < 0) {
2440 cb->args[0] = idx;
2441 cb->args[2] = (unsigned long) table;
2442 cb->args[3] = afi->family;
2443 goto done;
2444 }
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02002445 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002446cont:
2447 idx++;
2448 }
2449 if (s_idx)
2450 s_idx = 0;
2451 }
2452 }
2453 cb->args[1] = 1;
2454done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002455 rcu_read_unlock();
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002456 return skb->len;
2457}
2458
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002459static int nf_tables_dump_sets_done(struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002460{
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002461 kfree(cb->data);
2462 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002463}
2464
2465static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2466 const struct nlmsghdr *nlh,
2467 const struct nlattr * const nla[])
2468{
2469 const struct nft_set *set;
2470 struct nft_ctx ctx;
2471 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002472 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002473 int err;
2474
2475 /* Verify existance before starting dump */
2476 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2477 if (err < 0)
2478 return err;
2479
2480 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2481 struct netlink_dump_control c = {
2482 .dump = nf_tables_dump_sets,
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002483 .done = nf_tables_dump_sets_done,
Patrick McHardy20a69342013-10-11 12:06:22 +02002484 };
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002485 struct nft_ctx *ctx_dump;
2486
2487 ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2488 if (ctx_dump == NULL)
2489 return -ENOMEM;
2490
2491 *ctx_dump = ctx;
2492 c.data = ctx_dump;
2493
Patrick McHardy20a69342013-10-11 12:06:22 +02002494 return netlink_dump_start(nlsk, skb, nlh, &c);
2495 }
2496
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002497 /* Only accept unspec with dump */
2498 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2499 return -EAFNOSUPPORT;
2500
Patrick McHardy20a69342013-10-11 12:06:22 +02002501 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2502 if (IS_ERR(set))
2503 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002504 if (set->flags & NFT_SET_INACTIVE)
2505 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002506
2507 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2508 if (skb2 == NULL)
2509 return -ENOMEM;
2510
2511 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2512 if (err < 0)
2513 goto err;
2514
2515 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2516
2517err:
2518 kfree_skb(skb2);
2519 return err;
2520}
2521
Patrick McHardyc50b9602014-03-28 10:19:47 +00002522static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2523 struct nft_set_desc *desc,
2524 const struct nlattr *nla)
2525{
2526 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2527 int err;
2528
2529 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2530 if (err < 0)
2531 return err;
2532
2533 if (da[NFTA_SET_DESC_SIZE] != NULL)
2534 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2535
2536 return 0;
2537}
2538
Patrick McHardy20a69342013-10-11 12:06:22 +02002539static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2540 const struct nlmsghdr *nlh,
2541 const struct nlattr * const nla[])
2542{
2543 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2544 const struct nft_set_ops *ops;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002545 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002546 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002547 struct nft_table *table;
2548 struct nft_set *set;
2549 struct nft_ctx ctx;
2550 char name[IFNAMSIZ];
2551 unsigned int size;
2552 bool create;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002553 u32 ktype, dtype, flags, policy;
2554 struct nft_set_desc desc;
Patrick McHardy20a69342013-10-11 12:06:22 +02002555 int err;
2556
2557 if (nla[NFTA_SET_TABLE] == NULL ||
2558 nla[NFTA_SET_NAME] == NULL ||
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002559 nla[NFTA_SET_KEY_LEN] == NULL ||
2560 nla[NFTA_SET_ID] == NULL)
Patrick McHardy20a69342013-10-11 12:06:22 +02002561 return -EINVAL;
2562
Patrick McHardyc50b9602014-03-28 10:19:47 +00002563 memset(&desc, 0, sizeof(desc));
2564
Patrick McHardy20a69342013-10-11 12:06:22 +02002565 ktype = NFT_DATA_VALUE;
2566 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2567 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2568 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2569 return -EINVAL;
2570 }
2571
Patrick McHardyc50b9602014-03-28 10:19:47 +00002572 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2573 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002574 return -EINVAL;
2575
2576 flags = 0;
2577 if (nla[NFTA_SET_FLAGS] != NULL) {
2578 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2579 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2580 NFT_SET_INTERVAL | NFT_SET_MAP))
2581 return -EINVAL;
2582 }
2583
2584 dtype = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002585 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2586 if (!(flags & NFT_SET_MAP))
2587 return -EINVAL;
2588
2589 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2590 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2591 dtype != NFT_DATA_VERDICT)
2592 return -EINVAL;
2593
2594 if (dtype != NFT_DATA_VERDICT) {
2595 if (nla[NFTA_SET_DATA_LEN] == NULL)
2596 return -EINVAL;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002597 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2598 if (desc.dlen == 0 ||
2599 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002600 return -EINVAL;
2601 } else
Patrick McHardyc50b9602014-03-28 10:19:47 +00002602 desc.dlen = sizeof(struct nft_data);
Patrick McHardy20a69342013-10-11 12:06:22 +02002603 } else if (flags & NFT_SET_MAP)
2604 return -EINVAL;
2605
Patrick McHardyc50b9602014-03-28 10:19:47 +00002606 policy = NFT_SET_POL_PERFORMANCE;
2607 if (nla[NFTA_SET_POLICY] != NULL)
2608 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2609
2610 if (nla[NFTA_SET_DESC] != NULL) {
2611 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2612 if (err < 0)
2613 return err;
2614 }
2615
Patrick McHardy20a69342013-10-11 12:06:22 +02002616 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2617
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002618 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002619 if (IS_ERR(afi))
2620 return PTR_ERR(afi);
2621
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002622 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002623 if (IS_ERR(table))
2624 return PTR_ERR(table);
2625
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002626 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002627
2628 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2629 if (IS_ERR(set)) {
2630 if (PTR_ERR(set) != -ENOENT)
2631 return PTR_ERR(set);
2632 set = NULL;
2633 }
2634
2635 if (set != NULL) {
2636 if (nlh->nlmsg_flags & NLM_F_EXCL)
2637 return -EEXIST;
2638 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2639 return -EOPNOTSUPP;
2640 return 0;
2641 }
2642
2643 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2644 return -ENOENT;
2645
Patrick McHardyc50b9602014-03-28 10:19:47 +00002646 ops = nft_select_set_ops(nla, &desc, policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002647 if (IS_ERR(ops))
2648 return PTR_ERR(ops);
2649
2650 size = 0;
2651 if (ops->privsize != NULL)
2652 size = ops->privsize(nla);
2653
2654 err = -ENOMEM;
2655 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2656 if (set == NULL)
2657 goto err1;
2658
2659 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2660 err = nf_tables_set_alloc_name(&ctx, set, name);
2661 if (err < 0)
2662 goto err2;
2663
2664 INIT_LIST_HEAD(&set->bindings);
2665 set->ops = ops;
2666 set->ktype = ktype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002667 set->klen = desc.klen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002668 set->dtype = dtype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002669 set->dlen = desc.dlen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002670 set->flags = flags;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002671 set->size = desc.size;
Patrick McHardy20a69342013-10-11 12:06:22 +02002672
Patrick McHardyc50b9602014-03-28 10:19:47 +00002673 err = ops->init(set, &desc, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002674 if (err < 0)
2675 goto err2;
2676
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002677 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002678 if (err < 0)
2679 goto err2;
2680
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002681 list_add_tail_rcu(&set->list, &table->sets);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02002682 table->use++;
Patrick McHardy20a69342013-10-11 12:06:22 +02002683 return 0;
2684
2685err2:
2686 kfree(set);
2687err1:
2688 module_put(ops->owner);
2689 return err;
2690}
2691
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002692static void nft_set_destroy(struct nft_set *set)
2693{
2694 set->ops->destroy(set);
2695 module_put(set->ops->owner);
2696 kfree(set);
2697}
2698
Patrick McHardy20a69342013-10-11 12:06:22 +02002699static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2700{
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002701 list_del_rcu(&set->list);
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002702 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002703 nft_set_destroy(set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002704}
2705
2706static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2707 const struct nlmsghdr *nlh,
2708 const struct nlattr * const nla[])
2709{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002710 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002711 struct nft_set *set;
2712 struct nft_ctx ctx;
2713 int err;
2714
Patrick McHardyec2c9932014-02-05 15:03:35 +00002715 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2716 return -EAFNOSUPPORT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002717 if (nla[NFTA_SET_TABLE] == NULL)
2718 return -EINVAL;
2719
2720 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2721 if (err < 0)
2722 return err;
2723
2724 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2725 if (IS_ERR(set))
2726 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002727 if (set->flags & NFT_SET_INACTIVE)
2728 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002729 if (!list_empty(&set->bindings))
2730 return -EBUSY;
2731
Arturo Borreroee01d542014-09-02 16:42:25 +02002732 return nft_delset(&ctx, set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002733}
2734
2735static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2736 const struct nft_set *set,
2737 const struct nft_set_iter *iter,
2738 const struct nft_set_elem *elem)
2739{
2740 enum nft_registers dreg;
2741
2742 dreg = nft_type_to_reg(set->dtype);
Pablo Neira Ayuso2ee0d3c2013-12-28 00:59:38 +01002743 return nft_validate_data_load(ctx, dreg, &elem->data,
2744 set->dtype == NFT_DATA_VERDICT ?
2745 NFT_DATA_VERDICT : NFT_DATA_VALUE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002746}
2747
2748int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2749 struct nft_set_binding *binding)
2750{
2751 struct nft_set_binding *i;
2752 struct nft_set_iter iter;
2753
2754 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2755 return -EBUSY;
2756
2757 if (set->flags & NFT_SET_MAP) {
2758 /* If the set is already bound to the same chain all
2759 * jumps are already validated for that chain.
2760 */
2761 list_for_each_entry(i, &set->bindings, list) {
2762 if (i->chain == binding->chain)
2763 goto bind;
2764 }
2765
2766 iter.skip = 0;
2767 iter.count = 0;
2768 iter.err = 0;
2769 iter.fn = nf_tables_bind_check_setelem;
2770
2771 set->ops->walk(ctx, set, &iter);
2772 if (iter.err < 0) {
2773 /* Destroy anonymous sets if binding fails */
2774 if (set->flags & NFT_SET_ANONYMOUS)
2775 nf_tables_set_destroy(ctx, set);
2776
2777 return iter.err;
2778 }
2779 }
2780bind:
2781 binding->chain = ctx->chain;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002782 list_add_tail_rcu(&binding->list, &set->bindings);
Patrick McHardy20a69342013-10-11 12:06:22 +02002783 return 0;
2784}
2785
2786void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2787 struct nft_set_binding *binding)
2788{
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002789 list_del_rcu(&binding->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002790
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002791 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2792 !(set->flags & NFT_SET_INACTIVE))
Patrick McHardy20a69342013-10-11 12:06:22 +02002793 nf_tables_set_destroy(ctx, set);
2794}
2795
2796/*
2797 * Set elements
2798 */
2799
2800static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2801 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2802 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2803 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2804};
2805
2806static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2807 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2808 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2809 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002810 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002811};
2812
2813static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2814 const struct sk_buff *skb,
2815 const struct nlmsghdr *nlh,
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002816 const struct nlattr * const nla[],
2817 bool trans)
Patrick McHardy20a69342013-10-11 12:06:22 +02002818{
2819 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002820 struct nft_af_info *afi;
2821 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002822 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002823
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002824 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002825 if (IS_ERR(afi))
2826 return PTR_ERR(afi);
2827
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002828 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002829 if (IS_ERR(table))
2830 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002831 if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2832 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002833
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002834 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002835 return 0;
2836}
2837
2838static int nf_tables_fill_setelem(struct sk_buff *skb,
2839 const struct nft_set *set,
2840 const struct nft_set_elem *elem)
2841{
2842 unsigned char *b = skb_tail_pointer(skb);
2843 struct nlattr *nest;
2844
2845 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2846 if (nest == NULL)
2847 goto nla_put_failure;
2848
2849 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2850 set->klen) < 0)
2851 goto nla_put_failure;
2852
2853 if (set->flags & NFT_SET_MAP &&
2854 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2855 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2856 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2857 set->dlen) < 0)
2858 goto nla_put_failure;
2859
2860 if (elem->flags != 0)
2861 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2862 goto nla_put_failure;
2863
2864 nla_nest_end(skb, nest);
2865 return 0;
2866
2867nla_put_failure:
2868 nlmsg_trim(skb, b);
2869 return -EMSGSIZE;
2870}
2871
2872struct nft_set_dump_args {
2873 const struct netlink_callback *cb;
2874 struct nft_set_iter iter;
2875 struct sk_buff *skb;
2876};
2877
2878static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2879 const struct nft_set *set,
2880 const struct nft_set_iter *iter,
2881 const struct nft_set_elem *elem)
2882{
2883 struct nft_set_dump_args *args;
2884
2885 args = container_of(iter, struct nft_set_dump_args, iter);
2886 return nf_tables_fill_setelem(args->skb, set, elem);
2887}
2888
2889static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2890{
2891 const struct nft_set *set;
2892 struct nft_set_dump_args args;
2893 struct nft_ctx ctx;
2894 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2895 struct nfgenmsg *nfmsg;
2896 struct nlmsghdr *nlh;
2897 struct nlattr *nest;
2898 u32 portid, seq;
2899 int event, err;
2900
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01002901 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2902 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002903 if (err < 0)
2904 return err;
2905
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002906 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2907 false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002908 if (err < 0)
2909 return err;
2910
2911 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2912 if (IS_ERR(set))
2913 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002914 if (set->flags & NFT_SET_INACTIVE)
2915 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002916
2917 event = NFT_MSG_NEWSETELEM;
2918 event |= NFNL_SUBSYS_NFTABLES << 8;
2919 portid = NETLINK_CB(cb->skb).portid;
2920 seq = cb->nlh->nlmsg_seq;
2921
2922 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2923 NLM_F_MULTI);
2924 if (nlh == NULL)
2925 goto nla_put_failure;
2926
2927 nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso6403d962014-06-11 19:05:28 +02002928 nfmsg->nfgen_family = ctx.afi->family;
Patrick McHardy20a69342013-10-11 12:06:22 +02002929 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02002930 nfmsg->res_id = htons(ctx.net->nft.base_seq & 0xffff);
Patrick McHardy20a69342013-10-11 12:06:22 +02002931
2932 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2933 goto nla_put_failure;
2934 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2935 goto nla_put_failure;
2936
2937 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2938 if (nest == NULL)
2939 goto nla_put_failure;
2940
2941 args.cb = cb;
2942 args.skb = skb;
2943 args.iter.skip = cb->args[0];
2944 args.iter.count = 0;
2945 args.iter.err = 0;
2946 args.iter.fn = nf_tables_dump_setelem;
2947 set->ops->walk(&ctx, set, &args.iter);
2948
2949 nla_nest_end(skb, nest);
2950 nlmsg_end(skb, nlh);
2951
2952 if (args.iter.err && args.iter.err != -EMSGSIZE)
2953 return args.iter.err;
2954 if (args.iter.count == cb->args[0])
2955 return 0;
2956
2957 cb->args[0] = args.iter.count;
2958 return skb->len;
2959
2960nla_put_failure:
2961 return -ENOSPC;
2962}
2963
2964static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2965 const struct nlmsghdr *nlh,
2966 const struct nlattr * const nla[])
2967{
2968 const struct nft_set *set;
2969 struct nft_ctx ctx;
2970 int err;
2971
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002972 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002973 if (err < 0)
2974 return err;
2975
2976 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2977 if (IS_ERR(set))
2978 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002979 if (set->flags & NFT_SET_INACTIVE)
2980 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002981
2982 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2983 struct netlink_dump_control c = {
2984 .dump = nf_tables_dump_set,
2985 };
2986 return netlink_dump_start(nlsk, skb, nlh, &c);
2987 }
2988 return -EOPNOTSUPP;
2989}
2990
Arturo Borrerod60ce622014-04-01 14:06:07 +02002991static int nf_tables_fill_setelem_info(struct sk_buff *skb,
2992 const struct nft_ctx *ctx, u32 seq,
2993 u32 portid, int event, u16 flags,
2994 const struct nft_set *set,
2995 const struct nft_set_elem *elem)
2996{
2997 struct nfgenmsg *nfmsg;
2998 struct nlmsghdr *nlh;
2999 struct nlattr *nest;
3000 int err;
3001
3002 event |= NFNL_SUBSYS_NFTABLES << 8;
3003 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3004 flags);
3005 if (nlh == NULL)
3006 goto nla_put_failure;
3007
3008 nfmsg = nlmsg_data(nlh);
3009 nfmsg->nfgen_family = ctx->afi->family;
3010 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003011 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
Arturo Borrerod60ce622014-04-01 14:06:07 +02003012
3013 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3014 goto nla_put_failure;
3015 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3016 goto nla_put_failure;
3017
3018 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3019 if (nest == NULL)
3020 goto nla_put_failure;
3021
3022 err = nf_tables_fill_setelem(skb, set, elem);
3023 if (err < 0)
3024 goto nla_put_failure;
3025
3026 nla_nest_end(skb, nest);
3027
3028 return nlmsg_end(skb, nlh);
3029
3030nla_put_failure:
3031 nlmsg_trim(skb, nlh);
3032 return -1;
3033}
3034
3035static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
3036 const struct nft_set *set,
3037 const struct nft_set_elem *elem,
3038 int event, u16 flags)
3039{
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003040 struct net *net = ctx->net;
3041 u32 portid = ctx->portid;
Arturo Borrerod60ce622014-04-01 14:06:07 +02003042 struct sk_buff *skb;
3043 int err;
3044
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003045 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
Arturo Borrerod60ce622014-04-01 14:06:07 +02003046 return 0;
3047
3048 err = -ENOBUFS;
3049 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3050 if (skb == NULL)
3051 goto err;
3052
3053 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3054 set, elem);
3055 if (err < 0) {
3056 kfree_skb(skb);
3057 goto err;
3058 }
3059
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003060 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
Arturo Borrerod60ce622014-04-01 14:06:07 +02003061 GFP_KERNEL);
3062err:
3063 if (err < 0)
3064 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3065 return err;
3066}
3067
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003068static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3069 int msg_type,
3070 struct nft_set *set)
3071{
3072 struct nft_trans *trans;
3073
3074 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3075 if (trans == NULL)
3076 return NULL;
3077
3078 nft_trans_elem_set(trans) = set;
3079 return trans;
3080}
3081
3082static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +02003083 const struct nlattr *attr)
3084{
3085 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3086 struct nft_data_desc d1, d2;
3087 struct nft_set_elem elem;
3088 struct nft_set_binding *binding;
3089 enum nft_registers dreg;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003090 struct nft_trans *trans;
Patrick McHardy20a69342013-10-11 12:06:22 +02003091 int err;
3092
Patrick McHardyc50b9602014-03-28 10:19:47 +00003093 if (set->size && set->nelems == set->size)
3094 return -ENFILE;
3095
Patrick McHardy20a69342013-10-11 12:06:22 +02003096 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3097 nft_set_elem_policy);
3098 if (err < 0)
3099 return err;
3100
3101 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3102 return -EINVAL;
3103
3104 elem.flags = 0;
3105 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3106 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3107 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
3108 return -EINVAL;
3109 }
3110
3111 if (set->flags & NFT_SET_MAP) {
3112 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3113 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
3114 return -EINVAL;
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01003115 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3116 elem.flags & NFT_SET_ELEM_INTERVAL_END)
3117 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003118 } else {
3119 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3120 return -EINVAL;
3121 }
3122
3123 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3124 if (err < 0)
3125 goto err1;
3126 err = -EINVAL;
3127 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3128 goto err2;
3129
3130 err = -EEXIST;
3131 if (set->ops->get(set, &elem) == 0)
3132 goto err2;
3133
3134 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3135 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
3136 if (err < 0)
3137 goto err2;
3138
3139 err = -EINVAL;
3140 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3141 goto err3;
3142
3143 dreg = nft_type_to_reg(set->dtype);
3144 list_for_each_entry(binding, &set->bindings, list) {
3145 struct nft_ctx bind_ctx = {
3146 .afi = ctx->afi,
3147 .table = ctx->table,
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02003148 .chain = (struct nft_chain *)binding->chain,
Patrick McHardy20a69342013-10-11 12:06:22 +02003149 };
3150
3151 err = nft_validate_data_load(&bind_ctx, dreg,
3152 &elem.data, d2.type);
3153 if (err < 0)
3154 goto err3;
3155 }
3156 }
3157
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003158 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3159 if (trans == NULL)
Patrick McHardy20a69342013-10-11 12:06:22 +02003160 goto err3;
3161
Patrick McHardy20a69342013-10-11 12:06:22 +02003162 err = set->ops->insert(set, &elem);
3163 if (err < 0)
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003164 goto err4;
Patrick McHardy20a69342013-10-11 12:06:22 +02003165
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003166 nft_trans_elem(trans) = elem;
Pablo Neira Ayuso46bbafc2014-05-22 12:36:03 +02003167 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Patrick McHardy20a69342013-10-11 12:06:22 +02003168 return 0;
3169
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003170err4:
3171 kfree(trans);
Patrick McHardy20a69342013-10-11 12:06:22 +02003172err3:
3173 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3174 nft_data_uninit(&elem.data, d2.type);
3175err2:
3176 nft_data_uninit(&elem.key, d1.type);
3177err1:
3178 return err;
3179}
3180
3181static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3182 const struct nlmsghdr *nlh,
3183 const struct nlattr * const nla[])
3184{
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003185 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02003186 const struct nlattr *attr;
3187 struct nft_set *set;
3188 struct nft_ctx ctx;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003189 int rem, err = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003190
Pablo Neira Ayuso7d5570c2014-07-25 13:15:36 +02003191 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3192 return -EINVAL;
3193
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003194 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
Patrick McHardy20a69342013-10-11 12:06:22 +02003195 if (err < 0)
3196 return err;
3197
3198 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003199 if (IS_ERR(set)) {
3200 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3201 set = nf_tables_set_lookup_byid(net,
3202 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3203 }
3204 if (IS_ERR(set))
3205 return PTR_ERR(set);
3206 }
3207
Patrick McHardy20a69342013-10-11 12:06:22 +02003208 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3209 return -EBUSY;
3210
3211 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3212 err = nft_add_set_elem(&ctx, set, attr);
3213 if (err < 0)
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003214 break;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003215
3216 set->nelems++;
Patrick McHardy20a69342013-10-11 12:06:22 +02003217 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003218 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +02003219}
3220
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003221static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +02003222 const struct nlattr *attr)
3223{
3224 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3225 struct nft_data_desc desc;
3226 struct nft_set_elem elem;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003227 struct nft_trans *trans;
Patrick McHardy20a69342013-10-11 12:06:22 +02003228 int err;
3229
3230 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3231 nft_set_elem_policy);
3232 if (err < 0)
3233 goto err1;
3234
3235 err = -EINVAL;
3236 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3237 goto err1;
3238
3239 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3240 if (err < 0)
3241 goto err1;
3242
3243 err = -EINVAL;
3244 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3245 goto err2;
3246
3247 err = set->ops->get(set, &elem);
3248 if (err < 0)
3249 goto err2;
3250
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003251 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
Julia Lawall609ccf02014-08-07 14:49:08 +02003252 if (trans == NULL) {
3253 err = -ENOMEM;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003254 goto err2;
Julia Lawall609ccf02014-08-07 14:49:08 +02003255 }
Patrick McHardy20a69342013-10-11 12:06:22 +02003256
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003257 nft_trans_elem(trans) = elem;
Pablo Neira Ayuso46bbafc2014-05-22 12:36:03 +02003258 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Thomas Graf0dc13622014-08-01 17:25:38 +02003259 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003260err2:
3261 nft_data_uninit(&elem.key, desc.type);
3262err1:
3263 return err;
3264}
3265
3266static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3267 const struct nlmsghdr *nlh,
3268 const struct nlattr * const nla[])
3269{
3270 const struct nlattr *attr;
3271 struct nft_set *set;
3272 struct nft_ctx ctx;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003273 int rem, err = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003274
Pablo Neira Ayuso7d5570c2014-07-25 13:15:36 +02003275 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3276 return -EINVAL;
3277
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003278 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02003279 if (err < 0)
3280 return err;
3281
3282 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3283 if (IS_ERR(set))
3284 return PTR_ERR(set);
3285 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3286 return -EBUSY;
3287
3288 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3289 err = nft_del_setelem(&ctx, set, attr);
3290 if (err < 0)
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003291 break;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003292
3293 set->nelems--;
Patrick McHardy20a69342013-10-11 12:06:22 +02003294 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003295 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +02003296}
3297
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003298static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
3299 u32 portid, u32 seq)
3300{
3301 struct nlmsghdr *nlh;
3302 struct nfgenmsg *nfmsg;
3303 int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN;
3304
3305 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
3306 if (nlh == NULL)
3307 goto nla_put_failure;
3308
3309 nfmsg = nlmsg_data(nlh);
3310 nfmsg->nfgen_family = AF_UNSPEC;
3311 nfmsg->version = NFNETLINK_V0;
3312 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
3313
3314 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)))
3315 goto nla_put_failure;
3316
3317 return nlmsg_end(skb, nlh);
3318
3319nla_put_failure:
3320 nlmsg_trim(skb, nlh);
3321 return -EMSGSIZE;
3322}
3323
3324static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
3325{
3326 struct nlmsghdr *nlh = nlmsg_hdr(skb);
3327 struct sk_buff *skb2;
3328 int err;
3329
3330 if (nlmsg_report(nlh) &&
3331 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3332 return 0;
3333
3334 err = -ENOBUFS;
3335 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3336 if (skb2 == NULL)
3337 goto err;
3338
3339 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3340 nlh->nlmsg_seq);
3341 if (err < 0) {
3342 kfree_skb(skb2);
3343 goto err;
3344 }
3345
3346 err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
3347 NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
3348err:
3349 if (err < 0) {
3350 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
3351 err);
3352 }
3353 return err;
3354}
3355
3356static int nf_tables_getgen(struct sock *nlsk, struct sk_buff *skb,
3357 const struct nlmsghdr *nlh,
3358 const struct nlattr * const nla[])
3359{
3360 struct net *net = sock_net(skb->sk);
3361 struct sk_buff *skb2;
3362 int err;
3363
3364 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3365 if (skb2 == NULL)
3366 return -ENOMEM;
3367
3368 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3369 nlh->nlmsg_seq);
3370 if (err < 0)
3371 goto err;
3372
3373 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3374err:
3375 kfree_skb(skb2);
3376 return err;
3377}
3378
Patrick McHardy96518512013-10-14 11:00:02 +02003379static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3380 [NFT_MSG_NEWTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003381 .call_batch = nf_tables_newtable,
Patrick McHardy96518512013-10-14 11:00:02 +02003382 .attr_count = NFTA_TABLE_MAX,
3383 .policy = nft_table_policy,
3384 },
3385 [NFT_MSG_GETTABLE] = {
3386 .call = nf_tables_gettable,
3387 .attr_count = NFTA_TABLE_MAX,
3388 .policy = nft_table_policy,
3389 },
3390 [NFT_MSG_DELTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003391 .call_batch = nf_tables_deltable,
Patrick McHardy96518512013-10-14 11:00:02 +02003392 .attr_count = NFTA_TABLE_MAX,
3393 .policy = nft_table_policy,
3394 },
3395 [NFT_MSG_NEWCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003396 .call_batch = nf_tables_newchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003397 .attr_count = NFTA_CHAIN_MAX,
3398 .policy = nft_chain_policy,
3399 },
3400 [NFT_MSG_GETCHAIN] = {
3401 .call = nf_tables_getchain,
3402 .attr_count = NFTA_CHAIN_MAX,
3403 .policy = nft_chain_policy,
3404 },
3405 [NFT_MSG_DELCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003406 .call_batch = nf_tables_delchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003407 .attr_count = NFTA_CHAIN_MAX,
3408 .policy = nft_chain_policy,
3409 },
3410 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003411 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003412 .attr_count = NFTA_RULE_MAX,
3413 .policy = nft_rule_policy,
3414 },
3415 [NFT_MSG_GETRULE] = {
3416 .call = nf_tables_getrule,
3417 .attr_count = NFTA_RULE_MAX,
3418 .policy = nft_rule_policy,
3419 },
3420 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003421 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003422 .attr_count = NFTA_RULE_MAX,
3423 .policy = nft_rule_policy,
3424 },
Patrick McHardy20a69342013-10-11 12:06:22 +02003425 [NFT_MSG_NEWSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003426 .call_batch = nf_tables_newset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003427 .attr_count = NFTA_SET_MAX,
3428 .policy = nft_set_policy,
3429 },
3430 [NFT_MSG_GETSET] = {
3431 .call = nf_tables_getset,
3432 .attr_count = NFTA_SET_MAX,
3433 .policy = nft_set_policy,
3434 },
3435 [NFT_MSG_DELSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003436 .call_batch = nf_tables_delset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003437 .attr_count = NFTA_SET_MAX,
3438 .policy = nft_set_policy,
3439 },
3440 [NFT_MSG_NEWSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003441 .call_batch = nf_tables_newsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003442 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3443 .policy = nft_set_elem_list_policy,
3444 },
3445 [NFT_MSG_GETSETELEM] = {
3446 .call = nf_tables_getsetelem,
3447 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3448 .policy = nft_set_elem_list_policy,
3449 },
3450 [NFT_MSG_DELSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003451 .call_batch = nf_tables_delsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003452 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3453 .policy = nft_set_elem_list_policy,
3454 },
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003455 [NFT_MSG_GETGEN] = {
3456 .call = nf_tables_getgen,
3457 },
Patrick McHardy96518512013-10-14 11:00:02 +02003458};
3459
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003460static void nft_chain_commit_update(struct nft_trans *trans)
3461{
3462 struct nft_base_chain *basechain;
3463
3464 if (nft_trans_chain_name(trans)[0])
3465 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3466
3467 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3468 return;
3469
3470 basechain = nft_base_chain(trans->ctx.chain);
3471 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3472
3473 switch (nft_trans_chain_policy(trans)) {
3474 case NF_DROP:
3475 case NF_ACCEPT:
3476 basechain->policy = nft_trans_chain_policy(trans);
3477 break;
3478 }
3479}
3480
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003481/* Schedule objects for release via rcu to make sure no packets are accesing
3482 * removed rules.
3483 */
3484static void nf_tables_commit_release_rcu(struct rcu_head *rt)
3485{
3486 struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3487
3488 switch (trans->msg_type) {
3489 case NFT_MSG_DELTABLE:
3490 nf_tables_table_destroy(&trans->ctx);
3491 break;
3492 case NFT_MSG_DELCHAIN:
3493 nf_tables_chain_destroy(trans->ctx.chain);
3494 break;
3495 case NFT_MSG_DELRULE:
3496 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3497 break;
3498 case NFT_MSG_DELSET:
3499 nft_set_destroy(nft_trans_set(trans));
3500 break;
3501 }
3502 kfree(trans);
3503}
3504
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003505static int nf_tables_commit(struct sk_buff *skb)
3506{
3507 struct net *net = sock_net(skb->sk);
3508 struct nft_trans *trans, *next;
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003509 struct nft_trans_elem *te;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003510
3511 /* Bump generation counter, invalidate any dump in progress */
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02003512 while (++net->nft.base_seq == 0);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003513
3514 /* A new generation has just started */
3515 net->nft.gencursor = gencursor_next(net);
3516
3517 /* Make sure all packets have left the previous generation before
3518 * purging old rules.
3519 */
3520 synchronize_rcu();
3521
3522 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003523 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003524 case NFT_MSG_NEWTABLE:
3525 if (nft_trans_table_update(trans)) {
3526 if (!nft_trans_table_enable(trans)) {
3527 nf_tables_table_disable(trans->ctx.afi,
3528 trans->ctx.table);
3529 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3530 }
3531 } else {
3532 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3533 }
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003534 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003535 nft_trans_destroy(trans);
3536 break;
3537 case NFT_MSG_DELTABLE:
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003538 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003539 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003540 case NFT_MSG_NEWCHAIN:
3541 if (nft_trans_chain_update(trans))
3542 nft_chain_commit_update(trans);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003543 else
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003544 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003545
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003546 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003547 nft_trans_destroy(trans);
3548 break;
3549 case NFT_MSG_DELCHAIN:
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003550 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
Arturo Borreroc5598792014-09-02 16:42:23 +02003551 nf_tables_unregister_hooks(trans->ctx.table,
3552 trans->ctx.chain,
3553 trans->ctx.afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003554 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003555 case NFT_MSG_NEWRULE:
3556 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003557 nf_tables_rule_notify(&trans->ctx,
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003558 nft_trans_rule(trans),
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003559 NFT_MSG_NEWRULE);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003560 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003561 break;
3562 case NFT_MSG_DELRULE:
3563 list_del_rcu(&nft_trans_rule(trans)->list);
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003564 nf_tables_rule_notify(&trans->ctx,
3565 nft_trans_rule(trans),
3566 NFT_MSG_DELRULE);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003567 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003568 case NFT_MSG_NEWSET:
3569 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003570 /* This avoids hitting -EBUSY when deleting the table
3571 * from the transaction.
3572 */
3573 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3574 !list_empty(&nft_trans_set(trans)->bindings))
3575 trans->ctx.table->use--;
3576
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003577 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02003578 NFT_MSG_NEWSET, GFP_KERNEL);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003579 nft_trans_destroy(trans);
3580 break;
3581 case NFT_MSG_DELSET:
3582 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02003583 NFT_MSG_DELSET, GFP_KERNEL);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003584 break;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003585 case NFT_MSG_NEWSETELEM:
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003586 nf_tables_setelem_notify(&trans->ctx,
3587 nft_trans_elem_set(trans),
3588 &nft_trans_elem(trans),
3589 NFT_MSG_NEWSETELEM, 0);
3590 nft_trans_destroy(trans);
3591 break;
3592 case NFT_MSG_DELSETELEM:
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003593 te = (struct nft_trans_elem *)trans->data;
3594 nf_tables_setelem_notify(&trans->ctx, te->set,
3595 &te->elem,
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003596 NFT_MSG_DELSETELEM, 0);
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003597 te->set->ops->get(te->set, &te->elem);
3598 te->set->ops->remove(te->set, &te->elem);
3599 nft_data_uninit(&te->elem.key, NFT_DATA_VALUE);
3600 if (te->elem.flags & NFT_SET_MAP) {
3601 nft_data_uninit(&te->elem.data,
3602 te->set->dtype);
3603 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003604 nft_trans_destroy(trans);
3605 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003606 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003607 }
3608
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003609 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003610 list_del(&trans->list);
3611 trans->ctx.nla = NULL;
3612 call_rcu(&trans->rcu_head, nf_tables_commit_release_rcu);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003613 }
3614
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003615 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
3616
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003617 return 0;
3618}
3619
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003620/* Schedule objects for release via rcu to make sure no packets are accesing
3621 * aborted rules.
3622 */
3623static void nf_tables_abort_release_rcu(struct rcu_head *rt)
3624{
3625 struct nft_trans *trans = container_of(rt, struct nft_trans, rcu_head);
3626
3627 switch (trans->msg_type) {
3628 case NFT_MSG_NEWTABLE:
3629 nf_tables_table_destroy(&trans->ctx);
3630 break;
3631 case NFT_MSG_NEWCHAIN:
3632 nf_tables_chain_destroy(trans->ctx.chain);
3633 break;
3634 case NFT_MSG_NEWRULE:
3635 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3636 break;
3637 case NFT_MSG_NEWSET:
3638 nft_set_destroy(nft_trans_set(trans));
3639 break;
3640 }
3641 kfree(trans);
3642}
3643
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003644static int nf_tables_abort(struct sk_buff *skb)
3645{
3646 struct net *net = sock_net(skb->sk);
3647 struct nft_trans *trans, *next;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003648 struct nft_set *set;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003649
3650 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003651 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003652 case NFT_MSG_NEWTABLE:
3653 if (nft_trans_table_update(trans)) {
3654 if (nft_trans_table_enable(trans)) {
3655 nf_tables_table_disable(trans->ctx.afi,
3656 trans->ctx.table);
3657 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3658 }
3659 nft_trans_destroy(trans);
3660 } else {
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003661 list_del_rcu(&trans->ctx.table->list);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003662 }
3663 break;
3664 case NFT_MSG_DELTABLE:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003665 list_add_tail_rcu(&trans->ctx.table->list,
3666 &trans->ctx.afi->tables);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003667 nft_trans_destroy(trans);
3668 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003669 case NFT_MSG_NEWCHAIN:
3670 if (nft_trans_chain_update(trans)) {
3671 if (nft_trans_chain_stats(trans))
3672 free_percpu(nft_trans_chain_stats(trans));
3673
3674 nft_trans_destroy(trans);
3675 } else {
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003676 trans->ctx.table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003677 list_del_rcu(&trans->ctx.chain->list);
Arturo Borreroc5598792014-09-02 16:42:23 +02003678 nf_tables_unregister_hooks(trans->ctx.table,
3679 trans->ctx.chain,
3680 trans->ctx.afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003681 }
3682 break;
3683 case NFT_MSG_DELCHAIN:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003684 trans->ctx.table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003685 list_add_tail_rcu(&trans->ctx.chain->list,
3686 &trans->ctx.table->chains);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003687 nft_trans_destroy(trans);
3688 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003689 case NFT_MSG_NEWRULE:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003690 trans->ctx.chain->use--;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003691 list_del_rcu(&nft_trans_rule(trans)->list);
3692 break;
3693 case NFT_MSG_DELRULE:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003694 trans->ctx.chain->use++;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003695 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003696 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003697 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003698 case NFT_MSG_NEWSET:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003699 trans->ctx.table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003700 list_del_rcu(&nft_trans_set(trans)->list);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003701 break;
3702 case NFT_MSG_DELSET:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003703 trans->ctx.table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003704 list_add_tail_rcu(&nft_trans_set(trans)->list,
3705 &trans->ctx.table->sets);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003706 nft_trans_destroy(trans);
3707 break;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003708 case NFT_MSG_NEWSETELEM:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003709 nft_trans_elem_set(trans)->nelems--;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003710 set = nft_trans_elem_set(trans);
3711 set->ops->get(set, &nft_trans_elem(trans));
3712 set->ops->remove(set, &nft_trans_elem(trans));
3713 nft_trans_destroy(trans);
3714 break;
3715 case NFT_MSG_DELSETELEM:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003716 nft_trans_elem_set(trans)->nelems++;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003717 nft_trans_destroy(trans);
3718 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003719 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003720 }
3721
Pablo Neira Ayusoa1cee072014-05-23 11:09:42 +02003722 list_for_each_entry_safe_reverse(trans, next,
3723 &net->nft.commit_list, list) {
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003724 list_del(&trans->list);
3725 trans->ctx.nla = NULL;
3726 call_rcu(&trans->rcu_head, nf_tables_abort_release_rcu);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003727 }
3728
3729 return 0;
3730}
3731
Patrick McHardy96518512013-10-14 11:00:02 +02003732static const struct nfnetlink_subsystem nf_tables_subsys = {
3733 .name = "nf_tables",
3734 .subsys_id = NFNL_SUBSYS_NFTABLES,
3735 .cb_count = NFT_MSG_MAX,
3736 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003737 .commit = nf_tables_commit,
3738 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02003739};
3740
Patrick McHardy20a69342013-10-11 12:06:22 +02003741/*
3742 * Loop detection - walk through the ruleset beginning at the destination chain
3743 * of a new jump until either the source chain is reached (loop) or all
3744 * reachable chains have been traversed.
3745 *
3746 * The loop check is performed whenever a new jump verdict is added to an
3747 * expression or verdict map or a verdict map is bound to a new chain.
3748 */
3749
3750static int nf_tables_check_loops(const struct nft_ctx *ctx,
3751 const struct nft_chain *chain);
3752
3753static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3754 const struct nft_set *set,
3755 const struct nft_set_iter *iter,
3756 const struct nft_set_elem *elem)
3757{
Pablo Neira Ayuso62f9c8b2014-02-07 14:45:01 +01003758 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3759 return 0;
3760
Patrick McHardy20a69342013-10-11 12:06:22 +02003761 switch (elem->data.verdict) {
3762 case NFT_JUMP:
3763 case NFT_GOTO:
3764 return nf_tables_check_loops(ctx, elem->data.chain);
3765 default:
3766 return 0;
3767 }
3768}
3769
3770static int nf_tables_check_loops(const struct nft_ctx *ctx,
3771 const struct nft_chain *chain)
3772{
3773 const struct nft_rule *rule;
3774 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02003775 const struct nft_set *set;
3776 struct nft_set_binding *binding;
3777 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02003778
3779 if (ctx->chain == chain)
3780 return -ELOOP;
3781
3782 list_for_each_entry(rule, &chain->rules, list) {
3783 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003784 const struct nft_data *data = NULL;
3785 int err;
3786
3787 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02003788 continue;
3789
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003790 err = expr->ops->validate(ctx, expr, &data);
3791 if (err < 0)
3792 return err;
3793
Patrick McHardy20a69342013-10-11 12:06:22 +02003794 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003795 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02003796
3797 switch (data->verdict) {
3798 case NFT_JUMP:
3799 case NFT_GOTO:
3800 err = nf_tables_check_loops(ctx, data->chain);
3801 if (err < 0)
3802 return err;
3803 default:
3804 break;
3805 }
3806 }
3807 }
3808
3809 list_for_each_entry(set, &ctx->table->sets, list) {
3810 if (!(set->flags & NFT_SET_MAP) ||
3811 set->dtype != NFT_DATA_VERDICT)
3812 continue;
3813
3814 list_for_each_entry(binding, &set->bindings, list) {
3815 if (binding->chain != chain)
3816 continue;
3817
3818 iter.skip = 0;
3819 iter.count = 0;
3820 iter.err = 0;
3821 iter.fn = nf_tables_loop_check_setelem;
3822
3823 set->ops->walk(ctx, set, &iter);
3824 if (iter.err < 0)
3825 return iter.err;
3826 }
3827 }
3828
3829 return 0;
3830}
3831
Patrick McHardy96518512013-10-14 11:00:02 +02003832/**
3833 * nft_validate_input_register - validate an expressions' input register
3834 *
3835 * @reg: the register number
3836 *
3837 * Validate that the input register is one of the general purpose
3838 * registers.
3839 */
3840int nft_validate_input_register(enum nft_registers reg)
3841{
3842 if (reg <= NFT_REG_VERDICT)
3843 return -EINVAL;
3844 if (reg > NFT_REG_MAX)
3845 return -ERANGE;
3846 return 0;
3847}
3848EXPORT_SYMBOL_GPL(nft_validate_input_register);
3849
3850/**
3851 * nft_validate_output_register - validate an expressions' output register
3852 *
3853 * @reg: the register number
3854 *
3855 * Validate that the output register is one of the general purpose
3856 * registers or the verdict register.
3857 */
3858int nft_validate_output_register(enum nft_registers reg)
3859{
3860 if (reg < NFT_REG_VERDICT)
3861 return -EINVAL;
3862 if (reg > NFT_REG_MAX)
3863 return -ERANGE;
3864 return 0;
3865}
3866EXPORT_SYMBOL_GPL(nft_validate_output_register);
3867
3868/**
3869 * nft_validate_data_load - validate an expressions' data load
3870 *
3871 * @ctx: context of the expression performing the load
3872 * @reg: the destination register number
3873 * @data: the data to load
3874 * @type: the data type
3875 *
3876 * Validate that a data load uses the appropriate data type for
3877 * the destination register. A value of NULL for the data means
3878 * that its runtime gathered data, which is always of type
3879 * NFT_DATA_VALUE.
3880 */
3881int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3882 const struct nft_data *data,
3883 enum nft_data_types type)
3884{
Patrick McHardy20a69342013-10-11 12:06:22 +02003885 int err;
3886
Patrick McHardy96518512013-10-14 11:00:02 +02003887 switch (reg) {
3888 case NFT_REG_VERDICT:
3889 if (data == NULL || type != NFT_DATA_VERDICT)
3890 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003891
3892 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3893 err = nf_tables_check_loops(ctx, data->chain);
3894 if (err < 0)
3895 return err;
3896
3897 if (ctx->chain->level + 1 > data->chain->level) {
3898 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3899 return -EMLINK;
3900 data->chain->level = ctx->chain->level + 1;
3901 }
3902 }
3903
Patrick McHardy96518512013-10-14 11:00:02 +02003904 return 0;
3905 default:
3906 if (data != NULL && type != NFT_DATA_VALUE)
3907 return -EINVAL;
3908 return 0;
3909 }
3910}
3911EXPORT_SYMBOL_GPL(nft_validate_data_load);
3912
3913static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3914 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3915 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3916 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3917};
3918
3919static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3920 struct nft_data_desc *desc, const struct nlattr *nla)
3921{
3922 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3923 struct nft_chain *chain;
3924 int err;
3925
3926 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3927 if (err < 0)
3928 return err;
3929
3930 if (!tb[NFTA_VERDICT_CODE])
3931 return -EINVAL;
3932 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3933
3934 switch (data->verdict) {
Patrick McHardye0abdad2014-02-18 18:06:50 +00003935 default:
3936 switch (data->verdict & NF_VERDICT_MASK) {
3937 case NF_ACCEPT:
3938 case NF_DROP:
3939 case NF_QUEUE:
3940 break;
3941 default:
3942 return -EINVAL;
3943 }
3944 /* fall through */
Patrick McHardy96518512013-10-14 11:00:02 +02003945 case NFT_CONTINUE:
3946 case NFT_BREAK:
3947 case NFT_RETURN:
3948 desc->len = sizeof(data->verdict);
3949 break;
3950 case NFT_JUMP:
3951 case NFT_GOTO:
3952 if (!tb[NFTA_VERDICT_CHAIN])
3953 return -EINVAL;
3954 chain = nf_tables_chain_lookup(ctx->table,
3955 tb[NFTA_VERDICT_CHAIN]);
3956 if (IS_ERR(chain))
3957 return PTR_ERR(chain);
3958 if (chain->flags & NFT_BASE_CHAIN)
3959 return -EOPNOTSUPP;
3960
Patrick McHardy96518512013-10-14 11:00:02 +02003961 chain->use++;
3962 data->chain = chain;
3963 desc->len = sizeof(data);
3964 break;
Patrick McHardy96518512013-10-14 11:00:02 +02003965 }
3966
3967 desc->type = NFT_DATA_VERDICT;
3968 return 0;
3969}
3970
3971static void nft_verdict_uninit(const struct nft_data *data)
3972{
3973 switch (data->verdict) {
3974 case NFT_JUMP:
3975 case NFT_GOTO:
3976 data->chain->use--;
3977 break;
3978 }
3979}
3980
3981static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3982{
3983 struct nlattr *nest;
3984
3985 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3986 if (!nest)
3987 goto nla_put_failure;
3988
3989 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3990 goto nla_put_failure;
3991
3992 switch (data->verdict) {
3993 case NFT_JUMP:
3994 case NFT_GOTO:
3995 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3996 goto nla_put_failure;
3997 }
3998 nla_nest_end(skb, nest);
3999 return 0;
4000
4001nla_put_failure:
4002 return -1;
4003}
4004
4005static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
4006 struct nft_data_desc *desc, const struct nlattr *nla)
4007{
4008 unsigned int len;
4009
4010 len = nla_len(nla);
4011 if (len == 0)
4012 return -EINVAL;
4013 if (len > sizeof(data->data))
4014 return -EOVERFLOW;
4015
4016 nla_memcpy(data->data, nla, sizeof(data->data));
4017 desc->type = NFT_DATA_VALUE;
4018 desc->len = len;
4019 return 0;
4020}
4021
4022static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
4023 unsigned int len)
4024{
4025 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
4026}
4027
4028static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
4029 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
4030 .len = FIELD_SIZEOF(struct nft_data, data) },
4031 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
4032};
4033
4034/**
4035 * nft_data_init - parse nf_tables data netlink attributes
4036 *
4037 * @ctx: context of the expression using the data
4038 * @data: destination struct nft_data
4039 * @desc: data description
4040 * @nla: netlink attribute containing data
4041 *
4042 * Parse the netlink data attributes and initialize a struct nft_data.
4043 * The type and length of data are returned in the data description.
4044 *
4045 * The caller can indicate that it only wants to accept data of type
4046 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4047 */
4048int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
4049 struct nft_data_desc *desc, const struct nlattr *nla)
4050{
4051 struct nlattr *tb[NFTA_DATA_MAX + 1];
4052 int err;
4053
4054 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
4055 if (err < 0)
4056 return err;
4057
4058 if (tb[NFTA_DATA_VALUE])
4059 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
4060 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
4061 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
4062 return -EINVAL;
4063}
4064EXPORT_SYMBOL_GPL(nft_data_init);
4065
4066/**
4067 * nft_data_uninit - release a nft_data item
4068 *
4069 * @data: struct nft_data to release
4070 * @type: type of data
4071 *
4072 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4073 * all others need to be released by calling this function.
4074 */
4075void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
4076{
4077 switch (type) {
4078 case NFT_DATA_VALUE:
4079 return;
4080 case NFT_DATA_VERDICT:
4081 return nft_verdict_uninit(data);
4082 default:
4083 WARN_ON(1);
4084 }
4085}
4086EXPORT_SYMBOL_GPL(nft_data_uninit);
4087
4088int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
4089 enum nft_data_types type, unsigned int len)
4090{
4091 struct nlattr *nest;
4092 int err;
4093
4094 nest = nla_nest_start(skb, attr);
4095 if (nest == NULL)
4096 return -1;
4097
4098 switch (type) {
4099 case NFT_DATA_VALUE:
4100 err = nft_value_dump(skb, data, len);
4101 break;
4102 case NFT_DATA_VERDICT:
4103 err = nft_verdict_dump(skb, data);
4104 break;
4105 default:
4106 err = -EINVAL;
4107 WARN_ON(1);
4108 }
4109
4110 nla_nest_end(skb, nest);
4111 return err;
4112}
4113EXPORT_SYMBOL_GPL(nft_data_dump);
4114
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004115static int nf_tables_init_net(struct net *net)
4116{
4117 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02004118 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02004119 net->nft.base_seq = 1;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004120 return 0;
4121}
4122
4123static struct pernet_operations nf_tables_net_ops = {
4124 .init = nf_tables_init_net,
4125};
4126
Patrick McHardy96518512013-10-14 11:00:02 +02004127static int __init nf_tables_module_init(void)
4128{
4129 int err;
4130
4131 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
4132 GFP_KERNEL);
4133 if (info == NULL) {
4134 err = -ENOMEM;
4135 goto err1;
4136 }
4137
4138 err = nf_tables_core_module_init();
4139 if (err < 0)
4140 goto err2;
4141
4142 err = nfnetlink_subsys_register(&nf_tables_subsys);
4143 if (err < 0)
4144 goto err3;
4145
4146 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004147 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02004148err3:
4149 nf_tables_core_module_exit();
4150err2:
4151 kfree(info);
4152err1:
4153 return err;
4154}
4155
4156static void __exit nf_tables_module_exit(void)
4157{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004158 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02004159 nfnetlink_subsys_unregister(&nf_tables_subsys);
4160 nf_tables_core_module_exit();
4161 kfree(info);
4162}
4163
4164module_init(nf_tables_module_init);
4165module_exit(nf_tables_module_exit);
4166
4167MODULE_LICENSE("GPL");
4168MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4169MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);