1*10142Ssam /* ip_input.c 1.61 83/01/04 */ 24571Swnj 34495Swnj #include "../h/param.h" 44543Swnj #include "../h/systm.h" 54640Swnj #include "../h/mbuf.h" 69030Sroot #include "../h/domain.h" 74898Swnj #include "../h/protosw.h" 84923Swnj #include "../h/socket.h" 98695Sroot #include <errno.h> 108775Sroot #include <time.h> 118833Sroot #include "../h/kernel.h" 128695Sroot 138695Sroot #include "../net/if.h" 148695Sroot #include "../net/route.h" 158399Swnj #include "../netinet/in.h" 168597Sroot #include "../netinet/in_pcb.h" 178399Swnj #include "../netinet/in_systm.h" 188695Sroot #include "../netinet/ip.h" 198399Swnj #include "../netinet/ip_var.h" 208399Swnj #include "../netinet/ip_icmp.h" 218399Swnj #include "../netinet/tcp.h" 224495Swnj 234898Swnj u_char ip_protox[IPPROTO_MAX]; 246210Swnj int ipqmaxlen = IFQ_MAXLEN; 256338Ssam struct ifnet *ifinet; /* first inet interface */ 264898Swnj 274801Swnj /* 285172Swnj * IP initialization: fill in IP protocol switch table. 295161Swnj * All protocols not implemented in kernel go to raw IP protocol handler. 304801Swnj */ 314801Swnj ip_init() 324801Swnj { 334898Swnj register struct protosw *pr; 344898Swnj register int i; 354495Swnj 364898Swnj pr = pffindproto(PF_INET, IPPROTO_RAW); 374898Swnj if (pr == 0) 384898Swnj panic("ip_init"); 394898Swnj for (i = 0; i < IPPROTO_MAX; i++) 409030Sroot ip_protox[i] = pr - inetsw; 419030Sroot for (pr = inetdomain.dom_protosw; 429030Sroot pr <= inetdomain.dom_protoswNPROTOSW; pr++) 434898Swnj if (pr->pr_family == PF_INET && 444898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 459030Sroot ip_protox[pr->pr_protocol] = pr - inetsw; 464801Swnj ipq.next = ipq.prev = &ipq; 478172Sroot ip_id = time.tv_sec & 0xffff; 486210Swnj ipintrq.ifq_maxlen = ipqmaxlen; 496338Ssam ifinet = if_ifwithaf(AF_INET); 504801Swnj } 514801Swnj 524898Swnj u_char ipcksum = 1; 534640Swnj struct ip *ip_reass(); 546338Ssam struct sockaddr_in ipaddr = { AF_INET }; 554640Swnj 564640Swnj /* 574640Swnj * Ip input routine. Checksum and byte swap header. If fragmented 584640Swnj * try to reassamble. If complete and fragment queue exists, discard. 594640Swnj * Process options. Pass to next level. 604640Swnj */ 615084Swnj ipintr() 624495Swnj { 634923Swnj register struct ip *ip; 645084Swnj register struct mbuf *m; 658597Sroot struct mbuf *m0; 664640Swnj register int i; 674495Swnj register struct ipq *fp; 685084Swnj int hlen, s; 694495Swnj 705084Swnj next: 714640Swnj /* 725084Swnj * Get next datagram off input queue and get IP header 735084Swnj * in first mbuf. 744640Swnj */ 755084Swnj s = splimp(); 765084Swnj IF_DEQUEUE(&ipintrq, m); 775084Swnj splx(s); 785218Swnj if (m == 0) 795084Swnj return; 805306Sroot if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) && 815306Sroot (m = m_pullup(m, sizeof (struct ip))) == 0) 825306Sroot return; 834640Swnj ip = mtod(m, struct ip *); 845161Swnj if ((hlen = ip->ip_hl << 2) > m->m_len) { 855306Sroot if ((m = m_pullup(m, hlen)) == 0) 865306Sroot return; 875161Swnj ip = mtod(m, struct ip *); 885161Swnj } 894951Swnj if (ipcksum) 905217Swnj if (ip->ip_sum = in_cksum(m, hlen)) { 914951Swnj ipstat.ips_badsum++; 924951Swnj goto bad; 934495Swnj } 944951Swnj 954951Swnj /* 964951Swnj * Convert fields to host representation. 974951Swnj */ 984907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 994640Swnj ip->ip_id = ntohs(ip->ip_id); 1004951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 1014495Swnj 1024543Swnj /* 1034640Swnj * Check that the amount of data in the buffers 1044640Swnj * is as at least much as the IP header would have us expect. 1054640Swnj * Trim mbufs if longer than we expect. 1064640Swnj * Drop packet if shorter than we expect. 1074543Swnj */ 1086475Sroot i = -ip->ip_len; 1095161Swnj m0 = m; 1106475Sroot for (;;) { 1114495Swnj i += m->m_len; 1126475Sroot if (m->m_next == 0) 1136475Sroot break; 1146475Sroot m = m->m_next; 1156088Sroot } 1166475Sroot if (i != 0) { 1176475Sroot if (i < 0) { 1185161Swnj ipstat.ips_tooshort++; 1194951Swnj goto bad; 1205161Swnj } 1216475Sroot if (i <= m->m_len) 1226475Sroot m->m_len -= i; 1236475Sroot else 1246475Sroot m_adj(m0, -i); 1254495Swnj } 1266475Sroot m = m0; 1274495Swnj 1284640Swnj /* 1294640Swnj * Process options and, if not destined for us, 1306583Ssam * ship it on. ip_dooptions returns 1 when an 1316583Ssam * error was detected (causing an icmp message 1326583Ssam * to be sent). 1334640Swnj */ 1346583Ssam if (hlen > sizeof (struct ip) && ip_dooptions(ip)) 1356583Ssam goto next; 1366210Swnj 1376338Ssam /* 1386350Ssam * Fast check on the first internet 1396350Ssam * interface in the list. 1406338Ssam */ 1416338Ssam if (ifinet) { 1426338Ssam struct sockaddr_in *sin; 1436338Ssam 1446338Ssam sin = (struct sockaddr_in *)&ifinet->if_addr; 1456338Ssam if (sin->sin_addr.s_addr == ip->ip_dst.s_addr) 1466338Ssam goto ours; 1476483Ssam sin = (struct sockaddr_in *)&ifinet->if_broadaddr; 1486350Ssam if ((ifinet->if_flags & IFF_BROADCAST) && 1496350Ssam sin->sin_addr.s_addr == ip->ip_dst.s_addr) 1506350Ssam goto ours; 1516338Ssam } 1526338Ssam ipaddr.sin_addr = ip->ip_dst; 1536338Ssam if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) { 1546583Ssam ip_forward(ip); 1555084Swnj goto next; 1564543Swnj } 1574495Swnj 1586338Ssam ours: 1594640Swnj /* 1604640Swnj * Look for queue of fragments 1614640Swnj * of this datagram. 1624640Swnj */ 1634640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1644640Swnj if (ip->ip_id == fp->ipq_id && 1654640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1664640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1674640Swnj ip->ip_p == fp->ipq_p) 1684640Swnj goto found; 1694640Swnj fp = 0; 1704640Swnj found: 1714495Swnj 1724640Swnj /* 1734640Swnj * Adjust ip_len to not reflect header, 1744640Swnj * set ip_mff if more fragments are expected, 1754640Swnj * convert offset of this to bytes. 1764640Swnj */ 1774640Swnj ip->ip_len -= hlen; 1784898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 1794640Swnj if (ip->ip_off & IP_MF) 1804898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 1814640Swnj ip->ip_off <<= 3; 1824495Swnj 1834640Swnj /* 1844640Swnj * If datagram marked as having more fragments 1854640Swnj * or if this is not the first fragment, 1864640Swnj * attempt reassembly; if it succeeds, proceed. 1874640Swnj */ 1884898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 1894898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 1904640Swnj if (ip == 0) 1915084Swnj goto next; 1924640Swnj hlen = ip->ip_hl << 2; 1934640Swnj m = dtom(ip); 1944640Swnj } else 1954640Swnj if (fp) 1964640Swnj (void) ip_freef(fp); 1974951Swnj 1984951Swnj /* 1994951Swnj * Switch out to protocol's input routine. 2004951Swnj */ 2019030Sroot (*inetsw[ip_protox[ip->ip_p]].pr_input)(m); 2025084Swnj goto next; 2034951Swnj bad: 2044951Swnj m_freem(m); 2055084Swnj goto next; 2064640Swnj } 2074495Swnj 2084640Swnj /* 2094640Swnj * Take incoming datagram fragment and try to 2104951Swnj * reassemble it into whole datagram. If a chain for 2114640Swnj * reassembly of this datagram already exists, then it 2124640Swnj * is given as fp; otherwise have to make a chain. 2134640Swnj */ 2144640Swnj struct ip * 2154640Swnj ip_reass(ip, fp) 2164898Swnj register struct ipasfrag *ip; 2174640Swnj register struct ipq *fp; 2184640Swnj { 2194640Swnj register struct mbuf *m = dtom(ip); 2204898Swnj register struct ipasfrag *q; 2214640Swnj struct mbuf *t; 2224640Swnj int hlen = ip->ip_hl << 2; 2234640Swnj int i, next; 2244543Swnj 2254640Swnj /* 2264640Swnj * Presence of header sizes in mbufs 2274640Swnj * would confuse code below. 2284640Swnj */ 2294640Swnj m->m_off += hlen; 2304640Swnj m->m_len -= hlen; 2314495Swnj 2324640Swnj /* 2334640Swnj * If first fragment to arrive, create a reassembly queue. 2344640Swnj */ 2354640Swnj if (fp == 0) { 2369641Ssam if ((t = m_get(M_WAIT, MT_FTABLE)) == NULL) 2374640Swnj goto dropfrag; 2384640Swnj fp = mtod(t, struct ipq *); 2394640Swnj insque(fp, &ipq); 2404640Swnj fp->ipq_ttl = IPFRAGTTL; 2414640Swnj fp->ipq_p = ip->ip_p; 2424640Swnj fp->ipq_id = ip->ip_id; 2434898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 2444898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 2454898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2465161Swnj q = (struct ipasfrag *)fp; 2475161Swnj goto insert; 2484640Swnj } 2494495Swnj 2504640Swnj /* 2514640Swnj * Find a segment which begins after this one does. 2524640Swnj */ 2534898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2544640Swnj if (q->ip_off > ip->ip_off) 2554640Swnj break; 2564495Swnj 2574640Swnj /* 2584640Swnj * If there is a preceding segment, it may provide some of 2594640Swnj * our data already. If so, drop the data from the incoming 2604640Swnj * segment. If it provides all of our data, drop us. 2614640Swnj */ 2624898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 2634898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2644640Swnj if (i > 0) { 2654640Swnj if (i >= ip->ip_len) 2664640Swnj goto dropfrag; 2674640Swnj m_adj(dtom(ip), i); 2684640Swnj ip->ip_off += i; 2694640Swnj ip->ip_len -= i; 2704640Swnj } 2714640Swnj } 2724543Swnj 2734640Swnj /* 2744640Swnj * While we overlap succeeding segments trim them or, 2754640Swnj * if they are completely covered, dequeue them. 2764640Swnj */ 2774898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 2784640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 2794640Swnj if (i < q->ip_len) { 2804640Swnj q->ip_len -= i; 2816256Sroot q->ip_off += i; 2824640Swnj m_adj(dtom(q), i); 2834640Swnj break; 2844495Swnj } 2854898Swnj q = q->ipf_next; 2864898Swnj m_freem(dtom(q->ipf_prev)); 2874898Swnj ip_deq(q->ipf_prev); 2884543Swnj } 2894495Swnj 2905161Swnj insert: 2914640Swnj /* 2924640Swnj * Stick new segment in its place; 2934640Swnj * check for complete reassembly. 2944640Swnj */ 2954898Swnj ip_enq(ip, q->ipf_prev); 2964640Swnj next = 0; 2974898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 2984640Swnj if (q->ip_off != next) 2994640Swnj return (0); 3004640Swnj next += q->ip_len; 3014640Swnj } 3024898Swnj if (q->ipf_prev->ipf_mff) 3034640Swnj return (0); 3044495Swnj 3054640Swnj /* 3064640Swnj * Reassembly is complete; concatenate fragments. 3074640Swnj */ 3084640Swnj q = fp->ipq_next; 3094640Swnj m = dtom(q); 3104640Swnj t = m->m_next; 3114640Swnj m->m_next = 0; 3124640Swnj m_cat(m, t); 3136298Swnj q = q->ipf_next; 3146298Swnj while (q != (struct ipasfrag *)fp) { 3156298Swnj t = dtom(q); 3166298Swnj q = q->ipf_next; 3176298Swnj m_cat(m, t); 3186298Swnj } 3194495Swnj 3204640Swnj /* 3214640Swnj * Create header for new ip packet by 3224640Swnj * modifying header of first packet; 3234640Swnj * dequeue and discard fragment reassembly header. 3244640Swnj * Make header visible. 3254640Swnj */ 3264640Swnj ip = fp->ipq_next; 3274640Swnj ip->ip_len = next; 3284898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 3294898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 3304640Swnj remque(fp); 3314907Swnj (void) m_free(dtom(fp)); 3324640Swnj m = dtom(ip); 3334898Swnj m->m_len += sizeof (struct ipasfrag); 3344898Swnj m->m_off -= sizeof (struct ipasfrag); 3354898Swnj return ((struct ip *)ip); 3364495Swnj 3374640Swnj dropfrag: 3384640Swnj m_freem(m); 3394640Swnj return (0); 3404495Swnj } 3414495Swnj 3424640Swnj /* 3434640Swnj * Free a fragment reassembly header and all 3444640Swnj * associated datagrams. 3454640Swnj */ 3464640Swnj struct ipq * 3474640Swnj ip_freef(fp) 3484640Swnj struct ipq *fp; 3494495Swnj { 3504898Swnj register struct ipasfrag *q; 3514640Swnj struct mbuf *m; 3524495Swnj 3534898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3544640Swnj m_freem(dtom(q)); 3554640Swnj m = dtom(fp); 3564640Swnj fp = fp->next; 3574640Swnj remque(fp->prev); 3584907Swnj (void) m_free(m); 3594640Swnj return (fp); 3604495Swnj } 3614495Swnj 3624640Swnj /* 3634640Swnj * Put an ip fragment on a reassembly chain. 3644640Swnj * Like insque, but pointers in middle of structure. 3654640Swnj */ 3664640Swnj ip_enq(p, prev) 3674898Swnj register struct ipasfrag *p, *prev; 3684495Swnj { 3694951Swnj 3704898Swnj p->ipf_prev = prev; 3714898Swnj p->ipf_next = prev->ipf_next; 3724898Swnj prev->ipf_next->ipf_prev = p; 3734898Swnj prev->ipf_next = p; 3744495Swnj } 3754495Swnj 3764640Swnj /* 3774640Swnj * To ip_enq as remque is to insque. 3784640Swnj */ 3794640Swnj ip_deq(p) 3804898Swnj register struct ipasfrag *p; 3814640Swnj { 3824951Swnj 3834898Swnj p->ipf_prev->ipf_next = p->ipf_next; 3844898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3854495Swnj } 3864495Swnj 3874640Swnj /* 3884640Swnj * IP timer processing; 3894640Swnj * if a timer expires on a reassembly 3904640Swnj * queue, discard it. 3914640Swnj */ 3924801Swnj ip_slowtimo() 3934495Swnj { 3944495Swnj register struct ipq *fp; 3954640Swnj int s = splnet(); 3964951Swnj 3975243Sroot fp = ipq.next; 3985243Sroot if (fp == 0) { 3995243Sroot splx(s); 4005243Sroot return; 4015243Sroot } 4025243Sroot while (fp != &ipq) 4034640Swnj if (--fp->ipq_ttl == 0) 4044640Swnj fp = ip_freef(fp); 4054640Swnj else 4064640Swnj fp = fp->next; 4074640Swnj splx(s); 4084495Swnj } 4094495Swnj 4104951Swnj /* 4114951Swnj * Drain off all datagram fragments. 4124951Swnj */ 4134801Swnj ip_drain() 4144801Swnj { 4154801Swnj 4164951Swnj while (ipq.next != &ipq) 4174951Swnj (void) ip_freef(ipq.next); 4184801Swnj } 4194923Swnj 4204640Swnj /* 4214640Swnj * Do option processing on a datagram, 4224640Swnj * possibly discarding it if bad options 4234640Swnj * are encountered. 4244640Swnj */ 4254640Swnj ip_dooptions(ip) 4264640Swnj struct ip *ip; 4274495Swnj { 4284640Swnj register u_char *cp; 4296583Ssam int opt, optlen, cnt, code, type; 4304923Swnj struct in_addr *sin; 4314801Swnj register struct ip_timestamp *ipt; 4324951Swnj register struct ifnet *ifp; 4334951Swnj struct in_addr t; 4344495Swnj 4354640Swnj cp = (u_char *)(ip + 1); 4364640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 4374640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 4384640Swnj opt = cp[0]; 4394640Swnj if (opt == IPOPT_EOL) 4404640Swnj break; 4414640Swnj if (opt == IPOPT_NOP) 4424640Swnj optlen = 1; 4434640Swnj else 4444640Swnj optlen = cp[1]; 4454640Swnj switch (opt) { 4464495Swnj 4474640Swnj default: 4484640Swnj break; 4494495Swnj 4504951Swnj /* 4514951Swnj * Source routing with record. 4524951Swnj * Find interface with current destination address. 4534951Swnj * If none on this machine then drop if strictly routed, 4544951Swnj * or do nothing if loosely routed. 4554951Swnj * Record interface address and bring up next address 4564951Swnj * component. If strictly routed make sure next 4574951Swnj * address on directly accessible net. 4584951Swnj */ 4594640Swnj case IPOPT_LSRR: 4607508Sroot case IPOPT_SSRR: 4614801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 4624640Swnj break; 4634923Swnj sin = (struct in_addr *)(cp + cp[2]); 4646338Ssam ipaddr.sin_addr = *sin; 4656338Ssam ifp = if_ifwithaddr((struct sockaddr *)&ipaddr); 4666583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_SRCFAIL; 4674951Swnj if (ifp == 0) { 4684951Swnj if (opt == IPOPT_SSRR) 4694951Swnj goto bad; 4704951Swnj break; 4714640Swnj } 4724951Swnj t = ip->ip_dst; ip->ip_dst = *sin; *sin = t; 4734951Swnj cp[2] += 4; 4744951Swnj if (cp[2] > optlen - (sizeof (long) - 1)) 4754951Swnj break; 4764951Swnj ip->ip_dst = sin[1]; 4776338Ssam if (opt == IPOPT_SSRR && 4787166Ssam if_ifonnetof(in_netof(ip->ip_dst)) == 0) 4794951Swnj goto bad; 4804640Swnj break; 4814495Swnj 4824640Swnj case IPOPT_TS: 4836583Ssam code = cp - (u_char *)ip; 4846583Ssam type = ICMP_PARAMPROB; 4854801Swnj ipt = (struct ip_timestamp *)cp; 4864801Swnj if (ipt->ipt_len < 5) 4874640Swnj goto bad; 4884801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 4894801Swnj if (++ipt->ipt_oflw == 0) 4904640Swnj goto bad; 4914495Swnj break; 4924640Swnj } 4934923Swnj sin = (struct in_addr *)(cp+cp[2]); 4944801Swnj switch (ipt->ipt_flg) { 4954495Swnj 4964640Swnj case IPOPT_TS_TSONLY: 4974640Swnj break; 4984640Swnj 4994640Swnj case IPOPT_TS_TSANDADDR: 5004801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5014640Swnj goto bad; 5026338Ssam if (ifinet == 0) 5036338Ssam goto bad; /* ??? */ 5046338Ssam *sin++ = ((struct sockaddr_in *)&ifinet->if_addr)->sin_addr; 5054640Swnj break; 5064640Swnj 5074640Swnj case IPOPT_TS_PRESPEC: 5086338Ssam ipaddr.sin_addr = *sin; 509*10142Ssam if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) 5104951Swnj continue; 5114801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5124640Swnj goto bad; 5134801Swnj ipt->ipt_ptr += 4; 5144640Swnj break; 5154640Swnj 5164495Swnj default: 5174640Swnj goto bad; 5184495Swnj } 5194923Swnj *(n_time *)sin = iptime(); 5204801Swnj ipt->ipt_ptr += 4; 5214640Swnj } 5224495Swnj } 5236583Ssam return (0); 5244640Swnj bad: 5256583Ssam icmp_error(ip, type, code); 5266583Ssam return (1); 5274495Swnj } 5284495Swnj 5294640Swnj /* 5304951Swnj * Strip out IP options, at higher 5314951Swnj * level protocol in the kernel. 5324951Swnj * Second argument is buffer to which options 5334951Swnj * will be moved, and return value is their length. 5344640Swnj */ 5355217Swnj ip_stripoptions(ip, mopt) 5364640Swnj struct ip *ip; 5375217Swnj struct mbuf *mopt; 5384495Swnj { 5394640Swnj register int i; 5404640Swnj register struct mbuf *m; 5414640Swnj int olen; 5424640Swnj 5434640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 5444951Swnj m = dtom(ip); 5454951Swnj ip++; 5465217Swnj if (mopt) { 5475217Swnj mopt->m_len = olen; 5485217Swnj mopt->m_off = MMINOFF; 5495217Swnj bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen); 5505217Swnj } 5514640Swnj i = m->m_len - (sizeof (struct ip) + olen); 5524907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 5535243Sroot m->m_len -= olen; 5544495Swnj } 5556583Ssam 5566590Ssam u_char inetctlerrmap[] = { 5576583Ssam ECONNABORTED, ECONNABORTED, 0, 0, 5586609Ssam 0, 0, 5596609Ssam EHOSTDOWN, EHOSTUNREACH, ENETUNREACH, EHOSTUNREACH, 5606583Ssam ECONNREFUSED, ECONNREFUSED, EMSGSIZE, 0, 5616583Ssam 0, 0, 0, 0 5626583Ssam }; 5636583Ssam 5646583Ssam ip_ctlinput(cmd, arg) 5656583Ssam int cmd; 5666583Ssam caddr_t arg; 5676583Ssam { 5688775Sroot struct in_addr *in; 5696590Ssam int tcp_abort(), udp_abort(); 5706583Ssam extern struct inpcb tcb, udb; 5716583Ssam 5726583Ssam if (cmd < 0 || cmd > PRC_NCMDS) 5736583Ssam return; 5746590Ssam if (inetctlerrmap[cmd] == 0) 5756583Ssam return; /* XXX */ 5766583Ssam if (cmd == PRC_IFDOWN) 5778775Sroot in = &((struct sockaddr_in *)arg)->sin_addr; 5786583Ssam else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH) 5798775Sroot in = (struct in_addr *)arg; 5806583Ssam else 5818775Sroot in = &((struct icmp *)arg)->icmp_ip.ip_dst; 5828597Sroot /* THIS IS VERY QUESTIONABLE, SHOULD HIT ALL PROTOCOLS */ 5838775Sroot in_pcbnotify(&tcb, in, (int)inetctlerrmap[cmd], tcp_abort); 5848775Sroot in_pcbnotify(&udb, in, (int)inetctlerrmap[cmd], udp_abort); 5856583Ssam } 5866583Ssam 5876583Ssam int ipprintfs = 0; 5886583Ssam int ipforwarding = 1; 5896583Ssam /* 5906583Ssam * Forward a packet. If some error occurs return the sender 5916583Ssam * and icmp packet. Note we can't always generate a meaningful 5926583Ssam * icmp message because icmp doesn't have a large enough repetoire 5936583Ssam * of codes and types. 5946583Ssam */ 5956583Ssam ip_forward(ip) 5966583Ssam register struct ip *ip; 5976583Ssam { 5986583Ssam register int error, type, code; 5996609Ssam struct mbuf *mopt, *mcopy; 6006583Ssam 6016583Ssam if (ipprintfs) 6026583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 6036583Ssam ip->ip_dst, ip->ip_ttl); 6046583Ssam if (ipforwarding == 0) { 6056583Ssam /* can't tell difference between net and host */ 6066583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_NET; 6076583Ssam goto sendicmp; 6086583Ssam } 6096583Ssam if (ip->ip_ttl < IPTTLDEC) { 6106583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 6116583Ssam goto sendicmp; 6126583Ssam } 6136583Ssam ip->ip_ttl -= IPTTLDEC; 6149641Ssam mopt = m_get(M_DONTWAIT, MT_DATA); 6156583Ssam if (mopt == 0) { 6166583Ssam m_freem(dtom(ip)); 6176583Ssam return; 6186583Ssam } 6196609Ssam 6206609Ssam /* 6216609Ssam * Save at most 64 bytes of the packet in case 6226609Ssam * we need to generate an ICMP message to the src. 6236609Ssam */ 6247843Sroot mcopy = m_copy(dtom(ip), 0, imin(ip->ip_len, 64)); 6256583Ssam ip_stripoptions(ip, mopt); 6266583Ssam 6276583Ssam /* last 0 here means no directed broadcast */ 6288637Sroot if ((error = ip_output(dtom(ip), mopt, (struct route *)0, 0)) == 0) { 6296609Ssam if (mcopy) 6306609Ssam m_freem(mcopy); 6316583Ssam return; 6326609Ssam } 6336609Ssam ip = mtod(mcopy, struct ip *); 6346609Ssam type = ICMP_UNREACH, code = 0; /* need ``undefined'' */ 6356609Ssam switch (error) { 6366609Ssam 6376609Ssam case ENETUNREACH: 6386609Ssam case ENETDOWN: 6396583Ssam code = ICMP_UNREACH_NET; 6406609Ssam break; 6416609Ssam 6426609Ssam case EMSGSIZE: 6436583Ssam code = ICMP_UNREACH_NEEDFRAG; 6446609Ssam break; 6456609Ssam 6466609Ssam case EPERM: 6476609Ssam code = ICMP_UNREACH_PORT; 6486609Ssam break; 6496609Ssam 6506609Ssam case ENOBUFS: 6516609Ssam type = ICMP_SOURCEQUENCH; 6526609Ssam break; 6536609Ssam 6546609Ssam case EHOSTDOWN: 6556609Ssam case EHOSTUNREACH: 6566609Ssam code = ICMP_UNREACH_HOST; 6576609Ssam break; 6586609Ssam } 6596583Ssam sendicmp: 6606583Ssam icmp_error(ip, type, code); 6616583Ssam } 662