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