Fix qemu_send_packet
[qemu-kvm/fedora.git] / slirp / ip_input.c
blob116ee4565f389947238344d7c74628d88216594d
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. 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.
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.
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
38 * Changes and additions relating to SLiRP are
39 * Copyright (c) 1995 Danny Gasparovski.
41 * Please read the file COPYRIGHT for the
42 * terms and conditions of the copyright.
45 #include <slirp.h>
46 #include <osdep.h>
47 #include "ip_icmp.h"
49 #ifdef LOG_ENABLED
50 struct ipstat ipstat;
51 #endif
53 struct ipq ipq;
55 static struct ip *ip_reass(register struct ip *ip,
56 register struct ipq *fp);
57 static void ip_freef(struct ipq *fp);
58 static void ip_enq(register struct ipasfrag *p,
59 register struct ipasfrag *prev);
60 static void ip_deq(register struct ipasfrag *p);
63 * IP initialization: fill in IP protocol switch table.
64 * All protocols not implemented in kernel go to raw IP protocol handler.
66 void
67 ip_init()
69 ipq.ip_link.next = ipq.ip_link.prev = &ipq.ip_link;
70 ip_id = tt.tv_sec & 0xffff;
71 udp_init();
72 tcp_init();
76 * Ip input routine. Checksum and byte swap header. If fragmented
77 * try to reassemble. Process options. Pass to next level.
79 void
80 ip_input(m)
81 struct mbuf *m;
83 register struct ip *ip;
84 int hlen;
86 DEBUG_CALL("ip_input");
87 DEBUG_ARG("m = %lx", (long)m);
88 DEBUG_ARG("m_len = %d", m->m_len);
90 STAT(ipstat.ips_total++);
92 if (m->m_len < sizeof (struct ip)) {
93 STAT(ipstat.ips_toosmall++);
94 return;
97 ip = mtod(m, struct ip *);
99 if (ip->ip_v != IPVERSION) {
100 STAT(ipstat.ips_badvers++);
101 goto bad;
104 hlen = ip->ip_hl << 2;
105 if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
106 STAT(ipstat.ips_badhlen++); /* or packet too short */
107 goto bad;
110 /* keep ip header intact for ICMP reply
111 * ip->ip_sum = cksum(m, hlen);
112 * if (ip->ip_sum) {
114 if(cksum(m,hlen)) {
115 STAT(ipstat.ips_badsum++);
116 goto bad;
120 * Convert fields to host representation.
122 NTOHS(ip->ip_len);
123 if (ip->ip_len < hlen) {
124 STAT(ipstat.ips_badlen++);
125 goto bad;
127 NTOHS(ip->ip_id);
128 NTOHS(ip->ip_off);
131 * Check that the amount of data in the buffers
132 * is as at least much as the IP header would have us expect.
133 * Trim mbufs if longer than we expect.
134 * Drop packet if shorter than we expect.
136 if (m->m_len < ip->ip_len) {
137 STAT(ipstat.ips_tooshort++);
138 goto bad;
141 if (slirp_restrict) {
142 if (memcmp(&ip->ip_dst.s_addr, &special_addr, 3)) {
143 if (ip->ip_dst.s_addr == 0xffffffff && ip->ip_p != IPPROTO_UDP)
144 goto bad;
145 } else {
146 int host = ntohl(ip->ip_dst.s_addr) & 0xff;
147 struct ex_list *ex_ptr;
149 if (host == 0xff)
150 goto bad;
152 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
153 if (ex_ptr->ex_addr == host)
154 break;
156 if (!ex_ptr)
157 goto bad;
161 /* Should drop packet if mbuf too long? hmmm... */
162 if (m->m_len > ip->ip_len)
163 m_adj(m, ip->ip_len - m->m_len);
165 /* check ip_ttl for a correct ICMP reply */
166 if(ip->ip_ttl==0 || ip->ip_ttl==1) {
167 icmp_error(m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");
168 goto bad;
172 * Process options and, if not destined for us,
173 * ship it on. ip_dooptions returns 1 when an
174 * error was detected (causing an icmp message
175 * to be sent and the original packet to be freed).
177 /* We do no IP options */
178 /* if (hlen > sizeof (struct ip) && ip_dooptions(m))
179 * goto next;
182 * If offset or IP_MF are set, must reassemble.
183 * Otherwise, nothing need be done.
184 * (We could look in the reassembly queue to see
185 * if the packet was previously fragmented,
186 * but it's not worth the time; just let them time out.)
188 * XXX This should fail, don't fragment yet
190 if (ip->ip_off &~ IP_DF) {
191 register struct ipq *fp;
192 struct qlink *l;
194 * Look for queue of fragments
195 * of this datagram.
197 for (l = ipq.ip_link.next; l != &ipq.ip_link; l = l->next) {
198 fp = container_of(l, struct ipq, ip_link);
199 if (ip->ip_id == fp->ipq_id &&
200 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
201 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
202 ip->ip_p == fp->ipq_p)
203 goto found;
205 fp = NULL;
206 found:
209 * Adjust ip_len to not reflect header,
210 * set ip_mff if more fragments are expected,
211 * convert offset of this to bytes.
213 ip->ip_len -= hlen;
214 if (ip->ip_off & IP_MF)
215 ip->ip_tos |= 1;
216 else
217 ip->ip_tos &= ~1;
219 ip->ip_off <<= 3;
222 * If datagram marked as having more fragments
223 * or if this is not the first fragment,
224 * attempt reassembly; if it succeeds, proceed.
226 if (ip->ip_tos & 1 || ip->ip_off) {
227 STAT(ipstat.ips_fragments++);
228 ip = ip_reass(ip, fp);
229 if (ip == 0)
230 return;
231 STAT(ipstat.ips_reassembled++);
232 m = dtom(ip);
233 } else
234 if (fp)
235 ip_freef(fp);
237 } else
238 ip->ip_len -= hlen;
241 * Switch out to protocol's input routine.
243 STAT(ipstat.ips_delivered++);
244 switch (ip->ip_p) {
245 case IPPROTO_TCP:
246 tcp_input(m, hlen, (struct socket *)NULL);
247 break;
248 case IPPROTO_UDP:
249 udp_input(m, hlen);
250 break;
251 case IPPROTO_ICMP:
252 icmp_input(m, hlen);
253 break;
254 default:
255 STAT(ipstat.ips_noproto++);
256 m_free(m);
258 return;
259 bad:
260 m_freem(m);
261 return;
264 #define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
265 #define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
267 * Take incoming datagram fragment and try to
268 * reassemble it into whole datagram. If a chain for
269 * reassembly of this datagram already exists, then it
270 * is given as fp; otherwise have to make a chain.
272 static struct ip *
273 ip_reass(register struct ip *ip, register struct ipq *fp)
275 register struct mbuf *m = dtom(ip);
276 register struct ipasfrag *q;
277 int hlen = ip->ip_hl << 2;
278 int i, next;
280 DEBUG_CALL("ip_reass");
281 DEBUG_ARG("ip = %lx", (long)ip);
282 DEBUG_ARG("fp = %lx", (long)fp);
283 DEBUG_ARG("m = %lx", (long)m);
286 * Presence of header sizes in mbufs
287 * would confuse code below.
288 * Fragment m_data is concatenated.
290 m->m_data += hlen;
291 m->m_len -= hlen;
294 * If first fragment to arrive, create a reassembly queue.
296 if (fp == 0) {
297 struct mbuf *t;
298 if ((t = m_get()) == NULL) goto dropfrag;
299 fp = mtod(t, struct ipq *);
300 insque(&fp->ip_link, &ipq.ip_link);
301 fp->ipq_ttl = IPFRAGTTL;
302 fp->ipq_p = ip->ip_p;
303 fp->ipq_id = ip->ip_id;
304 fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
305 fp->ipq_src = ip->ip_src;
306 fp->ipq_dst = ip->ip_dst;
307 q = (struct ipasfrag *)fp;
308 goto insert;
312 * Find a segment which begins after this one does.
314 for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
315 q = q->ipf_next)
316 if (q->ipf_off > ip->ip_off)
317 break;
320 * If there is a preceding segment, it may provide some of
321 * our data already. If so, drop the data from the incoming
322 * segment. If it provides all of our data, drop us.
324 if (q->ipf_prev != &fp->frag_link) {
325 struct ipasfrag *pq = q->ipf_prev;
326 i = pq->ipf_off + pq->ipf_len - ip->ip_off;
327 if (i > 0) {
328 if (i >= ip->ip_len)
329 goto dropfrag;
330 m_adj(dtom(ip), i);
331 ip->ip_off += i;
332 ip->ip_len -= i;
337 * While we overlap succeeding segments trim them or,
338 * if they are completely covered, dequeue them.
340 while (q != (struct ipasfrag*)&fp->frag_link &&
341 ip->ip_off + ip->ip_len > q->ipf_off) {
342 i = (ip->ip_off + ip->ip_len) - q->ipf_off;
343 if (i < q->ipf_len) {
344 q->ipf_len -= i;
345 q->ipf_off += i;
346 m_adj(dtom(q), i);
347 break;
349 q = q->ipf_next;
350 m_freem(dtom(q->ipf_prev));
351 ip_deq(q->ipf_prev);
354 insert:
356 * Stick new segment in its place;
357 * check for complete reassembly.
359 ip_enq(iptofrag(ip), q->ipf_prev);
360 next = 0;
361 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link;
362 q = q->ipf_next) {
363 if (q->ipf_off != next)
364 return (0);
365 next += q->ipf_len;
367 if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
368 return (0);
371 * Reassembly is complete; concatenate fragments.
373 q = fp->frag_link.next;
374 m = dtom(q);
376 q = (struct ipasfrag *) q->ipf_next;
377 while (q != (struct ipasfrag*)&fp->frag_link) {
378 struct mbuf *t = dtom(q);
379 q = (struct ipasfrag *) q->ipf_next;
380 m_cat(m, t);
384 * Create header for new ip packet by
385 * modifying header of first packet;
386 * dequeue and discard fragment reassembly header.
387 * Make header visible.
389 q = fp->frag_link.next;
392 * If the fragments concatenated to an mbuf that's
393 * bigger than the total size of the fragment, then and
394 * m_ext buffer was alloced. But fp->ipq_next points to
395 * the old buffer (in the mbuf), so we must point ip
396 * into the new buffer.
398 if (m->m_flags & M_EXT) {
399 int delta;
400 delta = (char *)ip - m->m_dat;
401 q = (struct ipasfrag *)(m->m_ext + delta);
404 /* DEBUG_ARG("ip = %lx", (long)ip);
405 * ip=(struct ipasfrag *)m->m_data; */
407 ip = fragtoip(q);
408 ip->ip_len = next;
409 ip->ip_tos &= ~1;
410 ip->ip_src = fp->ipq_src;
411 ip->ip_dst = fp->ipq_dst;
412 remque(&fp->ip_link);
413 (void) m_free(dtom(fp));
414 m->m_len += (ip->ip_hl << 2);
415 m->m_data -= (ip->ip_hl << 2);
417 return ip;
419 dropfrag:
420 STAT(ipstat.ips_fragdropped++);
421 m_freem(m);
422 return (0);
426 * Free a fragment reassembly header and all
427 * associated datagrams.
429 static void
430 ip_freef(struct ipq *fp)
432 register struct ipasfrag *q, *p;
434 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) {
435 p = q->ipf_next;
436 ip_deq(q);
437 m_freem(dtom(q));
439 remque(&fp->ip_link);
440 (void) m_free(dtom(fp));
444 * Put an ip fragment on a reassembly chain.
445 * Like insque, but pointers in middle of structure.
447 static void
448 ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
450 DEBUG_CALL("ip_enq");
451 DEBUG_ARG("prev = %lx", (long)prev);
452 p->ipf_prev = prev;
453 p->ipf_next = prev->ipf_next;
454 ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
455 prev->ipf_next = p;
459 * To ip_enq as remque is to insque.
461 static void
462 ip_deq(register struct ipasfrag *p)
464 ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
465 ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
469 * IP timer processing;
470 * if a timer expires on a reassembly
471 * queue, discard it.
473 void
474 ip_slowtimo()
476 struct qlink *l;
478 DEBUG_CALL("ip_slowtimo");
480 l = ipq.ip_link.next;
482 if (l == 0)
483 return;
485 while (l != &ipq.ip_link) {
486 struct ipq *fp = container_of(l, struct ipq, ip_link);
487 l = l->next;
488 if (--fp->ipq_ttl == 0) {
489 STAT(ipstat.ips_fragtimeout++);
490 ip_freef(fp);
496 * Do option processing on a datagram,
497 * possibly discarding it if bad options are encountered,
498 * or forwarding it if source-routed.
499 * Returns 1 if packet has been forwarded/freed,
500 * 0 if the packet should be processed further.
503 #ifdef notdef
506 ip_dooptions(m)
507 struct mbuf *m;
509 register struct ip *ip = mtod(m, struct ip *);
510 register u_char *cp;
511 register struct ip_timestamp *ipt;
512 register struct in_ifaddr *ia;
513 /* int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
514 int opt, optlen, cnt, off, code, type, forward = 0;
515 struct in_addr *sin, dst;
516 typedef u_int32_t n_time;
517 n_time ntime;
519 dst = ip->ip_dst;
520 cp = (u_char *)(ip + 1);
521 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
522 for (; cnt > 0; cnt -= optlen, cp += optlen) {
523 opt = cp[IPOPT_OPTVAL];
524 if (opt == IPOPT_EOL)
525 break;
526 if (opt == IPOPT_NOP)
527 optlen = 1;
528 else {
529 optlen = cp[IPOPT_OLEN];
530 if (optlen <= 0 || optlen > cnt) {
531 code = &cp[IPOPT_OLEN] - (u_char *)ip;
532 goto bad;
535 switch (opt) {
537 default:
538 break;
541 * Source routing with record.
542 * Find interface with current destination address.
543 * If none on this machine then drop if strictly routed,
544 * or do nothing if loosely routed.
545 * Record interface address and bring up next address
546 * component. If strictly routed make sure next
547 * address is on directly accessible net.
549 case IPOPT_LSRR:
550 case IPOPT_SSRR:
551 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
552 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
553 goto bad;
555 ipaddr.sin_addr = ip->ip_dst;
556 ia = (struct in_ifaddr *)
557 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
558 if (ia == 0) {
559 if (opt == IPOPT_SSRR) {
560 type = ICMP_UNREACH;
561 code = ICMP_UNREACH_SRCFAIL;
562 goto bad;
565 * Loose routing, and not at next destination
566 * yet; nothing to do except forward.
568 break;
570 off--; / * 0 origin * /
571 if (off > optlen - sizeof(struct in_addr)) {
573 * End of source route. Should be for us.
575 save_rte(cp, ip->ip_src);
576 break;
579 * locate outgoing interface
581 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
582 sizeof(ipaddr.sin_addr));
583 if (opt == IPOPT_SSRR) {
584 #define INA struct in_ifaddr *
585 #define SA struct sockaddr *
586 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
587 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
588 } else
589 ia = ip_rtaddr(ipaddr.sin_addr);
590 if (ia == 0) {
591 type = ICMP_UNREACH;
592 code = ICMP_UNREACH_SRCFAIL;
593 goto bad;
595 ip->ip_dst = ipaddr.sin_addr;
596 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
597 (caddr_t)(cp + off), sizeof(struct in_addr));
598 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
600 * Let ip_intr's mcast routing check handle mcast pkts
602 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
603 break;
605 case IPOPT_RR:
606 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
607 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
608 goto bad;
611 * If no space remains, ignore.
613 off--; * 0 origin *
614 if (off > optlen - sizeof(struct in_addr))
615 break;
616 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
617 sizeof(ipaddr.sin_addr));
619 * locate outgoing interface; if we're the destination,
620 * use the incoming interface (should be same).
622 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
623 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
624 type = ICMP_UNREACH;
625 code = ICMP_UNREACH_HOST;
626 goto bad;
628 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
629 (caddr_t)(cp + off), sizeof(struct in_addr));
630 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
631 break;
633 case IPOPT_TS:
634 code = cp - (u_char *)ip;
635 ipt = (struct ip_timestamp *)cp;
636 if (ipt->ipt_len < 5)
637 goto bad;
638 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
639 if (++ipt->ipt_oflw == 0)
640 goto bad;
641 break;
643 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
644 switch (ipt->ipt_flg) {
646 case IPOPT_TS_TSONLY:
647 break;
649 case IPOPT_TS_TSANDADDR:
650 if (ipt->ipt_ptr + sizeof(n_time) +
651 sizeof(struct in_addr) > ipt->ipt_len)
652 goto bad;
653 ipaddr.sin_addr = dst;
654 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
655 m->m_pkthdr.rcvif);
656 if (ia == 0)
657 continue;
658 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
659 (caddr_t)sin, sizeof(struct in_addr));
660 ipt->ipt_ptr += sizeof(struct in_addr);
661 break;
663 case IPOPT_TS_PRESPEC:
664 if (ipt->ipt_ptr + sizeof(n_time) +
665 sizeof(struct in_addr) > ipt->ipt_len)
666 goto bad;
667 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
668 sizeof(struct in_addr));
669 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
670 continue;
671 ipt->ipt_ptr += sizeof(struct in_addr);
672 break;
674 default:
675 goto bad;
677 ntime = iptime();
678 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
679 sizeof(n_time));
680 ipt->ipt_ptr += sizeof(n_time);
683 if (forward) {
684 ip_forward(m, 1);
685 return (1);
689 return (0);
690 bad:
691 /* ip->ip_len -= ip->ip_hl << 2; XXX icmp_error adds in hdr length */
693 /* Not yet */
694 icmp_error(m, type, code, 0, 0);
696 STAT(ipstat.ips_badoptions++);
697 return (1);
700 #endif /* notdef */
703 * Strip out IP options, at higher
704 * level protocol in the kernel.
705 * Second argument is buffer to which options
706 * will be moved, and return value is their length.
707 * (XXX) should be deleted; last arg currently ignored.
709 void
710 ip_stripoptions(m, mopt)
711 register struct mbuf *m;
712 struct mbuf *mopt;
714 register int i;
715 struct ip *ip = mtod(m, struct ip *);
716 register caddr_t opts;
717 int olen;
719 olen = (ip->ip_hl<<2) - sizeof (struct ip);
720 opts = (caddr_t)(ip + 1);
721 i = m->m_len - (sizeof (struct ip) + olen);
722 memcpy(opts, opts + olen, (unsigned)i);
723 m->m_len -= olen;
725 ip->ip_hl = sizeof(struct ip) >> 2;