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