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. 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
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.
57 * IP initialization: fill in IP protocol switch table.
58 * All protocols not implemented in kernel go to raw IP protocol handler.
63 ipq.next = ipq.prev = (ipqp_32)&ipq;
64 ip_id = tt.tv_sec & 0xffff;
71 * Ip input routine. Checksum and byte swap header. If fragmented
72 * try to reassemble. Process options. Pass to next level.
78 register struct ip *ip;
81 DEBUG_CALL("ip_input");
82 DEBUG_ARG("m = %lx", (long)m);
83 DEBUG_ARG("m_len = %d", m->m_len);
85 STAT(ipstat.ips_total++);
87 if (m->m_len < sizeof (struct ip)) {
88 STAT(ipstat.ips_toosmall++);
92 ip = mtod(m, struct ip *);
94 if (ip->ip_v != IPVERSION) {
95 STAT(ipstat.ips_badvers++);
99 hlen = ip->ip_hl << 2;
100 if (hlen<sizeof(struct ip ) || hlen>m->m_len) {/* min header length */
101 STAT(ipstat.ips_badhlen++); /* or packet too short */
105 /* keep ip header intact for ICMP reply
106 * ip->ip_sum = cksum(m, hlen);
110 STAT(ipstat.ips_badsum++);
115 * Convert fields to host representation.
118 if (ip->ip_len < hlen) {
119 STAT(ipstat.ips_badlen++);
126 * Check that the amount of data in the buffers
127 * is as at least much as the IP header would have us expect.
128 * Trim mbufs if longer than we expect.
129 * Drop packet if shorter than we expect.
131 if (m->m_len < ip->ip_len) {
132 STAT(ipstat.ips_tooshort++);
135 /* Should drop packet if mbuf too long? hmmm... */
136 if (m->m_len > ip->ip_len)
137 m_adj(m, ip->ip_len - m->m_len);
139 /* check ip_ttl for a correct ICMP reply */
140 if(ip->ip_ttl==0 || ip->ip_ttl==1) {
141 icmp_error(m, ICMP_TIMXCEED,ICMP_TIMXCEED_INTRANS, 0,"ttl");
146 * Process options and, if not destined for us,
147 * ship it on. ip_dooptions returns 1 when an
148 * error was detected (causing an icmp message
149 * to be sent and the original packet to be freed).
151 /* We do no IP options */
152 /* if (hlen > sizeof (struct ip) && ip_dooptions(m))
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;
167 * Look for queue of fragments
170 for (fp = (struct ipq *) ipq.next; fp != &ipq;
171 fp = (struct ipq *) fp->next)
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)
181 * Adjust ip_len to not reflect header,
182 * set ip_mff if more fragments are expected,
183 * convert offset of this to bytes.
186 if (ip->ip_off & IP_MF)
187 ((struct ipasfrag *)ip)->ipf_mff |= 1;
189 ((struct ipasfrag *)ip)->ipf_mff &= ~1;
194 * If datagram marked as having more fragments
195 * or if this is not the first fragment,
196 * attempt reassembly; if it succeeds, proceed.
198 if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) {
199 STAT(ipstat.ips_fragments++);
200 ip = ip_reass((struct ipasfrag *)ip, fp);
203 STAT(ipstat.ips_reassembled++);
213 * Switch out to protocol's input routine.
215 STAT(ipstat.ips_delivered++);
218 tcp_input(m, hlen, (struct socket *)NULL);
227 STAT(ipstat.ips_noproto++);
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.
244 register struct ipasfrag *ip;
245 register struct ipq *fp;
247 register struct mbuf *m = dtom(ip);
248 register struct ipasfrag *q;
249 int hlen = ip->ip_hl << 2;
252 DEBUG_CALL("ip_reass");
253 DEBUG_ARG("ip = %lx", (long)ip);
254 DEBUG_ARG("fp = %lx", (long)fp);
255 DEBUG_ARG("m = %lx", (long)m);
258 * Presence of header sizes in mbufs
259 * would confuse code below.
260 * Fragment m_data is concatenated.
266 * If first fragment to arrive, create a reassembly queue.
270 if ((t = m_get()) == NULL) goto dropfrag;
271 fp = mtod(t, struct ipq *);
273 fp->ipq_ttl = IPFRAGTTL;
274 fp->ipq_p = ip->ip_p;
275 fp->ipq_id = ip->ip_id;
276 fp->ipq_next = fp->ipq_prev = (ipasfragp_32)fp;
277 fp->ipq_src = ((struct ip *)ip)->ip_src;
278 fp->ipq_dst = ((struct ip *)ip)->ip_dst;
279 q = (struct ipasfrag *)fp;
284 * Find a segment which begins after this one does.
286 for (q = (struct ipasfrag *)fp->ipq_next; q != (struct ipasfrag *)fp;
287 q = (struct ipasfrag *)q->ipf_next)
288 if (q->ip_off > ip->ip_off)
292 * If there is a preceding segment, it may provide some of
293 * our data already. If so, drop the data from the incoming
294 * segment. If it provides all of our data, drop us.
296 if (q->ipf_prev != (ipasfragp_32)fp) {
297 i = ((struct ipasfrag *)(q->ipf_prev))->ip_off +
298 ((struct ipasfrag *)(q->ipf_prev))->ip_len - ip->ip_off;
309 * While we overlap succeeding segments trim them or,
310 * if they are completely covered, dequeue them.
312 while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
313 i = (ip->ip_off + ip->ip_len) - q->ip_off;
320 q = (struct ipasfrag *) q->ipf_next;
321 m_freem(dtom((struct ipasfrag *) q->ipf_prev));
322 ip_deq((struct ipasfrag *) q->ipf_prev);
327 * Stick new segment in its place;
328 * check for complete reassembly.
330 ip_enq(ip, (struct ipasfrag *) q->ipf_prev);
332 for (q = (struct ipasfrag *) fp->ipq_next; q != (struct ipasfrag *)fp;
333 q = (struct ipasfrag *) q->ipf_next) {
334 if (q->ip_off != next)
338 if (((struct ipasfrag *)(q->ipf_prev))->ipf_mff & 1)
342 * Reassembly is complete; concatenate fragments.
344 q = (struct ipasfrag *) fp->ipq_next;
347 q = (struct ipasfrag *) q->ipf_next;
348 while (q != (struct ipasfrag *)fp) {
351 q = (struct ipasfrag *) q->ipf_next;
356 * Create header for new ip packet by
357 * modifying header of first packet;
358 * dequeue and discard fragment reassembly header.
359 * Make header visible.
361 ip = (struct ipasfrag *) fp->ipq_next;
364 * If the fragments concatenated to an mbuf that's
365 * bigger than the total size of the fragment, then and
366 * m_ext buffer was alloced. But fp->ipq_next points to
367 * the old buffer (in the mbuf), so we must point ip
368 * into the new buffer.
370 if (m->m_flags & M_EXT) {
372 delta = (char *)ip - m->m_dat;
373 ip = (struct ipasfrag *)(m->m_ext + delta);
376 /* DEBUG_ARG("ip = %lx", (long)ip);
377 * ip=(struct ipasfrag *)m->m_data; */
381 ((struct ip *)ip)->ip_src = fp->ipq_src;
382 ((struct ip *)ip)->ip_dst = fp->ipq_dst;
384 (void) m_free(dtom(fp));
386 m->m_len += (ip->ip_hl << 2);
387 m->m_data -= (ip->ip_hl << 2);
389 return ((struct ip *)ip);
392 STAT(ipstat.ips_fragdropped++);
398 * Free a fragment reassembly header and all
399 * associated datagrams.
405 register struct ipasfrag *q, *p;
407 for (q = (struct ipasfrag *) fp->ipq_next; q != (struct ipasfrag *)fp;
409 p = (struct ipasfrag *) q->ipf_next;
414 (void) m_free(dtom(fp));
418 * Put an ip fragment on a reassembly chain.
419 * Like insque, but pointers in middle of structure.
423 register struct ipasfrag *p, *prev;
425 DEBUG_CALL("ip_enq");
426 DEBUG_ARG("prev = %lx", (long)prev);
427 p->ipf_prev = (ipasfragp_32) prev;
428 p->ipf_next = prev->ipf_next;
429 ((struct ipasfrag *)(prev->ipf_next))->ipf_prev = (ipasfragp_32) p;
430 prev->ipf_next = (ipasfragp_32) p;
434 * To ip_enq as remque is to insque.
438 register struct ipasfrag *p;
440 ((struct ipasfrag *)(p->ipf_prev))->ipf_next = p->ipf_next;
441 ((struct ipasfrag *)(p->ipf_next))->ipf_prev = p->ipf_prev;
445 * IP timer processing;
446 * if a timer expires on a reassembly
452 register struct ipq *fp;
454 DEBUG_CALL("ip_slowtimo");
456 fp = (struct ipq *) ipq.next;
462 fp = (struct ipq *) fp->next;
463 if (((struct ipq *)(fp->prev))->ipq_ttl == 0) {
464 STAT(ipstat.ips_fragtimeout++);
465 ip_freef((struct ipq *) fp->prev);
471 * Do option processing on a datagram,
472 * possibly discarding it if bad options are encountered,
473 * or forwarding it if source-routed.
474 * Returns 1 if packet has been forwarded/freed,
475 * 0 if the packet should be processed further.
484 register struct ip *ip = mtod(m, struct ip *);
486 register struct ip_timestamp *ipt;
487 register struct in_ifaddr *ia;
488 /* int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; */
489 int opt, optlen, cnt, off, code, type, forward = 0;
490 struct in_addr *sin, dst;
491 typedef u_int32_t n_time;
495 cp = (u_char *)(ip + 1);
496 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
497 for (; cnt > 0; cnt -= optlen, cp += optlen) {
498 opt = cp[IPOPT_OPTVAL];
499 if (opt == IPOPT_EOL)
501 if (opt == IPOPT_NOP)
504 optlen = cp[IPOPT_OLEN];
505 if (optlen <= 0 || optlen > cnt) {
506 code = &cp[IPOPT_OLEN] - (u_char *)ip;
516 * Source routing with record.
517 * Find interface with current destination address.
518 * If none on this machine then drop if strictly routed,
519 * or do nothing if loosely routed.
520 * Record interface address and bring up next address
521 * component. If strictly routed make sure next
522 * address is on directly accessible net.
526 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
527 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
530 ipaddr.sin_addr = ip->ip_dst;
531 ia = (struct in_ifaddr *)
532 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
534 if (opt == IPOPT_SSRR) {
536 code = ICMP_UNREACH_SRCFAIL;
540 * Loose routing, and not at next destination
541 * yet; nothing to do except forward.
545 off--; / * 0 origin * /
546 if (off > optlen - sizeof(struct in_addr)) {
548 * End of source route. Should be for us.
550 save_rte(cp, ip->ip_src);
554 * locate outgoing interface
556 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
557 sizeof(ipaddr.sin_addr));
558 if (opt == IPOPT_SSRR) {
559 #define INA struct in_ifaddr *
560 #define SA struct sockaddr *
561 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
562 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
564 ia = ip_rtaddr(ipaddr.sin_addr);
567 code = ICMP_UNREACH_SRCFAIL;
570 ip->ip_dst = ipaddr.sin_addr;
571 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
572 (caddr_t)(cp + off), sizeof(struct in_addr));
573 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
575 * Let ip_intr's mcast routing check handle mcast pkts
577 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
581 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
582 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
586 * If no space remains, ignore.
589 if (off > optlen - sizeof(struct in_addr))
591 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
592 sizeof(ipaddr.sin_addr));
594 * locate outgoing interface; if we're the destination,
595 * use the incoming interface (should be same).
597 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
598 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
600 code = ICMP_UNREACH_HOST;
603 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
604 (caddr_t)(cp + off), sizeof(struct in_addr));
605 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
609 code = cp - (u_char *)ip;
610 ipt = (struct ip_timestamp *)cp;
611 if (ipt->ipt_len < 5)
613 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
614 if (++ipt->ipt_oflw == 0)
618 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
619 switch (ipt->ipt_flg) {
621 case IPOPT_TS_TSONLY:
624 case IPOPT_TS_TSANDADDR:
625 if (ipt->ipt_ptr + sizeof(n_time) +
626 sizeof(struct in_addr) > ipt->ipt_len)
628 ipaddr.sin_addr = dst;
629 ia = (INA)ifaof_ i f p foraddr((SA)&ipaddr,
633 bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
634 (caddr_t)sin, sizeof(struct in_addr));
635 ipt->ipt_ptr += sizeof(struct in_addr);
638 case IPOPT_TS_PRESPEC:
639 if (ipt->ipt_ptr + sizeof(n_time) +
640 sizeof(struct in_addr) > ipt->ipt_len)
642 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
643 sizeof(struct in_addr));
644 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
646 ipt->ipt_ptr += sizeof(struct in_addr);
653 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
655 ipt->ipt_ptr += sizeof(n_time);
666 /* ip->ip_len -= ip->ip_hl << 2; XXX icmp_error adds in hdr length */
669 icmp_error(m, type, code, 0, 0);
671 STAT(ipstat.ips_badoptions++);
678 * Strip out IP options, at higher
679 * level protocol in the kernel.
680 * Second argument is buffer to which options
681 * will be moved, and return value is their length.
682 * (XXX) should be deleted; last arg currently ignored.
685 ip_stripoptions(m, mopt)
686 register struct mbuf *m;
690 struct ip *ip = mtod(m, struct ip *);
691 register caddr_t opts;
694 olen = (ip->ip_hl<<2) - sizeof (struct ip);
695 opts = (caddr_t)(ip + 1);
696 i = m->m_len - (sizeof (struct ip) + olen);
697 memcpy(opts, opts + olen, (unsigned)i);
700 ip->ip_hl = sizeof(struct ip) >> 2;