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 tcph = (void *)(skb_network_header(skb) + thoff);
29 if (unlikely(tcph->fin || tcph->rst)) {
30 flow_offload_teardown(flow);
37 static int nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
38 __be32 addr, __be32 new_addr)
42 tcph = (void *)(skb_network_header(skb) + thoff);
43 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
48 static int 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;
64 static int nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
65 unsigned int thoff, __be32 addr,
68 switch (iph->protocol) {
70 if (nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr) < 0)
74 if (nf_flow_nat_ip_udp(skb, thoff, addr, new_addr) < 0)
82 static int nf_flow_snat_ip(const struct flow_offload *flow, struct sk_buff *skb,
83 struct iphdr *iph, unsigned int thoff,
84 enum flow_offload_tuple_dir dir)
86 __be32 addr, new_addr;
89 case FLOW_OFFLOAD_DIR_ORIGINAL:
91 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
92 iph->saddr = new_addr;
94 case FLOW_OFFLOAD_DIR_REPLY:
96 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
97 iph->daddr = new_addr;
102 csum_replace4(&iph->check, addr, new_addr);
104 return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
107 static int nf_flow_dnat_ip(const struct flow_offload *flow, struct sk_buff *skb,
108 struct iphdr *iph, unsigned int thoff,
109 enum flow_offload_tuple_dir dir)
111 __be32 addr, new_addr;
114 case FLOW_OFFLOAD_DIR_ORIGINAL:
116 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
117 iph->daddr = new_addr;
119 case FLOW_OFFLOAD_DIR_REPLY:
121 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
122 iph->saddr = new_addr;
127 csum_replace4(&iph->check, addr, new_addr);
129 return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
132 static int nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
133 unsigned int thoff, enum flow_offload_tuple_dir dir,
136 if (test_bit(NF_FLOW_SNAT, &flow->flags) &&
137 (nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
138 nf_flow_snat_ip(flow, skb, iph, thoff, dir) < 0))
141 if (test_bit(NF_FLOW_DNAT, &flow->flags) &&
142 (nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
143 nf_flow_dnat_ip(flow, skb, iph, thoff, dir) < 0))
149 static bool ip_has_options(unsigned int thoff)
151 return thoff != sizeof(struct iphdr);
154 static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
155 struct flow_offload_tuple *tuple, u32 *hdrsize)
157 struct flow_ports *ports;
161 if (!pskb_may_pull(skb, sizeof(*iph)))
165 thoff = iph->ihl * 4;
167 if (ip_is_fragment(iph) ||
168 unlikely(ip_has_options(thoff)))
171 switch (iph->protocol) {
173 *hdrsize = sizeof(struct tcphdr);
176 *hdrsize = sizeof(struct udphdr);
185 thoff = iph->ihl * 4;
186 if (!pskb_may_pull(skb, thoff + *hdrsize))
190 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
192 tuple->src_v4.s_addr = iph->saddr;
193 tuple->dst_v4.s_addr = iph->daddr;
194 tuple->src_port = ports->source;
195 tuple->dst_port = ports->dest;
196 tuple->l3proto = AF_INET;
197 tuple->l4proto = iph->protocol;
198 tuple->iifidx = dev->ifindex;
203 /* Based on ip_exceeds_mtu(). */
204 static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
209 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
215 static int nf_flow_offload_dst_check(struct dst_entry *dst)
217 if (unlikely(dst_xfrm(dst)))
218 return dst_check(dst, 0) ? 0 : -1;
223 static unsigned int nf_flow_xmit_xfrm(struct sk_buff *skb,
224 const struct nf_hook_state *state,
225 struct dst_entry *dst)
228 skb_dst_set_noref(skb, dst);
229 dst_output(state->net, state->sk, skb);
234 nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
235 const struct nf_hook_state *state)
237 struct flow_offload_tuple_rhash *tuplehash;
238 struct nf_flowtable *flow_table = priv;
239 struct flow_offload_tuple tuple = {};
240 enum flow_offload_tuple_dir dir;
241 struct flow_offload *flow;
242 struct net_device *outdev;
249 if (skb->protocol != htons(ETH_P_IP))
252 if (nf_flow_tuple_ip(skb, state->in, &tuple, &hdrsize) < 0)
255 tuplehash = flow_offload_lookup(flow_table, &tuple);
256 if (tuplehash == NULL)
259 dir = tuplehash->tuple.dir;
260 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
261 rt = (struct rtable *)flow->tuplehash[dir].tuple.dst_cache;
262 outdev = rt->dst.dev;
264 if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
268 thoff = iph->ihl * 4;
269 if (nf_flow_state_check(flow, iph->protocol, skb, thoff))
272 flow_offload_refresh(flow_table, flow);
274 if (nf_flow_offload_dst_check(&rt->dst)) {
275 flow_offload_teardown(flow);
279 if (skb_try_make_writable(skb, thoff + hdrsize))
283 if (nf_flow_nat_ip(flow, skb, thoff, dir, iph) < 0)
286 ip_decrease_ttl(iph);
289 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
290 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
292 if (unlikely(dst_xfrm(&rt->dst))) {
293 memset(skb->cb, 0, sizeof(struct inet_skb_parm));
294 IPCB(skb)->iif = skb->dev->ifindex;
295 IPCB(skb)->flags = IPSKB_FORWARDED;
296 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
300 nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
301 skb_dst_set_noref(skb, &rt->dst);
302 neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
306 EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
308 static int nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
309 struct in6_addr *addr,
310 struct in6_addr *new_addr)
314 tcph = (void *)(skb_network_header(skb) + thoff);
315 inet_proto_csum_replace16(&tcph->check, skb, addr->s6_addr32,
316 new_addr->s6_addr32, true);
321 static int nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
322 struct in6_addr *addr,
323 struct in6_addr *new_addr)
327 udph = (void *)(skb_network_header(skb) + thoff);
328 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
329 inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
330 new_addr->s6_addr32, true);
332 udph->check = CSUM_MANGLED_0;
338 static int nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
339 unsigned int thoff, struct in6_addr *addr,
340 struct in6_addr *new_addr)
342 switch (ip6h->nexthdr) {
344 if (nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr) < 0)
348 if (nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr) < 0)
356 static int nf_flow_snat_ipv6(const struct flow_offload *flow,
357 struct sk_buff *skb, struct ipv6hdr *ip6h,
359 enum flow_offload_tuple_dir dir)
361 struct in6_addr addr, new_addr;
364 case FLOW_OFFLOAD_DIR_ORIGINAL:
366 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6;
367 ip6h->saddr = new_addr;
369 case FLOW_OFFLOAD_DIR_REPLY:
371 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6;
372 ip6h->daddr = new_addr;
378 return nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
381 static int nf_flow_dnat_ipv6(const struct flow_offload *flow,
382 struct sk_buff *skb, struct ipv6hdr *ip6h,
384 enum flow_offload_tuple_dir dir)
386 struct in6_addr addr, new_addr;
389 case FLOW_OFFLOAD_DIR_ORIGINAL:
391 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6;
392 ip6h->daddr = new_addr;
394 case FLOW_OFFLOAD_DIR_REPLY:
396 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6;
397 ip6h->saddr = new_addr;
403 return nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
406 static int nf_flow_nat_ipv6(const struct flow_offload *flow,
408 enum flow_offload_tuple_dir dir,
409 struct ipv6hdr *ip6h)
411 unsigned int thoff = sizeof(*ip6h);
413 if (test_bit(NF_FLOW_SNAT, &flow->flags) &&
414 (nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir) < 0 ||
415 nf_flow_snat_ipv6(flow, skb, ip6h, thoff, dir) < 0))
418 if (test_bit(NF_FLOW_DNAT, &flow->flags) &&
419 (nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir) < 0 ||
420 nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir) < 0))
426 static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,
427 struct flow_offload_tuple *tuple, u32 *hdrsize)
429 struct flow_ports *ports;
430 struct ipv6hdr *ip6h;
433 if (!pskb_may_pull(skb, sizeof(*ip6h)))
436 ip6h = ipv6_hdr(skb);
438 switch (ip6h->nexthdr) {
440 *hdrsize = sizeof(struct tcphdr);
443 *hdrsize = sizeof(struct udphdr);
449 if (ip6h->hop_limit <= 1)
452 thoff = sizeof(*ip6h);
453 if (!pskb_may_pull(skb, thoff + *hdrsize))
456 ip6h = ipv6_hdr(skb);
457 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
459 tuple->src_v6 = ip6h->saddr;
460 tuple->dst_v6 = ip6h->daddr;
461 tuple->src_port = ports->source;
462 tuple->dst_port = ports->dest;
463 tuple->l3proto = AF_INET6;
464 tuple->l4proto = ip6h->nexthdr;
465 tuple->iifidx = dev->ifindex;
471 nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
472 const struct nf_hook_state *state)
474 struct flow_offload_tuple_rhash *tuplehash;
475 struct nf_flowtable *flow_table = priv;
476 struct flow_offload_tuple tuple = {};
477 enum flow_offload_tuple_dir dir;
478 const struct in6_addr *nexthop;
479 struct flow_offload *flow;
480 struct net_device *outdev;
481 struct ipv6hdr *ip6h;
485 if (skb->protocol != htons(ETH_P_IPV6))
488 if (nf_flow_tuple_ipv6(skb, state->in, &tuple, &hdrsize) < 0)
491 tuplehash = flow_offload_lookup(flow_table, &tuple);
492 if (tuplehash == NULL)
495 dir = tuplehash->tuple.dir;
496 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
497 rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst_cache;
498 outdev = rt->dst.dev;
500 if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
503 if (nf_flow_state_check(flow, ipv6_hdr(skb)->nexthdr, skb,
507 flow_offload_refresh(flow_table, flow);
509 if (nf_flow_offload_dst_check(&rt->dst)) {
510 flow_offload_teardown(flow);
514 if (skb_try_make_writable(skb, sizeof(*ip6h) + hdrsize))
517 ip6h = ipv6_hdr(skb);
518 if (nf_flow_nat_ipv6(flow, skb, dir, ip6h) < 0)
524 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
525 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
527 if (unlikely(dst_xfrm(&rt->dst))) {
528 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
529 IP6CB(skb)->iif = skb->dev->ifindex;
530 IP6CB(skb)->flags = IP6SKB_FORWARDED;
531 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
535 nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
536 skb_dst_set_noref(skb, &rt->dst);
537 neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
541 EXPORT_SYMBOL_GPL(nf_flow_offload_ipv6_hook);