VirtualBox

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

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

warning

  • 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_short hash;
435
436 /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
437 if (maxnipq == 0
438 || maxfragsperpacket == 0) {
439 ipstat.ips_fragments++;
440 ipstat.ips_fragdropped++;
441 m_freem(pData, m);
442 return (NULL);
443 }
444
445 ip = mtod(m, struct ip *);
446 hlen = ip->ip_hl << 2;
447
448 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
449 head = &ipq[hash];
450
451 /*
452 * Look for queue of fragments
453 * of this datagram.
454 */
455 TAILQ_FOREACH(fp, head, ipq_list)
456 if (ip->ip_id == fp->ipq_id &&
457 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
458 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
459 ip->ip_p == fp->ipq_p)
460 goto found;
461
462 fp = NULL;
463
464 /*
465 * Attempt to trim the number of allocated fragment queues if it
466 * exceeds the administrative limit.
467 */
468 if ((nipq > maxnipq) && (maxnipq > 0)) {
469 /*
470 * drop something from the tail of the current queue
471 * before proceeding further
472 */
473 struct ipq_t *q = TAILQ_LAST(head, ipqhead);
474 if (q == NULL) { /* gak */
475 for (i = 0; i < IPREASS_NHASH; i++) {
476 struct ipq_t *r = TAILQ_LAST(&ipq[i], ipqhead);
477 if (r) {
478 ipstat.ips_fragtimeout += r->ipq_nfrags;
479 ip_freef(pData, &ipq[i], r);
480 break;
481 }
482 }
483 } else {
484 ipstat.ips_fragtimeout += q->ipq_nfrags;
485 ip_freef(pData, head, q);
486 }
487 }
488
489found:
490 /*
491 * Adjust ip_len to not reflect header,
492 * convert offset of this to bytes.
493 */
494 ip->ip_len -= hlen;
495 if (ip->ip_off & IP_MF) {
496 /*
497 * Make sure that fragments have a data length
498 * that's a non-zero multiple of 8 bytes.
499 */
500 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
501 ipstat.ips_toosmall++; /* XXX */
502 goto dropfrag;
503 }
504 m->m_flags |= M_FRAG;
505 } else
506 m->m_flags &= ~M_FRAG;
507 ip->ip_off <<= 3;
508
509
510 /*
511 * Attempt reassembly; if it succeeds, proceed.
512 * ip_reass() will return a different mbuf.
513 */
514 ipstat.ips_fragments++;
515 m->m_hdr.header = ip;
516
517 /* Previous ip_reass() started here. */
518 /*
519 * Presence of header sizes in mbufs
520 * would confuse code below.
521 */
522 m->m_data += hlen;
523 m->m_len -= hlen;
524
525 /*
526 * If first fragment to arrive, create a reassembly queue.
527 */
528 if (fp == NULL) {
529 fp = malloc(sizeof(struct ipq_t));
530 if (fp == NULL)
531 goto dropfrag;
532 TAILQ_INSERT_HEAD(head, fp, ipq_list);
533 nipq++;
534 fp->ipq_nfrags = 1;
535 fp->ipq_ttl = IPFRAGTTL;
536 fp->ipq_p = ip->ip_p;
537 fp->ipq_id = ip->ip_id;
538 fp->ipq_src = ip->ip_src;
539 fp->ipq_dst = ip->ip_dst;
540 fp->ipq_frags = m;
541 m->m_nextpkt = NULL;
542 goto done;
543 } else {
544 fp->ipq_nfrags++;
545 }
546
547#define GETIP(m) ((struct ip*)((m)->m_hdr.header))
548
549
550 /*
551 * Find a segment which begins after this one does.
552 */
553 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
554 if (GETIP(q)->ip_off > ip->ip_off)
555 break;
556
557 /*
558 * If there is a preceding segment, it may provide some of
559 * our data already. If so, drop the data from the incoming
560 * segment. If it provides all of our data, drop us, otherwise
561 * stick new segment in the proper place.
562 *
563 * If some of the data is dropped from the the preceding
564 * segment, then it's checksum is invalidated.
565 */
566 if (p) {
567 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
568 if (i > 0) {
569 if (i >= ip->ip_len)
570 goto dropfrag;
571 m_adj(m, i);
572 ip->ip_off += i;
573 ip->ip_len -= i;
574 }
575 m->m_nextpkt = p->m_nextpkt;
576 p->m_nextpkt = m;
577 } else {
578 m->m_nextpkt = fp->ipq_frags;
579 fp->ipq_frags = m;
580 }
581
582 /*
583 * While we overlap succeeding segments trim them or,
584 * if they are completely covered, dequeue them.
585 */
586 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
587 q = nq) {
588 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
589 if (i < GETIP(q)->ip_len) {
590 GETIP(q)->ip_len -= i;
591 GETIP(q)->ip_off += i;
592 m_adj(q, i);
593 break;
594 }
595 nq = q->m_nextpkt;
596 m->m_nextpkt = nq;
597 ipstat.ips_fragdropped++;
598 fp->ipq_nfrags--;
599 m_freem(pData, q);
600 }
601
602 /*
603 * Check for complete reassembly and perform frag per packet
604 * limiting.
605 *
606 * Frag limiting is performed here so that the nth frag has
607 * a chance to complete the packet before we drop the packet.
608 * As a result, n+1 frags are actually allowed per packet, but
609 * only n will ever be stored. (n = maxfragsperpacket.)
610 *
611 */
612 next = 0;
613 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
614 if (GETIP(q)->ip_off != next) {
615 if (fp->ipq_nfrags > maxfragsperpacket) {
616 ipstat.ips_fragdropped += fp->ipq_nfrags;
617 ip_freef(pData, head, fp);
618 }
619 goto done;
620 }
621 next += GETIP(q)->ip_len;
622 }
623 /* Make sure the last packet didn't have the IP_MF flag */
624 if (p->m_flags & M_FRAG) {
625 if (fp->ipq_nfrags > maxfragsperpacket) {
626 ipstat.ips_fragdropped += fp->ipq_nfrags;
627 ip_freef(pData, head, fp);
628 }
629 goto done;
630 }
631
632 /*
633 * Reassembly is complete. Make sure the packet is a sane size.
634 */
635 q = fp->ipq_frags;
636 ip = GETIP(q);
637 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
638 ipstat.ips_fragdropped += fp->ipq_nfrags;
639 ip_freef(pData, head, fp);
640 goto done;
641 }
642
643 /*
644 * Concatenate fragments.
645 */
646 m = q;
647#if 0
648 t = m->m_next;
649 m->m_next = NULL;
650 m_cat(pData, m, t);
651#endif
652 nq = q->m_nextpkt;
653 q->m_nextpkt = NULL;
654 for (q = nq; q != NULL; q = nq) {
655 nq = q->m_nextpkt;
656 q->m_nextpkt = NULL;
657 m_cat(pData, m, q);
658 }
659
660 /*
661 * Create header for new ip packet by modifying header of first
662 * packet; dequeue and discard fragment reassembly header.
663 * Make header visible.
664 */
665#if 0
666 ip->ip_len = (ip->ip_hl << 2) + next;
667#else
668 ip->ip_len = next;
669#endif
670 ip->ip_src = fp->ipq_src;
671 ip->ip_dst = fp->ipq_dst;
672 TAILQ_REMOVE(head, fp, ipq_list);
673 nipq--;
674 free(fp);
675
676 m->m_len += (ip->ip_hl << 2);
677 m->m_data -= (ip->ip_hl << 2);
678 /* some debugging cruft by sklower, below, will go away soon */
679#if 0
680 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */
681 m_fixhdr(m);
682#endif
683 ipstat.ips_reassembled++;
684 return (m);
685
686dropfrag:
687 ipstat.ips_fragdropped++;
688 if (fp != NULL)
689 fp->ipq_nfrags--;
690 m_freem(pData, m);
691done:
692 return (NULL);
693
694#undef GETIP
695}
696
697void
698ip_freef(PNATState pData, struct ipqhead *fhp, struct ipq_t *fp) {
699 struct mbuf *q;
700
701 while (fp->ipq_frags) {
702 q = fp->ipq_frags;
703 fp->ipq_frags = q->m_nextpkt;
704 m_freem(pData, q);
705 }
706 TAILQ_REMOVE(fhp, fp, ipq_list);
707 free(fp);
708 nipq--;
709}
710#endif /* VBOX_WITH_BSD_REASS */
711
712#ifndef VBOX_WITH_BSD_REASS
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#endif /* !VBOX_WITH_BSD_REASS */
741
742/*
743 * IP timer processing;
744 * if a timer expires on a reassembly
745 * queue, discard it.
746 */
747void
748ip_slowtimo(PNATState pData)
749{
750 register struct ipq_t *fp;
751
752#ifndef VBOX_WITH_BSD_REASS
753 DEBUG_CALL("ip_slowtimo");
754
755 fp = u32_to_ptr(pData, ipq.next, struct ipq_t *);
756 if (fp == 0)
757 return;
758
759 while (fp != &ipq) {
760 --fp->ipq_ttl;
761 fp = u32_to_ptr(pData, fp->next, struct ipq_t *);
762 if (u32_to_ptr(pData, fp->prev, struct ipq_t *)->ipq_ttl == 0) {
763 ipstat.ips_fragtimeout++;
764 ip_freef(pData, u32_to_ptr(pData, fp->prev, struct ipq_t *));
765 }
766 }
767#else /* !VBOX_WITH_BSD_REASS */
768 /* XXX: the fragment expiration is the same but requier
769 * additional loop see (see ip_input.c in FreeBSD tree)
770 */
771 int i;
772 DEBUG_CALL("ip_slowtimo");
773 for (i = 0; i < IPREASS_NHASH; i++) {
774 for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
775 struct ipq_t *fpp;
776
777 fpp = fp;
778 fp = TAILQ_NEXT(fp, ipq_list);
779 if(--fpp->ipq_ttl == 0) {
780 ipstat.ips_fragtimeout += fpp->ipq_nfrags;
781 ip_freef(pData, &ipq[i], fpp);
782 }
783 }
784 }
785 /*
786 * If we are over the maximum number of fragments
787 * (due to the limit being lowered), drain off
788 * enough to get down to the new limit.
789 */
790 if (maxnipq >= 0 && nipq > maxnipq) {
791 for (i = 0; i < IPREASS_NHASH; i++) {
792 while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) {
793 ipstat.ips_fragdropped +=
794 TAILQ_FIRST(&ipq[i])->ipq_nfrags;
795 ip_freef(pData, &ipq[i], TAILQ_FIRST(&ipq[i]));
796 }
797 }
798 }
799#endif /* VBOX_WITH_BSD_REASS */
800}
801
802/*
803 * Do option processing on a datagram,
804 * possibly discarding it if bad options are encountered,
805 * or forwarding it if source-routed.
806 * Returns 1 if packet has been forwarded/freed,
807 * 0 if the packet should be processed further.
808 */
809
810#ifdef notdef
811
812int
813ip_dooptions(m)
814 struct mbuf *m;
815{
816 register struct ip *ip = mtod(m, struct ip *);
817 register u_char *cp;
818 register struct ip_timestamp *ipt;
819 register struct in_ifaddr *ia;
820 /* int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
821 int opt, optlen, cnt, off, code, type, forward = 0;
822 struct in_addr *sin, dst;
823 typedef u_int32_t n_time;
824 n_time ntime;
825
826 dst = ip->ip_dst;
827 cp = (u_char *)(ip + 1);
828 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
829 for (; cnt > 0; cnt -= optlen, cp += optlen) {
830 opt = cp[IPOPT_OPTVAL];
831 if (opt == IPOPT_EOL)
832 break;
833 if (opt == IPOPT_NOP)
834 optlen = 1;
835 else {
836 optlen = cp[IPOPT_OLEN];
837 if (optlen <= 0 || optlen > cnt) {
838 code = &cp[IPOPT_OLEN] - (u_char *)ip;
839 goto bad;
840 }
841 }
842 switch (opt) {
843
844 default:
845 break;
846
847 /*
848 * Source routing with record.
849 * Find interface with current destination address.
850 * If none on this machine then drop if strictly routed,
851 * or do nothing if loosely routed.
852 * Record interface address and bring up next address
853 * component. If strictly routed make sure next
854 * address is on directly accessible net.
855 */
856 case IPOPT_LSRR:
857 case IPOPT_SSRR:
858 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
859 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
860 goto bad;
861 }
862 ipaddr.sin_addr = ip->ip_dst;
863 ia = (struct in_ifaddr *)
864 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
865 if (ia == 0) {
866 if (opt == IPOPT_SSRR) {
867 type = ICMP_UNREACH;
868 code = ICMP_UNREACH_SRCFAIL;
869 goto bad;
870 }
871 /*
872 * Loose routing, and not at next destination
873 * yet; nothing to do except forward.
874 */
875 break;
876 }
877 off--; / * 0 origin * /
878 if (off > optlen - sizeof(struct in_addr)) {
879 /*
880 * End of source route. Should be for us.
881 */
882 save_rte(cp, ip->ip_src);
883 break;
884 }
885 /*
886 * locate outgoing interface
887 */
888 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
889 sizeof(ipaddr.sin_addr));
890 if (opt == IPOPT_SSRR) {
891#define INA struct in_ifaddr *
892#define SA struct sockaddr *
893 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
894 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
895 } else
896 ia = ip_rtaddr(ipaddr.sin_addr);
897 if (ia == 0) {
898 type = ICMP_UNREACH;
899 code = ICMP_UNREACH_SRCFAIL;
900 goto bad;
901 }
902 ip->ip_dst = ipaddr.sin_addr;
903 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
904 (caddr_t)(cp + off), sizeof(struct in_addr));
905 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
906 /*
907 * Let ip_intr's mcast routing check handle mcast pkts
908 */
909 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
910 break;
911
912 case IPOPT_RR:
913 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
914 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
915 goto bad;
916 }
917 /*
918 * If no space remains, ignore.
919 */
920 off--; * 0 origin *
921 if (off > optlen - sizeof(struct in_addr))
922 break;
923 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
924 sizeof(ipaddr.sin_addr));
925 /*
926 * locate outgoing interface; if we're the destination,
927 * use the incoming interface (should be same).
928 */
929 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
930 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
931 type = ICMP_UNREACH;
932 code = ICMP_UNREACH_HOST;
933 goto bad;
934 }
935 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
936 (caddr_t)(cp + off), sizeof(struct in_addr));
937 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
938 break;
939
940 case IPOPT_TS:
941 code = cp - (u_char *)ip;
942 ipt = (struct ip_timestamp *)cp;
943 if (ipt->ipt_len < 5)
944 goto bad;
945 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
946 if (++ipt->ipt_oflw == 0)
947 goto bad;
948 break;
949 }
950 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
951 switch (ipt->ipt_flg) {
952
953 case IPOPT_TS_TSONLY:
954 break;
955
956 case IPOPT_TS_TSANDADDR:
957 if (ipt->ipt_ptr + sizeof(n_time) +
958 sizeof(struct in_addr) > ipt->ipt_len)
959 goto bad;
960 ipaddr.sin_addr = dst;
961 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
962 m->m_pkthdr.rcvif);
963 if (ia == 0)
964 continue;
965 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
966 (caddr_t)sin, sizeof(struct in_addr));
967 ipt->ipt_ptr += sizeof(struct in_addr);
968 break;
969
970 case IPOPT_TS_PRESPEC:
971 if (ipt->ipt_ptr + sizeof(n_time) +
972 sizeof(struct in_addr) > ipt->ipt_len)
973 goto bad;
974 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
975 sizeof(struct in_addr));
976 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
977 continue;
978 ipt->ipt_ptr += sizeof(struct in_addr);
979 break;
980
981 default:
982 goto bad;
983 }
984 ntime = iptime();
985 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
986 sizeof(n_time));
987 ipt->ipt_ptr += sizeof(n_time);
988 }
989 }
990 if (forward) {
991 ip_forward(m, 1);
992 return (1);
993 }
994 }
995 }
996 return (0);
997bad:
998 /* ip->ip_len -= ip->ip_hl << 2; XXX icmp_error adds in hdr length */
999
1000 /* Not yet */
1001 icmp_error(m, type, code, 0, 0);
1002
1003 ipstat.ips_badoptions++;
1004 return (1);
1005}
1006
1007#endif /* notdef */
1008
1009/*
1010 * Strip out IP options, at higher
1011 * level protocol in the kernel.
1012 * Second argument is buffer to which options
1013 * will be moved, and return value is their length.
1014 * (XXX) should be deleted; last arg currently ignored.
1015 */
1016void
1017ip_stripoptions(m, mopt)
1018 register struct mbuf *m;
1019 struct mbuf *mopt;
1020{
1021 register int i;
1022 struct ip *ip = mtod(m, struct ip *);
1023 register caddr_t opts;
1024 int olen;
1025
1026 olen = (ip->ip_hl<<2) - sizeof (struct ip);
1027 opts = (caddr_t)(ip + 1);
1028 i = m->m_len - (sizeof (struct ip) + olen);
1029 memcpy(opts, opts + olen, (unsigned)i);
1030 m->m_len -= olen;
1031
1032 ip->ip_hl = sizeof(struct ip) >> 2;
1033}
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