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