xref: /openbsd-src/sys/netinet6/frag6.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: frag6.c,v 1.53 2014/07/12 18:44:23 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 <netinet6/in6_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 	ro.ro_tableid = m->m_pkthdr.ph_rtableid;
193 	dst = &ro.ro_dst;
194 	dst->sin6_family = AF_INET6;
195 	dst->sin6_len = sizeof(struct sockaddr_in6);
196 	dst->sin6_addr = ip6->ip6_dst;
197 
198 	rtalloc_mpath((struct route *)&ro, &ip6->ip6_src.s6_addr32[0]);
199 
200 	if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL)
201 		dstifp = ifatoia6(ro.ro_rt->rt_ifa)->ia_ifp;
202 	if (ro.ro_rt != NULL) {
203 		RTFREE(ro.ro_rt);
204 		ro.ro_rt = NULL;
205 	}
206 #else
207 	/* we are violating the spec, this is not the destination interface */
208 	if ((m->m_flags & M_PKTHDR) != 0)
209 		dstifp = m->m_pkthdr.rcvif;
210 #endif
211 
212 	/* jumbo payload can't contain a fragment header */
213 	if (ip6->ip6_plen == 0) {
214 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
215 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
216 		return IPPROTO_DONE;
217 	}
218 
219 	/*
220 	 * check whether fragment packet's fragment length is
221 	 * multiple of 8 octets.
222 	 * sizeof(struct ip6_frag) == 8
223 	 * sizeof(struct ip6_hdr) = 40
224 	 */
225 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
226 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
227 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
228 		    offsetof(struct ip6_hdr, ip6_plen));
229 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
230 		return IPPROTO_DONE;
231 	}
232 
233 	ip6stat.ip6s_fragments++;
234 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
235 
236 	/* offset now points to data portion */
237 	offset += sizeof(struct ip6_frag);
238 
239 	/*
240 	 * RFC6946:  A host that receives an IPv6 packet which includes
241 	 * a Fragment Header with the "Fragment Offset" equal to 0 and
242 	 * the "M" bit equal to 0 MUST process such packet in isolation
243 	 * from any other packets/fragments.
244 	 */
245 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
246 	if (fragoff == 0 && !(ip6f->ip6f_offlg & IP6F_MORE_FRAG)) {
247 		ip6stat.ip6s_reassembled++;
248 		in6_ifstat_inc(dstifp, ifs6_reass_ok);
249 		*offp = offset;
250 		return ip6f->ip6f_nxt;
251 	}
252 
253 	IP6Q_LOCK();
254 
255 	/*
256 	 * Enforce upper bound on number of fragments.
257 	 * If maxfrag is 0, never accept fragments.
258 	 * If maxfrag is -1, accept all fragments without limitation.
259 	 */
260 	if (ip6_maxfrags < 0)
261 		;
262 	else if (frag6_nfrags >= (u_int)ip6_maxfrags)
263 		goto dropfrag;
264 
265 	TAILQ_FOREACH(q6, &frag6_queue, ip6q_queue)
266 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
267 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
268 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
269 			break;
270 
271 	if (q6 == NULL) {
272 		/*
273 		 * the first fragment to arrive, create a reassembly queue.
274 		 */
275 		first_frag = 1;
276 
277 		/*
278 		 * Enforce upper bound on number of fragmented packets
279 		 * for which we attempt reassembly;
280 		 * If maxfragpackets is 0, never accept fragments.
281 		 * If maxfragpackets is -1, accept all fragments without
282 		 * limitation.
283 		 */
284 		if (ip6_maxfragpackets < 0)
285 			;
286 		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
287 			goto dropfrag;
288 		frag6_nfragpackets++;
289 		q6 = malloc(sizeof(*q6), M_FTABLE, M_NOWAIT | M_ZERO);
290 		if (q6 == NULL)
291 			goto dropfrag;
292 
293 		TAILQ_INSERT_HEAD(&frag6_queue, q6, ip6q_queue);
294 
295 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
296 		LIST_INIT(&q6->ip6q_asfrag);
297 		q6->ip6q_ident	= ip6f->ip6f_ident;
298 		q6->ip6q_ttl	= IPV6_FRAGTTL;
299 		q6->ip6q_src	= ip6->ip6_src;
300 		q6->ip6q_dst	= ip6->ip6_dst;
301 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
302 		q6->ip6q_nfrag = 0;
303 	}
304 
305 	/*
306 	 * If it's the 1st fragment, record the length of the
307 	 * unfragmentable part and the next header of the fragment header.
308 	 */
309 	if (fragoff == 0) {
310 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
311 		    sizeof(struct ip6_frag);
312 		q6->ip6q_nxt = ip6f->ip6f_nxt;
313 	}
314 
315 	/*
316 	 * Check that the reassembled packet would not exceed 65535 bytes
317 	 * in size.
318 	 * If it would exceed, discard the fragment and return an ICMP error.
319 	 */
320 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
321 	if (q6->ip6q_unfrglen >= 0) {
322 		/* The 1st fragment has already arrived. */
323 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
324 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
325 			    offset - sizeof(struct ip6_frag) +
326 			    offsetof(struct ip6_frag, ip6f_offlg));
327 			IP6Q_UNLOCK();
328 			return (IPPROTO_DONE);
329 		}
330 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
331 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
332 			    offset - sizeof(struct ip6_frag) +
333 				offsetof(struct ip6_frag, ip6f_offlg));
334 		IP6Q_UNLOCK();
335 		return (IPPROTO_DONE);
336 	}
337 	/*
338 	 * If it's the first fragment, do the above check for each
339 	 * fragment already stored in the reassembly queue.
340 	 */
341 	if (fragoff == 0) {
342 		LIST_FOREACH_SAFE(af6, &q6->ip6q_asfrag, ip6af_list, naf6) {
343 			if (q6->ip6q_unfrglen + af6->ip6af_off +
344 			    af6->ip6af_frglen > IPV6_MAXPACKET) {
345 				struct mbuf *merr = IP6_REASS_MBUF(af6);
346 				struct ip6_hdr *ip6err;
347 				int erroff = af6->ip6af_offset;
348 
349 				/* dequeue the fragment. */
350 				LIST_REMOVE(af6, ip6af_list);
351 				free(af6, M_FTABLE, 0);
352 
353 				/* adjust pointer. */
354 				ip6err = mtod(merr, struct ip6_hdr *);
355 
356 				/*
357 				 * Restore source and destination addresses
358 				 * in the erroneous IPv6 header.
359 				 */
360 				ip6err->ip6_src = q6->ip6q_src;
361 				ip6err->ip6_dst = q6->ip6q_dst;
362 
363 				icmp6_error(merr, ICMP6_PARAM_PROB,
364 				    ICMP6_PARAMPROB_HEADER,
365 				    erroff - sizeof(struct ip6_frag) +
366 				    offsetof(struct ip6_frag, ip6f_offlg));
367 			}
368 		}
369 	}
370 
371 	ip6af = malloc(sizeof(*ip6af), M_FTABLE, M_NOWAIT | M_ZERO);
372 	if (ip6af == NULL)
373 		goto dropfrag;
374 	ip6af->ip6af_flow = ip6->ip6_flow;
375 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
376 	ip6af->ip6af_off = fragoff;
377 	ip6af->ip6af_frglen = frgpartlen;
378 	ip6af->ip6af_offset = offset;
379 	IP6_REASS_MBUF(ip6af) = m;
380 
381 	if (first_frag) {
382 		paf6 = NULL;
383 		goto insert;
384 	}
385 
386 	/*
387 	 * Handle ECN by comparing this segment with the first one;
388 	 * if CE is set, do not lose CE.
389 	 * drop if CE and not-ECT are mixed for the same packet.
390 	 */
391 	af6 = LIST_FIRST(&q6->ip6q_asfrag);
392 	ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
393 	ecn0 = (ntohl(af6->ip6af_flow) >> 20) & IPTOS_ECN_MASK;
394 	if (ecn == IPTOS_ECN_CE) {
395 		if (ecn0 == IPTOS_ECN_NOTECT) {
396 			free(ip6af, M_FTABLE, 0);
397 			goto dropfrag;
398 		}
399 		if (ecn0 != IPTOS_ECN_CE)
400 			af6->ip6af_flow |= htonl(IPTOS_ECN_CE << 20);
401 	}
402 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
403 		free(ip6af, M_FTABLE, 0);
404 		goto dropfrag;
405 	}
406 
407 	/*
408 	 * Find a segment which begins after this one does.
409 	 */
410 	for (paf6 = NULL, af6 = LIST_FIRST(&q6->ip6q_asfrag);
411 	    af6 != NULL;
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 != NULL) {
423 		i = (paf6->ip6af_off + paf6->ip6af_frglen) - ip6af->ip6af_off;
424 		if (i > 0) {
425 #if 0				/* suppress the noisy log */
426 			char ip[INET6_ADDRSTRLEN];
427 			log(LOG_ERR, "%d bytes of a fragment from %s "
428 			    "overlaps the previous fragment\n",
429 			    i,
430 			    inet_ntop(AF_INET6, &q6->ip6q_src, ip, sizeof(ip)));
431 #endif
432 			free(ip6af, M_FTABLE, 0);
433 			goto flushfrags;
434 		}
435 	}
436 	if (af6 != NULL) {
437 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
438 		if (i > 0) {
439 #if 0				/* suppress the noisy log */
440 			char ip[INET6_ADDRSTRLEN];
441 			log(LOG_ERR, "%d bytes of a fragment from %s "
442 			    "overlaps the succeeding fragment",
443 			    i,
444 			    inet_ntop(AF_INET6, &q6->ip6q_src, ip, sizeof(ip)));
445 #endif
446 			free(ip6af, M_FTABLE, 0);
447 			goto flushfrags;
448 		}
449 	}
450 
451  insert:
452 	/*
453 	 * Stick new segment in its place;
454 	 * check for complete reassembly.
455 	 * Move to front of packet queue, as we are
456 	 * the most recently active fragmented packet.
457 	 */
458 	if (paf6 != NULL)
459 		LIST_INSERT_AFTER(paf6, ip6af, ip6af_list);
460 	else
461 		LIST_INSERT_HEAD(&q6->ip6q_asfrag, ip6af, ip6af_list);
462 	frag6_nfrags++;
463 	q6->ip6q_nfrag++;
464 #if 0 /* xxx */
465 	if (q6 != TAILQ_FIRST(&frag6_queue)) {
466 		TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
467 		TAILQ_INSERT_HEAD(&frag6_queue, q6, ip6q_queue);
468 	}
469 #endif
470 	next = 0;
471 	for (paf6 = NULL, af6 = LIST_FIRST(&q6->ip6q_asfrag);
472 	    af6 != NULL;
473 	    paf6 = af6, af6 = LIST_NEXT(af6, ip6af_list)) {
474 		if (af6->ip6af_off != next) {
475 			IP6Q_UNLOCK();
476 			return IPPROTO_DONE;
477 		}
478 		next += af6->ip6af_frglen;
479 	}
480 	if (paf6->ip6af_mff) {
481 		IP6Q_UNLOCK();
482 		return IPPROTO_DONE;
483 	}
484 
485 	/*
486 	 * Reassembly is complete; concatenate fragments.
487 	 */
488 	ip6af = LIST_FIRST(&q6->ip6q_asfrag);
489 	LIST_REMOVE(ip6af, ip6af_list);
490 	t = m = IP6_REASS_MBUF(ip6af);
491 	while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) != NULL) {
492 		LIST_REMOVE(af6, ip6af_list);
493 		while (t->m_next)
494 			t = t->m_next;
495 		t->m_next = IP6_REASS_MBUF(af6);
496 		m_adj(t->m_next, af6->ip6af_offset);
497 		free(af6, M_FTABLE, 0);
498 	}
499 
500 	/* adjust offset to point where the original next header starts */
501 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
502 	free(ip6af, M_FTABLE, 0);
503 	ip6 = mtod(m, struct ip6_hdr *);
504 	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
505 	ip6->ip6_src = q6->ip6q_src;
506 	ip6->ip6_dst = q6->ip6q_dst;
507 	nxt = q6->ip6q_nxt;
508 
509 	/* Delete frag6 header */
510 	if (frag6_deletefraghdr(m, offset) != 0) {
511 		TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
512 		frag6_nfrags -= q6->ip6q_nfrag;
513 		free(q6, M_FTABLE, 0);
514 		frag6_nfragpackets--;
515 		goto dropfrag;
516 	}
517 
518 	/*
519 	 * Store NXT to the original.
520 	 */
521 	{
522 		u_int8_t *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
523 		*prvnxtp = nxt;
524 	}
525 
526 	TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
527 	frag6_nfrags -= q6->ip6q_nfrag;
528 	free(q6, M_FTABLE, 0);
529 	frag6_nfragpackets--;
530 
531 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
532 		int plen = 0;
533 		for (t = m; t; t = t->m_next)
534 			plen += t->m_len;
535 		m->m_pkthdr.len = plen;
536 	}
537 
538 	ip6stat.ip6s_reassembled++;
539 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
540 
541 	/*
542 	 * Tell launch routine the next header
543 	 */
544 
545 	*mp = m;
546 	*offp = offset;
547 
548 	IP6Q_UNLOCK();
549 	return nxt;
550 
551  flushfrags:
552 	while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) != NULL) {
553 		LIST_REMOVE(af6, ip6af_list);
554 		m_freem(IP6_REASS_MBUF(af6));
555 		free(af6, M_FTABLE, 0);
556 	}
557 	ip6stat.ip6s_fragdropped += q6->ip6q_nfrag;
558 	TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
559 	frag6_nfrags -= q6->ip6q_nfrag;
560 	free(q6, M_FTABLE, 0);
561 	frag6_nfragpackets--;
562 
563  dropfrag:
564 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
565 	ip6stat.ip6s_fragdropped++;
566 	m_freem(m);
567 	IP6Q_UNLOCK();
568 	return IPPROTO_DONE;
569 }
570 
571 /*
572  * Delete fragment header after the unfragmentable header portions.
573  */
574 int
575 frag6_deletefraghdr(struct mbuf *m, int offset)
576 {
577 	struct mbuf *t;
578 
579 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
580 		memmove(mtod(m, caddr_t) + sizeof(struct ip6_frag),
581 		    mtod(m, caddr_t), offset);
582 		m->m_data += sizeof(struct ip6_frag);
583 		m->m_len -= sizeof(struct ip6_frag);
584 	} else {
585 		/* this comes with no copy if the boundary is on cluster */
586 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL)
587 			return (ENOBUFS);
588 		m_adj(t, sizeof(struct ip6_frag));
589 		m_cat(m, t);
590 	}
591 
592 	return (0);
593 }
594 
595 /*
596  * Free a fragment reassembly header and all
597  * associated datagrams.
598  */
599 void
600 frag6_freef(struct ip6q *q6)
601 {
602 	struct ip6asfrag *af6;
603 
604 	IP6Q_LOCK_CHECK();
605 
606 	while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) != NULL) {
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, 0);
630 	}
631 	TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
632 	frag6_nfrags -= q6->ip6q_nfrag;
633 	free(q6, M_FTABLE, 0);
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)) != NULL) {
694 		ip6stat.ip6s_fragdropped++;
695 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
696 		frag6_freef(q6);
697 	}
698 	IP6Q_UNLOCK();
699 }
700