VirtualBox

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

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

csum validation hack, for testing IP integration of fragmentation routines borrowed from BSD

  • Property svn:eol-style set to native
File size: 27.2 KB
Line 
1/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
34 * ip_input.c,v 1.11 1994/11/16 10:17:08 jkh Exp
35 */
36
37/*
38 * Changes and additions relating to SLiRP are
39 * Copyright (c) 1995 Danny Gasparovski.
40 *
41 * Please read the file COPYRIGHT for the
42 * terms and conditions of the copyright.
43 */
44
45#include <slirp.h>
46#include "ip_icmp.h"
47
48
49/*
50 * IP initialization: fill in IP protocol switch table.
51 * All protocols not implemented in kernel go to raw IP protocol handler.
52 */
53void
54ip_init(PNATState pData)
55{
56#ifndef VBOX_WITH_BSD_REASS
57 ipq.next = ipq.prev = ptr_to_u32(pData, &ipq);
58#else /* !VBOX_WITH_BSD_REASS */
59 int i = 0;
60 for (i = 0; i < IPREASS_NHASH; ++i)
61 TAILQ_INIT(&ipq[i]);
62 maxnipq = 100; /* ??? */
63 maxfragsperpacket = 16;
64 nipq = 0;
65#endif /* VBOX_WITH_BSD_REASS */
66 ip_currid = tt.tv_sec & 0xffff;
67 udp_init(pData);
68 tcp_init(pData);
69}
70
71/*
72 * Ip input routine. Checksum and byte swap header. If fragmented
73 * try to reassemble. Process options. Pass to next level.
74 */
75void
76ip_input(PNATState pData, struct mbuf *m)
77{
78 register struct ip *ip;
79 int hlen;
80
81 DEBUG_CALL("ip_input");
82 DEBUG_ARG("m = %lx", (long)m);
83 DEBUG_ARG("m_len = %d", m->m_len);
84
85 ipstat.ips_total++;
86
87 if (m->m_len < sizeof (struct ip)) {
88 ipstat.ips_toosmall++;
89 return;
90 }
91
92 ip = mtod(m, struct ip *);
93
94 if (ip->ip_v != IPVERSION) {
95 ipstat.ips_badvers++;
96 goto bad;
97 }
98
99 hlen = ip->ip_hl << 2;
100 if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
101 ipstat.ips_badhlen++; /* or packet too short */
102 goto bad;
103 }
104
105 /* keep ip header intact for ICMP reply
106 * ip->ip_sum = cksum(m, hlen);
107 * if (ip->ip_sum) {
108 */
109 if(cksum(m,hlen)) {
110 ipstat.ips_badsum++;
111 goto bad;
112 }
113
114 /*
115 * Convert fields to host representation.
116 */
117 NTOHS(ip->ip_len);
118 if (ip->ip_len < hlen) {
119 ipstat.ips_badlen++;
120 goto bad;
121 }
122 NTOHS(ip->ip_id);
123 NTOHS(ip->ip_off);
124
125 /*
126 * Check that the amount of data in the buffers
127 * is as at least much as the IP header would have us expect.
128 * Trim mbufs if longer than we expect.
129 * Drop packet if shorter than we expect.
130 */
131 if (m->m_len < ip->ip_len) {
132 ipstat.ips_tooshort++;
133 goto bad;
134 }
135 /* Should drop packet if mbuf too long? hmmm... */
136 if (m->m_len > ip->ip_len)
137 m_adj(m, ip->ip_len - m->m_len);
138
139 /* check ip_ttl for a correct ICMP reply */
140 if(ip->ip_ttl==0 || ip->ip_ttl==1) {
141 icmp_error(pData, m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");
142 goto bad;
143 }
144
145 /*
146 * Process options and, if not destined for us,
147 * ship it on. ip_dooptions returns 1 when an
148 * error was detected (causing an icmp message
149 * to be sent and the original packet to be freed).
150 */
151/* We do no IP options */
152/* if (hlen > sizeof (struct ip) && ip_dooptions(m))
153 * goto next;
154 */
155 /*
156 * If offset or IP_MF are set, must reassemble.
157 * Otherwise, nothing need be done.
158 * (We could look in the reassembly queue to see
159 * if the packet was previously fragmented,
160 * but it's not worth the time; just let them time out.)
161 *
162 * XXX This should fail, don't fragment yet
163 */
164#ifndef VBOX_WITH_BSD_REASS
165 if (ip->ip_off &~ IP_DF) {
166 register struct ipq_t *fp;
167 /*
168 * Look for queue of fragments
169 * of this datagram.
170 */
171 for (fp = u32_to_ptr(pData, ipq.next, struct ipq_t *); fp != &ipq;
172 fp = u32_to_ptr(pData, fp->next, struct ipq_t *))
173 if (ip->ip_id == fp->ipq_id &&
174 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
175 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
176 ip->ip_p == fp->ipq_p)
177 goto found;
178 fp = 0;
179 found:
180
181 /*
182 * Adjust ip_len to not reflect header,
183 * set ip_mff if more fragments are expected,
184 * convert offset of this to bytes.
185 */
186 ip->ip_len -= hlen;
187 if (ip->ip_off & IP_MF)
188 ((struct ipasfrag *)ip)->ipf_mff |= 1;
189 else
190 ((struct ipasfrag *)ip)->ipf_mff &= ~1;
191
192 ip->ip_off <<= 3;
193
194 /*
195 * If datagram marked as having more fragments
196 * or if this is not the first fragment,
197 * attempt reassembly; if it succeeds, proceed.
198 */
199 if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) {
200 ipstat.ips_fragments++;
201 ip = ip_reass(pData, (struct ipasfrag *)ip, fp);
202 if (ip == 0)
203 return;
204 ipstat.ips_reassembled++;
205 m = dtom(pData, ip);
206 } else
207 if (fp)
208 ip_freef(pData, fp);
209
210 } else
211 ip->ip_len -= hlen;
212#else /* !VBOX_WITH_BSD_REASS */
213 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
214 m = ip_reass(pData, m);
215 if (m == NULL)
216 return;
217 ip = mtod(m, struct ip *);
218 hlen = ip->ip_len;
219 }
220 else
221 ip->ip_len -= hlen;
222#endif /* !VBOX_WITH_BSD_REASS */
223
224 /*
225 * Switch out to protocol's input routine.
226 */
227 ipstat.ips_delivered++;
228 switch (ip->ip_p) {
229 case IPPROTO_TCP:
230 tcp_input(pData, m, hlen, (struct socket *)NULL);
231 break;
232 case IPPROTO_UDP:
233 udp_input(pData, m, hlen);
234 break;
235 case IPPROTO_ICMP:
236 icmp_input(pData, m, hlen);
237 break;
238 default:
239 ipstat.ips_noproto++;
240 m_free(pData, m);
241 }
242 return;
243bad:
244 m_freem(pData, m);
245 return;
246}
247
248#ifndef VBOX_WITH_BSD_REASS
249/*
250 * Take incoming datagram fragment and try to
251 * reassemble it into whole datagram. If a chain for
252 * reassembly of this datagram already exists, then it
253 * is given as fp; otherwise have to make a chain.
254 */
255struct ip *
256ip_reass(PNATState pData, register struct ipasfrag *ip, register struct ipq_t *fp)
257{
258 register struct mbuf *m = dtom(pData, ip);
259 register struct ipasfrag *q;
260 int hlen = ip->ip_hl << 2;
261 int i, next;
262
263 DEBUG_CALL("ip_reass");
264 DEBUG_ARG("ip = %lx", (long)ip);
265 DEBUG_ARG("fp = %lx", (long)fp);
266 DEBUG_ARG("m = %lx", (long)m);
267
268 /*
269 * Presence of header sizes in mbufs
270 * would confuse code below.
271 * Fragment m_data is concatenated.
272 */
273 m->m_data += hlen;
274 m->m_len -= hlen;
275
276 /*
277 * If first fragment to arrive, create a reassembly queue.
278 */
279 if (fp == 0) {
280 struct mbuf *t;
281 if ((t = m_get(pData)) == NULL) goto dropfrag;
282 fp = mtod(t, struct ipq_t *);
283 insque_32(pData, fp, &ipq);
284 fp->ipq_ttl = IPFRAGTTL;
285 fp->ipq_p = ip->ip_p;
286 fp->ipq_id = ip->ip_id;
287 fp->ipq_next = fp->ipq_prev = ptr_to_u32(pData, (struct ipasfrag *)fp);
288 fp->ipq_src = ((struct ip *)ip)->ip_src;
289 fp->ipq_dst = ((struct ip *)ip)->ip_dst;
290 q = (struct ipasfrag *)fp;
291 goto insert;
292 }
293
294 /*
295 * Find a segment which begins after this one does.
296 */
297 for (q = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *); q != (struct ipasfrag *)fp;
298 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *))
299 if (q->ip_off > ip->ip_off)
300 break;
301
302 /*
303 * If there is a preceding segment, it may provide some of
304 * our data already. If so, drop the data from the incoming
305 * segment. If it provides all of our data, drop us.
306 */
307 if (u32_to_ptr(pData, q->ipf_prev, struct ipq_t *) != fp) {
308 i = (u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *))->ip_off +
309 (u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *))->ip_len - ip->ip_off;
310 if (i > 0) {
311 if (i >= ip->ip_len)
312 goto dropfrag;
313 m_adj(dtom(pData, ip), i);
314 ip->ip_off += i;
315 ip->ip_len -= i;
316 }
317 }
318
319 /*
320 * While we overlap succeeding segments trim them or,
321 * if they are completely covered, dequeue them.
322 */
323 while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
324 i = (ip->ip_off + ip->ip_len) - q->ip_off;
325 if (i < q->ip_len) {
326 q->ip_len -= i;
327 q->ip_off += i;
328 m_adj(dtom(pData, q), i);
329 break;
330 }
331 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *);
332 m_freem(pData, dtom(pData, u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *)));
333 ip_deq(pData, u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *));
334 }
335
336insert:
337 /*
338 * Stick new segment in its place;
339 * check for complete reassembly.
340 */
341 ip_enq(pData, ip, u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *));
342 next = 0;
343 for (q = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *); q != (struct ipasfrag *)fp;
344 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *)) {
345 if (q->ip_off != next)
346 return (0);
347 next += q->ip_len;
348 }
349 if (u32_to_ptr(pData, q->ipf_prev, struct ipasfrag *)->ipf_mff & 1)
350 return (0);
351
352 /*
353 * Reassembly is complete; concatenate fragments.
354 */
355 q = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *);
356 m = dtom(pData, q);
357
358 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *);
359 while (q != (struct ipasfrag *)fp) {
360 struct mbuf *t;
361 t = dtom(pData, q);
362 q = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *);
363 m_cat(pData, m, t);
364 }
365
366 /*
367 * Create header for new ip packet by
368 * modifying header of first packet;
369 * dequeue and discard fragment reassembly header.
370 * Make header visible.
371 */
372 ip = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *);
373
374 /*
375 * If the fragments concatenated to an mbuf that's
376 * bigger than the total size of the fragment, then and
377 * m_ext buffer was alloced. But fp->ipq_next points to
378 * the old buffer (in the mbuf), so we must point ip
379 * into the new buffer.
380 */
381 if (m->m_flags & M_EXT) {
382 int delta;
383 delta = (char *)ip - m->m_dat;
384 ip = (struct ipasfrag *)(m->m_ext + delta);
385 }
386
387 /* DEBUG_ARG("ip = %lx", (long)ip);
388 * ip=(struct ipasfrag *)m->m_data; */
389
390 ip->ip_len = next;
391 ip->ipf_mff &= ~1;
392 ((struct ip *)ip)->ip_src = fp->ipq_src;
393 ((struct ip *)ip)->ip_dst = fp->ipq_dst;
394 remque_32(pData, fp);
395 (void) m_free(pData, dtom(pData, fp));
396 m = dtom(pData, ip);
397 m->m_len += (ip->ip_hl << 2);
398 m->m_data -= (ip->ip_hl << 2);
399
400 return ((struct ip *)ip);
401
402dropfrag:
403 ipstat.ips_fragdropped++;
404 m_freem(pData, m);
405 return (0);
406}
407
408/*
409 * Free a fragment reassembly header and all
410 * associated datagrams.
411 */
412void
413ip_freef(PNATState pData, struct ipq_t *fp)
414{
415 register struct ipasfrag *q, *p;
416
417 for (q = u32_to_ptr(pData, fp->ipq_next, struct ipasfrag *); q != (struct ipasfrag *)fp;
418 q = p) {
419 p = u32_to_ptr(pData, q->ipf_next, struct ipasfrag *);
420 ip_deq(pData, q);
421 m_freem(pData, dtom(pData, q));
422 }
423 remque_32(pData, fp);
424 (void) m_free(pData, dtom(pData, fp));
425}
426#else /* !VBOX_WITH_BSD_REASS */
427struct mbuf *
428ip_reass(PNATState pData, struct mbuf* m) {
429 struct ip *ip;
430 struct mbuf *p, *q, *nq, *t;
431 struct ipq_t *fp = NULL;
432 struct ipqhead *head;
433 int i, hlen, next;
434 u_int8_t ecn, ecn0;
435 u_short hash;
436
437 /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
438 if (maxnipq == 0
439 || maxfragsperpacket == 0) {
440 ipstat.ips_fragments++;
441 ipstat.ips_fragdropped++;
442 m_freem(pData, m);
443 return (NULL);
444 }
445
446 ip = mtod(m, struct ip *);
447 hlen = ip->ip_hl << 2;
448
449 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
450 head = &ipq[hash];
451
452 /*
453 * Look for queue of fragments
454 * of this datagram.
455 */
456 TAILQ_FOREACH(fp, head, ipq_list)
457 if (ip->ip_id == fp->ipq_id &&
458 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
459 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
460 ip->ip_p == fp->ipq_p)
461 goto found;
462
463 fp = NULL;
464
465 /*
466 * Attempt to trim the number of allocated fragment queues if it
467 * exceeds the administrative limit.
468 */
469 if ((nipq > maxnipq) && (maxnipq > 0)) {
470 /*
471 * drop something from the tail of the current queue
472 * before proceeding further
473 */
474 struct ipq_t *q = TAILQ_LAST(head, ipqhead);
475 if (q == NULL) { /* gak */
476 for (i = 0; i < IPREASS_NHASH; i++) {
477 struct ipq_t *r = TAILQ_LAST(&ipq[i], ipqhead);
478 if (r) {
479 ipstat.ips_fragtimeout += r->ipq_nfrags;
480 ip_freef(pData, &ipq[i], r);
481 break;
482 }
483 }
484 } else {
485 ipstat.ips_fragtimeout += q->ipq_nfrags;
486 ip_freef(pData, head, q);
487 }
488 }
489
490found:
491 /*
492 * Adjust ip_len to not reflect header,
493 * convert offset of this to bytes.
494 */
495 ip->ip_len -= hlen;
496 if (ip->ip_off & IP_MF) {
497 /*
498 * Make sure that fragments have a data length
499 * that's a non-zero multiple of 8 bytes.
500 */
501 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
502 ipstat.ips_toosmall++; /* XXX */
503 goto dropfrag;
504 }
505 m->m_flags |= M_FRAG;
506 } else
507 m->m_flags &= ~M_FRAG;
508 ip->ip_off <<= 3;
509
510
511 /*
512 * Attempt reassembly; if it succeeds, proceed.
513 * ip_reass() will return a different mbuf.
514 */
515 ipstat.ips_fragments++;
516 m->m_data = (caddr_t)ip;
517
518 /* Previous ip_reass() started here. */
519 /*
520 * Presence of header sizes in mbufs
521 * would confuse code below.
522 */
523#if 0
524 m->m_data += hlen;
525 m->m_len -= hlen;
526#endif
527
528 /*
529 * If first fragment to arrive, create a reassembly queue.
530 */
531 if (fp == NULL) {
532 fp = malloc(sizeof(struct ipq_t));
533 if (fp == NULL)
534 goto dropfrag;
535 TAILQ_INSERT_HEAD(head, fp, ipq_list);
536 nipq++;
537 fp->ipq_nfrags = 1;
538 fp->ipq_ttl = IPFRAGTTL;
539 fp->ipq_p = ip->ip_p;
540 fp->ipq_id = ip->ip_id;
541 fp->ipq_src = ip->ip_src;
542 fp->ipq_dst = ip->ip_dst;
543 fp->ipq_frags = m;
544 m->m_nextpkt = NULL;
545 goto done;
546 } else {
547 fp->ipq_nfrags++;
548 }
549
550#define GETIP(m) ((struct ip*)((m)->m_data))
551
552
553 /*
554 * Find a segment which begins after this one does.
555 */
556 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
557 if (GETIP(q)->ip_off > ip->ip_off)
558 break;
559
560 /*
561 * If there is a preceding segment, it may provide some of
562 * our data already. If so, drop the data from the incoming
563 * segment. If it provides all of our data, drop us, otherwise
564 * stick new segment in the proper place.
565 *
566 * If some of the data is dropped from the the preceding
567 * segment, then it's checksum is invalidated.
568 */
569 if (p) {
570 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
571 if (i > 0) {
572 if (i >= ip->ip_len)
573 goto dropfrag;
574 m_adj(m, i);
575 ip->ip_off += i;
576 ip->ip_len -= i;
577 }
578 m->m_nextpkt = p->m_nextpkt;
579 p->m_nextpkt = m;
580 } else {
581 m->m_nextpkt = fp->ipq_frags;
582 fp->ipq_frags = m;
583 }
584
585 /*
586 * While we overlap succeeding segments trim them or,
587 * if they are completely covered, dequeue them.
588 */
589 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
590 q = nq) {
591 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
592 if (i < GETIP(q)->ip_len) {
593 GETIP(q)->ip_len -= i;
594 GETIP(q)->ip_off += i;
595 m_adj(q, i);
596 break;
597 }
598 nq = q->m_nextpkt;
599 m->m_nextpkt = nq;
600 ipstat.ips_fragdropped++;
601 fp->ipq_nfrags--;
602 m_freem(pData, q);
603 }
604
605 /*
606 * Check for complete reassembly and perform frag per packet
607 * limiting.
608 *
609 * Frag limiting is performed here so that the nth frag has
610 * a chance to complete the packet before we drop the packet.
611 * As a result, n+1 frags are actually allowed per packet, but
612 * only n will ever be stored. (n = maxfragsperpacket.)
613 *
614 */
615 next = 0;
616 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
617 if (GETIP(q)->ip_off != next) {
618 if (fp->ipq_nfrags > maxfragsperpacket) {
619 ipstat.ips_fragdropped += fp->ipq_nfrags;
620 ip_freef(pData, head, fp);
621 }
622 goto done;
623 }
624 next += GETIP(q)->ip_len;
625 }
626 /* Make sure the last packet didn't have the IP_MF flag */
627 if (p->m_flags & M_FRAG) {
628 if (fp->ipq_nfrags > maxfragsperpacket) {
629 ipstat.ips_fragdropped += fp->ipq_nfrags;
630 ip_freef(pData, head, fp);
631 }
632 goto done;
633 }
634
635 /*
636 * Reassembly is complete. Make sure the packet is a sane size.
637 */
638 q = fp->ipq_frags;
639 ip = GETIP(q);
640 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
641 ipstat.ips_fragdropped += fp->ipq_nfrags;
642 ip_freef(pData, head, fp);
643 goto done;
644 }
645
646 /*
647 * Concatenate fragments.
648 */
649 m = q;
650#if 0
651 t = m->m_next;
652 m->m_next = NULL;
653 m_cat(pData, m, t);
654#endif
655 nq = q->m_nextpkt;
656 q->m_nextpkt = NULL;
657 for (q = nq; q != NULL; q = nq) {
658 nq = q->m_nextpkt;
659 q->m_nextpkt = NULL;
660 m_cat(pData, m, q);
661 }
662
663 /*
664 * Create header for new ip packet by modifying header of first
665 * packet; dequeue and discard fragment reassembly header.
666 * Make header visible.
667 */
668 ip->ip_len = (ip->ip_hl << 2) + next;
669 ip->ip_src = fp->ipq_src;
670 ip->ip_dst = fp->ipq_dst;
671#ifdef VBOX_WITH_BSD_REASS_CKSUM_HACK
672 ip->ip_sum = 0;
673 m->m_sum_recalculate = 1;
674#endif /* VBOX_WITH_BSD_REASS_CKSUM_HACK */
675 TAILQ_REMOVE(head, fp, ipq_list);
676 nipq--;
677 free(fp);
678
679 m->m_len += (ip->ip_hl << 2);
680#if 0
681 m->m_data -= (ip->ip_hl << 2);
682#endif
683 /* some debugging cruft by sklower, below, will go away soon */
684#if 0
685 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */
686 m_fixhdr(m);
687#endif
688 ipstat.ips_reassembled++;
689 return (m);
690
691dropfrag:
692 ipstat.ips_fragdropped++;
693 if (fp != NULL)
694 fp->ipq_nfrags--;
695 m_freem(pData, m);
696done:
697 return (NULL);
698
699#undef GETIP
700}
701
702void
703ip_freef(PNATState pData, struct ipqhead *fhp, struct ipq_t *fp) {
704 struct mbuf *q;
705
706 while (fp->ipq_frags) {
707 q = fp->ipq_frags;
708 fp->ipq_frags = q->m_nextpkt;
709 m_freem(pData, q);
710 }
711 TAILQ_REMOVE(fhp, fp, ipq_list);
712 free(fp);
713 nipq--;
714}
715#endif /* VBOX_WITH_BSD_REASS */
716
717/*
718 * Put an ip fragment on a reassembly chain.
719 * Like insque, but pointers in middle of structure.
720 */
721void
722ip_enq(PNATState pData, register struct ipasfrag *p, register struct ipasfrag *prev)
723{
724 DEBUG_CALL("ip_enq");
725 DEBUG_ARG("prev = %lx", (long)prev);
726 p->ipf_prev = ptr_to_u32(pData, prev);
727 p->ipf_next = prev->ipf_next;
728 u32_to_ptr(pData, prev->ipf_next, struct ipasfrag *)->ipf_prev = ptr_to_u32(pData, p);
729 prev->ipf_next = ptr_to_u32(pData, p);
730}
731
732/*
733 * To ip_enq as remque is to insque.
734 */
735void
736ip_deq(PNATState pData, register struct ipasfrag *p)
737{
738 struct ipasfrag *prev = u32_to_ptr(pData, p->ipf_prev, struct ipasfrag *);
739 struct ipasfrag *next = u32_to_ptr(pData, p->ipf_next, struct ipasfrag *);
740 u32ptr_done(pData, prev->ipf_next, p);
741 prev->ipf_next = p->ipf_next;
742 next->ipf_prev = p->ipf_prev;
743}
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;
826typedef 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.

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