1*4898Swnj /* ip_input.c 1.13 81/11/15 */ 24571Swnj 34495Swnj #include "../h/param.h" 44543Swnj #include "../h/systm.h" 54640Swnj #include "../h/clock.h" 64640Swnj #include "../h/mbuf.h" 7*4898Swnj #include "../h/protocol.h" 8*4898Swnj #include "../h/protosw.h" 94801Swnj #include "../net/inet_cksum.h" 104801Swnj #include "../net/inet.h" 114801Swnj #include "../net/inet_systm.h" 124801Swnj #include "../net/imp.h" 134801Swnj #include "../net/ip.h" /* belongs before inet.h */ 14*4898Swnj #include "../net/ip_var.h" 154801Swnj #include "../net/ip_icmp.h" 164801Swnj #include "../net/tcp.h" 174495Swnj 18*4898Swnj u_char ip_protox[IPPROTO_MAX]; 19*4898Swnj 204801Swnj /* 214801Swnj * Ip initialization. 224801Swnj */ 234801Swnj ip_init() 244801Swnj { 25*4898Swnj register struct protosw *pr; 26*4898Swnj register int i; 27*4898Swnj int raw; 284495Swnj 29*4898Swnj pr = pffindproto(PF_INET, IPPROTO_RAW); 30*4898Swnj if (pr == 0) 31*4898Swnj panic("ip_init"); 32*4898Swnj for (i = 0; i < IPPROTO_MAX; i++) 33*4898Swnj ip_protox[i] = pr - protosw; 34*4898Swnj for (pr = protosw; pr <= protoswLAST; pr++) 35*4898Swnj if (pr->pr_family == PF_INET && 36*4898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 37*4898Swnj ip_protox[pr->pr_protocol] = pr - protosw; 384801Swnj ipq.next = ipq.prev = &ipq; 394801Swnj ip_id = time & 0xffff; 404801Swnj } 414801Swnj 42*4898Swnj 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 { 574689Swnj register struct ip *ip; /* known to be r11 in CKSUM below */ 584689Swnj register struct mbuf *m = m0; 594640Swnj register int i; 604689Swnj register struct ipq *q; 614495Swnj register struct ipq *fp; 624495Swnj int hlen; 634495Swnj 644495Swnj COUNT(IP_INPUT); 654640Swnj /* 664640Swnj * Check header and byteswap. 674640Swnj */ 684640Swnj ip = mtod(m, struct ip *); 694640Swnj if ((hlen = ip->ip_hl << 2) > m->m_len) { 704640Swnj printf("ip hdr ovflo\n"); 714640Swnj m_freem(m); 724495Swnj return; 734495Swnj } 744689Swnj CKSUM_IPCHK(m, ip, r11, hlen); 754689Swnj if (ip->ip_sum) { 764689Swnj printf("ip_sum %x\n", ip->ip_sum); 774495Swnj netstat.ip_badsum++; 784801Swnj if (ipcksum) { 794640Swnj m_freem(m); 804640Swnj return; 814495Swnj } 824495Swnj } 834640Swnj ip->ip_len = ntohs(ip->ip_len); 844640Swnj ip->ip_id = ntohs(ip->ip_id); 854640Swnj ip->ip_off = ntohs(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 */ 934640Swnj i = 0; 944640Swnj for (; m != NULL; m = m->m_next) 954495Swnj i += m->m_len; 964640Swnj m = m0; 974640Swnj if (i != ip->ip_len) { 984640Swnj if (i < ip->ip_len) { 994640Swnj printf("ip_input: short packet\n"); 1004640Swnj m_freem(m); 1014640Swnj return; 1024640Swnj } 1034640Swnj m_adj(m, ip->ip_len - i); 1044495Swnj } 1054495Swnj 1064640Swnj /* 1074640Swnj * Process options and, if not destined for us, 1084640Swnj * ship it on. 1094640Swnj */ 1104543Swnj if (hlen > sizeof (struct ip)) 1114640Swnj ip_dooptions(ip, hlen); 1124640Swnj if (ip->ip_dst.s_addr != n_lhost.s_addr) { 1134640Swnj if (--ip->ip_ttl == 0) { 1144640Swnj icmp_error(ip, ICMP_TIMXCEED); 1154543Swnj return; 1164495Swnj } 1174640Swnj ip_output(dtom(ip)); 1184640Swnj return; 1194543Swnj } 1204495Swnj 1214640Swnj /* 1224640Swnj * Look for queue of fragments 1234640Swnj * of this datagram. 1244640Swnj */ 1254640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1264640Swnj if (ip->ip_id == fp->ipq_id && 1274640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1284640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1294640Swnj ip->ip_p == fp->ipq_p) 1304640Swnj goto found; 1314640Swnj fp = 0; 1324640Swnj found: 1334495Swnj 1344640Swnj /* 1354640Swnj * Adjust ip_len to not reflect header, 1364640Swnj * set ip_mff if more fragments are expected, 1374640Swnj * convert offset of this to bytes. 1384640Swnj */ 1394640Swnj ip->ip_len -= hlen; 140*4898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 1414640Swnj if (ip->ip_off & IP_MF) 142*4898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 1434640Swnj ip->ip_off <<= 3; 1444495Swnj 1454640Swnj /* 1464640Swnj * If datagram marked as having more fragments 1474640Swnj * or if this is not the first fragment, 1484640Swnj * attempt reassembly; if it succeeds, proceed. 1494640Swnj */ 150*4898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 151*4898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 1524640Swnj if (ip == 0) 1534640Swnj return; 1544640Swnj hlen = ip->ip_hl << 2; 1554640Swnj m = dtom(ip); 1564640Swnj } else 1574640Swnj if (fp) 1584640Swnj (void) ip_freef(fp); 159*4898Swnj (*protosw[ip_protox[ip->ip_p]].pr_input)(m); 1604640Swnj } 1614495Swnj 1624640Swnj /* 1634640Swnj * Take incoming datagram fragment and try to 1644640Swnj * reassamble it into whole datagram. If a chain for 1654640Swnj * reassembly of this datagram already exists, then it 1664640Swnj * is given as fp; otherwise have to make a chain. 1674640Swnj */ 1684640Swnj struct ip * 1694640Swnj ip_reass(ip, fp) 170*4898Swnj register struct ipasfrag *ip; 1714640Swnj register struct ipq *fp; 1724640Swnj { 1734640Swnj register struct mbuf *m = dtom(ip); 174*4898Swnj register struct ipasfrag *q; 1754640Swnj struct mbuf *t; 1764640Swnj int hlen = ip->ip_hl << 2; 1774640Swnj int i, next; 1784543Swnj 1794640Swnj /* 1804640Swnj * Presence of header sizes in mbufs 1814640Swnj * would confuse code below. 1824640Swnj */ 1834640Swnj m->m_off += hlen; 1844640Swnj m->m_len -= hlen; 1854495Swnj 1864640Swnj /* 1874640Swnj * If first fragment to arrive, create a reassembly queue. 1884640Swnj */ 1894640Swnj if (fp == 0) { 1904640Swnj if ((t = m_get(1)) == NULL) 1914640Swnj goto dropfrag; 1924640Swnj t->m_off = MMINOFF; 1934640Swnj fp = mtod(t, struct ipq *); 1944640Swnj insque(fp, &ipq); 1954640Swnj fp->ipq_ttl = IPFRAGTTL; 1964640Swnj fp->ipq_p = ip->ip_p; 1974640Swnj fp->ipq_id = ip->ip_id; 198*4898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 199*4898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 200*4898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2014640Swnj } 2024495Swnj 2034640Swnj /* 2044640Swnj * Find a segment which begins after this one does. 2054640Swnj */ 206*4898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2074640Swnj if (q->ip_off > ip->ip_off) 2084640Swnj break; 2094495Swnj 2104640Swnj /* 2114640Swnj * If there is a preceding segment, it may provide some of 2124640Swnj * our data already. If so, drop the data from the incoming 2134640Swnj * segment. If it provides all of our data, drop us. 2144640Swnj */ 215*4898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 216*4898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2174640Swnj if (i > 0) { 2184640Swnj if (i >= ip->ip_len) 2194640Swnj goto dropfrag; 2204640Swnj m_adj(dtom(ip), i); 2214640Swnj ip->ip_off += i; 2224640Swnj ip->ip_len -= i; 2234640Swnj } 2244640Swnj } 2254543Swnj 2264640Swnj /* 2274640Swnj * While we overlap succeeding segments trim them or, 2284640Swnj * if they are completely covered, dequeue them. 2294640Swnj */ 230*4898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 2314640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 2324640Swnj if (i < q->ip_len) { 2334640Swnj q->ip_len -= i; 2344640Swnj m_adj(dtom(q), i); 2354640Swnj break; 2364495Swnj } 237*4898Swnj q = q->ipf_next; 238*4898Swnj m_freem(dtom(q->ipf_prev)); 239*4898Swnj ip_deq(q->ipf_prev); 2404543Swnj } 2414495Swnj 2424640Swnj /* 2434640Swnj * Stick new segment in its place; 2444640Swnj * check for complete reassembly. 2454640Swnj */ 246*4898Swnj ip_enq(ip, q->ipf_prev); 2474640Swnj next = 0; 248*4898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 2494640Swnj if (q->ip_off != next) 2504640Swnj return (0); 2514640Swnj next += q->ip_len; 2524640Swnj } 253*4898Swnj if (q->ipf_prev->ipf_mff) 2544640Swnj return (0); 2554495Swnj 2564640Swnj /* 2574640Swnj * Reassembly is complete; concatenate fragments. 2584640Swnj */ 2594640Swnj q = fp->ipq_next; 2604640Swnj m = dtom(q); 2614640Swnj t = m->m_next; 2624640Swnj m->m_next = 0; 2634640Swnj m_cat(m, t); 264*4898Swnj while ((q = q->ipf_next) != (struct ipasfrag *)fp) 2654640Swnj m_cat(m, dtom(q)); 2664495Swnj 2674640Swnj /* 2684640Swnj * Create header for new ip packet by 2694640Swnj * modifying header of first packet; 2704640Swnj * dequeue and discard fragment reassembly header. 2714640Swnj * Make header visible. 2724640Swnj */ 2734640Swnj ip = fp->ipq_next; 2744640Swnj ip->ip_len = next; 275*4898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 276*4898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 2774640Swnj remque(fp); 2784640Swnj m_free(dtom(fp)); 2794640Swnj m = dtom(ip); 280*4898Swnj m->m_len += sizeof (struct ipasfrag); 281*4898Swnj m->m_off -= sizeof (struct ipasfrag); 282*4898Swnj return ((struct ip *)ip); 2834495Swnj 2844640Swnj dropfrag: 2854640Swnj m_freem(m); 2864640Swnj return (0); 2874495Swnj } 2884495Swnj 2894640Swnj /* 2904640Swnj * Free a fragment reassembly header and all 2914640Swnj * associated datagrams. 2924640Swnj */ 2934640Swnj struct ipq * 2944640Swnj ip_freef(fp) 2954640Swnj struct ipq *fp; 2964495Swnj { 297*4898Swnj register struct ipasfrag *q; 2984640Swnj struct mbuf *m; 2994495Swnj 300*4898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3014640Swnj m_freem(dtom(q)); 3024640Swnj m = dtom(fp); 3034640Swnj fp = fp->next; 3044640Swnj remque(fp->prev); 3054640Swnj m_free(m); 3064640Swnj return (fp); 3074495Swnj } 3084495Swnj 3094640Swnj /* 3104640Swnj * Put an ip fragment on a reassembly chain. 3114640Swnj * Like insque, but pointers in middle of structure. 3124640Swnj */ 3134640Swnj ip_enq(p, prev) 314*4898Swnj register struct ipasfrag *p, *prev; 3154495Swnj { 3164640Swnj COUNT(IP_ENQ); 3174495Swnj 318*4898Swnj p->ipf_prev = prev; 319*4898Swnj p->ipf_next = prev->ipf_next; 320*4898Swnj prev->ipf_next->ipf_prev = p; 321*4898Swnj prev->ipf_next = p; 3224495Swnj } 3234495Swnj 3244640Swnj /* 3254640Swnj * To ip_enq as remque is to insque. 3264640Swnj */ 3274640Swnj ip_deq(p) 328*4898Swnj register struct ipasfrag *p; 3294640Swnj { 3304640Swnj COUNT(IP_DEQ); 3314495Swnj 332*4898Swnj p->ipf_prev->ipf_next = p->ipf_next; 333*4898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3344495Swnj } 3354495Swnj 3364640Swnj /* 3374640Swnj * IP timer processing; 3384640Swnj * if a timer expires on a reassembly 3394640Swnj * queue, discard it. 3404640Swnj */ 3414801Swnj ip_slowtimo() 3424495Swnj { 3434495Swnj register struct ipq *fp; 3444640Swnj int s = splnet(); 3454801Swnj COUNT(IP_SLOWTIMO); 3464495Swnj 3474644Swnj for (fp = ipq.next; fp != &ipq; ) 3484640Swnj if (--fp->ipq_ttl == 0) 3494640Swnj fp = ip_freef(fp); 3504640Swnj else 3514640Swnj fp = fp->next; 3524640Swnj splx(s); 3534495Swnj } 3544495Swnj 3554801Swnj ip_drain() 3564801Swnj { 3574801Swnj 3584801Swnj } 3594640Swnj /* 3604640Swnj * Do option processing on a datagram, 3614640Swnj * possibly discarding it if bad options 3624640Swnj * are encountered. 3634640Swnj */ 3644640Swnj ip_dooptions(ip) 3654640Swnj struct ip *ip; 3664495Swnj { 3674640Swnj register u_char *cp; 3684640Swnj int opt, optlen, cnt, s; 3694801Swnj struct ip_addr *sp; 3704801Swnj register struct ip_timestamp *ipt; 3714801Swnj int x; 3724495Swnj 3734640Swnj cp = (u_char *)(ip + 1); 3744640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 3754640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 3764640Swnj opt = cp[0]; 3774640Swnj if (opt == IPOPT_EOL) 3784640Swnj break; 3794640Swnj if (opt == IPOPT_NOP) 3804640Swnj optlen = 1; 3814640Swnj else 3824640Swnj optlen = cp[1]; 3834640Swnj switch (opt) { 3844495Swnj 3854640Swnj default: 3864640Swnj break; 3874495Swnj 3884640Swnj case IPOPT_LSRR: 3894640Swnj case IPOPT_SSRR: 3904801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 3914640Swnj break; 3924801Swnj sp = (struct ip_addr *)(cp + cp[2]); 3934640Swnj if (n_lhost.s_addr == *(u_long *)sp) { 3944640Swnj if (opt == IPOPT_SSRR) { 3954801Swnj /* MAKE SURE *SP DIRECTLY ACCESSIBLE */ 3964640Swnj } 3974640Swnj ip->ip_dst = *sp; 3984640Swnj *sp = n_lhost; 3994640Swnj cp[2] += 4; 4004640Swnj } 4014640Swnj break; 4024495Swnj 4034640Swnj case IPOPT_TS: 4044801Swnj ipt = (struct ip_timestamp *)cp; 4054801Swnj if (ipt->ipt_len < 5) 4064640Swnj goto bad; 4074801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 4084801Swnj if (++ipt->ipt_oflw == 0) 4094640Swnj goto bad; 4104495Swnj break; 4114640Swnj } 4124801Swnj sp = (struct ip_addr *)(cp+cp[2]); 4134801Swnj switch (ipt->ipt_flg) { 4144495Swnj 4154640Swnj case IPOPT_TS_TSONLY: 4164640Swnj break; 4174640Swnj 4184640Swnj case IPOPT_TS_TSANDADDR: 4194801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4204640Swnj goto bad; 4214801Swnj *(struct ip_addr *)sp++ = n_lhost; 4224640Swnj break; 4234640Swnj 4244640Swnj case IPOPT_TS_PRESPEC: 4254640Swnj if (*(u_long *)sp != n_lhost.s_addr) 4264640Swnj break; 4274801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4284640Swnj goto bad; 4294801Swnj ipt->ipt_ptr += 4; 4304640Swnj break; 4314640Swnj 4324495Swnj default: 4334640Swnj goto bad; 4344495Swnj } 4354801Swnj *(n_time *)sp = ip_time(); 4364801Swnj ipt->ipt_ptr += 4; 4374640Swnj } 4384495Swnj } 4394640Swnj return (0); 4404640Swnj bad: 4414640Swnj /* SHOULD FORCE ICMP MESSAGE */ 4424640Swnj return (-1); 4434495Swnj } 4444495Swnj 4454640Swnj /* 4464640Swnj * Strip out IP options, e.g. before passing 4474640Swnj * to higher level protocol in the kernel. 4484640Swnj */ 4494640Swnj ip_stripoptions(ip) 4504640Swnj struct ip *ip; 4514495Swnj { 4524640Swnj register int i; 4534640Swnj register struct mbuf *m; 4544640Swnj char *op; 4554640Swnj int olen; 4564640Swnj COUNT(IP_OPT); 4574640Swnj 4584640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 4594640Swnj op = (caddr_t)ip + olen; 4604640Swnj m = dtom(++ip); 4614640Swnj i = m->m_len - (sizeof (struct ip) + olen); 4624640Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, i); 4634640Swnj m->m_len -= i; 4644495Swnj } 465