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 <linux/ip.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>
18 #include <net/ip.h>
19
20 struct nft_fwd_netdev {
21 enum nft_registers sreg_dev:8;
22 };
23
nft_fwd_netdev_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)24 static void nft_fwd_netdev_eval(const struct nft_expr *expr,
25 struct nft_regs *regs,
26 const struct nft_pktinfo *pkt)
27 {
28 struct nft_fwd_netdev *priv = nft_expr_priv(expr);
29 int oif = regs->data[priv->sreg_dev];
30
31 nf_fwd_netdev_egress(pkt, oif);
32 regs->verdict.code = NF_STOLEN;
33 }
34
35 static const struct nla_policy nft_fwd_netdev_policy[NFTA_FWD_MAX + 1] = {
36 [NFTA_FWD_SREG_DEV] = { .type = NLA_U32 },
37 [NFTA_FWD_SREG_ADDR] = { .type = NLA_U32 },
38 [NFTA_FWD_NFPROTO] = { .type = NLA_U32 },
39 };
40
nft_fwd_netdev_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])41 static int nft_fwd_netdev_init(const struct nft_ctx *ctx,
42 const struct nft_expr *expr,
43 const struct nlattr * const tb[])
44 {
45 struct nft_fwd_netdev *priv = nft_expr_priv(expr);
46
47 if (tb[NFTA_FWD_SREG_DEV] == NULL)
48 return -EINVAL;
49
50 priv->sreg_dev = nft_parse_register(tb[NFTA_FWD_SREG_DEV]);
51 return nft_validate_register_load(priv->sreg_dev, sizeof(int));
52 }
53
nft_fwd_netdev_dump(struct sk_buff * skb,const struct nft_expr * expr)54 static int nft_fwd_netdev_dump(struct sk_buff *skb, const struct nft_expr *expr)
55 {
56 struct nft_fwd_netdev *priv = nft_expr_priv(expr);
57
58 if (nft_dump_register(skb, NFTA_FWD_SREG_DEV, priv->sreg_dev))
59 goto nla_put_failure;
60
61 return 0;
62
63 nla_put_failure:
64 return -1;
65 }
66
nft_fwd_netdev_offload(struct nft_offload_ctx * ctx,struct nft_flow_rule * flow,const struct nft_expr * expr)67 static int nft_fwd_netdev_offload(struct nft_offload_ctx *ctx,
68 struct nft_flow_rule *flow,
69 const struct nft_expr *expr)
70 {
71 const struct nft_fwd_netdev *priv = nft_expr_priv(expr);
72 int oif = ctx->regs[priv->sreg_dev].data.data[0];
73
74 return nft_fwd_dup_netdev_offload(ctx, flow, FLOW_ACTION_REDIRECT, oif);
75 }
76
77 struct nft_fwd_neigh {
78 enum nft_registers sreg_dev:8;
79 enum nft_registers sreg_addr:8;
80 u8 nfproto;
81 };
82
nft_fwd_neigh_eval(const struct nft_expr * expr,struct nft_regs * regs,const struct nft_pktinfo * pkt)83 static void nft_fwd_neigh_eval(const struct nft_expr *expr,
84 struct nft_regs *regs,
85 const struct nft_pktinfo *pkt)
86 {
87 struct nft_fwd_neigh *priv = nft_expr_priv(expr);
88 void *addr = ®s->data[priv->sreg_addr];
89 int oif = regs->data[priv->sreg_dev];
90 unsigned int verdict = NF_STOLEN;
91 struct sk_buff *skb = pkt->skb;
92 struct net_device *dev;
93 int neigh_table;
94
95 switch (priv->nfproto) {
96 case NFPROTO_IPV4: {
97 struct iphdr *iph;
98
99 if (skb->protocol != htons(ETH_P_IP)) {
100 verdict = NFT_BREAK;
101 goto out;
102 }
103 if (skb_try_make_writable(skb, sizeof(*iph))) {
104 verdict = NF_DROP;
105 goto out;
106 }
107 iph = ip_hdr(skb);
108 ip_decrease_ttl(iph);
109 neigh_table = NEIGH_ARP_TABLE;
110 break;
111 }
112 case NFPROTO_IPV6: {
113 struct ipv6hdr *ip6h;
114
115 if (skb->protocol != htons(ETH_P_IPV6)) {
116 verdict = NFT_BREAK;
117 goto out;
118 }
119 if (skb_try_make_writable(skb, sizeof(*ip6h))) {
120 verdict = NF_DROP;
121 goto out;
122 }
123 ip6h = ipv6_hdr(skb);
124 ip6h->hop_limit--;
125 neigh_table = NEIGH_ND_TABLE;
126 break;
127 }
128 default:
129 verdict = NFT_BREAK;
130 goto out;
131 }
132
133 dev = dev_get_by_index_rcu(nft_net(pkt), oif);
134 if (dev == NULL)
135 return;
136
137 skb->dev = dev;
138 neigh_xmit(neigh_table, dev, addr, skb);
139 out:
140 regs->verdict.code = verdict;
141 }
142
nft_fwd_neigh_init(const struct nft_ctx * ctx,const struct nft_expr * expr,const struct nlattr * const tb[])143 static int nft_fwd_neigh_init(const struct nft_ctx *ctx,
144 const struct nft_expr *expr,
145 const struct nlattr * const tb[])
146 {
147 struct nft_fwd_neigh *priv = nft_expr_priv(expr);
148 unsigned int addr_len;
149 int err;
150
151 if (!tb[NFTA_FWD_SREG_DEV] ||
152 !tb[NFTA_FWD_SREG_ADDR] ||
153 !tb[NFTA_FWD_NFPROTO])
154 return -EINVAL;
155
156 priv->sreg_dev = nft_parse_register(tb[NFTA_FWD_SREG_DEV]);
157 priv->sreg_addr = nft_parse_register(tb[NFTA_FWD_SREG_ADDR]);
158 priv->nfproto = ntohl(nla_get_be32(tb[NFTA_FWD_NFPROTO]));
159
160 switch (priv->nfproto) {
161 case NFPROTO_IPV4:
162 addr_len = sizeof(struct in_addr);
163 break;
164 case NFPROTO_IPV6:
165 addr_len = sizeof(struct in6_addr);
166 break;
167 default:
168 return -EOPNOTSUPP;
169 }
170
171 err = nft_validate_register_load(priv->sreg_dev, sizeof(int));
172 if (err < 0)
173 return err;
174
175 return nft_validate_register_load(priv->sreg_addr, addr_len);
176 }
177
nft_fwd_neigh_dump(struct sk_buff * skb,const struct nft_expr * expr)178 static int nft_fwd_neigh_dump(struct sk_buff *skb, const struct nft_expr *expr)
179 {
180 struct nft_fwd_neigh *priv = nft_expr_priv(expr);
181
182 if (nft_dump_register(skb, NFTA_FWD_SREG_DEV, priv->sreg_dev) ||
183 nft_dump_register(skb, NFTA_FWD_SREG_ADDR, priv->sreg_addr) ||
184 nla_put_be32(skb, NFTA_FWD_NFPROTO, htonl(priv->nfproto)))
185 goto nla_put_failure;
186
187 return 0;
188
189 nla_put_failure:
190 return -1;
191 }
192
193 static struct nft_expr_type nft_fwd_netdev_type;
194 static const struct nft_expr_ops nft_fwd_neigh_netdev_ops = {
195 .type = &nft_fwd_netdev_type,
196 .size = NFT_EXPR_SIZE(sizeof(struct nft_fwd_neigh)),
197 .eval = nft_fwd_neigh_eval,
198 .init = nft_fwd_neigh_init,
199 .dump = nft_fwd_neigh_dump,
200 };
201
202 static const struct nft_expr_ops nft_fwd_netdev_ops = {
203 .type = &nft_fwd_netdev_type,
204 .size = NFT_EXPR_SIZE(sizeof(struct nft_fwd_netdev)),
205 .eval = nft_fwd_netdev_eval,
206 .init = nft_fwd_netdev_init,
207 .dump = nft_fwd_netdev_dump,
208 .offload = nft_fwd_netdev_offload,
209 };
210
211 static const struct nft_expr_ops *
nft_fwd_select_ops(const struct nft_ctx * ctx,const struct nlattr * const tb[])212 nft_fwd_select_ops(const struct nft_ctx *ctx,
213 const struct nlattr * const tb[])
214 {
215 if (tb[NFTA_FWD_SREG_ADDR])
216 return &nft_fwd_neigh_netdev_ops;
217 if (tb[NFTA_FWD_SREG_DEV])
218 return &nft_fwd_netdev_ops;
219
220 return ERR_PTR(-EOPNOTSUPP);
221 }
222
223 static struct nft_expr_type nft_fwd_netdev_type __read_mostly = {
224 .family = NFPROTO_NETDEV,
225 .name = "fwd",
226 .select_ops = nft_fwd_select_ops,
227 .policy = nft_fwd_netdev_policy,
228 .maxattr = NFTA_FWD_MAX,
229 .owner = THIS_MODULE,
230 };
231
nft_fwd_netdev_module_init(void)232 static int __init nft_fwd_netdev_module_init(void)
233 {
234 return nft_register_expr(&nft_fwd_netdev_type);
235 }
236
nft_fwd_netdev_module_exit(void)237 static void __exit nft_fwd_netdev_module_exit(void)
238 {
239 nft_unregister_expr(&nft_fwd_netdev_type);
240 }
241
242 module_init(nft_fwd_netdev_module_init);
243 module_exit(nft_fwd_netdev_module_exit);
244
245 MODULE_LICENSE("GPL");
246 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
247 MODULE_ALIAS_NFT_AF_EXPR(5, "fwd");
248