blob: 92c9faeb2bf891e7c9e7868308d425f6bf317330 [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
Patrick McHardy5ebb3352015-03-21 15:19:15 +00001408 write_pnet(&basechain->pnet, net);
Pablo Neira Ayuso93707612013-10-10 23:21:26 +02001409 basechain->type = type;
Patrick McHardy96518512013-10-14 11:00:02 +02001410 chain = &basechain->chain;
1411
Patrick McHardy115a60b2014-01-03 12:16:15 +00001412 for (i = 0; i < afi->nops; i++) {
1413 ops = &basechain->ops[i];
1414 ops->pf = family;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001415 ops->hooknum = hooknum;
1416 ops->priority = priority;
1417 ops->priv = chain;
1418 ops->hook = afi->hooks[ops->hooknum];
Pablo Neira Ayuso2cbce132015-06-12 13:55:41 +02001419 ops->dev = dev;
Patrick McHardy115a60b2014-01-03 12:16:15 +00001420 if (hookfn)
1421 ops->hook = hookfn;
1422 if (afi->hook_ops_init)
1423 afi->hook_ops_init(ops, i);
1424 }
Patrick McHardy96518512013-10-14 11:00:02 +02001425
1426 chain->flags |= NFT_BASE_CHAIN;
Patrick McHardy57de2a02014-01-09 18:42:31 +00001427 basechain->policy = policy;
Patrick McHardy96518512013-10-14 11:00:02 +02001428 } else {
1429 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1430 if (chain == NULL)
1431 return -ENOMEM;
1432 }
1433
1434 INIT_LIST_HEAD(&chain->rules);
1435 chain->handle = nf_tables_alloc_handle(table);
Pablo Neira Ayusob5bc89b2013-10-10 16:49:19 +02001436 chain->table = table;
Patrick McHardy96518512013-10-14 11:00:02 +02001437 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1438
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +02001439 err = nf_tables_register_hooks(net, table, chain, afi->nops);
Pablo Neira Ayusod8ee8f72015-06-15 02:42:31 +02001440 if (err < 0)
1441 goto err1;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001442
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01001443 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001444 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1445 if (err < 0)
1446 goto err2;
1447
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001448 table->use++;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001449 list_add_tail_rcu(&chain->list, &table->chains);
Patrick McHardy96518512013-10-14 11:00:02 +02001450 return 0;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001451err2:
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +02001452 nf_tables_unregister_hooks(net, table, chain, afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001453err1:
1454 nf_tables_chain_destroy(chain);
1455 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001456}
1457
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01001458static int nf_tables_delchain(struct net *net, struct sock *nlsk,
1459 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02001460 const struct nlattr * const nla[])
1461{
1462 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001463 u8 genmask = nft_genmask_next(net);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001464 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001465 struct nft_table *table;
1466 struct nft_chain *chain;
1467 int family = nfmsg->nfgen_family;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001468 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02001469
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001470 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001471 if (IS_ERR(afi))
1472 return PTR_ERR(afi);
1473
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001474 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001475 if (IS_ERR(table))
1476 return PTR_ERR(table);
1477
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02001478 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001479 if (IS_ERR(chain))
1480 return PTR_ERR(chain);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02001481 if (chain->use > 0)
Patrick McHardy96518512013-10-14 11:00:02 +02001482 return -EBUSY;
1483
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01001484 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02001485
Arturo Borreroee01d542014-09-02 16:42:25 +02001486 return nft_delchain(&ctx);
Patrick McHardy96518512013-10-14 11:00:02 +02001487}
1488
Patrick McHardy96518512013-10-14 11:00:02 +02001489/*
1490 * Expressions
1491 */
1492
1493/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001494 * nft_register_expr - register nf_tables expr type
1495 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001496 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001497 * Registers the expr type for use with nf_tables. Returns zero on
Patrick McHardy96518512013-10-14 11:00:02 +02001498 * success or a negative errno code otherwise.
1499 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001500int nft_register_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001501{
1502 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001503 if (type->family == NFPROTO_UNSPEC)
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001504 list_add_tail_rcu(&type->list, &nf_tables_expressions);
Tomasz Bursztyka758dbce2014-04-14 15:41:26 +03001505 else
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001506 list_add_rcu(&type->list, &nf_tables_expressions);
Patrick McHardy96518512013-10-14 11:00:02 +02001507 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1508 return 0;
1509}
1510EXPORT_SYMBOL_GPL(nft_register_expr);
1511
1512/**
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001513 * nft_unregister_expr - unregister nf_tables expr type
1514 * @ops: expr type
Patrick McHardy96518512013-10-14 11:00:02 +02001515 *
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001516 * Unregisters the expr typefor use with nf_tables.
Patrick McHardy96518512013-10-14 11:00:02 +02001517 */
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001518void nft_unregister_expr(struct nft_expr_type *type)
Patrick McHardy96518512013-10-14 11:00:02 +02001519{
1520 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001521 list_del_rcu(&type->list);
Patrick McHardy96518512013-10-14 11:00:02 +02001522 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1523}
1524EXPORT_SYMBOL_GPL(nft_unregister_expr);
1525
Patrick McHardy64d46802014-02-05 15:03:37 +00001526static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1527 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001528{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001529 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001530
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001531 list_for_each_entry(type, &nf_tables_expressions, list) {
Patrick McHardy64d46802014-02-05 15:03:37 +00001532 if (!nla_strcmp(nla, type->name) &&
1533 (!type->family || type->family == family))
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001534 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001535 }
1536 return NULL;
1537}
1538
Patrick McHardy64d46802014-02-05 15:03:37 +00001539static const struct nft_expr_type *nft_expr_type_get(u8 family,
1540 struct nlattr *nla)
Patrick McHardy96518512013-10-14 11:00:02 +02001541{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001542 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001543
1544 if (nla == NULL)
1545 return ERR_PTR(-EINVAL);
1546
Patrick McHardy64d46802014-02-05 15:03:37 +00001547 type = __nft_expr_type_get(family, nla);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001548 if (type != NULL && try_module_get(type->owner))
1549 return type;
Patrick McHardy96518512013-10-14 11:00:02 +02001550
1551#ifdef CONFIG_MODULES
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001552 if (type == NULL) {
Patrick McHardy96518512013-10-14 11:00:02 +02001553 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001554 request_module("nft-expr-%u-%.*s", family,
1555 nla_len(nla), (char *)nla_data(nla));
1556 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1557 if (__nft_expr_type_get(family, nla))
1558 return ERR_PTR(-EAGAIN);
1559
1560 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy96518512013-10-14 11:00:02 +02001561 request_module("nft-expr-%.*s",
1562 nla_len(nla), (char *)nla_data(nla));
1563 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Patrick McHardy64d46802014-02-05 15:03:37 +00001564 if (__nft_expr_type_get(family, nla))
Patrick McHardy96518512013-10-14 11:00:02 +02001565 return ERR_PTR(-EAGAIN);
1566 }
1567#endif
1568 return ERR_PTR(-ENOENT);
1569}
1570
1571static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1572 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1573 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1574};
1575
1576static int nf_tables_fill_expr_info(struct sk_buff *skb,
1577 const struct nft_expr *expr)
1578{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001579 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
Patrick McHardy96518512013-10-14 11:00:02 +02001580 goto nla_put_failure;
1581
1582 if (expr->ops->dump) {
1583 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1584 if (data == NULL)
1585 goto nla_put_failure;
1586 if (expr->ops->dump(skb, expr) < 0)
1587 goto nla_put_failure;
1588 nla_nest_end(skb, data);
1589 }
1590
1591 return skb->len;
1592
1593nla_put_failure:
1594 return -1;
1595};
1596
Patrick McHardy0b2d8a72015-04-11 10:46:38 +01001597int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
1598 const struct nft_expr *expr)
1599{
1600 struct nlattr *nest;
1601
1602 nest = nla_nest_start(skb, attr);
1603 if (!nest)
1604 goto nla_put_failure;
1605 if (nf_tables_fill_expr_info(skb, expr) < 0)
1606 goto nla_put_failure;
1607 nla_nest_end(skb, nest);
1608 return 0;
1609
1610nla_put_failure:
1611 return -1;
1612}
1613
Patrick McHardy96518512013-10-14 11:00:02 +02001614struct nft_expr_info {
1615 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001616 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001617};
1618
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001619static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1620 const struct nlattr *nla,
Patrick McHardy96518512013-10-14 11:00:02 +02001621 struct nft_expr_info *info)
1622{
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001623 const struct nft_expr_type *type;
Patrick McHardy96518512013-10-14 11:00:02 +02001624 const struct nft_expr_ops *ops;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001625 struct nlattr *tb[NFTA_EXPR_MAX + 1];
Patrick McHardy96518512013-10-14 11:00:02 +02001626 int err;
1627
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001628 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
Patrick McHardy96518512013-10-14 11:00:02 +02001629 if (err < 0)
1630 return err;
1631
Patrick McHardy64d46802014-02-05 15:03:37 +00001632 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001633 if (IS_ERR(type))
1634 return PTR_ERR(type);
1635
1636 if (tb[NFTA_EXPR_DATA]) {
1637 err = nla_parse_nested(info->tb, type->maxattr,
1638 tb[NFTA_EXPR_DATA], type->policy);
1639 if (err < 0)
1640 goto err1;
1641 } else
1642 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1643
1644 if (type->select_ops != NULL) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001645 ops = type->select_ops(ctx,
1646 (const struct nlattr * const *)info->tb);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001647 if (IS_ERR(ops)) {
1648 err = PTR_ERR(ops);
1649 goto err1;
1650 }
1651 } else
1652 ops = type->ops;
1653
Patrick McHardy96518512013-10-14 11:00:02 +02001654 info->ops = ops;
1655 return 0;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001656
1657err1:
1658 module_put(type->owner);
1659 return err;
Patrick McHardy96518512013-10-14 11:00:02 +02001660}
1661
1662static int nf_tables_newexpr(const struct nft_ctx *ctx,
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001663 const struct nft_expr_info *info,
Patrick McHardy96518512013-10-14 11:00:02 +02001664 struct nft_expr *expr)
1665{
1666 const struct nft_expr_ops *ops = info->ops;
1667 int err;
1668
1669 expr->ops = ops;
1670 if (ops->init) {
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001671 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
Patrick McHardy96518512013-10-14 11:00:02 +02001672 if (err < 0)
1673 goto err1;
1674 }
1675
Patrick McHardy96518512013-10-14 11:00:02 +02001676 return 0;
1677
1678err1:
1679 expr->ops = NULL;
1680 return err;
1681}
1682
Patrick McHardy62472bc2014-03-07 19:08:30 +01001683static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1684 struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +02001685{
1686 if (expr->ops->destroy)
Patrick McHardy62472bc2014-03-07 19:08:30 +01001687 expr->ops->destroy(ctx, expr);
Patrick McHardyef1f7df2013-10-10 11:41:20 +02001688 module_put(expr->ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02001689}
1690
Patrick McHardy0b2d8a72015-04-11 10:46:38 +01001691struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
1692 const struct nlattr *nla)
1693{
1694 struct nft_expr_info info;
1695 struct nft_expr *expr;
1696 int err;
1697
1698 err = nf_tables_expr_parse(ctx, nla, &info);
1699 if (err < 0)
1700 goto err1;
1701
1702 err = -ENOMEM;
1703 expr = kzalloc(info.ops->size, GFP_KERNEL);
1704 if (expr == NULL)
1705 goto err2;
1706
1707 err = nf_tables_newexpr(ctx, &info, expr);
1708 if (err < 0)
1709 goto err2;
1710
1711 return expr;
1712err2:
1713 module_put(info.ops->type->owner);
1714err1:
1715 return ERR_PTR(err);
1716}
1717
1718void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
1719{
1720 nf_tables_expr_destroy(ctx, expr);
1721 kfree(expr);
1722}
1723
Patrick McHardy96518512013-10-14 11:00:02 +02001724/*
1725 * Rules
1726 */
1727
1728static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1729 u64 handle)
1730{
1731 struct nft_rule *rule;
1732
1733 // FIXME: this sucks
1734 list_for_each_entry(rule, &chain->rules, list) {
1735 if (handle == rule->handle)
1736 return rule;
1737 }
1738
1739 return ERR_PTR(-ENOENT);
1740}
1741
1742static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1743 const struct nlattr *nla)
1744{
1745 if (nla == NULL)
1746 return ERR_PTR(-EINVAL);
1747
1748 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1749}
1750
1751static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1752 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1753 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1754 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1755 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1756 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001757 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
Eric Leblond5e948462013-10-10 13:41:44 +02001758 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001759 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1760 .len = NFT_USERDATA_MAXLEN },
Patrick McHardy96518512013-10-14 11:00:02 +02001761};
1762
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001763static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1764 u32 portid, u32 seq, int event,
1765 u32 flags, int family,
Patrick McHardy96518512013-10-14 11:00:02 +02001766 const struct nft_table *table,
1767 const struct nft_chain *chain,
1768 const struct nft_rule *rule)
1769{
1770 struct nlmsghdr *nlh;
1771 struct nfgenmsg *nfmsg;
1772 const struct nft_expr *expr, *next;
1773 struct nlattr *list;
Eric Leblond5e948462013-10-10 13:41:44 +02001774 const struct nft_rule *prule;
1775 int type = event | NFNL_SUBSYS_NFTABLES << 8;
Patrick McHardy96518512013-10-14 11:00:02 +02001776
Eric Leblond5e948462013-10-10 13:41:44 +02001777 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
Patrick McHardy96518512013-10-14 11:00:02 +02001778 flags);
1779 if (nlh == NULL)
1780 goto nla_put_failure;
1781
1782 nfmsg = nlmsg_data(nlh);
1783 nfmsg->nfgen_family = family;
1784 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001785 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
Patrick McHardy96518512013-10-14 11:00:02 +02001786
1787 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1788 goto nla_put_failure;
1789 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1790 goto nla_put_failure;
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +02001791 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle),
1792 NFTA_RULE_PAD))
Patrick McHardy96518512013-10-14 11:00:02 +02001793 goto nla_put_failure;
1794
Eric Leblond5e948462013-10-10 13:41:44 +02001795 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1796 prule = list_entry(rule->list.prev, struct nft_rule, list);
1797 if (nla_put_be64(skb, NFTA_RULE_POSITION,
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +02001798 cpu_to_be64(prule->handle),
1799 NFTA_RULE_PAD))
Eric Leblond5e948462013-10-10 13:41:44 +02001800 goto nla_put_failure;
1801 }
1802
Patrick McHardy96518512013-10-14 11:00:02 +02001803 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1804 if (list == NULL)
1805 goto nla_put_failure;
1806 nft_rule_for_each_expr(expr, next, rule) {
Patrick McHardy0b2d8a72015-04-11 10:46:38 +01001807 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0)
Patrick McHardy96518512013-10-14 11:00:02 +02001808 goto nla_put_failure;
Patrick McHardy96518512013-10-14 11:00:02 +02001809 }
1810 nla_nest_end(skb, list);
1811
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001812 if (rule->udata) {
1813 struct nft_userdata *udata = nft_userdata(rule);
1814 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
1815 udata->data) < 0)
1816 goto nla_put_failure;
1817 }
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01001818
Johannes Berg053c0952015-01-16 22:09:00 +01001819 nlmsg_end(skb, nlh);
1820 return 0;
Patrick McHardy96518512013-10-14 11:00:02 +02001821
1822nla_put_failure:
1823 nlmsg_trim(skb, nlh);
1824 return -1;
1825}
1826
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001827static int nf_tables_rule_notify(const struct nft_ctx *ctx,
Patrick McHardy96518512013-10-14 11:00:02 +02001828 const struct nft_rule *rule,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001829 int event)
Patrick McHardy96518512013-10-14 11:00:02 +02001830{
1831 struct sk_buff *skb;
Patrick McHardy96518512013-10-14 11:00:02 +02001832 int err;
1833
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001834 if (!ctx->report &&
1835 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy96518512013-10-14 11:00:02 +02001836 return 0;
1837
1838 err = -ENOBUFS;
1839 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1840 if (skb == NULL)
1841 goto err;
1842
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001843 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
1844 event, 0, ctx->afi->family, ctx->table,
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02001845 ctx->chain, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02001846 if (err < 0) {
1847 kfree_skb(skb);
1848 goto err;
1849 }
1850
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001851 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1852 ctx->report, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02001853err:
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02001854 if (err < 0) {
1855 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1856 err);
1857 }
Patrick McHardy96518512013-10-14 11:00:02 +02001858 return err;
1859}
1860
1861static int nf_tables_dump_rules(struct sk_buff *skb,
1862 struct netlink_callback *cb)
1863{
1864 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1865 const struct nft_af_info *afi;
1866 const struct nft_table *table;
1867 const struct nft_chain *chain;
1868 const struct nft_rule *rule;
1869 unsigned int idx = 0, s_idx = cb->args[0];
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001870 struct net *net = sock_net(skb->sk);
Patrick McHardy96518512013-10-14 11:00:02 +02001871 int family = nfmsg->nfgen_family;
1872
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001873 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001874 cb->seq = net->nft.base_seq;
1875
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001876 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Patrick McHardy96518512013-10-14 11:00:02 +02001877 if (family != NFPROTO_UNSPEC && family != afi->family)
1878 continue;
1879
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001880 list_for_each_entry_rcu(table, &afi->tables, list) {
1881 list_for_each_entry_rcu(chain, &table->chains, list) {
1882 list_for_each_entry_rcu(rule, &chain->rules, list) {
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02001883 if (!nft_is_active(net, rule))
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02001884 goto cont;
Patrick McHardy96518512013-10-14 11:00:02 +02001885 if (idx < s_idx)
1886 goto cont;
1887 if (idx > s_idx)
1888 memset(&cb->args[1], 0,
1889 sizeof(cb->args) - sizeof(cb->args[0]));
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001890 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001891 cb->nlh->nlmsg_seq,
1892 NFT_MSG_NEWRULE,
1893 NLM_F_MULTI | NLM_F_APPEND,
1894 afi->family, table, chain, rule) < 0)
1895 goto done;
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02001896
1897 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Patrick McHardy96518512013-10-14 11:00:02 +02001898cont:
1899 idx++;
1900 }
1901 }
1902 }
1903 }
1904done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02001905 rcu_read_unlock();
1906
Patrick McHardy96518512013-10-14 11:00:02 +02001907 cb->args[0] = idx;
1908 return skb->len;
1909}
1910
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +01001911static int nf_tables_getrule(struct net *net, struct sock *nlsk,
1912 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02001913 const struct nlattr * const nla[])
1914{
1915 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001916 u8 genmask = nft_genmask_cur(net);
Patrick McHardy96518512013-10-14 11:00:02 +02001917 const struct nft_af_info *afi;
1918 const struct nft_table *table;
1919 const struct nft_chain *chain;
1920 const struct nft_rule *rule;
1921 struct sk_buff *skb2;
1922 int family = nfmsg->nfgen_family;
1923 int err;
1924
1925 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1926 struct netlink_dump_control c = {
1927 .dump = nf_tables_dump_rules,
1928 };
1929 return netlink_dump_start(nlsk, skb, nlh, &c);
1930 }
1931
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02001932 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02001933 if (IS_ERR(afi))
1934 return PTR_ERR(afi);
1935
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001936 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001937 if (IS_ERR(table))
1938 return PTR_ERR(table);
1939
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02001940 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02001941 if (IS_ERR(chain))
1942 return PTR_ERR(chain);
1943
1944 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1945 if (IS_ERR(rule))
1946 return PTR_ERR(rule);
1947
1948 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1949 if (!skb2)
1950 return -ENOMEM;
1951
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02001952 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
Patrick McHardy96518512013-10-14 11:00:02 +02001953 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1954 family, table, chain, rule);
1955 if (err < 0)
1956 goto err;
1957
1958 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1959
1960err:
1961 kfree_skb(skb2);
1962 return err;
1963}
1964
Patrick McHardy62472bc2014-03-07 19:08:30 +01001965static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1966 struct nft_rule *rule)
Patrick McHardy96518512013-10-14 11:00:02 +02001967{
Patrick McHardy96518512013-10-14 11:00:02 +02001968 struct nft_expr *expr;
1969
1970 /*
1971 * Careful: some expressions might not be initialized in case this
1972 * is called on error from nf_tables_newrule().
1973 */
1974 expr = nft_expr_first(rule);
1975 while (expr->ops && expr != nft_expr_last(rule)) {
Patrick McHardy62472bc2014-03-07 19:08:30 +01001976 nf_tables_expr_destroy(ctx, expr);
Patrick McHardy96518512013-10-14 11:00:02 +02001977 expr = nft_expr_next(expr);
1978 }
1979 kfree(rule);
1980}
1981
Patrick McHardy96518512013-10-14 11:00:02 +02001982#define NFT_RULE_MAXEXPRS 128
1983
1984static struct nft_expr_info *info;
1985
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01001986static int nf_tables_newrule(struct net *net, struct sock *nlsk,
1987 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02001988 const struct nlattr * const nla[])
1989{
1990 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02001991 u8 genmask = nft_genmask_next(net);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02001992 struct nft_af_info *afi;
Patrick McHardy96518512013-10-14 11:00:02 +02001993 struct nft_table *table;
1994 struct nft_chain *chain;
1995 struct nft_rule *rule, *old_rule = NULL;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00001996 struct nft_userdata *udata;
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02001997 struct nft_trans *trans = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02001998 struct nft_expr *expr;
1999 struct nft_ctx ctx;
2000 struct nlattr *tmp;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002001 unsigned int size, i, n, ulen = 0, usize = 0;
Patrick McHardy96518512013-10-14 11:00:02 +02002002 int err, rem;
2003 bool create;
Eric Leblond5e948462013-10-10 13:41:44 +02002004 u64 handle, pos_handle;
Patrick McHardy96518512013-10-14 11:00:02 +02002005
2006 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2007
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002008 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy96518512013-10-14 11:00:02 +02002009 if (IS_ERR(afi))
2010 return PTR_ERR(afi);
2011
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002012 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02002013 if (IS_ERR(table))
2014 return PTR_ERR(table);
2015
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02002016 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02002017 if (IS_ERR(chain))
2018 return PTR_ERR(chain);
2019
2020 if (nla[NFTA_RULE_HANDLE]) {
2021 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
2022 rule = __nf_tables_rule_lookup(chain, handle);
2023 if (IS_ERR(rule))
2024 return PTR_ERR(rule);
2025
2026 if (nlh->nlmsg_flags & NLM_F_EXCL)
2027 return -EEXIST;
2028 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2029 old_rule = rule;
2030 else
2031 return -EOPNOTSUPP;
2032 } else {
2033 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
2034 return -EINVAL;
2035 handle = nf_tables_alloc_handle(table);
Pablo Neira Ayusoa0a73792014-06-10 10:53:01 +02002036
2037 if (chain->use == UINT_MAX)
2038 return -EOVERFLOW;
Patrick McHardy96518512013-10-14 11:00:02 +02002039 }
2040
Eric Leblond5e948462013-10-10 13:41:44 +02002041 if (nla[NFTA_RULE_POSITION]) {
2042 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2043 return -EOPNOTSUPP;
2044
2045 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
2046 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
2047 if (IS_ERR(old_rule))
2048 return PTR_ERR(old_rule);
2049 }
2050
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002051 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002052
Patrick McHardy96518512013-10-14 11:00:02 +02002053 n = 0;
2054 size = 0;
2055 if (nla[NFTA_RULE_EXPRESSIONS]) {
2056 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
2057 err = -EINVAL;
2058 if (nla_type(tmp) != NFTA_LIST_ELEM)
2059 goto err1;
2060 if (n == NFT_RULE_MAXEXPRS)
2061 goto err1;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02002062 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
Patrick McHardy96518512013-10-14 11:00:02 +02002063 if (err < 0)
2064 goto err1;
2065 size += info[n].ops->size;
2066 n++;
2067 }
2068 }
Patrick McHardy98898402015-03-03 20:04:19 +00002069 /* Check for overflow of dlen field */
2070 err = -EFBIG;
2071 if (size >= 1 << 12)
2072 goto err1;
Patrick McHardy96518512013-10-14 11:00:02 +02002073
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002074 if (nla[NFTA_RULE_USERDATA]) {
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01002075 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002076 if (ulen > 0)
2077 usize = sizeof(struct nft_userdata) + ulen;
2078 }
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01002079
Patrick McHardy96518512013-10-14 11:00:02 +02002080 err = -ENOMEM;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002081 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
Patrick McHardy96518512013-10-14 11:00:02 +02002082 if (rule == NULL)
2083 goto err1;
2084
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02002085 nft_activate_next(net, rule);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002086
Patrick McHardy96518512013-10-14 11:00:02 +02002087 rule->handle = handle;
2088 rule->dlen = size;
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002089 rule->udata = ulen ? 1 : 0;
Pablo Neira Ayuso0768b3b2014-02-19 17:27:06 +01002090
Patrick McHardy86f1ec32015-03-03 20:04:20 +00002091 if (ulen) {
2092 udata = nft_userdata(rule);
2093 udata->len = ulen - 1;
2094 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
2095 }
Patrick McHardy96518512013-10-14 11:00:02 +02002096
Patrick McHardy96518512013-10-14 11:00:02 +02002097 expr = nft_expr_first(rule);
2098 for (i = 0; i < n; i++) {
2099 err = nf_tables_newexpr(&ctx, &info[i], expr);
2100 if (err < 0)
2101 goto err2;
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002102 info[i].ops = NULL;
Patrick McHardy96518512013-10-14 11:00:02 +02002103 expr = nft_expr_next(expr);
2104 }
2105
Patrick McHardy96518512013-10-14 11:00:02 +02002106 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02002107 if (nft_is_active_next(net, old_rule)) {
Pablo Neira Ayusoac904ac2014-06-10 10:53:03 +02002108 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02002109 old_rule);
Pablo Neira Ayuso1081d112014-04-04 01:24:07 +02002110 if (trans == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002111 err = -ENOMEM;
2112 goto err2;
2113 }
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02002114 nft_deactivate_next(net, old_rule);
Pablo Neira Ayusoac34b862014-06-10 10:53:02 +02002115 chain->use--;
Pablo Neira Ayuso5bc5c302014-06-10 10:53:00 +02002116 list_add_tail_rcu(&rule->list, &old_rule->list);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002117 } else {
2118 err = -ENOENT;
2119 goto err2;
2120 }
Patrick McHardy96518512013-10-14 11:00:02 +02002121 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
Eric Leblond5e948462013-10-10 13:41:44 +02002122 if (old_rule)
2123 list_add_rcu(&rule->list, &old_rule->list);
2124 else
2125 list_add_tail_rcu(&rule->list, &chain->rules);
2126 else {
2127 if (old_rule)
2128 list_add_tail_rcu(&rule->list, &old_rule->list);
2129 else
2130 list_add_rcu(&rule->list, &chain->rules);
2131 }
Patrick McHardy96518512013-10-14 11:00:02 +02002132
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02002133 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002134 err = -ENOMEM;
2135 goto err3;
2136 }
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02002137 chain->use++;
Patrick McHardy96518512013-10-14 11:00:02 +02002138 return 0;
2139
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002140err3:
2141 list_del_rcu(&rule->list);
Patrick McHardy96518512013-10-14 11:00:02 +02002142err2:
Patrick McHardy62472bc2014-03-07 19:08:30 +01002143 nf_tables_rule_destroy(&ctx, rule);
Patrick McHardy96518512013-10-14 11:00:02 +02002144err1:
2145 for (i = 0; i < n; i++) {
2146 if (info[i].ops != NULL)
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002147 module_put(info[i].ops->type->owner);
Patrick McHardy96518512013-10-14 11:00:02 +02002148 }
2149 return err;
2150}
2151
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002152static int nf_tables_delrule(struct net *net, struct sock *nlsk,
2153 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy96518512013-10-14 11:00:02 +02002154 const struct nlattr * const nla[])
2155{
2156 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002157 u8 genmask = nft_genmask_next(net);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002158 struct nft_af_info *afi;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002159 struct nft_table *table;
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002160 struct nft_chain *chain = NULL;
2161 struct nft_rule *rule;
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002162 int family = nfmsg->nfgen_family, err = 0;
2163 struct nft_ctx ctx;
Patrick McHardy96518512013-10-14 11:00:02 +02002164
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002165 afi = nf_tables_afinfo_lookup(net, family, false);
Patrick McHardy96518512013-10-14 11:00:02 +02002166 if (IS_ERR(afi))
2167 return PTR_ERR(afi);
2168
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002169 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02002170 if (IS_ERR(table))
2171 return PTR_ERR(table);
2172
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002173 if (nla[NFTA_RULE_CHAIN]) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02002174 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN],
2175 genmask);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002176 if (IS_ERR(chain))
2177 return PTR_ERR(chain);
2178 }
Patrick McHardy96518512013-10-14 11:00:02 +02002179
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002180 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002181
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002182 if (chain) {
2183 if (nla[NFTA_RULE_HANDLE]) {
2184 rule = nf_tables_rule_lookup(chain,
2185 nla[NFTA_RULE_HANDLE]);
2186 if (IS_ERR(rule))
2187 return PTR_ERR(rule);
Patrick McHardy96518512013-10-14 11:00:02 +02002188
Arturo Borrero5e266fe2014-09-02 16:42:21 +02002189 err = nft_delrule(&ctx, rule);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002190 } else {
Arturo Borreroce24b722014-09-02 16:42:24 +02002191 err = nft_delrule_by_chain(&ctx);
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002192 }
2193 } else {
2194 list_for_each_entry(chain, &table->chains, list) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02002195 if (!nft_is_active_next(net, chain))
2196 continue;
2197
Pablo Neira Ayusocf9dc092013-11-24 20:39:10 +01002198 ctx.chain = chain;
Arturo Borreroce24b722014-09-02 16:42:24 +02002199 err = nft_delrule_by_chain(&ctx);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002200 if (err < 0)
2201 break;
Patrick McHardy96518512013-10-14 11:00:02 +02002202 }
2203 }
2204
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02002205 return err;
2206}
2207
Patrick McHardy20a69342013-10-11 12:06:22 +02002208/*
2209 * Sets
2210 */
2211
2212static LIST_HEAD(nf_tables_set_ops);
2213
2214int nft_register_set(struct nft_set_ops *ops)
2215{
2216 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002217 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
Patrick McHardy20a69342013-10-11 12:06:22 +02002218 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2219 return 0;
2220}
2221EXPORT_SYMBOL_GPL(nft_register_set);
2222
2223void nft_unregister_set(struct nft_set_ops *ops)
2224{
2225 nfnl_lock(NFNL_SUBSYS_NFTABLES);
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002226 list_del_rcu(&ops->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002227 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2228}
2229EXPORT_SYMBOL_GPL(nft_unregister_set);
2230
Patrick McHardyc50b9602014-03-28 10:19:47 +00002231/*
2232 * Select a set implementation based on the data characteristics and the
2233 * given policy. The total memory use might not be known if no size is
2234 * given, in that case the amount of memory per element is used.
2235 */
2236static const struct nft_set_ops *
2237nft_select_set_ops(const struct nlattr * const nla[],
2238 const struct nft_set_desc *desc,
2239 enum nft_set_policies policy)
Patrick McHardy20a69342013-10-11 12:06:22 +02002240{
Patrick McHardyc50b9602014-03-28 10:19:47 +00002241 const struct nft_set_ops *ops, *bops;
2242 struct nft_set_estimate est, best;
Patrick McHardy20a69342013-10-11 12:06:22 +02002243 u32 features;
2244
2245#ifdef CONFIG_MODULES
2246 if (list_empty(&nf_tables_set_ops)) {
2247 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2248 request_module("nft-set");
2249 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2250 if (!list_empty(&nf_tables_set_ops))
2251 return ERR_PTR(-EAGAIN);
2252 }
2253#endif
2254 features = 0;
2255 if (nla[NFTA_SET_FLAGS] != NULL) {
2256 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
Patrick McHardy4a8678e2015-04-05 14:41:05 +02002257 features &= NFT_SET_INTERVAL | NFT_SET_MAP | NFT_SET_TIMEOUT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002258 }
2259
Patrick McHardyc50b9602014-03-28 10:19:47 +00002260 bops = NULL;
2261 best.size = ~0;
2262 best.class = ~0;
2263
Patrick McHardy20a69342013-10-11 12:06:22 +02002264 list_for_each_entry(ops, &nf_tables_set_ops, list) {
2265 if ((ops->features & features) != features)
2266 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002267 if (!ops->estimate(desc, features, &est))
2268 continue;
2269
2270 switch (policy) {
2271 case NFT_SET_POL_PERFORMANCE:
2272 if (est.class < best.class)
2273 break;
2274 if (est.class == best.class && est.size < best.size)
2275 break;
2276 continue;
2277 case NFT_SET_POL_MEMORY:
2278 if (est.size < best.size)
2279 break;
2280 if (est.size == best.size && est.class < best.class)
2281 break;
2282 continue;
2283 default:
2284 break;
2285 }
2286
Patrick McHardy20a69342013-10-11 12:06:22 +02002287 if (!try_module_get(ops->owner))
2288 continue;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002289 if (bops != NULL)
2290 module_put(bops->owner);
2291
2292 bops = ops;
2293 best = est;
Patrick McHardy20a69342013-10-11 12:06:22 +02002294 }
2295
Patrick McHardyc50b9602014-03-28 10:19:47 +00002296 if (bops != NULL)
2297 return bops;
2298
Patrick McHardy20a69342013-10-11 12:06:22 +02002299 return ERR_PTR(-EOPNOTSUPP);
2300}
2301
2302static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2303 [NFTA_SET_TABLE] = { .type = NLA_STRING },
Pablo Neira Ayusoa9bdd832014-03-24 15:10:37 +01002304 [NFTA_SET_NAME] = { .type = NLA_STRING,
Pablo Neira Ayusocb39ad82016-05-04 17:49:53 +02002305 .len = NFT_SET_MAXNAMELEN - 1 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002306 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2307 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2308 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2309 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2310 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002311 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2312 [NFTA_SET_DESC] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002313 [NFTA_SET_ID] = { .type = NLA_U32 },
Patrick McHardy761da292015-03-26 12:39:36 +00002314 [NFTA_SET_TIMEOUT] = { .type = NLA_U64 },
2315 [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 },
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002316 [NFTA_SET_USERDATA] = { .type = NLA_BINARY,
2317 .len = NFT_USERDATA_MAXLEN },
Patrick McHardyc50b9602014-03-28 10:19:47 +00002318};
2319
2320static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2321 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02002322};
2323
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002324static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net,
Patrick McHardy20a69342013-10-11 12:06:22 +02002325 const struct sk_buff *skb,
2326 const struct nlmsghdr *nlh,
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002327 const struct nlattr * const nla[],
2328 u8 genmask)
Patrick McHardy20a69342013-10-11 12:06:22 +02002329{
2330 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002331 struct nft_af_info *afi = NULL;
2332 struct nft_table *table = NULL;
Patrick McHardy20a69342013-10-11 12:06:22 +02002333
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002334 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2335 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2336 if (IS_ERR(afi))
2337 return PTR_ERR(afi);
2338 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002339
2340 if (nla[NFTA_SET_TABLE] != NULL) {
Patrick McHardyec2c9932014-02-05 15:03:35 +00002341 if (afi == NULL)
2342 return -EAFNOSUPPORT;
2343
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002344 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE],
2345 genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002346 if (IS_ERR(table))
2347 return PTR_ERR(table);
2348 }
2349
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002350 nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002351 return 0;
2352}
2353
2354struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002355 const struct nlattr *nla, u8 genmask)
Patrick McHardy20a69342013-10-11 12:06:22 +02002356{
2357 struct nft_set *set;
2358
2359 if (nla == NULL)
2360 return ERR_PTR(-EINVAL);
2361
2362 list_for_each_entry(set, &table->sets, list) {
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002363 if (!nla_strcmp(nla, set->name) &&
2364 nft_active_genmask(set, genmask))
Patrick McHardy20a69342013-10-11 12:06:22 +02002365 return set;
2366 }
2367 return ERR_PTR(-ENOENT);
2368}
2369
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002370struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002371 const struct nlattr *nla,
2372 u8 genmask)
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002373{
2374 struct nft_trans *trans;
2375 u32 id = ntohl(nla_get_be32(nla));
2376
2377 list_for_each_entry(trans, &net->nft.commit_list, list) {
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002378 struct nft_set *set = nft_trans_set(trans);
2379
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002380 if (trans->msg_type == NFT_MSG_NEWSET &&
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002381 id == nft_trans_set_id(trans) &&
2382 nft_active_genmask(set, genmask))
2383 return set;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002384 }
2385 return ERR_PTR(-ENOENT);
2386}
2387
Patrick McHardy20a69342013-10-11 12:06:22 +02002388static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2389 const char *name)
2390{
2391 const struct nft_set *i;
2392 const char *p;
2393 unsigned long *inuse;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002394 unsigned int n = 0, min = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002395
Pablo Neira Ayusocb39ad82016-05-04 17:49:53 +02002396 p = strnchr(name, NFT_SET_MAXNAMELEN, '%');
Patrick McHardy20a69342013-10-11 12:06:22 +02002397 if (p != NULL) {
2398 if (p[1] != 'd' || strchr(p + 2, '%'))
2399 return -EINVAL;
2400
2401 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2402 if (inuse == NULL)
2403 return -ENOMEM;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002404cont:
Patrick McHardy20a69342013-10-11 12:06:22 +02002405 list_for_each_entry(i, &ctx->table->sets, list) {
Daniel Borkmann14662912013-12-31 12:40:05 +01002406 int tmp;
2407
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002408 if (!nft_is_active_next(ctx->net, set))
2409 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002410 if (!sscanf(i->name, name, &tmp))
Patrick McHardy20a69342013-10-11 12:06:22 +02002411 continue;
Patrick McHardy60eb1892014-03-07 12:34:05 +01002412 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
Patrick McHardy20a69342013-10-11 12:06:22 +02002413 continue;
Daniel Borkmann14662912013-12-31 12:40:05 +01002414
Patrick McHardy60eb1892014-03-07 12:34:05 +01002415 set_bit(tmp - min, inuse);
Patrick McHardy20a69342013-10-11 12:06:22 +02002416 }
2417
Patrick McHardy53b70282014-02-05 12:26:22 +01002418 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
Patrick McHardy60eb1892014-03-07 12:34:05 +01002419 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2420 min += BITS_PER_BYTE * PAGE_SIZE;
2421 memset(inuse, 0, PAGE_SIZE);
2422 goto cont;
2423 }
Patrick McHardy20a69342013-10-11 12:06:22 +02002424 free_page((unsigned long)inuse);
2425 }
2426
Patrick McHardy60eb1892014-03-07 12:34:05 +01002427 snprintf(set->name, sizeof(set->name), name, min + n);
Patrick McHardy20a69342013-10-11 12:06:22 +02002428 list_for_each_entry(i, &ctx->table->sets, list) {
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002429 if (!nft_is_active_next(ctx->net, i))
2430 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02002431 if (!strcmp(set->name, i->name))
2432 return -ENFILE;
2433 }
2434 return 0;
2435}
2436
2437static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2438 const struct nft_set *set, u16 event, u16 flags)
2439{
2440 struct nfgenmsg *nfmsg;
2441 struct nlmsghdr *nlh;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002442 struct nlattr *desc;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002443 u32 portid = ctx->portid;
2444 u32 seq = ctx->seq;
Patrick McHardy20a69342013-10-11 12:06:22 +02002445
2446 event |= NFNL_SUBSYS_NFTABLES << 8;
2447 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2448 flags);
2449 if (nlh == NULL)
2450 goto nla_put_failure;
2451
2452 nfmsg = nlmsg_data(nlh);
2453 nfmsg->nfgen_family = ctx->afi->family;
2454 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02002455 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
Patrick McHardy20a69342013-10-11 12:06:22 +02002456
2457 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2458 goto nla_put_failure;
2459 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2460 goto nla_put_failure;
2461 if (set->flags != 0)
2462 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2463 goto nla_put_failure;
2464
2465 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2466 goto nla_put_failure;
2467 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2468 goto nla_put_failure;
2469 if (set->flags & NFT_SET_MAP) {
2470 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2471 goto nla_put_failure;
2472 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2473 goto nla_put_failure;
2474 }
2475
Patrick McHardy761da292015-03-26 12:39:36 +00002476 if (set->timeout &&
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +02002477 nla_put_be64(skb, NFTA_SET_TIMEOUT, cpu_to_be64(set->timeout),
2478 NFTA_SET_PAD))
Patrick McHardy761da292015-03-26 12:39:36 +00002479 goto nla_put_failure;
2480 if (set->gc_int &&
2481 nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int)))
2482 goto nla_put_failure;
2483
Arturo Borrero9363dc42014-09-23 13:30:41 +02002484 if (set->policy != NFT_SET_POL_PERFORMANCE) {
2485 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
2486 goto nla_put_failure;
2487 }
2488
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002489 if (nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata))
2490 goto nla_put_failure;
2491
Patrick McHardyc50b9602014-03-28 10:19:47 +00002492 desc = nla_nest_start(skb, NFTA_SET_DESC);
2493 if (desc == NULL)
2494 goto nla_put_failure;
2495 if (set->size &&
2496 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2497 goto nla_put_failure;
2498 nla_nest_end(skb, desc);
2499
Johannes Berg053c0952015-01-16 22:09:00 +01002500 nlmsg_end(skb, nlh);
2501 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002502
2503nla_put_failure:
2504 nlmsg_trim(skb, nlh);
2505 return -1;
2506}
2507
2508static int nf_tables_set_notify(const struct nft_ctx *ctx,
2509 const struct nft_set *set,
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002510 int event, gfp_t gfp_flags)
Patrick McHardy20a69342013-10-11 12:06:22 +02002511{
2512 struct sk_buff *skb;
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002513 u32 portid = ctx->portid;
Patrick McHardy20a69342013-10-11 12:06:22 +02002514 int err;
2515
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002516 if (!ctx->report &&
2517 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
Patrick McHardy20a69342013-10-11 12:06:22 +02002518 return 0;
2519
2520 err = -ENOBUFS;
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002521 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
Patrick McHardy20a69342013-10-11 12:06:22 +02002522 if (skb == NULL)
2523 goto err;
2524
2525 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2526 if (err < 0) {
2527 kfree_skb(skb);
2528 goto err;
2529 }
2530
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02002531 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002532 ctx->report, gfp_flags);
Patrick McHardy20a69342013-10-11 12:06:22 +02002533err:
2534 if (err < 0)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002535 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
Patrick McHardy20a69342013-10-11 12:06:22 +02002536 return err;
2537}
2538
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002539static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002540{
2541 const struct nft_set *set;
2542 unsigned int idx, s_idx = cb->args[0];
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002543 struct nft_af_info *afi;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002544 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2545 struct net *net = sock_net(skb->sk);
2546 int cur_family = cb->args[3];
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002547 struct nft_ctx *ctx = cb->data, ctx_set;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002548
2549 if (cb->args[1])
2550 return skb->len;
2551
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002552 rcu_read_lock();
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02002553 cb->seq = net->nft.base_seq;
2554
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002555 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002556 if (ctx->afi && ctx->afi != afi)
2557 continue;
2558
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002559 if (cur_family) {
2560 if (afi->family != cur_family)
2561 continue;
2562
2563 cur_family = 0;
2564 }
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002565 list_for_each_entry_rcu(table, &afi->tables, list) {
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002566 if (ctx->table && ctx->table != table)
2567 continue;
2568
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002569 if (cur_table) {
2570 if (cur_table != table)
2571 continue;
2572
2573 cur_table = NULL;
2574 }
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002575 idx = 0;
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002576 list_for_each_entry_rcu(set, &table->sets, list) {
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002577 if (idx < s_idx)
2578 goto cont;
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002579 if (!nft_is_active(net, set))
2580 goto cont;
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002581
2582 ctx_set = *ctx;
2583 ctx_set.table = table;
2584 ctx_set.afi = afi;
2585 if (nf_tables_fill_set(skb, &ctx_set, set,
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002586 NFT_MSG_NEWSET,
2587 NLM_F_MULTI) < 0) {
2588 cb->args[0] = idx;
2589 cb->args[2] = (unsigned long) table;
2590 cb->args[3] = afi->family;
2591 goto done;
2592 }
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02002593 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002594cont:
2595 idx++;
2596 }
2597 if (s_idx)
2598 s_idx = 0;
2599 }
2600 }
2601 cb->args[1] = 1;
2602done:
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002603 rcu_read_unlock();
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002604 return skb->len;
2605}
2606
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002607static int nf_tables_dump_sets_done(struct netlink_callback *cb)
Patrick McHardy20a69342013-10-11 12:06:22 +02002608{
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002609 kfree(cb->data);
2610 return 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002611}
2612
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +01002613static int nf_tables_getset(struct net *net, struct sock *nlsk,
2614 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy20a69342013-10-11 12:06:22 +02002615 const struct nlattr * const nla[])
2616{
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002617 u8 genmask = nft_genmask_cur(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02002618 const struct nft_set *set;
2619 struct nft_ctx ctx;
2620 struct sk_buff *skb2;
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002621 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Patrick McHardy20a69342013-10-11 12:06:22 +02002622 int err;
2623
stephen hemminger01cfa0a2014-10-29 22:57:19 -07002624 /* Verify existence before starting dump */
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002625 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002626 if (err < 0)
2627 return err;
2628
2629 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2630 struct netlink_dump_control c = {
2631 .dump = nf_tables_dump_sets,
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002632 .done = nf_tables_dump_sets_done,
Patrick McHardy20a69342013-10-11 12:06:22 +02002633 };
Pablo Neira Ayuso5b96af72014-07-16 17:35:18 +02002634 struct nft_ctx *ctx_dump;
2635
2636 ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2637 if (ctx_dump == NULL)
2638 return -ENOMEM;
2639
2640 *ctx_dump = ctx;
2641 c.data = ctx_dump;
2642
Patrick McHardy20a69342013-10-11 12:06:22 +02002643 return netlink_dump_start(nlsk, skb, nlh, &c);
2644 }
2645
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002646 /* Only accept unspec with dump */
2647 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2648 return -EAFNOSUPPORT;
2649
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002650 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002651 if (IS_ERR(set))
2652 return PTR_ERR(set);
2653
2654 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2655 if (skb2 == NULL)
2656 return -ENOMEM;
2657
2658 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2659 if (err < 0)
2660 goto err;
2661
2662 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2663
2664err:
2665 kfree_skb(skb2);
2666 return err;
2667}
2668
Patrick McHardyc50b9602014-03-28 10:19:47 +00002669static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2670 struct nft_set_desc *desc,
2671 const struct nlattr *nla)
2672{
2673 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2674 int err;
2675
2676 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2677 if (err < 0)
2678 return err;
2679
2680 if (da[NFTA_SET_DESC_SIZE] != NULL)
2681 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2682
2683 return 0;
2684}
2685
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002686static int nf_tables_newset(struct net *net, struct sock *nlsk,
2687 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy20a69342013-10-11 12:06:22 +02002688 const struct nlattr * const nla[])
2689{
2690 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002691 u8 genmask = nft_genmask_next(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02002692 const struct nft_set_ops *ops;
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02002693 struct nft_af_info *afi;
Patrick McHardy20a69342013-10-11 12:06:22 +02002694 struct nft_table *table;
2695 struct nft_set *set;
2696 struct nft_ctx ctx;
Pablo Neira Ayusocb39ad82016-05-04 17:49:53 +02002697 char name[NFT_SET_MAXNAMELEN];
Patrick McHardy20a69342013-10-11 12:06:22 +02002698 unsigned int size;
2699 bool create;
Patrick McHardy761da292015-03-26 12:39:36 +00002700 u64 timeout;
2701 u32 ktype, dtype, flags, policy, gc_int;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002702 struct nft_set_desc desc;
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002703 unsigned char *udata;
2704 u16 udlen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002705 int err;
2706
2707 if (nla[NFTA_SET_TABLE] == NULL ||
2708 nla[NFTA_SET_NAME] == NULL ||
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002709 nla[NFTA_SET_KEY_LEN] == NULL ||
2710 nla[NFTA_SET_ID] == NULL)
Patrick McHardy20a69342013-10-11 12:06:22 +02002711 return -EINVAL;
2712
Patrick McHardyc50b9602014-03-28 10:19:47 +00002713 memset(&desc, 0, sizeof(desc));
2714
Patrick McHardy20a69342013-10-11 12:06:22 +02002715 ktype = NFT_DATA_VALUE;
2716 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2717 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2718 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2719 return -EINVAL;
2720 }
2721
Patrick McHardyc50b9602014-03-28 10:19:47 +00002722 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
Patrick McHardy7d740262015-04-11 02:27:39 +01002723 if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
Patrick McHardy20a69342013-10-11 12:06:22 +02002724 return -EINVAL;
2725
2726 flags = 0;
2727 if (nla[NFTA_SET_FLAGS] != NULL) {
2728 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2729 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
Patrick McHardy7c6c6e92015-04-11 10:46:41 +01002730 NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
2731 NFT_SET_MAP | NFT_SET_EVAL))
Patrick McHardy20a69342013-10-11 12:06:22 +02002732 return -EINVAL;
Patrick McHardy7c6c6e92015-04-11 10:46:41 +01002733 /* Only one of both operations is supported */
2734 if ((flags & (NFT_SET_MAP | NFT_SET_EVAL)) ==
2735 (NFT_SET_MAP | NFT_SET_EVAL))
2736 return -EOPNOTSUPP;
Patrick McHardy20a69342013-10-11 12:06:22 +02002737 }
2738
2739 dtype = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02002740 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2741 if (!(flags & NFT_SET_MAP))
2742 return -EINVAL;
2743
2744 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2745 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2746 dtype != NFT_DATA_VERDICT)
2747 return -EINVAL;
2748
2749 if (dtype != NFT_DATA_VERDICT) {
2750 if (nla[NFTA_SET_DATA_LEN] == NULL)
2751 return -EINVAL;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002752 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
Patrick McHardy7d740262015-04-11 02:27:39 +01002753 if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
Patrick McHardy20a69342013-10-11 12:06:22 +02002754 return -EINVAL;
2755 } else
Patrick McHardy7d740262015-04-11 02:27:39 +01002756 desc.dlen = sizeof(struct nft_verdict);
Patrick McHardy20a69342013-10-11 12:06:22 +02002757 } else if (flags & NFT_SET_MAP)
2758 return -EINVAL;
2759
Patrick McHardy761da292015-03-26 12:39:36 +00002760 timeout = 0;
2761 if (nla[NFTA_SET_TIMEOUT] != NULL) {
2762 if (!(flags & NFT_SET_TIMEOUT))
2763 return -EINVAL;
2764 timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_TIMEOUT]));
2765 }
2766 gc_int = 0;
2767 if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
2768 if (!(flags & NFT_SET_TIMEOUT))
2769 return -EINVAL;
2770 gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
2771 }
2772
Patrick McHardyc50b9602014-03-28 10:19:47 +00002773 policy = NFT_SET_POL_PERFORMANCE;
2774 if (nla[NFTA_SET_POLICY] != NULL)
2775 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2776
2777 if (nla[NFTA_SET_DESC] != NULL) {
2778 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2779 if (err < 0)
2780 return err;
2781 }
2782
Patrick McHardy20a69342013-10-11 12:06:22 +02002783 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2784
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02002785 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
Patrick McHardy20a69342013-10-11 12:06:22 +02002786 if (IS_ERR(afi))
2787 return PTR_ERR(afi);
2788
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002789 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE], genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002790 if (IS_ERR(table))
2791 return PTR_ERR(table);
2792
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002793 nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002794
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002795 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME], genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002796 if (IS_ERR(set)) {
2797 if (PTR_ERR(set) != -ENOENT)
2798 return PTR_ERR(set);
2799 set = NULL;
2800 }
2801
2802 if (set != NULL) {
2803 if (nlh->nlmsg_flags & NLM_F_EXCL)
2804 return -EEXIST;
2805 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2806 return -EOPNOTSUPP;
2807 return 0;
2808 }
2809
2810 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2811 return -ENOENT;
2812
Patrick McHardyc50b9602014-03-28 10:19:47 +00002813 ops = nft_select_set_ops(nla, &desc, policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02002814 if (IS_ERR(ops))
2815 return PTR_ERR(ops);
2816
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002817 udlen = 0;
2818 if (nla[NFTA_SET_USERDATA])
2819 udlen = nla_len(nla[NFTA_SET_USERDATA]);
2820
Patrick McHardy20a69342013-10-11 12:06:22 +02002821 size = 0;
2822 if (ops->privsize != NULL)
2823 size = ops->privsize(nla);
2824
2825 err = -ENOMEM;
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002826 set = kzalloc(sizeof(*set) + size + udlen, GFP_KERNEL);
Patrick McHardy20a69342013-10-11 12:06:22 +02002827 if (set == NULL)
2828 goto err1;
2829
2830 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2831 err = nf_tables_set_alloc_name(&ctx, set, name);
2832 if (err < 0)
2833 goto err2;
2834
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002835 udata = NULL;
2836 if (udlen) {
2837 udata = set->data + size;
2838 nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen);
2839 }
2840
Patrick McHardy20a69342013-10-11 12:06:22 +02002841 INIT_LIST_HEAD(&set->bindings);
Patrick McHardycc02e452015-03-25 14:08:50 +00002842 write_pnet(&set->pnet, net);
Patrick McHardy20a69342013-10-11 12:06:22 +02002843 set->ops = ops;
2844 set->ktype = ktype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002845 set->klen = desc.klen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002846 set->dtype = dtype;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002847 set->dlen = desc.dlen;
Patrick McHardy20a69342013-10-11 12:06:22 +02002848 set->flags = flags;
Patrick McHardyc50b9602014-03-28 10:19:47 +00002849 set->size = desc.size;
Arturo Borrero9363dc42014-09-23 13:30:41 +02002850 set->policy = policy;
Carlos Falgueras Garcíae6d8eca2016-01-05 14:03:32 +01002851 set->udlen = udlen;
2852 set->udata = udata;
Patrick McHardy761da292015-03-26 12:39:36 +00002853 set->timeout = timeout;
2854 set->gc_int = gc_int;
Patrick McHardy20a69342013-10-11 12:06:22 +02002855
Patrick McHardyc50b9602014-03-28 10:19:47 +00002856 err = ops->init(set, &desc, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02002857 if (err < 0)
2858 goto err2;
2859
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002860 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002861 if (err < 0)
2862 goto err2;
2863
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002864 list_add_tail_rcu(&set->list, &table->sets);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02002865 table->use++;
Patrick McHardy20a69342013-10-11 12:06:22 +02002866 return 0;
2867
2868err2:
2869 kfree(set);
2870err1:
2871 module_put(ops->owner);
2872 return err;
2873}
2874
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002875static void nft_set_destroy(struct nft_set *set)
2876{
2877 set->ops->destroy(set);
2878 module_put(set->ops->owner);
2879 kfree(set);
2880}
2881
Patrick McHardy20a69342013-10-11 12:06:22 +02002882static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2883{
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002884 list_del_rcu(&set->list);
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02002885 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002886 nft_set_destroy(set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002887}
2888
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01002889static int nf_tables_delset(struct net *net, struct sock *nlsk,
2890 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy20a69342013-10-11 12:06:22 +02002891 const struct nlattr * const nla[])
2892{
Pablo Neira Ayusoc9c8e482013-12-26 16:49:03 +01002893 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002894 u8 genmask = nft_genmask_next(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02002895 struct nft_set *set;
2896 struct nft_ctx ctx;
2897 int err;
2898
Patrick McHardyec2c9932014-02-05 15:03:35 +00002899 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2900 return -EAFNOSUPPORT;
Patrick McHardy20a69342013-10-11 12:06:22 +02002901 if (nla[NFTA_SET_TABLE] == NULL)
2902 return -EINVAL;
2903
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02002904 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002905 if (err < 0)
2906 return err;
2907
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002908 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02002909 if (IS_ERR(set))
2910 return PTR_ERR(set);
2911 if (!list_empty(&set->bindings))
2912 return -EBUSY;
2913
Arturo Borreroee01d542014-09-02 16:42:25 +02002914 return nft_delset(&ctx, set);
Patrick McHardy20a69342013-10-11 12:06:22 +02002915}
2916
2917static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2918 const struct nft_set *set,
2919 const struct nft_set_iter *iter,
2920 const struct nft_set_elem *elem)
2921{
Patrick McHardyfe2811e2015-03-25 13:07:50 +00002922 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02002923 enum nft_registers dreg;
2924
2925 dreg = nft_type_to_reg(set->dtype);
Patrick McHardy1ec10212015-04-11 02:27:27 +01002926 return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
2927 set->dtype == NFT_DATA_VERDICT ?
2928 NFT_DATA_VERDICT : NFT_DATA_VALUE,
2929 set->dlen);
Patrick McHardy20a69342013-10-11 12:06:22 +02002930}
2931
2932int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2933 struct nft_set_binding *binding)
2934{
2935 struct nft_set_binding *i;
2936 struct nft_set_iter iter;
2937
2938 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2939 return -EBUSY;
2940
Patrick McHardy11113e12015-04-05 14:41:07 +02002941 if (binding->flags & NFT_SET_MAP) {
Patrick McHardy20a69342013-10-11 12:06:22 +02002942 /* If the set is already bound to the same chain all
2943 * jumps are already validated for that chain.
2944 */
2945 list_for_each_entry(i, &set->bindings, list) {
Patrick McHardy11113e12015-04-05 14:41:07 +02002946 if (binding->flags & NFT_SET_MAP &&
2947 i->chain == binding->chain)
Patrick McHardy20a69342013-10-11 12:06:22 +02002948 goto bind;
2949 }
2950
2951 iter.skip = 0;
2952 iter.count = 0;
2953 iter.err = 0;
2954 iter.fn = nf_tables_bind_check_setelem;
2955
2956 set->ops->walk(ctx, set, &iter);
2957 if (iter.err < 0) {
2958 /* Destroy anonymous sets if binding fails */
2959 if (set->flags & NFT_SET_ANONYMOUS)
2960 nf_tables_set_destroy(ctx, set);
2961
2962 return iter.err;
2963 }
2964 }
2965bind:
2966 binding->chain = ctx->chain;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002967 list_add_tail_rcu(&binding->list, &set->bindings);
Patrick McHardy20a69342013-10-11 12:06:22 +02002968 return 0;
2969}
2970
2971void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2972 struct nft_set_binding *binding)
2973{
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02002974 list_del_rcu(&binding->list);
Patrick McHardy20a69342013-10-11 12:06:22 +02002975
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02002976 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02002977 nft_is_active(ctx->net, set))
Patrick McHardy20a69342013-10-11 12:06:22 +02002978 nf_tables_set_destroy(ctx, set);
2979}
2980
Patrick McHardy3ac4c072015-03-25 13:07:49 +00002981const struct nft_set_ext_type nft_set_ext_types[] = {
2982 [NFT_SET_EXT_KEY] = {
Patrick McHardy7d740262015-04-11 02:27:39 +01002983 .align = __alignof__(u32),
Patrick McHardy3ac4c072015-03-25 13:07:49 +00002984 },
2985 [NFT_SET_EXT_DATA] = {
Patrick McHardy7d740262015-04-11 02:27:39 +01002986 .align = __alignof__(u32),
Patrick McHardy3ac4c072015-03-25 13:07:49 +00002987 },
Patrick McHardyf25ad2e2015-04-11 10:46:39 +01002988 [NFT_SET_EXT_EXPR] = {
2989 .align = __alignof__(struct nft_expr),
2990 },
Patrick McHardy3ac4c072015-03-25 13:07:49 +00002991 [NFT_SET_EXT_FLAGS] = {
2992 .len = sizeof(u8),
2993 .align = __alignof__(u8),
2994 },
Patrick McHardyc3e1b002015-03-26 12:39:37 +00002995 [NFT_SET_EXT_TIMEOUT] = {
2996 .len = sizeof(u64),
2997 .align = __alignof__(u64),
2998 },
2999 [NFT_SET_EXT_EXPIRATION] = {
3000 .len = sizeof(unsigned long),
3001 .align = __alignof__(unsigned long),
3002 },
Patrick McHardy68e942e2015-04-05 14:43:38 +02003003 [NFT_SET_EXT_USERDATA] = {
3004 .len = sizeof(struct nft_userdata),
3005 .align = __alignof__(struct nft_userdata),
3006 },
Patrick McHardy3ac4c072015-03-25 13:07:49 +00003007};
3008EXPORT_SYMBOL_GPL(nft_set_ext_types);
3009
Patrick McHardy20a69342013-10-11 12:06:22 +02003010/*
3011 * Set elements
3012 */
3013
3014static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
3015 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
3016 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
3017 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003018 [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
Patrick McHardy68e942e2015-04-05 14:43:38 +02003019 [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
3020 .len = NFT_USERDATA_MAXLEN },
Patrick McHardy20a69342013-10-11 12:06:22 +02003021};
3022
3023static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
3024 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
3025 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
3026 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003027 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
Patrick McHardy20a69342013-10-11 12:06:22 +02003028};
3029
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01003030static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net,
Patrick McHardy20a69342013-10-11 12:06:22 +02003031 const struct sk_buff *skb,
3032 const struct nlmsghdr *nlh,
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003033 const struct nlattr * const nla[],
3034 u8 genmask)
Patrick McHardy20a69342013-10-11 12:06:22 +02003035{
3036 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02003037 struct nft_af_info *afi;
3038 struct nft_table *table;
Patrick McHardy20a69342013-10-11 12:06:22 +02003039
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02003040 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
Patrick McHardy20a69342013-10-11 12:06:22 +02003041 if (IS_ERR(afi))
3042 return PTR_ERR(afi);
3043
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003044 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE],
3045 genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003046 if (IS_ERR(table))
3047 return PTR_ERR(table);
3048
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01003049 nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla);
Patrick McHardy20a69342013-10-11 12:06:22 +02003050 return 0;
3051}
3052
3053static int nf_tables_fill_setelem(struct sk_buff *skb,
3054 const struct nft_set *set,
3055 const struct nft_set_elem *elem)
3056{
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003057 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02003058 unsigned char *b = skb_tail_pointer(skb);
3059 struct nlattr *nest;
3060
3061 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
3062 if (nest == NULL)
3063 goto nla_put_failure;
3064
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003065 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
3066 NFT_DATA_VALUE, set->klen) < 0)
Patrick McHardy20a69342013-10-11 12:06:22 +02003067 goto nla_put_failure;
3068
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003069 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
3070 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
Patrick McHardy20a69342013-10-11 12:06:22 +02003071 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
3072 set->dlen) < 0)
3073 goto nla_put_failure;
3074
Patrick McHardyf25ad2e2015-04-11 10:46:39 +01003075 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) &&
3076 nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0)
3077 goto nla_put_failure;
3078
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003079 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3080 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
3081 htonl(*nft_set_ext_flags(ext))))
3082 goto nla_put_failure;
Patrick McHardy20a69342013-10-11 12:06:22 +02003083
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003084 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
3085 nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +02003086 cpu_to_be64(*nft_set_ext_timeout(ext)),
3087 NFTA_SET_ELEM_PAD))
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003088 goto nla_put_failure;
3089
3090 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
3091 unsigned long expires, now = jiffies;
3092
3093 expires = *nft_set_ext_expiration(ext);
3094 if (time_before(now, expires))
3095 expires -= now;
3096 else
3097 expires = 0;
3098
3099 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +02003100 cpu_to_be64(jiffies_to_msecs(expires)),
3101 NFTA_SET_ELEM_PAD))
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003102 goto nla_put_failure;
3103 }
3104
Patrick McHardy68e942e2015-04-05 14:43:38 +02003105 if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
3106 struct nft_userdata *udata;
3107
3108 udata = nft_set_ext_userdata(ext);
3109 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
3110 udata->len + 1, udata->data))
3111 goto nla_put_failure;
3112 }
3113
Patrick McHardy20a69342013-10-11 12:06:22 +02003114 nla_nest_end(skb, nest);
3115 return 0;
3116
3117nla_put_failure:
3118 nlmsg_trim(skb, b);
3119 return -EMSGSIZE;
3120}
3121
3122struct nft_set_dump_args {
3123 const struct netlink_callback *cb;
3124 struct nft_set_iter iter;
3125 struct sk_buff *skb;
3126};
3127
3128static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
3129 const struct nft_set *set,
3130 const struct nft_set_iter *iter,
3131 const struct nft_set_elem *elem)
3132{
3133 struct nft_set_dump_args *args;
3134
3135 args = container_of(iter, struct nft_set_dump_args, iter);
3136 return nf_tables_fill_setelem(args->skb, set, elem);
3137}
3138
3139static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
3140{
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01003141 struct net *net = sock_net(skb->sk);
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003142 u8 genmask = nft_genmask_cur(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02003143 const struct nft_set *set;
3144 struct nft_set_dump_args args;
3145 struct nft_ctx ctx;
3146 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
3147 struct nfgenmsg *nfmsg;
3148 struct nlmsghdr *nlh;
3149 struct nlattr *nest;
3150 u32 portid, seq;
3151 int event, err;
3152
Michal Nazarewicz720e0df2014-01-01 06:27:19 +01003153 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
3154 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
Patrick McHardy20a69342013-10-11 12:06:22 +02003155 if (err < 0)
3156 return err;
3157
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01003158 err = nft_ctx_init_from_elemattr(&ctx, net, cb->skb, cb->nlh,
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003159 (void *)nla, genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003160 if (err < 0)
3161 return err;
3162
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02003163 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
3164 genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003165 if (IS_ERR(set))
3166 return PTR_ERR(set);
3167
3168 event = NFT_MSG_NEWSETELEM;
3169 event |= NFNL_SUBSYS_NFTABLES << 8;
3170 portid = NETLINK_CB(cb->skb).portid;
3171 seq = cb->nlh->nlmsg_seq;
3172
3173 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3174 NLM_F_MULTI);
3175 if (nlh == NULL)
3176 goto nla_put_failure;
3177
3178 nfmsg = nlmsg_data(nlh);
Pablo Neira Ayuso6403d962014-06-11 19:05:28 +02003179 nfmsg->nfgen_family = ctx.afi->family;
Patrick McHardy20a69342013-10-11 12:06:22 +02003180 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003181 nfmsg->res_id = htons(ctx.net->nft.base_seq & 0xffff);
Patrick McHardy20a69342013-10-11 12:06:22 +02003182
3183 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
3184 goto nla_put_failure;
3185 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
3186 goto nla_put_failure;
3187
3188 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3189 if (nest == NULL)
3190 goto nla_put_failure;
3191
3192 args.cb = cb;
3193 args.skb = skb;
3194 args.iter.skip = cb->args[0];
3195 args.iter.count = 0;
3196 args.iter.err = 0;
3197 args.iter.fn = nf_tables_dump_setelem;
3198 set->ops->walk(&ctx, set, &args.iter);
3199
3200 nla_nest_end(skb, nest);
3201 nlmsg_end(skb, nlh);
3202
3203 if (args.iter.err && args.iter.err != -EMSGSIZE)
3204 return args.iter.err;
3205 if (args.iter.count == cb->args[0])
3206 return 0;
3207
3208 cb->args[0] = args.iter.count;
3209 return skb->len;
3210
3211nla_put_failure:
3212 return -ENOSPC;
3213}
3214
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +01003215static int nf_tables_getsetelem(struct net *net, struct sock *nlsk,
3216 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy20a69342013-10-11 12:06:22 +02003217 const struct nlattr * const nla[])
3218{
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003219 u8 genmask = nft_genmask_cur(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02003220 const struct nft_set *set;
3221 struct nft_ctx ctx;
3222 int err;
3223
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003224 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003225 if (err < 0)
3226 return err;
3227
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02003228 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
3229 genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003230 if (IS_ERR(set))
3231 return PTR_ERR(set);
3232
3233 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3234 struct netlink_dump_control c = {
3235 .dump = nf_tables_dump_set,
3236 };
3237 return netlink_dump_start(nlsk, skb, nlh, &c);
3238 }
3239 return -EOPNOTSUPP;
3240}
3241
Arturo Borrerod60ce622014-04-01 14:06:07 +02003242static int nf_tables_fill_setelem_info(struct sk_buff *skb,
3243 const struct nft_ctx *ctx, u32 seq,
3244 u32 portid, int event, u16 flags,
3245 const struct nft_set *set,
3246 const struct nft_set_elem *elem)
3247{
3248 struct nfgenmsg *nfmsg;
3249 struct nlmsghdr *nlh;
3250 struct nlattr *nest;
3251 int err;
3252
3253 event |= NFNL_SUBSYS_NFTABLES << 8;
3254 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3255 flags);
3256 if (nlh == NULL)
3257 goto nla_put_failure;
3258
3259 nfmsg = nlmsg_data(nlh);
3260 nfmsg->nfgen_family = ctx->afi->family;
3261 nfmsg->version = NFNETLINK_V0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003262 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
Arturo Borrerod60ce622014-04-01 14:06:07 +02003263
3264 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3265 goto nla_put_failure;
3266 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3267 goto nla_put_failure;
3268
3269 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3270 if (nest == NULL)
3271 goto nla_put_failure;
3272
3273 err = nf_tables_fill_setelem(skb, set, elem);
3274 if (err < 0)
3275 goto nla_put_failure;
3276
3277 nla_nest_end(skb, nest);
3278
Johannes Berg053c0952015-01-16 22:09:00 +01003279 nlmsg_end(skb, nlh);
3280 return 0;
Arturo Borrerod60ce622014-04-01 14:06:07 +02003281
3282nla_put_failure:
3283 nlmsg_trim(skb, nlh);
3284 return -1;
3285}
3286
3287static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
3288 const struct nft_set *set,
3289 const struct nft_set_elem *elem,
3290 int event, u16 flags)
3291{
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003292 struct net *net = ctx->net;
3293 u32 portid = ctx->portid;
Arturo Borrerod60ce622014-04-01 14:06:07 +02003294 struct sk_buff *skb;
3295 int err;
3296
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003297 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
Arturo Borrerod60ce622014-04-01 14:06:07 +02003298 return 0;
3299
3300 err = -ENOBUFS;
3301 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3302 if (skb == NULL)
3303 goto err;
3304
3305 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3306 set, elem);
3307 if (err < 0) {
3308 kfree_skb(skb);
3309 goto err;
3310 }
3311
Pablo Neira Ayuso128ad332014-05-09 17:14:24 +02003312 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
Arturo Borrerod60ce622014-04-01 14:06:07 +02003313 GFP_KERNEL);
3314err:
3315 if (err < 0)
3316 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3317 return err;
3318}
3319
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003320static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3321 int msg_type,
3322 struct nft_set *set)
3323{
3324 struct nft_trans *trans;
3325
3326 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3327 if (trans == NULL)
3328 return NULL;
3329
3330 nft_trans_elem_set(trans) = set;
3331 return trans;
3332}
3333
Patrick McHardy22fe54d2015-04-05 14:41:08 +02003334void *nft_set_elem_init(const struct nft_set *set,
3335 const struct nft_set_ext_tmpl *tmpl,
Patrick McHardy49499c32015-04-11 02:27:37 +01003336 const u32 *key, const u32 *data,
Patrick McHardy22fe54d2015-04-05 14:41:08 +02003337 u64 timeout, gfp_t gfp)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003338{
3339 struct nft_set_ext *ext;
3340 void *elem;
3341
3342 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
3343 if (elem == NULL)
3344 return NULL;
3345
3346 ext = nft_set_elem_ext(set, elem);
3347 nft_set_ext_init(ext, tmpl);
3348
3349 memcpy(nft_set_ext_key(ext), key, set->klen);
3350 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3351 memcpy(nft_set_ext_data(ext), data, set->dlen);
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003352 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION))
3353 *nft_set_ext_expiration(ext) =
3354 jiffies + msecs_to_jiffies(timeout);
3355 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
3356 *nft_set_ext_timeout(ext) = timeout;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003357
3358 return elem;
3359}
3360
Patrick McHardy61edafb2015-03-25 14:08:47 +00003361void nft_set_elem_destroy(const struct nft_set *set, void *elem)
3362{
3363 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
3364
3365 nft_data_uninit(nft_set_ext_key(ext), NFT_DATA_VALUE);
3366 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3367 nft_data_uninit(nft_set_ext_data(ext), set->dtype);
Patrick McHardyf25ad2e2015-04-11 10:46:39 +01003368 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
3369 nf_tables_expr_destroy(NULL, nft_set_ext_expr(ext));
Patrick McHardy61edafb2015-03-25 14:08:47 +00003370
3371 kfree(elem);
3372}
3373EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
3374
Pablo Neira Ayuso0e9091d2016-04-12 23:50:34 +02003375static int nft_setelem_parse_flags(const struct nft_set *set,
3376 const struct nlattr *attr, u32 *flags)
3377{
3378 if (attr == NULL)
3379 return 0;
3380
3381 *flags = ntohl(nla_get_be32(attr));
3382 if (*flags & ~NFT_SET_ELEM_INTERVAL_END)
3383 return -EINVAL;
3384 if (!(set->flags & NFT_SET_INTERVAL) &&
3385 *flags & NFT_SET_ELEM_INTERVAL_END)
3386 return -EINVAL;
3387
3388 return 0;
3389}
3390
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003391static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +02003392 const struct nlattr *attr)
3393{
3394 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3395 struct nft_data_desc d1, d2;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003396 struct nft_set_ext_tmpl tmpl;
3397 struct nft_set_ext *ext;
Patrick McHardy20a69342013-10-11 12:06:22 +02003398 struct nft_set_elem elem;
3399 struct nft_set_binding *binding;
Patrick McHardy68e942e2015-04-05 14:43:38 +02003400 struct nft_userdata *udata;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003401 struct nft_data data;
Patrick McHardy20a69342013-10-11 12:06:22 +02003402 enum nft_registers dreg;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003403 struct nft_trans *trans;
Pablo Neira Ayuso0e9091d2016-04-12 23:50:34 +02003404 u32 flags = 0;
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003405 u64 timeout;
Patrick McHardy68e942e2015-04-05 14:43:38 +02003406 u8 ulen;
Patrick McHardy20a69342013-10-11 12:06:22 +02003407 int err;
3408
3409 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3410 nft_set_elem_policy);
3411 if (err < 0)
3412 return err;
3413
3414 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3415 return -EINVAL;
3416
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003417 nft_set_ext_prepare(&tmpl);
3418
Pablo Neira Ayuso0e9091d2016-04-12 23:50:34 +02003419 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
3420 if (err < 0)
3421 return err;
3422 if (flags != 0)
3423 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
Patrick McHardy20a69342013-10-11 12:06:22 +02003424
3425 if (set->flags & NFT_SET_MAP) {
3426 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003427 !(flags & NFT_SET_ELEM_INTERVAL_END))
Patrick McHardy20a69342013-10-11 12:06:22 +02003428 return -EINVAL;
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01003429 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003430 flags & NFT_SET_ELEM_INTERVAL_END)
Pablo Neira Ayusobd7fc642014-02-07 12:53:07 +01003431 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02003432 } else {
3433 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3434 return -EINVAL;
3435 }
3436
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003437 timeout = 0;
3438 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
3439 if (!(set->flags & NFT_SET_TIMEOUT))
3440 return -EINVAL;
3441 timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_ELEM_TIMEOUT]));
3442 } else if (set->flags & NFT_SET_TIMEOUT) {
3443 timeout = set->timeout;
3444 }
3445
Patrick McHardy7d740262015-04-11 02:27:39 +01003446 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1,
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01003447 nla[NFTA_SET_ELEM_KEY]);
Patrick McHardy20a69342013-10-11 12:06:22 +02003448 if (err < 0)
3449 goto err1;
3450 err = -EINVAL;
3451 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3452 goto err2;
3453
Patrick McHardy7d740262015-04-11 02:27:39 +01003454 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, d1.len);
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003455 if (timeout > 0) {
3456 nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
3457 if (timeout != set->timeout)
3458 nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
3459 }
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003460
Patrick McHardy20a69342013-10-11 12:06:22 +02003461 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01003462 err = nft_data_init(ctx, &data, sizeof(data), &d2,
3463 nla[NFTA_SET_ELEM_DATA]);
Patrick McHardy20a69342013-10-11 12:06:22 +02003464 if (err < 0)
3465 goto err2;
3466
3467 err = -EINVAL;
3468 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3469 goto err3;
3470
3471 dreg = nft_type_to_reg(set->dtype);
3472 list_for_each_entry(binding, &set->bindings, list) {
3473 struct nft_ctx bind_ctx = {
3474 .afi = ctx->afi,
3475 .table = ctx->table,
Pablo Neira Ayuso7c95f6d2014-04-04 01:22:45 +02003476 .chain = (struct nft_chain *)binding->chain,
Patrick McHardy20a69342013-10-11 12:06:22 +02003477 };
3478
Patrick McHardy11113e12015-04-05 14:41:07 +02003479 if (!(binding->flags & NFT_SET_MAP))
3480 continue;
3481
Patrick McHardy1ec10212015-04-11 02:27:27 +01003482 err = nft_validate_register_store(&bind_ctx, dreg,
3483 &data,
3484 d2.type, d2.len);
Patrick McHardy20a69342013-10-11 12:06:22 +02003485 if (err < 0)
3486 goto err3;
3487 }
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003488
Patrick McHardy7d740262015-04-11 02:27:39 +01003489 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, d2.len);
Patrick McHardy20a69342013-10-11 12:06:22 +02003490 }
3491
Patrick McHardy68e942e2015-04-05 14:43:38 +02003492 /* The full maximum length of userdata can exceed the maximum
3493 * offset value (U8_MAX) for following extensions, therefor it
3494 * must be the last extension added.
3495 */
3496 ulen = 0;
3497 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
3498 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
3499 if (ulen > 0)
3500 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
3501 ulen);
3502 }
3503
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003504 err = -ENOMEM;
Patrick McHardy7d740262015-04-11 02:27:39 +01003505 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, data.data,
Patrick McHardyc3e1b002015-03-26 12:39:37 +00003506 timeout, GFP_KERNEL);
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003507 if (elem.priv == NULL)
3508 goto err3;
3509
3510 ext = nft_set_elem_ext(set, elem.priv);
3511 if (flags)
3512 *nft_set_ext_flags(ext) = flags;
Patrick McHardy68e942e2015-04-05 14:43:38 +02003513 if (ulen > 0) {
3514 udata = nft_set_ext_userdata(ext);
3515 udata->len = ulen - 1;
3516 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
3517 }
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003518
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003519 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3520 if (trans == NULL)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003521 goto err4;
Patrick McHardy20a69342013-10-11 12:06:22 +02003522
Patrick McHardy69086652015-03-26 12:39:39 +00003523 ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
Patrick McHardy20a69342013-10-11 12:06:22 +02003524 err = set->ops->insert(set, &elem);
3525 if (err < 0)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003526 goto err5;
Patrick McHardy20a69342013-10-11 12:06:22 +02003527
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003528 nft_trans_elem(trans) = elem;
Pablo Neira Ayuso46bbafc2014-05-22 12:36:03 +02003529 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Patrick McHardy20a69342013-10-11 12:06:22 +02003530 return 0;
3531
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003532err5:
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003533 kfree(trans);
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003534err4:
3535 kfree(elem.priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02003536err3:
3537 if (nla[NFTA_SET_ELEM_DATA] != NULL)
Patrick McHardyfe2811e2015-03-25 13:07:50 +00003538 nft_data_uninit(&data, d2.type);
Patrick McHardy20a69342013-10-11 12:06:22 +02003539err2:
Patrick McHardy7d740262015-04-11 02:27:39 +01003540 nft_data_uninit(&elem.key.val, d1.type);
Patrick McHardy20a69342013-10-11 12:06:22 +02003541err1:
3542 return err;
3543}
3544
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01003545static int nf_tables_newsetelem(struct net *net, struct sock *nlsk,
3546 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy20a69342013-10-11 12:06:22 +02003547 const struct nlattr * const nla[])
3548{
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003549 u8 genmask = nft_genmask_next(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02003550 const struct nlattr *attr;
3551 struct nft_set *set;
3552 struct nft_ctx ctx;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003553 int rem, err = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003554
Pablo Neira Ayuso7d5570c2014-07-25 13:15:36 +02003555 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3556 return -EINVAL;
3557
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003558 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003559 if (err < 0)
3560 return err;
3561
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02003562 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
3563 genmask);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003564 if (IS_ERR(set)) {
3565 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3566 set = nf_tables_set_lookup_byid(net,
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02003567 nla[NFTA_SET_ELEM_LIST_SET_ID],
3568 genmask);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003569 }
3570 if (IS_ERR(set))
3571 return PTR_ERR(set);
3572 }
3573
Patrick McHardy20a69342013-10-11 12:06:22 +02003574 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3575 return -EBUSY;
3576
3577 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
Patrick McHardy3dd06732015-04-05 14:41:06 +02003578 if (set->size &&
3579 !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact))
3580 return -ENFILE;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003581
Patrick McHardy3dd06732015-04-05 14:41:06 +02003582 err = nft_add_set_elem(&ctx, set, attr);
3583 if (err < 0) {
3584 atomic_dec(&set->nelems);
3585 break;
3586 }
Patrick McHardy20a69342013-10-11 12:06:22 +02003587 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003588 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +02003589}
3590
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003591static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
Patrick McHardy20a69342013-10-11 12:06:22 +02003592 const struct nlattr *attr)
3593{
3594 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003595 struct nft_set_ext_tmpl tmpl;
Patrick McHardy20a69342013-10-11 12:06:22 +02003596 struct nft_data_desc desc;
3597 struct nft_set_elem elem;
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003598 struct nft_set_ext *ext;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003599 struct nft_trans *trans;
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003600 u32 flags = 0;
3601 void *priv;
Patrick McHardy20a69342013-10-11 12:06:22 +02003602 int err;
3603
3604 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3605 nft_set_elem_policy);
3606 if (err < 0)
3607 goto err1;
3608
3609 err = -EINVAL;
3610 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3611 goto err1;
3612
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003613 nft_set_ext_prepare(&tmpl);
3614
3615 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
3616 if (err < 0)
3617 return err;
3618 if (flags != 0)
3619 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
3620
Patrick McHardy7d740262015-04-11 02:27:39 +01003621 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc,
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01003622 nla[NFTA_SET_ELEM_KEY]);
Patrick McHardy20a69342013-10-11 12:06:22 +02003623 if (err < 0)
3624 goto err1;
3625
3626 err = -EINVAL;
3627 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3628 goto err2;
3629
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003630 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, desc.len);
3631
3632 err = -ENOMEM;
3633 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, NULL, 0,
3634 GFP_KERNEL);
3635 if (elem.priv == NULL)
3636 goto err2;
3637
3638 ext = nft_set_elem_ext(set, elem.priv);
3639 if (flags)
3640 *nft_set_ext_flags(ext) = flags;
3641
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003642 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
Julia Lawall609ccf02014-08-07 14:49:08 +02003643 if (trans == NULL) {
3644 err = -ENOMEM;
Patrick McHardycc02e452015-03-25 14:08:50 +00003645 goto err3;
3646 }
3647
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003648 priv = set->ops->deactivate(set, &elem);
3649 if (priv == NULL) {
3650 err = -ENOENT;
3651 goto err4;
3652 }
3653 kfree(elem.priv);
3654 elem.priv = priv;
3655
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003656 nft_trans_elem(trans) = elem;
Pablo Neira Ayuso46bbafc2014-05-22 12:36:03 +02003657 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
Thomas Graf0dc13622014-08-01 17:25:38 +02003658 return 0;
Patrick McHardycc02e452015-03-25 14:08:50 +00003659
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003660err4:
Patrick McHardycc02e452015-03-25 14:08:50 +00003661 kfree(trans);
Pablo Neira Ayuso3971ca12016-04-12 23:50:35 +02003662err3:
3663 kfree(elem.priv);
Patrick McHardy20a69342013-10-11 12:06:22 +02003664err2:
Patrick McHardy7d740262015-04-11 02:27:39 +01003665 nft_data_uninit(&elem.key.val, desc.type);
Patrick McHardy20a69342013-10-11 12:06:22 +02003666err1:
3667 return err;
3668}
3669
Pablo Neira Ayuso633c9a82015-12-09 12:08:26 +01003670static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
3671 struct sk_buff *skb, const struct nlmsghdr *nlh,
Patrick McHardy20a69342013-10-11 12:06:22 +02003672 const struct nlattr * const nla[])
3673{
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003674 u8 genmask = nft_genmask_next(net);
Patrick McHardy20a69342013-10-11 12:06:22 +02003675 const struct nlattr *attr;
3676 struct nft_set *set;
3677 struct nft_ctx ctx;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003678 int rem, err = 0;
Patrick McHardy20a69342013-10-11 12:06:22 +02003679
Pablo Neira Ayuso7d5570c2014-07-25 13:15:36 +02003680 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3681 return -EINVAL;
3682
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003683 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003684 if (err < 0)
3685 return err;
3686
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02003687 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
3688 genmask);
Patrick McHardy20a69342013-10-11 12:06:22 +02003689 if (IS_ERR(set))
3690 return PTR_ERR(set);
3691 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3692 return -EBUSY;
3693
3694 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3695 err = nft_del_setelem(&ctx, set, attr);
3696 if (err < 0)
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003697 break;
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003698
Patrick McHardy3dd06732015-04-05 14:41:06 +02003699 set->ndeact++;
Patrick McHardy20a69342013-10-11 12:06:22 +02003700 }
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02003701 return err;
Patrick McHardy20a69342013-10-11 12:06:22 +02003702}
3703
Patrick McHardycfed7e12015-03-26 12:39:38 +00003704void nft_set_gc_batch_release(struct rcu_head *rcu)
3705{
3706 struct nft_set_gc_batch *gcb;
3707 unsigned int i;
3708
3709 gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
3710 for (i = 0; i < gcb->head.cnt; i++)
3711 nft_set_elem_destroy(gcb->head.set, gcb->elems[i]);
3712 kfree(gcb);
3713}
3714EXPORT_SYMBOL_GPL(nft_set_gc_batch_release);
3715
3716struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
3717 gfp_t gfp)
3718{
3719 struct nft_set_gc_batch *gcb;
3720
3721 gcb = kzalloc(sizeof(*gcb), gfp);
3722 if (gcb == NULL)
3723 return gcb;
3724 gcb->head.set = set;
3725 return gcb;
3726}
3727EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc);
3728
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003729static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
3730 u32 portid, u32 seq)
3731{
3732 struct nlmsghdr *nlh;
3733 struct nfgenmsg *nfmsg;
3734 int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN;
3735
3736 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
3737 if (nlh == NULL)
3738 goto nla_put_failure;
3739
3740 nfmsg = nlmsg_data(nlh);
3741 nfmsg->nfgen_family = AF_UNSPEC;
3742 nfmsg->version = NFNETLINK_V0;
3743 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
3744
3745 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)))
3746 goto nla_put_failure;
3747
Johannes Berg053c0952015-01-16 22:09:00 +01003748 nlmsg_end(skb, nlh);
3749 return 0;
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003750
3751nla_put_failure:
3752 nlmsg_trim(skb, nlh);
3753 return -EMSGSIZE;
3754}
3755
3756static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
3757{
3758 struct nlmsghdr *nlh = nlmsg_hdr(skb);
3759 struct sk_buff *skb2;
3760 int err;
3761
3762 if (nlmsg_report(nlh) &&
3763 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3764 return 0;
3765
3766 err = -ENOBUFS;
3767 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3768 if (skb2 == NULL)
3769 goto err;
3770
3771 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3772 nlh->nlmsg_seq);
3773 if (err < 0) {
3774 kfree_skb(skb2);
3775 goto err;
3776 }
3777
3778 err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
3779 NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
3780err:
3781 if (err < 0) {
3782 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
3783 err);
3784 }
3785 return err;
3786}
3787
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +01003788static int nf_tables_getgen(struct net *net, struct sock *nlsk,
3789 struct sk_buff *skb, const struct nlmsghdr *nlh,
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003790 const struct nlattr * const nla[])
3791{
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003792 struct sk_buff *skb2;
3793 int err;
3794
3795 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3796 if (skb2 == NULL)
3797 return -ENOMEM;
3798
3799 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3800 nlh->nlmsg_seq);
3801 if (err < 0)
3802 goto err;
3803
3804 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3805err:
3806 kfree_skb(skb2);
3807 return err;
3808}
3809
Patrick McHardy96518512013-10-14 11:00:02 +02003810static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3811 [NFT_MSG_NEWTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003812 .call_batch = nf_tables_newtable,
Patrick McHardy96518512013-10-14 11:00:02 +02003813 .attr_count = NFTA_TABLE_MAX,
3814 .policy = nft_table_policy,
3815 },
3816 [NFT_MSG_GETTABLE] = {
3817 .call = nf_tables_gettable,
3818 .attr_count = NFTA_TABLE_MAX,
3819 .policy = nft_table_policy,
3820 },
3821 [NFT_MSG_DELTABLE] = {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003822 .call_batch = nf_tables_deltable,
Patrick McHardy96518512013-10-14 11:00:02 +02003823 .attr_count = NFTA_TABLE_MAX,
3824 .policy = nft_table_policy,
3825 },
3826 [NFT_MSG_NEWCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003827 .call_batch = nf_tables_newchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003828 .attr_count = NFTA_CHAIN_MAX,
3829 .policy = nft_chain_policy,
3830 },
3831 [NFT_MSG_GETCHAIN] = {
3832 .call = nf_tables_getchain,
3833 .attr_count = NFTA_CHAIN_MAX,
3834 .policy = nft_chain_policy,
3835 },
3836 [NFT_MSG_DELCHAIN] = {
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003837 .call_batch = nf_tables_delchain,
Patrick McHardy96518512013-10-14 11:00:02 +02003838 .attr_count = NFTA_CHAIN_MAX,
3839 .policy = nft_chain_policy,
3840 },
3841 [NFT_MSG_NEWRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003842 .call_batch = nf_tables_newrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003843 .attr_count = NFTA_RULE_MAX,
3844 .policy = nft_rule_policy,
3845 },
3846 [NFT_MSG_GETRULE] = {
3847 .call = nf_tables_getrule,
3848 .attr_count = NFTA_RULE_MAX,
3849 .policy = nft_rule_policy,
3850 },
3851 [NFT_MSG_DELRULE] = {
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02003852 .call_batch = nf_tables_delrule,
Patrick McHardy96518512013-10-14 11:00:02 +02003853 .attr_count = NFTA_RULE_MAX,
3854 .policy = nft_rule_policy,
3855 },
Patrick McHardy20a69342013-10-11 12:06:22 +02003856 [NFT_MSG_NEWSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003857 .call_batch = nf_tables_newset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003858 .attr_count = NFTA_SET_MAX,
3859 .policy = nft_set_policy,
3860 },
3861 [NFT_MSG_GETSET] = {
3862 .call = nf_tables_getset,
3863 .attr_count = NFTA_SET_MAX,
3864 .policy = nft_set_policy,
3865 },
3866 [NFT_MSG_DELSET] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003867 .call_batch = nf_tables_delset,
Patrick McHardy20a69342013-10-11 12:06:22 +02003868 .attr_count = NFTA_SET_MAX,
3869 .policy = nft_set_policy,
3870 },
3871 [NFT_MSG_NEWSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003872 .call_batch = nf_tables_newsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003873 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3874 .policy = nft_set_elem_list_policy,
3875 },
3876 [NFT_MSG_GETSETELEM] = {
3877 .call = nf_tables_getsetelem,
3878 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3879 .policy = nft_set_elem_list_policy,
3880 },
3881 [NFT_MSG_DELSETELEM] = {
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02003882 .call_batch = nf_tables_delsetelem,
Patrick McHardy20a69342013-10-11 12:06:22 +02003883 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3884 .policy = nft_set_elem_list_policy,
3885 },
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02003886 [NFT_MSG_GETGEN] = {
3887 .call = nf_tables_getgen,
3888 },
Patrick McHardy96518512013-10-14 11:00:02 +02003889};
3890
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003891static void nft_chain_commit_update(struct nft_trans *trans)
3892{
3893 struct nft_base_chain *basechain;
3894
3895 if (nft_trans_chain_name(trans)[0])
3896 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3897
3898 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3899 return;
3900
3901 basechain = nft_base_chain(trans->ctx.chain);
3902 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3903
3904 switch (nft_trans_chain_policy(trans)) {
3905 case NF_DROP:
3906 case NF_ACCEPT:
3907 basechain->policy = nft_trans_chain_policy(trans);
3908 break;
3909 }
3910}
3911
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01003912static void nf_tables_commit_release(struct nft_trans *trans)
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003913{
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003914 switch (trans->msg_type) {
3915 case NFT_MSG_DELTABLE:
3916 nf_tables_table_destroy(&trans->ctx);
3917 break;
3918 case NFT_MSG_DELCHAIN:
3919 nf_tables_chain_destroy(trans->ctx.chain);
3920 break;
3921 case NFT_MSG_DELRULE:
3922 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3923 break;
3924 case NFT_MSG_DELSET:
3925 nft_set_destroy(nft_trans_set(trans));
3926 break;
Patrick McHardy61edafb2015-03-25 14:08:47 +00003927 case NFT_MSG_DELSETELEM:
3928 nft_set_elem_destroy(nft_trans_elem_set(trans),
3929 nft_trans_elem(trans).priv);
3930 break;
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02003931 }
3932 kfree(trans);
3933}
3934
Pablo Neira Ayuso5913bea2015-12-15 19:41:57 +01003935static int nf_tables_commit(struct net *net, struct sk_buff *skb)
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003936{
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003937 struct nft_trans *trans, *next;
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02003938 struct nft_trans_elem *te;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003939
3940 /* Bump generation counter, invalidate any dump in progress */
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02003941 while (++net->nft.base_seq == 0);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003942
3943 /* A new generation has just started */
Patrick McHardyea4bd992015-03-25 14:08:49 +00003944 net->nft.gencursor = nft_gencursor_next(net);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003945
3946 /* Make sure all packets have left the previous generation before
3947 * purging old rules.
3948 */
3949 synchronize_rcu();
3950
3951 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003952 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003953 case NFT_MSG_NEWTABLE:
3954 if (nft_trans_table_update(trans)) {
3955 if (!nft_trans_table_enable(trans)) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02003956 nf_tables_table_disable(net,
3957 trans->ctx.afi,
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003958 trans->ctx.table);
3959 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3960 }
3961 } else {
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003962 nft_clear(net, trans->ctx.table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003963 }
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003964 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003965 nft_trans_destroy(trans);
3966 break;
3967 case NFT_MSG_DELTABLE:
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02003968 list_del_rcu(&trans->ctx.table->list);
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003969 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02003970 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003971 case NFT_MSG_NEWCHAIN:
3972 if (nft_trans_chain_update(trans))
3973 nft_chain_commit_update(trans);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003974 else
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02003975 nft_clear(net, trans->ctx.chain);
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02003976
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003977 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003978 nft_trans_destroy(trans);
3979 break;
3980 case NFT_MSG_DELCHAIN:
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02003981 list_del_rcu(&trans->ctx.chain->list);
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003982 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +02003983 nf_tables_unregister_hooks(trans->ctx.net,
3984 trans->ctx.table,
Arturo Borreroc5598792014-09-02 16:42:23 +02003985 trans->ctx.chain,
3986 trans->ctx.afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02003987 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003988 case NFT_MSG_NEWRULE:
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02003989 nft_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003990 nf_tables_rule_notify(&trans->ctx,
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003991 nft_trans_rule(trans),
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003992 NFT_MSG_NEWRULE);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02003993 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02003994 break;
3995 case NFT_MSG_DELRULE:
3996 list_del_rcu(&nft_trans_rule(trans)->list);
Pablo Neira Ayuso35151d82014-05-05 17:12:40 +02003997 nf_tables_rule_notify(&trans->ctx,
3998 nft_trans_rule(trans),
3999 NFT_MSG_DELRULE);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02004000 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004001 case NFT_MSG_NEWSET:
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02004002 nft_clear(net, nft_trans_set(trans));
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004003 /* This avoids hitting -EBUSY when deleting the table
4004 * from the transaction.
4005 */
4006 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
4007 !list_empty(&nft_trans_set(trans)->bindings))
4008 trans->ctx.table->use--;
4009
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004010 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02004011 NFT_MSG_NEWSET, GFP_KERNEL);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004012 nft_trans_destroy(trans);
4013 break;
4014 case NFT_MSG_DELSET:
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02004015 list_del_rcu(&nft_trans_set(trans)->list);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004016 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
Pablo Neira Ayuso31f84412014-05-29 10:29:58 +02004017 NFT_MSG_DELSET, GFP_KERNEL);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004018 break;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004019 case NFT_MSG_NEWSETELEM:
Patrick McHardycc02e452015-03-25 14:08:50 +00004020 te = (struct nft_trans_elem *)trans->data;
4021
4022 te->set->ops->activate(te->set, &te->elem);
4023 nf_tables_setelem_notify(&trans->ctx, te->set,
4024 &te->elem,
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004025 NFT_MSG_NEWSETELEM, 0);
4026 nft_trans_destroy(trans);
4027 break;
4028 case NFT_MSG_DELSETELEM:
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02004029 te = (struct nft_trans_elem *)trans->data;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00004030
Pablo Neira Ayusoa3716e72014-08-01 19:32:41 +02004031 nf_tables_setelem_notify(&trans->ctx, te->set,
4032 &te->elem,
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004033 NFT_MSG_DELSETELEM, 0);
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01004034 te->set->ops->remove(te->set, &te->elem);
Patrick McHardy3dd06732015-04-05 14:41:06 +02004035 atomic_dec(&te->set->nelems);
4036 te->set->ndeact--;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004037 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004038 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004039 }
4040
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01004041 synchronize_rcu();
4042
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004043 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02004044 list_del(&trans->list);
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01004045 nf_tables_commit_release(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004046 }
4047
Pablo Neira Ayuso84d7fce2014-09-04 14:30:22 +02004048 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
4049
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004050 return 0;
4051}
4052
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01004053static void nf_tables_abort_release(struct nft_trans *trans)
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02004054{
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02004055 switch (trans->msg_type) {
4056 case NFT_MSG_NEWTABLE:
4057 nf_tables_table_destroy(&trans->ctx);
4058 break;
4059 case NFT_MSG_NEWCHAIN:
4060 nf_tables_chain_destroy(trans->ctx.chain);
4061 break;
4062 case NFT_MSG_NEWRULE:
4063 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
4064 break;
4065 case NFT_MSG_NEWSET:
4066 nft_set_destroy(nft_trans_set(trans));
4067 break;
Patrick McHardy61edafb2015-03-25 14:08:47 +00004068 case NFT_MSG_NEWSETELEM:
4069 nft_set_elem_destroy(nft_trans_elem_set(trans),
4070 nft_trans_elem(trans).priv);
4071 break;
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02004072 }
4073 kfree(trans);
4074}
4075
Pablo Neira Ayuso5913bea2015-12-15 19:41:57 +01004076static int nf_tables_abort(struct net *net, struct sk_buff *skb)
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004077{
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004078 struct nft_trans *trans, *next;
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01004079 struct nft_trans_elem *te;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004080
Xin Longa907e362015-12-07 18:48:07 +08004081 list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list,
4082 list) {
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02004083 switch (trans->msg_type) {
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02004084 case NFT_MSG_NEWTABLE:
4085 if (nft_trans_table_update(trans)) {
4086 if (nft_trans_table_enable(trans)) {
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02004087 nf_tables_table_disable(net,
4088 trans->ctx.afi,
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02004089 trans->ctx.table);
4090 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
4091 }
4092 nft_trans_destroy(trans);
4093 } else {
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02004094 list_del_rcu(&trans->ctx.table->list);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02004095 }
4096 break;
4097 case NFT_MSG_DELTABLE:
Pablo Neira Ayusof2a6d762016-06-14 17:29:18 +02004098 nft_clear(trans->ctx.net, trans->ctx.table);
Pablo Neira Ayuso55dd6f92014-04-03 11:53:37 +02004099 nft_trans_destroy(trans);
4100 break;
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02004101 case NFT_MSG_NEWCHAIN:
4102 if (nft_trans_chain_update(trans)) {
Markus Elfring982f4052014-11-18 20:37:05 +01004103 free_percpu(nft_trans_chain_stats(trans));
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02004104
4105 nft_trans_destroy(trans);
4106 } else {
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004107 trans->ctx.table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02004108 list_del_rcu(&trans->ctx.chain->list);
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +02004109 nf_tables_unregister_hooks(trans->ctx.net,
4110 trans->ctx.table,
Arturo Borreroc5598792014-09-02 16:42:23 +02004111 trans->ctx.chain,
4112 trans->ctx.afi->nops);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02004113 }
4114 break;
4115 case NFT_MSG_DELCHAIN:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004116 trans->ctx.table->use++;
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02004117 nft_clear(trans->ctx.net, trans->ctx.chain);
Pablo Neira Ayuso91c7b382014-04-09 11:58:08 +02004118 nft_trans_destroy(trans);
4119 break;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02004120 case NFT_MSG_NEWRULE:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004121 trans->ctx.chain->use--;
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02004122 list_del_rcu(&nft_trans_rule(trans)->list);
4123 break;
4124 case NFT_MSG_DELRULE:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004125 trans->ctx.chain->use++;
Pablo Neira Ayuso889f7ee2016-06-12 18:07:07 +02004126 nft_clear(trans->ctx.net, nft_trans_rule(trans));
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004127 nft_trans_destroy(trans);
Pablo Neira Ayusob380e5c2014-04-04 01:38:51 +02004128 break;
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004129 case NFT_MSG_NEWSET:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004130 trans->ctx.table->use--;
Pablo Neira Ayusoe688a7f2014-07-01 11:49:18 +02004131 list_del_rcu(&nft_trans_set(trans)->list);
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004132 break;
4133 case NFT_MSG_DELSET:
Pablo Neira Ayuso4fefee52014-05-23 12:39:26 +02004134 trans->ctx.table->use++;
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02004135 nft_clear(trans->ctx.net, nft_trans_set(trans));
Pablo Neira Ayuso958bee12014-04-03 11:48:44 +02004136 nft_trans_destroy(trans);
4137 break;
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004138 case NFT_MSG_NEWSETELEM:
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01004139 te = (struct nft_trans_elem *)trans->data;
Patrick McHardyfe2811e2015-03-25 13:07:50 +00004140
Pablo Neira Ayuso02263db2015-02-20 17:11:10 +01004141 te->set->ops->remove(te->set, &te->elem);
Patrick McHardy3dd06732015-04-05 14:41:06 +02004142 atomic_dec(&te->set->nelems);
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004143 break;
4144 case NFT_MSG_DELSETELEM:
Patrick McHardycc02e452015-03-25 14:08:50 +00004145 te = (struct nft_trans_elem *)trans->data;
4146
Patrick McHardycc02e452015-03-25 14:08:50 +00004147 te->set->ops->activate(te->set, &te->elem);
Patrick McHardy3dd06732015-04-05 14:41:06 +02004148 te->set->ndeact--;
Patrick McHardycc02e452015-03-25 14:08:50 +00004149
Pablo Neira Ayuso60319eb2014-04-04 03:36:42 +02004150 nft_trans_destroy(trans);
4151 break;
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004152 }
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004153 }
4154
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01004155 synchronize_rcu();
4156
Pablo Neira Ayusoa1cee072014-05-23 11:09:42 +02004157 list_for_each_entry_safe_reverse(trans, next,
4158 &net->nft.commit_list, list) {
Pablo Neira Ayusoc7c32e72014-04-10 00:31:10 +02004159 list_del(&trans->list);
Pablo Neira Ayusob326dd32014-11-10 21:14:12 +01004160 nf_tables_abort_release(trans);
Pablo Neira Ayuso37082f92014-04-03 11:56:37 +02004161 }
4162
4163 return 0;
4164}
4165
Patrick McHardy96518512013-10-14 11:00:02 +02004166static const struct nfnetlink_subsystem nf_tables_subsys = {
4167 .name = "nf_tables",
4168 .subsys_id = NFNL_SUBSYS_NFTABLES,
4169 .cb_count = NFT_MSG_MAX,
4170 .cb = nf_tables_cb,
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02004171 .commit = nf_tables_commit,
4172 .abort = nf_tables_abort,
Patrick McHardy96518512013-10-14 11:00:02 +02004173};
4174
Pablo Neira Ayuso7210e4e2014-10-13 19:50:22 +02004175int nft_chain_validate_dependency(const struct nft_chain *chain,
4176 enum nft_chain_type type)
4177{
4178 const struct nft_base_chain *basechain;
4179
4180 if (chain->flags & NFT_BASE_CHAIN) {
4181 basechain = nft_base_chain(chain);
4182 if (basechain->type->type != type)
4183 return -EOPNOTSUPP;
4184 }
4185 return 0;
4186}
4187EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
4188
Pablo Neira Ayuso75e8d062015-01-14 15:33:57 +01004189int nft_chain_validate_hooks(const struct nft_chain *chain,
4190 unsigned int hook_flags)
4191{
4192 struct nft_base_chain *basechain;
4193
4194 if (chain->flags & NFT_BASE_CHAIN) {
4195 basechain = nft_base_chain(chain);
4196
4197 if ((1 << basechain->ops[0].hooknum) & hook_flags)
4198 return 0;
4199
4200 return -EOPNOTSUPP;
4201 }
4202
4203 return 0;
4204}
4205EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
4206
Patrick McHardy20a69342013-10-11 12:06:22 +02004207/*
4208 * Loop detection - walk through the ruleset beginning at the destination chain
4209 * of a new jump until either the source chain is reached (loop) or all
4210 * reachable chains have been traversed.
4211 *
4212 * The loop check is performed whenever a new jump verdict is added to an
4213 * expression or verdict map or a verdict map is bound to a new chain.
4214 */
4215
4216static int nf_tables_check_loops(const struct nft_ctx *ctx,
4217 const struct nft_chain *chain);
4218
4219static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
4220 const struct nft_set *set,
4221 const struct nft_set_iter *iter,
4222 const struct nft_set_elem *elem)
4223{
Patrick McHardyfe2811e2015-03-25 13:07:50 +00004224 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
4225 const struct nft_data *data;
4226
4227 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
4228 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
Pablo Neira Ayuso62f9c8b2014-02-07 14:45:01 +01004229 return 0;
4230
Patrick McHardyfe2811e2015-03-25 13:07:50 +00004231 data = nft_set_ext_data(ext);
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004232 switch (data->verdict.code) {
Patrick McHardy20a69342013-10-11 12:06:22 +02004233 case NFT_JUMP:
4234 case NFT_GOTO:
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004235 return nf_tables_check_loops(ctx, data->verdict.chain);
Patrick McHardy20a69342013-10-11 12:06:22 +02004236 default:
4237 return 0;
4238 }
4239}
4240
4241static int nf_tables_check_loops(const struct nft_ctx *ctx,
4242 const struct nft_chain *chain)
4243{
4244 const struct nft_rule *rule;
4245 const struct nft_expr *expr, *last;
Patrick McHardy20a69342013-10-11 12:06:22 +02004246 const struct nft_set *set;
4247 struct nft_set_binding *binding;
4248 struct nft_set_iter iter;
Patrick McHardy20a69342013-10-11 12:06:22 +02004249
4250 if (ctx->chain == chain)
4251 return -ELOOP;
4252
4253 list_for_each_entry(rule, &chain->rules, list) {
4254 nft_rule_for_each_expr(expr, last, rule) {
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02004255 const struct nft_data *data = NULL;
4256 int err;
4257
4258 if (!expr->ops->validate)
Patrick McHardy20a69342013-10-11 12:06:22 +02004259 continue;
4260
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02004261 err = expr->ops->validate(ctx, expr, &data);
4262 if (err < 0)
4263 return err;
4264
Patrick McHardy20a69342013-10-11 12:06:22 +02004265 if (data == NULL)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02004266 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02004267
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004268 switch (data->verdict.code) {
Patrick McHardy20a69342013-10-11 12:06:22 +02004269 case NFT_JUMP:
4270 case NFT_GOTO:
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004271 err = nf_tables_check_loops(ctx,
4272 data->verdict.chain);
Patrick McHardy20a69342013-10-11 12:06:22 +02004273 if (err < 0)
4274 return err;
4275 default:
4276 break;
4277 }
4278 }
4279 }
4280
4281 list_for_each_entry(set, &ctx->table->sets, list) {
Pablo Neira Ayuso37a9cc52016-06-12 22:52:45 +02004282 if (!nft_is_active_next(ctx->net, set))
4283 continue;
Patrick McHardy20a69342013-10-11 12:06:22 +02004284 if (!(set->flags & NFT_SET_MAP) ||
4285 set->dtype != NFT_DATA_VERDICT)
4286 continue;
4287
4288 list_for_each_entry(binding, &set->bindings, list) {
Patrick McHardy11113e12015-04-05 14:41:07 +02004289 if (!(binding->flags & NFT_SET_MAP) ||
4290 binding->chain != chain)
Patrick McHardy20a69342013-10-11 12:06:22 +02004291 continue;
4292
4293 iter.skip = 0;
4294 iter.count = 0;
4295 iter.err = 0;
4296 iter.fn = nf_tables_loop_check_setelem;
4297
4298 set->ops->walk(ctx, set, &iter);
4299 if (iter.err < 0)
4300 return iter.err;
4301 }
4302 }
4303
4304 return 0;
4305}
4306
Patrick McHardy49499c32015-04-11 02:27:37 +01004307/**
4308 * nft_parse_register - parse a register value from a netlink attribute
4309 *
4310 * @attr: netlink attribute
4311 *
4312 * Parse and translate a register value from a netlink attribute.
4313 * Registers used to be 128 bit wide, these register numbers will be
4314 * mapped to the corresponding 32 bit register numbers.
4315 */
Patrick McHardyb1c96ed2015-04-11 02:27:36 +01004316unsigned int nft_parse_register(const struct nlattr *attr)
4317{
Patrick McHardy49499c32015-04-11 02:27:37 +01004318 unsigned int reg;
4319
4320 reg = ntohl(nla_get_be32(attr));
4321 switch (reg) {
4322 case NFT_REG_VERDICT...NFT_REG_4:
4323 return reg * NFT_REG_SIZE / NFT_REG32_SIZE;
4324 default:
4325 return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
4326 }
Patrick McHardyb1c96ed2015-04-11 02:27:36 +01004327}
4328EXPORT_SYMBOL_GPL(nft_parse_register);
4329
Patrick McHardy49499c32015-04-11 02:27:37 +01004330/**
4331 * nft_dump_register - dump a register value to a netlink attribute
4332 *
4333 * @skb: socket buffer
4334 * @attr: attribute number
4335 * @reg: register number
4336 *
4337 * Construct a netlink attribute containing the register number. For
4338 * compatibility reasons, register numbers being a multiple of 4 are
4339 * translated to the corresponding 128 bit register numbers.
4340 */
Patrick McHardyb1c96ed2015-04-11 02:27:36 +01004341int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
4342{
Patrick McHardy49499c32015-04-11 02:27:37 +01004343 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
4344 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
4345 else
4346 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
4347
Patrick McHardyb1c96ed2015-04-11 02:27:36 +01004348 return nla_put_be32(skb, attr, htonl(reg));
4349}
4350EXPORT_SYMBOL_GPL(nft_dump_register);
4351
Patrick McHardy96518512013-10-14 11:00:02 +02004352/**
Patrick McHardyd07db982015-04-11 02:27:30 +01004353 * nft_validate_register_load - validate a load from a register
Patrick McHardy96518512013-10-14 11:00:02 +02004354 *
4355 * @reg: the register number
Patrick McHardyd07db982015-04-11 02:27:30 +01004356 * @len: the length of the data
Patrick McHardy96518512013-10-14 11:00:02 +02004357 *
4358 * Validate that the input register is one of the general purpose
Patrick McHardyd07db982015-04-11 02:27:30 +01004359 * registers and that the length of the load is within the bounds.
Patrick McHardy96518512013-10-14 11:00:02 +02004360 */
Patrick McHardyd07db982015-04-11 02:27:30 +01004361int nft_validate_register_load(enum nft_registers reg, unsigned int len)
Patrick McHardy96518512013-10-14 11:00:02 +02004362{
Patrick McHardy49499c32015-04-11 02:27:37 +01004363 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
Patrick McHardy96518512013-10-14 11:00:02 +02004364 return -EINVAL;
Patrick McHardyd07db982015-04-11 02:27:30 +01004365 if (len == 0)
4366 return -EINVAL;
Patrick McHardy49499c32015-04-11 02:27:37 +01004367 if (reg * NFT_REG32_SIZE + len > FIELD_SIZEOF(struct nft_regs, data))
Patrick McHardyd07db982015-04-11 02:27:30 +01004368 return -ERANGE;
Patrick McHardy49499c32015-04-11 02:27:37 +01004369
Patrick McHardy96518512013-10-14 11:00:02 +02004370 return 0;
4371}
Patrick McHardyd07db982015-04-11 02:27:30 +01004372EXPORT_SYMBOL_GPL(nft_validate_register_load);
Patrick McHardy96518512013-10-14 11:00:02 +02004373
4374/**
Patrick McHardy1ec10212015-04-11 02:27:27 +01004375 * nft_validate_register_store - validate an expressions' register store
Patrick McHardy96518512013-10-14 11:00:02 +02004376 *
4377 * @ctx: context of the expression performing the load
4378 * @reg: the destination register number
4379 * @data: the data to load
4380 * @type: the data type
Patrick McHardy45d9bcd2015-04-11 02:27:26 +01004381 * @len: the length of the data
Patrick McHardy96518512013-10-14 11:00:02 +02004382 *
4383 * Validate that a data load uses the appropriate data type for
Patrick McHardy45d9bcd2015-04-11 02:27:26 +01004384 * the destination register and the length is within the bounds.
4385 * A value of NULL for the data means that its runtime gathered
Patrick McHardy58f40ab2015-04-11 02:27:28 +01004386 * data.
Patrick McHardy96518512013-10-14 11:00:02 +02004387 */
Patrick McHardy1ec10212015-04-11 02:27:27 +01004388int nft_validate_register_store(const struct nft_ctx *ctx,
4389 enum nft_registers reg,
4390 const struct nft_data *data,
4391 enum nft_data_types type, unsigned int len)
Patrick McHardy96518512013-10-14 11:00:02 +02004392{
Patrick McHardy20a69342013-10-11 12:06:22 +02004393 int err;
4394
Patrick McHardy96518512013-10-14 11:00:02 +02004395 switch (reg) {
4396 case NFT_REG_VERDICT:
Patrick McHardy58f40ab2015-04-11 02:27:28 +01004397 if (type != NFT_DATA_VERDICT)
Patrick McHardy96518512013-10-14 11:00:02 +02004398 return -EINVAL;
Patrick McHardy20a69342013-10-11 12:06:22 +02004399
Patrick McHardy58f40ab2015-04-11 02:27:28 +01004400 if (data != NULL &&
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004401 (data->verdict.code == NFT_GOTO ||
4402 data->verdict.code == NFT_JUMP)) {
4403 err = nf_tables_check_loops(ctx, data->verdict.chain);
Patrick McHardy20a69342013-10-11 12:06:22 +02004404 if (err < 0)
4405 return err;
4406
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004407 if (ctx->chain->level + 1 >
4408 data->verdict.chain->level) {
Patrick McHardy20a69342013-10-11 12:06:22 +02004409 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
4410 return -EMLINK;
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004411 data->verdict.chain->level = ctx->chain->level + 1;
Patrick McHardy20a69342013-10-11 12:06:22 +02004412 }
4413 }
4414
Patrick McHardy96518512013-10-14 11:00:02 +02004415 return 0;
4416 default:
Patrick McHardy49499c32015-04-11 02:27:37 +01004417 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
Patrick McHardy27e6d202015-04-11 02:27:29 +01004418 return -EINVAL;
Patrick McHardy45d9bcd2015-04-11 02:27:26 +01004419 if (len == 0)
4420 return -EINVAL;
Patrick McHardy49499c32015-04-11 02:27:37 +01004421 if (reg * NFT_REG32_SIZE + len >
4422 FIELD_SIZEOF(struct nft_regs, data))
Patrick McHardy45d9bcd2015-04-11 02:27:26 +01004423 return -ERANGE;
Patrick McHardy27e6d202015-04-11 02:27:29 +01004424
Patrick McHardy96518512013-10-14 11:00:02 +02004425 if (data != NULL && type != NFT_DATA_VALUE)
4426 return -EINVAL;
4427 return 0;
4428 }
4429}
Patrick McHardy1ec10212015-04-11 02:27:27 +01004430EXPORT_SYMBOL_GPL(nft_validate_register_store);
Patrick McHardy96518512013-10-14 11:00:02 +02004431
4432static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
4433 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
4434 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
4435 .len = NFT_CHAIN_MAXNAMELEN - 1 },
4436};
4437
4438static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
4439 struct nft_data_desc *desc, const struct nlattr *nla)
4440{
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02004441 u8 genmask = nft_genmask_next(ctx->net);
Patrick McHardy96518512013-10-14 11:00:02 +02004442 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
4443 struct nft_chain *chain;
4444 int err;
4445
4446 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
4447 if (err < 0)
4448 return err;
4449
4450 if (!tb[NFTA_VERDICT_CODE])
4451 return -EINVAL;
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004452 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
Patrick McHardy96518512013-10-14 11:00:02 +02004453
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004454 switch (data->verdict.code) {
Patrick McHardye0abdad2014-02-18 18:06:50 +00004455 default:
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004456 switch (data->verdict.code & NF_VERDICT_MASK) {
Patrick McHardye0abdad2014-02-18 18:06:50 +00004457 case NF_ACCEPT:
4458 case NF_DROP:
4459 case NF_QUEUE:
4460 break;
4461 default:
4462 return -EINVAL;
4463 }
4464 /* fall through */
Patrick McHardy96518512013-10-14 11:00:02 +02004465 case NFT_CONTINUE:
4466 case NFT_BREAK:
4467 case NFT_RETURN:
Patrick McHardy96518512013-10-14 11:00:02 +02004468 break;
4469 case NFT_JUMP:
4470 case NFT_GOTO:
4471 if (!tb[NFTA_VERDICT_CHAIN])
4472 return -EINVAL;
4473 chain = nf_tables_chain_lookup(ctx->table,
Pablo Neira Ayuso664b0f82016-06-12 19:21:31 +02004474 tb[NFTA_VERDICT_CHAIN], genmask);
Patrick McHardy96518512013-10-14 11:00:02 +02004475 if (IS_ERR(chain))
4476 return PTR_ERR(chain);
4477 if (chain->flags & NFT_BASE_CHAIN)
4478 return -EOPNOTSUPP;
4479
Patrick McHardy96518512013-10-14 11:00:02 +02004480 chain->use++;
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004481 data->verdict.chain = chain;
Patrick McHardy96518512013-10-14 11:00:02 +02004482 break;
Patrick McHardy96518512013-10-14 11:00:02 +02004483 }
4484
Florian Westphal4c4ed072015-04-14 16:44:14 +02004485 desc->len = sizeof(data->verdict);
Patrick McHardy96518512013-10-14 11:00:02 +02004486 desc->type = NFT_DATA_VERDICT;
4487 return 0;
4488}
4489
4490static void nft_verdict_uninit(const struct nft_data *data)
4491{
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004492 switch (data->verdict.code) {
Patrick McHardy96518512013-10-14 11:00:02 +02004493 case NFT_JUMP:
4494 case NFT_GOTO:
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004495 data->verdict.chain->use--;
Patrick McHardy96518512013-10-14 11:00:02 +02004496 break;
4497 }
4498}
4499
Florian Westphal33d5a7b2015-11-28 21:53:04 +01004500int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
Patrick McHardy96518512013-10-14 11:00:02 +02004501{
4502 struct nlattr *nest;
4503
Florian Westphal33d5a7b2015-11-28 21:53:04 +01004504 nest = nla_nest_start(skb, type);
Patrick McHardy96518512013-10-14 11:00:02 +02004505 if (!nest)
4506 goto nla_put_failure;
4507
Florian Westphal33d5a7b2015-11-28 21:53:04 +01004508 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
Patrick McHardy96518512013-10-14 11:00:02 +02004509 goto nla_put_failure;
4510
Florian Westphal33d5a7b2015-11-28 21:53:04 +01004511 switch (v->code) {
Patrick McHardy96518512013-10-14 11:00:02 +02004512 case NFT_JUMP:
4513 case NFT_GOTO:
Patrick McHardy1ca2e172015-04-11 02:27:32 +01004514 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
Florian Westphal33d5a7b2015-11-28 21:53:04 +01004515 v->chain->name))
Patrick McHardy96518512013-10-14 11:00:02 +02004516 goto nla_put_failure;
4517 }
4518 nla_nest_end(skb, nest);
4519 return 0;
4520
4521nla_put_failure:
4522 return -1;
4523}
4524
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004525static int nft_value_init(const struct nft_ctx *ctx,
4526 struct nft_data *data, unsigned int size,
Patrick McHardy96518512013-10-14 11:00:02 +02004527 struct nft_data_desc *desc, const struct nlattr *nla)
4528{
4529 unsigned int len;
4530
4531 len = nla_len(nla);
4532 if (len == 0)
4533 return -EINVAL;
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004534 if (len > size)
Patrick McHardy96518512013-10-14 11:00:02 +02004535 return -EOVERFLOW;
4536
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004537 nla_memcpy(data->data, nla, len);
Patrick McHardy96518512013-10-14 11:00:02 +02004538 desc->type = NFT_DATA_VALUE;
4539 desc->len = len;
4540 return 0;
4541}
4542
4543static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
4544 unsigned int len)
4545{
4546 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
4547}
4548
4549static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004550 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
Patrick McHardy96518512013-10-14 11:00:02 +02004551 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
4552};
4553
4554/**
4555 * nft_data_init - parse nf_tables data netlink attributes
4556 *
4557 * @ctx: context of the expression using the data
4558 * @data: destination struct nft_data
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004559 * @size: maximum data length
Patrick McHardy96518512013-10-14 11:00:02 +02004560 * @desc: data description
4561 * @nla: netlink attribute containing data
4562 *
4563 * Parse the netlink data attributes and initialize a struct nft_data.
4564 * The type and length of data are returned in the data description.
4565 *
4566 * The caller can indicate that it only wants to accept data of type
4567 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4568 */
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004569int nft_data_init(const struct nft_ctx *ctx,
4570 struct nft_data *data, unsigned int size,
Patrick McHardy96518512013-10-14 11:00:02 +02004571 struct nft_data_desc *desc, const struct nlattr *nla)
4572{
4573 struct nlattr *tb[NFTA_DATA_MAX + 1];
4574 int err;
4575
4576 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
4577 if (err < 0)
4578 return err;
4579
4580 if (tb[NFTA_DATA_VALUE])
Patrick McHardyd0a11fc2015-04-11 02:27:38 +01004581 return nft_value_init(ctx, data, size, desc,
4582 tb[NFTA_DATA_VALUE]);
Patrick McHardy96518512013-10-14 11:00:02 +02004583 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
4584 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
4585 return -EINVAL;
4586}
4587EXPORT_SYMBOL_GPL(nft_data_init);
4588
4589/**
4590 * nft_data_uninit - release a nft_data item
4591 *
4592 * @data: struct nft_data to release
4593 * @type: type of data
4594 *
4595 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4596 * all others need to be released by calling this function.
4597 */
4598void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
4599{
Mirek Kratochvil960bd2c2015-05-15 21:15:29 +02004600 if (type < NFT_DATA_VERDICT)
Patrick McHardy96518512013-10-14 11:00:02 +02004601 return;
Mirek Kratochvil960bd2c2015-05-15 21:15:29 +02004602 switch (type) {
Patrick McHardy96518512013-10-14 11:00:02 +02004603 case NFT_DATA_VERDICT:
4604 return nft_verdict_uninit(data);
4605 default:
4606 WARN_ON(1);
4607 }
4608}
4609EXPORT_SYMBOL_GPL(nft_data_uninit);
4610
4611int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
4612 enum nft_data_types type, unsigned int len)
4613{
4614 struct nlattr *nest;
4615 int err;
4616
4617 nest = nla_nest_start(skb, attr);
4618 if (nest == NULL)
4619 return -1;
4620
4621 switch (type) {
4622 case NFT_DATA_VALUE:
4623 err = nft_value_dump(skb, data, len);
4624 break;
4625 case NFT_DATA_VERDICT:
Florian Westphal33d5a7b2015-11-28 21:53:04 +01004626 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
Patrick McHardy96518512013-10-14 11:00:02 +02004627 break;
4628 default:
4629 err = -EINVAL;
4630 WARN_ON(1);
4631 }
4632
4633 nla_nest_end(skb, nest);
4634 return err;
4635}
4636EXPORT_SYMBOL_GPL(nft_data_dump);
4637
Pablo Neira Ayusodf05ef82015-12-15 19:39:32 +01004638static int __net_init nf_tables_init_net(struct net *net)
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004639{
4640 INIT_LIST_HEAD(&net->nft.af_info);
Pablo Neira Ayuso0628b122013-10-14 11:05:33 +02004641 INIT_LIST_HEAD(&net->nft.commit_list);
Pablo Neira Ayuso38e029f2014-07-01 12:23:12 +02004642 net->nft.base_seq = 1;
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004643 return 0;
4644}
4645
Pablo Neira Ayuso5ebe0b02015-12-15 19:40:49 +01004646int __nft_release_basechain(struct nft_ctx *ctx)
4647{
4648 struct nft_rule *rule, *nr;
4649
4650 BUG_ON(!(ctx->chain->flags & NFT_BASE_CHAIN));
4651
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +02004652 nf_tables_unregister_hooks(ctx->net, ctx->chain->table, ctx->chain,
Pablo Neira Ayuso5ebe0b02015-12-15 19:40:49 +01004653 ctx->afi->nops);
4654 list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
4655 list_del(&rule->list);
4656 ctx->chain->use--;
4657 nf_tables_rule_destroy(ctx, rule);
4658 }
4659 list_del(&ctx->chain->list);
4660 ctx->table->use--;
4661 nf_tables_chain_destroy(ctx->chain);
4662
4663 return 0;
4664}
4665EXPORT_SYMBOL_GPL(__nft_release_basechain);
4666
Pablo Neira Ayusodf05ef82015-12-15 19:39:32 +01004667/* Called by nft_unregister_afinfo() from __net_exit path, nfnl_lock is held. */
4668static void __nft_release_afinfo(struct net *net, struct nft_af_info *afi)
4669{
4670 struct nft_table *table, *nt;
4671 struct nft_chain *chain, *nc;
4672 struct nft_rule *rule, *nr;
4673 struct nft_set *set, *ns;
4674 struct nft_ctx ctx = {
4675 .net = net,
4676 .afi = afi,
4677 };
4678
4679 list_for_each_entry_safe(table, nt, &afi->tables, list) {
4680 list_for_each_entry(chain, &table->chains, list)
Pablo Neira Ayuso82bec712016-06-22 14:26:33 +02004681 nf_tables_unregister_hooks(net, table, chain,
4682 afi->nops);
Pablo Neira Ayusodf05ef82015-12-15 19:39:32 +01004683 /* No packets are walking on these chains anymore. */
4684 ctx.table = table;
4685 list_for_each_entry(chain, &table->chains, list) {
4686 ctx.chain = chain;
4687 list_for_each_entry_safe(rule, nr, &chain->rules, list) {
4688 list_del(&rule->list);
4689 chain->use--;
4690 nf_tables_rule_destroy(&ctx, rule);
4691 }
4692 }
4693 list_for_each_entry_safe(set, ns, &table->sets, list) {
4694 list_del(&set->list);
4695 table->use--;
4696 nft_set_destroy(set);
4697 }
4698 list_for_each_entry_safe(chain, nc, &table->chains, list) {
4699 list_del(&chain->list);
4700 table->use--;
4701 nf_tables_chain_destroy(chain);
4702 }
4703 list_del(&table->list);
4704 nf_tables_table_destroy(&ctx);
4705 }
4706}
4707
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004708static struct pernet_operations nf_tables_net_ops = {
4709 .init = nf_tables_init_net,
4710};
4711
Patrick McHardy96518512013-10-14 11:00:02 +02004712static int __init nf_tables_module_init(void)
4713{
4714 int err;
4715
4716 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
4717 GFP_KERNEL);
4718 if (info == NULL) {
4719 err = -ENOMEM;
4720 goto err1;
4721 }
4722
4723 err = nf_tables_core_module_init();
4724 if (err < 0)
4725 goto err2;
4726
4727 err = nfnetlink_subsys_register(&nf_tables_subsys);
4728 if (err < 0)
4729 goto err3;
4730
4731 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004732 return register_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02004733err3:
4734 nf_tables_core_module_exit();
4735err2:
4736 kfree(info);
4737err1:
4738 return err;
4739}
4740
4741static void __exit nf_tables_module_exit(void)
4742{
Pablo Neira Ayuso99633ab2013-10-10 23:28:33 +02004743 unregister_pernet_subsys(&nf_tables_net_ops);
Patrick McHardy96518512013-10-14 11:00:02 +02004744 nfnetlink_subsys_unregister(&nf_tables_subsys);
Pablo Neira Ayuso1b1bc492014-10-01 13:53:20 +02004745 rcu_barrier();
Patrick McHardy96518512013-10-14 11:00:02 +02004746 nf_tables_core_module_exit();
4747 kfree(info);
4748}
4749
4750module_init(nf_tables_module_init);
4751module_exit(nf_tables_module_exit);
4752
4753MODULE_LICENSE("GPL");
4754MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4755MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);