1*4907Swnj /* ip_input.c 1.14 81/11/16 */ 24571Swnj 34495Swnj #include "../h/param.h" 44543Swnj #include "../h/systm.h" 54640Swnj #include "../h/clock.h" 64640Swnj #include "../h/mbuf.h" 74898Swnj #include "../h/protocol.h" 84898Swnj #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 */ 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 284898Swnj pr = pffindproto(PF_INET, IPPROTO_RAW); 294898Swnj if (pr == 0) 304898Swnj panic("ip_init"); 314898Swnj for (i = 0; i < IPPROTO_MAX; i++) 324898Swnj ip_protox[i] = pr - protosw; 334898Swnj for (pr = protosw; pr <= protoswLAST; pr++) 344898Swnj if (pr->pr_family == PF_INET && 354898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 364898Swnj ip_protox[pr->pr_protocol] = pr - protosw; 374801Swnj ipq.next = ipq.prev = &ipq; 384801Swnj ip_id = time & 0xffff; 394801Swnj } 404801Swnj 414898Swnj u_char ipcksum = 1; 424640Swnj struct ip *ip_reass(); 434640Swnj 444640Swnj /* 454640Swnj * Ip input routines. 464640Swnj */ 474640Swnj 484640Swnj /* 494640Swnj * Ip input routine. Checksum and byte swap header. If fragmented 504640Swnj * try to reassamble. If complete and fragment queue exists, discard. 514640Swnj * Process options. Pass to next level. 524640Swnj */ 534640Swnj ip_input(m0) 544640Swnj struct mbuf *m0; 554495Swnj { 564689Swnj register struct ip *ip; /* known to be r11 in CKSUM below */ 574689Swnj register struct mbuf *m = m0; 584640Swnj register int i; 594495Swnj register struct ipq *fp; 604495Swnj int hlen; 614495Swnj 624495Swnj COUNT(IP_INPUT); 634640Swnj /* 644640Swnj * Check header and byteswap. 654640Swnj */ 664640Swnj ip = mtod(m, struct ip *); 674640Swnj if ((hlen = ip->ip_hl << 2) > m->m_len) { 684640Swnj printf("ip hdr ovflo\n"); 694640Swnj m_freem(m); 704495Swnj return; 714495Swnj } 724689Swnj CKSUM_IPCHK(m, ip, r11, hlen); 734689Swnj if (ip->ip_sum) { 744689Swnj printf("ip_sum %x\n", ip->ip_sum); 754495Swnj netstat.ip_badsum++; 764801Swnj if (ipcksum) { 774640Swnj m_freem(m); 784640Swnj return; 794495Swnj } 804495Swnj } 81*4907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 824640Swnj ip->ip_id = ntohs(ip->ip_id); 834640Swnj ip->ip_off = ntohs(ip->ip_off); 844495Swnj 854543Swnj /* 864640Swnj * Check that the amount of data in the buffers 874640Swnj * is as at least much as the IP header would have us expect. 884640Swnj * Trim mbufs if longer than we expect. 894640Swnj * Drop packet if shorter than we expect. 904543Swnj */ 914640Swnj i = 0; 924640Swnj for (; m != NULL; m = m->m_next) 934495Swnj i += m->m_len; 944640Swnj m = m0; 954640Swnj if (i != ip->ip_len) { 964640Swnj if (i < ip->ip_len) { 974640Swnj printf("ip_input: short packet\n"); 984640Swnj m_freem(m); 994640Swnj return; 1004640Swnj } 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)) 109*4907Swnj ip_dooptions(ip); 1104640Swnj if (ip->ip_dst.s_addr != n_lhost.s_addr) { 1114640Swnj if (--ip->ip_ttl == 0) { 112*4907Swnj icmp_error(ip, ICMP_TIMXCEED, 0); 1134543Swnj return; 1144495Swnj } 1154640Swnj ip_output(dtom(ip)); 1164640Swnj return; 1174543Swnj } 1184495Swnj 1194640Swnj /* 1204640Swnj * Look for queue of fragments 1214640Swnj * of this datagram. 1224640Swnj */ 1234640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1244640Swnj if (ip->ip_id == fp->ipq_id && 1254640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1264640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1274640Swnj ip->ip_p == fp->ipq_p) 1284640Swnj goto found; 1294640Swnj fp = 0; 1304640Swnj found: 1314495Swnj 1324640Swnj /* 1334640Swnj * Adjust ip_len to not reflect header, 1344640Swnj * set ip_mff if more fragments are expected, 1354640Swnj * convert offset of this to bytes. 1364640Swnj */ 1374640Swnj ip->ip_len -= hlen; 1384898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 1394640Swnj if (ip->ip_off & IP_MF) 1404898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 1414640Swnj ip->ip_off <<= 3; 1424495Swnj 1434640Swnj /* 1444640Swnj * If datagram marked as having more fragments 1454640Swnj * or if this is not the first fragment, 1464640Swnj * attempt reassembly; if it succeeds, proceed. 1474640Swnj */ 1484898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 1494898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 1504640Swnj if (ip == 0) 1514640Swnj return; 1524640Swnj hlen = ip->ip_hl << 2; 1534640Swnj m = dtom(ip); 1544640Swnj } else 1554640Swnj if (fp) 1564640Swnj (void) ip_freef(fp); 1574898Swnj (*protosw[ip_protox[ip->ip_p]].pr_input)(m); 1584640Swnj } 1594495Swnj 1604640Swnj /* 1614640Swnj * Take incoming datagram fragment and try to 1624640Swnj * reassamble it into whole datagram. If a chain for 1634640Swnj * reassembly of this datagram already exists, then it 1644640Swnj * is given as fp; otherwise have to make a chain. 1654640Swnj */ 1664640Swnj struct ip * 1674640Swnj ip_reass(ip, fp) 1684898Swnj register struct ipasfrag *ip; 1694640Swnj register struct ipq *fp; 1704640Swnj { 1714640Swnj register struct mbuf *m = dtom(ip); 1724898Swnj register struct ipasfrag *q; 1734640Swnj struct mbuf *t; 1744640Swnj int hlen = ip->ip_hl << 2; 1754640Swnj int i, next; 1764543Swnj 1774640Swnj /* 1784640Swnj * Presence of header sizes in mbufs 1794640Swnj * would confuse code below. 1804640Swnj */ 1814640Swnj m->m_off += hlen; 1824640Swnj m->m_len -= hlen; 1834495Swnj 1844640Swnj /* 1854640Swnj * If first fragment to arrive, create a reassembly queue. 1864640Swnj */ 1874640Swnj if (fp == 0) { 1884640Swnj if ((t = m_get(1)) == NULL) 1894640Swnj goto dropfrag; 1904640Swnj t->m_off = MMINOFF; 1914640Swnj fp = mtod(t, struct ipq *); 1924640Swnj insque(fp, &ipq); 1934640Swnj fp->ipq_ttl = IPFRAGTTL; 1944640Swnj fp->ipq_p = ip->ip_p; 1954640Swnj fp->ipq_id = ip->ip_id; 1964898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 1974898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 1984898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 1994640Swnj } 2004495Swnj 2014640Swnj /* 2024640Swnj * Find a segment which begins after this one does. 2034640Swnj */ 2044898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2054640Swnj if (q->ip_off > ip->ip_off) 2064640Swnj break; 2074495Swnj 2084640Swnj /* 2094640Swnj * If there is a preceding segment, it may provide some of 2104640Swnj * our data already. If so, drop the data from the incoming 2114640Swnj * segment. If it provides all of our data, drop us. 2124640Swnj */ 2134898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 2144898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2154640Swnj if (i > 0) { 2164640Swnj if (i >= ip->ip_len) 2174640Swnj goto dropfrag; 2184640Swnj m_adj(dtom(ip), i); 2194640Swnj ip->ip_off += i; 2204640Swnj ip->ip_len -= i; 2214640Swnj } 2224640Swnj } 2234543Swnj 2244640Swnj /* 2254640Swnj * While we overlap succeeding segments trim them or, 2264640Swnj * if they are completely covered, dequeue them. 2274640Swnj */ 2284898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 2294640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 2304640Swnj if (i < q->ip_len) { 2314640Swnj q->ip_len -= i; 2324640Swnj m_adj(dtom(q), i); 2334640Swnj break; 2344495Swnj } 2354898Swnj q = q->ipf_next; 2364898Swnj m_freem(dtom(q->ipf_prev)); 2374898Swnj ip_deq(q->ipf_prev); 2384543Swnj } 2394495Swnj 2404640Swnj /* 2414640Swnj * Stick new segment in its place; 2424640Swnj * check for complete reassembly. 2434640Swnj */ 2444898Swnj ip_enq(ip, q->ipf_prev); 2454640Swnj next = 0; 2464898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 2474640Swnj if (q->ip_off != next) 2484640Swnj return (0); 2494640Swnj next += q->ip_len; 2504640Swnj } 2514898Swnj if (q->ipf_prev->ipf_mff) 2524640Swnj return (0); 2534495Swnj 2544640Swnj /* 2554640Swnj * Reassembly is complete; concatenate fragments. 2564640Swnj */ 2574640Swnj q = fp->ipq_next; 2584640Swnj m = dtom(q); 2594640Swnj t = m->m_next; 2604640Swnj m->m_next = 0; 2614640Swnj m_cat(m, t); 2624898Swnj while ((q = q->ipf_next) != (struct ipasfrag *)fp) 2634640Swnj m_cat(m, dtom(q)); 2644495Swnj 2654640Swnj /* 2664640Swnj * Create header for new ip packet by 2674640Swnj * modifying header of first packet; 2684640Swnj * dequeue and discard fragment reassembly header. 2694640Swnj * Make header visible. 2704640Swnj */ 2714640Swnj ip = fp->ipq_next; 2724640Swnj ip->ip_len = next; 2734898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 2744898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 2754640Swnj remque(fp); 276*4907Swnj (void) m_free(dtom(fp)); 2774640Swnj m = dtom(ip); 2784898Swnj m->m_len += sizeof (struct ipasfrag); 2794898Swnj m->m_off -= sizeof (struct ipasfrag); 2804898Swnj return ((struct ip *)ip); 2814495Swnj 2824640Swnj dropfrag: 2834640Swnj m_freem(m); 2844640Swnj return (0); 2854495Swnj } 2864495Swnj 2874640Swnj /* 2884640Swnj * Free a fragment reassembly header and all 2894640Swnj * associated datagrams. 2904640Swnj */ 2914640Swnj struct ipq * 2924640Swnj ip_freef(fp) 2934640Swnj struct ipq *fp; 2944495Swnj { 2954898Swnj register struct ipasfrag *q; 2964640Swnj struct mbuf *m; 2974495Swnj 2984898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2994640Swnj m_freem(dtom(q)); 3004640Swnj m = dtom(fp); 3014640Swnj fp = fp->next; 3024640Swnj remque(fp->prev); 303*4907Swnj (void) m_free(m); 3044640Swnj return (fp); 3054495Swnj } 3064495Swnj 3074640Swnj /* 3084640Swnj * Put an ip fragment on a reassembly chain. 3094640Swnj * Like insque, but pointers in middle of structure. 3104640Swnj */ 3114640Swnj ip_enq(p, prev) 3124898Swnj register struct ipasfrag *p, *prev; 3134495Swnj { 3144640Swnj COUNT(IP_ENQ); 3154495Swnj 3164898Swnj p->ipf_prev = prev; 3174898Swnj p->ipf_next = prev->ipf_next; 3184898Swnj prev->ipf_next->ipf_prev = p; 3194898Swnj prev->ipf_next = p; 3204495Swnj } 3214495Swnj 3224640Swnj /* 3234640Swnj * To ip_enq as remque is to insque. 3244640Swnj */ 3254640Swnj ip_deq(p) 3264898Swnj register struct ipasfrag *p; 3274640Swnj { 3284640Swnj COUNT(IP_DEQ); 3294495Swnj 3304898Swnj p->ipf_prev->ipf_next = p->ipf_next; 3314898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3324495Swnj } 3334495Swnj 3344640Swnj /* 3354640Swnj * IP timer processing; 3364640Swnj * if a timer expires on a reassembly 3374640Swnj * queue, discard it. 3384640Swnj */ 3394801Swnj ip_slowtimo() 3404495Swnj { 3414495Swnj register struct ipq *fp; 3424640Swnj int s = splnet(); 3434801Swnj COUNT(IP_SLOWTIMO); 3444495Swnj 3454644Swnj for (fp = ipq.next; fp != &ipq; ) 3464640Swnj if (--fp->ipq_ttl == 0) 3474640Swnj fp = ip_freef(fp); 3484640Swnj else 3494640Swnj fp = fp->next; 3504640Swnj splx(s); 3514495Swnj } 3524495Swnj 3534801Swnj ip_drain() 3544801Swnj { 3554801Swnj 3564801Swnj } 3574640Swnj /* 3584640Swnj * Do option processing on a datagram, 3594640Swnj * possibly discarding it if bad options 3604640Swnj * are encountered. 3614640Swnj */ 3624640Swnj ip_dooptions(ip) 3634640Swnj struct ip *ip; 3644495Swnj { 3654640Swnj register u_char *cp; 366*4907Swnj int opt, optlen, cnt; 3674801Swnj struct ip_addr *sp; 3684801Swnj register struct ip_timestamp *ipt; 3694495Swnj 3704640Swnj cp = (u_char *)(ip + 1); 3714640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 3724640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 3734640Swnj opt = cp[0]; 3744640Swnj if (opt == IPOPT_EOL) 3754640Swnj break; 3764640Swnj if (opt == IPOPT_NOP) 3774640Swnj optlen = 1; 3784640Swnj else 3794640Swnj optlen = cp[1]; 3804640Swnj switch (opt) { 3814495Swnj 3824640Swnj default: 3834640Swnj break; 3844495Swnj 3854640Swnj case IPOPT_LSRR: 3864640Swnj case IPOPT_SSRR: 3874801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 3884640Swnj break; 3894801Swnj sp = (struct ip_addr *)(cp + cp[2]); 3904640Swnj if (n_lhost.s_addr == *(u_long *)sp) { 3914640Swnj if (opt == IPOPT_SSRR) { 3924801Swnj /* MAKE SURE *SP DIRECTLY ACCESSIBLE */ 3934640Swnj } 3944640Swnj ip->ip_dst = *sp; 3954640Swnj *sp = n_lhost; 3964640Swnj cp[2] += 4; 3974640Swnj } 3984640Swnj break; 3994495Swnj 4004640Swnj case IPOPT_TS: 4014801Swnj ipt = (struct ip_timestamp *)cp; 4024801Swnj if (ipt->ipt_len < 5) 4034640Swnj goto bad; 4044801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 4054801Swnj if (++ipt->ipt_oflw == 0) 4064640Swnj goto bad; 4074495Swnj break; 4084640Swnj } 4094801Swnj sp = (struct ip_addr *)(cp+cp[2]); 4104801Swnj switch (ipt->ipt_flg) { 4114495Swnj 4124640Swnj case IPOPT_TS_TSONLY: 4134640Swnj break; 4144640Swnj 4154640Swnj case IPOPT_TS_TSANDADDR: 4164801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4174640Swnj goto bad; 4184801Swnj *(struct ip_addr *)sp++ = n_lhost; 4194640Swnj break; 4204640Swnj 4214640Swnj case IPOPT_TS_PRESPEC: 4224640Swnj if (*(u_long *)sp != n_lhost.s_addr) 4234640Swnj break; 4244801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4254640Swnj goto bad; 4264801Swnj ipt->ipt_ptr += 4; 4274640Swnj break; 4284640Swnj 4294495Swnj default: 4304640Swnj goto bad; 4314495Swnj } 4324801Swnj *(n_time *)sp = ip_time(); 4334801Swnj ipt->ipt_ptr += 4; 4344640Swnj } 4354495Swnj } 436*4907Swnj return; 4374640Swnj bad: 4384640Swnj /* SHOULD FORCE ICMP MESSAGE */ 439*4907Swnj return; 4404495Swnj } 4414495Swnj 4424640Swnj /* 4434640Swnj * Strip out IP options, e.g. before passing 4444640Swnj * to higher level protocol in the kernel. 4454640Swnj */ 4464640Swnj ip_stripoptions(ip) 4474640Swnj struct ip *ip; 4484495Swnj { 4494640Swnj register int i; 4504640Swnj register struct mbuf *m; 4514640Swnj int olen; 4524640Swnj COUNT(IP_OPT); 4534640Swnj 4544640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 4554640Swnj m = dtom(++ip); 4564640Swnj i = m->m_len - (sizeof (struct ip) + olen); 457*4907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 4584640Swnj m->m_len -= i; 4594495Swnj } 460