1*9030Sroot /* ip_input.c 1.58 82/11/03 */ 24571Swnj 34495Swnj #include "../h/param.h" 44543Swnj #include "../h/systm.h" 54640Swnj #include "../h/mbuf.h" 6*9030Sroot #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++) 40*9030Sroot ip_protox[i] = pr - inetsw; 41*9030Sroot for (pr = inetdomain.dom_protosw; 42*9030Sroot pr <= inetdomain.dom_protoswNPROTOSW; pr++) 434898Swnj if (pr->pr_family == PF_INET && 444898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 45*9030Sroot 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)) { 915161Swnj printf("ip_sum %x\n", ip->ip_sum); /* XXX */ 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 } 1536338Ssam ipaddr.sin_addr = ip->ip_dst; 1546338Ssam if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) { 1556583Ssam ip_forward(ip); 1565084Swnj goto next; 1574543Swnj } 1584495Swnj 1596338Ssam ours: 1604640Swnj /* 1614640Swnj * Look for queue of fragments 1624640Swnj * of this datagram. 1634640Swnj */ 1644640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1654640Swnj if (ip->ip_id == fp->ipq_id && 1664640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1674640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1684640Swnj ip->ip_p == fp->ipq_p) 1694640Swnj goto found; 1704640Swnj fp = 0; 1714640Swnj found: 1724495Swnj 1734640Swnj /* 1744640Swnj * Adjust ip_len to not reflect header, 1754640Swnj * set ip_mff if more fragments are expected, 1764640Swnj * convert offset of this to bytes. 1774640Swnj */ 1784640Swnj ip->ip_len -= hlen; 1794898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 1804640Swnj if (ip->ip_off & IP_MF) 1814898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 1824640Swnj ip->ip_off <<= 3; 1834495Swnj 1844640Swnj /* 1854640Swnj * If datagram marked as having more fragments 1864640Swnj * or if this is not the first fragment, 1874640Swnj * attempt reassembly; if it succeeds, proceed. 1884640Swnj */ 1894898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 1904898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 1914640Swnj if (ip == 0) 1925084Swnj goto next; 1934640Swnj hlen = ip->ip_hl << 2; 1944640Swnj m = dtom(ip); 1954640Swnj } else 1964640Swnj if (fp) 1974640Swnj (void) ip_freef(fp); 1984951Swnj 1994951Swnj /* 2004951Swnj * Switch out to protocol's input routine. 2014951Swnj */ 202*9030Sroot (*inetsw[ip_protox[ip->ip_p]].pr_input)(m); 2035084Swnj goto next; 2044951Swnj bad: 2054951Swnj m_freem(m); 2065084Swnj goto next; 2074640Swnj } 2084495Swnj 2094640Swnj /* 2104640Swnj * Take incoming datagram fragment and try to 2114951Swnj * reassemble it into whole datagram. If a chain for 2124640Swnj * reassembly of this datagram already exists, then it 2134640Swnj * is given as fp; otherwise have to make a chain. 2144640Swnj */ 2154640Swnj struct ip * 2164640Swnj ip_reass(ip, fp) 2174898Swnj register struct ipasfrag *ip; 2184640Swnj register struct ipq *fp; 2194640Swnj { 2204640Swnj register struct mbuf *m = dtom(ip); 2214898Swnj register struct ipasfrag *q; 2224640Swnj struct mbuf *t; 2234640Swnj int hlen = ip->ip_hl << 2; 2244640Swnj int i, next; 2254543Swnj 2264640Swnj /* 2274640Swnj * Presence of header sizes in mbufs 2284640Swnj * would confuse code below. 2294640Swnj */ 2304640Swnj m->m_off += hlen; 2314640Swnj m->m_len -= hlen; 2324495Swnj 2334640Swnj /* 2344640Swnj * If first fragment to arrive, create a reassembly queue. 2354640Swnj */ 2364640Swnj if (fp == 0) { 2375851Sroot if ((t = m_get(M_WAIT)) == NULL) 2384640Swnj goto dropfrag; 2394640Swnj fp = mtod(t, struct ipq *); 2404640Swnj insque(fp, &ipq); 2414640Swnj fp->ipq_ttl = IPFRAGTTL; 2424640Swnj fp->ipq_p = ip->ip_p; 2434640Swnj fp->ipq_id = ip->ip_id; 2444898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 2454898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 2464898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2475161Swnj q = (struct ipasfrag *)fp; 2485161Swnj goto insert; 2494640Swnj } 2504495Swnj 2514640Swnj /* 2524640Swnj * Find a segment which begins after this one does. 2534640Swnj */ 2544898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 2554640Swnj if (q->ip_off > ip->ip_off) 2564640Swnj break; 2574495Swnj 2584640Swnj /* 2594640Swnj * If there is a preceding segment, it may provide some of 2604640Swnj * our data already. If so, drop the data from the incoming 2614640Swnj * segment. If it provides all of our data, drop us. 2624640Swnj */ 2634898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 2644898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 2654640Swnj if (i > 0) { 2664640Swnj if (i >= ip->ip_len) 2674640Swnj goto dropfrag; 2684640Swnj m_adj(dtom(ip), i); 2694640Swnj ip->ip_off += i; 2704640Swnj ip->ip_len -= i; 2714640Swnj } 2724640Swnj } 2734543Swnj 2744640Swnj /* 2754640Swnj * While we overlap succeeding segments trim them or, 2764640Swnj * if they are completely covered, dequeue them. 2774640Swnj */ 2784898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 2794640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 2804640Swnj if (i < q->ip_len) { 2814640Swnj q->ip_len -= i; 2826256Sroot q->ip_off += i; 2834640Swnj m_adj(dtom(q), i); 2844640Swnj break; 2854495Swnj } 2864898Swnj q = q->ipf_next; 2874898Swnj m_freem(dtom(q->ipf_prev)); 2884898Swnj ip_deq(q->ipf_prev); 2894543Swnj } 2904495Swnj 2915161Swnj insert: 2924640Swnj /* 2934640Swnj * Stick new segment in its place; 2944640Swnj * check for complete reassembly. 2954640Swnj */ 2964898Swnj ip_enq(ip, q->ipf_prev); 2974640Swnj next = 0; 2984898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 2994640Swnj if (q->ip_off != next) 3004640Swnj return (0); 3014640Swnj next += q->ip_len; 3024640Swnj } 3034898Swnj if (q->ipf_prev->ipf_mff) 3044640Swnj return (0); 3054495Swnj 3064640Swnj /* 3074640Swnj * Reassembly is complete; concatenate fragments. 3084640Swnj */ 3094640Swnj q = fp->ipq_next; 3104640Swnj m = dtom(q); 3114640Swnj t = m->m_next; 3124640Swnj m->m_next = 0; 3134640Swnj m_cat(m, t); 3146298Swnj q = q->ipf_next; 3156298Swnj while (q != (struct ipasfrag *)fp) { 3166298Swnj t = dtom(q); 3176298Swnj q = q->ipf_next; 3186298Swnj m_cat(m, t); 3196298Swnj } 3204495Swnj 3214640Swnj /* 3224640Swnj * Create header for new ip packet by 3234640Swnj * modifying header of first packet; 3244640Swnj * dequeue and discard fragment reassembly header. 3254640Swnj * Make header visible. 3264640Swnj */ 3274640Swnj ip = fp->ipq_next; 3284640Swnj ip->ip_len = next; 3294898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 3304898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 3314640Swnj remque(fp); 3324907Swnj (void) m_free(dtom(fp)); 3334640Swnj m = dtom(ip); 3344898Swnj m->m_len += sizeof (struct ipasfrag); 3354898Swnj m->m_off -= sizeof (struct ipasfrag); 3364898Swnj return ((struct ip *)ip); 3374495Swnj 3384640Swnj dropfrag: 3394640Swnj m_freem(m); 3404640Swnj return (0); 3414495Swnj } 3424495Swnj 3434640Swnj /* 3444640Swnj * Free a fragment reassembly header and all 3454640Swnj * associated datagrams. 3464640Swnj */ 3474640Swnj struct ipq * 3484640Swnj ip_freef(fp) 3494640Swnj struct ipq *fp; 3504495Swnj { 3514898Swnj register struct ipasfrag *q; 3524640Swnj struct mbuf *m; 3534495Swnj 3544898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3554640Swnj m_freem(dtom(q)); 3564640Swnj m = dtom(fp); 3574640Swnj fp = fp->next; 3584640Swnj remque(fp->prev); 3594907Swnj (void) m_free(m); 3604640Swnj return (fp); 3614495Swnj } 3624495Swnj 3634640Swnj /* 3644640Swnj * Put an ip fragment on a reassembly chain. 3654640Swnj * Like insque, but pointers in middle of structure. 3664640Swnj */ 3674640Swnj ip_enq(p, prev) 3684898Swnj register struct ipasfrag *p, *prev; 3694495Swnj { 3704951Swnj 3714898Swnj p->ipf_prev = prev; 3724898Swnj p->ipf_next = prev->ipf_next; 3734898Swnj prev->ipf_next->ipf_prev = p; 3744898Swnj prev->ipf_next = p; 3754495Swnj } 3764495Swnj 3774640Swnj /* 3784640Swnj * To ip_enq as remque is to insque. 3794640Swnj */ 3804640Swnj ip_deq(p) 3814898Swnj register struct ipasfrag *p; 3824640Swnj { 3834951Swnj 3844898Swnj p->ipf_prev->ipf_next = p->ipf_next; 3854898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3864495Swnj } 3874495Swnj 3884640Swnj /* 3894640Swnj * IP timer processing; 3904640Swnj * if a timer expires on a reassembly 3914640Swnj * queue, discard it. 3924640Swnj */ 3934801Swnj ip_slowtimo() 3944495Swnj { 3954495Swnj register struct ipq *fp; 3964640Swnj int s = splnet(); 3974951Swnj 3985243Sroot fp = ipq.next; 3995243Sroot if (fp == 0) { 4005243Sroot splx(s); 4015243Sroot return; 4025243Sroot } 4035243Sroot while (fp != &ipq) 4044640Swnj if (--fp->ipq_ttl == 0) 4054640Swnj fp = ip_freef(fp); 4064640Swnj else 4074640Swnj fp = fp->next; 4084640Swnj splx(s); 4094495Swnj } 4104495Swnj 4114951Swnj /* 4124951Swnj * Drain off all datagram fragments. 4134951Swnj */ 4144801Swnj ip_drain() 4154801Swnj { 4164801Swnj 4174951Swnj while (ipq.next != &ipq) 4184951Swnj (void) ip_freef(ipq.next); 4194801Swnj } 4204923Swnj 4214640Swnj /* 4224640Swnj * Do option processing on a datagram, 4234640Swnj * possibly discarding it if bad options 4244640Swnj * are encountered. 4254640Swnj */ 4264640Swnj ip_dooptions(ip) 4274640Swnj struct ip *ip; 4284495Swnj { 4294640Swnj register u_char *cp; 4306583Ssam int opt, optlen, cnt, code, type; 4314923Swnj struct in_addr *sin; 4324801Swnj register struct ip_timestamp *ipt; 4334951Swnj register struct ifnet *ifp; 4344951Swnj struct in_addr t; 4354495Swnj 4364640Swnj cp = (u_char *)(ip + 1); 4374640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 4384640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 4394640Swnj opt = cp[0]; 4404640Swnj if (opt == IPOPT_EOL) 4414640Swnj break; 4424640Swnj if (opt == IPOPT_NOP) 4434640Swnj optlen = 1; 4444640Swnj else 4454640Swnj optlen = cp[1]; 4464640Swnj switch (opt) { 4474495Swnj 4484640Swnj default: 4494640Swnj break; 4504495Swnj 4514951Swnj /* 4524951Swnj * Source routing with record. 4534951Swnj * Find interface with current destination address. 4544951Swnj * If none on this machine then drop if strictly routed, 4554951Swnj * or do nothing if loosely routed. 4564951Swnj * Record interface address and bring up next address 4574951Swnj * component. If strictly routed make sure next 4584951Swnj * address on directly accessible net. 4594951Swnj */ 4604640Swnj case IPOPT_LSRR: 4617508Sroot case IPOPT_SSRR: 4624801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 4634640Swnj break; 4644923Swnj sin = (struct in_addr *)(cp + cp[2]); 4656338Ssam ipaddr.sin_addr = *sin; 4666338Ssam ifp = if_ifwithaddr((struct sockaddr *)&ipaddr); 4676583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_SRCFAIL; 4684951Swnj if (ifp == 0) { 4694951Swnj if (opt == IPOPT_SSRR) 4704951Swnj goto bad; 4714951Swnj break; 4724640Swnj } 4734951Swnj t = ip->ip_dst; ip->ip_dst = *sin; *sin = t; 4744951Swnj cp[2] += 4; 4754951Swnj if (cp[2] > optlen - (sizeof (long) - 1)) 4764951Swnj break; 4774951Swnj ip->ip_dst = sin[1]; 4786338Ssam if (opt == IPOPT_SSRR && 4797166Ssam if_ifonnetof(in_netof(ip->ip_dst)) == 0) 4804951Swnj goto bad; 4814640Swnj break; 4824495Swnj 4834640Swnj case IPOPT_TS: 4846583Ssam code = cp - (u_char *)ip; 4856583Ssam type = ICMP_PARAMPROB; 4864801Swnj ipt = (struct ip_timestamp *)cp; 4874801Swnj if (ipt->ipt_len < 5) 4884640Swnj goto bad; 4894801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 4904801Swnj if (++ipt->ipt_oflw == 0) 4914640Swnj goto bad; 4924495Swnj break; 4934640Swnj } 4944923Swnj sin = (struct in_addr *)(cp+cp[2]); 4954801Swnj switch (ipt->ipt_flg) { 4964495Swnj 4974640Swnj case IPOPT_TS_TSONLY: 4984640Swnj break; 4994640Swnj 5004640Swnj case IPOPT_TS_TSANDADDR: 5014801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5024640Swnj goto bad; 5036338Ssam if (ifinet == 0) 5046338Ssam goto bad; /* ??? */ 5056338Ssam *sin++ = ((struct sockaddr_in *)&ifinet->if_addr)->sin_addr; 5064640Swnj break; 5074640Swnj 5084640Swnj case IPOPT_TS_PRESPEC: 5096338Ssam ipaddr.sin_addr = *sin; 5106583Ssam if (!if_ifwithaddr((struct sockaddr *)&ipaddr)) 5114951Swnj continue; 5124801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5134640Swnj goto bad; 5144801Swnj ipt->ipt_ptr += 4; 5154640Swnj break; 5164640Swnj 5174495Swnj default: 5184640Swnj goto bad; 5194495Swnj } 5204923Swnj *(n_time *)sin = iptime(); 5214801Swnj ipt->ipt_ptr += 4; 5224640Swnj } 5234495Swnj } 5246583Ssam return (0); 5254640Swnj bad: 5266583Ssam icmp_error(ip, type, code); 5276583Ssam return (1); 5284495Swnj } 5294495Swnj 5304640Swnj /* 5314951Swnj * Strip out IP options, at higher 5324951Swnj * level protocol in the kernel. 5334951Swnj * Second argument is buffer to which options 5344951Swnj * will be moved, and return value is their length. 5354640Swnj */ 5365217Swnj ip_stripoptions(ip, mopt) 5374640Swnj struct ip *ip; 5385217Swnj struct mbuf *mopt; 5394495Swnj { 5404640Swnj register int i; 5414640Swnj register struct mbuf *m; 5424640Swnj int olen; 5434640Swnj 5444640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 5454951Swnj m = dtom(ip); 5464951Swnj ip++; 5475217Swnj if (mopt) { 5485217Swnj mopt->m_len = olen; 5495217Swnj mopt->m_off = MMINOFF; 5505217Swnj bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen); 5515217Swnj } 5524640Swnj i = m->m_len - (sizeof (struct ip) + olen); 5534907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 5545243Sroot m->m_len -= olen; 5554495Swnj } 5566583Ssam 5576590Ssam u_char inetctlerrmap[] = { 5586583Ssam ECONNABORTED, ECONNABORTED, 0, 0, 5596609Ssam 0, 0, 5606609Ssam EHOSTDOWN, EHOSTUNREACH, ENETUNREACH, EHOSTUNREACH, 5616583Ssam ECONNREFUSED, ECONNREFUSED, EMSGSIZE, 0, 5626583Ssam 0, 0, 0, 0 5636583Ssam }; 5646583Ssam 5656583Ssam ip_ctlinput(cmd, arg) 5666583Ssam int cmd; 5676583Ssam caddr_t arg; 5686583Ssam { 5698775Sroot struct in_addr *in; 5706590Ssam int tcp_abort(), udp_abort(); 5716583Ssam extern struct inpcb tcb, udb; 5726583Ssam 5736583Ssam if (cmd < 0 || cmd > PRC_NCMDS) 5746583Ssam return; 5756590Ssam if (inetctlerrmap[cmd] == 0) 5766583Ssam return; /* XXX */ 5776583Ssam if (cmd == PRC_IFDOWN) 5788775Sroot in = &((struct sockaddr_in *)arg)->sin_addr; 5796583Ssam else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH) 5808775Sroot in = (struct in_addr *)arg; 5816583Ssam else 5828775Sroot in = &((struct icmp *)arg)->icmp_ip.ip_dst; 5838597Sroot /* THIS IS VERY QUESTIONABLE, SHOULD HIT ALL PROTOCOLS */ 5848775Sroot in_pcbnotify(&tcb, in, (int)inetctlerrmap[cmd], tcp_abort); 5858775Sroot in_pcbnotify(&udb, in, (int)inetctlerrmap[cmd], udp_abort); 5866583Ssam } 5876583Ssam 5886583Ssam int ipprintfs = 0; 5896583Ssam int ipforwarding = 1; 5906583Ssam /* 5916583Ssam * Forward a packet. If some error occurs return the sender 5926583Ssam * and icmp packet. Note we can't always generate a meaningful 5936583Ssam * icmp message because icmp doesn't have a large enough repetoire 5946583Ssam * of codes and types. 5956583Ssam */ 5966583Ssam ip_forward(ip) 5976583Ssam register struct ip *ip; 5986583Ssam { 5996583Ssam register int error, type, code; 6006609Ssam struct mbuf *mopt, *mcopy; 6016583Ssam 6026583Ssam if (ipprintfs) 6036583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 6046583Ssam ip->ip_dst, ip->ip_ttl); 6056583Ssam if (ipforwarding == 0) { 6066583Ssam /* can't tell difference between net and host */ 6076583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_NET; 6086583Ssam goto sendicmp; 6096583Ssam } 6106583Ssam if (ip->ip_ttl < IPTTLDEC) { 6116583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 6126583Ssam goto sendicmp; 6136583Ssam } 6146583Ssam ip->ip_ttl -= IPTTLDEC; 6156583Ssam mopt = m_get(M_DONTWAIT); 6166583Ssam if (mopt == 0) { 6176583Ssam m_freem(dtom(ip)); 6186583Ssam return; 6196583Ssam } 6206609Ssam 6216609Ssam /* 6226609Ssam * Save at most 64 bytes of the packet in case 6236609Ssam * we need to generate an ICMP message to the src. 6246609Ssam */ 6257843Sroot mcopy = m_copy(dtom(ip), 0, imin(ip->ip_len, 64)); 6266583Ssam ip_stripoptions(ip, mopt); 6276583Ssam 6286583Ssam /* last 0 here means no directed broadcast */ 6298637Sroot if ((error = ip_output(dtom(ip), mopt, (struct route *)0, 0)) == 0) { 6306609Ssam if (mcopy) 6316609Ssam m_freem(mcopy); 6326583Ssam return; 6336609Ssam } 6346609Ssam ip = mtod(mcopy, struct ip *); 6356609Ssam type = ICMP_UNREACH, code = 0; /* need ``undefined'' */ 6366609Ssam switch (error) { 6376609Ssam 6386609Ssam case ENETUNREACH: 6396609Ssam case ENETDOWN: 6406583Ssam code = ICMP_UNREACH_NET; 6416609Ssam break; 6426609Ssam 6436609Ssam case EMSGSIZE: 6446583Ssam code = ICMP_UNREACH_NEEDFRAG; 6456609Ssam break; 6466609Ssam 6476609Ssam case EPERM: 6486609Ssam code = ICMP_UNREACH_PORT; 6496609Ssam break; 6506609Ssam 6516609Ssam case ENOBUFS: 6526609Ssam type = ICMP_SOURCEQUENCH; 6536609Ssam break; 6546609Ssam 6556609Ssam case EHOSTDOWN: 6566609Ssam case EHOSTUNREACH: 6576609Ssam code = ICMP_UNREACH_HOST; 6586609Ssam break; 6596609Ssam } 6606583Ssam sendicmp: 6616583Ssam icmp_error(ip, type, code); 6626583Ssam } 663