blob: f93047f974e1a5d020ed22708fc69a424f5f942a [file] [log] [blame]
Pablo Neira Ayuso39e6dea2015-11-25 13:39:38 +01001/*
2 * Copyright (c) 2015 Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 */
8
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/netlink.h>
13#include <linux/netfilter.h>
14#include <linux/netfilter/nf_tables.h>
15#include <net/netfilter/nf_tables.h>
16#include <net/netfilter/nf_dup_netdev.h>
17
18struct nft_fwd_netdev {
19 enum nft_registers sreg_dev:8;
20};
21
22static void nft_fwd_netdev_eval(const struct nft_expr *expr,
23 struct nft_regs *regs,
24 const struct nft_pktinfo *pkt)
25{
26 struct nft_fwd_netdev *priv = nft_expr_priv(expr);
27 int oif = regs->data[priv->sreg_dev];
28
29 nf_dup_netdev_egress(pkt, oif);
30 regs->verdict.code = NF_DROP;
31}
32
33static const struct nla_policy nft_fwd_netdev_policy[NFTA_FWD_MAX + 1] = {
34 [NFTA_FWD_SREG_DEV] = { .type = NLA_U32 },
35};
36
37static int nft_fwd_netdev_init(const struct nft_ctx *ctx,
38 const struct nft_expr *expr,
39 const struct nlattr * const tb[])
40{
41 struct nft_fwd_netdev *priv = nft_expr_priv(expr);
42
43 if (tb[NFTA_FWD_SREG_DEV] == NULL)
44 return -EINVAL;
45
46 priv->sreg_dev = nft_parse_register(tb[NFTA_FWD_SREG_DEV]);
47 return nft_validate_register_load(priv->sreg_dev, sizeof(int));
48}
49
50static const struct nft_expr_ops nft_fwd_netdev_ingress_ops;
51
52static int nft_fwd_netdev_dump(struct sk_buff *skb, const struct nft_expr *expr)
53{
54 struct nft_fwd_netdev *priv = nft_expr_priv(expr);
55
56 if (nft_dump_register(skb, NFTA_FWD_SREG_DEV, priv->sreg_dev))
57 goto nla_put_failure;
58
59 return 0;
60
61nla_put_failure:
62 return -1;
63}
64
Pablo Neira Ayuso282fd1f2020-03-23 14:27:16 +010065static int nft_fwd_validate(const struct nft_ctx *ctx,
66 const struct nft_expr *expr,
67 const struct nft_data **data)
68{
69 return nft_chain_validate_hooks(ctx->chain, (1 << NF_NETDEV_INGRESS));
70}
71
Pablo Neira Ayuso39e6dea2015-11-25 13:39:38 +010072static struct nft_expr_type nft_fwd_netdev_type;
73static const struct nft_expr_ops nft_fwd_netdev_ops = {
74 .type = &nft_fwd_netdev_type,
75 .size = NFT_EXPR_SIZE(sizeof(struct nft_fwd_netdev)),
76 .eval = nft_fwd_netdev_eval,
77 .init = nft_fwd_netdev_init,
78 .dump = nft_fwd_netdev_dump,
Pablo Neira Ayuso282fd1f2020-03-23 14:27:16 +010079 .validate = nft_fwd_validate,
Pablo Neira Ayuso39e6dea2015-11-25 13:39:38 +010080};
81
82static struct nft_expr_type nft_fwd_netdev_type __read_mostly = {
83 .family = NFPROTO_NETDEV,
84 .name = "fwd",
85 .ops = &nft_fwd_netdev_ops,
86 .policy = nft_fwd_netdev_policy,
87 .maxattr = NFTA_FWD_MAX,
88 .owner = THIS_MODULE,
89};
90
91static int __init nft_fwd_netdev_module_init(void)
92{
93 return nft_register_expr(&nft_fwd_netdev_type);
94}
95
96static void __exit nft_fwd_netdev_module_exit(void)
97{
98 nft_unregister_expr(&nft_fwd_netdev_type);
99}
100
101module_init(nft_fwd_netdev_module_init);
102module_exit(nft_fwd_netdev_module_exit);
103
104MODULE_LICENSE("GPL");
105MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
106MODULE_ALIAS_NFT_AF_EXPR(5, "fwd");