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