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