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/nf_tables_offload.h>
14 #include <net/netfilter/nf_dup_netdev.h>
15
16 struct nft_dup_netdev {
17 u8 sreg_dev;
18 };
19
nft_dup_netdev_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)20 static void nft_dup_netdev_eval(const struct nft_expr *expr,
21 struct nft_regs *regs,
22 const struct nft_pktinfo *pkt)
23 {
24 struct nft_dup_netdev *priv = nft_expr_priv(expr);
25 int oif = regs->data[priv->sreg_dev];
26
27 nf_dup_netdev_egress(pkt, oif);
28 }
29
30 static const struct nla_policy nft_dup_netdev_policy[NFTA_DUP_MAX + 1] = {
31 [NFTA_DUP_SREG_DEV] = { .type = NLA_U32 },
32 };
33
nft_dup_netdev_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])34 static int nft_dup_netdev_init(const struct nft_ctx *ctx,
35 const struct nft_expr *expr,
36 const struct nlattr * const tb[])
37 {
38 struct nft_dup_netdev *priv = nft_expr_priv(expr);
39
40 if (tb[NFTA_DUP_SREG_DEV] == NULL)
41 return -EINVAL;
42
43 return nft_parse_register_load(ctx, tb[NFTA_DUP_SREG_DEV], &priv->sreg_dev,
44 sizeof(int));
45 }
46
nft_dup_netdev_dump(struct sk_buff * skb,const struct nft_expr * expr,bool reset)47 static int nft_dup_netdev_dump(struct sk_buff *skb,
48 const struct nft_expr *expr, bool reset)
49 {
50 struct nft_dup_netdev *priv = nft_expr_priv(expr);
51
52 if (nft_dump_register(skb, NFTA_DUP_SREG_DEV, priv->sreg_dev))
53 goto nla_put_failure;
54
55 return 0;
56
57 nla_put_failure:
58 return -1;
59 }
60
nft_dup_netdev_offload(struct nft_offload_ctx * ctx,struct nft_flow_rule * flow,const struct nft_expr * expr)61 static int nft_dup_netdev_offload(struct nft_offload_ctx *ctx,
62 struct nft_flow_rule *flow,
63 const struct nft_expr *expr)
64 {
65 const struct nft_dup_netdev *priv = nft_expr_priv(expr);
66 int oif = ctx->regs[priv->sreg_dev].data.data[0];
67
68 return nft_fwd_dup_netdev_offload(ctx, flow, FLOW_ACTION_MIRRED, oif);
69 }
70
nft_dup_netdev_offload_action(const struct nft_expr * expr)71 static bool nft_dup_netdev_offload_action(const struct nft_expr *expr)
72 {
73 return true;
74 }
75
76 static struct nft_expr_type nft_dup_netdev_type;
77 static const struct nft_expr_ops nft_dup_netdev_ops = {
78 .type = &nft_dup_netdev_type,
79 .size = NFT_EXPR_SIZE(sizeof(struct nft_dup_netdev)),
80 .eval = nft_dup_netdev_eval,
81 .init = nft_dup_netdev_init,
82 .dump = nft_dup_netdev_dump,
83 .reduce = NFT_REDUCE_READONLY,
84 .offload = nft_dup_netdev_offload,
85 .offload_action = nft_dup_netdev_offload_action,
86 };
87
88 static struct nft_expr_type nft_dup_netdev_type __read_mostly = {
89 .family = NFPROTO_NETDEV,
90 .name = "dup",
91 .ops = &nft_dup_netdev_ops,
92 .policy = nft_dup_netdev_policy,
93 .maxattr = NFTA_DUP_MAX,
94 .owner = THIS_MODULE,
95 };
96
nft_dup_netdev_module_init(void)97 static int __init nft_dup_netdev_module_init(void)
98 {
99 return nft_register_expr(&nft_dup_netdev_type);
100 }
101
nft_dup_netdev_module_exit(void)102 static void __exit nft_dup_netdev_module_exit(void)
103 {
104 nft_unregister_expr(&nft_dup_netdev_type);
105 }
106
107 module_init(nft_dup_netdev_module_init);
108 module_exit(nft_dup_netdev_module_exit);
109
110 MODULE_LICENSE("GPL");
111 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
112 MODULE_ALIAS_NFT_AF_EXPR(5, "dup");
113 MODULE_DESCRIPTION("nftables netdev packet duplication support");
114