VirtualBox

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

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

NAT: comment updated.

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