blob: 99cb884b985f1283e809a4aff22f9bbc2280cad6 [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{
Patrick McHardy8670c3a2015-03-03 20:04:18 +0000230 rule->genmask &= ~(1 << gencursor_next(net));
Arturo Borreroee01d542014-09-02 16:42:25 +0200231}
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] = {
Pablo Neira Ayuso1cae5652015-03-05 15:05:36 +0100404 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
405 .len = NFT_TABLE_MAXNAMELEN - 1 },
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200406 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
Patrick McHardy96518512013-10-14 11:00:02 +0200407};
408
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200409static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
410 u32 portid, u32 seq, int event, u32 flags,
411 int family, const struct nft_table *table)
Patrick McHardy96518512013-10-14 11:00:02 +0200412{
413 struct nlmsghdr *nlh;
414 struct nfgenmsg *nfmsg;
415
416 event |= NFNL_SUBSYS_NFTABLES << 8;
417 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
418 if (nlh == NULL)
419 goto nla_put_failure;
420
421 nfmsg = nlmsg_data(nlh);
422 nfmsg->nfgen_family = family;
423 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200424 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +0200425
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200426 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
Tomasz Bursztykad8bcc7682013-12-12 15:00:42 +0200427 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
428 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
Patrick McHardy96518512013-10-14 11:00:02 +0200429 goto nla_put_failure;
430
Johannes Berg053c0952015-01-16 22:09:00 +0100431 nlmsg_end(skb, nlh);
432 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200433
434nla_put_failure:
435 nlmsg_trim(skb, nlh);
436 return -1;
437}
438
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200439static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)
Patrick McHardy96518512013-10-14 11:00:02 +0200440{
441 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +0200442 int err;
443
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200444 if (!ctx->report &&
445 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +0200446 return 0;
447
448 err = -ENOBUFS;
449 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
450 if (skb == NULL)
451 goto err;
452
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200453 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
454 event, 0, ctx->afi->family, ctx->table);
Patrick McHardy96518512013-10-14 11:00:02 +0200455 if (err < 0) {
456 kfree_skb(skb);
457 goto err;
458 }
459
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200460 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
461 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +0200462err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200463 if (err < 0) {
464 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
465 err);
466 }
Patrick McHardy96518512013-10-14 11:00:02 +0200467 return err;
468}
469
470static int nf_tables_dump_tables(struct sk_buff *skb,
471 struct netlink_callback *cb)
472{
473 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
474 const struct nft_af_info *afi;
475 const struct nft_table *table;
476 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200477 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200478 int family = nfmsg->nfgen_family;
479
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200480 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +0200481 cb->seq = net->nft.base_seq;
482
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200483 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200484 if (family != NFPROTO_UNSPEC && family != afi->family)
485 continue;
486
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200487 list_for_each_entry_rcu(table, &afi->tables, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200488 if (idx < s_idx)
489 goto cont;
490 if (idx > s_idx)
491 memset(&cb->args[1], 0,
492 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200493 if (nf_tables_fill_table_info(skb, net,
Patrick McHardy96518512013-10-14 11:00:02 +0200494 NETLINK_CB(cb->skb).portid,
495 cb->nlh->nlmsg_seq,
496 NFT_MSG_NEWTABLE,
497 NLM_F_MULTI,
498 afi->family, table) < 0)
499 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +0200500
501 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +0200502cont:
503 idx++;
504 }
505 }
506done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200507 rcu_read_unlock();
Patrick McHardy96518512013-10-14 11:00:02 +0200508 cb->args[0] = idx;
509 return skb->len;
510}
511
512static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
513 const struct nlmsghdr *nlh,
514 const struct nlattr * const nla[])
515{
516 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
517 const struct nft_af_info *afi;
518 const struct nft_table *table;
519 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200520 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200521 int family = nfmsg->nfgen_family;
522 int err;
523
524 if (nlh->nlmsg_flags & NLM_F_DUMP) {
525 struct netlink_dump_control c = {
526 .dump = nf_tables_dump_tables,
527 };
528 return netlink_dump_start(nlsk, skb, nlh, &c);
529 }
530
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200531 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200532 if (IS_ERR(afi))
533 return PTR_ERR(afi);
534
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200535 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200536 if (IS_ERR(table))
537 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200538 if (table->flags & NFT_TABLE_INACTIVE)
539 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200540
541 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
542 if (!skb2)
543 return -ENOMEM;
544
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200545 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +0200546 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
547 family, table);
548 if (err < 0)
549 goto err;
550
551 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
552
553err:
554 kfree_skb(skb2);
555 return err;
556}
557
Patrick McHardy115a60b2014-01-03 12:16:15 +0000558static int nf_tables_table_enable(const struct nft_af_info *afi,
559 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200560{
561 struct nft_chain *chain;
562 int err, i = 0;
563
564 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100565 if (!(chain->flags & NFT_BASE_CHAIN))
566 continue;
567
Patrick McHardy115a60b2014-01-03 12:16:15 +0000568 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200569 if (err < 0)
570 goto err;
571
572 i++;
573 }
574 return 0;
575err:
576 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100577 if (!(chain->flags & NFT_BASE_CHAIN))
578 continue;
579
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200580 if (i-- <= 0)
581 break;
582
Patrick McHardy115a60b2014-01-03 12:16:15 +0000583 nf_unregister_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200584 }
585 return err;
586}
587
Pablo Neira Ayusof75edf52014-03-30 14:04:52 +0200588static void nf_tables_table_disable(const struct nft_af_info *afi,
Patrick McHardy115a60b2014-01-03 12:16:15 +0000589 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200590{
591 struct nft_chain *chain;
592
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100593 list_for_each_entry(chain, &table->chains, list) {
594 if (chain->flags & NFT_BASE_CHAIN)
Patrick McHardy115a60b2014-01-03 12:16:15 +0000595 nf_unregister_hooks(nft_base_chain(chain)->ops,
596 afi->nops);
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100597 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200598}
599
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200600static int nf_tables_updtable(struct nft_ctx *ctx)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200601{
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200602 struct nft_trans *trans;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200603 u32 flags;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200604 int ret = 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200605
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200606 if (!ctx->nla[NFTA_TABLE_FLAGS])
607 return 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200608
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200609 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
610 if (flags & ~NFT_TABLE_F_DORMANT)
611 return -EINVAL;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200612
Pablo Neira Ayuso63283dd2014-06-27 18:51:39 +0200613 if (flags == ctx->table->flags)
614 return 0;
615
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200616 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
617 sizeof(struct nft_trans_table));
618 if (trans == NULL)
619 return -ENOMEM;
620
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200621 if ((flags & NFT_TABLE_F_DORMANT) &&
622 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200623 nft_trans_table_enable(trans) = false;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200624 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
625 ctx->table->flags & NFT_TABLE_F_DORMANT) {
626 ret = nf_tables_table_enable(ctx->afi, ctx->table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200627 if (ret >= 0) {
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200628 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200629 nft_trans_table_enable(trans) = true;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200630 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200631 }
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200632 if (ret < 0)
633 goto err;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200634
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200635 nft_trans_table_update(trans) = true;
636 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
637 return 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200638err:
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200639 nft_trans_destroy(trans);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200640 return ret;
641}
642
Patrick McHardy96518512013-10-14 11:00:02 +0200643static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
644 const struct nlmsghdr *nlh,
645 const struct nlattr * const nla[])
646{
647 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
648 const struct nlattr *name;
649 struct nft_af_info *afi;
650 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200651 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200652 int family = nfmsg->nfgen_family;
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000653 u32 flags = 0;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200654 struct nft_ctx ctx;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200655 int err;
Patrick McHardy96518512013-10-14 11:00:02 +0200656
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200657 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200658 if (IS_ERR(afi))
659 return PTR_ERR(afi);
660
661 name = nla[NFTA_TABLE_NAME];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200662 table = nf_tables_table_lookup(afi, name);
Patrick McHardy96518512013-10-14 11:00:02 +0200663 if (IS_ERR(table)) {
664 if (PTR_ERR(table) != -ENOENT)
665 return PTR_ERR(table);
666 table = NULL;
667 }
668
669 if (table != NULL) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200670 if (table->flags & NFT_TABLE_INACTIVE)
671 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200672 if (nlh->nlmsg_flags & NLM_F_EXCL)
673 return -EEXIST;
674 if (nlh->nlmsg_flags & NLM_F_REPLACE)
675 return -EOPNOTSUPP;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200676
677 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
678 return nf_tables_updtable(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +0200679 }
680
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000681 if (nla[NFTA_TABLE_FLAGS]) {
682 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
683 if (flags & ~NFT_TABLE_F_DORMANT)
684 return -EINVAL;
685 }
686
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000687 if (!try_module_get(afi->owner))
688 return -EAFNOSUPPORT;
689
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100690 err = -ENOMEM;
Pablo Neira Ayuso1cae5652015-03-05 15:05:36 +0100691 table = kzalloc(sizeof(*table), GFP_KERNEL);
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100692 if (table == NULL)
693 goto err1;
Patrick McHardy96518512013-10-14 11:00:02 +0200694
Pablo Neira Ayuso1cae5652015-03-05 15:05:36 +0100695 nla_strlcpy(table->name, name, NFT_TABLE_MAXNAMELEN);
Patrick McHardy96518512013-10-14 11:00:02 +0200696 INIT_LIST_HEAD(&table->chains);
Patrick McHardy20a69342013-10-11 12:06:22 +0200697 INIT_LIST_HEAD(&table->sets);
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000698 table->flags = flags;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200699
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200700 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
701 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100702 if (err < 0)
703 goto err2;
704
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200705 list_add_tail_rcu(&table->list, &afi->tables);
Patrick McHardy96518512013-10-14 11:00:02 +0200706 return 0;
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100707err2:
708 kfree(table);
709err1:
710 module_put(afi->owner);
711 return err;
Patrick McHardy96518512013-10-14 11:00:02 +0200712}
713
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200714static int nft_flush_table(struct nft_ctx *ctx)
715{
716 int err;
717 struct nft_chain *chain, *nc;
718 struct nft_set *set, *ns;
719
Pablo Neira Ayusoa2f18db2015-01-04 15:14:22 +0100720 list_for_each_entry(chain, &ctx->table->chains, list) {
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200721 ctx->chain = chain;
722
723 err = nft_delrule_by_chain(ctx);
724 if (err < 0)
725 goto out;
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200726 }
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
Pablo Neira Ayusoa2f18db2015-01-04 15:14:22 +0100738 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
739 ctx->chain = chain;
740
741 err = nft_delchain(ctx);
742 if (err < 0)
743 goto out;
744 }
745
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200746 err = nft_deltable(ctx);
747out:
748 return err;
749}
750
751static int nft_flush(struct nft_ctx *ctx, int family)
752{
753 struct nft_af_info *afi;
754 struct nft_table *table, *nt;
755 const struct nlattr * const *nla = ctx->nla;
756 int err = 0;
757
758 list_for_each_entry(afi, &ctx->net->nft.af_info, list) {
759 if (family != AF_UNSPEC && afi->family != family)
760 continue;
761
762 ctx->afi = afi;
763 list_for_each_entry_safe(table, nt, &afi->tables, list) {
764 if (nla[NFTA_TABLE_NAME] &&
765 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
766 continue;
767
768 ctx->table = table;
769
770 err = nft_flush_table(ctx);
771 if (err < 0)
772 goto out;
773 }
774 }
775out:
776 return err;
777}
778
Patrick McHardy96518512013-10-14 11:00:02 +0200779static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
780 const struct nlmsghdr *nlh,
781 const struct nlattr * const nla[])
782{
783 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
784 struct nft_af_info *afi;
785 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200786 struct net *net = sock_net(skb->sk);
Arturo Borreroee01d542014-09-02 16:42:25 +0200787 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200788 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +0200789
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200790 nft_ctx_init(&ctx, skb, nlh, NULL, NULL, NULL, nla);
791 if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL)
792 return nft_flush(&ctx, family);
793
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200794 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200795 if (IS_ERR(afi))
796 return PTR_ERR(afi);
797
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200798 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
Patrick McHardy96518512013-10-14 11:00:02 +0200799 if (IS_ERR(table))
800 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200801 if (table->flags & NFT_TABLE_INACTIVE)
802 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +0200803
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200804 ctx.afi = afi;
805 ctx.table = table;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200806
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200807 return nft_flush_table(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +0200808}
809
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200810static void nf_tables_table_destroy(struct nft_ctx *ctx)
811{
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +0200812 BUG_ON(ctx->table->use > 0);
813
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200814 kfree(ctx->table);
815 module_put(ctx->afi->owner);
816}
817
Patrick McHardy2a37d752014-01-09 18:42:37 +0000818int nft_register_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200819{
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200820 int err = 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200821
822 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200823 if (chain_type[ctype->family][ctype->type] != NULL) {
824 err = -EBUSY;
825 goto out;
Patrick McHardy96518512013-10-14 11:00:02 +0200826 }
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200827 chain_type[ctype->family][ctype->type] = ctype;
828out:
Patrick McHardy96518512013-10-14 11:00:02 +0200829 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
830 return err;
831}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200832EXPORT_SYMBOL_GPL(nft_register_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200833
Patrick McHardy2a37d752014-01-09 18:42:37 +0000834void nft_unregister_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200835{
Patrick McHardy96518512013-10-14 11:00:02 +0200836 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200837 chain_type[ctype->family][ctype->type] = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200838 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
839}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200840EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200841
842/*
843 * Chains
844 */
845
846static struct nft_chain *
847nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
848{
849 struct nft_chain *chain;
850
851 list_for_each_entry(chain, &table->chains, list) {
852 if (chain->handle == handle)
853 return chain;
854 }
855
856 return ERR_PTR(-ENOENT);
857}
858
859static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
860 const struct nlattr *nla)
861{
862 struct nft_chain *chain;
863
864 if (nla == NULL)
865 return ERR_PTR(-EINVAL);
866
867 list_for_each_entry(chain, &table->chains, list) {
868 if (!nla_strcmp(nla, chain->name))
869 return chain;
870 }
871
872 return ERR_PTR(-ENOENT);
873}
874
875static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
876 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
877 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
878 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
879 .len = NFT_CHAIN_MAXNAMELEN - 1 },
880 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200881 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
Pablo Neira4c1f7812014-03-31 17:43:47 +0200882 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200883 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
Patrick McHardy96518512013-10-14 11:00:02 +0200884};
885
886static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
887 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
888 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
889};
890
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200891static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
892{
893 struct nft_stats *cpu_stats, total;
894 struct nlattr *nest;
Eric Dumazetce355e22014-07-09 15:14:06 +0200895 unsigned int seq;
896 u64 pkts, bytes;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200897 int cpu;
898
899 memset(&total, 0, sizeof(total));
900 for_each_possible_cpu(cpu) {
901 cpu_stats = per_cpu_ptr(stats, cpu);
Eric Dumazetce355e22014-07-09 15:14:06 +0200902 do {
903 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
904 pkts = cpu_stats->pkts;
905 bytes = cpu_stats->bytes;
906 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
907 total.pkts += pkts;
908 total.bytes += bytes;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200909 }
910 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
911 if (nest == NULL)
912 goto nla_put_failure;
913
914 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
915 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
916 goto nla_put_failure;
917
918 nla_nest_end(skb, nest);
919 return 0;
920
921nla_put_failure:
922 return -ENOSPC;
923}
924
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200925static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
926 u32 portid, u32 seq, int event, u32 flags,
927 int family, const struct nft_table *table,
Patrick McHardy96518512013-10-14 11:00:02 +0200928 const struct nft_chain *chain)
929{
930 struct nlmsghdr *nlh;
931 struct nfgenmsg *nfmsg;
932
933 event |= NFNL_SUBSYS_NFTABLES << 8;
934 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
935 if (nlh == NULL)
936 goto nla_put_failure;
937
938 nfmsg = nlmsg_data(nlh);
939 nfmsg->nfgen_family = family;
940 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200941 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +0200942
943 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
944 goto nla_put_failure;
945 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
946 goto nla_put_failure;
947 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
948 goto nla_put_failure;
949
950 if (chain->flags & NFT_BASE_CHAIN) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200951 const struct nft_base_chain *basechain = nft_base_chain(chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000952 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200953 struct nlattr *nest;
954
955 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
Patrick McHardy96518512013-10-14 11:00:02 +0200956 if (nest == NULL)
957 goto nla_put_failure;
958 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
959 goto nla_put_failure;
960 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
961 goto nla_put_failure;
962 nla_nest_end(skb, nest);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200963
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200964 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
965 htonl(basechain->policy)))
966 goto nla_put_failure;
967
Patrick McHardybaae3e62014-01-09 18:42:34 +0000968 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
969 goto nla_put_failure;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200970
971 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
972 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +0200973 }
974
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200975 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
976 goto nla_put_failure;
977
Johannes Berg053c0952015-01-16 22:09:00 +0100978 nlmsg_end(skb, nlh);
979 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200980
981nla_put_failure:
982 nlmsg_trim(skb, nlh);
983 return -1;
984}
985
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200986static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
Patrick McHardy96518512013-10-14 11:00:02 +0200987{
988 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +0200989 int err;
990
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200991 if (!ctx->report &&
992 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +0200993 return 0;
994
995 err = -ENOBUFS;
996 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
997 if (skb == NULL)
998 goto err;
999
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001000 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
1001 event, 0, ctx->afi->family, ctx->table,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001002 ctx->chain);
Patrick McHardy96518512013-10-14 11:00:02 +02001003 if (err < 0) {
1004 kfree_skb(skb);
1005 goto err;
1006 }
1007
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001008 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1009 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001010err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001011 if (err < 0) {
1012 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1013 err);
1014 }
Patrick McHardy96518512013-10-14 11:00:02 +02001015 return err;
1016}
1017
1018static int nf_tables_dump_chains(struct sk_buff *skb,
1019 struct netlink_callback *cb)
1020{
1021 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1022 const struct nft_af_info *afi;
1023 const struct nft_table *table;
1024 const struct nft_chain *chain;
1025 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001026 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001027 int family = nfmsg->nfgen_family;
1028
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001029 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001030 cb->seq = net->nft.base_seq;
1031
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001032 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001033 if (family != NFPROTO_UNSPEC && family != afi->family)
1034 continue;
1035
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001036 list_for_each_entry_rcu(table, &afi->tables, list) {
1037 list_for_each_entry_rcu(chain, &table->chains, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001038 if (idx < s_idx)
1039 goto cont;
1040 if (idx > s_idx)
1041 memset(&cb->args[1], 0,
1042 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001043 if (nf_tables_fill_chain_info(skb, net,
1044 NETLINK_CB(cb->skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001045 cb->nlh->nlmsg_seq,
1046 NFT_MSG_NEWCHAIN,
1047 NLM_F_MULTI,
1048 afi->family, table, chain) < 0)
1049 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001050
1051 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +02001052cont:
1053 idx++;
1054 }
1055 }
1056 }
1057done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001058 rcu_read_unlock();
Patrick McHardy96518512013-10-14 11:00:02 +02001059 cb->args[0] = idx;
1060 return skb->len;
1061}
1062
Patrick McHardy96518512013-10-14 11:00:02 +02001063static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
1064 const struct nlmsghdr *nlh,
1065 const struct nlattr * const nla[])
1066{
1067 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1068 const struct nft_af_info *afi;
1069 const struct nft_table *table;
1070 const struct nft_chain *chain;
1071 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001072 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001073 int family = nfmsg->nfgen_family;
1074 int err;
1075
1076 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1077 struct netlink_dump_control c = {
1078 .dump = nf_tables_dump_chains,
1079 };
1080 return netlink_dump_start(nlsk, skb, nlh, &c);
1081 }
1082
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001083 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001084 if (IS_ERR(afi))
1085 return PTR_ERR(afi);
1086
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001087 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001088 if (IS_ERR(table))
1089 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001090 if (table->flags & NFT_TABLE_INACTIVE)
1091 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001092
1093 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1094 if (IS_ERR(chain))
1095 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001096 if (chain->flags & NFT_CHAIN_INACTIVE)
1097 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001098
1099 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1100 if (!skb2)
1101 return -ENOMEM;
1102
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001103 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001104 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
1105 family, table, chain);
1106 if (err < 0)
1107 goto err;
1108
1109 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1110
1111err:
1112 kfree_skb(skb2);
1113 return err;
1114}
1115
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001116static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1117 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
1118 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
1119};
1120
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001121static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001122{
1123 struct nlattr *tb[NFTA_COUNTER_MAX+1];
1124 struct nft_stats __percpu *newstats;
1125 struct nft_stats *stats;
1126 int err;
1127
1128 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
1129 if (err < 0)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001130 return ERR_PTR(err);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001131
1132 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001133 return ERR_PTR(-EINVAL);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001134
Eric Dumazetce355e22014-07-09 15:14:06 +02001135 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001136 if (newstats == NULL)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001137 return ERR_PTR(-ENOMEM);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001138
1139 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1140 * are not exposed to userspace.
1141 */
Pablo Neira Ayusoe8781f72015-01-21 18:04:18 +01001142 preempt_disable();
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001143 stats = this_cpu_ptr(newstats);
1144 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1145 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
Pablo Neira Ayusoe8781f72015-01-21 18:04:18 +01001146 preempt_enable();
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001147
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001148 return newstats;
1149}
1150
1151static void nft_chain_stats_replace(struct nft_base_chain *chain,
1152 struct nft_stats __percpu *newstats)
1153{
Pablo Neira Ayusob88825d2014-08-05 17:25:59 +02001154 if (newstats == NULL)
1155 return;
1156
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001157 if (chain->stats) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001158 struct nft_stats __percpu *oldstats =
Patrick McHardy67a8fc22014-02-18 18:06:49 +00001159 nft_dereference(chain->stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001160
1161 rcu_assign_pointer(chain->stats, newstats);
1162 synchronize_rcu();
1163 free_percpu(oldstats);
1164 } else
1165 rcu_assign_pointer(chain->stats, newstats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001166}
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001167
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001168static void nf_tables_chain_destroy(struct nft_chain *chain)
1169{
1170 BUG_ON(chain->use > 0);
1171
1172 if (chain->flags & NFT_BASE_CHAIN) {
1173 module_put(nft_base_chain(chain)->type->owner);
1174 free_percpu(nft_base_chain(chain)->stats);
1175 kfree(nft_base_chain(chain));
1176 } else {
1177 kfree(chain);
1178 }
1179}
1180
Patrick McHardy96518512013-10-14 11:00:02 +02001181static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
1182 const struct nlmsghdr *nlh,
1183 const struct nlattr * const nla[])
1184{
1185 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1186 const struct nlattr * uninitialized_var(name);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001187 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001188 struct nft_table *table;
1189 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001190 struct nft_base_chain *basechain = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001191 struct nlattr *ha[NFTA_HOOK_MAX + 1];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001192 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001193 int family = nfmsg->nfgen_family;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001194 u8 policy = NF_ACCEPT;
Patrick McHardy96518512013-10-14 11:00:02 +02001195 u64 handle = 0;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001196 unsigned int i;
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001197 struct nft_stats __percpu *stats;
Patrick McHardy96518512013-10-14 11:00:02 +02001198 int err;
1199 bool create;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001200 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001201
1202 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1203
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001204 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +02001205 if (IS_ERR(afi))
1206 return PTR_ERR(afi);
1207
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001208 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001209 if (IS_ERR(table))
1210 return PTR_ERR(table);
1211
Patrick McHardy96518512013-10-14 11:00:02 +02001212 chain = NULL;
1213 name = nla[NFTA_CHAIN_NAME];
1214
1215 if (nla[NFTA_CHAIN_HANDLE]) {
1216 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
1217 chain = nf_tables_chain_lookup_byhandle(table, handle);
1218 if (IS_ERR(chain))
1219 return PTR_ERR(chain);
1220 } else {
1221 chain = nf_tables_chain_lookup(table, name);
1222 if (IS_ERR(chain)) {
1223 if (PTR_ERR(chain) != -ENOENT)
1224 return PTR_ERR(chain);
1225 chain = NULL;
1226 }
1227 }
1228
Patrick McHardy57de2a02014-01-09 18:42:31 +00001229 if (nla[NFTA_CHAIN_POLICY]) {
1230 if ((chain != NULL &&
Pablo Neira Ayusod6b6cb12015-03-17 13:21:42 +01001231 !(chain->flags & NFT_BASE_CHAIN)))
1232 return -EOPNOTSUPP;
1233
1234 if (chain == NULL &&
Patrick McHardy57de2a02014-01-09 18:42:31 +00001235 nla[NFTA_CHAIN_HOOK] == NULL)
1236 return -EOPNOTSUPP;
1237
Pablo Neira Ayuso8f46df12014-01-10 15:11:25 +01001238 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
Patrick McHardy57de2a02014-01-09 18:42:31 +00001239 switch (policy) {
1240 case NF_DROP:
1241 case NF_ACCEPT:
1242 break;
1243 default:
1244 return -EINVAL;
1245 }
1246 }
1247
Patrick McHardy96518512013-10-14 11:00:02 +02001248 if (chain != NULL) {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001249 struct nft_stats *stats = NULL;
1250 struct nft_trans *trans;
1251
1252 if (chain->flags & NFT_CHAIN_INACTIVE)
1253 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001254 if (nlh->nlmsg_flags & NLM_F_EXCL)
1255 return -EEXIST;
1256 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1257 return -EOPNOTSUPP;
1258
1259 if (nla[NFTA_CHAIN_HANDLE] && name &&
1260 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1261 return -EEXIST;
1262
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001263 if (nla[NFTA_CHAIN_COUNTERS]) {
1264 if (!(chain->flags & NFT_BASE_CHAIN))
1265 return -EOPNOTSUPP;
1266
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001267 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1268 if (IS_ERR(stats))
1269 return PTR_ERR(stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001270 }
1271
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001272 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1273 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1274 sizeof(struct nft_trans_chain));
Pablo Neira Ayusof5553c12015-01-29 19:08:09 +01001275 if (trans == NULL) {
1276 free_percpu(stats);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001277 return -ENOMEM;
Pablo Neira Ayusof5553c12015-01-29 19:08:09 +01001278 }
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001279
1280 nft_trans_chain_stats(trans) = stats;
1281 nft_trans_chain_update(trans) = true;
1282
Patrick McHardy4401a862014-01-09 18:42:32 +00001283 if (nla[NFTA_CHAIN_POLICY])
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001284 nft_trans_chain_policy(trans) = policy;
1285 else
1286 nft_trans_chain_policy(trans) = -1;
Patrick McHardy4401a862014-01-09 18:42:32 +00001287
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001288 if (nla[NFTA_CHAIN_HANDLE] && name) {
1289 nla_strlcpy(nft_trans_chain_name(trans), name,
1290 NFT_CHAIN_MAXNAMELEN);
1291 }
1292 list_add_tail(&trans->list, &net->nft.commit_list);
1293 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001294 }
1295
Patrick McHardy75820672014-01-09 18:42:33 +00001296 if (table->use == UINT_MAX)
1297 return -EOVERFLOW;
1298
Patrick McHardy96518512013-10-14 11:00:02 +02001299 if (nla[NFTA_CHAIN_HOOK]) {
Patrick McHardy2a37d752014-01-09 18:42:37 +00001300 const struct nf_chain_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001301 struct nf_hook_ops *ops;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001302 nf_hookfn *hookfn;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001303 u32 hooknum, priority;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001304
Patrick McHardybaae3e62014-01-09 18:42:34 +00001305 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001306 if (nla[NFTA_CHAIN_TYPE]) {
1307 type = nf_tables_chain_type_lookup(afi,
1308 nla[NFTA_CHAIN_TYPE],
1309 create);
Patrick McHardy93b08062014-01-09 18:42:36 +00001310 if (IS_ERR(type))
1311 return PTR_ERR(type);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001312 }
Patrick McHardy96518512013-10-14 11:00:02 +02001313
1314 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1315 nft_hook_policy);
1316 if (err < 0)
1317 return err;
1318 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1319 ha[NFTA_HOOK_PRIORITY] == NULL)
1320 return -EINVAL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001321
1322 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1323 if (hooknum >= afi->nhooks)
Patrick McHardy96518512013-10-14 11:00:02 +02001324 return -EINVAL;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001325 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
Patrick McHardy96518512013-10-14 11:00:02 +02001326
Patrick McHardybaae3e62014-01-09 18:42:34 +00001327 if (!(type->hook_mask & (1 << hooknum)))
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001328 return -EOPNOTSUPP;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001329 if (!try_module_get(type->owner))
Patrick McHardybaae3e62014-01-09 18:42:34 +00001330 return -ENOENT;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001331 hookfn = type->hooks[hooknum];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001332
Patrick McHardy96518512013-10-14 11:00:02 +02001333 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
Pablo Neira Ayusof5553c12015-01-29 19:08:09 +01001334 if (basechain == NULL) {
1335 module_put(type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001336 return -ENOMEM;
Pablo Neira Ayusof5553c12015-01-29 19:08:09 +01001337 }
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001338
Patrick McHardy4401a862014-01-09 18:42:32 +00001339 if (nla[NFTA_CHAIN_COUNTERS]) {
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001340 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1341 if (IS_ERR(stats)) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001342 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +00001343 kfree(basechain);
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001344 return PTR_ERR(stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001345 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001346 basechain->stats = stats;
Patrick McHardy4401a862014-01-09 18:42:32 +00001347 } else {
Eric Dumazetce355e22014-07-09 15:14:06 +02001348 stats = netdev_alloc_pcpu_stats(struct nft_stats);
Sabrina Dubrocac123bb72014-10-21 11:08:21 +02001349 if (stats == NULL) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001350 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +00001351 kfree(basechain);
Sabrina Dubrocac123bb72014-10-21 11:08:21 +02001352 return -ENOMEM;
Patrick McHardy4401a862014-01-09 18:42:32 +00001353 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001354 rcu_assign_pointer(basechain->stats, stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001355 }
1356
Patrick McHardy5ebb3352015-03-21 15:19:15 +00001357 write_pnet(&basechain->pnet, net);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001358 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +02001359 chain = &basechain->chain;
1360
Patrick McHardy115a60b2014-01-03 12:16:15 +00001361 for (i = 0; i < afi->nops; i++) {
1362 ops = &basechain->ops[i];
1363 ops->pf = family;
1364 ops->owner = afi->owner;
1365 ops->hooknum = hooknum;
1366 ops->priority = priority;
1367 ops->priv = chain;
1368 ops->hook = afi->hooks[ops->hooknum];
1369 if (hookfn)
1370 ops->hook = hookfn;
1371 if (afi->hook_ops_init)
1372 afi->hook_ops_init(ops, i);
1373 }
Patrick McHardy96518512013-10-14 11:00:02 +02001374
1375 chain->flags |= NFT_BASE_CHAIN;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001376 basechain->policy = policy;
Patrick McHardy96518512013-10-14 11:00:02 +02001377 } else {
1378 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1379 if (chain == NULL)
1380 return -ENOMEM;
1381 }
1382
1383 INIT_LIST_HEAD(&chain->rules);
1384 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +02001385 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +02001386 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1387
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +02001388 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1389 chain->flags & NFT_BASE_CHAIN) {
Patrick McHardy115a60b2014-01-03 12:16:15 +00001390 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001391 if (err < 0)
1392 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001393 }
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001394
1395 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1396 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1397 if (err < 0)
1398 goto err2;
1399
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001400 table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001401 list_add_tail_rcu(&chain->list, &table->chains);
Patrick McHardy96518512013-10-14 11:00:02 +02001402 return 0;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001403err2:
Arturo Borreroc5598792014-09-02 16:42:23 +02001404 nf_tables_unregister_hooks(table, chain, afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001405err1:
1406 nf_tables_chain_destroy(chain);
1407 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001408}
1409
1410static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1411 const struct nlmsghdr *nlh,
1412 const struct nlattr * const nla[])
1413{
1414 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001415 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001416 struct nft_table *table;
1417 struct nft_chain *chain;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001418 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001419 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001420 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001421
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001422 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001423 if (IS_ERR(afi))
1424 return PTR_ERR(afi);
1425
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001426 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001427 if (IS_ERR(table))
1428 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001429 if (table->flags & NFT_TABLE_INACTIVE)
1430 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001431
1432 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1433 if (IS_ERR(chain))
1434 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001435 if (chain->flags & NFT_CHAIN_INACTIVE)
1436 return -ENOENT;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001437 if (chain->use > 0)
Patrick McHardy96518512013-10-14 11:00:02 +02001438 return -EBUSY;
1439
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001440 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001441
Arturo Borreroee01d542014-09-02 16:42:25 +02001442 return nft_delchain(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +02001443}
1444
Patrick McHardy96518512013-10-14 11:00:02 +02001445/*
1446 * Expressions
1447 */
1448
1449/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001450 * nft_register_expr - register nf_tables expr type
1451 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001452 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001453 * Registers the expr type for use with nf_tables. Returns zero on
Patrick McHardy96518512013-10-14 11:00:02 +02001454 * success or a negative errno code otherwise.
1455 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001456int nft_register_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001457{
1458 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001459 if (type->family == NFPROTO_UNSPEC)
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001460 list_add_tail_rcu(&type->list, &nf_tables_expressions);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001461 else
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001462 list_add_rcu(&type->list, &nf_tables_expressions);
Patrick McHardy96518512013-10-14 11:00:02 +02001463 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1464 return 0;
1465}
1466EXPORT_SYMBOL_GPL(nft_register_expr);
1467
1468/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001469 * nft_unregister_expr - unregister nf_tables expr type
1470 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001471 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001472 * Unregisters the expr typefor use with nf_tables.
Patrick McHardy96518512013-10-14 11:00:02 +02001473 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001474void nft_unregister_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001475{
1476 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001477 list_del_rcu(&type->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001478 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1479}
1480EXPORT_SYMBOL_GPL(nft_unregister_expr);
1481
Patrick McHardy64d46802014-02-05 15:03:37 +00001482static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1483 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001484{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001485 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001486
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001487 list_for_each_entry(type, &nf_tables_expressions, list) {
Patrick McHardy64d46802014-02-05 15:03:37 +00001488 if (!nla_strcmp(nla, type->name) &&
1489 (!type->family || type->family == family))
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001490 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001491 }
1492 return NULL;
1493}
1494
Patrick McHardy64d46802014-02-05 15:03:37 +00001495static const struct nft_expr_type *nft_expr_type_get(u8 family,
1496 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001497{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001498 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001499
1500 if (nla == NULL)
1501 return ERR_PTR(-EINVAL);
1502
Patrick McHardy64d46802014-02-05 15:03:37 +00001503 type = __nft_expr_type_get(family, nla);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001504 if (type != NULL && try_module_get(type->owner))
1505 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001506
1507#ifdef CONFIG_MODULES
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001508 if (type == NULL) {
Patrick McHardy96518512013-10-14 11:00:02 +02001509 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001510 request_module("nft-expr-%u-%.*s", family,
1511 nla_len(nla), (char *)nla_data(nla));
1512 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1513 if (__nft_expr_type_get(family, nla))
1514 return ERR_PTR(-EAGAIN);
1515
1516 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy96518512013-10-14 11:00:02 +02001517 request_module("nft-expr-%.*s",
1518 nla_len(nla), (char *)nla_data(nla));
1519 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001520 if (__nft_expr_type_get(family, nla))
Patrick McHardy96518512013-10-14 11:00:02 +02001521 return ERR_PTR(-EAGAIN);
1522 }
1523#endif
1524 return ERR_PTR(-ENOENT);
1525}
1526
1527static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1528 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1529 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1530};
1531
1532static int nf_tables_fill_expr_info(struct sk_buff *skb,
1533 const struct nft_expr *expr)
1534{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001535 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
Patrick McHardy96518512013-10-14 11:00:02 +02001536 goto nla_put_failure;
1537
1538 if (expr->ops->dump) {
1539 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1540 if (data == NULL)
1541 goto nla_put_failure;
1542 if (expr->ops->dump(skb, expr) < 0)
1543 goto nla_put_failure;
1544 nla_nest_end(skb, data);
1545 }
1546
1547 return skb->len;
1548
1549nla_put_failure:
1550 return -1;
1551};
1552
1553struct nft_expr_info {
1554 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001555 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001556};
1557
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001558static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1559 const struct nlattr *nla,
Patrick McHardy96518512013-10-14 11:00:02 +02001560 struct nft_expr_info *info)
1561{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001562 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001563 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001564 struct nlattr *tb[NFTA_EXPR_MAX + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001565 int err;
1566
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001567 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
Patrick McHardy96518512013-10-14 11:00:02 +02001568 if (err < 0)
1569 return err;
1570
Patrick McHardy64d46802014-02-05 15:03:37 +00001571 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001572 if (IS_ERR(type))
1573 return PTR_ERR(type);
1574
1575 if (tb[NFTA_EXPR_DATA]) {
1576 err = nla_parse_nested(info->tb, type->maxattr,
1577 tb[NFTA_EXPR_DATA], type->policy);
1578 if (err < 0)
1579 goto err1;
1580 } else
1581 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1582
1583 if (type->select_ops != NULL) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001584 ops = type->select_ops(ctx,
1585 (const struct nlattr * const *)info->tb);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001586 if (IS_ERR(ops)) {
1587 err = PTR_ERR(ops);
1588 goto err1;
1589 }
1590 } else
1591 ops = type->ops;
1592
Patrick McHardy96518512013-10-14 11:00:02 +02001593 info->ops = ops;
1594 return 0;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001595
1596err1:
1597 module_put(type->owner);
1598 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001599}
1600
1601static int nf_tables_newexpr(const struct nft_ctx *ctx,
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001602 const struct nft_expr_info *info,
Patrick McHardy96518512013-10-14 11:00:02 +02001603 struct nft_expr *expr)
1604{
1605 const struct nft_expr_ops *ops = info->ops;
1606 int err;
1607
1608 expr->ops = ops;
1609 if (ops->init) {
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001610 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
Patrick McHardy96518512013-10-14 11:00:02 +02001611 if (err < 0)
1612 goto err1;
1613 }
1614
Patrick McHardy96518512013-10-14 11:00:02 +02001615 return 0;
1616
1617err1:
1618 expr->ops = NULL;
1619 return err;
1620}
1621
Patrick McHardy62472bc2014-03-07 19:08:30 +01001622static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1623 struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +02001624{
1625 if (expr->ops->destroy)
Patrick McHardy62472bc2014-03-07 19:08:30 +01001626 expr->ops->destroy(ctx, expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001627 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001628}
1629
1630/*
1631 * Rules
1632 */
1633
1634static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1635 u64 handle)
1636{
1637 struct nft_rule *rule;
1638
1639 // FIXME: this sucks
1640 list_for_each_entry(rule, &chain->rules, list) {
1641 if (handle == rule->handle)
1642 return rule;
1643 }
1644
1645 return ERR_PTR(-ENOENT);
1646}
1647
1648static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1649 const struct nlattr *nla)
1650{
1651 if (nla == NULL)
1652 return ERR_PTR(-EINVAL);
1653
1654 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1655}
1656
1657static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1658 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1659 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1660 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1661 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1662 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001663 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001664 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001665 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1666 .len = NFT_USERDATA_MAXLEN },
Patrick McHardy96518512013-10-14 11:00:02 +02001667};
1668
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001669static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1670 u32 portid, u32 seq, int event,
1671 u32 flags, int family,
Patrick McHardy96518512013-10-14 11:00:02 +02001672 const struct nft_table *table,
1673 const struct nft_chain *chain,
1674 const struct nft_rule *rule)
1675{
1676 struct nlmsghdr *nlh;
1677 struct nfgenmsg *nfmsg;
1678 const struct nft_expr *expr, *next;
1679 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001680 const struct nft_rule *prule;
1681 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001682
Eric Leblond5e948462013-10-10 13:41:44 +02001683 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001684 flags);
1685 if (nlh == NULL)
1686 goto nla_put_failure;
1687
1688 nfmsg = nlmsg_data(nlh);
1689 nfmsg->nfgen_family = family;
1690 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001691 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +02001692
1693 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1694 goto nla_put_failure;
1695 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1696 goto nla_put_failure;
1697 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1698 goto nla_put_failure;
1699
Eric Leblond5e948462013-10-10 13:41:44 +02001700 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1701 prule = list_entry(rule->list.prev, struct nft_rule, list);
1702 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1703 cpu_to_be64(prule->handle)))
1704 goto nla_put_failure;
1705 }
1706
Patrick McHardy96518512013-10-14 11:00:02 +02001707 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1708 if (list == NULL)
1709 goto nla_put_failure;
1710 nft_rule_for_each_expr(expr, next, rule) {
1711 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1712 if (elem == NULL)
1713 goto nla_put_failure;
1714 if (nf_tables_fill_expr_info(skb, expr) < 0)
1715 goto nla_put_failure;
1716 nla_nest_end(skb, elem);
1717 }
1718 nla_nest_end(skb, list);
1719
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001720 if (rule->udata) {
1721 struct nft_userdata *udata = nft_userdata(rule);
1722 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
1723 udata->data) < 0)
1724 goto nla_put_failure;
1725 }
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001726
Johannes Berg053c0952015-01-16 22:09:00 +01001727 nlmsg_end(skb, nlh);
1728 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001729
1730nla_put_failure:
1731 nlmsg_trim(skb, nlh);
1732 return -1;
1733}
1734
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001735static int nf_tables_rule_notify(const struct nft_ctx *ctx,
Patrick McHardy96518512013-10-14 11:00:02 +02001736 const struct nft_rule *rule,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001737 int event)
Patrick McHardy96518512013-10-14 11:00:02 +02001738{
1739 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +02001740 int err;
1741
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001742 if (!ctx->report &&
1743 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +02001744 return 0;
1745
1746 err = -ENOBUFS;
1747 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1748 if (skb == NULL)
1749 goto err;
1750
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001751 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
1752 event, 0, ctx->afi->family, ctx->table,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001753 ctx->chain, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001754 if (err < 0) {
1755 kfree_skb(skb);
1756 goto err;
1757 }
1758
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001759 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1760 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001761err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001762 if (err < 0) {
1763 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1764 err);
1765 }
Patrick McHardy96518512013-10-14 11:00:02 +02001766 return err;
1767}
1768
1769static int nf_tables_dump_rules(struct sk_buff *skb,
1770 struct netlink_callback *cb)
1771{
1772 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1773 const struct nft_af_info *afi;
1774 const struct nft_table *table;
1775 const struct nft_chain *chain;
1776 const struct nft_rule *rule;
1777 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001778 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001779 int family = nfmsg->nfgen_family;
1780
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001781 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001782 cb->seq = net->nft.base_seq;
1783
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001784 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001785 if (family != NFPROTO_UNSPEC && family != afi->family)
1786 continue;
1787
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001788 list_for_each_entry_rcu(table, &afi->tables, list) {
1789 list_for_each_entry_rcu(chain, &table->chains, list) {
1790 list_for_each_entry_rcu(rule, &chain->rules, list) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001791 if (!nft_rule_is_active(net, rule))
1792 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001793 if (idx < s_idx)
1794 goto cont;
1795 if (idx > s_idx)
1796 memset(&cb->args[1], 0,
1797 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001798 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001799 cb->nlh->nlmsg_seq,
1800 NFT_MSG_NEWRULE,
1801 NLM_F_MULTI | NLM_F_APPEND,
1802 afi->family, table, chain, rule) < 0)
1803 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001804
1805 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +02001806cont:
1807 idx++;
1808 }
1809 }
1810 }
1811 }
1812done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001813 rcu_read_unlock();
1814
Patrick McHardy96518512013-10-14 11:00:02 +02001815 cb->args[0] = idx;
1816 return skb->len;
1817}
1818
1819static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1820 const struct nlmsghdr *nlh,
1821 const struct nlattr * const nla[])
1822{
1823 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1824 const struct nft_af_info *afi;
1825 const struct nft_table *table;
1826 const struct nft_chain *chain;
1827 const struct nft_rule *rule;
1828 struct sk_buff *skb2;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001829 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001830 int family = nfmsg->nfgen_family;
1831 int err;
1832
1833 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1834 struct netlink_dump_control c = {
1835 .dump = nf_tables_dump_rules,
1836 };
1837 return netlink_dump_start(nlsk, skb, nlh, &c);
1838 }
1839
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001840 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001841 if (IS_ERR(afi))
1842 return PTR_ERR(afi);
1843
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001844 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001845 if (IS_ERR(table))
1846 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02001847 if (table->flags & NFT_TABLE_INACTIVE)
1848 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001849
1850 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1851 if (IS_ERR(chain))
1852 return PTR_ERR(chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001853 if (chain->flags & NFT_CHAIN_INACTIVE)
1854 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02001855
1856 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1857 if (IS_ERR(rule))
1858 return PTR_ERR(rule);
1859
1860 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1861 if (!skb2)
1862 return -ENOMEM;
1863
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001864 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001865 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1866 family, table, chain, rule);
1867 if (err < 0)
1868 goto err;
1869
1870 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1871
1872err:
1873 kfree_skb(skb2);
1874 return err;
1875}
1876
Patrick McHardy62472bc2014-03-07 19:08:30 +01001877static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1878 struct nft_rule *rule)
Patrick McHardy96518512013-10-14 11:00:02 +02001879{
Patrick McHardy96518512013-10-14 11:00:02 +02001880 struct nft_expr *expr;
1881
1882 /*
1883 * Careful: some expressions might not be initialized in case this
1884 * is called on error from nf_tables_newrule().
1885 */
1886 expr = nft_expr_first(rule);
1887 while (expr->ops && expr != nft_expr_last(rule)) {
Patrick McHardy62472bc2014-03-07 19:08:30 +01001888 nf_tables_expr_destroy(ctx, expr);
Patrick McHardy96518512013-10-14 11:00:02 +02001889 expr = nft_expr_next(expr);
1890 }
1891 kfree(rule);
1892}
1893
Patrick McHardy96518512013-10-14 11:00:02 +02001894#define NFT_RULE_MAXEXPRS 128
1895
1896static struct nft_expr_info *info;
1897
1898static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1899 const struct nlmsghdr *nlh,
1900 const struct nlattr * const nla[])
1901{
1902 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001903 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001904 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001905 struct nft_table *table;
1906 struct nft_chain *chain;
1907 struct nft_rule *rule, *old_rule = NULL;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001908 struct nft_userdata *udata;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001909 struct nft_trans *trans = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001910 struct nft_expr *expr;
1911 struct nft_ctx ctx;
1912 struct nlattr *tmp;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001913 unsigned int size, i, n, ulen = 0, usize = 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001914 int err, rem;
1915 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02001916 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02001917
1918 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1919
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001920 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02001921 if (IS_ERR(afi))
1922 return PTR_ERR(afi);
1923
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001924 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02001925 if (IS_ERR(table))
1926 return PTR_ERR(table);
1927
1928 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1929 if (IS_ERR(chain))
1930 return PTR_ERR(chain);
1931
1932 if (nla[NFTA_RULE_HANDLE]) {
1933 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1934 rule = __nf_tables_rule_lookup(chain, handle);
1935 if (IS_ERR(rule))
1936 return PTR_ERR(rule);
1937
1938 if (nlh->nlmsg_flags & NLM_F_EXCL)
1939 return -EEXIST;
1940 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1941 old_rule = rule;
1942 else
1943 return -EOPNOTSUPP;
1944 } else {
1945 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1946 return -EINVAL;
1947 handle = nf_tables_alloc_handle(table);
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +02001948
1949 if (chain->use == UINT_MAX)
1950 return -EOVERFLOW;
Patrick McHardy96518512013-10-14 11:00:02 +02001951 }
1952
Eric Leblond5e948462013-10-10 13:41:44 +02001953 if (nla[NFTA_RULE_POSITION]) {
1954 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1955 return -EOPNOTSUPP;
1956
1957 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1958 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1959 if (IS_ERR(old_rule))
1960 return PTR_ERR(old_rule);
1961 }
1962
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001963 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1964
Patrick McHardy96518512013-10-14 11:00:02 +02001965 n = 0;
1966 size = 0;
1967 if (nla[NFTA_RULE_EXPRESSIONS]) {
1968 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1969 err = -EINVAL;
1970 if (nla_type(tmp) != NFTA_LIST_ELEM)
1971 goto err1;
1972 if (n == NFT_RULE_MAXEXPRS)
1973 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001974 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02001975 if (err < 0)
1976 goto err1;
1977 size += info[n].ops->size;
1978 n++;
1979 }
1980 }
Patrick McHardy98898402015-03-03 20:04:19 +00001981 /* Check for overflow of dlen field */
1982 err = -EFBIG;
1983 if (size >= 1 << 12)
1984 goto err1;
Patrick McHardy96518512013-10-14 11:00:02 +02001985
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001986 if (nla[NFTA_RULE_USERDATA]) {
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001987 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001988 if (ulen > 0)
1989 usize = sizeof(struct nft_userdata) + ulen;
1990 }
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001991
Patrick McHardy96518512013-10-14 11:00:02 +02001992 err = -ENOMEM;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001993 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001994 if (rule == NULL)
1995 goto err1;
1996
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001997 nft_rule_activate_next(net, rule);
1998
Patrick McHardy96518512013-10-14 11:00:02 +02001999 rule->handle = handle;
2000 rule->dlen = size;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002001 rule->udata = ulen ? 1 : 0;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01002002
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002003 if (ulen) {
2004 udata = nft_userdata(rule);
2005 udata->len = ulen - 1;
2006 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
2007 }
Patrick McHardy96518512013-10-14 11:00:02 +02002008
Patrick McHardy96518512013-10-14 11:00:02 +02002009 expr = nft_expr_first(rule);
2010 for (i = 0; i < n; i++) {
2011 err = nf_tables_newexpr(&ctx, &info[i], expr);
2012 if (err < 0)
2013 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002014 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02002015 expr = nft_expr_next(expr);
2016 }
2017
Patrick McHardy96518512013-10-14 11:00:02 +02002018 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002019 if (nft_rule_is_active_next(net, old_rule)) {
Pablo Neira Ayusoac904ac2014-06-10 10:53:03 +02002020 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02002021 old_rule);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02002022 if (trans == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002023 err = -ENOMEM;
2024 goto err2;
2025 }
Arturo Borreroee01d542014-09-02 16:42:25 +02002026 nft_rule_deactivate_next(net, old_rule);
Pablo Neira Ayusoac34b862014-06-10 10:53:02 +02002027 chain->use--;
Pablo Neira Ayuso5bc5c302014-06-10 10:53:00 +02002028 list_add_tail_rcu(&rule->list, &old_rule->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002029 } else {
2030 err = -ENOENT;
2031 goto err2;
2032 }
Patrick McHardy96518512013-10-14 11:00:02 +02002033 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02002034 if (old_rule)
2035 list_add_rcu(&rule->list, &old_rule->list);
2036 else
2037 list_add_tail_rcu(&rule->list, &chain->rules);
2038 else {
2039 if (old_rule)
2040 list_add_tail_rcu(&rule->list, &old_rule->list);
2041 else
2042 list_add_rcu(&rule->list, &chain->rules);
2043 }
Patrick McHardy96518512013-10-14 11:00:02 +02002044
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02002045 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002046 err = -ENOMEM;
2047 goto err3;
2048 }
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02002049 chain->use++;
Patrick McHardy96518512013-10-14 11:00:02 +02002050 return 0;
2051
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002052err3:
2053 list_del_rcu(&rule->list);
Patrick McHardy96518512013-10-14 11:00:02 +02002054err2:
Patrick McHardy62472bc2014-03-07 19:08:30 +01002055 nf_tables_rule_destroy(&ctx, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02002056err1:
2057 for (i = 0; i < n; i++) {
2058 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002059 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02002060 }
2061 return err;
2062}
2063
2064static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
2065 const struct nlmsghdr *nlh,
2066 const struct nlattr * const nla[])
2067{
2068 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002069 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002070 struct net *net = sock_net(skb->sk);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002071 struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002072 struct nft_chain *chain = NULL;
2073 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002074 int family = nfmsg->nfgen_family, err = 0;
2075 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02002076
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002077 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02002078 if (IS_ERR(afi))
2079 return PTR_ERR(afi);
2080
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002081 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
Patrick McHardy96518512013-10-14 11:00:02 +02002082 if (IS_ERR(table))
2083 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002084 if (table->flags & NFT_TABLE_INACTIVE)
2085 return -ENOENT;
Patrick McHardy96518512013-10-14 11:00:02 +02002086
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002087 if (nla[NFTA_RULE_CHAIN]) {
2088 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
2089 if (IS_ERR(chain))
2090 return PTR_ERR(chain);
2091 }
Patrick McHardy96518512013-10-14 11:00:02 +02002092
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002093 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
2094
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002095 if (chain) {
2096 if (nla[NFTA_RULE_HANDLE]) {
2097 rule = nf_tables_rule_lookup(chain,
2098 nla[NFTA_RULE_HANDLE]);
2099 if (IS_ERR(rule))
2100 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02002101
Arturo Borrero5e266fe2014-09-02 16:42:21 +02002102 err = nft_delrule(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002103 } else {
Arturo Borreroce24b722014-09-02 16:42:24 +02002104 err = nft_delrule_by_chain(&ctx);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002105 }
2106 } else {
2107 list_for_each_entry(chain, &table->chains, list) {
2108 ctx.chain = chain;
Arturo Borreroce24b722014-09-02 16:42:24 +02002109 err = nft_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002110 if (err < 0)
2111 break;
Patrick McHardy96518512013-10-14 11:00:02 +02002112 }
2113 }
2114
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002115 return err;
2116}
2117
Patrick McHardy20a69342013-10-11 12:06:22 +02002118/*
2119 * Sets
2120 */
2121
2122static LIST_HEAD(nf_tables_set_ops);
2123
2124int nft_register_set(struct nft_set_ops *ops)
2125{
2126 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002127 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
Patrick McHardy20a69342013-10-11 12:06:22 +02002128 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2129 return 0;
2130}
2131EXPORT_SYMBOL_GPL(nft_register_set);
2132
2133void nft_unregister_set(struct nft_set_ops *ops)
2134{
2135 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002136 list_del_rcu(&ops->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002137 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2138}
2139EXPORT_SYMBOL_GPL(nft_unregister_set);
2140
Patrick McHardyc50b9602014-03-28 10:19:47 +00002141/*
2142 * Select a set implementation based on the data characteristics and the
2143 * given policy. The total memory use might not be known if no size is
2144 * given, in that case the amount of memory per element is used.
2145 */
2146static const struct nft_set_ops *
2147nft_select_set_ops(const struct nlattr * const nla[],
2148 const struct nft_set_desc *desc,
2149 enum nft_set_policies policy)
Patrick McHardy20a69342013-10-11 12:06:22 +02002150{
Patrick McHardyc50b9602014-03-28 10:19:47 +00002151 const struct nft_set_ops *ops, *bops;
2152 struct nft_set_estimate est, best;
Patrick McHardy20a69342013-10-11 12:06:22 +02002153 u32 features;
2154
2155#ifdef CONFIG_MODULES
2156 if (list_empty(&nf_tables_set_ops)) {
2157 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2158 request_module("nft-set");
2159 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2160 if (!list_empty(&nf_tables_set_ops))
2161 return ERR_PTR(-EAGAIN);
2162 }
2163#endif
2164 features = 0;
2165 if (nla[NFTA_SET_FLAGS] != NULL) {
2166 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2167 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
2168 }
2169
Patrick McHardyc50b9602014-03-28 10:19:47 +00002170 bops = NULL;
2171 best.size = ~0;
2172 best.class = ~0;
2173
Patrick McHardy20a69342013-10-11 12:06:22 +02002174 list_for_each_entry(ops, &nf_tables_set_ops, list) {
2175 if ((ops->features & features) != features)
2176 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002177 if (!ops->estimate(desc, features, &est))
2178 continue;
2179
2180 switch (policy) {
2181 case NFT_SET_POL_PERFORMANCE:
2182 if (est.class < best.class)
2183 break;
2184 if (est.class == best.class && est.size < best.size)
2185 break;
2186 continue;
2187 case NFT_SET_POL_MEMORY:
2188 if (est.size < best.size)
2189 break;
2190 if (est.size == best.size && est.class < best.class)
2191 break;
2192 continue;
2193 default:
2194 break;
2195 }
2196
Patrick McHardy20a69342013-10-11 12:06:22 +02002197 if (!try_module_get(ops->owner))
2198 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002199 if (bops != NULL)
2200 module_put(bops->owner);
2201
2202 bops = ops;
2203 best = est;
Patrick McHardy20a69342013-10-11 12:06:22 +02002204 }
2205
Patrick McHardyc50b9602014-03-28 10:19:47 +00002206 if (bops != NULL)
2207 return bops;
2208
Patrick McHardy20a69342013-10-11 12:06:22 +02002209 return ERR_PTR(-EOPNOTSUPP);
2210}
2211
2212static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2213 [NFTA_SET_TABLE] = { .type = NLA_STRING },
Pablo Neira Ayusoa9bdd832014-03-24 15:10:37 +01002214 [NFTA_SET_NAME] = { .type = NLA_STRING,
2215 .len = IFNAMSIZ - 1 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002216 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2217 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2218 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2219 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2220 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002221 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2222 [NFTA_SET_DESC] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002223 [NFTA_SET_ID] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002224};
2225
2226static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2227 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002228};
2229
2230static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2231 const struct sk_buff *skb,
2232 const struct nlmsghdr *nlh,
2233 const struct nlattr * const nla[])
2234{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002235 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002236 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002237 struct nft_af_info *afi = NULL;
2238 struct nft_table *table = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02002239
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002240 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2241 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2242 if (IS_ERR(afi))
2243 return PTR_ERR(afi);
2244 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002245
2246 if (nla[NFTA_SET_TABLE] != NULL) {
Patrick McHardyec2c9932014-02-05 15:03:35 +00002247 if (afi == NULL)
2248 return -EAFNOSUPPORT;
2249
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002250 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002251 if (IS_ERR(table))
2252 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002253 if (table->flags & NFT_TABLE_INACTIVE)
2254 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002255 }
2256
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002257 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002258 return 0;
2259}
2260
2261struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2262 const struct nlattr *nla)
2263{
2264 struct nft_set *set;
2265
2266 if (nla == NULL)
2267 return ERR_PTR(-EINVAL);
2268
2269 list_for_each_entry(set, &table->sets, list) {
2270 if (!nla_strcmp(nla, set->name))
2271 return set;
2272 }
2273 return ERR_PTR(-ENOENT);
2274}
2275
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002276struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2277 const struct nlattr *nla)
2278{
2279 struct nft_trans *trans;
2280 u32 id = ntohl(nla_get_be32(nla));
2281
2282 list_for_each_entry(trans, &net->nft.commit_list, list) {
2283 if (trans->msg_type == NFT_MSG_NEWSET &&
2284 id == nft_trans_set_id(trans))
2285 return nft_trans_set(trans);
2286 }
2287 return ERR_PTR(-ENOENT);
2288}
2289
Patrick McHardy20a69342013-10-11 12:06:22 +02002290static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2291 const char *name)
2292{
2293 const struct nft_set *i;
2294 const char *p;
2295 unsigned long *inuse;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002296 unsigned int n = 0, min = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002297
2298 p = strnchr(name, IFNAMSIZ, '%');
2299 if (p != NULL) {
2300 if (p[1] != 'd' || strchr(p + 2, '%'))
2301 return -EINVAL;
2302
2303 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2304 if (inuse == NULL)
2305 return -ENOMEM;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002306cont:
Patrick McHardy20a69342013-10-11 12:06:22 +02002307 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01002308 int tmp;
2309
2310 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02002311 continue;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002312 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02002313 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002314
Patrick McHardy60eb1892014-03-07 12:34:05 +01002315 set_bit(tmp - min, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02002316 }
2317
Patrick McHardy53b70282014-02-05 12:26:22 +01002318 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
Patrick McHardy60eb1892014-03-07 12:34:05 +01002319 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2320 min += BITS_PER_BYTE * PAGE_SIZE;
2321 memset(inuse, 0, PAGE_SIZE);
2322 goto cont;
2323 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002324 free_page((unsigned long)inuse);
2325 }
2326
Patrick McHardy60eb1892014-03-07 12:34:05 +01002327 snprintf(set->name, sizeof(set->name), name, min + n);
Patrick McHardy20a69342013-10-11 12:06:22 +02002328 list_for_each_entry(i, &ctx->table->sets, list) {
2329 if (!strcmp(set->name, i->name))
2330 return -ENFILE;
2331 }
2332 return 0;
2333}
2334
2335static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2336 const struct nft_set *set, u16 event, u16 flags)
2337{
2338 struct nfgenmsg *nfmsg;
2339 struct nlmsghdr *nlh;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002340 struct nlattr *desc;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002341 u32 portid = ctx->portid;
2342 u32 seq = ctx->seq;
Patrick McHardy20a69342013-10-11 12:06:22 +02002343
2344 event |= NFNL_SUBSYS_NFTABLES << 8;
2345 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2346 flags);
2347 if (nlh == NULL)
2348 goto nla_put_failure;
2349
2350 nfmsg = nlmsg_data(nlh);
2351 nfmsg->nfgen_family = ctx->afi->family;
2352 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02002353 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
Patrick McHardy20a69342013-10-11 12:06:22 +02002354
2355 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2356 goto nla_put_failure;
2357 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2358 goto nla_put_failure;
2359 if (set->flags != 0)
2360 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2361 goto nla_put_failure;
2362
2363 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2364 goto nla_put_failure;
2365 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2366 goto nla_put_failure;
2367 if (set->flags & NFT_SET_MAP) {
2368 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2369 goto nla_put_failure;
2370 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2371 goto nla_put_failure;
2372 }
2373
Arturo Borrero9363dc42014-09-23 13:30:41 +02002374 if (set->policy != NFT_SET_POL_PERFORMANCE) {
2375 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
2376 goto nla_put_failure;
2377 }
2378
Patrick McHardyc50b9602014-03-28 10:19:47 +00002379 desc = nla_nest_start(skb, NFTA_SET_DESC);
2380 if (desc == NULL)
2381 goto nla_put_failure;
2382 if (set->size &&
2383 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2384 goto nla_put_failure;
2385 nla_nest_end(skb, desc);
2386
Johannes Berg053c0952015-01-16 22:09:00 +01002387 nlmsg_end(skb, nlh);
2388 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002389
2390nla_put_failure:
2391 nlmsg_trim(skb, nlh);
2392 return -1;
2393}
2394
2395static int nf_tables_set_notify(const struct nft_ctx *ctx,
2396 const struct nft_set *set,
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002397 int event, gfp_t gfp_flags)
Patrick McHardy20a69342013-10-11 12:06:22 +02002398{
2399 struct sk_buff *skb;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002400 u32 portid = ctx->portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002401 int err;
2402
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002403 if (!ctx->report &&
2404 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002405 return 0;
2406
2407 err = -ENOBUFS;
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002408 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
Patrick McHardy20a69342013-10-11 12:06:22 +02002409 if (skb == NULL)
2410 goto err;
2411
2412 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2413 if (err < 0) {
2414 kfree_skb(skb);
2415 goto err;
2416 }
2417
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002418 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002419 ctx->report, gfp_flags);
Patrick McHardy20a69342013-10-11 12:06:22 +02002420err:
2421 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002422 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002423 return err;
2424}
2425
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002426static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002427{
2428 const struct nft_set *set;
2429 unsigned int idx, s_idx = cb->args[0];
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002430 struct nft_af_info *afi;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002431 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2432 struct net *net = sock_net(skb->sk);
2433 int cur_family = cb->args[3];
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002434 struct nft_ctx *ctx = cb->data, ctx_set;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002435
2436 if (cb->args[1])
2437 return skb->len;
2438
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002439 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02002440 cb->seq = net->nft.base_seq;
2441
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002442 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002443 if (ctx->afi && ctx->afi != afi)
2444 continue;
2445
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002446 if (cur_family) {
2447 if (afi->family != cur_family)
2448 continue;
2449
2450 cur_family = 0;
2451 }
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002452 list_for_each_entry_rcu(table, &afi->tables, list) {
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002453 if (ctx->table && ctx->table != table)
2454 continue;
2455
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002456 if (cur_table) {
2457 if (cur_table != table)
2458 continue;
2459
2460 cur_table = NULL;
2461 }
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002462 idx = 0;
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002463 list_for_each_entry_rcu(set, &table->sets, list) {
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002464 if (idx < s_idx)
2465 goto cont;
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002466
2467 ctx_set = *ctx;
2468 ctx_set.table = table;
2469 ctx_set.afi = afi;
2470 if (nf_tables_fill_set(skb, &ctx_set, set,
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002471 NFT_MSG_NEWSET,
2472 NLM_F_MULTI) < 0) {
2473 cb->args[0] = idx;
2474 cb->args[2] = (unsigned long) table;
2475 cb->args[3] = afi->family;
2476 goto done;
2477 }
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02002478 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002479cont:
2480 idx++;
2481 }
2482 if (s_idx)
2483 s_idx = 0;
2484 }
2485 }
2486 cb->args[1] = 1;
2487done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002488 rcu_read_unlock();
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002489 return skb->len;
2490}
2491
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002492static int nf_tables_dump_sets_done(struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002493{
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002494 kfree(cb->data);
2495 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002496}
2497
2498static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2499 const struct nlmsghdr *nlh,
2500 const struct nlattr * const nla[])
2501{
2502 const struct nft_set *set;
2503 struct nft_ctx ctx;
2504 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002505 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002506 int err;
2507
stephen hemminger01cfa0a2014-10-29 22:57:19 -07002508 /* Verify existence before starting dump */
Patrick McHardy20a69342013-10-11 12:06:22 +02002509 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2510 if (err < 0)
2511 return err;
2512
2513 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2514 struct netlink_dump_control c = {
2515 .dump = nf_tables_dump_sets,
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002516 .done = nf_tables_dump_sets_done,
Patrick McHardy20a69342013-10-11 12:06:22 +02002517 };
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002518 struct nft_ctx *ctx_dump;
2519
2520 ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2521 if (ctx_dump == NULL)
2522 return -ENOMEM;
2523
2524 *ctx_dump = ctx;
2525 c.data = ctx_dump;
2526
Patrick McHardy20a69342013-10-11 12:06:22 +02002527 return netlink_dump_start(nlsk, skb, nlh, &c);
2528 }
2529
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002530 /* Only accept unspec with dump */
2531 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2532 return -EAFNOSUPPORT;
2533
Patrick McHardy20a69342013-10-11 12:06:22 +02002534 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2535 if (IS_ERR(set))
2536 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002537 if (set->flags & NFT_SET_INACTIVE)
2538 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002539
2540 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2541 if (skb2 == NULL)
2542 return -ENOMEM;
2543
2544 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2545 if (err < 0)
2546 goto err;
2547
2548 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2549
2550err:
2551 kfree_skb(skb2);
2552 return err;
2553}
2554
Patrick McHardyc50b9602014-03-28 10:19:47 +00002555static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2556 struct nft_set_desc *desc,
2557 const struct nlattr *nla)
2558{
2559 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2560 int err;
2561
2562 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2563 if (err < 0)
2564 return err;
2565
2566 if (da[NFTA_SET_DESC_SIZE] != NULL)
2567 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2568
2569 return 0;
2570}
2571
Patrick McHardy20a69342013-10-11 12:06:22 +02002572static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2573 const struct nlmsghdr *nlh,
2574 const struct nlattr * const nla[])
2575{
2576 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2577 const struct nft_set_ops *ops;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002578 struct nft_af_info *afi;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002579 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002580 struct nft_table *table;
2581 struct nft_set *set;
2582 struct nft_ctx ctx;
2583 char name[IFNAMSIZ];
2584 unsigned int size;
2585 bool create;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002586 u32 ktype, dtype, flags, policy;
2587 struct nft_set_desc desc;
Patrick McHardy20a69342013-10-11 12:06:22 +02002588 int err;
2589
2590 if (nla[NFTA_SET_TABLE] == NULL ||
2591 nla[NFTA_SET_NAME] == NULL ||
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002592 nla[NFTA_SET_KEY_LEN] == NULL ||
2593 nla[NFTA_SET_ID] == NULL)
Patrick McHardy20a69342013-10-11 12:06:22 +02002594 return -EINVAL;
2595
Patrick McHardyc50b9602014-03-28 10:19:47 +00002596 memset(&desc, 0, sizeof(desc));
2597
Patrick McHardy20a69342013-10-11 12:06:22 +02002598 ktype = NFT_DATA_VALUE;
2599 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2600 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2601 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2602 return -EINVAL;
2603 }
2604
Patrick McHardyc50b9602014-03-28 10:19:47 +00002605 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2606 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002607 return -EINVAL;
2608
2609 flags = 0;
2610 if (nla[NFTA_SET_FLAGS] != NULL) {
2611 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2612 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2613 NFT_SET_INTERVAL | NFT_SET_MAP))
2614 return -EINVAL;
2615 }
2616
2617 dtype = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002618 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2619 if (!(flags & NFT_SET_MAP))
2620 return -EINVAL;
2621
2622 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2623 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2624 dtype != NFT_DATA_VERDICT)
2625 return -EINVAL;
2626
2627 if (dtype != NFT_DATA_VERDICT) {
2628 if (nla[NFTA_SET_DATA_LEN] == NULL)
2629 return -EINVAL;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002630 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2631 if (desc.dlen == 0 ||
2632 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
Patrick McHardy20a69342013-10-11 12:06:22 +02002633 return -EINVAL;
2634 } else
Patrick McHardyc50b9602014-03-28 10:19:47 +00002635 desc.dlen = sizeof(struct nft_data);
Patrick McHardy20a69342013-10-11 12:06:22 +02002636 } else if (flags & NFT_SET_MAP)
2637 return -EINVAL;
2638
Patrick McHardyc50b9602014-03-28 10:19:47 +00002639 policy = NFT_SET_POL_PERFORMANCE;
2640 if (nla[NFTA_SET_POLICY] != NULL)
2641 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2642
2643 if (nla[NFTA_SET_DESC] != NULL) {
2644 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2645 if (err < 0)
2646 return err;
2647 }
2648
Patrick McHardy20a69342013-10-11 12:06:22 +02002649 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2650
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002651 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002652 if (IS_ERR(afi))
2653 return PTR_ERR(afi);
2654
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002655 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002656 if (IS_ERR(table))
2657 return PTR_ERR(table);
2658
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002659 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002660
2661 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2662 if (IS_ERR(set)) {
2663 if (PTR_ERR(set) != -ENOENT)
2664 return PTR_ERR(set);
2665 set = NULL;
2666 }
2667
2668 if (set != NULL) {
2669 if (nlh->nlmsg_flags & NLM_F_EXCL)
2670 return -EEXIST;
2671 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2672 return -EOPNOTSUPP;
2673 return 0;
2674 }
2675
2676 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2677 return -ENOENT;
2678
Patrick McHardyc50b9602014-03-28 10:19:47 +00002679 ops = nft_select_set_ops(nla, &desc, policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002680 if (IS_ERR(ops))
2681 return PTR_ERR(ops);
2682
2683 size = 0;
2684 if (ops->privsize != NULL)
2685 size = ops->privsize(nla);
2686
2687 err = -ENOMEM;
2688 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2689 if (set == NULL)
2690 goto err1;
2691
2692 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2693 err = nf_tables_set_alloc_name(&ctx, set, name);
2694 if (err < 0)
2695 goto err2;
2696
2697 INIT_LIST_HEAD(&set->bindings);
2698 set->ops = ops;
2699 set->ktype = ktype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002700 set->klen = desc.klen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002701 set->dtype = dtype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002702 set->dlen = desc.dlen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002703 set->flags = flags;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002704 set->size = desc.size;
Arturo Borrero9363dc42014-09-23 13:30:41 +02002705 set->policy = policy;
Patrick McHardy20a69342013-10-11 12:06:22 +02002706
Patrick McHardyc50b9602014-03-28 10:19:47 +00002707 err = ops->init(set, &desc, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002708 if (err < 0)
2709 goto err2;
2710
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002711 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002712 if (err < 0)
2713 goto err2;
2714
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002715 list_add_tail_rcu(&set->list, &table->sets);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02002716 table->use++;
Patrick McHardy20a69342013-10-11 12:06:22 +02002717 return 0;
2718
2719err2:
2720 kfree(set);
2721err1:
2722 module_put(ops->owner);
2723 return err;
2724}
2725
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002726static void nft_set_destroy(struct nft_set *set)
2727{
2728 set->ops->destroy(set);
2729 module_put(set->ops->owner);
2730 kfree(set);
2731}
2732
Patrick McHardy20a69342013-10-11 12:06:22 +02002733static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2734{
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002735 list_del_rcu(&set->list);
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002736 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002737 nft_set_destroy(set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002738}
2739
2740static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2741 const struct nlmsghdr *nlh,
2742 const struct nlattr * const nla[])
2743{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002744 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002745 struct nft_set *set;
2746 struct nft_ctx ctx;
2747 int err;
2748
Patrick McHardyec2c9932014-02-05 15:03:35 +00002749 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2750 return -EAFNOSUPPORT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002751 if (nla[NFTA_SET_TABLE] == NULL)
2752 return -EINVAL;
2753
2754 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2755 if (err < 0)
2756 return err;
2757
2758 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2759 if (IS_ERR(set))
2760 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002761 if (set->flags & NFT_SET_INACTIVE)
2762 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002763 if (!list_empty(&set->bindings))
2764 return -EBUSY;
2765
Arturo Borreroee01d542014-09-02 16:42:25 +02002766 return nft_delset(&ctx, set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002767}
2768
2769static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2770 const struct nft_set *set,
2771 const struct nft_set_iter *iter,
2772 const struct nft_set_elem *elem)
2773{
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002774 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02002775 enum nft_registers dreg;
2776
2777 dreg = nft_type_to_reg(set->dtype);
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002778 return nft_validate_data_load(ctx, dreg, nft_set_ext_data(ext),
Pablo Neira Ayuso2ee0d3c2013-12-28 00:59:38 +01002779 set->dtype == NFT_DATA_VERDICT ?
2780 NFT_DATA_VERDICT : NFT_DATA_VALUE);
Patrick McHardy20a69342013-10-11 12:06:22 +02002781}
2782
2783int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2784 struct nft_set_binding *binding)
2785{
2786 struct nft_set_binding *i;
2787 struct nft_set_iter iter;
2788
2789 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2790 return -EBUSY;
2791
2792 if (set->flags & NFT_SET_MAP) {
2793 /* If the set is already bound to the same chain all
2794 * jumps are already validated for that chain.
2795 */
2796 list_for_each_entry(i, &set->bindings, list) {
2797 if (i->chain == binding->chain)
2798 goto bind;
2799 }
2800
2801 iter.skip = 0;
2802 iter.count = 0;
2803 iter.err = 0;
2804 iter.fn = nf_tables_bind_check_setelem;
2805
2806 set->ops->walk(ctx, set, &iter);
2807 if (iter.err < 0) {
2808 /* Destroy anonymous sets if binding fails */
2809 if (set->flags & NFT_SET_ANONYMOUS)
2810 nf_tables_set_destroy(ctx, set);
2811
2812 return iter.err;
2813 }
2814 }
2815bind:
2816 binding->chain = ctx->chain;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002817 list_add_tail_rcu(&binding->list, &set->bindings);
Patrick McHardy20a69342013-10-11 12:06:22 +02002818 return 0;
2819}
2820
2821void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2822 struct nft_set_binding *binding)
2823{
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002824 list_del_rcu(&binding->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002825
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002826 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2827 !(set->flags & NFT_SET_INACTIVE))
Patrick McHardy20a69342013-10-11 12:06:22 +02002828 nf_tables_set_destroy(ctx, set);
2829}
2830
Patrick McHardy3ac4c072015-03-25 13:07:49 +00002831const struct nft_set_ext_type nft_set_ext_types[] = {
2832 [NFT_SET_EXT_KEY] = {
2833 .len = sizeof(struct nft_data),
2834 .align = __alignof__(struct nft_data),
2835 },
2836 [NFT_SET_EXT_DATA] = {
2837 .len = sizeof(struct nft_data),
2838 .align = __alignof__(struct nft_data),
2839 },
2840 [NFT_SET_EXT_FLAGS] = {
2841 .len = sizeof(u8),
2842 .align = __alignof__(u8),
2843 },
2844};
2845EXPORT_SYMBOL_GPL(nft_set_ext_types);
2846
Patrick McHardy20a69342013-10-11 12:06:22 +02002847/*
2848 * Set elements
2849 */
2850
2851static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2852 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2853 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2854 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2855};
2856
2857static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2858 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2859 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2860 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002861 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002862};
2863
2864static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2865 const struct sk_buff *skb,
2866 const struct nlmsghdr *nlh,
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002867 const struct nlattr * const nla[],
2868 bool trans)
Patrick McHardy20a69342013-10-11 12:06:22 +02002869{
2870 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002871 struct nft_af_info *afi;
2872 struct nft_table *table;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002873 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02002874
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002875 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002876 if (IS_ERR(afi))
2877 return PTR_ERR(afi);
2878
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02002879 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
Patrick McHardy20a69342013-10-11 12:06:22 +02002880 if (IS_ERR(table))
2881 return PTR_ERR(table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002882 if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2883 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002884
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002885 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002886 return 0;
2887}
2888
2889static int nf_tables_fill_setelem(struct sk_buff *skb,
2890 const struct nft_set *set,
2891 const struct nft_set_elem *elem)
2892{
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002893 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02002894 unsigned char *b = skb_tail_pointer(skb);
2895 struct nlattr *nest;
2896
2897 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2898 if (nest == NULL)
2899 goto nla_put_failure;
2900
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002901 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
2902 NFT_DATA_VALUE, set->klen) < 0)
Patrick McHardy20a69342013-10-11 12:06:22 +02002903 goto nla_put_failure;
2904
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002905 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
2906 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
Patrick McHardy20a69342013-10-11 12:06:22 +02002907 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2908 set->dlen) < 0)
2909 goto nla_put_failure;
2910
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002911 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
2912 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
2913 htonl(*nft_set_ext_flags(ext))))
2914 goto nla_put_failure;
Patrick McHardy20a69342013-10-11 12:06:22 +02002915
2916 nla_nest_end(skb, nest);
2917 return 0;
2918
2919nla_put_failure:
2920 nlmsg_trim(skb, b);
2921 return -EMSGSIZE;
2922}
2923
2924struct nft_set_dump_args {
2925 const struct netlink_callback *cb;
2926 struct nft_set_iter iter;
2927 struct sk_buff *skb;
2928};
2929
2930static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2931 const struct nft_set *set,
2932 const struct nft_set_iter *iter,
2933 const struct nft_set_elem *elem)
2934{
2935 struct nft_set_dump_args *args;
2936
2937 args = container_of(iter, struct nft_set_dump_args, iter);
2938 return nf_tables_fill_setelem(args->skb, set, elem);
2939}
2940
2941static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2942{
2943 const struct nft_set *set;
2944 struct nft_set_dump_args args;
2945 struct nft_ctx ctx;
2946 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2947 struct nfgenmsg *nfmsg;
2948 struct nlmsghdr *nlh;
2949 struct nlattr *nest;
2950 u32 portid, seq;
2951 int event, err;
2952
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01002953 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2954 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002955 if (err < 0)
2956 return err;
2957
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02002958 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2959 false);
Patrick McHardy20a69342013-10-11 12:06:22 +02002960 if (err < 0)
2961 return err;
2962
2963 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2964 if (IS_ERR(set))
2965 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002966 if (set->flags & NFT_SET_INACTIVE)
2967 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002968
2969 event = NFT_MSG_NEWSETELEM;
2970 event |= NFNL_SUBSYS_NFTABLES << 8;
2971 portid = NETLINK_CB(cb->skb).portid;
2972 seq = cb->nlh->nlmsg_seq;
2973
2974 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2975 NLM_F_MULTI);
2976 if (nlh == NULL)
2977 goto nla_put_failure;
2978
2979 nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso6403d962014-06-11 19:05:28 +02002980 nfmsg->nfgen_family = ctx.afi->family;
Patrick McHardy20a69342013-10-11 12:06:22 +02002981 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02002982 nfmsg->res_id = htons(ctx.net->nft.base_seq & 0xffff);
Patrick McHardy20a69342013-10-11 12:06:22 +02002983
2984 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2985 goto nla_put_failure;
2986 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2987 goto nla_put_failure;
2988
2989 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2990 if (nest == NULL)
2991 goto nla_put_failure;
2992
2993 args.cb = cb;
2994 args.skb = skb;
2995 args.iter.skip = cb->args[0];
2996 args.iter.count = 0;
2997 args.iter.err = 0;
2998 args.iter.fn = nf_tables_dump_setelem;
2999 set->ops->walk(&ctx, set, &args.iter);
3000
3001 nla_nest_end(skb, nest);
3002 nlmsg_end(skb, nlh);
3003
3004 if (args.iter.err && args.iter.err != -EMSGSIZE)
3005 return args.iter.err;
3006 if (args.iter.count == cb->args[0])
3007 return 0;
3008
3009 cb->args[0] = args.iter.count;
3010 return skb->len;
3011
3012nla_put_failure:
3013 return -ENOSPC;
3014}
3015
3016static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
3017 const struct nlmsghdr *nlh,
3018 const struct nlattr * const nla[])
3019{
3020 const struct nft_set *set;
3021 struct nft_ctx ctx;
3022 int err;
3023
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003024 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02003025 if (err < 0)
3026 return err;
3027
3028 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3029 if (IS_ERR(set))
3030 return PTR_ERR(set);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003031 if (set->flags & NFT_SET_INACTIVE)
3032 return -ENOENT;
Patrick McHardy20a69342013-10-11 12:06:22 +02003033
3034 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3035 struct netlink_dump_control c = {
3036 .dump = nf_tables_dump_set,
3037 };
3038 return netlink_dump_start(nlsk, skb, nlh, &c);
3039 }
3040 return -EOPNOTSUPP;
3041}
3042
Arturo Borrerod60ce622014-04-01 14:06:07 +02003043static int nf_tables_fill_setelem_info(struct sk_buff *skb,
3044 const struct nft_ctx *ctx, u32 seq,
3045 u32 portid, int event, u16 flags,
3046 const struct nft_set *set,
3047 const struct nft_set_elem *elem)
3048{
3049 struct nfgenmsg *nfmsg;
3050 struct nlmsghdr *nlh;
3051 struct nlattr *nest;
3052 int err;
3053
3054 event |= NFNL_SUBSYS_NFTABLES << 8;
3055 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3056 flags);
3057 if (nlh == NULL)
3058 goto nla_put_failure;
3059
3060 nfmsg = nlmsg_data(nlh);
3061 nfmsg->nfgen_family = ctx->afi->family;
3062 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003063 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
Arturo Borrerod60ce622014-04-01 14:06:07 +02003064
3065 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3066 goto nla_put_failure;
3067 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3068 goto nla_put_failure;
3069
3070 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3071 if (nest == NULL)
3072 goto nla_put_failure;
3073
3074 err = nf_tables_fill_setelem(skb, set, elem);
3075 if (err < 0)
3076 goto nla_put_failure;
3077
3078 nla_nest_end(skb, nest);
3079
Johannes Berg053c0952015-01-16 22:09:00 +01003080 nlmsg_end(skb, nlh);
3081 return 0;
Arturo Borrerod60ce622014-04-01 14:06:07 +02003082
3083nla_put_failure:
3084 nlmsg_trim(skb, nlh);
3085 return -1;
3086}
3087
3088static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
3089 const struct nft_set *set,
3090 const struct nft_set_elem *elem,
3091 int event, u16 flags)
3092{
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003093 struct net *net = ctx->net;
3094 u32 portid = ctx->portid;
Arturo Borrerod60ce622014-04-01 14:06:07 +02003095 struct sk_buff *skb;
3096 int err;
3097
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003098 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
Arturo Borrerod60ce622014-04-01 14:06:07 +02003099 return 0;
3100
3101 err = -ENOBUFS;
3102 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3103 if (skb == NULL)
3104 goto err;
3105
3106 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3107 set, elem);
3108 if (err < 0) {
3109 kfree_skb(skb);
3110 goto err;
3111 }
3112
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003113 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
Arturo Borrerod60ce622014-04-01 14:06:07 +02003114 GFP_KERNEL);
3115err:
3116 if (err < 0)
3117 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3118 return err;
3119}
3120
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003121static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3122 int msg_type,
3123 struct nft_set *set)
3124{
3125 struct nft_trans *trans;
3126
3127 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3128 if (trans == NULL)
3129 return NULL;
3130
3131 nft_trans_elem_set(trans) = set;
3132 return trans;
3133}
3134
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003135static void *nft_set_elem_init(const struct nft_set *set,
3136 const struct nft_set_ext_tmpl *tmpl,
3137 const struct nft_data *key,
3138 const struct nft_data *data,
3139 gfp_t gfp)
3140{
3141 struct nft_set_ext *ext;
3142 void *elem;
3143
3144 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
3145 if (elem == NULL)
3146 return NULL;
3147
3148 ext = nft_set_elem_ext(set, elem);
3149 nft_set_ext_init(ext, tmpl);
3150
3151 memcpy(nft_set_ext_key(ext), key, set->klen);
3152 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3153 memcpy(nft_set_ext_data(ext), data, set->dlen);
3154
3155 return elem;
3156}
3157
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003158static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +02003159 const struct nlattr *attr)
3160{
3161 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3162 struct nft_data_desc d1, d2;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003163 struct nft_set_ext_tmpl tmpl;
3164 struct nft_set_ext *ext;
Patrick McHardy20a69342013-10-11 12:06:22 +02003165 struct nft_set_elem elem;
3166 struct nft_set_binding *binding;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003167 struct nft_data data;
Patrick McHardy20a69342013-10-11 12:06:22 +02003168 enum nft_registers dreg;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003169 struct nft_trans *trans;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003170 u32 flags;
Patrick McHardy20a69342013-10-11 12:06:22 +02003171 int err;
3172
Patrick McHardyc50b9602014-03-28 10:19:47 +00003173 if (set->size && set->nelems == set->size)
3174 return -ENFILE;
3175
Patrick McHardy20a69342013-10-11 12:06:22 +02003176 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3177 nft_set_elem_policy);
3178 if (err < 0)
3179 return err;
3180
3181 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3182 return -EINVAL;
3183
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003184 nft_set_ext_prepare(&tmpl);
3185
3186 flags = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003187 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003188 flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3189 if (flags & ~NFT_SET_ELEM_INTERVAL_END)
Patrick McHardy20a69342013-10-11 12:06:22 +02003190 return -EINVAL;
Patrick McHardy55df35d2015-03-21 15:19:16 +00003191 if (!(set->flags & NFT_SET_INTERVAL) &&
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003192 flags & NFT_SET_ELEM_INTERVAL_END)
Patrick McHardy55df35d2015-03-21 15:19:16 +00003193 return -EINVAL;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003194 if (flags != 0)
3195 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
Patrick McHardy20a69342013-10-11 12:06:22 +02003196 }
3197
3198 if (set->flags & NFT_SET_MAP) {
3199 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003200 !(flags & NFT_SET_ELEM_INTERVAL_END))
Patrick McHardy20a69342013-10-11 12:06:22 +02003201 return -EINVAL;
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01003202 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003203 flags & NFT_SET_ELEM_INTERVAL_END)
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01003204 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003205 } else {
3206 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3207 return -EINVAL;
3208 }
3209
3210 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3211 if (err < 0)
3212 goto err1;
3213 err = -EINVAL;
3214 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3215 goto err2;
3216
3217 err = -EEXIST;
3218 if (set->ops->get(set, &elem) == 0)
3219 goto err2;
3220
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003221 nft_set_ext_add(&tmpl, NFT_SET_EXT_KEY);
3222
Patrick McHardy20a69342013-10-11 12:06:22 +02003223 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003224 err = nft_data_init(ctx, &data, &d2, nla[NFTA_SET_ELEM_DATA]);
Patrick McHardy20a69342013-10-11 12:06:22 +02003225 if (err < 0)
3226 goto err2;
3227
3228 err = -EINVAL;
3229 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3230 goto err3;
3231
3232 dreg = nft_type_to_reg(set->dtype);
3233 list_for_each_entry(binding, &set->bindings, list) {
3234 struct nft_ctx bind_ctx = {
3235 .afi = ctx->afi,
3236 .table = ctx->table,
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02003237 .chain = (struct nft_chain *)binding->chain,
Patrick McHardy20a69342013-10-11 12:06:22 +02003238 };
3239
3240 err = nft_validate_data_load(&bind_ctx, dreg,
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003241 &data, d2.type);
Patrick McHardy20a69342013-10-11 12:06:22 +02003242 if (err < 0)
3243 goto err3;
3244 }
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003245
3246 nft_set_ext_add(&tmpl, NFT_SET_EXT_DATA);
Patrick McHardy20a69342013-10-11 12:06:22 +02003247 }
3248
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003249 err = -ENOMEM;
3250 elem.priv = nft_set_elem_init(set, &tmpl, &elem.key, &data, GFP_KERNEL);
3251 if (elem.priv == NULL)
3252 goto err3;
3253
3254 ext = nft_set_elem_ext(set, elem.priv);
3255 if (flags)
3256 *nft_set_ext_flags(ext) = flags;
3257
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003258 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3259 if (trans == NULL)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003260 goto err4;
Patrick McHardy20a69342013-10-11 12:06:22 +02003261
Patrick McHardy20a69342013-10-11 12:06:22 +02003262 err = set->ops->insert(set, &elem);
3263 if (err < 0)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003264 goto err5;
Patrick McHardy20a69342013-10-11 12:06:22 +02003265
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003266 nft_trans_elem(trans) = elem;
Pablo Neira Ayuso46bbafc2014-05-22 12:36:03 +02003267 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Patrick McHardy20a69342013-10-11 12:06:22 +02003268 return 0;
3269
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003270err5:
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003271 kfree(trans);
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003272err4:
3273 kfree(elem.priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02003274err3:
3275 if (nla[NFTA_SET_ELEM_DATA] != NULL)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003276 nft_data_uninit(&data, d2.type);
Patrick McHardy20a69342013-10-11 12:06:22 +02003277err2:
3278 nft_data_uninit(&elem.key, d1.type);
3279err1:
3280 return err;
3281}
3282
3283static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3284 const struct nlmsghdr *nlh,
3285 const struct nlattr * const nla[])
3286{
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003287 struct net *net = sock_net(skb->sk);
Patrick McHardy20a69342013-10-11 12:06:22 +02003288 const struct nlattr *attr;
3289 struct nft_set *set;
3290 struct nft_ctx ctx;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003291 int rem, err = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003292
Pablo Neira Ayuso7d5570c2014-07-25 13:15:36 +02003293 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3294 return -EINVAL;
3295
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003296 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
Patrick McHardy20a69342013-10-11 12:06:22 +02003297 if (err < 0)
3298 return err;
3299
3300 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003301 if (IS_ERR(set)) {
3302 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3303 set = nf_tables_set_lookup_byid(net,
3304 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3305 }
3306 if (IS_ERR(set))
3307 return PTR_ERR(set);
3308 }
3309
Patrick McHardy20a69342013-10-11 12:06:22 +02003310 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3311 return -EBUSY;
3312
3313 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3314 err = nft_add_set_elem(&ctx, set, attr);
3315 if (err < 0)
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003316 break;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003317
3318 set->nelems++;
Patrick McHardy20a69342013-10-11 12:06:22 +02003319 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003320 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +02003321}
3322
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003323static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +02003324 const struct nlattr *attr)
3325{
3326 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3327 struct nft_data_desc desc;
3328 struct nft_set_elem elem;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003329 struct nft_trans *trans;
Patrick McHardy20a69342013-10-11 12:06:22 +02003330 int err;
3331
3332 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3333 nft_set_elem_policy);
3334 if (err < 0)
3335 goto err1;
3336
3337 err = -EINVAL;
3338 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3339 goto err1;
3340
3341 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3342 if (err < 0)
3343 goto err1;
3344
3345 err = -EINVAL;
3346 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3347 goto err2;
3348
3349 err = set->ops->get(set, &elem);
3350 if (err < 0)
3351 goto err2;
3352
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003353 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
Julia Lawall609ccf02014-08-07 14:49:08 +02003354 if (trans == NULL) {
3355 err = -ENOMEM;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003356 goto err2;
Julia Lawall609ccf02014-08-07 14:49:08 +02003357 }
Patrick McHardy20a69342013-10-11 12:06:22 +02003358
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003359 nft_trans_elem(trans) = elem;
Pablo Neira Ayuso46bbafc2014-05-22 12:36:03 +02003360 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Thomas Graf0dc13622014-08-01 17:25:38 +02003361 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003362err2:
3363 nft_data_uninit(&elem.key, desc.type);
3364err1:
3365 return err;
3366}
3367
3368static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3369 const struct nlmsghdr *nlh,
3370 const struct nlattr * const nla[])
3371{
3372 const struct nlattr *attr;
3373 struct nft_set *set;
3374 struct nft_ctx ctx;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003375 int rem, err = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003376
Pablo Neira Ayuso7d5570c2014-07-25 13:15:36 +02003377 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3378 return -EINVAL;
3379
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003380 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02003381 if (err < 0)
3382 return err;
3383
3384 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3385 if (IS_ERR(set))
3386 return PTR_ERR(set);
3387 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3388 return -EBUSY;
3389
3390 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3391 err = nft_del_setelem(&ctx, set, attr);
3392 if (err < 0)
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003393 break;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003394
3395 set->nelems--;
Patrick McHardy20a69342013-10-11 12:06:22 +02003396 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003397 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +02003398}
3399
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003400static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
3401 u32 portid, u32 seq)
3402{
3403 struct nlmsghdr *nlh;
3404 struct nfgenmsg *nfmsg;
3405 int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN;
3406
3407 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
3408 if (nlh == NULL)
3409 goto nla_put_failure;
3410
3411 nfmsg = nlmsg_data(nlh);
3412 nfmsg->nfgen_family = AF_UNSPEC;
3413 nfmsg->version = NFNETLINK_V0;
3414 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
3415
3416 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)))
3417 goto nla_put_failure;
3418
Johannes Berg053c0952015-01-16 22:09:00 +01003419 nlmsg_end(skb, nlh);
3420 return 0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003421
3422nla_put_failure:
3423 nlmsg_trim(skb, nlh);
3424 return -EMSGSIZE;
3425}
3426
3427static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
3428{
3429 struct nlmsghdr *nlh = nlmsg_hdr(skb);
3430 struct sk_buff *skb2;
3431 int err;
3432
3433 if (nlmsg_report(nlh) &&
3434 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3435 return 0;
3436
3437 err = -ENOBUFS;
3438 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3439 if (skb2 == NULL)
3440 goto err;
3441
3442 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3443 nlh->nlmsg_seq);
3444 if (err < 0) {
3445 kfree_skb(skb2);
3446 goto err;
3447 }
3448
3449 err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
3450 NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
3451err:
3452 if (err < 0) {
3453 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
3454 err);
3455 }
3456 return err;
3457}
3458
3459static int nf_tables_getgen(struct sock *nlsk, struct sk_buff *skb,
3460 const struct nlmsghdr *nlh,
3461 const struct nlattr * const nla[])
3462{
3463 struct net *net = sock_net(skb->sk);
3464 struct sk_buff *skb2;
3465 int err;
3466
3467 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3468 if (skb2 == NULL)
3469 return -ENOMEM;
3470
3471 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3472 nlh->nlmsg_seq);
3473 if (err < 0)
3474 goto err;
3475
3476 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3477err:
3478 kfree_skb(skb2);
3479 return err;
3480}
3481
Patrick McHardy96518512013-10-14 11:00:02 +02003482static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3483 [NFT_MSG_NEWTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003484 .call_batch = nf_tables_newtable,
Patrick McHardy96518512013-10-14 11:00:02 +02003485 .attr_count = NFTA_TABLE_MAX,
3486 .policy = nft_table_policy,
3487 },
3488 [NFT_MSG_GETTABLE] = {
3489 .call = nf_tables_gettable,
3490 .attr_count = NFTA_TABLE_MAX,
3491 .policy = nft_table_policy,
3492 },
3493 [NFT_MSG_DELTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003494 .call_batch = nf_tables_deltable,
Patrick McHardy96518512013-10-14 11:00:02 +02003495 .attr_count = NFTA_TABLE_MAX,
3496 .policy = nft_table_policy,
3497 },
3498 [NFT_MSG_NEWCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003499 .call_batch = nf_tables_newchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003500 .attr_count = NFTA_CHAIN_MAX,
3501 .policy = nft_chain_policy,
3502 },
3503 [NFT_MSG_GETCHAIN] = {
3504 .call = nf_tables_getchain,
3505 .attr_count = NFTA_CHAIN_MAX,
3506 .policy = nft_chain_policy,
3507 },
3508 [NFT_MSG_DELCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003509 .call_batch = nf_tables_delchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003510 .attr_count = NFTA_CHAIN_MAX,
3511 .policy = nft_chain_policy,
3512 },
3513 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003514 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003515 .attr_count = NFTA_RULE_MAX,
3516 .policy = nft_rule_policy,
3517 },
3518 [NFT_MSG_GETRULE] = {
3519 .call = nf_tables_getrule,
3520 .attr_count = NFTA_RULE_MAX,
3521 .policy = nft_rule_policy,
3522 },
3523 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003524 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003525 .attr_count = NFTA_RULE_MAX,
3526 .policy = nft_rule_policy,
3527 },
Patrick McHardy20a69342013-10-11 12:06:22 +02003528 [NFT_MSG_NEWSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003529 .call_batch = nf_tables_newset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003530 .attr_count = NFTA_SET_MAX,
3531 .policy = nft_set_policy,
3532 },
3533 [NFT_MSG_GETSET] = {
3534 .call = nf_tables_getset,
3535 .attr_count = NFTA_SET_MAX,
3536 .policy = nft_set_policy,
3537 },
3538 [NFT_MSG_DELSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003539 .call_batch = nf_tables_delset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003540 .attr_count = NFTA_SET_MAX,
3541 .policy = nft_set_policy,
3542 },
3543 [NFT_MSG_NEWSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003544 .call_batch = nf_tables_newsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003545 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3546 .policy = nft_set_elem_list_policy,
3547 },
3548 [NFT_MSG_GETSETELEM] = {
3549 .call = nf_tables_getsetelem,
3550 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3551 .policy = nft_set_elem_list_policy,
3552 },
3553 [NFT_MSG_DELSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003554 .call_batch = nf_tables_delsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003555 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3556 .policy = nft_set_elem_list_policy,
3557 },
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003558 [NFT_MSG_GETGEN] = {
3559 .call = nf_tables_getgen,
3560 },
Patrick McHardy96518512013-10-14 11:00:02 +02003561};
3562
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003563static void nft_chain_commit_update(struct nft_trans *trans)
3564{
3565 struct nft_base_chain *basechain;
3566
3567 if (nft_trans_chain_name(trans)[0])
3568 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3569
3570 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3571 return;
3572
3573 basechain = nft_base_chain(trans->ctx.chain);
3574 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3575
3576 switch (nft_trans_chain_policy(trans)) {
3577 case NF_DROP:
3578 case NF_ACCEPT:
3579 basechain->policy = nft_trans_chain_policy(trans);
3580 break;
3581 }
3582}
3583
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003584static void nf_tables_commit_release(struct nft_trans *trans)
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003585{
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003586 switch (trans->msg_type) {
3587 case NFT_MSG_DELTABLE:
3588 nf_tables_table_destroy(&trans->ctx);
3589 break;
3590 case NFT_MSG_DELCHAIN:
3591 nf_tables_chain_destroy(trans->ctx.chain);
3592 break;
3593 case NFT_MSG_DELRULE:
3594 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3595 break;
3596 case NFT_MSG_DELSET:
3597 nft_set_destroy(nft_trans_set(trans));
3598 break;
3599 }
3600 kfree(trans);
3601}
3602
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003603static int nf_tables_commit(struct sk_buff *skb)
3604{
3605 struct net *net = sock_net(skb->sk);
3606 struct nft_trans *trans, *next;
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003607 struct nft_trans_elem *te;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003608 struct nft_set_ext *ext;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003609
3610 /* Bump generation counter, invalidate any dump in progress */
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02003611 while (++net->nft.base_seq == 0);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003612
3613 /* A new generation has just started */
3614 net->nft.gencursor = gencursor_next(net);
3615
3616 /* Make sure all packets have left the previous generation before
3617 * purging old rules.
3618 */
3619 synchronize_rcu();
3620
3621 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003622 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003623 case NFT_MSG_NEWTABLE:
3624 if (nft_trans_table_update(trans)) {
3625 if (!nft_trans_table_enable(trans)) {
3626 nf_tables_table_disable(trans->ctx.afi,
3627 trans->ctx.table);
3628 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3629 }
3630 } else {
3631 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3632 }
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003633 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003634 nft_trans_destroy(trans);
3635 break;
3636 case NFT_MSG_DELTABLE:
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003637 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003638 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003639 case NFT_MSG_NEWCHAIN:
3640 if (nft_trans_chain_update(trans))
3641 nft_chain_commit_update(trans);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003642 else
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003643 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003644
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003645 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003646 nft_trans_destroy(trans);
3647 break;
3648 case NFT_MSG_DELCHAIN:
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003649 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
Arturo Borreroc5598792014-09-02 16:42:23 +02003650 nf_tables_unregister_hooks(trans->ctx.table,
3651 trans->ctx.chain,
3652 trans->ctx.afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003653 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003654 case NFT_MSG_NEWRULE:
3655 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003656 nf_tables_rule_notify(&trans->ctx,
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003657 nft_trans_rule(trans),
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003658 NFT_MSG_NEWRULE);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003659 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003660 break;
3661 case NFT_MSG_DELRULE:
3662 list_del_rcu(&nft_trans_rule(trans)->list);
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003663 nf_tables_rule_notify(&trans->ctx,
3664 nft_trans_rule(trans),
3665 NFT_MSG_DELRULE);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003666 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003667 case NFT_MSG_NEWSET:
3668 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003669 /* This avoids hitting -EBUSY when deleting the table
3670 * from the transaction.
3671 */
3672 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3673 !list_empty(&nft_trans_set(trans)->bindings))
3674 trans->ctx.table->use--;
3675
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003676 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02003677 NFT_MSG_NEWSET, GFP_KERNEL);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003678 nft_trans_destroy(trans);
3679 break;
3680 case NFT_MSG_DELSET:
3681 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02003682 NFT_MSG_DELSET, GFP_KERNEL);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003683 break;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003684 case NFT_MSG_NEWSETELEM:
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003685 nf_tables_setelem_notify(&trans->ctx,
3686 nft_trans_elem_set(trans),
3687 &nft_trans_elem(trans),
3688 NFT_MSG_NEWSETELEM, 0);
3689 nft_trans_destroy(trans);
3690 break;
3691 case NFT_MSG_DELSETELEM:
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003692 te = (struct nft_trans_elem *)trans->data;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003693 ext = nft_set_elem_ext(te->set, te->elem.priv);
3694
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003695 nf_tables_setelem_notify(&trans->ctx, te->set,
3696 &te->elem,
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003697 NFT_MSG_DELSETELEM, 0);
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003698 te->set->ops->get(te->set, &te->elem);
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003699 nft_data_uninit(&te->elem.key, NFT_DATA_VALUE);
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003700 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3701 nft_data_uninit(nft_set_ext_data(ext),
3702 te->set->dtype);
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01003703 te->set->ops->remove(te->set, &te->elem);
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003704 nft_trans_destroy(trans);
3705 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003706 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003707 }
3708
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003709 synchronize_rcu();
3710
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003711 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003712 list_del(&trans->list);
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003713 nf_tables_commit_release(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003714 }
3715
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003716 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
3717
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003718 return 0;
3719}
3720
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003721static void nf_tables_abort_release(struct nft_trans *trans)
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003722{
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003723 switch (trans->msg_type) {
3724 case NFT_MSG_NEWTABLE:
3725 nf_tables_table_destroy(&trans->ctx);
3726 break;
3727 case NFT_MSG_NEWCHAIN:
3728 nf_tables_chain_destroy(trans->ctx.chain);
3729 break;
3730 case NFT_MSG_NEWRULE:
3731 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3732 break;
3733 case NFT_MSG_NEWSET:
3734 nft_set_destroy(nft_trans_set(trans));
3735 break;
3736 }
3737 kfree(trans);
3738}
3739
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003740static int nf_tables_abort(struct sk_buff *skb)
3741{
3742 struct net *net = sock_net(skb->sk);
3743 struct nft_trans *trans, *next;
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01003744 struct nft_trans_elem *te;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003745 struct nft_set_ext *ext;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003746
3747 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003748 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003749 case NFT_MSG_NEWTABLE:
3750 if (nft_trans_table_update(trans)) {
3751 if (nft_trans_table_enable(trans)) {
3752 nf_tables_table_disable(trans->ctx.afi,
3753 trans->ctx.table);
3754 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3755 }
3756 nft_trans_destroy(trans);
3757 } else {
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003758 list_del_rcu(&trans->ctx.table->list);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003759 }
3760 break;
3761 case NFT_MSG_DELTABLE:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003762 list_add_tail_rcu(&trans->ctx.table->list,
3763 &trans->ctx.afi->tables);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003764 nft_trans_destroy(trans);
3765 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003766 case NFT_MSG_NEWCHAIN:
3767 if (nft_trans_chain_update(trans)) {
Markus Elfring982f4052014-11-18 20:37:05 +01003768 free_percpu(nft_trans_chain_stats(trans));
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003769
3770 nft_trans_destroy(trans);
3771 } else {
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003772 trans->ctx.table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003773 list_del_rcu(&trans->ctx.chain->list);
Arturo Borreroc5598792014-09-02 16:42:23 +02003774 nf_tables_unregister_hooks(trans->ctx.table,
3775 trans->ctx.chain,
3776 trans->ctx.afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003777 }
3778 break;
3779 case NFT_MSG_DELCHAIN:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003780 trans->ctx.table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003781 list_add_tail_rcu(&trans->ctx.chain->list,
3782 &trans->ctx.table->chains);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003783 nft_trans_destroy(trans);
3784 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003785 case NFT_MSG_NEWRULE:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003786 trans->ctx.chain->use--;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003787 list_del_rcu(&nft_trans_rule(trans)->list);
3788 break;
3789 case NFT_MSG_DELRULE:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003790 trans->ctx.chain->use++;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003791 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003792 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003793 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003794 case NFT_MSG_NEWSET:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003795 trans->ctx.table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003796 list_del_rcu(&nft_trans_set(trans)->list);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003797 break;
3798 case NFT_MSG_DELSET:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003799 trans->ctx.table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003800 list_add_tail_rcu(&nft_trans_set(trans)->list,
3801 &trans->ctx.table->sets);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003802 nft_trans_destroy(trans);
3803 break;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003804 case NFT_MSG_NEWSETELEM:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003805 nft_trans_elem_set(trans)->nelems--;
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01003806 te = (struct nft_trans_elem *)trans->data;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003807 ext = nft_set_elem_ext(te->set, te->elem.priv);
3808
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01003809 te->set->ops->get(te->set, &te->elem);
3810 nft_data_uninit(&te->elem.key, NFT_DATA_VALUE);
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003811 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3812 nft_data_uninit(nft_set_ext_data(ext),
3813 te->set->dtype);
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01003814 te->set->ops->remove(te->set, &te->elem);
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003815 nft_trans_destroy(trans);
3816 break;
3817 case NFT_MSG_DELSETELEM:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003818 nft_trans_elem_set(trans)->nelems++;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003819 nft_trans_destroy(trans);
3820 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003821 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003822 }
3823
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003824 synchronize_rcu();
3825
Pablo Neira Ayusoa1cee072014-05-23 11:09:42 +02003826 list_for_each_entry_safe_reverse(trans, next,
3827 &net->nft.commit_list, list) {
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003828 list_del(&trans->list);
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003829 nf_tables_abort_release(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003830 }
3831
3832 return 0;
3833}
3834
Patrick McHardy96518512013-10-14 11:00:02 +02003835static const struct nfnetlink_subsystem nf_tables_subsys = {
3836 .name = "nf_tables",
3837 .subsys_id = NFNL_SUBSYS_NFTABLES,
3838 .cb_count = NFT_MSG_MAX,
3839 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003840 .commit = nf_tables_commit,
3841 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02003842};
3843
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +02003844int nft_chain_validate_dependency(const struct nft_chain *chain,
3845 enum nft_chain_type type)
3846{
3847 const struct nft_base_chain *basechain;
3848
3849 if (chain->flags & NFT_BASE_CHAIN) {
3850 basechain = nft_base_chain(chain);
3851 if (basechain->type->type != type)
3852 return -EOPNOTSUPP;
3853 }
3854 return 0;
3855}
3856EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
3857
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +01003858int nft_chain_validate_hooks(const struct nft_chain *chain,
3859 unsigned int hook_flags)
3860{
3861 struct nft_base_chain *basechain;
3862
3863 if (chain->flags & NFT_BASE_CHAIN) {
3864 basechain = nft_base_chain(chain);
3865
3866 if ((1 << basechain->ops[0].hooknum) & hook_flags)
3867 return 0;
3868
3869 return -EOPNOTSUPP;
3870 }
3871
3872 return 0;
3873}
3874EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
3875
Patrick McHardy20a69342013-10-11 12:06:22 +02003876/*
3877 * Loop detection - walk through the ruleset beginning at the destination chain
3878 * of a new jump until either the source chain is reached (loop) or all
3879 * reachable chains have been traversed.
3880 *
3881 * The loop check is performed whenever a new jump verdict is added to an
3882 * expression or verdict map or a verdict map is bound to a new chain.
3883 */
3884
3885static int nf_tables_check_loops(const struct nft_ctx *ctx,
3886 const struct nft_chain *chain);
3887
3888static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3889 const struct nft_set *set,
3890 const struct nft_set_iter *iter,
3891 const struct nft_set_elem *elem)
3892{
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003893 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3894 const struct nft_data *data;
3895
3896 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3897 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
Pablo Neira Ayuso62f9c8b2014-02-07 14:45:01 +01003898 return 0;
3899
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003900 data = nft_set_ext_data(ext);
3901 switch (data->verdict) {
Patrick McHardy20a69342013-10-11 12:06:22 +02003902 case NFT_JUMP:
3903 case NFT_GOTO:
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003904 return nf_tables_check_loops(ctx, data->chain);
Patrick McHardy20a69342013-10-11 12:06:22 +02003905 default:
3906 return 0;
3907 }
3908}
3909
3910static int nf_tables_check_loops(const struct nft_ctx *ctx,
3911 const struct nft_chain *chain)
3912{
3913 const struct nft_rule *rule;
3914 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02003915 const struct nft_set *set;
3916 struct nft_set_binding *binding;
3917 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02003918
3919 if (ctx->chain == chain)
3920 return -ELOOP;
3921
3922 list_for_each_entry(rule, &chain->rules, list) {
3923 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003924 const struct nft_data *data = NULL;
3925 int err;
3926
3927 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02003928 continue;
3929
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003930 err = expr->ops->validate(ctx, expr, &data);
3931 if (err < 0)
3932 return err;
3933
Patrick McHardy20a69342013-10-11 12:06:22 +02003934 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02003935 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02003936
3937 switch (data->verdict) {
3938 case NFT_JUMP:
3939 case NFT_GOTO:
3940 err = nf_tables_check_loops(ctx, data->chain);
3941 if (err < 0)
3942 return err;
3943 default:
3944 break;
3945 }
3946 }
3947 }
3948
3949 list_for_each_entry(set, &ctx->table->sets, list) {
3950 if (!(set->flags & NFT_SET_MAP) ||
3951 set->dtype != NFT_DATA_VERDICT)
3952 continue;
3953
3954 list_for_each_entry(binding, &set->bindings, list) {
3955 if (binding->chain != chain)
3956 continue;
3957
3958 iter.skip = 0;
3959 iter.count = 0;
3960 iter.err = 0;
3961 iter.fn = nf_tables_loop_check_setelem;
3962
3963 set->ops->walk(ctx, set, &iter);
3964 if (iter.err < 0)
3965 return iter.err;
3966 }
3967 }
3968
3969 return 0;
3970}
3971
Patrick McHardy96518512013-10-14 11:00:02 +02003972/**
3973 * nft_validate_input_register - validate an expressions' input register
3974 *
3975 * @reg: the register number
3976 *
3977 * Validate that the input register is one of the general purpose
3978 * registers.
3979 */
3980int nft_validate_input_register(enum nft_registers reg)
3981{
3982 if (reg <= NFT_REG_VERDICT)
3983 return -EINVAL;
3984 if (reg > NFT_REG_MAX)
3985 return -ERANGE;
3986 return 0;
3987}
3988EXPORT_SYMBOL_GPL(nft_validate_input_register);
3989
3990/**
3991 * nft_validate_output_register - validate an expressions' output register
3992 *
3993 * @reg: the register number
3994 *
3995 * Validate that the output register is one of the general purpose
3996 * registers or the verdict register.
3997 */
3998int nft_validate_output_register(enum nft_registers reg)
3999{
4000 if (reg < NFT_REG_VERDICT)
4001 return -EINVAL;
4002 if (reg > NFT_REG_MAX)
4003 return -ERANGE;
4004 return 0;
4005}
4006EXPORT_SYMBOL_GPL(nft_validate_output_register);
4007
4008/**
4009 * nft_validate_data_load - validate an expressions' data load
4010 *
4011 * @ctx: context of the expression performing the load
4012 * @reg: the destination register number
4013 * @data: the data to load
4014 * @type: the data type
4015 *
4016 * Validate that a data load uses the appropriate data type for
4017 * the destination register. A value of NULL for the data means
4018 * that its runtime gathered data, which is always of type
4019 * NFT_DATA_VALUE.
4020 */
4021int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
4022 const struct nft_data *data,
4023 enum nft_data_types type)
4024{
Patrick McHardy20a69342013-10-11 12:06:22 +02004025 int err;
4026
Patrick McHardy96518512013-10-14 11:00:02 +02004027 switch (reg) {
4028 case NFT_REG_VERDICT:
4029 if (data == NULL || type != NFT_DATA_VERDICT)
4030 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02004031
4032 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
4033 err = nf_tables_check_loops(ctx, data->chain);
4034 if (err < 0)
4035 return err;
4036
4037 if (ctx->chain->level + 1 > data->chain->level) {
4038 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
4039 return -EMLINK;
4040 data->chain->level = ctx->chain->level + 1;
4041 }
4042 }
4043
Patrick McHardy96518512013-10-14 11:00:02 +02004044 return 0;
4045 default:
4046 if (data != NULL && type != NFT_DATA_VALUE)
4047 return -EINVAL;
4048 return 0;
4049 }
4050}
4051EXPORT_SYMBOL_GPL(nft_validate_data_load);
4052
4053static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
4054 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
4055 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
4056 .len = NFT_CHAIN_MAXNAMELEN - 1 },
4057};
4058
4059static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
4060 struct nft_data_desc *desc, const struct nlattr *nla)
4061{
4062 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
4063 struct nft_chain *chain;
4064 int err;
4065
4066 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
4067 if (err < 0)
4068 return err;
4069
4070 if (!tb[NFTA_VERDICT_CODE])
4071 return -EINVAL;
4072 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
4073
4074 switch (data->verdict) {
Patrick McHardye0abdad2014-02-18 18:06:50 +00004075 default:
4076 switch (data->verdict & NF_VERDICT_MASK) {
4077 case NF_ACCEPT:
4078 case NF_DROP:
4079 case NF_QUEUE:
4080 break;
4081 default:
4082 return -EINVAL;
4083 }
4084 /* fall through */
Patrick McHardy96518512013-10-14 11:00:02 +02004085 case NFT_CONTINUE:
4086 case NFT_BREAK:
4087 case NFT_RETURN:
4088 desc->len = sizeof(data->verdict);
4089 break;
4090 case NFT_JUMP:
4091 case NFT_GOTO:
4092 if (!tb[NFTA_VERDICT_CHAIN])
4093 return -EINVAL;
4094 chain = nf_tables_chain_lookup(ctx->table,
4095 tb[NFTA_VERDICT_CHAIN]);
4096 if (IS_ERR(chain))
4097 return PTR_ERR(chain);
4098 if (chain->flags & NFT_BASE_CHAIN)
4099 return -EOPNOTSUPP;
4100
Patrick McHardy96518512013-10-14 11:00:02 +02004101 chain->use++;
4102 data->chain = chain;
4103 desc->len = sizeof(data);
4104 break;
Patrick McHardy96518512013-10-14 11:00:02 +02004105 }
4106
4107 desc->type = NFT_DATA_VERDICT;
4108 return 0;
4109}
4110
4111static void nft_verdict_uninit(const struct nft_data *data)
4112{
4113 switch (data->verdict) {
4114 case NFT_JUMP:
4115 case NFT_GOTO:
4116 data->chain->use--;
4117 break;
4118 }
4119}
4120
4121static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
4122{
4123 struct nlattr *nest;
4124
4125 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
4126 if (!nest)
4127 goto nla_put_failure;
4128
4129 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
4130 goto nla_put_failure;
4131
4132 switch (data->verdict) {
4133 case NFT_JUMP:
4134 case NFT_GOTO:
4135 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
4136 goto nla_put_failure;
4137 }
4138 nla_nest_end(skb, nest);
4139 return 0;
4140
4141nla_put_failure:
4142 return -1;
4143}
4144
4145static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
4146 struct nft_data_desc *desc, const struct nlattr *nla)
4147{
4148 unsigned int len;
4149
4150 len = nla_len(nla);
4151 if (len == 0)
4152 return -EINVAL;
4153 if (len > sizeof(data->data))
4154 return -EOVERFLOW;
4155
4156 nla_memcpy(data->data, nla, sizeof(data->data));
4157 desc->type = NFT_DATA_VALUE;
4158 desc->len = len;
4159 return 0;
4160}
4161
4162static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
4163 unsigned int len)
4164{
4165 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
4166}
4167
4168static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
4169 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
4170 .len = FIELD_SIZEOF(struct nft_data, data) },
4171 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
4172};
4173
4174/**
4175 * nft_data_init - parse nf_tables data netlink attributes
4176 *
4177 * @ctx: context of the expression using the data
4178 * @data: destination struct nft_data
4179 * @desc: data description
4180 * @nla: netlink attribute containing data
4181 *
4182 * Parse the netlink data attributes and initialize a struct nft_data.
4183 * The type and length of data are returned in the data description.
4184 *
4185 * The caller can indicate that it only wants to accept data of type
4186 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4187 */
4188int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
4189 struct nft_data_desc *desc, const struct nlattr *nla)
4190{
4191 struct nlattr *tb[NFTA_DATA_MAX + 1];
4192 int err;
4193
4194 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
4195 if (err < 0)
4196 return err;
4197
4198 if (tb[NFTA_DATA_VALUE])
4199 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
4200 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
4201 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
4202 return -EINVAL;
4203}
4204EXPORT_SYMBOL_GPL(nft_data_init);
4205
4206/**
4207 * nft_data_uninit - release a nft_data item
4208 *
4209 * @data: struct nft_data to release
4210 * @type: type of data
4211 *
4212 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4213 * all others need to be released by calling this function.
4214 */
4215void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
4216{
4217 switch (type) {
4218 case NFT_DATA_VALUE:
4219 return;
4220 case NFT_DATA_VERDICT:
4221 return nft_verdict_uninit(data);
4222 default:
4223 WARN_ON(1);
4224 }
4225}
4226EXPORT_SYMBOL_GPL(nft_data_uninit);
4227
4228int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
4229 enum nft_data_types type, unsigned int len)
4230{
4231 struct nlattr *nest;
4232 int err;
4233
4234 nest = nla_nest_start(skb, attr);
4235 if (nest == NULL)
4236 return -1;
4237
4238 switch (type) {
4239 case NFT_DATA_VALUE:
4240 err = nft_value_dump(skb, data, len);
4241 break;
4242 case NFT_DATA_VERDICT:
4243 err = nft_verdict_dump(skb, data);
4244 break;
4245 default:
4246 err = -EINVAL;
4247 WARN_ON(1);
4248 }
4249
4250 nla_nest_end(skb, nest);
4251 return err;
4252}
4253EXPORT_SYMBOL_GPL(nft_data_dump);
4254
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004255static int nf_tables_init_net(struct net *net)
4256{
4257 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02004258 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02004259 net->nft.base_seq = 1;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004260 return 0;
4261}
4262
4263static struct pernet_operations nf_tables_net_ops = {
4264 .init = nf_tables_init_net,
4265};
4266
Patrick McHardy96518512013-10-14 11:00:02 +02004267static int __init nf_tables_module_init(void)
4268{
4269 int err;
4270
4271 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
4272 GFP_KERNEL);
4273 if (info == NULL) {
4274 err = -ENOMEM;
4275 goto err1;
4276 }
4277
4278 err = nf_tables_core_module_init();
4279 if (err < 0)
4280 goto err2;
4281
4282 err = nfnetlink_subsys_register(&nf_tables_subsys);
4283 if (err < 0)
4284 goto err3;
4285
4286 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004287 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02004288err3:
4289 nf_tables_core_module_exit();
4290err2:
4291 kfree(info);
4292err1:
4293 return err;
4294}
4295
4296static void __exit nf_tables_module_exit(void)
4297{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004298 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02004299 nfnetlink_subsys_unregister(&nf_tables_subsys);
Pablo Neira Ayuso1b1bc492014-10-01 13:53:20 +02004300 rcu_barrier();
Patrick McHardy96518512013-10-14 11:00:02 +02004301 nf_tables_core_module_exit();
4302 kfree(info);
4303}
4304
4305module_init(nf_tables_module_init);
4306module_exit(nf_tables_module_exit);
4307
4308MODULE_LICENSE("GPL");
4309MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4310MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);