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>
14 #include <net/ip6_route.h>
15 #include <net/neighbour.h>
16 #include <net/netfilter/nf_flow_table.h>
17 #include <net/netfilter/nf_conntrack_acct.h>
18 /* For layer 4 checksum field offset. */
19 #include <linux/tcp.h>
20 #include <linux/udp.h>
22 static int nf_flow_state_check(struct flow_offload *flow, int proto,
23 struct sk_buff *skb, unsigned int thoff)
27 if (proto != IPPROTO_TCP)
30 tcph = (void *)(skb_network_header(skb) + thoff);
31 if (unlikely(tcph->fin || tcph->rst)) {
32 flow_offload_teardown(flow);
39 static void nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
40 __be32 addr, __be32 new_addr)
44 tcph = (void *)(skb_network_header(skb) + thoff);
45 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
48 static void nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
49 __be32 addr, __be32 new_addr)
53 udph = (void *)(skb_network_header(skb) + thoff);
54 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
55 inet_proto_csum_replace4(&udph->check, skb, addr,
58 udph->check = CSUM_MANGLED_0;
62 static void nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
63 unsigned int thoff, __be32 addr,
66 switch (iph->protocol) {
68 nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr);
71 nf_flow_nat_ip_udp(skb, thoff, addr, new_addr);
76 static void nf_flow_snat_ip(const struct flow_offload *flow,
77 struct sk_buff *skb, struct iphdr *iph,
78 unsigned int thoff, enum flow_offload_tuple_dir dir)
80 __be32 addr, new_addr;
83 case FLOW_OFFLOAD_DIR_ORIGINAL:
85 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
86 iph->saddr = new_addr;
88 case FLOW_OFFLOAD_DIR_REPLY:
90 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
91 iph->daddr = new_addr;
94 csum_replace4(&iph->check, addr, new_addr);
96 nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
99 static void nf_flow_dnat_ip(const struct flow_offload *flow,
100 struct sk_buff *skb, struct iphdr *iph,
101 unsigned int thoff, enum flow_offload_tuple_dir dir)
103 __be32 addr, new_addr;
106 case FLOW_OFFLOAD_DIR_ORIGINAL:
108 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
109 iph->daddr = new_addr;
111 case FLOW_OFFLOAD_DIR_REPLY:
113 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
114 iph->saddr = new_addr;
117 csum_replace4(&iph->check, addr, new_addr);
119 nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
122 static void nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
123 unsigned int thoff, enum flow_offload_tuple_dir dir,
126 if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
127 nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir);
128 nf_flow_snat_ip(flow, skb, iph, thoff, dir);
130 if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
131 nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir);
132 nf_flow_dnat_ip(flow, skb, iph, thoff, dir);
136 static bool ip_has_options(unsigned int thoff)
138 return thoff != sizeof(struct iphdr);
141 static void nf_flow_tuple_encap(struct sk_buff *skb,
142 struct flow_offload_tuple *tuple)
144 struct vlan_ethhdr *veth;
145 struct pppoe_hdr *phdr;
148 if (skb_vlan_tag_present(skb)) {
149 tuple->encap[i].id = skb_vlan_tag_get(skb);
150 tuple->encap[i].proto = skb->vlan_proto;
153 switch (skb->protocol) {
154 case htons(ETH_P_8021Q):
155 veth = (struct vlan_ethhdr *)skb_mac_header(skb);
156 tuple->encap[i].id = ntohs(veth->h_vlan_TCI);
157 tuple->encap[i].proto = skb->protocol;
159 case htons(ETH_P_PPP_SES):
160 phdr = (struct pppoe_hdr *)skb_mac_header(skb);
161 tuple->encap[i].id = ntohs(phdr->sid);
162 tuple->encap[i].proto = skb->protocol;
167 struct nf_flowtable_ctx {
168 const struct net_device *in;
173 static int nf_flow_tuple_ip(struct nf_flowtable_ctx *ctx, struct sk_buff *skb,
174 struct flow_offload_tuple *tuple)
176 struct flow_ports *ports;
181 if (!pskb_may_pull(skb, sizeof(*iph) + ctx->offset))
184 iph = (struct iphdr *)(skb_network_header(skb) + ctx->offset);
185 thoff = (iph->ihl * 4);
187 if (ip_is_fragment(iph) ||
188 unlikely(ip_has_options(thoff)))
191 thoff += ctx->offset;
193 ipproto = iph->protocol;
196 ctx->hdrsize = sizeof(struct tcphdr);
199 ctx->hdrsize = sizeof(struct udphdr);
201 #ifdef CONFIG_NF_CT_PROTO_GRE
203 ctx->hdrsize = sizeof(struct gre_base_hdr);
213 if (!pskb_may_pull(skb, thoff + ctx->hdrsize))
219 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
220 tuple->src_port = ports->source;
221 tuple->dst_port = ports->dest;
224 struct gre_base_hdr *greh;
226 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
227 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
233 iph = (struct iphdr *)(skb_network_header(skb) + ctx->offset);
235 tuple->src_v4.s_addr = iph->saddr;
236 tuple->dst_v4.s_addr = iph->daddr;
237 tuple->l3proto = AF_INET;
238 tuple->l4proto = ipproto;
239 tuple->iifidx = ctx->in->ifindex;
240 nf_flow_tuple_encap(skb, tuple);
245 /* Based on ip_exceeds_mtu(). */
246 static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
251 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
257 static inline bool nf_flow_dst_check(struct flow_offload_tuple *tuple)
259 if (tuple->xmit_type != FLOW_OFFLOAD_XMIT_NEIGH &&
260 tuple->xmit_type != FLOW_OFFLOAD_XMIT_XFRM)
263 return dst_check(tuple->dst_cache, tuple->dst_cookie);
266 static unsigned int nf_flow_xmit_xfrm(struct sk_buff *skb,
267 const struct nf_hook_state *state,
268 struct dst_entry *dst)
271 skb_dst_set_noref(skb, dst);
272 dst_output(state->net, state->sk, skb);
276 static bool nf_flow_skb_encap_protocol(const struct sk_buff *skb, __be16 proto,
279 struct vlan_ethhdr *veth;
281 switch (skb->protocol) {
282 case htons(ETH_P_8021Q):
283 veth = (struct vlan_ethhdr *)skb_mac_header(skb);
284 if (veth->h_vlan_encapsulated_proto == proto) {
285 *offset += VLAN_HLEN;
289 case htons(ETH_P_PPP_SES):
290 if (nf_flow_pppoe_proto(skb) == proto) {
291 *offset += PPPOE_SES_HLEN;
300 static void nf_flow_encap_pop(struct sk_buff *skb,
301 struct flow_offload_tuple_rhash *tuplehash)
303 struct vlan_hdr *vlan_hdr;
306 for (i = 0; i < tuplehash->tuple.encap_num; i++) {
307 if (skb_vlan_tag_present(skb)) {
308 __vlan_hwaccel_clear_tag(skb);
311 switch (skb->protocol) {
312 case htons(ETH_P_8021Q):
313 vlan_hdr = (struct vlan_hdr *)skb->data;
314 __skb_pull(skb, VLAN_HLEN);
315 vlan_set_encap_proto(skb, vlan_hdr);
316 skb_reset_network_header(skb);
318 case htons(ETH_P_PPP_SES):
319 skb->protocol = nf_flow_pppoe_proto(skb);
320 skb_pull(skb, PPPOE_SES_HLEN);
321 skb_reset_network_header(skb);
327 static unsigned int nf_flow_queue_xmit(struct net *net, struct sk_buff *skb,
328 const struct flow_offload_tuple_rhash *tuplehash,
331 struct net_device *outdev;
333 outdev = dev_get_by_index_rcu(net, tuplehash->tuple.out.ifidx);
338 dev_hard_header(skb, skb->dev, type, tuplehash->tuple.out.h_dest,
339 tuplehash->tuple.out.h_source, skb->len);
345 static struct flow_offload_tuple_rhash *
346 nf_flow_offload_lookup(struct nf_flowtable_ctx *ctx,
347 struct nf_flowtable *flow_table, struct sk_buff *skb)
349 struct flow_offload_tuple tuple = {};
351 if (skb->protocol != htons(ETH_P_IP) &&
352 !nf_flow_skb_encap_protocol(skb, htons(ETH_P_IP), &ctx->offset))
355 if (nf_flow_tuple_ip(ctx, skb, &tuple) < 0)
358 return flow_offload_lookup(flow_table, &tuple);
361 static int nf_flow_offload_forward(struct nf_flowtable_ctx *ctx,
362 struct nf_flowtable *flow_table,
363 struct flow_offload_tuple_rhash *tuplehash,
366 enum flow_offload_tuple_dir dir;
367 struct flow_offload *flow;
368 unsigned int thoff, mtu;
371 dir = tuplehash->tuple.dir;
372 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
374 mtu = flow->tuplehash[dir].tuple.mtu + ctx->offset;
375 if (unlikely(nf_flow_exceeds_mtu(skb, mtu)))
378 iph = (struct iphdr *)(skb_network_header(skb) + ctx->offset);
379 thoff = (iph->ihl * 4) + ctx->offset;
380 if (nf_flow_state_check(flow, iph->protocol, skb, thoff))
383 if (!nf_flow_dst_check(&tuplehash->tuple)) {
384 flow_offload_teardown(flow);
388 if (skb_try_make_writable(skb, thoff + ctx->hdrsize))
391 flow_offload_refresh(flow_table, flow, false);
393 nf_flow_encap_pop(skb, tuplehash);
394 thoff -= ctx->offset;
397 nf_flow_nat_ip(flow, skb, thoff, dir, iph);
399 ip_decrease_ttl(iph);
400 skb_clear_tstamp(skb);
402 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
403 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
409 nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
410 const struct nf_hook_state *state)
412 struct flow_offload_tuple_rhash *tuplehash;
413 struct nf_flowtable *flow_table = priv;
414 enum flow_offload_tuple_dir dir;
415 struct nf_flowtable_ctx ctx = {
418 struct flow_offload *flow;
419 struct net_device *outdev;
424 tuplehash = nf_flow_offload_lookup(&ctx, flow_table, skb);
428 ret = nf_flow_offload_forward(&ctx, flow_table, tuplehash, skb);
434 if (unlikely(tuplehash->tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)) {
435 rt = (struct rtable *)tuplehash->tuple.dst_cache;
436 memset(skb->cb, 0, sizeof(struct inet_skb_parm));
437 IPCB(skb)->iif = skb->dev->ifindex;
438 IPCB(skb)->flags = IPSKB_FORWARDED;
439 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
442 dir = tuplehash->tuple.dir;
443 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
445 switch (tuplehash->tuple.xmit_type) {
446 case FLOW_OFFLOAD_XMIT_NEIGH:
447 rt = (struct rtable *)tuplehash->tuple.dst_cache;
448 outdev = rt->dst.dev;
450 nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
451 skb_dst_set_noref(skb, &rt->dst);
452 neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
455 case FLOW_OFFLOAD_XMIT_DIRECT:
456 ret = nf_flow_queue_xmit(state->net, skb, tuplehash, ETH_P_IP);
458 flow_offload_teardown(flow);
468 EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
470 static void nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
471 struct in6_addr *addr,
472 struct in6_addr *new_addr,
473 struct ipv6hdr *ip6h)
477 tcph = (void *)(skb_network_header(skb) + thoff);
478 inet_proto_csum_replace16(&tcph->check, skb, addr->s6_addr32,
479 new_addr->s6_addr32, true);
482 static void nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
483 struct in6_addr *addr,
484 struct in6_addr *new_addr)
488 udph = (void *)(skb_network_header(skb) + thoff);
489 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
490 inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
491 new_addr->s6_addr32, true);
493 udph->check = CSUM_MANGLED_0;
497 static void nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
498 unsigned int thoff, struct in6_addr *addr,
499 struct in6_addr *new_addr)
501 switch (ip6h->nexthdr) {
503 nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr, ip6h);
506 nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr);
511 static void nf_flow_snat_ipv6(const struct flow_offload *flow,
512 struct sk_buff *skb, struct ipv6hdr *ip6h,
514 enum flow_offload_tuple_dir dir)
516 struct in6_addr addr, new_addr;
519 case FLOW_OFFLOAD_DIR_ORIGINAL:
521 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6;
522 ip6h->saddr = new_addr;
524 case FLOW_OFFLOAD_DIR_REPLY:
526 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6;
527 ip6h->daddr = new_addr;
531 nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
534 static void nf_flow_dnat_ipv6(const struct flow_offload *flow,
535 struct sk_buff *skb, struct ipv6hdr *ip6h,
537 enum flow_offload_tuple_dir dir)
539 struct in6_addr addr, new_addr;
542 case FLOW_OFFLOAD_DIR_ORIGINAL:
544 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6;
545 ip6h->daddr = new_addr;
547 case FLOW_OFFLOAD_DIR_REPLY:
549 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6;
550 ip6h->saddr = new_addr;
554 nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
557 static void nf_flow_nat_ipv6(const struct flow_offload *flow,
559 enum flow_offload_tuple_dir dir,
560 struct ipv6hdr *ip6h)
562 unsigned int thoff = sizeof(*ip6h);
564 if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
565 nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir);
566 nf_flow_snat_ipv6(flow, skb, ip6h, thoff, dir);
568 if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
569 nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir);
570 nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir);
574 static int nf_flow_tuple_ipv6(struct nf_flowtable_ctx *ctx, struct sk_buff *skb,
575 struct flow_offload_tuple *tuple)
577 struct flow_ports *ports;
578 struct ipv6hdr *ip6h;
582 thoff = sizeof(*ip6h) + ctx->offset;
583 if (!pskb_may_pull(skb, thoff))
586 ip6h = (struct ipv6hdr *)(skb_network_header(skb) + ctx->offset);
588 nexthdr = ip6h->nexthdr;
591 ctx->hdrsize = sizeof(struct tcphdr);
594 ctx->hdrsize = sizeof(struct udphdr);
596 #ifdef CONFIG_NF_CT_PROTO_GRE
598 ctx->hdrsize = sizeof(struct gre_base_hdr);
605 if (ip6h->hop_limit <= 1)
608 if (!pskb_may_pull(skb, thoff + ctx->hdrsize))
614 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
615 tuple->src_port = ports->source;
616 tuple->dst_port = ports->dest;
619 struct gre_base_hdr *greh;
621 greh = (struct gre_base_hdr *)(skb_network_header(skb) + thoff);
622 if ((greh->flags & GRE_VERSION) != GRE_VERSION_0)
628 ip6h = (struct ipv6hdr *)(skb_network_header(skb) + ctx->offset);
630 tuple->src_v6 = ip6h->saddr;
631 tuple->dst_v6 = ip6h->daddr;
632 tuple->l3proto = AF_INET6;
633 tuple->l4proto = nexthdr;
634 tuple->iifidx = ctx->in->ifindex;
635 nf_flow_tuple_encap(skb, tuple);
640 static int nf_flow_offload_ipv6_forward(struct nf_flowtable_ctx *ctx,
641 struct nf_flowtable *flow_table,
642 struct flow_offload_tuple_rhash *tuplehash,
645 enum flow_offload_tuple_dir dir;
646 struct flow_offload *flow;
647 unsigned int thoff, mtu;
648 struct ipv6hdr *ip6h;
650 dir = tuplehash->tuple.dir;
651 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
653 mtu = flow->tuplehash[dir].tuple.mtu + ctx->offset;
654 if (unlikely(nf_flow_exceeds_mtu(skb, mtu)))
657 ip6h = (struct ipv6hdr *)(skb_network_header(skb) + ctx->offset);
658 thoff = sizeof(*ip6h) + ctx->offset;
659 if (nf_flow_state_check(flow, ip6h->nexthdr, skb, thoff))
662 if (!nf_flow_dst_check(&tuplehash->tuple)) {
663 flow_offload_teardown(flow);
667 if (skb_try_make_writable(skb, thoff + ctx->hdrsize))
670 flow_offload_refresh(flow_table, flow, false);
672 nf_flow_encap_pop(skb, tuplehash);
674 ip6h = ipv6_hdr(skb);
675 nf_flow_nat_ipv6(flow, skb, dir, ip6h);
678 skb_clear_tstamp(skb);
680 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
681 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
686 static struct flow_offload_tuple_rhash *
687 nf_flow_offload_ipv6_lookup(struct nf_flowtable_ctx *ctx,
688 struct nf_flowtable *flow_table,
691 struct flow_offload_tuple tuple = {};
693 if (skb->protocol != htons(ETH_P_IPV6) &&
694 !nf_flow_skb_encap_protocol(skb, htons(ETH_P_IPV6), &ctx->offset))
697 if (nf_flow_tuple_ipv6(ctx, skb, &tuple) < 0)
700 return flow_offload_lookup(flow_table, &tuple);
704 nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
705 const struct nf_hook_state *state)
707 struct flow_offload_tuple_rhash *tuplehash;
708 struct nf_flowtable *flow_table = priv;
709 enum flow_offload_tuple_dir dir;
710 struct nf_flowtable_ctx ctx = {
713 const struct in6_addr *nexthop;
714 struct flow_offload *flow;
715 struct net_device *outdev;
719 tuplehash = nf_flow_offload_ipv6_lookup(&ctx, flow_table, skb);
720 if (tuplehash == NULL)
723 ret = nf_flow_offload_ipv6_forward(&ctx, flow_table, tuplehash, skb);
729 if (unlikely(tuplehash->tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)) {
730 rt = (struct rt6_info *)tuplehash->tuple.dst_cache;
731 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
732 IP6CB(skb)->iif = skb->dev->ifindex;
733 IP6CB(skb)->flags = IP6SKB_FORWARDED;
734 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
737 dir = tuplehash->tuple.dir;
738 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
740 switch (tuplehash->tuple.xmit_type) {
741 case FLOW_OFFLOAD_XMIT_NEIGH:
742 rt = (struct rt6_info *)tuplehash->tuple.dst_cache;
743 outdev = rt->dst.dev;
745 nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
746 skb_dst_set_noref(skb, &rt->dst);
747 neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
750 case FLOW_OFFLOAD_XMIT_DIRECT:
751 ret = nf_flow_queue_xmit(state->net, skb, tuplehash, ETH_P_IPV6);
753 flow_offload_teardown(flow);
763 EXPORT_SYMBOL_GPL(nf_flow_offload_ipv6_hook);