frag6.c revision 157927
1281SN/A/*	$FreeBSD: head/sys/netinet6/frag6.c 157927 2006-04-21 09:25:40Z ps $	*/
2281SN/A/*	$KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $	*/
3281SN/A
4281SN/A/*-
5281SN/A * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6281SN/A * All rights reserved.
7281SN/A *
8281SN/A * Redistribution and use in source and binary forms, with or without
9281SN/A * modification, are permitted provided that the following conditions
10281SN/A * are met:
11281SN/A * 1. Redistributions of source code must retain the above copyright
12281SN/A *    notice, this list of conditions and the following disclaimer.
13281SN/A * 2. Redistributions in binary form must reproduce the above copyright
14281SN/A *    notice, this list of conditions and the following disclaimer in the
15281SN/A *    documentation and/or other materials provided with the distribution.
16281SN/A * 3. Neither the name of the project nor the names of its contributors
17281SN/A *    may be used to endorse or promote products derived from this software
18281SN/A *    without specific prior written permission.
19281SN/A *
20281SN/A * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21281SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22281SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23281SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24281SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25281SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26281SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27281SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28281SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29281SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30281SN/A * SUCH DAMAGE.
31281SN/A */
32281SN/A
33281SN/A#include <sys/param.h>
34281SN/A#include <sys/systm.h>
35281SN/A#include <sys/malloc.h>
36281SN/A#include <sys/mbuf.h>
37281SN/A#include <sys/domain.h>
38281SN/A#include <sys/protosw.h>
39281SN/A#include <sys/socket.h>
40281SN/A#include <sys/errno.h>
41281SN/A#include <sys/time.h>
42281SN/A#include <sys/kernel.h>
43281SN/A#include <sys/syslog.h>
44281SN/A
45281SN/A#include <net/if.h>
46281SN/A#include <net/route.h>
47281SN/A
48281SN/A#include <netinet/in.h>
49281SN/A#include <netinet/in_var.h>
50281SN/A#include <netinet/ip6.h>
51281SN/A#include <netinet6/ip6_var.h>
52281SN/A#include <netinet/icmp6.h>
531188Sjoehw#include <netinet/in_systm.h>	/* for ECN definitions */
54281SN/A#include <netinet/ip.h>		/* for ECN definitions */
55281SN/A
56281SN/A#include <net/net_osdep.h>
57281SN/A
58281SN/A/*
59281SN/A * Define it to get a correct behavior on per-interface statistics.
60281SN/A * You will need to perform an extra routing table lookup, per fragment,
61281SN/A * to do it.  This may, or may not be, a performance hit.
62281SN/A */
63281SN/A#define IN6_IFSTAT_STRICT
64281SN/A
65281SN/Astatic void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *));
66281SN/Astatic void frag6_deq __P((struct ip6asfrag *));
67281SN/Astatic void frag6_insque __P((struct ip6q *, struct ip6q *));
68281SN/Astatic void frag6_remque __P((struct ip6q *));
69281SN/Astatic void frag6_freef __P((struct ip6q *));
70281SN/A
71281SN/Astatic struct mtx ip6qlock;
72281SN/A/*
73281SN/A * These fields all protected by ip6qlock.
74281SN/A */
75281SN/Astatic u_int frag6_nfragpackets;
76281SN/Astatic u_int frag6_nfrags;
77281SN/Astatic struct	ip6q ip6q;	/* ip6 reassemble queue */
78281SN/A
79281SN/A#define	IP6Q_LOCK_INIT()	mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF);
80281SN/A#define	IP6Q_LOCK()		mtx_lock(&ip6qlock)
81281SN/A#define	IP6Q_TRYLOCK()		mtx_trylock(&ip6qlock)
82281SN/A#define	IP6Q_LOCK_ASSERT()	mtx_assert(&ip6qlock, MA_OWNED)
83605SN/A#define	IP6Q_UNLOCK()		mtx_unlock(&ip6qlock)
84281SN/A
85281SN/Astatic MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
86281SN/A
87281SN/A/*
88281SN/A * Initialise reassembly queue and fragment identifier.
89281SN/A */
90281SN/Astatic void
91281SN/Afrag6_change(void *tag)
92281SN/A{
93281SN/A
94281SN/A	ip6_maxfragpackets = nmbclusters / 4;
95281SN/A	ip6_maxfrags = nmbclusters / 4;
96281SN/A}
97281SN/A
98281SN/Avoid
99281SN/Afrag6_init()
100281SN/A{
101281SN/A
102281SN/A	ip6_maxfragpackets = nmbclusters / 4;
103281SN/A	ip6_maxfrags = nmbclusters / 4;
104281SN/A	EVENTHANDLER_REGISTER(nmbclusters_change,
105281SN/A	    frag6_change, NULL, EVENTHANDLER_PRI_ANY);
106281SN/A
107281SN/A	IP6Q_LOCK_INIT();
108405SN/A
109405SN/A	ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
110281SN/A}
111281SN/A
112281SN/A/*
113281SN/A * In RFC2460, fragment and reassembly rule do not agree with each other,
114281SN/A * in terms of next header field handling in fragment header.
115281SN/A * While the sender will use the same value for all of the fragmented packets,
116281SN/A * receiver is suggested not to check the consistency.
117281SN/A *
118281SN/A * fragment rule (p20):
119281SN/A *	(2) A Fragment header containing:
120281SN/A *	The Next Header value that identifies the first header of
121281SN/A *	the Fragmentable Part of the original packet.
122281SN/A *		-> next header field is same for all fragments
123281SN/A *
124281SN/A * reassembly rule (p21):
125281SN/A *	The Next Header field of the last header of the Unfragmentable
126281SN/A *	Part is obtained from the Next Header field of the first
127281SN/A *	fragment's Fragment header.
128281SN/A *		-> should grab it from the first fragment only
129281SN/A *
130281SN/A * The following note also contradicts with fragment rule - noone is going to
131281SN/A * send different fragment with different next header field.
132281SN/A *
133281SN/A * additional note (p22):
134281SN/A *	The Next Header values in the Fragment headers of different
135281SN/A *	fragments of the same original packet may differ.  Only the value
136281SN/A *	from the Offset zero fragment packet is used for reassembly.
137281SN/A *		-> should grab it from the first fragment only
138281SN/A *
139281SN/A * There is no explicit reason given in the RFC.  Historical reason maybe?
140281SN/A */
141281SN/A/*
142281SN/A * Fragment input
143281SN/A */
144281SN/Aint
145281SN/Afrag6_input(mp, offp, proto)
146281SN/A	struct mbuf **mp;
147281SN/A	int *offp, proto;
148281SN/A{
149281SN/A	struct mbuf *m = *mp, *t;
150281SN/A	struct ip6_hdr *ip6;
151281SN/A	struct ip6_frag *ip6f;
152281SN/A	struct ip6q *q6;
153281SN/A	struct ip6asfrag *af6, *ip6af, *af6dwn;
154281SN/A#ifdef IN6_IFSTAT_STRICT
155281SN/A	struct in6_ifaddr *ia;
156281SN/A#endif
157281SN/A	int offset = *offp, nxt, i, next;
158281SN/A	int first_frag = 0;
159281SN/A	int fragoff, frgpartlen;	/* must be larger than u_int16_t */
160281SN/A	struct ifnet *dstifp;
161281SN/A	u_int8_t ecn, ecn0;
162281SN/A
163281SN/A	ip6 = mtod(m, struct ip6_hdr *);
164281SN/A#ifndef PULLDOWN_TEST
165281SN/A	IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
166281SN/A	ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
167281SN/A#else
168281SN/A	IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
169281SN/A	if (ip6f == NULL)
170281SN/A		return (IPPROTO_DONE);
171281SN/A#endif
172281SN/A
173281SN/A	dstifp = NULL;
174405SN/A#ifdef IN6_IFSTAT_STRICT
175405SN/A	/* find the destination interface of the packet. */
176405SN/A	if ((ia = ip6_getdstifaddr(m)) != NULL)
177405SN/A		dstifp = ia->ia_ifp;
178405SN/A#else
179405SN/A	/* we are violating the spec, this is not the destination interface */
180405SN/A	if ((m->m_flags & M_PKTHDR) != 0)
181281SN/A		dstifp = m->m_pkthdr.rcvif;
182405SN/A#endif
183281SN/A
184281SN/A	/* jumbo payload can't contain a fragment header */
185281SN/A	if (ip6->ip6_plen == 0) {
186281SN/A		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
1871188Sjoehw		in6_ifstat_inc(dstifp, ifs6_reass_fail);
1881188Sjoehw		return IPPROTO_DONE;
1891188Sjoehw	}
1901188Sjoehw
1911188Sjoehw	/*
1921188Sjoehw	 * check whether fragment packet's fragment length is
1931188Sjoehw	 * multiple of 8 octets.
194281SN/A	 * sizeof(struct ip6_frag) == 8
195281SN/A	 * sizeof(struct ip6_hdr) = 40
196281SN/A	 */
197281SN/A	if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
198281SN/A	    (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
199281SN/A		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
200281SN/A		    offsetof(struct ip6_hdr, ip6_plen));
201281SN/A		in6_ifstat_inc(dstifp, ifs6_reass_fail);
202281SN/A		return IPPROTO_DONE;
203281SN/A	}
204281SN/A
205281SN/A	ip6stat.ip6s_fragments++;
206281SN/A	in6_ifstat_inc(dstifp, ifs6_reass_reqd);
207281SN/A
208281SN/A	/* offset now points to data portion */
209281SN/A	offset += sizeof(struct ip6_frag);
210281SN/A
211281SN/A	IP6Q_LOCK();
212281SN/A
213281SN/A	/*
214281SN/A	 * Enforce upper bound on number of fragments.
215281SN/A	 * If maxfrag is 0, never accept fragments.
216281SN/A	 * If maxfrag is -1, accept all fragments without limitation.
217281SN/A	 */
218281SN/A	if (ip6_maxfrags < 0)
219281SN/A		;
220281SN/A	else if (frag6_nfrags >= (u_int)ip6_maxfrags)
221281SN/A		goto dropfrag;
222281SN/A
223281SN/A	for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
224281SN/A		if (ip6f->ip6f_ident == q6->ip6q_ident &&
225281SN/A		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
226281SN/A		    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
227281SN/A			break;
228281SN/A
229281SN/A	if (q6 == &ip6q) {
230281SN/A		/*
231281SN/A		 * the first fragment to arrive, create a reassembly queue.
232281SN/A		 */
233281SN/A		first_frag = 1;
234281SN/A
235281SN/A		/*
236281SN/A		 * Enforce upper bound on number of fragmented packets
237281SN/A		 * for which we attempt reassembly;
238281SN/A		 * If maxfragpackets is 0, never accept fragments.
239281SN/A		 * If maxfragpackets is -1, accept all fragments without
240281SN/A		 * limitation.
241281SN/A		 */
242281SN/A		if (ip6_maxfragpackets < 0)
243281SN/A			;
244281SN/A		else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
245281SN/A			goto dropfrag;
246281SN/A		frag6_nfragpackets++;
247281SN/A		q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
248281SN/A		    M_NOWAIT);
249281SN/A		if (q6 == NULL)
250281SN/A			goto dropfrag;
251281SN/A		bzero(q6, sizeof(*q6));
252281SN/A
253281SN/A		frag6_insque(q6, &ip6q);
254281SN/A
255281SN/A		/* ip6q_nxt will be filled afterwards, from 1st fragment */
256281SN/A		q6->ip6q_down	= q6->ip6q_up = (struct ip6asfrag *)q6;
257281SN/A#ifdef notyet
258281SN/A		q6->ip6q_nxtp	= (u_char *)nxtp;
259281SN/A#endif
260281SN/A		q6->ip6q_ident	= ip6f->ip6f_ident;
261281SN/A		q6->ip6q_arrive = 0; /* Is it used anywhere? */
262281SN/A		q6->ip6q_ttl 	= IPV6_FRAGTTL;
263281SN/A		q6->ip6q_src	= ip6->ip6_src;
264281SN/A		q6->ip6q_dst	= ip6->ip6_dst;
265281SN/A		q6->ip6q_unfrglen = -1;	/* The 1st fragment has not arrived. */
266281SN/A
267281SN/A		q6->ip6q_nfrag = 0;
268281SN/A	}
269281SN/A
270281SN/A	/*
271281SN/A	 * If it's the 1st fragment, record the length of the
272281SN/A	 * unfragmentable part and the next header of the fragment header.
273281SN/A	 */
274281SN/A	fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
275281SN/A	if (fragoff == 0) {
276281SN/A		q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
277281SN/A		    sizeof(struct ip6_frag);
278281SN/A		q6->ip6q_nxt = ip6f->ip6f_nxt;
279281SN/A	}
280281SN/A
281281SN/A	/*
282281SN/A	 * Check that the reassembled packet would not exceed 65535 bytes
283281SN/A	 * in size.
284281SN/A	 * If it would exceed, discard the fragment and return an ICMP error.
285281SN/A	 */
286281SN/A	frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
287281SN/A	if (q6->ip6q_unfrglen >= 0) {
288281SN/A		/* The 1st fragment has already arrived. */
289281SN/A		if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
290281SN/A			icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
291281SN/A			    offset - sizeof(struct ip6_frag) +
292281SN/A			    offsetof(struct ip6_frag, ip6f_offlg));
293281SN/A			IP6Q_UNLOCK();
294281SN/A			return (IPPROTO_DONE);
295281SN/A		}
296281SN/A	} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
297281SN/A		icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
298281SN/A		    offset - sizeof(struct ip6_frag) +
299281SN/A		    offsetof(struct ip6_frag, ip6f_offlg));
300281SN/A		IP6Q_UNLOCK();
301281SN/A		return (IPPROTO_DONE);
302281SN/A	}
303281SN/A	/*
304281SN/A	 * If it's the first fragment, do the above check for each
305281SN/A	 * fragment already stored in the reassembly queue.
306281SN/A	 */
307281SN/A	if (fragoff == 0) {
308281SN/A		for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
309281SN/A		     af6 = af6dwn) {
310281SN/A			af6dwn = af6->ip6af_down;
311281SN/A
312281SN/A			if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
313281SN/A			    IPV6_MAXPACKET) {
314281SN/A				struct mbuf *merr = IP6_REASS_MBUF(af6);
315281SN/A				struct ip6_hdr *ip6err;
316281SN/A				int erroff = af6->ip6af_offset;
317281SN/A
318281SN/A				/* dequeue the fragment. */
319281SN/A				frag6_deq(af6);
320281SN/A				free(af6, M_FTABLE);
321281SN/A
322281SN/A				/* adjust pointer. */
323281SN/A				ip6err = mtod(merr, struct ip6_hdr *);
324281SN/A
325281SN/A				/*
326281SN/A				 * Restore source and destination addresses
327281SN/A				 * in the erroneous IPv6 header.
328281SN/A				 */
329281SN/A				ip6err->ip6_src = q6->ip6q_src;
330281SN/A				ip6err->ip6_dst = q6->ip6q_dst;
331281SN/A
332281SN/A				icmp6_error(merr, ICMP6_PARAM_PROB,
333281SN/A				    ICMP6_PARAMPROB_HEADER,
334281SN/A				    erroff - sizeof(struct ip6_frag) +
335281SN/A				    offsetof(struct ip6_frag, ip6f_offlg));
336281SN/A			}
337281SN/A		}
338281SN/A	}
339281SN/A
340281SN/A	ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
341761Sdfuchs	    M_NOWAIT);
342281SN/A	if (ip6af == NULL)
343281SN/A		goto dropfrag;
344281SN/A	bzero(ip6af, sizeof(*ip6af));
345281SN/A	ip6af->ip6af_head = ip6->ip6_flow;
346761Sdfuchs	ip6af->ip6af_len = ip6->ip6_plen;
347761Sdfuchs	ip6af->ip6af_nxt = ip6->ip6_nxt;
348761Sdfuchs	ip6af->ip6af_hlim = ip6->ip6_hlim;
349761Sdfuchs	ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
350761Sdfuchs	ip6af->ip6af_off = fragoff;
351761Sdfuchs	ip6af->ip6af_frglen = frgpartlen;
352761Sdfuchs	ip6af->ip6af_offset = offset;
353761Sdfuchs	IP6_REASS_MBUF(ip6af) = m;
354281SN/A
355761Sdfuchs	if (first_frag) {
356761Sdfuchs		af6 = (struct ip6asfrag *)q6;
357761Sdfuchs		goto insert;
358281SN/A	}
359281SN/A
360281SN/A	/*
361281SN/A	 * Handle ECN by comparing this segment with the first one;
362761Sdfuchs	 * if CE is set, do not lose CE.
363281SN/A	 * drop if CE and not-ECT are mixed for the same packet.
364281SN/A	 */
365281SN/A	ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
366281SN/A	ecn0 = (ntohl(q6->ip6q_down->ip6af_head) >> 20) & IPTOS_ECN_MASK;
367281SN/A	if (ecn == IPTOS_ECN_CE) {
368761Sdfuchs		if (ecn0 == IPTOS_ECN_NOTECT) {
369761Sdfuchs			free(ip6af, M_FTABLE);
370761Sdfuchs			goto dropfrag;
371281SN/A		}
372281SN/A		if (ecn0 != IPTOS_ECN_CE)
373281SN/A			q6->ip6q_down->ip6af_head |= htonl(IPTOS_ECN_CE << 20);
374281SN/A	}
375281SN/A	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
376281SN/A		free(ip6af, M_FTABLE);
377281SN/A		goto dropfrag;
378281SN/A	}
379761Sdfuchs
380281SN/A	/*
381281SN/A	 * Find a segment which begins after this one does.
382281SN/A	 */
383281SN/A	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
384281SN/A	     af6 = af6->ip6af_down)
385281SN/A		if (af6->ip6af_off > ip6af->ip6af_off)
386281SN/A			break;
387761Sdfuchs
388761Sdfuchs#if 0
389761Sdfuchs	/*
390761Sdfuchs	 * If there is a preceding segment, it may provide some of
391761Sdfuchs	 * our data already.  If so, drop the data from the incoming
392761Sdfuchs	 * segment.  If it provides all of our data, drop us.
393761Sdfuchs	 */
394761Sdfuchs	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
395761Sdfuchs		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
396761Sdfuchs			- ip6af->ip6af_off;
397761Sdfuchs		if (i > 0) {
398761Sdfuchs			if (i >= ip6af->ip6af_frglen)
399761Sdfuchs				goto dropfrag;
400281SN/A			m_adj(IP6_REASS_MBUF(ip6af), i);
401281SN/A			ip6af->ip6af_off += i;
402			ip6af->ip6af_frglen -= i;
403		}
404	}
405
406	/*
407	 * While we overlap succeeding segments trim them or,
408	 * if they are completely covered, dequeue them.
409	 */
410	while (af6 != (struct ip6asfrag *)q6 &&
411	       ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
412		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
413		if (i < af6->ip6af_frglen) {
414			af6->ip6af_frglen -= i;
415			af6->ip6af_off += i;
416			m_adj(IP6_REASS_MBUF(af6), i);
417			break;
418		}
419		af6 = af6->ip6af_down;
420		m_freem(IP6_REASS_MBUF(af6->ip6af_up));
421		frag6_deq(af6->ip6af_up);
422	}
423#else
424	/*
425	 * If the incoming framgent overlaps some existing fragments in
426	 * the reassembly queue, drop it, since it is dangerous to override
427	 * existing fragments from a security point of view.
428	 * We don't know which fragment is the bad guy - here we trust
429	 * fragment that came in earlier, with no real reason.
430	 */
431	if (af6->ip6af_up != (struct ip6asfrag *)q6) {
432		i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
433			- ip6af->ip6af_off;
434		if (i > 0) {
435#if 0				/* suppress the noisy log */
436			log(LOG_ERR, "%d bytes of a fragment from %s "
437			    "overlaps the previous fragment\n",
438			    i, ip6_sprintf(&q6->ip6q_src));
439#endif
440			free(ip6af, M_FTABLE);
441			goto dropfrag;
442		}
443	}
444	if (af6 != (struct ip6asfrag *)q6) {
445		i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
446		if (i > 0) {
447#if 0				/* suppress the noisy log */
448			log(LOG_ERR, "%d bytes of a fragment from %s "
449			    "overlaps the succeeding fragment",
450			    i, ip6_sprintf(&q6->ip6q_src));
451#endif
452			free(ip6af, M_FTABLE);
453			goto dropfrag;
454		}
455	}
456#endif
457
458insert:
459
460	/*
461	 * Stick new segment in its place;
462	 * check for complete reassembly.
463	 * Move to front of packet queue, as we are
464	 * the most recently active fragmented packet.
465	 */
466	frag6_enq(ip6af, af6->ip6af_up);
467	frag6_nfrags++;
468	q6->ip6q_nfrag++;
469#if 0 /* xxx */
470	if (q6 != ip6q.ip6q_next) {
471		frag6_remque(q6);
472		frag6_insque(q6, &ip6q);
473	}
474#endif
475	next = 0;
476	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
477	     af6 = af6->ip6af_down) {
478		if (af6->ip6af_off != next) {
479			IP6Q_UNLOCK();
480			return IPPROTO_DONE;
481		}
482		next += af6->ip6af_frglen;
483	}
484	if (af6->ip6af_up->ip6af_mff) {
485		IP6Q_UNLOCK();
486		return IPPROTO_DONE;
487	}
488
489	/*
490	 * Reassembly is complete; concatenate fragments.
491	 */
492	ip6af = q6->ip6q_down;
493	t = m = IP6_REASS_MBUF(ip6af);
494	af6 = ip6af->ip6af_down;
495	frag6_deq(ip6af);
496	while (af6 != (struct ip6asfrag *)q6) {
497		af6dwn = af6->ip6af_down;
498		frag6_deq(af6);
499		while (t->m_next)
500			t = t->m_next;
501		t->m_next = IP6_REASS_MBUF(af6);
502		m_adj(t->m_next, af6->ip6af_offset);
503		free(af6, M_FTABLE);
504		af6 = af6dwn;
505	}
506
507	/* adjust offset to point where the original next header starts */
508	offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
509	free(ip6af, M_FTABLE);
510	ip6 = mtod(m, struct ip6_hdr *);
511	ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
512	ip6->ip6_src = q6->ip6q_src;
513	ip6->ip6_dst = q6->ip6q_dst;
514	nxt = q6->ip6q_nxt;
515#ifdef notyet
516	*q6->ip6q_nxtp = (u_char)(nxt & 0xff);
517#endif
518
519	/*
520	 * Delete frag6 header with as a few cost as possible.
521	 */
522	if (offset < m->m_len) {
523		ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
524			offset);
525		m->m_data += sizeof(struct ip6_frag);
526		m->m_len -= sizeof(struct ip6_frag);
527	} else {
528		/* this comes with no copy if the boundary is on cluster */
529		if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
530			frag6_remque(q6);
531			frag6_nfrags -= q6->ip6q_nfrag;
532			free(q6, M_FTABLE);
533			frag6_nfragpackets--;
534			goto dropfrag;
535		}
536		m_adj(t, sizeof(struct ip6_frag));
537		m_cat(m, t);
538	}
539
540	/*
541	 * Store NXT to the original.
542	 */
543	{
544		char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
545		*prvnxtp = nxt;
546	}
547
548	frag6_remque(q6);
549	frag6_nfrags -= q6->ip6q_nfrag;
550	free(q6, M_FTABLE);
551	frag6_nfragpackets--;
552
553	if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
554		int plen = 0;
555		for (t = m; t; t = t->m_next)
556			plen += t->m_len;
557		m->m_pkthdr.len = plen;
558	}
559
560	ip6stat.ip6s_reassembled++;
561	in6_ifstat_inc(dstifp, ifs6_reass_ok);
562
563	/*
564	 * Tell launch routine the next header
565	 */
566
567	*mp = m;
568	*offp = offset;
569
570	IP6Q_UNLOCK();
571	return nxt;
572
573 dropfrag:
574	IP6Q_UNLOCK();
575	in6_ifstat_inc(dstifp, ifs6_reass_fail);
576	ip6stat.ip6s_fragdropped++;
577	m_freem(m);
578	return IPPROTO_DONE;
579}
580
581/*
582 * Free a fragment reassembly header and all
583 * associated datagrams.
584 */
585void
586frag6_freef(q6)
587	struct ip6q *q6;
588{
589	struct ip6asfrag *af6, *down6;
590
591	IP6Q_LOCK_ASSERT();
592
593	for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
594	     af6 = down6) {
595		struct mbuf *m = IP6_REASS_MBUF(af6);
596
597		down6 = af6->ip6af_down;
598		frag6_deq(af6);
599
600		/*
601		 * Return ICMP time exceeded error for the 1st fragment.
602		 * Just free other fragments.
603		 */
604		if (af6->ip6af_off == 0) {
605			struct ip6_hdr *ip6;
606
607			/* adjust pointer */
608			ip6 = mtod(m, struct ip6_hdr *);
609
610			/* restore source and destination addresses */
611			ip6->ip6_src = q6->ip6q_src;
612			ip6->ip6_dst = q6->ip6q_dst;
613
614			icmp6_error(m, ICMP6_TIME_EXCEEDED,
615				    ICMP6_TIME_EXCEED_REASSEMBLY, 0);
616		} else
617			m_freem(m);
618		free(af6, M_FTABLE);
619	}
620	frag6_remque(q6);
621	frag6_nfrags -= q6->ip6q_nfrag;
622	free(q6, M_FTABLE);
623	frag6_nfragpackets--;
624}
625
626/*
627 * Put an ip fragment on a reassembly chain.
628 * Like insque, but pointers in middle of structure.
629 */
630void
631frag6_enq(af6, up6)
632	struct ip6asfrag *af6, *up6;
633{
634
635	IP6Q_LOCK_ASSERT();
636
637	af6->ip6af_up = up6;
638	af6->ip6af_down = up6->ip6af_down;
639	up6->ip6af_down->ip6af_up = af6;
640	up6->ip6af_down = af6;
641}
642
643/*
644 * To frag6_enq as remque is to insque.
645 */
646void
647frag6_deq(af6)
648	struct ip6asfrag *af6;
649{
650
651	IP6Q_LOCK_ASSERT();
652
653	af6->ip6af_up->ip6af_down = af6->ip6af_down;
654	af6->ip6af_down->ip6af_up = af6->ip6af_up;
655}
656
657void
658frag6_insque(new, old)
659	struct ip6q *new, *old;
660{
661
662	IP6Q_LOCK_ASSERT();
663
664	new->ip6q_prev = old;
665	new->ip6q_next = old->ip6q_next;
666	old->ip6q_next->ip6q_prev= new;
667	old->ip6q_next = new;
668}
669
670void
671frag6_remque(p6)
672	struct ip6q *p6;
673{
674
675	IP6Q_LOCK_ASSERT();
676
677	p6->ip6q_prev->ip6q_next = p6->ip6q_next;
678	p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
679}
680
681/*
682 * IPv6 reassembling timer processing;
683 * if a timer expires on a reassembly
684 * queue, discard it.
685 */
686void
687frag6_slowtimo()
688{
689	struct ip6q *q6;
690
691	IP6Q_LOCK();
692	q6 = ip6q.ip6q_next;
693	if (q6)
694		while (q6 != &ip6q) {
695			--q6->ip6q_ttl;
696			q6 = q6->ip6q_next;
697			if (q6->ip6q_prev->ip6q_ttl == 0) {
698				ip6stat.ip6s_fragtimeout++;
699				/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
700				frag6_freef(q6->ip6q_prev);
701			}
702		}
703	/*
704	 * If we are over the maximum number of fragments
705	 * (due to the limit being lowered), drain off
706	 * enough to get down to the new limit.
707	 */
708	while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
709	    ip6q.ip6q_prev) {
710		ip6stat.ip6s_fragoverflow++;
711		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
712		frag6_freef(ip6q.ip6q_prev);
713	}
714	IP6Q_UNLOCK();
715
716#if 0
717	/*
718	 * Routing changes might produce a better route than we last used;
719	 * make sure we notice eventually, even if forwarding only for one
720	 * destination and the cache is never replaced.
721	 */
722	if (ip6_forward_rt.ro_rt) {
723		RTFREE(ip6_forward_rt.ro_rt);
724		ip6_forward_rt.ro_rt = 0;
725	}
726	if (ipsrcchk_rt.ro_rt) {
727		RTFREE(ipsrcchk_rt.ro_rt);
728		ipsrcchk_rt.ro_rt = 0;
729	}
730#endif
731}
732
733/*
734 * Drain off all datagram fragments.
735 */
736void
737frag6_drain()
738{
739
740	if (IP6Q_TRYLOCK() == 0)
741		return;
742	while (ip6q.ip6q_next != &ip6q) {
743		ip6stat.ip6s_fragdropped++;
744		/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
745		frag6_freef(ip6q.ip6q_next);
746	}
747	IP6Q_UNLOCK();
748}
749