VirtualBox

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

Last change on this file since 39101 was 39101, checked in by vboxsync, 13 years ago

NAT: warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 KB
Line 
1/* $Id: ip_input.c 39101 2011-10-25 02:44:01Z 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
91 struct m_tag *t;
92 if ((t = m_tag_find(m, PACKET_TAG_ALIAS, NULL)) != 0)
93 return (struct libalias *)&t[1];
94
95 return la;
96}
97
98/*
99 * Ip input routine. Checksum and byte swap header. If fragmented
100 * try to reassemble. Process options. Pass to next level.
101 */
102void
103ip_input(PNATState pData, struct mbuf *m)
104{
105 register struct ip *ip;
106 int hlen = 0;
107 int mlen = 0;
108
109 STAM_PROFILE_START(&pData->StatIP_input, a);
110
111 LogFlowFunc(("ENTER: m = %lx\n", (long)m));
112 ip = mtod(m, struct ip *);
113 Log2(("ip_dst=%RTnaipv4(len:%d) m_len = %d\n", ip->ip_dst, RT_N2H_U16(ip->ip_len), m->m_len));
114
115 ipstat.ips_total++;
116 {
117 int rc;
118 STAM_PROFILE_START(&pData->StatALIAS_input, b);
119 rc = LibAliasIn(select_alias(pData, m), mtod(m, char *), m_length(m, NULL));
120 STAM_PROFILE_STOP(&pData->StatALIAS_input, b);
121 Log2(("NAT: LibAlias return %d\n", rc));
122 if (m->m_len != RT_N2H_U16(ip->ip_len))
123 m->m_len = RT_N2H_U16(ip->ip_len);
124 }
125
126 mlen = m->m_len;
127
128 if (mlen < sizeof(struct ip))
129 {
130 ipstat.ips_toosmall++;
131 goto bad_free_m;
132 }
133
134 ip = mtod(m, struct ip *);
135 if (ip->ip_v != IPVERSION)
136 {
137 ipstat.ips_badvers++;
138 goto bad_free_m;
139 }
140
141 hlen = ip->ip_hl << 2;
142 if ( hlen < sizeof(struct ip)
143 || hlen > m->m_len)
144 {
145 /* min header length */
146 ipstat.ips_badhlen++; /* or packet too short */
147 goto bad_free_m;
148 }
149
150 /* keep ip header intact for ICMP reply
151 * ip->ip_sum = cksum(m, hlen);
152 * if (ip->ip_sum) {
153 */
154 if (cksum(m, hlen))
155 {
156 ipstat.ips_badsum++;
157 goto bad_free_m;
158 }
159
160 /*
161 * Convert fields to host representation.
162 */
163 NTOHS(ip->ip_len);
164 if (ip->ip_len < hlen)
165 {
166 ipstat.ips_badlen++;
167 goto bad_free_m;
168 }
169
170 NTOHS(ip->ip_id);
171 NTOHS(ip->ip_off);
172
173 /*
174 * Check that the amount of data in the buffers
175 * is as at least much as the IP header would have us expect.
176 * Trim mbufs if longer than we expect.
177 * Drop packet if shorter than we expect.
178 */
179 if (mlen < ip->ip_len)
180 {
181 ipstat.ips_tooshort++;
182 goto bad_free_m;
183 }
184
185 /* Should drop packet if mbuf too long? hmmm... */
186 if (mlen > ip->ip_len)
187 m_adj(m, ip->ip_len - m->m_len);
188
189 /* check ip_ttl for a correct ICMP reply */
190 if (ip->ip_ttl==0 || ip->ip_ttl == 1)
191 {
192 icmp_error(pData, m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, "ttl");
193 goto no_free_m;
194 }
195
196 ip->ip_ttl--;
197 /*
198 * If offset or IP_MF are set, must reassemble.
199 * Otherwise, nothing need be done.
200 * (We could look in the reassembly queue to see
201 * if the packet was previously fragmented,
202 * but it's not worth the time; just let them time out.)
203 *
204 */
205 if (ip->ip_off & (IP_MF | IP_OFFMASK))
206 {
207 m = ip_reass(pData, m);
208 if (m == NULL)
209 goto no_free_m;
210 ip = mtod(m, struct ip *);
211 hlen = ip->ip_hl << 2;
212 }
213 else
214 ip->ip_len -= hlen;
215
216 /*
217 * Switch out to protocol's input routine.
218 */
219 ipstat.ips_delivered++;
220 switch (ip->ip_p)
221 {
222 case IPPROTO_TCP:
223 tcp_input(pData, m, hlen, (struct socket *)NULL);
224 break;
225 case IPPROTO_UDP:
226 udp_input(pData, m, hlen);
227 break;
228 case IPPROTO_ICMP:
229 icmp_input(pData, m, hlen);
230 break;
231 default:
232 ipstat.ips_noproto++;
233 m_freem(pData, m);
234 }
235 goto no_free_m;
236
237bad_free_m:
238 Log2(("NAT: IP datagram to %RTnaipv4 with size(%d) claimed as bad\n",
239 ip->ip_dst, ip->ip_len));
240 m_freem(pData, m);
241no_free_m:
242 STAM_PROFILE_STOP(&pData->StatIP_input, a);
243 LogFlowFuncLeave();
244 return;
245}
246
247struct mbuf *
248ip_reass(PNATState pData, struct mbuf* m)
249{
250 struct ip *ip;
251 struct mbuf *p, *q, *nq;
252 struct ipq_t *fp = NULL;
253 struct ipqhead *head;
254 int i, hlen, next;
255 u_short hash;
256
257 /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
258 LogFlowFunc(("ENTER: m:%p\n", m));
259 if ( maxnipq == 0
260 || maxfragsperpacket == 0)
261 {
262 ipstat.ips_fragments++;
263 ipstat.ips_fragdropped++;
264 m_freem(pData, m);
265 LogFlowFunc(("LEAVE: NULL\n"));
266 return (NULL);
267 }
268
269 ip = mtod(m, struct ip *);
270 hlen = ip->ip_hl << 2;
271
272 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
273 head = &ipq[hash];
274
275 /*
276 * Look for queue of fragments
277 * of this datagram.
278 */
279 TAILQ_FOREACH(fp, head, ipq_list)
280 if (ip->ip_id == fp->ipq_id &&
281 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
282 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
283 ip->ip_p == fp->ipq_p)
284 goto found;
285
286 fp = NULL;
287
288 /*
289 * Attempt to trim the number of allocated fragment queues if it
290 * exceeds the administrative limit.
291 */
292 if ((nipq > maxnipq) && (maxnipq > 0))
293 {
294 /*
295 * drop something from the tail of the current queue
296 * before proceeding further
297 */
298 struct ipq_t *pHead = TAILQ_LAST(head, ipqhead);
299 if (pHead == NULL)
300 {
301 /* gak */
302 for (i = 0; i < IPREASS_NHASH; i++)
303 {
304 struct ipq_t *pTail = TAILQ_LAST(&ipq[i], ipqhead);
305 if (pTail)
306 {
307 ipstat.ips_fragtimeout += pTail->ipq_nfrags;
308 ip_freef(pData, &ipq[i], pTail);
309 break;
310 }
311 }
312 }
313 else
314 {
315 ipstat.ips_fragtimeout += pHead->ipq_nfrags;
316 ip_freef(pData, head, pHead);
317 }
318 }
319
320found:
321 /*
322 * Adjust ip_len to not reflect header,
323 * convert offset of this to bytes.
324 */
325 ip->ip_len -= hlen;
326 if (ip->ip_off & IP_MF)
327 {
328 /*
329 * Make sure that fragments have a data length
330 * that's a non-zero multiple of 8 bytes.
331 */
332 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0)
333 {
334 ipstat.ips_toosmall++; /* XXX */
335 goto dropfrag;
336 }
337 m->m_flags |= M_FRAG;
338 }
339 else
340 m->m_flags &= ~M_FRAG;
341 ip->ip_off <<= 3;
342
343
344 /*
345 * Attempt reassembly; if it succeeds, proceed.
346 * ip_reass() will return a different mbuf.
347 */
348 ipstat.ips_fragments++;
349
350 /* Previous ip_reass() started here. */
351 /*
352 * Presence of header sizes in mbufs
353 * would confuse code below.
354 */
355 m->m_data += hlen;
356 m->m_len -= hlen;
357
358 /*
359 * If first fragment to arrive, create a reassembly queue.
360 */
361 if (fp == NULL)
362 {
363 fp = RTMemAlloc(sizeof(struct ipq_t));
364 if (fp == NULL)
365 goto dropfrag;
366 TAILQ_INSERT_HEAD(head, fp, ipq_list);
367 nipq++;
368 fp->ipq_nfrags = 1;
369 fp->ipq_ttl = IPFRAGTTL;
370 fp->ipq_p = ip->ip_p;
371 fp->ipq_id = ip->ip_id;
372 fp->ipq_src = ip->ip_src;
373 fp->ipq_dst = ip->ip_dst;
374 fp->ipq_frags = m;
375 m->m_nextpkt = NULL;
376 goto done;
377 }
378 else
379 {
380 fp->ipq_nfrags++;
381 }
382
383#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
384
385 /*
386 * Find a segment which begins after this one does.
387 */
388 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
389 if (GETIP(q)->ip_off > ip->ip_off)
390 break;
391
392 /*
393 * If there is a preceding segment, it may provide some of
394 * our data already. If so, drop the data from the incoming
395 * segment. If it provides all of our data, drop us, otherwise
396 * stick new segment in the proper place.
397 *
398 * If some of the data is dropped from the the preceding
399 * segment, then it's checksum is invalidated.
400 */
401 if (p)
402 {
403 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
404 if (i > 0)
405 {
406 if (i >= ip->ip_len)
407 goto dropfrag;
408 m_adj(m, i);
409 ip->ip_off += i;
410 ip->ip_len -= i;
411 }
412 m->m_nextpkt = p->m_nextpkt;
413 p->m_nextpkt = m;
414 }
415 else
416 {
417 m->m_nextpkt = fp->ipq_frags;
418 fp->ipq_frags = m;
419 }
420
421 /*
422 * While we overlap succeeding segments trim them or,
423 * if they are completely covered, dequeue them.
424 */
425 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
426 q = nq)
427 {
428 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
429 if (i < GETIP(q)->ip_len)
430 {
431 GETIP(q)->ip_len -= i;
432 GETIP(q)->ip_off += i;
433 m_adj(q, i);
434 break;
435 }
436 nq = q->m_nextpkt;
437 m->m_nextpkt = nq;
438 ipstat.ips_fragdropped++;
439 fp->ipq_nfrags--;
440 m_freem(pData, q);
441 }
442
443 /*
444 * Check for complete reassembly and perform frag per packet
445 * limiting.
446 *
447 * Frag limiting is performed here so that the nth frag has
448 * a chance to complete the packet before we drop the packet.
449 * As a result, n+1 frags are actually allowed per packet, but
450 * only n will ever be stored. (n = maxfragsperpacket.)
451 *
452 */
453 next = 0;
454 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
455 {
456 if (GETIP(q)->ip_off != next)
457 {
458 if (fp->ipq_nfrags > maxfragsperpacket)
459 {
460 ipstat.ips_fragdropped += fp->ipq_nfrags;
461 ip_freef(pData, head, fp);
462 }
463 goto done;
464 }
465 next += GETIP(q)->ip_len;
466 }
467 /* Make sure the last packet didn't have the IP_MF flag */
468 if (p->m_flags & M_FRAG)
469 {
470 if (fp->ipq_nfrags > maxfragsperpacket)
471 {
472 ipstat.ips_fragdropped += fp->ipq_nfrags;
473 ip_freef(pData, head, fp);
474 }
475 goto done;
476 }
477
478 /*
479 * Reassembly is complete. Make sure the packet is a sane size.
480 */
481 q = fp->ipq_frags;
482 ip = GETIP(q);
483 hlen = ip->ip_hl << 2;
484 if (next + hlen > IP_MAXPACKET)
485 {
486 ipstat.ips_fragdropped += fp->ipq_nfrags;
487 ip_freef(pData, head, fp);
488 goto done;
489 }
490
491 /*
492 * Concatenate fragments.
493 */
494 m = q;
495 nq = q->m_nextpkt;
496 q->m_nextpkt = NULL;
497 for (q = nq; q != NULL; q = nq)
498 {
499 nq = q->m_nextpkt;
500 q->m_nextpkt = NULL;
501 m_cat(pData, m, q);
502
503 m->m_len += hlen;
504 m->m_data -= hlen;
505 ip = mtod(m, struct ip *); /*update ip pointer */
506 hlen = ip->ip_hl << 2;
507 m->m_len -= hlen;
508 m->m_data += hlen;
509 }
510 m->m_len += hlen;
511 m->m_data -= hlen;
512
513 /*
514 * Create header for new ip packet by modifying header of first
515 * packet; dequeue and discard fragment reassembly header.
516 * Make header visible.
517 */
518
519 ip->ip_len = next;
520 ip->ip_src = fp->ipq_src;
521 ip->ip_dst = fp->ipq_dst;
522 TAILQ_REMOVE(head, fp, ipq_list);
523 nipq--;
524 RTMemFree(fp);
525
526 Assert((ip->ip_len == next));
527 /* some debugging cruft by sklower, below, will go away soon */
528#if 0
529 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */
530 m_fixhdr(m);
531#endif
532 ipstat.ips_reassembled++;
533 LogFlowFunc(("LEAVE: %p\n", m));
534 return (m);
535
536dropfrag:
537 ipstat.ips_fragdropped++;
538 if (fp != NULL)
539 fp->ipq_nfrags--;
540 m_freem(pData, m);
541
542done:
543 LogFlowFunc(("LEAVE: NULL\n"));
544 return NULL;
545
546#undef GETIP
547}
548
549void
550ip_freef(PNATState pData, struct ipqhead *fhp, struct ipq_t *fp)
551{
552 struct mbuf *q;
553
554 while (fp->ipq_frags)
555 {
556 q = fp->ipq_frags;
557 fp->ipq_frags = q->m_nextpkt;
558 m_freem(pData, q);
559 }
560 TAILQ_REMOVE(fhp, fp, ipq_list);
561 RTMemFree(fp);
562 nipq--;
563}
564
565/*
566 * IP timer processing;
567 * if a timer expires on a reassembly
568 * queue, discard it.
569 */
570void
571ip_slowtimo(PNATState pData)
572{
573 register struct ipq_t *fp;
574
575 /* XXX: the fragment expiration is the same but requier
576 * additional loop see (see ip_input.c in FreeBSD tree)
577 */
578 int i;
579 LogFlow(("ip_slowtimo:\n"));
580 for (i = 0; i < IPREASS_NHASH; i++)
581 {
582 for(fp = TAILQ_FIRST(&ipq[i]); fp;)
583 {
584 struct ipq_t *fpp;
585
586 fpp = fp;
587 fp = TAILQ_NEXT(fp, ipq_list);
588 if(--fpp->ipq_ttl == 0)
589 {
590 ipstat.ips_fragtimeout += fpp->ipq_nfrags;
591 ip_freef(pData, &ipq[i], fpp);
592 }
593 }
594 }
595 /*
596 * If we are over the maximum number of fragments
597 * (due to the limit being lowered), drain off
598 * enough to get down to the new limit.
599 */
600 if (maxnipq >= 0 && nipq > maxnipq)
601 {
602 for (i = 0; i < IPREASS_NHASH; i++)
603 {
604 while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i]))
605 {
606 ipstat.ips_fragdropped += TAILQ_FIRST(&ipq[i])->ipq_nfrags;
607 ip_freef(pData, &ipq[i], TAILQ_FIRST(&ipq[i]));
608 }
609 }
610 }
611}
612
613
614/*
615 * Strip out IP options, at higher
616 * level protocol in the kernel.
617 * Second argument is buffer to which options
618 * will be moved, and return value is their length.
619 * (XXX) should be deleted; last arg currently ignored.
620 */
621void
622ip_stripoptions(struct mbuf *m, struct mbuf *mopt)
623{
624 register int i;
625 struct ip *ip = mtod(m, struct ip *);
626 register caddr_t opts;
627 int olen;
628 NOREF(mopt); /* @todo: do we really will need this options buffer? */
629
630 olen = (ip->ip_hl<<2) - sizeof(struct ip);
631 opts = (caddr_t)(ip + 1);
632 i = m->m_len - (sizeof(struct ip) + olen);
633 memcpy(opts, opts + olen, (unsigned)i);
634 m->m_len -= olen;
635
636 ip->ip_hl = sizeof(struct ip) >> 2;
637}
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