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 void 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);
46 static void nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
47 __be32 addr, __be32 new_addr)
51 udph = (void *)(skb_network_header(skb) + thoff);
52 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
53 inet_proto_csum_replace4(&udph->check, skb, addr,
56 udph->check = CSUM_MANGLED_0;
60 static void nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
61 unsigned int thoff, __be32 addr,
64 switch (iph->protocol) {
66 nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr);
69 nf_flow_nat_ip_udp(skb, thoff, addr, new_addr);
74 static void nf_flow_snat_ip(const struct flow_offload *flow,
75 struct sk_buff *skb, struct iphdr *iph,
76 unsigned int thoff, enum flow_offload_tuple_dir dir)
78 __be32 addr, new_addr;
81 case FLOW_OFFLOAD_DIR_ORIGINAL:
83 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
84 iph->saddr = new_addr;
86 case FLOW_OFFLOAD_DIR_REPLY:
88 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
89 iph->daddr = new_addr;
92 csum_replace4(&iph->check, addr, new_addr);
94 nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
97 static void nf_flow_dnat_ip(const struct flow_offload *flow,
98 struct sk_buff *skb, struct iphdr *iph,
99 unsigned int thoff, enum flow_offload_tuple_dir dir)
101 __be32 addr, new_addr;
104 case FLOW_OFFLOAD_DIR_ORIGINAL:
106 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
107 iph->daddr = new_addr;
109 case FLOW_OFFLOAD_DIR_REPLY:
111 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
112 iph->saddr = new_addr;
115 csum_replace4(&iph->check, addr, new_addr);
117 nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
120 static void nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
121 unsigned int thoff, enum flow_offload_tuple_dir dir,
124 if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
125 nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir);
126 nf_flow_snat_ip(flow, skb, iph, thoff, dir);
128 if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
129 nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir);
130 nf_flow_dnat_ip(flow, skb, iph, thoff, dir);
134 static bool ip_has_options(unsigned int thoff)
136 return thoff != sizeof(struct iphdr);
139 static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
140 struct flow_offload_tuple *tuple, u32 *hdrsize)
142 struct flow_ports *ports;
146 if (!pskb_may_pull(skb, sizeof(*iph)))
150 thoff = iph->ihl * 4;
152 if (ip_is_fragment(iph) ||
153 unlikely(ip_has_options(thoff)))
156 switch (iph->protocol) {
158 *hdrsize = sizeof(struct tcphdr);
161 *hdrsize = sizeof(struct udphdr);
170 thoff = iph->ihl * 4;
171 if (!pskb_may_pull(skb, thoff + *hdrsize))
175 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
177 tuple->src_v4.s_addr = iph->saddr;
178 tuple->dst_v4.s_addr = iph->daddr;
179 tuple->src_port = ports->source;
180 tuple->dst_port = ports->dest;
181 tuple->l3proto = AF_INET;
182 tuple->l4proto = iph->protocol;
183 tuple->iifidx = dev->ifindex;
188 /* Based on ip_exceeds_mtu(). */
189 static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
194 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
200 static unsigned int nf_flow_xmit_xfrm(struct sk_buff *skb,
201 const struct nf_hook_state *state,
202 struct dst_entry *dst)
205 skb_dst_set_noref(skb, dst);
206 dst_output(state->net, state->sk, skb);
211 nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
212 const struct nf_hook_state *state)
214 struct flow_offload_tuple_rhash *tuplehash;
215 struct nf_flowtable *flow_table = priv;
216 struct flow_offload_tuple tuple = {};
217 enum flow_offload_tuple_dir dir;
218 struct flow_offload *flow;
219 struct net_device *outdev;
226 if (skb->protocol != htons(ETH_P_IP))
229 if (nf_flow_tuple_ip(skb, state->in, &tuple, &hdrsize) < 0)
232 tuplehash = flow_offload_lookup(flow_table, &tuple);
233 if (tuplehash == NULL)
236 dir = tuplehash->tuple.dir;
237 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
238 rt = (struct rtable *)flow->tuplehash[dir].tuple.dst_cache;
239 outdev = rt->dst.dev;
241 if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
245 thoff = iph->ihl * 4;
246 if (nf_flow_state_check(flow, iph->protocol, skb, thoff))
249 flow_offload_refresh(flow_table, flow);
251 if (!dst_check(&rt->dst, 0)) {
252 flow_offload_teardown(flow);
256 if (skb_try_make_writable(skb, thoff + hdrsize))
260 nf_flow_nat_ip(flow, skb, thoff, dir, iph);
262 ip_decrease_ttl(iph);
265 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
266 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
268 if (unlikely(dst_xfrm(&rt->dst))) {
269 memset(skb->cb, 0, sizeof(struct inet_skb_parm));
270 IPCB(skb)->iif = skb->dev->ifindex;
271 IPCB(skb)->flags = IPSKB_FORWARDED;
272 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
276 nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
277 skb_dst_set_noref(skb, &rt->dst);
278 neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
282 EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
284 static void nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
285 struct in6_addr *addr,
286 struct in6_addr *new_addr,
287 struct ipv6hdr *ip6h)
291 tcph = (void *)(skb_network_header(skb) + thoff);
292 inet_proto_csum_replace16(&tcph->check, skb, addr->s6_addr32,
293 new_addr->s6_addr32, true);
296 static void nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
297 struct in6_addr *addr,
298 struct in6_addr *new_addr)
302 udph = (void *)(skb_network_header(skb) + thoff);
303 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
304 inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
305 new_addr->s6_addr32, true);
307 udph->check = CSUM_MANGLED_0;
311 static void nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
312 unsigned int thoff, struct in6_addr *addr,
313 struct in6_addr *new_addr)
315 switch (ip6h->nexthdr) {
317 nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr, ip6h);
320 nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr);
325 static void nf_flow_snat_ipv6(const struct flow_offload *flow,
326 struct sk_buff *skb, struct ipv6hdr *ip6h,
328 enum flow_offload_tuple_dir dir)
330 struct in6_addr addr, new_addr;
333 case FLOW_OFFLOAD_DIR_ORIGINAL:
335 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6;
336 ip6h->saddr = new_addr;
338 case FLOW_OFFLOAD_DIR_REPLY:
340 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6;
341 ip6h->daddr = new_addr;
345 nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
348 static void nf_flow_dnat_ipv6(const struct flow_offload *flow,
349 struct sk_buff *skb, struct ipv6hdr *ip6h,
351 enum flow_offload_tuple_dir dir)
353 struct in6_addr addr, new_addr;
356 case FLOW_OFFLOAD_DIR_ORIGINAL:
358 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6;
359 ip6h->daddr = new_addr;
361 case FLOW_OFFLOAD_DIR_REPLY:
363 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6;
364 ip6h->saddr = new_addr;
368 nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
371 static void nf_flow_nat_ipv6(const struct flow_offload *flow,
373 enum flow_offload_tuple_dir dir,
374 struct ipv6hdr *ip6h)
376 unsigned int thoff = sizeof(*ip6h);
378 if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
379 nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir);
380 nf_flow_snat_ipv6(flow, skb, ip6h, thoff, dir);
382 if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
383 nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir);
384 nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir);
388 static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,
389 struct flow_offload_tuple *tuple, u32 *hdrsize)
391 struct flow_ports *ports;
392 struct ipv6hdr *ip6h;
395 if (!pskb_may_pull(skb, sizeof(*ip6h)))
398 ip6h = ipv6_hdr(skb);
400 switch (ip6h->nexthdr) {
402 *hdrsize = sizeof(struct tcphdr);
405 *hdrsize = sizeof(struct udphdr);
411 if (ip6h->hop_limit <= 1)
414 thoff = sizeof(*ip6h);
415 if (!pskb_may_pull(skb, thoff + *hdrsize))
418 ip6h = ipv6_hdr(skb);
419 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
421 tuple->src_v6 = ip6h->saddr;
422 tuple->dst_v6 = ip6h->daddr;
423 tuple->src_port = ports->source;
424 tuple->dst_port = ports->dest;
425 tuple->l3proto = AF_INET6;
426 tuple->l4proto = ip6h->nexthdr;
427 tuple->iifidx = dev->ifindex;
433 nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
434 const struct nf_hook_state *state)
436 struct flow_offload_tuple_rhash *tuplehash;
437 struct nf_flowtable *flow_table = priv;
438 struct flow_offload_tuple tuple = {};
439 enum flow_offload_tuple_dir dir;
440 const struct in6_addr *nexthop;
441 struct flow_offload *flow;
442 struct net_device *outdev;
443 struct ipv6hdr *ip6h;
447 if (skb->protocol != htons(ETH_P_IPV6))
450 if (nf_flow_tuple_ipv6(skb, state->in, &tuple, &hdrsize) < 0)
453 tuplehash = flow_offload_lookup(flow_table, &tuple);
454 if (tuplehash == NULL)
457 dir = tuplehash->tuple.dir;
458 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
459 rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst_cache;
460 outdev = rt->dst.dev;
462 if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
465 if (nf_flow_state_check(flow, ipv6_hdr(skb)->nexthdr, skb,
469 flow_offload_refresh(flow_table, flow);
471 if (!dst_check(&rt->dst, 0)) {
472 flow_offload_teardown(flow);
476 if (skb_try_make_writable(skb, sizeof(*ip6h) + hdrsize))
479 ip6h = ipv6_hdr(skb);
480 nf_flow_nat_ipv6(flow, skb, dir, ip6h);
485 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
486 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
488 if (unlikely(dst_xfrm(&rt->dst))) {
489 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
490 IP6CB(skb)->iif = skb->dev->ifindex;
491 IP6CB(skb)->flags = IP6SKB_FORWARDED;
492 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
496 nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
497 skb_dst_set_noref(skb, &rt->dst);
498 neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
502 EXPORT_SYMBOL_GPL(nf_flow_offload_ipv6_hook);