VirtualBox

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

Last change on this file since 16069 was 15845, checked in by vboxsync, 16 years ago

warnings and minor fixes to coding style

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