Linux 3.2.102
[pandora-kernel.git] / net / ipv6 / reassembly.c
1 /*
2  *      IPv6 fragment reassembly
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>
7  *
8  *      Based on: net/ipv4/ip_fragment.c
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 /*
17  *      Fixes:
18  *      Andi Kleen      Make it work with multiple hosts.
19  *                      More RFC compliance.
20  *
21  *      Horst von Brand Add missing #include <linux/string.h>
22  *      Alexey Kuznetsov        SMP races, threading, cleanup.
23  *      Patrick McHardy         LRU queue of frag heads for evictor.
24  *      Mitsuru KANDA @USAGI    Register inet6_protocol{}.
25  *      David Stevens and
26  *      YOSHIFUJI,H. @USAGI     Always remove fragment header to
27  *                              calculate ICV correctly.
28  */
29
30 #define pr_fmt(fmt) "IPv6: " fmt
31
32 #include <linux/errno.h>
33 #include <linux/types.h>
34 #include <linux/string.h>
35 #include <linux/socket.h>
36 #include <linux/sockios.h>
37 #include <linux/jiffies.h>
38 #include <linux/net.h>
39 #include <linux/list.h>
40 #include <linux/netdevice.h>
41 #include <linux/in6.h>
42 #include <linux/ipv6.h>
43 #include <linux/icmpv6.h>
44 #include <linux/random.h>
45 #include <linux/jhash.h>
46 #include <linux/skbuff.h>
47 #include <linux/slab.h>
48 #include <linux/export.h>
49
50 #include <net/sock.h>
51 #include <net/snmp.h>
52
53 #include <net/ipv6.h>
54 #include <net/ip6_route.h>
55 #include <net/protocol.h>
56 #include <net/transp_v6.h>
57 #include <net/rawv6.h>
58 #include <net/ndisc.h>
59 #include <net/addrconf.h>
60 #include <net/inet_frag.h>
61
62 struct ip6frag_skb_cb
63 {
64         struct inet6_skb_parm   h;
65         int                     offset;
66 };
67
68 #define FRAG6_CB(skb)   ((struct ip6frag_skb_cb*)((skb)->cb))
69
70
71 /*
72  *      Equivalent of ipv4 struct ipq
73  */
74
75 struct frag_queue
76 {
77         struct inet_frag_queue  q;
78
79         __be32                  id;             /* fragment id          */
80         u32                     user;
81         struct in6_addr         saddr;
82         struct in6_addr         daddr;
83
84         int                     iif;
85         unsigned int            csum;
86         __u16                   nhoffset;
87 };
88
89 static struct inet_frags ip6_frags;
90
91 int ip6_frag_nqueues(struct net *net)
92 {
93         return net->ipv6.frags.nqueues;
94 }
95
96 int ip6_frag_mem(struct net *net)
97 {
98         return atomic_read(&net->ipv6.frags.mem);
99 }
100
101 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
102                           struct net_device *dev);
103
104 /*
105  * callers should be careful not to use the hash value outside the ipfrag_lock
106  * as doing so could race with ipfrag_hash_rnd being recalculated.
107  */
108 unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr,
109                              const struct in6_addr *daddr, u32 rnd)
110 {
111         u32 c;
112
113         c = jhash_3words((__force u32)saddr->s6_addr32[0],
114                          (__force u32)saddr->s6_addr32[1],
115                          (__force u32)saddr->s6_addr32[2],
116                          rnd);
117
118         c = jhash_3words((__force u32)saddr->s6_addr32[3],
119                          (__force u32)daddr->s6_addr32[0],
120                          (__force u32)daddr->s6_addr32[1],
121                          c);
122
123         c =  jhash_3words((__force u32)daddr->s6_addr32[2],
124                           (__force u32)daddr->s6_addr32[3],
125                           (__force u32)id,
126                           c);
127
128         return c & (INETFRAGS_HASHSZ - 1);
129 }
130 EXPORT_SYMBOL_GPL(inet6_hash_frag);
131
132 static unsigned int ip6_hashfn(struct inet_frag_queue *q)
133 {
134         struct frag_queue *fq;
135
136         fq = container_of(q, struct frag_queue, q);
137         return inet6_hash_frag(fq->id, &fq->saddr, &fq->daddr, ip6_frags.rnd);
138 }
139
140 int ip6_frag_match(struct inet_frag_queue *q, void *a)
141 {
142         struct frag_queue *fq;
143         struct ip6_create_arg *arg = a;
144
145         fq = container_of(q, struct frag_queue, q);
146         return (fq->id == arg->id && fq->user == arg->user &&
147                 ipv6_addr_equal(&fq->saddr, arg->src) &&
148                 ipv6_addr_equal(&fq->daddr, arg->dst) &&
149                 (arg->iif == fq->iif ||
150                  !(ipv6_addr_type(arg->dst) & (IPV6_ADDR_MULTICAST |
151                                                IPV6_ADDR_LINKLOCAL))));
152 }
153 EXPORT_SYMBOL(ip6_frag_match);
154
155 void ip6_frag_init(struct inet_frag_queue *q, void *a)
156 {
157         struct frag_queue *fq = container_of(q, struct frag_queue, q);
158         struct ip6_create_arg *arg = a;
159
160         fq->id = arg->id;
161         fq->user = arg->user;
162         ipv6_addr_copy(&fq->saddr, arg->src);
163         ipv6_addr_copy(&fq->daddr, arg->dst);
164 }
165 EXPORT_SYMBOL(ip6_frag_init);
166
167 /* Destruction primitives. */
168
169 static __inline__ void fq_put(struct frag_queue *fq)
170 {
171         inet_frag_put(&fq->q, &ip6_frags);
172 }
173
174 /* Kill fq entry. It is not destroyed immediately,
175  * because caller (and someone more) holds reference count.
176  */
177 static __inline__ void fq_kill(struct frag_queue *fq)
178 {
179         inet_frag_kill(&fq->q, &ip6_frags);
180 }
181
182 static void ip6_evictor(struct net *net, struct inet6_dev *idev)
183 {
184         int evicted;
185
186         evicted = inet_frag_evictor(&net->ipv6.frags, &ip6_frags);
187         if (evicted)
188                 IP6_ADD_STATS_BH(net, idev, IPSTATS_MIB_REASMFAILS, evicted);
189 }
190
191 static void ip6_frag_expire(unsigned long data)
192 {
193         struct frag_queue *fq;
194         struct net_device *dev = NULL;
195         struct net *net;
196
197         fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
198
199         spin_lock(&fq->q.lock);
200
201         if (fq->q.last_in & INET_FRAG_COMPLETE)
202                 goto out;
203
204         fq_kill(fq);
205
206         net = container_of(fq->q.net, struct net, ipv6.frags);
207         rcu_read_lock();
208         dev = dev_get_by_index_rcu(net, fq->iif);
209         if (!dev)
210                 goto out_rcu_unlock;
211
212         IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
213         IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
214
215         /* Don't send error if the first segment did not arrive. */
216         if (!(fq->q.last_in & INET_FRAG_FIRST_IN) || !fq->q.fragments)
217                 goto out_rcu_unlock;
218
219         /*
220            But use as source device on which LAST ARRIVED
221            segment was received. And do not use fq->dev
222            pointer directly, device might already disappeared.
223          */
224         fq->q.fragments->dev = dev;
225         icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
226 out_rcu_unlock:
227         rcu_read_unlock();
228 out:
229         spin_unlock(&fq->q.lock);
230         fq_put(fq);
231 }
232
233 static __inline__ struct frag_queue *
234 fq_find(struct net *net, __be32 id, const struct in6_addr *src,
235         const struct in6_addr *dst, int iif)
236 {
237         struct inet_frag_queue *q;
238         struct ip6_create_arg arg;
239         unsigned int hash;
240
241         arg.id = id;
242         arg.user = IP6_DEFRAG_LOCAL_DELIVER;
243         arg.src = src;
244         arg.dst = dst;
245         arg.iif = iif;
246
247         read_lock(&ip6_frags.lock);
248         hash = inet6_hash_frag(id, src, dst, ip6_frags.rnd);
249
250         q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash);
251         if (IS_ERR_OR_NULL(q)) {
252                 inet_frag_maybe_warn_overflow(q, pr_fmt());
253                 return NULL;
254         }
255         return container_of(q, struct frag_queue, q);
256 }
257
258 static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
259                            struct frag_hdr *fhdr, int nhoff)
260 {
261         struct sk_buff *prev, *next;
262         struct net_device *dev;
263         int offset, end;
264         struct net *net = dev_net(skb_dst(skb)->dev);
265
266         if (fq->q.last_in & INET_FRAG_COMPLETE)
267                 goto err;
268
269         offset = ntohs(fhdr->frag_off) & ~0x7;
270         end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
271                         ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
272
273         if ((unsigned int)end > IPV6_MAXPLEN) {
274                 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
275                                  IPSTATS_MIB_INHDRERRORS);
276                 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
277                                   ((u8 *)&fhdr->frag_off -
278                                    skb_network_header(skb)));
279                 return -1;
280         }
281
282         if (skb->ip_summed == CHECKSUM_COMPLETE) {
283                 const unsigned char *nh = skb_network_header(skb);
284                 skb->csum = csum_sub(skb->csum,
285                                      csum_partial(nh, (u8 *)(fhdr + 1) - nh,
286                                                   0));
287         }
288
289         /* Is this the final fragment? */
290         if (!(fhdr->frag_off & htons(IP6_MF))) {
291                 /* If we already have some bits beyond end
292                  * or have different end, the segment is corrupted.
293                  */
294                 if (end < fq->q.len ||
295                     ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len))
296                         goto err;
297                 fq->q.last_in |= INET_FRAG_LAST_IN;
298                 fq->q.len = end;
299         } else {
300                 /* Check if the fragment is rounded to 8 bytes.
301                  * Required by the RFC.
302                  */
303                 if (end & 0x7) {
304                         /* RFC2460 says always send parameter problem in
305                          * this case. -DaveM
306                          */
307                         IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
308                                          IPSTATS_MIB_INHDRERRORS);
309                         icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
310                                           offsetof(struct ipv6hdr, payload_len));
311                         return -1;
312                 }
313                 if (end > fq->q.len) {
314                         /* Some bits beyond end -> corruption. */
315                         if (fq->q.last_in & INET_FRAG_LAST_IN)
316                                 goto err;
317                         fq->q.len = end;
318                 }
319         }
320
321         if (end == offset)
322                 goto err;
323
324         /* Point into the IP datagram 'data' part. */
325         if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
326                 goto err;
327
328         if (pskb_trim_rcsum(skb, end - offset))
329                 goto err;
330
331         /* Find out which fragments are in front and at the back of us
332          * in the chain of fragments so far.  We must know where to put
333          * this fragment, right?
334          */
335         prev = fq->q.fragments_tail;
336         if (!prev || FRAG6_CB(prev)->offset < offset) {
337                 next = NULL;
338                 goto found;
339         }
340         prev = NULL;
341         for(next = fq->q.fragments; next != NULL; next = next->next) {
342                 if (FRAG6_CB(next)->offset >= offset)
343                         break;  /* bingo! */
344                 prev = next;
345         }
346
347 found:
348         /* RFC5722, Section 4:
349          *                                  When reassembling an IPv6 datagram, if
350          *   one or more its constituent fragments is determined to be an
351          *   overlapping fragment, the entire datagram (and any constituent
352          *   fragments, including those not yet received) MUST be silently
353          *   discarded.
354          */
355
356         /* Check for overlap with preceding fragment. */
357         if (prev &&
358             (FRAG6_CB(prev)->offset + prev->len) > offset)
359                 goto discard_fq;
360
361         /* Look for overlap with succeeding segment. */
362         if (next && FRAG6_CB(next)->offset < end)
363                 goto discard_fq;
364
365         FRAG6_CB(skb)->offset = offset;
366
367         /* Insert this fragment in the chain of fragments. */
368         skb->next = next;
369         if (!next)
370                 fq->q.fragments_tail = skb;
371         if (prev)
372                 prev->next = skb;
373         else
374                 fq->q.fragments = skb;
375
376         dev = skb->dev;
377         if (dev) {
378                 fq->iif = dev->ifindex;
379                 skb->dev = NULL;
380         }
381         fq->q.stamp = skb->tstamp;
382         fq->q.meat += skb->len;
383         atomic_add(skb->truesize, &fq->q.net->mem);
384
385         /* The first fragment.
386          * nhoffset is obtained from the first fragment, of course.
387          */
388         if (offset == 0) {
389                 fq->nhoffset = nhoff;
390                 fq->q.last_in |= INET_FRAG_FIRST_IN;
391         }
392
393         if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
394             fq->q.meat == fq->q.len) {
395                 int res;
396                 unsigned long orefdst = skb->_skb_refdst;
397
398                 skb->_skb_refdst = 0UL;
399                 res = ip6_frag_reasm(fq, prev, dev);
400                 skb->_skb_refdst = orefdst;
401                 return res;
402         }
403
404         skb_dst_drop(skb);
405
406         write_lock(&ip6_frags.lock);
407         list_move_tail(&fq->q.lru_list, &fq->q.net->lru_list);
408         write_unlock(&ip6_frags.lock);
409         return -1;
410
411 discard_fq:
412         fq_kill(fq);
413 err:
414         IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
415                       IPSTATS_MIB_REASMFAILS);
416         kfree_skb(skb);
417         return -1;
418 }
419
420 /*
421  *      Check if this packet is complete.
422  *      Returns NULL on failure by any reason, and pointer
423  *      to current nexthdr field in reassembled frame.
424  *
425  *      It is called with locked fq, and caller must check that
426  *      queue is eligible for reassembly i.e. it is not COMPLETE,
427  *      the last and the first frames arrived and all the bits are here.
428  */
429 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
430                           struct net_device *dev)
431 {
432         struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
433         struct sk_buff *fp, *head = fq->q.fragments;
434         int    payload_len;
435         unsigned int nhoff;
436
437         fq_kill(fq);
438
439         /* Make the one we just received the head. */
440         if (prev) {
441                 head = prev->next;
442                 fp = skb_clone(head, GFP_ATOMIC);
443
444                 if (!fp)
445                         goto out_oom;
446
447                 fp->next = head->next;
448                 if (!fp->next)
449                         fq->q.fragments_tail = fp;
450                 prev->next = fp;
451
452                 skb_morph(head, fq->q.fragments);
453                 head->next = fq->q.fragments->next;
454
455                 kfree_skb(fq->q.fragments);
456                 fq->q.fragments = head;
457         }
458
459         WARN_ON(head == NULL);
460         WARN_ON(FRAG6_CB(head)->offset != 0);
461
462         /* Unfragmented part is taken from the first segment. */
463         payload_len = ((head->data - skb_network_header(head)) -
464                        sizeof(struct ipv6hdr) + fq->q.len -
465                        sizeof(struct frag_hdr));
466         if (payload_len > IPV6_MAXPLEN)
467                 goto out_oversize;
468
469         /* Head of list must not be cloned. */
470         if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
471                 goto out_oom;
472
473         /* If the first fragment is fragmented itself, we split
474          * it to two chunks: the first with data and paged part
475          * and the second, holding only fragments. */
476         if (skb_has_frag_list(head)) {
477                 struct sk_buff *clone;
478                 int i, plen = 0;
479
480                 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
481                         goto out_oom;
482                 clone->next = head->next;
483                 head->next = clone;
484                 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
485                 skb_frag_list_init(head);
486                 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
487                         plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
488                 clone->len = clone->data_len = head->data_len - plen;
489                 head->data_len -= clone->len;
490                 head->len -= clone->len;
491                 clone->csum = 0;
492                 clone->ip_summed = head->ip_summed;
493                 atomic_add(clone->truesize, &fq->q.net->mem);
494         }
495
496         /* We have to remove fragment header from datagram and to relocate
497          * header in order to calculate ICV correctly. */
498         nhoff = fq->nhoffset;
499         skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
500         memmove(head->head + sizeof(struct frag_hdr), head->head,
501                 (head->data - head->head) - sizeof(struct frag_hdr));
502         head->mac_header += sizeof(struct frag_hdr);
503         head->network_header += sizeof(struct frag_hdr);
504
505         skb_shinfo(head)->frag_list = head->next;
506         skb_reset_transport_header(head);
507         skb_push(head, head->data - skb_network_header(head));
508
509         for (fp=head->next; fp; fp = fp->next) {
510                 head->data_len += fp->len;
511                 head->len += fp->len;
512                 if (head->ip_summed != fp->ip_summed)
513                         head->ip_summed = CHECKSUM_NONE;
514                 else if (head->ip_summed == CHECKSUM_COMPLETE)
515                         head->csum = csum_add(head->csum, fp->csum);
516                 head->truesize += fp->truesize;
517         }
518         atomic_sub(head->truesize, &fq->q.net->mem);
519
520         head->next = NULL;
521         head->dev = dev;
522         head->tstamp = fq->q.stamp;
523         ipv6_hdr(head)->payload_len = htons(payload_len);
524         IP6CB(head)->nhoff = nhoff;
525         IP6CB(head)->flags |= IP6SKB_FRAGMENTED;
526
527         /* Yes, and fold redundant checksum back. 8) */
528         if (head->ip_summed == CHECKSUM_COMPLETE)
529                 head->csum = csum_partial(skb_network_header(head),
530                                           skb_network_header_len(head),
531                                           head->csum);
532
533         rcu_read_lock();
534         IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
535         rcu_read_unlock();
536         fq->q.fragments = NULL;
537         fq->q.fragments_tail = NULL;
538         return 1;
539
540 out_oversize:
541         if (net_ratelimit())
542                 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
543         goto out_fail;
544 out_oom:
545         if (net_ratelimit())
546                 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
547 out_fail:
548         rcu_read_lock();
549         IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
550         rcu_read_unlock();
551         return -1;
552 }
553
554 static int ipv6_frag_rcv(struct sk_buff *skb)
555 {
556         struct frag_hdr *fhdr;
557         struct frag_queue *fq;
558         const struct ipv6hdr *hdr = ipv6_hdr(skb);
559         struct net *net = dev_net(skb_dst(skb)->dev);
560
561         if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
562                 goto fail_hdr;
563
564         IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
565
566         /* Jumbo payload inhibits frag. header */
567         if (hdr->payload_len==0)
568                 goto fail_hdr;
569
570         if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
571                                  sizeof(struct frag_hdr))))
572                 goto fail_hdr;
573
574         hdr = ipv6_hdr(skb);
575         fhdr = (struct frag_hdr *)skb_transport_header(skb);
576
577         if (!(fhdr->frag_off & htons(0xFFF9))) {
578                 /* It is not a fragmented frame */
579                 skb->transport_header += sizeof(struct frag_hdr);
580                 IP6_INC_STATS_BH(net,
581                                  ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
582
583                 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
584                 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
585                 return 1;
586         }
587
588         if (atomic_read(&net->ipv6.frags.mem) > net->ipv6.frags.high_thresh)
589                 ip6_evictor(net, ip6_dst_idev(skb_dst(skb)));
590
591         fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr,
592                      skb->dev ? skb->dev->ifindex : 0);
593         if (fq != NULL) {
594                 int ret;
595
596                 spin_lock(&fq->q.lock);
597
598                 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
599
600                 spin_unlock(&fq->q.lock);
601                 fq_put(fq);
602                 return ret;
603         }
604
605         IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
606         kfree_skb(skb);
607         return -1;
608
609 fail_hdr:
610         IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
611         icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
612         return -1;
613 }
614
615 static const struct inet6_protocol frag_protocol =
616 {
617         .handler        =       ipv6_frag_rcv,
618         .flags          =       INET6_PROTO_NOPOLICY,
619 };
620
621 #ifdef CONFIG_SYSCTL
622 static struct ctl_table ip6_frags_ns_ctl_table[] = {
623         {
624                 .procname       = "ip6frag_high_thresh",
625                 .data           = &init_net.ipv6.frags.high_thresh,
626                 .maxlen         = sizeof(int),
627                 .mode           = 0644,
628                 .proc_handler   = proc_dointvec
629         },
630         {
631                 .procname       = "ip6frag_low_thresh",
632                 .data           = &init_net.ipv6.frags.low_thresh,
633                 .maxlen         = sizeof(int),
634                 .mode           = 0644,
635                 .proc_handler   = proc_dointvec
636         },
637         {
638                 .procname       = "ip6frag_time",
639                 .data           = &init_net.ipv6.frags.timeout,
640                 .maxlen         = sizeof(int),
641                 .mode           = 0644,
642                 .proc_handler   = proc_dointvec_jiffies,
643         },
644         { }
645 };
646
647 static struct ctl_table ip6_frags_ctl_table[] = {
648         {
649                 .procname       = "ip6frag_secret_interval",
650                 .data           = &ip6_frags.secret_interval,
651                 .maxlen         = sizeof(int),
652                 .mode           = 0644,
653                 .proc_handler   = proc_dointvec_jiffies,
654         },
655         { }
656 };
657
658 static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
659 {
660         struct ctl_table *table;
661         struct ctl_table_header *hdr;
662
663         table = ip6_frags_ns_ctl_table;
664         if (!net_eq(net, &init_net)) {
665                 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
666                 if (table == NULL)
667                         goto err_alloc;
668
669                 table[0].data = &net->ipv6.frags.high_thresh;
670                 table[1].data = &net->ipv6.frags.low_thresh;
671                 table[2].data = &net->ipv6.frags.timeout;
672         }
673
674         hdr = register_net_sysctl_table(net, net_ipv6_ctl_path, table);
675         if (hdr == NULL)
676                 goto err_reg;
677
678         net->ipv6.sysctl.frags_hdr = hdr;
679         return 0;
680
681 err_reg:
682         if (!net_eq(net, &init_net))
683                 kfree(table);
684 err_alloc:
685         return -ENOMEM;
686 }
687
688 static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
689 {
690         struct ctl_table *table;
691
692         table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
693         unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
694         if (!net_eq(net, &init_net))
695                 kfree(table);
696 }
697
698 static struct ctl_table_header *ip6_ctl_header;
699
700 static int ip6_frags_sysctl_register(void)
701 {
702         ip6_ctl_header = register_net_sysctl_rotable(net_ipv6_ctl_path,
703                         ip6_frags_ctl_table);
704         return ip6_ctl_header == NULL ? -ENOMEM : 0;
705 }
706
707 static void ip6_frags_sysctl_unregister(void)
708 {
709         unregister_net_sysctl_table(ip6_ctl_header);
710 }
711 #else
712 static inline int ip6_frags_ns_sysctl_register(struct net *net)
713 {
714         return 0;
715 }
716
717 static inline void ip6_frags_ns_sysctl_unregister(struct net *net)
718 {
719 }
720
721 static inline int ip6_frags_sysctl_register(void)
722 {
723         return 0;
724 }
725
726 static inline void ip6_frags_sysctl_unregister(void)
727 {
728 }
729 #endif
730
731 static int __net_init ipv6_frags_init_net(struct net *net)
732 {
733         net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
734         net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
735         net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
736
737         inet_frags_init_net(&net->ipv6.frags);
738
739         return ip6_frags_ns_sysctl_register(net);
740 }
741
742 static void __net_exit ipv6_frags_exit_net(struct net *net)
743 {
744         ip6_frags_ns_sysctl_unregister(net);
745         inet_frags_exit_net(&net->ipv6.frags, &ip6_frags);
746 }
747
748 static struct pernet_operations ip6_frags_ops = {
749         .init = ipv6_frags_init_net,
750         .exit = ipv6_frags_exit_net,
751 };
752
753 int __init ipv6_frag_init(void)
754 {
755         int ret;
756
757         ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
758         if (ret)
759                 goto out;
760
761         ret = ip6_frags_sysctl_register();
762         if (ret)
763                 goto err_sysctl;
764
765         ret = register_pernet_subsys(&ip6_frags_ops);
766         if (ret)
767                 goto err_pernet;
768
769         ip6_frags.hashfn = ip6_hashfn;
770         ip6_frags.constructor = ip6_frag_init;
771         ip6_frags.destructor = NULL;
772         ip6_frags.skb_free = NULL;
773         ip6_frags.qsize = sizeof(struct frag_queue);
774         ip6_frags.match = ip6_frag_match;
775         ip6_frags.frag_expire = ip6_frag_expire;
776         ip6_frags.secret_interval = 10 * 60 * HZ;
777         inet_frags_init(&ip6_frags);
778 out:
779         return ret;
780
781 err_pernet:
782         ip6_frags_sysctl_unregister();
783 err_sysctl:
784         inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
785         goto out;
786 }
787
788 void ipv6_frag_exit(void)
789 {
790         inet_frags_fini(&ip6_frags);
791         ip6_frags_sysctl_unregister();
792         unregister_pernet_subsys(&ip6_frags_ops);
793         inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
794 }