VirtualBox

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

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

ICMP: support storaging and searching of sent packet.

  • Property svn:eol-style set to native
File size: 33.8 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#ifdef VBOX_WITH_SLIRP_ICMP
146 ip->ip_ttl--;
147#endif
148 /*
149 * Process options and, if not destined for us,
150 * ship it on. ip_dooptions returns 1 when an
151 * error was detected (causing an icmp message
152 * to be sent and the original packet to be freed).
153 */
154/* We do no IP options */
155/* if (hlen > sizeof (struct ip) && ip_dooptions(m))
156 * goto next;
157 */
158 /*
159 * If offset or IP_MF are set, must reassemble.
160 * Otherwise, nothing need be done.
161 * (We could look in the reassembly queue to see
162 * if the packet was previously fragmented,
163 * but it's not worth the time; just let them time out.)
164 *
165 * XXX This should fail, don't fragment yet
166 */
167#ifndef VBOX_WITH_BSD_REASS
168 if (ip->ip_off &~ IP_DF) {
169 register struct ipq_t *fp;
170 /*
171 * Look for queue of fragments
172 * of this datagram.
173 */
174 for (fp = u32_to_ptr(pData, ipq.next, struct ipq_t *); fp != &ipq;
175 fp = u32_to_ptr(pData, fp->next, struct ipq_t *))
176 if (ip->ip_id == fp->ipq_id &&
177 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
178 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
179 ip->ip_p == fp->ipq_p)
180 goto found;
181 fp = 0;
182 found:
183
184 /*
185 * Adjust ip_len to not reflect header,
186 * set ip_mff if more fragments are expected,
187 * convert offset of this to bytes.
188 */
189 ip->ip_len -= hlen;
190 if (ip->ip_off & IP_MF)
191 ((struct ipasfrag *)ip)->ipf_mff |= 1;
192 else
193 ((struct ipasfrag *)ip)->ipf_mff &= ~1;
194
195 ip->ip_off <<= 3;
196
197 /*
198 * If datagram marked as having more fragments
199 * or if this is not the first fragment,
200 * attempt reassembly; if it succeeds, proceed.
201 */
202 if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) {
203 ipstat.ips_fragments++;
204 ip = ip_reass(pData, (struct ipasfrag *)ip, fp);
205 if (ip == 0)
206 return;
207 ipstat.ips_reassembled++;
208 m = dtom(pData, ip);
209 } else
210 if (fp)
211 ip_freef(pData, fp);
212
213 } else
214 ip->ip_len -= hlen;
215#else /* !VBOX_WITH_BSD_REASS */
216 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
217 m = ip_reass(pData, m);
218 if (m == NULL)
219 return;
220 ip = mtod(m, struct ip *);
221 hlen = ip->ip_len;
222 }
223 else
224 ip->ip_len -= hlen;
225#endif /* VBOX_WITH_BSD_REASS */
226
227 /*
228 * Switch out to protocol's input routine.
229 */
230 ipstat.ips_delivered++;
231 switch (ip->ip_p) {
232 case IPPROTO_TCP:
233 tcp_input(pData, m, hlen, (struct socket *)NULL);
234 break;
235 case IPPROTO_UDP:
236 udp_input(pData, m, hlen);
237 break;
238 case IPPROTO_ICMP:
239 icmp_input(pData, m, hlen);
240 break;
241 default:
242 ipstat.ips_noproto++;
243 m_free(pData, m);
244 }
245 return;
246bad:
247 m_freem(pData, m);
248 return;
249}
250
251#ifndef VBOX_WITH_BSD_REASS
252/*
253 * Take incoming datagram fragment and try to
254 * reassemble it into whole datagram. If a chain for
255 * reassembly of this datagram already exists, then it
256 * is given as fp; otherwise have to make a chain.
257 */
258struct ip *
259ip_reass(PNATState pData, register struct ipasfrag *ip, register struct ipq_t *fp)
260{
261 register struct mbuf *m = dtom(pData, ip);
262 register struct ipasfrag *q;
263 int hlen = ip->ip_hl << 2;
264 int i, next;
265
266 DEBUG_CALL("ip_reass");
267 DEBUG_ARG("ip = %lx", (long)ip);
268 DEBUG_ARG("fp = %lx", (long)fp);
269 DEBUG_ARG("m = %lx", (long)m);
270
271 /*
272 * Presence of header sizes in mbufs
273 * would confuse code below.
274 * Fragment m_data is concatenated.
275 */
276 m->m_data += hlen;
277 m->m_len -= hlen;
278
279 /*
280 * If first fragment to arrive, create a reassembly queue.
281 */
282 if (fp == 0) {
283 struct mbuf *t;
284 if ((t = m_get(pData)) == NULL) goto dropfrag;
285 fp = mtod(t, struct ipq_t *);
286 insque_32(pData, fp, &ipq);
287 fp->ipq_ttl = IPFRAGTTL;
288 fp->ipq_p = ip->ip_p;
289 fp->ipq_id = ip->ip_id;
290 fp->ipq_next = fp->ipq_prev = ptr_to_u32(pData, (struct ipasfrag *)fp);
291 fp->ipq_src = ((struct ip *)ip)->ip_src;
292 fp->ipq_dst = ((struct ip *)ip)->ip_dst;
293 q = (struct ipasfrag *)fp;
294 goto insert;
295 }
296
297 /*
298 * Find a segment which begins after this one does.
299 */
300 for (q = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *); q != (struct ipasfrag *)fp;
301 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *))
302 if (q->ip_off > ip->ip_off)
303 break;
304
305 /*
306 * If there is a preceding segment, it may provide some of
307 * our data already. If so, drop the data from the incoming
308 * segment. If it provides all of our data, drop us.
309 */
310 if (u32_to_ptr(pData, q->ipf_prev, struct ipq_t *) != fp) {
311 i = (u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *))->ip_off +
312 (u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *))->ip_len - ip->ip_off;
313 if (i > 0) {
314 if (i >= ip->ip_len)
315 goto dropfrag;
316 m_adj(dtom(pData, ip), i);
317 ip->ip_off += i;
318 ip->ip_len -= i;
319 }
320 }
321
322 /*
323 * While we overlap succeeding segments trim them or,
324 * if they are completely covered, dequeue them.
325 */
326 while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
327 i = (ip->ip_off + ip->ip_len) - q->ip_off;
328 if (i < q->ip_len) {
329 q->ip_len -= i;
330 q->ip_off += i;
331 m_adj(dtom(pData, q), i);
332 break;
333 }
334 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *);
335 m_freem(pData, dtom(pData, u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *)));
336 ip_deq(pData, u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *));
337 }
338
339insert:
340 /*
341 * Stick new segment in its place;
342 * check for complete reassembly.
343 */
344 ip_enq(pData, ip, u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *));
345 next = 0;
346 for (q = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *); q != (struct ipasfrag *)fp;
347 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *)) {
348 if (q->ip_off != next)
349 return (0);
350 next += q->ip_len;
351 }
352 if (u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *)->ipf_mff & 1)
353 return (0);
354
355 /*
356 * Reassembly is complete; concatenate fragments.
357 */
358 q = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *);
359 m = dtom(pData, q);
360
361 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *);
362 while (q != (struct ipasfrag *)fp) {
363 struct mbuf *t;
364 t = dtom(pData, q);
365 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *);
366 m_cat(pData, m, t);
367 }
368
369 /*
370 * Create header for new ip packet by
371 * modifying header of first packet;
372 * dequeue and discard fragment reassembly header.
373 * Make header visible.
374 */
375 ip = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *);
376
377 /*
378 * If the fragments concatenated to an mbuf that's
379 * bigger than the total size of the fragment, then and
380 * m_ext buffer was alloced. But fp->ipq_next points to
381 * the old buffer (in the mbuf), so we must point ip
382 * into the new buffer.
383 */
384 if (m->m_flags & M_EXT) {
385 int delta;
386 delta = (char *)ip - m->m_dat;
387 ip = (struct ipasfrag *)(m->m_ext + delta);
388 }
389
390 /* DEBUG_ARG("ip = %lx", (long)ip);
391 * ip=(struct ipasfrag *)m->m_data; */
392
393 ip->ip_len = next;
394 ip->ipf_mff &= ~1;
395 ((struct ip *)ip)->ip_src = fp->ipq_src;
396 ((struct ip *)ip)->ip_dst = fp->ipq_dst;
397 remque_32(pData, fp);
398 (void) m_free(pData, dtom(pData, fp));
399 m = dtom(pData, ip);
400 m->m_len += (ip->ip_hl << 2);
401 m->m_data -= (ip->ip_hl << 2);
402
403 return ((struct ip *)ip);
404
405dropfrag:
406 ipstat.ips_fragdropped++;
407 m_freem(pData, m);
408 return (0);
409}
410
411/*
412 * Free a fragment reassembly header and all
413 * associated datagrams.
414 */
415void
416ip_freef(PNATState pData, struct ipq_t *fp)
417{
418 register struct ipasfrag *q, *p;
419
420 for (q = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *); q != (struct ipasfrag *)fp;
421 q = p) {
422 p = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *);
423 ip_deq(pData, q);
424 m_freem(pData, dtom(pData, q));
425 }
426 remque_32(pData, fp);
427 (void) m_free(pData, dtom(pData, fp));
428}
429#else /* !VBOX_WITH_BSD_REASS */
430struct mbuf *
431ip_reass(PNATState pData, struct mbuf* m) {
432 struct ip *ip;
433 struct mbuf *p, *q, *nq, *t;
434 struct ipq_t *fp = NULL;
435 struct ipqhead *head;
436 int i, hlen, next;
437 u_short hash;
438
439 /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
440 if (maxnipq == 0
441 || maxfragsperpacket == 0) {
442 ipstat.ips_fragments++;
443 ipstat.ips_fragdropped++;
444 m_freem(pData, m);
445 return (NULL);
446 }
447
448 ip = mtod(m, struct ip *);
449 hlen = ip->ip_hl << 2;
450
451 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
452 head = &ipq[hash];
453
454 /*
455 * Look for queue of fragments
456 * of this datagram.
457 */
458 TAILQ_FOREACH(fp, head, ipq_list)
459 if (ip->ip_id == fp->ipq_id &&
460 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
461 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
462 ip->ip_p == fp->ipq_p)
463 goto found;
464
465 fp = NULL;
466
467 /*
468 * Attempt to trim the number of allocated fragment queues if it
469 * exceeds the administrative limit.
470 */
471 if ((nipq > maxnipq) && (maxnipq > 0)) {
472 /*
473 * drop something from the tail of the current queue
474 * before proceeding further
475 */
476 struct ipq_t *q = TAILQ_LAST(head, ipqhead);
477 if (q == NULL) { /* gak */
478 for (i = 0; i < IPREASS_NHASH; i++) {
479 struct ipq_t *r = TAILQ_LAST(&ipq[i], ipqhead);
480 if (r) {
481 ipstat.ips_fragtimeout += r->ipq_nfrags;
482 ip_freef(pData, &ipq[i], r);
483 break;
484 }
485 }
486 } else {
487 ipstat.ips_fragtimeout += q->ipq_nfrags;
488 ip_freef(pData, head, q);
489 }
490 }
491
492found:
493 /*
494 * Adjust ip_len to not reflect header,
495 * convert offset of this to bytes.
496 */
497 ip->ip_len -= hlen;
498 if (ip->ip_off & IP_MF) {
499 /*
500 * Make sure that fragments have a data length
501 * that's a non-zero multiple of 8 bytes.
502 */
503 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
504 ipstat.ips_toosmall++; /* XXX */
505 goto dropfrag;
506 }
507 m->m_flags |= M_FRAG;
508 } else
509 m->m_flags &= ~M_FRAG;
510 ip->ip_off <<= 3;
511
512
513 /*
514 * Attempt reassembly; if it succeeds, proceed.
515 * ip_reass() will return a different mbuf.
516 */
517 ipstat.ips_fragments++;
518 m->m_hdr.header = ip;
519
520 /* Previous ip_reass() started here. */
521 /*
522 * Presence of header sizes in mbufs
523 * would confuse code below.
524 */
525 m->m_data += hlen;
526 m->m_len -= hlen;
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_hdr.header))
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#if 0
669 ip->ip_len = (ip->ip_hl << 2) + next;
670#else
671 ip->ip_len = next;
672#endif
673 ip->ip_src = fp->ipq_src;
674 ip->ip_dst = fp->ipq_dst;
675 TAILQ_REMOVE(head, fp, ipq_list);
676 nipq--;
677 free(fp);
678
679 m->m_len += (ip->ip_hl << 2);
680 m->m_data -= (ip->ip_hl << 2);
681 /* some debugging cruft by sklower, below, will go away soon */
682#if 0
683 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */
684 m_fixhdr(m);
685#endif
686 ipstat.ips_reassembled++;
687 return (m);
688
689dropfrag:
690 ipstat.ips_fragdropped++;
691 if (fp != NULL)
692 fp->ipq_nfrags--;
693 m_freem(pData, m);
694done:
695 return (NULL);
696
697#undef GETIP
698}
699
700void
701ip_freef(PNATState pData, struct ipqhead *fhp, struct ipq_t *fp) {
702 struct mbuf *q;
703
704 while (fp->ipq_frags) {
705 q = fp->ipq_frags;
706 fp->ipq_frags = q->m_nextpkt;
707 m_freem(pData, q);
708 }
709 TAILQ_REMOVE(fhp, fp, ipq_list);
710 free(fp);
711 nipq--;
712}
713#endif /* VBOX_WITH_BSD_REASS */
714
715#ifndef VBOX_WITH_BSD_REASS
716/*
717 * Put an ip fragment on a reassembly chain.
718 * Like insque, but pointers in middle of structure.
719 */
720void
721ip_enq(PNATState pData, register struct ipasfrag *p, register struct ipasfrag *prev)
722{
723 DEBUG_CALL("ip_enq");
724 DEBUG_ARG("prev = %lx", (long)prev);
725 p->ipf_prev = ptr_to_u32(pData, prev);
726 p->ipf_next = prev->ipf_next;
727 u32_to_ptr(pData, prev->ipf_next, struct ipasfrag *)->ipf_prev = ptr_to_u32(pData, p);
728 prev->ipf_next = ptr_to_u32(pData, p);
729}
730
731/*
732 * To ip_enq as remque is to insque.
733 */
734void
735ip_deq(PNATState pData, register struct ipasfrag *p)
736{
737 struct ipasfrag *prev = u32_to_ptr(pData, p->ipf_prev, struct ipasfrag *);
738 struct ipasfrag *next = u32_to_ptr(pData, p->ipf_next, struct ipasfrag *);
739 u32ptr_done(pData, prev->ipf_next, p);
740 prev->ipf_next = p->ipf_next;
741 next->ipf_prev = p->ipf_prev;
742}
743#endif /* !VBOX_WITH_BSD_REASS */
744
745/*
746 * IP timer processing;
747 * if a timer expires on a reassembly
748 * queue, discard it.
749 */
750void
751ip_slowtimo(PNATState pData)
752{
753 register struct ipq_t *fp;
754
755#ifndef VBOX_WITH_BSD_REASS
756 DEBUG_CALL("ip_slowtimo");
757
758 fp = u32_to_ptr(pData, ipq.next, struct ipq_t *);
759 if (fp == 0)
760 return;
761
762 while (fp != &ipq) {
763 --fp->ipq_ttl;
764 fp = u32_to_ptr(pData, fp->next, struct ipq_t *);
765 if (u32_to_ptr(pData, fp->prev, struct ipq_t *)->ipq_ttl == 0) {
766 ipstat.ips_fragtimeout++;
767 ip_freef(pData, u32_to_ptr(pData, fp->prev, struct ipq_t *));
768 }
769 }
770#else /* !VBOX_WITH_BSD_REASS */
771 /* XXX: the fragment expiration is the same but requier
772 * additional loop see (see ip_input.c in FreeBSD tree)
773 */
774 int i;
775 DEBUG_CALL("ip_slowtimo");
776 for (i = 0; i < IPREASS_NHASH; i++) {
777 for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
778 struct ipq_t *fpp;
779
780 fpp = fp;
781 fp = TAILQ_NEXT(fp, ipq_list);
782 if(--fpp->ipq_ttl == 0) {
783 ipstat.ips_fragtimeout += fpp->ipq_nfrags;
784 ip_freef(pData, &ipq[i], fpp);
785 }
786 }
787 }
788 /*
789 * If we are over the maximum number of fragments
790 * (due to the limit being lowered), drain off
791 * enough to get down to the new limit.
792 */
793 if (maxnipq >= 0 && nipq > maxnipq) {
794 for (i = 0; i < IPREASS_NHASH; i++) {
795 while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) {
796 ipstat.ips_fragdropped +=
797 TAILQ_FIRST(&ipq[i])->ipq_nfrags;
798 ip_freef(pData, &ipq[i], TAILQ_FIRST(&ipq[i]));
799 }
800 }
801 }
802#endif /* VBOX_WITH_BSD_REASS */
803}
804
805/*
806 * Do option processing on a datagram,
807 * possibly discarding it if bad options are encountered,
808 * or forwarding it if source-routed.
809 * Returns 1 if packet has been forwarded/freed,
810 * 0 if the packet should be processed further.
811 */
812
813#ifdef notdef
814
815int
816ip_dooptions(m)
817 struct mbuf *m;
818{
819 register struct ip *ip = mtod(m, struct ip *);
820 register u_char *cp;
821 register struct ip_timestamp *ipt;
822 register struct in_ifaddr *ia;
823 /* int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
824 int opt, optlen, cnt, off, code, type, forward = 0;
825 struct in_addr *sin, dst;
826 typedef u_int32_t n_time;
827 n_time ntime;
828
829 dst = ip->ip_dst;
830 cp = (u_char *)(ip + 1);
831 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
832 for (; cnt > 0; cnt -= optlen, cp += optlen) {
833 opt = cp[IPOPT_OPTVAL];
834 if (opt == IPOPT_EOL)
835 break;
836 if (opt == IPOPT_NOP)
837 optlen = 1;
838 else {
839 optlen = cp[IPOPT_OLEN];
840 if (optlen <= 0 || optlen > cnt) {
841 code = &cp[IPOPT_OLEN] - (u_char *)ip;
842 goto bad;
843 }
844 }
845 switch (opt) {
846
847 default:
848 break;
849
850 /*
851 * Source routing with record.
852 * Find interface with current destination address.
853 * If none on this machine then drop if strictly routed,
854 * or do nothing if loosely routed.
855 * Record interface address and bring up next address
856 * component. If strictly routed make sure next
857 * address is on directly accessible net.
858 */
859 case IPOPT_LSRR:
860 case IPOPT_SSRR:
861 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
862 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
863 goto bad;
864 }
865 ipaddr.sin_addr = ip->ip_dst;
866 ia = (struct in_ifaddr *)
867 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
868 if (ia == 0) {
869 if (opt == IPOPT_SSRR) {
870 type = ICMP_UNREACH;
871 code = ICMP_UNREACH_SRCFAIL;
872 goto bad;
873 }
874 /*
875 * Loose routing, and not at next destination
876 * yet; nothing to do except forward.
877 */
878 break;
879 }
880 off--; / * 0 origin * /
881 if (off > optlen - sizeof(struct in_addr)) {
882 /*
883 * End of source route. Should be for us.
884 */
885 save_rte(cp, ip->ip_src);
886 break;
887 }
888 /*
889 * locate outgoing interface
890 */
891 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
892 sizeof(ipaddr.sin_addr));
893 if (opt == IPOPT_SSRR) {
894#define INA struct in_ifaddr *
895#define SA struct sockaddr *
896 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
897 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
898 } else
899 ia = ip_rtaddr(ipaddr.sin_addr);
900 if (ia == 0) {
901 type = ICMP_UNREACH;
902 code = ICMP_UNREACH_SRCFAIL;
903 goto bad;
904 }
905 ip->ip_dst = ipaddr.sin_addr;
906 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
907 (caddr_t)(cp + off), sizeof(struct in_addr));
908 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
909 /*
910 * Let ip_intr's mcast routing check handle mcast pkts
911 */
912 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
913 break;
914
915 case IPOPT_RR:
916 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
917 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
918 goto bad;
919 }
920 /*
921 * If no space remains, ignore.
922 */
923 off--; * 0 origin *
924 if (off > optlen - sizeof(struct in_addr))
925 break;
926 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
927 sizeof(ipaddr.sin_addr));
928 /*
929 * locate outgoing interface; if we're the destination,
930 * use the incoming interface (should be same).
931 */
932 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
933 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
934 type = ICMP_UNREACH;
935 code = ICMP_UNREACH_HOST;
936 goto bad;
937 }
938 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
939 (caddr_t)(cp + off), sizeof(struct in_addr));
940 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
941 break;
942
943 case IPOPT_TS:
944 code = cp - (u_char *)ip;
945 ipt = (struct ip_timestamp *)cp;
946 if (ipt->ipt_len < 5)
947 goto bad;
948 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
949 if (++ipt->ipt_oflw == 0)
950 goto bad;
951 break;
952 }
953 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
954 switch (ipt->ipt_flg) {
955
956 case IPOPT_TS_TSONLY:
957 break;
958
959 case IPOPT_TS_TSANDADDR:
960 if (ipt->ipt_ptr + sizeof(n_time) +
961 sizeof(struct in_addr) > ipt->ipt_len)
962 goto bad;
963 ipaddr.sin_addr = dst;
964 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
965 m->m_pkthdr.rcvif);
966 if (ia == 0)
967 continue;
968 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
969 (caddr_t)sin, sizeof(struct in_addr));
970 ipt->ipt_ptr += sizeof(struct in_addr);
971 break;
972
973 case IPOPT_TS_PRESPEC:
974 if (ipt->ipt_ptr + sizeof(n_time) +
975 sizeof(struct in_addr) > ipt->ipt_len)
976 goto bad;
977 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
978 sizeof(struct in_addr));
979 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
980 continue;
981 ipt->ipt_ptr += sizeof(struct in_addr);
982 break;
983
984 default:
985 goto bad;
986 }
987 ntime = iptime();
988 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
989 sizeof(n_time));
990 ipt->ipt_ptr += sizeof(n_time);
991 }
992 }
993 if (forward) {
994 ip_forward(m, 1);
995 return (1);
996 }
997 }
998 }
999 return (0);
1000bad:
1001 /* ip->ip_len -= ip->ip_hl << 2; XXX icmp_error adds in hdr length */
1002
1003 /* Not yet */
1004 icmp_error(m, type, code, 0, 0);
1005
1006 ipstat.ips_badoptions++;
1007 return (1);
1008}
1009
1010#endif /* notdef */
1011
1012/*
1013 * Strip out IP options, at higher
1014 * level protocol in the kernel.
1015 * Second argument is buffer to which options
1016 * will be moved, and return value is their length.
1017 * (XXX) should be deleted; last arg currently ignored.
1018 */
1019void
1020ip_stripoptions(m, mopt)
1021 register struct mbuf *m;
1022 struct mbuf *mopt;
1023{
1024 register int i;
1025 struct ip *ip = mtod(m, struct ip *);
1026 register caddr_t opts;
1027 int olen;
1028
1029 olen = (ip->ip_hl<<2) - sizeof (struct ip);
1030 opts = (caddr_t)(ip + 1);
1031 i = m->m_len - (sizeof (struct ip) + olen);
1032 memcpy(opts, opts + olen, (unsigned)i);
1033 m->m_len -= olen;
1034
1035 ip->ip_hl = sizeof(struct ip) >> 2;
1036}
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