xref: /openbsd-src/sys/netinet6/frag6.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: frag6.c,v 1.34 2011/05/02 22:16:33 chl 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_enq(struct ip6asfrag *, struct ip6asfrag *);
66 void frag6_deq(struct ip6asfrag *);
67 void frag6_insque(struct ip6q *, struct ip6q *);
68 void frag6_remque(struct ip6q *);
69 void frag6_freef(struct ip6q *);
70 
71 static int ip6q_locked;
72 u_int frag6_nfragpackets;
73 u_int frag6_nfrags;
74 struct	ip6q ip6q;	/* ip6 reassemble queue */
75 
76 static __inline int ip6q_lock_try(void);
77 static __inline void ip6q_unlock(void);
78 
79 static __inline int
80 ip6q_lock_try()
81 {
82 	int s;
83 
84 	/* Use splvm() due to mbuf allocation. */
85 	s = splvm();
86 	if (ip6q_locked) {
87 		splx(s);
88 		return (0);
89 	}
90 	ip6q_locked = 1;
91 	splx(s);
92 	return (1);
93 }
94 
95 static __inline void
96 ip6q_unlock()
97 {
98 	int s;
99 
100 	s = splvm();
101 	ip6q_locked = 0;
102 	splx(s);
103 }
104 
105 #ifdef DIAGNOSTIC
106 #define	IP6Q_LOCK()							\
107 do {									\
108 	if (ip6q_lock_try() == 0) {					\
109 		printf("%s:%d: ip6q already locked\n", __FILE__, __LINE__); \
110 		panic("ip6q_lock");					\
111 	}								\
112 } while (0)
113 #define	IP6Q_LOCK_CHECK()						\
114 do {									\
115 	if (ip6q_locked == 0) {						\
116 		printf("%s:%d: ip6q lock not held\n", __FILE__, __LINE__); \
117 		panic("ip6q lock check");				\
118 	}								\
119 } while (0)
120 #else
121 #define	IP6Q_LOCK()		(void) ip6q_lock_try()
122 #define	IP6Q_LOCK_CHECK()	/* nothing */
123 #endif
124 
125 #define	IP6Q_UNLOCK()		ip6q_unlock()
126 
127 /*
128  * Initialise reassembly queue and fragment identifier.
129  */
130 void
131 frag6_init(void)
132 {
133 
134 	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
135 }
136 
137 /*
138  * In RFC2460, fragment and reassembly rule do not agree with each other,
139  * in terms of next header field handling in fragment header.
140  * While the sender will use the same value for all of the fragmented packets,
141  * receiver is suggested not to check the consistency.
142  *
143  * fragment rule (p20):
144  *	(2) A Fragment header containing:
145  *	The Next Header value that identifies the first header of
146  *	the Fragmentable Part of the original packet.
147  *		-> next header field is same for all fragments
148  *
149  * reassembly rule (p21):
150  *	The Next Header field of the last header of the Unfragmentable
151  *	Part is obtained from the Next Header field of the first
152  *	fragment's Fragment header.
153  *		-> should grab it from the first fragment only
154  *
155  * The following note also contradicts with fragment rule - noone is going to
156  * send different fragment with different next header field.
157  *
158  * additional note (p22):
159  *	The Next Header values in the Fragment headers of different
160  *	fragments of the same original packet may differ.  Only the value
161  *	from the Offset zero fragment packet is used for reassembly.
162  *		-> should grab it from the first fragment only
163  *
164  * There is no explicit reason given in the RFC.  Historical reason maybe?
165  */
166 /*
167  * Fragment input
168  */
169 int
170 frag6_input(struct mbuf **mp, int *offp, int proto)
171 {
172 	struct mbuf *m = *mp, *t;
173 	struct ip6_hdr *ip6;
174 	struct ip6_frag *ip6f;
175 	struct ip6q *q6;
176 	struct ip6asfrag *af6, *ip6af, *af6dwn;
177 	int offset = *offp, nxt, i, next;
178 	int first_frag = 0;
179 	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
180 	struct ifnet *dstifp;
181 #ifdef IN6_IFSTAT_STRICT
182 	struct route_in6 ro;
183 	struct sockaddr_in6 *dst;
184 #endif
185 	u_int8_t ecn, ecn0;
186 
187 	ip6 = mtod(m, struct ip6_hdr *);
188 	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
189 	if (ip6f == NULL)
190 		return IPPROTO_DONE;
191 
192 	dstifp = NULL;
193 #ifdef IN6_IFSTAT_STRICT
194 	/* find the destination interface of the packet. */
195 	bzero(&ro, sizeof(ro));
196 	dst = (struct sockaddr_in6 *)&ro.ro_dst;
197 	dst->sin6_family = AF_INET6;
198 	dst->sin6_len = sizeof(struct sockaddr_in6);
199 	dst->sin6_addr = ip6->ip6_dst;
200 
201 	rtalloc_mpath((struct route *)&ro, &ip6->ip6_src.s6_addr32[0]);
202 
203 	if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL)
204 		dstifp = ((struct in6_ifaddr *)ro.ro_rt->rt_ifa)->ia_ifp;
205 	if (ro.ro_rt != NULL) {
206 		RTFREE(ro.ro_rt);
207 		ro.ro_rt = NULL;
208 	}
209 #else
210 	/* we are violating the spec, this is not the destination interface */
211 	if ((m->m_flags & M_PKTHDR) != 0)
212 		dstifp = m->m_pkthdr.rcvif;
213 #endif
214 
215 	/* jumbo payload can't contain a fragment header */
216 	if (ip6->ip6_plen == 0) {
217 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
218 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
219 		return IPPROTO_DONE;
220 	}
221 
222 	/*
223 	 * check whether fragment packet's fragment length is
224 	 * multiple of 8 octets.
225 	 * sizeof(struct ip6_frag) == 8
226 	 * sizeof(struct ip6_hdr) = 40
227 	 */
228 	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
229 	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
230 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
231 		    offsetof(struct ip6_hdr, ip6_plen));
232 		in6_ifstat_inc(dstifp, ifs6_reass_fail);
233 		return IPPROTO_DONE;
234 	}
235 
236 	ip6stat.ip6s_fragments++;
237 	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
238 
239 	/* offset now points to data portion */
240 	offset += sizeof(struct ip6_frag);
241 
242 	IP6Q_LOCK();
243 
244 	/*
245 	 * Enforce upper bound on number of fragments.
246 	 * If maxfrag is 0, never accept fragments.
247 	 * If maxfrag is -1, accept all fragments without limitation.
248 	 */
249 	if (ip6_maxfrags < 0)
250 		;
251 	else if (frag6_nfrags >= (u_int)ip6_maxfrags)
252 		goto dropfrag;
253 
254 	for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
255 		if (ip6f->ip6f_ident == q6->ip6q_ident &&
256 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
257 		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
258 			break;
259 
260 	if (q6 == &ip6q) {
261 		/*
262 		 * the first fragment to arrive, create a reassembly queue.
263 		 */
264 		first_frag = 1;
265 
266 		/*
267 		 * Enforce upper bound on number of fragmented packets
268 		 * for which we attempt reassembly;
269 		 * If maxfragpackets is 0, never accept fragments.
270 		 * If maxfragpackets is -1, accept all fragments without
271 		 * limitation.
272 		 */
273 		if (ip6_maxfragpackets < 0)
274 			;
275 		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
276 			goto dropfrag;
277 		frag6_nfragpackets++;
278 		q6 = malloc(sizeof(*q6), M_FTABLE, M_DONTWAIT | M_ZERO);
279 		if (q6 == NULL)
280 			goto dropfrag;
281 
282 		frag6_insque(q6, &ip6q);
283 
284 		/* ip6q_nxt will be filled afterwards, from 1st fragment */
285 		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
286 #ifdef notyet
287 		q6->ip6q_nxtp	= (u_char *)nxtp;
288 #endif
289 		q6->ip6q_ident	= ip6f->ip6f_ident;
290 		q6->ip6q_arrive = 0; /* Is it used anywhere? */
291 		q6->ip6q_ttl 	= IPV6_FRAGTTL;
292 		q6->ip6q_src	= ip6->ip6_src;
293 		q6->ip6q_dst	= ip6->ip6_dst;
294 		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
295 
296 		q6->ip6q_nfrag = 0;
297 	}
298 
299 	/*
300 	 * If it's the 1st fragment, record the length of the
301 	 * unfragmentable part and the next header of the fragment header.
302 	 */
303 	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
304 	if (fragoff == 0) {
305 		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
306 		    sizeof(struct ip6_frag);
307 		q6->ip6q_nxt = ip6f->ip6f_nxt;
308 	}
309 
310 	/*
311 	 * Check that the reassembled packet would not exceed 65535 bytes
312 	 * in size.
313 	 * If it would exceed, discard the fragment and return an ICMP error.
314 	 */
315 	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
316 	if (q6->ip6q_unfrglen >= 0) {
317 		/* The 1st fragment has already arrived. */
318 		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
319 			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
320 			    offset - sizeof(struct ip6_frag) +
321 			    offsetof(struct ip6_frag, ip6f_offlg));
322 			IP6Q_UNLOCK();
323 			return (IPPROTO_DONE);
324 		}
325 	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
326 		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
327 			    offset - sizeof(struct ip6_frag) +
328 				offsetof(struct ip6_frag, ip6f_offlg));
329 		IP6Q_UNLOCK();
330 		return (IPPROTO_DONE);
331 	}
332 	/*
333 	 * If it's the first fragment, do the above check for each
334 	 * fragment already stored in the reassembly queue.
335 	 */
336 	if (fragoff == 0) {
337 		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
338 		     af6 = af6dwn) {
339 			af6dwn = af6->ip6af_down;
340 
341 			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
342 			    IPV6_MAXPACKET) {
343 				struct mbuf *merr = IP6_REASS_MBUF(af6);
344 				struct ip6_hdr *ip6err;
345 				int erroff = af6->ip6af_offset;
346 
347 				/* dequeue the fragment. */
348 				frag6_deq(af6);
349 				free(af6, M_FTABLE);
350 
351 				/* adjust pointer. */
352 				ip6err = mtod(merr, struct ip6_hdr *);
353 
354 				/*
355 				 * Restore source and destination addresses
356 				 * in the erroneous IPv6 header.
357 				 */
358 				ip6err->ip6_src = q6->ip6q_src;
359 				ip6err->ip6_dst = q6->ip6q_dst;
360 
361 				icmp6_error(merr, ICMP6_PARAM_PROB,
362 				    ICMP6_PARAMPROB_HEADER,
363 				    erroff - sizeof(struct ip6_frag) +
364 				    offsetof(struct ip6_frag, ip6f_offlg));
365 			}
366 		}
367 	}
368 
369 	ip6af = malloc(sizeof(*ip6af), M_FTABLE, M_DONTWAIT | M_ZERO);
370 	if (ip6af == NULL)
371 		goto dropfrag;
372 	ip6af->ip6af_head = ip6->ip6_flow;
373 	ip6af->ip6af_len = ip6->ip6_plen;
374 	ip6af->ip6af_nxt = ip6->ip6_nxt;
375 	ip6af->ip6af_hlim = ip6->ip6_hlim;
376 	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
377 	ip6af->ip6af_off = fragoff;
378 	ip6af->ip6af_frglen = frgpartlen;
379 	ip6af->ip6af_offset = offset;
380 	IP6_REASS_MBUF(ip6af) = m;
381 
382 	if (first_frag) {
383 		af6 = (struct ip6asfrag *)q6;
384 		goto insert;
385 	}
386 
387 	/*
388 	 * Handle ECN by comparing this segment with the first one;
389 	 * if CE is set, do not lose CE.
390 	 * drop if CE and not-ECT are mixed for the same packet.
391 	 */
392 	ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
393 	ecn0 = (ntohl(q6->ip6q_down->ip6af_head) >> 20) & IPTOS_ECN_MASK;
394 	if (ecn == IPTOS_ECN_CE) {
395 		if (ecn0 == IPTOS_ECN_NOTECT) {
396 			free(ip6af, M_FTABLE);
397 			goto dropfrag;
398 		}
399 		if (ecn0 != IPTOS_ECN_CE)
400 			q6->ip6q_down->ip6af_head |= htonl(IPTOS_ECN_CE << 20);
401 	}
402 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
403 		free(ip6af, M_FTABLE);
404 		goto dropfrag;
405 	}
406 
407 	/*
408 	 * Find a segment which begins after this one does.
409 	 */
410 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
411 	     af6 = af6->ip6af_down)
412 		if (af6->ip6af_off > ip6af->ip6af_off)
413 			break;
414 
415 #if 0
416 	/*
417 	 * If there is a preceding segment, it may provide some of
418 	 * our data already.  If so, drop the data from the incoming
419 	 * segment.  If it provides all of our data, drop us.
420 	 */
421 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
422 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
423 			- ip6af->ip6af_off;
424 		if (i > 0) {
425 			if (i >= ip6af->ip6af_frglen)
426 				goto dropfrag;
427 			m_adj(IP6_REASS_MBUF(ip6af), i);
428 			ip6af->ip6af_off += i;
429 			ip6af->ip6af_frglen -= i;
430 		}
431 	}
432 
433 	/*
434 	 * While we overlap succeeding segments trim them or,
435 	 * if they are completely covered, dequeue them.
436 	 */
437 	while (af6 != (struct ip6asfrag *)q6 &&
438 	       ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
439 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
440 		if (i < af6->ip6af_frglen) {
441 			af6->ip6af_frglen -= i;
442 			af6->ip6af_off += i;
443 			m_adj(IP6_REASS_MBUF(af6), i);
444 			break;
445 		}
446 		af6 = af6->ip6af_down;
447 		m_freem(IP6_REASS_MBUF(af6->ip6af_up));
448 		frag6_deq(af6->ip6af_up);
449 	}
450 #else
451 	/*
452 	 * If the incoming fragment overlaps some existing fragments in
453 	 * the reassembly queue, drop it, since it is dangerous to override
454 	 * existing fragments from a security point of view.
455 	 * We don't know which fragment is the bad guy - here we trust
456 	 * fragment that came in earlier, with no real reason.
457 	 */
458 	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
459 		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
460 			- ip6af->ip6af_off;
461 		if (i > 0) {
462 #if 0				/* suppress the noisy log */
463 			log(LOG_ERR, "%d bytes of a fragment from %s "
464 			    "overlaps the previous fragment\n",
465 			    i, ip6_sprintf(&q6->ip6q_src));
466 #endif
467 			free(ip6af, M_FTABLE);
468 			goto dropfrag;
469 		}
470 	}
471 	if (af6 != (struct ip6asfrag *)q6) {
472 		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
473 		if (i > 0) {
474 #if 0				/* suppress the noisy log */
475 			log(LOG_ERR, "%d bytes of a fragment from %s "
476 			    "overlaps the succeeding fragment",
477 			    i, ip6_sprintf(&q6->ip6q_src));
478 #endif
479 			free(ip6af, M_FTABLE);
480 			goto dropfrag;
481 		}
482 	}
483 #endif
484 
485 insert:
486 
487 	/*
488 	 * Stick new segment in its place;
489 	 * check for complete reassembly.
490 	 * Move to front of packet queue, as we are
491 	 * the most recently active fragmented packet.
492 	 */
493 	frag6_enq(ip6af, af6->ip6af_up);
494 	frag6_nfrags++;
495 	q6->ip6q_nfrag++;
496 #if 0 /* xxx */
497 	if (q6 != ip6q.ip6q_next) {
498 		frag6_remque(q6);
499 		frag6_insque(q6, &ip6q);
500 	}
501 #endif
502 	next = 0;
503 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
504 	     af6 = af6->ip6af_down) {
505 		if (af6->ip6af_off != next) {
506 			IP6Q_UNLOCK();
507 			return IPPROTO_DONE;
508 		}
509 		next += af6->ip6af_frglen;
510 	}
511 	if (af6->ip6af_up->ip6af_mff) {
512 		IP6Q_UNLOCK();
513 		return IPPROTO_DONE;
514 	}
515 
516 	/*
517 	 * Reassembly is complete; concatenate fragments.
518 	 */
519 	ip6af = q6->ip6q_down;
520 	t = m = IP6_REASS_MBUF(ip6af);
521 	af6 = ip6af->ip6af_down;
522 	frag6_deq(ip6af);
523 	while (af6 != (struct ip6asfrag *)q6) {
524 		af6dwn = af6->ip6af_down;
525 		frag6_deq(af6);
526 		while (t->m_next)
527 			t = t->m_next;
528 		t->m_next = IP6_REASS_MBUF(af6);
529 		m_adj(t->m_next, af6->ip6af_offset);
530 		free(af6, M_FTABLE);
531 		af6 = af6dwn;
532 	}
533 
534 	/* adjust offset to point where the original next header starts */
535 	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
536 	free(ip6af, M_FTABLE);
537 	ip6 = mtod(m, struct ip6_hdr *);
538 	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
539 	ip6->ip6_src = q6->ip6q_src;
540 	ip6->ip6_dst = q6->ip6q_dst;
541 	nxt = q6->ip6q_nxt;
542 #ifdef notyet
543 	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
544 #endif
545 
546 	/* Delete frag6 header */
547 	if (frag6_deletefraghdr(m, offset) != 0) {
548 		frag6_remque(q6);
549 		frag6_nfrags -= q6->ip6q_nfrag;
550 		free(q6, M_FTABLE);
551 		frag6_nfragpackets--;
552 		goto dropfrag;
553 	}
554 
555 	/*
556 	 * Store NXT to the original.
557 	 */
558 	{
559 		u_int8_t *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
560 		*prvnxtp = nxt;
561 	}
562 
563 	frag6_remque(q6);
564 	frag6_nfrags -= q6->ip6q_nfrag;
565 	free(q6, M_FTABLE);
566 	frag6_nfragpackets--;
567 
568 	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
569 		int plen = 0;
570 		for (t = m; t; t = t->m_next)
571 			plen += t->m_len;
572 		m->m_pkthdr.len = plen;
573 	}
574 
575 	ip6stat.ip6s_reassembled++;
576 	in6_ifstat_inc(dstifp, ifs6_reass_ok);
577 
578 	/*
579 	 * Tell launch routine the next header
580 	 */
581 
582 	*mp = m;
583 	*offp = offset;
584 
585 	IP6Q_UNLOCK();
586 	return nxt;
587 
588  dropfrag:
589 	in6_ifstat_inc(dstifp, ifs6_reass_fail);
590 	ip6stat.ip6s_fragdropped++;
591 	m_freem(m);
592 	IP6Q_UNLOCK();
593 	return IPPROTO_DONE;
594 }
595 
596 /*
597  * Delete fragment header after the unfragmentable header portions.
598  */
599 int
600 frag6_deletefraghdr(struct mbuf *m, int offset)
601 {
602 	struct mbuf *t;
603 
604 	if (m->m_len >= offset + sizeof(struct ip6_frag)) {
605 		ovbcopy(mtod(m, caddr_t), mtod(m, caddr_t) +
606 		    sizeof(struct ip6_frag), offset);
607 		m->m_data += sizeof(struct ip6_frag);
608 		m->m_len -= sizeof(struct ip6_frag);
609 	} else {
610 		/* this comes with no copy if the boundary is on cluster */
611 		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL)
612 			return (ENOBUFS);
613 		m_adj(t, sizeof(struct ip6_frag));
614 		m_cat(m, t);
615 	}
616 
617 	return (0);
618 }
619 
620 /*
621  * Free a fragment reassembly header and all
622  * associated datagrams.
623  */
624 void
625 frag6_freef(struct ip6q *q6)
626 {
627 	struct ip6asfrag *af6, *down6;
628 
629 	IP6Q_LOCK_CHECK();
630 
631 	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
632 	     af6 = down6) {
633 		struct mbuf *m = IP6_REASS_MBUF(af6);
634 
635 		down6 = af6->ip6af_down;
636 		frag6_deq(af6);
637 
638 		/*
639 		 * Return ICMP time exceeded error for the 1st fragment.
640 		 * Just free other fragments.
641 		 */
642 		if (af6->ip6af_off == 0) {
643 			struct ip6_hdr *ip6;
644 
645 			/* adjust pointer */
646 			ip6 = mtod(m, struct ip6_hdr *);
647 
648 			/* restore source and destination addresses */
649 			ip6->ip6_src = q6->ip6q_src;
650 			ip6->ip6_dst = q6->ip6q_dst;
651 
652 			icmp6_error(m, ICMP6_TIME_EXCEEDED,
653 				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
654 		} else
655 			m_freem(m);
656 		free(af6, M_FTABLE);
657 	}
658 	frag6_remque(q6);
659 	frag6_nfrags -= q6->ip6q_nfrag;
660 	free(q6, M_FTABLE);
661 	frag6_nfragpackets--;
662 }
663 
664 /*
665  * Put an ip fragment on a reassembly chain.
666  * Like insque, but pointers in middle of structure.
667  */
668 void
669 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
670 {
671 
672 	IP6Q_LOCK_CHECK();
673 
674 	af6->ip6af_up = up6;
675 	af6->ip6af_down = up6->ip6af_down;
676 	up6->ip6af_down->ip6af_up = af6;
677 	up6->ip6af_down = af6;
678 }
679 
680 /*
681  * To frag6_enq as remque is to insque.
682  */
683 void
684 frag6_deq(struct ip6asfrag *af6)
685 {
686 
687 	IP6Q_LOCK_CHECK();
688 
689 	af6->ip6af_up->ip6af_down = af6->ip6af_down;
690 	af6->ip6af_down->ip6af_up = af6->ip6af_up;
691 }
692 
693 void
694 frag6_insque(struct ip6q *new, struct ip6q *old)
695 {
696 
697 	IP6Q_LOCK_CHECK();
698 
699 	new->ip6q_prev = old;
700 	new->ip6q_next = old->ip6q_next;
701 	old->ip6q_next->ip6q_prev= new;
702 	old->ip6q_next = new;
703 }
704 
705 void
706 frag6_remque(struct ip6q *p6)
707 {
708 
709 	IP6Q_LOCK_CHECK();
710 
711 	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
712 	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
713 }
714 
715 /*
716  * IPv6 reassembling timer processing;
717  * if a timer expires on a reassembly
718  * queue, discard it.
719  */
720 void
721 frag6_slowtimo(void)
722 {
723 	struct ip6q *q6;
724 	int s = splsoftnet();
725 
726 	IP6Q_LOCK();
727 	q6 = ip6q.ip6q_next;
728 	if (q6)
729 		while (q6 != &ip6q) {
730 			--q6->ip6q_ttl;
731 			q6 = q6->ip6q_next;
732 			if (q6->ip6q_prev->ip6q_ttl == 0) {
733 				ip6stat.ip6s_fragtimeout++;
734 				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
735 				frag6_freef(q6->ip6q_prev);
736 			}
737 		}
738 	/*
739 	 * If we are over the maximum number of fragments
740 	 * (due to the limit being lowered), drain off
741 	 * enough to get down to the new limit.
742 	 */
743 	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
744 	    ip6q.ip6q_prev) {
745 		ip6stat.ip6s_fragoverflow++;
746 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
747 		frag6_freef(ip6q.ip6q_prev);
748 	}
749 	IP6Q_UNLOCK();
750 
751 #if 0
752 	/*
753 	 * Routing changes might produce a better route than we last used;
754 	 * make sure we notice eventually, even if forwarding only for one
755 	 * destination and the cache is never replaced.
756 	 */
757 	if (ip6_forward_rt.ro_rt) {
758 		RTFREE(ip6_forward_rt.ro_rt);
759 		ip6_forward_rt.ro_rt = 0;
760 	}
761 	if (ipsrcchk_rt.ro_rt) {
762 		RTFREE(ipsrcchk_rt.ro_rt);
763 		ipsrcchk_rt.ro_rt = 0;
764 	}
765 #endif
766 
767 	splx(s);
768 }
769 
770 /*
771  * Drain off all datagram fragments.
772  */
773 void
774 frag6_drain(void)
775 {
776 
777 	if (ip6q_lock_try() == 0)
778 		return;
779 	while (ip6q.ip6q_next != &ip6q) {
780 		ip6stat.ip6s_fragdropped++;
781 		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
782 		frag6_freef(ip6q.ip6q_next);
783 	}
784 	IP6Q_UNLOCK();
785 }
786