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