VirtualBox

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

Last change on this file since 53360 was 52755, checked in by vboxsync, 10 years ago

NAT: G/c socket:so_la - per-socket libalias instance used for
port-forwarding - and concomitant code. It is never actually used as
PACKET_TAG_ALIAS that is supposed to select it is never set. Its
LibAliasRedirectPort() arguments were wrong too.

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