xref: /openbsd/sys/netinet6/frag6.c (revision 898184e3)
1 /*	$OpenBSD: frag6.c,v 1.45 2013/03/22 01:41:12 tedu 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 = ifatoia6(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 == NULL) {
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_NOWAIT | 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_NOWAIT | 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 = NULL;
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 = NULL, af6 = LIST_FIRST(&q6->ip6q_asfrag);
410 	    af6 != NULL;
411 	    paf6 = af6, af6 = LIST_NEXT(af6, ip6af_list))
412 		if (af6->ip6af_off > ip6af->ip6af_off)
413 			break;
414 
415 	/*
416 	 * RFC 5722, Errata 3089:  When reassembling an IPv6 datagram, if one
417 	 * or more its constituent fragments is determined to be an overlapping
418 	 * fragment, the entire datagram (and any constituent fragments) MUST
419 	 * be silently discarded.
420 	 */
421 	if (paf6 != NULL) {
422 		i = (paf6->ip6af_off + paf6->ip6af_frglen) - ip6af->ip6af_off;
423 		if (i > 0) {
424 #if 0				/* suppress the noisy log */
425 			log(LOG_ERR, "%d bytes of a fragment from %s "
426 			    "overlaps the previous fragment\n",
427 			    i, ip6_sprintf(&q6->ip6q_src));
428 #endif
429 			free(ip6af, M_FTABLE);
430 			goto flushfrags;
431 		}
432 	}
433 	if (af6 != NULL) {
434 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
435 		if (i > 0) {
436 #if 0				/* suppress the noisy log */
437 			log(LOG_ERR, "%d bytes of a fragment from %s "
438 			    "overlaps the succeeding fragment",
439 			    i, ip6_sprintf(&q6->ip6q_src));
440 #endif
441 			free(ip6af, M_FTABLE);
442 			goto flushfrags;
443 		}
444 	}
445 
446  insert:
447 	/*
448 	 * Stick new segment in its place;
449 	 * check for complete reassembly.
450 	 * Move to front of packet queue, as we are
451 	 * the most recently active fragmented packet.
452 	 */
453 	if (paf6 != NULL)
454 		LIST_INSERT_AFTER(paf6, ip6af, ip6af_list);
455 	else
456 		LIST_INSERT_HEAD(&q6->ip6q_asfrag, ip6af, ip6af_list);
457 	frag6_nfrags++;
458 	q6->ip6q_nfrag++;
459 #if 0 /* xxx */
460 	if (q6 != TAILQ_FIRST(&frag6_queue)) {
461 		TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
462 		TAILQ_INSERT_HEAD(&frag6_queue, q6, ip6q_queue);
463 	}
464 #endif
465 	next = 0;
466 	for (paf6 = NULL, af6 = LIST_FIRST(&q6->ip6q_asfrag);
467 	    af6 != NULL;
468 	    paf6 = af6, af6 = LIST_NEXT(af6, ip6af_list)) {
469 		if (af6->ip6af_off != next) {
470 			IP6Q_UNLOCK();
471 			return IPPROTO_DONE;
472 		}
473 		next += af6->ip6af_frglen;
474 	}
475 	if (paf6->ip6af_mff) {
476 		IP6Q_UNLOCK();
477 		return IPPROTO_DONE;
478 	}
479 
480 	/*
481 	 * Reassembly is complete; concatenate fragments.
482 	 */
483 	ip6af = LIST_FIRST(&q6->ip6q_asfrag);
484 	LIST_REMOVE(ip6af, ip6af_list);
485 	t = m = IP6_REASS_MBUF(ip6af);
486 	while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) != NULL) {
487 		LIST_REMOVE(af6, ip6af_list);
488 		while (t->m_next)
489 			t = t->m_next;
490 		t->m_next = IP6_REASS_MBUF(af6);
491 		m_adj(t->m_next, af6->ip6af_offset);
492 		free(af6, M_FTABLE);
493 	}
494 
495 	/* adjust offset to point where the original next header starts */
496 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
497 	free(ip6af, M_FTABLE);
498 	ip6 = mtod(m, struct ip6_hdr *);
499 	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
500 	ip6->ip6_src = q6->ip6q_src;
501 	ip6->ip6_dst = q6->ip6q_dst;
502 	nxt = q6->ip6q_nxt;
503 
504 	/* Delete frag6 header */
505 	if (frag6_deletefraghdr(m, offset) != 0) {
506 		TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
507 		frag6_nfrags -= q6->ip6q_nfrag;
508 		free(q6, M_FTABLE);
509 		frag6_nfragpackets--;
510 		goto dropfrag;
511 	}
512 
513 	/*
514 	 * Store NXT to the original.
515 	 */
516 	{
517 		u_int8_t *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
518 		*prvnxtp = nxt;
519 	}
520 
521 	TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
522 	frag6_nfrags -= q6->ip6q_nfrag;
523 	free(q6, M_FTABLE);
524 	frag6_nfragpackets--;
525 
526 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
527 		int plen = 0;
528 		for (t = m; t; t = t->m_next)
529 			plen += t->m_len;
530 		m->m_pkthdr.len = plen;
531 	}
532 
533 	ip6stat.ip6s_reassembled++;
534 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
535 
536 	/*
537 	 * Tell launch routine the next header
538 	 */
539 
540 	*mp = m;
541 	*offp = offset;
542 
543 	IP6Q_UNLOCK();
544 	return nxt;
545 
546  flushfrags:
547 	while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) != NULL) {
548 		LIST_REMOVE(af6, ip6af_list);
549 		m_freem(IP6_REASS_MBUF(af6));
550 		free(af6, M_FTABLE);
551 	}
552 	ip6stat.ip6s_fragdropped += q6->ip6q_nfrag;
553 	TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
554 	frag6_nfrags -= q6->ip6q_nfrag;
555 	free(q6, M_FTABLE);
556 	frag6_nfragpackets--;
557 
558  dropfrag:
559 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
560 	ip6stat.ip6s_fragdropped++;
561 	m_freem(m);
562 	IP6Q_UNLOCK();
563 	return IPPROTO_DONE;
564 }
565 
566 /*
567  * Delete fragment header after the unfragmentable header portions.
568  */
569 int
570 frag6_deletefraghdr(struct mbuf *m, int offset)
571 {
572 	struct mbuf *t;
573 
574 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
575 		ovbcopy(mtod(m, caddr_t), mtod(m, caddr_t) +
576 		    sizeof(struct ip6_frag), offset);
577 		m->m_data += sizeof(struct ip6_frag);
578 		m->m_len -= sizeof(struct ip6_frag);
579 	} else {
580 		/* this comes with no copy if the boundary is on cluster */
581 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL)
582 			return (ENOBUFS);
583 		m_adj(t, sizeof(struct ip6_frag));
584 		m_cat(m, t);
585 	}
586 
587 	return (0);
588 }
589 
590 /*
591  * Free a fragment reassembly header and all
592  * associated datagrams.
593  */
594 void
595 frag6_freef(struct ip6q *q6)
596 {
597 	struct ip6asfrag *af6;
598 
599 	IP6Q_LOCK_CHECK();
600 
601 	while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) != NULL) {
602 		struct mbuf *m = IP6_REASS_MBUF(af6);
603 
604 		LIST_REMOVE(af6, ip6af_list);
605 
606 		/*
607 		 * Return ICMP time exceeded error for the 1st fragment.
608 		 * Just free other fragments.
609 		 */
610 		if (af6->ip6af_off == 0) {
611 			struct ip6_hdr *ip6;
612 
613 			/* adjust pointer */
614 			ip6 = mtod(m, struct ip6_hdr *);
615 
616 			/* restore source and destination addresses */
617 			ip6->ip6_src = q6->ip6q_src;
618 			ip6->ip6_dst = q6->ip6q_dst;
619 
620 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
621 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
622 		} else
623 			m_freem(m);
624 		free(af6, M_FTABLE);
625 	}
626 	TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
627 	frag6_nfrags -= q6->ip6q_nfrag;
628 	free(q6, M_FTABLE);
629 	frag6_nfragpackets--;
630 }
631 
632 /*
633  * IPv6 reassembling timer processing;
634  * if a timer expires on a reassembly
635  * queue, discard it.
636  */
637 void
638 frag6_slowtimo(void)
639 {
640 	struct ip6q *q6, *nq6;
641 	int s = splsoftnet();
642 	extern struct route_in6 ip6_forward_rt;
643 
644 	IP6Q_LOCK();
645 	TAILQ_FOREACH_SAFE(q6, &frag6_queue, ip6q_queue, nq6)
646 		if (--q6->ip6q_ttl == 0) {
647 			ip6stat.ip6s_fragtimeout++;
648 			/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
649 			frag6_freef(q6);
650 		}
651 
652 	/*
653 	 * If we are over the maximum number of fragments
654 	 * (due to the limit being lowered), drain off
655 	 * enough to get down to the new limit.
656 	 */
657 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
658 	    !TAILQ_EMPTY(&frag6_queue)) {
659 		ip6stat.ip6s_fragoverflow++;
660 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
661 		frag6_freef(TAILQ_LAST(&frag6_queue, ip6q_head));
662 	}
663 	IP6Q_UNLOCK();
664 
665 	/*
666 	 * Routing changes might produce a better route than we last used;
667 	 * make sure we notice eventually, even if forwarding only for one
668 	 * destination and the cache is never replaced.
669 	 */
670 	if (ip6_forward_rt.ro_rt) {
671 		RTFREE(ip6_forward_rt.ro_rt);
672 		ip6_forward_rt.ro_rt = 0;
673 	}
674 
675 	splx(s);
676 }
677 
678 /*
679  * Drain off all datagram fragments.
680  */
681 void
682 frag6_drain(void)
683 {
684 	struct ip6q *q6;
685 
686 	if (ip6q_lock_try() == 0)
687 		return;
688 	while ((q6 = TAILQ_FIRST(&frag6_queue)) != NULL) {
689 		ip6stat.ip6s_fragdropped++;
690 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
691 		frag6_freef(q6);
692 	}
693 	IP6Q_UNLOCK();
694 }
695