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>
13 #include <linux/ipv6.h>
14 #include <net/netfilter/nf_tables.h>
15 #include <net/netfilter/nf_tables_offload.h>
16 #include <net/netfilter/nf_dup_netdev.h>
17 #include <net/neighbour.h>
20 struct nft_fwd_netdev {
24 static void nft_fwd_netdev_eval(const struct nft_expr *expr,
25 struct nft_regs *regs,
26 const struct nft_pktinfo *pkt)
28 struct nft_fwd_netdev *priv = nft_expr_priv(expr);
29 int oif = regs->data[priv->sreg_dev];
31 /* This is used by ifb only. */
32 skb_set_redirected(pkt->skb, true);
34 nf_fwd_netdev_egress(pkt, oif);
35 regs->verdict.code = NF_STOLEN;
38 static const struct nla_policy nft_fwd_netdev_policy[NFTA_FWD_MAX + 1] = {
39 [NFTA_FWD_SREG_DEV] = { .type = NLA_U32 },
40 [NFTA_FWD_SREG_ADDR] = { .type = NLA_U32 },
41 [NFTA_FWD_NFPROTO] = { .type = NLA_U32 },
44 static int nft_fwd_netdev_init(const struct nft_ctx *ctx,
45 const struct nft_expr *expr,
46 const struct nlattr * const tb[])
48 struct nft_fwd_netdev *priv = nft_expr_priv(expr);
50 if (tb[NFTA_FWD_SREG_DEV] == NULL)
53 return nft_parse_register_load(tb[NFTA_FWD_SREG_DEV], &priv->sreg_dev,
57 static int nft_fwd_netdev_dump(struct sk_buff *skb, const struct nft_expr *expr)
59 struct nft_fwd_netdev *priv = nft_expr_priv(expr);
61 if (nft_dump_register(skb, NFTA_FWD_SREG_DEV, priv->sreg_dev))
70 static int nft_fwd_netdev_offload(struct nft_offload_ctx *ctx,
71 struct nft_flow_rule *flow,
72 const struct nft_expr *expr)
74 const struct nft_fwd_netdev *priv = nft_expr_priv(expr);
75 int oif = ctx->regs[priv->sreg_dev].data.data[0];
77 return nft_fwd_dup_netdev_offload(ctx, flow, FLOW_ACTION_REDIRECT, oif);
80 static bool nft_fwd_netdev_offload_action(const struct nft_expr *expr)
85 struct nft_fwd_neigh {
91 static void nft_fwd_neigh_eval(const struct nft_expr *expr,
92 struct nft_regs *regs,
93 const struct nft_pktinfo *pkt)
95 struct nft_fwd_neigh *priv = nft_expr_priv(expr);
96 void *addr = ®s->data[priv->sreg_addr];
97 int oif = regs->data[priv->sreg_dev];
98 unsigned int verdict = NF_STOLEN;
99 struct sk_buff *skb = pkt->skb;
100 struct net_device *dev;
103 switch (priv->nfproto) {
107 if (skb->protocol != htons(ETH_P_IP)) {
111 if (skb_try_make_writable(skb, sizeof(*iph))) {
116 ip_decrease_ttl(iph);
117 neigh_table = NEIGH_ARP_TABLE;
121 struct ipv6hdr *ip6h;
123 if (skb->protocol != htons(ETH_P_IPV6)) {
127 if (skb_try_make_writable(skb, sizeof(*ip6h))) {
131 ip6h = ipv6_hdr(skb);
133 neigh_table = NEIGH_ND_TABLE;
141 dev = dev_get_by_index_rcu(nft_net(pkt), oif);
147 neigh_xmit(neigh_table, dev, addr, skb);
149 regs->verdict.code = verdict;
152 static int nft_fwd_neigh_init(const struct nft_ctx *ctx,
153 const struct nft_expr *expr,
154 const struct nlattr * const tb[])
156 struct nft_fwd_neigh *priv = nft_expr_priv(expr);
157 unsigned int addr_len;
160 if (!tb[NFTA_FWD_SREG_DEV] ||
161 !tb[NFTA_FWD_SREG_ADDR] ||
162 !tb[NFTA_FWD_NFPROTO])
165 priv->nfproto = ntohl(nla_get_be32(tb[NFTA_FWD_NFPROTO]));
167 switch (priv->nfproto) {
169 addr_len = sizeof(struct in_addr);
172 addr_len = sizeof(struct in6_addr);
178 err = nft_parse_register_load(tb[NFTA_FWD_SREG_DEV], &priv->sreg_dev,
183 return nft_parse_register_load(tb[NFTA_FWD_SREG_ADDR], &priv->sreg_addr,
187 static int nft_fwd_neigh_dump(struct sk_buff *skb, const struct nft_expr *expr)
189 struct nft_fwd_neigh *priv = nft_expr_priv(expr);
191 if (nft_dump_register(skb, NFTA_FWD_SREG_DEV, priv->sreg_dev) ||
192 nft_dump_register(skb, NFTA_FWD_SREG_ADDR, priv->sreg_addr) ||
193 nla_put_be32(skb, NFTA_FWD_NFPROTO, htonl(priv->nfproto)))
194 goto nla_put_failure;
202 static int nft_fwd_validate(const struct nft_ctx *ctx,
203 const struct nft_expr *expr,
204 const struct nft_data **data)
206 return nft_chain_validate_hooks(ctx->chain, (1 << NF_NETDEV_INGRESS));
209 static struct nft_expr_type nft_fwd_netdev_type;
210 static const struct nft_expr_ops nft_fwd_neigh_netdev_ops = {
211 .type = &nft_fwd_netdev_type,
212 .size = NFT_EXPR_SIZE(sizeof(struct nft_fwd_neigh)),
213 .eval = nft_fwd_neigh_eval,
214 .init = nft_fwd_neigh_init,
215 .dump = nft_fwd_neigh_dump,
216 .validate = nft_fwd_validate,
219 static const struct nft_expr_ops nft_fwd_netdev_ops = {
220 .type = &nft_fwd_netdev_type,
221 .size = NFT_EXPR_SIZE(sizeof(struct nft_fwd_netdev)),
222 .eval = nft_fwd_netdev_eval,
223 .init = nft_fwd_netdev_init,
224 .dump = nft_fwd_netdev_dump,
225 .validate = nft_fwd_validate,
226 .offload = nft_fwd_netdev_offload,
227 .offload_action = nft_fwd_netdev_offload_action,
230 static const struct nft_expr_ops *
231 nft_fwd_select_ops(const struct nft_ctx *ctx,
232 const struct nlattr * const tb[])
234 if (tb[NFTA_FWD_SREG_ADDR])
235 return &nft_fwd_neigh_netdev_ops;
236 if (tb[NFTA_FWD_SREG_DEV])
237 return &nft_fwd_netdev_ops;
239 return ERR_PTR(-EOPNOTSUPP);
242 static struct nft_expr_type nft_fwd_netdev_type __read_mostly = {
243 .family = NFPROTO_NETDEV,
245 .select_ops = nft_fwd_select_ops,
246 .policy = nft_fwd_netdev_policy,
247 .maxattr = NFTA_FWD_MAX,
248 .owner = THIS_MODULE,
251 static int __init nft_fwd_netdev_module_init(void)
253 return nft_register_expr(&nft_fwd_netdev_type);
256 static void __exit nft_fwd_netdev_module_exit(void)
258 nft_unregister_expr(&nft_fwd_netdev_type);
261 module_init(nft_fwd_netdev_module_init);
262 module_exit(nft_fwd_netdev_module_exit);
264 MODULE_LICENSE("GPL");
265 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
266 MODULE_ALIAS_NFT_AF_EXPR(5, "fwd");