1*7166Ssam /* ip_input.c 1.44 82/06/13 */ 24571Swnj 34495Swnj #include "../h/param.h" 44543Swnj #include "../h/systm.h" 54640Swnj #include "../h/clock.h" 64640Swnj #include "../h/mbuf.h" 74898Swnj #include "../h/protosw.h" 84923Swnj #include "../h/socket.h" 95084Swnj #include "../net/in.h" 105084Swnj #include "../net/in_systm.h" 114951Swnj #include "../net/if.h" 125084Swnj #include "../net/ip.h" /* belongs before in.h */ 134898Swnj #include "../net/ip_var.h" 144801Swnj #include "../net/ip_icmp.h" 154801Swnj #include "../net/tcp.h" 166583Ssam #include <errno.h> 174495Swnj 184898Swnj u_char ip_protox[IPPROTO_MAX]; 196210Swnj int ipqmaxlen = IFQ_MAXLEN; 206338Ssam struct ifnet *ifinet; /* first inet interface */ 214898Swnj 224801Swnj /* 235172Swnj * IP initialization: fill in IP protocol switch table. 245161Swnj * All protocols not implemented in kernel go to raw IP protocol handler. 254801Swnj */ 264801Swnj ip_init() 274801Swnj { 284898Swnj register struct protosw *pr; 294898Swnj register int i; 304495Swnj 314951Swnj COUNT(IP_INIT); 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; 424801Swnj ip_id = time & 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 COUNT(IPINTR); 665084Swnj next: 674640Swnj /* 685084Swnj * Get next datagram off input queue and get IP header 695084Swnj * in first mbuf. 704640Swnj */ 715084Swnj s = splimp(); 725084Swnj IF_DEQUEUE(&ipintrq, m); 735084Swnj splx(s); 745218Swnj if (m == 0) 755084Swnj return; 765306Sroot if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) && 775306Sroot (m = m_pullup(m, sizeof (struct ip))) == 0) 785306Sroot return; 794640Swnj ip = mtod(m, struct ip *); 805161Swnj if ((hlen = ip->ip_hl << 2) > m->m_len) { 815306Sroot if ((m = m_pullup(m, hlen)) == 0) 825306Sroot return; 835161Swnj ip = mtod(m, struct ip *); 845161Swnj } 854951Swnj if (ipcksum) 865217Swnj if (ip->ip_sum = in_cksum(m, hlen)) { 875161Swnj printf("ip_sum %x\n", ip->ip_sum); /* XXX */ 884951Swnj ipstat.ips_badsum++; 894951Swnj goto bad; 904495Swnj } 914951Swnj 925217Swnj #if vax 934951Swnj /* 944951Swnj * Convert fields to host representation. 954951Swnj */ 964907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 974640Swnj ip->ip_id = ntohs(ip->ip_id); 984951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 995217Swnj #endif 1004495Swnj 1014543Swnj /* 1024640Swnj * Check that the amount of data in the buffers 1034640Swnj * is as at least much as the IP header would have us expect. 1044640Swnj * Trim mbufs if longer than we expect. 1054640Swnj * Drop packet if shorter than we expect. 1064543Swnj */ 1076475Sroot i = -ip->ip_len; 1085161Swnj m0 = m; 1096475Sroot for (;;) { 1104495Swnj i += m->m_len; 1116475Sroot if (m->m_next == 0) 1126475Sroot break; 1136475Sroot m = m->m_next; 1146088Sroot } 1156475Sroot if (i != 0) { 1166475Sroot if (i < 0) { 1175161Swnj ipstat.ips_tooshort++; 1184951Swnj goto bad; 1195161Swnj } 1206475Sroot if (i <= m->m_len) 1216475Sroot m->m_len -= i; 1226475Sroot else 1236475Sroot m_adj(m0, -i); 1244495Swnj } 1256475Sroot m = m0; 1264495Swnj 1274640Swnj /* 1284640Swnj * Process options and, if not destined for us, 1296583Ssam * ship it on. ip_dooptions returns 1 when an 1306583Ssam * error was detected (causing an icmp message 1316583Ssam * to be sent). 1324640Swnj */ 1336583Ssam if (hlen > sizeof (struct ip) && ip_dooptions(ip)) 1346583Ssam goto next; 1356210Swnj 1366338Ssam /* 1376350Ssam * Fast check on the first internet 1386350Ssam * interface in the list. 1396338Ssam */ 1406338Ssam if (ifinet) { 1416338Ssam struct sockaddr_in *sin; 1426338Ssam 1436338Ssam sin = (struct sockaddr_in *)&ifinet->if_addr; 1446338Ssam if (sin->sin_addr.s_addr == ip->ip_dst.s_addr) 1456338Ssam goto ours; 1466483Ssam sin = (struct sockaddr_in *)&ifinet->if_broadaddr; 1476350Ssam if ((ifinet->if_flags & IFF_BROADCAST) && 1486350Ssam sin->sin_addr.s_addr == ip->ip_dst.s_addr) 1496350Ssam goto ours; 1506338Ssam } 1516338Ssam ipaddr.sin_addr = ip->ip_dst; 1526338Ssam if (if_ifwithaddr((struct sockaddr *)&ipaddr) == 0) { 1536583Ssam ip_forward(ip); 1545084Swnj goto next; 1554543Swnj } 1564495Swnj 1576338Ssam ours: 1584640Swnj /* 1594640Swnj * Look for queue of fragments 1604640Swnj * of this datagram. 1614640Swnj */ 1624640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 1634640Swnj if (ip->ip_id == fp->ipq_id && 1644640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 1654640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 1664640Swnj ip->ip_p == fp->ipq_p) 1674640Swnj goto found; 1684640Swnj fp = 0; 1694640Swnj found: 1704495Swnj 1714640Swnj /* 1724640Swnj * Adjust ip_len to not reflect header, 1734640Swnj * set ip_mff if more fragments are expected, 1744640Swnj * convert offset of this to bytes. 1754640Swnj */ 1764640Swnj ip->ip_len -= hlen; 1774898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 1784640Swnj if (ip->ip_off & IP_MF) 1794898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 1804640Swnj ip->ip_off <<= 3; 1814495Swnj 1824640Swnj /* 1834640Swnj * If datagram marked as having more fragments 1844640Swnj * or if this is not the first fragment, 1854640Swnj * attempt reassembly; if it succeeds, proceed. 1864640Swnj */ 1874898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 1884898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 1894640Swnj if (ip == 0) 1905084Swnj goto next; 1914640Swnj hlen = ip->ip_hl << 2; 1924640Swnj m = dtom(ip); 1934640Swnj } else 1944640Swnj if (fp) 1954640Swnj (void) ip_freef(fp); 1964951Swnj 1974951Swnj /* 1984951Swnj * Switch out to protocol's input routine. 1994951Swnj */ 2004898Swnj (*protosw[ip_protox[ip->ip_p]].pr_input)(m); 2015084Swnj goto next; 2024951Swnj bad: 2034951Swnj m_freem(m); 2045084Swnj goto next; 2054640Swnj } 2064495Swnj 2074640Swnj /* 2084640Swnj * Take incoming datagram fragment and try to 2094951Swnj * reassemble it into whole datagram. If a chain for 2104640Swnj * reassembly of this datagram already exists, then it 2114640Swnj * is given as fp; otherwise have to make a chain. 2124640Swnj */ 2134640Swnj struct ip * 2144640Swnj ip_reass(ip, fp) 2154898Swnj register struct ipasfrag *ip; 2164640Swnj register struct ipq *fp; 2174640Swnj { 2184640Swnj register struct mbuf *m = dtom(ip); 2194898Swnj register struct ipasfrag *q; 2204640Swnj struct mbuf *t; 2214640Swnj int hlen = ip->ip_hl << 2; 2224640Swnj int i, next; 2234951Swnj COUNT(IP_REASS); 2244543Swnj 2254640Swnj /* 2264640Swnj * Presence of header sizes in mbufs 2274640Swnj * would confuse code below. 2284640Swnj */ 2294640Swnj m->m_off += hlen; 2304640Swnj m->m_len -= hlen; 2314495Swnj 2324640Swnj /* 2334640Swnj * If first fragment to arrive, create a reassembly queue. 2344640Swnj */ 2354640Swnj if (fp == 0) { 2365851Sroot if ((t = m_get(M_WAIT)) == NULL) 2374640Swnj goto dropfrag; 2384640Swnj t->m_off = MMINOFF; 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; 3534951Swnj COUNT(IP_FREEF); 3544495Swnj 3554898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3564640Swnj m_freem(dtom(q)); 3574640Swnj m = dtom(fp); 3584640Swnj fp = fp->next; 3594640Swnj remque(fp->prev); 3604907Swnj (void) m_free(m); 3614640Swnj return (fp); 3624495Swnj } 3634495Swnj 3644640Swnj /* 3654640Swnj * Put an ip fragment on a reassembly chain. 3664640Swnj * Like insque, but pointers in middle of structure. 3674640Swnj */ 3684640Swnj ip_enq(p, prev) 3694898Swnj register struct ipasfrag *p, *prev; 3704495Swnj { 3714951Swnj 3724640Swnj COUNT(IP_ENQ); 3734898Swnj p->ipf_prev = prev; 3744898Swnj p->ipf_next = prev->ipf_next; 3754898Swnj prev->ipf_next->ipf_prev = p; 3764898Swnj prev->ipf_next = p; 3774495Swnj } 3784495Swnj 3794640Swnj /* 3804640Swnj * To ip_enq as remque is to insque. 3814640Swnj */ 3824640Swnj ip_deq(p) 3834898Swnj register struct ipasfrag *p; 3844640Swnj { 3854951Swnj 3864640Swnj COUNT(IP_DEQ); 3874898Swnj p->ipf_prev->ipf_next = p->ipf_next; 3884898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 3894495Swnj } 3904495Swnj 3914640Swnj /* 3924640Swnj * IP timer processing; 3934640Swnj * if a timer expires on a reassembly 3944640Swnj * queue, discard it. 3954640Swnj */ 3964801Swnj ip_slowtimo() 3974495Swnj { 3984495Swnj register struct ipq *fp; 3994640Swnj int s = splnet(); 4004951Swnj 4014801Swnj COUNT(IP_SLOWTIMO); 4025243Sroot fp = ipq.next; 4035243Sroot if (fp == 0) { 4045243Sroot splx(s); 4055243Sroot return; 4065243Sroot } 4075243Sroot while (fp != &ipq) 4084640Swnj if (--fp->ipq_ttl == 0) 4094640Swnj fp = ip_freef(fp); 4104640Swnj else 4114640Swnj fp = fp->next; 4124640Swnj splx(s); 4134495Swnj } 4144495Swnj 4154951Swnj /* 4164951Swnj * Drain off all datagram fragments. 4174951Swnj */ 4184801Swnj ip_drain() 4194801Swnj { 4204801Swnj 4214951Swnj COUNT(IP_DRAIN); 4224951Swnj while (ipq.next != &ipq) 4234951Swnj (void) ip_freef(ipq.next); 4244801Swnj } 4254923Swnj 4264640Swnj /* 4274640Swnj * Do option processing on a datagram, 4284640Swnj * possibly discarding it if bad options 4294640Swnj * are encountered. 4304640Swnj */ 4314640Swnj ip_dooptions(ip) 4324640Swnj struct ip *ip; 4334495Swnj { 4344640Swnj register u_char *cp; 4356583Ssam int opt, optlen, cnt, code, type; 4364923Swnj struct in_addr *sin; 4374801Swnj register struct ip_timestamp *ipt; 4384951Swnj register struct ifnet *ifp; 4394951Swnj struct in_addr t; 4404495Swnj 4414951Swnj COUNT(IP_DOOPTIONS); 4424640Swnj cp = (u_char *)(ip + 1); 4434640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 4444640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 4454640Swnj opt = cp[0]; 4464640Swnj if (opt == IPOPT_EOL) 4474640Swnj break; 4484640Swnj if (opt == IPOPT_NOP) 4494640Swnj optlen = 1; 4504640Swnj else 4514640Swnj optlen = cp[1]; 4524640Swnj switch (opt) { 4534495Swnj 4544640Swnj default: 4554640Swnj break; 4564495Swnj 4574951Swnj /* 4584951Swnj * Source routing with record. 4594951Swnj * Find interface with current destination address. 4604951Swnj * If none on this machine then drop if strictly routed, 4614951Swnj * or do nothing if loosely routed. 4624951Swnj * Record interface address and bring up next address 4634951Swnj * component. If strictly routed make sure next 4644951Swnj * address on directly accessible net. 4654951Swnj */ 4664640Swnj case IPOPT_LSRR: 4674801Swnj if (cp[2] < 4 || cp[2] > optlen - (sizeof (long) - 1)) 4684640Swnj break; 4694923Swnj sin = (struct in_addr *)(cp + cp[2]); 4706338Ssam ipaddr.sin_addr = *sin; 4716338Ssam ifp = if_ifwithaddr((struct sockaddr *)&ipaddr); 4726583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_SRCFAIL; 4734951Swnj if (ifp == 0) { 4744951Swnj if (opt == IPOPT_SSRR) 4754951Swnj goto bad; 4764951Swnj break; 4774640Swnj } 4784951Swnj t = ip->ip_dst; ip->ip_dst = *sin; *sin = t; 4794951Swnj cp[2] += 4; 4804951Swnj if (cp[2] > optlen - (sizeof (long) - 1)) 4814951Swnj break; 4824951Swnj ip->ip_dst = sin[1]; 4836338Ssam if (opt == IPOPT_SSRR && 484*7166Ssam if_ifonnetof(in_netof(ip->ip_dst)) == 0) 4854951Swnj goto bad; 4864640Swnj break; 4874495Swnj 4884640Swnj case IPOPT_TS: 4896583Ssam code = cp - (u_char *)ip; 4906583Ssam type = ICMP_PARAMPROB; 4914801Swnj ipt = (struct ip_timestamp *)cp; 4924801Swnj if (ipt->ipt_len < 5) 4934640Swnj goto bad; 4944801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 4954801Swnj if (++ipt->ipt_oflw == 0) 4964640Swnj goto bad; 4974495Swnj break; 4984640Swnj } 4994923Swnj sin = (struct in_addr *)(cp+cp[2]); 5004801Swnj switch (ipt->ipt_flg) { 5014495Swnj 5024640Swnj case IPOPT_TS_TSONLY: 5034640Swnj break; 5044640Swnj 5054640Swnj case IPOPT_TS_TSANDADDR: 5064801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5074640Swnj goto bad; 5086338Ssam if (ifinet == 0) 5096338Ssam goto bad; /* ??? */ 5106338Ssam *sin++ = ((struct sockaddr_in *)&ifinet->if_addr)->sin_addr; 5114640Swnj break; 5124640Swnj 5134640Swnj case IPOPT_TS_PRESPEC: 5146338Ssam ipaddr.sin_addr = *sin; 5156583Ssam if (!if_ifwithaddr((struct sockaddr *)&ipaddr)) 5164951Swnj continue; 5174801Swnj if (ipt->ipt_ptr + 8 > ipt->ipt_len) 5184640Swnj goto bad; 5194801Swnj ipt->ipt_ptr += 4; 5204640Swnj break; 5214640Swnj 5224495Swnj default: 5234640Swnj goto bad; 5244495Swnj } 5254923Swnj *(n_time *)sin = iptime(); 5264801Swnj ipt->ipt_ptr += 4; 5274640Swnj } 5284495Swnj } 5296583Ssam return (0); 5304640Swnj bad: 5316583Ssam icmp_error(ip, type, code); 5326583Ssam return (1); 5334495Swnj } 5344495Swnj 5354640Swnj /* 5364951Swnj * Strip out IP options, at higher 5374951Swnj * level protocol in the kernel. 5384951Swnj * Second argument is buffer to which options 5394951Swnj * will be moved, and return value is their length. 5404640Swnj */ 5415217Swnj ip_stripoptions(ip, mopt) 5424640Swnj struct ip *ip; 5435217Swnj struct mbuf *mopt; 5444495Swnj { 5454640Swnj register int i; 5464640Swnj register struct mbuf *m; 5474640Swnj int olen; 5484951Swnj COUNT(IP_STRIPOPTIONS); 5494640Swnj 5504640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 5514951Swnj m = dtom(ip); 5524951Swnj ip++; 5535217Swnj if (mopt) { 5545217Swnj mopt->m_len = olen; 5555217Swnj mopt->m_off = MMINOFF; 5565217Swnj bcopy((caddr_t)ip, mtod(m, caddr_t), (unsigned)olen); 5575217Swnj } 5584640Swnj i = m->m_len - (sizeof (struct ip) + olen); 5594907Swnj bcopy((caddr_t)ip+olen, (caddr_t)ip, (unsigned)i); 5605243Sroot m->m_len -= olen; 5614495Swnj } 5626583Ssam 5636590Ssam u_char inetctlerrmap[] = { 5646583Ssam ECONNABORTED, ECONNABORTED, 0, 0, 5656609Ssam 0, 0, 5666609Ssam EHOSTDOWN, EHOSTUNREACH, ENETUNREACH, EHOSTUNREACH, 5676583Ssam ECONNREFUSED, ECONNREFUSED, EMSGSIZE, 0, 5686583Ssam 0, 0, 0, 0 5696583Ssam }; 5706583Ssam 5716583Ssam ip_ctlinput(cmd, arg) 5726583Ssam int cmd; 5736583Ssam caddr_t arg; 5746583Ssam { 5756583Ssam struct in_addr *sin; 5766590Ssam int tcp_abort(), udp_abort(); 5776583Ssam extern struct inpcb tcb, udb; 5786583Ssam 5796583Ssam if (cmd < 0 || cmd > PRC_NCMDS) 5806583Ssam return; 5816590Ssam if (inetctlerrmap[cmd] == 0) 5826583Ssam return; /* XXX */ 5836583Ssam if (cmd == PRC_IFDOWN) 5846583Ssam sin = &((struct sockaddr_in *)arg)->sin_addr; 5856583Ssam else if (cmd == PRC_HOSTDEAD || cmd == PRC_HOSTUNREACH) 5866583Ssam sin = (struct in_addr *)arg; 5876583Ssam else 5886583Ssam sin = &((struct icmp *)arg)->icmp_ip.ip_dst; 5896590Ssam in_pcbnotify(&tcb, sin, inetctlerrmap[cmd], tcp_abort); 5906590Ssam in_pcbnotify(&udb, sin, inetctlerrmap[cmd], udp_abort); 5916583Ssam } 5926583Ssam 5936583Ssam int ipprintfs = 0; 5946583Ssam int ipforwarding = 1; 5956583Ssam /* 5966583Ssam * Forward a packet. If some error occurs return the sender 5976583Ssam * and icmp packet. Note we can't always generate a meaningful 5986583Ssam * icmp message because icmp doesn't have a large enough repetoire 5996583Ssam * of codes and types. 6006583Ssam */ 6016583Ssam ip_forward(ip) 6026583Ssam register struct ip *ip; 6036583Ssam { 6046583Ssam register int error, type, code; 6056609Ssam struct mbuf *mopt, *mcopy; 6066583Ssam 6076583Ssam if (ipprintfs) 6086583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 6096583Ssam ip->ip_dst, ip->ip_ttl); 6106583Ssam if (ipforwarding == 0) { 6116583Ssam /* can't tell difference between net and host */ 6126583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_NET; 6136583Ssam goto sendicmp; 6146583Ssam } 6156583Ssam if (ip->ip_ttl < IPTTLDEC) { 6166583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 6176583Ssam goto sendicmp; 6186583Ssam } 6196583Ssam ip->ip_ttl -= IPTTLDEC; 6206583Ssam mopt = m_get(M_DONTWAIT); 6216583Ssam if (mopt == 0) { 6226583Ssam m_freem(dtom(ip)); 6236583Ssam return; 6246583Ssam } 6256609Ssam 6266609Ssam /* 6276609Ssam * Save at most 64 bytes of the packet in case 6286609Ssam * we need to generate an ICMP message to the src. 6296609Ssam */ 6306609Ssam mcopy = m_copy(dtom(ip), 0, min(ip->ip_len, 64)); 6316583Ssam ip_stripoptions(ip, mopt); 6326583Ssam 6336583Ssam /* last 0 here means no directed broadcast */ 6346609Ssam if ((error = ip_output(dtom(ip), mopt, 0, 0)) == 0) { 6356609Ssam if (mcopy) 6366609Ssam m_freem(mcopy); 6376583Ssam return; 6386609Ssam } 6396609Ssam ip = mtod(mcopy, struct ip *); 6406609Ssam type = ICMP_UNREACH, code = 0; /* need ``undefined'' */ 6416609Ssam switch (error) { 6426609Ssam 6436609Ssam case ENETUNREACH: 6446609Ssam case ENETDOWN: 6456583Ssam code = ICMP_UNREACH_NET; 6466609Ssam break; 6476609Ssam 6486609Ssam case EMSGSIZE: 6496583Ssam code = ICMP_UNREACH_NEEDFRAG; 6506609Ssam break; 6516609Ssam 6526609Ssam case EPERM: 6536609Ssam code = ICMP_UNREACH_PORT; 6546609Ssam break; 6556609Ssam 6566609Ssam case ENOBUFS: 6576609Ssam type = ICMP_SOURCEQUENCH; 6586609Ssam break; 6596609Ssam 6606609Ssam case EHOSTDOWN: 6616609Ssam case EHOSTUNREACH: 6626609Ssam code = ICMP_UNREACH_HOST; 6636609Ssam break; 6646609Ssam } 6656583Ssam sendicmp: 6666583Ssam icmp_error(ip, type, code); 6676583Ssam } 668