]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/netfilter/nf_flow_table_ip.c
net: dsa: resolve forwarding path for dsa slave ports
[mirror_ubuntu-jammy-kernel.git] / net / netfilter / nf_flow_table_ip.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
7d208687
FF
2#include <linux/kernel.h>
3#include <linux/init.h>
4#include <linux/module.h>
5#include <linux/netfilter.h>
6#include <linux/rhashtable.h>
7#include <linux/ip.h>
a908fdec 8#include <linux/ipv6.h>
7d208687
FF
9#include <linux/netdevice.h>
10#include <net/ip.h>
a908fdec
FF
11#include <net/ipv6.h>
12#include <net/ip6_route.h>
7d208687
FF
13#include <net/neighbour.h>
14#include <net/netfilter/nf_flow_table.h>
53c2b289 15#include <net/netfilter/nf_conntrack_acct.h>
7d208687
FF
16/* For layer 4 checksum field offset. */
17#include <linux/tcp.h>
18#include <linux/udp.h>
19
33894c36
FF
20static int nf_flow_state_check(struct flow_offload *flow, int proto,
21 struct sk_buff *skb, unsigned int thoff)
b6f27d32
FF
22{
23 struct tcphdr *tcph;
24
33894c36
FF
25 if (proto != IPPROTO_TCP)
26 return 0;
27
b6f27d32
FF
28 tcph = (void *)(skb_network_header(skb) + thoff);
29 if (unlikely(tcph->fin || tcph->rst)) {
30 flow_offload_teardown(flow);
31 return -1;
32 }
33
34 return 0;
35}
36
f4401262
PNA
37static void nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
38 __be32 addr, __be32 new_addr)
7d208687
FF
39{
40 struct tcphdr *tcph;
41
7d208687
FF
42 tcph = (void *)(skb_network_header(skb) + thoff);
43 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
7d208687
FF
44}
45
f4401262
PNA
46static void nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
47 __be32 addr, __be32 new_addr)
7d208687
FF
48{
49 struct udphdr *udph;
50
7d208687
FF
51 udph = (void *)(skb_network_header(skb) + thoff);
52 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
53 inet_proto_csum_replace4(&udph->check, skb, addr,
54 new_addr, true);
55 if (!udph->check)
56 udph->check = CSUM_MANGLED_0;
57 }
7d208687
FF
58}
59
f4401262
PNA
60static void nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
61 unsigned int thoff, __be32 addr,
62 __be32 new_addr)
7d208687
FF
63{
64 switch (iph->protocol) {
65 case IPPROTO_TCP:
f4401262 66 nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr);
7d208687
FF
67 break;
68 case IPPROTO_UDP:
f4401262 69 nf_flow_nat_ip_udp(skb, thoff, addr, new_addr);
7d208687
FF
70 break;
71 }
7d208687
FF
72}
73
f4401262
PNA
74static void nf_flow_snat_ip(const struct flow_offload *flow,
75 struct sk_buff *skb, struct iphdr *iph,
76 unsigned int thoff, enum flow_offload_tuple_dir dir)
7d208687
FF
77{
78 __be32 addr, new_addr;
79
80 switch (dir) {
81 case FLOW_OFFLOAD_DIR_ORIGINAL:
82 addr = iph->saddr;
83 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
84 iph->saddr = new_addr;
85 break;
86 case FLOW_OFFLOAD_DIR_REPLY:
87 addr = iph->daddr;
88 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
89 iph->daddr = new_addr;
90 break;
7d208687
FF
91 }
92 csum_replace4(&iph->check, addr, new_addr);
93
f4401262 94 nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
7d208687
FF
95}
96
f4401262
PNA
97static void nf_flow_dnat_ip(const struct flow_offload *flow,
98 struct sk_buff *skb, struct iphdr *iph,
99 unsigned int thoff, enum flow_offload_tuple_dir dir)
7d208687
FF
100{
101 __be32 addr, new_addr;
102
103 switch (dir) {
104 case FLOW_OFFLOAD_DIR_ORIGINAL:
105 addr = iph->daddr;
106 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
107 iph->daddr = new_addr;
108 break;
109 case FLOW_OFFLOAD_DIR_REPLY:
110 addr = iph->saddr;
111 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
112 iph->saddr = new_addr;
113 break;
7d208687
FF
114 }
115 csum_replace4(&iph->check, addr, new_addr);
116
f4401262 117 nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
7d208687
FF
118}
119
f4401262 120static void nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
2fc11745
PNA
121 unsigned int thoff, enum flow_offload_tuple_dir dir,
122 struct iphdr *iph)
7d208687 123{
f4401262
PNA
124 if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
125 nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir);
126 nf_flow_snat_ip(flow, skb, iph, thoff, dir);
127 }
128 if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
129 nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir);
130 nf_flow_dnat_ip(flow, skb, iph, thoff, dir);
131 }
7d208687
FF
132}
133
134static bool ip_has_options(unsigned int thoff)
135{
136 return thoff != sizeof(struct iphdr);
137}
138
139static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
2fc11745 140 struct flow_offload_tuple *tuple, u32 *hdrsize)
7d208687
FF
141{
142 struct flow_ports *ports;
2fc11745 143 unsigned int thoff;
7d208687
FF
144 struct iphdr *iph;
145
146 if (!pskb_may_pull(skb, sizeof(*iph)))
147 return -1;
148
149 iph = ip_hdr(skb);
150 thoff = iph->ihl * 4;
151
152 if (ip_is_fragment(iph) ||
153 unlikely(ip_has_options(thoff)))
154 return -1;
155
793d5d61
PNA
156 switch (iph->protocol) {
157 case IPPROTO_TCP:
2fc11745 158 *hdrsize = sizeof(struct tcphdr);
793d5d61
PNA
159 break;
160 case IPPROTO_UDP:
2fc11745 161 *hdrsize = sizeof(struct udphdr);
793d5d61
PNA
162 break;
163 default:
7d208687 164 return -1;
793d5d61 165 }
7d208687 166
33cc3c0c
TY
167 if (iph->ttl <= 1)
168 return -1;
169
7d208687 170 thoff = iph->ihl * 4;
2fc11745 171 if (!pskb_may_pull(skb, thoff + *hdrsize))
7d208687
FF
172 return -1;
173
41e9ec5a 174 iph = ip_hdr(skb);
7d208687
FF
175 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
176
177 tuple->src_v4.s_addr = iph->saddr;
178 tuple->dst_v4.s_addr = iph->daddr;
179 tuple->src_port = ports->source;
180 tuple->dst_port = ports->dest;
181 tuple->l3proto = AF_INET;
182 tuple->l4proto = iph->protocol;
183 tuple->iifidx = dev->ifindex;
184
185 return 0;
186}
187
188/* Based on ip_exceeds_mtu(). */
189static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
190{
191 if (skb->len <= mtu)
192 return false;
193
7d208687
FF
194 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
195 return false;
196
197 return true;
198}
199
589b474a
FW
200static unsigned int nf_flow_xmit_xfrm(struct sk_buff *skb,
201 const struct nf_hook_state *state,
202 struct dst_entry *dst)
203{
204 skb_orphan(skb);
205 skb_dst_set_noref(skb, dst);
589b474a
FW
206 dst_output(state->net, state->sk, skb);
207 return NF_STOLEN;
208}
209
7d208687
FF
210unsigned int
211nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
212 const struct nf_hook_state *state)
213{
214 struct flow_offload_tuple_rhash *tuplehash;
215 struct nf_flowtable *flow_table = priv;
216 struct flow_offload_tuple tuple = {};
217 enum flow_offload_tuple_dir dir;
218 struct flow_offload *flow;
219 struct net_device *outdev;
2a79fd39 220 struct rtable *rt;
b6f27d32 221 unsigned int thoff;
7d208687
FF
222 struct iphdr *iph;
223 __be32 nexthop;
2fc11745 224 u32 hdrsize;
7d208687
FF
225
226 if (skb->protocol != htons(ETH_P_IP))
227 return NF_ACCEPT;
228
2fc11745 229 if (nf_flow_tuple_ip(skb, state->in, &tuple, &hdrsize) < 0)
7d208687
FF
230 return NF_ACCEPT;
231
232 tuplehash = flow_offload_lookup(flow_table, &tuple);
233 if (tuplehash == NULL)
234 return NF_ACCEPT;
235
7d208687
FF
236 dir = tuplehash->tuple.dir;
237 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
2a79fd39 238 rt = (struct rtable *)flow->tuplehash[dir].tuple.dst_cache;
227e1e4d 239 outdev = rt->dst.dev;
7d208687 240
e75b3e1c 241 if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
7d208687
FF
242 return NF_ACCEPT;
243
2fc11745
PNA
244 iph = ip_hdr(skb);
245 thoff = iph->ihl * 4;
2fc11745 246 if (nf_flow_state_check(flow, iph->protocol, skb, thoff))
b6f27d32
FF
247 return NF_ACCEPT;
248
e5075c0b 249 if (!dst_check(&rt->dst, 0)) {
589b474a
FW
250 flow_offload_teardown(flow);
251 return NF_ACCEPT;
252 }
253
2babb46c
PNA
254 if (skb_try_make_writable(skb, thoff + hdrsize))
255 return NF_DROP;
256
1b9cd769
PNA
257 flow_offload_refresh(flow_table, flow);
258
2babb46c 259 iph = ip_hdr(skb);
f4401262 260 nf_flow_nat_ip(flow, skb, thoff, dir, iph);
7d208687 261
7d208687 262 ip_decrease_ttl(iph);
de20900f 263 skb->tstamp = 0;
7d208687 264
53c2b289
PNA
265 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
266 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
267
589b474a
FW
268 if (unlikely(dst_xfrm(&rt->dst))) {
269 memset(skb->cb, 0, sizeof(struct inet_skb_parm));
270 IPCB(skb)->iif = skb->dev->ifindex;
271 IPCB(skb)->flags = IPSKB_FORWARDED;
272 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
273 }
274
7d208687
FF
275 skb->dev = outdev;
276 nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
2a79fd39 277 skb_dst_set_noref(skb, &rt->dst);
7d208687
FF
278 neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
279
280 return NF_STOLEN;
281}
282EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
a908fdec 283
f4401262
PNA
284static void nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
285 struct in6_addr *addr,
286 struct in6_addr *new_addr,
287 struct ipv6hdr *ip6h)
a908fdec
FF
288{
289 struct tcphdr *tcph;
290
a908fdec
FF
291 tcph = (void *)(skb_network_header(skb) + thoff);
292 inet_proto_csum_replace16(&tcph->check, skb, addr->s6_addr32,
293 new_addr->s6_addr32, true);
a908fdec
FF
294}
295
f4401262
PNA
296static void nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
297 struct in6_addr *addr,
298 struct in6_addr *new_addr)
a908fdec
FF
299{
300 struct udphdr *udph;
301
a908fdec
FF
302 udph = (void *)(skb_network_header(skb) + thoff);
303 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
304 inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
305 new_addr->s6_addr32, true);
306 if (!udph->check)
307 udph->check = CSUM_MANGLED_0;
308 }
a908fdec
FF
309}
310
f4401262
PNA
311static void nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
312 unsigned int thoff, struct in6_addr *addr,
313 struct in6_addr *new_addr)
a908fdec
FF
314{
315 switch (ip6h->nexthdr) {
316 case IPPROTO_TCP:
f4401262 317 nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr, ip6h);
a908fdec
FF
318 break;
319 case IPPROTO_UDP:
f4401262 320 nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr);
a908fdec
FF
321 break;
322 }
a908fdec
FF
323}
324
f4401262
PNA
325static void nf_flow_snat_ipv6(const struct flow_offload *flow,
326 struct sk_buff *skb, struct ipv6hdr *ip6h,
327 unsigned int thoff,
328 enum flow_offload_tuple_dir dir)
a908fdec
FF
329{
330 struct in6_addr addr, new_addr;
331
332 switch (dir) {
333 case FLOW_OFFLOAD_DIR_ORIGINAL:
334 addr = ip6h->saddr;
335 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6;
336 ip6h->saddr = new_addr;
337 break;
338 case FLOW_OFFLOAD_DIR_REPLY:
339 addr = ip6h->daddr;
340 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6;
341 ip6h->daddr = new_addr;
342 break;
a908fdec
FF
343 }
344
f4401262 345 nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
a908fdec
FF
346}
347
f4401262
PNA
348static void nf_flow_dnat_ipv6(const struct flow_offload *flow,
349 struct sk_buff *skb, struct ipv6hdr *ip6h,
350 unsigned int thoff,
351 enum flow_offload_tuple_dir dir)
a908fdec
FF
352{
353 struct in6_addr addr, new_addr;
354
355 switch (dir) {
356 case FLOW_OFFLOAD_DIR_ORIGINAL:
357 addr = ip6h->daddr;
358 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6;
359 ip6h->daddr = new_addr;
360 break;
361 case FLOW_OFFLOAD_DIR_REPLY:
362 addr = ip6h->saddr;
363 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6;
364 ip6h->saddr = new_addr;
365 break;
a908fdec
FF
366 }
367
f4401262 368 nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
a908fdec
FF
369}
370
f4401262
PNA
371static void nf_flow_nat_ipv6(const struct flow_offload *flow,
372 struct sk_buff *skb,
373 enum flow_offload_tuple_dir dir,
374 struct ipv6hdr *ip6h)
a908fdec 375{
a908fdec
FF
376 unsigned int thoff = sizeof(*ip6h);
377
f4401262
PNA
378 if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
379 nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir);
380 nf_flow_snat_ipv6(flow, skb, ip6h, thoff, dir);
381 }
382 if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
383 nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir);
384 nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir);
385 }
a908fdec
FF
386}
387
388static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,
2fc11745 389 struct flow_offload_tuple *tuple, u32 *hdrsize)
a908fdec
FF
390{
391 struct flow_ports *ports;
392 struct ipv6hdr *ip6h;
2fc11745 393 unsigned int thoff;
a908fdec
FF
394
395 if (!pskb_may_pull(skb, sizeof(*ip6h)))
396 return -1;
397
398 ip6h = ipv6_hdr(skb);
399
793d5d61
PNA
400 switch (ip6h->nexthdr) {
401 case IPPROTO_TCP:
2fc11745 402 *hdrsize = sizeof(struct tcphdr);
793d5d61
PNA
403 break;
404 case IPPROTO_UDP:
2fc11745 405 *hdrsize = sizeof(struct udphdr);
793d5d61
PNA
406 break;
407 default:
a908fdec 408 return -1;
793d5d61 409 }
a908fdec 410
33cc3c0c
TY
411 if (ip6h->hop_limit <= 1)
412 return -1;
413
a908fdec 414 thoff = sizeof(*ip6h);
2fc11745 415 if (!pskb_may_pull(skb, thoff + *hdrsize))
a908fdec
FF
416 return -1;
417
41e9ec5a 418 ip6h = ipv6_hdr(skb);
a908fdec
FF
419 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
420
421 tuple->src_v6 = ip6h->saddr;
422 tuple->dst_v6 = ip6h->daddr;
423 tuple->src_port = ports->source;
424 tuple->dst_port = ports->dest;
425 tuple->l3proto = AF_INET6;
426 tuple->l4proto = ip6h->nexthdr;
427 tuple->iifidx = dev->ifindex;
428
429 return 0;
430}
431
432unsigned int
433nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
434 const struct nf_hook_state *state)
435{
436 struct flow_offload_tuple_rhash *tuplehash;
437 struct nf_flowtable *flow_table = priv;
438 struct flow_offload_tuple tuple = {};
439 enum flow_offload_tuple_dir dir;
9b1c1ef1 440 const struct in6_addr *nexthop;
a908fdec
FF
441 struct flow_offload *flow;
442 struct net_device *outdev;
a908fdec
FF
443 struct ipv6hdr *ip6h;
444 struct rt6_info *rt;
2fc11745 445 u32 hdrsize;
a908fdec
FF
446
447 if (skb->protocol != htons(ETH_P_IPV6))
448 return NF_ACCEPT;
449
2fc11745 450 if (nf_flow_tuple_ipv6(skb, state->in, &tuple, &hdrsize) < 0)
a908fdec
FF
451 return NF_ACCEPT;
452
453 tuplehash = flow_offload_lookup(flow_table, &tuple);
454 if (tuplehash == NULL)
455 return NF_ACCEPT;
456
a908fdec
FF
457 dir = tuplehash->tuple.dir;
458 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
459 rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst_cache;
227e1e4d 460 outdev = rt->dst.dev;
a908fdec
FF
461
462 if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
463 return NF_ACCEPT;
464
33894c36
FF
465 if (nf_flow_state_check(flow, ipv6_hdr(skb)->nexthdr, skb,
466 sizeof(*ip6h)))
b6f27d32
FF
467 return NF_ACCEPT;
468
e5075c0b 469 if (!dst_check(&rt->dst, 0)) {
589b474a
FW
470 flow_offload_teardown(flow);
471 return NF_ACCEPT;
472 }
473
2fc11745 474 if (skb_try_make_writable(skb, sizeof(*ip6h) + hdrsize))
a908fdec
FF
475 return NF_DROP;
476
1b9cd769
PNA
477 flow_offload_refresh(flow_table, flow);
478
2fc11745 479 ip6h = ipv6_hdr(skb);
f4401262 480 nf_flow_nat_ipv6(flow, skb, dir, ip6h);
a908fdec 481
a908fdec 482 ip6h->hop_limit--;
de20900f 483 skb->tstamp = 0;
a908fdec 484
53c2b289
PNA
485 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
486 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
487
589b474a
FW
488 if (unlikely(dst_xfrm(&rt->dst))) {
489 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
490 IP6CB(skb)->iif = skb->dev->ifindex;
491 IP6CB(skb)->flags = IP6SKB_FORWARDED;
492 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
493 }
494
a908fdec
FF
495 skb->dev = outdev;
496 nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
2a79fd39 497 skb_dst_set_noref(skb, &rt->dst);
a908fdec
FF
498 neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
499
500 return NF_STOLEN;
501}
502EXPORT_SYMBOL_GPL(nf_flow_offload_ipv6_hook);