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 632787Sbostic * provided that this notice is preserved and that due credit is given 732787Sbostic * to the University of California at Berkeley. The name of the University 832787Sbostic * may not be used to endorse or promote products derived from this 932787Sbostic * software without specific prior written permission. This software 1032787Sbostic * is provided ``as is'' without express or implied warranty. 1132787Sbostic * 12*33743Skarels * @(#)ip_input.c 7.9 (Berkeley) 03/15/88 1323184Smckusick */ 144571Swnj 1517060Sbloom #include "param.h" 1617060Sbloom #include "systm.h" 1717060Sbloom #include "mbuf.h" 1817060Sbloom #include "domain.h" 1917060Sbloom #include "protosw.h" 2017060Sbloom #include "socket.h" 2117060Sbloom #include "errno.h" 2217060Sbloom #include "time.h" 2317060Sbloom #include "kernel.h" 248695Sroot 258695Sroot #include "../net/if.h" 268695Sroot #include "../net/route.h" 2710892Ssam 2817060Sbloom #include "in.h" 2917060Sbloom #include "in_pcb.h" 3017060Sbloom #include "in_systm.h" 3118376Skarels #include "in_var.h" 3217060Sbloom #include "ip.h" 3317060Sbloom #include "ip_var.h" 3417060Sbloom #include "ip_icmp.h" 3517060Sbloom #include "tcp.h" 364495Swnj 374898Swnj u_char ip_protox[IPPROTO_MAX]; 386210Swnj int ipqmaxlen = IFQ_MAXLEN; 3918376Skarels struct in_ifaddr *in_ifaddr; /* first inet address */ 404898Swnj 414801Swnj /* 4224813Skarels * We need to save the IP options in case a protocol wants to respond 4324813Skarels * to an incoming packet over the same route if the packet got here 4424813Skarels * using IP source routing. This allows connection establishment and 4524813Skarels * maintenance when the remote end is on a network that is not known 4624813Skarels * to us. 4724813Skarels */ 4824813Skarels int ip_nhops = 0; 4924813Skarels static struct ip_srcrt { 5024813Skarels char nop; /* one NOP to align */ 5124813Skarels char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 5224813Skarels struct in_addr route[MAX_IPOPTLEN]; 5324813Skarels } ip_srcrt; 5424813Skarels 5524813Skarels /* 565172Swnj * IP initialization: fill in IP protocol switch table. 575161Swnj * All protocols not implemented in kernel go to raw IP protocol handler. 584801Swnj */ 594801Swnj ip_init() 604801Swnj { 614898Swnj register struct protosw *pr; 624898Swnj register int i; 634495Swnj 6424813Skarels pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 654898Swnj if (pr == 0) 664898Swnj panic("ip_init"); 674898Swnj for (i = 0; i < IPPROTO_MAX; i++) 689030Sroot ip_protox[i] = pr - inetsw; 699030Sroot for (pr = inetdomain.dom_protosw; 7017551Skarels pr < inetdomain.dom_protoswNPROTOSW; pr++) 7116990Skarels if (pr->pr_domain->dom_family == PF_INET && 724898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 739030Sroot ip_protox[pr->pr_protocol] = pr - inetsw; 744801Swnj ipq.next = ipq.prev = &ipq; 758172Sroot ip_id = time.tv_sec & 0xffff; 766210Swnj ipintrq.ifq_maxlen = ipqmaxlen; 774801Swnj } 784801Swnj 794898Swnj u_char ipcksum = 1; 804640Swnj struct ip *ip_reass(); 816338Ssam struct sockaddr_in ipaddr = { AF_INET }; 8224813Skarels struct route ipforward_rt; 834640Swnj 844640Swnj /* 854640Swnj * Ip input routine. Checksum and byte swap header. If fragmented 864640Swnj * try to reassamble. If complete and fragment queue exists, discard. 874640Swnj * Process options. Pass to next level. 884640Swnj */ 895084Swnj ipintr() 904495Swnj { 914923Swnj register struct ip *ip; 925084Swnj register struct mbuf *m; 938597Sroot struct mbuf *m0; 944640Swnj register int i; 954495Swnj register struct ipq *fp; 9618376Skarels register struct in_ifaddr *ia; 9724813Skarels struct ifnet *ifp; 985084Swnj int hlen, s; 994495Swnj 1005084Swnj next: 1014640Swnj /* 1025084Swnj * Get next datagram off input queue and get IP header 1035084Swnj * in first mbuf. 1044640Swnj */ 1055084Swnj s = splimp(); 10624813Skarels IF_DEQUEUEIF(&ipintrq, m, ifp); 1075084Swnj splx(s); 1085218Swnj if (m == 0) 1095084Swnj return; 11026001Skarels /* 11126001Skarels * If no IP addresses have been set yet but the interfaces 11226001Skarels * are receiving, can't do anything with incoming packets yet. 11326001Skarels */ 11426001Skarels if (in_ifaddr == NULL) 11526001Skarels goto bad; 11625920Skarels ipstat.ips_total++; 1175306Sroot if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) && 11811232Ssam (m = m_pullup(m, sizeof (struct ip))) == 0) { 11911232Ssam ipstat.ips_toosmall++; 12011232Ssam goto next; 12111232Ssam } 1224640Swnj ip = mtod(m, struct ip *); 12318376Skarels hlen = ip->ip_hl << 2; 12424813Skarels if (hlen < sizeof(struct ip)) { /* minimum header length */ 12518376Skarels ipstat.ips_badhlen++; 12621117Skarels goto bad; 12718376Skarels } 12818376Skarels if (hlen > m->m_len) { 12911232Ssam if ((m = m_pullup(m, hlen)) == 0) { 13011232Ssam ipstat.ips_badhlen++; 13111232Ssam goto next; 13211232Ssam } 1335161Swnj ip = mtod(m, struct ip *); 1345161Swnj } 1354951Swnj if (ipcksum) 1365217Swnj if (ip->ip_sum = in_cksum(m, hlen)) { 1374951Swnj ipstat.ips_badsum++; 1384951Swnj goto bad; 1394495Swnj } 1404951Swnj 1414951Swnj /* 1424951Swnj * Convert fields to host representation. 1434951Swnj */ 1444907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 14511232Ssam if (ip->ip_len < hlen) { 14611232Ssam ipstat.ips_badlen++; 14711232Ssam goto bad; 14811232Ssam } 1494640Swnj ip->ip_id = ntohs(ip->ip_id); 1504951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 1514495Swnj 1524543Swnj /* 1534640Swnj * Check that the amount of data in the buffers 1544640Swnj * is as at least much as the IP header would have us expect. 1554640Swnj * Trim mbufs if longer than we expect. 1564640Swnj * Drop packet if shorter than we expect. 1574543Swnj */ 15824813Skarels i = -(u_short)ip->ip_len; 1595161Swnj m0 = m; 1606475Sroot for (;;) { 1614495Swnj i += m->m_len; 1626475Sroot if (m->m_next == 0) 1636475Sroot break; 1646475Sroot m = m->m_next; 1656088Sroot } 1666475Sroot if (i != 0) { 1676475Sroot if (i < 0) { 1685161Swnj ipstat.ips_tooshort++; 16917358Skarels m = m0; 1704951Swnj goto bad; 1715161Swnj } 1726475Sroot if (i <= m->m_len) 1736475Sroot m->m_len -= i; 1746475Sroot else 1756475Sroot m_adj(m0, -i); 1764495Swnj } 1776475Sroot m = m0; 1784495Swnj 1794640Swnj /* 1804640Swnj * Process options and, if not destined for us, 1816583Ssam * ship it on. ip_dooptions returns 1 when an 1826583Ssam * error was detected (causing an icmp message 18321117Skarels * to be sent and the original packet to be freed). 1844640Swnj */ 18524813Skarels ip_nhops = 0; /* for source routed packets */ 18626031Skarels if (hlen > sizeof (struct ip) && ip_dooptions(ip, ifp)) 1876583Ssam goto next; 1886210Swnj 1896338Ssam /* 19018376Skarels * Check our list of addresses, to see if the packet is for us. 1916338Ssam */ 19218376Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) { 19318376Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 1946338Ssam 19518376Skarels if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 19624813Skarels goto ours; 19725195Skarels if ( 19825195Skarels #ifdef DIRECTED_BROADCAST 19925195Skarels ia->ia_ifp == ifp && 20025195Skarels #endif 20125195Skarels (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 20226247Skarels u_long t; 20325195Skarels 20425195Skarels if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 20525195Skarels ip->ip_dst.s_addr) 20625195Skarels goto ours; 20725195Skarels if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 20825195Skarels goto ours; 20925195Skarels /* 21025195Skarels * Look for all-0's host part (old broadcast addr), 21125195Skarels * either for subnet or net. 21225195Skarels */ 21326247Skarels t = ntohl(ip->ip_dst.s_addr); 21426247Skarels if (t == ia->ia_subnet) 21525195Skarels goto ours; 21626247Skarels if (t == ia->ia_net) 21725195Skarels goto ours; 21825195Skarels } 2196338Ssam } 22024813Skarels if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 22124813Skarels goto ours; 22224813Skarels if (ip->ip_dst.s_addr == INADDR_ANY) 22324813Skarels goto ours; 2244495Swnj 2254640Swnj /* 22624813Skarels * Not for us; forward if possible and desirable. 22724813Skarels */ 22824813Skarels ip_forward(ip, ifp); 22924813Skarels goto next; 23024813Skarels 23124813Skarels ours: 23224813Skarels /* 233*33743Skarels * If offset or IP_MF are set, must reassemble. 234*33743Skarels * Otherwise, nothing need be done. 235*33743Skarels * (We could look in the reassembly queue to see 236*33743Skarels * if the packet was previously fragmented, 237*33743Skarels * but it's not worth the time; just let them time out.) 2384640Swnj */ 239*33743Skarels if (ip->ip_off &~ IP_DF) { 240*33743Skarels /* 241*33743Skarels * Look for queue of fragments 242*33743Skarels * of this datagram. 243*33743Skarels */ 244*33743Skarels for (fp = ipq.next; fp != &ipq; fp = fp->next) 245*33743Skarels if (ip->ip_id == fp->ipq_id && 246*33743Skarels ip->ip_src.s_addr == fp->ipq_src.s_addr && 247*33743Skarels ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 248*33743Skarels ip->ip_p == fp->ipq_p) 249*33743Skarels goto found; 250*33743Skarels fp = 0; 2514640Swnj found: 2524495Swnj 253*33743Skarels /* 254*33743Skarels * Adjust ip_len to not reflect header, 255*33743Skarels * set ip_mff if more fragments are expected, 256*33743Skarels * convert offset of this to bytes. 257*33743Skarels */ 258*33743Skarels ip->ip_len -= hlen; 259*33743Skarels ((struct ipasfrag *)ip)->ipf_mff = 0; 260*33743Skarels if (ip->ip_off & IP_MF) 261*33743Skarels ((struct ipasfrag *)ip)->ipf_mff = 1; 262*33743Skarels ip->ip_off <<= 3; 2634495Swnj 264*33743Skarels /* 265*33743Skarels * If datagram marked as having more fragments 266*33743Skarels * or if this is not the first fragment, 267*33743Skarels * attempt reassembly; if it succeeds, proceed. 268*33743Skarels */ 269*33743Skarels if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 270*33743Skarels ipstat.ips_fragments++; 271*33743Skarels ip = ip_reass((struct ipasfrag *)ip, fp); 272*33743Skarels if (ip == 0) 273*33743Skarels goto next; 274*33743Skarels m = dtom(ip); 275*33743Skarels } else 276*33743Skarels if (fp) 277*33743Skarels ip_freef(fp); 2784640Swnj } else 279*33743Skarels ip->ip_len -= hlen; 2804951Swnj 2814951Swnj /* 2824951Swnj * Switch out to protocol's input routine. 2834951Swnj */ 28424813Skarels (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, ifp); 2855084Swnj goto next; 2864951Swnj bad: 2874951Swnj m_freem(m); 2885084Swnj goto next; 2894640Swnj } 2904495Swnj 2914640Swnj /* 2924640Swnj * Take incoming datagram fragment and try to 2934951Swnj * reassemble it into whole datagram. If a chain for 2944640Swnj * reassembly of this datagram already exists, then it 2954640Swnj * is given as fp; otherwise have to make a chain. 2964640Swnj */ 2974640Swnj struct ip * 2984640Swnj ip_reass(ip, fp) 2994898Swnj register struct ipasfrag *ip; 3004640Swnj register struct ipq *fp; 3014640Swnj { 3024640Swnj register struct mbuf *m = dtom(ip); 3034898Swnj register struct ipasfrag *q; 3044640Swnj struct mbuf *t; 3054640Swnj int hlen = ip->ip_hl << 2; 3064640Swnj int i, next; 3074543Swnj 3084640Swnj /* 3094640Swnj * Presence of header sizes in mbufs 3104640Swnj * would confuse code below. 3114640Swnj */ 3124640Swnj m->m_off += hlen; 3134640Swnj m->m_len -= hlen; 3144495Swnj 3154640Swnj /* 3164640Swnj * If first fragment to arrive, create a reassembly queue. 3174640Swnj */ 3184640Swnj if (fp == 0) { 31931201Skarels if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 3204640Swnj goto dropfrag; 3214640Swnj fp = mtod(t, struct ipq *); 3224640Swnj insque(fp, &ipq); 3234640Swnj fp->ipq_ttl = IPFRAGTTL; 3244640Swnj fp->ipq_p = ip->ip_p; 3254640Swnj fp->ipq_id = ip->ip_id; 3264898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 3274898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 3284898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 3295161Swnj q = (struct ipasfrag *)fp; 3305161Swnj goto insert; 3314640Swnj } 3324495Swnj 3334640Swnj /* 3344640Swnj * Find a segment which begins after this one does. 3354640Swnj */ 3364898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3374640Swnj if (q->ip_off > ip->ip_off) 3384640Swnj break; 3394495Swnj 3404640Swnj /* 3414640Swnj * If there is a preceding segment, it may provide some of 3424640Swnj * our data already. If so, drop the data from the incoming 3434640Swnj * segment. If it provides all of our data, drop us. 3444640Swnj */ 3454898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 3464898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 3474640Swnj if (i > 0) { 3484640Swnj if (i >= ip->ip_len) 3494640Swnj goto dropfrag; 3504640Swnj m_adj(dtom(ip), i); 3514640Swnj ip->ip_off += i; 3524640Swnj ip->ip_len -= i; 3534640Swnj } 3544640Swnj } 3554543Swnj 3564640Swnj /* 3574640Swnj * While we overlap succeeding segments trim them or, 3584640Swnj * if they are completely covered, dequeue them. 3594640Swnj */ 3604898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 3614640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 3624640Swnj if (i < q->ip_len) { 3634640Swnj q->ip_len -= i; 3646256Sroot q->ip_off += i; 3654640Swnj m_adj(dtom(q), i); 3664640Swnj break; 3674495Swnj } 3684898Swnj q = q->ipf_next; 3694898Swnj m_freem(dtom(q->ipf_prev)); 3704898Swnj ip_deq(q->ipf_prev); 3714543Swnj } 3724495Swnj 3735161Swnj insert: 3744640Swnj /* 3754640Swnj * Stick new segment in its place; 3764640Swnj * check for complete reassembly. 3774640Swnj */ 3784898Swnj ip_enq(ip, q->ipf_prev); 3794640Swnj next = 0; 3804898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 3814640Swnj if (q->ip_off != next) 3824640Swnj return (0); 3834640Swnj next += q->ip_len; 3844640Swnj } 3854898Swnj if (q->ipf_prev->ipf_mff) 3864640Swnj return (0); 3874495Swnj 3884640Swnj /* 3894640Swnj * Reassembly is complete; concatenate fragments. 3904640Swnj */ 3914640Swnj q = fp->ipq_next; 3924640Swnj m = dtom(q); 3934640Swnj t = m->m_next; 3944640Swnj m->m_next = 0; 3954640Swnj m_cat(m, t); 3966298Swnj q = q->ipf_next; 3976298Swnj while (q != (struct ipasfrag *)fp) { 3986298Swnj t = dtom(q); 3996298Swnj q = q->ipf_next; 4006298Swnj m_cat(m, t); 4016298Swnj } 4024495Swnj 4034640Swnj /* 4044640Swnj * Create header for new ip packet by 4054640Swnj * modifying header of first packet; 4064640Swnj * dequeue and discard fragment reassembly header. 4074640Swnj * Make header visible. 4084640Swnj */ 4094640Swnj ip = fp->ipq_next; 4104640Swnj ip->ip_len = next; 4114898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 4124898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 4134640Swnj remque(fp); 4144907Swnj (void) m_free(dtom(fp)); 4154640Swnj m = dtom(ip); 41624813Skarels m->m_len += (ip->ip_hl << 2); 41724813Skarels m->m_off -= (ip->ip_hl << 2); 4184898Swnj return ((struct ip *)ip); 4194495Swnj 4204640Swnj dropfrag: 42124813Skarels ipstat.ips_fragdropped++; 4224640Swnj m_freem(m); 4234640Swnj return (0); 4244495Swnj } 4254495Swnj 4264640Swnj /* 4274640Swnj * Free a fragment reassembly header and all 4284640Swnj * associated datagrams. 4294640Swnj */ 4304640Swnj ip_freef(fp) 4314640Swnj struct ipq *fp; 4324495Swnj { 43310735Ssam register struct ipasfrag *q, *p; 4344495Swnj 43510735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 43610735Ssam p = q->ipf_next; 43710735Ssam ip_deq(q); 4384640Swnj m_freem(dtom(q)); 43910735Ssam } 44010735Ssam remque(fp); 44110735Ssam (void) m_free(dtom(fp)); 4424495Swnj } 4434495Swnj 4444640Swnj /* 4454640Swnj * Put an ip fragment on a reassembly chain. 4464640Swnj * Like insque, but pointers in middle of structure. 4474640Swnj */ 4484640Swnj ip_enq(p, prev) 4494898Swnj register struct ipasfrag *p, *prev; 4504495Swnj { 4514951Swnj 4524898Swnj p->ipf_prev = prev; 4534898Swnj p->ipf_next = prev->ipf_next; 4544898Swnj prev->ipf_next->ipf_prev = p; 4554898Swnj prev->ipf_next = p; 4564495Swnj } 4574495Swnj 4584640Swnj /* 4594640Swnj * To ip_enq as remque is to insque. 4604640Swnj */ 4614640Swnj ip_deq(p) 4624898Swnj register struct ipasfrag *p; 4634640Swnj { 4644951Swnj 4654898Swnj p->ipf_prev->ipf_next = p->ipf_next; 4664898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 4674495Swnj } 4684495Swnj 4694640Swnj /* 4704640Swnj * IP timer processing; 4714640Swnj * if a timer expires on a reassembly 4724640Swnj * queue, discard it. 4734640Swnj */ 4744801Swnj ip_slowtimo() 4754495Swnj { 4764495Swnj register struct ipq *fp; 4774640Swnj int s = splnet(); 4784951Swnj 4795243Sroot fp = ipq.next; 4805243Sroot if (fp == 0) { 4815243Sroot splx(s); 4825243Sroot return; 4835243Sroot } 48410735Ssam while (fp != &ipq) { 48510735Ssam --fp->ipq_ttl; 48610735Ssam fp = fp->next; 48724813Skarels if (fp->prev->ipq_ttl == 0) { 48824813Skarels ipstat.ips_fragtimeout++; 48910735Ssam ip_freef(fp->prev); 49024813Skarels } 49110735Ssam } 4924640Swnj splx(s); 4934495Swnj } 4944495Swnj 4954951Swnj /* 4964951Swnj * Drain off all datagram fragments. 4974951Swnj */ 4984801Swnj ip_drain() 4994801Swnj { 5004801Swnj 50124813Skarels while (ipq.next != &ipq) { 50224813Skarels ipstat.ips_fragdropped++; 50310735Ssam ip_freef(ipq.next); 50424813Skarels } 5054801Swnj } 5064923Swnj 50730925Skarels extern struct in_ifaddr *ifptoia(); 50824813Skarels struct in_ifaddr *ip_rtaddr(); 50924813Skarels 5104640Swnj /* 5114640Swnj * Do option processing on a datagram, 5124640Swnj * possibly discarding it if bad options 5134640Swnj * are encountered. 5144640Swnj */ 51526031Skarels ip_dooptions(ip, ifp) 51626031Skarels register struct ip *ip; 51726031Skarels struct ifnet *ifp; 5184495Swnj { 5194640Swnj register u_char *cp; 52024813Skarels int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB; 52124813Skarels register struct ip_timestamp *ipt; 52224813Skarels register struct in_ifaddr *ia; 5234923Swnj struct in_addr *sin; 52424813Skarels n_time ntime; 5254495Swnj 5264640Swnj cp = (u_char *)(ip + 1); 5274640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 5284640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 52924813Skarels opt = cp[IPOPT_OPTVAL]; 5304640Swnj if (opt == IPOPT_EOL) 5314640Swnj break; 5324640Swnj if (opt == IPOPT_NOP) 5334640Swnj optlen = 1; 53416392Ssam else { 53524813Skarels optlen = cp[IPOPT_OLEN]; 53624813Skarels if (optlen <= 0 || optlen > cnt) { 53724813Skarels code = &cp[IPOPT_OLEN] - (u_char *)ip; 53817551Skarels goto bad; 53924813Skarels } 54016392Ssam } 5414640Swnj switch (opt) { 5424495Swnj 5434640Swnj default: 5444640Swnj break; 5454495Swnj 5464951Swnj /* 5474951Swnj * Source routing with record. 5484951Swnj * Find interface with current destination address. 5494951Swnj * If none on this machine then drop if strictly routed, 5504951Swnj * or do nothing if loosely routed. 5514951Swnj * Record interface address and bring up next address 5524951Swnj * component. If strictly routed make sure next 5534951Swnj * address on directly accessible net. 5544951Swnj */ 5554640Swnj case IPOPT_LSRR: 5567508Sroot case IPOPT_SSRR: 55724813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 55824813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 55924813Skarels goto bad; 56024813Skarels } 56124813Skarels ipaddr.sin_addr = ip->ip_dst; 56224813Skarels ia = (struct in_ifaddr *) 56324813Skarels ifa_ifwithaddr((struct sockaddr *)&ipaddr); 56424813Skarels if (ia == 0) { 56524813Skarels if (opt == IPOPT_SSRR) { 56624813Skarels type = ICMP_UNREACH; 56724813Skarels code = ICMP_UNREACH_SRCFAIL; 5684951Swnj goto bad; 56924813Skarels } 57024813Skarels /* 57124813Skarels * Loose routing, and not at next destination 57224813Skarels * yet; nothing to do except forward. 57324813Skarels */ 5744951Swnj break; 5754640Swnj } 57624813Skarels off--; /* 0 origin */ 57724813Skarels if (off > optlen - sizeof(struct in_addr)) { 57824813Skarels /* 57924813Skarels * End of source route. Should be for us. 58024813Skarels */ 58124813Skarels save_rte(cp, ip->ip_src); 5824951Swnj break; 58324813Skarels } 58424813Skarels /* 58524813Skarels * locate outgoing interface 58624813Skarels */ 58726384Skarels bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 58824813Skarels sizeof(ipaddr.sin_addr)); 58924813Skarels if ((opt == IPOPT_SSRR && 59024813Skarels in_iaonnetof(in_netof(ipaddr.sin_addr)) == 0) || 59124813Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 59224813Skarels type = ICMP_UNREACH; 59324813Skarels code = ICMP_UNREACH_SRCFAIL; 5944951Swnj goto bad; 59524813Skarels } 59624813Skarels ip->ip_dst = ipaddr.sin_addr; 59726384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 59826384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 59924813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 6004640Swnj break; 6014495Swnj 60224813Skarels case IPOPT_RR: 60324813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 60424813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 60524813Skarels goto bad; 60624813Skarels } 60724813Skarels /* 60824813Skarels * If no space remains, ignore. 60924813Skarels */ 61024813Skarels off--; /* 0 origin */ 61124813Skarels if (off > optlen - sizeof(struct in_addr)) 61224813Skarels break; 61331393Skarels bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 61424813Skarels sizeof(ipaddr.sin_addr)); 61524813Skarels /* 61624813Skarels * locate outgoing interface 61724813Skarels */ 61824813Skarels if ((ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 61924813Skarels type = ICMP_UNREACH; 62032113Skarels code = ICMP_UNREACH_HOST; 62124813Skarels goto bad; 62224813Skarels } 62326384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 62426384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 62524813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 62624813Skarels break; 62724813Skarels 6284640Swnj case IPOPT_TS: 6296583Ssam code = cp - (u_char *)ip; 6304801Swnj ipt = (struct ip_timestamp *)cp; 6314801Swnj if (ipt->ipt_len < 5) 6324640Swnj goto bad; 6334801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 6344801Swnj if (++ipt->ipt_oflw == 0) 6354640Swnj goto bad; 6364495Swnj break; 6374640Swnj } 63830925Skarels sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 6394801Swnj switch (ipt->ipt_flg) { 6404495Swnj 6414640Swnj case IPOPT_TS_TSONLY: 6424640Swnj break; 6434640Swnj 6444640Swnj case IPOPT_TS_TSANDADDR: 64524813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 64624813Skarels sizeof(struct in_addr) > ipt->ipt_len) 6474640Swnj goto bad; 64830925Skarels ia = ifptoia(ifp); 64930925Skarels bcopy((caddr_t)&IA_SIN(ia)->sin_addr, 65024813Skarels (caddr_t)sin, sizeof(struct in_addr)); 65130925Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6524640Swnj break; 6534640Swnj 6544640Swnj case IPOPT_TS_PRESPEC: 65530925Skarels if (ipt->ipt_ptr + sizeof(n_time) + 65630925Skarels sizeof(struct in_addr) > ipt->ipt_len) 65730925Skarels goto bad; 65824813Skarels bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 65924813Skarels sizeof(struct in_addr)); 66018376Skarels if (ifa_ifwithaddr((struct sockaddr *)&ipaddr) == 0) 6614951Swnj continue; 66224813Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6634640Swnj break; 6644640Swnj 6654495Swnj default: 6664640Swnj goto bad; 6674495Swnj } 66824813Skarels ntime = iptime(); 66930925Skarels bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 67030925Skarels sizeof(n_time)); 67124813Skarels ipt->ipt_ptr += sizeof(n_time); 6724640Swnj } 6734495Swnj } 6746583Ssam return (0); 6754640Swnj bad: 67626031Skarels icmp_error(ip, type, code, ifp); 6776583Ssam return (1); 6784495Swnj } 6794495Swnj 6804640Swnj /* 68124813Skarels * Given address of next destination (final or next hop), 68224813Skarels * return internet address info of interface to be used to get there. 68324813Skarels */ 68424813Skarels struct in_ifaddr * 68524813Skarels ip_rtaddr(dst) 68624813Skarels struct in_addr dst; 68724813Skarels { 68824813Skarels register struct sockaddr_in *sin; 68924813Skarels register struct in_ifaddr *ia; 69024813Skarels 69124813Skarels sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 69224813Skarels 69324813Skarels if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 69424813Skarels if (ipforward_rt.ro_rt) { 69524813Skarels RTFREE(ipforward_rt.ro_rt); 69624813Skarels ipforward_rt.ro_rt = 0; 69724813Skarels } 69824813Skarels sin->sin_family = AF_INET; 69924813Skarels sin->sin_addr = dst; 70024813Skarels 70124813Skarels rtalloc(&ipforward_rt); 70224813Skarels } 70324813Skarels if (ipforward_rt.ro_rt == 0) 70424813Skarels return ((struct in_ifaddr *)0); 70524813Skarels /* 70624813Skarels * Find address associated with outgoing interface. 70724813Skarels */ 70824813Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) 70924813Skarels if (ia->ia_ifp == ipforward_rt.ro_rt->rt_ifp) 71024813Skarels break; 71124813Skarels return (ia); 71224813Skarels } 71324813Skarels 71424813Skarels /* 71524813Skarels * Save incoming source route for use in replies, 71624813Skarels * to be picked up later by ip_srcroute if the receiver is interested. 71724813Skarels */ 71824813Skarels save_rte(option, dst) 71926384Skarels u_char *option; 72024813Skarels struct in_addr dst; 72124813Skarels { 72226384Skarels unsigned olen; 72324813Skarels extern ipprintfs; 72424813Skarels 72524813Skarels olen = option[IPOPT_OLEN]; 72624813Skarels if (olen > sizeof(ip_srcrt) - 1) { 72724813Skarels if (ipprintfs) 72824813Skarels printf("save_rte: olen %d\n", olen); 72924813Skarels return; 73024813Skarels } 73126384Skarels bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 73224813Skarels ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 73324813Skarels ip_srcrt.route[ip_nhops++] = dst; 73424813Skarels } 73524813Skarels 73624813Skarels /* 73724813Skarels * Retrieve incoming source route for use in replies, 73824813Skarels * in the same form used by setsockopt. 73924813Skarels * The first hop is placed before the options, will be removed later. 74024813Skarels */ 74124813Skarels struct mbuf * 74224813Skarels ip_srcroute() 74324813Skarels { 74424813Skarels register struct in_addr *p, *q; 74524813Skarels register struct mbuf *m; 74624813Skarels 74724813Skarels if (ip_nhops == 0) 74824813Skarels return ((struct mbuf *)0); 74931201Skarels m = m_get(M_DONTWAIT, MT_SOOPTS); 75031201Skarels if (m == 0) 75131201Skarels return ((struct mbuf *)0); 75224813Skarels m->m_len = ip_nhops * sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1; 75324813Skarels 75424813Skarels /* 75524813Skarels * First save first hop for return route 75624813Skarels */ 75724813Skarels p = &ip_srcrt.route[ip_nhops - 1]; 75824813Skarels *(mtod(m, struct in_addr *)) = *p--; 75924813Skarels 76024813Skarels /* 76124813Skarels * Copy option fields and padding (nop) to mbuf. 76224813Skarels */ 76324813Skarels ip_srcrt.nop = IPOPT_NOP; 76424813Skarels bcopy((caddr_t)&ip_srcrt, mtod(m, caddr_t) + sizeof(struct in_addr), 76524813Skarels IPOPT_OFFSET + 1 + 1); 76624813Skarels q = (struct in_addr *)(mtod(m, caddr_t) + 76724813Skarels sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1); 76824813Skarels /* 76924813Skarels * Record return path as an IP source route, 77024813Skarels * reversing the path (pointers are now aligned). 77124813Skarels */ 77224813Skarels while (p >= ip_srcrt.route) 77324813Skarels *q++ = *p--; 77424813Skarels return (m); 77524813Skarels } 77624813Skarels 77724813Skarels /* 7784951Swnj * Strip out IP options, at higher 7794951Swnj * level protocol in the kernel. 7804951Swnj * Second argument is buffer to which options 7814951Swnj * will be moved, and return value is their length. 7824640Swnj */ 7835217Swnj ip_stripoptions(ip, mopt) 7844640Swnj struct ip *ip; 7855217Swnj struct mbuf *mopt; 7864495Swnj { 7874640Swnj register int i; 7884640Swnj register struct mbuf *m; 78924813Skarels register caddr_t opts; 7904640Swnj int olen; 7914640Swnj 7924640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 7934951Swnj m = dtom(ip); 79424813Skarels opts = (caddr_t)(ip + 1); 7955217Swnj if (mopt) { 7965217Swnj mopt->m_len = olen; 7975217Swnj mopt->m_off = MMINOFF; 79824813Skarels bcopy(opts, mtod(mopt, caddr_t), (unsigned)olen); 7995217Swnj } 8004640Swnj i = m->m_len - (sizeof (struct ip) + olen); 80124813Skarels bcopy(opts + olen, opts, (unsigned)i); 8025243Sroot m->m_len -= olen; 80324813Skarels ip->ip_hl = sizeof(struct ip) >> 2; 8044495Swnj } 8056583Ssam 80614670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 80724813Skarels 0, 0, 0, 0, 80814670Ssam 0, 0, EHOSTDOWN, EHOSTUNREACH, 80914670Ssam ENETUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 81024813Skarels EMSGSIZE, EHOSTUNREACH, 0, 0, 81124813Skarels 0, 0, 0, 0, 81224813Skarels ENOPROTOOPT 8136583Ssam }; 8146583Ssam 81524813Skarels #ifndef IPFORWARDING 81624813Skarels #define IPFORWARDING 1 81724813Skarels #endif 81824813Skarels #ifndef IPSENDREDIRECTS 81924813Skarels #define IPSENDREDIRECTS 1 82024813Skarels #endif 8216583Ssam int ipprintfs = 0; 82224813Skarels int ipforwarding = IPFORWARDING; 82324813Skarels extern int in_interfaces; 82424813Skarels int ipsendredirects = IPSENDREDIRECTS; 82524813Skarels 8266583Ssam /* 8276583Ssam * Forward a packet. If some error occurs return the sender 82818376Skarels * an icmp packet. Note we can't always generate a meaningful 82924813Skarels * icmp message because icmp doesn't have a large enough repertoire 8306583Ssam * of codes and types. 83126308Skarels * 83226308Skarels * If not forwarding (possibly because we have only a single external 83326308Skarels * network), just drop the packet. This could be confusing if ipforwarding 83426308Skarels * was zero but some routing protocol was advancing us as a gateway 83526308Skarels * to somewhere. However, we must let the routing protocol deal with that. 8366583Ssam */ 83724813Skarels ip_forward(ip, ifp) 8386583Ssam register struct ip *ip; 83924813Skarels struct ifnet *ifp; 8406583Ssam { 84124813Skarels register int error, type = 0, code; 84224813Skarels register struct sockaddr_in *sin; 84318376Skarels struct mbuf *mcopy; 84424813Skarels struct in_addr dest; 8456583Ssam 84624813Skarels dest.s_addr = 0; 8476583Ssam if (ipprintfs) 8486583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 8496583Ssam ip->ip_dst, ip->ip_ttl); 85018376Skarels ip->ip_id = htons(ip->ip_id); 85124813Skarels if (ipforwarding == 0 || in_interfaces <= 1) { 85226308Skarels ipstat.ips_cantforward++; 85326985Skarels #ifdef GATEWAY 85426985Skarels type = ICMP_UNREACH, code = ICMP_UNREACH_NET; 85526985Skarels goto sendicmp; 85626985Skarels #else 85726308Skarels m_freem(dtom(ip)); 85826308Skarels return; 85926985Skarels #endif 8606583Ssam } 86132572Skarels if (in_canforward(ip->ip_dst) == 0) { 86232572Skarels m_freem(dtom(ip)); 86332572Skarels return; 86432572Skarels } 86531393Skarels if (ip->ip_ttl <= IPTTLDEC) { 8666583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 8676583Ssam goto sendicmp; 8686583Ssam } 8696583Ssam ip->ip_ttl -= IPTTLDEC; 8706609Ssam 8716609Ssam /* 8726609Ssam * Save at most 64 bytes of the packet in case 8736609Ssam * we need to generate an ICMP message to the src. 8746609Ssam */ 87528944Skarels mcopy = m_copy(dtom(ip), 0, imin((int)ip->ip_len, 64)); 8766583Ssam 87724813Skarels sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 87824813Skarels if (ipforward_rt.ro_rt == 0 || 87924813Skarels ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 88024813Skarels if (ipforward_rt.ro_rt) { 88124813Skarels RTFREE(ipforward_rt.ro_rt); 88224813Skarels ipforward_rt.ro_rt = 0; 88324813Skarels } 88424813Skarels sin->sin_family = AF_INET; 88524813Skarels sin->sin_addr = ip->ip_dst; 88624813Skarels 88724813Skarels rtalloc(&ipforward_rt); 88824813Skarels } 88924813Skarels /* 89024813Skarels * If forwarding packet using same interface that it came in on, 89124813Skarels * perhaps should send a redirect to sender to shortcut a hop. 89224813Skarels * Only send redirect if source is sending directly to us, 89324813Skarels * and if packet was not source routed (or has any options). 89430447Skarels * Also, don't send redirect if forwarding using a default route 89530447Skarels * or a route modfied by a redirect. 89624813Skarels */ 89730447Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 89824813Skarels if (ipforward_rt.ro_rt && ipforward_rt.ro_rt->rt_ifp == ifp && 89932572Skarels (ipforward_rt.ro_rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 90030447Skarels satosin(&ipforward_rt.ro_rt->rt_dst)->sin_addr.s_addr != 0 && 90124813Skarels ipsendredirects && ip->ip_hl == (sizeof(struct ip) >> 2)) { 90224813Skarels struct in_ifaddr *ia; 90324813Skarels u_long src = ntohl(ip->ip_src.s_addr); 90424813Skarels u_long dst = ntohl(ip->ip_dst.s_addr); 90524813Skarels 90624813Skarels if ((ia = ifptoia(ifp)) && 90724813Skarels (src & ia->ia_subnetmask) == ia->ia_subnet) { 90824813Skarels if (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) 90924813Skarels dest = satosin(&ipforward_rt.ro_rt->rt_gateway)->sin_addr; 91024813Skarels else 91124813Skarels dest = ip->ip_dst; 91224813Skarels /* 91324813Skarels * If the destination is reached by a route to host, 91427145Skarels * is on a subnet of a local net, or is directly 91527145Skarels * on the attached net (!), use host redirect. 91624813Skarels * (We may be the correct first hop for other subnets.) 91724813Skarels */ 91824813Skarels type = ICMP_REDIRECT; 91924813Skarels code = ICMP_REDIRECT_NET; 92024813Skarels if ((ipforward_rt.ro_rt->rt_flags & RTF_HOST) || 92124813Skarels (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) == 0) 92224813Skarels code = ICMP_REDIRECT_HOST; 92324813Skarels else for (ia = in_ifaddr; ia = ia->ia_next; ) 92424813Skarels if ((dst & ia->ia_netmask) == ia->ia_net) { 92527145Skarels if (ia->ia_subnetmask != ia->ia_netmask) 92627145Skarels code = ICMP_REDIRECT_HOST; 92724813Skarels break; 92824813Skarels } 92924813Skarels if (ipprintfs) 93024813Skarels printf("redirect (%d) to %x\n", code, dest); 93124813Skarels } 93224813Skarels } 93324813Skarels 93424813Skarels error = ip_output(dtom(ip), (struct mbuf *)0, &ipforward_rt, 93518376Skarels IP_FORWARDING); 93624813Skarels if (error) 93724813Skarels ipstat.ips_cantforward++; 93824813Skarels else if (type) 93924813Skarels ipstat.ips_redirectsent++; 94024813Skarels else { 9416609Ssam if (mcopy) 9426609Ssam m_freem(mcopy); 94321117Skarels ipstat.ips_forward++; 9446583Ssam return; 9456609Ssam } 94611540Ssam if (mcopy == NULL) 94711540Ssam return; 9486609Ssam ip = mtod(mcopy, struct ip *); 94924813Skarels type = ICMP_UNREACH; 9506609Ssam switch (error) { 9516609Ssam 95224813Skarels case 0: /* forwarded, but need redirect */ 95324813Skarels type = ICMP_REDIRECT; 95424813Skarels /* code set above */ 95524813Skarels break; 95624813Skarels 9576609Ssam case ENETUNREACH: 9586609Ssam case ENETDOWN: 95932572Skarels if (in_localaddr(ip->ip_dst)) 96032572Skarels code = ICMP_UNREACH_HOST; 96132572Skarels else 96232572Skarels code = ICMP_UNREACH_NET; 9636609Ssam break; 9646609Ssam 9656609Ssam case EMSGSIZE: 9666583Ssam code = ICMP_UNREACH_NEEDFRAG; 9676609Ssam break; 9686609Ssam 9696609Ssam case EPERM: 9706609Ssam code = ICMP_UNREACH_PORT; 9716609Ssam break; 9726609Ssam 9736609Ssam case ENOBUFS: 9746609Ssam type = ICMP_SOURCEQUENCH; 9756609Ssam break; 9766609Ssam 9776609Ssam case EHOSTDOWN: 9786609Ssam case EHOSTUNREACH: 9796609Ssam code = ICMP_UNREACH_HOST; 9806609Ssam break; 9816609Ssam } 9826583Ssam sendicmp: 98326031Skarels icmp_error(ip, type, code, ifp, dest); 9846583Ssam } 985