1*16990Skarels /* ip_input.c 6.4 84/08/21 */ 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" 910892Ssam #include "../h/errno.h" 1010892Ssam #include "../h/time.h" 118833Sroot #include "../h/kernel.h" 128695Sroot 138695Sroot #include "../net/if.h" 148695Sroot #include "../net/route.h" 1510892Ssam 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++) 44*16990Skarels if (pr->pr_domain->dom_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)) && 8211232Ssam (m = m_pullup(m, sizeof (struct ip))) == 0) { 8311232Ssam ipstat.ips_toosmall++; 8411232Ssam goto next; 8511232Ssam } 864640Swnj ip = mtod(m, struct ip *); 875161Swnj if ((hlen = ip->ip_hl << 2) > m->m_len) { 8811232Ssam if ((m = m_pullup(m, hlen)) == 0) { 8911232Ssam ipstat.ips_badhlen++; 9011232Ssam goto next; 9111232Ssam } 925161Swnj ip = mtod(m, struct ip *); 935161Swnj } 944951Swnj if (ipcksum) 955217Swnj if (ip->ip_sum = in_cksum(m, hlen)) { 964951Swnj ipstat.ips_badsum++; 974951Swnj goto bad; 984495Swnj } 994951Swnj 1004951Swnj /* 1014951Swnj * Convert fields to host representation. 1024951Swnj */ 1034907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 10411232Ssam if (ip->ip_len < hlen) { 10511232Ssam ipstat.ips_badlen++; 10611232Ssam goto bad; 10711232Ssam } 1084640Swnj ip->ip_id = ntohs(ip->ip_id); 1094951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 1104495Swnj 1114543Swnj /* 1124640Swnj * Check that the amount of data in the buffers 1134640Swnj * is as at least much as the IP header would have us expect. 1144640Swnj * Trim mbufs if longer than we expect. 1154640Swnj * Drop packet if shorter than we expect. 1164543Swnj */ 1176475Sroot i = -ip->ip_len; 1185161Swnj m0 = m; 1196475Sroot for (;;) { 1204495Swnj i += m->m_len; 1216475Sroot if (m->m_next == 0) 1226475Sroot break; 1236475Sroot m = m->m_next; 1246088Sroot } 1256475Sroot if (i != 0) { 1266475Sroot if (i < 0) { 1275161Swnj ipstat.ips_tooshort++; 1284951Swnj goto bad; 1295161Swnj } 1306475Sroot if (i <= m->m_len) 1316475Sroot m->m_len -= i; 1326475Sroot else 1336475Sroot m_adj(m0, -i); 1344495Swnj } 1356475Sroot m = m0; 1364495Swnj 1374640Swnj /* 1384640Swnj * Process options and, if not destined for us, 1396583Ssam * ship it on. ip_dooptions returns 1 when an 1406583Ssam * error was detected (causing an icmp message 1416583Ssam * to be sent). 1424640Swnj */ 1436583Ssam if (hlen > sizeof (struct ip) && ip_dooptions(ip)) 1446583Ssam goto next; 1456210Swnj 1466338Ssam /* 1476350Ssam * Fast check on the first internet 1486350Ssam * interface in the list. 1496338Ssam */ 1506338Ssam if (ifinet) { 1516338Ssam struct sockaddr_in *sin; 1526338Ssam 1536338Ssam sin = (struct sockaddr_in *)&ifinet->if_addr; 1546338Ssam if (sin->sin_addr.s_addr == ip->ip_dst.s_addr) 1556338Ssam goto ours; 1566483Ssam sin = (struct sockaddr_in *)&ifinet->if_broadaddr; 1576350Ssam if ((ifinet->if_flags & IFF_BROADCAST) && 1586350Ssam sin->sin_addr.s_addr == ip->ip_dst.s_addr) 1596350Ssam goto ours; 1606338Ssam } 16110387Ssam /* BEGIN GROT */ 16210387Ssam #include "nd.h" 16310387Ssam #if NND > 0 16410387Ssam /* 16510387Ssam * Diskless machines don't initially know 16610387Ssam * their address, so take packets from them 16710387Ssam * if we're acting as a network disk server. 16810387Ssam */ 16912459Ssam if (in_netof(ip->ip_dst) == INADDR_ANY && 17010387Ssam (in_netof(ip->ip_src) == INADDR_ANY && 17110387Ssam in_lnaof(ip->ip_src) != INADDR_ANY)) 17210387Ssam goto ours; 17310387Ssam #endif 17410387Ssam /* END GROT */ 1756338Ssam ipaddr.sin_addr = ip->ip_dst; 1766338Ssam if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) { 1776583Ssam ip_forward(ip); 1785084Swnj goto next; 1794543Swnj } 1804495Swnj 1816338Ssam ours: 1824640Swnj /* 1834640Swnj * Look for queue of fragments 1844640Swnj * of this datagram. 1854640Swnj */ 1864640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1874640Swnj if (ip->ip_id == fp->ipq_id && 1884640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1894640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1904640Swnj ip->ip_p == fp->ipq_p) 1914640Swnj goto found; 1924640Swnj fp = 0; 1934640Swnj found: 1944495Swnj 1954640Swnj /* 1964640Swnj * Adjust ip_len to not reflect header, 1974640Swnj * set ip_mff if more fragments are expected, 1984640Swnj * convert offset of this to bytes. 1994640Swnj */ 2004640Swnj ip->ip_len -= hlen; 2014898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 2024640Swnj if (ip->ip_off & IP_MF) 2034898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 2044640Swnj ip->ip_off <<= 3; 2054495Swnj 2064640Swnj /* 2074640Swnj * If datagram marked as having more fragments 2084640Swnj * or if this is not the first fragment, 2094640Swnj * attempt reassembly; if it succeeds, proceed. 2104640Swnj */ 2114898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 2124898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 2134640Swnj if (ip == 0) 2145084Swnj goto next; 2154640Swnj hlen = ip->ip_hl << 2; 2164640Swnj m = dtom(ip); 2174640Swnj } else 2184640Swnj if (fp) 21910735Ssam ip_freef(fp); 2204951Swnj 2214951Swnj /* 2224951Swnj * Switch out to protocol's input routine. 2234951Swnj */ 2249030Sroot (*inetsw[ip_protox[ip->ip_p]].pr_input)(m); 2255084Swnj goto next; 2264951Swnj bad: 2274951Swnj m_freem(m); 2285084Swnj goto next; 2294640Swnj } 2304495Swnj 2314640Swnj /* 2324640Swnj * Take incoming datagram fragment and try to 2334951Swnj * reassemble it into whole datagram. If a chain for 2344640Swnj * reassembly of this datagram already exists, then it 2354640Swnj * is given as fp; otherwise have to make a chain. 2364640Swnj */ 2374640Swnj struct ip * 2384640Swnj ip_reass(ip, fp) 2394898Swnj register struct ipasfrag *ip; 2404640Swnj register struct ipq *fp; 2414640Swnj { 2424640Swnj register struct mbuf *m = dtom(ip); 2434898Swnj register struct ipasfrag *q; 2444640Swnj struct mbuf *t; 2454640Swnj int hlen = ip->ip_hl << 2; 2464640Swnj int i, next; 2474543Swnj 2484640Swnj /* 2494640Swnj * Presence of header sizes in mbufs 2504640Swnj * would confuse code below. 2514640Swnj */ 2524640Swnj m->m_off += hlen; 2534640Swnj m->m_len -= hlen; 2544495Swnj 2554640Swnj /* 2564640Swnj * If first fragment to arrive, create a reassembly queue. 2574640Swnj */ 2584640Swnj if (fp == 0) { 2599641Ssam if ((t = m_get(M_WAIT, MT_FTABLE)) == NULL) 2604640Swnj goto dropfrag; 2614640Swnj fp = mtod(t, struct ipq *); 2624640Swnj insque(fp, &ipq); 2634640Swnj fp->ipq_ttl = IPFRAGTTL; 2644640Swnj fp->ipq_p = ip->ip_p; 2654640Swnj fp->ipq_id = ip->ip_id; 2664898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 2674898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 2684898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2695161Swnj q = (struct ipasfrag *)fp; 2705161Swnj goto insert; 2714640Swnj } 2724495Swnj 2734640Swnj /* 2744640Swnj * Find a segment which begins after this one does. 2754640Swnj */ 2764898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2774640Swnj if (q->ip_off > ip->ip_off) 2784640Swnj break; 2794495Swnj 2804640Swnj /* 2814640Swnj * If there is a preceding segment, it may provide some of 2824640Swnj * our data already. If so, drop the data from the incoming 2834640Swnj * segment. If it provides all of our data, drop us. 2844640Swnj */ 2854898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 2864898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2874640Swnj if (i > 0) { 2884640Swnj if (i >= ip->ip_len) 2894640Swnj goto dropfrag; 2904640Swnj m_adj(dtom(ip), i); 2914640Swnj ip->ip_off += i; 2924640Swnj ip->ip_len -= i; 2934640Swnj } 2944640Swnj } 2954543Swnj 2964640Swnj /* 2974640Swnj * While we overlap succeeding segments trim them or, 2984640Swnj * if they are completely covered, dequeue them. 2994640Swnj */ 3004898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 3014640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 3024640Swnj if (i < q->ip_len) { 3034640Swnj q->ip_len -= i; 3046256Sroot q->ip_off += i; 3054640Swnj m_adj(dtom(q), i); 3064640Swnj break; 3074495Swnj } 3084898Swnj q = q->ipf_next; 3094898Swnj m_freem(dtom(q->ipf_prev)); 3104898Swnj ip_deq(q->ipf_prev); 3114543Swnj } 3124495Swnj 3135161Swnj insert: 3144640Swnj /* 3154640Swnj * Stick new segment in its place; 3164640Swnj * check for complete reassembly. 3174640Swnj */ 3184898Swnj ip_enq(ip, q->ipf_prev); 3194640Swnj next = 0; 3204898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 3214640Swnj if (q->ip_off != next) 3224640Swnj return (0); 3234640Swnj next += q->ip_len; 3244640Swnj } 3254898Swnj if (q->ipf_prev->ipf_mff) 3264640Swnj return (0); 3274495Swnj 3284640Swnj /* 3294640Swnj * Reassembly is complete; concatenate fragments. 3304640Swnj */ 3314640Swnj q = fp->ipq_next; 3324640Swnj m = dtom(q); 3334640Swnj t = m->m_next; 3344640Swnj m->m_next = 0; 3354640Swnj m_cat(m, t); 3366298Swnj q = q->ipf_next; 3376298Swnj while (q != (struct ipasfrag *)fp) { 3386298Swnj t = dtom(q); 3396298Swnj q = q->ipf_next; 3406298Swnj m_cat(m, t); 3416298Swnj } 3424495Swnj 3434640Swnj /* 3444640Swnj * Create header for new ip packet by 3454640Swnj * modifying header of first packet; 3464640Swnj * dequeue and discard fragment reassembly header. 3474640Swnj * Make header visible. 3484640Swnj */ 3494640Swnj ip = fp->ipq_next; 3504640Swnj ip->ip_len = next; 3514898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 3524898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 3534640Swnj remque(fp); 3544907Swnj (void) m_free(dtom(fp)); 3554640Swnj m = dtom(ip); 3564898Swnj m->m_len += sizeof (struct ipasfrag); 3574898Swnj m->m_off -= sizeof (struct ipasfrag); 3584898Swnj return ((struct ip *)ip); 3594495Swnj 3604640Swnj dropfrag: 3614640Swnj m_freem(m); 3624640Swnj return (0); 3634495Swnj } 3644495Swnj 3654640Swnj /* 3664640Swnj * Free a fragment reassembly header and all 3674640Swnj * associated datagrams. 3684640Swnj */ 3694640Swnj ip_freef(fp) 3704640Swnj struct ipq *fp; 3714495Swnj { 37210735Ssam register struct ipasfrag *q, *p; 3734495Swnj 37410735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 37510735Ssam p = q->ipf_next; 37610735Ssam ip_deq(q); 3774640Swnj m_freem(dtom(q)); 37810735Ssam } 37910735Ssam remque(fp); 38010735Ssam (void) m_free(dtom(fp)); 3814495Swnj } 3824495Swnj 3834640Swnj /* 3844640Swnj * Put an ip fragment on a reassembly chain. 3854640Swnj * Like insque, but pointers in middle of structure. 3864640Swnj */ 3874640Swnj ip_enq(p, prev) 3884898Swnj register struct ipasfrag *p, *prev; 3894495Swnj { 3904951Swnj 3914898Swnj p->ipf_prev = prev; 3924898Swnj p->ipf_next = prev->ipf_next; 3934898Swnj prev->ipf_next->ipf_prev = p; 3944898Swnj prev->ipf_next = p; 3954495Swnj } 3964495Swnj 3974640Swnj /* 3984640Swnj * To ip_enq as remque is to insque. 3994640Swnj */ 4004640Swnj ip_deq(p) 4014898Swnj register struct ipasfrag *p; 4024640Swnj { 4034951Swnj 4044898Swnj p->ipf_prev->ipf_next = p->ipf_next; 4054898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 4064495Swnj } 4074495Swnj 4084640Swnj /* 4094640Swnj * IP timer processing; 4104640Swnj * if a timer expires on a reassembly 4114640Swnj * queue, discard it. 4124640Swnj */ 4134801Swnj ip_slowtimo() 4144495Swnj { 4154495Swnj register struct ipq *fp; 4164640Swnj int s = splnet(); 4174951Swnj 4185243Sroot fp = ipq.next; 4195243Sroot if (fp == 0) { 4205243Sroot splx(s); 4215243Sroot return; 4225243Sroot } 42310735Ssam while (fp != &ipq) { 42410735Ssam --fp->ipq_ttl; 42510735Ssam fp = fp->next; 42610735Ssam if (fp->prev->ipq_ttl == 0) 42710735Ssam ip_freef(fp->prev); 42810735Ssam } 4294640Swnj splx(s); 4304495Swnj } 4314495Swnj 4324951Swnj /* 4334951Swnj * Drain off all datagram fragments. 4344951Swnj */ 4354801Swnj ip_drain() 4364801Swnj { 4374801Swnj 4384951Swnj while (ipq.next != &ipq) 43910735Ssam ip_freef(ipq.next); 4404801Swnj } 4414923Swnj 4424640Swnj /* 4434640Swnj * Do option processing on a datagram, 4444640Swnj * possibly discarding it if bad options 4454640Swnj * are encountered. 4464640Swnj */ 4474640Swnj ip_dooptions(ip) 4484640Swnj struct ip *ip; 4494495Swnj { 4504640Swnj register u_char *cp; 4516583Ssam int opt, optlen, cnt, code, type; 4524923Swnj struct in_addr *sin; 4534801Swnj register struct ip_timestamp *ipt; 4544951Swnj register struct ifnet *ifp; 4554951Swnj struct in_addr t; 4564495Swnj 4574640Swnj cp = (u_char *)(ip + 1); 4584640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 4594640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 4604640Swnj opt = cp[0]; 4614640Swnj if (opt == IPOPT_EOL) 4624640Swnj break; 4634640Swnj if (opt == IPOPT_NOP) 4644640Swnj optlen = 1; 46516392Ssam else { 4664640Swnj optlen = cp[1]; 46716392Ssam if (optlen <= 0) 46816392Ssam break; 46916392Ssam } 4704640Swnj switch (opt) { 4714495Swnj 4724640Swnj default: 4734640Swnj break; 4744495Swnj 4754951Swnj /* 4764951Swnj * Source routing with record. 4774951Swnj * Find interface with current destination address. 4784951Swnj * If none on this machine then drop if strictly routed, 4794951Swnj * or do nothing if loosely routed. 4804951Swnj * Record interface address and bring up next address 4814951Swnj * component. If strictly routed make sure next 4824951Swnj * address on directly accessible net. 4834951Swnj */ 4844640Swnj case IPOPT_LSRR: 4857508Sroot case IPOPT_SSRR: 4864801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 4874640Swnj break; 4884923Swnj sin = (struct in_addr *)(cp + cp[2]); 4896338Ssam ipaddr.sin_addr = *sin; 4906338Ssam ifp = if_ifwithaddr((struct sockaddr *)&ipaddr); 4916583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_SRCFAIL; 4924951Swnj if (ifp == 0) { 4934951Swnj if (opt == IPOPT_SSRR) 4944951Swnj goto bad; 4954951Swnj break; 4964640Swnj } 4974951Swnj t = ip->ip_dst; ip->ip_dst = *sin; *sin = t; 4984951Swnj cp[2] += 4; 4994951Swnj if (cp[2] > optlen - (sizeof (long) - 1)) 5004951Swnj break; 5014951Swnj ip->ip_dst = sin[1]; 5026338Ssam if (opt == IPOPT_SSRR && 5037166Ssam if_ifonnetof(in_netof(ip->ip_dst)) == 0) 5044951Swnj goto bad; 5054640Swnj break; 5064495Swnj 5074640Swnj case IPOPT_TS: 5086583Ssam code = cp - (u_char *)ip; 5096583Ssam type = ICMP_PARAMPROB; 5104801Swnj ipt = (struct ip_timestamp *)cp; 5114801Swnj if (ipt->ipt_len < 5) 5124640Swnj goto bad; 5134801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 5144801Swnj if (++ipt->ipt_oflw == 0) 5154640Swnj goto bad; 5164495Swnj break; 5174640Swnj } 5184923Swnj sin = (struct in_addr *)(cp+cp[2]); 5194801Swnj switch (ipt->ipt_flg) { 5204495Swnj 5214640Swnj case IPOPT_TS_TSONLY: 5224640Swnj break; 5234640Swnj 5244640Swnj case IPOPT_TS_TSANDADDR: 5254801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5264640Swnj goto bad; 5276338Ssam if (ifinet == 0) 5286338Ssam goto bad; /* ??? */ 5296338Ssam *sin++ = ((struct sockaddr_in *)&ifinet->if_addr)->sin_addr; 5304640Swnj break; 5314640Swnj 5324640Swnj case IPOPT_TS_PRESPEC: 5336338Ssam ipaddr.sin_addr = *sin; 53410142Ssam if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) 5354951Swnj continue; 5364801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5374640Swnj goto bad; 5384801Swnj ipt->ipt_ptr += 4; 5394640Swnj break; 5404640Swnj 5414495Swnj default: 5424640Swnj goto bad; 5434495Swnj } 5444923Swnj *(n_time *)sin = iptime(); 5454801Swnj ipt->ipt_ptr += 4; 5464640Swnj } 5474495Swnj } 5486583Ssam return (0); 5494640Swnj bad: 5506583Ssam icmp_error(ip, type, code); 5516583Ssam return (1); 5524495Swnj } 5534495Swnj 5544640Swnj /* 5554951Swnj * Strip out IP options, at higher 5564951Swnj * level protocol in the kernel. 5574951Swnj * Second argument is buffer to which options 5584951Swnj * will be moved, and return value is their length. 5594640Swnj */ 5605217Swnj ip_stripoptions(ip, mopt) 5614640Swnj struct ip *ip; 5625217Swnj struct mbuf *mopt; 5634495Swnj { 5644640Swnj register int i; 5654640Swnj register struct mbuf *m; 5664640Swnj int olen; 5674640Swnj 5684640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 5694951Swnj m = dtom(ip); 5704951Swnj ip++; 5715217Swnj if (mopt) { 5725217Swnj mopt->m_len = olen; 5735217Swnj mopt->m_off = MMINOFF; 57416544Skarels bcopy((caddr_t)ip, mtod(mopt, caddr_t), (unsigned)olen); 5755217Swnj } 5764640Swnj i = m->m_len - (sizeof (struct ip) + olen); 5774907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 5785243Sroot m->m_len -= olen; 5794495Swnj } 5806583Ssam 58114670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 5826583Ssam ECONNABORTED, ECONNABORTED, 0, 0, 58314670Ssam 0, 0, EHOSTDOWN, EHOSTUNREACH, 58414670Ssam ENETUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 58514670Ssam EMSGSIZE, 0, 0, 0, 5866583Ssam 0, 0, 0, 0 5876583Ssam }; 5886583Ssam 5896583Ssam ip_ctlinput(cmd, arg) 5906583Ssam int cmd; 5916583Ssam caddr_t arg; 5926583Ssam { 5938775Sroot struct in_addr *in; 5946590Ssam int tcp_abort(), udp_abort(); 5956583Ssam extern struct inpcb tcb, udb; 5966583Ssam 5976583Ssam if (cmd < 0 || cmd > PRC_NCMDS) 5986583Ssam return; 5996590Ssam if (inetctlerrmap[cmd] == 0) 6006583Ssam return; /* XXX */ 6016583Ssam if (cmd == PRC_IFDOWN) 6028775Sroot in = &((struct sockaddr_in *)arg)->sin_addr; 6036583Ssam else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH) 6048775Sroot in = (struct in_addr *)arg; 6056583Ssam else 6068775Sroot in = &((struct icmp *)arg)->icmp_ip.ip_dst; 6078597Sroot /* THIS IS VERY QUESTIONABLE, SHOULD HIT ALL PROTOCOLS */ 6088775Sroot in_pcbnotify(&tcb, in, (int)inetctlerrmap[cmd], tcp_abort); 6098775Sroot in_pcbnotify(&udb, in, (int)inetctlerrmap[cmd], udp_abort); 6106583Ssam } 6116583Ssam 6126583Ssam int ipprintfs = 0; 6136583Ssam int ipforwarding = 1; 6146583Ssam /* 6156583Ssam * Forward a packet. If some error occurs return the sender 6166583Ssam * and icmp packet. Note we can't always generate a meaningful 6176583Ssam * icmp message because icmp doesn't have a large enough repetoire 6186583Ssam * of codes and types. 6196583Ssam */ 6206583Ssam ip_forward(ip) 6216583Ssam register struct ip *ip; 6226583Ssam { 6236583Ssam register int error, type, code; 6246609Ssam struct mbuf *mopt, *mcopy; 6256583Ssam 6266583Ssam if (ipprintfs) 6276583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 6286583Ssam ip->ip_dst, ip->ip_ttl); 6296583Ssam if (ipforwarding == 0) { 6306583Ssam /* can't tell difference between net and host */ 6316583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_NET; 6326583Ssam goto sendicmp; 6336583Ssam } 6346583Ssam if (ip->ip_ttl < IPTTLDEC) { 6356583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 6366583Ssam goto sendicmp; 6376583Ssam } 6386583Ssam ip->ip_ttl -= IPTTLDEC; 6399641Ssam mopt = m_get(M_DONTWAIT, MT_DATA); 64011232Ssam if (mopt == NULL) { 6416583Ssam m_freem(dtom(ip)); 6426583Ssam return; 6436583Ssam } 6446609Ssam 6456609Ssam /* 6466609Ssam * Save at most 64 bytes of the packet in case 6476609Ssam * we need to generate an ICMP message to the src. 6486609Ssam */ 6497843Sroot mcopy = m_copy(dtom(ip), 0, imin(ip->ip_len, 64)); 6506583Ssam ip_stripoptions(ip, mopt); 6516583Ssam 65212416Ssam error = ip_output(dtom(ip), mopt, (struct route *)0, IP_FORWARDING); 65311540Ssam if (error == 0) { 6546609Ssam if (mcopy) 6556609Ssam m_freem(mcopy); 6566583Ssam return; 6576609Ssam } 65811540Ssam if (mcopy == NULL) 65911540Ssam return; 6606609Ssam ip = mtod(mcopy, struct ip *); 6616609Ssam type = ICMP_UNREACH, code = 0; /* need ``undefined'' */ 6626609Ssam switch (error) { 6636609Ssam 6646609Ssam case ENETUNREACH: 6656609Ssam case ENETDOWN: 6666583Ssam code = ICMP_UNREACH_NET; 6676609Ssam break; 6686609Ssam 6696609Ssam case EMSGSIZE: 6706583Ssam code = ICMP_UNREACH_NEEDFRAG; 6716609Ssam break; 6726609Ssam 6736609Ssam case EPERM: 6746609Ssam code = ICMP_UNREACH_PORT; 6756609Ssam break; 6766609Ssam 6776609Ssam case ENOBUFS: 6786609Ssam type = ICMP_SOURCEQUENCH; 6796609Ssam break; 6806609Ssam 6816609Ssam case EHOSTDOWN: 6826609Ssam case EHOSTUNREACH: 6836609Ssam code = ICMP_UNREACH_HOST; 6846609Ssam break; 6856609Ssam } 6866583Ssam sendicmp: 6876583Ssam icmp_error(ip, type, code); 6886583Ssam } 689