1 // SPDX-License-Identifier: GPL-2.0-only
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>
8 #include <linux/ipv6.h>
9 #include <linux/netdevice.h>
12 #include <net/ip6_route.h>
13 #include <net/neighbour.h>
14 #include <net/netfilter/nf_flow_table.h>
15 #include <net/netfilter/nf_conntrack_acct.h>
16 /* For layer 4 checksum field offset. */
17 #include <linux/tcp.h>
18 #include <linux/udp.h>
20 static int nf_flow_state_check(struct flow_offload *flow, int proto,
21 struct sk_buff *skb, unsigned int thoff)
25 if (proto != IPPROTO_TCP)
28 if (!pskb_may_pull(skb, thoff + sizeof(*tcph)))
31 tcph = (void *)(skb_network_header(skb) + thoff);
32 if (unlikely(tcph->fin || tcph->rst)) {
33 flow_offload_teardown(flow);
40 static int nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
41 __be32 addr, __be32 new_addr)
45 if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
46 skb_try_make_writable(skb, thoff + sizeof(*tcph)))
49 tcph = (void *)(skb_network_header(skb) + thoff);
50 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
55 static int nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
56 __be32 addr, __be32 new_addr)
60 if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
61 skb_try_make_writable(skb, thoff + sizeof(*udph)))
64 udph = (void *)(skb_network_header(skb) + thoff);
65 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
66 inet_proto_csum_replace4(&udph->check, skb, addr,
69 udph->check = CSUM_MANGLED_0;
75 static int nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
76 unsigned int thoff, __be32 addr,
79 switch (iph->protocol) {
81 if (nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr) < 0)
85 if (nf_flow_nat_ip_udp(skb, thoff, addr, new_addr) < 0)
93 static int nf_flow_snat_ip(const struct flow_offload *flow, struct sk_buff *skb,
94 struct iphdr *iph, unsigned int thoff,
95 enum flow_offload_tuple_dir dir)
97 __be32 addr, new_addr;
100 case FLOW_OFFLOAD_DIR_ORIGINAL:
102 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
103 iph->saddr = new_addr;
105 case FLOW_OFFLOAD_DIR_REPLY:
107 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
108 iph->daddr = new_addr;
113 csum_replace4(&iph->check, addr, new_addr);
115 return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
118 static int nf_flow_dnat_ip(const struct flow_offload *flow, struct sk_buff *skb,
119 struct iphdr *iph, unsigned int thoff,
120 enum flow_offload_tuple_dir dir)
122 __be32 addr, new_addr;
125 case FLOW_OFFLOAD_DIR_ORIGINAL:
127 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
128 iph->daddr = new_addr;
130 case FLOW_OFFLOAD_DIR_REPLY:
132 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
133 iph->saddr = new_addr;
138 csum_replace4(&iph->check, addr, new_addr);
140 return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
143 static int nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
144 unsigned int thoff, enum flow_offload_tuple_dir dir)
146 struct iphdr *iph = ip_hdr(skb);
148 if (test_bit(NF_FLOW_SNAT, &flow->flags) &&
149 (nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
150 nf_flow_snat_ip(flow, skb, ip_hdr(skb), thoff, dir) < 0))
154 if (test_bit(NF_FLOW_DNAT, &flow->flags) &&
155 (nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
156 nf_flow_dnat_ip(flow, skb, ip_hdr(skb), thoff, dir) < 0))
162 static bool ip_has_options(unsigned int thoff)
164 return thoff != sizeof(struct iphdr);
167 static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
168 struct flow_offload_tuple *tuple)
170 struct flow_ports *ports;
174 if (!pskb_may_pull(skb, sizeof(*iph)))
178 thoff = iph->ihl * 4;
180 if (ip_is_fragment(iph) ||
181 unlikely(ip_has_options(thoff)))
184 if (iph->protocol != IPPROTO_TCP &&
185 iph->protocol != IPPROTO_UDP)
191 thoff = iph->ihl * 4;
192 if (!pskb_may_pull(skb, thoff + sizeof(*ports)))
196 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
198 tuple->src_v4.s_addr = iph->saddr;
199 tuple->dst_v4.s_addr = iph->daddr;
200 tuple->src_port = ports->source;
201 tuple->dst_port = ports->dest;
202 tuple->l3proto = AF_INET;
203 tuple->l4proto = iph->protocol;
204 tuple->iifidx = dev->ifindex;
209 /* Based on ip_exceeds_mtu(). */
210 static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
215 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
221 static int nf_flow_offload_dst_check(struct dst_entry *dst)
223 if (unlikely(dst_xfrm(dst)))
224 return dst_check(dst, 0) ? 0 : -1;
229 static unsigned int nf_flow_xmit_xfrm(struct sk_buff *skb,
230 const struct nf_hook_state *state,
231 struct dst_entry *dst)
234 skb_dst_set_noref(skb, dst);
235 dst_output(state->net, state->sk, skb);
240 nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
241 const struct nf_hook_state *state)
243 struct flow_offload_tuple_rhash *tuplehash;
244 struct nf_flowtable *flow_table = priv;
245 struct flow_offload_tuple tuple = {};
246 enum flow_offload_tuple_dir dir;
247 struct flow_offload *flow;
248 struct net_device *outdev;
254 if (skb->protocol != htons(ETH_P_IP))
257 if (nf_flow_tuple_ip(skb, state->in, &tuple) < 0)
260 tuplehash = flow_offload_lookup(flow_table, &tuple);
261 if (tuplehash == NULL)
264 dir = tuplehash->tuple.dir;
265 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
266 rt = (struct rtable *)flow->tuplehash[dir].tuple.dst_cache;
267 outdev = rt->dst.dev;
269 if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
272 if (skb_try_make_writable(skb, sizeof(*iph)))
275 thoff = ip_hdr(skb)->ihl * 4;
276 if (nf_flow_state_check(flow, ip_hdr(skb)->protocol, skb, thoff))
279 flow_offload_refresh(flow_table, flow);
281 if (nf_flow_offload_dst_check(&rt->dst)) {
282 flow_offload_teardown(flow);
286 if (nf_flow_nat_ip(flow, skb, thoff, dir) < 0)
290 ip_decrease_ttl(iph);
293 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
294 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
296 if (unlikely(dst_xfrm(&rt->dst))) {
297 memset(skb->cb, 0, sizeof(struct inet_skb_parm));
298 IPCB(skb)->iif = skb->dev->ifindex;
299 IPCB(skb)->flags = IPSKB_FORWARDED;
300 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
304 nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
305 skb_dst_set_noref(skb, &rt->dst);
306 neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
310 EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
312 static int nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
313 struct in6_addr *addr,
314 struct in6_addr *new_addr)
318 if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
319 skb_try_make_writable(skb, thoff + sizeof(*tcph)))
322 tcph = (void *)(skb_network_header(skb) + thoff);
323 inet_proto_csum_replace16(&tcph->check, skb, addr->s6_addr32,
324 new_addr->s6_addr32, true);
329 static int nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
330 struct in6_addr *addr,
331 struct in6_addr *new_addr)
335 if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
336 skb_try_make_writable(skb, thoff + sizeof(*udph)))
339 udph = (void *)(skb_network_header(skb) + thoff);
340 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
341 inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
342 new_addr->s6_addr32, true);
344 udph->check = CSUM_MANGLED_0;
350 static int nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
351 unsigned int thoff, struct in6_addr *addr,
352 struct in6_addr *new_addr)
354 switch (ip6h->nexthdr) {
356 if (nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr) < 0)
360 if (nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr) < 0)
368 static int nf_flow_snat_ipv6(const struct flow_offload *flow,
369 struct sk_buff *skb, struct ipv6hdr *ip6h,
371 enum flow_offload_tuple_dir dir)
373 struct in6_addr addr, new_addr;
376 case FLOW_OFFLOAD_DIR_ORIGINAL:
378 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6;
379 ip6h->saddr = new_addr;
381 case FLOW_OFFLOAD_DIR_REPLY:
383 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6;
384 ip6h->daddr = new_addr;
390 return nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
393 static int nf_flow_dnat_ipv6(const struct flow_offload *flow,
394 struct sk_buff *skb, struct ipv6hdr *ip6h,
396 enum flow_offload_tuple_dir dir)
398 struct in6_addr addr, new_addr;
401 case FLOW_OFFLOAD_DIR_ORIGINAL:
403 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6;
404 ip6h->daddr = new_addr;
406 case FLOW_OFFLOAD_DIR_REPLY:
408 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6;
409 ip6h->saddr = new_addr;
415 return nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
418 static int nf_flow_nat_ipv6(const struct flow_offload *flow,
420 enum flow_offload_tuple_dir dir)
422 struct ipv6hdr *ip6h = ipv6_hdr(skb);
423 unsigned int thoff = sizeof(*ip6h);
425 if (test_bit(NF_FLOW_SNAT, &flow->flags) &&
426 (nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir) < 0 ||
427 nf_flow_snat_ipv6(flow, skb, ipv6_hdr(skb), thoff, dir) < 0))
430 ip6h = ipv6_hdr(skb);
431 if (test_bit(NF_FLOW_DNAT, &flow->flags) &&
432 (nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir) < 0 ||
433 nf_flow_dnat_ipv6(flow, skb, ipv6_hdr(skb), thoff, dir) < 0))
439 static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,
440 struct flow_offload_tuple *tuple)
442 struct flow_ports *ports;
443 struct ipv6hdr *ip6h;
446 if (!pskb_may_pull(skb, sizeof(*ip6h)))
449 ip6h = ipv6_hdr(skb);
451 if (ip6h->nexthdr != IPPROTO_TCP &&
452 ip6h->nexthdr != IPPROTO_UDP)
455 if (ip6h->hop_limit <= 1)
458 thoff = sizeof(*ip6h);
459 if (!pskb_may_pull(skb, thoff + sizeof(*ports)))
462 ip6h = ipv6_hdr(skb);
463 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
465 tuple->src_v6 = ip6h->saddr;
466 tuple->dst_v6 = ip6h->daddr;
467 tuple->src_port = ports->source;
468 tuple->dst_port = ports->dest;
469 tuple->l3proto = AF_INET6;
470 tuple->l4proto = ip6h->nexthdr;
471 tuple->iifidx = dev->ifindex;
477 nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
478 const struct nf_hook_state *state)
480 struct flow_offload_tuple_rhash *tuplehash;
481 struct nf_flowtable *flow_table = priv;
482 struct flow_offload_tuple tuple = {};
483 enum flow_offload_tuple_dir dir;
484 const struct in6_addr *nexthop;
485 struct flow_offload *flow;
486 struct net_device *outdev;
487 struct ipv6hdr *ip6h;
490 if (skb->protocol != htons(ETH_P_IPV6))
493 if (nf_flow_tuple_ipv6(skb, state->in, &tuple) < 0)
496 tuplehash = flow_offload_lookup(flow_table, &tuple);
497 if (tuplehash == NULL)
500 dir = tuplehash->tuple.dir;
501 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
502 rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst_cache;
503 outdev = rt->dst.dev;
505 if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
508 if (nf_flow_state_check(flow, ipv6_hdr(skb)->nexthdr, skb,
512 flow_offload_refresh(flow_table, flow);
514 if (nf_flow_offload_dst_check(&rt->dst)) {
515 flow_offload_teardown(flow);
519 if (skb_try_make_writable(skb, sizeof(*ip6h)))
522 if (nf_flow_nat_ipv6(flow, skb, dir) < 0)
525 ip6h = ipv6_hdr(skb);
529 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
530 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
532 if (unlikely(dst_xfrm(&rt->dst))) {
533 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
534 IP6CB(skb)->iif = skb->dev->ifindex;
535 IP6CB(skb)->flags = IP6SKB_FORWARDED;
536 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
540 nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
541 skb_dst_set_noref(skb, &rt->dst);
542 neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
546 EXPORT_SYMBOL_GPL(nf_flow_offload_ipv6_hook);