123184Smckusick /* 229143Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 332787Sbostic * All rights reserved. 423184Smckusick * 532787Sbostic * Redistribution and use in source and binary forms are permitted 6*34854Sbostic * provided that the above copyright notice and this paragraph are 7*34854Sbostic * duplicated in all such forms and that any documentation, 8*34854Sbostic * advertising materials, and other materials related to such 9*34854Sbostic * distribution and use acknowledge that the software was developed 10*34854Sbostic * by the University of California, Berkeley. The name of the 11*34854Sbostic * University may not be used to endorse or promote products derived 12*34854Sbostic * from this software without specific prior written permission. 13*34854Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34854Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34854Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1632787Sbostic * 17*34854Sbostic * @(#)ip_input.c 7.10 (Berkeley) 06/29/88 1823184Smckusick */ 194571Swnj 2017060Sbloom #include "param.h" 2117060Sbloom #include "systm.h" 2217060Sbloom #include "mbuf.h" 2317060Sbloom #include "domain.h" 2417060Sbloom #include "protosw.h" 2517060Sbloom #include "socket.h" 2617060Sbloom #include "errno.h" 2717060Sbloom #include "time.h" 2817060Sbloom #include "kernel.h" 298695Sroot 308695Sroot #include "../net/if.h" 318695Sroot #include "../net/route.h" 3210892Ssam 3317060Sbloom #include "in.h" 3417060Sbloom #include "in_pcb.h" 3517060Sbloom #include "in_systm.h" 3618376Skarels #include "in_var.h" 3717060Sbloom #include "ip.h" 3817060Sbloom #include "ip_var.h" 3917060Sbloom #include "ip_icmp.h" 4017060Sbloom #include "tcp.h" 414495Swnj 424898Swnj u_char ip_protox[IPPROTO_MAX]; 436210Swnj int ipqmaxlen = IFQ_MAXLEN; 4418376Skarels struct in_ifaddr *in_ifaddr; /* first inet address */ 454898Swnj 464801Swnj /* 4724813Skarels * We need to save the IP options in case a protocol wants to respond 4824813Skarels * to an incoming packet over the same route if the packet got here 4924813Skarels * using IP source routing. This allows connection establishment and 5024813Skarels * maintenance when the remote end is on a network that is not known 5124813Skarels * to us. 5224813Skarels */ 5324813Skarels int ip_nhops = 0; 5424813Skarels static struct ip_srcrt { 5524813Skarels char nop; /* one NOP to align */ 5624813Skarels char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 5724813Skarels struct in_addr route[MAX_IPOPTLEN]; 5824813Skarels } ip_srcrt; 5924813Skarels 6024813Skarels /* 615172Swnj * IP initialization: fill in IP protocol switch table. 625161Swnj * All protocols not implemented in kernel go to raw IP protocol handler. 634801Swnj */ 644801Swnj ip_init() 654801Swnj { 664898Swnj register struct protosw *pr; 674898Swnj register int i; 684495Swnj 6924813Skarels pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 704898Swnj if (pr == 0) 714898Swnj panic("ip_init"); 724898Swnj for (i = 0; i < IPPROTO_MAX; i++) 739030Sroot ip_protox[i] = pr - inetsw; 749030Sroot for (pr = inetdomain.dom_protosw; 7517551Skarels pr < inetdomain.dom_protoswNPROTOSW; pr++) 7616990Skarels if (pr->pr_domain->dom_family == PF_INET && 774898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 789030Sroot ip_protox[pr->pr_protocol] = pr - inetsw; 794801Swnj ipq.next = ipq.prev = &ipq; 808172Sroot ip_id = time.tv_sec & 0xffff; 816210Swnj ipintrq.ifq_maxlen = ipqmaxlen; 824801Swnj } 834801Swnj 844898Swnj u_char ipcksum = 1; 854640Swnj struct ip *ip_reass(); 866338Ssam struct sockaddr_in ipaddr = { AF_INET }; 8724813Skarels struct route ipforward_rt; 884640Swnj 894640Swnj /* 904640Swnj * Ip input routine. Checksum and byte swap header. If fragmented 914640Swnj * try to reassamble. If complete and fragment queue exists, discard. 924640Swnj * Process options. Pass to next level. 934640Swnj */ 945084Swnj ipintr() 954495Swnj { 964923Swnj register struct ip *ip; 975084Swnj register struct mbuf *m; 988597Sroot struct mbuf *m0; 994640Swnj register int i; 1004495Swnj register struct ipq *fp; 10118376Skarels register struct in_ifaddr *ia; 10224813Skarels struct ifnet *ifp; 1035084Swnj int hlen, s; 1044495Swnj 1055084Swnj next: 1064640Swnj /* 1075084Swnj * Get next datagram off input queue and get IP header 1085084Swnj * in first mbuf. 1094640Swnj */ 1105084Swnj s = splimp(); 11124813Skarels IF_DEQUEUEIF(&ipintrq, m, ifp); 1125084Swnj splx(s); 1135218Swnj if (m == 0) 1145084Swnj return; 11526001Skarels /* 11626001Skarels * If no IP addresses have been set yet but the interfaces 11726001Skarels * are receiving, can't do anything with incoming packets yet. 11826001Skarels */ 11926001Skarels if (in_ifaddr == NULL) 12026001Skarels goto bad; 12125920Skarels ipstat.ips_total++; 1225306Sroot if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) && 12311232Ssam (m = m_pullup(m, sizeof (struct ip))) == 0) { 12411232Ssam ipstat.ips_toosmall++; 12511232Ssam goto next; 12611232Ssam } 1274640Swnj ip = mtod(m, struct ip *); 12818376Skarels hlen = ip->ip_hl << 2; 12924813Skarels if (hlen < sizeof(struct ip)) { /* minimum header length */ 13018376Skarels ipstat.ips_badhlen++; 13121117Skarels goto bad; 13218376Skarels } 13318376Skarels if (hlen > m->m_len) { 13411232Ssam if ((m = m_pullup(m, hlen)) == 0) { 13511232Ssam ipstat.ips_badhlen++; 13611232Ssam goto next; 13711232Ssam } 1385161Swnj ip = mtod(m, struct ip *); 1395161Swnj } 1404951Swnj if (ipcksum) 1415217Swnj if (ip->ip_sum = in_cksum(m, hlen)) { 1424951Swnj ipstat.ips_badsum++; 1434951Swnj goto bad; 1444495Swnj } 1454951Swnj 1464951Swnj /* 1474951Swnj * Convert fields to host representation. 1484951Swnj */ 1494907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 15011232Ssam if (ip->ip_len < hlen) { 15111232Ssam ipstat.ips_badlen++; 15211232Ssam goto bad; 15311232Ssam } 1544640Swnj ip->ip_id = ntohs(ip->ip_id); 1554951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 1564495Swnj 1574543Swnj /* 1584640Swnj * Check that the amount of data in the buffers 1594640Swnj * is as at least much as the IP header would have us expect. 1604640Swnj * Trim mbufs if longer than we expect. 1614640Swnj * Drop packet if shorter than we expect. 1624543Swnj */ 16324813Skarels i = -(u_short)ip->ip_len; 1645161Swnj m0 = m; 1656475Sroot for (;;) { 1664495Swnj i += m->m_len; 1676475Sroot if (m->m_next == 0) 1686475Sroot break; 1696475Sroot m = m->m_next; 1706088Sroot } 1716475Sroot if (i != 0) { 1726475Sroot if (i < 0) { 1735161Swnj ipstat.ips_tooshort++; 17417358Skarels m = m0; 1754951Swnj goto bad; 1765161Swnj } 1776475Sroot if (i <= m->m_len) 1786475Sroot m->m_len -= i; 1796475Sroot else 1806475Sroot m_adj(m0, -i); 1814495Swnj } 1826475Sroot m = m0; 1834495Swnj 1844640Swnj /* 1854640Swnj * Process options and, if not destined for us, 1866583Ssam * ship it on. ip_dooptions returns 1 when an 1876583Ssam * error was detected (causing an icmp message 18821117Skarels * to be sent and the original packet to be freed). 1894640Swnj */ 19024813Skarels ip_nhops = 0; /* for source routed packets */ 19126031Skarels if (hlen > sizeof (struct ip) && ip_dooptions(ip, ifp)) 1926583Ssam goto next; 1936210Swnj 1946338Ssam /* 19518376Skarels * Check our list of addresses, to see if the packet is for us. 1966338Ssam */ 19718376Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) { 19818376Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 1996338Ssam 20018376Skarels if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 20124813Skarels goto ours; 20225195Skarels if ( 20325195Skarels #ifdef DIRECTED_BROADCAST 20425195Skarels ia->ia_ifp == ifp && 20525195Skarels #endif 20625195Skarels (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 20726247Skarels u_long t; 20825195Skarels 20925195Skarels if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 21025195Skarels ip->ip_dst.s_addr) 21125195Skarels goto ours; 21225195Skarels if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 21325195Skarels goto ours; 21425195Skarels /* 21525195Skarels * Look for all-0's host part (old broadcast addr), 21625195Skarels * either for subnet or net. 21725195Skarels */ 21826247Skarels t = ntohl(ip->ip_dst.s_addr); 21926247Skarels if (t == ia->ia_subnet) 22025195Skarels goto ours; 22126247Skarels if (t == ia->ia_net) 22225195Skarels goto ours; 22325195Skarels } 2246338Ssam } 22524813Skarels if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 22624813Skarels goto ours; 22724813Skarels if (ip->ip_dst.s_addr == INADDR_ANY) 22824813Skarels goto ours; 2294495Swnj 2304640Swnj /* 23124813Skarels * Not for us; forward if possible and desirable. 23224813Skarels */ 23324813Skarels ip_forward(ip, ifp); 23424813Skarels goto next; 23524813Skarels 23624813Skarels ours: 23724813Skarels /* 23833743Skarels * If offset or IP_MF are set, must reassemble. 23933743Skarels * Otherwise, nothing need be done. 24033743Skarels * (We could look in the reassembly queue to see 24133743Skarels * if the packet was previously fragmented, 24233743Skarels * but it's not worth the time; just let them time out.) 2434640Swnj */ 24433743Skarels if (ip->ip_off &~ IP_DF) { 24533743Skarels /* 24633743Skarels * Look for queue of fragments 24733743Skarels * of this datagram. 24833743Skarels */ 24933743Skarels for (fp = ipq.next; fp != &ipq; fp = fp->next) 25033743Skarels if (ip->ip_id == fp->ipq_id && 25133743Skarels ip->ip_src.s_addr == fp->ipq_src.s_addr && 25233743Skarels ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 25333743Skarels ip->ip_p == fp->ipq_p) 25433743Skarels goto found; 25533743Skarels fp = 0; 2564640Swnj found: 2574495Swnj 25833743Skarels /* 25933743Skarels * Adjust ip_len to not reflect header, 26033743Skarels * set ip_mff if more fragments are expected, 26133743Skarels * convert offset of this to bytes. 26233743Skarels */ 26333743Skarels ip->ip_len -= hlen; 26433743Skarels ((struct ipasfrag *)ip)->ipf_mff = 0; 26533743Skarels if (ip->ip_off & IP_MF) 26633743Skarels ((struct ipasfrag *)ip)->ipf_mff = 1; 26733743Skarels ip->ip_off <<= 3; 2684495Swnj 26933743Skarels /* 27033743Skarels * If datagram marked as having more fragments 27133743Skarels * or if this is not the first fragment, 27233743Skarels * attempt reassembly; if it succeeds, proceed. 27333743Skarels */ 27433743Skarels if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 27533743Skarels ipstat.ips_fragments++; 27633743Skarels ip = ip_reass((struct ipasfrag *)ip, fp); 27733743Skarels if (ip == 0) 27833743Skarels goto next; 27933743Skarels m = dtom(ip); 28033743Skarels } else 28133743Skarels if (fp) 28233743Skarels ip_freef(fp); 2834640Swnj } else 28433743Skarels ip->ip_len -= hlen; 2854951Swnj 2864951Swnj /* 2874951Swnj * Switch out to protocol's input routine. 2884951Swnj */ 28924813Skarels (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, ifp); 2905084Swnj goto next; 2914951Swnj bad: 2924951Swnj m_freem(m); 2935084Swnj goto next; 2944640Swnj } 2954495Swnj 2964640Swnj /* 2974640Swnj * Take incoming datagram fragment and try to 2984951Swnj * reassemble it into whole datagram. If a chain for 2994640Swnj * reassembly of this datagram already exists, then it 3004640Swnj * is given as fp; otherwise have to make a chain. 3014640Swnj */ 3024640Swnj struct ip * 3034640Swnj ip_reass(ip, fp) 3044898Swnj register struct ipasfrag *ip; 3054640Swnj register struct ipq *fp; 3064640Swnj { 3074640Swnj register struct mbuf *m = dtom(ip); 3084898Swnj register struct ipasfrag *q; 3094640Swnj struct mbuf *t; 3104640Swnj int hlen = ip->ip_hl << 2; 3114640Swnj int i, next; 3124543Swnj 3134640Swnj /* 3144640Swnj * Presence of header sizes in mbufs 3154640Swnj * would confuse code below. 3164640Swnj */ 3174640Swnj m->m_off += hlen; 3184640Swnj m->m_len -= hlen; 3194495Swnj 3204640Swnj /* 3214640Swnj * If first fragment to arrive, create a reassembly queue. 3224640Swnj */ 3234640Swnj if (fp == 0) { 32431201Skarels if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 3254640Swnj goto dropfrag; 3264640Swnj fp = mtod(t, struct ipq *); 3274640Swnj insque(fp, &ipq); 3284640Swnj fp->ipq_ttl = IPFRAGTTL; 3294640Swnj fp->ipq_p = ip->ip_p; 3304640Swnj fp->ipq_id = ip->ip_id; 3314898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 3324898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 3334898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 3345161Swnj q = (struct ipasfrag *)fp; 3355161Swnj goto insert; 3364640Swnj } 3374495Swnj 3384640Swnj /* 3394640Swnj * Find a segment which begins after this one does. 3404640Swnj */ 3414898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3424640Swnj if (q->ip_off > ip->ip_off) 3434640Swnj break; 3444495Swnj 3454640Swnj /* 3464640Swnj * If there is a preceding segment, it may provide some of 3474640Swnj * our data already. If so, drop the data from the incoming 3484640Swnj * segment. If it provides all of our data, drop us. 3494640Swnj */ 3504898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 3514898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 3524640Swnj if (i > 0) { 3534640Swnj if (i >= ip->ip_len) 3544640Swnj goto dropfrag; 3554640Swnj m_adj(dtom(ip), i); 3564640Swnj ip->ip_off += i; 3574640Swnj ip->ip_len -= i; 3584640Swnj } 3594640Swnj } 3604543Swnj 3614640Swnj /* 3624640Swnj * While we overlap succeeding segments trim them or, 3634640Swnj * if they are completely covered, dequeue them. 3644640Swnj */ 3654898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 3664640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 3674640Swnj if (i < q->ip_len) { 3684640Swnj q->ip_len -= i; 3696256Sroot q->ip_off += i; 3704640Swnj m_adj(dtom(q), i); 3714640Swnj break; 3724495Swnj } 3734898Swnj q = q->ipf_next; 3744898Swnj m_freem(dtom(q->ipf_prev)); 3754898Swnj ip_deq(q->ipf_prev); 3764543Swnj } 3774495Swnj 3785161Swnj insert: 3794640Swnj /* 3804640Swnj * Stick new segment in its place; 3814640Swnj * check for complete reassembly. 3824640Swnj */ 3834898Swnj ip_enq(ip, q->ipf_prev); 3844640Swnj next = 0; 3854898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 3864640Swnj if (q->ip_off != next) 3874640Swnj return (0); 3884640Swnj next += q->ip_len; 3894640Swnj } 3904898Swnj if (q->ipf_prev->ipf_mff) 3914640Swnj return (0); 3924495Swnj 3934640Swnj /* 3944640Swnj * Reassembly is complete; concatenate fragments. 3954640Swnj */ 3964640Swnj q = fp->ipq_next; 3974640Swnj m = dtom(q); 3984640Swnj t = m->m_next; 3994640Swnj m->m_next = 0; 4004640Swnj m_cat(m, t); 4016298Swnj q = q->ipf_next; 4026298Swnj while (q != (struct ipasfrag *)fp) { 4036298Swnj t = dtom(q); 4046298Swnj q = q->ipf_next; 4056298Swnj m_cat(m, t); 4066298Swnj } 4074495Swnj 4084640Swnj /* 4094640Swnj * Create header for new ip packet by 4104640Swnj * modifying header of first packet; 4114640Swnj * dequeue and discard fragment reassembly header. 4124640Swnj * Make header visible. 4134640Swnj */ 4144640Swnj ip = fp->ipq_next; 4154640Swnj ip->ip_len = next; 4164898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 4174898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 4184640Swnj remque(fp); 4194907Swnj (void) m_free(dtom(fp)); 4204640Swnj m = dtom(ip); 42124813Skarels m->m_len += (ip->ip_hl << 2); 42224813Skarels m->m_off -= (ip->ip_hl << 2); 4234898Swnj return ((struct ip *)ip); 4244495Swnj 4254640Swnj dropfrag: 42624813Skarels ipstat.ips_fragdropped++; 4274640Swnj m_freem(m); 4284640Swnj return (0); 4294495Swnj } 4304495Swnj 4314640Swnj /* 4324640Swnj * Free a fragment reassembly header and all 4334640Swnj * associated datagrams. 4344640Swnj */ 4354640Swnj ip_freef(fp) 4364640Swnj struct ipq *fp; 4374495Swnj { 43810735Ssam register struct ipasfrag *q, *p; 4394495Swnj 44010735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 44110735Ssam p = q->ipf_next; 44210735Ssam ip_deq(q); 4434640Swnj m_freem(dtom(q)); 44410735Ssam } 44510735Ssam remque(fp); 44610735Ssam (void) m_free(dtom(fp)); 4474495Swnj } 4484495Swnj 4494640Swnj /* 4504640Swnj * Put an ip fragment on a reassembly chain. 4514640Swnj * Like insque, but pointers in middle of structure. 4524640Swnj */ 4534640Swnj ip_enq(p, prev) 4544898Swnj register struct ipasfrag *p, *prev; 4554495Swnj { 4564951Swnj 4574898Swnj p->ipf_prev = prev; 4584898Swnj p->ipf_next = prev->ipf_next; 4594898Swnj prev->ipf_next->ipf_prev = p; 4604898Swnj prev->ipf_next = p; 4614495Swnj } 4624495Swnj 4634640Swnj /* 4644640Swnj * To ip_enq as remque is to insque. 4654640Swnj */ 4664640Swnj ip_deq(p) 4674898Swnj register struct ipasfrag *p; 4684640Swnj { 4694951Swnj 4704898Swnj p->ipf_prev->ipf_next = p->ipf_next; 4714898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 4724495Swnj } 4734495Swnj 4744640Swnj /* 4754640Swnj * IP timer processing; 4764640Swnj * if a timer expires on a reassembly 4774640Swnj * queue, discard it. 4784640Swnj */ 4794801Swnj ip_slowtimo() 4804495Swnj { 4814495Swnj register struct ipq *fp; 4824640Swnj int s = splnet(); 4834951Swnj 4845243Sroot fp = ipq.next; 4855243Sroot if (fp == 0) { 4865243Sroot splx(s); 4875243Sroot return; 4885243Sroot } 48910735Ssam while (fp != &ipq) { 49010735Ssam --fp->ipq_ttl; 49110735Ssam fp = fp->next; 49224813Skarels if (fp->prev->ipq_ttl == 0) { 49324813Skarels ipstat.ips_fragtimeout++; 49410735Ssam ip_freef(fp->prev); 49524813Skarels } 49610735Ssam } 4974640Swnj splx(s); 4984495Swnj } 4994495Swnj 5004951Swnj /* 5014951Swnj * Drain off all datagram fragments. 5024951Swnj */ 5034801Swnj ip_drain() 5044801Swnj { 5054801Swnj 50624813Skarels while (ipq.next != &ipq) { 50724813Skarels ipstat.ips_fragdropped++; 50810735Ssam ip_freef(ipq.next); 50924813Skarels } 5104801Swnj } 5114923Swnj 51230925Skarels extern struct in_ifaddr *ifptoia(); 51324813Skarels struct in_ifaddr *ip_rtaddr(); 51424813Skarels 5154640Swnj /* 5164640Swnj * Do option processing on a datagram, 5174640Swnj * possibly discarding it if bad options 5184640Swnj * are encountered. 5194640Swnj */ 52026031Skarels ip_dooptions(ip, ifp) 52126031Skarels register struct ip *ip; 52226031Skarels struct ifnet *ifp; 5234495Swnj { 5244640Swnj register u_char *cp; 52524813Skarels int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB; 52624813Skarels register struct ip_timestamp *ipt; 52724813Skarels register struct in_ifaddr *ia; 5284923Swnj struct in_addr *sin; 52924813Skarels n_time ntime; 5304495Swnj 5314640Swnj cp = (u_char *)(ip + 1); 5324640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 5334640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 53424813Skarels opt = cp[IPOPT_OPTVAL]; 5354640Swnj if (opt == IPOPT_EOL) 5364640Swnj break; 5374640Swnj if (opt == IPOPT_NOP) 5384640Swnj optlen = 1; 53916392Ssam else { 54024813Skarels optlen = cp[IPOPT_OLEN]; 54124813Skarels if (optlen <= 0 || optlen > cnt) { 54224813Skarels code = &cp[IPOPT_OLEN] - (u_char *)ip; 54317551Skarels goto bad; 54424813Skarels } 54516392Ssam } 5464640Swnj switch (opt) { 5474495Swnj 5484640Swnj default: 5494640Swnj break; 5504495Swnj 5514951Swnj /* 5524951Swnj * Source routing with record. 5534951Swnj * Find interface with current destination address. 5544951Swnj * If none on this machine then drop if strictly routed, 5554951Swnj * or do nothing if loosely routed. 5564951Swnj * Record interface address and bring up next address 5574951Swnj * component. If strictly routed make sure next 5584951Swnj * address on directly accessible net. 5594951Swnj */ 5604640Swnj case IPOPT_LSRR: 5617508Sroot case IPOPT_SSRR: 56224813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 56324813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 56424813Skarels goto bad; 56524813Skarels } 56624813Skarels ipaddr.sin_addr = ip->ip_dst; 56724813Skarels ia = (struct in_ifaddr *) 56824813Skarels ifa_ifwithaddr((struct sockaddr *)&ipaddr); 56924813Skarels if (ia == 0) { 57024813Skarels if (opt == IPOPT_SSRR) { 57124813Skarels type = ICMP_UNREACH; 57224813Skarels code = ICMP_UNREACH_SRCFAIL; 5734951Swnj goto bad; 57424813Skarels } 57524813Skarels /* 57624813Skarels * Loose routing, and not at next destination 57724813Skarels * yet; nothing to do except forward. 57824813Skarels */ 5794951Swnj break; 5804640Swnj } 58124813Skarels off--; /* 0 origin */ 58224813Skarels if (off > optlen - sizeof(struct in_addr)) { 58324813Skarels /* 58424813Skarels * End of source route. Should be for us. 58524813Skarels */ 58624813Skarels save_rte(cp, ip->ip_src); 5874951Swnj break; 58824813Skarels } 58924813Skarels /* 59024813Skarels * locate outgoing interface 59124813Skarels */ 59226384Skarels bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 59324813Skarels sizeof(ipaddr.sin_addr)); 59424813Skarels if ((opt == IPOPT_SSRR && 59524813Skarels in_iaonnetof(in_netof(ipaddr.sin_addr)) == 0) || 59624813Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 59724813Skarels type = ICMP_UNREACH; 59824813Skarels code = ICMP_UNREACH_SRCFAIL; 5994951Swnj goto bad; 60024813Skarels } 60124813Skarels ip->ip_dst = ipaddr.sin_addr; 60226384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 60326384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 60424813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 6054640Swnj break; 6064495Swnj 60724813Skarels case IPOPT_RR: 60824813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 60924813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 61024813Skarels goto bad; 61124813Skarels } 61224813Skarels /* 61324813Skarels * If no space remains, ignore. 61424813Skarels */ 61524813Skarels off--; /* 0 origin */ 61624813Skarels if (off > optlen - sizeof(struct in_addr)) 61724813Skarels break; 61831393Skarels bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 61924813Skarels sizeof(ipaddr.sin_addr)); 62024813Skarels /* 62124813Skarels * locate outgoing interface 62224813Skarels */ 62324813Skarels if ((ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 62424813Skarels type = ICMP_UNREACH; 62532113Skarels code = ICMP_UNREACH_HOST; 62624813Skarels goto bad; 62724813Skarels } 62826384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 62926384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 63024813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 63124813Skarels break; 63224813Skarels 6334640Swnj case IPOPT_TS: 6346583Ssam code = cp - (u_char *)ip; 6354801Swnj ipt = (struct ip_timestamp *)cp; 6364801Swnj if (ipt->ipt_len < 5) 6374640Swnj goto bad; 6384801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 6394801Swnj if (++ipt->ipt_oflw == 0) 6404640Swnj goto bad; 6414495Swnj break; 6424640Swnj } 64330925Skarels sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 6444801Swnj switch (ipt->ipt_flg) { 6454495Swnj 6464640Swnj case IPOPT_TS_TSONLY: 6474640Swnj break; 6484640Swnj 6494640Swnj case IPOPT_TS_TSANDADDR: 65024813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 65124813Skarels sizeof(struct in_addr) > ipt->ipt_len) 6524640Swnj goto bad; 65330925Skarels ia = ifptoia(ifp); 65430925Skarels bcopy((caddr_t)&IA_SIN(ia)->sin_addr, 65524813Skarels (caddr_t)sin, sizeof(struct in_addr)); 65630925Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6574640Swnj break; 6584640Swnj 6594640Swnj case IPOPT_TS_PRESPEC: 66030925Skarels if (ipt->ipt_ptr + sizeof(n_time) + 66130925Skarels sizeof(struct in_addr) > ipt->ipt_len) 66230925Skarels goto bad; 66324813Skarels bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 66424813Skarels sizeof(struct in_addr)); 66518376Skarels if (ifa_ifwithaddr((struct sockaddr *)&ipaddr) == 0) 6664951Swnj continue; 66724813Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6684640Swnj break; 6694640Swnj 6704495Swnj default: 6714640Swnj goto bad; 6724495Swnj } 67324813Skarels ntime = iptime(); 67430925Skarels bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 67530925Skarels sizeof(n_time)); 67624813Skarels ipt->ipt_ptr += sizeof(n_time); 6774640Swnj } 6784495Swnj } 6796583Ssam return (0); 6804640Swnj bad: 68126031Skarels icmp_error(ip, type, code, ifp); 6826583Ssam return (1); 6834495Swnj } 6844495Swnj 6854640Swnj /* 68624813Skarels * Given address of next destination (final or next hop), 68724813Skarels * return internet address info of interface to be used to get there. 68824813Skarels */ 68924813Skarels struct in_ifaddr * 69024813Skarels ip_rtaddr(dst) 69124813Skarels struct in_addr dst; 69224813Skarels { 69324813Skarels register struct sockaddr_in *sin; 69424813Skarels register struct in_ifaddr *ia; 69524813Skarels 69624813Skarels sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 69724813Skarels 69824813Skarels if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 69924813Skarels if (ipforward_rt.ro_rt) { 70024813Skarels RTFREE(ipforward_rt.ro_rt); 70124813Skarels ipforward_rt.ro_rt = 0; 70224813Skarels } 70324813Skarels sin->sin_family = AF_INET; 70424813Skarels sin->sin_addr = dst; 70524813Skarels 70624813Skarels rtalloc(&ipforward_rt); 70724813Skarels } 70824813Skarels if (ipforward_rt.ro_rt == 0) 70924813Skarels return ((struct in_ifaddr *)0); 71024813Skarels /* 71124813Skarels * Find address associated with outgoing interface. 71224813Skarels */ 71324813Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) 71424813Skarels if (ia->ia_ifp == ipforward_rt.ro_rt->rt_ifp) 71524813Skarels break; 71624813Skarels return (ia); 71724813Skarels } 71824813Skarels 71924813Skarels /* 72024813Skarels * Save incoming source route for use in replies, 72124813Skarels * to be picked up later by ip_srcroute if the receiver is interested. 72224813Skarels */ 72324813Skarels save_rte(option, dst) 72426384Skarels u_char *option; 72524813Skarels struct in_addr dst; 72624813Skarels { 72726384Skarels unsigned olen; 72824813Skarels extern ipprintfs; 72924813Skarels 73024813Skarels olen = option[IPOPT_OLEN]; 73124813Skarels if (olen > sizeof(ip_srcrt) - 1) { 73224813Skarels if (ipprintfs) 73324813Skarels printf("save_rte: olen %d\n", olen); 73424813Skarels return; 73524813Skarels } 73626384Skarels bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 73724813Skarels ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 73824813Skarels ip_srcrt.route[ip_nhops++] = dst; 73924813Skarels } 74024813Skarels 74124813Skarels /* 74224813Skarels * Retrieve incoming source route for use in replies, 74324813Skarels * in the same form used by setsockopt. 74424813Skarels * The first hop is placed before the options, will be removed later. 74524813Skarels */ 74624813Skarels struct mbuf * 74724813Skarels ip_srcroute() 74824813Skarels { 74924813Skarels register struct in_addr *p, *q; 75024813Skarels register struct mbuf *m; 75124813Skarels 75224813Skarels if (ip_nhops == 0) 75324813Skarels return ((struct mbuf *)0); 75431201Skarels m = m_get(M_DONTWAIT, MT_SOOPTS); 75531201Skarels if (m == 0) 75631201Skarels return ((struct mbuf *)0); 75724813Skarels m->m_len = ip_nhops * sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1; 75824813Skarels 75924813Skarels /* 76024813Skarels * First save first hop for return route 76124813Skarels */ 76224813Skarels p = &ip_srcrt.route[ip_nhops - 1]; 76324813Skarels *(mtod(m, struct in_addr *)) = *p--; 76424813Skarels 76524813Skarels /* 76624813Skarels * Copy option fields and padding (nop) to mbuf. 76724813Skarels */ 76824813Skarels ip_srcrt.nop = IPOPT_NOP; 76924813Skarels bcopy((caddr_t)&ip_srcrt, mtod(m, caddr_t) + sizeof(struct in_addr), 77024813Skarels IPOPT_OFFSET + 1 + 1); 77124813Skarels q = (struct in_addr *)(mtod(m, caddr_t) + 77224813Skarels sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1); 77324813Skarels /* 77424813Skarels * Record return path as an IP source route, 77524813Skarels * reversing the path (pointers are now aligned). 77624813Skarels */ 77724813Skarels while (p >= ip_srcrt.route) 77824813Skarels *q++ = *p--; 77924813Skarels return (m); 78024813Skarels } 78124813Skarels 78224813Skarels /* 7834951Swnj * Strip out IP options, at higher 7844951Swnj * level protocol in the kernel. 7854951Swnj * Second argument is buffer to which options 7864951Swnj * will be moved, and return value is their length. 7874640Swnj */ 7885217Swnj ip_stripoptions(ip, mopt) 7894640Swnj struct ip *ip; 7905217Swnj struct mbuf *mopt; 7914495Swnj { 7924640Swnj register int i; 7934640Swnj register struct mbuf *m; 79424813Skarels register caddr_t opts; 7954640Swnj int olen; 7964640Swnj 7974640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 7984951Swnj m = dtom(ip); 79924813Skarels opts = (caddr_t)(ip + 1); 8005217Swnj if (mopt) { 8015217Swnj mopt->m_len = olen; 8025217Swnj mopt->m_off = MMINOFF; 80324813Skarels bcopy(opts, mtod(mopt, caddr_t), (unsigned)olen); 8045217Swnj } 8054640Swnj i = m->m_len - (sizeof (struct ip) + olen); 80624813Skarels bcopy(opts + olen, opts, (unsigned)i); 8075243Sroot m->m_len -= olen; 80824813Skarels ip->ip_hl = sizeof(struct ip) >> 2; 8094495Swnj } 8106583Ssam 81114670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 81224813Skarels 0, 0, 0, 0, 81314670Ssam 0, 0, EHOSTDOWN, EHOSTUNREACH, 81414670Ssam ENETUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 81524813Skarels EMSGSIZE, EHOSTUNREACH, 0, 0, 81624813Skarels 0, 0, 0, 0, 81724813Skarels ENOPROTOOPT 8186583Ssam }; 8196583Ssam 82024813Skarels #ifndef IPFORWARDING 82124813Skarels #define IPFORWARDING 1 82224813Skarels #endif 82324813Skarels #ifndef IPSENDREDIRECTS 82424813Skarels #define IPSENDREDIRECTS 1 82524813Skarels #endif 8266583Ssam int ipprintfs = 0; 82724813Skarels int ipforwarding = IPFORWARDING; 82824813Skarels extern int in_interfaces; 82924813Skarels int ipsendredirects = IPSENDREDIRECTS; 83024813Skarels 8316583Ssam /* 8326583Ssam * Forward a packet. If some error occurs return the sender 83318376Skarels * an icmp packet. Note we can't always generate a meaningful 83424813Skarels * icmp message because icmp doesn't have a large enough repertoire 8356583Ssam * of codes and types. 83626308Skarels * 83726308Skarels * If not forwarding (possibly because we have only a single external 83826308Skarels * network), just drop the packet. This could be confusing if ipforwarding 83926308Skarels * was zero but some routing protocol was advancing us as a gateway 84026308Skarels * to somewhere. However, we must let the routing protocol deal with that. 8416583Ssam */ 84224813Skarels ip_forward(ip, ifp) 8436583Ssam register struct ip *ip; 84424813Skarels struct ifnet *ifp; 8456583Ssam { 84624813Skarels register int error, type = 0, code; 84724813Skarels register struct sockaddr_in *sin; 84818376Skarels struct mbuf *mcopy; 84924813Skarels struct in_addr dest; 8506583Ssam 85124813Skarels dest.s_addr = 0; 8526583Ssam if (ipprintfs) 8536583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 8546583Ssam ip->ip_dst, ip->ip_ttl); 85518376Skarels ip->ip_id = htons(ip->ip_id); 85624813Skarels if (ipforwarding == 0 || in_interfaces <= 1) { 85726308Skarels ipstat.ips_cantforward++; 85826985Skarels #ifdef GATEWAY 85926985Skarels type = ICMP_UNREACH, code = ICMP_UNREACH_NET; 86026985Skarels goto sendicmp; 86126985Skarels #else 86226308Skarels m_freem(dtom(ip)); 86326308Skarels return; 86426985Skarels #endif 8656583Ssam } 86632572Skarels if (in_canforward(ip->ip_dst) == 0) { 86732572Skarels m_freem(dtom(ip)); 86832572Skarels return; 86932572Skarels } 87031393Skarels if (ip->ip_ttl <= IPTTLDEC) { 8716583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 8726583Ssam goto sendicmp; 8736583Ssam } 8746583Ssam ip->ip_ttl -= IPTTLDEC; 8756609Ssam 8766609Ssam /* 8776609Ssam * Save at most 64 bytes of the packet in case 8786609Ssam * we need to generate an ICMP message to the src. 8796609Ssam */ 88028944Skarels mcopy = m_copy(dtom(ip), 0, imin((int)ip->ip_len, 64)); 8816583Ssam 88224813Skarels sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 88324813Skarels if (ipforward_rt.ro_rt == 0 || 88424813Skarels ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 88524813Skarels if (ipforward_rt.ro_rt) { 88624813Skarels RTFREE(ipforward_rt.ro_rt); 88724813Skarels ipforward_rt.ro_rt = 0; 88824813Skarels } 88924813Skarels sin->sin_family = AF_INET; 89024813Skarels sin->sin_addr = ip->ip_dst; 89124813Skarels 89224813Skarels rtalloc(&ipforward_rt); 89324813Skarels } 89424813Skarels /* 89524813Skarels * If forwarding packet using same interface that it came in on, 89624813Skarels * perhaps should send a redirect to sender to shortcut a hop. 89724813Skarels * Only send redirect if source is sending directly to us, 89824813Skarels * and if packet was not source routed (or has any options). 89930447Skarels * Also, don't send redirect if forwarding using a default route 90030447Skarels * or a route modfied by a redirect. 90124813Skarels */ 90230447Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 90324813Skarels if (ipforward_rt.ro_rt && ipforward_rt.ro_rt->rt_ifp == ifp && 90432572Skarels (ipforward_rt.ro_rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 90530447Skarels satosin(&ipforward_rt.ro_rt->rt_dst)->sin_addr.s_addr != 0 && 90624813Skarels ipsendredirects && ip->ip_hl == (sizeof(struct ip) >> 2)) { 90724813Skarels struct in_ifaddr *ia; 90824813Skarels u_long src = ntohl(ip->ip_src.s_addr); 90924813Skarels u_long dst = ntohl(ip->ip_dst.s_addr); 91024813Skarels 91124813Skarels if ((ia = ifptoia(ifp)) && 91224813Skarels (src & ia->ia_subnetmask) == ia->ia_subnet) { 91324813Skarels if (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) 91424813Skarels dest = satosin(&ipforward_rt.ro_rt->rt_gateway)->sin_addr; 91524813Skarels else 91624813Skarels dest = ip->ip_dst; 91724813Skarels /* 91824813Skarels * If the destination is reached by a route to host, 91927145Skarels * is on a subnet of a local net, or is directly 92027145Skarels * on the attached net (!), use host redirect. 92124813Skarels * (We may be the correct first hop for other subnets.) 92224813Skarels */ 92324813Skarels type = ICMP_REDIRECT; 92424813Skarels code = ICMP_REDIRECT_NET; 92524813Skarels if ((ipforward_rt.ro_rt->rt_flags & RTF_HOST) || 92624813Skarels (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) == 0) 92724813Skarels code = ICMP_REDIRECT_HOST; 92824813Skarels else for (ia = in_ifaddr; ia = ia->ia_next; ) 92924813Skarels if ((dst & ia->ia_netmask) == ia->ia_net) { 93027145Skarels if (ia->ia_subnetmask != ia->ia_netmask) 93127145Skarels code = ICMP_REDIRECT_HOST; 93224813Skarels break; 93324813Skarels } 93424813Skarels if (ipprintfs) 93524813Skarels printf("redirect (%d) to %x\n", code, dest); 93624813Skarels } 93724813Skarels } 93824813Skarels 93924813Skarels error = ip_output(dtom(ip), (struct mbuf *)0, &ipforward_rt, 94018376Skarels IP_FORWARDING); 94124813Skarels if (error) 94224813Skarels ipstat.ips_cantforward++; 94324813Skarels else if (type) 94424813Skarels ipstat.ips_redirectsent++; 94524813Skarels else { 9466609Ssam if (mcopy) 9476609Ssam m_freem(mcopy); 94821117Skarels ipstat.ips_forward++; 9496583Ssam return; 9506609Ssam } 95111540Ssam if (mcopy == NULL) 95211540Ssam return; 9536609Ssam ip = mtod(mcopy, struct ip *); 95424813Skarels type = ICMP_UNREACH; 9556609Ssam switch (error) { 9566609Ssam 95724813Skarels case 0: /* forwarded, but need redirect */ 95824813Skarels type = ICMP_REDIRECT; 95924813Skarels /* code set above */ 96024813Skarels break; 96124813Skarels 9626609Ssam case ENETUNREACH: 9636609Ssam case ENETDOWN: 96432572Skarels if (in_localaddr(ip->ip_dst)) 96532572Skarels code = ICMP_UNREACH_HOST; 96632572Skarels else 96732572Skarels code = ICMP_UNREACH_NET; 9686609Ssam break; 9696609Ssam 9706609Ssam case EMSGSIZE: 9716583Ssam code = ICMP_UNREACH_NEEDFRAG; 9726609Ssam break; 9736609Ssam 9746609Ssam case EPERM: 9756609Ssam code = ICMP_UNREACH_PORT; 9766609Ssam break; 9776609Ssam 9786609Ssam case ENOBUFS: 9796609Ssam type = ICMP_SOURCEQUENCH; 9806609Ssam break; 9816609Ssam 9826609Ssam case EHOSTDOWN: 9836609Ssam case EHOSTUNREACH: 9846609Ssam code = ICMP_UNREACH_HOST; 9856609Ssam break; 9866609Ssam } 9876583Ssam sendicmp: 98826031Skarels icmp_error(ip, type, code, ifp, dest); 9896583Ssam } 990