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