blob: 0480f57a4eb6c62da5b02951e51db77d4af3e0b4 [file] [log] [blame]
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +02001/*
2 * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
3 *
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 * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nfnetlink.h>
17#include <linux/netfilter/nf_tables.h>
18#include <linux/netfilter/nf_tables_compat.h>
19#include <linux/netfilter/x_tables.h>
20#include <linux/netfilter_ipv4/ip_tables.h>
21#include <linux/netfilter_ipv6/ip6_tables.h>
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020022#include <net/netfilter/nf_tables.h>
23
Pablo Neira Ayusof3f5dde2014-10-14 10:13:48 +020024static const struct {
25 const char *name;
26 u8 type;
27} table_to_chaintype[] = {
28 { "filter", NFT_CHAIN_T_DEFAULT },
29 { "raw", NFT_CHAIN_T_DEFAULT },
30 { "security", NFT_CHAIN_T_DEFAULT },
31 { "mangle", NFT_CHAIN_T_ROUTE },
32 { "nat", NFT_CHAIN_T_NAT },
33 { },
34};
35
36static int nft_compat_table_to_chaintype(const char *table)
37{
38 int i;
39
40 for (i = 0; table_to_chaintype[i].name != NULL; i++) {
41 if (strcmp(table_to_chaintype[i].name, table) == 0)
42 return table_to_chaintype[i].type;
43 }
44
45 return -1;
46}
47
48static int nft_compat_chain_validate_dependency(const char *tablename,
49 const struct nft_chain *chain)
50{
51 enum nft_chain_type type;
52 const struct nft_base_chain *basechain;
53
54 if (!tablename || !(chain->flags & NFT_BASE_CHAIN))
55 return 0;
56
57 type = nft_compat_table_to_chaintype(tablename);
58 if (type < 0)
59 return -EINVAL;
60
61 basechain = nft_base_chain(chain);
62 if (basechain->type->type != type)
63 return -EINVAL;
64
65 return 0;
66}
67
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +020068union nft_entry {
69 struct ipt_entry e4;
70 struct ip6t_entry e6;
71};
72
73static inline void
74nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info)
75{
76 par->target = xt;
77 par->targinfo = xt_info;
78 par->hotdrop = false;
79}
80
81static void nft_target_eval(const struct nft_expr *expr,
82 struct nft_data data[NFT_REG_MAX + 1],
83 const struct nft_pktinfo *pkt)
84{
85 void *info = nft_expr_priv(expr);
86 struct xt_target *target = expr->ops->data;
87 struct sk_buff *skb = pkt->skb;
88 int ret;
89
90 nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
91
92 ret = target->target(skb, &pkt->xt);
93
94 if (pkt->xt.hotdrop)
95 ret = NF_DROP;
96
97 switch(ret) {
98 case XT_CONTINUE:
99 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE;
100 break;
101 default:
102 data[NFT_REG_VERDICT].verdict = ret;
103 break;
104 }
105 return;
106}
107
108static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = {
109 [NFTA_TARGET_NAME] = { .type = NLA_NUL_STRING },
110 [NFTA_TARGET_REV] = { .type = NLA_U32 },
111 [NFTA_TARGET_INFO] = { .type = NLA_BINARY },
112};
113
114static void
115nft_target_set_tgchk_param(struct xt_tgchk_param *par,
116 const struct nft_ctx *ctx,
117 struct xt_target *target, void *info,
118 union nft_entry *entry, u8 proto, bool inv)
119{
120 par->net = &init_net;
121 par->table = ctx->table->name;
122 switch (ctx->afi->family) {
123 case AF_INET:
124 entry->e4.ip.proto = proto;
125 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
126 break;
127 case AF_INET6:
128 entry->e6.ipv6.proto = proto;
129 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
130 break;
131 }
132 par->entryinfo = entry;
133 par->target = target;
134 par->targinfo = info;
135 if (ctx->chain->flags & NFT_BASE_CHAIN) {
136 const struct nft_base_chain *basechain =
137 nft_base_chain(ctx->chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000138 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200139
140 par->hook_mask = 1 << ops->hooknum;
Pablo Neira Ayuso493618a2014-10-14 12:43:50 +0200141 } else {
142 par->hook_mask = 0;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200143 }
144 par->family = ctx->afi->family;
145}
146
147static void target_compat_from_user(struct xt_target *t, void *in, void *out)
148{
Pablo Neira Ayuso756c1b12014-06-17 21:18:44 +0200149 int pad;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200150
Pablo Neira Ayuso756c1b12014-06-17 21:18:44 +0200151 memcpy(out, in, t->targetsize);
152 pad = XT_ALIGN(t->targetsize) - t->targetsize;
153 if (pad > 0)
154 memset(out + t->targetsize, 0, pad);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200155}
156
157static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = {
158 [NFTA_RULE_COMPAT_PROTO] = { .type = NLA_U32 },
159 [NFTA_RULE_COMPAT_FLAGS] = { .type = NLA_U32 },
160};
161
Pablo Neira Ayuso8691a9a2013-11-16 22:16:47 +0100162static int nft_parse_compat(const struct nlattr *attr, u8 *proto, bool *inv)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200163{
164 struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1];
165 u32 flags;
166 int err;
167
168 err = nla_parse_nested(tb, NFTA_RULE_COMPAT_MAX, attr,
169 nft_rule_compat_policy);
170 if (err < 0)
171 return err;
172
173 if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
174 return -EINVAL;
175
176 flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
177 if (flags & ~NFT_RULE_COMPAT_F_MASK)
178 return -EINVAL;
179 if (flags & NFT_RULE_COMPAT_F_INV)
180 *inv = true;
181
Pablo Neira Ayuso8691a9a2013-11-16 22:16:47 +0100182 *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
183 return 0;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200184}
185
186static int
187nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
188 const struct nlattr * const tb[])
189{
190 void *info = nft_expr_priv(expr);
191 struct xt_target *target = expr->ops->data;
192 struct xt_tgchk_param par;
193 size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
194 u8 proto = 0;
195 bool inv = false;
196 union nft_entry e = {};
197 int ret;
198
Pablo Neira Ayusof3f5dde2014-10-14 10:13:48 +0200199 ret = nft_compat_chain_validate_dependency(target->table, ctx->chain);
200 if (ret < 0)
201 goto err;
202
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200203 target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
204
Pablo Neira Ayuso8691a9a2013-11-16 22:16:47 +0100205 if (ctx->nla[NFTA_RULE_COMPAT]) {
206 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
207 if (ret < 0)
208 goto err;
209 }
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200210
211 nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
212
213 ret = xt_check_target(&par, size, proto, inv);
214 if (ret < 0)
215 goto err;
216
217 /* The standard target cannot be used */
218 if (target->target == NULL) {
219 ret = -EINVAL;
220 goto err;
221 }
222
223 return 0;
224err:
225 module_put(target->me);
226 return ret;
227}
228
229static void
Patrick McHardy62472bc2014-03-07 19:08:30 +0100230nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200231{
232 struct xt_target *target = expr->ops->data;
Pablo Neira Ayuso3d9b1422014-06-11 14:27:46 +0200233 void *info = nft_expr_priv(expr);
234 struct xt_tgdtor_param par;
235
236 par.net = ctx->net;
237 par.target = target;
238 par.targinfo = info;
239 par.family = ctx->afi->family;
240 if (par.target->destroy != NULL)
241 par.target->destroy(&par);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200242
243 module_put(target->me);
244}
245
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200246static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
247{
248 const struct xt_target *target = expr->ops->data;
249 void *info = nft_expr_priv(expr);
250
251 if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
252 nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
Pablo Neira Ayuso756c1b12014-06-17 21:18:44 +0200253 nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(target->targetsize), info))
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200254 goto nla_put_failure;
255
256 return 0;
257
258nla_put_failure:
259 return -1;
260}
261
262static int nft_target_validate(const struct nft_ctx *ctx,
263 const struct nft_expr *expr,
264 const struct nft_data **data)
265{
266 struct xt_target *target = expr->ops->data;
267 unsigned int hook_mask = 0;
Pablo Neira Ayusof3f5dde2014-10-14 10:13:48 +0200268 int ret;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200269
270 if (ctx->chain->flags & NFT_BASE_CHAIN) {
271 const struct nft_base_chain *basechain =
272 nft_base_chain(ctx->chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000273 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200274
275 hook_mask = 1 << ops->hooknum;
Pablo Neira Ayusof3f5dde2014-10-14 10:13:48 +0200276 if (!(hook_mask & target->hooks))
277 return -EINVAL;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200278
Pablo Neira Ayusof3f5dde2014-10-14 10:13:48 +0200279 ret = nft_compat_chain_validate_dependency(target->table,
280 ctx->chain);
281 if (ret < 0)
282 return ret;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200283 }
284 return 0;
285}
286
287static void nft_match_eval(const struct nft_expr *expr,
288 struct nft_data data[NFT_REG_MAX + 1],
289 const struct nft_pktinfo *pkt)
290{
291 void *info = nft_expr_priv(expr);
292 struct xt_match *match = expr->ops->data;
293 struct sk_buff *skb = pkt->skb;
294 bool ret;
295
296 nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
297
298 ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
299
300 if (pkt->xt.hotdrop) {
301 data[NFT_REG_VERDICT].verdict = NF_DROP;
302 return;
303 }
304
305 switch(ret) {
306 case true:
307 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE;
308 break;
309 case false:
310 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
311 break;
312 }
313}
314
315static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
316 [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING },
317 [NFTA_MATCH_REV] = { .type = NLA_U32 },
318 [NFTA_MATCH_INFO] = { .type = NLA_BINARY },
319};
320
321/* struct xt_mtchk_param and xt_tgchk_param look very similar */
322static void
323nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
324 struct xt_match *match, void *info,
325 union nft_entry *entry, u8 proto, bool inv)
326{
327 par->net = &init_net;
328 par->table = ctx->table->name;
329 switch (ctx->afi->family) {
330 case AF_INET:
331 entry->e4.ip.proto = proto;
332 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
333 break;
334 case AF_INET6:
335 entry->e6.ipv6.proto = proto;
336 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
337 break;
338 }
339 par->entryinfo = entry;
340 par->match = match;
341 par->matchinfo = info;
342 if (ctx->chain->flags & NFT_BASE_CHAIN) {
343 const struct nft_base_chain *basechain =
344 nft_base_chain(ctx->chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000345 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200346
347 par->hook_mask = 1 << ops->hooknum;
Pablo Neira Ayuso493618a2014-10-14 12:43:50 +0200348 } else {
349 par->hook_mask = 0;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200350 }
351 par->family = ctx->afi->family;
352}
353
354static void match_compat_from_user(struct xt_match *m, void *in, void *out)
355{
Pablo Neira Ayuso756c1b12014-06-17 21:18:44 +0200356 int pad;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200357
Pablo Neira Ayuso756c1b12014-06-17 21:18:44 +0200358 memcpy(out, in, m->matchsize);
359 pad = XT_ALIGN(m->matchsize) - m->matchsize;
360 if (pad > 0)
361 memset(out + m->matchsize, 0, pad);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200362}
363
364static int
365nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
366 const struct nlattr * const tb[])
367{
368 void *info = nft_expr_priv(expr);
369 struct xt_match *match = expr->ops->data;
370 struct xt_mtchk_param par;
371 size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
372 u8 proto = 0;
373 bool inv = false;
374 union nft_entry e = {};
375 int ret;
376
Pablo Neira Ayusof3f5dde2014-10-14 10:13:48 +0200377 ret = nft_compat_chain_validate_dependency(match->name, ctx->chain);
378 if (ret < 0)
379 goto err;
380
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200381 match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
382
Pablo Neira Ayuso8691a9a2013-11-16 22:16:47 +0100383 if (ctx->nla[NFTA_RULE_COMPAT]) {
384 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
385 if (ret < 0)
386 goto err;
387 }
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200388
389 nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
390
391 ret = xt_check_match(&par, size, proto, inv);
392 if (ret < 0)
393 goto err;
394
395 return 0;
396err:
397 module_put(match->me);
398 return ret;
399}
400
401static void
Patrick McHardy62472bc2014-03-07 19:08:30 +0100402nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200403{
404 struct xt_match *match = expr->ops->data;
Pablo Neira Ayuso3d9b1422014-06-11 14:27:46 +0200405 void *info = nft_expr_priv(expr);
406 struct xt_mtdtor_param par;
407
408 par.net = ctx->net;
409 par.match = match;
410 par.matchinfo = info;
411 par.family = ctx->afi->family;
412 if (par.match->destroy != NULL)
413 par.match->destroy(&par);
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200414
415 module_put(match->me);
416}
417
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200418static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
419{
420 void *info = nft_expr_priv(expr);
421 struct xt_match *match = expr->ops->data;
422
423 if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
424 nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
Pablo Neira Ayuso756c1b12014-06-17 21:18:44 +0200425 nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(match->matchsize), info))
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200426 goto nla_put_failure;
427
428 return 0;
429
430nla_put_failure:
431 return -1;
432}
433
434static int nft_match_validate(const struct nft_ctx *ctx,
435 const struct nft_expr *expr,
436 const struct nft_data **data)
437{
438 struct xt_match *match = expr->ops->data;
439 unsigned int hook_mask = 0;
Pablo Neira Ayusof3f5dde2014-10-14 10:13:48 +0200440 int ret;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200441
442 if (ctx->chain->flags & NFT_BASE_CHAIN) {
443 const struct nft_base_chain *basechain =
444 nft_base_chain(ctx->chain);
Patrick McHardy115a60b2014-01-03 12:16:15 +0000445 const struct nf_hook_ops *ops = &basechain->ops[0];
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200446
447 hook_mask = 1 << ops->hooknum;
Pablo Neira Ayusof3f5dde2014-10-14 10:13:48 +0200448 if (!(hook_mask & match->hooks))
449 return -EINVAL;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200450
Pablo Neira Ayusof3f5dde2014-10-14 10:13:48 +0200451 ret = nft_compat_chain_validate_dependency(match->name,
452 ctx->chain);
453 if (ret < 0)
454 return ret;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200455 }
456 return 0;
457}
458
459static int
460nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
461 int event, u16 family, const char *name,
462 int rev, int target)
463{
464 struct nlmsghdr *nlh;
465 struct nfgenmsg *nfmsg;
466 unsigned int flags = portid ? NLM_F_MULTI : 0;
467
468 event |= NFNL_SUBSYS_NFT_COMPAT << 8;
469 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
470 if (nlh == NULL)
471 goto nlmsg_failure;
472
473 nfmsg = nlmsg_data(nlh);
474 nfmsg->nfgen_family = family;
475 nfmsg->version = NFNETLINK_V0;
476 nfmsg->res_id = 0;
477
478 if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
479 nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
480 nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
481 goto nla_put_failure;
482
483 nlmsg_end(skb, nlh);
484 return skb->len;
485
486nlmsg_failure:
487nla_put_failure:
488 nlmsg_cancel(skb, nlh);
489 return -1;
490}
491
492static int
493nfnl_compat_get(struct sock *nfnl, struct sk_buff *skb,
494 const struct nlmsghdr *nlh, const struct nlattr * const tb[])
495{
496 int ret = 0, target;
497 struct nfgenmsg *nfmsg;
498 const char *fmt;
499 const char *name;
500 u32 rev;
501 struct sk_buff *skb2;
502
503 if (tb[NFTA_COMPAT_NAME] == NULL ||
504 tb[NFTA_COMPAT_REV] == NULL ||
505 tb[NFTA_COMPAT_TYPE] == NULL)
506 return -EINVAL;
507
508 name = nla_data(tb[NFTA_COMPAT_NAME]);
509 rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
510 target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
511
512 nfmsg = nlmsg_data(nlh);
513
514 switch(nfmsg->nfgen_family) {
515 case AF_INET:
516 fmt = "ipt_%s";
517 break;
518 case AF_INET6:
519 fmt = "ip6t_%s";
520 break;
521 default:
522 pr_err("nft_compat: unsupported protocol %d\n",
523 nfmsg->nfgen_family);
524 return -EINVAL;
525 }
526
527 try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
528 rev, target, &ret),
529 fmt, name);
530
531 if (ret < 0)
532 return ret;
533
534 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
535 if (skb2 == NULL)
536 return -ENOMEM;
537
538 /* include the best revision for this extension in the message */
539 if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
540 nlh->nlmsg_seq,
541 NFNL_MSG_TYPE(nlh->nlmsg_type),
542 NFNL_MSG_COMPAT_GET,
543 nfmsg->nfgen_family,
544 name, ret, target) <= 0) {
545 kfree_skb(skb2);
546 return -ENOSPC;
547 }
548
549 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
550 MSG_DONTWAIT);
551 if (ret > 0)
552 ret = 0;
553
554 return ret == -EAGAIN ? -ENOBUFS : ret;
555}
556
557static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
558 [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
559 .len = NFT_COMPAT_NAME_MAX-1 },
560 [NFTA_COMPAT_REV] = { .type = NLA_U32 },
561 [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
562};
563
564static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
565 [NFNL_MSG_COMPAT_GET] = { .call = nfnl_compat_get,
566 .attr_count = NFTA_COMPAT_MAX,
567 .policy = nfnl_compat_policy_get },
568};
569
570static const struct nfnetlink_subsystem nfnl_compat_subsys = {
571 .name = "nft-compat",
572 .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
573 .cb_count = NFNL_MSG_COMPAT_MAX,
574 .cb = nfnl_nft_compat_cb,
575};
576
577static LIST_HEAD(nft_match_list);
578
579struct nft_xt {
580 struct list_head head;
581 struct nft_expr_ops ops;
582};
583
584static struct nft_expr_type nft_match_type;
585
586static const struct nft_expr_ops *
587nft_match_select_ops(const struct nft_ctx *ctx,
588 const struct nlattr * const tb[])
589{
590 struct nft_xt *nft_match;
591 struct xt_match *match;
592 char *mt_name;
593 __u32 rev, family;
594
595 if (tb[NFTA_MATCH_NAME] == NULL ||
596 tb[NFTA_MATCH_REV] == NULL ||
597 tb[NFTA_MATCH_INFO] == NULL)
598 return ERR_PTR(-EINVAL);
599
600 mt_name = nla_data(tb[NFTA_MATCH_NAME]);
601 rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
602 family = ctx->afi->family;
603
604 /* Re-use the existing match if it's already loaded. */
605 list_for_each_entry(nft_match, &nft_match_list, head) {
606 struct xt_match *match = nft_match->ops.data;
607
608 if (strcmp(match->name, mt_name) == 0 &&
609 match->revision == rev && match->family == family)
610 return &nft_match->ops;
611 }
612
613 match = xt_request_find_match(family, mt_name, rev);
614 if (IS_ERR(match))
615 return ERR_PTR(-ENOENT);
616
617 /* This is the first time we use this match, allocate operations */
618 nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
619 if (nft_match == NULL)
620 return ERR_PTR(-ENOMEM);
621
622 nft_match->ops.type = &nft_match_type;
Pablo Neira Ayuso756c1b12014-06-17 21:18:44 +0200623 nft_match->ops.size = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200624 nft_match->ops.eval = nft_match_eval;
625 nft_match->ops.init = nft_match_init;
626 nft_match->ops.destroy = nft_match_destroy;
627 nft_match->ops.dump = nft_match_dump;
628 nft_match->ops.validate = nft_match_validate;
629 nft_match->ops.data = match;
630
631 list_add(&nft_match->head, &nft_match_list);
632
633 return &nft_match->ops;
634}
635
636static void nft_match_release(void)
637{
Dan Carpenterc359c412013-11-04 15:58:56 +0300638 struct nft_xt *nft_match, *tmp;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200639
Dan Carpenterc359c412013-11-04 15:58:56 +0300640 list_for_each_entry_safe(nft_match, tmp, &nft_match_list, head)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200641 kfree(nft_match);
642}
643
644static struct nft_expr_type nft_match_type __read_mostly = {
645 .name = "match",
646 .select_ops = nft_match_select_ops,
647 .policy = nft_match_policy,
648 .maxattr = NFTA_MATCH_MAX,
649 .owner = THIS_MODULE,
650};
651
652static LIST_HEAD(nft_target_list);
653
654static struct nft_expr_type nft_target_type;
655
656static const struct nft_expr_ops *
657nft_target_select_ops(const struct nft_ctx *ctx,
658 const struct nlattr * const tb[])
659{
660 struct nft_xt *nft_target;
661 struct xt_target *target;
662 char *tg_name;
663 __u32 rev, family;
664
665 if (tb[NFTA_TARGET_NAME] == NULL ||
666 tb[NFTA_TARGET_REV] == NULL ||
667 tb[NFTA_TARGET_INFO] == NULL)
668 return ERR_PTR(-EINVAL);
669
670 tg_name = nla_data(tb[NFTA_TARGET_NAME]);
671 rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
672 family = ctx->afi->family;
673
674 /* Re-use the existing target if it's already loaded. */
675 list_for_each_entry(nft_target, &nft_match_list, head) {
676 struct xt_target *target = nft_target->ops.data;
677
678 if (strcmp(target->name, tg_name) == 0 &&
679 target->revision == rev && target->family == family)
680 return &nft_target->ops;
681 }
682
683 target = xt_request_find_target(family, tg_name, rev);
684 if (IS_ERR(target))
685 return ERR_PTR(-ENOENT);
686
687 /* This is the first time we use this target, allocate operations */
688 nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
689 if (nft_target == NULL)
690 return ERR_PTR(-ENOMEM);
691
692 nft_target->ops.type = &nft_target_type;
Pablo Neira Ayuso756c1b12014-06-17 21:18:44 +0200693 nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200694 nft_target->ops.eval = nft_target_eval;
695 nft_target->ops.init = nft_target_init;
696 nft_target->ops.destroy = nft_target_destroy;
697 nft_target->ops.dump = nft_target_dump;
698 nft_target->ops.validate = nft_target_validate;
699 nft_target->ops.data = target;
700
701 list_add(&nft_target->head, &nft_target_list);
702
703 return &nft_target->ops;
704}
705
706static void nft_target_release(void)
707{
Dan Carpenterc359c412013-11-04 15:58:56 +0300708 struct nft_xt *nft_target, *tmp;
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200709
Dan Carpenterc359c412013-11-04 15:58:56 +0300710 list_for_each_entry_safe(nft_target, tmp, &nft_target_list, head)
Pablo Neira Ayuso0ca743a2013-10-14 00:06:06 +0200711 kfree(nft_target);
712}
713
714static struct nft_expr_type nft_target_type __read_mostly = {
715 .name = "target",
716 .select_ops = nft_target_select_ops,
717 .policy = nft_target_policy,
718 .maxattr = NFTA_TARGET_MAX,
719 .owner = THIS_MODULE,
720};
721
722static int __init nft_compat_module_init(void)
723{
724 int ret;
725
726 ret = nft_register_expr(&nft_match_type);
727 if (ret < 0)
728 return ret;
729
730 ret = nft_register_expr(&nft_target_type);
731 if (ret < 0)
732 goto err_match;
733
734 ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
735 if (ret < 0) {
736 pr_err("nft_compat: cannot register with nfnetlink.\n");
737 goto err_target;
738 }
739
740 pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n");
741
742 return ret;
743
744err_target:
745 nft_unregister_expr(&nft_target_type);
746err_match:
747 nft_unregister_expr(&nft_match_type);
748 return ret;
749}
750
751static void __exit nft_compat_module_exit(void)
752{
753 nfnetlink_subsys_unregister(&nfnl_compat_subsys);
754 nft_unregister_expr(&nft_target_type);
755 nft_unregister_expr(&nft_match_type);
756 nft_match_release();
757 nft_target_release();
758}
759
760MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
761
762module_init(nft_compat_module_init);
763module_exit(nft_compat_module_exit);
764
765MODULE_LICENSE("GPL");
766MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
767MODULE_ALIAS_NFT_EXPR("match");
768MODULE_ALIAS_NFT_EXPR("target");