1*8172Sroot /* ip_input.c 1.49 82/09/12 */ 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" 85084Swnj #include "../net/in.h" 95084Swnj #include "../net/in_systm.h" 104951Swnj #include "../net/if.h" 115084Swnj #include "../net/ip.h" /* belongs before in.h */ 124898Swnj #include "../net/ip_var.h" 134801Swnj #include "../net/ip_icmp.h" 144801Swnj #include "../net/tcp.h" 15*8172Sroot #include <time.h> 16*8172Sroot #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; 42*8172Sroot 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 t->m_off = MMINOFF; 2374640Swnj fp = mtod(t, struct ipq *); 2384640Swnj insque(fp, &ipq); 2394640Swnj fp->ipq_ttl = IPFRAGTTL; 2404640Swnj fp->ipq_p = ip->ip_p; 2414640Swnj fp->ipq_id = ip->ip_id; 2424898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 2434898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 2444898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2455161Swnj q = (struct ipasfrag *)fp; 2465161Swnj goto insert; 2474640Swnj } 2484495Swnj 2494640Swnj /* 2504640Swnj * Find a segment which begins after this one does. 2514640Swnj */ 2524898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2534640Swnj if (q->ip_off > ip->ip_off) 2544640Swnj break; 2554495Swnj 2564640Swnj /* 2574640Swnj * If there is a preceding segment, it may provide some of 2584640Swnj * our data already. If so, drop the data from the incoming 2594640Swnj * segment. If it provides all of our data, drop us. 2604640Swnj */ 2614898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 2624898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2634640Swnj if (i > 0) { 2644640Swnj if (i >= ip->ip_len) 2654640Swnj goto dropfrag; 2664640Swnj m_adj(dtom(ip), i); 2674640Swnj ip->ip_off += i; 2684640Swnj ip->ip_len -= i; 2694640Swnj } 2704640Swnj } 2714543Swnj 2724640Swnj /* 2734640Swnj * While we overlap succeeding segments trim them or, 2744640Swnj * if they are completely covered, dequeue them. 2754640Swnj */ 2764898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 2774640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 2784640Swnj if (i < q->ip_len) { 2794640Swnj q->ip_len -= i; 2806256Sroot q->ip_off += i; 2814640Swnj m_adj(dtom(q), i); 2824640Swnj break; 2834495Swnj } 2844898Swnj q = q->ipf_next; 2854898Swnj m_freem(dtom(q->ipf_prev)); 2864898Swnj ip_deq(q->ipf_prev); 2874543Swnj } 2884495Swnj 2895161Swnj insert: 2904640Swnj /* 2914640Swnj * Stick new segment in its place; 2924640Swnj * check for complete reassembly. 2934640Swnj */ 2944898Swnj ip_enq(ip, q->ipf_prev); 2954640Swnj next = 0; 2964898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 2974640Swnj if (q->ip_off != next) 2984640Swnj return (0); 2994640Swnj next += q->ip_len; 3004640Swnj } 3014898Swnj if (q->ipf_prev->ipf_mff) 3024640Swnj return (0); 3034495Swnj 3044640Swnj /* 3054640Swnj * Reassembly is complete; concatenate fragments. 3064640Swnj */ 3074640Swnj q = fp->ipq_next; 3084640Swnj m = dtom(q); 3094640Swnj t = m->m_next; 3104640Swnj m->m_next = 0; 3114640Swnj m_cat(m, t); 3126298Swnj q = q->ipf_next; 3136298Swnj while (q != (struct ipasfrag *)fp) { 3146298Swnj t = dtom(q); 3156298Swnj q = q->ipf_next; 3166298Swnj m_cat(m, t); 3176298Swnj } 3184495Swnj 3194640Swnj /* 3204640Swnj * Create header for new ip packet by 3214640Swnj * modifying header of first packet; 3224640Swnj * dequeue and discard fragment reassembly header. 3234640Swnj * Make header visible. 3244640Swnj */ 3254640Swnj ip = fp->ipq_next; 3264640Swnj ip->ip_len = next; 3274898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 3284898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 3294640Swnj remque(fp); 3304907Swnj (void) m_free(dtom(fp)); 3314640Swnj m = dtom(ip); 3324898Swnj m->m_len += sizeof (struct ipasfrag); 3334898Swnj m->m_off -= sizeof (struct ipasfrag); 3344898Swnj return ((struct ip *)ip); 3354495Swnj 3364640Swnj dropfrag: 3374640Swnj m_freem(m); 3384640Swnj return (0); 3394495Swnj } 3404495Swnj 3414640Swnj /* 3424640Swnj * Free a fragment reassembly header and all 3434640Swnj * associated datagrams. 3444640Swnj */ 3454640Swnj struct ipq * 3464640Swnj ip_freef(fp) 3474640Swnj struct ipq *fp; 3484495Swnj { 3494898Swnj register struct ipasfrag *q; 3504640Swnj struct mbuf *m; 3514495Swnj 3524898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3534640Swnj m_freem(dtom(q)); 3544640Swnj m = dtom(fp); 3554640Swnj fp = fp->next; 3564640Swnj remque(fp->prev); 3574907Swnj (void) m_free(m); 3584640Swnj return (fp); 3594495Swnj } 3604495Swnj 3614640Swnj /* 3624640Swnj * Put an ip fragment on a reassembly chain. 3634640Swnj * Like insque, but pointers in middle of structure. 3644640Swnj */ 3654640Swnj ip_enq(p, prev) 3664898Swnj register struct ipasfrag *p, *prev; 3674495Swnj { 3684951Swnj 3694898Swnj p->ipf_prev = prev; 3704898Swnj p->ipf_next = prev->ipf_next; 3714898Swnj prev->ipf_next->ipf_prev = p; 3724898Swnj prev->ipf_next = p; 3734495Swnj } 3744495Swnj 3754640Swnj /* 3764640Swnj * To ip_enq as remque is to insque. 3774640Swnj */ 3784640Swnj ip_deq(p) 3794898Swnj register struct ipasfrag *p; 3804640Swnj { 3814951Swnj 3824898Swnj p->ipf_prev->ipf_next = p->ipf_next; 3834898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3844495Swnj } 3854495Swnj 3864640Swnj /* 3874640Swnj * IP timer processing; 3884640Swnj * if a timer expires on a reassembly 3894640Swnj * queue, discard it. 3904640Swnj */ 3914801Swnj ip_slowtimo() 3924495Swnj { 3934495Swnj register struct ipq *fp; 3944640Swnj int s = splnet(); 3954951Swnj 3965243Sroot fp = ipq.next; 3975243Sroot if (fp == 0) { 3985243Sroot splx(s); 3995243Sroot return; 4005243Sroot } 4015243Sroot while (fp != &ipq) 4024640Swnj if (--fp->ipq_ttl == 0) 4034640Swnj fp = ip_freef(fp); 4044640Swnj else 4054640Swnj fp = fp->next; 4064640Swnj splx(s); 4074495Swnj } 4084495Swnj 4094951Swnj /* 4104951Swnj * Drain off all datagram fragments. 4114951Swnj */ 4124801Swnj ip_drain() 4134801Swnj { 4144801Swnj 4154951Swnj while (ipq.next != &ipq) 4164951Swnj (void) ip_freef(ipq.next); 4174801Swnj } 4184923Swnj 4194640Swnj /* 4204640Swnj * Do option processing on a datagram, 4214640Swnj * possibly discarding it if bad options 4224640Swnj * are encountered. 4234640Swnj */ 4244640Swnj ip_dooptions(ip) 4254640Swnj struct ip *ip; 4264495Swnj { 4274640Swnj register u_char *cp; 4286583Ssam int opt, optlen, cnt, code, type; 4294923Swnj struct in_addr *sin; 4304801Swnj register struct ip_timestamp *ipt; 4314951Swnj register struct ifnet *ifp; 4324951Swnj struct in_addr t; 4334495Swnj 4344640Swnj cp = (u_char *)(ip + 1); 4354640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 4364640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 4374640Swnj opt = cp[0]; 4384640Swnj if (opt == IPOPT_EOL) 4394640Swnj break; 4404640Swnj if (opt == IPOPT_NOP) 4414640Swnj optlen = 1; 4424640Swnj else 4434640Swnj optlen = cp[1]; 4444640Swnj switch (opt) { 4454495Swnj 4464640Swnj default: 4474640Swnj break; 4484495Swnj 4494951Swnj /* 4504951Swnj * Source routing with record. 4514951Swnj * Find interface with current destination address. 4524951Swnj * If none on this machine then drop if strictly routed, 4534951Swnj * or do nothing if loosely routed. 4544951Swnj * Record interface address and bring up next address 4554951Swnj * component. If strictly routed make sure next 4564951Swnj * address on directly accessible net. 4574951Swnj */ 4584640Swnj case IPOPT_LSRR: 4597508Sroot case IPOPT_SSRR: 4604801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 4614640Swnj break; 4624923Swnj sin = (struct in_addr *)(cp + cp[2]); 4636338Ssam ipaddr.sin_addr = *sin; 4646338Ssam ifp = if_ifwithaddr((struct sockaddr *)&ipaddr); 4656583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_SRCFAIL; 4664951Swnj if (ifp == 0) { 4674951Swnj if (opt == IPOPT_SSRR) 4684951Swnj goto bad; 4694951Swnj break; 4704640Swnj } 4714951Swnj t = ip->ip_dst; ip->ip_dst = *sin; *sin = t; 4724951Swnj cp[2] += 4; 4734951Swnj if (cp[2] > optlen - (sizeof (long) - 1)) 4744951Swnj break; 4754951Swnj ip->ip_dst = sin[1]; 4766338Ssam if (opt == IPOPT_SSRR && 4777166Ssam if_ifonnetof(in_netof(ip->ip_dst)) == 0) 4784951Swnj goto bad; 4794640Swnj break; 4804495Swnj 4814640Swnj case IPOPT_TS: 4826583Ssam code = cp - (u_char *)ip; 4836583Ssam type = ICMP_PARAMPROB; 4844801Swnj ipt = (struct ip_timestamp *)cp; 4854801Swnj if (ipt->ipt_len < 5) 4864640Swnj goto bad; 4874801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 4884801Swnj if (++ipt->ipt_oflw == 0) 4894640Swnj goto bad; 4904495Swnj break; 4914640Swnj } 4924923Swnj sin = (struct in_addr *)(cp+cp[2]); 4934801Swnj switch (ipt->ipt_flg) { 4944495Swnj 4954640Swnj case IPOPT_TS_TSONLY: 4964640Swnj break; 4974640Swnj 4984640Swnj case IPOPT_TS_TSANDADDR: 4994801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5004640Swnj goto bad; 5016338Ssam if (ifinet == 0) 5026338Ssam goto bad; /* ??? */ 5036338Ssam *sin++ = ((struct sockaddr_in *)&ifinet->if_addr)->sin_addr; 5044640Swnj break; 5054640Swnj 5064640Swnj case IPOPT_TS_PRESPEC: 5076338Ssam ipaddr.sin_addr = *sin; 5086583Ssam if (!if_ifwithaddr((struct sockaddr *)&ipaddr)) 5094951Swnj continue; 5104801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5114640Swnj goto bad; 5124801Swnj ipt->ipt_ptr += 4; 5134640Swnj break; 5144640Swnj 5154495Swnj default: 5164640Swnj goto bad; 5174495Swnj } 5184923Swnj *(n_time *)sin = iptime(); 5194801Swnj ipt->ipt_ptr += 4; 5204640Swnj } 5214495Swnj } 5226583Ssam return (0); 5234640Swnj bad: 5246583Ssam icmp_error(ip, type, code); 5256583Ssam return (1); 5264495Swnj } 5274495Swnj 5284640Swnj /* 5294951Swnj * Strip out IP options, at higher 5304951Swnj * level protocol in the kernel. 5314951Swnj * Second argument is buffer to which options 5324951Swnj * will be moved, and return value is their length. 5334640Swnj */ 5345217Swnj ip_stripoptions(ip, mopt) 5354640Swnj struct ip *ip; 5365217Swnj struct mbuf *mopt; 5374495Swnj { 5384640Swnj register int i; 5394640Swnj register struct mbuf *m; 5404640Swnj int olen; 5414640Swnj 5424640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 5434951Swnj m = dtom(ip); 5444951Swnj ip++; 5455217Swnj if (mopt) { 5465217Swnj mopt->m_len = olen; 5475217Swnj mopt->m_off = MMINOFF; 5485217Swnj bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen); 5495217Swnj } 5504640Swnj i = m->m_len - (sizeof (struct ip) + olen); 5514907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 5525243Sroot m->m_len -= olen; 5534495Swnj } 5546583Ssam 5556590Ssam u_char inetctlerrmap[] = { 5566583Ssam ECONNABORTED, ECONNABORTED, 0, 0, 5576609Ssam 0, 0, 5586609Ssam EHOSTDOWN, EHOSTUNREACH, ENETUNREACH, EHOSTUNREACH, 5596583Ssam ECONNREFUSED, ECONNREFUSED, EMSGSIZE, 0, 5606583Ssam 0, 0, 0, 0 5616583Ssam }; 5626583Ssam 5636583Ssam ip_ctlinput(cmd, arg) 5646583Ssam int cmd; 5656583Ssam caddr_t arg; 5666583Ssam { 5676583Ssam struct in_addr *sin; 5686590Ssam int tcp_abort(), udp_abort(); 5696583Ssam extern struct inpcb tcb, udb; 5706583Ssam 5716583Ssam if (cmd < 0 || cmd > PRC_NCMDS) 5726583Ssam return; 5736590Ssam if (inetctlerrmap[cmd] == 0) 5746583Ssam return; /* XXX */ 5756583Ssam if (cmd == PRC_IFDOWN) 5766583Ssam sin = &((struct sockaddr_in *)arg)->sin_addr; 5776583Ssam else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH) 5786583Ssam sin = (struct in_addr *)arg; 5796583Ssam else 5806583Ssam sin = &((struct icmp *)arg)->icmp_ip.ip_dst; 5816590Ssam in_pcbnotify(&tcb, sin, inetctlerrmap[cmd], tcp_abort); 5826590Ssam in_pcbnotify(&udb, sin, inetctlerrmap[cmd], udp_abort); 5836583Ssam } 5846583Ssam 5856583Ssam int ipprintfs = 0; 5866583Ssam int ipforwarding = 1; 5876583Ssam /* 5886583Ssam * Forward a packet. If some error occurs return the sender 5896583Ssam * and icmp packet. Note we can't always generate a meaningful 5906583Ssam * icmp message because icmp doesn't have a large enough repetoire 5916583Ssam * of codes and types. 5926583Ssam */ 5936583Ssam ip_forward(ip) 5946583Ssam register struct ip *ip; 5956583Ssam { 5966583Ssam register int error, type, code; 5976609Ssam struct mbuf *mopt, *mcopy; 5986583Ssam 5996583Ssam if (ipprintfs) 6006583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 6016583Ssam ip->ip_dst, ip->ip_ttl); 6026583Ssam if (ipforwarding == 0) { 6036583Ssam /* can't tell difference between net and host */ 6046583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_NET; 6056583Ssam goto sendicmp; 6066583Ssam } 6076583Ssam if (ip->ip_ttl < IPTTLDEC) { 6086583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 6096583Ssam goto sendicmp; 6106583Ssam } 6116583Ssam ip->ip_ttl -= IPTTLDEC; 6126583Ssam mopt = m_get(M_DONTWAIT); 6136583Ssam if (mopt == 0) { 6146583Ssam m_freem(dtom(ip)); 6156583Ssam return; 6166583Ssam } 6176609Ssam 6186609Ssam /* 6196609Ssam * Save at most 64 bytes of the packet in case 6206609Ssam * we need to generate an ICMP message to the src. 6216609Ssam */ 6227843Sroot mcopy = m_copy(dtom(ip), 0, imin(ip->ip_len, 64)); 6236583Ssam ip_stripoptions(ip, mopt); 6246583Ssam 6256583Ssam /* last 0 here means no directed broadcast */ 6266609Ssam if ((error = ip_output(dtom(ip), mopt, 0, 0)) == 0) { 6276609Ssam if (mcopy) 6286609Ssam m_freem(mcopy); 6296583Ssam return; 6306609Ssam } 6316609Ssam ip = mtod(mcopy, struct ip *); 6326609Ssam type = ICMP_UNREACH, code = 0; /* need ``undefined'' */ 6336609Ssam switch (error) { 6346609Ssam 6356609Ssam case ENETUNREACH: 6366609Ssam case ENETDOWN: 6376583Ssam code = ICMP_UNREACH_NET; 6386609Ssam break; 6396609Ssam 6406609Ssam case EMSGSIZE: 6416583Ssam code = ICMP_UNREACH_NEEDFRAG; 6426609Ssam break; 6436609Ssam 6446609Ssam case EPERM: 6456609Ssam code = ICMP_UNREACH_PORT; 6466609Ssam break; 6476609Ssam 6486609Ssam case ENOBUFS: 6496609Ssam type = ICMP_SOURCEQUENCH; 6506609Ssam break; 6516609Ssam 6526609Ssam case EHOSTDOWN: 6536609Ssam case EHOSTUNREACH: 6546609Ssam code = ICMP_UNREACH_HOST; 6556609Ssam break; 6566609Ssam } 6576583Ssam sendicmp: 6586583Ssam icmp_error(ip, type, code); 6596583Ssam } 660