VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/ip_input.c@ 14243

Last change on this file since 14243 was 14243, checked in by vboxsync, 16 years ago

IP fragmentation seems works fine
todo: fix the checksum calculation

  • Property svn:eol-style set to native
File size: 27.1 KB
Line 
1/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
34 * ip_input.c,v 1.11 1994/11/16 10:17:08 jkh Exp
35 */
36
37/*
38 * Changes and additions relating to SLiRP are
39 * Copyright (c) 1995 Danny Gasparovski.
40 *
41 * Please read the file COPYRIGHT for the
42 * terms and conditions of the copyright.
43 */
44
45#include <slirp.h>
46#include "ip_icmp.h"
47
48
49/*
50 * IP initialization: fill in IP protocol switch table.
51 * All protocols not implemented in kernel go to raw IP protocol handler.
52 */
53void
54ip_init(PNATState pData)
55{
56#ifndef VBOX_WITH_BSD_REASS
57 ipq.next = ipq.prev = ptr_to_u32(pData, &ipq);
58#else /* !VBOX_WITH_BSD_REASS */
59 int i = 0;
60 for (i = 0; i < IPREASS_NHASH; ++i)
61 TAILQ_INIT(&ipq[i]);
62 maxnipq = 100; /* ??? */
63 maxfragsperpacket = 16;
64 nipq = 0;
65#endif /* VBOX_WITH_BSD_REASS */
66 ip_currid = tt.tv_sec & 0xffff;
67 udp_init(pData);
68 tcp_init(pData);
69}
70
71/*
72 * Ip input routine. Checksum and byte swap header. If fragmented
73 * try to reassemble. Process options. Pass to next level.
74 */
75void
76ip_input(PNATState pData, struct mbuf *m)
77{
78 register struct ip *ip;
79 int hlen;
80
81 DEBUG_CALL("ip_input");
82 DEBUG_ARG("m = %lx", (long)m);
83 DEBUG_ARG("m_len = %d", m->m_len);
84
85 ipstat.ips_total++;
86
87 if (m->m_len < sizeof (struct ip)) {
88 ipstat.ips_toosmall++;
89 return;
90 }
91
92 ip = mtod(m, struct ip *);
93
94 if (ip->ip_v != IPVERSION) {
95 ipstat.ips_badvers++;
96 goto bad;
97 }
98
99 hlen = ip->ip_hl << 2;
100 if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
101 ipstat.ips_badhlen++; /* or packet too short */
102 goto bad;
103 }
104
105 /* keep ip header intact for ICMP reply
106 * ip->ip_sum = cksum(m, hlen);
107 * if (ip->ip_sum) {
108 */
109 if(cksum(m,hlen)) {
110 ipstat.ips_badsum++;
111 goto bad;
112 }
113
114 /*
115 * Convert fields to host representation.
116 */
117 NTOHS(ip->ip_len);
118 if (ip->ip_len < hlen) {
119 ipstat.ips_badlen++;
120 goto bad;
121 }
122 NTOHS(ip->ip_id);
123 NTOHS(ip->ip_off);
124
125 /*
126 * Check that the amount of data in the buffers
127 * is as at least much as the IP header would have us expect.
128 * Trim mbufs if longer than we expect.
129 * Drop packet if shorter than we expect.
130 */
131 if (m->m_len < ip->ip_len) {
132 ipstat.ips_tooshort++;
133 goto bad;
134 }
135 /* Should drop packet if mbuf too long? hmmm... */
136 if (m->m_len > ip->ip_len)
137 m_adj(m, ip->ip_len - m->m_len);
138
139 /* check ip_ttl for a correct ICMP reply */
140 if(ip->ip_ttl==0 || ip->ip_ttl==1) {
141 icmp_error(pData, m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");
142 goto bad;
143 }
144
145 /*
146 * Process options and, if not destined for us,
147 * ship it on. ip_dooptions returns 1 when an
148 * error was detected (causing an icmp message
149 * to be sent and the original packet to be freed).
150 */
151/* We do no IP options */
152/* if (hlen > sizeof (struct ip) && ip_dooptions(m))
153 * goto next;
154 */
155 /*
156 * If offset or IP_MF are set, must reassemble.
157 * Otherwise, nothing need be done.
158 * (We could look in the reassembly queue to see
159 * if the packet was previously fragmented,
160 * but it's not worth the time; just let them time out.)
161 *
162 * XXX This should fail, don't fragment yet
163 */
164#ifndef VBOX_WITH_BSD_REASS
165 if (ip->ip_off &~ IP_DF) {
166 register struct ipq_t *fp;
167 /*
168 * Look for queue of fragments
169 * of this datagram.
170 */
171 for (fp = u32_to_ptr(pData, ipq.next, struct ipq_t *); fp != &ipq;
172 fp = u32_to_ptr(pData, fp->next, struct ipq_t *))
173 if (ip->ip_id == fp->ipq_id &&
174 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
175 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
176 ip->ip_p == fp->ipq_p)
177 goto found;
178 fp = 0;
179 found:
180
181 /*
182 * Adjust ip_len to not reflect header,
183 * set ip_mff if more fragments are expected,
184 * convert offset of this to bytes.
185 */
186 ip->ip_len -= hlen;
187 if (ip->ip_off & IP_MF)
188 ((struct ipasfrag *)ip)->ipf_mff |= 1;
189 else
190 ((struct ipasfrag *)ip)->ipf_mff &= ~1;
191
192 ip->ip_off <<= 3;
193
194 /*
195 * If datagram marked as having more fragments
196 * or if this is not the first fragment,
197 * attempt reassembly; if it succeeds, proceed.
198 */
199 if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) {
200 ipstat.ips_fragments++;
201 ip = ip_reass(pData, (struct ipasfrag *)ip, fp);
202 if (ip == 0)
203 return;
204 ipstat.ips_reassembled++;
205 m = dtom(pData, ip);
206 } else
207 if (fp)
208 ip_freef(pData, fp);
209
210 } else
211 ip->ip_len -= hlen;
212#else /* !VBOX_WITH_BSD_REASS */
213 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
214 m = ip_reass(pData, m);
215 if (m == NULL)
216 return;
217 ip = mtod(m, struct ip *);
218 hlen = ip->ip_len;
219 }
220 else
221 ip->ip_len -= hlen;
222#endif /* !VBOX_WITH_BSD_REASS */
223
224 /*
225 * Switch out to protocol's input routine.
226 */
227 ipstat.ips_delivered++;
228 switch (ip->ip_p) {
229 case IPPROTO_TCP:
230 tcp_input(pData, m, hlen, (struct socket *)NULL);
231 break;
232 case IPPROTO_UDP:
233 udp_input(pData, m, hlen);
234 break;
235 case IPPROTO_ICMP:
236 icmp_input(pData, m, hlen);
237 break;
238 default:
239 ipstat.ips_noproto++;
240 m_free(pData, m);
241 }
242 return;
243bad:
244 m_freem(pData, m);
245 return;
246}
247
248#ifndef VBOX_WITH_BSD_REASS
249/*
250 * Take incoming datagram fragment and try to
251 * reassemble it into whole datagram. If a chain for
252 * reassembly of this datagram already exists, then it
253 * is given as fp; otherwise have to make a chain.
254 */
255struct ip *
256ip_reass(PNATState pData, register struct ipasfrag *ip, register struct ipq_t *fp)
257{
258 register struct mbuf *m = dtom(pData, ip);
259 register struct ipasfrag *q;
260 int hlen = ip->ip_hl << 2;
261 int i, next;
262
263 DEBUG_CALL("ip_reass");
264 DEBUG_ARG("ip = %lx", (long)ip);
265 DEBUG_ARG("fp = %lx", (long)fp);
266 DEBUG_ARG("m = %lx", (long)m);
267
268 /*
269 * Presence of header sizes in mbufs
270 * would confuse code below.
271 * Fragment m_data is concatenated.
272 */
273 m->m_data += hlen;
274 m->m_len -= hlen;
275
276 /*
277 * If first fragment to arrive, create a reassembly queue.
278 */
279 if (fp == 0) {
280 struct mbuf *t;
281 if ((t = m_get(pData)) == NULL) goto dropfrag;
282 fp = mtod(t, struct ipq_t *);
283 insque_32(pData, fp, &ipq);
284 fp->ipq_ttl = IPFRAGTTL;
285 fp->ipq_p = ip->ip_p;
286 fp->ipq_id = ip->ip_id;
287 fp->ipq_next = fp->ipq_prev = ptr_to_u32(pData, (struct ipasfrag *)fp);
288 fp->ipq_src = ((struct ip *)ip)->ip_src;
289 fp->ipq_dst = ((struct ip *)ip)->ip_dst;
290 q = (struct ipasfrag *)fp;
291 goto insert;
292 }
293
294 /*
295 * Find a segment which begins after this one does.
296 */
297 for (q = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *); q != (struct ipasfrag *)fp;
298 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *))
299 if (q->ip_off > ip->ip_off)
300 break;
301
302 /*
303 * If there is a preceding segment, it may provide some of
304 * our data already. If so, drop the data from the incoming
305 * segment. If it provides all of our data, drop us.
306 */
307 if (u32_to_ptr(pData, q->ipf_prev, struct ipq_t *) != fp) {
308 i = (u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *))->ip_off +
309 (u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *))->ip_len - ip->ip_off;
310 if (i > 0) {
311 if (i >= ip->ip_len)
312 goto dropfrag;
313 m_adj(dtom(pData, ip), i);
314 ip->ip_off += i;
315 ip->ip_len -= i;
316 }
317 }
318
319 /*
320 * While we overlap succeeding segments trim them or,
321 * if they are completely covered, dequeue them.
322 */
323 while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
324 i = (ip->ip_off + ip->ip_len) - q->ip_off;
325 if (i < q->ip_len) {
326 q->ip_len -= i;
327 q->ip_off += i;
328 m_adj(dtom(pData, q), i);
329 break;
330 }
331 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *);
332 m_freem(pData, dtom(pData, u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *)));
333 ip_deq(pData, u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *));
334 }
335
336insert:
337 /*
338 * Stick new segment in its place;
339 * check for complete reassembly.
340 */
341 ip_enq(pData, ip, u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *));
342 next = 0;
343 for (q = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *); q != (struct ipasfrag *)fp;
344 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *)) {
345 if (q->ip_off != next)
346 return (0);
347 next += q->ip_len;
348 }
349 if (u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *)->ipf_mff & 1)
350 return (0);
351
352 /*
353 * Reassembly is complete; concatenate fragments.
354 */
355 q = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *);
356 m = dtom(pData, q);
357
358 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *);
359 while (q != (struct ipasfrag *)fp) {
360 struct mbuf *t;
361 t = dtom(pData, q);
362 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *);
363 m_cat(pData, m, t);
364 }
365
366 /*
367 * Create header for new ip packet by
368 * modifying header of first packet;
369 * dequeue and discard fragment reassembly header.
370 * Make header visible.
371 */
372 ip = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *);
373
374 /*
375 * If the fragments concatenated to an mbuf that's
376 * bigger than the total size of the fragment, then and
377 * m_ext buffer was alloced. But fp->ipq_next points to
378 * the old buffer (in the mbuf), so we must point ip
379 * into the new buffer.
380 */
381 if (m->m_flags & M_EXT) {
382 int delta;
383 delta = (char *)ip - m->m_dat;
384 ip = (struct ipasfrag *)(m->m_ext + delta);
385 }
386
387 /* DEBUG_ARG("ip = %lx", (long)ip);
388 * ip=(struct ipasfrag *)m->m_data; */
389
390 ip->ip_len = next;
391 ip->ipf_mff &= ~1;
392 ((struct ip *)ip)->ip_src = fp->ipq_src;
393 ((struct ip *)ip)->ip_dst = fp->ipq_dst;
394 remque_32(pData, fp);
395 (void) m_free(pData, dtom(pData, fp));
396 m = dtom(pData, ip);
397 m->m_len += (ip->ip_hl << 2);
398 m->m_data -= (ip->ip_hl << 2);
399
400 return ((struct ip *)ip);
401
402dropfrag:
403 ipstat.ips_fragdropped++;
404 m_freem(pData, m);
405 return (0);
406}
407
408/*
409 * Free a fragment reassembly header and all
410 * associated datagrams.
411 */
412void
413ip_freef(PNATState pData, struct ipq_t *fp)
414{
415 register struct ipasfrag *q, *p;
416
417 for (q = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *); q != (struct ipasfrag *)fp;
418 q = p) {
419 p = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *);
420 ip_deq(pData, q);
421 m_freem(pData, dtom(pData, q));
422 }
423 remque_32(pData, fp);
424 (void) m_free(pData, dtom(pData, fp));
425}
426#else /* !VBOX_WITH_BSD_REASS */
427struct mbuf *
428ip_reass(PNATState pData, struct mbuf* m) {
429 struct ip *ip;
430 struct mbuf *p, *q, *nq, *t;
431 struct ipq_t *fp = NULL;
432 struct ipqhead *head;
433 int i, hlen, next;
434 u_int8_t ecn, ecn0;
435 u_short hash;
436
437 /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
438 if (maxnipq == 0
439 || maxfragsperpacket == 0) {
440 ipstat.ips_fragments++;
441 ipstat.ips_fragdropped++;
442 m_freem(pData, m);
443 return (NULL);
444 }
445
446 ip = mtod(m, struct ip *);
447 hlen = ip->ip_hl << 2;
448
449 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
450 head = &ipq[hash];
451
452 /*
453 * Look for queue of fragments
454 * of this datagram.
455 */
456 TAILQ_FOREACH(fp, head, ipq_list)
457 if (ip->ip_id == fp->ipq_id &&
458 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
459 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
460 ip->ip_p == fp->ipq_p)
461 goto found;
462
463 fp = NULL;
464
465 /*
466 * Attempt to trim the number of allocated fragment queues if it
467 * exceeds the administrative limit.
468 */
469 if ((nipq > maxnipq) && (maxnipq > 0)) {
470 /*
471 * drop something from the tail of the current queue
472 * before proceeding further
473 */
474 struct ipq_t *q = TAILQ_LAST(head, ipqhead);
475 if (q == NULL) { /* gak */
476 for (i = 0; i < IPREASS_NHASH; i++) {
477 struct ipq_t *r = TAILQ_LAST(&ipq[i], ipqhead);
478 if (r) {
479 ipstat.ips_fragtimeout += r->ipq_nfrags;
480 ip_freef(pData, &ipq[i], r);
481 break;
482 }
483 }
484 } else {
485 ipstat.ips_fragtimeout += q->ipq_nfrags;
486 ip_freef(pData, head, q);
487 }
488 }
489
490found:
491 /*
492 * Adjust ip_len to not reflect header,
493 * convert offset of this to bytes.
494 */
495 ip->ip_len -= hlen;
496 if (ip->ip_off & IP_MF) {
497 /*
498 * Make sure that fragments have a data length
499 * that's a non-zero multiple of 8 bytes.
500 */
501 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
502 ipstat.ips_toosmall++; /* XXX */
503 goto dropfrag;
504 }
505 m->m_flags |= M_FRAG;
506 } else
507 m->m_flags &= ~M_FRAG;
508 ip->ip_off <<= 3;
509
510
511 /*
512 * Attempt reassembly; if it succeeds, proceed.
513 * ip_reass() will return a different mbuf.
514 */
515 ipstat.ips_fragments++;
516 m->m_data = (caddr_t)ip;
517
518 /* Previous ip_reass() started here. */
519 /*
520 * Presence of header sizes in mbufs
521 * would confuse code below.
522 */
523#if 0
524 m->m_data += hlen;
525 m->m_len -= hlen;
526#endif
527
528 /*
529 * If first fragment to arrive, create a reassembly queue.
530 */
531 if (fp == NULL) {
532 fp = malloc(sizeof(struct ipq_t));
533 if (fp == NULL)
534 goto dropfrag;
535 TAILQ_INSERT_HEAD(head, fp, ipq_list);
536 nipq++;
537 fp->ipq_nfrags = 1;
538 fp->ipq_ttl = IPFRAGTTL;
539 fp->ipq_p = ip->ip_p;
540 fp->ipq_id = ip->ip_id;
541 fp->ipq_src = ip->ip_src;
542 fp->ipq_dst = ip->ip_dst;
543 fp->ipq_frags = m;
544 m->m_nextpkt = NULL;
545 goto done;
546 } else {
547 fp->ipq_nfrags++;
548 }
549
550#define GETIP(m) ((struct ip*)((m)->m_data))
551
552
553 /*
554 * Find a segment which begins after this one does.
555 */
556 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
557 if (GETIP(q)->ip_off > ip->ip_off)
558 break;
559
560 /*
561 * If there is a preceding segment, it may provide some of
562 * our data already. If so, drop the data from the incoming
563 * segment. If it provides all of our data, drop us, otherwise
564 * stick new segment in the proper place.
565 *
566 * If some of the data is dropped from the the preceding
567 * segment, then it's checksum is invalidated.
568 */
569 if (p) {
570 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
571 if (i > 0) {
572 if (i >= ip->ip_len)
573 goto dropfrag;
574 m_adj(m, i);
575 ip->ip_off += i;
576 ip->ip_len -= i;
577 }
578 m->m_nextpkt = p->m_nextpkt;
579 p->m_nextpkt = m;
580 } else {
581 m->m_nextpkt = fp->ipq_frags;
582 fp->ipq_frags = m;
583 }
584
585 /*
586 * While we overlap succeeding segments trim them or,
587 * if they are completely covered, dequeue them.
588 */
589 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
590 q = nq) {
591 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
592 if (i < GETIP(q)->ip_len) {
593 GETIP(q)->ip_len -= i;
594 GETIP(q)->ip_off += i;
595 m_adj(q, i);
596 break;
597 }
598 nq = q->m_nextpkt;
599 m->m_nextpkt = nq;
600 ipstat.ips_fragdropped++;
601 fp->ipq_nfrags--;
602 m_freem(pData, q);
603 }
604
605 /*
606 * Check for complete reassembly and perform frag per packet
607 * limiting.
608 *
609 * Frag limiting is performed here so that the nth frag has
610 * a chance to complete the packet before we drop the packet.
611 * As a result, n+1 frags are actually allowed per packet, but
612 * only n will ever be stored. (n = maxfragsperpacket.)
613 *
614 */
615 next = 0;
616 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
617 if (GETIP(q)->ip_off != next) {
618 if (fp->ipq_nfrags > maxfragsperpacket) {
619 ipstat.ips_fragdropped += fp->ipq_nfrags;
620 ip_freef(pData, head, fp);
621 }
622 goto done;
623 }
624 next += GETIP(q)->ip_len;
625 }
626 /* Make sure the last packet didn't have the IP_MF flag */
627 if (p->m_flags & M_FRAG) {
628 if (fp->ipq_nfrags > maxfragsperpacket) {
629 ipstat.ips_fragdropped += fp->ipq_nfrags;
630 ip_freef(pData, head, fp);
631 }
632 goto done;
633 }
634
635 /*
636 * Reassembly is complete. Make sure the packet is a sane size.
637 */
638 q = fp->ipq_frags;
639 ip = GETIP(q);
640 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
641 ipstat.ips_fragdropped += fp->ipq_nfrags;
642 ip_freef(pData, head, fp);
643 goto done;
644 }
645
646 /*
647 * Concatenate fragments.
648 */
649 m = q;
650#if 0
651 t = m->m_next;
652 m->m_next = NULL;
653 m_cat(pData, m, t);
654#endif
655 nq = q->m_nextpkt;
656 q->m_nextpkt = NULL;
657 for (q = nq; q != NULL; q = nq) {
658 nq = q->m_nextpkt;
659 q->m_nextpkt = NULL;
660 m_cat(pData, m, q);
661 }
662
663 /*
664 * Create header for new ip packet by modifying header of first
665 * packet; dequeue and discard fragment reassembly header.
666 * Make header visible.
667 */
668 ip->ip_len = (ip->ip_hl << 2) + next;
669 ip->ip_src = fp->ipq_src;
670 ip->ip_dst = fp->ipq_dst;
671 TAILQ_REMOVE(head, fp, ipq_list);
672 nipq--;
673 free(fp);
674
675 m->m_len += (ip->ip_hl << 2);
676#if 0
677 m->m_data -= (ip->ip_hl << 2);
678#endif
679 /* some debugging cruft by sklower, below, will go away soon */
680#if 0
681 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */
682 m_fixhdr(m);
683#endif
684 ipstat.ips_reassembled++;
685 return (m);
686
687dropfrag:
688 ipstat.ips_fragdropped++;
689 if (fp != NULL)
690 fp->ipq_nfrags--;
691 m_freem(pData, m);
692done:
693 return (NULL);
694
695#undef GETIP
696}
697
698void
699ip_freef(PNATState pData, struct ipqhead *fhp, struct ipq_t *fp) {
700 struct mbuf *q;
701
702 while (fp->ipq_frags) {
703 q = fp->ipq_frags;
704 fp->ipq_frags = q->m_nextpkt;
705 m_freem(pData, q);
706 }
707 TAILQ_REMOVE(fhp, fp, ipq_list);
708 free(fp);
709 nipq--;
710}
711#endif /* VBOX_WITH_BSD_REASS */
712
713/*
714 * Put an ip fragment on a reassembly chain.
715 * Like insque, but pointers in middle of structure.
716 */
717void
718ip_enq(PNATState pData, register struct ipasfrag *p, register struct ipasfrag *prev)
719{
720 DEBUG_CALL("ip_enq");
721 DEBUG_ARG("prev = %lx", (long)prev);
722 p->ipf_prev = ptr_to_u32(pData, prev);
723 p->ipf_next = prev->ipf_next;
724 u32_to_ptr(pData, prev->ipf_next, struct ipasfrag *)->ipf_prev = ptr_to_u32(pData, p);
725 prev->ipf_next = ptr_to_u32(pData, p);
726}
727
728/*
729 * To ip_enq as remque is to insque.
730 */
731void
732ip_deq(PNATState pData, register struct ipasfrag *p)
733{
734 struct ipasfrag *prev = u32_to_ptr(pData, p->ipf_prev, struct ipasfrag *);
735 struct ipasfrag *next = u32_to_ptr(pData, p->ipf_next, struct ipasfrag *);
736 u32ptr_done(pData, prev->ipf_next, p);
737 prev->ipf_next = p->ipf_next;
738 next->ipf_prev = p->ipf_prev;
739}
740
741/*
742 * IP timer processing;
743 * if a timer expires on a reassembly
744 * queue, discard it.
745 */
746void
747ip_slowtimo(PNATState pData)
748{
749 register struct ipq_t *fp;
750
751#ifndef VBOX_WITH_BSD_REASS
752 DEBUG_CALL("ip_slowtimo");
753
754 fp = u32_to_ptr(pData, ipq.next, struct ipq_t *);
755 if (fp == 0)
756 return;
757
758 while (fp != &ipq) {
759 --fp->ipq_ttl;
760 fp = u32_to_ptr(pData, fp->next, struct ipq_t *);
761 if (u32_to_ptr(pData, fp->prev, struct ipq_t *)->ipq_ttl == 0) {
762 ipstat.ips_fragtimeout++;
763 ip_freef(pData, u32_to_ptr(pData, fp->prev, struct ipq_t *));
764 }
765 }
766#else /* !VBOX_WITH_BSD_REASS */
767 /* XXX: the fragment expiration is the same but requier
768 * additional loop see (see ip_input.c in FreeBSD tree)
769 */
770 int i;
771 DEBUG_CALL("ip_slowtimo");
772 for (i = 0; i < IPREASS_NHASH; i++) {
773 for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
774 struct ipq_t *fpp;
775
776 fpp = fp;
777 fp = TAILQ_NEXT(fp, ipq_list);
778 if(--fpp->ipq_ttl == 0) {
779 ipstat.ips_fragtimeout += fpp->ipq_nfrags;
780 ip_freef(pData, &ipq[i], fpp);
781 }
782 }
783 }
784 /*
785 * If we are over the maximum number of fragments
786 * (due to the limit being lowered), drain off
787 * enough to get down to the new limit.
788 */
789 if (maxnipq >= 0 && nipq > maxnipq) {
790 for (i = 0; i < IPREASS_NHASH; i++) {
791 while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) {
792 ipstat.ips_fragdropped +=
793 TAILQ_FIRST(&ipq[i])->ipq_nfrags;
794 ip_freef(pData, &ipq[i], TAILQ_FIRST(&ipq[i]));
795 }
796 }
797 }
798#endif /* VBOX_WITH_BSD_REASS */
799}
800
801/*
802 * Do option processing on a datagram,
803 * possibly discarding it if bad options are encountered,
804 * or forwarding it if source-routed.
805 * Returns 1 if packet has been forwarded/freed,
806 * 0 if the packet should be processed further.
807 */
808
809#ifdef notdef
810
811int
812ip_dooptions(m)
813 struct mbuf *m;
814{
815 register struct ip *ip = mtod(m, struct ip *);
816 register u_char *cp;
817 register struct ip_timestamp *ipt;
818 register struct in_ifaddr *ia;
819/* int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
820 int opt, optlen, cnt, off, code, type, forward = 0;
821 struct in_addr *sin, dst;
822typedef u_int32_t n_time;
823 n_time ntime;
824
825 dst = ip->ip_dst;
826 cp = (u_char *)(ip + 1);
827 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
828 for (; cnt > 0; cnt -= optlen, cp += optlen) {
829 opt = cp[IPOPT_OPTVAL];
830 if (opt == IPOPT_EOL)
831 break;
832 if (opt == IPOPT_NOP)
833 optlen = 1;
834 else {
835 optlen = cp[IPOPT_OLEN];
836 if (optlen <= 0 || optlen > cnt) {
837 code = &cp[IPOPT_OLEN] - (u_char *)ip;
838 goto bad;
839 }
840 }
841 switch (opt) {
842
843 default:
844 break;
845
846 /*
847 * Source routing with record.
848 * Find interface with current destination address.
849 * If none on this machine then drop if strictly routed,
850 * or do nothing if loosely routed.
851 * Record interface address and bring up next address
852 * component. If strictly routed make sure next
853 * address is on directly accessible net.
854 */
855 case IPOPT_LSRR:
856 case IPOPT_SSRR:
857 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
858 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
859 goto bad;
860 }
861 ipaddr.sin_addr = ip->ip_dst;
862 ia = (struct in_ifaddr *)
863 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
864 if (ia == 0) {
865 if (opt == IPOPT_SSRR) {
866 type = ICMP_UNREACH;
867 code = ICMP_UNREACH_SRCFAIL;
868 goto bad;
869 }
870 /*
871 * Loose routing, and not at next destination
872 * yet; nothing to do except forward.
873 */
874 break;
875 }
876 off--; / * 0 origin * /
877 if (off > optlen - sizeof(struct in_addr)) {
878 /*
879 * End of source route. Should be for us.
880 */
881 save_rte(cp, ip->ip_src);
882 break;
883 }
884 /*
885 * locate outgoing interface
886 */
887 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
888 sizeof(ipaddr.sin_addr));
889 if (opt == IPOPT_SSRR) {
890#define INA struct in_ifaddr *
891#define SA struct sockaddr *
892 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
893 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
894 } else
895 ia = ip_rtaddr(ipaddr.sin_addr);
896 if (ia == 0) {
897 type = ICMP_UNREACH;
898 code = ICMP_UNREACH_SRCFAIL;
899 goto bad;
900 }
901 ip->ip_dst = ipaddr.sin_addr;
902 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
903 (caddr_t)(cp + off), sizeof(struct in_addr));
904 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
905 /*
906 * Let ip_intr's mcast routing check handle mcast pkts
907 */
908 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
909 break;
910
911 case IPOPT_RR:
912 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
913 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
914 goto bad;
915 }
916 /*
917 * If no space remains, ignore.
918 */
919 off--; * 0 origin *
920 if (off > optlen - sizeof(struct in_addr))
921 break;
922 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
923 sizeof(ipaddr.sin_addr));
924 /*
925 * locate outgoing interface; if we're the destination,
926 * use the incoming interface (should be same).
927 */
928 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
929 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
930 type = ICMP_UNREACH;
931 code = ICMP_UNREACH_HOST;
932 goto bad;
933 }
934 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
935 (caddr_t)(cp + off), sizeof(struct in_addr));
936 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
937 break;
938
939 case IPOPT_TS:
940 code = cp - (u_char *)ip;
941 ipt = (struct ip_timestamp *)cp;
942 if (ipt->ipt_len < 5)
943 goto bad;
944 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
945 if (++ipt->ipt_oflw == 0)
946 goto bad;
947 break;
948 }
949 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
950 switch (ipt->ipt_flg) {
951
952 case IPOPT_TS_TSONLY:
953 break;
954
955 case IPOPT_TS_TSANDADDR:
956 if (ipt->ipt_ptr + sizeof(n_time) +
957 sizeof(struct in_addr) > ipt->ipt_len)
958 goto bad;
959 ipaddr.sin_addr = dst;
960 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
961 m->m_pkthdr.rcvif);
962 if (ia == 0)
963 continue;
964 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
965 (caddr_t)sin, sizeof(struct in_addr));
966 ipt->ipt_ptr += sizeof(struct in_addr);
967 break;
968
969 case IPOPT_TS_PRESPEC:
970 if (ipt->ipt_ptr + sizeof(n_time) +
971 sizeof(struct in_addr) > ipt->ipt_len)
972 goto bad;
973 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
974 sizeof(struct in_addr));
975 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
976 continue;
977 ipt->ipt_ptr += sizeof(struct in_addr);
978 break;
979
980 default:
981 goto bad;
982 }
983 ntime = iptime();
984 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
985 sizeof(n_time));
986 ipt->ipt_ptr += sizeof(n_time);
987 }
988 }
989 if (forward) {
990 ip_forward(m, 1);
991 return (1);
992 }
993 }
994 }
995 return (0);
996bad:
997 /* ip->ip_len -= ip->ip_hl << 2; XXX icmp_error adds in hdr length */
998
999/* Not yet */
1000 icmp_error(m, type, code, 0, 0);
1001
1002 ipstat.ips_badoptions++;
1003 return (1);
1004}
1005
1006#endif /* notdef */
1007
1008/*
1009 * Strip out IP options, at higher
1010 * level protocol in the kernel.
1011 * Second argument is buffer to which options
1012 * will be moved, and return value is their length.
1013 * (XXX) should be deleted; last arg currently ignored.
1014 */
1015void
1016ip_stripoptions(m, mopt)
1017 register struct mbuf *m;
1018 struct mbuf *mopt;
1019{
1020 register int i;
1021 struct ip *ip = mtod(m, struct ip *);
1022 register caddr_t opts;
1023 int olen;
1024
1025 olen = (ip->ip_hl<<2) - sizeof (struct ip);
1026 opts = (caddr_t)(ip + 1);
1027 i = m->m_len - (sizeof (struct ip) + olen);
1028 memcpy(opts, opts + olen, (unsigned)i);
1029 m->m_len -= olen;
1030
1031 ip->ip_hl = sizeof(struct ip) >> 2;
1032}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette