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_mac_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;
174 if (!pskb_may_pull(skb, sizeof(*iph) + offset))
177 iph = (struct iphdr *)(skb_network_header(skb) + offset);
178 thoff = (iph->ihl * 4);
180 if (ip_is_fragment(iph) ||
181 unlikely(ip_has_options(thoff)))
186 switch (iph->protocol) {
188 *hdrsize = sizeof(struct tcphdr);
191 *hdrsize = sizeof(struct udphdr);
200 if (!pskb_may_pull(skb, thoff + *hdrsize))
203 iph = (struct iphdr *)(skb_network_header(skb) + offset);
204 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
206 tuple->src_v4.s_addr = iph->saddr;
207 tuple->dst_v4.s_addr = iph->daddr;
208 tuple->src_port = ports->source;
209 tuple->dst_port = ports->dest;
210 tuple->l3proto = AF_INET;
211 tuple->l4proto = iph->protocol;
212 tuple->iifidx = dev->ifindex;
213 nf_flow_tuple_encap(skb, tuple);
218 /* Based on ip_exceeds_mtu(). */
219 static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
224 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
230 static inline bool nf_flow_dst_check(struct flow_offload_tuple *tuple)
232 if (tuple->xmit_type != FLOW_OFFLOAD_XMIT_NEIGH &&
233 tuple->xmit_type != FLOW_OFFLOAD_XMIT_XFRM)
236 return dst_check(tuple->dst_cache, tuple->dst_cookie);
239 static unsigned int nf_flow_xmit_xfrm(struct sk_buff *skb,
240 const struct nf_hook_state *state,
241 struct dst_entry *dst)
244 skb_dst_set_noref(skb, dst);
245 dst_output(state->net, state->sk, skb);
249 static bool nf_flow_skb_encap_protocol(const struct sk_buff *skb, __be16 proto,
252 struct vlan_ethhdr *veth;
254 switch (skb->protocol) {
255 case htons(ETH_P_8021Q):
256 veth = (struct vlan_ethhdr *)skb_mac_header(skb);
257 if (veth->h_vlan_encapsulated_proto == proto) {
258 *offset += VLAN_HLEN;
262 case htons(ETH_P_PPP_SES):
263 if (nf_flow_pppoe_proto(skb) == proto) {
264 *offset += PPPOE_SES_HLEN;
273 static void nf_flow_encap_pop(struct sk_buff *skb,
274 struct flow_offload_tuple_rhash *tuplehash)
276 struct vlan_hdr *vlan_hdr;
279 for (i = 0; i < tuplehash->tuple.encap_num; i++) {
280 if (skb_vlan_tag_present(skb)) {
281 __vlan_hwaccel_clear_tag(skb);
284 switch (skb->protocol) {
285 case htons(ETH_P_8021Q):
286 vlan_hdr = (struct vlan_hdr *)skb->data;
287 __skb_pull(skb, VLAN_HLEN);
288 vlan_set_encap_proto(skb, vlan_hdr);
289 skb_reset_network_header(skb);
291 case htons(ETH_P_PPP_SES):
292 skb->protocol = nf_flow_pppoe_proto(skb);
293 skb_pull(skb, PPPOE_SES_HLEN);
294 skb_reset_network_header(skb);
300 static unsigned int nf_flow_queue_xmit(struct net *net, struct sk_buff *skb,
301 const struct flow_offload_tuple_rhash *tuplehash,
304 struct net_device *outdev;
306 outdev = dev_get_by_index_rcu(net, tuplehash->tuple.out.ifidx);
311 dev_hard_header(skb, skb->dev, type, tuplehash->tuple.out.h_dest,
312 tuplehash->tuple.out.h_source, skb->len);
319 nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
320 const struct nf_hook_state *state)
322 struct flow_offload_tuple_rhash *tuplehash;
323 struct nf_flowtable *flow_table = priv;
324 struct flow_offload_tuple tuple = {};
325 enum flow_offload_tuple_dir dir;
326 struct flow_offload *flow;
327 struct net_device *outdev;
328 u32 hdrsize, offset = 0;
329 unsigned int thoff, mtu;
335 if (skb->protocol != htons(ETH_P_IP) &&
336 !nf_flow_skb_encap_protocol(skb, htons(ETH_P_IP), &offset))
339 if (nf_flow_tuple_ip(skb, state->in, &tuple, &hdrsize, offset) < 0)
342 tuplehash = flow_offload_lookup(flow_table, &tuple);
343 if (tuplehash == NULL)
346 dir = tuplehash->tuple.dir;
347 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
349 mtu = flow->tuplehash[dir].tuple.mtu + offset;
350 if (unlikely(nf_flow_exceeds_mtu(skb, mtu)))
353 iph = (struct iphdr *)(skb_network_header(skb) + offset);
354 thoff = (iph->ihl * 4) + offset;
355 if (nf_flow_state_check(flow, iph->protocol, skb, thoff))
358 if (!nf_flow_dst_check(&tuplehash->tuple)) {
359 flow_offload_teardown(flow);
363 if (skb_try_make_writable(skb, thoff + hdrsize))
366 flow_offload_refresh(flow_table, flow);
368 nf_flow_encap_pop(skb, tuplehash);
372 nf_flow_nat_ip(flow, skb, thoff, dir, iph);
374 ip_decrease_ttl(iph);
377 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
378 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
380 if (unlikely(tuplehash->tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)) {
381 rt = (struct rtable *)tuplehash->tuple.dst_cache;
382 memset(skb->cb, 0, sizeof(struct inet_skb_parm));
383 IPCB(skb)->iif = skb->dev->ifindex;
384 IPCB(skb)->flags = IPSKB_FORWARDED;
385 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
388 switch (tuplehash->tuple.xmit_type) {
389 case FLOW_OFFLOAD_XMIT_NEIGH:
390 rt = (struct rtable *)tuplehash->tuple.dst_cache;
391 outdev = rt->dst.dev;
393 nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
394 skb_dst_set_noref(skb, &rt->dst);
395 neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
398 case FLOW_OFFLOAD_XMIT_DIRECT:
399 ret = nf_flow_queue_xmit(state->net, skb, tuplehash, ETH_P_IP);
401 flow_offload_teardown(flow);
407 EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
409 static void nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
410 struct in6_addr *addr,
411 struct in6_addr *new_addr,
412 struct ipv6hdr *ip6h)
416 tcph = (void *)(skb_network_header(skb) + thoff);
417 inet_proto_csum_replace16(&tcph->check, skb, addr->s6_addr32,
418 new_addr->s6_addr32, true);
421 static void nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
422 struct in6_addr *addr,
423 struct in6_addr *new_addr)
427 udph = (void *)(skb_network_header(skb) + thoff);
428 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
429 inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
430 new_addr->s6_addr32, true);
432 udph->check = CSUM_MANGLED_0;
436 static void nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
437 unsigned int thoff, struct in6_addr *addr,
438 struct in6_addr *new_addr)
440 switch (ip6h->nexthdr) {
442 nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr, ip6h);
445 nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr);
450 static void nf_flow_snat_ipv6(const struct flow_offload *flow,
451 struct sk_buff *skb, struct ipv6hdr *ip6h,
453 enum flow_offload_tuple_dir dir)
455 struct in6_addr addr, new_addr;
458 case FLOW_OFFLOAD_DIR_ORIGINAL:
460 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6;
461 ip6h->saddr = new_addr;
463 case FLOW_OFFLOAD_DIR_REPLY:
465 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6;
466 ip6h->daddr = new_addr;
470 nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
473 static void nf_flow_dnat_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.src_v6;
484 ip6h->daddr = new_addr;
486 case FLOW_OFFLOAD_DIR_REPLY:
488 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6;
489 ip6h->saddr = new_addr;
493 nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
496 static void nf_flow_nat_ipv6(const struct flow_offload *flow,
498 enum flow_offload_tuple_dir dir,
499 struct ipv6hdr *ip6h)
501 unsigned int thoff = sizeof(*ip6h);
503 if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
504 nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir);
505 nf_flow_snat_ipv6(flow, skb, ip6h, thoff, dir);
507 if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
508 nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir);
509 nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir);
513 static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,
514 struct flow_offload_tuple *tuple, u32 *hdrsize,
517 struct flow_ports *ports;
518 struct ipv6hdr *ip6h;
521 thoff = sizeof(*ip6h) + offset;
522 if (!pskb_may_pull(skb, thoff))
525 ip6h = (struct ipv6hdr *)(skb_network_header(skb) + offset);
527 switch (ip6h->nexthdr) {
529 *hdrsize = sizeof(struct tcphdr);
532 *hdrsize = sizeof(struct udphdr);
538 if (ip6h->hop_limit <= 1)
541 if (!pskb_may_pull(skb, thoff + *hdrsize))
544 ip6h = (struct ipv6hdr *)(skb_network_header(skb) + offset);
545 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
547 tuple->src_v6 = ip6h->saddr;
548 tuple->dst_v6 = ip6h->daddr;
549 tuple->src_port = ports->source;
550 tuple->dst_port = ports->dest;
551 tuple->l3proto = AF_INET6;
552 tuple->l4proto = ip6h->nexthdr;
553 tuple->iifidx = dev->ifindex;
554 nf_flow_tuple_encap(skb, tuple);
560 nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
561 const struct nf_hook_state *state)
563 struct flow_offload_tuple_rhash *tuplehash;
564 struct nf_flowtable *flow_table = priv;
565 struct flow_offload_tuple tuple = {};
566 enum flow_offload_tuple_dir dir;
567 const struct in6_addr *nexthop;
568 struct flow_offload *flow;
569 struct net_device *outdev;
570 unsigned int thoff, mtu;
571 u32 hdrsize, offset = 0;
572 struct ipv6hdr *ip6h;
576 if (skb->protocol != htons(ETH_P_IPV6) &&
577 !nf_flow_skb_encap_protocol(skb, htons(ETH_P_IPV6), &offset))
580 if (nf_flow_tuple_ipv6(skb, state->in, &tuple, &hdrsize, offset) < 0)
583 tuplehash = flow_offload_lookup(flow_table, &tuple);
584 if (tuplehash == NULL)
587 dir = tuplehash->tuple.dir;
588 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
590 mtu = flow->tuplehash[dir].tuple.mtu + offset;
591 if (unlikely(nf_flow_exceeds_mtu(skb, mtu)))
594 ip6h = (struct ipv6hdr *)(skb_network_header(skb) + offset);
595 thoff = sizeof(*ip6h) + offset;
596 if (nf_flow_state_check(flow, ip6h->nexthdr, skb, thoff))
599 if (!nf_flow_dst_check(&tuplehash->tuple)) {
600 flow_offload_teardown(flow);
604 if (skb_try_make_writable(skb, thoff + hdrsize))
607 flow_offload_refresh(flow_table, flow);
609 nf_flow_encap_pop(skb, tuplehash);
611 ip6h = ipv6_hdr(skb);
612 nf_flow_nat_ipv6(flow, skb, dir, ip6h);
617 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
618 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
620 if (unlikely(tuplehash->tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)) {
621 rt = (struct rt6_info *)tuplehash->tuple.dst_cache;
622 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
623 IP6CB(skb)->iif = skb->dev->ifindex;
624 IP6CB(skb)->flags = IP6SKB_FORWARDED;
625 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
628 switch (tuplehash->tuple.xmit_type) {
629 case FLOW_OFFLOAD_XMIT_NEIGH:
630 rt = (struct rt6_info *)tuplehash->tuple.dst_cache;
631 outdev = rt->dst.dev;
633 nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
634 skb_dst_set_noref(skb, &rt->dst);
635 neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
638 case FLOW_OFFLOAD_XMIT_DIRECT:
639 ret = nf_flow_queue_xmit(state->net, skb, tuplehash, ETH_P_IPV6);
641 flow_offload_teardown(flow);
647 EXPORT_SYMBOL_GPL(nf_flow_offload_ipv6_hook);