1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2015 Pablo Neira Ayuso <pablo@netfilter.org>
4 */
5
6 #include <linux/kernel.h>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/netlink.h>
10 #include <linux/netfilter.h>
11 #include <linux/netfilter/nf_tables.h>
12 #include <net/netfilter/nf_tables.h>
13 #include <net/netfilter/ipv6/nf_dup_ipv6.h>
14
15 struct nft_dup_ipv6 {
16 enum nft_registers sreg_addr:8;
17 enum nft_registers sreg_dev:8;
18 };
19
nft_dup_ipv6_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)20 static void nft_dup_ipv6_eval(const struct nft_expr *expr,
21 struct nft_regs *regs,
22 const struct nft_pktinfo *pkt)
23 {
24 struct nft_dup_ipv6 *priv = nft_expr_priv(expr);
25 struct in6_addr *gw = (struct in6_addr *)®s->data[priv->sreg_addr];
26 int oif = priv->sreg_dev ? regs->data[priv->sreg_dev] : -1;
27
28 nf_dup_ipv6(nft_net(pkt), pkt->skb, nft_hook(pkt), gw, oif);
29 }
30
nft_dup_ipv6_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])31 static int nft_dup_ipv6_init(const struct nft_ctx *ctx,
32 const struct nft_expr *expr,
33 const struct nlattr * const tb[])
34 {
35 struct nft_dup_ipv6 *priv = nft_expr_priv(expr);
36 int err;
37
38 if (tb[NFTA_DUP_SREG_ADDR] == NULL)
39 return -EINVAL;
40
41 priv->sreg_addr = nft_parse_register(tb[NFTA_DUP_SREG_ADDR]);
42 err = nft_validate_register_load(priv->sreg_addr, sizeof(struct in6_addr));
43 if (err < 0)
44 return err;
45
46 if (tb[NFTA_DUP_SREG_DEV] != NULL) {
47 priv->sreg_dev = nft_parse_register(tb[NFTA_DUP_SREG_DEV]);
48 return nft_validate_register_load(priv->sreg_dev, sizeof(int));
49 }
50 return 0;
51 }
52
nft_dup_ipv6_dump(struct sk_buff * skb,const struct nft_expr * expr)53 static int nft_dup_ipv6_dump(struct sk_buff *skb, const struct nft_expr *expr)
54 {
55 struct nft_dup_ipv6 *priv = nft_expr_priv(expr);
56
57 if (nft_dump_register(skb, NFTA_DUP_SREG_ADDR, priv->sreg_addr))
58 goto nla_put_failure;
59 if (priv->sreg_dev &&
60 nft_dump_register(skb, NFTA_DUP_SREG_DEV, priv->sreg_dev))
61 goto nla_put_failure;
62
63 return 0;
64
65 nla_put_failure:
66 return -1;
67 }
68
69 static struct nft_expr_type nft_dup_ipv6_type;
70 static const struct nft_expr_ops nft_dup_ipv6_ops = {
71 .type = &nft_dup_ipv6_type,
72 .size = NFT_EXPR_SIZE(sizeof(struct nft_dup_ipv6)),
73 .eval = nft_dup_ipv6_eval,
74 .init = nft_dup_ipv6_init,
75 .dump = nft_dup_ipv6_dump,
76 };
77
78 static const struct nla_policy nft_dup_ipv6_policy[NFTA_DUP_MAX + 1] = {
79 [NFTA_DUP_SREG_ADDR] = { .type = NLA_U32 },
80 [NFTA_DUP_SREG_DEV] = { .type = NLA_U32 },
81 };
82
83 static struct nft_expr_type nft_dup_ipv6_type __read_mostly = {
84 .family = NFPROTO_IPV6,
85 .name = "dup",
86 .ops = &nft_dup_ipv6_ops,
87 .policy = nft_dup_ipv6_policy,
88 .maxattr = NFTA_DUP_MAX,
89 .owner = THIS_MODULE,
90 };
91
nft_dup_ipv6_module_init(void)92 static int __init nft_dup_ipv6_module_init(void)
93 {
94 return nft_register_expr(&nft_dup_ipv6_type);
95 }
96
nft_dup_ipv6_module_exit(void)97 static void __exit nft_dup_ipv6_module_exit(void)
98 {
99 nft_unregister_expr(&nft_dup_ipv6_type);
100 }
101
102 module_init(nft_dup_ipv6_module_init);
103 module_exit(nft_dup_ipv6_module_exit);
104
105 MODULE_LICENSE("GPL");
106 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
107 MODULE_ALIAS_NFT_AF_EXPR(AF_INET6, "dup");
108