1*10892Ssam /* ip_input.c 1.64 83/02/10 */ 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" 9*10892Ssam #include "../h/errno.h" 10*10892Ssam #include "../h/time.h" 118833Sroot #include "../h/kernel.h" 128695Sroot 138695Sroot #include "../net/if.h" 148695Sroot #include "../net/route.h" 15*10892Ssam 168399Swnj #include "../netinet/in.h" 178597Sroot #include "../netinet/in_pcb.h" 188399Swnj #include "../netinet/in_systm.h" 198695Sroot #include "../netinet/ip.h" 208399Swnj #include "../netinet/ip_var.h" 218399Swnj #include "../netinet/ip_icmp.h" 228399Swnj #include "../netinet/tcp.h" 234495Swnj 244898Swnj u_char ip_protox[IPPROTO_MAX]; 256210Swnj int ipqmaxlen = IFQ_MAXLEN; 266338Ssam struct ifnet *ifinet; /* first inet interface */ 274898Swnj 284801Swnj /* 295172Swnj * IP initialization: fill in IP protocol switch table. 305161Swnj * All protocols not implemented in kernel go to raw IP protocol handler. 314801Swnj */ 324801Swnj ip_init() 334801Swnj { 344898Swnj register struct protosw *pr; 354898Swnj register int i; 364495Swnj 374898Swnj pr = pffindproto(PF_INET, IPPROTO_RAW); 384898Swnj if (pr == 0) 394898Swnj panic("ip_init"); 404898Swnj for (i = 0; i < IPPROTO_MAX; i++) 419030Sroot ip_protox[i] = pr - inetsw; 429030Sroot for (pr = inetdomain.dom_protosw; 439030Sroot pr <= inetdomain.dom_protoswNPROTOSW; pr++) 444898Swnj if (pr->pr_family == PF_INET && 454898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 469030Sroot ip_protox[pr->pr_protocol] = pr - inetsw; 474801Swnj ipq.next = ipq.prev = &ipq; 488172Sroot ip_id = time.tv_sec & 0xffff; 496210Swnj ipintrq.ifq_maxlen = ipqmaxlen; 506338Ssam ifinet = if_ifwithaf(AF_INET); 514801Swnj } 524801Swnj 534898Swnj u_char ipcksum = 1; 544640Swnj struct ip *ip_reass(); 556338Ssam struct sockaddr_in ipaddr = { AF_INET }; 564640Swnj 574640Swnj /* 584640Swnj * Ip input routine. Checksum and byte swap header. If fragmented 594640Swnj * try to reassamble. If complete and fragment queue exists, discard. 604640Swnj * Process options. Pass to next level. 614640Swnj */ 625084Swnj ipintr() 634495Swnj { 644923Swnj register struct ip *ip; 655084Swnj register struct mbuf *m; 668597Sroot struct mbuf *m0; 674640Swnj register int i; 684495Swnj register struct ipq *fp; 695084Swnj int hlen, s; 704495Swnj 715084Swnj next: 724640Swnj /* 735084Swnj * Get next datagram off input queue and get IP header 745084Swnj * in first mbuf. 754640Swnj */ 765084Swnj s = splimp(); 775084Swnj IF_DEQUEUE(&ipintrq, m); 785084Swnj splx(s); 795218Swnj if (m == 0) 805084Swnj return; 815306Sroot if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) && 825306Sroot (m = m_pullup(m, sizeof (struct ip))) == 0) 835306Sroot return; 844640Swnj ip = mtod(m, struct ip *); 855161Swnj if ((hlen = ip->ip_hl << 2) > m->m_len) { 865306Sroot if ((m = m_pullup(m, hlen)) == 0) 875306Sroot return; 885161Swnj ip = mtod(m, struct ip *); 895161Swnj } 904951Swnj if (ipcksum) 915217Swnj if (ip->ip_sum = in_cksum(m, hlen)) { 924951Swnj ipstat.ips_badsum++; 934951Swnj goto bad; 944495Swnj } 954951Swnj 964951Swnj /* 974951Swnj * Convert fields to host representation. 984951Swnj */ 994907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 1004640Swnj ip->ip_id = ntohs(ip->ip_id); 1014951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 1024495Swnj 1034543Swnj /* 1044640Swnj * Check that the amount of data in the buffers 1054640Swnj * is as at least much as the IP header would have us expect. 1064640Swnj * Trim mbufs if longer than we expect. 1074640Swnj * Drop packet if shorter than we expect. 1084543Swnj */ 1096475Sroot i = -ip->ip_len; 1105161Swnj m0 = m; 1116475Sroot for (;;) { 1124495Swnj i += m->m_len; 1136475Sroot if (m->m_next == 0) 1146475Sroot break; 1156475Sroot m = m->m_next; 1166088Sroot } 1176475Sroot if (i != 0) { 1186475Sroot if (i < 0) { 1195161Swnj ipstat.ips_tooshort++; 1204951Swnj goto bad; 1215161Swnj } 1226475Sroot if (i <= m->m_len) 1236475Sroot m->m_len -= i; 1246475Sroot else 1256475Sroot m_adj(m0, -i); 1264495Swnj } 1276475Sroot m = m0; 1284495Swnj 1294640Swnj /* 1304640Swnj * Process options and, if not destined for us, 1316583Ssam * ship it on. ip_dooptions returns 1 when an 1326583Ssam * error was detected (causing an icmp message 1336583Ssam * to be sent). 1344640Swnj */ 1356583Ssam if (hlen > sizeof (struct ip) && ip_dooptions(ip)) 1366583Ssam goto next; 1376210Swnj 1386338Ssam /* 1396350Ssam * Fast check on the first internet 1406350Ssam * interface in the list. 1416338Ssam */ 1426338Ssam if (ifinet) { 1436338Ssam struct sockaddr_in *sin; 1446338Ssam 1456338Ssam sin = (struct sockaddr_in *)&ifinet->if_addr; 1466338Ssam if (sin->sin_addr.s_addr == ip->ip_dst.s_addr) 1476338Ssam goto ours; 1486483Ssam sin = (struct sockaddr_in *)&ifinet->if_broadaddr; 1496350Ssam if ((ifinet->if_flags & IFF_BROADCAST) && 1506350Ssam sin->sin_addr.s_addr == ip->ip_dst.s_addr) 1516350Ssam goto ours; 1526338Ssam } 15310387Ssam /* BEGIN GROT */ 15410387Ssam #include "nd.h" 15510387Ssam #if NND > 0 15610387Ssam /* 15710387Ssam * Diskless machines don't initially know 15810387Ssam * their address, so take packets from them 15910387Ssam * if we're acting as a network disk server. 16010387Ssam */ 16110387Ssam if (ip->ip_dst.s_addr == INADDR_ANY && 16210387Ssam (in_netof(ip->ip_src) == INADDR_ANY && 16310387Ssam in_lnaof(ip->ip_src) != INADDR_ANY)) 16410387Ssam goto ours; 16510387Ssam #endif 16610387Ssam /* END GROT */ 1676338Ssam ipaddr.sin_addr = ip->ip_dst; 1686338Ssam if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) { 1696583Ssam ip_forward(ip); 1705084Swnj goto next; 1714543Swnj } 1724495Swnj 1736338Ssam ours: 1744640Swnj /* 1754640Swnj * Look for queue of fragments 1764640Swnj * of this datagram. 1774640Swnj */ 1784640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1794640Swnj if (ip->ip_id == fp->ipq_id && 1804640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1814640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1824640Swnj ip->ip_p == fp->ipq_p) 1834640Swnj goto found; 1844640Swnj fp = 0; 1854640Swnj found: 1864495Swnj 1874640Swnj /* 1884640Swnj * Adjust ip_len to not reflect header, 1894640Swnj * set ip_mff if more fragments are expected, 1904640Swnj * convert offset of this to bytes. 1914640Swnj */ 1924640Swnj ip->ip_len -= hlen; 1934898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 1944640Swnj if (ip->ip_off & IP_MF) 1954898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 1964640Swnj ip->ip_off <<= 3; 1974495Swnj 1984640Swnj /* 1994640Swnj * If datagram marked as having more fragments 2004640Swnj * or if this is not the first fragment, 2014640Swnj * attempt reassembly; if it succeeds, proceed. 2024640Swnj */ 2034898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 2044898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 2054640Swnj if (ip == 0) 2065084Swnj goto next; 2074640Swnj hlen = ip->ip_hl << 2; 2084640Swnj m = dtom(ip); 2094640Swnj } else 2104640Swnj if (fp) 21110735Ssam ip_freef(fp); 2124951Swnj 2134951Swnj /* 2144951Swnj * Switch out to protocol's input routine. 2154951Swnj */ 2169030Sroot (*inetsw[ip_protox[ip->ip_p]].pr_input)(m); 2175084Swnj goto next; 2184951Swnj bad: 2194951Swnj m_freem(m); 2205084Swnj goto next; 2214640Swnj } 2224495Swnj 2234640Swnj /* 2244640Swnj * Take incoming datagram fragment and try to 2254951Swnj * reassemble it into whole datagram. If a chain for 2264640Swnj * reassembly of this datagram already exists, then it 2274640Swnj * is given as fp; otherwise have to make a chain. 2284640Swnj */ 2294640Swnj struct ip * 2304640Swnj ip_reass(ip, fp) 2314898Swnj register struct ipasfrag *ip; 2324640Swnj register struct ipq *fp; 2334640Swnj { 2344640Swnj register struct mbuf *m = dtom(ip); 2354898Swnj register struct ipasfrag *q; 2364640Swnj struct mbuf *t; 2374640Swnj int hlen = ip->ip_hl << 2; 2384640Swnj int i, next; 2394543Swnj 2404640Swnj /* 2414640Swnj * Presence of header sizes in mbufs 2424640Swnj * would confuse code below. 2434640Swnj */ 2444640Swnj m->m_off += hlen; 2454640Swnj m->m_len -= hlen; 2464495Swnj 2474640Swnj /* 2484640Swnj * If first fragment to arrive, create a reassembly queue. 2494640Swnj */ 2504640Swnj if (fp == 0) { 2519641Ssam if ((t = m_get(M_WAIT, MT_FTABLE)) == NULL) 2524640Swnj goto dropfrag; 2534640Swnj fp = mtod(t, struct ipq *); 2544640Swnj insque(fp, &ipq); 2554640Swnj fp->ipq_ttl = IPFRAGTTL; 2564640Swnj fp->ipq_p = ip->ip_p; 2574640Swnj fp->ipq_id = ip->ip_id; 2584898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 2594898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 2604898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2615161Swnj q = (struct ipasfrag *)fp; 2625161Swnj goto insert; 2634640Swnj } 2644495Swnj 2654640Swnj /* 2664640Swnj * Find a segment which begins after this one does. 2674640Swnj */ 2684898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2694640Swnj if (q->ip_off > ip->ip_off) 2704640Swnj break; 2714495Swnj 2724640Swnj /* 2734640Swnj * If there is a preceding segment, it may provide some of 2744640Swnj * our data already. If so, drop the data from the incoming 2754640Swnj * segment. If it provides all of our data, drop us. 2764640Swnj */ 2774898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 2784898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2794640Swnj if (i > 0) { 2804640Swnj if (i >= ip->ip_len) 2814640Swnj goto dropfrag; 2824640Swnj m_adj(dtom(ip), i); 2834640Swnj ip->ip_off += i; 2844640Swnj ip->ip_len -= i; 2854640Swnj } 2864640Swnj } 2874543Swnj 2884640Swnj /* 2894640Swnj * While we overlap succeeding segments trim them or, 2904640Swnj * if they are completely covered, dequeue them. 2914640Swnj */ 2924898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 2934640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 2944640Swnj if (i < q->ip_len) { 2954640Swnj q->ip_len -= i; 2966256Sroot q->ip_off += i; 2974640Swnj m_adj(dtom(q), i); 2984640Swnj break; 2994495Swnj } 3004898Swnj q = q->ipf_next; 3014898Swnj m_freem(dtom(q->ipf_prev)); 3024898Swnj ip_deq(q->ipf_prev); 3034543Swnj } 3044495Swnj 3055161Swnj insert: 3064640Swnj /* 3074640Swnj * Stick new segment in its place; 3084640Swnj * check for complete reassembly. 3094640Swnj */ 3104898Swnj ip_enq(ip, q->ipf_prev); 3114640Swnj next = 0; 3124898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 3134640Swnj if (q->ip_off != next) 3144640Swnj return (0); 3154640Swnj next += q->ip_len; 3164640Swnj } 3174898Swnj if (q->ipf_prev->ipf_mff) 3184640Swnj return (0); 3194495Swnj 3204640Swnj /* 3214640Swnj * Reassembly is complete; concatenate fragments. 3224640Swnj */ 3234640Swnj q = fp->ipq_next; 3244640Swnj m = dtom(q); 3254640Swnj t = m->m_next; 3264640Swnj m->m_next = 0; 3274640Swnj m_cat(m, t); 3286298Swnj q = q->ipf_next; 3296298Swnj while (q != (struct ipasfrag *)fp) { 3306298Swnj t = dtom(q); 3316298Swnj q = q->ipf_next; 3326298Swnj m_cat(m, t); 3336298Swnj } 3344495Swnj 3354640Swnj /* 3364640Swnj * Create header for new ip packet by 3374640Swnj * modifying header of first packet; 3384640Swnj * dequeue and discard fragment reassembly header. 3394640Swnj * Make header visible. 3404640Swnj */ 3414640Swnj ip = fp->ipq_next; 3424640Swnj ip->ip_len = next; 3434898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 3444898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 3454640Swnj remque(fp); 3464907Swnj (void) m_free(dtom(fp)); 3474640Swnj m = dtom(ip); 3484898Swnj m->m_len += sizeof (struct ipasfrag); 3494898Swnj m->m_off -= sizeof (struct ipasfrag); 3504898Swnj return ((struct ip *)ip); 3514495Swnj 3524640Swnj dropfrag: 3534640Swnj m_freem(m); 3544640Swnj return (0); 3554495Swnj } 3564495Swnj 3574640Swnj /* 3584640Swnj * Free a fragment reassembly header and all 3594640Swnj * associated datagrams. 3604640Swnj */ 3614640Swnj ip_freef(fp) 3624640Swnj struct ipq *fp; 3634495Swnj { 36410735Ssam register struct ipasfrag *q, *p; 3654495Swnj 36610735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 36710735Ssam p = q->ipf_next; 36810735Ssam ip_deq(q); 3694640Swnj m_freem(dtom(q)); 37010735Ssam } 37110735Ssam remque(fp); 37210735Ssam (void) m_free(dtom(fp)); 3734495Swnj } 3744495Swnj 3754640Swnj /* 3764640Swnj * Put an ip fragment on a reassembly chain. 3774640Swnj * Like insque, but pointers in middle of structure. 3784640Swnj */ 3794640Swnj ip_enq(p, prev) 3804898Swnj register struct ipasfrag *p, *prev; 3814495Swnj { 3824951Swnj 3834898Swnj p->ipf_prev = prev; 3844898Swnj p->ipf_next = prev->ipf_next; 3854898Swnj prev->ipf_next->ipf_prev = p; 3864898Swnj prev->ipf_next = p; 3874495Swnj } 3884495Swnj 3894640Swnj /* 3904640Swnj * To ip_enq as remque is to insque. 3914640Swnj */ 3924640Swnj ip_deq(p) 3934898Swnj register struct ipasfrag *p; 3944640Swnj { 3954951Swnj 3964898Swnj p->ipf_prev->ipf_next = p->ipf_next; 3974898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3984495Swnj } 3994495Swnj 4004640Swnj /* 4014640Swnj * IP timer processing; 4024640Swnj * if a timer expires on a reassembly 4034640Swnj * queue, discard it. 4044640Swnj */ 4054801Swnj ip_slowtimo() 4064495Swnj { 4074495Swnj register struct ipq *fp; 4084640Swnj int s = splnet(); 4094951Swnj 4105243Sroot fp = ipq.next; 4115243Sroot if (fp == 0) { 4125243Sroot splx(s); 4135243Sroot return; 4145243Sroot } 41510735Ssam while (fp != &ipq) { 41610735Ssam --fp->ipq_ttl; 41710735Ssam fp = fp->next; 41810735Ssam if (fp->prev->ipq_ttl == 0) 41910735Ssam ip_freef(fp->prev); 42010735Ssam } 4214640Swnj splx(s); 4224495Swnj } 4234495Swnj 4244951Swnj /* 4254951Swnj * Drain off all datagram fragments. 4264951Swnj */ 4274801Swnj ip_drain() 4284801Swnj { 4294801Swnj 4304951Swnj while (ipq.next != &ipq) 43110735Ssam ip_freef(ipq.next); 4324801Swnj } 4334923Swnj 4344640Swnj /* 4354640Swnj * Do option processing on a datagram, 4364640Swnj * possibly discarding it if bad options 4374640Swnj * are encountered. 4384640Swnj */ 4394640Swnj ip_dooptions(ip) 4404640Swnj struct ip *ip; 4414495Swnj { 4424640Swnj register u_char *cp; 4436583Ssam int opt, optlen, cnt, code, type; 4444923Swnj struct in_addr *sin; 4454801Swnj register struct ip_timestamp *ipt; 4464951Swnj register struct ifnet *ifp; 4474951Swnj struct in_addr t; 4484495Swnj 4494640Swnj cp = (u_char *)(ip + 1); 4504640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 4514640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 4524640Swnj opt = cp[0]; 4534640Swnj if (opt == IPOPT_EOL) 4544640Swnj break; 4554640Swnj if (opt == IPOPT_NOP) 4564640Swnj optlen = 1; 4574640Swnj else 4584640Swnj optlen = cp[1]; 4594640Swnj switch (opt) { 4604495Swnj 4614640Swnj default: 4624640Swnj break; 4634495Swnj 4644951Swnj /* 4654951Swnj * Source routing with record. 4664951Swnj * Find interface with current destination address. 4674951Swnj * If none on this machine then drop if strictly routed, 4684951Swnj * or do nothing if loosely routed. 4694951Swnj * Record interface address and bring up next address 4704951Swnj * component. If strictly routed make sure next 4714951Swnj * address on directly accessible net. 4724951Swnj */ 4734640Swnj case IPOPT_LSRR: 4747508Sroot case IPOPT_SSRR: 4754801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 4764640Swnj break; 4774923Swnj sin = (struct in_addr *)(cp + cp[2]); 4786338Ssam ipaddr.sin_addr = *sin; 4796338Ssam ifp = if_ifwithaddr((struct sockaddr *)&ipaddr); 4806583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_SRCFAIL; 4814951Swnj if (ifp == 0) { 4824951Swnj if (opt == IPOPT_SSRR) 4834951Swnj goto bad; 4844951Swnj break; 4854640Swnj } 4864951Swnj t = ip->ip_dst; ip->ip_dst = *sin; *sin = t; 4874951Swnj cp[2] += 4; 4884951Swnj if (cp[2] > optlen - (sizeof (long) - 1)) 4894951Swnj break; 4904951Swnj ip->ip_dst = sin[1]; 4916338Ssam if (opt == IPOPT_SSRR && 4927166Ssam if_ifonnetof(in_netof(ip->ip_dst)) == 0) 4934951Swnj goto bad; 4944640Swnj break; 4954495Swnj 4964640Swnj case IPOPT_TS: 4976583Ssam code = cp - (u_char *)ip; 4986583Ssam type = ICMP_PARAMPROB; 4994801Swnj ipt = (struct ip_timestamp *)cp; 5004801Swnj if (ipt->ipt_len < 5) 5014640Swnj goto bad; 5024801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 5034801Swnj if (++ipt->ipt_oflw == 0) 5044640Swnj goto bad; 5054495Swnj break; 5064640Swnj } 5074923Swnj sin = (struct in_addr *)(cp+cp[2]); 5084801Swnj switch (ipt->ipt_flg) { 5094495Swnj 5104640Swnj case IPOPT_TS_TSONLY: 5114640Swnj break; 5124640Swnj 5134640Swnj case IPOPT_TS_TSANDADDR: 5144801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5154640Swnj goto bad; 5166338Ssam if (ifinet == 0) 5176338Ssam goto bad; /* ??? */ 5186338Ssam *sin++ = ((struct sockaddr_in *)&ifinet->if_addr)->sin_addr; 5194640Swnj break; 5204640Swnj 5214640Swnj case IPOPT_TS_PRESPEC: 5226338Ssam ipaddr.sin_addr = *sin; 52310142Ssam if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) 5244951Swnj continue; 5254801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5264640Swnj goto bad; 5274801Swnj ipt->ipt_ptr += 4; 5284640Swnj break; 5294640Swnj 5304495Swnj default: 5314640Swnj goto bad; 5324495Swnj } 5334923Swnj *(n_time *)sin = iptime(); 5344801Swnj ipt->ipt_ptr += 4; 5354640Swnj } 5364495Swnj } 5376583Ssam return (0); 5384640Swnj bad: 5396583Ssam icmp_error(ip, type, code); 5406583Ssam return (1); 5414495Swnj } 5424495Swnj 5434640Swnj /* 5444951Swnj * Strip out IP options, at higher 5454951Swnj * level protocol in the kernel. 5464951Swnj * Second argument is buffer to which options 5474951Swnj * will be moved, and return value is their length. 5484640Swnj */ 5495217Swnj ip_stripoptions(ip, mopt) 5504640Swnj struct ip *ip; 5515217Swnj struct mbuf *mopt; 5524495Swnj { 5534640Swnj register int i; 5544640Swnj register struct mbuf *m; 5554640Swnj int olen; 5564640Swnj 5574640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 5584951Swnj m = dtom(ip); 5594951Swnj ip++; 5605217Swnj if (mopt) { 5615217Swnj mopt->m_len = olen; 5625217Swnj mopt->m_off = MMINOFF; 5635217Swnj bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen); 5645217Swnj } 5654640Swnj i = m->m_len - (sizeof (struct ip) + olen); 5664907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 5675243Sroot m->m_len -= olen; 5684495Swnj } 5696583Ssam 5706590Ssam u_char inetctlerrmap[] = { 5716583Ssam ECONNABORTED, ECONNABORTED, 0, 0, 5726609Ssam 0, 0, 5736609Ssam EHOSTDOWN, EHOSTUNREACH, ENETUNREACH, EHOSTUNREACH, 5746583Ssam ECONNREFUSED, ECONNREFUSED, EMSGSIZE, 0, 5756583Ssam 0, 0, 0, 0 5766583Ssam }; 5776583Ssam 5786583Ssam ip_ctlinput(cmd, arg) 5796583Ssam int cmd; 5806583Ssam caddr_t arg; 5816583Ssam { 5828775Sroot struct in_addr *in; 5836590Ssam int tcp_abort(), udp_abort(); 5846583Ssam extern struct inpcb tcb, udb; 5856583Ssam 5866583Ssam if (cmd < 0 || cmd > PRC_NCMDS) 5876583Ssam return; 5886590Ssam if (inetctlerrmap[cmd] == 0) 5896583Ssam return; /* XXX */ 5906583Ssam if (cmd == PRC_IFDOWN) 5918775Sroot in = &((struct sockaddr_in *)arg)->sin_addr; 5926583Ssam else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH) 5938775Sroot in = (struct in_addr *)arg; 5946583Ssam else 5958775Sroot in = &((struct icmp *)arg)->icmp_ip.ip_dst; 5968597Sroot /* THIS IS VERY QUESTIONABLE, SHOULD HIT ALL PROTOCOLS */ 5978775Sroot in_pcbnotify(&tcb, in, (int)inetctlerrmap[cmd], tcp_abort); 5988775Sroot in_pcbnotify(&udb, in, (int)inetctlerrmap[cmd], udp_abort); 5996583Ssam } 6006583Ssam 6016583Ssam int ipprintfs = 0; 6026583Ssam int ipforwarding = 1; 6036583Ssam /* 6046583Ssam * Forward a packet. If some error occurs return the sender 6056583Ssam * and icmp packet. Note we can't always generate a meaningful 6066583Ssam * icmp message because icmp doesn't have a large enough repetoire 6076583Ssam * of codes and types. 6086583Ssam */ 6096583Ssam ip_forward(ip) 6106583Ssam register struct ip *ip; 6116583Ssam { 6126583Ssam register int error, type, code; 6136609Ssam struct mbuf *mopt, *mcopy; 6146583Ssam 6156583Ssam if (ipprintfs) 6166583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 6176583Ssam ip->ip_dst, ip->ip_ttl); 6186583Ssam if (ipforwarding == 0) { 6196583Ssam /* can't tell difference between net and host */ 6206583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_NET; 6216583Ssam goto sendicmp; 6226583Ssam } 6236583Ssam if (ip->ip_ttl < IPTTLDEC) { 6246583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 6256583Ssam goto sendicmp; 6266583Ssam } 6276583Ssam ip->ip_ttl -= IPTTLDEC; 6289641Ssam mopt = m_get(M_DONTWAIT, MT_DATA); 6296583Ssam if (mopt == 0) { 6306583Ssam m_freem(dtom(ip)); 6316583Ssam return; 6326583Ssam } 6336609Ssam 6346609Ssam /* 6356609Ssam * Save at most 64 bytes of the packet in case 6366609Ssam * we need to generate an ICMP message to the src. 6376609Ssam */ 6387843Sroot mcopy = m_copy(dtom(ip), 0, imin(ip->ip_len, 64)); 6396583Ssam ip_stripoptions(ip, mopt); 6406583Ssam 6416583Ssam /* last 0 here means no directed broadcast */ 6428637Sroot if ((error = ip_output(dtom(ip), mopt, (struct route *)0, 0)) == 0) { 6436609Ssam if (mcopy) 6446609Ssam m_freem(mcopy); 6456583Ssam return; 6466609Ssam } 6476609Ssam ip = mtod(mcopy, struct ip *); 6486609Ssam type = ICMP_UNREACH, code = 0; /* need ``undefined'' */ 6496609Ssam switch (error) { 6506609Ssam 6516609Ssam case ENETUNREACH: 6526609Ssam case ENETDOWN: 6536583Ssam code = ICMP_UNREACH_NET; 6546609Ssam break; 6556609Ssam 6566609Ssam case EMSGSIZE: 6576583Ssam code = ICMP_UNREACH_NEEDFRAG; 6586609Ssam break; 6596609Ssam 6606609Ssam case EPERM: 6616609Ssam code = ICMP_UNREACH_PORT; 6626609Ssam break; 6636609Ssam 6646609Ssam case ENOBUFS: 6656609Ssam type = ICMP_SOURCEQUENCH; 6666609Ssam break; 6676609Ssam 6686609Ssam case EHOSTDOWN: 6696609Ssam case EHOSTUNREACH: 6706609Ssam code = ICMP_UNREACH_HOST; 6716609Ssam break; 6726609Ssam } 6736583Ssam sendicmp: 6746583Ssam icmp_error(ip, type, code); 6756583Ssam } 676