1*5851Sroot /* ip_input.c 1.30 82/02/15 */ 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" 95084Swnj #include "../net/in.h" 105084Swnj #include "../net/in_systm.h" 114951Swnj #include "../net/if.h" 125084Swnj #include "../net/ip.h" /* belongs before in.h */ 134898Swnj #include "../net/ip_var.h" 144801Swnj #include "../net/ip_icmp.h" 154801Swnj #include "../net/tcp.h" 164495Swnj 174898Swnj u_char ip_protox[IPPROTO_MAX]; 184898Swnj 194801Swnj /* 205172Swnj * IP initialization: fill in IP protocol switch table. 215161Swnj * All protocols not implemented in kernel go to raw IP protocol handler. 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 routine. Checksum and byte swap header. If fragmented 474640Swnj * try to reassamble. If complete and fragment queue exists, discard. 484640Swnj * Process options. Pass to next level. 494640Swnj */ 505084Swnj ipintr() 514495Swnj { 524923Swnj register struct ip *ip; 535084Swnj register struct mbuf *m; 545217Swnj struct mbuf *m0, *mopt; 554640Swnj register int i; 564495Swnj register struct ipq *fp; 575084Swnj int hlen, s; 584495Swnj 595084Swnj COUNT(IPINTR); 605084Swnj next: 614640Swnj /* 625084Swnj * Get next datagram off input queue and get IP header 635084Swnj * in first mbuf. 644640Swnj */ 655084Swnj s = splimp(); 665084Swnj IF_DEQUEUE(&ipintrq, m); 675084Swnj splx(s); 685218Swnj if (m == 0) 695084Swnj return; 705306Sroot if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) && 715306Sroot (m = m_pullup(m, sizeof (struct ip))) == 0) 725306Sroot return; 734640Swnj ip = mtod(m, struct ip *); 745161Swnj if ((hlen = ip->ip_hl << 2) > m->m_len) { 755306Sroot if ((m = m_pullup(m, hlen)) == 0) 765306Sroot return; 775161Swnj ip = mtod(m, struct ip *); 785161Swnj } 794951Swnj if (ipcksum) 805217Swnj if (ip->ip_sum = in_cksum(m, hlen)) { 815161Swnj printf("ip_sum %x\n", ip->ip_sum); /* XXX */ 824951Swnj ipstat.ips_badsum++; 834951Swnj goto bad; 844495Swnj } 854951Swnj 865217Swnj #if vax 874951Swnj /* 884951Swnj * Convert fields to host representation. 894951Swnj */ 904907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 914640Swnj ip->ip_id = ntohs(ip->ip_id); 924951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 935217Swnj #endif 944495Swnj 954543Swnj /* 964640Swnj * Check that the amount of data in the buffers 974640Swnj * is as at least much as the IP header would have us expect. 984640Swnj * Trim mbufs if longer than we expect. 994640Swnj * Drop packet if shorter than we expect. 1004543Swnj */ 1014640Swnj i = 0; 1025161Swnj m0 = m; 1035161Swnj for (; m != NULL; m = m->m_next) 1044495Swnj i += m->m_len; 1054640Swnj m = m0; 1064640Swnj if (i != ip->ip_len) { 1075161Swnj if (i < ip->ip_len) { 1085161Swnj ipstat.ips_tooshort++; 1094951Swnj goto bad; 1105161Swnj } 1114640Swnj m_adj(m, ip->ip_len - i); 1124495Swnj } 1134495Swnj 1144640Swnj /* 1154640Swnj * Process options and, if not destined for us, 1164640Swnj * ship it on. 1174640Swnj */ 1184543Swnj if (hlen > sizeof (struct ip)) 1194907Swnj ip_dooptions(ip); 1205084Swnj if (ifnet && ip->ip_dst.s_addr != ifnet->if_addr.s_addr && 1215045Swnj if_ifwithaddr(ip->ip_dst) == 0) { 1225298Sroot printf("ip->ip_dst %x ip->ip_ttl %x\n", ip->ip_dst, ip->ip_ttl); 1234640Swnj if (--ip->ip_ttl == 0) { 1244907Swnj icmp_error(ip, ICMP_TIMXCEED, 0); 1255084Swnj goto next; 1264495Swnj } 1275217Swnj mopt = m_get(M_DONTWAIT); 1285217Swnj if (mopt == 0) 1295217Swnj goto bad; 1305217Swnj ip_stripoptions(ip, mopt); 1315217Swnj (void) ip_output(m0, mopt); 1325084Swnj goto next; 1334543Swnj } 1344495Swnj 1354640Swnj /* 1364640Swnj * Look for queue of fragments 1374640Swnj * of this datagram. 1384640Swnj */ 1394640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1404640Swnj if (ip->ip_id == fp->ipq_id && 1414640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1424640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1434640Swnj ip->ip_p == fp->ipq_p) 1444640Swnj goto found; 1454640Swnj fp = 0; 1464640Swnj found: 1474495Swnj 1484640Swnj /* 1494640Swnj * Adjust ip_len to not reflect header, 1504640Swnj * set ip_mff if more fragments are expected, 1514640Swnj * convert offset of this to bytes. 1524640Swnj */ 1534640Swnj ip->ip_len -= hlen; 1544898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 1554640Swnj if (ip->ip_off & IP_MF) 1564898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 1574640Swnj ip->ip_off <<= 3; 1584495Swnj 1594640Swnj /* 1604640Swnj * If datagram marked as having more fragments 1614640Swnj * or if this is not the first fragment, 1624640Swnj * attempt reassembly; if it succeeds, proceed. 1634640Swnj */ 1644898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 1654898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 1664640Swnj if (ip == 0) 1675084Swnj goto next; 1684640Swnj hlen = ip->ip_hl << 2; 1694640Swnj m = dtom(ip); 1704640Swnj } else 1714640Swnj if (fp) 1724640Swnj (void) ip_freef(fp); 1734951Swnj 1744951Swnj /* 1754951Swnj * Switch out to protocol's input routine. 1764951Swnj */ 1774898Swnj (*protosw[ip_protox[ip->ip_p]].pr_input)(m); 1785084Swnj goto next; 1794951Swnj bad: 1804951Swnj m_freem(m); 1815084Swnj goto next; 1824640Swnj } 1834495Swnj 1844640Swnj /* 1854640Swnj * Take incoming datagram fragment and try to 1864951Swnj * reassemble it into whole datagram. If a chain for 1874640Swnj * reassembly of this datagram already exists, then it 1884640Swnj * is given as fp; otherwise have to make a chain. 1894640Swnj */ 1904640Swnj struct ip * 1914640Swnj ip_reass(ip, fp) 1924898Swnj register struct ipasfrag *ip; 1934640Swnj register struct ipq *fp; 1944640Swnj { 1954640Swnj register struct mbuf *m = dtom(ip); 1964898Swnj register struct ipasfrag *q; 1974640Swnj struct mbuf *t; 1984640Swnj int hlen = ip->ip_hl << 2; 1994640Swnj int i, next; 2004951Swnj COUNT(IP_REASS); 2014543Swnj 2024640Swnj /* 2034640Swnj * Presence of header sizes in mbufs 2044640Swnj * would confuse code below. 2054640Swnj */ 2064640Swnj m->m_off += hlen; 2074640Swnj m->m_len -= hlen; 2084495Swnj 2094640Swnj /* 2104640Swnj * If first fragment to arrive, create a reassembly queue. 2114640Swnj */ 2124640Swnj if (fp == 0) { 213*5851Sroot if ((t = m_get(M_WAIT)) == NULL) 2144640Swnj goto dropfrag; 2154640Swnj t->m_off = MMINOFF; 2164640Swnj fp = mtod(t, struct ipq *); 2174640Swnj insque(fp, &ipq); 2184640Swnj fp->ipq_ttl = IPFRAGTTL; 2194640Swnj fp->ipq_p = ip->ip_p; 2204640Swnj fp->ipq_id = ip->ip_id; 2214898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 2224898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 2234898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2245161Swnj q = (struct ipasfrag *)fp; 2255161Swnj goto insert; 2264640Swnj } 2274495Swnj 2284640Swnj /* 2294640Swnj * Find a segment which begins after this one does. 2304640Swnj */ 2314898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2324640Swnj if (q->ip_off > ip->ip_off) 2334640Swnj break; 2344495Swnj 2354640Swnj /* 2364640Swnj * If there is a preceding segment, it may provide some of 2374640Swnj * our data already. If so, drop the data from the incoming 2384640Swnj * segment. If it provides all of our data, drop us. 2394640Swnj */ 2404898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 2414898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2424640Swnj if (i > 0) { 2434640Swnj if (i >= ip->ip_len) 2444640Swnj goto dropfrag; 2454640Swnj m_adj(dtom(ip), i); 2464640Swnj ip->ip_off += i; 2474640Swnj ip->ip_len -= i; 2484640Swnj } 2494640Swnj } 2504543Swnj 2514640Swnj /* 2524640Swnj * While we overlap succeeding segments trim them or, 2534640Swnj * if they are completely covered, dequeue them. 2544640Swnj */ 2554898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 2564640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 2574640Swnj if (i < q->ip_len) { 2584640Swnj q->ip_len -= i; 2594640Swnj m_adj(dtom(q), i); 2604640Swnj break; 2614495Swnj } 2624898Swnj q = q->ipf_next; 2634898Swnj m_freem(dtom(q->ipf_prev)); 2644898Swnj ip_deq(q->ipf_prev); 2654543Swnj } 2664495Swnj 2675161Swnj insert: 2684640Swnj /* 2694640Swnj * Stick new segment in its place; 2704640Swnj * check for complete reassembly. 2714640Swnj */ 2724898Swnj ip_enq(ip, q->ipf_prev); 2734640Swnj next = 0; 2744898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 2754640Swnj if (q->ip_off != next) 2764640Swnj return (0); 2774640Swnj next += q->ip_len; 2784640Swnj } 2794898Swnj if (q->ipf_prev->ipf_mff) 2804640Swnj return (0); 2814495Swnj 2824640Swnj /* 2834640Swnj * Reassembly is complete; concatenate fragments. 2844640Swnj */ 2854640Swnj q = fp->ipq_next; 2864640Swnj m = dtom(q); 2874640Swnj t = m->m_next; 2884640Swnj m->m_next = 0; 2894640Swnj m_cat(m, t); 2904898Swnj while ((q = q->ipf_next) != (struct ipasfrag *)fp) 2914640Swnj m_cat(m, dtom(q)); 2924495Swnj 2934640Swnj /* 2944640Swnj * Create header for new ip packet by 2954640Swnj * modifying header of first packet; 2964640Swnj * dequeue and discard fragment reassembly header. 2974640Swnj * Make header visible. 2984640Swnj */ 2994640Swnj ip = fp->ipq_next; 3004640Swnj ip->ip_len = next; 3014898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 3024898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 3034640Swnj remque(fp); 3044907Swnj (void) m_free(dtom(fp)); 3054640Swnj m = dtom(ip); 3064898Swnj m->m_len += sizeof (struct ipasfrag); 3074898Swnj m->m_off -= sizeof (struct ipasfrag); 3084898Swnj return ((struct ip *)ip); 3094495Swnj 3104640Swnj dropfrag: 3114640Swnj m_freem(m); 3124640Swnj return (0); 3134495Swnj } 3144495Swnj 3154640Swnj /* 3164640Swnj * Free a fragment reassembly header and all 3174640Swnj * associated datagrams. 3184640Swnj */ 3194640Swnj struct ipq * 3204640Swnj ip_freef(fp) 3214640Swnj struct ipq *fp; 3224495Swnj { 3234898Swnj register struct ipasfrag *q; 3244640Swnj struct mbuf *m; 3254951Swnj COUNT(IP_FREEF); 3264495Swnj 3274898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3284640Swnj m_freem(dtom(q)); 3294640Swnj m = dtom(fp); 3304640Swnj fp = fp->next; 3314640Swnj remque(fp->prev); 3324907Swnj (void) m_free(m); 3334640Swnj return (fp); 3344495Swnj } 3354495Swnj 3364640Swnj /* 3374640Swnj * Put an ip fragment on a reassembly chain. 3384640Swnj * Like insque, but pointers in middle of structure. 3394640Swnj */ 3404640Swnj ip_enq(p, prev) 3414898Swnj register struct ipasfrag *p, *prev; 3424495Swnj { 3434951Swnj 3444640Swnj COUNT(IP_ENQ); 3454898Swnj p->ipf_prev = prev; 3464898Swnj p->ipf_next = prev->ipf_next; 3474898Swnj prev->ipf_next->ipf_prev = p; 3484898Swnj prev->ipf_next = p; 3494495Swnj } 3504495Swnj 3514640Swnj /* 3524640Swnj * To ip_enq as remque is to insque. 3534640Swnj */ 3544640Swnj ip_deq(p) 3554898Swnj register struct ipasfrag *p; 3564640Swnj { 3574951Swnj 3584640Swnj COUNT(IP_DEQ); 3594898Swnj p->ipf_prev->ipf_next = p->ipf_next; 3604898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3614495Swnj } 3624495Swnj 3634640Swnj /* 3644640Swnj * IP timer processing; 3654640Swnj * if a timer expires on a reassembly 3664640Swnj * queue, discard it. 3674640Swnj */ 3684801Swnj ip_slowtimo() 3694495Swnj { 3704495Swnj register struct ipq *fp; 3714640Swnj int s = splnet(); 3724951Swnj 3734801Swnj COUNT(IP_SLOWTIMO); 3745243Sroot fp = ipq.next; 3755243Sroot if (fp == 0) { 3765243Sroot splx(s); 3775243Sroot return; 3785243Sroot } 3795243Sroot while (fp != &ipq) 3804640Swnj if (--fp->ipq_ttl == 0) 3814640Swnj fp = ip_freef(fp); 3824640Swnj else 3834640Swnj fp = fp->next; 3844640Swnj splx(s); 3854495Swnj } 3864495Swnj 3874951Swnj /* 3884951Swnj * Drain off all datagram fragments. 3894951Swnj */ 3904801Swnj ip_drain() 3914801Swnj { 3924801Swnj 3934951Swnj COUNT(IP_DRAIN); 3944951Swnj while (ipq.next != &ipq) 3954951Swnj (void) ip_freef(ipq.next); 3964801Swnj } 3974923Swnj 3984640Swnj /* 3994640Swnj * Do option processing on a datagram, 4004640Swnj * possibly discarding it if bad options 4014640Swnj * are encountered. 4024640Swnj */ 4034640Swnj ip_dooptions(ip) 4044640Swnj struct ip *ip; 4054495Swnj { 4064640Swnj register u_char *cp; 4074907Swnj int opt, optlen, cnt; 4084923Swnj struct in_addr *sin; 4094801Swnj register struct ip_timestamp *ipt; 4104951Swnj register struct ifnet *ifp; 4114951Swnj struct in_addr t; 4124495Swnj 4134951Swnj COUNT(IP_DOOPTIONS); 4144640Swnj cp = (u_char *)(ip + 1); 4154640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 4164640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 4174640Swnj opt = cp[0]; 4184640Swnj if (opt == IPOPT_EOL) 4194640Swnj break; 4204640Swnj if (opt == IPOPT_NOP) 4214640Swnj optlen = 1; 4224640Swnj else 4234640Swnj optlen = cp[1]; 4244640Swnj switch (opt) { 4254495Swnj 4264640Swnj default: 4274640Swnj break; 4284495Swnj 4294951Swnj /* 4304951Swnj * Source routing with record. 4314951Swnj * Find interface with current destination address. 4324951Swnj * If none on this machine then drop if strictly routed, 4334951Swnj * or do nothing if loosely routed. 4344951Swnj * Record interface address and bring up next address 4354951Swnj * component. If strictly routed make sure next 4364951Swnj * address on directly accessible net. 4374951Swnj */ 4384640Swnj case IPOPT_LSRR: 4394801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 4404640Swnj break; 4414923Swnj sin = (struct in_addr *)(cp + cp[2]); 4424951Swnj ifp = if_ifwithaddr(*sin); 4434951Swnj if (ifp == 0) { 4444951Swnj if (opt == IPOPT_SSRR) 4454951Swnj goto bad; 4464951Swnj break; 4474640Swnj } 4484951Swnj t = ip->ip_dst; ip->ip_dst = *sin; *sin = t; 4494951Swnj cp[2] += 4; 4504951Swnj if (cp[2] > optlen - (sizeof (long) - 1)) 4514951Swnj break; 4524951Swnj ip->ip_dst = sin[1]; 4534951Swnj if (opt == IPOPT_SSRR && if_ifonnetof(ip->ip_dst)==0) 4544951Swnj goto bad; 4554640Swnj break; 4564495Swnj 4574640Swnj case IPOPT_TS: 4584801Swnj ipt = (struct ip_timestamp *)cp; 4594801Swnj if (ipt->ipt_len < 5) 4604640Swnj goto bad; 4614801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 4624801Swnj if (++ipt->ipt_oflw == 0) 4634640Swnj goto bad; 4644495Swnj break; 4654640Swnj } 4664923Swnj sin = (struct in_addr *)(cp+cp[2]); 4674801Swnj switch (ipt->ipt_flg) { 4684495Swnj 4694640Swnj case IPOPT_TS_TSONLY: 4704640Swnj break; 4714640Swnj 4724640Swnj case IPOPT_TS_TSANDADDR: 4734801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4744640Swnj goto bad; 4754951Swnj /* stamp with ``first'' interface address */ 4764951Swnj *sin++ = ifnet->if_addr; 4774640Swnj break; 4784640Swnj 4794640Swnj case IPOPT_TS_PRESPEC: 4804951Swnj if (if_ifwithaddr(*sin) == 0) 4814951Swnj continue; 4824801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4834640Swnj goto bad; 4844801Swnj ipt->ipt_ptr += 4; 4854640Swnj break; 4864640Swnj 4874495Swnj default: 4884640Swnj goto bad; 4894495Swnj } 4904923Swnj *(n_time *)sin = iptime(); 4914801Swnj ipt->ipt_ptr += 4; 4924640Swnj } 4934495Swnj } 4944907Swnj return; 4954640Swnj bad: 4964640Swnj /* SHOULD FORCE ICMP MESSAGE */ 4974907Swnj return; 4984495Swnj } 4994495Swnj 5004640Swnj /* 5014951Swnj * Strip out IP options, at higher 5024951Swnj * level protocol in the kernel. 5034951Swnj * Second argument is buffer to which options 5044951Swnj * will be moved, and return value is their length. 5054640Swnj */ 5065217Swnj ip_stripoptions(ip, mopt) 5074640Swnj struct ip *ip; 5085217Swnj struct mbuf *mopt; 5094495Swnj { 5104640Swnj register int i; 5114640Swnj register struct mbuf *m; 5124640Swnj int olen; 5134951Swnj COUNT(IP_STRIPOPTIONS); 5144640Swnj 5154640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 5164951Swnj m = dtom(ip); 5174951Swnj ip++; 5185217Swnj if (mopt) { 5195217Swnj mopt->m_len = olen; 5205217Swnj mopt->m_off = MMINOFF; 5215217Swnj bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen); 5225217Swnj } 5234640Swnj i = m->m_len - (sizeof (struct ip) + olen); 5244907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 5255243Sroot m->m_len -= olen; 5264495Swnj } 527