1*6088Sroot /* ip_input.c 1.31 82/03/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); 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; 103*6088Sroot for (; m != NULL; m = m->m_next) { 104*6088Sroot if (m->m_free) panic("ipinput already free"); 1054495Swnj i += m->m_len; 106*6088Sroot } 1074640Swnj m = m0; 1084640Swnj if (i != ip->ip_len) { 1095161Swnj if (i < ip->ip_len) { 1105161Swnj ipstat.ips_tooshort++; 1114951Swnj goto bad; 1125161Swnj } 1134640Swnj m_adj(m, ip->ip_len - i); 1144495Swnj } 1154495Swnj 1164640Swnj /* 1174640Swnj * Process options and, if not destined for us, 1184640Swnj * ship it on. 1194640Swnj */ 1204543Swnj if (hlen > sizeof (struct ip)) 1214907Swnj ip_dooptions(ip); 1225084Swnj if (ifnet && ip->ip_dst.s_addr != ifnet->if_addr.s_addr && 123*6088Sroot ip->ip_dst.s_addr != ifnet->if_broadaddr.s_addr && 1245045Swnj if_ifwithaddr(ip->ip_dst) == 0) { 1255298Sroot printf("ip->ip_dst %x ip->ip_ttl %x\n", ip->ip_dst, ip->ip_ttl); 126*6088Sroot if (1) 127*6088Sroot goto bad; 1284640Swnj if (--ip->ip_ttl == 0) { 1294907Swnj icmp_error(ip, ICMP_TIMXCEED, 0); 1305084Swnj goto next; 1314495Swnj } 1325217Swnj mopt = m_get(M_DONTWAIT); 1335217Swnj if (mopt == 0) 1345217Swnj goto bad; 1355217Swnj ip_stripoptions(ip, mopt); 1365217Swnj (void) ip_output(m0, mopt); 1375084Swnj goto next; 1384543Swnj } 1394495Swnj 1404640Swnj /* 1414640Swnj * Look for queue of fragments 1424640Swnj * of this datagram. 1434640Swnj */ 1444640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1454640Swnj if (ip->ip_id == fp->ipq_id && 1464640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1474640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1484640Swnj ip->ip_p == fp->ipq_p) 1494640Swnj goto found; 1504640Swnj fp = 0; 1514640Swnj found: 1524495Swnj 1534640Swnj /* 1544640Swnj * Adjust ip_len to not reflect header, 1554640Swnj * set ip_mff if more fragments are expected, 1564640Swnj * convert offset of this to bytes. 1574640Swnj */ 1584640Swnj ip->ip_len -= hlen; 1594898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 1604640Swnj if (ip->ip_off & IP_MF) 1614898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 1624640Swnj ip->ip_off <<= 3; 1634495Swnj 1644640Swnj /* 1654640Swnj * If datagram marked as having more fragments 1664640Swnj * or if this is not the first fragment, 1674640Swnj * attempt reassembly; if it succeeds, proceed. 1684640Swnj */ 1694898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 1704898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 1714640Swnj if (ip == 0) 1725084Swnj goto next; 1734640Swnj hlen = ip->ip_hl << 2; 1744640Swnj m = dtom(ip); 1754640Swnj } else 1764640Swnj if (fp) 1774640Swnj (void) ip_freef(fp); 1784951Swnj 1794951Swnj /* 1804951Swnj * Switch out to protocol's input routine. 1814951Swnj */ 1824898Swnj (*protosw[ip_protox[ip->ip_p]].pr_input)(m); 1835084Swnj goto next; 1844951Swnj bad: 1854951Swnj m_freem(m); 1865084Swnj goto next; 1874640Swnj } 1884495Swnj 1894640Swnj /* 1904640Swnj * Take incoming datagram fragment and try to 1914951Swnj * reassemble it into whole datagram. If a chain for 1924640Swnj * reassembly of this datagram already exists, then it 1934640Swnj * is given as fp; otherwise have to make a chain. 1944640Swnj */ 1954640Swnj struct ip * 1964640Swnj ip_reass(ip, fp) 1974898Swnj register struct ipasfrag *ip; 1984640Swnj register struct ipq *fp; 1994640Swnj { 2004640Swnj register struct mbuf *m = dtom(ip); 2014898Swnj register struct ipasfrag *q; 2024640Swnj struct mbuf *t; 2034640Swnj int hlen = ip->ip_hl << 2; 2044640Swnj int i, next; 2054951Swnj COUNT(IP_REASS); 2064543Swnj 2074640Swnj /* 2084640Swnj * Presence of header sizes in mbufs 2094640Swnj * would confuse code below. 2104640Swnj */ 2114640Swnj m->m_off += hlen; 2124640Swnj m->m_len -= hlen; 2134495Swnj 2144640Swnj /* 2154640Swnj * If first fragment to arrive, create a reassembly queue. 2164640Swnj */ 2174640Swnj if (fp == 0) { 2185851Sroot if ((t = m_get(M_WAIT)) == NULL) 2194640Swnj goto dropfrag; 2204640Swnj t->m_off = MMINOFF; 2214640Swnj fp = mtod(t, struct ipq *); 2224640Swnj insque(fp, &ipq); 2234640Swnj fp->ipq_ttl = IPFRAGTTL; 2244640Swnj fp->ipq_p = ip->ip_p; 2254640Swnj fp->ipq_id = ip->ip_id; 2264898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 2274898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 2284898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2295161Swnj q = (struct ipasfrag *)fp; 2305161Swnj goto insert; 2314640Swnj } 2324495Swnj 2334640Swnj /* 2344640Swnj * Find a segment which begins after this one does. 2354640Swnj */ 2364898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2374640Swnj if (q->ip_off > ip->ip_off) 2384640Swnj break; 2394495Swnj 2404640Swnj /* 2414640Swnj * If there is a preceding segment, it may provide some of 2424640Swnj * our data already. If so, drop the data from the incoming 2434640Swnj * segment. If it provides all of our data, drop us. 2444640Swnj */ 2454898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 2464898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2474640Swnj if (i > 0) { 2484640Swnj if (i >= ip->ip_len) 2494640Swnj goto dropfrag; 2504640Swnj m_adj(dtom(ip), i); 2514640Swnj ip->ip_off += i; 2524640Swnj ip->ip_len -= i; 2534640Swnj } 2544640Swnj } 2554543Swnj 2564640Swnj /* 2574640Swnj * While we overlap succeeding segments trim them or, 2584640Swnj * if they are completely covered, dequeue them. 2594640Swnj */ 2604898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 2614640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 2624640Swnj if (i < q->ip_len) { 2634640Swnj q->ip_len -= i; 2644640Swnj m_adj(dtom(q), i); 2654640Swnj break; 2664495Swnj } 2674898Swnj q = q->ipf_next; 2684898Swnj m_freem(dtom(q->ipf_prev)); 2694898Swnj ip_deq(q->ipf_prev); 2704543Swnj } 2714495Swnj 2725161Swnj insert: 2734640Swnj /* 2744640Swnj * Stick new segment in its place; 2754640Swnj * check for complete reassembly. 2764640Swnj */ 2774898Swnj ip_enq(ip, q->ipf_prev); 2784640Swnj next = 0; 2794898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 2804640Swnj if (q->ip_off != next) 2814640Swnj return (0); 2824640Swnj next += q->ip_len; 2834640Swnj } 2844898Swnj if (q->ipf_prev->ipf_mff) 2854640Swnj return (0); 2864495Swnj 2874640Swnj /* 2884640Swnj * Reassembly is complete; concatenate fragments. 2894640Swnj */ 2904640Swnj q = fp->ipq_next; 2914640Swnj m = dtom(q); 2924640Swnj t = m->m_next; 2934640Swnj m->m_next = 0; 2944640Swnj m_cat(m, t); 2954898Swnj while ((q = q->ipf_next) != (struct ipasfrag *)fp) 2964640Swnj m_cat(m, dtom(q)); 2974495Swnj 2984640Swnj /* 2994640Swnj * Create header for new ip packet by 3004640Swnj * modifying header of first packet; 3014640Swnj * dequeue and discard fragment reassembly header. 3024640Swnj * Make header visible. 3034640Swnj */ 3044640Swnj ip = fp->ipq_next; 3054640Swnj ip->ip_len = next; 3064898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 3074898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 3084640Swnj remque(fp); 3094907Swnj (void) m_free(dtom(fp)); 3104640Swnj m = dtom(ip); 3114898Swnj m->m_len += sizeof (struct ipasfrag); 3124898Swnj m->m_off -= sizeof (struct ipasfrag); 3134898Swnj return ((struct ip *)ip); 3144495Swnj 3154640Swnj dropfrag: 3164640Swnj m_freem(m); 3174640Swnj return (0); 3184495Swnj } 3194495Swnj 3204640Swnj /* 3214640Swnj * Free a fragment reassembly header and all 3224640Swnj * associated datagrams. 3234640Swnj */ 3244640Swnj struct ipq * 3254640Swnj ip_freef(fp) 3264640Swnj struct ipq *fp; 3274495Swnj { 3284898Swnj register struct ipasfrag *q; 3294640Swnj struct mbuf *m; 3304951Swnj COUNT(IP_FREEF); 3314495Swnj 3324898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3334640Swnj m_freem(dtom(q)); 3344640Swnj m = dtom(fp); 3354640Swnj fp = fp->next; 3364640Swnj remque(fp->prev); 3374907Swnj (void) m_free(m); 3384640Swnj return (fp); 3394495Swnj } 3404495Swnj 3414640Swnj /* 3424640Swnj * Put an ip fragment on a reassembly chain. 3434640Swnj * Like insque, but pointers in middle of structure. 3444640Swnj */ 3454640Swnj ip_enq(p, prev) 3464898Swnj register struct ipasfrag *p, *prev; 3474495Swnj { 3484951Swnj 3494640Swnj COUNT(IP_ENQ); 3504898Swnj p->ipf_prev = prev; 3514898Swnj p->ipf_next = prev->ipf_next; 3524898Swnj prev->ipf_next->ipf_prev = p; 3534898Swnj prev->ipf_next = p; 3544495Swnj } 3554495Swnj 3564640Swnj /* 3574640Swnj * To ip_enq as remque is to insque. 3584640Swnj */ 3594640Swnj ip_deq(p) 3604898Swnj register struct ipasfrag *p; 3614640Swnj { 3624951Swnj 3634640Swnj COUNT(IP_DEQ); 3644898Swnj p->ipf_prev->ipf_next = p->ipf_next; 3654898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3664495Swnj } 3674495Swnj 3684640Swnj /* 3694640Swnj * IP timer processing; 3704640Swnj * if a timer expires on a reassembly 3714640Swnj * queue, discard it. 3724640Swnj */ 3734801Swnj ip_slowtimo() 3744495Swnj { 3754495Swnj register struct ipq *fp; 3764640Swnj int s = splnet(); 3774951Swnj 3784801Swnj COUNT(IP_SLOWTIMO); 3795243Sroot fp = ipq.next; 3805243Sroot if (fp == 0) { 3815243Sroot splx(s); 3825243Sroot return; 3835243Sroot } 3845243Sroot while (fp != &ipq) 3854640Swnj if (--fp->ipq_ttl == 0) 3864640Swnj fp = ip_freef(fp); 3874640Swnj else 3884640Swnj fp = fp->next; 3894640Swnj splx(s); 3904495Swnj } 3914495Swnj 3924951Swnj /* 3934951Swnj * Drain off all datagram fragments. 3944951Swnj */ 3954801Swnj ip_drain() 3964801Swnj { 3974801Swnj 3984951Swnj COUNT(IP_DRAIN); 3994951Swnj while (ipq.next != &ipq) 4004951Swnj (void) ip_freef(ipq.next); 4014801Swnj } 4024923Swnj 4034640Swnj /* 4044640Swnj * Do option processing on a datagram, 4054640Swnj * possibly discarding it if bad options 4064640Swnj * are encountered. 4074640Swnj */ 4084640Swnj ip_dooptions(ip) 4094640Swnj struct ip *ip; 4104495Swnj { 4114640Swnj register u_char *cp; 4124907Swnj int opt, optlen, cnt; 4134923Swnj struct in_addr *sin; 4144801Swnj register struct ip_timestamp *ipt; 4154951Swnj register struct ifnet *ifp; 4164951Swnj struct in_addr t; 4174495Swnj 4184951Swnj COUNT(IP_DOOPTIONS); 4194640Swnj cp = (u_char *)(ip + 1); 4204640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 4214640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 4224640Swnj opt = cp[0]; 4234640Swnj if (opt == IPOPT_EOL) 4244640Swnj break; 4254640Swnj if (opt == IPOPT_NOP) 4264640Swnj optlen = 1; 4274640Swnj else 4284640Swnj optlen = cp[1]; 4294640Swnj switch (opt) { 4304495Swnj 4314640Swnj default: 4324640Swnj break; 4334495Swnj 4344951Swnj /* 4354951Swnj * Source routing with record. 4364951Swnj * Find interface with current destination address. 4374951Swnj * If none on this machine then drop if strictly routed, 4384951Swnj * or do nothing if loosely routed. 4394951Swnj * Record interface address and bring up next address 4404951Swnj * component. If strictly routed make sure next 4414951Swnj * address on directly accessible net. 4424951Swnj */ 4434640Swnj case IPOPT_LSRR: 4444801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 4454640Swnj break; 4464923Swnj sin = (struct in_addr *)(cp + cp[2]); 4474951Swnj ifp = if_ifwithaddr(*sin); 4484951Swnj if (ifp == 0) { 4494951Swnj if (opt == IPOPT_SSRR) 4504951Swnj goto bad; 4514951Swnj break; 4524640Swnj } 4534951Swnj t = ip->ip_dst; ip->ip_dst = *sin; *sin = t; 4544951Swnj cp[2] += 4; 4554951Swnj if (cp[2] > optlen - (sizeof (long) - 1)) 4564951Swnj break; 4574951Swnj ip->ip_dst = sin[1]; 4584951Swnj if (opt == IPOPT_SSRR && if_ifonnetof(ip->ip_dst)==0) 4594951Swnj goto bad; 4604640Swnj break; 4614495Swnj 4624640Swnj case IPOPT_TS: 4634801Swnj ipt = (struct ip_timestamp *)cp; 4644801Swnj if (ipt->ipt_len < 5) 4654640Swnj goto bad; 4664801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 4674801Swnj if (++ipt->ipt_oflw == 0) 4684640Swnj goto bad; 4694495Swnj break; 4704640Swnj } 4714923Swnj sin = (struct in_addr *)(cp+cp[2]); 4724801Swnj switch (ipt->ipt_flg) { 4734495Swnj 4744640Swnj case IPOPT_TS_TSONLY: 4754640Swnj break; 4764640Swnj 4774640Swnj case IPOPT_TS_TSANDADDR: 4784801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4794640Swnj goto bad; 4804951Swnj /* stamp with ``first'' interface address */ 4814951Swnj *sin++ = ifnet->if_addr; 4824640Swnj break; 4834640Swnj 4844640Swnj case IPOPT_TS_PRESPEC: 4854951Swnj if (if_ifwithaddr(*sin) == 0) 4864951Swnj continue; 4874801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4884640Swnj goto bad; 4894801Swnj ipt->ipt_ptr += 4; 4904640Swnj break; 4914640Swnj 4924495Swnj default: 4934640Swnj goto bad; 4944495Swnj } 4954923Swnj *(n_time *)sin = iptime(); 4964801Swnj ipt->ipt_ptr += 4; 4974640Swnj } 4984495Swnj } 4994907Swnj return; 5004640Swnj bad: 5014640Swnj /* SHOULD FORCE ICMP MESSAGE */ 5024907Swnj return; 5034495Swnj } 5044495Swnj 5054640Swnj /* 5064951Swnj * Strip out IP options, at higher 5074951Swnj * level protocol in the kernel. 5084951Swnj * Second argument is buffer to which options 5094951Swnj * will be moved, and return value is their length. 5104640Swnj */ 5115217Swnj ip_stripoptions(ip, mopt) 5124640Swnj struct ip *ip; 5135217Swnj struct mbuf *mopt; 5144495Swnj { 5154640Swnj register int i; 5164640Swnj register struct mbuf *m; 5174640Swnj int olen; 5184951Swnj COUNT(IP_STRIPOPTIONS); 5194640Swnj 5204640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 5214951Swnj m = dtom(ip); 5224951Swnj ip++; 5235217Swnj if (mopt) { 5245217Swnj mopt->m_len = olen; 5255217Swnj mopt->m_off = MMINOFF; 5265217Swnj bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen); 5275217Swnj } 5284640Swnj i = m->m_len - (sizeof (struct ip) + olen); 5294907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 5305243Sroot m->m_len -= olen; 5314495Swnj } 532