1*5045Swnj /* ip_input.c 1.17 81/11/23 */ 24571Swnj 34495Swnj #include "../h/param.h" 44543Swnj #include "../h/systm.h" 54640Swnj #include "../h/clock.h" 64640Swnj #include "../h/mbuf.h" 74898Swnj #include "../h/protosw.h" 84923Swnj #include "../h/socket.h" 94801Swnj #include "../net/inet.h" 104801Swnj #include "../net/inet_systm.h" 114951Swnj #include "../net/if.h" 124801Swnj #include "../net/imp.h" 134801Swnj #include "../net/ip.h" /* belongs before inet.h */ 144898Swnj #include "../net/ip_var.h" 154801Swnj #include "../net/ip_icmp.h" 164801Swnj #include "../net/tcp.h" 174495Swnj 184898Swnj u_char ip_protox[IPPROTO_MAX]; 194898Swnj 204801Swnj /* 214801Swnj * Ip initialization. 224801Swnj */ 234801Swnj ip_init() 244801Swnj { 254898Swnj register struct protosw *pr; 264898Swnj register int i; 274495Swnj 284951Swnj COUNT(IP_INIT); 294898Swnj pr = pffindproto(PF_INET, IPPROTO_RAW); 304898Swnj if (pr == 0) 314898Swnj panic("ip_init"); 324898Swnj for (i = 0; i < IPPROTO_MAX; i++) 334898Swnj ip_protox[i] = pr - protosw; 344898Swnj for (pr = protosw; pr <= protoswLAST; pr++) 354898Swnj if (pr->pr_family == PF_INET && 364898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 374898Swnj ip_protox[pr->pr_protocol] = pr - protosw; 384801Swnj ipq.next = ipq.prev = &ipq; 394801Swnj ip_id = time & 0xffff; 404801Swnj } 414801Swnj 424898Swnj u_char ipcksum = 1; 434640Swnj struct ip *ip_reass(); 444640Swnj 454640Swnj /* 464640Swnj * Ip input routines. 474640Swnj */ 484640Swnj 494640Swnj /* 504640Swnj * Ip input routine. Checksum and byte swap header. If fragmented 514640Swnj * try to reassamble. If complete and fragment queue exists, discard. 524640Swnj * Process options. Pass to next level. 534640Swnj */ 544640Swnj ip_input(m0) 554640Swnj struct mbuf *m0; 564495Swnj { 574923Swnj register struct ip *ip; 584689Swnj register struct mbuf *m = m0; 594640Swnj register int i; 604495Swnj register struct ipq *fp; 614495Swnj int hlen; 624495Swnj 634495Swnj COUNT(IP_INPUT); 644640Swnj /* 654640Swnj * Check header and byteswap. 664640Swnj */ 67*5045Swnj printf("ip_input\n"); 684640Swnj ip = mtod(m, struct ip *); 694640Swnj if ((hlen = ip->ip_hl << 2) > m->m_len) { 704640Swnj printf("ip hdr ovflo\n"); 714951Swnj goto bad; 724495Swnj } 734951Swnj if (ipcksum) 74*5045Swnj if ((ip->ip_sum = inet_cksum(m, hlen)) != 0xffff) { 754951Swnj printf("ip_sum %x\n", ip->ip_sum); 764951Swnj ipstat.ips_badsum++; 774951Swnj goto bad; 784495Swnj } 794951Swnj 804951Swnj /* 814951Swnj * Convert fields to host representation. 824951Swnj */ 834907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 844640Swnj ip->ip_id = ntohs(ip->ip_id); 854951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 864495Swnj 874543Swnj /* 884640Swnj * Check that the amount of data in the buffers 894640Swnj * is as at least much as the IP header would have us expect. 904640Swnj * Trim mbufs if longer than we expect. 914640Swnj * Drop packet if shorter than we expect. 924543Swnj */ 93*5045Swnj printf("ip_input: %d:", ip->ip_len); 944640Swnj i = 0; 95*5045Swnj for (; m != NULL; m = m->m_next) { 964495Swnj i += m->m_len; 97*5045Swnj printf(" %d", m->m_len); 98*5045Swnj } 99*5045Swnj printf("\n"); 1004640Swnj m = m0; 1014640Swnj if (i != ip->ip_len) { 1024640Swnj if (i < ip->ip_len) { 1034640Swnj printf("ip_input: short packet\n"); 1044951Swnj goto bad; 1054640Swnj } 106*5045Swnj printf("m_adj %d\n", ip->ip_len - i); 1074640Swnj m_adj(m, ip->ip_len - i); 1084495Swnj } 1094495Swnj 1104640Swnj /* 1114640Swnj * Process options and, if not destined for us, 1124640Swnj * ship it on. 1134640Swnj */ 1144543Swnj if (hlen > sizeof (struct ip)) 1154907Swnj ip_dooptions(ip); 116*5045Swnj if (ip->ip_dst.s_addr != n_lhost.s_addr && 117*5045Swnj if_ifwithaddr(ip->ip_dst) == 0) { 1184640Swnj if (--ip->ip_ttl == 0) { 1194907Swnj icmp_error(ip, ICMP_TIMXCEED, 0); 1204543Swnj return; 1214495Swnj } 1224640Swnj ip_output(dtom(ip)); 1234640Swnj return; 1244543Swnj } 1254495Swnj 1264640Swnj /* 1274640Swnj * Look for queue of fragments 1284640Swnj * of this datagram. 1294640Swnj */ 1304640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1314640Swnj if (ip->ip_id == fp->ipq_id && 1324640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1334640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1344640Swnj ip->ip_p == fp->ipq_p) 1354640Swnj goto found; 1364640Swnj fp = 0; 1374640Swnj found: 1384495Swnj 1394640Swnj /* 1404640Swnj * Adjust ip_len to not reflect header, 1414640Swnj * set ip_mff if more fragments are expected, 1424640Swnj * convert offset of this to bytes. 1434640Swnj */ 1444640Swnj ip->ip_len -= hlen; 1454898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 1464640Swnj if (ip->ip_off & IP_MF) 1474898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 1484640Swnj ip->ip_off <<= 3; 1494495Swnj 1504640Swnj /* 1514640Swnj * If datagram marked as having more fragments 1524640Swnj * or if this is not the first fragment, 1534640Swnj * attempt reassembly; if it succeeds, proceed. 1544640Swnj */ 1554898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 1564898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 1574640Swnj if (ip == 0) 1584640Swnj return; 1594640Swnj hlen = ip->ip_hl << 2; 1604640Swnj m = dtom(ip); 1614640Swnj } else 1624640Swnj if (fp) 1634640Swnj (void) ip_freef(fp); 1644951Swnj 1654951Swnj /* 1664951Swnj * Switch out to protocol's input routine. 1674951Swnj */ 1684898Swnj (*protosw[ip_protox[ip->ip_p]].pr_input)(m); 1694951Swnj return; 1704951Swnj bad: 1714951Swnj m_freem(m); 1724640Swnj } 1734495Swnj 1744640Swnj /* 1754640Swnj * Take incoming datagram fragment and try to 1764951Swnj * reassemble it into whole datagram. If a chain for 1774640Swnj * reassembly of this datagram already exists, then it 1784640Swnj * is given as fp; otherwise have to make a chain. 1794640Swnj */ 1804640Swnj struct ip * 1814640Swnj ip_reass(ip, fp) 1824898Swnj register struct ipasfrag *ip; 1834640Swnj register struct ipq *fp; 1844640Swnj { 1854640Swnj register struct mbuf *m = dtom(ip); 1864898Swnj register struct ipasfrag *q; 1874640Swnj struct mbuf *t; 1884640Swnj int hlen = ip->ip_hl << 2; 1894640Swnj int i, next; 1904951Swnj COUNT(IP_REASS); 1914543Swnj 1924640Swnj /* 1934640Swnj * Presence of header sizes in mbufs 1944640Swnj * would confuse code below. 1954640Swnj */ 1964640Swnj m->m_off += hlen; 1974640Swnj m->m_len -= hlen; 1984495Swnj 1994640Swnj /* 2004640Swnj * If first fragment to arrive, create a reassembly queue. 2014640Swnj */ 2024640Swnj if (fp == 0) { 2034640Swnj if ((t = m_get(1)) == NULL) 2044640Swnj goto dropfrag; 2054640Swnj t->m_off = MMINOFF; 2064640Swnj fp = mtod(t, struct ipq *); 2074640Swnj insque(fp, &ipq); 2084640Swnj fp->ipq_ttl = IPFRAGTTL; 2094640Swnj fp->ipq_p = ip->ip_p; 2104640Swnj fp->ipq_id = ip->ip_id; 2114898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 2124898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 2134898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2144640Swnj } 2154495Swnj 2164640Swnj /* 2174640Swnj * Find a segment which begins after this one does. 2184640Swnj */ 2194898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2204640Swnj if (q->ip_off > ip->ip_off) 2214640Swnj break; 2224495Swnj 2234640Swnj /* 2244640Swnj * If there is a preceding segment, it may provide some of 2254640Swnj * our data already. If so, drop the data from the incoming 2264640Swnj * segment. If it provides all of our data, drop us. 2274640Swnj */ 2284898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 2294898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2304640Swnj if (i > 0) { 2314640Swnj if (i >= ip->ip_len) 2324640Swnj goto dropfrag; 2334640Swnj m_adj(dtom(ip), i); 2344640Swnj ip->ip_off += i; 2354640Swnj ip->ip_len -= i; 2364640Swnj } 2374640Swnj } 2384543Swnj 2394640Swnj /* 2404640Swnj * While we overlap succeeding segments trim them or, 2414640Swnj * if they are completely covered, dequeue them. 2424640Swnj */ 2434898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 2444640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 2454640Swnj if (i < q->ip_len) { 2464640Swnj q->ip_len -= i; 2474640Swnj m_adj(dtom(q), i); 2484640Swnj break; 2494495Swnj } 2504898Swnj q = q->ipf_next; 2514898Swnj m_freem(dtom(q->ipf_prev)); 2524898Swnj ip_deq(q->ipf_prev); 2534543Swnj } 2544495Swnj 2554640Swnj /* 2564640Swnj * Stick new segment in its place; 2574640Swnj * check for complete reassembly. 2584640Swnj */ 2594898Swnj ip_enq(ip, q->ipf_prev); 2604640Swnj next = 0; 2614898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 2624640Swnj if (q->ip_off != next) 2634640Swnj return (0); 2644640Swnj next += q->ip_len; 2654640Swnj } 2664898Swnj if (q->ipf_prev->ipf_mff) 2674640Swnj return (0); 2684495Swnj 2694640Swnj /* 2704640Swnj * Reassembly is complete; concatenate fragments. 2714640Swnj */ 2724640Swnj q = fp->ipq_next; 2734640Swnj m = dtom(q); 2744640Swnj t = m->m_next; 2754640Swnj m->m_next = 0; 2764640Swnj m_cat(m, t); 2774898Swnj while ((q = q->ipf_next) != (struct ipasfrag *)fp) 2784640Swnj m_cat(m, dtom(q)); 2794495Swnj 2804640Swnj /* 2814640Swnj * Create header for new ip packet by 2824640Swnj * modifying header of first packet; 2834640Swnj * dequeue and discard fragment reassembly header. 2844640Swnj * Make header visible. 2854640Swnj */ 2864640Swnj ip = fp->ipq_next; 2874640Swnj ip->ip_len = next; 2884898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 2894898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 2904640Swnj remque(fp); 2914907Swnj (void) m_free(dtom(fp)); 2924640Swnj m = dtom(ip); 2934898Swnj m->m_len += sizeof (struct ipasfrag); 2944898Swnj m->m_off -= sizeof (struct ipasfrag); 2954898Swnj return ((struct ip *)ip); 2964495Swnj 2974640Swnj dropfrag: 2984640Swnj m_freem(m); 2994640Swnj return (0); 3004495Swnj } 3014495Swnj 3024640Swnj /* 3034640Swnj * Free a fragment reassembly header and all 3044640Swnj * associated datagrams. 3054640Swnj */ 3064640Swnj struct ipq * 3074640Swnj ip_freef(fp) 3084640Swnj struct ipq *fp; 3094495Swnj { 3104898Swnj register struct ipasfrag *q; 3114640Swnj struct mbuf *m; 3124951Swnj COUNT(IP_FREEF); 3134495Swnj 3144898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3154640Swnj m_freem(dtom(q)); 3164640Swnj m = dtom(fp); 3174640Swnj fp = fp->next; 3184640Swnj remque(fp->prev); 3194907Swnj (void) m_free(m); 3204640Swnj return (fp); 3214495Swnj } 3224495Swnj 3234640Swnj /* 3244640Swnj * Put an ip fragment on a reassembly chain. 3254640Swnj * Like insque, but pointers in middle of structure. 3264640Swnj */ 3274640Swnj ip_enq(p, prev) 3284898Swnj register struct ipasfrag *p, *prev; 3294495Swnj { 3304951Swnj 3314640Swnj COUNT(IP_ENQ); 3324898Swnj p->ipf_prev = prev; 3334898Swnj p->ipf_next = prev->ipf_next; 3344898Swnj prev->ipf_next->ipf_prev = p; 3354898Swnj prev->ipf_next = p; 3364495Swnj } 3374495Swnj 3384640Swnj /* 3394640Swnj * To ip_enq as remque is to insque. 3404640Swnj */ 3414640Swnj ip_deq(p) 3424898Swnj register struct ipasfrag *p; 3434640Swnj { 3444951Swnj 3454640Swnj COUNT(IP_DEQ); 3464898Swnj p->ipf_prev->ipf_next = p->ipf_next; 3474898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3484495Swnj } 3494495Swnj 3504640Swnj /* 3514640Swnj * IP timer processing; 3524640Swnj * if a timer expires on a reassembly 3534640Swnj * queue, discard it. 3544640Swnj */ 3554801Swnj ip_slowtimo() 3564495Swnj { 3574495Swnj register struct ipq *fp; 3584640Swnj int s = splnet(); 3594951Swnj 3604801Swnj COUNT(IP_SLOWTIMO); 3614644Swnj for (fp = ipq.next; fp != &ipq; ) 3624640Swnj if (--fp->ipq_ttl == 0) 3634640Swnj fp = ip_freef(fp); 3644640Swnj else 3654640Swnj fp = fp->next; 3664640Swnj splx(s); 3674495Swnj } 3684495Swnj 3694951Swnj /* 3704951Swnj * Drain off all datagram fragments. 3714951Swnj */ 3724801Swnj ip_drain() 3734801Swnj { 3744801Swnj 3754951Swnj COUNT(IP_DRAIN); 3764951Swnj while (ipq.next != &ipq) 3774951Swnj (void) ip_freef(ipq.next); 3784801Swnj } 3794923Swnj 3804640Swnj /* 3814640Swnj * Do option processing on a datagram, 3824640Swnj * possibly discarding it if bad options 3834640Swnj * are encountered. 3844640Swnj */ 3854640Swnj ip_dooptions(ip) 3864640Swnj struct ip *ip; 3874495Swnj { 3884640Swnj register u_char *cp; 3894907Swnj int opt, optlen, cnt; 3904923Swnj struct in_addr *sin; 3914801Swnj register struct ip_timestamp *ipt; 3924951Swnj register struct ifnet *ifp; 3934951Swnj struct in_addr t; 3944495Swnj 3954951Swnj COUNT(IP_DOOPTIONS); 3964640Swnj cp = (u_char *)(ip + 1); 3974640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 3984640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 3994640Swnj opt = cp[0]; 4004640Swnj if (opt == IPOPT_EOL) 4014640Swnj break; 4024640Swnj if (opt == IPOPT_NOP) 4034640Swnj optlen = 1; 4044640Swnj else 4054640Swnj optlen = cp[1]; 4064640Swnj switch (opt) { 4074495Swnj 4084640Swnj default: 4094640Swnj break; 4104495Swnj 4114951Swnj /* 4124951Swnj * Source routing with record. 4134951Swnj * Find interface with current destination address. 4144951Swnj * If none on this machine then drop if strictly routed, 4154951Swnj * or do nothing if loosely routed. 4164951Swnj * Record interface address and bring up next address 4174951Swnj * component. If strictly routed make sure next 4184951Swnj * address on directly accessible net. 4194951Swnj */ 4204640Swnj case IPOPT_LSRR: 4214801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 4224640Swnj break; 4234923Swnj sin = (struct in_addr *)(cp + cp[2]); 4244951Swnj ifp = if_ifwithaddr(*sin); 4254951Swnj if (ifp == 0) { 4264951Swnj if (opt == IPOPT_SSRR) 4274951Swnj goto bad; 4284951Swnj break; 4294640Swnj } 4304951Swnj t = ip->ip_dst; ip->ip_dst = *sin; *sin = t; 4314951Swnj cp[2] += 4; 4324951Swnj if (cp[2] > optlen - (sizeof (long) - 1)) 4334951Swnj break; 4344951Swnj ip->ip_dst = sin[1]; 4354951Swnj if (opt == IPOPT_SSRR && if_ifonnetof(ip->ip_dst)==0) 4364951Swnj goto bad; 4374640Swnj break; 4384495Swnj 4394640Swnj case IPOPT_TS: 4404801Swnj ipt = (struct ip_timestamp *)cp; 4414801Swnj if (ipt->ipt_len < 5) 4424640Swnj goto bad; 4434801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 4444801Swnj if (++ipt->ipt_oflw == 0) 4454640Swnj goto bad; 4464495Swnj break; 4474640Swnj } 4484923Swnj sin = (struct in_addr *)(cp+cp[2]); 4494801Swnj switch (ipt->ipt_flg) { 4504495Swnj 4514640Swnj case IPOPT_TS_TSONLY: 4524640Swnj break; 4534640Swnj 4544640Swnj case IPOPT_TS_TSANDADDR: 4554801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4564640Swnj goto bad; 4574951Swnj /* stamp with ``first'' interface address */ 4584951Swnj *sin++ = ifnet->if_addr; 4594640Swnj break; 4604640Swnj 4614640Swnj case IPOPT_TS_PRESPEC: 4624951Swnj if (if_ifwithaddr(*sin) == 0) 4634951Swnj continue; 4644801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4654640Swnj goto bad; 4664801Swnj ipt->ipt_ptr += 4; 4674640Swnj break; 4684640Swnj 4694495Swnj default: 4704640Swnj goto bad; 4714495Swnj } 4724923Swnj *(n_time *)sin = iptime(); 4734801Swnj ipt->ipt_ptr += 4; 4744640Swnj } 4754495Swnj } 4764907Swnj return; 4774640Swnj bad: 4784640Swnj /* SHOULD FORCE ICMP MESSAGE */ 4794907Swnj return; 4804495Swnj } 4814495Swnj 4824640Swnj /* 4834951Swnj * Strip out IP options, at higher 4844951Swnj * level protocol in the kernel. 4854951Swnj * Second argument is buffer to which options 4864951Swnj * will be moved, and return value is their length. 4874640Swnj */ 4884951Swnj ip_stripoptions(ip, cp) 4894640Swnj struct ip *ip; 4904951Swnj char *cp; 4914495Swnj { 4924640Swnj register int i; 4934640Swnj register struct mbuf *m; 4944640Swnj int olen; 4954951Swnj COUNT(IP_STRIPOPTIONS); 4964640Swnj 4974640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 4984951Swnj m = dtom(ip); 4994951Swnj ip++; 5004951Swnj if (cp) 5014951Swnj bcopy((caddr_t)ip, cp, (unsigned)olen); 5024640Swnj i = m->m_len - (sizeof (struct ip) + olen); 5034907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 5044640Swnj m->m_len -= i; 5054495Swnj } 506