1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 Pablo Neira Ayuso <pablo@netfilter.org>
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>
16 struct nft_dup_netdev
{
20 static void nft_dup_netdev_eval(const struct nft_expr
*expr
,
21 struct nft_regs
*regs
,
22 const struct nft_pktinfo
*pkt
)
24 struct nft_dup_netdev
*priv
= nft_expr_priv(expr
);
25 int oif
= regs
->data
[priv
->sreg_dev
];
27 nf_dup_netdev_egress(pkt
, oif
);
30 static const struct nla_policy nft_dup_netdev_policy
[NFTA_DUP_MAX
+ 1] = {
31 [NFTA_DUP_SREG_DEV
] = { .type
= NLA_U32
},
34 static int nft_dup_netdev_init(const struct nft_ctx
*ctx
,
35 const struct nft_expr
*expr
,
36 const struct nlattr
* const tb
[])
38 struct nft_dup_netdev
*priv
= nft_expr_priv(expr
);
40 if (tb
[NFTA_DUP_SREG_DEV
] == NULL
)
43 return nft_parse_register_load(ctx
, tb
[NFTA_DUP_SREG_DEV
], &priv
->sreg_dev
,
47 static int nft_dup_netdev_dump(struct sk_buff
*skb
,
48 const struct nft_expr
*expr
, bool reset
)
50 struct nft_dup_netdev
*priv
= nft_expr_priv(expr
);
52 if (nft_dump_register(skb
, NFTA_DUP_SREG_DEV
, priv
->sreg_dev
))
61 static int nft_dup_netdev_offload(struct nft_offload_ctx
*ctx
,
62 struct nft_flow_rule
*flow
,
63 const struct nft_expr
*expr
)
65 const struct nft_dup_netdev
*priv
= nft_expr_priv(expr
);
66 int oif
= ctx
->regs
[priv
->sreg_dev
].data
.data
[0];
68 return nft_fwd_dup_netdev_offload(ctx
, flow
, FLOW_ACTION_MIRRED
, oif
);
71 static bool nft_dup_netdev_offload_action(const struct nft_expr
*expr
)
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
,
88 static struct nft_expr_type nft_dup_netdev_type __read_mostly
= {
89 .family
= NFPROTO_NETDEV
,
91 .ops
= &nft_dup_netdev_ops
,
92 .policy
= nft_dup_netdev_policy
,
93 .maxattr
= NFTA_DUP_MAX
,
97 static int __init
nft_dup_netdev_module_init(void)
99 return nft_register_expr(&nft_dup_netdev_type
);
102 static void __exit
nft_dup_netdev_module_exit(void)
104 nft_unregister_expr(&nft_dup_netdev_type
);
107 module_init(nft_dup_netdev_module_init
);
108 module_exit(nft_dup_netdev_module_exit
);
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");