VirtualBox

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

Last change on this file since 41783 was 41783, checked in by vboxsync, 12 years ago

Doxygen, comment typos.

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