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>
10 #include <linux/if_ether.h>
13 #include <net/ip6_route.h>
14 #include <net/neighbour.h>
15 #include <net/netfilter/nf_flow_table.h>
16 #include <net/netfilter/nf_conntrack_acct.h>
17 /* For layer 4 checksum field offset. */
18 #include <linux/tcp.h>
19 #include <linux/udp.h>
21 static int nf_flow_state_check(struct flow_offload *flow, int proto,
22 struct sk_buff *skb, unsigned int thoff)
26 if (proto != IPPROTO_TCP)
29 tcph = (void *)(skb_network_header(skb) + thoff);
30 if (unlikely(tcph->fin || tcph->rst)) {
31 flow_offload_teardown(flow);
38 static void nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
39 __be32 addr, __be32 new_addr)
43 tcph = (void *)(skb_network_header(skb) + thoff);
44 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
47 static void nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
48 __be32 addr, __be32 new_addr)
52 udph = (void *)(skb_network_header(skb) + thoff);
53 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
54 inet_proto_csum_replace4(&udph->check, skb, addr,
57 udph->check = CSUM_MANGLED_0;
61 static void nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
62 unsigned int thoff, __be32 addr,
65 switch (iph->protocol) {
67 nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr);
70 nf_flow_nat_ip_udp(skb, thoff, addr, new_addr);
75 static void nf_flow_snat_ip(const struct flow_offload *flow,
76 struct sk_buff *skb, struct iphdr *iph,
77 unsigned int thoff, enum flow_offload_tuple_dir dir)
79 __be32 addr, new_addr;
82 case FLOW_OFFLOAD_DIR_ORIGINAL:
84 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
85 iph->saddr = new_addr;
87 case FLOW_OFFLOAD_DIR_REPLY:
89 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
90 iph->daddr = new_addr;
93 csum_replace4(&iph->check, addr, new_addr);
95 nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
98 static void nf_flow_dnat_ip(const struct flow_offload *flow,
99 struct sk_buff *skb, struct iphdr *iph,
100 unsigned int thoff, enum flow_offload_tuple_dir dir)
102 __be32 addr, new_addr;
105 case FLOW_OFFLOAD_DIR_ORIGINAL:
107 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
108 iph->daddr = new_addr;
110 case FLOW_OFFLOAD_DIR_REPLY:
112 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
113 iph->saddr = new_addr;
116 csum_replace4(&iph->check, addr, new_addr);
118 nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
121 static void nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
122 unsigned int thoff, enum flow_offload_tuple_dir dir,
125 if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
126 nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir);
127 nf_flow_snat_ip(flow, skb, iph, thoff, dir);
129 if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
130 nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir);
131 nf_flow_dnat_ip(flow, skb, iph, thoff, dir);
135 static bool ip_has_options(unsigned int thoff)
137 return thoff != sizeof(struct iphdr);
140 static void nf_flow_tuple_encap(struct sk_buff *skb,
141 struct flow_offload_tuple *tuple)
143 struct vlan_ethhdr *veth;
144 struct pppoe_hdr *phdr;
147 if (skb_vlan_tag_present(skb)) {
148 tuple->encap[i].id = skb_vlan_tag_get(skb);
149 tuple->encap[i].proto = skb->vlan_proto;
152 switch (skb->protocol) {
153 case htons(ETH_P_8021Q):
154 veth = (struct vlan_ethhdr *)skb_mac_header(skb);
155 tuple->encap[i].id = ntohs(veth->h_vlan_TCI);
156 tuple->encap[i].proto = skb->protocol;
158 case htons(ETH_P_PPP_SES):
159 phdr = (struct pppoe_hdr *)skb_network_header(skb);
160 tuple->encap[i].id = ntohs(phdr->sid);
161 tuple->encap[i].proto = skb->protocol;
166 static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
167 struct flow_offload_tuple *tuple, u32 *hdrsize,
170 struct flow_ports *ports;
175 if (!pskb_may_pull(skb, sizeof(*iph) + offset))
178 iph = (struct iphdr *)(skb_network_header(skb) + offset);
179 thoff = (iph->ihl * 4);
181 if (ip_is_fragment(iph) ||
182 unlikely(ip_has_options(thoff)))
187 ipproto = iph->protocol;
190 *hdrsize = sizeof(struct tcphdr);
193 *hdrsize = sizeof(struct udphdr);
195 #ifdef CONFIG_NF_CT_PROTO_GRE
197 *hdrsize = sizeof(struct gre_base_hdr);
207 if (!pskb_may_pull(skb, thoff + *hdrsize))
213 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
214 tuple->src_port = ports->source;
215 tuple->dst_port = ports->dest;
218 struct gre_base_hdr *greh;
220 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
221 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
227 iph = (struct iphdr *)(skb_network_header(skb) + offset);
229 tuple->src_v4.s_addr = iph->saddr;
230 tuple->dst_v4.s_addr = iph->daddr;
231 tuple->l3proto = AF_INET;
232 tuple->l4proto = ipproto;
233 tuple->iifidx = dev->ifindex;
234 nf_flow_tuple_encap(skb, tuple);
239 /* Based on ip_exceeds_mtu(). */
240 static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
245 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
251 static inline bool nf_flow_dst_check(struct flow_offload_tuple *tuple)
253 if (tuple->xmit_type != FLOW_OFFLOAD_XMIT_NEIGH &&
254 tuple->xmit_type != FLOW_OFFLOAD_XMIT_XFRM)
257 return dst_check(tuple->dst_cache, tuple->dst_cookie);
260 static unsigned int nf_flow_xmit_xfrm(struct sk_buff *skb,
261 const struct nf_hook_state *state,
262 struct dst_entry *dst)
265 skb_dst_set_noref(skb, dst);
266 dst_output(state->net, state->sk, skb);
270 static bool nf_flow_skb_encap_protocol(struct sk_buff *skb, __be16 proto,
273 struct vlan_ethhdr *veth;
276 switch (skb->protocol) {
277 case htons(ETH_P_8021Q):
278 veth = (struct vlan_ethhdr *)skb_mac_header(skb);
279 if (veth->h_vlan_encapsulated_proto == proto) {
280 *offset += VLAN_HLEN;
284 case htons(ETH_P_PPP_SES):
285 if (nf_flow_pppoe_proto(skb, &inner_proto) &&
286 inner_proto == proto) {
287 *offset += PPPOE_SES_HLEN;
296 static void nf_flow_encap_pop(struct sk_buff *skb,
297 struct flow_offload_tuple_rhash *tuplehash)
299 struct vlan_hdr *vlan_hdr;
302 for (i = 0; i < tuplehash->tuple.encap_num; i++) {
303 if (skb_vlan_tag_present(skb)) {
304 __vlan_hwaccel_clear_tag(skb);
307 switch (skb->protocol) {
308 case htons(ETH_P_8021Q):
309 vlan_hdr = (struct vlan_hdr *)skb->data;
310 __skb_pull(skb, VLAN_HLEN);
311 vlan_set_encap_proto(skb, vlan_hdr);
312 skb_reset_network_header(skb);
314 case htons(ETH_P_PPP_SES):
315 skb->protocol = __nf_flow_pppoe_proto(skb);
316 skb_pull(skb, PPPOE_SES_HLEN);
317 skb_reset_network_header(skb);
323 static unsigned int nf_flow_queue_xmit(struct net *net, struct sk_buff *skb,
324 const struct flow_offload_tuple_rhash *tuplehash,
327 struct net_device *outdev;
329 outdev = dev_get_by_index_rcu(net, tuplehash->tuple.out.ifidx);
334 dev_hard_header(skb, skb->dev, type, tuplehash->tuple.out.h_dest,
335 tuplehash->tuple.out.h_source, skb->len);
342 nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
343 const struct nf_hook_state *state)
345 struct flow_offload_tuple_rhash *tuplehash;
346 struct nf_flowtable *flow_table = priv;
347 struct flow_offload_tuple tuple = {};
348 enum flow_offload_tuple_dir dir;
349 struct flow_offload *flow;
350 struct net_device *outdev;
351 u32 hdrsize, offset = 0;
352 unsigned int thoff, mtu;
358 if (skb->protocol != htons(ETH_P_IP) &&
359 !nf_flow_skb_encap_protocol(skb, htons(ETH_P_IP), &offset))
362 if (nf_flow_tuple_ip(skb, state->in, &tuple, &hdrsize, offset) < 0)
365 tuplehash = flow_offload_lookup(flow_table, &tuple);
366 if (tuplehash == NULL)
369 dir = tuplehash->tuple.dir;
370 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
372 mtu = flow->tuplehash[dir].tuple.mtu + offset;
373 if (unlikely(nf_flow_exceeds_mtu(skb, mtu)))
376 iph = (struct iphdr *)(skb_network_header(skb) + offset);
377 thoff = (iph->ihl * 4) + offset;
378 if (nf_flow_state_check(flow, iph->protocol, skb, thoff))
381 if (!nf_flow_dst_check(&tuplehash->tuple)) {
382 flow_offload_teardown(flow);
386 if (skb_try_make_writable(skb, thoff + hdrsize))
389 flow_offload_refresh(flow_table, flow, false);
391 nf_flow_encap_pop(skb, tuplehash);
395 nf_flow_nat_ip(flow, skb, thoff, dir, iph);
397 ip_decrease_ttl(iph);
398 skb_clear_tstamp(skb);
400 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
401 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
403 if (unlikely(tuplehash->tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)) {
404 rt = (struct rtable *)tuplehash->tuple.dst_cache;
405 memset(skb->cb, 0, sizeof(struct inet_skb_parm));
406 IPCB(skb)->iif = skb->dev->ifindex;
407 IPCB(skb)->flags = IPSKB_FORWARDED;
408 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
411 switch (tuplehash->tuple.xmit_type) {
412 case FLOW_OFFLOAD_XMIT_NEIGH:
413 rt = (struct rtable *)tuplehash->tuple.dst_cache;
414 outdev = rt->dst.dev;
416 nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
417 skb_dst_set_noref(skb, &rt->dst);
418 neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
421 case FLOW_OFFLOAD_XMIT_DIRECT:
422 ret = nf_flow_queue_xmit(state->net, skb, tuplehash, ETH_P_IP);
424 flow_offload_teardown(flow);
430 EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
432 static void nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
433 struct in6_addr *addr,
434 struct in6_addr *new_addr,
435 struct ipv6hdr *ip6h)
439 tcph = (void *)(skb_network_header(skb) + thoff);
440 inet_proto_csum_replace16(&tcph->check, skb, addr->s6_addr32,
441 new_addr->s6_addr32, true);
444 static void nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
445 struct in6_addr *addr,
446 struct in6_addr *new_addr)
450 udph = (void *)(skb_network_header(skb) + thoff);
451 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
452 inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
453 new_addr->s6_addr32, true);
455 udph->check = CSUM_MANGLED_0;
459 static void nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
460 unsigned int thoff, struct in6_addr *addr,
461 struct in6_addr *new_addr)
463 switch (ip6h->nexthdr) {
465 nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr, ip6h);
468 nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr);
473 static void nf_flow_snat_ipv6(const struct flow_offload *flow,
474 struct sk_buff *skb, struct ipv6hdr *ip6h,
476 enum flow_offload_tuple_dir dir)
478 struct in6_addr addr, new_addr;
481 case FLOW_OFFLOAD_DIR_ORIGINAL:
483 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6;
484 ip6h->saddr = new_addr;
486 case FLOW_OFFLOAD_DIR_REPLY:
488 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6;
489 ip6h->daddr = new_addr;
493 nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
496 static void nf_flow_dnat_ipv6(const struct flow_offload *flow,
497 struct sk_buff *skb, struct ipv6hdr *ip6h,
499 enum flow_offload_tuple_dir dir)
501 struct in6_addr addr, new_addr;
504 case FLOW_OFFLOAD_DIR_ORIGINAL:
506 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6;
507 ip6h->daddr = new_addr;
509 case FLOW_OFFLOAD_DIR_REPLY:
511 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6;
512 ip6h->saddr = new_addr;
516 nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
519 static void nf_flow_nat_ipv6(const struct flow_offload *flow,
521 enum flow_offload_tuple_dir dir,
522 struct ipv6hdr *ip6h)
524 unsigned int thoff = sizeof(*ip6h);
526 if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
527 nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir);
528 nf_flow_snat_ipv6(flow, skb, ip6h, thoff, dir);
530 if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
531 nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir);
532 nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir);
536 static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,
537 struct flow_offload_tuple *tuple, u32 *hdrsize,
540 struct flow_ports *ports;
541 struct ipv6hdr *ip6h;
545 thoff = sizeof(*ip6h) + offset;
546 if (!pskb_may_pull(skb, thoff))
549 ip6h = (struct ipv6hdr *)(skb_network_header(skb) + offset);
551 nexthdr = ip6h->nexthdr;
554 *hdrsize = sizeof(struct tcphdr);
557 *hdrsize = sizeof(struct udphdr);
559 #ifdef CONFIG_NF_CT_PROTO_GRE
561 *hdrsize = sizeof(struct gre_base_hdr);
568 if (ip6h->hop_limit <= 1)
571 if (!pskb_may_pull(skb, thoff + *hdrsize))
577 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
578 tuple->src_port = ports->source;
579 tuple->dst_port = ports->dest;
582 struct gre_base_hdr *greh;
584 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
585 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
591 ip6h = (struct ipv6hdr *)(skb_network_header(skb) + offset);
593 tuple->src_v6 = ip6h->saddr;
594 tuple->dst_v6 = ip6h->daddr;
595 tuple->l3proto = AF_INET6;
596 tuple->l4proto = nexthdr;
597 tuple->iifidx = dev->ifindex;
598 nf_flow_tuple_encap(skb, tuple);
604 nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
605 const struct nf_hook_state *state)
607 struct flow_offload_tuple_rhash *tuplehash;
608 struct nf_flowtable *flow_table = priv;
609 struct flow_offload_tuple tuple = {};
610 enum flow_offload_tuple_dir dir;
611 const struct in6_addr *nexthop;
612 struct flow_offload *flow;
613 struct net_device *outdev;
614 unsigned int thoff, mtu;
615 u32 hdrsize, offset = 0;
616 struct ipv6hdr *ip6h;
620 if (skb->protocol != htons(ETH_P_IPV6) &&
621 !nf_flow_skb_encap_protocol(skb, htons(ETH_P_IPV6), &offset))
624 if (nf_flow_tuple_ipv6(skb, state->in, &tuple, &hdrsize, offset) < 0)
627 tuplehash = flow_offload_lookup(flow_table, &tuple);
628 if (tuplehash == NULL)
631 dir = tuplehash->tuple.dir;
632 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
634 mtu = flow->tuplehash[dir].tuple.mtu + offset;
635 if (unlikely(nf_flow_exceeds_mtu(skb, mtu)))
638 ip6h = (struct ipv6hdr *)(skb_network_header(skb) + offset);
639 thoff = sizeof(*ip6h) + offset;
640 if (nf_flow_state_check(flow, ip6h->nexthdr, skb, thoff))
643 if (!nf_flow_dst_check(&tuplehash->tuple)) {
644 flow_offload_teardown(flow);
648 if (skb_try_make_writable(skb, thoff + hdrsize))
651 flow_offload_refresh(flow_table, flow, false);
653 nf_flow_encap_pop(skb, tuplehash);
655 ip6h = ipv6_hdr(skb);
656 nf_flow_nat_ipv6(flow, skb, dir, ip6h);
659 skb_clear_tstamp(skb);
661 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
662 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
664 if (unlikely(tuplehash->tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)) {
665 rt = (struct rt6_info *)tuplehash->tuple.dst_cache;
666 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
667 IP6CB(skb)->iif = skb->dev->ifindex;
668 IP6CB(skb)->flags = IP6SKB_FORWARDED;
669 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
672 switch (tuplehash->tuple.xmit_type) {
673 case FLOW_OFFLOAD_XMIT_NEIGH:
674 rt = (struct rt6_info *)tuplehash->tuple.dst_cache;
675 outdev = rt->dst.dev;
677 nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
678 skb_dst_set_noref(skb, &rt->dst);
679 neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
682 case FLOW_OFFLOAD_XMIT_DIRECT:
683 ret = nf_flow_queue_xmit(state->net, skb, tuplehash, ETH_P_IPV6);
685 flow_offload_teardown(flow);
691 EXPORT_SYMBOL_GPL(nf_flow_offload_ipv6_hook);