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 u8 sreg_addr;
17 u8 sreg_dev;
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 err = nft_parse_register_load(tb[NFTA_DUP_SREG_ADDR], &priv->sreg_addr,
42 sizeof(struct in6_addr));
43 if (err < 0)
44 return err;
45
46 if (tb[NFTA_DUP_SREG_DEV])
47 err = nft_parse_register_load(tb[NFTA_DUP_SREG_DEV],
48 &priv->sreg_dev, sizeof(int));
49
50 return err;
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 .reduce = NFT_REDUCE_READONLY,
77 };
78
79 static const struct nla_policy nft_dup_ipv6_policy[NFTA_DUP_MAX + 1] = {
80 [NFTA_DUP_SREG_ADDR] = { .type = NLA_U32 },
81 [NFTA_DUP_SREG_DEV] = { .type = NLA_U32 },
82 };
83
84 static struct nft_expr_type nft_dup_ipv6_type __read_mostly = {
85 .family = NFPROTO_IPV6,
86 .name = "dup",
87 .ops = &nft_dup_ipv6_ops,
88 .policy = nft_dup_ipv6_policy,
89 .maxattr = NFTA_DUP_MAX,
90 .owner = THIS_MODULE,
91 };
92
nft_dup_ipv6_module_init(void)93 static int __init nft_dup_ipv6_module_init(void)
94 {
95 return nft_register_expr(&nft_dup_ipv6_type);
96 }
97
nft_dup_ipv6_module_exit(void)98 static void __exit nft_dup_ipv6_module_exit(void)
99 {
100 nft_unregister_expr(&nft_dup_ipv6_type);
101 }
102
103 module_init(nft_dup_ipv6_module_init);
104 module_exit(nft_dup_ipv6_module_exit);
105
106 MODULE_LICENSE("GPL");
107 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
108 MODULE_ALIAS_NFT_AF_EXPR(AF_INET6, "dup");
109 MODULE_DESCRIPTION("IPv6 nftables packet duplication support");
110