blob: 13d50e7cfe2fe48d89096af578041e7351e04fb5 [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
Pablo Neira Ayusodf05ef82015-12-15 19:39:32 +010044static void __nft_release_afinfo(struct net *net, struct nft_af_info *afi);
45
Patrick McHardy96518512013-10-14 11:00:02 +020046/**
47 * nft_unregister_afinfo - unregister nf_tables address family info
48 *
49 * @afi: address family info to unregister
50 *
51 * Unregister the address family for use with nf_tables.
52 */
Pablo Neira Ayusodf05ef82015-12-15 19:39:32 +010053void nft_unregister_afinfo(struct net *net, struct nft_af_info *afi)
Patrick McHardy96518512013-10-14 11:00:02 +020054{
55 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusodf05ef82015-12-15 19:39:32 +010056 __nft_release_afinfo(net, afi);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +020057 list_del_rcu(&afi->list);
Patrick McHardy96518512013-10-14 11:00:02 +020058 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
59}
60EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
61
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020062static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
Patrick McHardy96518512013-10-14 11:00:02 +020063{
64 struct nft_af_info *afi;
65
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020066 list_for_each_entry(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +020067 if (afi->family == family)
68 return afi;
69 }
70 return NULL;
71}
72
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020073static struct nft_af_info *
74nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
Patrick McHardy96518512013-10-14 11:00:02 +020075{
76 struct nft_af_info *afi;
77
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020078 afi = nft_afinfo_lookup(net, family);
Patrick McHardy96518512013-10-14 11:00:02 +020079 if (afi != NULL)
80 return afi;
81#ifdef CONFIG_MODULES
82 if (autoload) {
83 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
84 request_module("nft-afinfo-%u", family);
85 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +020086 afi = nft_afinfo_lookup(net, family);
Patrick McHardy96518512013-10-14 11:00:02 +020087 if (afi != NULL)
88 return ERR_PTR(-EAGAIN);
89 }
90#endif
91 return ERR_PTR(-EAFNOSUPPORT);
92}
93
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +020094static void nft_ctx_init(struct nft_ctx *ctx,
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +010095 struct net *net,
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +020096 const struct sk_buff *skb,
97 const struct nlmsghdr *nlh,
98 struct nft_af_info *afi,
99 struct nft_table *table,
100 struct nft_chain *chain,
101 const struct nlattr * const *nla)
102{
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +0100103 ctx->net = net;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200104 ctx->afi = afi;
105 ctx->table = table;
106 ctx->chain = chain;
107 ctx->nla = nla;
108 ctx->portid = NETLINK_CB(skb).portid;
109 ctx->report = nlmsg_report(nlh);
110 ctx->seq = nlh->nlmsg_seq;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +0200111}
112
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +0200113static struct nft_trans *nft_trans_alloc(struct nft_ctx *ctx, int msg_type,
114 u32 size)
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +0200115{
116 struct nft_trans *trans;
117
118 trans = kzalloc(sizeof(struct nft_trans) + size, GFP_KERNEL);
119 if (trans == NULL)
120 return NULL;
121
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +0200122 trans->msg_type = msg_type;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +0200123 trans->ctx = *ctx;
124
125 return trans;
126}
127
128static void nft_trans_destroy(struct nft_trans *trans)
129{
130 list_del(&trans->list);
131 kfree(trans);
132}
133
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +0200134static int nf_tables_register_hooks(struct net *net,
135 const struct nft_table *table,
Pablo Neira Ayusod8ee8f72015-06-15 02:42:31 +0200136 struct nft_chain *chain,
137 unsigned int hook_nops)
138{
139 if (table->flags & NFT_TABLE_F_DORMANT ||
140 !(chain->flags & NFT_BASE_CHAIN))
141 return 0;
142
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +0200143 return nf_register_net_hooks(net, nft_base_chain(chain)->ops,
144 hook_nops);
Pablo Neira Ayusod8ee8f72015-06-15 02:42:31 +0200145}
146
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +0200147static void nf_tables_unregister_hooks(struct net *net,
148 const struct nft_table *table,
Pablo Neira Ayusod8ee8f72015-06-15 02:42:31 +0200149 struct nft_chain *chain,
Arturo Borreroc5598792014-09-02 16:42:23 +0200150 unsigned int hook_nops)
151{
Pablo Neira Ayusod8ee8f72015-06-15 02:42:31 +0200152 if (table->flags & NFT_TABLE_F_DORMANT ||
153 !(chain->flags & NFT_BASE_CHAIN))
154 return;
155
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +0200156 nf_unregister_net_hooks(net, nft_base_chain(chain)->ops, hook_nops);
Arturo Borreroc5598792014-09-02 16:42:23 +0200157}
158
Arturo Borreroee01d542014-09-02 16:42:25 +0200159static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
160{
161 struct nft_trans *trans;
162
163 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
164 if (trans == NULL)
165 return -ENOMEM;
166
167 if (msg_type == NFT_MSG_NEWTABLE)
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200168 nft_activate_next(ctx->net, ctx->table);
Arturo Borreroee01d542014-09-02 16:42:25 +0200169
170 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
171 return 0;
172}
173
174static int nft_deltable(struct nft_ctx *ctx)
175{
176 int err;
177
178 err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
179 if (err < 0)
180 return err;
181
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200182 nft_deactivate_next(ctx->net, ctx->table);
Arturo Borreroee01d542014-09-02 16:42:25 +0200183 return err;
184}
185
186static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
187{
188 struct nft_trans *trans;
189
190 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
191 if (trans == NULL)
192 return -ENOMEM;
193
194 if (msg_type == NFT_MSG_NEWCHAIN)
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200195 nft_activate_next(ctx->net, ctx->chain);
Arturo Borreroee01d542014-09-02 16:42:25 +0200196
197 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
198 return 0;
199}
200
201static int nft_delchain(struct nft_ctx *ctx)
202{
203 int err;
204
205 err = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
206 if (err < 0)
207 return err;
208
209 ctx->table->use--;
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200210 nft_deactivate_next(ctx->net, ctx->chain);
Arturo Borreroee01d542014-09-02 16:42:25 +0200211
212 return err;
213}
214
Arturo Borreroee01d542014-09-02 16:42:25 +0200215static int
216nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
217{
218 /* You cannot delete the same rule twice */
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +0200219 if (nft_is_active_next(ctx->net, rule)) {
220 nft_deactivate_next(ctx->net, rule);
Arturo Borreroee01d542014-09-02 16:42:25 +0200221 ctx->chain->use--;
222 return 0;
223 }
224 return -ENOENT;
225}
226
227static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
228 struct nft_rule *rule)
229{
230 struct nft_trans *trans;
231
232 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
233 if (trans == NULL)
234 return NULL;
235
236 nft_trans_rule(trans) = rule;
237 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
238
239 return trans;
240}
241
242static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
243{
244 struct nft_trans *trans;
245 int err;
246
247 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
248 if (trans == NULL)
249 return -ENOMEM;
250
251 err = nf_tables_delrule_deactivate(ctx, rule);
252 if (err < 0) {
253 nft_trans_destroy(trans);
254 return err;
255 }
256
257 return 0;
258}
259
260static int nft_delrule_by_chain(struct nft_ctx *ctx)
261{
262 struct nft_rule *rule;
263 int err;
264
265 list_for_each_entry(rule, &ctx->chain->rules, list) {
266 err = nft_delrule(ctx, rule);
267 if (err < 0)
268 return err;
269 }
270 return 0;
271}
272
Arturo Borreroee01d542014-09-02 16:42:25 +0200273static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
274 struct nft_set *set)
275{
276 struct nft_trans *trans;
277
278 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
279 if (trans == NULL)
280 return -ENOMEM;
281
282 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
283 nft_trans_set_id(trans) =
284 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200285 nft_activate_next(ctx->net, set);
Arturo Borreroee01d542014-09-02 16:42:25 +0200286 }
287 nft_trans_set(trans) = set;
288 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
289
290 return 0;
291}
292
293static int nft_delset(struct nft_ctx *ctx, struct nft_set *set)
294{
295 int err;
296
297 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
298 if (err < 0)
299 return err;
300
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200301 nft_deactivate_next(ctx->net, set);
Arturo Borreroee01d542014-09-02 16:42:25 +0200302 ctx->table->use--;
303
304 return err;
305}
306
Patrick McHardy96518512013-10-14 11:00:02 +0200307/*
308 * Tables
309 */
310
311static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200312 const struct nlattr *nla,
313 u8 genmask)
Patrick McHardy96518512013-10-14 11:00:02 +0200314{
315 struct nft_table *table;
316
317 list_for_each_entry(table, &afi->tables, list) {
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200318 if (!nla_strcmp(nla, table->name) &&
319 nft_active_genmask(table, genmask))
Patrick McHardy96518512013-10-14 11:00:02 +0200320 return table;
321 }
322 return NULL;
323}
324
325static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200326 const struct nlattr *nla,
327 u8 genmask)
Patrick McHardy96518512013-10-14 11:00:02 +0200328{
329 struct nft_table *table;
330
331 if (nla == NULL)
332 return ERR_PTR(-EINVAL);
333
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200334 table = nft_table_lookup(afi, nla, genmask);
Patrick McHardy96518512013-10-14 11:00:02 +0200335 if (table != NULL)
336 return table;
337
Patrick McHardy96518512013-10-14 11:00:02 +0200338 return ERR_PTR(-ENOENT);
339}
340
341static inline u64 nf_tables_alloc_handle(struct nft_table *table)
342{
343 return ++table->hgenerator;
344}
345
Patrick McHardy2a37d752014-01-09 18:42:37 +0000346static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200347
Patrick McHardy2a37d752014-01-09 18:42:37 +0000348static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000349__nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200350{
351 int i;
352
Patrick McHardybaae3e62014-01-09 18:42:34 +0000353 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200354 if (chain_type[family][i] != NULL &&
355 !nla_strcmp(nla, chain_type[family][i]->name))
Patrick McHardybaae3e62014-01-09 18:42:34 +0000356 return chain_type[family][i];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200357 }
Patrick McHardybaae3e62014-01-09 18:42:34 +0000358 return NULL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200359}
360
Patrick McHardy2a37d752014-01-09 18:42:37 +0000361static const struct nf_chain_type *
Patrick McHardybaae3e62014-01-09 18:42:34 +0000362nf_tables_chain_type_lookup(const struct nft_af_info *afi,
363 const struct nlattr *nla,
364 bool autoload)
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200365{
Patrick McHardy2a37d752014-01-09 18:42:37 +0000366 const struct nf_chain_type *type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200367
368 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000369 if (type != NULL)
370 return type;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200371#ifdef CONFIG_MODULES
Patrick McHardy93b08062014-01-09 18:42:36 +0000372 if (autoload) {
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200373 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso2fec6bb2014-03-31 12:26:39 +0200374 request_module("nft-chain-%u-%.*s", afi->family,
375 nla_len(nla), (const char *)nla_data(nla));
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200376 nfnl_lock(NFNL_SUBSYS_NFTABLES);
377 type = __nf_tables_chain_type_lookup(afi->family, nla);
Patrick McHardy93b08062014-01-09 18:42:36 +0000378 if (type != NULL)
379 return ERR_PTR(-EAGAIN);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200380 }
381#endif
Patrick McHardy93b08062014-01-09 18:42:36 +0000382 return ERR_PTR(-ENOENT);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200383}
384
Patrick McHardy96518512013-10-14 11:00:02 +0200385static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
Pablo Neira Ayuso1cae5652015-03-05 15:05:36 +0100386 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
387 .len = NFT_TABLE_MAXNAMELEN - 1 },
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200388 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
Patrick McHardy96518512013-10-14 11:00:02 +0200389};
390
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200391static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
392 u32 portid, u32 seq, int event, u32 flags,
393 int family, const struct nft_table *table)
Patrick McHardy96518512013-10-14 11:00:02 +0200394{
395 struct nlmsghdr *nlh;
396 struct nfgenmsg *nfmsg;
397
398 event |= NFNL_SUBSYS_NFTABLES << 8;
399 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
400 if (nlh == NULL)
401 goto nla_put_failure;
402
403 nfmsg = nlmsg_data(nlh);
404 nfmsg->nfgen_family = family;
405 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200406 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +0200407
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200408 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
Tomasz Bursztykad8bcc7682013-12-12 15:00:42 +0200409 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
410 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
Patrick McHardy96518512013-10-14 11:00:02 +0200411 goto nla_put_failure;
412
Johannes Berg053c0952015-01-16 22:09:00 +0100413 nlmsg_end(skb, nlh);
414 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200415
416nla_put_failure:
417 nlmsg_trim(skb, nlh);
418 return -1;
419}
420
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +0200421static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)
Patrick McHardy96518512013-10-14 11:00:02 +0200422{
423 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +0200424 int err;
425
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200426 if (!ctx->report &&
427 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +0200428 return 0;
429
430 err = -ENOBUFS;
431 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
432 if (skb == NULL)
433 goto err;
434
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200435 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
436 event, 0, ctx->afi->family, ctx->table);
Patrick McHardy96518512013-10-14 11:00:02 +0200437 if (err < 0) {
438 kfree_skb(skb);
439 goto err;
440 }
441
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200442 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
443 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +0200444err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +0200445 if (err < 0) {
446 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
447 err);
448 }
Patrick McHardy96518512013-10-14 11:00:02 +0200449 return err;
450}
451
452static int nf_tables_dump_tables(struct sk_buff *skb,
453 struct netlink_callback *cb)
454{
455 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
456 const struct nft_af_info *afi;
457 const struct nft_table *table;
458 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200459 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +0200460 int family = nfmsg->nfgen_family;
461
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200462 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +0200463 cb->seq = net->nft.base_seq;
464
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200465 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200466 if (family != NFPROTO_UNSPEC && family != afi->family)
467 continue;
468
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200469 list_for_each_entry_rcu(table, &afi->tables, list) {
Patrick McHardy96518512013-10-14 11:00:02 +0200470 if (idx < s_idx)
471 goto cont;
472 if (idx > s_idx)
473 memset(&cb->args[1], 0,
474 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200475 if (!nft_is_active(net, table))
476 continue;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200477 if (nf_tables_fill_table_info(skb, net,
Patrick McHardy96518512013-10-14 11:00:02 +0200478 NETLINK_CB(cb->skb).portid,
479 cb->nlh->nlmsg_seq,
480 NFT_MSG_NEWTABLE,
481 NLM_F_MULTI,
482 afi->family, table) < 0)
483 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +0200484
485 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +0200486cont:
487 idx++;
488 }
489 }
490done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200491 rcu_read_unlock();
Patrick McHardy96518512013-10-14 11:00:02 +0200492 cb->args[0] = idx;
493 return skb->len;
494}
495
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +0100496static int nf_tables_gettable(struct net *net, struct sock *nlsk,
497 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +0200498 const struct nlattr * const nla[])
499{
500 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200501 u8 genmask = nft_genmask_cur(net);
Patrick McHardy96518512013-10-14 11:00:02 +0200502 const struct nft_af_info *afi;
503 const struct nft_table *table;
504 struct sk_buff *skb2;
505 int family = nfmsg->nfgen_family;
506 int err;
507
508 if (nlh->nlmsg_flags & NLM_F_DUMP) {
509 struct netlink_dump_control c = {
510 .dump = nf_tables_dump_tables,
511 };
512 return netlink_dump_start(nlsk, skb, nlh, &c);
513 }
514
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200515 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200516 if (IS_ERR(afi))
517 return PTR_ERR(afi);
518
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200519 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +0200520 if (IS_ERR(table))
521 return PTR_ERR(table);
522
523 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
524 if (!skb2)
525 return -ENOMEM;
526
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200527 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +0200528 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
529 family, table);
530 if (err < 0)
531 goto err;
532
533 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
534
535err:
536 kfree_skb(skb2);
537 return err;
538}
539
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200540static int nf_tables_table_enable(struct net *net,
541 const struct nft_af_info *afi,
Patrick McHardy115a60b2014-01-03 12:16:15 +0000542 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200543{
544 struct nft_chain *chain;
545 int err, i = 0;
546
547 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200548 if (!nft_is_active_next(net, chain))
549 continue;
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100550 if (!(chain->flags & NFT_BASE_CHAIN))
551 continue;
552
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +0200553 err = nf_register_net_hooks(net, nft_base_chain(chain)->ops,
554 afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200555 if (err < 0)
556 goto err;
557
558 i++;
559 }
560 return 0;
561err:
562 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200563 if (!nft_is_active_next(net, chain))
564 continue;
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100565 if (!(chain->flags & NFT_BASE_CHAIN))
566 continue;
567
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200568 if (i-- <= 0)
569 break;
570
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +0200571 nf_unregister_net_hooks(net, nft_base_chain(chain)->ops,
572 afi->nops);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200573 }
574 return err;
575}
576
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200577static void nf_tables_table_disable(struct net *net,
578 const struct nft_af_info *afi,
Pablo Neira Ayusod8ee8f72015-06-15 02:42:31 +0200579 struct nft_table *table)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200580{
581 struct nft_chain *chain;
582
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100583 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200584 if (!nft_is_active_next(net, chain))
585 continue;
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +0200586 if (!(chain->flags & NFT_BASE_CHAIN))
587 continue;
588
589 nf_unregister_net_hooks(net, nft_base_chain(chain)->ops,
590 afi->nops);
Pablo Neira Ayusod2012972013-12-27 10:44:23 +0100591 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200592}
593
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200594static int nf_tables_updtable(struct nft_ctx *ctx)
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200595{
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200596 struct nft_trans *trans;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200597 u32 flags;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200598 int ret = 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200599
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200600 if (!ctx->nla[NFTA_TABLE_FLAGS])
601 return 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200602
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200603 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
604 if (flags & ~NFT_TABLE_F_DORMANT)
605 return -EINVAL;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200606
Pablo Neira Ayuso63283dd2014-06-27 18:51:39 +0200607 if (flags == ctx->table->flags)
608 return 0;
609
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200610 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
611 sizeof(struct nft_trans_table));
612 if (trans == NULL)
613 return -ENOMEM;
614
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200615 if ((flags & NFT_TABLE_F_DORMANT) &&
616 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200617 nft_trans_table_enable(trans) = false;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200618 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
619 ctx->table->flags & NFT_TABLE_F_DORMANT) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200620 ret = nf_tables_table_enable(ctx->net, ctx->afi, ctx->table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200621 if (ret >= 0) {
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200622 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200623 nft_trans_table_enable(trans) = true;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200624 }
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200625 }
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200626 if (ret < 0)
627 goto err;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200628
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200629 nft_trans_table_update(trans) = true;
630 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
631 return 0;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200632err:
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200633 nft_trans_destroy(trans);
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200634 return ret;
635}
636
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +0100637static int nf_tables_newtable(struct net *net, struct sock *nlsk,
638 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +0200639 const struct nlattr * const nla[])
640{
641 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200642 u8 genmask = nft_genmask_next(net);
Patrick McHardy96518512013-10-14 11:00:02 +0200643 const struct nlattr *name;
644 struct nft_af_info *afi;
645 struct nft_table *table;
646 int family = nfmsg->nfgen_family;
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000647 u32 flags = 0;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200648 struct nft_ctx ctx;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200649 int err;
Patrick McHardy96518512013-10-14 11:00:02 +0200650
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200651 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +0200652 if (IS_ERR(afi))
653 return PTR_ERR(afi);
654
655 name = nla[NFTA_TABLE_NAME];
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200656 table = nf_tables_table_lookup(afi, name, genmask);
Patrick McHardy96518512013-10-14 11:00:02 +0200657 if (IS_ERR(table)) {
658 if (PTR_ERR(table) != -ENOENT)
659 return PTR_ERR(table);
660 table = NULL;
661 }
662
663 if (table != NULL) {
664 if (nlh->nlmsg_flags & NLM_F_EXCL)
665 return -EEXIST;
666 if (nlh->nlmsg_flags & NLM_F_REPLACE)
667 return -EOPNOTSUPP;
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200668
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +0100669 nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
Pablo Neira Ayusoe1aaca92014-03-30 14:04:53 +0200670 return nf_tables_updtable(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +0200671 }
672
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000673 if (nla[NFTA_TABLE_FLAGS]) {
674 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
675 if (flags & ~NFT_TABLE_F_DORMANT)
676 return -EINVAL;
677 }
678
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200679 err = -EAFNOSUPPORT;
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000680 if (!try_module_get(afi->owner))
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200681 goto err1;
Patrick McHardy7047f9d2014-01-09 18:42:40 +0000682
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100683 err = -ENOMEM;
Pablo Neira Ayuso1cae5652015-03-05 15:05:36 +0100684 table = kzalloc(sizeof(*table), GFP_KERNEL);
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100685 if (table == NULL)
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200686 goto err2;
Patrick McHardy96518512013-10-14 11:00:02 +0200687
Pablo Neira Ayuso1cae5652015-03-05 15:05:36 +0100688 nla_strlcpy(table->name, name, NFT_TABLE_MAXNAMELEN);
Patrick McHardy96518512013-10-14 11:00:02 +0200689 INIT_LIST_HEAD(&table->chains);
Patrick McHardy20a69342013-10-11 12:06:22 +0200690 INIT_LIST_HEAD(&table->sets);
Patrick McHardyc5c1f972014-01-09 18:42:39 +0000691 table->flags = flags;
Pablo Neira Ayuso9ddf6322013-10-10 13:26:33 +0200692
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +0100693 nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200694 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100695 if (err < 0)
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200696 goto err3;
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100697
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +0200698 list_add_tail_rcu(&table->list, &afi->tables);
Patrick McHardy96518512013-10-14 11:00:02 +0200699 return 0;
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200700err3:
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100701 kfree(table);
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200702err2:
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100703 module_put(afi->owner);
Pablo Neira Ayusoebddf1a2015-05-26 18:41:20 +0200704err1:
Pablo Neira Ayusoffdb2102015-03-17 19:53:23 +0100705 return err;
Patrick McHardy96518512013-10-14 11:00:02 +0200706}
707
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200708static int nft_flush_table(struct nft_ctx *ctx)
709{
710 int err;
711 struct nft_chain *chain, *nc;
712 struct nft_set *set, *ns;
713
Pablo Neira Ayusoa2f18db2015-01-04 15:14:22 +0100714 list_for_each_entry(chain, &ctx->table->chains, list) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200715 if (!nft_is_active_next(ctx->net, chain))
716 continue;
717
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200718 ctx->chain = chain;
719
720 err = nft_delrule_by_chain(ctx);
721 if (err < 0)
722 goto out;
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200723 }
724
725 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +0200726 if (!nft_is_active_next(ctx->net, set))
727 continue;
728
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200729 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) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200739 if (!nft_is_active_next(ctx->net, chain))
740 continue;
741
Pablo Neira Ayusoa2f18db2015-01-04 15:14:22 +0100742 ctx->chain = chain;
743
744 err = nft_delchain(ctx);
745 if (err < 0)
746 goto out;
747 }
748
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200749 err = nft_deltable(ctx);
750out:
751 return err;
752}
753
754static int nft_flush(struct nft_ctx *ctx, int family)
755{
756 struct nft_af_info *afi;
757 struct nft_table *table, *nt;
758 const struct nlattr * const *nla = ctx->nla;
759 int err = 0;
760
761 list_for_each_entry(afi, &ctx->net->nft.af_info, list) {
762 if (family != AF_UNSPEC && afi->family != family)
763 continue;
764
765 ctx->afi = afi;
766 list_for_each_entry_safe(table, nt, &afi->tables, list) {
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200767 if (!nft_is_active_next(ctx->net, table))
768 continue;
769
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200770 if (nla[NFTA_TABLE_NAME] &&
771 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
772 continue;
773
774 ctx->table = table;
775
776 err = nft_flush_table(ctx);
777 if (err < 0)
778 goto out;
779 }
780 }
781out:
782 return err;
783}
784
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +0100785static int nf_tables_deltable(struct net *net, struct sock *nlsk,
786 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +0200787 const struct nlattr * const nla[])
788{
789 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200790 u8 genmask = nft_genmask_next(net);
Patrick McHardy96518512013-10-14 11:00:02 +0200791 struct nft_af_info *afi;
792 struct nft_table *table;
Arturo Borreroee01d542014-09-02 16:42:25 +0200793 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200794 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +0200795
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +0100796 nft_ctx_init(&ctx, net, skb, nlh, NULL, NULL, NULL, nla);
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200797 if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL)
798 return nft_flush(&ctx, family);
799
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +0200800 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +0200801 if (IS_ERR(afi))
802 return PTR_ERR(afi);
803
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +0200804 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +0200805 if (IS_ERR(table))
806 return PTR_ERR(table);
Patrick McHardy96518512013-10-14 11:00:02 +0200807
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200808 ctx.afi = afi;
809 ctx.table = table;
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200810
Arturo Borrerob9ac12e2014-09-02 16:42:26 +0200811 return nft_flush_table(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +0200812}
813
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200814static void nf_tables_table_destroy(struct nft_ctx *ctx)
815{
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +0200816 BUG_ON(ctx->table->use > 0);
817
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +0200818 kfree(ctx->table);
819 module_put(ctx->afi->owner);
820}
821
Patrick McHardy2a37d752014-01-09 18:42:37 +0000822int nft_register_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200823{
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200824 int err = 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200825
826 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200827 if (chain_type[ctype->family][ctype->type] != NULL) {
828 err = -EBUSY;
829 goto out;
Patrick McHardy96518512013-10-14 11:00:02 +0200830 }
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200831 chain_type[ctype->family][ctype->type] = ctype;
832out:
Patrick McHardy96518512013-10-14 11:00:02 +0200833 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
834 return err;
835}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200836EXPORT_SYMBOL_GPL(nft_register_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200837
Patrick McHardy2a37d752014-01-09 18:42:37 +0000838void nft_unregister_chain_type(const struct nf_chain_type *ctype)
Patrick McHardy96518512013-10-14 11:00:02 +0200839{
Patrick McHardy96518512013-10-14 11:00:02 +0200840 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200841 chain_type[ctype->family][ctype->type] = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +0200842 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
843}
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200844EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200845
846/*
847 * Chains
848 */
849
850static struct nft_chain *
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200851nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle,
852 u8 genmask)
Patrick McHardy96518512013-10-14 11:00:02 +0200853{
854 struct nft_chain *chain;
855
856 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200857 if (chain->handle == handle &&
858 nft_active_genmask(chain, genmask))
Patrick McHardy96518512013-10-14 11:00:02 +0200859 return chain;
860 }
861
862 return ERR_PTR(-ENOENT);
863}
864
865static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200866 const struct nlattr *nla,
867 u8 genmask)
Patrick McHardy96518512013-10-14 11:00:02 +0200868{
869 struct nft_chain *chain;
870
871 if (nla == NULL)
872 return ERR_PTR(-EINVAL);
873
874 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +0200875 if (!nla_strcmp(nla, chain->name) &&
876 nft_active_genmask(chain, genmask))
Patrick McHardy96518512013-10-14 11:00:02 +0200877 return chain;
878 }
879
880 return ERR_PTR(-ENOENT);
881}
882
883static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
884 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
885 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
886 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
887 .len = NFT_CHAIN_MAXNAMELEN - 1 },
888 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200889 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
Pablo Neira4c1f7812014-03-31 17:43:47 +0200890 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200891 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
Patrick McHardy96518512013-10-14 11:00:02 +0200892};
893
894static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
895 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
896 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +0200897 [NFTA_HOOK_DEV] = { .type = NLA_STRING,
898 .len = IFNAMSIZ - 1 },
Patrick McHardy96518512013-10-14 11:00:02 +0200899};
900
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200901static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
902{
903 struct nft_stats *cpu_stats, total;
904 struct nlattr *nest;
Eric Dumazetce355e22014-07-09 15:14:06 +0200905 unsigned int seq;
906 u64 pkts, bytes;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200907 int cpu;
908
909 memset(&total, 0, sizeof(total));
910 for_each_possible_cpu(cpu) {
911 cpu_stats = per_cpu_ptr(stats, cpu);
Eric Dumazetce355e22014-07-09 15:14:06 +0200912 do {
913 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
914 pkts = cpu_stats->pkts;
915 bytes = cpu_stats->bytes;
916 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
917 total.pkts += pkts;
918 total.bytes += bytes;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200919 }
920 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
921 if (nest == NULL)
922 goto nla_put_failure;
923
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200924 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts),
925 NFTA_COUNTER_PAD) ||
926 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
927 NFTA_COUNTER_PAD))
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200928 goto nla_put_failure;
929
930 nla_nest_end(skb, nest);
931 return 0;
932
933nla_put_failure:
934 return -ENOSPC;
935}
936
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200937static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
938 u32 portid, u32 seq, int event, u32 flags,
939 int family, const struct nft_table *table,
Patrick McHardy96518512013-10-14 11:00:02 +0200940 const struct nft_chain *chain)
941{
942 struct nlmsghdr *nlh;
943 struct nfgenmsg *nfmsg;
944
945 event |= NFNL_SUBSYS_NFTABLES << 8;
946 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
947 if (nlh == NULL)
948 goto nla_put_failure;
949
950 nfmsg = nlmsg_data(nlh);
951 nfmsg->nfgen_family = family;
952 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +0200953 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +0200954
955 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
956 goto nla_put_failure;
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200957 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle),
958 NFTA_CHAIN_PAD))
Patrick McHardy96518512013-10-14 11:00:02 +0200959 goto nla_put_failure;
960 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
961 goto nla_put_failure;
962
963 if (chain->flags & NFT_BASE_CHAIN) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200964 const struct nft_base_chain *basechain = nft_base_chain(chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000965 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200966 struct nlattr *nest;
967
968 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
Patrick McHardy96518512013-10-14 11:00:02 +0200969 if (nest == NULL)
970 goto nla_put_failure;
971 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
972 goto nla_put_failure;
973 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
974 goto nla_put_failure;
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +0200975 if (basechain->dev_name[0] &&
976 nla_put_string(skb, NFTA_HOOK_DEV, basechain->dev_name))
977 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +0200978 nla_nest_end(skb, nest);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +0200979
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200980 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
981 htonl(basechain->policy)))
982 goto nla_put_failure;
983
Patrick McHardybaae3e62014-01-09 18:42:34 +0000984 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
985 goto nla_put_failure;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200986
987 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
988 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +0200989 }
990
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200991 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
992 goto nla_put_failure;
993
Johannes Berg053c0952015-01-16 22:09:00 +0100994 nlmsg_end(skb, nlh);
995 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +0200996
997nla_put_failure:
998 nlmsg_trim(skb, nlh);
999 return -1;
1000}
1001
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001002static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
Patrick McHardy96518512013-10-14 11:00:02 +02001003{
1004 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +02001005 int err;
1006
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001007 if (!ctx->report &&
1008 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +02001009 return 0;
1010
1011 err = -ENOBUFS;
1012 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1013 if (skb == NULL)
1014 goto err;
1015
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001016 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
1017 event, 0, ctx->afi->family, ctx->table,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001018 ctx->chain);
Patrick McHardy96518512013-10-14 11:00:02 +02001019 if (err < 0) {
1020 kfree_skb(skb);
1021 goto err;
1022 }
1023
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001024 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1025 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001026err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001027 if (err < 0) {
1028 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1029 err);
1030 }
Patrick McHardy96518512013-10-14 11:00:02 +02001031 return err;
1032}
1033
1034static int nf_tables_dump_chains(struct sk_buff *skb,
1035 struct netlink_callback *cb)
1036{
1037 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1038 const struct nft_af_info *afi;
1039 const struct nft_table *table;
1040 const struct nft_chain *chain;
1041 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001042 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001043 int family = nfmsg->nfgen_family;
1044
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001045 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001046 cb->seq = net->nft.base_seq;
1047
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001048 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001049 if (family != NFPROTO_UNSPEC && family != afi->family)
1050 continue;
1051
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001052 list_for_each_entry_rcu(table, &afi->tables, list) {
1053 list_for_each_entry_rcu(chain, &table->chains, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001054 if (idx < s_idx)
1055 goto cont;
1056 if (idx > s_idx)
1057 memset(&cb->args[1], 0,
1058 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02001059 if (!nft_is_active(net, chain))
1060 continue;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001061 if (nf_tables_fill_chain_info(skb, net,
1062 NETLINK_CB(cb->skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001063 cb->nlh->nlmsg_seq,
1064 NFT_MSG_NEWCHAIN,
1065 NLM_F_MULTI,
1066 afi->family, table, chain) < 0)
1067 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001068
1069 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +02001070cont:
1071 idx++;
1072 }
1073 }
1074 }
1075done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001076 rcu_read_unlock();
Patrick McHardy96518512013-10-14 11:00:02 +02001077 cb->args[0] = idx;
1078 return skb->len;
1079}
1080
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +01001081static int nf_tables_getchain(struct net *net, struct sock *nlsk,
1082 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02001083 const struct nlattr * const nla[])
1084{
1085 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001086 u8 genmask = nft_genmask_cur(net);
Patrick McHardy96518512013-10-14 11:00:02 +02001087 const struct nft_af_info *afi;
1088 const struct nft_table *table;
1089 const struct nft_chain *chain;
1090 struct sk_buff *skb2;
1091 int family = nfmsg->nfgen_family;
1092 int err;
1093
1094 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1095 struct netlink_dump_control c = {
1096 .dump = nf_tables_dump_chains,
1097 };
1098 return netlink_dump_start(nlsk, skb, nlh, &c);
1099 }
1100
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001101 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001102 if (IS_ERR(afi))
1103 return PTR_ERR(afi);
1104
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001105 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001106 if (IS_ERR(table))
1107 return PTR_ERR(table);
1108
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02001109 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001110 if (IS_ERR(chain))
1111 return PTR_ERR(chain);
1112
1113 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1114 if (!skb2)
1115 return -ENOMEM;
1116
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001117 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001118 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
1119 family, table, chain);
1120 if (err < 0)
1121 goto err;
1122
1123 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1124
1125err:
1126 kfree_skb(skb2);
1127 return err;
1128}
1129
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001130static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1131 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
1132 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
1133};
1134
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001135static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001136{
1137 struct nlattr *tb[NFTA_COUNTER_MAX+1];
1138 struct nft_stats __percpu *newstats;
1139 struct nft_stats *stats;
1140 int err;
1141
1142 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
1143 if (err < 0)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001144 return ERR_PTR(err);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001145
1146 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001147 return ERR_PTR(-EINVAL);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001148
Eric Dumazetce355e22014-07-09 15:14:06 +02001149 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001150 if (newstats == NULL)
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001151 return ERR_PTR(-ENOMEM);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001152
1153 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1154 * are not exposed to userspace.
1155 */
Pablo Neira Ayusoe8781f72015-01-21 18:04:18 +01001156 preempt_disable();
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001157 stats = this_cpu_ptr(newstats);
1158 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1159 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
Pablo Neira Ayusoe8781f72015-01-21 18:04:18 +01001160 preempt_enable();
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001161
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001162 return newstats;
1163}
1164
1165static void nft_chain_stats_replace(struct nft_base_chain *chain,
1166 struct nft_stats __percpu *newstats)
1167{
Pablo Neira Ayusob88825d2014-08-05 17:25:59 +02001168 if (newstats == NULL)
1169 return;
1170
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001171 if (chain->stats) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001172 struct nft_stats __percpu *oldstats =
Patrick McHardy67a8fc22014-02-18 18:06:49 +00001173 nft_dereference(chain->stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001174
1175 rcu_assign_pointer(chain->stats, newstats);
1176 synchronize_rcu();
1177 free_percpu(oldstats);
1178 } else
1179 rcu_assign_pointer(chain->stats, newstats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001180}
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001181
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001182static void nf_tables_chain_destroy(struct nft_chain *chain)
1183{
1184 BUG_ON(chain->use > 0);
1185
1186 if (chain->flags & NFT_BASE_CHAIN) {
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +02001187 struct nft_base_chain *basechain = nft_base_chain(chain);
1188
1189 module_put(basechain->type->owner);
1190 free_percpu(basechain->stats);
1191 if (basechain->ops[0].dev != NULL)
1192 dev_put(basechain->ops[0].dev);
1193 kfree(basechain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001194 } else {
1195 kfree(chain);
1196 }
1197}
1198
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01001199static int nf_tables_newchain(struct net *net, struct sock *nlsk,
1200 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02001201 const struct nlattr * const nla[])
1202{
1203 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1204 const struct nlattr * uninitialized_var(name);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001205 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001206 struct nft_table *table;
1207 struct nft_chain *chain;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001208 struct nft_base_chain *basechain = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001209 struct nlattr *ha[NFTA_HOOK_MAX + 1];
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001210 u8 genmask = nft_genmask_next(net);
Patrick McHardy96518512013-10-14 11:00:02 +02001211 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +02001212 struct net_device *dev = NULL;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001213 u8 policy = NF_ACCEPT;
Patrick McHardy96518512013-10-14 11:00:02 +02001214 u64 handle = 0;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001215 unsigned int i;
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001216 struct nft_stats __percpu *stats;
Patrick McHardy96518512013-10-14 11:00:02 +02001217 int err;
1218 bool create;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001219 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001220
1221 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1222
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001223 afi = nf_tables_afinfo_lookup(net, family, true);
Patrick McHardy96518512013-10-14 11:00:02 +02001224 if (IS_ERR(afi))
1225 return PTR_ERR(afi);
1226
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001227 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001228 if (IS_ERR(table))
1229 return PTR_ERR(table);
1230
Patrick McHardy96518512013-10-14 11:00:02 +02001231 chain = NULL;
1232 name = nla[NFTA_CHAIN_NAME];
1233
1234 if (nla[NFTA_CHAIN_HANDLE]) {
1235 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02001236 chain = nf_tables_chain_lookup_byhandle(table, handle, genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001237 if (IS_ERR(chain))
1238 return PTR_ERR(chain);
1239 } else {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02001240 chain = nf_tables_chain_lookup(table, name, genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001241 if (IS_ERR(chain)) {
1242 if (PTR_ERR(chain) != -ENOENT)
1243 return PTR_ERR(chain);
1244 chain = NULL;
1245 }
1246 }
1247
Patrick McHardy57de2a02014-01-09 18:42:31 +00001248 if (nla[NFTA_CHAIN_POLICY]) {
1249 if ((chain != NULL &&
Pablo Neira Ayusod6b6cb12015-03-17 13:21:42 +01001250 !(chain->flags & NFT_BASE_CHAIN)))
1251 return -EOPNOTSUPP;
1252
1253 if (chain == NULL &&
Patrick McHardy57de2a02014-01-09 18:42:31 +00001254 nla[NFTA_CHAIN_HOOK] == NULL)
1255 return -EOPNOTSUPP;
1256
Pablo Neira Ayuso8f46df12014-01-10 15:11:25 +01001257 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
Patrick McHardy57de2a02014-01-09 18:42:31 +00001258 switch (policy) {
1259 case NF_DROP:
1260 case NF_ACCEPT:
1261 break;
1262 default:
1263 return -EINVAL;
1264 }
1265 }
1266
Patrick McHardy96518512013-10-14 11:00:02 +02001267 if (chain != NULL) {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001268 struct nft_stats *stats = NULL;
1269 struct nft_trans *trans;
1270
Patrick McHardy96518512013-10-14 11:00:02 +02001271 if (nlh->nlmsg_flags & NLM_F_EXCL)
1272 return -EEXIST;
1273 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1274 return -EOPNOTSUPP;
1275
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02001276 if (nla[NFTA_CHAIN_HANDLE] && name) {
1277 struct nft_chain *chain2;
1278
1279 chain2 = nf_tables_chain_lookup(table,
1280 nla[NFTA_CHAIN_NAME],
1281 genmask);
1282 if (IS_ERR(chain2))
1283 return PTR_ERR(chain2);
1284 }
Patrick McHardy96518512013-10-14 11:00:02 +02001285
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001286 if (nla[NFTA_CHAIN_COUNTERS]) {
1287 if (!(chain->flags & NFT_BASE_CHAIN))
1288 return -EOPNOTSUPP;
1289
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001290 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1291 if (IS_ERR(stats))
1292 return PTR_ERR(stats);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001293 }
1294
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01001295 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001296 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1297 sizeof(struct nft_trans_chain));
Pablo Neira Ayusof5553c12015-01-29 19:08:09 +01001298 if (trans == NULL) {
1299 free_percpu(stats);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001300 return -ENOMEM;
Pablo Neira Ayusof5553c12015-01-29 19:08:09 +01001301 }
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001302
1303 nft_trans_chain_stats(trans) = stats;
1304 nft_trans_chain_update(trans) = true;
1305
Patrick McHardy4401a862014-01-09 18:42:32 +00001306 if (nla[NFTA_CHAIN_POLICY])
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001307 nft_trans_chain_policy(trans) = policy;
1308 else
1309 nft_trans_chain_policy(trans) = -1;
Patrick McHardy4401a862014-01-09 18:42:32 +00001310
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001311 if (nla[NFTA_CHAIN_HANDLE] && name) {
1312 nla_strlcpy(nft_trans_chain_name(trans), name,
1313 NFT_CHAIN_MAXNAMELEN);
1314 }
1315 list_add_tail(&trans->list, &net->nft.commit_list);
1316 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001317 }
1318
Patrick McHardy75820672014-01-09 18:42:33 +00001319 if (table->use == UINT_MAX)
1320 return -EOVERFLOW;
1321
Patrick McHardy96518512013-10-14 11:00:02 +02001322 if (nla[NFTA_CHAIN_HOOK]) {
Patrick McHardy2a37d752014-01-09 18:42:37 +00001323 const struct nf_chain_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001324 struct nf_hook_ops *ops;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001325 nf_hookfn *hookfn;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001326 u32 hooknum, priority;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001327
Patrick McHardybaae3e62014-01-09 18:42:34 +00001328 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001329 if (nla[NFTA_CHAIN_TYPE]) {
1330 type = nf_tables_chain_type_lookup(afi,
1331 nla[NFTA_CHAIN_TYPE],
1332 create);
Patrick McHardy93b08062014-01-09 18:42:36 +00001333 if (IS_ERR(type))
1334 return PTR_ERR(type);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001335 }
Patrick McHardy96518512013-10-14 11:00:02 +02001336
1337 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1338 nft_hook_policy);
1339 if (err < 0)
1340 return err;
1341 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1342 ha[NFTA_HOOK_PRIORITY] == NULL)
1343 return -EINVAL;
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001344
1345 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1346 if (hooknum >= afi->nhooks)
Patrick McHardy96518512013-10-14 11:00:02 +02001347 return -EINVAL;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001348 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
Patrick McHardy96518512013-10-14 11:00:02 +02001349
Patrick McHardybaae3e62014-01-09 18:42:34 +00001350 if (!(type->hook_mask & (1 << hooknum)))
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001351 return -EOPNOTSUPP;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001352 if (!try_module_get(type->owner))
Patrick McHardybaae3e62014-01-09 18:42:34 +00001353 return -ENOENT;
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001354 hookfn = type->hooks[hooknum];
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001355
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +02001356 if (afi->flags & NFT_AF_NEEDS_DEV) {
1357 char ifname[IFNAMSIZ];
1358
1359 if (!ha[NFTA_HOOK_DEV]) {
1360 module_put(type->owner);
1361 return -EOPNOTSUPP;
1362 }
1363
1364 nla_strlcpy(ifname, ha[NFTA_HOOK_DEV], IFNAMSIZ);
1365 dev = dev_get_by_name(net, ifname);
1366 if (!dev) {
1367 module_put(type->owner);
1368 return -ENOENT;
1369 }
1370 } else if (ha[NFTA_HOOK_DEV]) {
1371 module_put(type->owner);
1372 return -EOPNOTSUPP;
1373 }
1374
Patrick McHardy96518512013-10-14 11:00:02 +02001375 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
Pablo Neira Ayusof5553c12015-01-29 19:08:09 +01001376 if (basechain == NULL) {
1377 module_put(type->owner);
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +02001378 if (dev != NULL)
1379 dev_put(dev);
Patrick McHardy96518512013-10-14 11:00:02 +02001380 return -ENOMEM;
Pablo Neira Ayusof5553c12015-01-29 19:08:09 +01001381 }
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001382
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +02001383 if (dev != NULL)
1384 strncpy(basechain->dev_name, dev->name, IFNAMSIZ);
1385
Patrick McHardy4401a862014-01-09 18:42:32 +00001386 if (nla[NFTA_CHAIN_COUNTERS]) {
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001387 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1388 if (IS_ERR(stats)) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001389 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +00001390 kfree(basechain);
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +02001391 if (dev != NULL)
1392 dev_put(dev);
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001393 return PTR_ERR(stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001394 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001395 basechain->stats = stats;
Patrick McHardy4401a862014-01-09 18:42:32 +00001396 } else {
Eric Dumazetce355e22014-07-09 15:14:06 +02001397 stats = netdev_alloc_pcpu_stats(struct nft_stats);
Sabrina Dubrocac123bb72014-10-21 11:08:21 +02001398 if (stats == NULL) {
Patrick McHardyfa2c1de2014-01-09 18:42:38 +00001399 module_put(type->owner);
Patrick McHardy4401a862014-01-09 18:42:32 +00001400 kfree(basechain);
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +02001401 if (dev != NULL)
1402 dev_put(dev);
Sabrina Dubrocac123bb72014-10-21 11:08:21 +02001403 return -ENOMEM;
Patrick McHardy4401a862014-01-09 18:42:32 +00001404 }
Pablo Neira Ayusoff3cd7b2014-04-09 11:53:06 +02001405 rcu_assign_pointer(basechain->stats, stats);
Patrick McHardy4401a862014-01-09 18:42:32 +00001406 }
1407
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001408 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +02001409 chain = &basechain->chain;
1410
Patrick McHardy115a60b2014-01-03 12:16:15 +00001411 for (i = 0; i < afi->nops; i++) {
1412 ops = &basechain->ops[i];
1413 ops->pf = family;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001414 ops->hooknum = hooknum;
1415 ops->priority = priority;
1416 ops->priv = chain;
1417 ops->hook = afi->hooks[ops->hooknum];
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +02001418 ops->dev = dev;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001419 if (hookfn)
1420 ops->hook = hookfn;
1421 if (afi->hook_ops_init)
1422 afi->hook_ops_init(ops, i);
1423 }
Patrick McHardy96518512013-10-14 11:00:02 +02001424
1425 chain->flags |= NFT_BASE_CHAIN;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001426 basechain->policy = policy;
Patrick McHardy96518512013-10-14 11:00:02 +02001427 } else {
1428 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1429 if (chain == NULL)
1430 return -ENOMEM;
1431 }
1432
1433 INIT_LIST_HEAD(&chain->rules);
1434 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +02001435 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +02001436 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1437
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +02001438 err = nf_tables_register_hooks(net, table, chain, afi->nops);
Pablo Neira Ayusod8ee8f72015-06-15 02:42:31 +02001439 if (err < 0)
1440 goto err1;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001441
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01001442 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001443 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1444 if (err < 0)
1445 goto err2;
1446
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001447 table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001448 list_add_tail_rcu(&chain->list, &table->chains);
Patrick McHardy96518512013-10-14 11:00:02 +02001449 return 0;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001450err2:
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +02001451 nf_tables_unregister_hooks(net, table, chain, afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001452err1:
1453 nf_tables_chain_destroy(chain);
1454 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001455}
1456
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01001457static int nf_tables_delchain(struct net *net, struct sock *nlsk,
1458 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02001459 const struct nlattr * const nla[])
1460{
1461 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001462 u8 genmask = nft_genmask_next(net);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001463 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001464 struct nft_table *table;
1465 struct nft_chain *chain;
1466 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001467 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001468
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001469 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001470 if (IS_ERR(afi))
1471 return PTR_ERR(afi);
1472
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001473 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001474 if (IS_ERR(table))
1475 return PTR_ERR(table);
1476
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02001477 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001478 if (IS_ERR(chain))
1479 return PTR_ERR(chain);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001480 if (chain->use > 0)
Patrick McHardy96518512013-10-14 11:00:02 +02001481 return -EBUSY;
1482
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01001483 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001484
Arturo Borreroee01d542014-09-02 16:42:25 +02001485 return nft_delchain(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +02001486}
1487
Patrick McHardy96518512013-10-14 11:00:02 +02001488/*
1489 * Expressions
1490 */
1491
1492/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001493 * nft_register_expr - register nf_tables expr type
1494 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001495 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001496 * Registers the expr type for use with nf_tables. Returns zero on
Patrick McHardy96518512013-10-14 11:00:02 +02001497 * success or a negative errno code otherwise.
1498 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001499int nft_register_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001500{
1501 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001502 if (type->family == NFPROTO_UNSPEC)
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001503 list_add_tail_rcu(&type->list, &nf_tables_expressions);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001504 else
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001505 list_add_rcu(&type->list, &nf_tables_expressions);
Patrick McHardy96518512013-10-14 11:00:02 +02001506 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1507 return 0;
1508}
1509EXPORT_SYMBOL_GPL(nft_register_expr);
1510
1511/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001512 * nft_unregister_expr - unregister nf_tables expr type
1513 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001514 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001515 * Unregisters the expr typefor use with nf_tables.
Patrick McHardy96518512013-10-14 11:00:02 +02001516 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001517void nft_unregister_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001518{
1519 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001520 list_del_rcu(&type->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001521 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1522}
1523EXPORT_SYMBOL_GPL(nft_unregister_expr);
1524
Patrick McHardy64d46802014-02-05 15:03:37 +00001525static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1526 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001527{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001528 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001529
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001530 list_for_each_entry(type, &nf_tables_expressions, list) {
Patrick McHardy64d46802014-02-05 15:03:37 +00001531 if (!nla_strcmp(nla, type->name) &&
1532 (!type->family || type->family == family))
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001533 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001534 }
1535 return NULL;
1536}
1537
Patrick McHardy64d46802014-02-05 15:03:37 +00001538static const struct nft_expr_type *nft_expr_type_get(u8 family,
1539 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001540{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001541 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001542
1543 if (nla == NULL)
1544 return ERR_PTR(-EINVAL);
1545
Patrick McHardy64d46802014-02-05 15:03:37 +00001546 type = __nft_expr_type_get(family, nla);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001547 if (type != NULL && try_module_get(type->owner))
1548 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001549
1550#ifdef CONFIG_MODULES
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001551 if (type == NULL) {
Patrick McHardy96518512013-10-14 11:00:02 +02001552 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001553 request_module("nft-expr-%u-%.*s", family,
1554 nla_len(nla), (char *)nla_data(nla));
1555 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1556 if (__nft_expr_type_get(family, nla))
1557 return ERR_PTR(-EAGAIN);
1558
1559 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy96518512013-10-14 11:00:02 +02001560 request_module("nft-expr-%.*s",
1561 nla_len(nla), (char *)nla_data(nla));
1562 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001563 if (__nft_expr_type_get(family, nla))
Patrick McHardy96518512013-10-14 11:00:02 +02001564 return ERR_PTR(-EAGAIN);
1565 }
1566#endif
1567 return ERR_PTR(-ENOENT);
1568}
1569
1570static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1571 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1572 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1573};
1574
1575static int nf_tables_fill_expr_info(struct sk_buff *skb,
1576 const struct nft_expr *expr)
1577{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001578 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
Patrick McHardy96518512013-10-14 11:00:02 +02001579 goto nla_put_failure;
1580
1581 if (expr->ops->dump) {
1582 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1583 if (data == NULL)
1584 goto nla_put_failure;
1585 if (expr->ops->dump(skb, expr) < 0)
1586 goto nla_put_failure;
1587 nla_nest_end(skb, data);
1588 }
1589
1590 return skb->len;
1591
1592nla_put_failure:
1593 return -1;
1594};
1595
Patrick McHardy0b2d8a72015-04-11 10:46:38 +01001596int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
1597 const struct nft_expr *expr)
1598{
1599 struct nlattr *nest;
1600
1601 nest = nla_nest_start(skb, attr);
1602 if (!nest)
1603 goto nla_put_failure;
1604 if (nf_tables_fill_expr_info(skb, expr) < 0)
1605 goto nla_put_failure;
1606 nla_nest_end(skb, nest);
1607 return 0;
1608
1609nla_put_failure:
1610 return -1;
1611}
1612
Patrick McHardy96518512013-10-14 11:00:02 +02001613struct nft_expr_info {
1614 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001615 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001616};
1617
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001618static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1619 const struct nlattr *nla,
Patrick McHardy96518512013-10-14 11:00:02 +02001620 struct nft_expr_info *info)
1621{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001622 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001623 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001624 struct nlattr *tb[NFTA_EXPR_MAX + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001625 int err;
1626
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001627 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
Patrick McHardy96518512013-10-14 11:00:02 +02001628 if (err < 0)
1629 return err;
1630
Patrick McHardy64d46802014-02-05 15:03:37 +00001631 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001632 if (IS_ERR(type))
1633 return PTR_ERR(type);
1634
1635 if (tb[NFTA_EXPR_DATA]) {
1636 err = nla_parse_nested(info->tb, type->maxattr,
1637 tb[NFTA_EXPR_DATA], type->policy);
1638 if (err < 0)
1639 goto err1;
1640 } else
1641 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1642
1643 if (type->select_ops != NULL) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001644 ops = type->select_ops(ctx,
1645 (const struct nlattr * const *)info->tb);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001646 if (IS_ERR(ops)) {
1647 err = PTR_ERR(ops);
1648 goto err1;
1649 }
1650 } else
1651 ops = type->ops;
1652
Patrick McHardy96518512013-10-14 11:00:02 +02001653 info->ops = ops;
1654 return 0;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001655
1656err1:
1657 module_put(type->owner);
1658 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001659}
1660
1661static int nf_tables_newexpr(const struct nft_ctx *ctx,
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001662 const struct nft_expr_info *info,
Patrick McHardy96518512013-10-14 11:00:02 +02001663 struct nft_expr *expr)
1664{
1665 const struct nft_expr_ops *ops = info->ops;
1666 int err;
1667
1668 expr->ops = ops;
1669 if (ops->init) {
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001670 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
Patrick McHardy96518512013-10-14 11:00:02 +02001671 if (err < 0)
1672 goto err1;
1673 }
1674
Patrick McHardy96518512013-10-14 11:00:02 +02001675 return 0;
1676
1677err1:
1678 expr->ops = NULL;
1679 return err;
1680}
1681
Patrick McHardy62472bc2014-03-07 19:08:30 +01001682static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1683 struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +02001684{
1685 if (expr->ops->destroy)
Patrick McHardy62472bc2014-03-07 19:08:30 +01001686 expr->ops->destroy(ctx, expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001687 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001688}
1689
Patrick McHardy0b2d8a72015-04-11 10:46:38 +01001690struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
1691 const struct nlattr *nla)
1692{
1693 struct nft_expr_info info;
1694 struct nft_expr *expr;
1695 int err;
1696
1697 err = nf_tables_expr_parse(ctx, nla, &info);
1698 if (err < 0)
1699 goto err1;
1700
1701 err = -ENOMEM;
1702 expr = kzalloc(info.ops->size, GFP_KERNEL);
1703 if (expr == NULL)
1704 goto err2;
1705
1706 err = nf_tables_newexpr(ctx, &info, expr);
1707 if (err < 0)
1708 goto err2;
1709
1710 return expr;
1711err2:
1712 module_put(info.ops->type->owner);
1713err1:
1714 return ERR_PTR(err);
1715}
1716
1717void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
1718{
1719 nf_tables_expr_destroy(ctx, expr);
1720 kfree(expr);
1721}
1722
Patrick McHardy96518512013-10-14 11:00:02 +02001723/*
1724 * Rules
1725 */
1726
1727static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1728 u64 handle)
1729{
1730 struct nft_rule *rule;
1731
1732 // FIXME: this sucks
1733 list_for_each_entry(rule, &chain->rules, list) {
1734 if (handle == rule->handle)
1735 return rule;
1736 }
1737
1738 return ERR_PTR(-ENOENT);
1739}
1740
1741static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1742 const struct nlattr *nla)
1743{
1744 if (nla == NULL)
1745 return ERR_PTR(-EINVAL);
1746
1747 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1748}
1749
1750static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1751 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1752 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1753 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1754 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1755 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001756 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001757 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001758 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1759 .len = NFT_USERDATA_MAXLEN },
Patrick McHardy96518512013-10-14 11:00:02 +02001760};
1761
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001762static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1763 u32 portid, u32 seq, int event,
1764 u32 flags, int family,
Patrick McHardy96518512013-10-14 11:00:02 +02001765 const struct nft_table *table,
1766 const struct nft_chain *chain,
1767 const struct nft_rule *rule)
1768{
1769 struct nlmsghdr *nlh;
1770 struct nfgenmsg *nfmsg;
1771 const struct nft_expr *expr, *next;
1772 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001773 const struct nft_rule *prule;
1774 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001775
Eric Leblond5e948462013-10-10 13:41:44 +02001776 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001777 flags);
1778 if (nlh == NULL)
1779 goto nla_put_failure;
1780
1781 nfmsg = nlmsg_data(nlh);
1782 nfmsg->nfgen_family = family;
1783 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001784 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +02001785
1786 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1787 goto nla_put_failure;
1788 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1789 goto nla_put_failure;
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +02001790 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle),
1791 NFTA_RULE_PAD))
Patrick McHardy96518512013-10-14 11:00:02 +02001792 goto nla_put_failure;
1793
Eric Leblond5e948462013-10-10 13:41:44 +02001794 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1795 prule = list_entry(rule->list.prev, struct nft_rule, list);
1796 if (nla_put_be64(skb, NFTA_RULE_POSITION,
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +02001797 cpu_to_be64(prule->handle),
1798 NFTA_RULE_PAD))
Eric Leblond5e948462013-10-10 13:41:44 +02001799 goto nla_put_failure;
1800 }
1801
Patrick McHardy96518512013-10-14 11:00:02 +02001802 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1803 if (list == NULL)
1804 goto nla_put_failure;
1805 nft_rule_for_each_expr(expr, next, rule) {
Patrick McHardy0b2d8a72015-04-11 10:46:38 +01001806 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0)
Patrick McHardy96518512013-10-14 11:00:02 +02001807 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +02001808 }
1809 nla_nest_end(skb, list);
1810
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001811 if (rule->udata) {
1812 struct nft_userdata *udata = nft_userdata(rule);
1813 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
1814 udata->data) < 0)
1815 goto nla_put_failure;
1816 }
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001817
Johannes Berg053c0952015-01-16 22:09:00 +01001818 nlmsg_end(skb, nlh);
1819 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001820
1821nla_put_failure:
1822 nlmsg_trim(skb, nlh);
1823 return -1;
1824}
1825
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001826static int nf_tables_rule_notify(const struct nft_ctx *ctx,
Patrick McHardy96518512013-10-14 11:00:02 +02001827 const struct nft_rule *rule,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001828 int event)
Patrick McHardy96518512013-10-14 11:00:02 +02001829{
1830 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +02001831 int err;
1832
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001833 if (!ctx->report &&
1834 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +02001835 return 0;
1836
1837 err = -ENOBUFS;
1838 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1839 if (skb == NULL)
1840 goto err;
1841
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001842 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
1843 event, 0, ctx->afi->family, ctx->table,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001844 ctx->chain, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001845 if (err < 0) {
1846 kfree_skb(skb);
1847 goto err;
1848 }
1849
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001850 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1851 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001852err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001853 if (err < 0) {
1854 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1855 err);
1856 }
Patrick McHardy96518512013-10-14 11:00:02 +02001857 return err;
1858}
1859
Pablo Neira Ayuso6e1f7602016-07-19 12:20:45 +02001860struct nft_rule_dump_ctx {
1861 char table[NFT_TABLE_MAXNAMELEN];
1862 char chain[NFT_CHAIN_MAXNAMELEN];
1863};
1864
Patrick McHardy96518512013-10-14 11:00:02 +02001865static int nf_tables_dump_rules(struct sk_buff *skb,
1866 struct netlink_callback *cb)
1867{
1868 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
Pablo Neira Ayuso6e1f7602016-07-19 12:20:45 +02001869 const struct nft_rule_dump_ctx *ctx = cb->data;
Patrick McHardy96518512013-10-14 11:00:02 +02001870 const struct nft_af_info *afi;
1871 const struct nft_table *table;
1872 const struct nft_chain *chain;
1873 const struct nft_rule *rule;
1874 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001875 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001876 int family = nfmsg->nfgen_family;
1877
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001878 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001879 cb->seq = net->nft.base_seq;
1880
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001881 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001882 if (family != NFPROTO_UNSPEC && family != afi->family)
1883 continue;
1884
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001885 list_for_each_entry_rcu(table, &afi->tables, list) {
Pablo Neira Ayuso6e1f7602016-07-19 12:20:45 +02001886 if (ctx && ctx->table[0] &&
1887 strcmp(ctx->table, table->name) != 0)
1888 continue;
1889
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001890 list_for_each_entry_rcu(chain, &table->chains, list) {
Pablo Neira Ayuso6e1f7602016-07-19 12:20:45 +02001891 if (ctx && ctx->chain[0] &&
1892 strcmp(ctx->chain, chain->name) != 0)
1893 continue;
1894
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001895 list_for_each_entry_rcu(rule, &chain->rules, list) {
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02001896 if (!nft_is_active(net, rule))
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001897 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001898 if (idx < s_idx)
1899 goto cont;
1900 if (idx > s_idx)
1901 memset(&cb->args[1], 0,
1902 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001903 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001904 cb->nlh->nlmsg_seq,
1905 NFT_MSG_NEWRULE,
1906 NLM_F_MULTI | NLM_F_APPEND,
1907 afi->family, table, chain, rule) < 0)
1908 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001909
1910 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +02001911cont:
1912 idx++;
1913 }
1914 }
1915 }
1916 }
1917done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001918 rcu_read_unlock();
1919
Patrick McHardy96518512013-10-14 11:00:02 +02001920 cb->args[0] = idx;
1921 return skb->len;
1922}
1923
Pablo Neira Ayuso6e1f7602016-07-19 12:20:45 +02001924static int nf_tables_dump_rules_done(struct netlink_callback *cb)
1925{
1926 kfree(cb->data);
1927 return 0;
1928}
1929
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +01001930static int nf_tables_getrule(struct net *net, struct sock *nlsk,
1931 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02001932 const struct nlattr * const nla[])
1933{
1934 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001935 u8 genmask = nft_genmask_cur(net);
Patrick McHardy96518512013-10-14 11:00:02 +02001936 const struct nft_af_info *afi;
1937 const struct nft_table *table;
1938 const struct nft_chain *chain;
1939 const struct nft_rule *rule;
1940 struct sk_buff *skb2;
1941 int family = nfmsg->nfgen_family;
1942 int err;
1943
1944 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1945 struct netlink_dump_control c = {
1946 .dump = nf_tables_dump_rules,
Pablo Neira Ayuso6e1f7602016-07-19 12:20:45 +02001947 .done = nf_tables_dump_rules_done,
Patrick McHardy96518512013-10-14 11:00:02 +02001948 };
Pablo Neira Ayuso6e1f7602016-07-19 12:20:45 +02001949
1950 if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) {
1951 struct nft_rule_dump_ctx *ctx;
1952
1953 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1954 if (!ctx)
1955 return -ENOMEM;
1956
1957 if (nla[NFTA_RULE_TABLE])
1958 nla_strlcpy(ctx->table, nla[NFTA_RULE_TABLE],
1959 sizeof(ctx->table));
1960 if (nla[NFTA_RULE_CHAIN])
1961 nla_strlcpy(ctx->chain, nla[NFTA_RULE_CHAIN],
1962 sizeof(ctx->chain));
1963 c.data = ctx;
1964 }
1965
Patrick McHardy96518512013-10-14 11:00:02 +02001966 return netlink_dump_start(nlsk, skb, nlh, &c);
1967 }
1968
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001969 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001970 if (IS_ERR(afi))
1971 return PTR_ERR(afi);
1972
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001973 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001974 if (IS_ERR(table))
1975 return PTR_ERR(table);
1976
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02001977 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001978 if (IS_ERR(chain))
1979 return PTR_ERR(chain);
1980
1981 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1982 if (IS_ERR(rule))
1983 return PTR_ERR(rule);
1984
1985 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1986 if (!skb2)
1987 return -ENOMEM;
1988
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001989 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001990 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1991 family, table, chain, rule);
1992 if (err < 0)
1993 goto err;
1994
1995 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1996
1997err:
1998 kfree_skb(skb2);
1999 return err;
2000}
2001
Patrick McHardy62472bc2014-03-07 19:08:30 +01002002static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
2003 struct nft_rule *rule)
Patrick McHardy96518512013-10-14 11:00:02 +02002004{
Patrick McHardy96518512013-10-14 11:00:02 +02002005 struct nft_expr *expr;
2006
2007 /*
2008 * Careful: some expressions might not be initialized in case this
2009 * is called on error from nf_tables_newrule().
2010 */
2011 expr = nft_expr_first(rule);
2012 while (expr->ops && expr != nft_expr_last(rule)) {
Patrick McHardy62472bc2014-03-07 19:08:30 +01002013 nf_tables_expr_destroy(ctx, expr);
Patrick McHardy96518512013-10-14 11:00:02 +02002014 expr = nft_expr_next(expr);
2015 }
2016 kfree(rule);
2017}
2018
Patrick McHardy96518512013-10-14 11:00:02 +02002019#define NFT_RULE_MAXEXPRS 128
2020
2021static struct nft_expr_info *info;
2022
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002023static int nf_tables_newrule(struct net *net, struct sock *nlsk,
2024 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02002025 const struct nlattr * const nla[])
2026{
2027 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002028 u8 genmask = nft_genmask_next(net);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002029 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02002030 struct nft_table *table;
2031 struct nft_chain *chain;
2032 struct nft_rule *rule, *old_rule = NULL;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002033 struct nft_userdata *udata;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02002034 struct nft_trans *trans = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02002035 struct nft_expr *expr;
2036 struct nft_ctx ctx;
2037 struct nlattr *tmp;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002038 unsigned int size, i, n, ulen = 0, usize = 0;
Patrick McHardy96518512013-10-14 11:00:02 +02002039 int err, rem;
2040 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02002041 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02002042
2043 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2044
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002045 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02002046 if (IS_ERR(afi))
2047 return PTR_ERR(afi);
2048
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002049 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02002050 if (IS_ERR(table))
2051 return PTR_ERR(table);
2052
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02002053 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02002054 if (IS_ERR(chain))
2055 return PTR_ERR(chain);
2056
2057 if (nla[NFTA_RULE_HANDLE]) {
2058 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
2059 rule = __nf_tables_rule_lookup(chain, handle);
2060 if (IS_ERR(rule))
2061 return PTR_ERR(rule);
2062
2063 if (nlh->nlmsg_flags & NLM_F_EXCL)
2064 return -EEXIST;
2065 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2066 old_rule = rule;
2067 else
2068 return -EOPNOTSUPP;
2069 } else {
2070 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
2071 return -EINVAL;
2072 handle = nf_tables_alloc_handle(table);
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +02002073
2074 if (chain->use == UINT_MAX)
2075 return -EOVERFLOW;
Patrick McHardy96518512013-10-14 11:00:02 +02002076 }
2077
Eric Leblond5e948462013-10-10 13:41:44 +02002078 if (nla[NFTA_RULE_POSITION]) {
2079 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2080 return -EOPNOTSUPP;
2081
2082 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
2083 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
2084 if (IS_ERR(old_rule))
2085 return PTR_ERR(old_rule);
2086 }
2087
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002088 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002089
Patrick McHardy96518512013-10-14 11:00:02 +02002090 n = 0;
2091 size = 0;
2092 if (nla[NFTA_RULE_EXPRESSIONS]) {
2093 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
2094 err = -EINVAL;
2095 if (nla_type(tmp) != NFTA_LIST_ELEM)
2096 goto err1;
2097 if (n == NFT_RULE_MAXEXPRS)
2098 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002099 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02002100 if (err < 0)
2101 goto err1;
2102 size += info[n].ops->size;
2103 n++;
2104 }
2105 }
Patrick McHardy98898402015-03-03 20:04:19 +00002106 /* Check for overflow of dlen field */
2107 err = -EFBIG;
2108 if (size >= 1 << 12)
2109 goto err1;
Patrick McHardy96518512013-10-14 11:00:02 +02002110
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002111 if (nla[NFTA_RULE_USERDATA]) {
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01002112 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002113 if (ulen > 0)
2114 usize = sizeof(struct nft_userdata) + ulen;
2115 }
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01002116
Patrick McHardy96518512013-10-14 11:00:02 +02002117 err = -ENOMEM;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002118 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02002119 if (rule == NULL)
2120 goto err1;
2121
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02002122 nft_activate_next(net, rule);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002123
Patrick McHardy96518512013-10-14 11:00:02 +02002124 rule->handle = handle;
2125 rule->dlen = size;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002126 rule->udata = ulen ? 1 : 0;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01002127
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002128 if (ulen) {
2129 udata = nft_userdata(rule);
2130 udata->len = ulen - 1;
2131 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
2132 }
Patrick McHardy96518512013-10-14 11:00:02 +02002133
Patrick McHardy96518512013-10-14 11:00:02 +02002134 expr = nft_expr_first(rule);
2135 for (i = 0; i < n; i++) {
2136 err = nf_tables_newexpr(&ctx, &info[i], expr);
2137 if (err < 0)
2138 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002139 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02002140 expr = nft_expr_next(expr);
2141 }
2142
Patrick McHardy96518512013-10-14 11:00:02 +02002143 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02002144 if (nft_is_active_next(net, old_rule)) {
Pablo Neira Ayusoac904ac2014-06-10 10:53:03 +02002145 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02002146 old_rule);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02002147 if (trans == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002148 err = -ENOMEM;
2149 goto err2;
2150 }
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02002151 nft_deactivate_next(net, old_rule);
Pablo Neira Ayusoac34b862014-06-10 10:53:02 +02002152 chain->use--;
Pablo Neira Ayuso5bc5c302014-06-10 10:53:00 +02002153 list_add_tail_rcu(&rule->list, &old_rule->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002154 } else {
2155 err = -ENOENT;
2156 goto err2;
2157 }
Patrick McHardy96518512013-10-14 11:00:02 +02002158 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02002159 if (old_rule)
2160 list_add_rcu(&rule->list, &old_rule->list);
2161 else
2162 list_add_tail_rcu(&rule->list, &chain->rules);
2163 else {
2164 if (old_rule)
2165 list_add_tail_rcu(&rule->list, &old_rule->list);
2166 else
2167 list_add_rcu(&rule->list, &chain->rules);
2168 }
Patrick McHardy96518512013-10-14 11:00:02 +02002169
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02002170 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002171 err = -ENOMEM;
2172 goto err3;
2173 }
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02002174 chain->use++;
Patrick McHardy96518512013-10-14 11:00:02 +02002175 return 0;
2176
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002177err3:
2178 list_del_rcu(&rule->list);
Patrick McHardy96518512013-10-14 11:00:02 +02002179err2:
Patrick McHardy62472bc2014-03-07 19:08:30 +01002180 nf_tables_rule_destroy(&ctx, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02002181err1:
2182 for (i = 0; i < n; i++) {
2183 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002184 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02002185 }
2186 return err;
2187}
2188
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002189static int nf_tables_delrule(struct net *net, struct sock *nlsk,
2190 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02002191 const struct nlattr * const nla[])
2192{
2193 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002194 u8 genmask = nft_genmask_next(net);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002195 struct nft_af_info *afi;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002196 struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002197 struct nft_chain *chain = NULL;
2198 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002199 int family = nfmsg->nfgen_family, err = 0;
2200 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02002201
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002202 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02002203 if (IS_ERR(afi))
2204 return PTR_ERR(afi);
2205
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002206 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02002207 if (IS_ERR(table))
2208 return PTR_ERR(table);
2209
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002210 if (nla[NFTA_RULE_CHAIN]) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02002211 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN],
2212 genmask);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002213 if (IS_ERR(chain))
2214 return PTR_ERR(chain);
2215 }
Patrick McHardy96518512013-10-14 11:00:02 +02002216
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002217 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002218
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002219 if (chain) {
2220 if (nla[NFTA_RULE_HANDLE]) {
2221 rule = nf_tables_rule_lookup(chain,
2222 nla[NFTA_RULE_HANDLE]);
2223 if (IS_ERR(rule))
2224 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02002225
Arturo Borrero5e266fe2014-09-02 16:42:21 +02002226 err = nft_delrule(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002227 } else {
Arturo Borreroce24b722014-09-02 16:42:24 +02002228 err = nft_delrule_by_chain(&ctx);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002229 }
2230 } else {
2231 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02002232 if (!nft_is_active_next(net, chain))
2233 continue;
2234
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002235 ctx.chain = chain;
Arturo Borreroce24b722014-09-02 16:42:24 +02002236 err = nft_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002237 if (err < 0)
2238 break;
Patrick McHardy96518512013-10-14 11:00:02 +02002239 }
2240 }
2241
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002242 return err;
2243}
2244
Patrick McHardy20a69342013-10-11 12:06:22 +02002245/*
2246 * Sets
2247 */
2248
2249static LIST_HEAD(nf_tables_set_ops);
2250
2251int nft_register_set(struct nft_set_ops *ops)
2252{
2253 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002254 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
Patrick McHardy20a69342013-10-11 12:06:22 +02002255 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2256 return 0;
2257}
2258EXPORT_SYMBOL_GPL(nft_register_set);
2259
2260void nft_unregister_set(struct nft_set_ops *ops)
2261{
2262 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002263 list_del_rcu(&ops->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002264 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2265}
2266EXPORT_SYMBOL_GPL(nft_unregister_set);
2267
Patrick McHardyc50b9602014-03-28 10:19:47 +00002268/*
2269 * Select a set implementation based on the data characteristics and the
2270 * given policy. The total memory use might not be known if no size is
2271 * given, in that case the amount of memory per element is used.
2272 */
2273static const struct nft_set_ops *
2274nft_select_set_ops(const struct nlattr * const nla[],
2275 const struct nft_set_desc *desc,
2276 enum nft_set_policies policy)
Patrick McHardy20a69342013-10-11 12:06:22 +02002277{
Patrick McHardyc50b9602014-03-28 10:19:47 +00002278 const struct nft_set_ops *ops, *bops;
2279 struct nft_set_estimate est, best;
Patrick McHardy20a69342013-10-11 12:06:22 +02002280 u32 features;
2281
2282#ifdef CONFIG_MODULES
2283 if (list_empty(&nf_tables_set_ops)) {
2284 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2285 request_module("nft-set");
2286 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2287 if (!list_empty(&nf_tables_set_ops))
2288 return ERR_PTR(-EAGAIN);
2289 }
2290#endif
2291 features = 0;
2292 if (nla[NFTA_SET_FLAGS] != NULL) {
2293 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
Patrick McHardy4a8678e2015-04-05 14:41:05 +02002294 features &= NFT_SET_INTERVAL | NFT_SET_MAP | NFT_SET_TIMEOUT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002295 }
2296
Patrick McHardyc50b9602014-03-28 10:19:47 +00002297 bops = NULL;
2298 best.size = ~0;
2299 best.class = ~0;
2300
Patrick McHardy20a69342013-10-11 12:06:22 +02002301 list_for_each_entry(ops, &nf_tables_set_ops, list) {
2302 if ((ops->features & features) != features)
2303 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002304 if (!ops->estimate(desc, features, &est))
2305 continue;
2306
2307 switch (policy) {
2308 case NFT_SET_POL_PERFORMANCE:
2309 if (est.class < best.class)
2310 break;
2311 if (est.class == best.class && est.size < best.size)
2312 break;
2313 continue;
2314 case NFT_SET_POL_MEMORY:
2315 if (est.size < best.size)
2316 break;
2317 if (est.size == best.size && est.class < best.class)
2318 break;
2319 continue;
2320 default:
2321 break;
2322 }
2323
Patrick McHardy20a69342013-10-11 12:06:22 +02002324 if (!try_module_get(ops->owner))
2325 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002326 if (bops != NULL)
2327 module_put(bops->owner);
2328
2329 bops = ops;
2330 best = est;
Patrick McHardy20a69342013-10-11 12:06:22 +02002331 }
2332
Patrick McHardyc50b9602014-03-28 10:19:47 +00002333 if (bops != NULL)
2334 return bops;
2335
Patrick McHardy20a69342013-10-11 12:06:22 +02002336 return ERR_PTR(-EOPNOTSUPP);
2337}
2338
2339static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2340 [NFTA_SET_TABLE] = { .type = NLA_STRING },
Pablo Neira Ayusoa9bdd832014-03-24 15:10:37 +01002341 [NFTA_SET_NAME] = { .type = NLA_STRING,
Pablo Neira Ayusocb39ad82016-05-04 17:49:53 +02002342 .len = NFT_SET_MAXNAMELEN - 1 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002343 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2344 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2345 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2346 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2347 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002348 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2349 [NFTA_SET_DESC] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002350 [NFTA_SET_ID] = { .type = NLA_U32 },
Patrick McHardy761da292015-03-26 12:39:36 +00002351 [NFTA_SET_TIMEOUT] = { .type = NLA_U64 },
2352 [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 },
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002353 [NFTA_SET_USERDATA] = { .type = NLA_BINARY,
2354 .len = NFT_USERDATA_MAXLEN },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002355};
2356
2357static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2358 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002359};
2360
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002361static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net,
Patrick McHardy20a69342013-10-11 12:06:22 +02002362 const struct sk_buff *skb,
2363 const struct nlmsghdr *nlh,
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002364 const struct nlattr * const nla[],
2365 u8 genmask)
Patrick McHardy20a69342013-10-11 12:06:22 +02002366{
2367 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002368 struct nft_af_info *afi = NULL;
2369 struct nft_table *table = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02002370
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002371 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2372 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2373 if (IS_ERR(afi))
2374 return PTR_ERR(afi);
2375 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002376
2377 if (nla[NFTA_SET_TABLE] != NULL) {
Patrick McHardyec2c9932014-02-05 15:03:35 +00002378 if (afi == NULL)
2379 return -EAFNOSUPPORT;
2380
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002381 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE],
2382 genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002383 if (IS_ERR(table))
2384 return PTR_ERR(table);
2385 }
2386
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002387 nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002388 return 0;
2389}
2390
2391struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002392 const struct nlattr *nla, u8 genmask)
Patrick McHardy20a69342013-10-11 12:06:22 +02002393{
2394 struct nft_set *set;
2395
2396 if (nla == NULL)
2397 return ERR_PTR(-EINVAL);
2398
2399 list_for_each_entry(set, &table->sets, list) {
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002400 if (!nla_strcmp(nla, set->name) &&
2401 nft_active_genmask(set, genmask))
Patrick McHardy20a69342013-10-11 12:06:22 +02002402 return set;
2403 }
2404 return ERR_PTR(-ENOENT);
2405}
2406
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002407struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002408 const struct nlattr *nla,
2409 u8 genmask)
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002410{
2411 struct nft_trans *trans;
2412 u32 id = ntohl(nla_get_be32(nla));
2413
2414 list_for_each_entry(trans, &net->nft.commit_list, list) {
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002415 struct nft_set *set = nft_trans_set(trans);
2416
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002417 if (trans->msg_type == NFT_MSG_NEWSET &&
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002418 id == nft_trans_set_id(trans) &&
2419 nft_active_genmask(set, genmask))
2420 return set;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002421 }
2422 return ERR_PTR(-ENOENT);
2423}
2424
Patrick McHardy20a69342013-10-11 12:06:22 +02002425static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2426 const char *name)
2427{
2428 const struct nft_set *i;
2429 const char *p;
2430 unsigned long *inuse;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002431 unsigned int n = 0, min = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002432
Pablo Neira Ayusocb39ad82016-05-04 17:49:53 +02002433 p = strnchr(name, NFT_SET_MAXNAMELEN, '%');
Patrick McHardy20a69342013-10-11 12:06:22 +02002434 if (p != NULL) {
2435 if (p[1] != 'd' || strchr(p + 2, '%'))
2436 return -EINVAL;
2437
2438 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2439 if (inuse == NULL)
2440 return -ENOMEM;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002441cont:
Patrick McHardy20a69342013-10-11 12:06:22 +02002442 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01002443 int tmp;
2444
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002445 if (!nft_is_active_next(ctx->net, set))
2446 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002447 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02002448 continue;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002449 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02002450 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002451
Patrick McHardy60eb1892014-03-07 12:34:05 +01002452 set_bit(tmp - min, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02002453 }
2454
Patrick McHardy53b70282014-02-05 12:26:22 +01002455 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
Patrick McHardy60eb1892014-03-07 12:34:05 +01002456 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2457 min += BITS_PER_BYTE * PAGE_SIZE;
2458 memset(inuse, 0, PAGE_SIZE);
2459 goto cont;
2460 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002461 free_page((unsigned long)inuse);
2462 }
2463
Patrick McHardy60eb1892014-03-07 12:34:05 +01002464 snprintf(set->name, sizeof(set->name), name, min + n);
Patrick McHardy20a69342013-10-11 12:06:22 +02002465 list_for_each_entry(i, &ctx->table->sets, list) {
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002466 if (!nft_is_active_next(ctx->net, i))
2467 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02002468 if (!strcmp(set->name, i->name))
2469 return -ENFILE;
2470 }
2471 return 0;
2472}
2473
2474static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2475 const struct nft_set *set, u16 event, u16 flags)
2476{
2477 struct nfgenmsg *nfmsg;
2478 struct nlmsghdr *nlh;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002479 struct nlattr *desc;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002480 u32 portid = ctx->portid;
2481 u32 seq = ctx->seq;
Patrick McHardy20a69342013-10-11 12:06:22 +02002482
2483 event |= NFNL_SUBSYS_NFTABLES << 8;
2484 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2485 flags);
2486 if (nlh == NULL)
2487 goto nla_put_failure;
2488
2489 nfmsg = nlmsg_data(nlh);
2490 nfmsg->nfgen_family = ctx->afi->family;
2491 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02002492 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
Patrick McHardy20a69342013-10-11 12:06:22 +02002493
2494 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2495 goto nla_put_failure;
2496 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2497 goto nla_put_failure;
2498 if (set->flags != 0)
2499 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2500 goto nla_put_failure;
2501
2502 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2503 goto nla_put_failure;
2504 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2505 goto nla_put_failure;
2506 if (set->flags & NFT_SET_MAP) {
2507 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2508 goto nla_put_failure;
2509 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2510 goto nla_put_failure;
2511 }
2512
Patrick McHardy761da292015-03-26 12:39:36 +00002513 if (set->timeout &&
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +02002514 nla_put_be64(skb, NFTA_SET_TIMEOUT, cpu_to_be64(set->timeout),
2515 NFTA_SET_PAD))
Patrick McHardy761da292015-03-26 12:39:36 +00002516 goto nla_put_failure;
2517 if (set->gc_int &&
2518 nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int)))
2519 goto nla_put_failure;
2520
Arturo Borrero9363dc42014-09-23 13:30:41 +02002521 if (set->policy != NFT_SET_POL_PERFORMANCE) {
2522 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
2523 goto nla_put_failure;
2524 }
2525
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002526 if (nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata))
2527 goto nla_put_failure;
2528
Patrick McHardyc50b9602014-03-28 10:19:47 +00002529 desc = nla_nest_start(skb, NFTA_SET_DESC);
2530 if (desc == NULL)
2531 goto nla_put_failure;
2532 if (set->size &&
2533 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2534 goto nla_put_failure;
2535 nla_nest_end(skb, desc);
2536
Johannes Berg053c0952015-01-16 22:09:00 +01002537 nlmsg_end(skb, nlh);
2538 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002539
2540nla_put_failure:
2541 nlmsg_trim(skb, nlh);
2542 return -1;
2543}
2544
2545static int nf_tables_set_notify(const struct nft_ctx *ctx,
2546 const struct nft_set *set,
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002547 int event, gfp_t gfp_flags)
Patrick McHardy20a69342013-10-11 12:06:22 +02002548{
2549 struct sk_buff *skb;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002550 u32 portid = ctx->portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002551 int err;
2552
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002553 if (!ctx->report &&
2554 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002555 return 0;
2556
2557 err = -ENOBUFS;
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002558 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
Patrick McHardy20a69342013-10-11 12:06:22 +02002559 if (skb == NULL)
2560 goto err;
2561
2562 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2563 if (err < 0) {
2564 kfree_skb(skb);
2565 goto err;
2566 }
2567
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002568 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002569 ctx->report, gfp_flags);
Patrick McHardy20a69342013-10-11 12:06:22 +02002570err:
2571 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002572 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002573 return err;
2574}
2575
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002576static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002577{
2578 const struct nft_set *set;
2579 unsigned int idx, s_idx = cb->args[0];
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002580 struct nft_af_info *afi;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002581 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2582 struct net *net = sock_net(skb->sk);
2583 int cur_family = cb->args[3];
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002584 struct nft_ctx *ctx = cb->data, ctx_set;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002585
2586 if (cb->args[1])
2587 return skb->len;
2588
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002589 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02002590 cb->seq = net->nft.base_seq;
2591
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002592 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002593 if (ctx->afi && ctx->afi != afi)
2594 continue;
2595
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002596 if (cur_family) {
2597 if (afi->family != cur_family)
2598 continue;
2599
2600 cur_family = 0;
2601 }
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002602 list_for_each_entry_rcu(table, &afi->tables, list) {
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002603 if (ctx->table && ctx->table != table)
2604 continue;
2605
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002606 if (cur_table) {
2607 if (cur_table != table)
2608 continue;
2609
2610 cur_table = NULL;
2611 }
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002612 idx = 0;
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002613 list_for_each_entry_rcu(set, &table->sets, list) {
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002614 if (idx < s_idx)
2615 goto cont;
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002616 if (!nft_is_active(net, set))
2617 goto cont;
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002618
2619 ctx_set = *ctx;
2620 ctx_set.table = table;
2621 ctx_set.afi = afi;
2622 if (nf_tables_fill_set(skb, &ctx_set, set,
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002623 NFT_MSG_NEWSET,
2624 NLM_F_MULTI) < 0) {
2625 cb->args[0] = idx;
2626 cb->args[2] = (unsigned long) table;
2627 cb->args[3] = afi->family;
2628 goto done;
2629 }
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02002630 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002631cont:
2632 idx++;
2633 }
2634 if (s_idx)
2635 s_idx = 0;
2636 }
2637 }
2638 cb->args[1] = 1;
2639done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002640 rcu_read_unlock();
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002641 return skb->len;
2642}
2643
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002644static int nf_tables_dump_sets_done(struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002645{
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002646 kfree(cb->data);
2647 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002648}
2649
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +01002650static int nf_tables_getset(struct net *net, struct sock *nlsk,
2651 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy20a69342013-10-11 12:06:22 +02002652 const struct nlattr * const nla[])
2653{
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002654 u8 genmask = nft_genmask_cur(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02002655 const struct nft_set *set;
2656 struct nft_ctx ctx;
2657 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002658 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002659 int err;
2660
stephen hemminger01cfa0a2014-10-29 22:57:19 -07002661 /* Verify existence before starting dump */
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002662 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002663 if (err < 0)
2664 return err;
2665
2666 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2667 struct netlink_dump_control c = {
2668 .dump = nf_tables_dump_sets,
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002669 .done = nf_tables_dump_sets_done,
Patrick McHardy20a69342013-10-11 12:06:22 +02002670 };
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002671 struct nft_ctx *ctx_dump;
2672
2673 ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2674 if (ctx_dump == NULL)
2675 return -ENOMEM;
2676
2677 *ctx_dump = ctx;
2678 c.data = ctx_dump;
2679
Patrick McHardy20a69342013-10-11 12:06:22 +02002680 return netlink_dump_start(nlsk, skb, nlh, &c);
2681 }
2682
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002683 /* Only accept unspec with dump */
2684 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2685 return -EAFNOSUPPORT;
Phil Turnbulleaa2bcd2016-05-27 13:34:04 -04002686 if (!nla[NFTA_SET_TABLE])
2687 return -EINVAL;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002688
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002689 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002690 if (IS_ERR(set))
2691 return PTR_ERR(set);
2692
2693 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2694 if (skb2 == NULL)
2695 return -ENOMEM;
2696
2697 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2698 if (err < 0)
2699 goto err;
2700
2701 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2702
2703err:
2704 kfree_skb(skb2);
2705 return err;
2706}
2707
Patrick McHardyc50b9602014-03-28 10:19:47 +00002708static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2709 struct nft_set_desc *desc,
2710 const struct nlattr *nla)
2711{
2712 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2713 int err;
2714
2715 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2716 if (err < 0)
2717 return err;
2718
2719 if (da[NFTA_SET_DESC_SIZE] != NULL)
2720 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2721
2722 return 0;
2723}
2724
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002725static int nf_tables_newset(struct net *net, struct sock *nlsk,
2726 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy20a69342013-10-11 12:06:22 +02002727 const struct nlattr * const nla[])
2728{
2729 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002730 u8 genmask = nft_genmask_next(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02002731 const struct nft_set_ops *ops;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002732 struct nft_af_info *afi;
Patrick McHardy20a69342013-10-11 12:06:22 +02002733 struct nft_table *table;
2734 struct nft_set *set;
2735 struct nft_ctx ctx;
Pablo Neira Ayusocb39ad82016-05-04 17:49:53 +02002736 char name[NFT_SET_MAXNAMELEN];
Patrick McHardy20a69342013-10-11 12:06:22 +02002737 unsigned int size;
2738 bool create;
Patrick McHardy761da292015-03-26 12:39:36 +00002739 u64 timeout;
2740 u32 ktype, dtype, flags, policy, gc_int;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002741 struct nft_set_desc desc;
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002742 unsigned char *udata;
2743 u16 udlen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002744 int err;
2745
2746 if (nla[NFTA_SET_TABLE] == NULL ||
2747 nla[NFTA_SET_NAME] == NULL ||
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002748 nla[NFTA_SET_KEY_LEN] == NULL ||
2749 nla[NFTA_SET_ID] == NULL)
Patrick McHardy20a69342013-10-11 12:06:22 +02002750 return -EINVAL;
2751
Patrick McHardyc50b9602014-03-28 10:19:47 +00002752 memset(&desc, 0, sizeof(desc));
2753
Patrick McHardy20a69342013-10-11 12:06:22 +02002754 ktype = NFT_DATA_VALUE;
2755 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2756 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2757 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2758 return -EINVAL;
2759 }
2760
Patrick McHardyc50b9602014-03-28 10:19:47 +00002761 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
Patrick McHardy7d740262015-04-11 02:27:39 +01002762 if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
Patrick McHardy20a69342013-10-11 12:06:22 +02002763 return -EINVAL;
2764
2765 flags = 0;
2766 if (nla[NFTA_SET_FLAGS] != NULL) {
2767 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2768 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
Patrick McHardy7c6c6e92015-04-11 10:46:41 +01002769 NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
2770 NFT_SET_MAP | NFT_SET_EVAL))
Patrick McHardy20a69342013-10-11 12:06:22 +02002771 return -EINVAL;
Patrick McHardy7c6c6e92015-04-11 10:46:41 +01002772 /* Only one of both operations is supported */
2773 if ((flags & (NFT_SET_MAP | NFT_SET_EVAL)) ==
2774 (NFT_SET_MAP | NFT_SET_EVAL))
2775 return -EOPNOTSUPP;
Patrick McHardy20a69342013-10-11 12:06:22 +02002776 }
2777
2778 dtype = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002779 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2780 if (!(flags & NFT_SET_MAP))
2781 return -EINVAL;
2782
2783 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2784 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2785 dtype != NFT_DATA_VERDICT)
2786 return -EINVAL;
2787
2788 if (dtype != NFT_DATA_VERDICT) {
2789 if (nla[NFTA_SET_DATA_LEN] == NULL)
2790 return -EINVAL;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002791 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
Patrick McHardy7d740262015-04-11 02:27:39 +01002792 if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
Patrick McHardy20a69342013-10-11 12:06:22 +02002793 return -EINVAL;
2794 } else
Patrick McHardy7d740262015-04-11 02:27:39 +01002795 desc.dlen = sizeof(struct nft_verdict);
Patrick McHardy20a69342013-10-11 12:06:22 +02002796 } else if (flags & NFT_SET_MAP)
2797 return -EINVAL;
2798
Patrick McHardy761da292015-03-26 12:39:36 +00002799 timeout = 0;
2800 if (nla[NFTA_SET_TIMEOUT] != NULL) {
2801 if (!(flags & NFT_SET_TIMEOUT))
2802 return -EINVAL;
2803 timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_TIMEOUT]));
2804 }
2805 gc_int = 0;
2806 if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
2807 if (!(flags & NFT_SET_TIMEOUT))
2808 return -EINVAL;
2809 gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
2810 }
2811
Patrick McHardyc50b9602014-03-28 10:19:47 +00002812 policy = NFT_SET_POL_PERFORMANCE;
2813 if (nla[NFTA_SET_POLICY] != NULL)
2814 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2815
2816 if (nla[NFTA_SET_DESC] != NULL) {
2817 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2818 if (err < 0)
2819 return err;
2820 }
2821
Patrick McHardy20a69342013-10-11 12:06:22 +02002822 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2823
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002824 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002825 if (IS_ERR(afi))
2826 return PTR_ERR(afi);
2827
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002828 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE], genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002829 if (IS_ERR(table))
2830 return PTR_ERR(table);
2831
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002832 nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002833
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002834 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME], genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002835 if (IS_ERR(set)) {
2836 if (PTR_ERR(set) != -ENOENT)
2837 return PTR_ERR(set);
2838 set = NULL;
2839 }
2840
2841 if (set != NULL) {
2842 if (nlh->nlmsg_flags & NLM_F_EXCL)
2843 return -EEXIST;
2844 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2845 return -EOPNOTSUPP;
2846 return 0;
2847 }
2848
2849 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2850 return -ENOENT;
2851
Patrick McHardyc50b9602014-03-28 10:19:47 +00002852 ops = nft_select_set_ops(nla, &desc, policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002853 if (IS_ERR(ops))
2854 return PTR_ERR(ops);
2855
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002856 udlen = 0;
2857 if (nla[NFTA_SET_USERDATA])
2858 udlen = nla_len(nla[NFTA_SET_USERDATA]);
2859
Patrick McHardy20a69342013-10-11 12:06:22 +02002860 size = 0;
2861 if (ops->privsize != NULL)
2862 size = ops->privsize(nla);
2863
2864 err = -ENOMEM;
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002865 set = kzalloc(sizeof(*set) + size + udlen, GFP_KERNEL);
Patrick McHardy20a69342013-10-11 12:06:22 +02002866 if (set == NULL)
2867 goto err1;
2868
2869 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2870 err = nf_tables_set_alloc_name(&ctx, set, name);
2871 if (err < 0)
2872 goto err2;
2873
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002874 udata = NULL;
2875 if (udlen) {
2876 udata = set->data + size;
2877 nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen);
2878 }
2879
Patrick McHardy20a69342013-10-11 12:06:22 +02002880 INIT_LIST_HEAD(&set->bindings);
2881 set->ops = ops;
2882 set->ktype = ktype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002883 set->klen = desc.klen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002884 set->dtype = dtype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002885 set->dlen = desc.dlen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002886 set->flags = flags;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002887 set->size = desc.size;
Arturo Borrero9363dc42014-09-23 13:30:41 +02002888 set->policy = policy;
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002889 set->udlen = udlen;
2890 set->udata = udata;
Patrick McHardy761da292015-03-26 12:39:36 +00002891 set->timeout = timeout;
2892 set->gc_int = gc_int;
Patrick McHardy20a69342013-10-11 12:06:22 +02002893
Patrick McHardyc50b9602014-03-28 10:19:47 +00002894 err = ops->init(set, &desc, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002895 if (err < 0)
2896 goto err2;
2897
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002898 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002899 if (err < 0)
2900 goto err2;
2901
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002902 list_add_tail_rcu(&set->list, &table->sets);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02002903 table->use++;
Patrick McHardy20a69342013-10-11 12:06:22 +02002904 return 0;
2905
2906err2:
2907 kfree(set);
2908err1:
2909 module_put(ops->owner);
2910 return err;
2911}
2912
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002913static void nft_set_destroy(struct nft_set *set)
2914{
2915 set->ops->destroy(set);
2916 module_put(set->ops->owner);
2917 kfree(set);
2918}
2919
Patrick McHardy20a69342013-10-11 12:06:22 +02002920static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2921{
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002922 list_del_rcu(&set->list);
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002923 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002924 nft_set_destroy(set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002925}
2926
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002927static int nf_tables_delset(struct net *net, struct sock *nlsk,
2928 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy20a69342013-10-11 12:06:22 +02002929 const struct nlattr * const nla[])
2930{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002931 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002932 u8 genmask = nft_genmask_next(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02002933 struct nft_set *set;
2934 struct nft_ctx ctx;
2935 int err;
2936
Patrick McHardyec2c9932014-02-05 15:03:35 +00002937 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2938 return -EAFNOSUPPORT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002939 if (nla[NFTA_SET_TABLE] == NULL)
2940 return -EINVAL;
2941
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002942 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002943 if (err < 0)
2944 return err;
2945
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002946 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002947 if (IS_ERR(set))
2948 return PTR_ERR(set);
2949 if (!list_empty(&set->bindings))
2950 return -EBUSY;
2951
Arturo Borreroee01d542014-09-02 16:42:25 +02002952 return nft_delset(&ctx, set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002953}
2954
2955static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2956 const struct nft_set *set,
2957 const struct nft_set_iter *iter,
2958 const struct nft_set_elem *elem)
2959{
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002960 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02002961 enum nft_registers dreg;
2962
2963 dreg = nft_type_to_reg(set->dtype);
Patrick McHardy1ec10212015-04-11 02:27:27 +01002964 return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
2965 set->dtype == NFT_DATA_VERDICT ?
2966 NFT_DATA_VERDICT : NFT_DATA_VALUE,
2967 set->dlen);
Patrick McHardy20a69342013-10-11 12:06:22 +02002968}
2969
2970int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2971 struct nft_set_binding *binding)
2972{
2973 struct nft_set_binding *i;
2974 struct nft_set_iter iter;
2975
2976 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2977 return -EBUSY;
2978
Patrick McHardy11113e12015-04-05 14:41:07 +02002979 if (binding->flags & NFT_SET_MAP) {
Patrick McHardy20a69342013-10-11 12:06:22 +02002980 /* If the set is already bound to the same chain all
2981 * jumps are already validated for that chain.
2982 */
2983 list_for_each_entry(i, &set->bindings, list) {
Liping Zhanga4684402016-06-11 12:20:26 +08002984 if (i->flags & NFT_SET_MAP &&
Patrick McHardy11113e12015-04-05 14:41:07 +02002985 i->chain == binding->chain)
Patrick McHardy20a69342013-10-11 12:06:22 +02002986 goto bind;
2987 }
2988
Pablo Neira Ayuso8588ac02016-06-11 12:20:27 +08002989 iter.genmask = nft_genmask_next(ctx->net);
Patrick McHardy20a69342013-10-11 12:06:22 +02002990 iter.skip = 0;
2991 iter.count = 0;
2992 iter.err = 0;
2993 iter.fn = nf_tables_bind_check_setelem;
2994
2995 set->ops->walk(ctx, set, &iter);
Liping Zhanga02f4242016-06-11 12:20:28 +08002996 if (iter.err < 0)
Patrick McHardy20a69342013-10-11 12:06:22 +02002997 return iter.err;
Patrick McHardy20a69342013-10-11 12:06:22 +02002998 }
2999bind:
3000 binding->chain = ctx->chain;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003001 list_add_tail_rcu(&binding->list, &set->bindings);
Patrick McHardy20a69342013-10-11 12:06:22 +02003002 return 0;
3003}
3004
3005void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
3006 struct nft_set_binding *binding)
3007{
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02003008 list_del_rcu(&binding->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02003009
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003010 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02003011 nft_is_active(ctx->net, set))
Patrick McHardy20a69342013-10-11 12:06:22 +02003012 nf_tables_set_destroy(ctx, set);
3013}
3014
Patrick McHardy3ac4c072015-03-25 13:07:49 +00003015const struct nft_set_ext_type nft_set_ext_types[] = {
3016 [NFT_SET_EXT_KEY] = {
Patrick McHardy7d740262015-04-11 02:27:39 +01003017 .align = __alignof__(u32),
Patrick McHardy3ac4c072015-03-25 13:07:49 +00003018 },
3019 [NFT_SET_EXT_DATA] = {
Patrick McHardy7d740262015-04-11 02:27:39 +01003020 .align = __alignof__(u32),
Patrick McHardy3ac4c072015-03-25 13:07:49 +00003021 },
Patrick McHardyf25ad2e2015-04-11 10:46:39 +01003022 [NFT_SET_EXT_EXPR] = {
3023 .align = __alignof__(struct nft_expr),
3024 },
Patrick McHardy3ac4c072015-03-25 13:07:49 +00003025 [NFT_SET_EXT_FLAGS] = {
3026 .len = sizeof(u8),
3027 .align = __alignof__(u8),
3028 },
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003029 [NFT_SET_EXT_TIMEOUT] = {
3030 .len = sizeof(u64),
3031 .align = __alignof__(u64),
3032 },
3033 [NFT_SET_EXT_EXPIRATION] = {
3034 .len = sizeof(unsigned long),
3035 .align = __alignof__(unsigned long),
3036 },
Patrick McHardy68e942e2015-04-05 14:43:38 +02003037 [NFT_SET_EXT_USERDATA] = {
3038 .len = sizeof(struct nft_userdata),
3039 .align = __alignof__(struct nft_userdata),
3040 },
Patrick McHardy3ac4c072015-03-25 13:07:49 +00003041};
3042EXPORT_SYMBOL_GPL(nft_set_ext_types);
3043
Patrick McHardy20a69342013-10-11 12:06:22 +02003044/*
3045 * Set elements
3046 */
3047
3048static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
3049 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
3050 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
3051 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003052 [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
Patrick McHardy68e942e2015-04-05 14:43:38 +02003053 [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
3054 .len = NFT_USERDATA_MAXLEN },
Patrick McHardy20a69342013-10-11 12:06:22 +02003055};
3056
3057static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
3058 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
3059 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
3060 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003061 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02003062};
3063
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01003064static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net,
Patrick McHardy20a69342013-10-11 12:06:22 +02003065 const struct sk_buff *skb,
3066 const struct nlmsghdr *nlh,
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003067 const struct nlattr * const nla[],
3068 u8 genmask)
Patrick McHardy20a69342013-10-11 12:06:22 +02003069{
3070 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02003071 struct nft_af_info *afi;
3072 struct nft_table *table;
Patrick McHardy20a69342013-10-11 12:06:22 +02003073
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003074 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02003075 if (IS_ERR(afi))
3076 return PTR_ERR(afi);
3077
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003078 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE],
3079 genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003080 if (IS_ERR(table))
3081 return PTR_ERR(table);
3082
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01003083 nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02003084 return 0;
3085}
3086
3087static int nf_tables_fill_setelem(struct sk_buff *skb,
3088 const struct nft_set *set,
3089 const struct nft_set_elem *elem)
3090{
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003091 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02003092 unsigned char *b = skb_tail_pointer(skb);
3093 struct nlattr *nest;
3094
3095 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
3096 if (nest == NULL)
3097 goto nla_put_failure;
3098
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003099 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
3100 NFT_DATA_VALUE, set->klen) < 0)
Patrick McHardy20a69342013-10-11 12:06:22 +02003101 goto nla_put_failure;
3102
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003103 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
3104 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
Patrick McHardy20a69342013-10-11 12:06:22 +02003105 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
3106 set->dlen) < 0)
3107 goto nla_put_failure;
3108
Patrick McHardyf25ad2e2015-04-11 10:46:39 +01003109 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) &&
3110 nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0)
3111 goto nla_put_failure;
3112
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003113 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3114 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
3115 htonl(*nft_set_ext_flags(ext))))
3116 goto nla_put_failure;
Patrick McHardy20a69342013-10-11 12:06:22 +02003117
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003118 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
3119 nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +02003120 cpu_to_be64(*nft_set_ext_timeout(ext)),
3121 NFTA_SET_ELEM_PAD))
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003122 goto nla_put_failure;
3123
3124 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
3125 unsigned long expires, now = jiffies;
3126
3127 expires = *nft_set_ext_expiration(ext);
3128 if (time_before(now, expires))
3129 expires -= now;
3130 else
3131 expires = 0;
3132
3133 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +02003134 cpu_to_be64(jiffies_to_msecs(expires)),
3135 NFTA_SET_ELEM_PAD))
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003136 goto nla_put_failure;
3137 }
3138
Patrick McHardy68e942e2015-04-05 14:43:38 +02003139 if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
3140 struct nft_userdata *udata;
3141
3142 udata = nft_set_ext_userdata(ext);
3143 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
3144 udata->len + 1, udata->data))
3145 goto nla_put_failure;
3146 }
3147
Patrick McHardy20a69342013-10-11 12:06:22 +02003148 nla_nest_end(skb, nest);
3149 return 0;
3150
3151nla_put_failure:
3152 nlmsg_trim(skb, b);
3153 return -EMSGSIZE;
3154}
3155
3156struct nft_set_dump_args {
3157 const struct netlink_callback *cb;
3158 struct nft_set_iter iter;
3159 struct sk_buff *skb;
3160};
3161
3162static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
3163 const struct nft_set *set,
3164 const struct nft_set_iter *iter,
3165 const struct nft_set_elem *elem)
3166{
3167 struct nft_set_dump_args *args;
3168
3169 args = container_of(iter, struct nft_set_dump_args, iter);
3170 return nf_tables_fill_setelem(args->skb, set, elem);
3171}
3172
3173static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
3174{
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01003175 struct net *net = sock_net(skb->sk);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003176 u8 genmask = nft_genmask_cur(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02003177 const struct nft_set *set;
3178 struct nft_set_dump_args args;
3179 struct nft_ctx ctx;
3180 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
3181 struct nfgenmsg *nfmsg;
3182 struct nlmsghdr *nlh;
3183 struct nlattr *nest;
3184 u32 portid, seq;
3185 int event, err;
3186
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01003187 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
3188 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02003189 if (err < 0)
3190 return err;
3191
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01003192 err = nft_ctx_init_from_elemattr(&ctx, net, cb->skb, cb->nlh,
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003193 (void *)nla, genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003194 if (err < 0)
3195 return err;
3196
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02003197 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
3198 genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003199 if (IS_ERR(set))
3200 return PTR_ERR(set);
3201
3202 event = NFT_MSG_NEWSETELEM;
3203 event |= NFNL_SUBSYS_NFTABLES << 8;
3204 portid = NETLINK_CB(cb->skb).portid;
3205 seq = cb->nlh->nlmsg_seq;
3206
3207 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3208 NLM_F_MULTI);
3209 if (nlh == NULL)
3210 goto nla_put_failure;
3211
3212 nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso6403d962014-06-11 19:05:28 +02003213 nfmsg->nfgen_family = ctx.afi->family;
Patrick McHardy20a69342013-10-11 12:06:22 +02003214 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003215 nfmsg->res_id = htons(ctx.net->nft.base_seq & 0xffff);
Patrick McHardy20a69342013-10-11 12:06:22 +02003216
3217 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
3218 goto nla_put_failure;
3219 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
3220 goto nla_put_failure;
3221
3222 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3223 if (nest == NULL)
3224 goto nla_put_failure;
3225
Pablo Neira Ayuso8588ac02016-06-11 12:20:27 +08003226 args.cb = cb;
3227 args.skb = skb;
3228 args.iter.genmask = nft_genmask_cur(ctx.net);
3229 args.iter.skip = cb->args[0];
3230 args.iter.count = 0;
3231 args.iter.err = 0;
3232 args.iter.fn = nf_tables_dump_setelem;
Patrick McHardy20a69342013-10-11 12:06:22 +02003233 set->ops->walk(&ctx, set, &args.iter);
3234
3235 nla_nest_end(skb, nest);
3236 nlmsg_end(skb, nlh);
3237
3238 if (args.iter.err && args.iter.err != -EMSGSIZE)
3239 return args.iter.err;
3240 if (args.iter.count == cb->args[0])
3241 return 0;
3242
3243 cb->args[0] = args.iter.count;
3244 return skb->len;
3245
3246nla_put_failure:
3247 return -ENOSPC;
3248}
3249
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +01003250static int nf_tables_getsetelem(struct net *net, struct sock *nlsk,
3251 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy20a69342013-10-11 12:06:22 +02003252 const struct nlattr * const nla[])
3253{
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003254 u8 genmask = nft_genmask_cur(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02003255 const struct nft_set *set;
3256 struct nft_ctx ctx;
3257 int err;
3258
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003259 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003260 if (err < 0)
3261 return err;
3262
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02003263 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
3264 genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003265 if (IS_ERR(set))
3266 return PTR_ERR(set);
3267
3268 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3269 struct netlink_dump_control c = {
3270 .dump = nf_tables_dump_set,
3271 };
3272 return netlink_dump_start(nlsk, skb, nlh, &c);
3273 }
3274 return -EOPNOTSUPP;
3275}
3276
Arturo Borrerod60ce622014-04-01 14:06:07 +02003277static int nf_tables_fill_setelem_info(struct sk_buff *skb,
3278 const struct nft_ctx *ctx, u32 seq,
3279 u32 portid, int event, u16 flags,
3280 const struct nft_set *set,
3281 const struct nft_set_elem *elem)
3282{
3283 struct nfgenmsg *nfmsg;
3284 struct nlmsghdr *nlh;
3285 struct nlattr *nest;
3286 int err;
3287
3288 event |= NFNL_SUBSYS_NFTABLES << 8;
3289 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3290 flags);
3291 if (nlh == NULL)
3292 goto nla_put_failure;
3293
3294 nfmsg = nlmsg_data(nlh);
3295 nfmsg->nfgen_family = ctx->afi->family;
3296 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003297 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
Arturo Borrerod60ce622014-04-01 14:06:07 +02003298
3299 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3300 goto nla_put_failure;
3301 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3302 goto nla_put_failure;
3303
3304 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3305 if (nest == NULL)
3306 goto nla_put_failure;
3307
3308 err = nf_tables_fill_setelem(skb, set, elem);
3309 if (err < 0)
3310 goto nla_put_failure;
3311
3312 nla_nest_end(skb, nest);
3313
Johannes Berg053c0952015-01-16 22:09:00 +01003314 nlmsg_end(skb, nlh);
3315 return 0;
Arturo Borrerod60ce622014-04-01 14:06:07 +02003316
3317nla_put_failure:
3318 nlmsg_trim(skb, nlh);
3319 return -1;
3320}
3321
3322static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
3323 const struct nft_set *set,
3324 const struct nft_set_elem *elem,
3325 int event, u16 flags)
3326{
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003327 struct net *net = ctx->net;
3328 u32 portid = ctx->portid;
Arturo Borrerod60ce622014-04-01 14:06:07 +02003329 struct sk_buff *skb;
3330 int err;
3331
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003332 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
Arturo Borrerod60ce622014-04-01 14:06:07 +02003333 return 0;
3334
3335 err = -ENOBUFS;
3336 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3337 if (skb == NULL)
3338 goto err;
3339
3340 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3341 set, elem);
3342 if (err < 0) {
3343 kfree_skb(skb);
3344 goto err;
3345 }
3346
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003347 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
Arturo Borrerod60ce622014-04-01 14:06:07 +02003348 GFP_KERNEL);
3349err:
3350 if (err < 0)
3351 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3352 return err;
3353}
3354
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003355static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3356 int msg_type,
3357 struct nft_set *set)
3358{
3359 struct nft_trans *trans;
3360
3361 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3362 if (trans == NULL)
3363 return NULL;
3364
3365 nft_trans_elem_set(trans) = set;
3366 return trans;
3367}
3368
Patrick McHardy22fe54d2015-04-05 14:41:08 +02003369void *nft_set_elem_init(const struct nft_set *set,
3370 const struct nft_set_ext_tmpl *tmpl,
Patrick McHardy49499c32015-04-11 02:27:37 +01003371 const u32 *key, const u32 *data,
Patrick McHardy22fe54d2015-04-05 14:41:08 +02003372 u64 timeout, gfp_t gfp)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003373{
3374 struct nft_set_ext *ext;
3375 void *elem;
3376
3377 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
3378 if (elem == NULL)
3379 return NULL;
3380
3381 ext = nft_set_elem_ext(set, elem);
3382 nft_set_ext_init(ext, tmpl);
3383
3384 memcpy(nft_set_ext_key(ext), key, set->klen);
3385 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3386 memcpy(nft_set_ext_data(ext), data, set->dlen);
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003387 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION))
3388 *nft_set_ext_expiration(ext) =
3389 jiffies + msecs_to_jiffies(timeout);
3390 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
3391 *nft_set_ext_timeout(ext) = timeout;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003392
3393 return elem;
3394}
3395
Patrick McHardy61edafb2015-03-25 14:08:47 +00003396void nft_set_elem_destroy(const struct nft_set *set, void *elem)
3397{
3398 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
3399
3400 nft_data_uninit(nft_set_ext_key(ext), NFT_DATA_VALUE);
3401 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3402 nft_data_uninit(nft_set_ext_data(ext), set->dtype);
Patrick McHardyf25ad2e2015-04-11 10:46:39 +01003403 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
3404 nf_tables_expr_destroy(NULL, nft_set_ext_expr(ext));
Patrick McHardy61edafb2015-03-25 14:08:47 +00003405
3406 kfree(elem);
3407}
3408EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
3409
Pablo Neira Ayuso0e9091d2016-04-12 23:50:34 +02003410static int nft_setelem_parse_flags(const struct nft_set *set,
3411 const struct nlattr *attr, u32 *flags)
3412{
3413 if (attr == NULL)
3414 return 0;
3415
3416 *flags = ntohl(nla_get_be32(attr));
3417 if (*flags & ~NFT_SET_ELEM_INTERVAL_END)
3418 return -EINVAL;
3419 if (!(set->flags & NFT_SET_INTERVAL) &&
3420 *flags & NFT_SET_ELEM_INTERVAL_END)
3421 return -EINVAL;
3422
3423 return 0;
3424}
3425
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003426static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +02003427 const struct nlattr *attr)
3428{
3429 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3430 struct nft_data_desc d1, d2;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003431 struct nft_set_ext_tmpl tmpl;
3432 struct nft_set_ext *ext;
Patrick McHardy20a69342013-10-11 12:06:22 +02003433 struct nft_set_elem elem;
3434 struct nft_set_binding *binding;
Patrick McHardy68e942e2015-04-05 14:43:38 +02003435 struct nft_userdata *udata;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003436 struct nft_data data;
Patrick McHardy20a69342013-10-11 12:06:22 +02003437 enum nft_registers dreg;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003438 struct nft_trans *trans;
Pablo Neira Ayuso0e9091d2016-04-12 23:50:34 +02003439 u32 flags = 0;
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003440 u64 timeout;
Patrick McHardy68e942e2015-04-05 14:43:38 +02003441 u8 ulen;
Patrick McHardy20a69342013-10-11 12:06:22 +02003442 int err;
3443
3444 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3445 nft_set_elem_policy);
3446 if (err < 0)
3447 return err;
3448
3449 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3450 return -EINVAL;
3451
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003452 nft_set_ext_prepare(&tmpl);
3453
Pablo Neira Ayuso0e9091d2016-04-12 23:50:34 +02003454 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
3455 if (err < 0)
3456 return err;
3457 if (flags != 0)
3458 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
Patrick McHardy20a69342013-10-11 12:06:22 +02003459
3460 if (set->flags & NFT_SET_MAP) {
3461 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003462 !(flags & NFT_SET_ELEM_INTERVAL_END))
Patrick McHardy20a69342013-10-11 12:06:22 +02003463 return -EINVAL;
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01003464 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003465 flags & NFT_SET_ELEM_INTERVAL_END)
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01003466 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003467 } else {
3468 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3469 return -EINVAL;
3470 }
3471
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003472 timeout = 0;
3473 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
3474 if (!(set->flags & NFT_SET_TIMEOUT))
3475 return -EINVAL;
3476 timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_ELEM_TIMEOUT]));
3477 } else if (set->flags & NFT_SET_TIMEOUT) {
3478 timeout = set->timeout;
3479 }
3480
Patrick McHardy7d740262015-04-11 02:27:39 +01003481 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1,
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01003482 nla[NFTA_SET_ELEM_KEY]);
Patrick McHardy20a69342013-10-11 12:06:22 +02003483 if (err < 0)
3484 goto err1;
3485 err = -EINVAL;
3486 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3487 goto err2;
3488
Patrick McHardy7d740262015-04-11 02:27:39 +01003489 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, d1.len);
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003490 if (timeout > 0) {
3491 nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
3492 if (timeout != set->timeout)
3493 nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
3494 }
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003495
Patrick McHardy20a69342013-10-11 12:06:22 +02003496 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01003497 err = nft_data_init(ctx, &data, sizeof(data), &d2,
3498 nla[NFTA_SET_ELEM_DATA]);
Patrick McHardy20a69342013-10-11 12:06:22 +02003499 if (err < 0)
3500 goto err2;
3501
3502 err = -EINVAL;
3503 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3504 goto err3;
3505
3506 dreg = nft_type_to_reg(set->dtype);
3507 list_for_each_entry(binding, &set->bindings, list) {
3508 struct nft_ctx bind_ctx = {
3509 .afi = ctx->afi,
3510 .table = ctx->table,
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02003511 .chain = (struct nft_chain *)binding->chain,
Patrick McHardy20a69342013-10-11 12:06:22 +02003512 };
3513
Patrick McHardy11113e12015-04-05 14:41:07 +02003514 if (!(binding->flags & NFT_SET_MAP))
3515 continue;
3516
Patrick McHardy1ec10212015-04-11 02:27:27 +01003517 err = nft_validate_register_store(&bind_ctx, dreg,
3518 &data,
3519 d2.type, d2.len);
Patrick McHardy20a69342013-10-11 12:06:22 +02003520 if (err < 0)
3521 goto err3;
3522 }
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003523
Patrick McHardy7d740262015-04-11 02:27:39 +01003524 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, d2.len);
Patrick McHardy20a69342013-10-11 12:06:22 +02003525 }
3526
Patrick McHardy68e942e2015-04-05 14:43:38 +02003527 /* The full maximum length of userdata can exceed the maximum
3528 * offset value (U8_MAX) for following extensions, therefor it
3529 * must be the last extension added.
3530 */
3531 ulen = 0;
3532 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
3533 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
3534 if (ulen > 0)
3535 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
3536 ulen);
3537 }
3538
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003539 err = -ENOMEM;
Patrick McHardy7d740262015-04-11 02:27:39 +01003540 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, data.data,
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003541 timeout, GFP_KERNEL);
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003542 if (elem.priv == NULL)
3543 goto err3;
3544
3545 ext = nft_set_elem_ext(set, elem.priv);
3546 if (flags)
3547 *nft_set_ext_flags(ext) = flags;
Patrick McHardy68e942e2015-04-05 14:43:38 +02003548 if (ulen > 0) {
3549 udata = nft_set_ext_userdata(ext);
3550 udata->len = ulen - 1;
3551 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
3552 }
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003553
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003554 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3555 if (trans == NULL)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003556 goto err4;
Patrick McHardy20a69342013-10-11 12:06:22 +02003557
Patrick McHardy69086652015-03-26 12:39:39 +00003558 ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +02003559 err = set->ops->insert(ctx->net, set, &elem);
Patrick McHardy20a69342013-10-11 12:06:22 +02003560 if (err < 0)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003561 goto err5;
Patrick McHardy20a69342013-10-11 12:06:22 +02003562
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003563 nft_trans_elem(trans) = elem;
Pablo Neira Ayuso46bbafc2014-05-22 12:36:03 +02003564 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Patrick McHardy20a69342013-10-11 12:06:22 +02003565 return 0;
3566
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003567err5:
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003568 kfree(trans);
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003569err4:
3570 kfree(elem.priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02003571err3:
3572 if (nla[NFTA_SET_ELEM_DATA] != NULL)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003573 nft_data_uninit(&data, d2.type);
Patrick McHardy20a69342013-10-11 12:06:22 +02003574err2:
Patrick McHardy7d740262015-04-11 02:27:39 +01003575 nft_data_uninit(&elem.key.val, d1.type);
Patrick McHardy20a69342013-10-11 12:06:22 +02003576err1:
3577 return err;
3578}
3579
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01003580static int nf_tables_newsetelem(struct net *net, struct sock *nlsk,
3581 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy20a69342013-10-11 12:06:22 +02003582 const struct nlattr * const nla[])
3583{
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003584 u8 genmask = nft_genmask_next(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02003585 const struct nlattr *attr;
3586 struct nft_set *set;
3587 struct nft_ctx ctx;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003588 int rem, err = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003589
Pablo Neira Ayuso7d5570c2014-07-25 13:15:36 +02003590 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3591 return -EINVAL;
3592
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003593 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003594 if (err < 0)
3595 return err;
3596
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02003597 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
3598 genmask);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003599 if (IS_ERR(set)) {
3600 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3601 set = nf_tables_set_lookup_byid(net,
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02003602 nla[NFTA_SET_ELEM_LIST_SET_ID],
3603 genmask);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003604 }
3605 if (IS_ERR(set))
3606 return PTR_ERR(set);
3607 }
3608
Patrick McHardy20a69342013-10-11 12:06:22 +02003609 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3610 return -EBUSY;
3611
3612 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
Patrick McHardy3dd06732015-04-05 14:41:06 +02003613 if (set->size &&
3614 !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact))
3615 return -ENFILE;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003616
Patrick McHardy3dd06732015-04-05 14:41:06 +02003617 err = nft_add_set_elem(&ctx, set, attr);
3618 if (err < 0) {
3619 atomic_dec(&set->nelems);
3620 break;
3621 }
Patrick McHardy20a69342013-10-11 12:06:22 +02003622 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003623 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +02003624}
3625
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003626static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +02003627 const struct nlattr *attr)
3628{
3629 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003630 struct nft_set_ext_tmpl tmpl;
Patrick McHardy20a69342013-10-11 12:06:22 +02003631 struct nft_data_desc desc;
3632 struct nft_set_elem elem;
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003633 struct nft_set_ext *ext;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003634 struct nft_trans *trans;
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003635 u32 flags = 0;
3636 void *priv;
Patrick McHardy20a69342013-10-11 12:06:22 +02003637 int err;
3638
3639 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3640 nft_set_elem_policy);
3641 if (err < 0)
3642 goto err1;
3643
3644 err = -EINVAL;
3645 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3646 goto err1;
3647
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003648 nft_set_ext_prepare(&tmpl);
3649
3650 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
3651 if (err < 0)
3652 return err;
3653 if (flags != 0)
3654 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
3655
Patrick McHardy7d740262015-04-11 02:27:39 +01003656 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc,
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01003657 nla[NFTA_SET_ELEM_KEY]);
Patrick McHardy20a69342013-10-11 12:06:22 +02003658 if (err < 0)
3659 goto err1;
3660
3661 err = -EINVAL;
3662 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3663 goto err2;
3664
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003665 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, desc.len);
3666
3667 err = -ENOMEM;
3668 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, NULL, 0,
3669 GFP_KERNEL);
3670 if (elem.priv == NULL)
3671 goto err2;
3672
3673 ext = nft_set_elem_ext(set, elem.priv);
3674 if (flags)
3675 *nft_set_ext_flags(ext) = flags;
3676
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003677 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
Julia Lawall609ccf02014-08-07 14:49:08 +02003678 if (trans == NULL) {
3679 err = -ENOMEM;
Patrick McHardycc02e452015-03-25 14:08:50 +00003680 goto err3;
3681 }
3682
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +02003683 priv = set->ops->deactivate(ctx->net, set, &elem);
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003684 if (priv == NULL) {
3685 err = -ENOENT;
3686 goto err4;
3687 }
3688 kfree(elem.priv);
3689 elem.priv = priv;
3690
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003691 nft_trans_elem(trans) = elem;
Pablo Neira Ayuso46bbafc2014-05-22 12:36:03 +02003692 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Thomas Graf0dc13622014-08-01 17:25:38 +02003693 return 0;
Patrick McHardycc02e452015-03-25 14:08:50 +00003694
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003695err4:
Patrick McHardycc02e452015-03-25 14:08:50 +00003696 kfree(trans);
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003697err3:
3698 kfree(elem.priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02003699err2:
Patrick McHardy7d740262015-04-11 02:27:39 +01003700 nft_data_uninit(&elem.key.val, desc.type);
Patrick McHardy20a69342013-10-11 12:06:22 +02003701err1:
3702 return err;
3703}
3704
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01003705static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
3706 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy20a69342013-10-11 12:06:22 +02003707 const struct nlattr * const nla[])
3708{
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003709 u8 genmask = nft_genmask_next(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02003710 const struct nlattr *attr;
3711 struct nft_set *set;
3712 struct nft_ctx ctx;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003713 int rem, err = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003714
Pablo Neira Ayuso7d5570c2014-07-25 13:15:36 +02003715 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3716 return -EINVAL;
3717
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003718 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003719 if (err < 0)
3720 return err;
3721
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02003722 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
3723 genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003724 if (IS_ERR(set))
3725 return PTR_ERR(set);
3726 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3727 return -EBUSY;
3728
3729 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3730 err = nft_del_setelem(&ctx, set, attr);
3731 if (err < 0)
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003732 break;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003733
Patrick McHardy3dd06732015-04-05 14:41:06 +02003734 set->ndeact++;
Patrick McHardy20a69342013-10-11 12:06:22 +02003735 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003736 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +02003737}
3738
Patrick McHardycfed7e12015-03-26 12:39:38 +00003739void nft_set_gc_batch_release(struct rcu_head *rcu)
3740{
3741 struct nft_set_gc_batch *gcb;
3742 unsigned int i;
3743
3744 gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
3745 for (i = 0; i < gcb->head.cnt; i++)
3746 nft_set_elem_destroy(gcb->head.set, gcb->elems[i]);
3747 kfree(gcb);
3748}
3749EXPORT_SYMBOL_GPL(nft_set_gc_batch_release);
3750
3751struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
3752 gfp_t gfp)
3753{
3754 struct nft_set_gc_batch *gcb;
3755
3756 gcb = kzalloc(sizeof(*gcb), gfp);
3757 if (gcb == NULL)
3758 return gcb;
3759 gcb->head.set = set;
3760 return gcb;
3761}
3762EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc);
3763
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003764static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
3765 u32 portid, u32 seq)
3766{
3767 struct nlmsghdr *nlh;
3768 struct nfgenmsg *nfmsg;
3769 int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN;
3770
3771 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
3772 if (nlh == NULL)
3773 goto nla_put_failure;
3774
3775 nfmsg = nlmsg_data(nlh);
3776 nfmsg->nfgen_family = AF_UNSPEC;
3777 nfmsg->version = NFNETLINK_V0;
3778 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
3779
3780 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)))
3781 goto nla_put_failure;
3782
Johannes Berg053c0952015-01-16 22:09:00 +01003783 nlmsg_end(skb, nlh);
3784 return 0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003785
3786nla_put_failure:
3787 nlmsg_trim(skb, nlh);
3788 return -EMSGSIZE;
3789}
3790
3791static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
3792{
3793 struct nlmsghdr *nlh = nlmsg_hdr(skb);
3794 struct sk_buff *skb2;
3795 int err;
3796
3797 if (nlmsg_report(nlh) &&
3798 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3799 return 0;
3800
3801 err = -ENOBUFS;
3802 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3803 if (skb2 == NULL)
3804 goto err;
3805
3806 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3807 nlh->nlmsg_seq);
3808 if (err < 0) {
3809 kfree_skb(skb2);
3810 goto err;
3811 }
3812
3813 err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
3814 NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
3815err:
3816 if (err < 0) {
3817 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
3818 err);
3819 }
3820 return err;
3821}
3822
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +01003823static int nf_tables_getgen(struct net *net, struct sock *nlsk,
3824 struct sk_buff *skb, const struct nlmsghdr *nlh,
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003825 const struct nlattr * const nla[])
3826{
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003827 struct sk_buff *skb2;
3828 int err;
3829
3830 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3831 if (skb2 == NULL)
3832 return -ENOMEM;
3833
3834 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3835 nlh->nlmsg_seq);
3836 if (err < 0)
3837 goto err;
3838
3839 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3840err:
3841 kfree_skb(skb2);
3842 return err;
3843}
3844
Patrick McHardy96518512013-10-14 11:00:02 +02003845static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3846 [NFT_MSG_NEWTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003847 .call_batch = nf_tables_newtable,
Patrick McHardy96518512013-10-14 11:00:02 +02003848 .attr_count = NFTA_TABLE_MAX,
3849 .policy = nft_table_policy,
3850 },
3851 [NFT_MSG_GETTABLE] = {
3852 .call = nf_tables_gettable,
3853 .attr_count = NFTA_TABLE_MAX,
3854 .policy = nft_table_policy,
3855 },
3856 [NFT_MSG_DELTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003857 .call_batch = nf_tables_deltable,
Patrick McHardy96518512013-10-14 11:00:02 +02003858 .attr_count = NFTA_TABLE_MAX,
3859 .policy = nft_table_policy,
3860 },
3861 [NFT_MSG_NEWCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003862 .call_batch = nf_tables_newchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003863 .attr_count = NFTA_CHAIN_MAX,
3864 .policy = nft_chain_policy,
3865 },
3866 [NFT_MSG_GETCHAIN] = {
3867 .call = nf_tables_getchain,
3868 .attr_count = NFTA_CHAIN_MAX,
3869 .policy = nft_chain_policy,
3870 },
3871 [NFT_MSG_DELCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003872 .call_batch = nf_tables_delchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003873 .attr_count = NFTA_CHAIN_MAX,
3874 .policy = nft_chain_policy,
3875 },
3876 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003877 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003878 .attr_count = NFTA_RULE_MAX,
3879 .policy = nft_rule_policy,
3880 },
3881 [NFT_MSG_GETRULE] = {
3882 .call = nf_tables_getrule,
3883 .attr_count = NFTA_RULE_MAX,
3884 .policy = nft_rule_policy,
3885 },
3886 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003887 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003888 .attr_count = NFTA_RULE_MAX,
3889 .policy = nft_rule_policy,
3890 },
Patrick McHardy20a69342013-10-11 12:06:22 +02003891 [NFT_MSG_NEWSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003892 .call_batch = nf_tables_newset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003893 .attr_count = NFTA_SET_MAX,
3894 .policy = nft_set_policy,
3895 },
3896 [NFT_MSG_GETSET] = {
3897 .call = nf_tables_getset,
3898 .attr_count = NFTA_SET_MAX,
3899 .policy = nft_set_policy,
3900 },
3901 [NFT_MSG_DELSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003902 .call_batch = nf_tables_delset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003903 .attr_count = NFTA_SET_MAX,
3904 .policy = nft_set_policy,
3905 },
3906 [NFT_MSG_NEWSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003907 .call_batch = nf_tables_newsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003908 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3909 .policy = nft_set_elem_list_policy,
3910 },
3911 [NFT_MSG_GETSETELEM] = {
3912 .call = nf_tables_getsetelem,
3913 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3914 .policy = nft_set_elem_list_policy,
3915 },
3916 [NFT_MSG_DELSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003917 .call_batch = nf_tables_delsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003918 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3919 .policy = nft_set_elem_list_policy,
3920 },
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003921 [NFT_MSG_GETGEN] = {
3922 .call = nf_tables_getgen,
3923 },
Patrick McHardy96518512013-10-14 11:00:02 +02003924};
3925
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003926static void nft_chain_commit_update(struct nft_trans *trans)
3927{
3928 struct nft_base_chain *basechain;
3929
3930 if (nft_trans_chain_name(trans)[0])
3931 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3932
3933 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3934 return;
3935
3936 basechain = nft_base_chain(trans->ctx.chain);
3937 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3938
3939 switch (nft_trans_chain_policy(trans)) {
3940 case NF_DROP:
3941 case NF_ACCEPT:
3942 basechain->policy = nft_trans_chain_policy(trans);
3943 break;
3944 }
3945}
3946
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003947static void nf_tables_commit_release(struct nft_trans *trans)
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003948{
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003949 switch (trans->msg_type) {
3950 case NFT_MSG_DELTABLE:
3951 nf_tables_table_destroy(&trans->ctx);
3952 break;
3953 case NFT_MSG_DELCHAIN:
3954 nf_tables_chain_destroy(trans->ctx.chain);
3955 break;
3956 case NFT_MSG_DELRULE:
3957 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3958 break;
3959 case NFT_MSG_DELSET:
3960 nft_set_destroy(nft_trans_set(trans));
3961 break;
Patrick McHardy61edafb2015-03-25 14:08:47 +00003962 case NFT_MSG_DELSETELEM:
3963 nft_set_elem_destroy(nft_trans_elem_set(trans),
3964 nft_trans_elem(trans).priv);
3965 break;
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003966 }
3967 kfree(trans);
3968}
3969
Pablo Neira Ayuso5913bea2015-12-15 19:41:57 +01003970static int nf_tables_commit(struct net *net, struct sk_buff *skb)
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003971{
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003972 struct nft_trans *trans, *next;
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003973 struct nft_trans_elem *te;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003974
3975 /* Bump generation counter, invalidate any dump in progress */
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02003976 while (++net->nft.base_seq == 0);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003977
3978 /* A new generation has just started */
Patrick McHardyea4bd992015-03-25 14:08:49 +00003979 net->nft.gencursor = nft_gencursor_next(net);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003980
3981 /* Make sure all packets have left the previous generation before
3982 * purging old rules.
3983 */
3984 synchronize_rcu();
3985
3986 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003987 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003988 case NFT_MSG_NEWTABLE:
3989 if (nft_trans_table_update(trans)) {
3990 if (!nft_trans_table_enable(trans)) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02003991 nf_tables_table_disable(net,
3992 trans->ctx.afi,
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003993 trans->ctx.table);
3994 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3995 }
3996 } else {
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003997 nft_clear(net, trans->ctx.table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003998 }
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003999 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02004000 nft_trans_destroy(trans);
4001 break;
4002 case NFT_MSG_DELTABLE:
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02004003 list_del_rcu(&trans->ctx.table->list);
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02004004 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02004005 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02004006 case NFT_MSG_NEWCHAIN:
4007 if (nft_trans_chain_update(trans))
4008 nft_chain_commit_update(trans);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004009 else
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02004010 nft_clear(net, trans->ctx.chain);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004011
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02004012 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02004013 nft_trans_destroy(trans);
4014 break;
4015 case NFT_MSG_DELCHAIN:
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02004016 list_del_rcu(&trans->ctx.chain->list);
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02004017 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +02004018 nf_tables_unregister_hooks(trans->ctx.net,
4019 trans->ctx.table,
Arturo Borreroc5598792014-09-02 16:42:23 +02004020 trans->ctx.chain,
4021 trans->ctx.afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02004022 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02004023 case NFT_MSG_NEWRULE:
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02004024 nft_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02004025 nf_tables_rule_notify(&trans->ctx,
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004026 nft_trans_rule(trans),
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02004027 NFT_MSG_NEWRULE);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004028 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02004029 break;
4030 case NFT_MSG_DELRULE:
4031 list_del_rcu(&nft_trans_rule(trans)->list);
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02004032 nf_tables_rule_notify(&trans->ctx,
4033 nft_trans_rule(trans),
4034 NFT_MSG_DELRULE);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02004035 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004036 case NFT_MSG_NEWSET:
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02004037 nft_clear(net, nft_trans_set(trans));
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004038 /* This avoids hitting -EBUSY when deleting the table
4039 * from the transaction.
4040 */
4041 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
4042 !list_empty(&nft_trans_set(trans)->bindings))
4043 trans->ctx.table->use--;
4044
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004045 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02004046 NFT_MSG_NEWSET, GFP_KERNEL);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004047 nft_trans_destroy(trans);
4048 break;
4049 case NFT_MSG_DELSET:
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02004050 list_del_rcu(&nft_trans_set(trans)->list);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004051 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02004052 NFT_MSG_DELSET, GFP_KERNEL);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004053 break;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004054 case NFT_MSG_NEWSETELEM:
Patrick McHardycc02e452015-03-25 14:08:50 +00004055 te = (struct nft_trans_elem *)trans->data;
4056
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +02004057 te->set->ops->activate(net, te->set, &te->elem);
Patrick McHardycc02e452015-03-25 14:08:50 +00004058 nf_tables_setelem_notify(&trans->ctx, te->set,
4059 &te->elem,
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004060 NFT_MSG_NEWSETELEM, 0);
4061 nft_trans_destroy(trans);
4062 break;
4063 case NFT_MSG_DELSETELEM:
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02004064 te = (struct nft_trans_elem *)trans->data;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00004065
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02004066 nf_tables_setelem_notify(&trans->ctx, te->set,
4067 &te->elem,
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004068 NFT_MSG_DELSETELEM, 0);
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01004069 te->set->ops->remove(te->set, &te->elem);
Patrick McHardy3dd06732015-04-05 14:41:06 +02004070 atomic_dec(&te->set->nelems);
4071 te->set->ndeact--;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004072 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004073 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004074 }
4075
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01004076 synchronize_rcu();
4077
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004078 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02004079 list_del(&trans->list);
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01004080 nf_tables_commit_release(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004081 }
4082
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02004083 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
4084
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004085 return 0;
4086}
4087
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01004088static void nf_tables_abort_release(struct nft_trans *trans)
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02004089{
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02004090 switch (trans->msg_type) {
4091 case NFT_MSG_NEWTABLE:
4092 nf_tables_table_destroy(&trans->ctx);
4093 break;
4094 case NFT_MSG_NEWCHAIN:
4095 nf_tables_chain_destroy(trans->ctx.chain);
4096 break;
4097 case NFT_MSG_NEWRULE:
4098 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
4099 break;
4100 case NFT_MSG_NEWSET:
4101 nft_set_destroy(nft_trans_set(trans));
4102 break;
Patrick McHardy61edafb2015-03-25 14:08:47 +00004103 case NFT_MSG_NEWSETELEM:
4104 nft_set_elem_destroy(nft_trans_elem_set(trans),
4105 nft_trans_elem(trans).priv);
4106 break;
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02004107 }
4108 kfree(trans);
4109}
4110
Pablo Neira Ayuso5913bea2015-12-15 19:41:57 +01004111static int nf_tables_abort(struct net *net, struct sk_buff *skb)
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004112{
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004113 struct nft_trans *trans, *next;
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01004114 struct nft_trans_elem *te;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004115
Xin Longa907e362015-12-07 18:48:07 +08004116 list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list,
4117 list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02004118 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02004119 case NFT_MSG_NEWTABLE:
4120 if (nft_trans_table_update(trans)) {
4121 if (nft_trans_table_enable(trans)) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02004122 nf_tables_table_disable(net,
4123 trans->ctx.afi,
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02004124 trans->ctx.table);
4125 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
4126 }
4127 nft_trans_destroy(trans);
4128 } else {
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02004129 list_del_rcu(&trans->ctx.table->list);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02004130 }
4131 break;
4132 case NFT_MSG_DELTABLE:
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02004133 nft_clear(trans->ctx.net, trans->ctx.table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02004134 nft_trans_destroy(trans);
4135 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02004136 case NFT_MSG_NEWCHAIN:
4137 if (nft_trans_chain_update(trans)) {
Markus Elfring982f4052014-11-18 20:37:05 +01004138 free_percpu(nft_trans_chain_stats(trans));
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02004139
4140 nft_trans_destroy(trans);
4141 } else {
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004142 trans->ctx.table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02004143 list_del_rcu(&trans->ctx.chain->list);
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +02004144 nf_tables_unregister_hooks(trans->ctx.net,
4145 trans->ctx.table,
Arturo Borreroc5598792014-09-02 16:42:23 +02004146 trans->ctx.chain,
4147 trans->ctx.afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02004148 }
4149 break;
4150 case NFT_MSG_DELCHAIN:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004151 trans->ctx.table->use++;
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02004152 nft_clear(trans->ctx.net, trans->ctx.chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02004153 nft_trans_destroy(trans);
4154 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02004155 case NFT_MSG_NEWRULE:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004156 trans->ctx.chain->use--;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02004157 list_del_rcu(&nft_trans_rule(trans)->list);
4158 break;
4159 case NFT_MSG_DELRULE:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004160 trans->ctx.chain->use++;
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02004161 nft_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004162 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02004163 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004164 case NFT_MSG_NEWSET:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004165 trans->ctx.table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02004166 list_del_rcu(&nft_trans_set(trans)->list);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004167 break;
4168 case NFT_MSG_DELSET:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004169 trans->ctx.table->use++;
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02004170 nft_clear(trans->ctx.net, nft_trans_set(trans));
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004171 nft_trans_destroy(trans);
4172 break;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004173 case NFT_MSG_NEWSETELEM:
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01004174 te = (struct nft_trans_elem *)trans->data;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00004175
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01004176 te->set->ops->remove(te->set, &te->elem);
Patrick McHardy3dd06732015-04-05 14:41:06 +02004177 atomic_dec(&te->set->nelems);
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004178 break;
4179 case NFT_MSG_DELSETELEM:
Patrick McHardycc02e452015-03-25 14:08:50 +00004180 te = (struct nft_trans_elem *)trans->data;
4181
Pablo Neira Ayuso42a55762016-07-08 14:41:49 +02004182 te->set->ops->activate(net, te->set, &te->elem);
Patrick McHardy3dd06732015-04-05 14:41:06 +02004183 te->set->ndeact--;
Patrick McHardycc02e452015-03-25 14:08:50 +00004184
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004185 nft_trans_destroy(trans);
4186 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004187 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004188 }
4189
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01004190 synchronize_rcu();
4191
Pablo Neira Ayusoa1cee072014-05-23 11:09:42 +02004192 list_for_each_entry_safe_reverse(trans, next,
4193 &net->nft.commit_list, list) {
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02004194 list_del(&trans->list);
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01004195 nf_tables_abort_release(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004196 }
4197
4198 return 0;
4199}
4200
Patrick McHardy96518512013-10-14 11:00:02 +02004201static const struct nfnetlink_subsystem nf_tables_subsys = {
4202 .name = "nf_tables",
4203 .subsys_id = NFNL_SUBSYS_NFTABLES,
4204 .cb_count = NFT_MSG_MAX,
4205 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02004206 .commit = nf_tables_commit,
4207 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02004208};
4209
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +02004210int nft_chain_validate_dependency(const struct nft_chain *chain,
4211 enum nft_chain_type type)
4212{
4213 const struct nft_base_chain *basechain;
4214
4215 if (chain->flags & NFT_BASE_CHAIN) {
4216 basechain = nft_base_chain(chain);
4217 if (basechain->type->type != type)
4218 return -EOPNOTSUPP;
4219 }
4220 return 0;
4221}
4222EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
4223
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +01004224int nft_chain_validate_hooks(const struct nft_chain *chain,
4225 unsigned int hook_flags)
4226{
4227 struct nft_base_chain *basechain;
4228
4229 if (chain->flags & NFT_BASE_CHAIN) {
4230 basechain = nft_base_chain(chain);
4231
4232 if ((1 << basechain->ops[0].hooknum) & hook_flags)
4233 return 0;
4234
4235 return -EOPNOTSUPP;
4236 }
4237
4238 return 0;
4239}
4240EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
4241
Patrick McHardy20a69342013-10-11 12:06:22 +02004242/*
4243 * Loop detection - walk through the ruleset beginning at the destination chain
4244 * of a new jump until either the source chain is reached (loop) or all
4245 * reachable chains have been traversed.
4246 *
4247 * The loop check is performed whenever a new jump verdict is added to an
4248 * expression or verdict map or a verdict map is bound to a new chain.
4249 */
4250
4251static int nf_tables_check_loops(const struct nft_ctx *ctx,
4252 const struct nft_chain *chain);
4253
4254static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
4255 const struct nft_set *set,
4256 const struct nft_set_iter *iter,
4257 const struct nft_set_elem *elem)
4258{
Patrick McHardyfe2811e2015-03-25 13:07:50 +00004259 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
4260 const struct nft_data *data;
4261
4262 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
4263 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
Pablo Neira Ayuso62f9c8b2014-02-07 14:45:01 +01004264 return 0;
4265
Patrick McHardyfe2811e2015-03-25 13:07:50 +00004266 data = nft_set_ext_data(ext);
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004267 switch (data->verdict.code) {
Patrick McHardy20a69342013-10-11 12:06:22 +02004268 case NFT_JUMP:
4269 case NFT_GOTO:
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004270 return nf_tables_check_loops(ctx, data->verdict.chain);
Patrick McHardy20a69342013-10-11 12:06:22 +02004271 default:
4272 return 0;
4273 }
4274}
4275
4276static int nf_tables_check_loops(const struct nft_ctx *ctx,
4277 const struct nft_chain *chain)
4278{
4279 const struct nft_rule *rule;
4280 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02004281 const struct nft_set *set;
4282 struct nft_set_binding *binding;
4283 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02004284
4285 if (ctx->chain == chain)
4286 return -ELOOP;
4287
4288 list_for_each_entry(rule, &chain->rules, list) {
4289 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02004290 const struct nft_data *data = NULL;
4291 int err;
4292
4293 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02004294 continue;
4295
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02004296 err = expr->ops->validate(ctx, expr, &data);
4297 if (err < 0)
4298 return err;
4299
Patrick McHardy20a69342013-10-11 12:06:22 +02004300 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02004301 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02004302
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004303 switch (data->verdict.code) {
Patrick McHardy20a69342013-10-11 12:06:22 +02004304 case NFT_JUMP:
4305 case NFT_GOTO:
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004306 err = nf_tables_check_loops(ctx,
4307 data->verdict.chain);
Patrick McHardy20a69342013-10-11 12:06:22 +02004308 if (err < 0)
4309 return err;
4310 default:
4311 break;
4312 }
4313 }
4314 }
4315
4316 list_for_each_entry(set, &ctx->table->sets, list) {
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02004317 if (!nft_is_active_next(ctx->net, set))
4318 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02004319 if (!(set->flags & NFT_SET_MAP) ||
4320 set->dtype != NFT_DATA_VERDICT)
4321 continue;
4322
4323 list_for_each_entry(binding, &set->bindings, list) {
Patrick McHardy11113e12015-04-05 14:41:07 +02004324 if (!(binding->flags & NFT_SET_MAP) ||
4325 binding->chain != chain)
Patrick McHardy20a69342013-10-11 12:06:22 +02004326 continue;
4327
Pablo Neira Ayuso8588ac02016-06-11 12:20:27 +08004328 iter.genmask = nft_genmask_next(ctx->net);
Patrick McHardy20a69342013-10-11 12:06:22 +02004329 iter.skip = 0;
4330 iter.count = 0;
4331 iter.err = 0;
4332 iter.fn = nf_tables_loop_check_setelem;
4333
4334 set->ops->walk(ctx, set, &iter);
4335 if (iter.err < 0)
4336 return iter.err;
4337 }
4338 }
4339
4340 return 0;
4341}
4342
Patrick McHardy49499c32015-04-11 02:27:37 +01004343/**
4344 * nft_parse_register - parse a register value from a netlink attribute
4345 *
4346 * @attr: netlink attribute
4347 *
4348 * Parse and translate a register value from a netlink attribute.
4349 * Registers used to be 128 bit wide, these register numbers will be
4350 * mapped to the corresponding 32 bit register numbers.
4351 */
Patrick McHardyb1c96ed2015-04-11 02:27:36 +01004352unsigned int nft_parse_register(const struct nlattr *attr)
4353{
Patrick McHardy49499c32015-04-11 02:27:37 +01004354 unsigned int reg;
4355
4356 reg = ntohl(nla_get_be32(attr));
4357 switch (reg) {
4358 case NFT_REG_VERDICT...NFT_REG_4:
4359 return reg * NFT_REG_SIZE / NFT_REG32_SIZE;
4360 default:
4361 return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
4362 }
Patrick McHardyb1c96ed2015-04-11 02:27:36 +01004363}
4364EXPORT_SYMBOL_GPL(nft_parse_register);
4365
Patrick McHardy49499c32015-04-11 02:27:37 +01004366/**
4367 * nft_dump_register - dump a register value to a netlink attribute
4368 *
4369 * @skb: socket buffer
4370 * @attr: attribute number
4371 * @reg: register number
4372 *
4373 * Construct a netlink attribute containing the register number. For
4374 * compatibility reasons, register numbers being a multiple of 4 are
4375 * translated to the corresponding 128 bit register numbers.
4376 */
Patrick McHardyb1c96ed2015-04-11 02:27:36 +01004377int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
4378{
Patrick McHardy49499c32015-04-11 02:27:37 +01004379 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
4380 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
4381 else
4382 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
4383
Patrick McHardyb1c96ed2015-04-11 02:27:36 +01004384 return nla_put_be32(skb, attr, htonl(reg));
4385}
4386EXPORT_SYMBOL_GPL(nft_dump_register);
4387
Patrick McHardy96518512013-10-14 11:00:02 +02004388/**
Patrick McHardyd07db982015-04-11 02:27:30 +01004389 * nft_validate_register_load - validate a load from a register
Patrick McHardy96518512013-10-14 11:00:02 +02004390 *
4391 * @reg: the register number
Patrick McHardyd07db982015-04-11 02:27:30 +01004392 * @len: the length of the data
Patrick McHardy96518512013-10-14 11:00:02 +02004393 *
4394 * Validate that the input register is one of the general purpose
Patrick McHardyd07db982015-04-11 02:27:30 +01004395 * registers and that the length of the load is within the bounds.
Patrick McHardy96518512013-10-14 11:00:02 +02004396 */
Patrick McHardyd07db982015-04-11 02:27:30 +01004397int nft_validate_register_load(enum nft_registers reg, unsigned int len)
Patrick McHardy96518512013-10-14 11:00:02 +02004398{
Patrick McHardy49499c32015-04-11 02:27:37 +01004399 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
Patrick McHardy96518512013-10-14 11:00:02 +02004400 return -EINVAL;
Patrick McHardyd07db982015-04-11 02:27:30 +01004401 if (len == 0)
4402 return -EINVAL;
Patrick McHardy49499c32015-04-11 02:27:37 +01004403 if (reg * NFT_REG32_SIZE + len > FIELD_SIZEOF(struct nft_regs, data))
Patrick McHardyd07db982015-04-11 02:27:30 +01004404 return -ERANGE;
Patrick McHardy49499c32015-04-11 02:27:37 +01004405
Patrick McHardy96518512013-10-14 11:00:02 +02004406 return 0;
4407}
Patrick McHardyd07db982015-04-11 02:27:30 +01004408EXPORT_SYMBOL_GPL(nft_validate_register_load);
Patrick McHardy96518512013-10-14 11:00:02 +02004409
4410/**
Patrick McHardy1ec10212015-04-11 02:27:27 +01004411 * nft_validate_register_store - validate an expressions' register store
Patrick McHardy96518512013-10-14 11:00:02 +02004412 *
4413 * @ctx: context of the expression performing the load
4414 * @reg: the destination register number
4415 * @data: the data to load
4416 * @type: the data type
Patrick McHardy45d9bcd2015-04-11 02:27:26 +01004417 * @len: the length of the data
Patrick McHardy96518512013-10-14 11:00:02 +02004418 *
4419 * Validate that a data load uses the appropriate data type for
Patrick McHardy45d9bcd2015-04-11 02:27:26 +01004420 * the destination register and the length is within the bounds.
4421 * A value of NULL for the data means that its runtime gathered
Patrick McHardy58f40ab2015-04-11 02:27:28 +01004422 * data.
Patrick McHardy96518512013-10-14 11:00:02 +02004423 */
Patrick McHardy1ec10212015-04-11 02:27:27 +01004424int nft_validate_register_store(const struct nft_ctx *ctx,
4425 enum nft_registers reg,
4426 const struct nft_data *data,
4427 enum nft_data_types type, unsigned int len)
Patrick McHardy96518512013-10-14 11:00:02 +02004428{
Patrick McHardy20a69342013-10-11 12:06:22 +02004429 int err;
4430
Patrick McHardy96518512013-10-14 11:00:02 +02004431 switch (reg) {
4432 case NFT_REG_VERDICT:
Patrick McHardy58f40ab2015-04-11 02:27:28 +01004433 if (type != NFT_DATA_VERDICT)
Patrick McHardy96518512013-10-14 11:00:02 +02004434 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02004435
Patrick McHardy58f40ab2015-04-11 02:27:28 +01004436 if (data != NULL &&
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004437 (data->verdict.code == NFT_GOTO ||
4438 data->verdict.code == NFT_JUMP)) {
4439 err = nf_tables_check_loops(ctx, data->verdict.chain);
Patrick McHardy20a69342013-10-11 12:06:22 +02004440 if (err < 0)
4441 return err;
4442
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004443 if (ctx->chain->level + 1 >
4444 data->verdict.chain->level) {
Patrick McHardy20a69342013-10-11 12:06:22 +02004445 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
4446 return -EMLINK;
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004447 data->verdict.chain->level = ctx->chain->level + 1;
Patrick McHardy20a69342013-10-11 12:06:22 +02004448 }
4449 }
4450
Patrick McHardy96518512013-10-14 11:00:02 +02004451 return 0;
4452 default:
Patrick McHardy49499c32015-04-11 02:27:37 +01004453 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
Patrick McHardy27e6d202015-04-11 02:27:29 +01004454 return -EINVAL;
Patrick McHardy45d9bcd2015-04-11 02:27:26 +01004455 if (len == 0)
4456 return -EINVAL;
Patrick McHardy49499c32015-04-11 02:27:37 +01004457 if (reg * NFT_REG32_SIZE + len >
4458 FIELD_SIZEOF(struct nft_regs, data))
Patrick McHardy45d9bcd2015-04-11 02:27:26 +01004459 return -ERANGE;
Patrick McHardy27e6d202015-04-11 02:27:29 +01004460
Patrick McHardy96518512013-10-14 11:00:02 +02004461 if (data != NULL && type != NFT_DATA_VALUE)
4462 return -EINVAL;
4463 return 0;
4464 }
4465}
Patrick McHardy1ec10212015-04-11 02:27:27 +01004466EXPORT_SYMBOL_GPL(nft_validate_register_store);
Patrick McHardy96518512013-10-14 11:00:02 +02004467
4468static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
4469 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
4470 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
4471 .len = NFT_CHAIN_MAXNAMELEN - 1 },
4472};
4473
4474static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
4475 struct nft_data_desc *desc, const struct nlattr *nla)
4476{
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02004477 u8 genmask = nft_genmask_next(ctx->net);
Patrick McHardy96518512013-10-14 11:00:02 +02004478 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
4479 struct nft_chain *chain;
4480 int err;
4481
4482 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
4483 if (err < 0)
4484 return err;
4485
4486 if (!tb[NFTA_VERDICT_CODE])
4487 return -EINVAL;
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004488 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
Patrick McHardy96518512013-10-14 11:00:02 +02004489
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004490 switch (data->verdict.code) {
Patrick McHardye0abdad2014-02-18 18:06:50 +00004491 default:
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004492 switch (data->verdict.code & NF_VERDICT_MASK) {
Patrick McHardye0abdad2014-02-18 18:06:50 +00004493 case NF_ACCEPT:
4494 case NF_DROP:
4495 case NF_QUEUE:
4496 break;
4497 default:
4498 return -EINVAL;
4499 }
4500 /* fall through */
Patrick McHardy96518512013-10-14 11:00:02 +02004501 case NFT_CONTINUE:
4502 case NFT_BREAK:
4503 case NFT_RETURN:
Patrick McHardy96518512013-10-14 11:00:02 +02004504 break;
4505 case NFT_JUMP:
4506 case NFT_GOTO:
4507 if (!tb[NFTA_VERDICT_CHAIN])
4508 return -EINVAL;
4509 chain = nf_tables_chain_lookup(ctx->table,
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02004510 tb[NFTA_VERDICT_CHAIN], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02004511 if (IS_ERR(chain))
4512 return PTR_ERR(chain);
4513 if (chain->flags & NFT_BASE_CHAIN)
4514 return -EOPNOTSUPP;
4515
Patrick McHardy96518512013-10-14 11:00:02 +02004516 chain->use++;
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004517 data->verdict.chain = chain;
Patrick McHardy96518512013-10-14 11:00:02 +02004518 break;
Patrick McHardy96518512013-10-14 11:00:02 +02004519 }
4520
Florian Westphal4c4ed072015-04-14 16:44:14 +02004521 desc->len = sizeof(data->verdict);
Patrick McHardy96518512013-10-14 11:00:02 +02004522 desc->type = NFT_DATA_VERDICT;
4523 return 0;
4524}
4525
4526static void nft_verdict_uninit(const struct nft_data *data)
4527{
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004528 switch (data->verdict.code) {
Patrick McHardy96518512013-10-14 11:00:02 +02004529 case NFT_JUMP:
4530 case NFT_GOTO:
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004531 data->verdict.chain->use--;
Patrick McHardy96518512013-10-14 11:00:02 +02004532 break;
4533 }
4534}
4535
Florian Westphal33d5a7b2015-11-28 21:53:04 +01004536int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
Patrick McHardy96518512013-10-14 11:00:02 +02004537{
4538 struct nlattr *nest;
4539
Florian Westphal33d5a7b2015-11-28 21:53:04 +01004540 nest = nla_nest_start(skb, type);
Patrick McHardy96518512013-10-14 11:00:02 +02004541 if (!nest)
4542 goto nla_put_failure;
4543
Florian Westphal33d5a7b2015-11-28 21:53:04 +01004544 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
Patrick McHardy96518512013-10-14 11:00:02 +02004545 goto nla_put_failure;
4546
Florian Westphal33d5a7b2015-11-28 21:53:04 +01004547 switch (v->code) {
Patrick McHardy96518512013-10-14 11:00:02 +02004548 case NFT_JUMP:
4549 case NFT_GOTO:
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004550 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
Florian Westphal33d5a7b2015-11-28 21:53:04 +01004551 v->chain->name))
Patrick McHardy96518512013-10-14 11:00:02 +02004552 goto nla_put_failure;
4553 }
4554 nla_nest_end(skb, nest);
4555 return 0;
4556
4557nla_put_failure:
4558 return -1;
4559}
4560
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004561static int nft_value_init(const struct nft_ctx *ctx,
4562 struct nft_data *data, unsigned int size,
Patrick McHardy96518512013-10-14 11:00:02 +02004563 struct nft_data_desc *desc, const struct nlattr *nla)
4564{
4565 unsigned int len;
4566
4567 len = nla_len(nla);
4568 if (len == 0)
4569 return -EINVAL;
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004570 if (len > size)
Patrick McHardy96518512013-10-14 11:00:02 +02004571 return -EOVERFLOW;
4572
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004573 nla_memcpy(data->data, nla, len);
Patrick McHardy96518512013-10-14 11:00:02 +02004574 desc->type = NFT_DATA_VALUE;
4575 desc->len = len;
4576 return 0;
4577}
4578
4579static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
4580 unsigned int len)
4581{
4582 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
4583}
4584
4585static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004586 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
Patrick McHardy96518512013-10-14 11:00:02 +02004587 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
4588};
4589
4590/**
4591 * nft_data_init - parse nf_tables data netlink attributes
4592 *
4593 * @ctx: context of the expression using the data
4594 * @data: destination struct nft_data
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004595 * @size: maximum data length
Patrick McHardy96518512013-10-14 11:00:02 +02004596 * @desc: data description
4597 * @nla: netlink attribute containing data
4598 *
4599 * Parse the netlink data attributes and initialize a struct nft_data.
4600 * The type and length of data are returned in the data description.
4601 *
4602 * The caller can indicate that it only wants to accept data of type
4603 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4604 */
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004605int nft_data_init(const struct nft_ctx *ctx,
4606 struct nft_data *data, unsigned int size,
Patrick McHardy96518512013-10-14 11:00:02 +02004607 struct nft_data_desc *desc, const struct nlattr *nla)
4608{
4609 struct nlattr *tb[NFTA_DATA_MAX + 1];
4610 int err;
4611
4612 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
4613 if (err < 0)
4614 return err;
4615
4616 if (tb[NFTA_DATA_VALUE])
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004617 return nft_value_init(ctx, data, size, desc,
4618 tb[NFTA_DATA_VALUE]);
Patrick McHardy96518512013-10-14 11:00:02 +02004619 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
4620 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
4621 return -EINVAL;
4622}
4623EXPORT_SYMBOL_GPL(nft_data_init);
4624
4625/**
4626 * nft_data_uninit - release a nft_data item
4627 *
4628 * @data: struct nft_data to release
4629 * @type: type of data
4630 *
4631 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4632 * all others need to be released by calling this function.
4633 */
4634void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
4635{
Mirek Kratochvil960bd2c2015-05-15 21:15:29 +02004636 if (type < NFT_DATA_VERDICT)
Patrick McHardy96518512013-10-14 11:00:02 +02004637 return;
Mirek Kratochvil960bd2c2015-05-15 21:15:29 +02004638 switch (type) {
Patrick McHardy96518512013-10-14 11:00:02 +02004639 case NFT_DATA_VERDICT:
4640 return nft_verdict_uninit(data);
4641 default:
4642 WARN_ON(1);
4643 }
4644}
4645EXPORT_SYMBOL_GPL(nft_data_uninit);
4646
4647int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
4648 enum nft_data_types type, unsigned int len)
4649{
4650 struct nlattr *nest;
4651 int err;
4652
4653 nest = nla_nest_start(skb, attr);
4654 if (nest == NULL)
4655 return -1;
4656
4657 switch (type) {
4658 case NFT_DATA_VALUE:
4659 err = nft_value_dump(skb, data, len);
4660 break;
4661 case NFT_DATA_VERDICT:
Florian Westphal33d5a7b2015-11-28 21:53:04 +01004662 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
Patrick McHardy96518512013-10-14 11:00:02 +02004663 break;
4664 default:
4665 err = -EINVAL;
4666 WARN_ON(1);
4667 }
4668
4669 nla_nest_end(skb, nest);
4670 return err;
4671}
4672EXPORT_SYMBOL_GPL(nft_data_dump);
4673
Pablo Neira Ayusodf05ef82015-12-15 19:39:32 +01004674static int __net_init nf_tables_init_net(struct net *net)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004675{
4676 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02004677 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02004678 net->nft.base_seq = 1;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004679 return 0;
4680}
4681
Pablo Neira Ayuso5ebe0b02015-12-15 19:40:49 +01004682int __nft_release_basechain(struct nft_ctx *ctx)
4683{
4684 struct nft_rule *rule, *nr;
4685
4686 BUG_ON(!(ctx->chain->flags & NFT_BASE_CHAIN));
4687
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +02004688 nf_tables_unregister_hooks(ctx->net, ctx->chain->table, ctx->chain,
Pablo Neira Ayuso5ebe0b02015-12-15 19:40:49 +01004689 ctx->afi->nops);
4690 list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
4691 list_del(&rule->list);
4692 ctx->chain->use--;
4693 nf_tables_rule_destroy(ctx, rule);
4694 }
4695 list_del(&ctx->chain->list);
4696 ctx->table->use--;
4697 nf_tables_chain_destroy(ctx->chain);
4698
4699 return 0;
4700}
4701EXPORT_SYMBOL_GPL(__nft_release_basechain);
4702
Pablo Neira Ayusodf05ef82015-12-15 19:39:32 +01004703/* Called by nft_unregister_afinfo() from __net_exit path, nfnl_lock is held. */
4704static void __nft_release_afinfo(struct net *net, struct nft_af_info *afi)
4705{
4706 struct nft_table *table, *nt;
4707 struct nft_chain *chain, *nc;
4708 struct nft_rule *rule, *nr;
4709 struct nft_set *set, *ns;
4710 struct nft_ctx ctx = {
4711 .net = net,
4712 .afi = afi,
4713 };
4714
4715 list_for_each_entry_safe(table, nt, &afi->tables, list) {
4716 list_for_each_entry(chain, &table->chains, list)
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +02004717 nf_tables_unregister_hooks(net, table, chain,
4718 afi->nops);
Pablo Neira Ayusodf05ef82015-12-15 19:39:32 +01004719 /* No packets are walking on these chains anymore. */
4720 ctx.table = table;
4721 list_for_each_entry(chain, &table->chains, list) {
4722 ctx.chain = chain;
4723 list_for_each_entry_safe(rule, nr, &chain->rules, list) {
4724 list_del(&rule->list);
4725 chain->use--;
4726 nf_tables_rule_destroy(&ctx, rule);
4727 }
4728 }
4729 list_for_each_entry_safe(set, ns, &table->sets, list) {
4730 list_del(&set->list);
4731 table->use--;
4732 nft_set_destroy(set);
4733 }
4734 list_for_each_entry_safe(chain, nc, &table->chains, list) {
4735 list_del(&chain->list);
4736 table->use--;
4737 nf_tables_chain_destroy(chain);
4738 }
4739 list_del(&table->list);
4740 nf_tables_table_destroy(&ctx);
4741 }
4742}
4743
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004744static struct pernet_operations nf_tables_net_ops = {
4745 .init = nf_tables_init_net,
4746};
4747
Patrick McHardy96518512013-10-14 11:00:02 +02004748static int __init nf_tables_module_init(void)
4749{
4750 int err;
4751
4752 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
4753 GFP_KERNEL);
4754 if (info == NULL) {
4755 err = -ENOMEM;
4756 goto err1;
4757 }
4758
4759 err = nf_tables_core_module_init();
4760 if (err < 0)
4761 goto err2;
4762
4763 err = nfnetlink_subsys_register(&nf_tables_subsys);
4764 if (err < 0)
4765 goto err3;
4766
4767 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004768 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02004769err3:
4770 nf_tables_core_module_exit();
4771err2:
4772 kfree(info);
4773err1:
4774 return err;
4775}
4776
4777static void __exit nf_tables_module_exit(void)
4778{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004779 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02004780 nfnetlink_subsys_unregister(&nf_tables_subsys);
Pablo Neira Ayuso1b1bc492014-10-01 13:53:20 +02004781 rcu_barrier();
Patrick McHardy96518512013-10-14 11:00:02 +02004782 nf_tables_core_module_exit();
4783 kfree(info);
4784}
4785
4786module_init(nf_tables_module_init);
4787module_exit(nf_tables_module_exit);
4788
4789MODULE_LICENSE("GPL");
4790MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4791MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);