xref: /freebsd/sys/netinet6/frag6.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
1 /*	$FreeBSD$	*/
2 /*	$KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "opt_random_ip_id.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/domain.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/errno.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/syslog.h>
46 
47 #include <net/if.h>
48 #include <net/route.h>
49 
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <netinet/ip6.h>
53 #include <netinet6/ip6_var.h>
54 #include <netinet/icmp6.h>
55 #include <netinet/in_systm.h>	/* for ECN definitions */
56 #include <netinet/ip.h>		/* for ECN definitions */
57 
58 #include <net/net_osdep.h>
59 
60 /*
61  * Define it to get a correct behavior on per-interface statistics.
62  * You will need to perform an extra routing table lookup, per fragment,
63  * to do it.  This may, or may not be, a performance hit.
64  */
65 #define IN6_IFSTAT_STRICT
66 
67 static void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *));
68 static void frag6_deq __P((struct ip6asfrag *));
69 static void frag6_insque __P((struct ip6q *, struct ip6q *));
70 static void frag6_remque __P((struct ip6q *));
71 static void frag6_freef __P((struct ip6q *));
72 
73 static struct mtx ip6qlock;
74 /*
75  * These fields all protected by ip6qlock.
76  */
77 static u_int frag6_nfragpackets;
78 static u_int frag6_nfrags;
79 static struct	ip6q ip6q;	/* ip6 reassemble queue */
80 
81 #define	IP6Q_LOCK_INIT()	mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF);
82 #define	IP6Q_LOCK()		mtx_lock(&ip6qlock)
83 #define	IP6Q_TRYLOCK()		mtx_trylock(&ip6qlock)
84 #define	IP6Q_LOCK_ASSERT()	mtx_assert(&ip6qlock, MA_OWNED)
85 #define	IP6Q_UNLOCK()		mtx_unlock(&ip6qlock)
86 
87 static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
88 
89 /*
90  * Initialise reassembly queue and fragment identifier.
91  */
92 void
93 frag6_init()
94 {
95 
96 	ip6_maxfragpackets = nmbclusters / 4;
97 	ip6_maxfrags = nmbclusters / 4;
98 
99 	IP6Q_LOCK_INIT();
100 
101 #ifndef RANDOM_IP_ID
102 	ip6_id = arc4random();
103 #endif
104 	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
105 }
106 
107 /*
108  * In RFC2460, fragment and reassembly rule do not agree with each other,
109  * in terms of next header field handling in fragment header.
110  * While the sender will use the same value for all of the fragmented packets,
111  * receiver is suggested not to check the consistency.
112  *
113  * fragment rule (p20):
114  *	(2) A Fragment header containing:
115  *	The Next Header value that identifies the first header of
116  *	the Fragmentable Part of the original packet.
117  *		-> next header field is same for all fragments
118  *
119  * reassembly rule (p21):
120  *	The Next Header field of the last header of the Unfragmentable
121  *	Part is obtained from the Next Header field of the first
122  *	fragment's Fragment header.
123  *		-> should grab it from the first fragment only
124  *
125  * The following note also contradicts with fragment rule - noone is going to
126  * send different fragment with different next header field.
127  *
128  * additional note (p22):
129  *	The Next Header values in the Fragment headers of different
130  *	fragments of the same original packet may differ.  Only the value
131  *	from the Offset zero fragment packet is used for reassembly.
132  *		-> should grab it from the first fragment only
133  *
134  * There is no explicit reason given in the RFC.  Historical reason maybe?
135  */
136 /*
137  * Fragment input
138  */
139 int
140 frag6_input(mp, offp, proto)
141 	struct mbuf **mp;
142 	int *offp, proto;
143 {
144 	struct mbuf *m = *mp, *t;
145 	struct ip6_hdr *ip6;
146 	struct ip6_frag *ip6f;
147 	struct ip6q *q6;
148 	struct ip6asfrag *af6, *ip6af, *af6dwn;
149 #ifdef IN6_IFSTAT_STRICT
150 	struct in6_ifaddr *ia;
151 #endif
152 	int offset = *offp, nxt, i, next;
153 	int first_frag = 0;
154 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
155 	struct ifnet *dstifp;
156 	u_int8_t ecn, ecn0;
157 
158 	ip6 = mtod(m, struct ip6_hdr *);
159 #ifndef PULLDOWN_TEST
160 	IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
161 	ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
162 #else
163 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
164 	if (ip6f == NULL)
165 		return (IPPROTO_DONE);
166 #endif
167 
168 	dstifp = NULL;
169 #ifdef IN6_IFSTAT_STRICT
170 	/* find the destination interface of the packet. */
171 	if ((ia = ip6_getdstifaddr(m)) != NULL)
172 		dstifp = ia->ia_ifp;
173 #else
174 	/* we are violating the spec, this is not the destination interface */
175 	if ((m->m_flags & M_PKTHDR) != 0)
176 		dstifp = m->m_pkthdr.rcvif;
177 #endif
178 
179 	/* jumbo payload can't contain a fragment header */
180 	if (ip6->ip6_plen == 0) {
181 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
182 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
183 		return IPPROTO_DONE;
184 	}
185 
186 	/*
187 	 * check whether fragment packet's fragment length is
188 	 * multiple of 8 octets.
189 	 * sizeof(struct ip6_frag) == 8
190 	 * sizeof(struct ip6_hdr) = 40
191 	 */
192 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
193 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
194 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
195 		    offsetof(struct ip6_hdr, ip6_plen));
196 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
197 		return IPPROTO_DONE;
198 	}
199 
200 	ip6stat.ip6s_fragments++;
201 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
202 
203 	/* offset now points to data portion */
204 	offset += sizeof(struct ip6_frag);
205 
206 	IP6Q_LOCK();
207 
208 	/*
209 	 * Enforce upper bound on number of fragments.
210 	 * If maxfrag is 0, never accept fragments.
211 	 * If maxfrag is -1, accept all fragments without limitation.
212 	 */
213 	if (ip6_maxfrags < 0)
214 		;
215 	else if (frag6_nfrags >= (u_int)ip6_maxfrags)
216 		goto dropfrag;
217 
218 	for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
219 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
220 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
221 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
222 			break;
223 
224 	if (q6 == &ip6q) {
225 		/*
226 		 * the first fragment to arrive, create a reassembly queue.
227 		 */
228 		first_frag = 1;
229 
230 		/*
231 		 * Enforce upper bound on number of fragmented packets
232 		 * for which we attempt reassembly;
233 		 * If maxfragpackets is 0, never accept fragments.
234 		 * If maxfragpackets is -1, accept all fragments without
235 		 * limitation.
236 		 */
237 		if (ip6_maxfragpackets < 0)
238 			;
239 		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
240 			goto dropfrag;
241 		frag6_nfragpackets++;
242 		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
243 		    M_NOWAIT);
244 		if (q6 == NULL)
245 			goto dropfrag;
246 		bzero(q6, sizeof(*q6));
247 
248 		frag6_insque(q6, &ip6q);
249 
250 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
251 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
252 #ifdef notyet
253 		q6->ip6q_nxtp	= (u_char *)nxtp;
254 #endif
255 		q6->ip6q_ident	= ip6f->ip6f_ident;
256 		q6->ip6q_arrive = 0; /* Is it used anywhere? */
257 		q6->ip6q_ttl 	= IPV6_FRAGTTL;
258 		q6->ip6q_src	= ip6->ip6_src;
259 		q6->ip6q_dst	= ip6->ip6_dst;
260 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
261 
262 		q6->ip6q_nfrag = 0;
263 	}
264 
265 	/*
266 	 * If it's the 1st fragment, record the length of the
267 	 * unfragmentable part and the next header of the fragment header.
268 	 */
269 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
270 	if (fragoff == 0) {
271 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
272 		    sizeof(struct ip6_frag);
273 		q6->ip6q_nxt = ip6f->ip6f_nxt;
274 	}
275 
276 	/*
277 	 * Check that the reassembled packet would not exceed 65535 bytes
278 	 * in size.
279 	 * If it would exceed, discard the fragment and return an ICMP error.
280 	 */
281 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
282 	if (q6->ip6q_unfrglen >= 0) {
283 		/* The 1st fragment has already arrived. */
284 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
285 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
286 			    offset - sizeof(struct ip6_frag) +
287 			    offsetof(struct ip6_frag, ip6f_offlg));
288 			IP6Q_UNLOCK();
289 			return (IPPROTO_DONE);
290 		}
291 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
292 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
293 		    offset - sizeof(struct ip6_frag) +
294 		    offsetof(struct ip6_frag, ip6f_offlg));
295 		IP6Q_UNLOCK();
296 		return (IPPROTO_DONE);
297 	}
298 	/*
299 	 * If it's the first fragment, do the above check for each
300 	 * fragment already stored in the reassembly queue.
301 	 */
302 	if (fragoff == 0) {
303 		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
304 		     af6 = af6dwn) {
305 			af6dwn = af6->ip6af_down;
306 
307 			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
308 			    IPV6_MAXPACKET) {
309 				struct mbuf *merr = IP6_REASS_MBUF(af6);
310 				struct ip6_hdr *ip6err;
311 				int erroff = af6->ip6af_offset;
312 
313 				/* dequeue the fragment. */
314 				frag6_deq(af6);
315 				free(af6, M_FTABLE);
316 
317 				/* adjust pointer. */
318 				ip6err = mtod(merr, struct ip6_hdr *);
319 
320 				/*
321 				 * Restore source and destination addresses
322 				 * in the erroneous IPv6 header.
323 				 */
324 				ip6err->ip6_src = q6->ip6q_src;
325 				ip6err->ip6_dst = q6->ip6q_dst;
326 
327 				icmp6_error(merr, ICMP6_PARAM_PROB,
328 				    ICMP6_PARAMPROB_HEADER,
329 				    erroff - sizeof(struct ip6_frag) +
330 				    offsetof(struct ip6_frag, ip6f_offlg));
331 			}
332 		}
333 	}
334 
335 	ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
336 	    M_NOWAIT);
337 	if (ip6af == NULL)
338 		goto dropfrag;
339 	bzero(ip6af, sizeof(*ip6af));
340 	ip6af->ip6af_head = ip6->ip6_flow;
341 	ip6af->ip6af_len = ip6->ip6_plen;
342 	ip6af->ip6af_nxt = ip6->ip6_nxt;
343 	ip6af->ip6af_hlim = ip6->ip6_hlim;
344 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
345 	ip6af->ip6af_off = fragoff;
346 	ip6af->ip6af_frglen = frgpartlen;
347 	ip6af->ip6af_offset = offset;
348 	IP6_REASS_MBUF(ip6af) = m;
349 
350 	if (first_frag) {
351 		af6 = (struct ip6asfrag *)q6;
352 		goto insert;
353 	}
354 
355 	/*
356 	 * Handle ECN by comparing this segment with the first one;
357 	 * if CE is set, do not lose CE.
358 	 * drop if CE and not-ECT are mixed for the same packet.
359 	 */
360 	ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
361 	ecn0 = (ntohl(q6->ip6q_down->ip6af_head) >> 20) & IPTOS_ECN_MASK;
362 	if (ecn == IPTOS_ECN_CE) {
363 		if (ecn0 == IPTOS_ECN_NOTECT) {
364 			free(ip6af, M_FTABLE);
365 			goto dropfrag;
366 		}
367 		if (ecn0 != IPTOS_ECN_CE)
368 			q6->ip6q_down->ip6af_head |= htonl(IPTOS_ECN_CE << 20);
369 	}
370 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
371 		free(ip6af, M_FTABLE);
372 		goto dropfrag;
373 	}
374 
375 	/*
376 	 * Find a segment which begins after this one does.
377 	 */
378 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
379 	     af6 = af6->ip6af_down)
380 		if (af6->ip6af_off > ip6af->ip6af_off)
381 			break;
382 
383 #if 0
384 	/*
385 	 * If there is a preceding segment, it may provide some of
386 	 * our data already.  If so, drop the data from the incoming
387 	 * segment.  If it provides all of our data, drop us.
388 	 */
389 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
390 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
391 			- ip6af->ip6af_off;
392 		if (i > 0) {
393 			if (i >= ip6af->ip6af_frglen)
394 				goto dropfrag;
395 			m_adj(IP6_REASS_MBUF(ip6af), i);
396 			ip6af->ip6af_off += i;
397 			ip6af->ip6af_frglen -= i;
398 		}
399 	}
400 
401 	/*
402 	 * While we overlap succeeding segments trim them or,
403 	 * if they are completely covered, dequeue them.
404 	 */
405 	while (af6 != (struct ip6asfrag *)q6 &&
406 	       ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
407 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
408 		if (i < af6->ip6af_frglen) {
409 			af6->ip6af_frglen -= i;
410 			af6->ip6af_off += i;
411 			m_adj(IP6_REASS_MBUF(af6), i);
412 			break;
413 		}
414 		af6 = af6->ip6af_down;
415 		m_freem(IP6_REASS_MBUF(af6->ip6af_up));
416 		frag6_deq(af6->ip6af_up);
417 	}
418 #else
419 	/*
420 	 * If the incoming framgent overlaps some existing fragments in
421 	 * the reassembly queue, drop it, since it is dangerous to override
422 	 * existing fragments from a security point of view.
423 	 * We don't know which fragment is the bad guy - here we trust
424 	 * fragment that came in earlier, with no real reason.
425 	 */
426 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
427 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
428 			- ip6af->ip6af_off;
429 		if (i > 0) {
430 #if 0				/* suppress the noisy log */
431 			log(LOG_ERR, "%d bytes of a fragment from %s "
432 			    "overlaps the previous fragment\n",
433 			    i, ip6_sprintf(&q6->ip6q_src));
434 #endif
435 			free(ip6af, M_FTABLE);
436 			goto dropfrag;
437 		}
438 	}
439 	if (af6 != (struct ip6asfrag *)q6) {
440 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
441 		if (i > 0) {
442 #if 0				/* suppress the noisy log */
443 			log(LOG_ERR, "%d bytes of a fragment from %s "
444 			    "overlaps the succeeding fragment",
445 			    i, ip6_sprintf(&q6->ip6q_src));
446 #endif
447 			free(ip6af, M_FTABLE);
448 			goto dropfrag;
449 		}
450 	}
451 #endif
452 
453 insert:
454 
455 	/*
456 	 * Stick new segment in its place;
457 	 * check for complete reassembly.
458 	 * Move to front of packet queue, as we are
459 	 * the most recently active fragmented packet.
460 	 */
461 	frag6_enq(ip6af, af6->ip6af_up);
462 	frag6_nfrags++;
463 	q6->ip6q_nfrag++;
464 #if 0 /* xxx */
465 	if (q6 != ip6q.ip6q_next) {
466 		frag6_remque(q6);
467 		frag6_insque(q6, &ip6q);
468 	}
469 #endif
470 	next = 0;
471 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
472 	     af6 = af6->ip6af_down) {
473 		if (af6->ip6af_off != next) {
474 			IP6Q_UNLOCK();
475 			return IPPROTO_DONE;
476 		}
477 		next += af6->ip6af_frglen;
478 	}
479 	if (af6->ip6af_up->ip6af_mff) {
480 		IP6Q_UNLOCK();
481 		return IPPROTO_DONE;
482 	}
483 
484 	/*
485 	 * Reassembly is complete; concatenate fragments.
486 	 */
487 	ip6af = q6->ip6q_down;
488 	t = m = IP6_REASS_MBUF(ip6af);
489 	af6 = ip6af->ip6af_down;
490 	frag6_deq(ip6af);
491 	while (af6 != (struct ip6asfrag *)q6) {
492 		af6dwn = af6->ip6af_down;
493 		frag6_deq(af6);
494 		while (t->m_next)
495 			t = t->m_next;
496 		t->m_next = IP6_REASS_MBUF(af6);
497 		m_adj(t->m_next, af6->ip6af_offset);
498 		free(af6, M_FTABLE);
499 		af6 = af6dwn;
500 	}
501 
502 	/* adjust offset to point where the original next header starts */
503 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
504 	free(ip6af, M_FTABLE);
505 	ip6 = mtod(m, struct ip6_hdr *);
506 	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
507 	ip6->ip6_src = q6->ip6q_src;
508 	ip6->ip6_dst = q6->ip6q_dst;
509 	nxt = q6->ip6q_nxt;
510 #ifdef notyet
511 	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
512 #endif
513 
514 	/*
515 	 * Delete frag6 header with as a few cost as possible.
516 	 */
517 	if (offset < m->m_len) {
518 		ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
519 			offset);
520 		m->m_data += sizeof(struct ip6_frag);
521 		m->m_len -= sizeof(struct ip6_frag);
522 	} else {
523 		/* this comes with no copy if the boundary is on cluster */
524 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
525 			frag6_remque(q6);
526 			frag6_nfrags -= q6->ip6q_nfrag;
527 			free(q6, M_FTABLE);
528 			frag6_nfragpackets--;
529 			goto dropfrag;
530 		}
531 		m_adj(t, sizeof(struct ip6_frag));
532 		m_cat(m, t);
533 	}
534 
535 	/*
536 	 * Store NXT to the original.
537 	 */
538 	{
539 		char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
540 		*prvnxtp = nxt;
541 	}
542 
543 	frag6_remque(q6);
544 	frag6_nfrags -= q6->ip6q_nfrag;
545 	free(q6, M_FTABLE);
546 	frag6_nfragpackets--;
547 
548 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
549 		int plen = 0;
550 		for (t = m; t; t = t->m_next)
551 			plen += t->m_len;
552 		m->m_pkthdr.len = plen;
553 	}
554 
555 	ip6stat.ip6s_reassembled++;
556 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
557 
558 	/*
559 	 * Tell launch routine the next header
560 	 */
561 
562 	*mp = m;
563 	*offp = offset;
564 
565 	IP6Q_UNLOCK();
566 	return nxt;
567 
568  dropfrag:
569 	IP6Q_UNLOCK();
570 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
571 	ip6stat.ip6s_fragdropped++;
572 	m_freem(m);
573 	return IPPROTO_DONE;
574 }
575 
576 /*
577  * Free a fragment reassembly header and all
578  * associated datagrams.
579  */
580 void
581 frag6_freef(q6)
582 	struct ip6q *q6;
583 {
584 	struct ip6asfrag *af6, *down6;
585 
586 	IP6Q_LOCK_ASSERT();
587 
588 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
589 	     af6 = down6) {
590 		struct mbuf *m = IP6_REASS_MBUF(af6);
591 
592 		down6 = af6->ip6af_down;
593 		frag6_deq(af6);
594 
595 		/*
596 		 * Return ICMP time exceeded error for the 1st fragment.
597 		 * Just free other fragments.
598 		 */
599 		if (af6->ip6af_off == 0) {
600 			struct ip6_hdr *ip6;
601 
602 			/* adjust pointer */
603 			ip6 = mtod(m, struct ip6_hdr *);
604 
605 			/* restore source and destination addresses */
606 			ip6->ip6_src = q6->ip6q_src;
607 			ip6->ip6_dst = q6->ip6q_dst;
608 
609 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
610 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
611 		} else
612 			m_freem(m);
613 		free(af6, M_FTABLE);
614 	}
615 	frag6_remque(q6);
616 	frag6_nfrags -= q6->ip6q_nfrag;
617 	free(q6, M_FTABLE);
618 	frag6_nfragpackets--;
619 }
620 
621 /*
622  * Put an ip fragment on a reassembly chain.
623  * Like insque, but pointers in middle of structure.
624  */
625 void
626 frag6_enq(af6, up6)
627 	struct ip6asfrag *af6, *up6;
628 {
629 
630 	IP6Q_LOCK_ASSERT();
631 
632 	af6->ip6af_up = up6;
633 	af6->ip6af_down = up6->ip6af_down;
634 	up6->ip6af_down->ip6af_up = af6;
635 	up6->ip6af_down = af6;
636 }
637 
638 /*
639  * To frag6_enq as remque is to insque.
640  */
641 void
642 frag6_deq(af6)
643 	struct ip6asfrag *af6;
644 {
645 
646 	IP6Q_LOCK_ASSERT();
647 
648 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
649 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
650 }
651 
652 void
653 frag6_insque(new, old)
654 	struct ip6q *new, *old;
655 {
656 
657 	IP6Q_LOCK_ASSERT();
658 
659 	new->ip6q_prev = old;
660 	new->ip6q_next = old->ip6q_next;
661 	old->ip6q_next->ip6q_prev= new;
662 	old->ip6q_next = new;
663 }
664 
665 void
666 frag6_remque(p6)
667 	struct ip6q *p6;
668 {
669 
670 	IP6Q_LOCK_ASSERT();
671 
672 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
673 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
674 }
675 
676 /*
677  * IPv6 reassembling timer processing;
678  * if a timer expires on a reassembly
679  * queue, discard it.
680  */
681 void
682 frag6_slowtimo()
683 {
684 	struct ip6q *q6;
685 	int s = splnet();
686 
687 	IP6Q_LOCK();
688 	q6 = ip6q.ip6q_next;
689 	if (q6)
690 		while (q6 != &ip6q) {
691 			--q6->ip6q_ttl;
692 			q6 = q6->ip6q_next;
693 			if (q6->ip6q_prev->ip6q_ttl == 0) {
694 				ip6stat.ip6s_fragtimeout++;
695 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
696 				frag6_freef(q6->ip6q_prev);
697 			}
698 		}
699 	/*
700 	 * If we are over the maximum number of fragments
701 	 * (due to the limit being lowered), drain off
702 	 * enough to get down to the new limit.
703 	 */
704 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
705 	    ip6q.ip6q_prev) {
706 		ip6stat.ip6s_fragoverflow++;
707 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
708 		frag6_freef(ip6q.ip6q_prev);
709 	}
710 	IP6Q_UNLOCK();
711 
712 #if 0
713 	/*
714 	 * Routing changes might produce a better route than we last used;
715 	 * make sure we notice eventually, even if forwarding only for one
716 	 * destination and the cache is never replaced.
717 	 */
718 	if (ip6_forward_rt.ro_rt) {
719 		RTFREE(ip6_forward_rt.ro_rt);
720 		ip6_forward_rt.ro_rt = 0;
721 	}
722 	if (ipsrcchk_rt.ro_rt) {
723 		RTFREE(ipsrcchk_rt.ro_rt);
724 		ipsrcchk_rt.ro_rt = 0;
725 	}
726 #endif
727 
728 	splx(s);
729 }
730 
731 /*
732  * Drain off all datagram fragments.
733  */
734 void
735 frag6_drain()
736 {
737 
738 	if (IP6Q_TRYLOCK() == 0)
739 		return;
740 	while (ip6q.ip6q_next != &ip6q) {
741 		ip6stat.ip6s_fragdropped++;
742 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
743 		frag6_freef(ip6q.ip6q_next);
744 	}
745 	IP6Q_UNLOCK();
746 }
747