1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
4 *
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>
19 #include <linux/netfilter_bridge/ebtables.h>
20 #include <linux/netfilter_arp/arp_tables.h>
21 #include <net/netfilter/nf_tables.h>
22 #include <net/netfilter/nf_log.h>
23
24 /* Used for matches where *info is larger than X byte */
25 #define NFT_MATCH_LARGE_THRESH 192
26
27 struct nft_xt_match_priv {
28 void *info;
29 };
30
nft_compat_chain_validate_dependency(const struct nft_ctx * ctx,const char * tablename)31 static int nft_compat_chain_validate_dependency(const struct nft_ctx *ctx,
32 const char *tablename)
33 {
34 enum nft_chain_types type = NFT_CHAIN_T_DEFAULT;
35 const struct nft_chain *chain = ctx->chain;
36 const struct nft_base_chain *basechain;
37
38 if (!tablename ||
39 !nft_is_base_chain(chain))
40 return 0;
41
42 basechain = nft_base_chain(chain);
43 if (strcmp(tablename, "nat") == 0) {
44 if (ctx->family != NFPROTO_BRIDGE)
45 type = NFT_CHAIN_T_NAT;
46 if (basechain->type->type != type)
47 return -EINVAL;
48 }
49
50 return 0;
51 }
52
53 union nft_entry {
54 struct ipt_entry e4;
55 struct ip6t_entry e6;
56 struct ebt_entry ebt;
57 struct arpt_entry arp;
58 };
59
60 static inline void
nft_compat_set_par(struct xt_action_param * par,const struct nft_pktinfo * pkt,const void * xt,const void * xt_info)61 nft_compat_set_par(struct xt_action_param *par,
62 const struct nft_pktinfo *pkt,
63 const void *xt, const void *xt_info)
64 {
65 par->state = pkt->state;
66 par->thoff = nft_thoff(pkt);
67 par->fragoff = pkt->fragoff;
68 par->target = xt;
69 par->targinfo = xt_info;
70 par->hotdrop = false;
71 }
72
nft_target_eval_xt(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)73 static void nft_target_eval_xt(const struct nft_expr *expr,
74 struct nft_regs *regs,
75 const struct nft_pktinfo *pkt)
76 {
77 void *info = nft_expr_priv(expr);
78 struct xt_target *target = expr->ops->data;
79 struct sk_buff *skb = pkt->skb;
80 struct xt_action_param xt;
81 int ret;
82
83 nft_compat_set_par(&xt, pkt, target, info);
84
85 ret = target->target(skb, &xt);
86
87 if (xt.hotdrop)
88 ret = NF_DROP;
89
90 switch (ret) {
91 case XT_CONTINUE:
92 regs->verdict.code = NFT_CONTINUE;
93 break;
94 default:
95 regs->verdict.code = ret;
96 break;
97 }
98 }
99
nft_target_eval_bridge(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)100 static void nft_target_eval_bridge(const struct nft_expr *expr,
101 struct nft_regs *regs,
102 const struct nft_pktinfo *pkt)
103 {
104 void *info = nft_expr_priv(expr);
105 struct xt_target *target = expr->ops->data;
106 struct sk_buff *skb = pkt->skb;
107 struct xt_action_param xt;
108 int ret;
109
110 nft_compat_set_par(&xt, pkt, target, info);
111
112 ret = target->target(skb, &xt);
113
114 if (xt.hotdrop)
115 ret = NF_DROP;
116
117 switch (ret) {
118 case EBT_ACCEPT:
119 regs->verdict.code = NF_ACCEPT;
120 break;
121 case EBT_DROP:
122 regs->verdict.code = NF_DROP;
123 break;
124 case EBT_CONTINUE:
125 regs->verdict.code = NFT_CONTINUE;
126 break;
127 case EBT_RETURN:
128 regs->verdict.code = NFT_RETURN;
129 break;
130 default:
131 regs->verdict.code = ret;
132 break;
133 }
134 }
135
136 static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = {
137 [NFTA_TARGET_NAME] = { .type = NLA_NUL_STRING,
138 .len = XT_EXTENSION_MAXNAMELEN, },
139 [NFTA_TARGET_REV] = NLA_POLICY_MAX(NLA_BE32, 255),
140 [NFTA_TARGET_INFO] = { .type = NLA_BINARY },
141 };
142
143 static void
nft_target_set_tgchk_param(struct xt_tgchk_param * par,const struct nft_ctx * ctx,struct xt_target * target,void * info,union nft_entry * entry,u16 proto,bool inv)144 nft_target_set_tgchk_param(struct xt_tgchk_param *par,
145 const struct nft_ctx *ctx,
146 struct xt_target *target, void *info,
147 union nft_entry *entry, u16 proto, bool inv)
148 {
149 par->net = ctx->net;
150 par->table = ctx->table->name;
151 switch (ctx->family) {
152 case AF_INET:
153 entry->e4.ip.proto = proto;
154 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
155 break;
156 case AF_INET6:
157 if (proto)
158 entry->e6.ipv6.flags |= IP6T_F_PROTO;
159
160 entry->e6.ipv6.proto = proto;
161 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
162 break;
163 case NFPROTO_BRIDGE:
164 entry->ebt.ethproto = (__force __be16)proto;
165 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
166 break;
167 case NFPROTO_ARP:
168 break;
169 }
170 par->entryinfo = entry;
171 par->target = target;
172 par->targinfo = info;
173 if (nft_is_base_chain(ctx->chain)) {
174 const struct nft_base_chain *basechain =
175 nft_base_chain(ctx->chain);
176 const struct nf_hook_ops *ops = &basechain->ops;
177
178 par->hook_mask = 1 << ops->hooknum;
179 } else {
180 par->hook_mask = 0;
181 }
182 par->family = ctx->family;
183 par->nft_compat = true;
184 }
185
target_compat_from_user(struct xt_target * t,void * in,void * out)186 static void target_compat_from_user(struct xt_target *t, void *in, void *out)
187 {
188 int pad;
189
190 memcpy(out, in, t->targetsize);
191 pad = XT_ALIGN(t->targetsize) - t->targetsize;
192 if (pad > 0)
193 memset(out + t->targetsize, 0, pad);
194 }
195
196 static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = {
197 [NFTA_RULE_COMPAT_PROTO] = { .type = NLA_U32 },
198 [NFTA_RULE_COMPAT_FLAGS] = NLA_POLICY_MASK(NLA_BE32, NFT_RULE_COMPAT_F_MASK),
199 };
200
nft_parse_compat(const struct nlattr * attr,u16 * proto,bool * inv)201 static int nft_parse_compat(const struct nlattr *attr, u16 *proto, bool *inv)
202 {
203 struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1];
204 u32 l4proto;
205 u32 flags;
206 int err;
207
208 err = nla_parse_nested_deprecated(tb, NFTA_RULE_COMPAT_MAX, attr,
209 nft_rule_compat_policy, NULL);
210 if (err < 0)
211 return err;
212
213 if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
214 return -EINVAL;
215
216 flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
217 if (flags & NFT_RULE_COMPAT_F_UNUSED ||
218 flags & ~NFT_RULE_COMPAT_F_MASK)
219 return -EINVAL;
220 if (flags & NFT_RULE_COMPAT_F_INV)
221 *inv = true;
222
223 l4proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
224 if (l4proto > U16_MAX)
225 return -EINVAL;
226
227 *proto = l4proto;
228
229 return 0;
230 }
231
nft_compat_wait_for_destructors(struct net * net)232 static void nft_compat_wait_for_destructors(struct net *net)
233 {
234 /* xtables matches or targets can have side effects, e.g.
235 * creation/destruction of /proc files.
236 * The xt ->destroy functions are run asynchronously from
237 * work queue. If we have pending invocations we thus
238 * need to wait for those to finish.
239 */
240 nf_tables_trans_destroy_flush_work(net);
241 }
242
243 static int
nft_target_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])244 nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
245 const struct nlattr * const tb[])
246 {
247 void *info = nft_expr_priv(expr);
248 struct xt_target *target = expr->ops->data;
249 struct xt_tgchk_param par;
250 size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
251 u16 proto = 0;
252 bool inv = false;
253 union nft_entry e = {};
254 int ret;
255
256 target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
257
258 if (ctx->nla[NFTA_RULE_COMPAT]) {
259 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
260 if (ret < 0)
261 return ret;
262 }
263
264 nft_compat_wait_for_destructors(ctx->net);
265
266 nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
267
268 ret = xt_check_target(&par, size, proto, inv);
269 if (ret < 0) {
270 if (ret == -ENOENT) {
271 const char *modname = NULL;
272
273 if (strcmp(target->name, "LOG") == 0)
274 modname = "nf_log_syslog";
275 else if (strcmp(target->name, "NFLOG") == 0)
276 modname = "nfnetlink_log";
277
278 if (modname &&
279 nft_request_module(ctx->net, "%s", modname) == -EAGAIN)
280 return -EAGAIN;
281 }
282
283 return ret;
284 }
285
286 /* The standard target cannot be used */
287 if (!target->target)
288 return -EINVAL;
289
290 return 0;
291 }
292
__nft_mt_tg_destroy(struct module * me,const struct nft_expr * expr)293 static void __nft_mt_tg_destroy(struct module *me, const struct nft_expr *expr)
294 {
295 module_put(me);
296 kfree(expr->ops);
297 }
298
299 static void
nft_target_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)300 nft_target_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
301 {
302 struct xt_target *target = expr->ops->data;
303 void *info = nft_expr_priv(expr);
304 struct module *me = target->me;
305 struct xt_tgdtor_param par;
306
307 par.net = ctx->net;
308 par.target = target;
309 par.targinfo = info;
310 par.family = ctx->family;
311 if (par.target->destroy != NULL)
312 par.target->destroy(&par);
313
314 __nft_mt_tg_destroy(me, expr);
315 }
316
nft_extension_dump_info(struct sk_buff * skb,int attr,const void * info,unsigned int size,unsigned int user_size)317 static int nft_extension_dump_info(struct sk_buff *skb, int attr,
318 const void *info,
319 unsigned int size, unsigned int user_size)
320 {
321 unsigned int info_size, aligned_size = XT_ALIGN(size);
322 struct nlattr *nla;
323
324 nla = nla_reserve(skb, attr, aligned_size);
325 if (!nla)
326 return -1;
327
328 info_size = user_size ? : size;
329 memcpy(nla_data(nla), info, info_size);
330 memset(nla_data(nla) + info_size, 0, aligned_size - info_size);
331
332 return 0;
333 }
334
nft_target_dump(struct sk_buff * skb,const struct nft_expr * expr,bool reset)335 static int nft_target_dump(struct sk_buff *skb,
336 const struct nft_expr *expr, bool reset)
337 {
338 const struct xt_target *target = expr->ops->data;
339 void *info = nft_expr_priv(expr);
340
341 if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
342 nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
343 nft_extension_dump_info(skb, NFTA_TARGET_INFO, info,
344 target->targetsize, target->usersize))
345 goto nla_put_failure;
346
347 return 0;
348
349 nla_put_failure:
350 return -1;
351 }
352
nft_target_validate(const struct nft_ctx * ctx,const struct nft_expr * expr)353 static int nft_target_validate(const struct nft_ctx *ctx,
354 const struct nft_expr *expr)
355 {
356 int ret;
357
358 if (ctx->family != NFPROTO_IPV4 &&
359 ctx->family != NFPROTO_IPV6 &&
360 ctx->family != NFPROTO_INET &&
361 ctx->family != NFPROTO_BRIDGE &&
362 ctx->family != NFPROTO_ARP)
363 return -EOPNOTSUPP;
364
365 ret = nft_chain_validate_hooks(ctx->chain,
366 (1 << NF_INET_PRE_ROUTING) |
367 (1 << NF_INET_LOCAL_IN) |
368 (1 << NF_INET_FORWARD) |
369 (1 << NF_INET_LOCAL_OUT) |
370 (1 << NF_INET_POST_ROUTING));
371 if (ret)
372 return ret;
373
374 if (nft_is_base_chain(ctx->chain)) {
375 const struct nft_base_chain *basechain =
376 nft_base_chain(ctx->chain);
377 const struct nf_hook_ops *ops = &basechain->ops;
378 unsigned int hook_mask = 1 << ops->hooknum;
379 struct xt_target *target = expr->ops->data;
380 void *info = nft_expr_priv(expr);
381 struct xt_tgchk_param par;
382 union nft_entry e = {};
383
384 if (target->hooks && !(hook_mask & target->hooks))
385 return -EINVAL;
386
387 nft_target_set_tgchk_param(&par, ctx, target, info, &e, 0, false);
388
389 ret = xt_check_hooks_target(&par);
390 if (ret < 0)
391 return ret;
392
393 ret = nft_compat_chain_validate_dependency(ctx, target->table);
394 if (ret < 0)
395 return ret;
396 }
397 return 0;
398 }
399
nft_target_bridge_validate(const struct nft_ctx * ctx,const struct nft_expr * expr)400 static int nft_target_bridge_validate(const struct nft_ctx *ctx,
401 const struct nft_expr *expr)
402 {
403 struct xt_target *target = expr->ops->data;
404
405 /* Do not allow UNSPEC to stand-in for NFPROTO_BRIDGE
406 * targets: they are incompatible. ebtables targets return
407 * EBT_ACCEPT, DROP and so on which are not compatible with
408 * NF_ACCEPT, NF_DROP and so on.
409 */
410 if (target->family != NFPROTO_BRIDGE)
411 return -ENOENT;
412
413 return nft_target_validate(ctx, expr);
414 }
415
__nft_match_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt,void * info)416 static void __nft_match_eval(const struct nft_expr *expr,
417 struct nft_regs *regs,
418 const struct nft_pktinfo *pkt,
419 void *info)
420 {
421 struct xt_match *match = expr->ops->data;
422 struct sk_buff *skb = pkt->skb;
423 struct xt_action_param xt;
424 bool ret;
425
426 nft_compat_set_par(&xt, pkt, match, info);
427
428 ret = match->match(skb, &xt);
429
430 if (xt.hotdrop) {
431 regs->verdict.code = NF_DROP;
432 return;
433 }
434
435 switch (ret ? 1 : 0) {
436 case 1:
437 regs->verdict.code = NFT_CONTINUE;
438 break;
439 case 0:
440 regs->verdict.code = NFT_BREAK;
441 break;
442 }
443 }
444
nft_match_large_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)445 static void nft_match_large_eval(const struct nft_expr *expr,
446 struct nft_regs *regs,
447 const struct nft_pktinfo *pkt)
448 {
449 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
450
451 __nft_match_eval(expr, regs, pkt, priv->info);
452 }
453
nft_match_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)454 static void nft_match_eval(const struct nft_expr *expr,
455 struct nft_regs *regs,
456 const struct nft_pktinfo *pkt)
457 {
458 __nft_match_eval(expr, regs, pkt, nft_expr_priv(expr));
459 }
460
461 static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
462 [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING,
463 .len = XT_EXTENSION_MAXNAMELEN },
464 [NFTA_MATCH_REV] = NLA_POLICY_MAX(NLA_BE32, 255),
465 [NFTA_MATCH_INFO] = { .type = NLA_BINARY },
466 };
467
468 /* struct xt_mtchk_param and xt_tgchk_param look very similar */
469 static void
nft_match_set_mtchk_param(struct xt_mtchk_param * par,const struct nft_ctx * ctx,struct xt_match * match,void * info,union nft_entry * entry,u16 proto,bool inv)470 nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
471 struct xt_match *match, void *info,
472 union nft_entry *entry, u16 proto, bool inv)
473 {
474 par->net = ctx->net;
475 par->table = ctx->table->name;
476 switch (ctx->family) {
477 case AF_INET:
478 entry->e4.ip.proto = proto;
479 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
480 break;
481 case AF_INET6:
482 if (proto)
483 entry->e6.ipv6.flags |= IP6T_F_PROTO;
484
485 entry->e6.ipv6.proto = proto;
486 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
487 break;
488 case NFPROTO_BRIDGE:
489 entry->ebt.ethproto = (__force __be16)proto;
490 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
491 break;
492 case NFPROTO_ARP:
493 break;
494 }
495 par->entryinfo = entry;
496 par->match = match;
497 par->matchinfo = info;
498 if (nft_is_base_chain(ctx->chain)) {
499 const struct nft_base_chain *basechain =
500 nft_base_chain(ctx->chain);
501 const struct nf_hook_ops *ops = &basechain->ops;
502
503 par->hook_mask = 1 << ops->hooknum;
504 } else {
505 par->hook_mask = 0;
506 }
507 par->family = ctx->family;
508 par->nft_compat = true;
509 }
510
match_compat_from_user(struct xt_match * m,void * in,void * out)511 static void match_compat_from_user(struct xt_match *m, void *in, void *out)
512 {
513 int pad;
514
515 memcpy(out, in, m->matchsize);
516 pad = XT_ALIGN(m->matchsize) - m->matchsize;
517 if (pad > 0)
518 memset(out + m->matchsize, 0, pad);
519 }
520
521 static int
__nft_match_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[],void * info)522 __nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
523 const struct nlattr * const tb[],
524 void *info)
525 {
526 struct xt_match *match = expr->ops->data;
527 struct xt_mtchk_param par;
528 size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
529 u16 proto = 0;
530 bool inv = false;
531 union nft_entry e = {};
532 int ret;
533
534 match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
535
536 if (ctx->nla[NFTA_RULE_COMPAT]) {
537 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
538 if (ret < 0)
539 return ret;
540 }
541
542 nft_compat_wait_for_destructors(ctx->net);
543
544 nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
545
546 return xt_check_match(&par, size, proto, inv);
547 }
548
549 static int
nft_match_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])550 nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
551 const struct nlattr * const tb[])
552 {
553 return __nft_match_init(ctx, expr, tb, nft_expr_priv(expr));
554 }
555
556 static int
nft_match_large_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])557 nft_match_large_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
558 const struct nlattr * const tb[])
559 {
560 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
561 struct xt_match *m = expr->ops->data;
562 int ret;
563
564 priv->info = kmalloc(XT_ALIGN(m->matchsize), GFP_KERNEL_ACCOUNT);
565 if (!priv->info)
566 return -ENOMEM;
567
568 ret = __nft_match_init(ctx, expr, tb, priv->info);
569 if (ret)
570 kfree(priv->info);
571 return ret;
572 }
573
574 static void
__nft_match_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr,void * info)575 __nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr,
576 void *info)
577 {
578 struct xt_match *match = expr->ops->data;
579 struct module *me = match->me;
580 struct xt_mtdtor_param par;
581
582 par.net = ctx->net;
583 par.match = match;
584 par.matchinfo = info;
585 par.family = ctx->family;
586 if (par.match->destroy != NULL)
587 par.match->destroy(&par);
588
589 __nft_mt_tg_destroy(me, expr);
590 }
591
592 static void
nft_match_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)593 nft_match_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
594 {
595 __nft_match_destroy(ctx, expr, nft_expr_priv(expr));
596 }
597
598 static void
nft_match_large_destroy(const struct nft_ctx * ctx,const struct nft_expr * expr)599 nft_match_large_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
600 {
601 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
602
603 __nft_match_destroy(ctx, expr, priv->info);
604 kfree(priv->info);
605 }
606
__nft_match_dump(struct sk_buff * skb,const struct nft_expr * expr,void * info)607 static int __nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr,
608 void *info)
609 {
610 struct xt_match *match = expr->ops->data;
611
612 if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
613 nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
614 nft_extension_dump_info(skb, NFTA_MATCH_INFO, info,
615 match->matchsize, match->usersize))
616 goto nla_put_failure;
617
618 return 0;
619
620 nla_put_failure:
621 return -1;
622 }
623
nft_match_dump(struct sk_buff * skb,const struct nft_expr * expr,bool reset)624 static int nft_match_dump(struct sk_buff *skb,
625 const struct nft_expr *expr, bool reset)
626 {
627 return __nft_match_dump(skb, expr, nft_expr_priv(expr));
628 }
629
nft_match_large_dump(struct sk_buff * skb,const struct nft_expr * e,bool reset)630 static int nft_match_large_dump(struct sk_buff *skb,
631 const struct nft_expr *e, bool reset)
632 {
633 struct nft_xt_match_priv *priv = nft_expr_priv(e);
634
635 return __nft_match_dump(skb, e, priv->info);
636 }
637
nft_match_validate(const struct nft_ctx * ctx,const struct nft_expr * expr)638 static int nft_match_validate(const struct nft_ctx *ctx,
639 const struct nft_expr *expr)
640 {
641 int ret;
642
643 if (ctx->family != NFPROTO_IPV4 &&
644 ctx->family != NFPROTO_IPV6 &&
645 ctx->family != NFPROTO_INET &&
646 ctx->family != NFPROTO_BRIDGE &&
647 ctx->family != NFPROTO_ARP)
648 return -EOPNOTSUPP;
649
650 ret = nft_chain_validate_hooks(ctx->chain,
651 (1 << NF_INET_PRE_ROUTING) |
652 (1 << NF_INET_LOCAL_IN) |
653 (1 << NF_INET_FORWARD) |
654 (1 << NF_INET_LOCAL_OUT) |
655 (1 << NF_INET_POST_ROUTING));
656 if (ret)
657 return ret;
658
659 if (nft_is_base_chain(ctx->chain)) {
660 const struct nft_base_chain *basechain =
661 nft_base_chain(ctx->chain);
662 const struct nf_hook_ops *ops = &basechain->ops;
663 unsigned int hook_mask = 1 << ops->hooknum;
664 struct xt_match *match = expr->ops->data;
665 size_t size = XT_ALIGN(match->matchsize);
666 struct xt_mtchk_param par;
667 union nft_entry e = {};
668 void *info;
669
670 if (match->hooks && !(hook_mask & match->hooks))
671 return -EINVAL;
672
673 if (NFT_EXPR_SIZE(size) > NFT_MATCH_LARGE_THRESH) {
674 struct nft_xt_match_priv *priv = nft_expr_priv(expr);
675
676 info = priv->info;
677 } else {
678 info = nft_expr_priv(expr);
679 }
680
681 nft_match_set_mtchk_param(&par, ctx, match, info, &e, 0, false);
682
683 ret = xt_check_hooks_match(&par);
684 if (ret < 0)
685 return ret;
686
687 ret = nft_compat_chain_validate_dependency(ctx, match->table);
688 if (ret < 0)
689 return ret;
690 }
691 return 0;
692 }
693
694 static int
nfnl_compat_fill_info(struct sk_buff * skb,u32 portid,u32 seq,u32 type,int event,u16 family,const char * name,int rev,int target)695 nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
696 int event, u16 family, const char *name,
697 int rev, int target)
698 {
699 struct nlmsghdr *nlh;
700 unsigned int flags = portid ? NLM_F_MULTI : 0;
701
702 event = nfnl_msg_type(NFNL_SUBSYS_NFT_COMPAT, event);
703 nlh = nfnl_msg_put(skb, portid, seq, event, flags, family,
704 NFNETLINK_V0, 0);
705 if (!nlh)
706 goto nlmsg_failure;
707
708 if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
709 nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
710 nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
711 goto nla_put_failure;
712
713 nlmsg_end(skb, nlh);
714 return skb->len;
715
716 nlmsg_failure:
717 nla_put_failure:
718 nlmsg_cancel(skb, nlh);
719 return -1;
720 }
721
nfnl_compat_get_rcu(struct sk_buff * skb,const struct nfnl_info * info,const struct nlattr * const tb[])722 static int nfnl_compat_get_rcu(struct sk_buff *skb,
723 const struct nfnl_info *info,
724 const struct nlattr * const tb[])
725 {
726 u8 family = info->nfmsg->nfgen_family;
727 const char *name, *fmt;
728 struct sk_buff *skb2;
729 int ret = 0, target;
730 u32 rev;
731
732 if (tb[NFTA_COMPAT_NAME] == NULL ||
733 tb[NFTA_COMPAT_REV] == NULL ||
734 tb[NFTA_COMPAT_TYPE] == NULL)
735 return -EINVAL;
736
737 name = nla_data(tb[NFTA_COMPAT_NAME]);
738 rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
739 /* x_tables api checks for 'target == 1' to mean target,
740 * everything else means 'match'.
741 * In x_tables world, the number is set by kernel, not
742 * userspace.
743 */
744 target = nla_get_be32(tb[NFTA_COMPAT_TYPE]) == htonl(1);
745
746 switch(family) {
747 case AF_INET:
748 fmt = "ipt_%s";
749 break;
750 case AF_INET6:
751 fmt = "ip6t_%s";
752 break;
753 case NFPROTO_BRIDGE:
754 fmt = "ebt_%s";
755 break;
756 case NFPROTO_ARP:
757 fmt = "arpt_%s";
758 break;
759 default:
760 pr_err("nft_compat: unsupported protocol %d\n", family);
761 return -EINVAL;
762 }
763
764 if (!try_module_get(THIS_MODULE))
765 return -EINVAL;
766
767 rcu_read_unlock();
768 try_then_request_module(xt_find_revision(family, name, rev, target, &ret),
769 fmt, name);
770 if (ret < 0)
771 goto out_put;
772
773 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
774 if (skb2 == NULL) {
775 ret = -ENOMEM;
776 goto out_put;
777 }
778
779 /* include the best revision for this extension in the message */
780 if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
781 info->nlh->nlmsg_seq,
782 NFNL_MSG_TYPE(info->nlh->nlmsg_type),
783 NFNL_MSG_COMPAT_GET,
784 family, name, ret, target) <= 0) {
785 kfree_skb(skb2);
786 goto out_put;
787 }
788
789 ret = nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid);
790 out_put:
791 rcu_read_lock();
792 module_put(THIS_MODULE);
793
794 return ret;
795 }
796
797 static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
798 [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
799 .len = NFT_COMPAT_NAME_MAX-1 },
800 [NFTA_COMPAT_REV] = NLA_POLICY_MAX(NLA_BE32, 255),
801 [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
802 };
803
804 static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
805 [NFNL_MSG_COMPAT_GET] = {
806 .call = nfnl_compat_get_rcu,
807 .type = NFNL_CB_RCU,
808 .attr_count = NFTA_COMPAT_MAX,
809 .policy = nfnl_compat_policy_get
810 },
811 };
812
813 static const struct nfnetlink_subsystem nfnl_compat_subsys = {
814 .name = "nft-compat",
815 .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
816 .cb_count = NFNL_MSG_COMPAT_MAX,
817 .cb = nfnl_nft_compat_cb,
818 };
819
820 static struct nft_expr_type nft_match_type;
821
822 static const struct nft_expr_ops *
nft_match_select_ops(const struct nft_ctx * ctx,const struct nlattr * const tb[])823 nft_match_select_ops(const struct nft_ctx *ctx,
824 const struct nlattr * const tb[])
825 {
826 struct nft_expr_ops *ops;
827 struct xt_match *match;
828 unsigned int matchsize;
829 char *mt_name;
830 u32 rev, family;
831 int err;
832
833 if (tb[NFTA_MATCH_NAME] == NULL ||
834 tb[NFTA_MATCH_REV] == NULL ||
835 tb[NFTA_MATCH_INFO] == NULL)
836 return ERR_PTR(-EINVAL);
837
838 mt_name = nla_data(tb[NFTA_MATCH_NAME]);
839 rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
840 family = ctx->family;
841
842 match = xt_request_find_match(family, mt_name, rev);
843 if (IS_ERR(match))
844 return ERR_PTR(-ENOENT);
845
846 if (match->matchsize > nla_len(tb[NFTA_MATCH_INFO])) {
847 err = -EINVAL;
848 goto err;
849 }
850
851 ops = kzalloc_obj(struct nft_expr_ops, GFP_KERNEL_ACCOUNT);
852 if (!ops) {
853 err = -ENOMEM;
854 goto err;
855 }
856
857 ops->type = &nft_match_type;
858 ops->eval = nft_match_eval;
859 ops->init = nft_match_init;
860 ops->destroy = nft_match_destroy;
861 ops->dump = nft_match_dump;
862 ops->validate = nft_match_validate;
863 ops->data = match;
864
865 matchsize = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize));
866 if (matchsize > NFT_MATCH_LARGE_THRESH) {
867 matchsize = NFT_EXPR_SIZE(sizeof(struct nft_xt_match_priv));
868
869 ops->eval = nft_match_large_eval;
870 ops->init = nft_match_large_init;
871 ops->destroy = nft_match_large_destroy;
872 ops->dump = nft_match_large_dump;
873 }
874
875 ops->size = matchsize;
876
877 return ops;
878 err:
879 module_put(match->me);
880 return ERR_PTR(err);
881 }
882
nft_match_release_ops(const struct nft_expr_ops * ops)883 static void nft_match_release_ops(const struct nft_expr_ops *ops)
884 {
885 struct xt_match *match = ops->data;
886
887 module_put(match->me);
888 kfree(ops);
889 }
890
891 static struct nft_expr_type nft_match_type __read_mostly = {
892 .name = "match",
893 .select_ops = nft_match_select_ops,
894 .release_ops = nft_match_release_ops,
895 .policy = nft_match_policy,
896 .maxattr = NFTA_MATCH_MAX,
897 .owner = THIS_MODULE,
898 };
899
900 static struct nft_expr_type nft_target_type;
901
902 static const struct nft_expr_ops *
nft_target_select_ops(const struct nft_ctx * ctx,const struct nlattr * const tb[])903 nft_target_select_ops(const struct nft_ctx *ctx,
904 const struct nlattr * const tb[])
905 {
906 struct nft_expr_ops *ops;
907 struct xt_target *target;
908 char *tg_name;
909 u32 rev, family;
910 int err;
911
912 if (tb[NFTA_TARGET_NAME] == NULL ||
913 tb[NFTA_TARGET_REV] == NULL ||
914 tb[NFTA_TARGET_INFO] == NULL)
915 return ERR_PTR(-EINVAL);
916
917 tg_name = nla_data(tb[NFTA_TARGET_NAME]);
918 rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
919 family = ctx->family;
920
921 if (strcmp(tg_name, XT_ERROR_TARGET) == 0 ||
922 strcmp(tg_name, XT_STANDARD_TARGET) == 0 ||
923 strcmp(tg_name, "standard") == 0)
924 return ERR_PTR(-EINVAL);
925
926 target = xt_request_find_target(family, tg_name, rev);
927 if (IS_ERR(target))
928 return ERR_PTR(-ENOENT);
929
930 if (!target->target) {
931 err = -EINVAL;
932 goto err;
933 }
934
935 if (target->targetsize > nla_len(tb[NFTA_TARGET_INFO])) {
936 err = -EINVAL;
937 goto err;
938 }
939
940 ops = kzalloc_obj(struct nft_expr_ops, GFP_KERNEL_ACCOUNT);
941 if (!ops) {
942 err = -ENOMEM;
943 goto err;
944 }
945
946 ops->type = &nft_target_type;
947 ops->size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize));
948 ops->init = nft_target_init;
949 ops->destroy = nft_target_destroy;
950 ops->dump = nft_target_dump;
951 ops->data = target;
952
953 if (family == NFPROTO_BRIDGE) {
954 ops->eval = nft_target_eval_bridge;
955 ops->validate = nft_target_bridge_validate;
956 } else {
957 ops->eval = nft_target_eval_xt;
958 ops->validate = nft_target_validate;
959 }
960
961 return ops;
962 err:
963 module_put(target->me);
964 return ERR_PTR(err);
965 }
966
nft_target_release_ops(const struct nft_expr_ops * ops)967 static void nft_target_release_ops(const struct nft_expr_ops *ops)
968 {
969 struct xt_target *target = ops->data;
970
971 module_put(target->me);
972 kfree(ops);
973 }
974
975 static struct nft_expr_type nft_target_type __read_mostly = {
976 .name = "target",
977 .select_ops = nft_target_select_ops,
978 .release_ops = nft_target_release_ops,
979 .policy = nft_target_policy,
980 .maxattr = NFTA_TARGET_MAX,
981 .owner = THIS_MODULE,
982 };
983
nft_compat_module_init(void)984 static int __init nft_compat_module_init(void)
985 {
986 int ret;
987
988 ret = nft_register_expr(&nft_match_type);
989 if (ret < 0)
990 return ret;
991
992 ret = nft_register_expr(&nft_target_type);
993 if (ret < 0)
994 goto err_match;
995
996 ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
997 if (ret < 0) {
998 pr_err("nft_compat: cannot register with nfnetlink.\n");
999 goto err_target;
1000 }
1001
1002 return ret;
1003 err_target:
1004 nft_unregister_expr(&nft_target_type);
1005 err_match:
1006 nft_unregister_expr(&nft_match_type);
1007 return ret;
1008 }
1009
nft_compat_module_exit(void)1010 static void __exit nft_compat_module_exit(void)
1011 {
1012 nfnetlink_subsys_unregister(&nfnl_compat_subsys);
1013 nft_unregister_expr(&nft_target_type);
1014 nft_unregister_expr(&nft_match_type);
1015 }
1016
1017 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
1018
1019 module_init(nft_compat_module_init);
1020 module_exit(nft_compat_module_exit);
1021
1022 MODULE_LICENSE("GPL");
1023 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
1024 MODULE_ALIAS_NFT_EXPR("match");
1025 MODULE_ALIAS_NFT_EXPR("target");
1026 MODULE_DESCRIPTION("x_tables over nftables support");
1027