compat: nf_defrag_ipv6: avoid nf_iterate recursion.
[cascardo/ovs.git] / datapath / linux / compat / nf_conntrack_reasm.c
1 /*
2  * Backported from upstream commit 5b490047240f
3  * ("ipv6: Export nf_ct_frag6_gather()")
4  *
5  * IPv6 fragment reassembly for connection tracking
6  *
7  * Copyright (C)2004 USAGI/WIDE Project
8  *
9  * Author:
10  *      Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
11  *
12  * Based on: net/ipv6/reassembly.c
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version
17  * 2 of the License, or (at your option) any later version.
18  */
19
20 #define pr_fmt(fmt) "IPv6-nf: " fmt
21
22 #include <linux/version.h>
23
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/string.h>
27 #include <linux/socket.h>
28 #include <linux/sockios.h>
29 #include <linux/jiffies.h>
30 #include <linux/net.h>
31 #include <linux/list.h>
32 #include <linux/netdevice.h>
33 #include <linux/in6.h>
34 #include <linux/ipv6.h>
35 #include <linux/icmpv6.h>
36 #include <linux/random.h>
37 #include <linux/slab.h>
38
39 #include <net/sock.h>
40 #include <net/snmp.h>
41 #include <net/inet_frag.h>
42
43 #include <net/ipv6.h>
44 #include <net/protocol.h>
45 #include <net/transp_v6.h>
46 #include <net/rawv6.h>
47 #include <net/ndisc.h>
48 #include <net/addrconf.h>
49 #include <net/inet_ecn.h>
50 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
51 #include <linux/netfilter.h>
52 #include <linux/netfilter_ipv6.h>
53 #include <linux/kernel.h>
54 #include <linux/module.h>
55 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
56
57 #ifdef OVS_NF_DEFRAG6_BACKPORT
58
59 static const char nf_frags_cache_name[] = "ovs-frag6";
60
61 struct nf_ct_frag6_skb_cb
62 {
63         struct inet6_skb_parm   h;
64         int                     offset;
65 };
66
67 #define NFCT_FRAG6_CB(skb)      ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
68
69 static struct inet_frags nf_frags;
70
71 static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
72 {
73         return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
74 }
75
76 static unsigned int nf_hash_frag(__be32 id, const struct in6_addr *saddr,
77                                  const struct in6_addr *daddr)
78 {
79         net_get_random_once(&nf_frags.rnd, sizeof(nf_frags.rnd));
80         return jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr),
81                             (__force u32)id, nf_frags.rnd);
82 }
83
84 #ifdef HAVE_INET_FRAGS_CONST
85 static unsigned int nf_hashfn(const struct inet_frag_queue *q)
86 #else
87 static unsigned int nf_hashfn(struct inet_frag_queue *q)
88 #endif
89 {
90         const struct frag_queue *nq;
91
92         nq = container_of(q, struct frag_queue, q);
93         return nf_hash_frag(nq->id, &nq->saddr, &nq->daddr);
94 }
95
96 static void nf_ct_frag6_expire(unsigned long data)
97 {
98         struct frag_queue *fq;
99         struct net *net;
100
101         fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
102         net = container_of(fq->q.net, struct net, nf_frag.frags);
103
104         ip6_expire_frag_queue(net, fq, &nf_frags);
105 }
106
107 /* Creation primitives. */
108 static inline struct frag_queue *fq_find(struct net *net, __be32 id,
109                                          u32 user, struct in6_addr *src,
110                                          struct in6_addr *dst, u8 ecn)
111 {
112         struct inet_frag_queue *q;
113         struct ip6_create_arg arg;
114         unsigned int hash;
115
116         arg.id = id;
117         arg.user = user;
118         arg.src = src;
119         arg.dst = dst;
120         arg.ecn = ecn;
121
122         local_bh_disable();
123         hash = nf_hash_frag(id, src, dst);
124
125         q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash);
126         local_bh_enable();
127         if (IS_ERR_OR_NULL(q)) {
128                 inet_frag_maybe_warn_overflow(q, pr_fmt());
129                 return NULL;
130         }
131         return container_of(q, struct frag_queue, q);
132 }
133
134
135 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
136                              const struct frag_hdr *fhdr, int nhoff)
137 {
138         struct sk_buff *prev, *next;
139         unsigned int payload_len;
140         int offset, end;
141         u8 ecn;
142
143         if (qp_flags(fq) & INET_FRAG_COMPLETE) {
144                 pr_debug("Already completed\n");
145                 goto err;
146         }
147
148         payload_len = ntohs(ipv6_hdr(skb)->payload_len);
149
150         offset = ntohs(fhdr->frag_off) & ~0x7;
151         end = offset + (payload_len -
152                         ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
153
154         if ((unsigned int)end > IPV6_MAXPLEN) {
155                 pr_debug("offset is too large.\n");
156                 return -1;
157         }
158
159         ecn = ip6_frag_ecn(ipv6_hdr(skb));
160
161         if (skb->ip_summed == CHECKSUM_COMPLETE) {
162                 const unsigned char *nh = skb_network_header(skb);
163                 skb->csum = csum_sub(skb->csum,
164                                      csum_partial(nh, (u8 *)(fhdr + 1) - nh,
165                                                   0));
166         }
167
168         /* Is this the final fragment? */
169         if (!(fhdr->frag_off & htons(IP6_MF))) {
170                 /* If we already have some bits beyond end
171                  * or have different end, the segment is corrupted.
172                  */
173                 if (end < fq->q.len ||
174                     ((qp_flags(fq) & INET_FRAG_LAST_IN) && end != fq->q.len)) {
175                         pr_debug("already received last fragment\n");
176                         goto err;
177                 }
178                 qp_flags(fq) |= INET_FRAG_LAST_IN;
179                 fq->q.len = end;
180         } else {
181                 /* Check if the fragment is rounded to 8 bytes.
182                  * Required by the RFC.
183                  */
184                 if (end & 0x7) {
185                         /* RFC2460 says always send parameter problem in
186                          * this case. -DaveM
187                          */
188                         pr_debug("end of fragment not rounded to 8 bytes.\n");
189                         return -1;
190                 }
191                 if (end > fq->q.len) {
192                         /* Some bits beyond end -> corruption. */
193                         if (qp_flags(fq) & INET_FRAG_LAST_IN) {
194                                 pr_debug("last packet already reached.\n");
195                                 goto err;
196                         }
197                         fq->q.len = end;
198                 }
199         }
200
201         if (end == offset)
202                 goto err;
203
204         /* Point into the IP datagram 'data' part. */
205         if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
206                 pr_debug("queue: message is too short.\n");
207                 goto err;
208         }
209         if (pskb_trim_rcsum(skb, end - offset)) {
210                 pr_debug("Can't trim\n");
211                 goto err;
212         }
213
214         /* Find out which fragments are in front and at the back of us
215          * in the chain of fragments so far.  We must know where to put
216          * this fragment, right?
217          */
218         prev = fq->q.fragments_tail;
219         if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) {
220                 next = NULL;
221                 goto found;
222         }
223         prev = NULL;
224         for (next = fq->q.fragments; next != NULL; next = next->next) {
225                 if (NFCT_FRAG6_CB(next)->offset >= offset)
226                         break;  /* bingo! */
227                 prev = next;
228         }
229
230 found:
231         /* RFC5722, Section 4:
232          *                                  When reassembling an IPv6 datagram, if
233          *   one or more its constituent fragments is determined to be an
234          *   overlapping fragment, the entire datagram (and any constituent
235          *   fragments, including those not yet received) MUST be silently
236          *   discarded.
237          */
238
239         /* Check for overlap with preceding fragment. */
240         if (prev &&
241             (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset)
242                 goto discard_fq;
243
244         /* Look for overlap with succeeding segment. */
245         if (next && NFCT_FRAG6_CB(next)->offset < end)
246                 goto discard_fq;
247
248         NFCT_FRAG6_CB(skb)->offset = offset;
249
250         /* Insert this fragment in the chain of fragments. */
251         skb->next = next;
252         if (!next)
253                 fq->q.fragments_tail = skb;
254         if (prev)
255                 prev->next = skb;
256         else
257                 fq->q.fragments = skb;
258
259         if (skb->dev) {
260                 fq->iif = skb->dev->ifindex;
261                 skb->dev = NULL;
262         }
263         fq->q.stamp = skb->tstamp;
264         fq->q.meat += skb->len;
265         fq->ecn |= ecn;
266         if (payload_len > fq->q.max_size)
267                 fq->q.max_size = payload_len;
268         add_frag_mem_limit(fq->q.net, skb->truesize);
269
270         /* The first fragment.
271          * nhoffset is obtained from the first fragment, of course.
272          */
273         if (offset == 0) {
274                 fq->nhoffset = nhoff;
275                 qp_flags(fq) |= INET_FRAG_FIRST_IN;
276         }
277
278         return 0;
279
280 discard_fq:
281         inet_frag_kill(&fq->q, &nf_frags);
282 err:
283         return -1;
284 }
285
286 /*
287  *      Check if this packet is complete.
288  *
289  *      It is called with locked fq, and caller must check that
290  *      queue is eligible for reassembly i.e. it is not COMPLETE,
291  *      the last and the first frames arrived and all the bits are here.
292  *
293  *      returns true if *prev skb has been transformed into the reassembled
294  *      skb, false otherwise.
295  */
296 static bool
297 nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *prev,  struct net_device *dev)
298 {
299         struct sk_buff *fp, *head = fq->q.fragments;
300         int    payload_len;
301         u8 ecn;
302
303         inet_frag_kill(&fq->q, &nf_frags);
304
305         WARN_ON(head == NULL);
306         WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
307
308         ecn = ip_frag_ecn_table[fq->ecn];
309         if (unlikely(ecn == 0xff))
310                 return false;
311
312         /* Unfragmented part is taken from the first segment. */
313         payload_len = ((head->data - skb_network_header(head)) -
314                        sizeof(struct ipv6hdr) + fq->q.len -
315                        sizeof(struct frag_hdr));
316         if (payload_len > IPV6_MAXPLEN) {
317                 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
318                                     payload_len);
319                 return false;
320         }
321
322         /* Head of list must not be cloned. */
323         if (skb_unclone(head, GFP_ATOMIC))
324                 return false;
325
326         /* If the first fragment is fragmented itself, we split
327          * it to two chunks: the first with data and paged part
328          * and the second, holding only fragments. */
329         if (skb_has_frag_list(head)) {
330                 struct sk_buff *clone;
331                 int i, plen = 0;
332
333                 clone = alloc_skb(0, GFP_ATOMIC);
334                 if (clone == NULL)
335                         return false;
336
337                 clone->next = head->next;
338                 head->next = clone;
339                 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
340                 skb_frag_list_init(head);
341                 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
342                         plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
343                 clone->len = clone->data_len = head->data_len - plen;
344                 head->data_len -= clone->len;
345                 head->len -= clone->len;
346                 clone->csum = 0;
347                 clone->ip_summed = head->ip_summed;
348
349                 add_frag_mem_limit(fq->q.net, clone->truesize);
350         }
351
352         /* morph head into last received skb: prev.
353          *
354          * This allows callers of ipv6 conntrack defrag to continue
355          * to use the last skb(frag) passed into the reasm engine.
356          * The last skb frag 'silently' turns into the full reassembled skb.
357          *
358          * Since prev is also part of q->fragments we have to clone it first.
359          */
360         if (head != prev) {
361                 struct sk_buff *iter;
362
363                 fp = skb_clone(prev, GFP_ATOMIC);
364                 if (!fp)
365                         return false;
366
367                 fp->next = prev->next;
368                 skb_queue_walk(head, iter) {
369                         if (iter->next != prev)
370                                 continue;
371                         iter->next = fp;
372                         break;
373                 }
374
375                 skb_morph(prev, head);
376                 prev->next = head->next;
377                 consume_skb(head);
378                 head = prev;
379         }
380
381         /* We have to remove fragment header from datagram and to relocate
382          * header in order to calculate ICV correctly. */
383         skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
384         memmove(head->head + sizeof(struct frag_hdr), head->head,
385                 (head->data - head->head) - sizeof(struct frag_hdr));
386         head->mac_header += sizeof(struct frag_hdr);
387         head->network_header += sizeof(struct frag_hdr);
388
389         skb_shinfo(head)->frag_list = head->next;
390         skb_reset_transport_header(head);
391         skb_push(head, head->data - skb_network_header(head));
392
393         for (fp=head->next; fp; fp = fp->next) {
394                 head->data_len += fp->len;
395                 head->len += fp->len;
396                 if (head->ip_summed != fp->ip_summed)
397                         head->ip_summed = CHECKSUM_NONE;
398                 else if (head->ip_summed == CHECKSUM_COMPLETE)
399                         head->csum = csum_add(head->csum, fp->csum);
400                 head->truesize += fp->truesize;
401         }
402         sub_frag_mem_limit(fq->q.net, head->truesize);
403
404         head->ignore_df = 1;
405         head->next = NULL;
406         head->dev = dev;
407         head->tstamp = fq->q.stamp;
408         ipv6_hdr(head)->payload_len = htons(payload_len);
409         ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
410         IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
411
412         /* Yes, and fold redundant checksum back. 8) */
413         if (head->ip_summed == CHECKSUM_COMPLETE)
414                 head->csum = csum_partial(skb_network_header(head),
415                                           skb_network_header_len(head),
416                                           head->csum);
417
418         fq->q.fragments = NULL;
419         fq->q.fragments_tail = NULL;
420
421         return true;
422 }
423
424 /*
425  * find the header just before Fragment Header.
426  *
427  * if success return 0 and set ...
428  * (*prevhdrp): the value of "Next Header Field" in the header
429  *              just before Fragment Header.
430  * (*prevhoff): the offset of "Next Header Field" in the header
431  *              just before Fragment Header.
432  * (*fhoff)   : the offset of Fragment Header.
433  *
434  * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
435  *
436  */
437 static int
438 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
439 {
440         u8 nexthdr = ipv6_hdr(skb)->nexthdr;
441         const int netoff = skb_network_offset(skb);
442         u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
443         int start = netoff + sizeof(struct ipv6hdr);
444         int len = skb->len - start;
445         u8 prevhdr = NEXTHDR_IPV6;
446
447         while (nexthdr != NEXTHDR_FRAGMENT) {
448                 struct ipv6_opt_hdr hdr;
449                 int hdrlen;
450
451                 if (!ipv6_ext_hdr(nexthdr)) {
452                         return -1;
453                 }
454                 if (nexthdr == NEXTHDR_NONE) {
455                         pr_debug("next header is none\n");
456                         return -1;
457                 }
458                 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
459                         pr_debug("too short\n");
460                         return -1;
461                 }
462                 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
463                         BUG();
464                 if (nexthdr == NEXTHDR_AUTH)
465                         hdrlen = (hdr.hdrlen+2)<<2;
466                 else
467                         hdrlen = ipv6_optlen(&hdr);
468
469                 prevhdr = nexthdr;
470                 prev_nhoff = start;
471
472                 nexthdr = hdr.nexthdr;
473                 len -= hdrlen;
474                 start += hdrlen;
475         }
476
477         if (len < 0)
478                 return -1;
479
480         *prevhdrp = prevhdr;
481         *prevhoff = prev_nhoff;
482         *fhoff = start;
483
484         return 0;
485 }
486
487 int rpl_nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)
488 {
489         struct net_device *dev = skb->dev;
490         int fhoff, nhoff, ret;
491         struct frag_hdr *fhdr;
492         struct frag_queue *fq;
493         struct ipv6hdr *hdr;
494         u8 prevhdr;
495
496         /* Jumbo payload inhibits frag. header */
497         if (ipv6_hdr(skb)->payload_len == 0) {
498                 pr_debug("payload len = 0\n");
499                 return -EINVAL;
500         }
501
502         if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
503                 return -EINVAL;
504
505         if (!pskb_may_pull(skb, fhoff + sizeof(*fhdr)))
506                 return -ENOMEM;
507
508         skb_set_transport_header(skb, fhoff);
509         hdr = ipv6_hdr(skb);
510         fhdr = (struct frag_hdr *)skb_transport_header(skb);
511
512         fq = fq_find(net, fhdr->identification, user, &hdr->saddr, &hdr->daddr,
513                      ip6_frag_ecn(hdr));
514         if (fq == NULL)
515                 return -ENOMEM;
516
517         spin_lock_bh(&fq->q.lock);
518
519         if (nf_ct_frag6_queue(fq, skb, fhdr, nhoff) < 0) {
520                 ret = -EINVAL;
521                 goto out_unlock;
522         }
523
524         /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
525          * must be returned.
526          */
527         ret = -EINPROGRESS;
528         if (qp_flags(fq) == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
529             fq->q.meat == fq->q.len &&
530             nf_ct_frag6_reasm(fq, skb, dev))
531                 ret = 0;
532
533 out_unlock:
534         spin_unlock_bh(&fq->q.lock);
535         inet_frag_put(&fq->q, &nf_frags);
536         return ret;
537 }
538 EXPORT_SYMBOL_GPL(rpl_nf_ct_frag6_gather);
539
540 #ifdef HAVE_INET_FRAGS_CONST
541 static void rpl_ip6_frag_init(struct inet_frag_queue *q, const void *a)
542 #else
543 static void rpl_ip6_frag_init(struct inet_frag_queue *q, void *a)
544 #endif
545 {
546         struct frag_queue *fq = container_of(q, struct frag_queue, q);
547         const struct ip6_create_arg *arg = a;
548
549         fq->id = arg->id;
550         fq->user = arg->user;
551         fq->saddr = *arg->src;
552         fq->daddr = *arg->dst;
553         fq->ecn = arg->ecn;
554 }
555
556 #ifdef HAVE_INET_FRAGS_CONST
557 static bool rpl_ip6_frag_match(const struct inet_frag_queue *q, const void *a)
558 #else
559 static bool rpl_ip6_frag_match(struct inet_frag_queue *q, void *a)
560 #endif
561 {
562         const struct frag_queue *fq;
563         const struct ip6_create_arg *arg = a;
564
565         fq = container_of(q, struct frag_queue, q);
566         return  fq->id == arg->id &&
567                 fq->user == arg->user &&
568                 ipv6_addr_equal(&fq->saddr, arg->src) &&
569                 ipv6_addr_equal(&fq->daddr, arg->dst);
570 }
571
572 static int nf_ct_net_init(struct net *net)
573 {
574         nf_defrag_ipv6_enable();
575
576         return 0;
577 }
578
579 static void nf_ct_net_exit(struct net *net)
580 {
581         inet_frags_exit_net(&net->ipv6.frags, &nf_frags);
582 }
583
584 static struct pernet_operations nf_ct_net_ops = {
585         .init = nf_ct_net_init,
586         .exit = nf_ct_net_exit,
587 };
588
589 int rpl_nf_ct_frag6_init(void)
590 {
591         int ret = 0;
592
593         nf_frags.hashfn = nf_hashfn;
594         nf_frags.constructor = rpl_ip6_frag_init;
595         nf_frags.destructor = NULL;
596         nf_frags.qsize = sizeof(struct frag_queue);
597         nf_frags.match = rpl_ip6_frag_match;
598         nf_frags.frag_expire = nf_ct_frag6_expire;
599 #ifdef HAVE_INET_FRAGS_WITH_FRAGS_WORK
600         nf_frags.frags_cache_name = nf_frags_cache_name;
601 #endif
602         ret = inet_frags_init(&nf_frags);
603         if (ret)
604                 goto out;
605         ret = register_pernet_subsys(&nf_ct_net_ops);
606         if (ret)
607                 inet_frags_fini(&nf_frags);
608
609 out:
610         return ret;
611 }
612
613 void rpl_nf_ct_frag6_cleanup(void)
614 {
615         unregister_pernet_subsys(&nf_ct_net_ops);
616         inet_frags_fini(&nf_frags);
617 }
618
619 #endif /* OVS_NF_DEFRAG6_BACKPORT */