1*5218Swnj /* ip_input.c 1.24 81/12/09 */ 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); 68*5218Swnj if (m == 0) 695084Swnj return; 705046Swnj if (m->m_len < sizeof (struct ip) && 71*5218Swnj m_pullup(m, sizeof (struct ip)) == 0) 725046Swnj goto bad; 734640Swnj ip = mtod(m, struct ip *); 745161Swnj if ((hlen = ip->ip_hl << 2) > m->m_len) { 755161Swnj if (m_pullup(m, hlen) == 0) 765161Swnj goto bad; 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) { 1224640Swnj if (--ip->ip_ttl == 0) { 1234907Swnj icmp_error(ip, ICMP_TIMXCEED, 0); 1245084Swnj goto next; 1254495Swnj } 1265217Swnj mopt = m_get(M_DONTWAIT); 1275217Swnj if (mopt == 0) 1285217Swnj goto bad; 1295217Swnj ip_stripoptions(ip, mopt); 1305217Swnj (void) ip_output(m0, mopt); 1315084Swnj goto next; 1324543Swnj } 1334495Swnj 1344640Swnj /* 1354640Swnj * Look for queue of fragments 1364640Swnj * of this datagram. 1374640Swnj */ 1384640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1394640Swnj if (ip->ip_id == fp->ipq_id && 1404640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1414640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1424640Swnj ip->ip_p == fp->ipq_p) 1434640Swnj goto found; 1444640Swnj fp = 0; 1454640Swnj found: 1464495Swnj 1474640Swnj /* 1484640Swnj * Adjust ip_len to not reflect header, 1494640Swnj * set ip_mff if more fragments are expected, 1504640Swnj * convert offset of this to bytes. 1514640Swnj */ 1524640Swnj ip->ip_len -= hlen; 1534898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 1544640Swnj if (ip->ip_off & IP_MF) 1554898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 1564640Swnj ip->ip_off <<= 3; 1574495Swnj 1584640Swnj /* 1594640Swnj * If datagram marked as having more fragments 1604640Swnj * or if this is not the first fragment, 1614640Swnj * attempt reassembly; if it succeeds, proceed. 1624640Swnj */ 1634898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 1644898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 1654640Swnj if (ip == 0) 1665084Swnj goto next; 1674640Swnj hlen = ip->ip_hl << 2; 1684640Swnj m = dtom(ip); 1694640Swnj } else 1704640Swnj if (fp) 1714640Swnj (void) ip_freef(fp); 1724951Swnj 1734951Swnj /* 1744951Swnj * Switch out to protocol's input routine. 1754951Swnj */ 1764898Swnj (*protosw[ip_protox[ip->ip_p]].pr_input)(m); 1775084Swnj goto next; 1784951Swnj bad: 1794951Swnj m_freem(m); 1805084Swnj goto next; 1814640Swnj } 1824495Swnj 1834640Swnj /* 1844640Swnj * Take incoming datagram fragment and try to 1854951Swnj * reassemble it into whole datagram. If a chain for 1864640Swnj * reassembly of this datagram already exists, then it 1874640Swnj * is given as fp; otherwise have to make a chain. 1884640Swnj */ 1894640Swnj struct ip * 1904640Swnj ip_reass(ip, fp) 1914898Swnj register struct ipasfrag *ip; 1924640Swnj register struct ipq *fp; 1934640Swnj { 1944640Swnj register struct mbuf *m = dtom(ip); 1954898Swnj register struct ipasfrag *q; 1964640Swnj struct mbuf *t; 1974640Swnj int hlen = ip->ip_hl << 2; 1984640Swnj int i, next; 1994951Swnj COUNT(IP_REASS); 2004543Swnj 2014640Swnj /* 2024640Swnj * Presence of header sizes in mbufs 2034640Swnj * would confuse code below. 2044640Swnj */ 2054640Swnj m->m_off += hlen; 2064640Swnj m->m_len -= hlen; 2074495Swnj 2084640Swnj /* 2094640Swnj * If first fragment to arrive, create a reassembly queue. 2104640Swnj */ 2114640Swnj if (fp == 0) { 2124640Swnj if ((t = m_get(1)) == NULL) 2134640Swnj goto dropfrag; 2144640Swnj t->m_off = MMINOFF; 2154640Swnj fp = mtod(t, struct ipq *); 2164640Swnj insque(fp, &ipq); 2174640Swnj fp->ipq_ttl = IPFRAGTTL; 2184640Swnj fp->ipq_p = ip->ip_p; 2194640Swnj fp->ipq_id = ip->ip_id; 2204898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 2214898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 2224898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2235161Swnj q = (struct ipasfrag *)fp; 2245161Swnj goto insert; 2254640Swnj } 2264495Swnj 2274640Swnj /* 2284640Swnj * Find a segment which begins after this one does. 2294640Swnj */ 2304898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2314640Swnj if (q->ip_off > ip->ip_off) 2324640Swnj break; 2334495Swnj 2344640Swnj /* 2354640Swnj * If there is a preceding segment, it may provide some of 2364640Swnj * our data already. If so, drop the data from the incoming 2374640Swnj * segment. If it provides all of our data, drop us. 2384640Swnj */ 2394898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 2404898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2414640Swnj if (i > 0) { 2424640Swnj if (i >= ip->ip_len) 2434640Swnj goto dropfrag; 2444640Swnj m_adj(dtom(ip), i); 2454640Swnj ip->ip_off += i; 2464640Swnj ip->ip_len -= i; 2474640Swnj } 2484640Swnj } 2494543Swnj 2504640Swnj /* 2514640Swnj * While we overlap succeeding segments trim them or, 2524640Swnj * if they are completely covered, dequeue them. 2534640Swnj */ 2544898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 2554640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 2564640Swnj if (i < q->ip_len) { 2574640Swnj q->ip_len -= i; 2584640Swnj m_adj(dtom(q), i); 2594640Swnj break; 2604495Swnj } 2614898Swnj q = q->ipf_next; 2624898Swnj m_freem(dtom(q->ipf_prev)); 2634898Swnj ip_deq(q->ipf_prev); 2644543Swnj } 2654495Swnj 2665161Swnj insert: 2674640Swnj /* 2684640Swnj * Stick new segment in its place; 2694640Swnj * check for complete reassembly. 2704640Swnj */ 2714898Swnj ip_enq(ip, q->ipf_prev); 2724640Swnj next = 0; 2734898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 2744640Swnj if (q->ip_off != next) 2754640Swnj return (0); 2764640Swnj next += q->ip_len; 2774640Swnj } 2784898Swnj if (q->ipf_prev->ipf_mff) 2794640Swnj return (0); 2804495Swnj 2814640Swnj /* 2824640Swnj * Reassembly is complete; concatenate fragments. 2834640Swnj */ 2844640Swnj q = fp->ipq_next; 2854640Swnj m = dtom(q); 2864640Swnj t = m->m_next; 2874640Swnj m->m_next = 0; 2884640Swnj m_cat(m, t); 2894898Swnj while ((q = q->ipf_next) != (struct ipasfrag *)fp) 2904640Swnj m_cat(m, dtom(q)); 2914495Swnj 2924640Swnj /* 2934640Swnj * Create header for new ip packet by 2944640Swnj * modifying header of first packet; 2954640Swnj * dequeue and discard fragment reassembly header. 2964640Swnj * Make header visible. 2974640Swnj */ 2984640Swnj ip = fp->ipq_next; 2994640Swnj ip->ip_len = next; 3004898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 3014898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 3024640Swnj remque(fp); 3034907Swnj (void) m_free(dtom(fp)); 3044640Swnj m = dtom(ip); 3054898Swnj m->m_len += sizeof (struct ipasfrag); 3064898Swnj m->m_off -= sizeof (struct ipasfrag); 3074898Swnj return ((struct ip *)ip); 3084495Swnj 3094640Swnj dropfrag: 3104640Swnj m_freem(m); 3114640Swnj return (0); 3124495Swnj } 3134495Swnj 3144640Swnj /* 3154640Swnj * Free a fragment reassembly header and all 3164640Swnj * associated datagrams. 3174640Swnj */ 3184640Swnj struct ipq * 3194640Swnj ip_freef(fp) 3204640Swnj struct ipq *fp; 3214495Swnj { 3224898Swnj register struct ipasfrag *q; 3234640Swnj struct mbuf *m; 3244951Swnj COUNT(IP_FREEF); 3254495Swnj 3264898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3274640Swnj m_freem(dtom(q)); 3284640Swnj m = dtom(fp); 3294640Swnj fp = fp->next; 3304640Swnj remque(fp->prev); 3314907Swnj (void) m_free(m); 3324640Swnj return (fp); 3334495Swnj } 3344495Swnj 3354640Swnj /* 3364640Swnj * Put an ip fragment on a reassembly chain. 3374640Swnj * Like insque, but pointers in middle of structure. 3384640Swnj */ 3394640Swnj ip_enq(p, prev) 3404898Swnj register struct ipasfrag *p, *prev; 3414495Swnj { 3424951Swnj 3434640Swnj COUNT(IP_ENQ); 3444898Swnj p->ipf_prev = prev; 3454898Swnj p->ipf_next = prev->ipf_next; 3464898Swnj prev->ipf_next->ipf_prev = p; 3474898Swnj prev->ipf_next = p; 3484495Swnj } 3494495Swnj 3504640Swnj /* 3514640Swnj * To ip_enq as remque is to insque. 3524640Swnj */ 3534640Swnj ip_deq(p) 3544898Swnj register struct ipasfrag *p; 3554640Swnj { 3564951Swnj 3574640Swnj COUNT(IP_DEQ); 3584898Swnj p->ipf_prev->ipf_next = p->ipf_next; 3594898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3604495Swnj } 3614495Swnj 3624640Swnj /* 3634640Swnj * IP timer processing; 3644640Swnj * if a timer expires on a reassembly 3654640Swnj * queue, discard it. 3664640Swnj */ 3674801Swnj ip_slowtimo() 3684495Swnj { 3694495Swnj register struct ipq *fp; 3704640Swnj int s = splnet(); 3714951Swnj 3724801Swnj COUNT(IP_SLOWTIMO); 3734644Swnj for (fp = ipq.next; fp != &ipq; ) 3744640Swnj if (--fp->ipq_ttl == 0) 3754640Swnj fp = ip_freef(fp); 3764640Swnj else 3774640Swnj fp = fp->next; 3784640Swnj splx(s); 3794495Swnj } 3804495Swnj 3814951Swnj /* 3824951Swnj * Drain off all datagram fragments. 3834951Swnj */ 3844801Swnj ip_drain() 3854801Swnj { 3864801Swnj 3874951Swnj COUNT(IP_DRAIN); 3884951Swnj while (ipq.next != &ipq) 3894951Swnj (void) ip_freef(ipq.next); 3904801Swnj } 3914923Swnj 3924640Swnj /* 3934640Swnj * Do option processing on a datagram, 3944640Swnj * possibly discarding it if bad options 3954640Swnj * are encountered. 3964640Swnj */ 3974640Swnj ip_dooptions(ip) 3984640Swnj struct ip *ip; 3994495Swnj { 4004640Swnj register u_char *cp; 4014907Swnj int opt, optlen, cnt; 4024923Swnj struct in_addr *sin; 4034801Swnj register struct ip_timestamp *ipt; 4044951Swnj register struct ifnet *ifp; 4054951Swnj struct in_addr t; 4064495Swnj 4074951Swnj COUNT(IP_DOOPTIONS); 4084640Swnj cp = (u_char *)(ip + 1); 4094640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 4104640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 4114640Swnj opt = cp[0]; 4124640Swnj if (opt == IPOPT_EOL) 4134640Swnj break; 4144640Swnj if (opt == IPOPT_NOP) 4154640Swnj optlen = 1; 4164640Swnj else 4174640Swnj optlen = cp[1]; 4184640Swnj switch (opt) { 4194495Swnj 4204640Swnj default: 4214640Swnj break; 4224495Swnj 4234951Swnj /* 4244951Swnj * Source routing with record. 4254951Swnj * Find interface with current destination address. 4264951Swnj * If none on this machine then drop if strictly routed, 4274951Swnj * or do nothing if loosely routed. 4284951Swnj * Record interface address and bring up next address 4294951Swnj * component. If strictly routed make sure next 4304951Swnj * address on directly accessible net. 4314951Swnj */ 4324640Swnj case IPOPT_LSRR: 4334801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 4344640Swnj break; 4354923Swnj sin = (struct in_addr *)(cp + cp[2]); 4364951Swnj ifp = if_ifwithaddr(*sin); 4374951Swnj if (ifp == 0) { 4384951Swnj if (opt == IPOPT_SSRR) 4394951Swnj goto bad; 4404951Swnj break; 4414640Swnj } 4424951Swnj t = ip->ip_dst; ip->ip_dst = *sin; *sin = t; 4434951Swnj cp[2] += 4; 4444951Swnj if (cp[2] > optlen - (sizeof (long) - 1)) 4454951Swnj break; 4464951Swnj ip->ip_dst = sin[1]; 4474951Swnj if (opt == IPOPT_SSRR && if_ifonnetof(ip->ip_dst)==0) 4484951Swnj goto bad; 4494640Swnj break; 4504495Swnj 4514640Swnj case IPOPT_TS: 4524801Swnj ipt = (struct ip_timestamp *)cp; 4534801Swnj if (ipt->ipt_len < 5) 4544640Swnj goto bad; 4554801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 4564801Swnj if (++ipt->ipt_oflw == 0) 4574640Swnj goto bad; 4584495Swnj break; 4594640Swnj } 4604923Swnj sin = (struct in_addr *)(cp+cp[2]); 4614801Swnj switch (ipt->ipt_flg) { 4624495Swnj 4634640Swnj case IPOPT_TS_TSONLY: 4644640Swnj break; 4654640Swnj 4664640Swnj case IPOPT_TS_TSANDADDR: 4674801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4684640Swnj goto bad; 4694951Swnj /* stamp with ``first'' interface address */ 4704951Swnj *sin++ = ifnet->if_addr; 4714640Swnj break; 4724640Swnj 4734640Swnj case IPOPT_TS_PRESPEC: 4744951Swnj if (if_ifwithaddr(*sin) == 0) 4754951Swnj continue; 4764801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4774640Swnj goto bad; 4784801Swnj ipt->ipt_ptr += 4; 4794640Swnj break; 4804640Swnj 4814495Swnj default: 4824640Swnj goto bad; 4834495Swnj } 4844923Swnj *(n_time *)sin = iptime(); 4854801Swnj ipt->ipt_ptr += 4; 4864640Swnj } 4874495Swnj } 4884907Swnj return; 4894640Swnj bad: 4904640Swnj /* SHOULD FORCE ICMP MESSAGE */ 4914907Swnj return; 4924495Swnj } 4934495Swnj 4944640Swnj /* 4954951Swnj * Strip out IP options, at higher 4964951Swnj * level protocol in the kernel. 4974951Swnj * Second argument is buffer to which options 4984951Swnj * will be moved, and return value is their length. 4994640Swnj */ 5005217Swnj ip_stripoptions(ip, mopt) 5014640Swnj struct ip *ip; 5025217Swnj struct mbuf *mopt; 5034495Swnj { 5044640Swnj register int i; 5054640Swnj register struct mbuf *m; 5064640Swnj int olen; 5074951Swnj COUNT(IP_STRIPOPTIONS); 5084640Swnj 5094640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 5104951Swnj m = dtom(ip); 5114951Swnj ip++; 5125217Swnj if (mopt) { 5135217Swnj mopt->m_len = olen; 5145217Swnj mopt->m_off = MMINOFF; 5155217Swnj bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen); 5165217Swnj } 5174640Swnj i = m->m_len - (sizeof (struct ip) + olen); 5184907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 5194640Swnj m->m_len -= i; 5204495Swnj } 521