VirtualBox

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

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

more cosmetic

  • Property svn:eol-style set to native
File size: 33.7 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_hdr.header = ip;
517
518 /* Previous ip_reass() started here. */
519 /*
520 * Presence of header sizes in mbufs
521 * would confuse code below.
522 */
523 m->m_data += hlen;
524 m->m_len -= hlen;
525
526 /*
527 * If first fragment to arrive, create a reassembly queue.
528 */
529 if (fp == NULL) {
530 fp = malloc(sizeof(struct ipq_t));
531 if (fp == NULL)
532 goto dropfrag;
533 TAILQ_INSERT_HEAD(head, fp, ipq_list);
534 nipq++;
535 fp->ipq_nfrags = 1;
536 fp->ipq_ttl = IPFRAGTTL;
537 fp->ipq_p = ip->ip_p;
538 fp->ipq_id = ip->ip_id;
539 fp->ipq_src = ip->ip_src;
540 fp->ipq_dst = ip->ip_dst;
541 fp->ipq_frags = m;
542 m->m_nextpkt = NULL;
543 goto done;
544 } else {
545 fp->ipq_nfrags++;
546 }
547
548#define GETIP(m) ((struct ip*)((m)->m_hdr.header))
549
550
551 /*
552 * Find a segment which begins after this one does.
553 */
554 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
555 if (GETIP(q)->ip_off > ip->ip_off)
556 break;
557
558 /*
559 * If there is a preceding segment, it may provide some of
560 * our data already. If so, drop the data from the incoming
561 * segment. If it provides all of our data, drop us, otherwise
562 * stick new segment in the proper place.
563 *
564 * If some of the data is dropped from the the preceding
565 * segment, then it's checksum is invalidated.
566 */
567 if (p) {
568 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
569 if (i > 0) {
570 if (i >= ip->ip_len)
571 goto dropfrag;
572 m_adj(m, i);
573 ip->ip_off += i;
574 ip->ip_len -= i;
575 }
576 m->m_nextpkt = p->m_nextpkt;
577 p->m_nextpkt = m;
578 } else {
579 m->m_nextpkt = fp->ipq_frags;
580 fp->ipq_frags = m;
581 }
582
583 /*
584 * While we overlap succeeding segments trim them or,
585 * if they are completely covered, dequeue them.
586 */
587 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
588 q = nq) {
589 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
590 if (i < GETIP(q)->ip_len) {
591 GETIP(q)->ip_len -= i;
592 GETIP(q)->ip_off += i;
593 m_adj(q, i);
594 break;
595 }
596 nq = q->m_nextpkt;
597 m->m_nextpkt = nq;
598 ipstat.ips_fragdropped++;
599 fp->ipq_nfrags--;
600 m_freem(pData, q);
601 }
602
603 /*
604 * Check for complete reassembly and perform frag per packet
605 * limiting.
606 *
607 * Frag limiting is performed here so that the nth frag has
608 * a chance to complete the packet before we drop the packet.
609 * As a result, n+1 frags are actually allowed per packet, but
610 * only n will ever be stored. (n = maxfragsperpacket.)
611 *
612 */
613 next = 0;
614 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
615 if (GETIP(q)->ip_off != next) {
616 if (fp->ipq_nfrags > maxfragsperpacket) {
617 ipstat.ips_fragdropped += fp->ipq_nfrags;
618 ip_freef(pData, head, fp);
619 }
620 goto done;
621 }
622 next += GETIP(q)->ip_len;
623 }
624 /* Make sure the last packet didn't have the IP_MF flag */
625 if (p->m_flags & M_FRAG) {
626 if (fp->ipq_nfrags > maxfragsperpacket) {
627 ipstat.ips_fragdropped += fp->ipq_nfrags;
628 ip_freef(pData, head, fp);
629 }
630 goto done;
631 }
632
633 /*
634 * Reassembly is complete. Make sure the packet is a sane size.
635 */
636 q = fp->ipq_frags;
637 ip = GETIP(q);
638 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
639 ipstat.ips_fragdropped += fp->ipq_nfrags;
640 ip_freef(pData, head, fp);
641 goto done;
642 }
643
644 /*
645 * Concatenate fragments.
646 */
647 m = q;
648#if 0
649 t = m->m_next;
650 m->m_next = NULL;
651 m_cat(pData, m, t);
652#endif
653 nq = q->m_nextpkt;
654 q->m_nextpkt = NULL;
655 for (q = nq; q != NULL; q = nq) {
656 nq = q->m_nextpkt;
657 q->m_nextpkt = NULL;
658 m_cat(pData, m, q);
659 }
660
661 /*
662 * Create header for new ip packet by modifying header of first
663 * packet; dequeue and discard fragment reassembly header.
664 * Make header visible.
665 */
666#if 0
667 ip->ip_len = (ip->ip_hl << 2) + next;
668#else
669 ip->ip_len = next;
670#endif
671 ip->ip_src = fp->ipq_src;
672 ip->ip_dst = fp->ipq_dst;
673 TAILQ_REMOVE(head, fp, ipq_list);
674 nipq--;
675 free(fp);
676
677 m->m_len += (ip->ip_hl << 2);
678 m->m_data -= (ip->ip_hl << 2);
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#ifndef VBOX_WITH_BSD_REASS
714/*
715 * Put an ip fragment on a reassembly chain.
716 * Like insque, but pointers in middle of structure.
717 */
718void
719ip_enq(PNATState pData, register struct ipasfrag *p, register struct ipasfrag *prev)
720{
721 DEBUG_CALL("ip_enq");
722 DEBUG_ARG("prev = %lx", (long)prev);
723 p->ipf_prev = ptr_to_u32(pData, prev);
724 p->ipf_next = prev->ipf_next;
725 u32_to_ptr(pData, prev->ipf_next, struct ipasfrag *)->ipf_prev = ptr_to_u32(pData, p);
726 prev->ipf_next = ptr_to_u32(pData, p);
727}
728
729/*
730 * To ip_enq as remque is to insque.
731 */
732void
733ip_deq(PNATState pData, register struct ipasfrag *p)
734{
735 struct ipasfrag *prev = u32_to_ptr(pData, p->ipf_prev, struct ipasfrag *);
736 struct ipasfrag *next = u32_to_ptr(pData, p->ipf_next, struct ipasfrag *);
737 u32ptr_done(pData, prev->ipf_next, p);
738 prev->ipf_next = p->ipf_next;
739 next->ipf_prev = p->ipf_prev;
740}
741#endif /* !VBOX_WITH_BSD_REASS */
742
743/*
744 * IP timer processing;
745 * if a timer expires on a reassembly
746 * queue, discard it.
747 */
748void
749ip_slowtimo(PNATState pData)
750{
751 register struct ipq_t *fp;
752
753#ifndef VBOX_WITH_BSD_REASS
754 DEBUG_CALL("ip_slowtimo");
755
756 fp = u32_to_ptr(pData, ipq.next, struct ipq_t *);
757 if (fp == 0)
758 return;
759
760 while (fp != &ipq) {
761 --fp->ipq_ttl;
762 fp = u32_to_ptr(pData, fp->next, struct ipq_t *);
763 if (u32_to_ptr(pData, fp->prev, struct ipq_t *)->ipq_ttl == 0) {
764 ipstat.ips_fragtimeout++;
765 ip_freef(pData, u32_to_ptr(pData, fp->prev, struct ipq_t *));
766 }
767 }
768#else /* !VBOX_WITH_BSD_REASS */
769 /* XXX: the fragment expiration is the same but requier
770 * additional loop see (see ip_input.c in FreeBSD tree)
771 */
772 int i;
773 DEBUG_CALL("ip_slowtimo");
774 for (i = 0; i < IPREASS_NHASH; i++) {
775 for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
776 struct ipq_t *fpp;
777
778 fpp = fp;
779 fp = TAILQ_NEXT(fp, ipq_list);
780 if(--fpp->ipq_ttl == 0) {
781 ipstat.ips_fragtimeout += fpp->ipq_nfrags;
782 ip_freef(pData, &ipq[i], fpp);
783 }
784 }
785 }
786 /*
787 * If we are over the maximum number of fragments
788 * (due to the limit being lowered), drain off
789 * enough to get down to the new limit.
790 */
791 if (maxnipq >= 0 && nipq > maxnipq) {
792 for (i = 0; i < IPREASS_NHASH; i++) {
793 while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) {
794 ipstat.ips_fragdropped +=
795 TAILQ_FIRST(&ipq[i])->ipq_nfrags;
796 ip_freef(pData, &ipq[i], TAILQ_FIRST(&ipq[i]));
797 }
798 }
799 }
800#endif /* VBOX_WITH_BSD_REASS */
801}
802
803/*
804 * Do option processing on a datagram,
805 * possibly discarding it if bad options are encountered,
806 * or forwarding it if source-routed.
807 * Returns 1 if packet has been forwarded/freed,
808 * 0 if the packet should be processed further.
809 */
810
811#ifdef notdef
812
813int
814ip_dooptions(m)
815 struct mbuf *m;
816{
817 register struct ip *ip = mtod(m, struct ip *);
818 register u_char *cp;
819 register struct ip_timestamp *ipt;
820 register struct in_ifaddr *ia;
821 /* int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
822 int opt, optlen, cnt, off, code, type, forward = 0;
823 struct in_addr *sin, dst;
824 typedef u_int32_t n_time;
825 n_time ntime;
826
827 dst = ip->ip_dst;
828 cp = (u_char *)(ip + 1);
829 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
830 for (; cnt > 0; cnt -= optlen, cp += optlen) {
831 opt = cp[IPOPT_OPTVAL];
832 if (opt == IPOPT_EOL)
833 break;
834 if (opt == IPOPT_NOP)
835 optlen = 1;
836 else {
837 optlen = cp[IPOPT_OLEN];
838 if (optlen <= 0 || optlen > cnt) {
839 code = &cp[IPOPT_OLEN] - (u_char *)ip;
840 goto bad;
841 }
842 }
843 switch (opt) {
844
845 default:
846 break;
847
848 /*
849 * Source routing with record.
850 * Find interface with current destination address.
851 * If none on this machine then drop if strictly routed,
852 * or do nothing if loosely routed.
853 * Record interface address and bring up next address
854 * component. If strictly routed make sure next
855 * address is on directly accessible net.
856 */
857 case IPOPT_LSRR:
858 case IPOPT_SSRR:
859 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
860 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
861 goto bad;
862 }
863 ipaddr.sin_addr = ip->ip_dst;
864 ia = (struct in_ifaddr *)
865 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
866 if (ia == 0) {
867 if (opt == IPOPT_SSRR) {
868 type = ICMP_UNREACH;
869 code = ICMP_UNREACH_SRCFAIL;
870 goto bad;
871 }
872 /*
873 * Loose routing, and not at next destination
874 * yet; nothing to do except forward.
875 */
876 break;
877 }
878 off--; / * 0 origin * /
879 if (off > optlen - sizeof(struct in_addr)) {
880 /*
881 * End of source route. Should be for us.
882 */
883 save_rte(cp, ip->ip_src);
884 break;
885 }
886 /*
887 * locate outgoing interface
888 */
889 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
890 sizeof(ipaddr.sin_addr));
891 if (opt == IPOPT_SSRR) {
892#define INA struct in_ifaddr *
893#define SA struct sockaddr *
894 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
895 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
896 } else
897 ia = ip_rtaddr(ipaddr.sin_addr);
898 if (ia == 0) {
899 type = ICMP_UNREACH;
900 code = ICMP_UNREACH_SRCFAIL;
901 goto bad;
902 }
903 ip->ip_dst = ipaddr.sin_addr;
904 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
905 (caddr_t)(cp + off), sizeof(struct in_addr));
906 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
907 /*
908 * Let ip_intr's mcast routing check handle mcast pkts
909 */
910 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
911 break;
912
913 case IPOPT_RR:
914 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
915 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
916 goto bad;
917 }
918 /*
919 * If no space remains, ignore.
920 */
921 off--; * 0 origin *
922 if (off > optlen - sizeof(struct in_addr))
923 break;
924 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
925 sizeof(ipaddr.sin_addr));
926 /*
927 * locate outgoing interface; if we're the destination,
928 * use the incoming interface (should be same).
929 */
930 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
931 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
932 type = ICMP_UNREACH;
933 code = ICMP_UNREACH_HOST;
934 goto bad;
935 }
936 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
937 (caddr_t)(cp + off), sizeof(struct in_addr));
938 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
939 break;
940
941 case IPOPT_TS:
942 code = cp - (u_char *)ip;
943 ipt = (struct ip_timestamp *)cp;
944 if (ipt->ipt_len < 5)
945 goto bad;
946 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
947 if (++ipt->ipt_oflw == 0)
948 goto bad;
949 break;
950 }
951 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
952 switch (ipt->ipt_flg) {
953
954 case IPOPT_TS_TSONLY:
955 break;
956
957 case IPOPT_TS_TSANDADDR:
958 if (ipt->ipt_ptr + sizeof(n_time) +
959 sizeof(struct in_addr) > ipt->ipt_len)
960 goto bad;
961 ipaddr.sin_addr = dst;
962 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
963 m->m_pkthdr.rcvif);
964 if (ia == 0)
965 continue;
966 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
967 (caddr_t)sin, sizeof(struct in_addr));
968 ipt->ipt_ptr += sizeof(struct in_addr);
969 break;
970
971 case IPOPT_TS_PRESPEC:
972 if (ipt->ipt_ptr + sizeof(n_time) +
973 sizeof(struct in_addr) > ipt->ipt_len)
974 goto bad;
975 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
976 sizeof(struct in_addr));
977 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
978 continue;
979 ipt->ipt_ptr += sizeof(struct in_addr);
980 break;
981
982 default:
983 goto bad;
984 }
985 ntime = iptime();
986 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
987 sizeof(n_time));
988 ipt->ipt_ptr += sizeof(n_time);
989 }
990 }
991 if (forward) {
992 ip_forward(m, 1);
993 return (1);
994 }
995 }
996 }
997 return (0);
998bad:
999 /* ip->ip_len -= ip->ip_hl << 2; XXX icmp_error adds in hdr length */
1000
1001 /* Not yet */
1002 icmp_error(m, type, code, 0, 0);
1003
1004 ipstat.ips_badoptions++;
1005 return (1);
1006}
1007
1008#endif /* notdef */
1009
1010/*
1011 * Strip out IP options, at higher
1012 * level protocol in the kernel.
1013 * Second argument is buffer to which options
1014 * will be moved, and return value is their length.
1015 * (XXX) should be deleted; last arg currently ignored.
1016 */
1017void
1018ip_stripoptions(m, mopt)
1019 register struct mbuf *m;
1020 struct mbuf *mopt;
1021{
1022 register int i;
1023 struct ip *ip = mtod(m, struct ip *);
1024 register caddr_t opts;
1025 int olen;
1026
1027 olen = (ip->ip_hl<<2) - sizeof (struct ip);
1028 opts = (caddr_t)(ip + 1);
1029 i = m->m_len - (sizeof (struct ip) + olen);
1030 memcpy(opts, opts + olen, (unsigned)i);
1031 m->m_len -= olen;
1032
1033 ip->ip_hl = sizeof(struct ip) >> 2;
1034}
Note: See TracBrowser for help on using the repository browser.

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