netfilter: nf_log_syslog: Unset bridge logger in pernet exit
[linux-block.git] / net / netfilter / nf_flow_table_ip.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
7d208687
FF
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>
7#include <linux/ip.h>
a908fdec 8#include <linux/ipv6.h>
7d208687 9#include <linux/netdevice.h>
72efd585
PNA
10#include <linux/if_ether.h>
11#include <linux/if_pppox.h>
12#include <linux/ppp_defs.h>
7d208687 13#include <net/ip.h>
a908fdec
FF
14#include <net/ipv6.h>
15#include <net/ip6_route.h>
7d208687
FF
16#include <net/neighbour.h>
17#include <net/netfilter/nf_flow_table.h>
53c2b289 18#include <net/netfilter/nf_conntrack_acct.h>
7d208687
FF
19/* For layer 4 checksum field offset. */
20#include <linux/tcp.h>
21#include <linux/udp.h>
22
33894c36
FF
23static int nf_flow_state_check(struct flow_offload *flow, int proto,
24 struct sk_buff *skb, unsigned int thoff)
b6f27d32
FF
25{
26 struct tcphdr *tcph;
27
33894c36
FF
28 if (proto != IPPROTO_TCP)
29 return 0;
30
b6f27d32
FF
31 tcph = (void *)(skb_network_header(skb) + thoff);
32 if (unlikely(tcph->fin || tcph->rst)) {
33 flow_offload_teardown(flow);
34 return -1;
35 }
36
37 return 0;
38}
39
f4401262
PNA
40static void nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
41 __be32 addr, __be32 new_addr)
7d208687
FF
42{
43 struct tcphdr *tcph;
44
7d208687
FF
45 tcph = (void *)(skb_network_header(skb) + thoff);
46 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
7d208687
FF
47}
48
f4401262
PNA
49static void nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
50 __be32 addr, __be32 new_addr)
7d208687
FF
51{
52 struct udphdr *udph;
53
7d208687
FF
54 udph = (void *)(skb_network_header(skb) + thoff);
55 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
56 inet_proto_csum_replace4(&udph->check, skb, addr,
57 new_addr, true);
58 if (!udph->check)
59 udph->check = CSUM_MANGLED_0;
60 }
7d208687
FF
61}
62
f4401262
PNA
63static void nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
64 unsigned int thoff, __be32 addr,
65 __be32 new_addr)
7d208687
FF
66{
67 switch (iph->protocol) {
68 case IPPROTO_TCP:
f4401262 69 nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr);
7d208687
FF
70 break;
71 case IPPROTO_UDP:
f4401262 72 nf_flow_nat_ip_udp(skb, thoff, addr, new_addr);
7d208687
FF
73 break;
74 }
7d208687
FF
75}
76
f4401262
PNA
77static void nf_flow_snat_ip(const struct flow_offload *flow,
78 struct sk_buff *skb, struct iphdr *iph,
79 unsigned int thoff, enum flow_offload_tuple_dir dir)
7d208687
FF
80{
81 __be32 addr, new_addr;
82
83 switch (dir) {
84 case FLOW_OFFLOAD_DIR_ORIGINAL:
85 addr = iph->saddr;
86 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
87 iph->saddr = new_addr;
88 break;
89 case FLOW_OFFLOAD_DIR_REPLY:
90 addr = iph->daddr;
91 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
92 iph->daddr = new_addr;
93 break;
7d208687
FF
94 }
95 csum_replace4(&iph->check, addr, new_addr);
96
f4401262 97 nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
7d208687
FF
98}
99
f4401262
PNA
100static void nf_flow_dnat_ip(const struct flow_offload *flow,
101 struct sk_buff *skb, struct iphdr *iph,
102 unsigned int thoff, enum flow_offload_tuple_dir dir)
7d208687
FF
103{
104 __be32 addr, new_addr;
105
106 switch (dir) {
107 case FLOW_OFFLOAD_DIR_ORIGINAL:
108 addr = iph->daddr;
109 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
110 iph->daddr = new_addr;
111 break;
112 case FLOW_OFFLOAD_DIR_REPLY:
113 addr = iph->saddr;
114 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
115 iph->saddr = new_addr;
116 break;
7d208687
FF
117 }
118 csum_replace4(&iph->check, addr, new_addr);
119
f4401262 120 nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
7d208687
FF
121}
122
f4401262 123static void nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
2fc11745
PNA
124 unsigned int thoff, enum flow_offload_tuple_dir dir,
125 struct iphdr *iph)
7d208687 126{
f4401262
PNA
127 if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
128 nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir);
129 nf_flow_snat_ip(flow, skb, iph, thoff, dir);
130 }
131 if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
132 nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir);
133 nf_flow_dnat_ip(flow, skb, iph, thoff, dir);
134 }
7d208687
FF
135}
136
137static bool ip_has_options(unsigned int thoff)
138{
139 return thoff != sizeof(struct iphdr);
140}
141
4cd91f7c
PNA
142static void nf_flow_tuple_encap(struct sk_buff *skb,
143 struct flow_offload_tuple *tuple)
144{
72efd585
PNA
145 struct vlan_ethhdr *veth;
146 struct pppoe_hdr *phdr;
4cd91f7c
PNA
147 int i = 0;
148
149 if (skb_vlan_tag_present(skb)) {
150 tuple->encap[i].id = skb_vlan_tag_get(skb);
151 tuple->encap[i].proto = skb->vlan_proto;
152 i++;
153 }
72efd585
PNA
154 switch (skb->protocol) {
155 case htons(ETH_P_8021Q):
156 veth = (struct vlan_ethhdr *)skb_mac_header(skb);
4cd91f7c
PNA
157 tuple->encap[i].id = ntohs(veth->h_vlan_TCI);
158 tuple->encap[i].proto = skb->protocol;
72efd585
PNA
159 break;
160 case htons(ETH_P_PPP_SES):
161 phdr = (struct pppoe_hdr *)skb_mac_header(skb);
162 tuple->encap[i].id = ntohs(phdr->sid);
163 tuple->encap[i].proto = skb->protocol;
164 break;
4cd91f7c
PNA
165 }
166}
167
7d208687 168static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
4cd91f7c
PNA
169 struct flow_offload_tuple *tuple, u32 *hdrsize,
170 u32 offset)
7d208687
FF
171{
172 struct flow_ports *ports;
2fc11745 173 unsigned int thoff;
7d208687
FF
174 struct iphdr *iph;
175
4cd91f7c 176 if (!pskb_may_pull(skb, sizeof(*iph) + offset))
7d208687
FF
177 return -1;
178
4cd91f7c
PNA
179 iph = (struct iphdr *)(skb_network_header(skb) + offset);
180 thoff = (iph->ihl * 4);
7d208687
FF
181
182 if (ip_is_fragment(iph) ||
183 unlikely(ip_has_options(thoff)))
184 return -1;
185
4cd91f7c
PNA
186 thoff += offset;
187
793d5d61
PNA
188 switch (iph->protocol) {
189 case IPPROTO_TCP:
2fc11745 190 *hdrsize = sizeof(struct tcphdr);
793d5d61
PNA
191 break;
192 case IPPROTO_UDP:
2fc11745 193 *hdrsize = sizeof(struct udphdr);
793d5d61
PNA
194 break;
195 default:
7d208687 196 return -1;
793d5d61 197 }
7d208687 198
33cc3c0c
TY
199 if (iph->ttl <= 1)
200 return -1;
201
2fc11745 202 if (!pskb_may_pull(skb, thoff + *hdrsize))
7d208687
FF
203 return -1;
204
4cd91f7c 205 iph = (struct iphdr *)(skb_network_header(skb) + offset);
7d208687
FF
206 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
207
208 tuple->src_v4.s_addr = iph->saddr;
209 tuple->dst_v4.s_addr = iph->daddr;
210 tuple->src_port = ports->source;
211 tuple->dst_port = ports->dest;
212 tuple->l3proto = AF_INET;
213 tuple->l4proto = iph->protocol;
214 tuple->iifidx = dev->ifindex;
4cd91f7c 215 nf_flow_tuple_encap(skb, tuple);
7d208687
FF
216
217 return 0;
218}
219
220/* Based on ip_exceeds_mtu(). */
221static bool nf_flow_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
222{
223 if (skb->len <= mtu)
224 return false;
225
7d208687
FF
226 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
227 return false;
228
229 return true;
230}
231
589b474a
FW
232static unsigned int nf_flow_xmit_xfrm(struct sk_buff *skb,
233 const struct nf_hook_state *state,
234 struct dst_entry *dst)
235{
236 skb_orphan(skb);
237 skb_dst_set_noref(skb, dst);
589b474a
FW
238 dst_output(state->net, state->sk, skb);
239 return NF_STOLEN;
240}
241
72efd585
PNA
242static inline __be16 nf_flow_pppoe_proto(const struct sk_buff *skb)
243{
244 __be16 proto;
245
246 proto = *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
247 sizeof(struct pppoe_hdr)));
248 switch (proto) {
249 case htons(PPP_IP):
250 return htons(ETH_P_IP);
251 case htons(PPP_IPV6):
252 return htons(ETH_P_IPV6);
253 }
254
255 return 0;
256}
257
4cd91f7c
PNA
258static bool nf_flow_skb_encap_protocol(const struct sk_buff *skb, __be16 proto,
259 u32 *offset)
260{
72efd585 261 struct vlan_ethhdr *veth;
4cd91f7c 262
72efd585
PNA
263 switch (skb->protocol) {
264 case htons(ETH_P_8021Q):
4cd91f7c
PNA
265 veth = (struct vlan_ethhdr *)skb_mac_header(skb);
266 if (veth->h_vlan_encapsulated_proto == proto) {
267 *offset += VLAN_HLEN;
268 return true;
269 }
72efd585
PNA
270 break;
271 case htons(ETH_P_PPP_SES):
272 if (nf_flow_pppoe_proto(skb) == proto) {
273 *offset += PPPOE_SES_HLEN;
274 return true;
275 }
276 break;
4cd91f7c
PNA
277 }
278
279 return false;
280}
281
282static void nf_flow_encap_pop(struct sk_buff *skb,
283 struct flow_offload_tuple_rhash *tuplehash)
284{
285 struct vlan_hdr *vlan_hdr;
286 int i;
287
288 for (i = 0; i < tuplehash->tuple.encap_num; i++) {
289 if (skb_vlan_tag_present(skb)) {
290 __vlan_hwaccel_clear_tag(skb);
291 continue;
292 }
72efd585
PNA
293 switch (skb->protocol) {
294 case htons(ETH_P_8021Q):
4cd91f7c
PNA
295 vlan_hdr = (struct vlan_hdr *)skb->data;
296 __skb_pull(skb, VLAN_HLEN);
297 vlan_set_encap_proto(skb, vlan_hdr);
298 skb_reset_network_header(skb);
299 break;
72efd585
PNA
300 case htons(ETH_P_PPP_SES):
301 skb->protocol = nf_flow_pppoe_proto(skb);
302 skb_pull(skb, PPPOE_SES_HLEN);
303 skb_reset_network_header(skb);
304 break;
4cd91f7c
PNA
305 }
306 }
307}
308
7a27f6ab
PNA
309static unsigned int nf_flow_queue_xmit(struct net *net, struct sk_buff *skb,
310 const struct flow_offload_tuple_rhash *tuplehash,
311 unsigned short type)
312{
313 struct net_device *outdev;
314
315 outdev = dev_get_by_index_rcu(net, tuplehash->tuple.out.ifidx);
316 if (!outdev)
317 return NF_DROP;
318
319 skb->dev = outdev;
320 dev_hard_header(skb, skb->dev, type, tuplehash->tuple.out.h_dest,
321 tuplehash->tuple.out.h_source, skb->len);
322 dev_queue_xmit(skb);
323
324 return NF_STOLEN;
325}
326
7d208687
FF
327unsigned int
328nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
329 const struct nf_hook_state *state)
330{
331 struct flow_offload_tuple_rhash *tuplehash;
332 struct nf_flowtable *flow_table = priv;
333 struct flow_offload_tuple tuple = {};
334 enum flow_offload_tuple_dir dir;
335 struct flow_offload *flow;
336 struct net_device *outdev;
4cd91f7c
PNA
337 u32 hdrsize, offset = 0;
338 unsigned int thoff, mtu;
2a79fd39 339 struct rtable *rt;
7d208687
FF
340 struct iphdr *iph;
341 __be32 nexthop;
7a27f6ab 342 int ret;
7d208687 343
4cd91f7c
PNA
344 if (skb->protocol != htons(ETH_P_IP) &&
345 !nf_flow_skb_encap_protocol(skb, htons(ETH_P_IP), &offset))
7d208687
FF
346 return NF_ACCEPT;
347
4cd91f7c 348 if (nf_flow_tuple_ip(skb, state->in, &tuple, &hdrsize, offset) < 0)
7d208687
FF
349 return NF_ACCEPT;
350
351 tuplehash = flow_offload_lookup(flow_table, &tuple);
352 if (tuplehash == NULL)
353 return NF_ACCEPT;
354
7d208687
FF
355 dir = tuplehash->tuple.dir;
356 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
7d208687 357
4cd91f7c
PNA
358 mtu = flow->tuplehash[dir].tuple.mtu + offset;
359 if (unlikely(nf_flow_exceeds_mtu(skb, mtu)))
7d208687
FF
360 return NF_ACCEPT;
361
4cd91f7c
PNA
362 iph = (struct iphdr *)(skb_network_header(skb) + offset);
363 thoff = (iph->ihl * 4) + offset;
2fc11745 364 if (nf_flow_state_check(flow, iph->protocol, skb, thoff))
b6f27d32
FF
365 return NF_ACCEPT;
366
2babb46c
PNA
367 if (skb_try_make_writable(skb, thoff + hdrsize))
368 return NF_DROP;
369
1b9cd769
PNA
370 flow_offload_refresh(flow_table, flow);
371
4cd91f7c
PNA
372 nf_flow_encap_pop(skb, tuplehash);
373 thoff -= offset;
374
2babb46c 375 iph = ip_hdr(skb);
f4401262 376 nf_flow_nat_ip(flow, skb, thoff, dir, iph);
7d208687 377
7d208687 378 ip_decrease_ttl(iph);
de20900f 379 skb->tstamp = 0;
7d208687 380
53c2b289
PNA
381 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
382 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
383
5139c0c0 384 if (unlikely(tuplehash->tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)) {
8b9229d1 385 rt = (struct rtable *)tuplehash->tuple.dst_cache;
589b474a
FW
386 memset(skb->cb, 0, sizeof(struct inet_skb_parm));
387 IPCB(skb)->iif = skb->dev->ifindex;
388 IPCB(skb)->flags = IPSKB_FORWARDED;
389 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
390 }
391
7a27f6ab
PNA
392 switch (tuplehash->tuple.xmit_type) {
393 case FLOW_OFFLOAD_XMIT_NEIGH:
8b9229d1 394 rt = (struct rtable *)tuplehash->tuple.dst_cache;
7a27f6ab
PNA
395 outdev = rt->dst.dev;
396 skb->dev = outdev;
397 nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
398 skb_dst_set_noref(skb, &rt->dst);
399 neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
400 ret = NF_STOLEN;
401 break;
402 case FLOW_OFFLOAD_XMIT_DIRECT:
403 ret = nf_flow_queue_xmit(state->net, skb, tuplehash, ETH_P_IP);
404 if (ret == NF_DROP)
405 flow_offload_teardown(flow);
406 break;
407 }
7d208687 408
7a27f6ab 409 return ret;
7d208687
FF
410}
411EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
a908fdec 412
f4401262
PNA
413static void nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
414 struct in6_addr *addr,
415 struct in6_addr *new_addr,
416 struct ipv6hdr *ip6h)
a908fdec
FF
417{
418 struct tcphdr *tcph;
419
a908fdec
FF
420 tcph = (void *)(skb_network_header(skb) + thoff);
421 inet_proto_csum_replace16(&tcph->check, skb, addr->s6_addr32,
422 new_addr->s6_addr32, true);
a908fdec
FF
423}
424
f4401262
PNA
425static void nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
426 struct in6_addr *addr,
427 struct in6_addr *new_addr)
a908fdec
FF
428{
429 struct udphdr *udph;
430
a908fdec
FF
431 udph = (void *)(skb_network_header(skb) + thoff);
432 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
433 inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
434 new_addr->s6_addr32, true);
435 if (!udph->check)
436 udph->check = CSUM_MANGLED_0;
437 }
a908fdec
FF
438}
439
f4401262
PNA
440static void nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
441 unsigned int thoff, struct in6_addr *addr,
442 struct in6_addr *new_addr)
a908fdec
FF
443{
444 switch (ip6h->nexthdr) {
445 case IPPROTO_TCP:
f4401262 446 nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr, ip6h);
a908fdec
FF
447 break;
448 case IPPROTO_UDP:
f4401262 449 nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr);
a908fdec
FF
450 break;
451 }
a908fdec
FF
452}
453
f4401262
PNA
454static void nf_flow_snat_ipv6(const struct flow_offload *flow,
455 struct sk_buff *skb, struct ipv6hdr *ip6h,
456 unsigned int thoff,
457 enum flow_offload_tuple_dir dir)
a908fdec
FF
458{
459 struct in6_addr addr, new_addr;
460
461 switch (dir) {
462 case FLOW_OFFLOAD_DIR_ORIGINAL:
463 addr = ip6h->saddr;
464 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6;
465 ip6h->saddr = new_addr;
466 break;
467 case FLOW_OFFLOAD_DIR_REPLY:
468 addr = ip6h->daddr;
469 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6;
470 ip6h->daddr = new_addr;
471 break;
a908fdec
FF
472 }
473
f4401262 474 nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
a908fdec
FF
475}
476
f4401262
PNA
477static void nf_flow_dnat_ipv6(const struct flow_offload *flow,
478 struct sk_buff *skb, struct ipv6hdr *ip6h,
479 unsigned int thoff,
480 enum flow_offload_tuple_dir dir)
a908fdec
FF
481{
482 struct in6_addr addr, new_addr;
483
484 switch (dir) {
485 case FLOW_OFFLOAD_DIR_ORIGINAL:
486 addr = ip6h->daddr;
487 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6;
488 ip6h->daddr = new_addr;
489 break;
490 case FLOW_OFFLOAD_DIR_REPLY:
491 addr = ip6h->saddr;
492 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6;
493 ip6h->saddr = new_addr;
494 break;
a908fdec
FF
495 }
496
f4401262 497 nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
a908fdec
FF
498}
499
f4401262
PNA
500static void nf_flow_nat_ipv6(const struct flow_offload *flow,
501 struct sk_buff *skb,
502 enum flow_offload_tuple_dir dir,
503 struct ipv6hdr *ip6h)
a908fdec 504{
a908fdec
FF
505 unsigned int thoff = sizeof(*ip6h);
506
f4401262
PNA
507 if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
508 nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir);
509 nf_flow_snat_ipv6(flow, skb, ip6h, thoff, dir);
510 }
511 if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
512 nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir);
513 nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir);
514 }
a908fdec
FF
515}
516
517static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,
4cd91f7c
PNA
518 struct flow_offload_tuple *tuple, u32 *hdrsize,
519 u32 offset)
a908fdec
FF
520{
521 struct flow_ports *ports;
522 struct ipv6hdr *ip6h;
2fc11745 523 unsigned int thoff;
a908fdec 524
4cd91f7c
PNA
525 thoff = sizeof(*ip6h) + offset;
526 if (!pskb_may_pull(skb, thoff))
a908fdec
FF
527 return -1;
528
4cd91f7c 529 ip6h = (struct ipv6hdr *)(skb_network_header(skb) + offset);
a908fdec 530
793d5d61
PNA
531 switch (ip6h->nexthdr) {
532 case IPPROTO_TCP:
2fc11745 533 *hdrsize = sizeof(struct tcphdr);
793d5d61
PNA
534 break;
535 case IPPROTO_UDP:
2fc11745 536 *hdrsize = sizeof(struct udphdr);
793d5d61
PNA
537 break;
538 default:
a908fdec 539 return -1;
793d5d61 540 }
a908fdec 541
33cc3c0c
TY
542 if (ip6h->hop_limit <= 1)
543 return -1;
544
2fc11745 545 if (!pskb_may_pull(skb, thoff + *hdrsize))
a908fdec
FF
546 return -1;
547
4cd91f7c 548 ip6h = (struct ipv6hdr *)(skb_network_header(skb) + offset);
a908fdec
FF
549 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
550
551 tuple->src_v6 = ip6h->saddr;
552 tuple->dst_v6 = ip6h->daddr;
553 tuple->src_port = ports->source;
554 tuple->dst_port = ports->dest;
555 tuple->l3proto = AF_INET6;
556 tuple->l4proto = ip6h->nexthdr;
557 tuple->iifidx = dev->ifindex;
4cd91f7c 558 nf_flow_tuple_encap(skb, tuple);
a908fdec
FF
559
560 return 0;
561}
562
563unsigned int
564nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
565 const struct nf_hook_state *state)
566{
567 struct flow_offload_tuple_rhash *tuplehash;
568 struct nf_flowtable *flow_table = priv;
569 struct flow_offload_tuple tuple = {};
570 enum flow_offload_tuple_dir dir;
9b1c1ef1 571 const struct in6_addr *nexthop;
a908fdec
FF
572 struct flow_offload *flow;
573 struct net_device *outdev;
4cd91f7c
PNA
574 unsigned int thoff, mtu;
575 u32 hdrsize, offset = 0;
a908fdec
FF
576 struct ipv6hdr *ip6h;
577 struct rt6_info *rt;
7a27f6ab 578 int ret;
a908fdec 579
4cd91f7c
PNA
580 if (skb->protocol != htons(ETH_P_IPV6) &&
581 !nf_flow_skb_encap_protocol(skb, htons(ETH_P_IPV6), &offset))
a908fdec
FF
582 return NF_ACCEPT;
583
4cd91f7c 584 if (nf_flow_tuple_ipv6(skb, state->in, &tuple, &hdrsize, offset) < 0)
a908fdec
FF
585 return NF_ACCEPT;
586
587 tuplehash = flow_offload_lookup(flow_table, &tuple);
588 if (tuplehash == NULL)
589 return NF_ACCEPT;
590
a908fdec
FF
591 dir = tuplehash->tuple.dir;
592 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
a908fdec 593
4cd91f7c
PNA
594 mtu = flow->tuplehash[dir].tuple.mtu + offset;
595 if (unlikely(nf_flow_exceeds_mtu(skb, mtu)))
a908fdec
FF
596 return NF_ACCEPT;
597
4cd91f7c
PNA
598 ip6h = (struct ipv6hdr *)(skb_network_header(skb) + offset);
599 thoff = sizeof(*ip6h) + offset;
600 if (nf_flow_state_check(flow, ip6h->nexthdr, skb, thoff))
b6f27d32
FF
601 return NF_ACCEPT;
602
4cd91f7c 603 if (skb_try_make_writable(skb, thoff + hdrsize))
a908fdec
FF
604 return NF_DROP;
605
1b9cd769
PNA
606 flow_offload_refresh(flow_table, flow);
607
4cd91f7c
PNA
608 nf_flow_encap_pop(skb, tuplehash);
609
2fc11745 610 ip6h = ipv6_hdr(skb);
f4401262 611 nf_flow_nat_ipv6(flow, skb, dir, ip6h);
a908fdec 612
a908fdec 613 ip6h->hop_limit--;
de20900f 614 skb->tstamp = 0;
a908fdec 615
53c2b289
PNA
616 if (flow_table->flags & NF_FLOWTABLE_COUNTER)
617 nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
618
5139c0c0 619 if (unlikely(tuplehash->tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)) {
8b9229d1 620 rt = (struct rt6_info *)tuplehash->tuple.dst_cache;
589b474a
FW
621 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
622 IP6CB(skb)->iif = skb->dev->ifindex;
623 IP6CB(skb)->flags = IP6SKB_FORWARDED;
624 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
625 }
626
7a27f6ab
PNA
627 switch (tuplehash->tuple.xmit_type) {
628 case FLOW_OFFLOAD_XMIT_NEIGH:
8b9229d1 629 rt = (struct rt6_info *)tuplehash->tuple.dst_cache;
7a27f6ab
PNA
630 outdev = rt->dst.dev;
631 skb->dev = outdev;
632 nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
633 skb_dst_set_noref(skb, &rt->dst);
634 neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
635 ret = NF_STOLEN;
636 break;
637 case FLOW_OFFLOAD_XMIT_DIRECT:
638 ret = nf_flow_queue_xmit(state->net, skb, tuplehash, ETH_P_IPV6);
639 if (ret == NF_DROP)
640 flow_offload_teardown(flow);
641 break;
642 }
a908fdec 643
7a27f6ab 644 return ret;
a908fdec
FF
645}
646EXPORT_SYMBOL_GPL(nf_flow_offload_ipv6_hook);