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/ipv4/nf_dup_ipv4.h>
14
15 struct nft_dup_ipv4 {
16 u8 sreg_addr;
17 u8 sreg_dev;
18 };
19
nft_dup_ipv4_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)20 static void nft_dup_ipv4_eval(const struct nft_expr *expr,
21 struct nft_regs *regs,
22 const struct nft_pktinfo *pkt)
23 {
24 struct nft_dup_ipv4 *priv = nft_expr_priv(expr);
25 struct in_addr gw = {
26 .s_addr = (__force __be32)regs->data[priv->sreg_addr],
27 };
28 int oif = priv->sreg_dev ? regs->data[priv->sreg_dev] : -1;
29
30 nf_dup_ipv4(nft_net(pkt), pkt->skb, nft_hook(pkt), &gw, oif);
31 }
32
nft_dup_ipv4_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])33 static int nft_dup_ipv4_init(const struct nft_ctx *ctx,
34 const struct nft_expr *expr,
35 const struct nlattr * const tb[])
36 {
37 struct nft_dup_ipv4 *priv = nft_expr_priv(expr);
38 int err;
39
40 if (tb[NFTA_DUP_SREG_ADDR] == NULL)
41 return -EINVAL;
42
43 err = nft_parse_register_load(tb[NFTA_DUP_SREG_ADDR], &priv->sreg_addr,
44 sizeof(struct in_addr));
45 if (err < 0)
46 return err;
47
48 if (tb[NFTA_DUP_SREG_DEV])
49 err = nft_parse_register_load(tb[NFTA_DUP_SREG_DEV],
50 &priv->sreg_dev, sizeof(int));
51
52 return err;
53 }
54
nft_dup_ipv4_dump(struct sk_buff * skb,const struct nft_expr * expr,bool reset)55 static int nft_dup_ipv4_dump(struct sk_buff *skb,
56 const struct nft_expr *expr, bool reset)
57 {
58 struct nft_dup_ipv4 *priv = nft_expr_priv(expr);
59
60 if (nft_dump_register(skb, NFTA_DUP_SREG_ADDR, priv->sreg_addr))
61 goto nla_put_failure;
62 if (priv->sreg_dev &&
63 nft_dump_register(skb, NFTA_DUP_SREG_DEV, priv->sreg_dev))
64 goto nla_put_failure;
65
66 return 0;
67
68 nla_put_failure:
69 return -1;
70 }
71
72 static struct nft_expr_type nft_dup_ipv4_type;
73 static const struct nft_expr_ops nft_dup_ipv4_ops = {
74 .type = &nft_dup_ipv4_type,
75 .size = NFT_EXPR_SIZE(sizeof(struct nft_dup_ipv4)),
76 .eval = nft_dup_ipv4_eval,
77 .init = nft_dup_ipv4_init,
78 .dump = nft_dup_ipv4_dump,
79 .reduce = NFT_REDUCE_READONLY,
80 };
81
82 static const struct nla_policy nft_dup_ipv4_policy[NFTA_DUP_MAX + 1] = {
83 [NFTA_DUP_SREG_ADDR] = { .type = NLA_U32 },
84 [NFTA_DUP_SREG_DEV] = { .type = NLA_U32 },
85 };
86
87 static struct nft_expr_type nft_dup_ipv4_type __read_mostly = {
88 .family = NFPROTO_IPV4,
89 .name = "dup",
90 .ops = &nft_dup_ipv4_ops,
91 .policy = nft_dup_ipv4_policy,
92 .maxattr = NFTA_DUP_MAX,
93 .owner = THIS_MODULE,
94 };
95
nft_dup_ipv4_module_init(void)96 static int __init nft_dup_ipv4_module_init(void)
97 {
98 return nft_register_expr(&nft_dup_ipv4_type);
99 }
100
nft_dup_ipv4_module_exit(void)101 static void __exit nft_dup_ipv4_module_exit(void)
102 {
103 nft_unregister_expr(&nft_dup_ipv4_type);
104 }
105
106 module_init(nft_dup_ipv4_module_init);
107 module_exit(nft_dup_ipv4_module_exit);
108
109 MODULE_LICENSE("GPL");
110 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
111 MODULE_ALIAS_NFT_AF_EXPR(AF_INET, "dup");
112 MODULE_DESCRIPTION("IPv4 nftables packet duplication support");
113