frag6.c revision 1.3 1 /* $NetBSD: frag6.c,v 1.3 1999/07/03 21:30:17 thorpej 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
44 #include <net/if.h>
45 #include <net/route.h>
46
47 #include <netinet/in.h>
48 #include <netinet/in_var.h>
49 #include <netinet6/in6_systm.h>
50 #include <netinet6/ip6.h>
51 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
52 #include <netinet6/in6_pcb.h>
53 #endif
54 #include <netinet6/ip6_var.h>
55 #include <netinet6/icmp6.h>
56
57 static void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *));
58 static void frag6_deq __P((struct ip6asfrag *));
59 static void frag6_insque __P((struct ip6q *, struct ip6q *));
60 static void frag6_remque __P((struct ip6q *));
61 static void frag6_freef __P((struct ip6q *));
62
63 int frag6_doing_reass;
64 u_int frag6_nfragpackets;
65 struct ip6q ip6q; /* ip6 reassemble queue */
66
67 /*
68 * Initialise reassembly queue and fragment identifier.
69 */
70 void
71 frag6_init()
72 {
73 ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
74 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
75 ip6_id = time.tv_sec & 0xffff;
76 #else
77 ip6_id = time_second & 0xffff;
78 #endif
79 }
80
81 /*
82 * Fragment input
83 */
84 int
85 frag6_input(mp, offp, proto)
86 struct mbuf **mp;
87 int *offp, proto;
88 {
89 struct mbuf *m = *mp, *t;
90 struct ip6_hdr *ip6;
91 struct ip6_frag *ip6f;
92 struct ip6q *q6;
93 struct ip6asfrag *af6, *ip6af;
94 int offset = *offp, nxt, i, next;
95 int first_frag = 0;
96 u_short fragoff, frgpartlen;
97
98 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
99
100 ip6 = mtod(m, struct ip6_hdr *);
101 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
102
103 /* jumbo payload can't contain a fragment header */
104 if (ip6->ip6_plen == 0) {
105 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
106 return IPPROTO_DONE;
107 }
108
109 /*
110 * check whether fragment packet's fragment length is
111 * multiple of 8 octets.
112 * sizeof(struct ip6_frag) == 8
113 * sizeof(struct ip6_hdr) = 40
114 */
115 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
116 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
117 icmp6_error(m, ICMP6_PARAM_PROB,
118 ICMP6_PARAMPROB_HEADER,
119 (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
120 return IPPROTO_DONE;
121 }
122
123 ip6stat.ip6s_fragments++;
124
125 /*
126 * Presence of header sizes in mbufs
127 * would confuse code below.
128 */
129
130 offset += sizeof(struct ip6_frag);
131 m->m_data += offset;
132 m->m_len -= offset;
133
134 for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
135 if (ip6f->ip6f_ident == q6->ip6q_ident &&
136 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
137 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
138 break;
139
140 if (q6 == &ip6q) {
141 /*
142 * the first fragment to arrive, create a reassembly queue.
143 */
144 first_frag = 1;
145 frag6_nfragpackets++;
146
147 /*
148 * Enforce upper bound on number of fragmented packets
149 * for which we attempt reassembly;
150 * If maxfrag is 0, never accept fragments.
151 * If maxfrag is -1, accept all fragments without limitation.
152 */
153 if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets) {
154 ip6stat.ip6s_fragoverflow++;
155 frag6_freef(ip6q.ip6q_prev);
156 }
157 q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
158 M_DONTWAIT);
159 if (q6 == NULL)
160 goto dropfrag;
161
162 frag6_insque(q6, &ip6q);
163
164 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
165 #if 0
166 /*
167 * It is not necessarily the first segment; fragment offset
168 * might be non-0.
169 */
170 q6->ip6q_nxt = ip6f->ip6f_nxt;
171 #endif
172 #ifdef notyet
173 q6->ip6q_nxtp = (u_char *)nxtp;
174 #endif
175 q6->ip6q_ident = ip6f->ip6f_ident;
176 q6->ip6q_arrive = 0; /* Is it used anywhere? */
177 q6->ip6q_ttl = IPV6_FRAGTTL;
178 q6->ip6q_src = ip6->ip6_src;
179 q6->ip6q_dst = ip6->ip6_dst;
180 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
181 }
182
183 /*
184 * If it's the 1st fragment, record the length of the
185 * unfragmentable part and the next header of the fragment header.
186 */
187 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
188 if (fragoff == 0) {
189 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr)
190 - sizeof(struct ip6_frag);
191 q6->ip6q_nxt = ip6f->ip6f_nxt;
192 }
193
194 /*
195 * Check that the reassembled packet would not exceed 65535 bytes
196 * in size.
197 * If it would exceed, discard the fragment and return an ICMP error.
198 */
199 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
200 if (q6->ip6q_unfrglen >= 0) {
201 /* The 1st fragment has already arrived. */
202 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
203 m->m_data -= offset;
204 m->m_len += offset;
205 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
206 offset - sizeof(struct ip6_frag) + 2);
207 return(IPPROTO_DONE);
208 }
209 }
210 else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
211 m->m_data -= offset;
212 m->m_len += offset;
213 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
214 offset - sizeof(struct ip6_frag) + 2);
215 return(IPPROTO_DONE);
216 }
217 /*
218 * If it's the first fragment, do the above check for each
219 * fragment already stored in the reassembly queue.
220 */
221 if (fragoff == 0) {
222 struct ip6asfrag *af6dwn;
223
224 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
225 af6 = af6dwn) {
226 af6dwn = af6->ip6af_down;
227
228 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
229 IPV6_MAXPACKET) {
230 struct mbuf *merr = IP6_REASS_MBUF(af6);
231 struct ip6_hdr *ip6err;
232 int erroff = af6->ip6af_offset;
233
234 /* dequeue the fragment. */
235 frag6_deq(af6);
236
237 /* adjust pointer. */
238 merr->m_data -= af6->ip6af_offset;
239 merr->m_len += af6->ip6af_offset;
240 ip6err = mtod(merr, struct ip6_hdr *);
241
242 /*
243 * Restore source and destination addresses
244 * in the erroneous IPv6 header.
245 */
246 ip6err->ip6_src = q6->ip6q_src;
247 ip6err->ip6_dst = q6->ip6q_dst;
248
249 icmp6_error(merr, ICMP6_PARAM_PROB,
250 ICMP6_PARAMPROB_HEADER,
251 erroff - sizeof(struct ip6_frag) + 2);
252 }
253 }
254 }
255
256 /* Override the IPv6 header */
257 ip6af = (struct ip6asfrag *)ip6;
258 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
259 ip6af->ip6af_off = fragoff;
260 ip6af->ip6af_frglen = frgpartlen;
261 ip6af->ip6af_offset = offset;
262 IP6_REASS_MBUF(ip6af) = m;
263
264 if (first_frag) {
265 af6 = (struct ip6asfrag *)q6;
266 goto insert;
267 }
268
269 /*
270 * Find a segment which begins after this one does.
271 */
272 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
273 af6 = af6->ip6af_down)
274 if (af6->ip6af_off > ip6af->ip6af_off)
275 break;
276
277 #if 0
278 /*
279 * If there is a preceding segment, it may provide some of
280 * our data already. If so, drop the data from the incoming
281 * segment. If it provides all of our data, drop us.
282 */
283 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
284 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
285 - ip6af->ip6af_off;
286 if (i > 0) {
287 if (i >= ip6af->ip6af_frglen)
288 goto dropfrag;
289 m_adj(IP6_REASS_MBUF(ip6af), i);
290 ip6af->ip6af_off += i;
291 ip6af->ip6af_frglen -= i;
292 }
293 }
294
295 /*
296 * While we overlap succeeding segments trim them or,
297 * if they are completely covered, dequeue them.
298 */
299 while (af6 != (struct ip6asfrag *)q6 &&
300 ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
301 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
302 if (i < af6->ip6af_frglen) {
303 af6->ip6af_frglen -= i;
304 af6->ip6af_off += i;
305 m_adj(IP6_REASS_MBUF(af6), i);
306 break;
307 }
308 af6 = af6->ip6af_down;
309 m_freem(IP6_REASS_MBUF(af6->ip6af_up));
310 frag6_deq(af6->ip6af_up);
311 }
312 #else
313 /*
314 * If the incoming framgent overlaps some existing fragments in
315 * the reassembly queue, drop it, since it is dangerous to override
316 * existing fragments from a security point of view.
317 */
318 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
319 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
320 - ip6af->ip6af_off;
321 if (i > 0) {
322 log(LOG_ERR, "%d bytes of a fragment from %s "
323 "overlaps the previous fragment\n",
324 i, ip6_sprintf(&q6->ip6q_src));
325 goto dropfrag;
326 }
327 }
328 if (af6 != (struct ip6asfrag *)q6) {
329 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
330 if (i > 0) {
331 log(LOG_ERR, "%d bytes of a fragment from %s "
332 "overlaps the succeeding fragment",
333 i, ip6_sprintf(&q6->ip6q_src));
334 goto dropfrag;
335 }
336 }
337 #endif
338
339 insert:
340
341 /*
342 * Stick new segment in its place;
343 * check for complete reassembly.
344 * Move to front of packet queue, as we are
345 * the most recently active fragmented packet.
346 */
347 frag6_enq(ip6af, af6->ip6af_up);
348 #if 0 /* xxx */
349 if (q6 != ip6q.ip6q_next) {
350 frag6_remque(q6);
351 frag6_insque(q6, &ip6q);
352 }
353 #endif
354 next = 0;
355 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
356 af6 = af6->ip6af_down) {
357 if (af6->ip6af_off != next) {
358 frag6_doing_reass = 0;
359 return IPPROTO_DONE;
360 }
361 next += af6->ip6af_frglen;
362 }
363 if (af6->ip6af_up->ip6af_mff) {
364 frag6_doing_reass = 0;
365 return IPPROTO_DONE;
366 }
367
368 /*
369 * Reassembly is complete; concatenate fragments.
370 */
371
372 ip6af = q6->ip6q_down;
373 t = m = IP6_REASS_MBUF(ip6af);
374 af6 = ip6af->ip6af_down;
375 while (af6 != (struct ip6asfrag *)q6) {
376 while (t->m_next)
377 t = t->m_next;
378 t->m_next = IP6_REASS_MBUF(af6);
379 af6 = af6->ip6af_down;
380 }
381
382 /* adjust offset to point where the original next header starts */
383 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
384 ip6 = (struct ip6_hdr *)ip6af;
385 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
386 ip6->ip6_src = q6->ip6q_src;
387 ip6->ip6_dst = q6->ip6q_dst;
388 nxt = q6->ip6q_nxt;
389 #ifdef notyet
390 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
391 #endif
392
393 /*
394 * Delete frag6 header with as a few cost as possible.
395 */
396
397 if (offset < m->m_len)
398 bcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
399 offset);
400 else {
401 bcopy(mtod(m, caddr_t), (caddr_t)ip6 + offset, m->m_len);
402 m->m_data -= sizeof(struct ip6_frag);
403 }
404 m->m_data -= offset;
405 m->m_len += offset;
406
407 /*
408 * Store NXT to the original.
409 */
410 {
411 char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
412 *prvnxtp = nxt;
413 }
414
415 frag6_remque(q6);
416 free(q6, M_FTABLE);
417 frag6_nfragpackets--;
418
419 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
420 int plen = 0;
421 for (t = m; t; t = t->m_next)
422 plen += t->m_len;
423 m->m_pkthdr.len = plen;
424 }
425
426 ip6stat.ip6s_reassembled++;
427
428 /*
429 * Tell launch routine the next header
430 */
431
432 *mp = m;
433 *offp = offset;
434
435 frag6_doing_reass = 0;
436 return nxt;
437
438 dropfrag:
439 ip6stat.ip6s_fragdropped++;
440 m_freem(m);
441 return IPPROTO_DONE;
442 }
443
444 /*
445 * Free a fragment reassembly header and all
446 * associated datagrams.
447 */
448 void
449 frag6_freef(q6)
450 struct ip6q *q6;
451 {
452 struct ip6asfrag *af6, *down6;
453
454 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
455 af6 = down6) {
456 struct mbuf *m = IP6_REASS_MBUF(af6);
457
458 down6 = af6->ip6af_down;
459 frag6_deq(af6);
460
461 /*
462 * Return ICMP time exceeded error for the 1st fragment.
463 * Just free other fragments.
464 */
465 if (af6->ip6af_off == 0) {
466 struct ip6_hdr *ip6;
467
468 /* adjust pointer */
469 m->m_data -= af6->ip6af_offset;
470 m->m_len += af6->ip6af_offset;
471 ip6 = mtod(m, struct ip6_hdr *);
472
473 /* restoure source and destination addresses */
474 ip6->ip6_src = q6->ip6q_src;
475 ip6->ip6_dst = q6->ip6q_dst;
476
477 icmp6_error(m, ICMP6_TIME_EXCEEDED,
478 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
479 }
480 else
481 m_freem(m);
482 }
483 frag6_remque(q6);
484 free(q6, M_FTABLE);
485 frag6_nfragpackets--;
486 }
487
488 /*
489 * Put an ip fragment on a reassembly chain.
490 * Like insque, but pointers in middle of structure.
491 */
492 void
493 frag6_enq(af6, up6)
494 struct ip6asfrag *af6, *up6;
495 {
496 af6->ip6af_up = up6;
497 af6->ip6af_down = up6->ip6af_down;
498 up6->ip6af_down->ip6af_up = af6;
499 up6->ip6af_down = af6;
500 }
501
502 /*
503 * To frag6_enq as remque is to insque.
504 */
505 void
506 frag6_deq(af6)
507 struct ip6asfrag *af6;
508 {
509 af6->ip6af_up->ip6af_down = af6->ip6af_down;
510 af6->ip6af_down->ip6af_up = af6->ip6af_up;
511 }
512
513 void
514 frag6_insque(new, old)
515 struct ip6q *new, *old;
516 {
517 new->ip6q_prev = old;
518 new->ip6q_next = old->ip6q_next;
519 old->ip6q_next->ip6q_prev= new;
520 old->ip6q_next = new;
521 }
522
523 void
524 frag6_remque(p6)
525 struct ip6q *p6;
526 {
527 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
528 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
529 }
530
531 /*
532 * IP timer processing;
533 * if a timer expires on a reassembly
534 * queue, discard it.
535 */
536 void
537 frag6_slowtimo()
538 {
539 struct ip6q *q6;
540 int s = splnet();
541 #if 0
542 extern struct route_in6 ip6_forward_rt;
543 #endif
544
545 frag6_doing_reass = 1;
546 q6 = ip6q.ip6q_next;
547 if (q6)
548 while (q6 != &ip6q) {
549 --q6->ip6q_ttl;
550 q6 = q6->ip6q_next;
551 if (q6->ip6q_prev->ip6q_ttl == 0) {
552 ip6stat.ip6s_fragtimeout++;
553 frag6_freef(q6->ip6q_prev);
554 }
555 }
556 /*
557 * If we are over the maximum number of fragments
558 * (due to the limit being lowered), drain off
559 * enough to get down to the new limit.
560 */
561 while (frag6_nfragpackets > (u_int)ip6_maxfragpackets) {
562 ip6stat.ip6s_fragoverflow++;
563 frag6_freef(ip6q.ip6q_prev);
564 }
565 frag6_doing_reass = 0;
566
567 #if 0
568 /*
569 * Routing changes might produce a better route than we last used;
570 * make sure we notice eventually, even if forwarding only for one
571 * destination and the cache is never replaced.
572 */
573 if (ip6_forward_rt.ro_rt) {
574 RTFREE(ip6_forward_rt.ro_rt);
575 ip6_forward_rt.ro_rt = 0;
576 }
577 if (ipsrcchk_rt.ro_rt) {
578 RTFREE(ipsrcchk_rt.ro_rt);
579 ipsrcchk_rt.ro_rt = 0;
580 }
581 #endif
582
583 splx(s);
584 }
585
586 /*
587 * Drain off all datagram fragments.
588 */
589 void
590 frag6_drain()
591 {
592 if (frag6_doing_reass)
593 return;
594 while (ip6q.ip6q_next != &ip6q) {
595 ip6stat.ip6s_fragdropped++;
596 frag6_freef(ip6q.ip6q_next);
597 }
598 }
599