VirtualBox

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

Last change on this file since 22881 was 22843, checked in by vboxsync, 15 years ago

NAT: using host resolver instead of dnsproxy.

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