VirtualBox

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

Last change on this file since 34087 was 34087, checked in by vboxsync, 14 years ago

NAT: typo.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.6 KB
Line 
1/* $Id: ip_input.c 34087 2010-11-15 18:20:20Z vboxsync $ */
2/** @file
3 * NAT - IP input.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*
19 * This code is based on:
20 *
21 * Copyright (c) 1982, 1986, 1988, 1993
22 * The Regents of the University of California. All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
53 * ip_input.c,v 1.11 1994/11/16 10:17:08 jkh Exp
54 */
55
56/*
57 * Changes and additions relating to SLiRP are
58 * Copyright (c) 1995 Danny Gasparovski.
59 *
60 * Please read the file COPYRIGHT for the
61 * terms and conditions of the copyright.
62 */
63
64#include <slirp.h>
65#include "ip_icmp.h"
66#include "alias.h"
67
68
69/*
70 * IP initialization: fill in IP protocol switch table.
71 * All protocols not implemented in kernel go to raw IP protocol handler.
72 */
73void
74ip_init(PNATState pData)
75{
76 int i = 0;
77 for (i = 0; i < IPREASS_NHASH; ++i)
78 TAILQ_INIT(&ipq[i]);
79 maxnipq = 100; /* ??? */
80 maxfragsperpacket = 16;
81 nipq = 0;
82 ip_currid = tt.tv_sec & 0xffff;
83 udp_init(pData);
84 tcp_init(pData);
85}
86
87static struct libalias *select_alias(PNATState pData, struct mbuf* m)
88{
89 struct libalias *la = pData->proxy_alias;
90 struct udphdr *udp = NULL;
91 struct ip *pip = NULL;
92
93 struct m_tag *t;
94 if ((t = m_tag_find(m, PACKET_TAG_ALIAS, NULL)) != 0)
95 return (struct libalias *)&t[1];
96
97 return la;
98}
99
100/*
101 * Ip input routine. Checksum and byte swap header. If fragmented
102 * try to reassemble. Process options. Pass to next level.
103 */
104void
105ip_input(PNATState pData, struct mbuf *m)
106{
107 register struct ip *ip;
108 int hlen = 0;
109 int mlen = 0;
110
111 STAM_PROFILE_START(&pData->StatIP_input, a);
112
113 DEBUG_CALL("ip_input");
114 DEBUG_ARG("m = %lx", (long)m);
115 ip = mtod(m, struct ip *);
116 Log2(("ip_dst=%R[IP4](len:%d) m_len = %d\n", &ip->ip_dst, RT_N2H_U16(ip->ip_len), m->m_len));
117
118 ipstat.ips_total++;
119 {
120 int rc;
121 STAM_PROFILE_START(&pData->StatALIAS_input, b);
122 rc = LibAliasIn(select_alias(pData, m), mtod(m, char *), m_length(m, NULL));
123 STAM_PROFILE_STOP(&pData->StatALIAS_input, b);
124 Log2(("NAT: LibAlias return %d\n", rc));
125 if (m->m_len != RT_N2H_U16(ip->ip_len))
126 m->m_len = RT_N2H_U16(ip->ip_len);
127 }
128
129 mlen = m->m_len;
130
131 if (mlen < sizeof(struct ip))
132 {
133 ipstat.ips_toosmall++;
134 STAM_PROFILE_STOP(&pData->StatIP_input, a);
135 return;
136 }
137
138 ip = mtod(m, struct ip *);
139 if (ip->ip_v != IPVERSION)
140 {
141 ipstat.ips_badvers++;
142 goto bad_free_m;
143 }
144
145 hlen = ip->ip_hl << 2;
146 if ( hlen < sizeof(struct ip)
147 || hlen > m->m_len)
148 {
149 /* min header length */
150 ipstat.ips_badhlen++; /* or packet too short */
151 goto bad_free_m;
152 }
153
154 /* keep ip header intact for ICMP reply
155 * ip->ip_sum = cksum(m, hlen);
156 * if (ip->ip_sum) {
157 */
158 if (cksum(m, hlen))
159 {
160 ipstat.ips_badsum++;
161 goto bad_free_m;
162 }
163
164 /*
165 * Convert fields to host representation.
166 */
167 NTOHS(ip->ip_len);
168 if (ip->ip_len < hlen)
169 {
170 ipstat.ips_badlen++;
171 goto bad_free_m;
172 }
173
174 NTOHS(ip->ip_id);
175 NTOHS(ip->ip_off);
176
177 /*
178 * Check that the amount of data in the buffers
179 * is as at least much as the IP header would have us expect.
180 * Trim mbufs if longer than we expect.
181 * Drop packet if shorter than we expect.
182 */
183 if (mlen < ip->ip_len)
184 {
185 ipstat.ips_tooshort++;
186 goto bad_free_m;
187 }
188
189 /* Should drop packet if mbuf too long? hmmm... */
190 if (mlen > ip->ip_len)
191 m_adj(m, ip->ip_len - m->m_len);
192
193 /* check ip_ttl for a correct ICMP reply */
194 if (ip->ip_ttl==0 || ip->ip_ttl == 1)
195 {
196 icmp_error(pData, m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, "ttl");
197 goto bad_free_m;
198 }
199
200 ip->ip_ttl--;
201 /*
202 * If offset or IP_MF are set, must reassemble.
203 * Otherwise, nothing need be done.
204 * (We could look in the reassembly queue to see
205 * if the packet was previously fragmented,
206 * but it's not worth the time; just let them time out.)
207 *
208 */
209 if (ip->ip_off & (IP_MF | IP_OFFMASK))
210 {
211 m = ip_reass(pData, m);
212 if (m == NULL)
213 {
214 STAM_PROFILE_STOP(&pData->StatIP_input, a);
215 return;
216 }
217 ip = mtod(m, struct ip *);
218 hlen = ip->ip_hl << 2;
219 }
220 else
221 ip->ip_len -= hlen;
222
223 /*
224 * Switch out to protocol's input routine.
225 */
226 ipstat.ips_delivered++;
227 switch (ip->ip_p)
228 {
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_freem(pData, m);
241 }
242 STAM_PROFILE_STOP(&pData->StatIP_input, a);
243 return;
244
245bad_free_m:
246 Log2(("NAT: IP datagram to %R[IP4] with size(%d) claimed as bad\n",
247 &ip->ip_dst, ip->ip_len));
248 m_freem(pData, m);
249 STAM_PROFILE_STOP(&pData->StatIP_input, a);
250 return;
251}
252
253struct mbuf *
254ip_reass(PNATState pData, struct mbuf* m)
255{
256 struct ip *ip;
257 struct mbuf *p, *q, *nq;
258 struct ipq_t *fp = NULL;
259 struct ipqhead *head;
260 int i, hlen, next;
261 u_short hash;
262
263 /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
264 if ( maxnipq == 0
265 || maxfragsperpacket == 0)
266 {
267 ipstat.ips_fragments++;
268 ipstat.ips_fragdropped++;
269 m_freem(pData, m);
270 return (NULL);
271 }
272
273 ip = mtod(m, struct ip *);
274 hlen = ip->ip_hl << 2;
275
276 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
277 head = &ipq[hash];
278
279 /*
280 * Look for queue of fragments
281 * of this datagram.
282 */
283 TAILQ_FOREACH(fp, head, ipq_list)
284 if (ip->ip_id == fp->ipq_id &&
285 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
286 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
287 ip->ip_p == fp->ipq_p)
288 goto found;
289
290 fp = NULL;
291
292 /*
293 * Attempt to trim the number of allocated fragment queues if it
294 * exceeds the administrative limit.
295 */
296 if ((nipq > maxnipq) && (maxnipq > 0))
297 {
298 /*
299 * drop something from the tail of the current queue
300 * before proceeding further
301 */
302 struct ipq_t *pHead = TAILQ_LAST(head, ipqhead);
303 if (pHead == NULL)
304 {
305 /* gak */
306 for (i = 0; i < IPREASS_NHASH; i++)
307 {
308 struct ipq_t *pTail = TAILQ_LAST(&ipq[i], ipqhead);
309 if (pTail)
310 {
311 ipstat.ips_fragtimeout += pTail->ipq_nfrags;
312 ip_freef(pData, &ipq[i], pTail);
313 break;
314 }
315 }
316 }
317 else
318 {
319 ipstat.ips_fragtimeout += pHead->ipq_nfrags;
320 ip_freef(pData, head, pHead);
321 }
322 }
323
324found:
325 /*
326 * Adjust ip_len to not reflect header,
327 * convert offset of this to bytes.
328 */
329 ip->ip_len -= hlen;
330 if (ip->ip_off & IP_MF)
331 {
332 /*
333 * Make sure that fragments have a data length
334 * that's a non-zero multiple of 8 bytes.
335 */
336 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0)
337 {
338 ipstat.ips_toosmall++; /* XXX */
339 goto dropfrag;
340 }
341 m->m_flags |= M_FRAG;
342 }
343 else
344 m->m_flags &= ~M_FRAG;
345 ip->ip_off <<= 3;
346
347
348 /*
349 * Attempt reassembly; if it succeeds, proceed.
350 * ip_reass() will return a different mbuf.
351 */
352 ipstat.ips_fragments++;
353
354 /* Previous ip_reass() started here. */
355 /*
356 * Presence of header sizes in mbufs
357 * would confuse code below.
358 */
359 m->m_data += hlen;
360 m->m_len -= hlen;
361
362 /*
363 * If first fragment to arrive, create a reassembly queue.
364 */
365 if (fp == NULL)
366 {
367 fp = RTMemAlloc(sizeof(struct ipq_t));
368 if (fp == NULL)
369 goto dropfrag;
370 TAILQ_INSERT_HEAD(head, fp, ipq_list);
371 nipq++;
372 fp->ipq_nfrags = 1;
373 fp->ipq_ttl = IPFRAGTTL;
374 fp->ipq_p = ip->ip_p;
375 fp->ipq_id = ip->ip_id;
376 fp->ipq_src = ip->ip_src;
377 fp->ipq_dst = ip->ip_dst;
378 fp->ipq_frags = m;
379 m->m_nextpkt = NULL;
380 goto done;
381 }
382 else
383 {
384 fp->ipq_nfrags++;
385 }
386
387#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
388
389 /*
390 * Find a segment which begins after this one does.
391 */
392 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
393 if (GETIP(q)->ip_off > ip->ip_off)
394 break;
395
396 /*
397 * If there is a preceding segment, it may provide some of
398 * our data already. If so, drop the data from the incoming
399 * segment. If it provides all of our data, drop us, otherwise
400 * stick new segment in the proper place.
401 *
402 * If some of the data is dropped from the the preceding
403 * segment, then it's checksum is invalidated.
404 */
405 if (p)
406 {
407 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
408 if (i > 0)
409 {
410 if (i >= ip->ip_len)
411 goto dropfrag;
412 m_adj(m, i);
413 ip->ip_off += i;
414 ip->ip_len -= i;
415 }
416 m->m_nextpkt = p->m_nextpkt;
417 p->m_nextpkt = m;
418 }
419 else
420 {
421 m->m_nextpkt = fp->ipq_frags;
422 fp->ipq_frags = m;
423 }
424
425 /*
426 * While we overlap succeeding segments trim them or,
427 * if they are completely covered, dequeue them.
428 */
429 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
430 q = nq)
431 {
432 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
433 if (i < GETIP(q)->ip_len)
434 {
435 GETIP(q)->ip_len -= i;
436 GETIP(q)->ip_off += i;
437 m_adj(q, i);
438 break;
439 }
440 nq = q->m_nextpkt;
441 m->m_nextpkt = nq;
442 ipstat.ips_fragdropped++;
443 fp->ipq_nfrags--;
444 m_freem(pData, q);
445 }
446
447 /*
448 * Check for complete reassembly and perform frag per packet
449 * limiting.
450 *
451 * Frag limiting is performed here so that the nth frag has
452 * a chance to complete the packet before we drop the packet.
453 * As a result, n+1 frags are actually allowed per packet, but
454 * only n will ever be stored. (n = maxfragsperpacket.)
455 *
456 */
457 next = 0;
458 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
459 {
460 if (GETIP(q)->ip_off != next)
461 {
462 if (fp->ipq_nfrags > maxfragsperpacket)
463 {
464 ipstat.ips_fragdropped += fp->ipq_nfrags;
465 ip_freef(pData, head, fp);
466 }
467 goto done;
468 }
469 next += GETIP(q)->ip_len;
470 }
471 /* Make sure the last packet didn't have the IP_MF flag */
472 if (p->m_flags & M_FRAG)
473 {
474 if (fp->ipq_nfrags > maxfragsperpacket)
475 {
476 ipstat.ips_fragdropped += fp->ipq_nfrags;
477 ip_freef(pData, head, fp);
478 }
479 goto done;
480 }
481
482 /*
483 * Reassembly is complete. Make sure the packet is a sane size.
484 */
485 q = fp->ipq_frags;
486 ip = GETIP(q);
487 hlen = ip->ip_hl << 2;
488 if (next + hlen > IP_MAXPACKET)
489 {
490 ipstat.ips_fragdropped += fp->ipq_nfrags;
491 ip_freef(pData, head, fp);
492 goto done;
493 }
494
495 /*
496 * Concatenate fragments.
497 */
498 m = q;
499 nq = q->m_nextpkt;
500 q->m_nextpkt = NULL;
501 for (q = nq; q != NULL; q = nq)
502 {
503 nq = q->m_nextpkt;
504 q->m_nextpkt = NULL;
505 m_cat(pData, m, q);
506
507 m->m_len += hlen;
508 m->m_data -= hlen;
509 ip = mtod(m, struct ip *); /*update ip pointer */
510 hlen = ip->ip_hl << 2;
511 m->m_len -= hlen;
512 m->m_data += hlen;
513 }
514 m->m_len += hlen;
515 m->m_data -= hlen;
516
517 /*
518 * Create header for new ip packet by modifying header of first
519 * packet; dequeue and discard fragment reassembly header.
520 * Make header visible.
521 */
522
523 ip->ip_len = next;
524 ip->ip_src = fp->ipq_src;
525 ip->ip_dst = fp->ipq_dst;
526 TAILQ_REMOVE(head, fp, ipq_list);
527 nipq--;
528 RTMemFree(fp);
529
530 Assert((ip->ip_len == next));
531 /* some debugging cruft by sklower, below, will go away soon */
532#if 0
533 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */
534 m_fixhdr(m);
535#endif
536 ipstat.ips_reassembled++;
537 return (m);
538
539dropfrag:
540 ipstat.ips_fragdropped++;
541 if (fp != NULL)
542 fp->ipq_nfrags--;
543 m_freem(pData, m);
544
545done:
546 return NULL;
547
548#undef GETIP
549}
550
551void
552ip_freef(PNATState pData, struct ipqhead *fhp, struct ipq_t *fp)
553{
554 struct mbuf *q;
555
556 while (fp->ipq_frags)
557 {
558 q = fp->ipq_frags;
559 fp->ipq_frags = q->m_nextpkt;
560 m_freem(pData, q);
561 }
562 TAILQ_REMOVE(fhp, fp, ipq_list);
563 RTMemFree(fp);
564 nipq--;
565}
566
567/*
568 * IP timer processing;
569 * if a timer expires on a reassembly
570 * queue, discard it.
571 */
572void
573ip_slowtimo(PNATState pData)
574{
575 register struct ipq_t *fp;
576
577 /* XXX: the fragment expiration is the same but requier
578 * additional loop see (see ip_input.c in FreeBSD tree)
579 */
580 int i;
581 DEBUG_CALL("ip_slowtimo");
582 for (i = 0; i < IPREASS_NHASH; i++)
583 {
584 for(fp = TAILQ_FIRST(&ipq[i]); fp;)
585 {
586 struct ipq_t *fpp;
587
588 fpp = fp;
589 fp = TAILQ_NEXT(fp, ipq_list);
590 if(--fpp->ipq_ttl == 0)
591 {
592 ipstat.ips_fragtimeout += fpp->ipq_nfrags;
593 ip_freef(pData, &ipq[i], fpp);
594 }
595 }
596 }
597 /*
598 * If we are over the maximum number of fragments
599 * (due to the limit being lowered), drain off
600 * enough to get down to the new limit.
601 */
602 if (maxnipq >= 0 && nipq > maxnipq)
603 {
604 for (i = 0; i < IPREASS_NHASH; i++)
605 {
606 while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i]))
607 {
608 ipstat.ips_fragdropped += TAILQ_FIRST(&ipq[i])->ipq_nfrags;
609 ip_freef(pData, &ipq[i], TAILQ_FIRST(&ipq[i]));
610 }
611 }
612 }
613}
614
615
616/*
617 * Strip out IP options, at higher
618 * level protocol in the kernel.
619 * Second argument is buffer to which options
620 * will be moved, and return value is their length.
621 * (XXX) should be deleted; last arg currently ignored.
622 */
623void
624ip_stripoptions(struct mbuf *m, struct mbuf *mopt)
625{
626 register int i;
627 struct ip *ip = mtod(m, struct ip *);
628 register caddr_t opts;
629 int olen;
630
631 olen = (ip->ip_hl<<2) - sizeof(struct ip);
632 opts = (caddr_t)(ip + 1);
633 i = m->m_len - (sizeof(struct ip) + olen);
634 memcpy(opts, opts + olen, (unsigned)i);
635 m->m_len -= olen;
636
637 ip->ip_hl = sizeof(struct ip) >> 2;
638}
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