slirp: Make IP packet ID consistent
[qemu-kvm/fedora.git] / slirp / ip_input.c
blobb07d3d5d5d3ff3f952cedd57d1e915246527c417
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 udp_init();
63 tcp_init();
67 * Ip input routine. Checksum and byte swap header. If fragmented
68 * try to reassemble. Process options. Pass to next level.
70 void
71 ip_input(struct mbuf *m)
73 register struct ip *ip;
74 int hlen;
76 DEBUG_CALL("ip_input");
77 DEBUG_ARG("m = %lx", (long)m);
78 DEBUG_ARG("m_len = %d", m->m_len);
80 if (m->m_len < sizeof (struct ip)) {
81 return;
84 ip = mtod(m, struct ip *);
86 if (ip->ip_v != IPVERSION) {
87 goto bad;
90 hlen = ip->ip_hl << 2;
91 if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
92 goto bad; /* or packet too short */
95 /* keep ip header intact for ICMP reply
96 * ip->ip_sum = cksum(m, hlen);
97 * if (ip->ip_sum) {
99 if(cksum(m,hlen)) {
100 goto bad;
104 * Convert fields to host representation.
106 NTOHS(ip->ip_len);
107 if (ip->ip_len < hlen) {
108 goto bad;
110 NTOHS(ip->ip_id);
111 NTOHS(ip->ip_off);
114 * Check that the amount of data in the buffers
115 * is as at least much as the IP header would have us expect.
116 * Trim mbufs if longer than we expect.
117 * Drop packet if shorter than we expect.
119 if (m->m_len < ip->ip_len) {
120 goto bad;
123 if (slirp_restrict) {
124 if ((ip->ip_dst.s_addr & vnetwork_mask.s_addr) ==
125 vnetwork_addr.s_addr) {
126 if (ip->ip_dst.s_addr == 0xffffffff && ip->ip_p != IPPROTO_UDP)
127 goto bad;
128 } else {
129 struct ex_list *ex_ptr;
131 if ((ip->ip_dst.s_addr & ~vnetwork_mask.s_addr) ==
132 ~vnetwork_mask.s_addr)
133 goto bad;
135 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
136 if (ex_ptr->ex_addr.s_addr == ip->ip_dst.s_addr)
137 break;
139 if (!ex_ptr)
140 goto bad;
144 /* Should drop packet if mbuf too long? hmmm... */
145 if (m->m_len > ip->ip_len)
146 m_adj(m, ip->ip_len - m->m_len);
148 /* check ip_ttl for a correct ICMP reply */
149 if(ip->ip_ttl==0 || ip->ip_ttl==1) {
150 icmp_error(m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");
151 goto bad;
155 * If offset or IP_MF are set, must reassemble.
156 * Otherwise, nothing need be done.
157 * (We could look in the reassembly queue to see
158 * if the packet was previously fragmented,
159 * but it's not worth the time; just let them time out.)
161 * XXX This should fail, don't fragment yet
163 if (ip->ip_off &~ IP_DF) {
164 register struct ipq *fp;
165 struct qlink *l;
167 * Look for queue of fragments
168 * of this datagram.
170 for (l = ipq.ip_link.next; l != &ipq.ip_link; l = l->next) {
171 fp = container_of(l, struct ipq, ip_link);
172 if (ip->ip_id == fp->ipq_id &&
173 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
174 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
175 ip->ip_p == fp->ipq_p)
176 goto found;
178 fp = NULL;
179 found:
182 * Adjust ip_len to not reflect header,
183 * set ip_mff if more fragments are expected,
184 * convert offset of this to bytes.
186 ip->ip_len -= hlen;
187 if (ip->ip_off & IP_MF)
188 ip->ip_tos |= 1;
189 else
190 ip->ip_tos &= ~1;
192 ip->ip_off <<= 3;
195 * If datagram marked as having more fragments
196 * or if this is not the first fragment,
197 * attempt reassembly; if it succeeds, proceed.
199 if (ip->ip_tos & 1 || ip->ip_off) {
200 ip = ip_reass(ip, fp);
201 if (ip == NULL)
202 return;
203 m = dtom(ip);
204 } else
205 if (fp)
206 ip_freef(fp);
208 } else
209 ip->ip_len -= hlen;
212 * Switch out to protocol's input routine.
214 switch (ip->ip_p) {
215 case IPPROTO_TCP:
216 tcp_input(m, hlen, (struct socket *)NULL);
217 break;
218 case IPPROTO_UDP:
219 udp_input(m, hlen);
220 break;
221 case IPPROTO_ICMP:
222 icmp_input(m, hlen);
223 break;
224 default:
225 m_free(m);
227 return;
228 bad:
229 m_freem(m);
230 return;
233 #define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
234 #define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
236 * Take incoming datagram fragment and try to
237 * reassemble it into whole datagram. If a chain for
238 * reassembly of this datagram already exists, then it
239 * is given as fp; otherwise have to make a chain.
241 static struct ip *
242 ip_reass(register struct ip *ip, register struct ipq *fp)
244 register struct mbuf *m = dtom(ip);
245 register struct ipasfrag *q;
246 int hlen = ip->ip_hl << 2;
247 int i, next;
249 DEBUG_CALL("ip_reass");
250 DEBUG_ARG("ip = %lx", (long)ip);
251 DEBUG_ARG("fp = %lx", (long)fp);
252 DEBUG_ARG("m = %lx", (long)m);
255 * Presence of header sizes in mbufs
256 * would confuse code below.
257 * Fragment m_data is concatenated.
259 m->m_data += hlen;
260 m->m_len -= hlen;
263 * If first fragment to arrive, create a reassembly queue.
265 if (fp == NULL) {
266 struct mbuf *t;
267 if ((t = m_get()) == NULL) goto dropfrag;
268 fp = mtod(t, struct ipq *);
269 insque(&fp->ip_link, &ipq.ip_link);
270 fp->ipq_ttl = IPFRAGTTL;
271 fp->ipq_p = ip->ip_p;
272 fp->ipq_id = ip->ip_id;
273 fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
274 fp->ipq_src = ip->ip_src;
275 fp->ipq_dst = ip->ip_dst;
276 q = (struct ipasfrag *)fp;
277 goto insert;
281 * Find a segment which begins after this one does.
283 for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
284 q = q->ipf_next)
285 if (q->ipf_off > ip->ip_off)
286 break;
289 * If there is a preceding segment, it may provide some of
290 * our data already. If so, drop the data from the incoming
291 * segment. If it provides all of our data, drop us.
293 if (q->ipf_prev != &fp->frag_link) {
294 struct ipasfrag *pq = q->ipf_prev;
295 i = pq->ipf_off + pq->ipf_len - ip->ip_off;
296 if (i > 0) {
297 if (i >= ip->ip_len)
298 goto dropfrag;
299 m_adj(dtom(ip), i);
300 ip->ip_off += i;
301 ip->ip_len -= i;
306 * While we overlap succeeding segments trim them or,
307 * if they are completely covered, dequeue them.
309 while (q != (struct ipasfrag*)&fp->frag_link &&
310 ip->ip_off + ip->ip_len > q->ipf_off) {
311 i = (ip->ip_off + ip->ip_len) - q->ipf_off;
312 if (i < q->ipf_len) {
313 q->ipf_len -= i;
314 q->ipf_off += i;
315 m_adj(dtom(q), i);
316 break;
318 q = q->ipf_next;
319 m_freem(dtom(q->ipf_prev));
320 ip_deq(q->ipf_prev);
323 insert:
325 * Stick new segment in its place;
326 * check for complete reassembly.
328 ip_enq(iptofrag(ip), q->ipf_prev);
329 next = 0;
330 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link;
331 q = q->ipf_next) {
332 if (q->ipf_off != next)
333 return NULL;
334 next += q->ipf_len;
336 if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
337 return NULL;
340 * Reassembly is complete; concatenate fragments.
342 q = fp->frag_link.next;
343 m = dtom(q);
345 q = (struct ipasfrag *) q->ipf_next;
346 while (q != (struct ipasfrag*)&fp->frag_link) {
347 struct mbuf *t = dtom(q);
348 q = (struct ipasfrag *) q->ipf_next;
349 m_cat(m, t);
353 * Create header for new ip packet by
354 * modifying header of first packet;
355 * dequeue and discard fragment reassembly header.
356 * Make header visible.
358 q = fp->frag_link.next;
361 * If the fragments concatenated to an mbuf that's
362 * bigger than the total size of the fragment, then and
363 * m_ext buffer was alloced. But fp->ipq_next points to
364 * the old buffer (in the mbuf), so we must point ip
365 * into the new buffer.
367 if (m->m_flags & M_EXT) {
368 int delta = (char *)q - m->m_dat;
369 q = (struct ipasfrag *)(m->m_ext + delta);
372 ip = fragtoip(q);
373 ip->ip_len = next;
374 ip->ip_tos &= ~1;
375 ip->ip_src = fp->ipq_src;
376 ip->ip_dst = fp->ipq_dst;
377 remque(&fp->ip_link);
378 (void) m_free(dtom(fp));
379 m->m_len += (ip->ip_hl << 2);
380 m->m_data -= (ip->ip_hl << 2);
382 return ip;
384 dropfrag:
385 m_freem(m);
386 return NULL;
390 * Free a fragment reassembly header and all
391 * associated datagrams.
393 static void
394 ip_freef(struct ipq *fp)
396 register struct ipasfrag *q, *p;
398 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) {
399 p = q->ipf_next;
400 ip_deq(q);
401 m_freem(dtom(q));
403 remque(&fp->ip_link);
404 (void) m_free(dtom(fp));
408 * Put an ip fragment on a reassembly chain.
409 * Like insque, but pointers in middle of structure.
411 static void
412 ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
414 DEBUG_CALL("ip_enq");
415 DEBUG_ARG("prev = %lx", (long)prev);
416 p->ipf_prev = prev;
417 p->ipf_next = prev->ipf_next;
418 ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
419 prev->ipf_next = p;
423 * To ip_enq as remque is to insque.
425 static void
426 ip_deq(register struct ipasfrag *p)
428 ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
429 ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
433 * IP timer processing;
434 * if a timer expires on a reassembly
435 * queue, discard it.
437 void
438 ip_slowtimo(void)
440 struct qlink *l;
442 DEBUG_CALL("ip_slowtimo");
444 l = ipq.ip_link.next;
446 if (l == NULL)
447 return;
449 while (l != &ipq.ip_link) {
450 struct ipq *fp = container_of(l, struct ipq, ip_link);
451 l = l->next;
452 if (--fp->ipq_ttl == 0) {
453 ip_freef(fp);
459 * Do option processing on a datagram,
460 * possibly discarding it if bad options are encountered,
461 * or forwarding it if source-routed.
462 * Returns 1 if packet has been forwarded/freed,
463 * 0 if the packet should be processed further.
466 #ifdef notdef
469 ip_dooptions(m)
470 struct mbuf *m;
472 register struct ip *ip = mtod(m, struct ip *);
473 register u_char *cp;
474 register struct ip_timestamp *ipt;
475 register struct in_ifaddr *ia;
476 int opt, optlen, cnt, off, code, type, forward = 0;
477 struct in_addr *sin, dst;
478 typedef u_int32_t n_time;
479 n_time ntime;
481 dst = ip->ip_dst;
482 cp = (u_char *)(ip + 1);
483 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
484 for (; cnt > 0; cnt -= optlen, cp += optlen) {
485 opt = cp[IPOPT_OPTVAL];
486 if (opt == IPOPT_EOL)
487 break;
488 if (opt == IPOPT_NOP)
489 optlen = 1;
490 else {
491 optlen = cp[IPOPT_OLEN];
492 if (optlen <= 0 || optlen > cnt) {
493 code = &cp[IPOPT_OLEN] - (u_char *)ip;
494 goto bad;
497 switch (opt) {
499 default:
500 break;
503 * Source routing with record.
504 * Find interface with current destination address.
505 * If none on this machine then drop if strictly routed,
506 * or do nothing if loosely routed.
507 * Record interface address and bring up next address
508 * component. If strictly routed make sure next
509 * address is on directly accessible net.
511 case IPOPT_LSRR:
512 case IPOPT_SSRR:
513 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
514 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
515 goto bad;
517 ipaddr.sin_addr = ip->ip_dst;
518 ia = (struct in_ifaddr *)
519 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
520 if (ia == 0) {
521 if (opt == IPOPT_SSRR) {
522 type = ICMP_UNREACH;
523 code = ICMP_UNREACH_SRCFAIL;
524 goto bad;
527 * Loose routing, and not at next destination
528 * yet; nothing to do except forward.
530 break;
532 off--; / * 0 origin * /
533 if (off > optlen - sizeof(struct in_addr)) {
535 * End of source route. Should be for us.
537 save_rte(cp, ip->ip_src);
538 break;
541 * locate outgoing interface
543 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
544 sizeof(ipaddr.sin_addr));
545 if (opt == IPOPT_SSRR) {
546 #define INA struct in_ifaddr *
547 #define SA struct sockaddr *
548 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
549 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
550 } else
551 ia = ip_rtaddr(ipaddr.sin_addr);
552 if (ia == 0) {
553 type = ICMP_UNREACH;
554 code = ICMP_UNREACH_SRCFAIL;
555 goto bad;
557 ip->ip_dst = ipaddr.sin_addr;
558 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
559 (caddr_t)(cp + off), sizeof(struct in_addr));
560 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
562 * Let ip_intr's mcast routing check handle mcast pkts
564 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
565 break;
567 case IPOPT_RR:
568 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
569 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
570 goto bad;
573 * If no space remains, ignore.
575 off--; * 0 origin *
576 if (off > optlen - sizeof(struct in_addr))
577 break;
578 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
579 sizeof(ipaddr.sin_addr));
581 * locate outgoing interface; if we're the destination,
582 * use the incoming interface (should be same).
584 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
585 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
586 type = ICMP_UNREACH;
587 code = ICMP_UNREACH_HOST;
588 goto bad;
590 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
591 (caddr_t)(cp + off), sizeof(struct in_addr));
592 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
593 break;
595 case IPOPT_TS:
596 code = cp - (u_char *)ip;
597 ipt = (struct ip_timestamp *)cp;
598 if (ipt->ipt_len < 5)
599 goto bad;
600 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
601 if (++ipt->ipt_oflw == 0)
602 goto bad;
603 break;
605 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
606 switch (ipt->ipt_flg) {
608 case IPOPT_TS_TSONLY:
609 break;
611 case IPOPT_TS_TSANDADDR:
612 if (ipt->ipt_ptr + sizeof(n_time) +
613 sizeof(struct in_addr) > ipt->ipt_len)
614 goto bad;
615 ipaddr.sin_addr = dst;
616 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
617 m->m_pkthdr.rcvif);
618 if (ia == 0)
619 continue;
620 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
621 (caddr_t)sin, sizeof(struct in_addr));
622 ipt->ipt_ptr += sizeof(struct in_addr);
623 break;
625 case IPOPT_TS_PRESPEC:
626 if (ipt->ipt_ptr + sizeof(n_time) +
627 sizeof(struct in_addr) > ipt->ipt_len)
628 goto bad;
629 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
630 sizeof(struct in_addr));
631 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
632 continue;
633 ipt->ipt_ptr += sizeof(struct in_addr);
634 break;
636 default:
637 goto bad;
639 ntime = iptime();
640 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
641 sizeof(n_time));
642 ipt->ipt_ptr += sizeof(n_time);
645 if (forward) {
646 ip_forward(m, 1);
647 return (1);
651 return (0);
652 bad:
653 icmp_error(m, type, code, 0, 0);
655 return (1);
658 #endif /* notdef */
661 * Strip out IP options, at higher
662 * level protocol in the kernel.
663 * Second argument is buffer to which options
664 * will be moved, and return value is their length.
665 * (XXX) should be deleted; last arg currently ignored.
667 void
668 ip_stripoptions(register struct mbuf *m, struct mbuf *mopt)
670 register int i;
671 struct ip *ip = mtod(m, struct ip *);
672 register caddr_t opts;
673 int olen;
675 olen = (ip->ip_hl<<2) - sizeof (struct ip);
676 opts = (caddr_t)(ip + 1);
677 i = m->m_len - (sizeof (struct ip) + olen);
678 memcpy(opts, opts + olen, (unsigned)i);
679 m->m_len -= olen;
681 ip->ip_hl = sizeof(struct ip) >> 2;