VirtualBox

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

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

NAT: 'icmp_error` frees source mbuf.

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