blob: bab2c270f29207c37ed7b107b6d25c6e12a6053d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 fragment reassembly
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090016/*
17 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/errno.h>
30#include <linux/types.h>
31#include <linux/string.h>
32#include <linux/socket.h>
33#include <linux/sockios.h>
34#include <linux/jiffies.h>
35#include <linux/net.h>
36#include <linux/list.h>
37#include <linux/netdevice.h>
38#include <linux/in6.h>
39#include <linux/ipv6.h>
40#include <linux/icmpv6.h>
41#include <linux/random.h>
42#include <linux/jhash.h>
Herbert Xuf61944e2007-10-15 01:28:47 -070043#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040045#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <net/sock.h>
48#include <net/snmp.h>
49
50#include <net/ipv6.h>
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +090051#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/protocol.h>
53#include <net/transp_v6.h>
54#include <net/rawv6.h>
55#include <net/ndisc.h>
56#include <net/addrconf.h>
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070057#include <net/inet_frag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct ip6frag_skb_cb
60{
61 struct inet6_skb_parm h;
62 int offset;
63};
64
65#define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb))
66
67
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070068static struct inet_frags ip6_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Herbert Xuf61944e2007-10-15 01:28:47 -070070static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
71 struct net_device *dev);
72
Zach Brownf6596f92006-04-10 16:05:34 -070073/*
74 * callers should be careful not to use the hash value outside the ipfrag_lock
75 * as doing so could race with ipfrag_hash_rnd being recalculated.
76 */
Ilpo Järvinen93c8b902008-10-01 02:48:31 -070077unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr,
78 const struct in6_addr *daddr, u32 rnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Jozsef Kadlecsik82a39eb2010-11-25 03:15:07 +000080 u32 c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Jozsef Kadlecsik82a39eb2010-11-25 03:15:07 +000082 c = jhash_3words((__force u32)saddr->s6_addr32[0],
83 (__force u32)saddr->s6_addr32[1],
84 (__force u32)saddr->s6_addr32[2],
85 rnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Jozsef Kadlecsik82a39eb2010-11-25 03:15:07 +000087 c = jhash_3words((__force u32)saddr->s6_addr32[3],
88 (__force u32)daddr->s6_addr32[0],
89 (__force u32)daddr->s6_addr32[1],
90 c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jozsef Kadlecsik82a39eb2010-11-25 03:15:07 +000092 c = jhash_3words((__force u32)daddr->s6_addr32[2],
93 (__force u32)daddr->s6_addr32[3],
94 (__force u32)id,
95 c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070097 return c & (INETFRAGS_HASHSZ - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
Ilpo Järvinen93c8b902008-10-01 02:48:31 -070099EXPORT_SYMBOL_GPL(inet6_hash_frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700101static unsigned int ip6_hashfn(struct inet_frag_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700103 struct frag_queue *fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700105 fq = container_of(q, struct frag_queue, q);
Ilpo Järvinen93c8b902008-10-01 02:48:31 -0700106 return inet6_hash_frag(fq->id, &fq->saddr, &fq->daddr, ip6_frags.rnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Eric Dumazetcbc264c2012-05-18 05:57:13 +0200109bool ip6_frag_match(struct inet_frag_queue *q, void *a)
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700110{
111 struct frag_queue *fq;
112 struct ip6_create_arg *arg = a;
113
114 fq = container_of(q, struct frag_queue, q);
Eric Dumazetcbc264c2012-05-18 05:57:13 +0200115 return fq->id == arg->id &&
116 fq->user == arg->user &&
117 ipv6_addr_equal(&fq->saddr, arg->src) &&
118 ipv6_addr_equal(&fq->daddr, arg->dst);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700119}
120EXPORT_SYMBOL(ip6_frag_match);
121
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700122void ip6_frag_init(struct inet_frag_queue *q, void *a)
123{
124 struct frag_queue *fq = container_of(q, struct frag_queue, q);
125 struct ip6_create_arg *arg = a;
126
127 fq->id = arg->id;
Patrick McHardy0b5ccb22009-12-15 16:59:18 +0100128 fq->user = arg->user;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000129 fq->saddr = *arg->src;
130 fq->daddr = *arg->dst;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700131}
132EXPORT_SYMBOL(ip6_frag_init);
133
Amerigo Wangb836c992012-09-18 16:50:09 +0000134void ip6_expire_frag_queue(struct net *net, struct frag_queue *fq,
135 struct inet_frags *frags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900137 struct net_device *dev = NULL;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700138
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700139 spin_lock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Joe Perchesbc578a52008-03-28 16:35:27 -0700141 if (fq->q.last_in & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 goto out;
143
Amerigo Wangb836c992012-09-18 16:50:09 +0000144 inet_frag_kill(&fq->q, frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900146 rcu_read_lock();
Eric Dumazet69df9d52009-11-05 20:59:47 -0800147 dev = dev_get_by_index_rcu(net, fq->iif);
148 if (!dev)
149 goto out_rcu_unlock;
150
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700151 IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
152 IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Ingo Oeser78c784c2006-03-20 23:01:17 -0800154 /* Don't send error if the first segment did not arrive. */
Joe Perchesbc578a52008-03-28 16:35:27 -0700155 if (!(fq->q.last_in & INET_FRAG_FIRST_IN) || !fq->q.fragments)
Eric Dumazet69df9d52009-11-05 20:59:47 -0800156 goto out_rcu_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Ingo Oeser78c784c2006-03-20 23:01:17 -0800158 /*
159 But use as source device on which LAST ARRIVED
160 segment was received. And do not use fq->dev
161 pointer directly, device might already disappeared.
162 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700163 fq->q.fragments->dev = dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000164 icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
Eric Dumazet69df9d52009-11-05 20:59:47 -0800165out_rcu_unlock:
166 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167out:
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700168 spin_unlock(&fq->q.lock);
Amerigo Wangb836c992012-09-18 16:50:09 +0000169 inet_frag_put(&fq->q, frags);
170}
171EXPORT_SYMBOL(ip6_expire_frag_queue);
172
173static void ip6_frag_expire(unsigned long data)
174{
175 struct frag_queue *fq;
176 struct net *net;
177
178 fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
179 net = container_of(fq->q.net, struct net, ipv6.frags);
180
181 ip6_expire_frag_queue(net, fq, &ip6_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700184static __inline__ struct frag_queue *
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000185fq_find(struct net *net, __be32 id, const struct in6_addr *src, const struct in6_addr *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700187 struct inet_frag_queue *q;
188 struct ip6_create_arg arg;
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700189 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700191 arg.id = id;
Patrick McHardy0b5ccb22009-12-15 16:59:18 +0100192 arg.user = IP6_DEFRAG_LOCAL_DELIVER;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700193 arg.src = src;
194 arg.dst = dst;
Pavel Emelyanov9a375802008-06-27 20:06:08 -0700195
196 read_lock(&ip6_frags.lock);
Ilpo Järvinen93c8b902008-10-01 02:48:31 -0700197 hash = inet6_hash_frag(id, src, dst, ip6_frags.rnd);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700198
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800199 q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700200 if (q == NULL)
Shan Wei95463772010-02-11 00:12:45 +0000201 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700203 return container_of(q, struct frag_queue, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Herbert Xuf61944e2007-10-15 01:28:47 -0700206static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct frag_hdr *fhdr, int nhoff)
208{
209 struct sk_buff *prev, *next;
Herbert Xuf61944e2007-10-15 01:28:47 -0700210 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 int offset, end;
Eric Dumazetadf30902009-06-02 05:19:30 +0000212 struct net *net = dev_net(skb_dst(skb)->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Joe Perchesbc578a52008-03-28 16:35:27 -0700214 if (fq->q.last_in & INET_FRAG_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 goto err;
216
217 offset = ntohs(fhdr->frag_off) & ~0x7;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700218 end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
219 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 if ((unsigned int)end > IPV6_MAXPLEN) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000222 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900223 IPSTATS_MIB_INHDRERRORS);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700224 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
225 ((u8 *)&fhdr->frag_off -
226 skb_network_header(skb)));
Herbert Xuf61944e2007-10-15 01:28:47 -0700227 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700230 if (skb->ip_summed == CHECKSUM_COMPLETE) {
231 const unsigned char *nh = skb_network_header(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900232 skb->csum = csum_sub(skb->csum,
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700233 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
234 0));
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /* Is this the final fragment? */
238 if (!(fhdr->frag_off & htons(IP6_MF))) {
239 /* If we already have some bits beyond end
240 * or have different end, the segment is corrupted.
241 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700242 if (end < fq->q.len ||
Joe Perchesbc578a52008-03-28 16:35:27 -0700243 ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto err;
Joe Perchesbc578a52008-03-28 16:35:27 -0700245 fq->q.last_in |= INET_FRAG_LAST_IN;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700246 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 } else {
248 /* Check if the fragment is rounded to 8 bytes.
249 * Required by the RFC.
250 */
251 if (end & 0x7) {
252 /* RFC2460 says always send parameter problem in
253 * this case. -DaveM
254 */
Eric Dumazetadf30902009-06-02 05:19:30 +0000255 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900256 IPSTATS_MIB_INHDRERRORS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900257 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 offsetof(struct ipv6hdr, payload_len));
Herbert Xuf61944e2007-10-15 01:28:47 -0700259 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700261 if (end > fq->q.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /* Some bits beyond end -> corruption. */
Joe Perchesbc578a52008-03-28 16:35:27 -0700263 if (fq->q.last_in & INET_FRAG_LAST_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto err;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700265 fq->q.len = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 }
268
269 if (end == offset)
270 goto err;
271
272 /* Point into the IP datagram 'data' part. */
273 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
274 goto err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900275
Stephen Hemminger42ca89c2005-09-08 12:57:43 -0700276 if (pskb_trim_rcsum(skb, end - offset))
277 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* Find out which fragments are in front and at the back of us
280 * in the chain of fragments so far. We must know where to put
281 * this fragment, right?
282 */
Changli Gaod6bebca2010-06-29 04:39:37 +0000283 prev = fq->q.fragments_tail;
284 if (!prev || FRAG6_CB(prev)->offset < offset) {
285 next = NULL;
286 goto found;
287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 prev = NULL;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700289 for(next = fq->q.fragments; next != NULL; next = next->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (FRAG6_CB(next)->offset >= offset)
291 break; /* bingo! */
292 prev = next;
293 }
294
Changli Gaod6bebca2010-06-29 04:39:37 +0000295found:
Eric Dumazet5de658f2012-01-30 04:29:24 +0000296 /* RFC5722, Section 4, amended by Errata ID : 3089
297 * When reassembling an IPv6 datagram, if
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000298 * one or more its constituent fragments is determined to be an
299 * overlapping fragment, the entire datagram (and any constituent
Eric Dumazet5de658f2012-01-30 04:29:24 +0000300 * fragments) MUST be silently discarded.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000303 /* Check for overlap with preceding fragment. */
304 if (prev &&
Shan Weif4642142010-11-05 01:56:34 +0000305 (FRAG6_CB(prev)->offset + prev->len) > offset)
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000306 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000308 /* Look for overlap with succeeding segment. */
309 if (next && FRAG6_CB(next)->offset < end)
310 goto discard_fq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 FRAG6_CB(skb)->offset = offset;
313
314 /* Insert this fragment in the chain of fragments. */
315 skb->next = next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000316 if (!next)
317 fq->q.fragments_tail = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (prev)
319 prev->next = skb;
320 else
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700321 fq->q.fragments = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Herbert Xuf61944e2007-10-15 01:28:47 -0700323 dev = skb->dev;
324 if (dev) {
325 fq->iif = dev->ifindex;
326 skb->dev = NULL;
327 }
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700328 fq->q.stamp = skb->tstamp;
329 fq->q.meat += skb->len;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000330 add_frag_mem_limit(&fq->q, skb->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* The first fragment.
333 * nhoffset is obtained from the first fragment, of course.
334 */
335 if (offset == 0) {
336 fq->nhoffset = nhoff;
Joe Perchesbc578a52008-03-28 16:35:27 -0700337 fq->q.last_in |= INET_FRAG_FIRST_IN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Herbert Xuf61944e2007-10-15 01:28:47 -0700339
Joe Perchesbc578a52008-03-28 16:35:27 -0700340 if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
341 fq->q.meat == fq->q.len)
Herbert Xuf61944e2007-10-15 01:28:47 -0700342 return ip6_frag_reasm(fq, prev, dev);
343
Jesper Dangaard Brouer3ef0eb02013-01-28 23:45:51 +0000344 inet_frag_lru_move(&fq->q);
Herbert Xuf61944e2007-10-15 01:28:47 -0700345 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Nicolas Dichtel70789d72010-09-03 05:13:05 +0000347discard_fq:
Amerigo Wangb836c992012-09-18 16:50:09 +0000348 inet_frag_kill(&fq->q, &ip6_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349err:
Eric Dumazetadf30902009-06-02 05:19:30 +0000350 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700351 IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 kfree_skb(skb);
Herbert Xuf61944e2007-10-15 01:28:47 -0700353 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356/*
357 * Check if this packet is complete.
358 * Returns NULL on failure by any reason, and pointer
359 * to current nexthdr field in reassembled frame.
360 *
361 * It is called with locked fq, and caller must check that
362 * queue is eligible for reassembly i.e. it is not COMPLETE,
363 * the last and the first frames arrived and all the bits are here.
364 */
Herbert Xuf61944e2007-10-15 01:28:47 -0700365static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 struct net_device *dev)
367{
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700368 struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700369 struct sk_buff *fp, *head = fq->q.fragments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int payload_len;
371 unsigned int nhoff;
Eric Dumazetec164392012-05-19 03:02:35 +0000372 int sum_truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Amerigo Wangb836c992012-09-18 16:50:09 +0000374 inet_frag_kill(&fq->q, &ip6_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Herbert Xuf61944e2007-10-15 01:28:47 -0700376 /* Make the one we just received the head. */
377 if (prev) {
378 head = prev->next;
379 fp = skb_clone(head, GFP_ATOMIC);
380
381 if (!fp)
382 goto out_oom;
383
384 fp->next = head->next;
Changli Gaod6bebca2010-06-29 04:39:37 +0000385 if (!fp->next)
386 fq->q.fragments_tail = fp;
Herbert Xuf61944e2007-10-15 01:28:47 -0700387 prev->next = fp;
388
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700389 skb_morph(head, fq->q.fragments);
390 head->next = fq->q.fragments->next;
Herbert Xuf61944e2007-10-15 01:28:47 -0700391
Eric Dumazet808db802012-04-24 10:17:59 +0000392 consume_skb(fq->q.fragments);
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700393 fq->q.fragments = head;
Herbert Xuf61944e2007-10-15 01:28:47 -0700394 }
395
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700396 WARN_ON(head == NULL);
397 WARN_ON(FRAG6_CB(head)->offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* Unfragmented part is taken from the first segment. */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700400 payload_len = ((head->data - skb_network_header(head)) -
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700401 sizeof(struct ipv6hdr) + fq->q.len -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700402 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (payload_len > IPV6_MAXPLEN)
404 goto out_oversize;
405
406 /* Head of list must not be cloned. */
407 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
408 goto out_oom;
409
410 /* If the first fragment is fragmented itself, we split
411 * it to two chunks: the first with data and paged part
412 * and the second, holding only fragments. */
David S. Miller21dc3302010-08-23 00:13:46 -0700413 if (skb_has_frag_list(head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct sk_buff *clone;
415 int i, plen = 0;
416
417 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
418 goto out_oom;
419 clone->next = head->next;
420 head->next = clone;
421 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
David S. Miller4d9092b2009-06-09 00:20:05 -0700422 skb_frag_list_init(head);
Eric Dumazet9e903e02011-10-18 21:00:24 +0000423 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
424 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 clone->len = clone->data_len = head->data_len - plen;
426 head->data_len -= clone->len;
427 head->len -= clone->len;
428 clone->csum = 0;
429 clone->ip_summed = head->ip_summed;
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000430 add_frag_mem_limit(&fq->q, clone->truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
433 /* We have to remove fragment header from datagram and to relocate
434 * header in order to calculate ICV correctly. */
435 nhoff = fq->nhoffset;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700436 skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900437 memmove(head->head + sizeof(struct frag_hdr), head->head,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 (head->data - head->head) - sizeof(struct frag_hdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700439 head->mac_header += sizeof(struct frag_hdr);
440 head->network_header += sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300442 skb_reset_transport_header(head);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700443 skb_push(head, head->data - skb_network_header(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Eric Dumazetec164392012-05-19 03:02:35 +0000445 sum_truesize = head->truesize;
446 for (fp = head->next; fp;) {
447 bool headstolen;
448 int delta;
449 struct sk_buff *next = fp->next;
450
451 sum_truesize += fp->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (head->ip_summed != fp->ip_summed)
453 head->ip_summed = CHECKSUM_NONE;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700454 else if (head->ip_summed == CHECKSUM_COMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 head->csum = csum_add(head->csum, fp->csum);
Eric Dumazetec164392012-05-19 03:02:35 +0000456
457 if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
458 kfree_skb_partial(fp, headstolen);
459 } else {
460 if (!skb_shinfo(head)->frag_list)
461 skb_shinfo(head)->frag_list = fp;
462 head->data_len += fp->len;
463 head->len += fp->len;
464 head->truesize += fp->truesize;
465 }
466 fp = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000468 sub_frag_mem_limit(&fq->q, sum_truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 head->next = NULL;
471 head->dev = dev;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700472 head->tstamp = fq->q.stamp;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700473 ipv6_hdr(head)->payload_len = htons(payload_len);
Patrick McHardy951dbc82006-01-06 23:02:34 -0800474 IP6CB(head)->nhoff = nhoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /* Yes, and fold redundant checksum back. 8) */
Patrick McHardy84fa7932006-08-29 16:44:56 -0700477 if (head->ip_summed == CHECKSUM_COMPLETE)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700478 head->csum = csum_partial(skb_network_header(head),
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300479 skb_network_header_len(head),
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700480 head->csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900482 rcu_read_lock();
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700483 IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900484 rcu_read_unlock();
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700485 fq->q.fragments = NULL;
Changli Gaod6bebca2010-06-29 04:39:37 +0000486 fq->q.fragments_tail = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 1;
488
489out_oversize:
Joe Perchese87cc472012-05-13 21:56:26 +0000490 net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto out_fail;
492out_oom:
Joe Perchese87cc472012-05-13 21:56:26 +0000493 net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494out_fail:
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900495 rcu_read_lock();
Jorge Boncompte [DTI2]2bad35b2009-03-18 23:26:11 -0700496 IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900497 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -1;
499}
500
Herbert Xue5bbef22007-10-15 12:50:28 -0700501static int ipv6_frag_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct frag_hdr *fhdr;
504 struct frag_queue *fq;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000505 const struct ipv6hdr *hdr = ipv6_hdr(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +0000506 struct net *net = dev_net(skb_dst(skb)->dev);
Amerigo Wang6b102862012-09-18 16:50:11 +0000507 int evicted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Eric Dumazetadf30902009-06-02 05:19:30 +0000509 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* Jumbo payload inhibits frag. header */
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700512 if (hdr->payload_len==0)
513 goto fail_hdr;
514
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700515 if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700516 sizeof(struct frag_hdr))))
517 goto fail_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700519 hdr = ipv6_hdr(skb);
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700520 fhdr = (struct frag_hdr *)skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 if (!(fhdr->frag_off & htons(0xFFF9))) {
523 /* It is not a fragmented frame */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700524 skb->transport_header += sizeof(struct frag_hdr);
Denis V. Lunev483a47d2008-10-08 11:09:27 -0700525 IP6_INC_STATS_BH(net,
Eric Dumazetadf30902009-06-02 05:19:30 +0000526 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700528 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return 1;
530 }
531
Amerigo Wang6b102862012-09-18 16:50:11 +0000532 evicted = inet_frag_evictor(&net->ipv6.frags, &ip6_frags, false);
533 if (evicted)
534 IP6_ADD_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
535 IPSTATS_MIB_REASMFAILS, evicted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Shan Wei95463772010-02-11 00:12:45 +0000537 fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr);
538 if (fq != NULL) {
Herbert Xuf61944e2007-10-15 01:28:47 -0700539 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700541 spin_lock(&fq->q.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Herbert Xuf61944e2007-10-15 01:28:47 -0700543 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700545 spin_unlock(&fq->q.lock);
Amerigo Wangb836c992012-09-18 16:50:09 +0000546 inet_frag_put(&fq->q, &ip6_frags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return ret;
548 }
549
Eric Dumazetadf30902009-06-02 05:19:30 +0000550 IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 kfree_skb(skb);
552 return -1;
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700553
554fail_hdr:
Eric Dumazetadf30902009-06-02 05:19:30 +0000555 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
Denis V. Lunev98b3377c2008-10-08 10:31:44 -0700556 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
557 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000560static const struct inet6_protocol frag_protocol =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 .handler = ipv6_frag_rcv,
563 .flags = INET6_PROTO_NOPOLICY,
564};
565
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800566#ifdef CONFIG_SYSCTL
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700567static struct ctl_table ip6_frags_ns_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800568 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800569 .procname = "ip6frag_high_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800570 .data = &init_net.ipv6.frags.high_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800571 .maxlen = sizeof(int),
572 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800573 .proc_handler = proc_dointvec
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800574 },
575 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800576 .procname = "ip6frag_low_thresh",
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800577 .data = &init_net.ipv6.frags.low_thresh,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800578 .maxlen = sizeof(int),
579 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800580 .proc_handler = proc_dointvec
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800581 },
582 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800583 .procname = "ip6frag_time",
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800584 .data = &init_net.ipv6.frags.timeout,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800585 .maxlen = sizeof(int),
586 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800587 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800588 },
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700589 { }
590};
591
592static struct ctl_table ip6_frags_ctl_table[] = {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800593 {
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800594 .procname = "ip6frag_secret_interval",
Pavel Emelyanov3b4bc4a2008-01-22 06:11:04 -0800595 .data = &ip6_frags.secret_interval,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800596 .maxlen = sizeof(int),
597 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800598 .proc_handler = proc_dointvec_jiffies,
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800599 },
600 { }
601};
Daniel Lezcano7d460db2008-01-18 23:52:35 -0800602
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000603static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800604{
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800605 struct ctl_table *table;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800606 struct ctl_table_header *hdr;
607
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700608 table = ip6_frags_ns_ctl_table;
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800609 if (!net_eq(net, &init_net)) {
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700610 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800611 if (table == NULL)
612 goto err_alloc;
613
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -0800614 table[0].data = &net->ipv6.frags.high_thresh;
615 table[1].data = &net->ipv6.frags.low_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800616 table[2].data = &net->ipv6.frags.timeout;
Eric W. Biederman464dc802012-11-16 03:02:59 +0000617
618 /* Don't export sysctls to unprivileged users */
619 if (net->user_ns != &init_user_ns)
620 table[0].procname = NULL;
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800621 }
622
Eric W. Biedermanec8f23c2012-04-19 13:44:49 +0000623 hdr = register_net_sysctl(net, "net/ipv6", table);
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800624 if (hdr == NULL)
625 goto err_reg;
626
627 net->ipv6.sysctl.frags_hdr = hdr;
628 return 0;
629
630err_reg:
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800631 if (!net_eq(net, &init_net))
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800632 kfree(table);
633err_alloc:
634 return -ENOMEM;
635}
636
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000637static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800638{
639 struct ctl_table *table;
640
641 table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
642 unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
Yang Hongyang3705e112009-12-18 20:25:13 -0800643 if (!net_eq(net, &init_net))
644 kfree(table);
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800645}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700646
647static struct ctl_table_header *ip6_ctl_header;
648
649static int ip6_frags_sysctl_register(void)
650{
Eric W. Biederman43444752012-04-19 13:22:55 +0000651 ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700652 ip6_frags_ctl_table);
653 return ip6_ctl_header == NULL ? -ENOMEM : 0;
654}
655
656static void ip6_frags_sysctl_unregister(void)
657{
658 unregister_net_sysctl_table(ip6_ctl_header);
659}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800660#else
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700661static inline int ip6_frags_ns_sysctl_register(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800662{
663 return 0;
664}
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800665
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700666static inline void ip6_frags_ns_sysctl_unregister(struct net *net)
Pavel Emelyanove4a2d5c2008-01-22 06:08:36 -0800667{
668}
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700669
670static inline int ip6_frags_sysctl_register(void)
671{
672 return 0;
673}
674
675static inline void ip6_frags_sysctl_unregister(void)
676{
677}
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800678#endif
679
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000680static int __net_init ipv6_frags_init_net(struct net *net)
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800681{
Shan Wei7c070aa2010-01-20 10:42:41 +0100682 net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
683 net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -0800684 net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800685
Pavel Emelyanove5a2bb82008-01-22 06:06:23 -0800686 inet_frags_init_net(&net->ipv6.frags);
687
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700688 return ip6_frags_ns_sysctl_register(net);
Daniel Lezcanoe71e0342008-01-10 02:56:03 -0800689}
690
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000691static void __net_exit ipv6_frags_exit_net(struct net *net)
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800692{
Pavel Emelyanov0a64b4b2008-05-19 13:51:29 -0700693 ip6_frags_ns_sysctl_unregister(net);
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800694 inet_frags_exit_net(&net->ipv6.frags, &ip6_frags);
695}
696
697static struct pernet_operations ip6_frags_ops = {
698 .init = ipv6_frags_init_net,
699 .exit = ipv6_frags_exit_net,
700};
701
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800702int __init ipv6_frag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800704 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800706 ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
707 if (ret)
708 goto out;
Daniel Lezcanoe71e0342008-01-10 02:56:03 -0800709
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700710 ret = ip6_frags_sysctl_register();
711 if (ret)
712 goto err_sysctl;
713
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700714 ret = register_pernet_subsys(&ip6_frags_ops);
715 if (ret)
716 goto err_pernet;
Pavel Emelyanov8d8354d2008-01-22 05:58:31 -0800717
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700718 ip6_frags.hashfn = ip6_hashfn;
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700719 ip6_frags.constructor = ip6_frag_init;
Pavel Emelyanovc9547702007-10-17 19:48:26 -0700720 ip6_frags.destructor = NULL;
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700721 ip6_frags.skb_free = NULL;
722 ip6_frags.qsize = sizeof(struct frag_queue);
Pavel Emelyanovabd65232007-10-17 19:47:21 -0700723 ip6_frags.match = ip6_frag_match;
Pavel Emelyanove521db92007-10-17 19:45:23 -0700724 ip6_frags.frag_expire = ip6_frag_expire;
Pavel Emelyanov3b4bc4a2008-01-22 06:11:04 -0800725 ip6_frags.secret_interval = 10 * 60 * HZ;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700726 inet_frags_init(&ip6_frags);
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800727out:
728 return ret;
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700729
730err_pernet:
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700731 ip6_frags_sysctl_unregister();
732err_sysctl:
Pavel Emelyanov0002c632008-05-19 13:52:28 -0700733 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
734 goto out;
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800735}
736
737void ipv6_frag_exit(void)
738{
739 inet_frags_fini(&ip6_frags);
Pavel Emelyanov7d291eb2008-05-19 13:53:02 -0700740 ip6_frags_sysctl_unregister();
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800741 unregister_pernet_subsys(&ip6_frags_ops);
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800742 inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}