slirp: Drop statistic code
[qemu/aliguori-queue.git] / slirp / ip_input.c
blob0356eb587bb2f77c18f36d1d166e3ca422012e22
1 /*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
30 * ip_input.c,v 1.11 1994/11/16 10:17:08 jkh Exp
34 * Changes and additions relating to SLiRP are
35 * Copyright (c) 1995 Danny Gasparovski.
37 * Please read the file COPYRIGHT for the
38 * terms and conditions of the copyright.
41 #include <slirp.h>
42 #include <osdep.h>
43 #include "ip_icmp.h"
45 struct ipq ipq;
47 static struct ip *ip_reass(register struct ip *ip,
48 register struct ipq *fp);
49 static void ip_freef(struct ipq *fp);
50 static void ip_enq(register struct ipasfrag *p,
51 register struct ipasfrag *prev);
52 static void ip_deq(register struct ipasfrag *p);
55 * IP initialization: fill in IP protocol switch table.
56 * All protocols not implemented in kernel go to raw IP protocol handler.
58 void
59 ip_init(void)
61 ipq.ip_link.next = ipq.ip_link.prev = &ipq.ip_link;
62 ip_id = tt.tv_sec & 0xffff;
63 udp_init();
64 tcp_init();
68 * Ip input routine. Checksum and byte swap header. If fragmented
69 * try to reassemble. Process options. Pass to next level.
71 void
72 ip_input(struct mbuf *m)
74 register struct ip *ip;
75 int hlen;
77 DEBUG_CALL("ip_input");
78 DEBUG_ARG("m = %lx", (long)m);
79 DEBUG_ARG("m_len = %d", m->m_len);
81 if (m->m_len < sizeof (struct ip)) {
82 return;
85 ip = mtod(m, struct ip *);
87 if (ip->ip_v != IPVERSION) {
88 goto bad;
91 hlen = ip->ip_hl << 2;
92 if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
93 goto bad; /* or packet too short */
96 /* keep ip header intact for ICMP reply
97 * ip->ip_sum = cksum(m, hlen);
98 * if (ip->ip_sum) {
100 if(cksum(m,hlen)) {
101 goto bad;
105 * Convert fields to host representation.
107 NTOHS(ip->ip_len);
108 if (ip->ip_len < hlen) {
109 goto bad;
111 NTOHS(ip->ip_id);
112 NTOHS(ip->ip_off);
115 * Check that the amount of data in the buffers
116 * is as at least much as the IP header would have us expect.
117 * Trim mbufs if longer than we expect.
118 * Drop packet if shorter than we expect.
120 if (m->m_len < ip->ip_len) {
121 goto bad;
124 if (slirp_restrict) {
125 if ((ip->ip_dst.s_addr & vnetwork_mask.s_addr) ==
126 vnetwork_addr.s_addr) {
127 if (ip->ip_dst.s_addr == 0xffffffff && ip->ip_p != IPPROTO_UDP)
128 goto bad;
129 } else {
130 struct ex_list *ex_ptr;
132 if ((ip->ip_dst.s_addr & ~vnetwork_mask.s_addr) ==
133 ~vnetwork_mask.s_addr)
134 goto bad;
136 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
137 if (ex_ptr->ex_addr.s_addr == ip->ip_dst.s_addr)
138 break;
140 if (!ex_ptr)
141 goto bad;
145 /* Should drop packet if mbuf too long? hmmm... */
146 if (m->m_len > ip->ip_len)
147 m_adj(m, ip->ip_len - m->m_len);
149 /* check ip_ttl for a correct ICMP reply */
150 if(ip->ip_ttl==0 || ip->ip_ttl==1) {
151 icmp_error(m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");
152 goto bad;
156 * If offset or IP_MF are set, must reassemble.
157 * Otherwise, nothing need be done.
158 * (We could look in the reassembly queue to see
159 * if the packet was previously fragmented,
160 * but it's not worth the time; just let them time out.)
162 * XXX This should fail, don't fragment yet
164 if (ip->ip_off &~ IP_DF) {
165 register struct ipq *fp;
166 struct qlink *l;
168 * Look for queue of fragments
169 * of this datagram.
171 for (l = ipq.ip_link.next; l != &ipq.ip_link; l = l->next) {
172 fp = container_of(l, struct ipq, ip_link);
173 if (ip->ip_id == fp->ipq_id &&
174 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
175 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
176 ip->ip_p == fp->ipq_p)
177 goto found;
179 fp = NULL;
180 found:
183 * Adjust ip_len to not reflect header,
184 * set ip_mff if more fragments are expected,
185 * convert offset of this to bytes.
187 ip->ip_len -= hlen;
188 if (ip->ip_off & IP_MF)
189 ip->ip_tos |= 1;
190 else
191 ip->ip_tos &= ~1;
193 ip->ip_off <<= 3;
196 * If datagram marked as having more fragments
197 * or if this is not the first fragment,
198 * attempt reassembly; if it succeeds, proceed.
200 if (ip->ip_tos & 1 || ip->ip_off) {
201 ip = ip_reass(ip, fp);
202 if (ip == NULL)
203 return;
204 m = dtom(ip);
205 } else
206 if (fp)
207 ip_freef(fp);
209 } else
210 ip->ip_len -= hlen;
213 * Switch out to protocol's input routine.
215 switch (ip->ip_p) {
216 case IPPROTO_TCP:
217 tcp_input(m, hlen, (struct socket *)NULL);
218 break;
219 case IPPROTO_UDP:
220 udp_input(m, hlen);
221 break;
222 case IPPROTO_ICMP:
223 icmp_input(m, hlen);
224 break;
225 default:
226 m_free(m);
228 return;
229 bad:
230 m_freem(m);
231 return;
234 #define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
235 #define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
237 * Take incoming datagram fragment and try to
238 * reassemble it into whole datagram. If a chain for
239 * reassembly of this datagram already exists, then it
240 * is given as fp; otherwise have to make a chain.
242 static struct ip *
243 ip_reass(register struct ip *ip, register struct ipq *fp)
245 register struct mbuf *m = dtom(ip);
246 register struct ipasfrag *q;
247 int hlen = ip->ip_hl << 2;
248 int i, next;
250 DEBUG_CALL("ip_reass");
251 DEBUG_ARG("ip = %lx", (long)ip);
252 DEBUG_ARG("fp = %lx", (long)fp);
253 DEBUG_ARG("m = %lx", (long)m);
256 * Presence of header sizes in mbufs
257 * would confuse code below.
258 * Fragment m_data is concatenated.
260 m->m_data += hlen;
261 m->m_len -= hlen;
264 * If first fragment to arrive, create a reassembly queue.
266 if (fp == NULL) {
267 struct mbuf *t;
268 if ((t = m_get()) == NULL) goto dropfrag;
269 fp = mtod(t, struct ipq *);
270 insque(&fp->ip_link, &ipq.ip_link);
271 fp->ipq_ttl = IPFRAGTTL;
272 fp->ipq_p = ip->ip_p;
273 fp->ipq_id = ip->ip_id;
274 fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
275 fp->ipq_src = ip->ip_src;
276 fp->ipq_dst = ip->ip_dst;
277 q = (struct ipasfrag *)fp;
278 goto insert;
282 * Find a segment which begins after this one does.
284 for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
285 q = q->ipf_next)
286 if (q->ipf_off > ip->ip_off)
287 break;
290 * If there is a preceding segment, it may provide some of
291 * our data already. If so, drop the data from the incoming
292 * segment. If it provides all of our data, drop us.
294 if (q->ipf_prev != &fp->frag_link) {
295 struct ipasfrag *pq = q->ipf_prev;
296 i = pq->ipf_off + pq->ipf_len - ip->ip_off;
297 if (i > 0) {
298 if (i >= ip->ip_len)
299 goto dropfrag;
300 m_adj(dtom(ip), i);
301 ip->ip_off += i;
302 ip->ip_len -= i;
307 * While we overlap succeeding segments trim them or,
308 * if they are completely covered, dequeue them.
310 while (q != (struct ipasfrag*)&fp->frag_link &&
311 ip->ip_off + ip->ip_len > q->ipf_off) {
312 i = (ip->ip_off + ip->ip_len) - q->ipf_off;
313 if (i < q->ipf_len) {
314 q->ipf_len -= i;
315 q->ipf_off += i;
316 m_adj(dtom(q), i);
317 break;
319 q = q->ipf_next;
320 m_freem(dtom(q->ipf_prev));
321 ip_deq(q->ipf_prev);
324 insert:
326 * Stick new segment in its place;
327 * check for complete reassembly.
329 ip_enq(iptofrag(ip), q->ipf_prev);
330 next = 0;
331 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link;
332 q = q->ipf_next) {
333 if (q->ipf_off != next)
334 return NULL;
335 next += q->ipf_len;
337 if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
338 return NULL;
341 * Reassembly is complete; concatenate fragments.
343 q = fp->frag_link.next;
344 m = dtom(q);
346 q = (struct ipasfrag *) q->ipf_next;
347 while (q != (struct ipasfrag*)&fp->frag_link) {
348 struct mbuf *t = dtom(q);
349 q = (struct ipasfrag *) q->ipf_next;
350 m_cat(m, t);
354 * Create header for new ip packet by
355 * modifying header of first packet;
356 * dequeue and discard fragment reassembly header.
357 * Make header visible.
359 q = fp->frag_link.next;
362 * If the fragments concatenated to an mbuf that's
363 * bigger than the total size of the fragment, then and
364 * m_ext buffer was alloced. But fp->ipq_next points to
365 * the old buffer (in the mbuf), so we must point ip
366 * into the new buffer.
368 if (m->m_flags & M_EXT) {
369 int delta = (char *)q - m->m_dat;
370 q = (struct ipasfrag *)(m->m_ext + delta);
373 ip = fragtoip(q);
374 ip->ip_len = next;
375 ip->ip_tos &= ~1;
376 ip->ip_src = fp->ipq_src;
377 ip->ip_dst = fp->ipq_dst;
378 remque(&fp->ip_link);
379 (void) m_free(dtom(fp));
380 m->m_len += (ip->ip_hl << 2);
381 m->m_data -= (ip->ip_hl << 2);
383 return ip;
385 dropfrag:
386 m_freem(m);
387 return NULL;
391 * Free a fragment reassembly header and all
392 * associated datagrams.
394 static void
395 ip_freef(struct ipq *fp)
397 register struct ipasfrag *q, *p;
399 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) {
400 p = q->ipf_next;
401 ip_deq(q);
402 m_freem(dtom(q));
404 remque(&fp->ip_link);
405 (void) m_free(dtom(fp));
409 * Put an ip fragment on a reassembly chain.
410 * Like insque, but pointers in middle of structure.
412 static void
413 ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
415 DEBUG_CALL("ip_enq");
416 DEBUG_ARG("prev = %lx", (long)prev);
417 p->ipf_prev = prev;
418 p->ipf_next = prev->ipf_next;
419 ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
420 prev->ipf_next = p;
424 * To ip_enq as remque is to insque.
426 static void
427 ip_deq(register struct ipasfrag *p)
429 ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
430 ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
434 * IP timer processing;
435 * if a timer expires on a reassembly
436 * queue, discard it.
438 void
439 ip_slowtimo(void)
441 struct qlink *l;
443 DEBUG_CALL("ip_slowtimo");
445 l = ipq.ip_link.next;
447 if (l == NULL)
448 return;
450 while (l != &ipq.ip_link) {
451 struct ipq *fp = container_of(l, struct ipq, ip_link);
452 l = l->next;
453 if (--fp->ipq_ttl == 0) {
454 ip_freef(fp);
460 * Do option processing on a datagram,
461 * possibly discarding it if bad options are encountered,
462 * or forwarding it if source-routed.
463 * Returns 1 if packet has been forwarded/freed,
464 * 0 if the packet should be processed further.
467 #ifdef notdef
470 ip_dooptions(m)
471 struct mbuf *m;
473 register struct ip *ip = mtod(m, struct ip *);
474 register u_char *cp;
475 register struct ip_timestamp *ipt;
476 register struct in_ifaddr *ia;
477 int opt, optlen, cnt, off, code, type, forward = 0;
478 struct in_addr *sin, dst;
479 typedef u_int32_t n_time;
480 n_time ntime;
482 dst = ip->ip_dst;
483 cp = (u_char *)(ip + 1);
484 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
485 for (; cnt > 0; cnt -= optlen, cp += optlen) {
486 opt = cp[IPOPT_OPTVAL];
487 if (opt == IPOPT_EOL)
488 break;
489 if (opt == IPOPT_NOP)
490 optlen = 1;
491 else {
492 optlen = cp[IPOPT_OLEN];
493 if (optlen <= 0 || optlen > cnt) {
494 code = &cp[IPOPT_OLEN] - (u_char *)ip;
495 goto bad;
498 switch (opt) {
500 default:
501 break;
504 * Source routing with record.
505 * Find interface with current destination address.
506 * If none on this machine then drop if strictly routed,
507 * or do nothing if loosely routed.
508 * Record interface address and bring up next address
509 * component. If strictly routed make sure next
510 * address is on directly accessible net.
512 case IPOPT_LSRR:
513 case IPOPT_SSRR:
514 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
515 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
516 goto bad;
518 ipaddr.sin_addr = ip->ip_dst;
519 ia = (struct in_ifaddr *)
520 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
521 if (ia == 0) {
522 if (opt == IPOPT_SSRR) {
523 type = ICMP_UNREACH;
524 code = ICMP_UNREACH_SRCFAIL;
525 goto bad;
528 * Loose routing, and not at next destination
529 * yet; nothing to do except forward.
531 break;
533 off--; / * 0 origin * /
534 if (off > optlen - sizeof(struct in_addr)) {
536 * End of source route. Should be for us.
538 save_rte(cp, ip->ip_src);
539 break;
542 * locate outgoing interface
544 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
545 sizeof(ipaddr.sin_addr));
546 if (opt == IPOPT_SSRR) {
547 #define INA struct in_ifaddr *
548 #define SA struct sockaddr *
549 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
550 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
551 } else
552 ia = ip_rtaddr(ipaddr.sin_addr);
553 if (ia == 0) {
554 type = ICMP_UNREACH;
555 code = ICMP_UNREACH_SRCFAIL;
556 goto bad;
558 ip->ip_dst = ipaddr.sin_addr;
559 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
560 (caddr_t)(cp + off), sizeof(struct in_addr));
561 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
563 * Let ip_intr's mcast routing check handle mcast pkts
565 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
566 break;
568 case IPOPT_RR:
569 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
570 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
571 goto bad;
574 * If no space remains, ignore.
576 off--; * 0 origin *
577 if (off > optlen - sizeof(struct in_addr))
578 break;
579 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
580 sizeof(ipaddr.sin_addr));
582 * locate outgoing interface; if we're the destination,
583 * use the incoming interface (should be same).
585 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
586 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
587 type = ICMP_UNREACH;
588 code = ICMP_UNREACH_HOST;
589 goto bad;
591 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
592 (caddr_t)(cp + off), sizeof(struct in_addr));
593 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
594 break;
596 case IPOPT_TS:
597 code = cp - (u_char *)ip;
598 ipt = (struct ip_timestamp *)cp;
599 if (ipt->ipt_len < 5)
600 goto bad;
601 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
602 if (++ipt->ipt_oflw == 0)
603 goto bad;
604 break;
606 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
607 switch (ipt->ipt_flg) {
609 case IPOPT_TS_TSONLY:
610 break;
612 case IPOPT_TS_TSANDADDR:
613 if (ipt->ipt_ptr + sizeof(n_time) +
614 sizeof(struct in_addr) > ipt->ipt_len)
615 goto bad;
616 ipaddr.sin_addr = dst;
617 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
618 m->m_pkthdr.rcvif);
619 if (ia == 0)
620 continue;
621 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
622 (caddr_t)sin, sizeof(struct in_addr));
623 ipt->ipt_ptr += sizeof(struct in_addr);
624 break;
626 case IPOPT_TS_PRESPEC:
627 if (ipt->ipt_ptr + sizeof(n_time) +
628 sizeof(struct in_addr) > ipt->ipt_len)
629 goto bad;
630 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
631 sizeof(struct in_addr));
632 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
633 continue;
634 ipt->ipt_ptr += sizeof(struct in_addr);
635 break;
637 default:
638 goto bad;
640 ntime = iptime();
641 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
642 sizeof(n_time));
643 ipt->ipt_ptr += sizeof(n_time);
646 if (forward) {
647 ip_forward(m, 1);
648 return (1);
652 return (0);
653 bad:
654 icmp_error(m, type, code, 0, 0);
656 return (1);
659 #endif /* notdef */
662 * Strip out IP options, at higher
663 * level protocol in the kernel.
664 * Second argument is buffer to which options
665 * will be moved, and return value is their length.
666 * (XXX) should be deleted; last arg currently ignored.
668 void
669 ip_stripoptions(register struct mbuf *m, struct mbuf *mopt)
671 register int i;
672 struct ip *ip = mtod(m, struct ip *);
673 register caddr_t opts;
674 int olen;
676 olen = (ip->ip_hl<<2) - sizeof (struct ip);
677 opts = (caddr_t)(ip + 1);
678 i = m->m_len - (sizeof (struct ip) + olen);
679 memcpy(opts, opts + olen, (unsigned)i);
680 m->m_len -= olen;
682 ip->ip_hl = sizeof(struct ip) >> 2;