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
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
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.
51 static struct ip *ip_reass(register struct ip *ip,
52 register struct ipq *fp);
53 static void ip_freef(struct ipq *fp);
54 static void ip_enq(register struct ipasfrag *p,
55 register struct ipasfrag *prev);
56 static void ip_deq(register struct ipasfrag *p);
59 * IP initialization: fill in IP protocol switch table.
60 * All protocols not implemented in kernel go to raw IP protocol handler.
65 ipq.ip_link.next = ipq.ip_link.prev = &ipq.ip_link;
66 ip_id = tt.tv_sec & 0xffff;
72 * Ip input routine. Checksum and byte swap header. If fragmented
73 * try to reassemble. Process options. Pass to next level.
79 register struct ip *ip;
82 DEBUG_CALL("ip_input");
83 DEBUG_ARG("m = %lx", (long)m);
84 DEBUG_ARG("m_len = %d", m->m_len);
86 STAT(ipstat.ips_total++);
88 if (m->m_len < sizeof (struct ip)) {
89 STAT(ipstat.ips_toosmall++);
93 ip = mtod(m, struct ip *);
95 if (ip->ip_v != IPVERSION) {
96 STAT(ipstat.ips_badvers++);
100 hlen = ip->ip_hl << 2;
101 if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
102 STAT(ipstat.ips_badhlen++); /* or packet too short */
106 /* keep ip header intact for ICMP reply
107 * ip->ip_sum = cksum(m, hlen);
111 STAT(ipstat.ips_badsum++);
116 * Convert fields to host representation.
119 if (ip->ip_len < hlen) {
120 STAT(ipstat.ips_badlen++);
127 * Check that the amount of data in the buffers
128 * is as at least much as the IP header would have us expect.
129 * Trim mbufs if longer than we expect.
130 * Drop packet if shorter than we expect.
132 if (m->m_len < ip->ip_len) {
133 STAT(ipstat.ips_tooshort++);
137 if (slirp_restrict) {
138 if (memcmp(&ip->ip_dst.s_addr, &special_addr, 3)) {
139 if (ip->ip_dst.s_addr == 0xffffffff && ip->ip_p != IPPROTO_UDP)
142 int host = ntohl(ip->ip_dst.s_addr) & 0xff;
143 struct ex_list *ex_ptr;
148 for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
149 if (ex_ptr->ex_addr == host)
157 /* Should drop packet if mbuf too long? hmmm... */
158 if (m->m_len > ip->ip_len)
159 m_adj(m, ip->ip_len - m->m_len);
161 /* check ip_ttl for a correct ICMP reply */
162 if(ip->ip_ttl==0 || ip->ip_ttl==1) {
163 icmp_error(m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");
168 * Process options and, if not destined for us,
169 * ship it on. ip_dooptions returns 1 when an
170 * error was detected (causing an icmp message
171 * to be sent and the original packet to be freed).
173 /* We do no IP options */
174 /* if (hlen > sizeof (struct ip) && ip_dooptions(m))
178 * If offset or IP_MF are set, must reassemble.
179 * Otherwise, nothing need be done.
180 * (We could look in the reassembly queue to see
181 * if the packet was previously fragmented,
182 * but it's not worth the time; just let them time out.)
184 * XXX This should fail, don't fragment yet
186 if (ip->ip_off &~ IP_DF) {
187 register struct ipq *fp;
190 * Look for queue of fragments
193 for (l = ipq.ip_link.next; l != &ipq.ip_link; l = l->next) {
194 fp = container_of(l, struct ipq, ip_link);
195 if (ip->ip_id == fp->ipq_id &&
196 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
197 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
198 ip->ip_p == fp->ipq_p)
205 * Adjust ip_len to not reflect header,
206 * set ip_mff if more fragments are expected,
207 * convert offset of this to bytes.
210 if (ip->ip_off & IP_MF)
218 * If datagram marked as having more fragments
219 * or if this is not the first fragment,
220 * attempt reassembly; if it succeeds, proceed.
222 if (ip->ip_tos & 1 || ip->ip_off) {
223 STAT(ipstat.ips_fragments++);
224 ip = ip_reass(ip, fp);
227 STAT(ipstat.ips_reassembled++);
237 * Switch out to protocol's input routine.
239 STAT(ipstat.ips_delivered++);
242 tcp_input(m, hlen, (struct socket *)NULL);
251 STAT(ipstat.ips_noproto++);
260 #define iptofrag(P) ((struct ipasfrag *)(((char*)(P)) - sizeof(struct qlink)))
261 #define fragtoip(P) ((struct ip*)(((char*)(P)) + sizeof(struct qlink)))
263 * Take incoming datagram fragment and try to
264 * reassemble it into whole datagram. If a chain for
265 * reassembly of this datagram already exists, then it
266 * is given as fp; otherwise have to make a chain.
269 ip_reass(register struct ip *ip, register struct ipq *fp)
271 register struct mbuf *m = dtom(ip);
272 register struct ipasfrag *q;
273 int hlen = ip->ip_hl << 2;
276 DEBUG_CALL("ip_reass");
277 DEBUG_ARG("ip = %lx", (long)ip);
278 DEBUG_ARG("fp = %lx", (long)fp);
279 DEBUG_ARG("m = %lx", (long)m);
282 * Presence of header sizes in mbufs
283 * would confuse code below.
284 * Fragment m_data is concatenated.
290 * If first fragment to arrive, create a reassembly queue.
294 if ((t = m_get()) == NULL) goto dropfrag;
295 fp = mtod(t, struct ipq *);
296 insque(&fp->ip_link, &ipq.ip_link);
297 fp->ipq_ttl = IPFRAGTTL;
298 fp->ipq_p = ip->ip_p;
299 fp->ipq_id = ip->ip_id;
300 fp->frag_link.next = fp->frag_link.prev = &fp->frag_link;
301 fp->ipq_src = ip->ip_src;
302 fp->ipq_dst = ip->ip_dst;
303 q = (struct ipasfrag *)fp;
308 * Find a segment which begins after this one does.
310 for (q = fp->frag_link.next; q != (struct ipasfrag *)&fp->frag_link;
312 if (q->ipf_off > ip->ip_off)
316 * If there is a preceding segment, it may provide some of
317 * our data already. If so, drop the data from the incoming
318 * segment. If it provides all of our data, drop us.
320 if (q->ipf_prev != &fp->frag_link) {
321 struct ipasfrag *pq = q->ipf_prev;
322 i = pq->ipf_off + pq->ipf_len - ip->ip_off;
333 * While we overlap succeeding segments trim them or,
334 * if they are completely covered, dequeue them.
336 while (q != (struct ipasfrag*)&fp->frag_link &&
337 ip->ip_off + ip->ip_len > q->ipf_off) {
338 i = (ip->ip_off + ip->ip_len) - q->ipf_off;
339 if (i < q->ipf_len) {
346 m_freem(dtom(q->ipf_prev));
352 * Stick new segment in its place;
353 * check for complete reassembly.
355 ip_enq(iptofrag(ip), q->ipf_prev);
357 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link;
359 if (q->ipf_off != next)
363 if (((struct ipasfrag *)(q->ipf_prev))->ipf_tos & 1)
367 * Reassembly is complete; concatenate fragments.
369 q = fp->frag_link.next;
372 q = (struct ipasfrag *) q->ipf_next;
373 while (q != (struct ipasfrag*)&fp->frag_link) {
374 struct mbuf *t = dtom(q);
375 q = (struct ipasfrag *) q->ipf_next;
380 * Create header for new ip packet by
381 * modifying header of first packet;
382 * dequeue and discard fragment reassembly header.
383 * Make header visible.
385 q = fp->frag_link.next;
388 * If the fragments concatenated to an mbuf that's
389 * bigger than the total size of the fragment, then and
390 * m_ext buffer was alloced. But fp->ipq_next points to
391 * the old buffer (in the mbuf), so we must point ip
392 * into the new buffer.
394 if (m->m_flags & M_EXT) {
396 delta = (char *)ip - m->m_dat;
397 q = (struct ipasfrag *)(m->m_ext + delta);
400 /* DEBUG_ARG("ip = %lx", (long)ip);
401 * ip=(struct ipasfrag *)m->m_data; */
406 ip->ip_src = fp->ipq_src;
407 ip->ip_dst = fp->ipq_dst;
408 remque(&fp->ip_link);
409 (void) m_free(dtom(fp));
410 m->m_len += (ip->ip_hl << 2);
411 m->m_data -= (ip->ip_hl << 2);
416 STAT(ipstat.ips_fragdropped++);
422 * Free a fragment reassembly header and all
423 * associated datagrams.
426 ip_freef(struct ipq *fp)
428 register struct ipasfrag *q, *p;
430 for (q = fp->frag_link.next; q != (struct ipasfrag*)&fp->frag_link; q = p) {
435 remque(&fp->ip_link);
436 (void) m_free(dtom(fp));
440 * Put an ip fragment on a reassembly chain.
441 * Like insque, but pointers in middle of structure.
444 ip_enq(register struct ipasfrag *p, register struct ipasfrag *prev)
446 DEBUG_CALL("ip_enq");
447 DEBUG_ARG("prev = %lx", (long)prev);
449 p->ipf_next = prev->ipf_next;
450 ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = p;
455 * To ip_enq as remque is to insque.
458 ip_deq(register struct ipasfrag *p)
460 ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
461 ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
465 * IP timer processing;
466 * if a timer expires on a reassembly
474 DEBUG_CALL("ip_slowtimo");
476 l = ipq.ip_link.next;
481 while (l != &ipq.ip_link) {
482 struct ipq *fp = container_of(l, struct ipq, ip_link);
484 if (--fp->ipq_ttl == 0) {
485 STAT(ipstat.ips_fragtimeout++);
492 * Do option processing on a datagram,
493 * possibly discarding it if bad options are encountered,
494 * or forwarding it if source-routed.
495 * Returns 1 if packet has been forwarded/freed,
496 * 0 if the packet should be processed further.
505 register struct ip *ip = mtod(m, struct ip *);
507 register struct ip_timestamp *ipt;
508 register struct in_ifaddr *ia;
509 /* int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
510 int opt, optlen, cnt, off, code, type, forward = 0;
511 struct in_addr *sin, dst;
512 typedef u_int32_t n_time;
516 cp = (u_char *)(ip + 1);
517 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
518 for (; cnt > 0; cnt -= optlen, cp += optlen) {
519 opt = cp[IPOPT_OPTVAL];
520 if (opt == IPOPT_EOL)
522 if (opt == IPOPT_NOP)
525 optlen = cp[IPOPT_OLEN];
526 if (optlen <= 0 || optlen > cnt) {
527 code = &cp[IPOPT_OLEN] - (u_char *)ip;
537 * Source routing with record.
538 * Find interface with current destination address.
539 * If none on this machine then drop if strictly routed,
540 * or do nothing if loosely routed.
541 * Record interface address and bring up next address
542 * component. If strictly routed make sure next
543 * address is on directly accessible net.
547 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
548 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
551 ipaddr.sin_addr = ip->ip_dst;
552 ia = (struct in_ifaddr *)
553 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
555 if (opt == IPOPT_SSRR) {
557 code = ICMP_UNREACH_SRCFAIL;
561 * Loose routing, and not at next destination
562 * yet; nothing to do except forward.
566 off--; / * 0 origin * /
567 if (off > optlen - sizeof(struct in_addr)) {
569 * End of source route. Should be for us.
571 save_rte(cp, ip->ip_src);
575 * locate outgoing interface
577 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
578 sizeof(ipaddr.sin_addr));
579 if (opt == IPOPT_SSRR) {
580 #define INA struct in_ifaddr *
581 #define SA struct sockaddr *
582 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
583 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
585 ia = ip_rtaddr(ipaddr.sin_addr);
588 code = ICMP_UNREACH_SRCFAIL;
591 ip->ip_dst = ipaddr.sin_addr;
592 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
593 (caddr_t)(cp + off), sizeof(struct in_addr));
594 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
596 * Let ip_intr's mcast routing check handle mcast pkts
598 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
602 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
603 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
607 * If no space remains, ignore.
610 if (off > optlen - sizeof(struct in_addr))
612 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
613 sizeof(ipaddr.sin_addr));
615 * locate outgoing interface; if we're the destination,
616 * use the incoming interface (should be same).
618 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
619 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
621 code = ICMP_UNREACH_HOST;
624 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
625 (caddr_t)(cp + off), sizeof(struct in_addr));
626 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
630 code = cp - (u_char *)ip;
631 ipt = (struct ip_timestamp *)cp;
632 if (ipt->ipt_len < 5)
634 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
635 if (++ipt->ipt_oflw == 0)
639 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
640 switch (ipt->ipt_flg) {
642 case IPOPT_TS_TSONLY:
645 case IPOPT_TS_TSANDADDR:
646 if (ipt->ipt_ptr + sizeof(n_time) +
647 sizeof(struct in_addr) > ipt->ipt_len)
649 ipaddr.sin_addr = dst;
650 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
654 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
655 (caddr_t)sin, sizeof(struct in_addr));
656 ipt->ipt_ptr += sizeof(struct in_addr);
659 case IPOPT_TS_PRESPEC:
660 if (ipt->ipt_ptr + sizeof(n_time) +
661 sizeof(struct in_addr) > ipt->ipt_len)
663 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
664 sizeof(struct in_addr));
665 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
667 ipt->ipt_ptr += sizeof(struct in_addr);
674 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
676 ipt->ipt_ptr += sizeof(n_time);
687 /* ip->ip_len -= ip->ip_hl << 2; XXX icmp_error adds in hdr length */
690 icmp_error(m, type, code, 0, 0);
692 STAT(ipstat.ips_badoptions++);
699 * Strip out IP options, at higher
700 * level protocol in the kernel.
701 * Second argument is buffer to which options
702 * will be moved, and return value is their length.
703 * (XXX) should be deleted; last arg currently ignored.
706 ip_stripoptions(m, mopt)
707 register struct mbuf *m;
711 struct ip *ip = mtod(m, struct ip *);
712 register caddr_t opts;
715 olen = (ip->ip_hl<<2) - sizeof (struct ip);
716 opts = (caddr_t)(ip + 1);
717 i = m->m_len - (sizeof (struct ip) + olen);
718 memcpy(opts, opts + olen, (unsigned)i);
721 ip->ip_hl = sizeof(struct ip) >> 2;