1*5046Swnj /* ip_input.c 1.18 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*5046Swnj if (m->m_len < sizeof (struct ip) && 68*5046Swnj m_pullup(m, sizeof (struct ip)) == 0) 69*5046Swnj goto bad; 704640Swnj ip = mtod(m, struct ip *); 71*5046Swnj if ((hlen = ip->ip_hl << 2) > m->m_len && 72*5046Swnj m_pullup(m, hlen) == 0) 734951Swnj goto bad; 744951Swnj if (ipcksum) 755045Swnj if ((ip->ip_sum = inet_cksum(m, hlen)) != 0xffff) { 764951Swnj printf("ip_sum %x\n", ip->ip_sum); 774951Swnj ipstat.ips_badsum++; 784951Swnj goto bad; 794495Swnj } 804951Swnj 814951Swnj /* 824951Swnj * Convert fields to host representation. 834951Swnj */ 844907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 854640Swnj ip->ip_id = ntohs(ip->ip_id); 864951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 874495Swnj 884543Swnj /* 894640Swnj * Check that the amount of data in the buffers 904640Swnj * is as at least much as the IP header would have us expect. 914640Swnj * Trim mbufs if longer than we expect. 924640Swnj * Drop packet if shorter than we expect. 934543Swnj */ 944640Swnj i = 0; 95*5046Swnj for (; m != NULL; m = m->m_next) 964495Swnj i += m->m_len; 974640Swnj m = m0; 984640Swnj if (i != ip->ip_len) { 99*5046Swnj if (i < ip->ip_len) 1004951Swnj goto bad; 1014640Swnj m_adj(m, ip->ip_len - i); 1024495Swnj } 1034495Swnj 1044640Swnj /* 1054640Swnj * Process options and, if not destined for us, 1064640Swnj * ship it on. 1074640Swnj */ 1084543Swnj if (hlen > sizeof (struct ip)) 1094907Swnj ip_dooptions(ip); 1105045Swnj if (ip->ip_dst.s_addr != n_lhost.s_addr && 1115045Swnj if_ifwithaddr(ip->ip_dst) == 0) { 1124640Swnj if (--ip->ip_ttl == 0) { 1134907Swnj icmp_error(ip, ICMP_TIMXCEED, 0); 1144543Swnj return; 1154495Swnj } 1164640Swnj ip_output(dtom(ip)); 1174640Swnj return; 1184543Swnj } 1194495Swnj 1204640Swnj /* 1214640Swnj * Look for queue of fragments 1224640Swnj * of this datagram. 1234640Swnj */ 1244640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1254640Swnj if (ip->ip_id == fp->ipq_id && 1264640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1274640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1284640Swnj ip->ip_p == fp->ipq_p) 1294640Swnj goto found; 1304640Swnj fp = 0; 1314640Swnj found: 1324495Swnj 1334640Swnj /* 1344640Swnj * Adjust ip_len to not reflect header, 1354640Swnj * set ip_mff if more fragments are expected, 1364640Swnj * convert offset of this to bytes. 1374640Swnj */ 1384640Swnj ip->ip_len -= hlen; 1394898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 1404640Swnj if (ip->ip_off & IP_MF) 1414898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 1424640Swnj ip->ip_off <<= 3; 1434495Swnj 1444640Swnj /* 1454640Swnj * If datagram marked as having more fragments 1464640Swnj * or if this is not the first fragment, 1474640Swnj * attempt reassembly; if it succeeds, proceed. 1484640Swnj */ 1494898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 1504898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 1514640Swnj if (ip == 0) 1524640Swnj return; 1534640Swnj hlen = ip->ip_hl << 2; 1544640Swnj m = dtom(ip); 1554640Swnj } else 1564640Swnj if (fp) 1574640Swnj (void) ip_freef(fp); 1584951Swnj 1594951Swnj /* 1604951Swnj * Switch out to protocol's input routine. 1614951Swnj */ 1624898Swnj (*protosw[ip_protox[ip->ip_p]].pr_input)(m); 1634951Swnj return; 1644951Swnj bad: 1654951Swnj m_freem(m); 1664640Swnj } 1674495Swnj 1684640Swnj /* 1694640Swnj * Take incoming datagram fragment and try to 1704951Swnj * reassemble it into whole datagram. If a chain for 1714640Swnj * reassembly of this datagram already exists, then it 1724640Swnj * is given as fp; otherwise have to make a chain. 1734640Swnj */ 1744640Swnj struct ip * 1754640Swnj ip_reass(ip, fp) 1764898Swnj register struct ipasfrag *ip; 1774640Swnj register struct ipq *fp; 1784640Swnj { 1794640Swnj register struct mbuf *m = dtom(ip); 1804898Swnj register struct ipasfrag *q; 1814640Swnj struct mbuf *t; 1824640Swnj int hlen = ip->ip_hl << 2; 1834640Swnj int i, next; 1844951Swnj COUNT(IP_REASS); 1854543Swnj 1864640Swnj /* 1874640Swnj * Presence of header sizes in mbufs 1884640Swnj * would confuse code below. 1894640Swnj */ 1904640Swnj m->m_off += hlen; 1914640Swnj m->m_len -= hlen; 1924495Swnj 1934640Swnj /* 1944640Swnj * If first fragment to arrive, create a reassembly queue. 1954640Swnj */ 1964640Swnj if (fp == 0) { 1974640Swnj if ((t = m_get(1)) == NULL) 1984640Swnj goto dropfrag; 1994640Swnj t->m_off = MMINOFF; 2004640Swnj fp = mtod(t, struct ipq *); 2014640Swnj insque(fp, &ipq); 2024640Swnj fp->ipq_ttl = IPFRAGTTL; 2034640Swnj fp->ipq_p = ip->ip_p; 2044640Swnj fp->ipq_id = ip->ip_id; 2054898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 2064898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 2074898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2084640Swnj } 2094495Swnj 2104640Swnj /* 2114640Swnj * Find a segment which begins after this one does. 2124640Swnj */ 2134898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2144640Swnj if (q->ip_off > ip->ip_off) 2154640Swnj break; 2164495Swnj 2174640Swnj /* 2184640Swnj * If there is a preceding segment, it may provide some of 2194640Swnj * our data already. If so, drop the data from the incoming 2204640Swnj * segment. If it provides all of our data, drop us. 2214640Swnj */ 2224898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 2234898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2244640Swnj if (i > 0) { 2254640Swnj if (i >= ip->ip_len) 2264640Swnj goto dropfrag; 2274640Swnj m_adj(dtom(ip), i); 2284640Swnj ip->ip_off += i; 2294640Swnj ip->ip_len -= i; 2304640Swnj } 2314640Swnj } 2324543Swnj 2334640Swnj /* 2344640Swnj * While we overlap succeeding segments trim them or, 2354640Swnj * if they are completely covered, dequeue them. 2364640Swnj */ 2374898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 2384640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 2394640Swnj if (i < q->ip_len) { 2404640Swnj q->ip_len -= i; 2414640Swnj m_adj(dtom(q), i); 2424640Swnj break; 2434495Swnj } 2444898Swnj q = q->ipf_next; 2454898Swnj m_freem(dtom(q->ipf_prev)); 2464898Swnj ip_deq(q->ipf_prev); 2474543Swnj } 2484495Swnj 2494640Swnj /* 2504640Swnj * Stick new segment in its place; 2514640Swnj * check for complete reassembly. 2524640Swnj */ 2534898Swnj ip_enq(ip, q->ipf_prev); 2544640Swnj next = 0; 2554898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 2564640Swnj if (q->ip_off != next) 2574640Swnj return (0); 2584640Swnj next += q->ip_len; 2594640Swnj } 2604898Swnj if (q->ipf_prev->ipf_mff) 2614640Swnj return (0); 2624495Swnj 2634640Swnj /* 2644640Swnj * Reassembly is complete; concatenate fragments. 2654640Swnj */ 2664640Swnj q = fp->ipq_next; 2674640Swnj m = dtom(q); 2684640Swnj t = m->m_next; 2694640Swnj m->m_next = 0; 2704640Swnj m_cat(m, t); 2714898Swnj while ((q = q->ipf_next) != (struct ipasfrag *)fp) 2724640Swnj m_cat(m, dtom(q)); 2734495Swnj 2744640Swnj /* 2754640Swnj * Create header for new ip packet by 2764640Swnj * modifying header of first packet; 2774640Swnj * dequeue and discard fragment reassembly header. 2784640Swnj * Make header visible. 2794640Swnj */ 2804640Swnj ip = fp->ipq_next; 2814640Swnj ip->ip_len = next; 2824898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 2834898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 2844640Swnj remque(fp); 2854907Swnj (void) m_free(dtom(fp)); 2864640Swnj m = dtom(ip); 2874898Swnj m->m_len += sizeof (struct ipasfrag); 2884898Swnj m->m_off -= sizeof (struct ipasfrag); 2894898Swnj return ((struct ip *)ip); 2904495Swnj 2914640Swnj dropfrag: 2924640Swnj m_freem(m); 2934640Swnj return (0); 2944495Swnj } 2954495Swnj 2964640Swnj /* 2974640Swnj * Free a fragment reassembly header and all 2984640Swnj * associated datagrams. 2994640Swnj */ 3004640Swnj struct ipq * 3014640Swnj ip_freef(fp) 3024640Swnj struct ipq *fp; 3034495Swnj { 3044898Swnj register struct ipasfrag *q; 3054640Swnj struct mbuf *m; 3064951Swnj COUNT(IP_FREEF); 3074495Swnj 3084898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3094640Swnj m_freem(dtom(q)); 3104640Swnj m = dtom(fp); 3114640Swnj fp = fp->next; 3124640Swnj remque(fp->prev); 3134907Swnj (void) m_free(m); 3144640Swnj return (fp); 3154495Swnj } 3164495Swnj 3174640Swnj /* 3184640Swnj * Put an ip fragment on a reassembly chain. 3194640Swnj * Like insque, but pointers in middle of structure. 3204640Swnj */ 3214640Swnj ip_enq(p, prev) 3224898Swnj register struct ipasfrag *p, *prev; 3234495Swnj { 3244951Swnj 3254640Swnj COUNT(IP_ENQ); 3264898Swnj p->ipf_prev = prev; 3274898Swnj p->ipf_next = prev->ipf_next; 3284898Swnj prev->ipf_next->ipf_prev = p; 3294898Swnj prev->ipf_next = p; 3304495Swnj } 3314495Swnj 3324640Swnj /* 3334640Swnj * To ip_enq as remque is to insque. 3344640Swnj */ 3354640Swnj ip_deq(p) 3364898Swnj register struct ipasfrag *p; 3374640Swnj { 3384951Swnj 3394640Swnj COUNT(IP_DEQ); 3404898Swnj p->ipf_prev->ipf_next = p->ipf_next; 3414898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3424495Swnj } 3434495Swnj 3444640Swnj /* 3454640Swnj * IP timer processing; 3464640Swnj * if a timer expires on a reassembly 3474640Swnj * queue, discard it. 3484640Swnj */ 3494801Swnj ip_slowtimo() 3504495Swnj { 3514495Swnj register struct ipq *fp; 3524640Swnj int s = splnet(); 3534951Swnj 3544801Swnj COUNT(IP_SLOWTIMO); 3554644Swnj for (fp = ipq.next; fp != &ipq; ) 3564640Swnj if (--fp->ipq_ttl == 0) 3574640Swnj fp = ip_freef(fp); 3584640Swnj else 3594640Swnj fp = fp->next; 3604640Swnj splx(s); 3614495Swnj } 3624495Swnj 3634951Swnj /* 3644951Swnj * Drain off all datagram fragments. 3654951Swnj */ 3664801Swnj ip_drain() 3674801Swnj { 3684801Swnj 3694951Swnj COUNT(IP_DRAIN); 3704951Swnj while (ipq.next != &ipq) 3714951Swnj (void) ip_freef(ipq.next); 3724801Swnj } 3734923Swnj 3744640Swnj /* 3754640Swnj * Do option processing on a datagram, 3764640Swnj * possibly discarding it if bad options 3774640Swnj * are encountered. 3784640Swnj */ 3794640Swnj ip_dooptions(ip) 3804640Swnj struct ip *ip; 3814495Swnj { 3824640Swnj register u_char *cp; 3834907Swnj int opt, optlen, cnt; 3844923Swnj struct in_addr *sin; 3854801Swnj register struct ip_timestamp *ipt; 3864951Swnj register struct ifnet *ifp; 3874951Swnj struct in_addr t; 3884495Swnj 3894951Swnj COUNT(IP_DOOPTIONS); 3904640Swnj cp = (u_char *)(ip + 1); 3914640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 3924640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 3934640Swnj opt = cp[0]; 3944640Swnj if (opt == IPOPT_EOL) 3954640Swnj break; 3964640Swnj if (opt == IPOPT_NOP) 3974640Swnj optlen = 1; 3984640Swnj else 3994640Swnj optlen = cp[1]; 4004640Swnj switch (opt) { 4014495Swnj 4024640Swnj default: 4034640Swnj break; 4044495Swnj 4054951Swnj /* 4064951Swnj * Source routing with record. 4074951Swnj * Find interface with current destination address. 4084951Swnj * If none on this machine then drop if strictly routed, 4094951Swnj * or do nothing if loosely routed. 4104951Swnj * Record interface address and bring up next address 4114951Swnj * component. If strictly routed make sure next 4124951Swnj * address on directly accessible net. 4134951Swnj */ 4144640Swnj case IPOPT_LSRR: 4154801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 4164640Swnj break; 4174923Swnj sin = (struct in_addr *)(cp + cp[2]); 4184951Swnj ifp = if_ifwithaddr(*sin); 4194951Swnj if (ifp == 0) { 4204951Swnj if (opt == IPOPT_SSRR) 4214951Swnj goto bad; 4224951Swnj break; 4234640Swnj } 4244951Swnj t = ip->ip_dst; ip->ip_dst = *sin; *sin = t; 4254951Swnj cp[2] += 4; 4264951Swnj if (cp[2] > optlen - (sizeof (long) - 1)) 4274951Swnj break; 4284951Swnj ip->ip_dst = sin[1]; 4294951Swnj if (opt == IPOPT_SSRR && if_ifonnetof(ip->ip_dst)==0) 4304951Swnj goto bad; 4314640Swnj break; 4324495Swnj 4334640Swnj case IPOPT_TS: 4344801Swnj ipt = (struct ip_timestamp *)cp; 4354801Swnj if (ipt->ipt_len < 5) 4364640Swnj goto bad; 4374801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 4384801Swnj if (++ipt->ipt_oflw == 0) 4394640Swnj goto bad; 4404495Swnj break; 4414640Swnj } 4424923Swnj sin = (struct in_addr *)(cp+cp[2]); 4434801Swnj switch (ipt->ipt_flg) { 4444495Swnj 4454640Swnj case IPOPT_TS_TSONLY: 4464640Swnj break; 4474640Swnj 4484640Swnj case IPOPT_TS_TSANDADDR: 4494801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4504640Swnj goto bad; 4514951Swnj /* stamp with ``first'' interface address */ 4524951Swnj *sin++ = ifnet->if_addr; 4534640Swnj break; 4544640Swnj 4554640Swnj case IPOPT_TS_PRESPEC: 4564951Swnj if (if_ifwithaddr(*sin) == 0) 4574951Swnj continue; 4584801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4594640Swnj goto bad; 4604801Swnj ipt->ipt_ptr += 4; 4614640Swnj break; 4624640Swnj 4634495Swnj default: 4644640Swnj goto bad; 4654495Swnj } 4664923Swnj *(n_time *)sin = iptime(); 4674801Swnj ipt->ipt_ptr += 4; 4684640Swnj } 4694495Swnj } 4704907Swnj return; 4714640Swnj bad: 4724640Swnj /* SHOULD FORCE ICMP MESSAGE */ 4734907Swnj return; 4744495Swnj } 4754495Swnj 4764640Swnj /* 4774951Swnj * Strip out IP options, at higher 4784951Swnj * level protocol in the kernel. 4794951Swnj * Second argument is buffer to which options 4804951Swnj * will be moved, and return value is their length. 4814640Swnj */ 4824951Swnj ip_stripoptions(ip, cp) 4834640Swnj struct ip *ip; 4844951Swnj char *cp; 4854495Swnj { 4864640Swnj register int i; 4874640Swnj register struct mbuf *m; 4884640Swnj int olen; 4894951Swnj COUNT(IP_STRIPOPTIONS); 4904640Swnj 4914640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 4924951Swnj m = dtom(ip); 4934951Swnj ip++; 4944951Swnj if (cp) 4954951Swnj bcopy((caddr_t)ip, cp, (unsigned)olen); 4964640Swnj i = m->m_len - (sizeof (struct ip) + olen); 4974907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 4984640Swnj m->m_len -= i; 4994495Swnj } 500