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