]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* $KAME: frag6.c,v 1.23 2000/02/28 16:18:11 itojun Exp $ */ |
2 | ||
3 | /* | |
4 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
5 | * All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3. Neither the name of the project nor the names of its contributors | |
16 | * may be used to endorse or promote products derived from this software | |
17 | * without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | #include <sys/param.h> | |
33 | #include <sys/systm.h> | |
34 | #include <sys/malloc.h> | |
35 | #include <sys/mbuf.h> | |
36 | #include <sys/domain.h> | |
37 | #include <sys/protosw.h> | |
38 | #include <sys/socket.h> | |
39 | #include <sys/errno.h> | |
40 | #include <sys/time.h> | |
41 | #include <sys/kernel.h> | |
42 | #include <sys/syslog.h> | |
43 | #include <kern/queue.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 | #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__OpenBSD__) && !(defined(__bsdi__) && _BSDI_VERSION >= 199802) && !defined(__APPLE__) | |
53 | #include <netinet6/in6_pcb.h> | |
54 | #endif | |
55 | #include <netinet/icmp6.h> | |
56 | ||
57 | #include <net/net_osdep.h> | |
58 | ||
59 | /* | |
60 | * Define it to get a correct behavior on per-interface statistics. | |
61 | * You will need to perform an extra routing table lookup, per fragment, | |
62 | * to do it. This may, or may not be, a performance hit. | |
63 | */ | |
64 | #define IN6_IFSTAT_STRICT | |
65 | ||
66 | static void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *)); | |
67 | static void frag6_deq __P((struct ip6asfrag *)); | |
68 | static void frag6_insque __P((struct ip6q *, struct ip6q *)); | |
69 | static void frag6_remque __P((struct ip6q *)); | |
70 | static void frag6_freef __P((struct ip6q *)); | |
71 | ||
72 | int frag6_doing_reass; | |
73 | u_int frag6_nfragpackets; | |
74 | struct ip6q ip6q; /* ip6 reassemble queue */ | |
75 | ||
76 | /* FreeBSD tweak */ | |
77 | #if !defined(M_FTABLE) && (defined(__FreeBSD__) && __FreeBSD__ >= 3) | |
78 | MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header"); | |
79 | #endif | |
80 | ||
81 | #ifndef offsetof /* XXX */ | |
82 | #define offsetof(type, member) ((size_t)(&((type *)0)->member)) | |
83 | #endif | |
84 | ||
85 | /* | |
86 | * Initialise reassembly queue and fragment identifier. | |
87 | */ | |
88 | void | |
89 | frag6_init() | |
90 | { | |
91 | struct timeval tv; | |
92 | ||
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); | |
98 | ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q; | |
99 | ip6_id = random() ^ tv.tv_usec; | |
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 | */ | |
134 | int | |
135 | frag6_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 | int offset = *offp, nxt, i, next; | |
145 | int first_frag = 0; | |
146 | int fragoff, frgpartlen; /* must be larger than u_int16_t */ | |
147 | struct ifnet *dstifp; | |
148 | #ifdef IN6_IFSTAT_STRICT | |
149 | static struct route_in6 ro; | |
150 | struct sockaddr_in6 *dst; | |
151 | #endif | |
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 | dst = (struct sockaddr_in6 *)&ro.ro_dst; | |
167 | if (ro.ro_rt | |
168 | && ((ro.ro_rt->rt_flags & RTF_UP) == 0 | |
169 | || !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) { | |
170 | RTFREE(ro.ro_rt); | |
171 | ro.ro_rt = (struct rtentry *)0; | |
172 | } | |
173 | if (ro.ro_rt == NULL) { | |
174 | bzero(dst, sizeof(*dst)); | |
175 | dst->sin6_family = AF_INET6; | |
176 | dst->sin6_len = sizeof(struct sockaddr_in6); | |
177 | dst->sin6_addr = ip6->ip6_dst; | |
178 | } | |
179 | #ifndef __bsdi__ | |
180 | rtalloc((struct route *)&ro); | |
181 | #else | |
182 | rtcalloc((struct route *)&ro); | |
183 | #endif | |
184 | if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL) | |
185 | dstifp = ((struct in6_ifaddr *)ro.ro_rt->rt_ifa)->ia_ifp; | |
186 | #else | |
187 | /* we are violating the spec, this is not the destination interface */ | |
188 | if ((m->m_flags & M_PKTHDR) != 0) | |
189 | dstifp = m->m_pkthdr.rcvif; | |
190 | #endif | |
191 | ||
192 | /* jumbo payload can't contain a fragment header */ | |
193 | if (ip6->ip6_plen == 0) { | |
194 | icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset); | |
195 | in6_ifstat_inc(dstifp, ifs6_reass_fail); | |
196 | return IPPROTO_DONE; | |
197 | } | |
198 | ||
199 | /* | |
200 | * check whether fragment packet's fragment length is | |
201 | * multiple of 8 octets. | |
202 | * sizeof(struct ip6_frag) == 8 | |
203 | * sizeof(struct ip6_hdr) = 40 | |
204 | */ | |
205 | if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) && | |
206 | (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) { | |
207 | icmp6_error(m, ICMP6_PARAM_PROB, | |
208 | ICMP6_PARAMPROB_HEADER, | |
209 | offsetof(struct ip6_hdr, ip6_plen)); | |
210 | in6_ifstat_inc(dstifp, ifs6_reass_fail); | |
211 | return IPPROTO_DONE; | |
212 | } | |
213 | ||
214 | ip6stat.ip6s_fragments++; | |
215 | in6_ifstat_inc(dstifp, ifs6_reass_reqd); | |
216 | ||
217 | /* offset now points to data portion */ | |
218 | offset += sizeof(struct ip6_frag); | |
219 | ||
220 | for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next) | |
221 | if (ip6f->ip6f_ident == q6->ip6q_ident && | |
222 | IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) && | |
223 | IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)) | |
224 | break; | |
225 | ||
226 | if (q6 == &ip6q) { | |
227 | /* | |
228 | * the first fragment to arrive, create a reassembly queue. | |
229 | */ | |
230 | first_frag = 1; | |
231 | frag6_nfragpackets++; | |
232 | ||
233 | /* | |
234 | * Enforce upper bound on number of fragmented packets | |
235 | * for which we attempt reassembly; | |
236 | * If maxfrag is 0, never accept fragments. | |
237 | * If maxfrag is -1, accept all fragments without limitation. | |
238 | */ | |
239 | if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets) { | |
240 | ip6stat.ip6s_fragoverflow++; | |
241 | in6_ifstat_inc(dstifp, ifs6_reass_fail); | |
242 | frag6_freef(ip6q.ip6q_prev); | |
243 | } | |
244 | q6 = (struct ip6q *)_MALLOC(sizeof(struct ip6q), M_FTABLE, | |
245 | M_DONTWAIT); | |
246 | if (q6 == NULL) | |
247 | goto dropfrag; | |
248 | bzero(q6, sizeof(*q6)); | |
249 | ||
250 | frag6_insque(q6, &ip6q); | |
251 | ||
252 | /* ip6q_nxt will be filled afterwards, from 1st fragment */ | |
253 | q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6; | |
254 | #if notyet | |
255 | q6->ip6q_nxtp = (u_char *)nxtp; | |
256 | #endif | |
257 | q6->ip6q_ident = ip6f->ip6f_ident; | |
258 | q6->ip6q_arrive = 0; /* Is it used anywhere? */ | |
259 | q6->ip6q_ttl = IPV6_FRAGTTL; | |
260 | q6->ip6q_src = ip6->ip6_src; | |
261 | q6->ip6q_dst = ip6->ip6_dst; | |
262 | q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */ | |
263 | } | |
264 | ||
265 | /* | |
266 | * If it's the 1st fragment, record the length of the | |
267 | * unfragmentable part and the next header of the fragment header. | |
268 | */ | |
269 | fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK); | |
270 | if (fragoff == 0) { | |
271 | q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) | |
272 | - sizeof(struct ip6_frag); | |
273 | q6->ip6q_nxt = ip6f->ip6f_nxt; | |
274 | } | |
275 | ||
276 | /* | |
277 | * Check that the reassembled packet would not exceed 65535 bytes | |
278 | * in size. | |
279 | * If it would exceed, discard the fragment and return an ICMP error. | |
280 | */ | |
281 | frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset; | |
282 | if (q6->ip6q_unfrglen >= 0) { | |
283 | /* The 1st fragment has already arrived. */ | |
284 | if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) { | |
285 | icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, | |
286 | offset - sizeof(struct ip6_frag) + | |
287 | offsetof(struct ip6_frag, ip6f_offlg)); | |
288 | return(IPPROTO_DONE); | |
289 | } | |
290 | } | |
291 | else if (fragoff + frgpartlen > IPV6_MAXPACKET) { | |
292 | icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, | |
293 | offset - sizeof(struct ip6_frag) + | |
294 | offsetof(struct ip6_frag, ip6f_offlg)); | |
295 | return(IPPROTO_DONE); | |
296 | } | |
297 | /* | |
298 | * If it's the first fragment, do the above check for each | |
299 | * fragment already stored in the reassembly queue. | |
300 | */ | |
301 | if (fragoff == 0) { | |
302 | for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; | |
303 | af6 = af6dwn) { | |
304 | af6dwn = af6->ip6af_down; | |
305 | ||
306 | if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen > | |
307 | IPV6_MAXPACKET) { | |
308 | struct mbuf *merr = IP6_REASS_MBUF(af6); | |
309 | struct ip6_hdr *ip6err; | |
310 | int erroff = af6->ip6af_offset; | |
311 | ||
312 | /* dequeue the fragment. */ | |
313 | frag6_deq(af6); | |
314 | _FREE(af6, M_FTABLE); | |
315 | ||
316 | /* adjust pointer. */ | |
317 | ip6err = mtod(merr, struct ip6_hdr *); | |
318 | ||
319 | /* | |
320 | * Restore source and destination addresses | |
321 | * in the erroneous IPv6 header. | |
322 | */ | |
323 | ip6err->ip6_src = q6->ip6q_src; | |
324 | ip6err->ip6_dst = q6->ip6q_dst; | |
325 | ||
326 | icmp6_error(merr, ICMP6_PARAM_PROB, | |
327 | ICMP6_PARAMPROB_HEADER, | |
328 | erroff - sizeof(struct ip6_frag) + | |
329 | offsetof(struct ip6_frag, ip6f_offlg)); | |
330 | } | |
331 | } | |
332 | } | |
333 | ||
334 | ip6af = (struct ip6asfrag *)_MALLOC(sizeof(struct ip6asfrag), M_FTABLE, | |
335 | M_DONTWAIT); | |
336 | if (ip6af == NULL) | |
337 | goto dropfrag; | |
338 | bzero(ip6af, sizeof(*ip6af)); | |
339 | ip6af->ip6af_head = ip6->ip6_flow; | |
340 | ip6af->ip6af_len = ip6->ip6_plen; | |
341 | ip6af->ip6af_nxt = ip6->ip6_nxt; | |
342 | ip6af->ip6af_hlim = ip6->ip6_hlim; | |
343 | ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG; | |
344 | ip6af->ip6af_off = fragoff; | |
345 | ip6af->ip6af_frglen = frgpartlen; | |
346 | ip6af->ip6af_offset = offset; | |
347 | IP6_REASS_MBUF(ip6af) = m; | |
348 | ||
349 | if (first_frag) { | |
350 | af6 = (struct ip6asfrag *)q6; | |
351 | goto insert; | |
352 | } | |
353 | ||
354 | /* | |
355 | * Find a segment which begins after this one does. | |
356 | */ | |
357 | for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; | |
358 | af6 = af6->ip6af_down) | |
359 | if (af6->ip6af_off > ip6af->ip6af_off) | |
360 | break; | |
361 | ||
362 | #if 0 | |
363 | /* | |
364 | * If there is a preceding segment, it may provide some of | |
365 | * our data already. If so, drop the data from the incoming | |
366 | * segment. If it provides all of our data, drop us. | |
367 | */ | |
368 | if (af6->ip6af_up != (struct ip6asfrag *)q6) { | |
369 | i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen | |
370 | - ip6af->ip6af_off; | |
371 | if (i > 0) { | |
372 | if (i >= ip6af->ip6af_frglen) | |
373 | goto dropfrag; | |
374 | m_adj(IP6_REASS_MBUF(ip6af), i); | |
375 | ip6af->ip6af_off += i; | |
376 | ip6af->ip6af_frglen -= i; | |
377 | } | |
378 | } | |
379 | ||
380 | /* | |
381 | * While we overlap succeeding segments trim them or, | |
382 | * if they are completely covered, dequeue them. | |
383 | */ | |
384 | while (af6 != (struct ip6asfrag *)q6 && | |
385 | ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) { | |
386 | i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; | |
387 | if (i < af6->ip6af_frglen) { | |
388 | af6->ip6af_frglen -= i; | |
389 | af6->ip6af_off += i; | |
390 | m_adj(IP6_REASS_MBUF(af6), i); | |
391 | break; | |
392 | } | |
393 | af6 = af6->ip6af_down; | |
394 | m_freem(IP6_REASS_MBUF(af6->ip6af_up)); | |
395 | frag6_deq(af6->ip6af_up); | |
396 | } | |
397 | #else | |
398 | /* | |
399 | * If the incoming framgent overlaps some existing fragments in | |
400 | * the reassembly queue, drop it, since it is dangerous to override | |
401 | * existing fragments from a security point of view. | |
402 | */ | |
403 | if (af6->ip6af_up != (struct ip6asfrag *)q6) { | |
404 | i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen | |
405 | - ip6af->ip6af_off; | |
406 | if (i > 0) { | |
407 | log(LOG_ERR, "%d bytes of a fragment from %s " | |
408 | "overlaps the previous fragment\n", | |
409 | i, ip6_sprintf(&q6->ip6q_src)); | |
410 | goto dropfrag; | |
411 | } | |
412 | } | |
413 | if (af6 != (struct ip6asfrag *)q6) { | |
414 | i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; | |
415 | if (i > 0) { | |
416 | log(LOG_ERR, "%d bytes of a fragment from %s " | |
417 | "overlaps the succeeding fragment", | |
418 | i, ip6_sprintf(&q6->ip6q_src)); | |
419 | goto dropfrag; | |
420 | } | |
421 | } | |
422 | #endif | |
423 | ||
424 | insert: | |
425 | ||
426 | /* | |
427 | * Stick new segment in its place; | |
428 | * check for complete reassembly. | |
429 | * Move to front of packet queue, as we are | |
430 | * the most recently active fragmented packet. | |
431 | */ | |
432 | frag6_enq(ip6af, af6->ip6af_up); | |
433 | #if 0 /* xxx */ | |
434 | if (q6 != ip6q.ip6q_next) { | |
435 | frag6_remque(q6); | |
436 | frag6_insque(q6, &ip6q); | |
437 | } | |
438 | #endif | |
439 | next = 0; | |
440 | for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; | |
441 | af6 = af6->ip6af_down) { | |
442 | if (af6->ip6af_off != next) { | |
443 | frag6_doing_reass = 0; | |
444 | return IPPROTO_DONE; | |
445 | } | |
446 | next += af6->ip6af_frglen; | |
447 | } | |
448 | if (af6->ip6af_up->ip6af_mff) { | |
449 | frag6_doing_reass = 0; | |
450 | return IPPROTO_DONE; | |
451 | } | |
452 | ||
453 | /* | |
454 | * Reassembly is complete; concatenate fragments. | |
455 | */ | |
456 | ip6af = q6->ip6q_down; | |
457 | t = m = IP6_REASS_MBUF(ip6af); | |
458 | af6 = ip6af->ip6af_down; | |
459 | frag6_deq(ip6af); | |
460 | while (af6 != (struct ip6asfrag *)q6) { | |
461 | af6dwn = af6->ip6af_down; | |
462 | frag6_deq(af6); | |
463 | while (t->m_next) | |
464 | t = t->m_next; | |
465 | t->m_next = IP6_REASS_MBUF(af6); | |
466 | m_adj(t->m_next, af6->ip6af_offset); | |
467 | _FREE(af6, M_FTABLE); | |
468 | af6 = af6dwn; | |
469 | } | |
470 | ||
471 | /* adjust offset to point where the original next header starts */ | |
472 | offset = ip6af->ip6af_offset - sizeof(struct ip6_frag); | |
473 | _FREE(ip6af, M_FTABLE); | |
474 | ip6 = mtod(m, struct ip6_hdr *); | |
475 | ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr)); | |
476 | ip6->ip6_src = q6->ip6q_src; | |
477 | ip6->ip6_dst = q6->ip6q_dst; | |
478 | nxt = q6->ip6q_nxt; | |
479 | #if notyet | |
480 | *q6->ip6q_nxtp = (u_char)(nxt & 0xff); | |
481 | #endif | |
482 | ||
483 | /* | |
484 | * Delete frag6 header with as a few cost as possible. | |
485 | */ | |
486 | if (offset < m->m_len) { | |
487 | ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag), | |
488 | offset); | |
489 | m->m_data += sizeof(struct ip6_frag); | |
490 | m->m_len -= sizeof(struct ip6_frag); | |
491 | } else { | |
492 | /* this comes with no copy if the boundary is on cluster */ | |
493 | if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) { | |
494 | frag6_remque(q6); | |
495 | _FREE(q6, M_FTABLE); | |
496 | frag6_nfragpackets--; | |
497 | goto dropfrag; | |
498 | } | |
499 | m_adj(t, sizeof(struct ip6_frag)); | |
500 | m_cat(m, t); | |
501 | } | |
502 | ||
503 | /* | |
504 | * Store NXT to the original. | |
505 | */ | |
506 | { | |
507 | char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */ | |
508 | *prvnxtp = nxt; | |
509 | } | |
510 | ||
511 | frag6_remque(q6); | |
512 | _FREE(q6, M_FTABLE); | |
513 | frag6_nfragpackets--; | |
514 | ||
515 | if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */ | |
516 | int plen = 0; | |
517 | for (t = m; t; t = t->m_next) | |
518 | plen += t->m_len; | |
519 | m->m_pkthdr.len = plen; | |
520 | } | |
521 | ||
522 | ip6stat.ip6s_reassembled++; | |
523 | in6_ifstat_inc(dstifp, ifs6_reass_ok); | |
524 | ||
525 | /* | |
526 | * Tell launch routine the next header | |
527 | */ | |
528 | ||
529 | *mp = m; | |
530 | *offp = offset; | |
531 | ||
532 | frag6_doing_reass = 0; | |
533 | return nxt; | |
534 | ||
535 | dropfrag: | |
536 | in6_ifstat_inc(dstifp, ifs6_reass_fail); | |
537 | ip6stat.ip6s_fragdropped++; | |
538 | m_freem(m); | |
539 | return IPPROTO_DONE; | |
540 | } | |
541 | ||
542 | /* | |
543 | * Free a fragment reassembly header and all | |
544 | * associated datagrams. | |
545 | */ | |
546 | void | |
547 | frag6_freef(q6) | |
548 | struct ip6q *q6; | |
549 | { | |
550 | struct ip6asfrag *af6, *down6; | |
551 | ||
552 | for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; | |
553 | af6 = down6) { | |
554 | struct mbuf *m = IP6_REASS_MBUF(af6); | |
555 | ||
556 | down6 = af6->ip6af_down; | |
557 | frag6_deq(af6); | |
558 | ||
559 | /* | |
560 | * Return ICMP time exceeded error for the 1st fragment. | |
561 | * Just free other fragments. | |
562 | */ | |
563 | if (af6->ip6af_off == 0) { | |
564 | struct ip6_hdr *ip6; | |
565 | ||
566 | /* adjust pointer */ | |
567 | ip6 = mtod(m, struct ip6_hdr *); | |
568 | ||
569 | /* restoure source and destination addresses */ | |
570 | ip6->ip6_src = q6->ip6q_src; | |
571 | ip6->ip6_dst = q6->ip6q_dst; | |
572 | ||
573 | icmp6_error(m, ICMP6_TIME_EXCEEDED, | |
574 | ICMP6_TIME_EXCEED_REASSEMBLY, 0); | |
575 | } else | |
576 | m_freem(m); | |
577 | _FREE(af6, M_FTABLE); | |
578 | ||
579 | } | |
580 | frag6_remque(q6); | |
581 | _FREE(q6, M_FTABLE); | |
582 | frag6_nfragpackets--; | |
583 | } | |
584 | ||
585 | /* | |
586 | * Put an ip fragment on a reassembly chain. | |
587 | * Like insque, but pointers in middle of structure. | |
588 | */ | |
589 | void | |
590 | frag6_enq(af6, up6) | |
591 | struct ip6asfrag *af6, *up6; | |
592 | { | |
593 | af6->ip6af_up = up6; | |
594 | af6->ip6af_down = up6->ip6af_down; | |
595 | up6->ip6af_down->ip6af_up = af6; | |
596 | up6->ip6af_down = af6; | |
597 | } | |
598 | ||
599 | /* | |
600 | * To frag6_enq as remque is to insque. | |
601 | */ | |
602 | void | |
603 | frag6_deq(af6) | |
604 | struct ip6asfrag *af6; | |
605 | { | |
606 | af6->ip6af_up->ip6af_down = af6->ip6af_down; | |
607 | af6->ip6af_down->ip6af_up = af6->ip6af_up; | |
608 | } | |
609 | ||
610 | void | |
611 | frag6_insque(new, old) | |
612 | struct ip6q *new, *old; | |
613 | { | |
614 | new->ip6q_prev = old; | |
615 | new->ip6q_next = old->ip6q_next; | |
616 | old->ip6q_next->ip6q_prev= new; | |
617 | old->ip6q_next = new; | |
618 | } | |
619 | ||
620 | void | |
621 | frag6_remque(p6) | |
622 | struct ip6q *p6; | |
623 | { | |
624 | p6->ip6q_prev->ip6q_next = p6->ip6q_next; | |
625 | p6->ip6q_next->ip6q_prev = p6->ip6q_prev; | |
626 | } | |
627 | ||
628 | /* | |
629 | * IP timer processing; | |
630 | * if a timer expires on a reassembly | |
631 | * queue, discard it. | |
632 | */ | |
633 | void | |
634 | frag6_slowtimo() | |
635 | { | |
636 | struct ip6q *q6; | |
637 | int s; | |
1c79356b A |
638 | #ifdef __NetBSD__ |
639 | s = splsoftnet(); | |
640 | #else | |
641 | s = splnet(); | |
642 | #endif | |
643 | #if 0 | |
644 | extern struct route_in6 ip6_forward_rt; | |
645 | #endif | |
646 | ||
647 | frag6_doing_reass = 1; | |
648 | q6 = ip6q.ip6q_next; | |
649 | if (q6) | |
650 | while (q6 != &ip6q) { | |
651 | --q6->ip6q_ttl; | |
652 | q6 = q6->ip6q_next; | |
653 | if (q6->ip6q_prev->ip6q_ttl == 0) { | |
654 | ip6stat.ip6s_fragtimeout++; | |
655 | /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ | |
656 | frag6_freef(q6->ip6q_prev); | |
657 | } | |
658 | } | |
659 | /* | |
660 | * If we are over the maximum number of fragments | |
661 | * (due to the limit being lowered), drain off | |
662 | * enough to get down to the new limit. | |
663 | */ | |
664 | while (frag6_nfragpackets > (u_int)ip6_maxfragpackets) { | |
665 | ip6stat.ip6s_fragoverflow++; | |
666 | /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ | |
667 | frag6_freef(ip6q.ip6q_prev); | |
668 | } | |
669 | frag6_doing_reass = 0; | |
670 | ||
671 | #if 0 | |
672 | /* | |
673 | * Routing changes might produce a better route than we last used; | |
674 | * make sure we notice eventually, even if forwarding only for one | |
675 | * destination and the cache is never replaced. | |
676 | */ | |
677 | if (ip6_forward_rt.ro_rt) { | |
678 | RTFREE(ip6_forward_rt.ro_rt); | |
679 | ip6_forward_rt.ro_rt = 0; | |
680 | } | |
681 | if (ipsrcchk_rt.ro_rt) { | |
682 | RTFREE(ipsrcchk_rt.ro_rt); | |
683 | ipsrcchk_rt.ro_rt = 0; | |
684 | } | |
685 | #endif | |
686 | ||
687 | splx(s); | |
1c79356b A |
688 | } |
689 | ||
690 | /* | |
691 | * Drain off all datagram fragments. | |
692 | */ | |
693 | void | |
694 | frag6_drain() | |
695 | { | |
696 | if (frag6_doing_reass) | |
697 | return; | |
698 | while (ip6q.ip6q_next != &ip6q) { | |
699 | ip6stat.ip6s_fragdropped++; | |
700 | /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ | |
701 | frag6_freef(ip6q.ip6q_next); | |
702 | } | |
703 | } |