xref: /openbsd-src/sys/netinet6/frag6.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: frag6.c,v 1.42 2012/01/24 19:08:46 bluhm Exp $	*/
2 /*	$KAME: frag6.c,v 1.40 2002/05/27 21:40:31 itojun 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 <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/domain.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
40 #include <sys/errno.h>
41 #include <sys/time.h>
42 #include <sys/kernel.h>
43 #include <sys/syslog.h>
44 
45 #include <net/if.h>
46 #include <net/route.h>
47 
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/ip6.h>
51 #include <netinet6/ip6_var.h>
52 #include <netinet/icmp6.h>
53 #include <netinet/in_systm.h>	/* for ECN definitions */
54 #include <netinet/ip.h>		/* for ECN definitions */
55 
56 #include <dev/rndvar.h>
57 
58 /*
59  * Define it to get a correct behavior on per-interface statistics.
60  * You will need to perform an extra routing table lookup, per fragment,
61  * to do it.  This may, or may not be, a performance hit.
62  */
63 #define IN6_IFSTAT_STRICT
64 
65 void frag6_freef(struct ip6q *);
66 
67 static int ip6q_locked;
68 u_int frag6_nfragpackets;
69 u_int frag6_nfrags;
70 TAILQ_HEAD(ip6q_head, ip6q) frag6_queue;	/* ip6 reassemble queue */
71 
72 static __inline int ip6q_lock_try(void);
73 static __inline void ip6q_unlock(void);
74 
75 static __inline int
76 ip6q_lock_try()
77 {
78 	int s;
79 
80 	/* Use splvm() due to mbuf allocation. */
81 	s = splvm();
82 	if (ip6q_locked) {
83 		splx(s);
84 		return (0);
85 	}
86 	ip6q_locked = 1;
87 	splx(s);
88 	return (1);
89 }
90 
91 static __inline void
92 ip6q_unlock()
93 {
94 	int s;
95 
96 	s = splvm();
97 	ip6q_locked = 0;
98 	splx(s);
99 }
100 
101 #ifdef DIAGNOSTIC
102 #define	IP6Q_LOCK()							\
103 do {									\
104 	if (ip6q_lock_try() == 0) {					\
105 		printf("%s:%d: ip6q already locked\n", __FILE__, __LINE__); \
106 		panic("ip6q_lock");					\
107 	}								\
108 } while (0)
109 #define	IP6Q_LOCK_CHECK()						\
110 do {									\
111 	if (ip6q_locked == 0) {						\
112 		printf("%s:%d: ip6q lock not held\n", __FILE__, __LINE__); \
113 		panic("ip6q lock check");				\
114 	}								\
115 } while (0)
116 #else
117 #define	IP6Q_LOCK()		(void) ip6q_lock_try()
118 #define	IP6Q_LOCK_CHECK()	/* nothing */
119 #endif
120 
121 #define	IP6Q_UNLOCK()		ip6q_unlock()
122 
123 /*
124  * Initialise reassembly queue and fragment identifier.
125  */
126 void
127 frag6_init(void)
128 {
129 
130 	TAILQ_INIT(&frag6_queue);
131 }
132 
133 /*
134  * In RFC2460, fragment and reassembly rule do not agree with each other,
135  * in terms of next header field handling in fragment header.
136  * While the sender will use the same value for all of the fragmented packets,
137  * receiver is suggested not to check the consistency.
138  *
139  * fragment rule (p20):
140  *	(2) A Fragment header containing:
141  *	The Next Header value that identifies the first header of
142  *	the Fragmentable Part of the original packet.
143  *		-> next header field is same for all fragments
144  *
145  * reassembly rule (p21):
146  *	The Next Header field of the last header of the Unfragmentable
147  *	Part is obtained from the Next Header field of the first
148  *	fragment's Fragment header.
149  *		-> should grab it from the first fragment only
150  *
151  * The following note also contradicts with fragment rule - noone is going to
152  * send different fragment with different next header field.
153  *
154  * additional note (p22):
155  *	The Next Header values in the Fragment headers of different
156  *	fragments of the same original packet may differ.  Only the value
157  *	from the Offset zero fragment packet is used for reassembly.
158  *		-> should grab it from the first fragment only
159  *
160  * There is no explicit reason given in the RFC.  Historical reason maybe?
161  */
162 /*
163  * Fragment input
164  */
165 int
166 frag6_input(struct mbuf **mp, int *offp, int proto)
167 {
168 	struct mbuf *m = *mp, *t;
169 	struct ip6_hdr *ip6;
170 	struct ip6_frag *ip6f;
171 	struct ip6q *q6;
172 	struct ip6asfrag *af6, *ip6af, *naf6, *paf6;
173 	int offset = *offp, nxt, i, next;
174 	int first_frag = 0;
175 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
176 	struct ifnet *dstifp;
177 #ifdef IN6_IFSTAT_STRICT
178 	struct route_in6 ro;
179 	struct sockaddr_in6 *dst;
180 #endif
181 	u_int8_t ecn, ecn0;
182 
183 	ip6 = mtod(m, struct ip6_hdr *);
184 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
185 	if (ip6f == NULL)
186 		return IPPROTO_DONE;
187 
188 	dstifp = NULL;
189 #ifdef IN6_IFSTAT_STRICT
190 	/* find the destination interface of the packet. */
191 	bzero(&ro, sizeof(ro));
192 	dst = (struct sockaddr_in6 *)&ro.ro_dst;
193 	dst->sin6_family = AF_INET6;
194 	dst->sin6_len = sizeof(struct sockaddr_in6);
195 	dst->sin6_addr = ip6->ip6_dst;
196 
197 	rtalloc_mpath((struct route *)&ro, &ip6->ip6_src.s6_addr32[0]);
198 
199 	if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL)
200 		dstifp = ((struct in6_ifaddr *)ro.ro_rt->rt_ifa)->ia_ifp;
201 	if (ro.ro_rt != NULL) {
202 		RTFREE(ro.ro_rt);
203 		ro.ro_rt = NULL;
204 	}
205 #else
206 	/* we are violating the spec, this is not the destination interface */
207 	if ((m->m_flags & M_PKTHDR) != 0)
208 		dstifp = m->m_pkthdr.rcvif;
209 #endif
210 
211 	/* jumbo payload can't contain a fragment header */
212 	if (ip6->ip6_plen == 0) {
213 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
214 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
215 		return IPPROTO_DONE;
216 	}
217 
218 	/*
219 	 * check whether fragment packet's fragment length is
220 	 * multiple of 8 octets.
221 	 * sizeof(struct ip6_frag) == 8
222 	 * sizeof(struct ip6_hdr) = 40
223 	 */
224 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
225 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
226 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
227 		    offsetof(struct ip6_hdr, ip6_plen));
228 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
229 		return IPPROTO_DONE;
230 	}
231 
232 	ip6stat.ip6s_fragments++;
233 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
234 
235 	/* offset now points to data portion */
236 	offset += sizeof(struct ip6_frag);
237 
238 	/*
239 	 * draft-gont-6man-ipv6-atomic-fragments-00:  A host that receives an
240 	 * IPv6 packet which includes a Fragment Header with the "Fragment
241 	 * Offset" equal to 0 and the "M" bit equal to 0 MUST process such
242 	 * packet in isolation from any other packets/fragments.
243 	 */
244 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
245 	if (fragoff == 0 && !(ip6f->ip6f_offlg & IP6F_MORE_FRAG)) {
246 		ip6stat.ip6s_reassembled++;
247 		in6_ifstat_inc(dstifp, ifs6_reass_ok);
248 		*offp = offset;
249 		return ip6f->ip6f_nxt;
250 	}
251 
252 	IP6Q_LOCK();
253 
254 	/*
255 	 * Enforce upper bound on number of fragments.
256 	 * If maxfrag is 0, never accept fragments.
257 	 * If maxfrag is -1, accept all fragments without limitation.
258 	 */
259 	if (ip6_maxfrags < 0)
260 		;
261 	else if (frag6_nfrags >= (u_int)ip6_maxfrags)
262 		goto dropfrag;
263 
264 	TAILQ_FOREACH(q6, &frag6_queue, ip6q_queue)
265 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
266 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
267 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
268 			break;
269 
270 	if (q6 == TAILQ_END(&frag6_queue)) {
271 		/*
272 		 * the first fragment to arrive, create a reassembly queue.
273 		 */
274 		first_frag = 1;
275 
276 		/*
277 		 * Enforce upper bound on number of fragmented packets
278 		 * for which we attempt reassembly;
279 		 * If maxfragpackets is 0, never accept fragments.
280 		 * If maxfragpackets is -1, accept all fragments without
281 		 * limitation.
282 		 */
283 		if (ip6_maxfragpackets < 0)
284 			;
285 		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
286 			goto dropfrag;
287 		frag6_nfragpackets++;
288 		q6 = malloc(sizeof(*q6), M_FTABLE, M_DONTWAIT | M_ZERO);
289 		if (q6 == NULL)
290 			goto dropfrag;
291 
292 		TAILQ_INSERT_HEAD(&frag6_queue, q6, ip6q_queue);
293 
294 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
295 		LIST_INIT(&q6->ip6q_asfrag);
296 		q6->ip6q_ident	= ip6f->ip6f_ident;
297 		q6->ip6q_ttl	= IPV6_FRAGTTL;
298 		q6->ip6q_src	= ip6->ip6_src;
299 		q6->ip6q_dst	= ip6->ip6_dst;
300 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
301 		q6->ip6q_nfrag = 0;
302 	}
303 
304 	/*
305 	 * If it's the 1st fragment, record the length of the
306 	 * unfragmentable part and the next header of the fragment header.
307 	 */
308 	if (fragoff == 0) {
309 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
310 		    sizeof(struct ip6_frag);
311 		q6->ip6q_nxt = ip6f->ip6f_nxt;
312 	}
313 
314 	/*
315 	 * Check that the reassembled packet would not exceed 65535 bytes
316 	 * in size.
317 	 * If it would exceed, discard the fragment and return an ICMP error.
318 	 */
319 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
320 	if (q6->ip6q_unfrglen >= 0) {
321 		/* The 1st fragment has already arrived. */
322 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
323 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
324 			    offset - sizeof(struct ip6_frag) +
325 			    offsetof(struct ip6_frag, ip6f_offlg));
326 			IP6Q_UNLOCK();
327 			return (IPPROTO_DONE);
328 		}
329 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
330 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
331 			    offset - sizeof(struct ip6_frag) +
332 				offsetof(struct ip6_frag, ip6f_offlg));
333 		IP6Q_UNLOCK();
334 		return (IPPROTO_DONE);
335 	}
336 	/*
337 	 * If it's the first fragment, do the above check for each
338 	 * fragment already stored in the reassembly queue.
339 	 */
340 	if (fragoff == 0) {
341 		LIST_FOREACH_SAFE(af6, &q6->ip6q_asfrag, ip6af_list, naf6) {
342 			if (q6->ip6q_unfrglen + af6->ip6af_off +
343 			    af6->ip6af_frglen > IPV6_MAXPACKET) {
344 				struct mbuf *merr = IP6_REASS_MBUF(af6);
345 				struct ip6_hdr *ip6err;
346 				int erroff = af6->ip6af_offset;
347 
348 				/* dequeue the fragment. */
349 				LIST_REMOVE(af6, ip6af_list);
350 				free(af6, M_FTABLE);
351 
352 				/* adjust pointer. */
353 				ip6err = mtod(merr, struct ip6_hdr *);
354 
355 				/*
356 				 * Restore source and destination addresses
357 				 * in the erroneous IPv6 header.
358 				 */
359 				ip6err->ip6_src = q6->ip6q_src;
360 				ip6err->ip6_dst = q6->ip6q_dst;
361 
362 				icmp6_error(merr, ICMP6_PARAM_PROB,
363 				    ICMP6_PARAMPROB_HEADER,
364 				    erroff - sizeof(struct ip6_frag) +
365 				    offsetof(struct ip6_frag, ip6f_offlg));
366 			}
367 		}
368 	}
369 
370 	ip6af = malloc(sizeof(*ip6af), M_FTABLE, M_DONTWAIT | M_ZERO);
371 	if (ip6af == NULL)
372 		goto dropfrag;
373 	ip6af->ip6af_flow = ip6->ip6_flow;
374 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
375 	ip6af->ip6af_off = fragoff;
376 	ip6af->ip6af_frglen = frgpartlen;
377 	ip6af->ip6af_offset = offset;
378 	IP6_REASS_MBUF(ip6af) = m;
379 
380 	if (first_frag) {
381 		paf6 = LIST_END(&q6->ip6q_asfrag);
382 		goto insert;
383 	}
384 
385 	/*
386 	 * Handle ECN by comparing this segment with the first one;
387 	 * if CE is set, do not lose CE.
388 	 * drop if CE and not-ECT are mixed for the same packet.
389 	 */
390 	af6 = LIST_FIRST(&q6->ip6q_asfrag);
391 	ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
392 	ecn0 = (ntohl(af6->ip6af_flow) >> 20) & IPTOS_ECN_MASK;
393 	if (ecn == IPTOS_ECN_CE) {
394 		if (ecn0 == IPTOS_ECN_NOTECT) {
395 			free(ip6af, M_FTABLE);
396 			goto dropfrag;
397 		}
398 		if (ecn0 != IPTOS_ECN_CE)
399 			af6->ip6af_flow |= htonl(IPTOS_ECN_CE << 20);
400 	}
401 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
402 		free(ip6af, M_FTABLE);
403 		goto dropfrag;
404 	}
405 
406 	/*
407 	 * Find a segment which begins after this one does.
408 	 */
409 	for (paf6 = LIST_END(&q6->ip6q_asfrag),
410 	    af6 = LIST_FIRST(&q6->ip6q_asfrag);
411 	    af6 != LIST_END(&q6->ip6q_asfrag);
412 	    paf6 = af6, af6 = LIST_NEXT(af6, ip6af_list))
413 		if (af6->ip6af_off > ip6af->ip6af_off)
414 			break;
415 
416 	/*
417 	 * RFC 5722, Errata 3089:  When reassembling an IPv6 datagram, if one
418 	 * or more its constituent fragments is determined to be an overlapping
419 	 * fragment, the entire datagram (and any constituent fragments) MUST
420 	 * be silently discarded.
421 	 */
422 	if (paf6 != LIST_END(&q6->ip6q_asfrag)) {
423 		i = (paf6->ip6af_off + paf6->ip6af_frglen) - ip6af->ip6af_off;
424 		if (i > 0) {
425 #if 0				/* suppress the noisy log */
426 			log(LOG_ERR, "%d bytes of a fragment from %s "
427 			    "overlaps the previous fragment\n",
428 			    i, ip6_sprintf(&q6->ip6q_src));
429 #endif
430 			free(ip6af, M_FTABLE);
431 			goto flushfrags;
432 		}
433 	}
434 	if (af6 != LIST_END(&q6->ip6q_asfrag)) {
435 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
436 		if (i > 0) {
437 #if 0				/* suppress the noisy log */
438 			log(LOG_ERR, "%d bytes of a fragment from %s "
439 			    "overlaps the succeeding fragment",
440 			    i, ip6_sprintf(&q6->ip6q_src));
441 #endif
442 			free(ip6af, M_FTABLE);
443 			goto flushfrags;
444 		}
445 	}
446 
447  insert:
448 	/*
449 	 * Stick new segment in its place;
450 	 * check for complete reassembly.
451 	 * Move to front of packet queue, as we are
452 	 * the most recently active fragmented packet.
453 	 */
454 	if (paf6 != LIST_END(&q6->ip6q_asfrag))
455 		LIST_INSERT_AFTER(paf6, ip6af, ip6af_list);
456 	else
457 		LIST_INSERT_HEAD(&q6->ip6q_asfrag, ip6af, ip6af_list);
458 	frag6_nfrags++;
459 	q6->ip6q_nfrag++;
460 #if 0 /* xxx */
461 	if (q6 != TAILQ_FIRST(&frag6_queue)) {
462 		TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
463 		TAILQ_INSERT_HEAD(&frag6_queue, q6, ip6q_queue);
464 	}
465 #endif
466 	next = 0;
467 	for (paf6 = LIST_END(&q6->ip6q_asfrag),
468 	    af6 = LIST_FIRST(&q6->ip6q_asfrag);
469 	    af6 != LIST_END(&q6->ip6q_asfrag);
470 	    paf6 = af6, af6 = LIST_NEXT(af6, ip6af_list)) {
471 		if (af6->ip6af_off != next) {
472 			IP6Q_UNLOCK();
473 			return IPPROTO_DONE;
474 		}
475 		next += af6->ip6af_frglen;
476 	}
477 	if (paf6->ip6af_mff) {
478 		IP6Q_UNLOCK();
479 		return IPPROTO_DONE;
480 	}
481 
482 	/*
483 	 * Reassembly is complete; concatenate fragments.
484 	 */
485 	ip6af = LIST_FIRST(&q6->ip6q_asfrag);
486 	LIST_REMOVE(ip6af, ip6af_list);
487 	t = m = IP6_REASS_MBUF(ip6af);
488 	while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) !=
489 	    LIST_END(&q6->ip6q_asfrag)) {
490 		LIST_REMOVE(af6, ip6af_list);
491 		while (t->m_next)
492 			t = t->m_next;
493 		t->m_next = IP6_REASS_MBUF(af6);
494 		m_adj(t->m_next, af6->ip6af_offset);
495 		free(af6, M_FTABLE);
496 	}
497 
498 	/* adjust offset to point where the original next header starts */
499 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
500 	free(ip6af, M_FTABLE);
501 	ip6 = mtod(m, struct ip6_hdr *);
502 	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
503 	ip6->ip6_src = q6->ip6q_src;
504 	ip6->ip6_dst = q6->ip6q_dst;
505 	nxt = q6->ip6q_nxt;
506 
507 	/* Delete frag6 header */
508 	if (frag6_deletefraghdr(m, offset) != 0) {
509 		TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
510 		frag6_nfrags -= q6->ip6q_nfrag;
511 		free(q6, M_FTABLE);
512 		frag6_nfragpackets--;
513 		goto dropfrag;
514 	}
515 
516 	/*
517 	 * Store NXT to the original.
518 	 */
519 	{
520 		u_int8_t *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
521 		*prvnxtp = nxt;
522 	}
523 
524 	TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
525 	frag6_nfrags -= q6->ip6q_nfrag;
526 	free(q6, M_FTABLE);
527 	frag6_nfragpackets--;
528 
529 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
530 		int plen = 0;
531 		for (t = m; t; t = t->m_next)
532 			plen += t->m_len;
533 		m->m_pkthdr.len = plen;
534 	}
535 
536 	ip6stat.ip6s_reassembled++;
537 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
538 
539 	/*
540 	 * Tell launch routine the next header
541 	 */
542 
543 	*mp = m;
544 	*offp = offset;
545 
546 	IP6Q_UNLOCK();
547 	return nxt;
548 
549  flushfrags:
550 	while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) !=
551 	    LIST_END(&q6->ip6q_asfrag)) {
552 		LIST_REMOVE(af6, ip6af_list);
553 		m_freem(IP6_REASS_MBUF(af6));
554 		free(af6, M_FTABLE);
555 	}
556 	ip6stat.ip6s_fragdropped += q6->ip6q_nfrag;
557 	TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
558 	frag6_nfrags -= q6->ip6q_nfrag;
559 	free(q6, M_FTABLE);
560 	frag6_nfragpackets--;
561 
562  dropfrag:
563 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
564 	ip6stat.ip6s_fragdropped++;
565 	m_freem(m);
566 	IP6Q_UNLOCK();
567 	return IPPROTO_DONE;
568 }
569 
570 /*
571  * Delete fragment header after the unfragmentable header portions.
572  */
573 int
574 frag6_deletefraghdr(struct mbuf *m, int offset)
575 {
576 	struct mbuf *t;
577 
578 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
579 		ovbcopy(mtod(m, caddr_t), mtod(m, caddr_t) +
580 		    sizeof(struct ip6_frag), offset);
581 		m->m_data += sizeof(struct ip6_frag);
582 		m->m_len -= sizeof(struct ip6_frag);
583 	} else {
584 		/* this comes with no copy if the boundary is on cluster */
585 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL)
586 			return (ENOBUFS);
587 		m_adj(t, sizeof(struct ip6_frag));
588 		m_cat(m, t);
589 	}
590 
591 	return (0);
592 }
593 
594 /*
595  * Free a fragment reassembly header and all
596  * associated datagrams.
597  */
598 void
599 frag6_freef(struct ip6q *q6)
600 {
601 	struct ip6asfrag *af6;
602 
603 	IP6Q_LOCK_CHECK();
604 
605 	while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) !=
606 	    LIST_END(&q6->ip6q_asfrag)) {
607 		struct mbuf *m = IP6_REASS_MBUF(af6);
608 
609 		LIST_REMOVE(af6, ip6af_list);
610 
611 		/*
612 		 * Return ICMP time exceeded error for the 1st fragment.
613 		 * Just free other fragments.
614 		 */
615 		if (af6->ip6af_off == 0) {
616 			struct ip6_hdr *ip6;
617 
618 			/* adjust pointer */
619 			ip6 = mtod(m, struct ip6_hdr *);
620 
621 			/* restore source and destination addresses */
622 			ip6->ip6_src = q6->ip6q_src;
623 			ip6->ip6_dst = q6->ip6q_dst;
624 
625 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
626 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
627 		} else
628 			m_freem(m);
629 		free(af6, M_FTABLE);
630 	}
631 	TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
632 	frag6_nfrags -= q6->ip6q_nfrag;
633 	free(q6, M_FTABLE);
634 	frag6_nfragpackets--;
635 }
636 
637 /*
638  * IPv6 reassembling timer processing;
639  * if a timer expires on a reassembly
640  * queue, discard it.
641  */
642 void
643 frag6_slowtimo(void)
644 {
645 	struct ip6q *q6, *nq6;
646 	int s = splsoftnet();
647 	extern struct route_in6 ip6_forward_rt;
648 
649 	IP6Q_LOCK();
650 	TAILQ_FOREACH_SAFE(q6, &frag6_queue, ip6q_queue, nq6)
651 		if (--q6->ip6q_ttl == 0) {
652 			ip6stat.ip6s_fragtimeout++;
653 			/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
654 			frag6_freef(q6);
655 		}
656 
657 	/*
658 	 * If we are over the maximum number of fragments
659 	 * (due to the limit being lowered), drain off
660 	 * enough to get down to the new limit.
661 	 */
662 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
663 	    !TAILQ_EMPTY(&frag6_queue)) {
664 		ip6stat.ip6s_fragoverflow++;
665 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
666 		frag6_freef(TAILQ_LAST(&frag6_queue, ip6q_head));
667 	}
668 	IP6Q_UNLOCK();
669 
670 	/*
671 	 * Routing changes might produce a better route than we last used;
672 	 * make sure we notice eventually, even if forwarding only for one
673 	 * destination and the cache is never replaced.
674 	 */
675 	if (ip6_forward_rt.ro_rt) {
676 		RTFREE(ip6_forward_rt.ro_rt);
677 		ip6_forward_rt.ro_rt = 0;
678 	}
679 
680 	splx(s);
681 }
682 
683 /*
684  * Drain off all datagram fragments.
685  */
686 void
687 frag6_drain(void)
688 {
689 	struct ip6q *q6;
690 
691 	if (ip6q_lock_try() == 0)
692 		return;
693 	while ((q6 = TAILQ_FIRST(&frag6_queue)) != TAILQ_END(&frag6_queue)) {
694 		ip6stat.ip6s_fragdropped++;
695 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
696 		frag6_freef(q6);
697 	}
698 	IP6Q_UNLOCK();
699 }
700