VirtualBox

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

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

Adapted versions of ip_reass, ip_freef and ip_slowtimo from FreeBSD (compilable on Linux, need some debugging)

  • Property svn:eol-style set to native
File size: 27.0 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 = (void *)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_data))
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 t = m->m_next;
649 m->m_next = NULL;
650 m_cat(pData, m, t);
651 nq = q->m_nextpkt;
652 q->m_nextpkt = NULL;
653 for (q = nq; q != NULL; q = nq) {
654 nq = q->m_nextpkt;
655 q->m_nextpkt = NULL;
656 m_cat(pData, m, q);
657 }
658
659 /*
660 * Create header for new ip packet by modifying header of first
661 * packet; dequeue and discard fragment reassembly header.
662 * Make header visible.
663 */
664 ip->ip_len = (ip->ip_hl << 2) + next;
665 ip->ip_src = fp->ipq_src;
666 ip->ip_dst = fp->ipq_dst;
667 TAILQ_REMOVE(head, fp, ipq_list);
668 nipq--;
669 free(fp);
670 m->m_len += (ip->ip_hl << 2);
671 m->m_data -= (ip->ip_hl << 2);
672 /* some debugging cruft by sklower, below, will go away soon */
673#if 0
674 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */
675 m_fixhdr(m);
676#endif
677 ipstat.ips_reassembled++;
678 return (m);
679
680dropfrag:
681 ipstat.ips_fragdropped++;
682 if (fp != NULL)
683 fp->ipq_nfrags--;
684 m_freem(pData, m);
685done:
686 return (NULL);
687
688#undef GETIP
689}
690
691void
692ip_freef(PNATState pData, struct ipqhead *fhp, struct ipq_t *fp) {
693 struct mbuf *q;
694
695 while (fp->ipq_frags) {
696 q = fp->ipq_frags;
697 fp->ipq_frags = q->m_nextpkt;
698 m_freem(pData, q);
699 }
700 TAILQ_REMOVE(fhp, fp, ipq_list);
701 free(fp);
702 nipq--;
703}
704#endif /* VBOX_WITH_BSD_REASS */
705
706/*
707 * Put an ip fragment on a reassembly chain.
708 * Like insque, but pointers in middle of structure.
709 */
710void
711ip_enq(PNATState pData, register struct ipasfrag *p, register struct ipasfrag *prev)
712{
713 DEBUG_CALL("ip_enq");
714 DEBUG_ARG("prev = %lx", (long)prev);
715 p->ipf_prev = ptr_to_u32(pData, prev);
716 p->ipf_next = prev->ipf_next;
717 u32_to_ptr(pData, prev->ipf_next, struct ipasfrag *)->ipf_prev = ptr_to_u32(pData, p);
718 prev->ipf_next = ptr_to_u32(pData, p);
719}
720
721/*
722 * To ip_enq as remque is to insque.
723 */
724void
725ip_deq(PNATState pData, register struct ipasfrag *p)
726{
727 struct ipasfrag *prev = u32_to_ptr(pData, p->ipf_prev, struct ipasfrag *);
728 struct ipasfrag *next = u32_to_ptr(pData, p->ipf_next, struct ipasfrag *);
729 u32ptr_done(pData, prev->ipf_next, p);
730 prev->ipf_next = p->ipf_next;
731 next->ipf_prev = p->ipf_prev;
732}
733
734/*
735 * IP timer processing;
736 * if a timer expires on a reassembly
737 * queue, discard it.
738 */
739void
740ip_slowtimo(PNATState pData)
741{
742 register struct ipq_t *fp;
743
744#ifndef VBOX_WITH_BSD_REASS
745 DEBUG_CALL("ip_slowtimo");
746
747 fp = u32_to_ptr(pData, ipq.next, struct ipq_t *);
748 if (fp == 0)
749 return;
750
751 while (fp != &ipq) {
752 --fp->ipq_ttl;
753 fp = u32_to_ptr(pData, fp->next, struct ipq_t *);
754 if (u32_to_ptr(pData, fp->prev, struct ipq_t *)->ipq_ttl == 0) {
755 ipstat.ips_fragtimeout++;
756 ip_freef(pData, u32_to_ptr(pData, fp->prev, struct ipq_t *));
757 }
758 }
759#else /* !VBOX_WITH_BSD_REASS */
760 /* XXX: the fragment expiration is the same but requier
761 * additional loop see (see ip_input.c in FreeBSD tree)
762 */
763 int i;
764 DEBUG_CALL("ip_slowtimo");
765 for (i = 0; i < IPREASS_NHASH; i++) {
766 for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
767 struct ipq_t *fpp;
768
769 fpp = fp;
770 fp = TAILQ_NEXT(fp, ipq_list);
771 if(--fpp->ipq_ttl == 0) {
772 ipstat.ips_fragtimeout += fpp->ipq_nfrags;
773 ip_freef(pData, &ipq[i], fpp);
774 }
775 }
776 }
777 /*
778 * If we are over the maximum number of fragments
779 * (due to the limit being lowered), drain off
780 * enough to get down to the new limit.
781 */
782 if (maxnipq >= 0 && nipq > maxnipq) {
783 for (i = 0; i < IPREASS_NHASH; i++) {
784 while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) {
785 ipstat.ips_fragdropped +=
786 TAILQ_FIRST(&ipq[i])->ipq_nfrags;
787 ip_freef(pData, &ipq[i], TAILQ_FIRST(&ipq[i]));
788 }
789 }
790 }
791#endif /* VBOX_WITH_BSD_REASS */
792}
793
794/*
795 * Do option processing on a datagram,
796 * possibly discarding it if bad options are encountered,
797 * or forwarding it if source-routed.
798 * Returns 1 if packet has been forwarded/freed,
799 * 0 if the packet should be processed further.
800 */
801
802#ifdef notdef
803
804int
805ip_dooptions(m)
806 struct mbuf *m;
807{
808 register struct ip *ip = mtod(m, struct ip *);
809 register u_char *cp;
810 register struct ip_timestamp *ipt;
811 register struct in_ifaddr *ia;
812/* int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
813 int opt, optlen, cnt, off, code, type, forward = 0;
814 struct in_addr *sin, dst;
815typedef u_int32_t n_time;
816 n_time ntime;
817
818 dst = ip->ip_dst;
819 cp = (u_char *)(ip + 1);
820 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
821 for (; cnt > 0; cnt -= optlen, cp += optlen) {
822 opt = cp[IPOPT_OPTVAL];
823 if (opt == IPOPT_EOL)
824 break;
825 if (opt == IPOPT_NOP)
826 optlen = 1;
827 else {
828 optlen = cp[IPOPT_OLEN];
829 if (optlen <= 0 || optlen > cnt) {
830 code = &cp[IPOPT_OLEN] - (u_char *)ip;
831 goto bad;
832 }
833 }
834 switch (opt) {
835
836 default:
837 break;
838
839 /*
840 * Source routing with record.
841 * Find interface with current destination address.
842 * If none on this machine then drop if strictly routed,
843 * or do nothing if loosely routed.
844 * Record interface address and bring up next address
845 * component. If strictly routed make sure next
846 * address is on directly accessible net.
847 */
848 case IPOPT_LSRR:
849 case IPOPT_SSRR:
850 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
851 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
852 goto bad;
853 }
854 ipaddr.sin_addr = ip->ip_dst;
855 ia = (struct in_ifaddr *)
856 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
857 if (ia == 0) {
858 if (opt == IPOPT_SSRR) {
859 type = ICMP_UNREACH;
860 code = ICMP_UNREACH_SRCFAIL;
861 goto bad;
862 }
863 /*
864 * Loose routing, and not at next destination
865 * yet; nothing to do except forward.
866 */
867 break;
868 }
869 off--; / * 0 origin * /
870 if (off > optlen - sizeof(struct in_addr)) {
871 /*
872 * End of source route. Should be for us.
873 */
874 save_rte(cp, ip->ip_src);
875 break;
876 }
877 /*
878 * locate outgoing interface
879 */
880 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
881 sizeof(ipaddr.sin_addr));
882 if (opt == IPOPT_SSRR) {
883#define INA struct in_ifaddr *
884#define SA struct sockaddr *
885 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
886 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
887 } else
888 ia = ip_rtaddr(ipaddr.sin_addr);
889 if (ia == 0) {
890 type = ICMP_UNREACH;
891 code = ICMP_UNREACH_SRCFAIL;
892 goto bad;
893 }
894 ip->ip_dst = ipaddr.sin_addr;
895 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
896 (caddr_t)(cp + off), sizeof(struct in_addr));
897 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
898 /*
899 * Let ip_intr's mcast routing check handle mcast pkts
900 */
901 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
902 break;
903
904 case IPOPT_RR:
905 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
906 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
907 goto bad;
908 }
909 /*
910 * If no space remains, ignore.
911 */
912 off--; * 0 origin *
913 if (off > optlen - sizeof(struct in_addr))
914 break;
915 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
916 sizeof(ipaddr.sin_addr));
917 /*
918 * locate outgoing interface; if we're the destination,
919 * use the incoming interface (should be same).
920 */
921 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
922 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
923 type = ICMP_UNREACH;
924 code = ICMP_UNREACH_HOST;
925 goto bad;
926 }
927 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
928 (caddr_t)(cp + off), sizeof(struct in_addr));
929 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
930 break;
931
932 case IPOPT_TS:
933 code = cp - (u_char *)ip;
934 ipt = (struct ip_timestamp *)cp;
935 if (ipt->ipt_len < 5)
936 goto bad;
937 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
938 if (++ipt->ipt_oflw == 0)
939 goto bad;
940 break;
941 }
942 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
943 switch (ipt->ipt_flg) {
944
945 case IPOPT_TS_TSONLY:
946 break;
947
948 case IPOPT_TS_TSANDADDR:
949 if (ipt->ipt_ptr + sizeof(n_time) +
950 sizeof(struct in_addr) > ipt->ipt_len)
951 goto bad;
952 ipaddr.sin_addr = dst;
953 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
954 m->m_pkthdr.rcvif);
955 if (ia == 0)
956 continue;
957 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
958 (caddr_t)sin, sizeof(struct in_addr));
959 ipt->ipt_ptr += sizeof(struct in_addr);
960 break;
961
962 case IPOPT_TS_PRESPEC:
963 if (ipt->ipt_ptr + sizeof(n_time) +
964 sizeof(struct in_addr) > ipt->ipt_len)
965 goto bad;
966 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
967 sizeof(struct in_addr));
968 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
969 continue;
970 ipt->ipt_ptr += sizeof(struct in_addr);
971 break;
972
973 default:
974 goto bad;
975 }
976 ntime = iptime();
977 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
978 sizeof(n_time));
979 ipt->ipt_ptr += sizeof(n_time);
980 }
981 }
982 if (forward) {
983 ip_forward(m, 1);
984 return (1);
985 }
986 }
987 }
988 return (0);
989bad:
990 /* ip->ip_len -= ip->ip_hl << 2; XXX icmp_error adds in hdr length */
991
992/* Not yet */
993 icmp_error(m, type, code, 0, 0);
994
995 ipstat.ips_badoptions++;
996 return (1);
997}
998
999#endif /* notdef */
1000
1001/*
1002 * Strip out IP options, at higher
1003 * level protocol in the kernel.
1004 * Second argument is buffer to which options
1005 * will be moved, and return value is their length.
1006 * (XXX) should be deleted; last arg currently ignored.
1007 */
1008void
1009ip_stripoptions(m, mopt)
1010 register struct mbuf *m;
1011 struct mbuf *mopt;
1012{
1013 register int i;
1014 struct ip *ip = mtod(m, struct ip *);
1015 register caddr_t opts;
1016 int olen;
1017
1018 olen = (ip->ip_hl<<2) - sizeof (struct ip);
1019 opts = (caddr_t)(ip + 1);
1020 i = m->m_len - (sizeof (struct ip) + olen);
1021 memcpy(opts, opts + olen, (unsigned)i);
1022 m->m_len -= olen;
1023
1024 ip->ip_hl = sizeof(struct ip) >> 2;
1025}
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