1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/module.h>
4 #include <linux/netfilter.h>
5 #include <linux/rhashtable.h>
7 #include <linux/ipv6.h>
8 #include <linux/netdevice.h>
11 #include <net/ip6_route.h>
12 #include <net/neighbour.h>
13 #include <net/netfilter/nf_flow_table.h>
14 /* For layer 4 checksum field offset. */
15 #include <linux/tcp.h>
16 #include <linux/udp.h>
18 static int nf_flow_state_check(struct flow_offload *flow, int proto,
19 struct sk_buff *skb, unsigned int thoff)
23 if (proto != IPPROTO_TCP)
26 if (!pskb_may_pull(skb, thoff + sizeof(*tcph)))
29 tcph = (void *)(skb_network_header(skb) + thoff);
30 if (unlikely(tcph->fin || tcph->rst)) {
31 flow_offload_teardown(flow);
38 static int nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
39 __be32 addr, __be32 new_addr)
43 if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
44 skb_try_make_writable(skb, thoff + sizeof(*tcph)))
47 tcph = (void *)(skb_network_header(skb) + thoff);
48 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
53 static int nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
54 __be32 addr, __be32 new_addr)
58 if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
59 skb_try_make_writable(skb, thoff + sizeof(*udph)))
62 udph = (void *)(skb_network_header(skb) + thoff);
63 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
64 inet_proto_csum_replace4(&udph->check, skb, addr,
67 udph->check = CSUM_MANGLED_0;
73 static int nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
74 unsigned int thoff, __be32 addr,
77 switch (iph->protocol) {
79 if (nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr) < 0)
83 if (nf_flow_nat_ip_udp(skb, thoff, addr, new_addr) < 0)
91 static int nf_flow_snat_ip(const struct flow_offload *flow, struct sk_buff *skb,
92 struct iphdr *iph, unsigned int thoff,
93 enum flow_offload_tuple_dir dir)
95 __be32 addr, new_addr;
98 case FLOW_OFFLOAD_DIR_ORIGINAL:
100 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
101 iph->saddr = new_addr;
103 case FLOW_OFFLOAD_DIR_REPLY:
105 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
106 iph->daddr = new_addr;
111 csum_replace4(&iph->check, addr, new_addr);
113 return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
116 static int nf_flow_dnat_ip(const struct flow_offload *flow, struct sk_buff *skb,
117 struct iphdr *iph, unsigned int thoff,
118 enum flow_offload_tuple_dir dir)
120 __be32 addr, new_addr;
123 case FLOW_OFFLOAD_DIR_ORIGINAL:
125 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
126 iph->daddr = new_addr;
128 case FLOW_OFFLOAD_DIR_REPLY:
130 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
131 iph->saddr = new_addr;
136 csum_replace4(&iph->check, addr, new_addr);
138 return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
141 static int nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
142 unsigned int thoff, enum flow_offload_tuple_dir dir)
144 struct iphdr *iph = ip_hdr(skb);
146 if (flow->flags & FLOW_OFFLOAD_SNAT &&
147 (nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
148 nf_flow_snat_ip(flow, skb, iph, thoff, dir) < 0))
150 if (flow->flags & FLOW_OFFLOAD_DNAT &&
151 (nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
152 nf_flow_dnat_ip(flow, skb, iph, thoff, dir) < 0))
158 static bool ip_has_options(unsigned int thoff)
160 return thoff != sizeof(struct iphdr);
163 static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
164 struct flow_offload_tuple *tuple)
166 struct flow_ports *ports;
170 if (!pskb_may_pull(skb, sizeof(*iph)))
174 thoff = iph->ihl * 4;
176 if (ip_is_fragment(iph) ||
177 unlikely(ip_has_options(thoff)))
180 if (iph->protocol != IPPROTO_TCP &&
181 iph->protocol != IPPROTO_UDP)
184 thoff = iph->ihl * 4;
185 if (!pskb_may_pull(skb, thoff + sizeof(*ports)))
188 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
190 tuple->src_v4.s_addr = iph->saddr;
191 tuple->dst_v4.s_addr = iph->daddr;
192 tuple->src_port = ports->source;
193 tuple->dst_port = ports->dest;
194 tuple->l3proto = AF_INET;
195 tuple->l4proto = iph->protocol;
196 tuple->iifidx = dev->ifindex;
201 /* Based on ip_exceeds_mtu(). */
202 static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
207 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
214 nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
215 const struct nf_hook_state *state)
217 struct flow_offload_tuple_rhash *tuplehash;
218 struct nf_flowtable *flow_table = priv;
219 struct flow_offload_tuple tuple = {};
220 enum flow_offload_tuple_dir dir;
221 struct flow_offload *flow;
222 struct net_device *outdev;
228 if (skb->protocol != htons(ETH_P_IP))
231 if (nf_flow_tuple_ip(skb, state->in, &tuple) < 0)
234 tuplehash = flow_offload_lookup(flow_table, &tuple);
235 if (tuplehash == NULL)
238 outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.oifidx);
242 dir = tuplehash->tuple.dir;
243 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
244 rt = (struct rtable *)flow->tuplehash[dir].tuple.dst_cache;
246 if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)) &&
247 (ip_hdr(skb)->frag_off & htons(IP_DF)) != 0)
250 if (skb_try_make_writable(skb, sizeof(*iph)))
253 thoff = ip_hdr(skb)->ihl * 4;
254 if (nf_flow_state_check(flow, ip_hdr(skb)->protocol, skb, thoff))
257 if (flow->flags & (FLOW_OFFLOAD_SNAT | FLOW_OFFLOAD_DNAT) &&
258 nf_flow_nat_ip(flow, skb, thoff, dir) < 0)
261 flow->timeout = (u32)jiffies + NF_FLOW_TIMEOUT;
263 ip_decrease_ttl(iph);
266 nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
267 skb_dst_set_noref(skb, &rt->dst);
268 neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
272 EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
274 static int nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
275 struct in6_addr *addr,
276 struct in6_addr *new_addr)
280 if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
281 skb_try_make_writable(skb, thoff + sizeof(*tcph)))
284 tcph = (void *)(skb_network_header(skb) + thoff);
285 inet_proto_csum_replace16(&tcph->check, skb, addr->s6_addr32,
286 new_addr->s6_addr32, true);
291 static int nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
292 struct in6_addr *addr,
293 struct in6_addr *new_addr)
297 if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
298 skb_try_make_writable(skb, thoff + sizeof(*udph)))
301 udph = (void *)(skb_network_header(skb) + thoff);
302 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
303 inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
304 new_addr->s6_addr32, true);
306 udph->check = CSUM_MANGLED_0;
312 static int nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
313 unsigned int thoff, struct in6_addr *addr,
314 struct in6_addr *new_addr)
316 switch (ip6h->nexthdr) {
318 if (nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr) < 0)
322 if (nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr) < 0)
330 static int nf_flow_snat_ipv6(const struct flow_offload *flow,
331 struct sk_buff *skb, struct ipv6hdr *ip6h,
333 enum flow_offload_tuple_dir dir)
335 struct in6_addr addr, new_addr;
338 case FLOW_OFFLOAD_DIR_ORIGINAL:
340 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6;
341 ip6h->saddr = new_addr;
343 case FLOW_OFFLOAD_DIR_REPLY:
345 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6;
346 ip6h->daddr = new_addr;
352 return nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
355 static int nf_flow_dnat_ipv6(const struct flow_offload *flow,
356 struct sk_buff *skb, struct ipv6hdr *ip6h,
358 enum flow_offload_tuple_dir dir)
360 struct in6_addr addr, new_addr;
363 case FLOW_OFFLOAD_DIR_ORIGINAL:
365 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6;
366 ip6h->daddr = new_addr;
368 case FLOW_OFFLOAD_DIR_REPLY:
370 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6;
371 ip6h->saddr = new_addr;
377 return nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
380 static int nf_flow_nat_ipv6(const struct flow_offload *flow,
382 enum flow_offload_tuple_dir dir)
384 struct ipv6hdr *ip6h = ipv6_hdr(skb);
385 unsigned int thoff = sizeof(*ip6h);
387 if (flow->flags & FLOW_OFFLOAD_SNAT &&
388 (nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir) < 0 ||
389 nf_flow_snat_ipv6(flow, skb, ip6h, thoff, dir) < 0))
391 if (flow->flags & FLOW_OFFLOAD_DNAT &&
392 (nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir) < 0 ||
393 nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir) < 0))
399 static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,
400 struct flow_offload_tuple *tuple)
402 struct flow_ports *ports;
403 struct ipv6hdr *ip6h;
406 if (!pskb_may_pull(skb, sizeof(*ip6h)))
409 ip6h = ipv6_hdr(skb);
411 if (ip6h->nexthdr != IPPROTO_TCP &&
412 ip6h->nexthdr != IPPROTO_UDP)
415 thoff = sizeof(*ip6h);
416 if (!pskb_may_pull(skb, thoff + sizeof(*ports)))
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 struct flow_offload *flow;
441 struct net_device *outdev;
442 struct in6_addr *nexthop;
443 struct ipv6hdr *ip6h;
446 if (skb->protocol != htons(ETH_P_IPV6))
449 if (nf_flow_tuple_ipv6(skb, state->in, &tuple) < 0)
452 tuplehash = flow_offload_lookup(flow_table, &tuple);
453 if (tuplehash == NULL)
456 outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.oifidx);
460 dir = tuplehash->tuple.dir;
461 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
462 rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst_cache;
464 if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
467 if (nf_flow_state_check(flow, ipv6_hdr(skb)->nexthdr, skb,
471 if (skb_try_make_writable(skb, sizeof(*ip6h)))
474 if (flow->flags & (FLOW_OFFLOAD_SNAT | FLOW_OFFLOAD_DNAT) &&
475 nf_flow_nat_ipv6(flow, skb, dir) < 0)
478 flow->timeout = (u32)jiffies + NF_FLOW_TIMEOUT;
479 ip6h = ipv6_hdr(skb);
483 nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
484 skb_dst_set_noref(skb, &rt->dst);
485 neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
489 EXPORT_SYMBOL_GPL(nf_flow_offload_ipv6_hook);