1*8399Swnj /* ip_input.c 1.51 82/10/09 */ 24571Swnj 34495Swnj #include "../h/param.h" 44543Swnj #include "../h/systm.h" 54640Swnj #include "../h/mbuf.h" 64898Swnj #include "../h/protosw.h" 74923Swnj #include "../h/socket.h" 8*8399Swnj #include "../netinet/in.h" 9*8399Swnj #include "../netinet/in_systm.h" 104951Swnj #include "../net/if.h" 11*8399Swnj #include "../netinet/ip.h" /* belongs before in.h */ 12*8399Swnj #include "../netinet/ip_var.h" 13*8399Swnj #include "../netinet/ip_icmp.h" 14*8399Swnj #include "../netinet/tcp.h" 158172Sroot #include <time.h> 168172Sroot #include "../h/kernel.h" 176583Ssam #include <errno.h> 184495Swnj 194898Swnj u_char ip_protox[IPPROTO_MAX]; 206210Swnj int ipqmaxlen = IFQ_MAXLEN; 216338Ssam struct ifnet *ifinet; /* first inet interface */ 224898Swnj 234801Swnj /* 245172Swnj * IP initialization: fill in IP protocol switch table. 255161Swnj * All protocols not implemented in kernel go to raw IP protocol handler. 264801Swnj */ 274801Swnj ip_init() 284801Swnj { 294898Swnj register struct protosw *pr; 304898Swnj register int i; 314495Swnj 324898Swnj pr = pffindproto(PF_INET, IPPROTO_RAW); 334898Swnj if (pr == 0) 344898Swnj panic("ip_init"); 354898Swnj for (i = 0; i < IPPROTO_MAX; i++) 364898Swnj ip_protox[i] = pr - protosw; 374898Swnj for (pr = protosw; pr <= protoswLAST; pr++) 384898Swnj if (pr->pr_family == PF_INET && 394898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 404898Swnj ip_protox[pr->pr_protocol] = pr - protosw; 414801Swnj ipq.next = ipq.prev = &ipq; 428172Sroot ip_id = time.tv_sec & 0xffff; 436210Swnj ipintrq.ifq_maxlen = ipqmaxlen; 446338Ssam ifinet = if_ifwithaf(AF_INET); 454801Swnj } 464801Swnj 474898Swnj u_char ipcksum = 1; 484640Swnj struct ip *ip_reass(); 496338Ssam struct sockaddr_in ipaddr = { AF_INET }; 504640Swnj 514640Swnj /* 524640Swnj * Ip input routine. Checksum and byte swap header. If fragmented 534640Swnj * try to reassamble. If complete and fragment queue exists, discard. 544640Swnj * Process options. Pass to next level. 554640Swnj */ 565084Swnj ipintr() 574495Swnj { 584923Swnj register struct ip *ip; 595084Swnj register struct mbuf *m; 605217Swnj struct mbuf *m0, *mopt; 614640Swnj register int i; 624495Swnj register struct ipq *fp; 635084Swnj int hlen, s; 644495Swnj 655084Swnj next: 664640Swnj /* 675084Swnj * Get next datagram off input queue and get IP header 685084Swnj * in first mbuf. 694640Swnj */ 705084Swnj s = splimp(); 715084Swnj IF_DEQUEUE(&ipintrq, m); 725084Swnj splx(s); 735218Swnj if (m == 0) 745084Swnj return; 755306Sroot if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) && 765306Sroot (m = m_pullup(m, sizeof (struct ip))) == 0) 775306Sroot return; 784640Swnj ip = mtod(m, struct ip *); 795161Swnj if ((hlen = ip->ip_hl << 2) > m->m_len) { 805306Sroot if ((m = m_pullup(m, hlen)) == 0) 815306Sroot return; 825161Swnj ip = mtod(m, struct ip *); 835161Swnj } 844951Swnj if (ipcksum) 855217Swnj if (ip->ip_sum = in_cksum(m, hlen)) { 865161Swnj printf("ip_sum %x\n", ip->ip_sum); /* XXX */ 874951Swnj ipstat.ips_badsum++; 884951Swnj goto bad; 894495Swnj } 904951Swnj 915217Swnj #if vax 924951Swnj /* 934951Swnj * Convert fields to host representation. 944951Swnj */ 954907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 964640Swnj ip->ip_id = ntohs(ip->ip_id); 974951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 985217Swnj #endif 994495Swnj 1004543Swnj /* 1014640Swnj * Check that the amount of data in the buffers 1024640Swnj * is as at least much as the IP header would have us expect. 1034640Swnj * Trim mbufs if longer than we expect. 1044640Swnj * Drop packet if shorter than we expect. 1054543Swnj */ 1066475Sroot i = -ip->ip_len; 1075161Swnj m0 = m; 1086475Sroot for (;;) { 1094495Swnj i += m->m_len; 1106475Sroot if (m->m_next == 0) 1116475Sroot break; 1126475Sroot m = m->m_next; 1136088Sroot } 1146475Sroot if (i != 0) { 1156475Sroot if (i < 0) { 1165161Swnj ipstat.ips_tooshort++; 1174951Swnj goto bad; 1185161Swnj } 1196475Sroot if (i <= m->m_len) 1206475Sroot m->m_len -= i; 1216475Sroot else 1226475Sroot m_adj(m0, -i); 1234495Swnj } 1246475Sroot m = m0; 1254495Swnj 1264640Swnj /* 1274640Swnj * Process options and, if not destined for us, 1286583Ssam * ship it on. ip_dooptions returns 1 when an 1296583Ssam * error was detected (causing an icmp message 1306583Ssam * to be sent). 1314640Swnj */ 1326583Ssam if (hlen > sizeof (struct ip) && ip_dooptions(ip)) 1336583Ssam goto next; 1346210Swnj 1356338Ssam /* 1366350Ssam * Fast check on the first internet 1376350Ssam * interface in the list. 1386338Ssam */ 1396338Ssam if (ifinet) { 1406338Ssam struct sockaddr_in *sin; 1416338Ssam 1426338Ssam sin = (struct sockaddr_in *)&ifinet->if_addr; 1436338Ssam if (sin->sin_addr.s_addr == ip->ip_dst.s_addr) 1446338Ssam goto ours; 1456483Ssam sin = (struct sockaddr_in *)&ifinet->if_broadaddr; 1466350Ssam if ((ifinet->if_flags & IFF_BROADCAST) && 1476350Ssam sin->sin_addr.s_addr == ip->ip_dst.s_addr) 1486350Ssam goto ours; 1496338Ssam } 1506338Ssam ipaddr.sin_addr = ip->ip_dst; 1516338Ssam if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) { 1526583Ssam ip_forward(ip); 1535084Swnj goto next; 1544543Swnj } 1554495Swnj 1566338Ssam ours: 1574640Swnj /* 1584640Swnj * Look for queue of fragments 1594640Swnj * of this datagram. 1604640Swnj */ 1614640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1624640Swnj if (ip->ip_id == fp->ipq_id && 1634640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1644640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1654640Swnj ip->ip_p == fp->ipq_p) 1664640Swnj goto found; 1674640Swnj fp = 0; 1684640Swnj found: 1694495Swnj 1704640Swnj /* 1714640Swnj * Adjust ip_len to not reflect header, 1724640Swnj * set ip_mff if more fragments are expected, 1734640Swnj * convert offset of this to bytes. 1744640Swnj */ 1754640Swnj ip->ip_len -= hlen; 1764898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 1774640Swnj if (ip->ip_off & IP_MF) 1784898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 1794640Swnj ip->ip_off <<= 3; 1804495Swnj 1814640Swnj /* 1824640Swnj * If datagram marked as having more fragments 1834640Swnj * or if this is not the first fragment, 1844640Swnj * attempt reassembly; if it succeeds, proceed. 1854640Swnj */ 1864898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 1874898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 1884640Swnj if (ip == 0) 1895084Swnj goto next; 1904640Swnj hlen = ip->ip_hl << 2; 1914640Swnj m = dtom(ip); 1924640Swnj } else 1934640Swnj if (fp) 1944640Swnj (void) ip_freef(fp); 1954951Swnj 1964951Swnj /* 1974951Swnj * Switch out to protocol's input routine. 1984951Swnj */ 1994898Swnj (*protosw[ip_protox[ip->ip_p]].pr_input)(m); 2005084Swnj goto next; 2014951Swnj bad: 2024951Swnj m_freem(m); 2035084Swnj goto next; 2044640Swnj } 2054495Swnj 2064640Swnj /* 2074640Swnj * Take incoming datagram fragment and try to 2084951Swnj * reassemble it into whole datagram. If a chain for 2094640Swnj * reassembly of this datagram already exists, then it 2104640Swnj * is given as fp; otherwise have to make a chain. 2114640Swnj */ 2124640Swnj struct ip * 2134640Swnj ip_reass(ip, fp) 2144898Swnj register struct ipasfrag *ip; 2154640Swnj register struct ipq *fp; 2164640Swnj { 2174640Swnj register struct mbuf *m = dtom(ip); 2184898Swnj register struct ipasfrag *q; 2194640Swnj struct mbuf *t; 2204640Swnj int hlen = ip->ip_hl << 2; 2214640Swnj int i, next; 2224543Swnj 2234640Swnj /* 2244640Swnj * Presence of header sizes in mbufs 2254640Swnj * would confuse code below. 2264640Swnj */ 2274640Swnj m->m_off += hlen; 2284640Swnj m->m_len -= hlen; 2294495Swnj 2304640Swnj /* 2314640Swnj * If first fragment to arrive, create a reassembly queue. 2324640Swnj */ 2334640Swnj if (fp == 0) { 2345851Sroot if ((t = m_get(M_WAIT)) == NULL) 2354640Swnj goto dropfrag; 2364640Swnj fp = mtod(t, struct ipq *); 2374640Swnj insque(fp, &ipq); 2384640Swnj fp->ipq_ttl = IPFRAGTTL; 2394640Swnj fp->ipq_p = ip->ip_p; 2404640Swnj fp->ipq_id = ip->ip_id; 2414898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 2424898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 2434898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2445161Swnj q = (struct ipasfrag *)fp; 2455161Swnj goto insert; 2464640Swnj } 2474495Swnj 2484640Swnj /* 2494640Swnj * Find a segment which begins after this one does. 2504640Swnj */ 2514898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2524640Swnj if (q->ip_off > ip->ip_off) 2534640Swnj break; 2544495Swnj 2554640Swnj /* 2564640Swnj * If there is a preceding segment, it may provide some of 2574640Swnj * our data already. If so, drop the data from the incoming 2584640Swnj * segment. If it provides all of our data, drop us. 2594640Swnj */ 2604898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 2614898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2624640Swnj if (i > 0) { 2634640Swnj if (i >= ip->ip_len) 2644640Swnj goto dropfrag; 2654640Swnj m_adj(dtom(ip), i); 2664640Swnj ip->ip_off += i; 2674640Swnj ip->ip_len -= i; 2684640Swnj } 2694640Swnj } 2704543Swnj 2714640Swnj /* 2724640Swnj * While we overlap succeeding segments trim them or, 2734640Swnj * if they are completely covered, dequeue them. 2744640Swnj */ 2754898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 2764640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 2774640Swnj if (i < q->ip_len) { 2784640Swnj q->ip_len -= i; 2796256Sroot q->ip_off += i; 2804640Swnj m_adj(dtom(q), i); 2814640Swnj break; 2824495Swnj } 2834898Swnj q = q->ipf_next; 2844898Swnj m_freem(dtom(q->ipf_prev)); 2854898Swnj ip_deq(q->ipf_prev); 2864543Swnj } 2874495Swnj 2885161Swnj insert: 2894640Swnj /* 2904640Swnj * Stick new segment in its place; 2914640Swnj * check for complete reassembly. 2924640Swnj */ 2934898Swnj ip_enq(ip, q->ipf_prev); 2944640Swnj next = 0; 2954898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 2964640Swnj if (q->ip_off != next) 2974640Swnj return (0); 2984640Swnj next += q->ip_len; 2994640Swnj } 3004898Swnj if (q->ipf_prev->ipf_mff) 3014640Swnj return (0); 3024495Swnj 3034640Swnj /* 3044640Swnj * Reassembly is complete; concatenate fragments. 3054640Swnj */ 3064640Swnj q = fp->ipq_next; 3074640Swnj m = dtom(q); 3084640Swnj t = m->m_next; 3094640Swnj m->m_next = 0; 3104640Swnj m_cat(m, t); 3116298Swnj q = q->ipf_next; 3126298Swnj while (q != (struct ipasfrag *)fp) { 3136298Swnj t = dtom(q); 3146298Swnj q = q->ipf_next; 3156298Swnj m_cat(m, t); 3166298Swnj } 3174495Swnj 3184640Swnj /* 3194640Swnj * Create header for new ip packet by 3204640Swnj * modifying header of first packet; 3214640Swnj * dequeue and discard fragment reassembly header. 3224640Swnj * Make header visible. 3234640Swnj */ 3244640Swnj ip = fp->ipq_next; 3254640Swnj ip->ip_len = next; 3264898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 3274898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 3284640Swnj remque(fp); 3294907Swnj (void) m_free(dtom(fp)); 3304640Swnj m = dtom(ip); 3314898Swnj m->m_len += sizeof (struct ipasfrag); 3324898Swnj m->m_off -= sizeof (struct ipasfrag); 3334898Swnj return ((struct ip *)ip); 3344495Swnj 3354640Swnj dropfrag: 3364640Swnj m_freem(m); 3374640Swnj return (0); 3384495Swnj } 3394495Swnj 3404640Swnj /* 3414640Swnj * Free a fragment reassembly header and all 3424640Swnj * associated datagrams. 3434640Swnj */ 3444640Swnj struct ipq * 3454640Swnj ip_freef(fp) 3464640Swnj struct ipq *fp; 3474495Swnj { 3484898Swnj register struct ipasfrag *q; 3494640Swnj struct mbuf *m; 3504495Swnj 3514898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3524640Swnj m_freem(dtom(q)); 3534640Swnj m = dtom(fp); 3544640Swnj fp = fp->next; 3554640Swnj remque(fp->prev); 3564907Swnj (void) m_free(m); 3574640Swnj return (fp); 3584495Swnj } 3594495Swnj 3604640Swnj /* 3614640Swnj * Put an ip fragment on a reassembly chain. 3624640Swnj * Like insque, but pointers in middle of structure. 3634640Swnj */ 3644640Swnj ip_enq(p, prev) 3654898Swnj register struct ipasfrag *p, *prev; 3664495Swnj { 3674951Swnj 3684898Swnj p->ipf_prev = prev; 3694898Swnj p->ipf_next = prev->ipf_next; 3704898Swnj prev->ipf_next->ipf_prev = p; 3714898Swnj prev->ipf_next = p; 3724495Swnj } 3734495Swnj 3744640Swnj /* 3754640Swnj * To ip_enq as remque is to insque. 3764640Swnj */ 3774640Swnj ip_deq(p) 3784898Swnj register struct ipasfrag *p; 3794640Swnj { 3804951Swnj 3814898Swnj p->ipf_prev->ipf_next = p->ipf_next; 3824898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3834495Swnj } 3844495Swnj 3854640Swnj /* 3864640Swnj * IP timer processing; 3874640Swnj * if a timer expires on a reassembly 3884640Swnj * queue, discard it. 3894640Swnj */ 3904801Swnj ip_slowtimo() 3914495Swnj { 3924495Swnj register struct ipq *fp; 3934640Swnj int s = splnet(); 3944951Swnj 3955243Sroot fp = ipq.next; 3965243Sroot if (fp == 0) { 3975243Sroot splx(s); 3985243Sroot return; 3995243Sroot } 4005243Sroot while (fp != &ipq) 4014640Swnj if (--fp->ipq_ttl == 0) 4024640Swnj fp = ip_freef(fp); 4034640Swnj else 4044640Swnj fp = fp->next; 4054640Swnj splx(s); 4064495Swnj } 4074495Swnj 4084951Swnj /* 4094951Swnj * Drain off all datagram fragments. 4104951Swnj */ 4114801Swnj ip_drain() 4124801Swnj { 4134801Swnj 4144951Swnj while (ipq.next != &ipq) 4154951Swnj (void) ip_freef(ipq.next); 4164801Swnj } 4174923Swnj 4184640Swnj /* 4194640Swnj * Do option processing on a datagram, 4204640Swnj * possibly discarding it if bad options 4214640Swnj * are encountered. 4224640Swnj */ 4234640Swnj ip_dooptions(ip) 4244640Swnj struct ip *ip; 4254495Swnj { 4264640Swnj register u_char *cp; 4276583Ssam int opt, optlen, cnt, code, type; 4284923Swnj struct in_addr *sin; 4294801Swnj register struct ip_timestamp *ipt; 4304951Swnj register struct ifnet *ifp; 4314951Swnj struct in_addr t; 4324495Swnj 4334640Swnj cp = (u_char *)(ip + 1); 4344640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 4354640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 4364640Swnj opt = cp[0]; 4374640Swnj if (opt == IPOPT_EOL) 4384640Swnj break; 4394640Swnj if (opt == IPOPT_NOP) 4404640Swnj optlen = 1; 4414640Swnj else 4424640Swnj optlen = cp[1]; 4434640Swnj switch (opt) { 4444495Swnj 4454640Swnj default: 4464640Swnj break; 4474495Swnj 4484951Swnj /* 4494951Swnj * Source routing with record. 4504951Swnj * Find interface with current destination address. 4514951Swnj * If none on this machine then drop if strictly routed, 4524951Swnj * or do nothing if loosely routed. 4534951Swnj * Record interface address and bring up next address 4544951Swnj * component. If strictly routed make sure next 4554951Swnj * address on directly accessible net. 4564951Swnj */ 4574640Swnj case IPOPT_LSRR: 4587508Sroot case IPOPT_SSRR: 4594801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 4604640Swnj break; 4614923Swnj sin = (struct in_addr *)(cp + cp[2]); 4626338Ssam ipaddr.sin_addr = *sin; 4636338Ssam ifp = if_ifwithaddr((struct sockaddr *)&ipaddr); 4646583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_SRCFAIL; 4654951Swnj if (ifp == 0) { 4664951Swnj if (opt == IPOPT_SSRR) 4674951Swnj goto bad; 4684951Swnj break; 4694640Swnj } 4704951Swnj t = ip->ip_dst; ip->ip_dst = *sin; *sin = t; 4714951Swnj cp[2] += 4; 4724951Swnj if (cp[2] > optlen - (sizeof (long) - 1)) 4734951Swnj break; 4744951Swnj ip->ip_dst = sin[1]; 4756338Ssam if (opt == IPOPT_SSRR && 4767166Ssam if_ifonnetof(in_netof(ip->ip_dst)) == 0) 4774951Swnj goto bad; 4784640Swnj break; 4794495Swnj 4804640Swnj case IPOPT_TS: 4816583Ssam code = cp - (u_char *)ip; 4826583Ssam type = ICMP_PARAMPROB; 4834801Swnj ipt = (struct ip_timestamp *)cp; 4844801Swnj if (ipt->ipt_len < 5) 4854640Swnj goto bad; 4864801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 4874801Swnj if (++ipt->ipt_oflw == 0) 4884640Swnj goto bad; 4894495Swnj break; 4904640Swnj } 4914923Swnj sin = (struct in_addr *)(cp+cp[2]); 4924801Swnj switch (ipt->ipt_flg) { 4934495Swnj 4944640Swnj case IPOPT_TS_TSONLY: 4954640Swnj break; 4964640Swnj 4974640Swnj case IPOPT_TS_TSANDADDR: 4984801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 4994640Swnj goto bad; 5006338Ssam if (ifinet == 0) 5016338Ssam goto bad; /* ??? */ 5026338Ssam *sin++ = ((struct sockaddr_in *)&ifinet->if_addr)->sin_addr; 5034640Swnj break; 5044640Swnj 5054640Swnj case IPOPT_TS_PRESPEC: 5066338Ssam ipaddr.sin_addr = *sin; 5076583Ssam if (!if_ifwithaddr((struct sockaddr *)&ipaddr)) 5084951Swnj continue; 5094801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5104640Swnj goto bad; 5114801Swnj ipt->ipt_ptr += 4; 5124640Swnj break; 5134640Swnj 5144495Swnj default: 5154640Swnj goto bad; 5164495Swnj } 5174923Swnj *(n_time *)sin = iptime(); 5184801Swnj ipt->ipt_ptr += 4; 5194640Swnj } 5204495Swnj } 5216583Ssam return (0); 5224640Swnj bad: 5236583Ssam icmp_error(ip, type, code); 5246583Ssam return (1); 5254495Swnj } 5264495Swnj 5274640Swnj /* 5284951Swnj * Strip out IP options, at higher 5294951Swnj * level protocol in the kernel. 5304951Swnj * Second argument is buffer to which options 5314951Swnj * will be moved, and return value is their length. 5324640Swnj */ 5335217Swnj ip_stripoptions(ip, mopt) 5344640Swnj struct ip *ip; 5355217Swnj struct mbuf *mopt; 5364495Swnj { 5374640Swnj register int i; 5384640Swnj register struct mbuf *m; 5394640Swnj int olen; 5404640Swnj 5414640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 5424951Swnj m = dtom(ip); 5434951Swnj ip++; 5445217Swnj if (mopt) { 5455217Swnj mopt->m_len = olen; 5465217Swnj mopt->m_off = MMINOFF; 5475217Swnj bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen); 5485217Swnj } 5494640Swnj i = m->m_len - (sizeof (struct ip) + olen); 5504907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 5515243Sroot m->m_len -= olen; 5524495Swnj } 5536583Ssam 5546590Ssam u_char inetctlerrmap[] = { 5556583Ssam ECONNABORTED, ECONNABORTED, 0, 0, 5566609Ssam 0, 0, 5576609Ssam EHOSTDOWN, EHOSTUNREACH, ENETUNREACH, EHOSTUNREACH, 5586583Ssam ECONNREFUSED, ECONNREFUSED, EMSGSIZE, 0, 5596583Ssam 0, 0, 0, 0 5606583Ssam }; 5616583Ssam 5626583Ssam ip_ctlinput(cmd, arg) 5636583Ssam int cmd; 5646583Ssam caddr_t arg; 5656583Ssam { 5666583Ssam struct in_addr *sin; 5676590Ssam int tcp_abort(), udp_abort(); 5686583Ssam extern struct inpcb tcb, udb; 5696583Ssam 5706583Ssam if (cmd < 0 || cmd > PRC_NCMDS) 5716583Ssam return; 5726590Ssam if (inetctlerrmap[cmd] == 0) 5736583Ssam return; /* XXX */ 5746583Ssam if (cmd == PRC_IFDOWN) 5756583Ssam sin = &((struct sockaddr_in *)arg)->sin_addr; 5766583Ssam else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH) 5776583Ssam sin = (struct in_addr *)arg; 5786583Ssam else 5796583Ssam sin = &((struct icmp *)arg)->icmp_ip.ip_dst; 5806590Ssam in_pcbnotify(&tcb, sin, inetctlerrmap[cmd], tcp_abort); 5816590Ssam in_pcbnotify(&udb, sin, inetctlerrmap[cmd], udp_abort); 5826583Ssam } 5836583Ssam 5846583Ssam int ipprintfs = 0; 5856583Ssam int ipforwarding = 1; 5866583Ssam /* 5876583Ssam * Forward a packet. If some error occurs return the sender 5886583Ssam * and icmp packet. Note we can't always generate a meaningful 5896583Ssam * icmp message because icmp doesn't have a large enough repetoire 5906583Ssam * of codes and types. 5916583Ssam */ 5926583Ssam ip_forward(ip) 5936583Ssam register struct ip *ip; 5946583Ssam { 5956583Ssam register int error, type, code; 5966609Ssam struct mbuf *mopt, *mcopy; 5976583Ssam 5986583Ssam if (ipprintfs) 5996583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 6006583Ssam ip->ip_dst, ip->ip_ttl); 6016583Ssam if (ipforwarding == 0) { 6026583Ssam /* can't tell difference between net and host */ 6036583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_NET; 6046583Ssam goto sendicmp; 6056583Ssam } 6066583Ssam if (ip->ip_ttl < IPTTLDEC) { 6076583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 6086583Ssam goto sendicmp; 6096583Ssam } 6106583Ssam ip->ip_ttl -= IPTTLDEC; 6116583Ssam mopt = m_get(M_DONTWAIT); 6126583Ssam if (mopt == 0) { 6136583Ssam m_freem(dtom(ip)); 6146583Ssam return; 6156583Ssam } 6166609Ssam 6176609Ssam /* 6186609Ssam * Save at most 64 bytes of the packet in case 6196609Ssam * we need to generate an ICMP message to the src. 6206609Ssam */ 6217843Sroot mcopy = m_copy(dtom(ip), 0, imin(ip->ip_len, 64)); 6226583Ssam ip_stripoptions(ip, mopt); 6236583Ssam 6246583Ssam /* last 0 here means no directed broadcast */ 6256609Ssam if ((error = ip_output(dtom(ip), mopt, 0, 0)) == 0) { 6266609Ssam if (mcopy) 6276609Ssam m_freem(mcopy); 6286583Ssam return; 6296609Ssam } 6306609Ssam ip = mtod(mcopy, struct ip *); 6316609Ssam type = ICMP_UNREACH, code = 0; /* need ``undefined'' */ 6326609Ssam switch (error) { 6336609Ssam 6346609Ssam case ENETUNREACH: 6356609Ssam case ENETDOWN: 6366583Ssam code = ICMP_UNREACH_NET; 6376609Ssam break; 6386609Ssam 6396609Ssam case EMSGSIZE: 6406583Ssam code = ICMP_UNREACH_NEEDFRAG; 6416609Ssam break; 6426609Ssam 6436609Ssam case EPERM: 6446609Ssam code = ICMP_UNREACH_PORT; 6456609Ssam break; 6466609Ssam 6476609Ssam case ENOBUFS: 6486609Ssam type = ICMP_SOURCEQUENCH; 6496609Ssam break; 6506609Ssam 6516609Ssam case EHOSTDOWN: 6526609Ssam case EHOSTUNREACH: 6536609Ssam code = ICMP_UNREACH_HOST; 6546609Ssam break; 6556609Ssam } 6566583Ssam sendicmp: 6576583Ssam icmp_error(ip, type, code); 6586583Ssam } 659