]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/netfilter/nft_compat.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500
[thirdparty/kernel/stable.git] / net / netfilter / nft_compat.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
0ca743a5
PNA
2/*
3 * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
4 *
0ca743a5
PNA
5 * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
6 */
7
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/netlink.h>
12#include <linux/netfilter.h>
13#include <linux/netfilter/nfnetlink.h>
14#include <linux/netfilter/nf_tables.h>
15#include <linux/netfilter/nf_tables_compat.h>
16#include <linux/netfilter/x_tables.h>
17#include <linux/netfilter_ipv4/ip_tables.h>
18#include <linux/netfilter_ipv6/ip6_tables.h>
5191f4d8 19#include <linux/netfilter_bridge/ebtables.h>
5f158939 20#include <linux/netfilter_arp/arp_tables.h>
0ca743a5 21#include <net/netfilter/nf_tables.h>
4b512e1c 22
732a8049
FW
23/* Used for matches where *info is larger than X byte */
24#define NFT_MATCH_LARGE_THRESH 192
25
26struct nft_xt_match_priv {
27 void *info;
28};
29
e4844c9c
FW
30static int nft_compat_chain_validate_dependency(const struct nft_ctx *ctx,
31 const char *tablename)
f3f5dded 32{
e4844c9c
FW
33 enum nft_chain_types type = NFT_CHAIN_T_DEFAULT;
34 const struct nft_chain *chain = ctx->chain;
f3f5dded
PNA
35 const struct nft_base_chain *basechain;
36
f323d954
PNA
37 if (!tablename ||
38 !nft_is_base_chain(chain))
f3f5dded
PNA
39 return 0;
40
f3f5dded 41 basechain = nft_base_chain(chain);
e4844c9c
FW
42 if (strcmp(tablename, "nat") == 0) {
43 if (ctx->family != NFPROTO_BRIDGE)
44 type = NFT_CHAIN_T_NAT;
45 if (basechain->type->type != type)
46 return -EINVAL;
47 }
f3f5dded
PNA
48
49 return 0;
50}
51
0ca743a5
PNA
52union nft_entry {
53 struct ipt_entry e4;
54 struct ip6t_entry e6;
5191f4d8 55 struct ebt_entry ebt;
5f158939 56 struct arpt_entry arp;
0ca743a5
PNA
57};
58
59static inline void
60nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info)
61{
62 par->target = xt;
63 par->targinfo = xt_info;
64 par->hotdrop = false;
65}
66
5191f4d8 67static void nft_target_eval_xt(const struct nft_expr *expr,
a55e22e9 68 struct nft_regs *regs,
5191f4d8 69 const struct nft_pktinfo *pkt)
0ca743a5
PNA
70{
71 void *info = nft_expr_priv(expr);
72 struct xt_target *target = expr->ops->data;
73 struct sk_buff *skb = pkt->skb;
74 int ret;
75
76 nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
77
78 ret = target->target(skb, &pkt->xt);
79
80 if (pkt->xt.hotdrop)
81 ret = NF_DROP;
82
5191f4d8 83 switch (ret) {
0ca743a5 84 case XT_CONTINUE:
a55e22e9 85 regs->verdict.code = NFT_CONTINUE;
0ca743a5
PNA
86 break;
87 default:
a55e22e9 88 regs->verdict.code = ret;
0ca743a5
PNA
89 break;
90 }
5191f4d8
AB
91}
92
93static void nft_target_eval_bridge(const struct nft_expr *expr,
a55e22e9 94 struct nft_regs *regs,
5191f4d8
AB
95 const struct nft_pktinfo *pkt)
96{
97 void *info = nft_expr_priv(expr);
98 struct xt_target *target = expr->ops->data;
99 struct sk_buff *skb = pkt->skb;
100 int ret;
101
102 nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
103
104 ret = target->target(skb, &pkt->xt);
105
106 if (pkt->xt.hotdrop)
107 ret = NF_DROP;
108
109 switch (ret) {
110 case EBT_ACCEPT:
a55e22e9 111 regs->verdict.code = NF_ACCEPT;
5191f4d8
AB
112 break;
113 case EBT_DROP:
a55e22e9 114 regs->verdict.code = NF_DROP;
5191f4d8
AB
115 break;
116 case EBT_CONTINUE:
a55e22e9 117 regs->verdict.code = NFT_CONTINUE;
5191f4d8
AB
118 break;
119 case EBT_RETURN:
a55e22e9 120 regs->verdict.code = NFT_RETURN;
5191f4d8
AB
121 break;
122 default:
a55e22e9 123 regs->verdict.code = ret;
5191f4d8
AB
124 break;
125 }
0ca743a5
PNA
126}
127
128static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = {
129 [NFTA_TARGET_NAME] = { .type = NLA_NUL_STRING },
130 [NFTA_TARGET_REV] = { .type = NLA_U32 },
131 [NFTA_TARGET_INFO] = { .type = NLA_BINARY },
132};
133
134static void
135nft_target_set_tgchk_param(struct xt_tgchk_param *par,
136 const struct nft_ctx *ctx,
137 struct xt_target *target, void *info,
2156d321 138 union nft_entry *entry, u16 proto, bool inv)
0ca743a5 139{
2daf1b4d 140 par->net = ctx->net;
0ca743a5 141 par->table = ctx->table->name;
36596dad 142 switch (ctx->family) {
0ca743a5
PNA
143 case AF_INET:
144 entry->e4.ip.proto = proto;
145 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
146 break;
147 case AF_INET6:
749177cc
PNA
148 if (proto)
149 entry->e6.ipv6.flags |= IP6T_F_PROTO;
150
0ca743a5
PNA
151 entry->e6.ipv6.proto = proto;
152 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
153 break;
5191f4d8 154 case NFPROTO_BRIDGE:
2156d321 155 entry->ebt.ethproto = (__force __be16)proto;
5191f4d8
AB
156 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
157 break;
5f158939
AB
158 case NFPROTO_ARP:
159 break;
0ca743a5
PNA
160 }
161 par->entryinfo = entry;
162 par->target = target;
163 par->targinfo = info;
f323d954 164 if (nft_is_base_chain(ctx->chain)) {
0ca743a5
PNA
165 const struct nft_base_chain *basechain =
166 nft_base_chain(ctx->chain);
c974a3a3 167 const struct nf_hook_ops *ops = &basechain->ops;
0ca743a5
PNA
168
169 par->hook_mask = 1 << ops->hooknum;
493618a9
PNA
170 } else {
171 par->hook_mask = 0;
0ca743a5 172 }
36596dad 173 par->family = ctx->family;
55917a21 174 par->nft_compat = true;
0ca743a5
PNA
175}
176
177static void target_compat_from_user(struct xt_target *t, void *in, void *out)
178{
756c1b1a 179 int pad;
0ca743a5 180
756c1b1a
PNA
181 memcpy(out, in, t->targetsize);
182 pad = XT_ALIGN(t->targetsize) - t->targetsize;
183 if (pad > 0)
184 memset(out + t->targetsize, 0, pad);
0ca743a5
PNA
185}
186
187static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = {
188 [NFTA_RULE_COMPAT_PROTO] = { .type = NLA_U32 },
189 [NFTA_RULE_COMPAT_FLAGS] = { .type = NLA_U32 },
190};
191
2156d321 192static int nft_parse_compat(const struct nlattr *attr, u16 *proto, bool *inv)
0ca743a5
PNA
193{
194 struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1];
195 u32 flags;
196 int err;
197
8cb08174
JB
198 err = nla_parse_nested_deprecated(tb, NFTA_RULE_COMPAT_MAX, attr,
199 nft_rule_compat_policy, NULL);
0ca743a5
PNA
200 if (err < 0)
201 return err;
202
203 if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
204 return -EINVAL;
205
206 flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
207 if (flags & ~NFT_RULE_COMPAT_F_MASK)
208 return -EINVAL;
209 if (flags & NFT_RULE_COMPAT_F_INV)
210 *inv = true;
211
8691a9a3
PNA
212 *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
213 return 0;
0ca743a5
PNA
214}
215
216static int
217nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
218 const struct nlattr * const tb[])
219{
220 void *info = nft_expr_priv(expr);
221 struct xt_target *target = expr->ops->data;
222 struct xt_tgchk_param par;
223 size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
2156d321 224 u16 proto = 0;
0ca743a5
PNA
225 bool inv = false;
226 union nft_entry e = {};
227 int ret;
228
229 target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
230
8691a9a3
PNA
231 if (ctx->nla[NFTA_RULE_COMPAT]) {
232 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
233 if (ret < 0)
b8e9dc1c 234 return ret;
8691a9a3 235 }
0ca743a5
PNA
236
237 nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
238
239 ret = xt_check_target(&par, size, proto, inv);
240 if (ret < 0)
b8e9dc1c 241 return ret;
0ca743a5
PNA
242
243 /* The standard target cannot be used */
b8e9dc1c
FW
244 if (!target->target)
245 return -EINVAL;
0ca743a5
PNA
246
247 return 0;
0ca743a5
PNA
248}
249
250static void
62472bce 251nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
0ca743a5
PNA
252{
253 struct xt_target *target = expr->ops->data;
3d9b1421 254 void *info = nft_expr_priv(expr);
753c111f 255 struct module *me = target->me;
3d9b1421
PNA
256 struct xt_tgdtor_param par;
257
258 par.net = ctx->net;
259 par.target = target;
260 par.targinfo = info;
36596dad 261 par.family = ctx->family;
3d9b1421
PNA
262 if (par.target->destroy != NULL)
263 par.target->destroy(&par);
0ca743a5 264
b8e20400
PNA
265 module_put(me);
266 kfree(expr->ops);
0ca743a5
PNA
267}
268
d701d811
PNA
269static int nft_extension_dump_info(struct sk_buff *skb, int attr,
270 const void *info,
271 unsigned int size, unsigned int user_size)
272{
273 unsigned int info_size, aligned_size = XT_ALIGN(size);
274 struct nlattr *nla;
275
276 nla = nla_reserve(skb, attr, aligned_size);
277 if (!nla)
278 return -1;
279
280 info_size = user_size ? : size;
281 memcpy(nla_data(nla), info, info_size);
282 memset(nla_data(nla) + info_size, 0, aligned_size - info_size);
283
284 return 0;
285}
286
0ca743a5
PNA
287static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
288{
289 const struct xt_target *target = expr->ops->data;
290 void *info = nft_expr_priv(expr);
291
292 if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
293 nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
d701d811
PNA
294 nft_extension_dump_info(skb, NFTA_TARGET_INFO, info,
295 target->targetsize, target->usersize))
0ca743a5
PNA
296 goto nla_put_failure;
297
298 return 0;
299
300nla_put_failure:
301 return -1;
302}
303
304static int nft_target_validate(const struct nft_ctx *ctx,
305 const struct nft_expr *expr,
306 const struct nft_data **data)
307{
308 struct xt_target *target = expr->ops->data;
309 unsigned int hook_mask = 0;
f3f5dded 310 int ret;
0ca743a5 311
f323d954 312 if (nft_is_base_chain(ctx->chain)) {
0ca743a5
PNA
313 const struct nft_base_chain *basechain =
314 nft_base_chain(ctx->chain);
c974a3a3 315 const struct nf_hook_ops *ops = &basechain->ops;
0ca743a5
PNA
316
317 hook_mask = 1 << ops->hooknum;
f7fb77fc 318 if (target->hooks && !(hook_mask & target->hooks))
f3f5dded 319 return -EINVAL;
0ca743a5 320
e4844c9c 321 ret = nft_compat_chain_validate_dependency(ctx, target->table);
f3f5dded
PNA
322 if (ret < 0)
323 return ret;
0ca743a5
PNA
324 }
325 return 0;
326}
327
8bdf1647
FW
328static void __nft_match_eval(const struct nft_expr *expr,
329 struct nft_regs *regs,
330 const struct nft_pktinfo *pkt,
331 void *info)
0ca743a5 332{
0ca743a5
PNA
333 struct xt_match *match = expr->ops->data;
334 struct sk_buff *skb = pkt->skb;
335 bool ret;
336
337 nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
338
339 ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
340
341 if (pkt->xt.hotdrop) {
a55e22e9 342 regs->verdict.code = NF_DROP;
0ca743a5
PNA
343 return;
344 }
345
c1f86676
DM
346 switch (ret ? 1 : 0) {
347 case 1:
a55e22e9 348 regs->verdict.code = NFT_CONTINUE;
0ca743a5 349 break;
c1f86676 350 case 0:
a55e22e9 351 regs->verdict.code = NFT_BREAK;
0ca743a5
PNA
352 break;
353 }
354}
355
732a8049
FW
356static void nft_match_large_eval(const struct nft_expr *expr,
357 struct nft_regs *regs,
358 const struct nft_pktinfo *pkt)
359{
360 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
361
362 __nft_match_eval(expr, regs, pkt, priv->info);
363}
364
8bdf1647
FW
365static void nft_match_eval(const struct nft_expr *expr,
366 struct nft_regs *regs,
367 const struct nft_pktinfo *pkt)
368{
369 __nft_match_eval(expr, regs, pkt, nft_expr_priv(expr));
370}
371
0ca743a5
PNA
372static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
373 [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING },
374 [NFTA_MATCH_REV] = { .type = NLA_U32 },
375 [NFTA_MATCH_INFO] = { .type = NLA_BINARY },
376};
377
378/* struct xt_mtchk_param and xt_tgchk_param look very similar */
379static void
380nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
381 struct xt_match *match, void *info,
2156d321 382 union nft_entry *entry, u16 proto, bool inv)
0ca743a5 383{
2daf1b4d 384 par->net = ctx->net;
0ca743a5 385 par->table = ctx->table->name;
36596dad 386 switch (ctx->family) {
0ca743a5
PNA
387 case AF_INET:
388 entry->e4.ip.proto = proto;
389 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
390 break;
391 case AF_INET6:
749177cc
PNA
392 if (proto)
393 entry->e6.ipv6.flags |= IP6T_F_PROTO;
394
0ca743a5
PNA
395 entry->e6.ipv6.proto = proto;
396 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
397 break;
5191f4d8 398 case NFPROTO_BRIDGE:
2156d321 399 entry->ebt.ethproto = (__force __be16)proto;
5191f4d8
AB
400 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
401 break;
5f158939
AB
402 case NFPROTO_ARP:
403 break;
0ca743a5
PNA
404 }
405 par->entryinfo = entry;
406 par->match = match;
407 par->matchinfo = info;
f323d954 408 if (nft_is_base_chain(ctx->chain)) {
0ca743a5
PNA
409 const struct nft_base_chain *basechain =
410 nft_base_chain(ctx->chain);
c974a3a3 411 const struct nf_hook_ops *ops = &basechain->ops;
0ca743a5
PNA
412
413 par->hook_mask = 1 << ops->hooknum;
493618a9
PNA
414 } else {
415 par->hook_mask = 0;
0ca743a5 416 }
36596dad 417 par->family = ctx->family;
55917a21 418 par->nft_compat = true;
0ca743a5
PNA
419}
420
421static void match_compat_from_user(struct xt_match *m, void *in, void *out)
422{
756c1b1a
PNA
423 int pad;
424
425 memcpy(out, in, m->matchsize);
426 pad = XT_ALIGN(m->matchsize) - m->matchsize;
427 if (pad > 0)
428 memset(out + m->matchsize, 0, pad);
0ca743a5
PNA
429}
430
431static int
8bdf1647
FW
432__nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
433 const struct nlattr * const tb[],
434 void *info)
0ca743a5 435{
0ca743a5
PNA
436 struct xt_match *match = expr->ops->data;
437 struct xt_mtchk_param par;
438 size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
2156d321 439 u16 proto = 0;
0ca743a5
PNA
440 bool inv = false;
441 union nft_entry e = {};
442 int ret;
443
444 match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
445
8691a9a3
PNA
446 if (ctx->nla[NFTA_RULE_COMPAT]) {
447 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
448 if (ret < 0)
b8e9dc1c 449 return ret;
8691a9a3 450 }
0ca743a5
PNA
451
452 nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
453
b8e20400 454 return xt_check_match(&par, size, proto, inv);
0ca743a5
PNA
455}
456
8bdf1647
FW
457static int
458nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
459 const struct nlattr * const tb[])
460{
461 return __nft_match_init(ctx, expr, tb, nft_expr_priv(expr));
462}
463
732a8049
FW
464static int
465nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
466 const struct nlattr * const tb[])
467{
468 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
469 struct xt_match *m = expr->ops->data;
470 int ret;
471
472 priv->info = kmalloc(XT_ALIGN(m->matchsize), GFP_KERNEL);
473 if (!priv->info)
474 return -ENOMEM;
475
476 ret = __nft_match_init(ctx, expr, tb, priv->info);
477 if (ret)
478 kfree(priv->info);
479 return ret;
480}
481
0ca743a5 482static void
8bdf1647
FW
483__nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr,
484 void *info)
0ca743a5
PNA
485{
486 struct xt_match *match = expr->ops->data;
29e38801 487 struct module *me = match->me;
3d9b1421
PNA
488 struct xt_mtdtor_param par;
489
490 par.net = ctx->net;
491 par.match = match;
492 par.matchinfo = info;
36596dad 493 par.family = ctx->family;
3d9b1421
PNA
494 if (par.match->destroy != NULL)
495 par.match->destroy(&par);
0ca743a5 496
b8e20400
PNA
497 module_put(me);
498 kfree(expr->ops);
0ca743a5
PNA
499}
500
8bdf1647
FW
501static void
502nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
503{
504 __nft_match_destroy(ctx, expr, nft_expr_priv(expr));
505}
506
732a8049
FW
507static void
508nft_match_large_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
509{
510 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
511
512 __nft_match_destroy(ctx, expr, priv->info);
513 kfree(priv->info);
514}
515
8bdf1647
FW
516static int __nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr,
517 void *info)
0ca743a5 518{
0ca743a5
PNA
519 struct xt_match *match = expr->ops->data;
520
521 if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
522 nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
d701d811
PNA
523 nft_extension_dump_info(skb, NFTA_MATCH_INFO, info,
524 match->matchsize, match->usersize))
0ca743a5
PNA
525 goto nla_put_failure;
526
527 return 0;
528
529nla_put_failure:
530 return -1;
531}
532
8bdf1647
FW
533static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
534{
535 return __nft_match_dump(skb, expr, nft_expr_priv(expr));
536}
537
732a8049
FW
538static int nft_match_large_dump(struct sk_buff *skb, const struct nft_expr *e)
539{
540 struct nft_xt_match_priv *priv = nft_expr_priv(e);
541
542 return __nft_match_dump(skb, e, priv->info);
543}
544
0ca743a5
PNA
545static int nft_match_validate(const struct nft_ctx *ctx,
546 const struct nft_expr *expr,
547 const struct nft_data **data)
548{
549 struct xt_match *match = expr->ops->data;
550 unsigned int hook_mask = 0;
f3f5dded 551 int ret;
0ca743a5 552
f323d954 553 if (nft_is_base_chain(ctx->chain)) {
0ca743a5
PNA
554 const struct nft_base_chain *basechain =
555 nft_base_chain(ctx->chain);
c974a3a3 556 const struct nf_hook_ops *ops = &basechain->ops;
0ca743a5
PNA
557
558 hook_mask = 1 << ops->hooknum;
f7fb77fc 559 if (match->hooks && !(hook_mask & match->hooks))
f3f5dded 560 return -EINVAL;
0ca743a5 561
e4844c9c 562 ret = nft_compat_chain_validate_dependency(ctx, match->table);
f3f5dded
PNA
563 if (ret < 0)
564 return ret;
0ca743a5
PNA
565 }
566 return 0;
567}
568
569static int
570nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
571 int event, u16 family, const char *name,
572 int rev, int target)
573{
574 struct nlmsghdr *nlh;
575 struct nfgenmsg *nfmsg;
576 unsigned int flags = portid ? NLM_F_MULTI : 0;
577
dedb67c4 578 event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event);
0ca743a5
PNA
579 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
580 if (nlh == NULL)
581 goto nlmsg_failure;
582
583 nfmsg = nlmsg_data(nlh);
584 nfmsg->nfgen_family = family;
585 nfmsg->version = NFNETLINK_V0;
586 nfmsg->res_id = 0;
587
588 if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
589 nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
590 nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
591 goto nla_put_failure;
592
593 nlmsg_end(skb, nlh);
594 return skb->len;
595
596nlmsg_failure:
597nla_put_failure:
598 nlmsg_cancel(skb, nlh);
599 return -1;
600}
601
eb1fb147
FW
602static int nfnl_compat_get_rcu(struct net *net, struct sock *nfnl,
603 struct sk_buff *skb, const struct nlmsghdr *nlh,
604 const struct nlattr * const tb[],
605 struct netlink_ext_ack *extack)
0ca743a5
PNA
606{
607 int ret = 0, target;
608 struct nfgenmsg *nfmsg;
609 const char *fmt;
610 const char *name;
611 u32 rev;
612 struct sk_buff *skb2;
613
614 if (tb[NFTA_COMPAT_NAME] == NULL ||
615 tb[NFTA_COMPAT_REV] == NULL ||
616 tb[NFTA_COMPAT_TYPE] == NULL)
617 return -EINVAL;
618
619 name = nla_data(tb[NFTA_COMPAT_NAME]);
620 rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
621 target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
622
623 nfmsg = nlmsg_data(nlh);
624
625 switch(nfmsg->nfgen_family) {
626 case AF_INET:
627 fmt = "ipt_%s";
628 break;
629 case AF_INET6:
630 fmt = "ip6t_%s";
631 break;
5191f4d8
AB
632 case NFPROTO_BRIDGE:
633 fmt = "ebt_%s";
634 break;
5f158939
AB
635 case NFPROTO_ARP:
636 fmt = "arpt_%s";
637 break;
0ca743a5
PNA
638 default:
639 pr_err("nft_compat: unsupported protocol %d\n",
640 nfmsg->nfgen_family);
641 return -EINVAL;
642 }
643
eb1fb147
FW
644 if (!try_module_get(THIS_MODULE))
645 return -EINVAL;
646
647 rcu_read_unlock();
0ca743a5
PNA
648 try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
649 rev, target, &ret),
650 fmt, name);
0ca743a5 651 if (ret < 0)
eb1fb147 652 goto out_put;
0ca743a5
PNA
653
654 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
eb1fb147
FW
655 if (skb2 == NULL) {
656 ret = -ENOMEM;
657 goto out_put;
658 }
0ca743a5
PNA
659
660 /* include the best revision for this extension in the message */
661 if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
662 nlh->nlmsg_seq,
663 NFNL_MSG_TYPE(nlh->nlmsg_type),
664 NFNL_MSG_COMPAT_GET,
665 nfmsg->nfgen_family,
666 name, ret, target) <= 0) {
667 kfree_skb(skb2);
eb1fb147 668 goto out_put;
0ca743a5
PNA
669 }
670
671 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
672 MSG_DONTWAIT);
673 if (ret > 0)
674 ret = 0;
eb1fb147
FW
675out_put:
676 rcu_read_lock();
677 module_put(THIS_MODULE);
0ca743a5
PNA
678 return ret == -EAGAIN ? -ENOBUFS : ret;
679}
680
681static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
682 [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
683 .len = NFT_COMPAT_NAME_MAX-1 },
684 [NFTA_COMPAT_REV] = { .type = NLA_U32 },
685 [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
686};
687
688static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
eb1fb147 689 [NFNL_MSG_COMPAT_GET] = { .call_rcu = nfnl_compat_get_rcu,
0ca743a5
PNA
690 .attr_count = NFTA_COMPAT_MAX,
691 .policy = nfnl_compat_policy_get },
692};
693
694static const struct nfnetlink_subsystem nfnl_compat_subsys = {
695 .name = "nft-compat",
696 .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
697 .cb_count = NFNL_MSG_COMPAT_MAX,
698 .cb = nfnl_nft_compat_cb,
699};
700
b8e20400 701static struct nft_expr_type nft_match_type;
ba378ca9 702
0ca743a5
PNA
703static const struct nft_expr_ops *
704nft_match_select_ops(const struct nft_ctx *ctx,
705 const struct nlattr * const tb[])
706{
b8e20400 707 struct nft_expr_ops *ops;
0ca743a5 708 struct xt_match *match;
732a8049 709 unsigned int matchsize;
0ca743a5 710 char *mt_name;
ba378ca9 711 u32 rev, family;
2bf4fade 712 int err;
0ca743a5
PNA
713
714 if (tb[NFTA_MATCH_NAME] == NULL ||
715 tb[NFTA_MATCH_REV] == NULL ||
716 tb[NFTA_MATCH_INFO] == NULL)
717 return ERR_PTR(-EINVAL);
718
719 mt_name = nla_data(tb[NFTA_MATCH_NAME]);
720 rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
36596dad 721 family = ctx->family;
0ca743a5 722
0ca743a5
PNA
723 match = xt_request_find_match(family, mt_name, rev);
724 if (IS_ERR(match))
725 return ERR_PTR(-ENOENT);
726
2bf4fade
LZ
727 if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
728 err = -EINVAL;
729 goto err;
730 }
f0716cd6 731
b8e20400
PNA
732 ops = kzalloc(sizeof(struct nft_expr_ops), GFP_KERNEL);
733 if (!ops) {
2bf4fade
LZ
734 err = -ENOMEM;
735 goto err;
736 }
0ca743a5 737
b8e20400
PNA
738 ops->type = &nft_match_type;
739 ops->eval = nft_match_eval;
740 ops->init = nft_match_init;
741 ops->destroy = nft_match_destroy;
742 ops->dump = nft_match_dump;
743 ops->validate = nft_match_validate;
744 ops->data = match;
0ca743a5 745
732a8049
FW
746 matchsize = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
747 if (matchsize > NFT_MATCH_LARGE_THRESH) {
748 matchsize = NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv));
749
b8e20400
PNA
750 ops->eval = nft_match_large_eval;
751 ops->init = nft_match_large_init;
752 ops->destroy = nft_match_large_destroy;
753 ops->dump = nft_match_large_dump;
732a8049
FW
754 }
755
b8e20400 756 ops->size = matchsize;
732a8049 757
b8e20400 758 return ops;
2bf4fade
LZ
759err:
760 module_put(match->me);
761 return ERR_PTR(err);
0ca743a5
PNA
762}
763
b8e20400
PNA
764static void nft_match_release_ops(const struct nft_expr_ops *ops)
765{
766 struct xt_match *match = ops->data;
767
768 module_put(match->me);
769 kfree(ops);
770}
771
0ca743a5
PNA
772static struct nft_expr_type nft_match_type __read_mostly = {
773 .name = "match",
774 .select_ops = nft_match_select_ops,
b8e20400 775 .release_ops = nft_match_release_ops,
0ca743a5
PNA
776 .policy = nft_match_policy,
777 .maxattr = NFTA_MATCH_MAX,
778 .owner = THIS_MODULE,
779};
780
b8e20400 781static struct nft_expr_type nft_target_type;
ba378ca9 782
0ca743a5
PNA
783static const struct nft_expr_ops *
784nft_target_select_ops(const struct nft_ctx *ctx,
785 const struct nlattr * const tb[])
786{
b8e20400 787 struct nft_expr_ops *ops;
0ca743a5
PNA
788 struct xt_target *target;
789 char *tg_name;
ba378ca9 790 u32 rev, family;
2bf4fade 791 int err;
0ca743a5
PNA
792
793 if (tb[NFTA_TARGET_NAME] == NULL ||
794 tb[NFTA_TARGET_REV] == NULL ||
795 tb[NFTA_TARGET_INFO] == NULL)
796 return ERR_PTR(-EINVAL);
797
798 tg_name = nla_data(tb[NFTA_TARGET_NAME]);
799 rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
36596dad 800 family = ctx->family;
0ca743a5 801
21d5e078
FW
802 if (strcmp(tg_name, XT_ERROR_TARGET) == 0 ||
803 strcmp(tg_name, XT_STANDARD_TARGET) == 0 ||
804 strcmp(tg_name, "standard") == 0)
805 return ERR_PTR(-EINVAL);
806
0ca743a5
PNA
807 target = xt_request_find_target(family, tg_name, rev);
808 if (IS_ERR(target))
809 return ERR_PTR(-ENOENT);
810
21d5e078
FW
811 if (!target->target) {
812 err = -EINVAL;
813 goto err;
814 }
815
2bf4fade
LZ
816 if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
817 err = -EINVAL;
818 goto err;
819 }
f0716cd6 820
b8e20400
PNA
821 ops = kzalloc(sizeof(struct nft_expr_ops), GFP_KERNEL);
822 if (!ops) {
2bf4fade
LZ
823 err = -ENOMEM;
824 goto err;
825 }
0ca743a5 826
b8e20400
PNA
827 ops->type = &nft_target_type;
828 ops->size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
829 ops->init = nft_target_init;
830 ops->destroy = nft_target_destroy;
831 ops->dump = nft_target_dump;
832 ops->validate = nft_target_validate;
833 ops->data = target;
0ca743a5 834
5191f4d8 835 if (family == NFPROTO_BRIDGE)
b8e20400 836 ops->eval = nft_target_eval_bridge;
5191f4d8 837 else
b8e20400 838 ops->eval = nft_target_eval_xt;
0ca743a5 839
b8e20400 840 return ops;
2bf4fade
LZ
841err:
842 module_put(target->me);
843 return ERR_PTR(err);
0ca743a5
PNA
844}
845
b8e20400
PNA
846static void nft_target_release_ops(const struct nft_expr_ops *ops)
847{
848 struct xt_target *target = ops->data;
849
850 module_put(target->me);
851 kfree(ops);
852}
853
0ca743a5
PNA
854static struct nft_expr_type nft_target_type __read_mostly = {
855 .name = "target",
856 .select_ops = nft_target_select_ops,
b8e20400 857 .release_ops = nft_target_release_ops,
0ca743a5
PNA
858 .policy = nft_target_policy,
859 .maxattr = NFTA_TARGET_MAX,
860 .owner = THIS_MODULE,
861};
862
863static int __init nft_compat_module_init(void)
864{
865 int ret;
866
867 ret = nft_register_expr(&nft_match_type);
868 if (ret < 0)
b8e20400 869 return ret;
0ca743a5
PNA
870
871 ret = nft_register_expr(&nft_target_type);
872 if (ret < 0)
873 goto err_match;
874
875 ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
876 if (ret < 0) {
877 pr_err("nft_compat: cannot register with nfnetlink.\n");
878 goto err_target;
879 }
880
0ca743a5 881 return ret;
0ca743a5
PNA
882err_target:
883 nft_unregister_expr(&nft_target_type);
884err_match:
885 nft_unregister_expr(&nft_match_type);
886 return ret;
887}
888
889static void __exit nft_compat_module_exit(void)
890{
891 nfnetlink_subsys_unregister(&nfnl_compat_subsys);
892 nft_unregister_expr(&nft_target_type);
893 nft_unregister_expr(&nft_match_type);
0ca743a5
PNA
894}
895
896MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
897
898module_init(nft_compat_module_init);
899module_exit(nft_compat_module_exit);
900
901MODULE_LICENSE("GPL");
902MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
903MODULE_ALIAS_NFT_EXPR("match");
904MODULE_ALIAS_NFT_EXPR("target");