123184Smckusick /* 236814Skarels * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 332787Sbostic * All rights reserved. 423184Smckusick * 532787Sbostic * Redistribution and use in source and binary forms are permitted 634854Sbostic * provided that the above copyright notice and this paragraph are 734854Sbostic * duplicated in all such forms and that any documentation, 834854Sbostic * advertising materials, and other materials related to such 934854Sbostic * distribution and use acknowledge that the software was developed 1034854Sbostic * by the University of California, Berkeley. The name of the 1134854Sbostic * University may not be used to endorse or promote products derived 1234854Sbostic * from this software without specific prior written permission. 1334854Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434854Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534854Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1632787Sbostic * 17*37319Skarels * @(#)ip_input.c 7.12 (Berkeley) 04/08/89 1823184Smckusick */ 194571Swnj 2017060Sbloom #include "param.h" 2117060Sbloom #include "systm.h" 22*37319Skarels #include "malloc.h" 2317060Sbloom #include "mbuf.h" 2417060Sbloom #include "domain.h" 2517060Sbloom #include "protosw.h" 2617060Sbloom #include "socket.h" 2717060Sbloom #include "errno.h" 2817060Sbloom #include "time.h" 2917060Sbloom #include "kernel.h" 308695Sroot 318695Sroot #include "../net/if.h" 328695Sroot #include "../net/route.h" 3310892Ssam 3417060Sbloom #include "in.h" 3517060Sbloom #include "in_pcb.h" 3617060Sbloom #include "in_systm.h" 3718376Skarels #include "in_var.h" 3817060Sbloom #include "ip.h" 3917060Sbloom #include "ip_var.h" 4017060Sbloom #include "ip_icmp.h" 4117060Sbloom #include "tcp.h" 424495Swnj 4336814Skarels #ifndef IPFORWARDING 4436814Skarels #ifdef GATEWAY 4536814Skarels #define IPFORWARDING 1 4636814Skarels #else /* GATEWAY */ 4736814Skarels #define IPFORWARDING 0 4836814Skarels #endif /* GATEWAY */ 4936814Skarels #endif /* IPFORWARDING */ 5036814Skarels #ifndef IPSENDREDIRECTS 5136814Skarels #define IPSENDREDIRECTS 1 5236814Skarels #endif 5336814Skarels int ipprintfs = 0; 5436814Skarels int ipforwarding = IPFORWARDING; 5536814Skarels extern int in_interfaces; 5636814Skarels int ipsendredirects = IPSENDREDIRECTS; 5736814Skarels 584898Swnj u_char ip_protox[IPPROTO_MAX]; 596210Swnj int ipqmaxlen = IFQ_MAXLEN; 6018376Skarels struct in_ifaddr *in_ifaddr; /* first inet address */ 614898Swnj 624801Swnj /* 6324813Skarels * We need to save the IP options in case a protocol wants to respond 6424813Skarels * to an incoming packet over the same route if the packet got here 6524813Skarels * using IP source routing. This allows connection establishment and 6624813Skarels * maintenance when the remote end is on a network that is not known 6724813Skarels * to us. 6824813Skarels */ 6924813Skarels int ip_nhops = 0; 7024813Skarels static struct ip_srcrt { 7136814Skarels struct in_addr dst; /* final destination */ 7224813Skarels char nop; /* one NOP to align */ 7324813Skarels char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 7436814Skarels struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 7524813Skarels } ip_srcrt; 7624813Skarels 7724813Skarels /* 785172Swnj * IP initialization: fill in IP protocol switch table. 795161Swnj * All protocols not implemented in kernel go to raw IP protocol handler. 804801Swnj */ 814801Swnj ip_init() 824801Swnj { 834898Swnj register struct protosw *pr; 844898Swnj register int i; 854495Swnj 8624813Skarels pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 874898Swnj if (pr == 0) 884898Swnj panic("ip_init"); 894898Swnj for (i = 0; i < IPPROTO_MAX; i++) 909030Sroot ip_protox[i] = pr - inetsw; 919030Sroot for (pr = inetdomain.dom_protosw; 9217551Skarels pr < inetdomain.dom_protoswNPROTOSW; pr++) 9316990Skarels if (pr->pr_domain->dom_family == PF_INET && 944898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 959030Sroot ip_protox[pr->pr_protocol] = pr - inetsw; 964801Swnj ipq.next = ipq.prev = &ipq; 978172Sroot ip_id = time.tv_sec & 0xffff; 986210Swnj ipintrq.ifq_maxlen = ipqmaxlen; 994801Swnj } 1004801Swnj 1014640Swnj struct ip *ip_reass(); 102*37319Skarels struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 10324813Skarels struct route ipforward_rt; 1044640Swnj 1054640Swnj /* 1064640Swnj * Ip input routine. Checksum and byte swap header. If fragmented 1074640Swnj * try to reassamble. If complete and fragment queue exists, discard. 1084640Swnj * Process options. Pass to next level. 1094640Swnj */ 1105084Swnj ipintr() 1114495Swnj { 1124923Swnj register struct ip *ip; 1135084Swnj register struct mbuf *m; 1144495Swnj register struct ipq *fp; 11518376Skarels register struct in_ifaddr *ia; 1165084Swnj int hlen, s; 1174495Swnj 1185084Swnj next: 1194640Swnj /* 1205084Swnj * Get next datagram off input queue and get IP header 1215084Swnj * in first mbuf. 1224640Swnj */ 1235084Swnj s = splimp(); 124*37319Skarels IF_DEQUEUE(&ipintrq, m); 1255084Swnj splx(s); 1265218Swnj if (m == 0) 1275084Swnj return; 128*37319Skarels if ((m->m_flags & M_PKTHDR) == 0) 129*37319Skarels panic("ipintr no HDR"); 13026001Skarels /* 13126001Skarels * If no IP addresses have been set yet but the interfaces 13226001Skarels * are receiving, can't do anything with incoming packets yet. 13326001Skarels */ 13426001Skarels if (in_ifaddr == NULL) 13526001Skarels goto bad; 13625920Skarels ipstat.ips_total++; 137*37319Skarels if ((m->m_flags & M_EXT || m->m_len < sizeof (struct ip)) && 13811232Ssam (m = m_pullup(m, sizeof (struct ip))) == 0) { 13911232Ssam ipstat.ips_toosmall++; 14011232Ssam goto next; 14111232Ssam } 1424640Swnj ip = mtod(m, struct ip *); 14318376Skarels hlen = ip->ip_hl << 2; 14424813Skarels if (hlen < sizeof(struct ip)) { /* minimum header length */ 14518376Skarels ipstat.ips_badhlen++; 14621117Skarels goto bad; 14718376Skarels } 14818376Skarels if (hlen > m->m_len) { 14911232Ssam if ((m = m_pullup(m, hlen)) == 0) { 15011232Ssam ipstat.ips_badhlen++; 15111232Ssam goto next; 15211232Ssam } 1535161Swnj ip = mtod(m, struct ip *); 1545161Swnj } 155*37319Skarels if (ip->ip_sum = in_cksum(m, hlen)) { 156*37319Skarels ipstat.ips_badsum++; 157*37319Skarels goto bad; 158*37319Skarels } 1594951Swnj 1604951Swnj /* 1614951Swnj * Convert fields to host representation. 1624951Swnj */ 1634907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 16411232Ssam if (ip->ip_len < hlen) { 16511232Ssam ipstat.ips_badlen++; 16611232Ssam goto bad; 16711232Ssam } 1684640Swnj ip->ip_id = ntohs(ip->ip_id); 1694951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 1704495Swnj 1714543Swnj /* 1724640Swnj * Check that the amount of data in the buffers 1734640Swnj * is as at least much as the IP header would have us expect. 1744640Swnj * Trim mbufs if longer than we expect. 1754640Swnj * Drop packet if shorter than we expect. 1764543Swnj */ 177*37319Skarels if (m->m_pkthdr.len < ip->ip_len) { 178*37319Skarels ipstat.ips_tooshort++; 179*37319Skarels goto bad; 1806088Sroot } 181*37319Skarels if (m->m_pkthdr.len > ip->ip_len) { 182*37319Skarels if (m->m_len == m->m_pkthdr.len) { 183*37319Skarels m->m_len = ip->ip_len; 184*37319Skarels m->m_pkthdr.len = ip->ip_len; 185*37319Skarels } else 186*37319Skarels m_adj(m, ip->ip_len - m->m_pkthdr.len); 1874495Swnj } 1884495Swnj 1894640Swnj /* 1904640Swnj * Process options and, if not destined for us, 1916583Ssam * ship it on. ip_dooptions returns 1 when an 1926583Ssam * error was detected (causing an icmp message 19321117Skarels * to be sent and the original packet to be freed). 1944640Swnj */ 19524813Skarels ip_nhops = 0; /* for source routed packets */ 196*37319Skarels if (hlen > sizeof (struct ip) && ip_dooptions(m)) 1976583Ssam goto next; 1986210Swnj 1996338Ssam /* 20018376Skarels * Check our list of addresses, to see if the packet is for us. 2016338Ssam */ 20218376Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) { 20318376Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 2046338Ssam 20518376Skarels if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 20624813Skarels goto ours; 20725195Skarels if ( 20825195Skarels #ifdef DIRECTED_BROADCAST 209*37319Skarels ia->ia_ifp == m->m_pkthdr.rcvif && 21025195Skarels #endif 21125195Skarels (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 21226247Skarels u_long t; 21325195Skarels 21425195Skarels if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 21525195Skarels ip->ip_dst.s_addr) 21625195Skarels goto ours; 21725195Skarels if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 21825195Skarels goto ours; 21925195Skarels /* 22025195Skarels * Look for all-0's host part (old broadcast addr), 22125195Skarels * either for subnet or net. 22225195Skarels */ 22326247Skarels t = ntohl(ip->ip_dst.s_addr); 22426247Skarels if (t == ia->ia_subnet) 22525195Skarels goto ours; 22626247Skarels if (t == ia->ia_net) 22725195Skarels goto ours; 22825195Skarels } 2296338Ssam } 23024813Skarels if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 23124813Skarels goto ours; 23224813Skarels if (ip->ip_dst.s_addr == INADDR_ANY) 23324813Skarels goto ours; 2344495Swnj 2354640Swnj /* 23624813Skarels * Not for us; forward if possible and desirable. 23724813Skarels */ 23836814Skarels if (ipforwarding == 0 23936814Skarels #ifndef GATEWAY 24036814Skarels || in_interfaces <= 1 24136814Skarels #endif 24236814Skarels ) { 24336814Skarels ipstat.ips_cantforward++; 24436814Skarels m_freem(m); 24536814Skarels } else 246*37319Skarels ip_forward(m); 24724813Skarels goto next; 24824813Skarels 24924813Skarels ours: 25024813Skarels /* 25133743Skarels * If offset or IP_MF are set, must reassemble. 25233743Skarels * Otherwise, nothing need be done. 25333743Skarels * (We could look in the reassembly queue to see 25433743Skarels * if the packet was previously fragmented, 25533743Skarels * but it's not worth the time; just let them time out.) 2564640Swnj */ 25733743Skarels if (ip->ip_off &~ IP_DF) { 25833743Skarels /* 25933743Skarels * Look for queue of fragments 26033743Skarels * of this datagram. 26133743Skarels */ 26233743Skarels for (fp = ipq.next; fp != &ipq; fp = fp->next) 26333743Skarels if (ip->ip_id == fp->ipq_id && 26433743Skarels ip->ip_src.s_addr == fp->ipq_src.s_addr && 26533743Skarels ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 26633743Skarels ip->ip_p == fp->ipq_p) 26733743Skarels goto found; 26833743Skarels fp = 0; 2694640Swnj found: 2704495Swnj 27133743Skarels /* 27233743Skarels * Adjust ip_len to not reflect header, 27333743Skarels * set ip_mff if more fragments are expected, 27433743Skarels * convert offset of this to bytes. 27533743Skarels */ 27633743Skarels ip->ip_len -= hlen; 27733743Skarels ((struct ipasfrag *)ip)->ipf_mff = 0; 27833743Skarels if (ip->ip_off & IP_MF) 27933743Skarels ((struct ipasfrag *)ip)->ipf_mff = 1; 28033743Skarels ip->ip_off <<= 3; 2814495Swnj 28233743Skarels /* 28333743Skarels * If datagram marked as having more fragments 28433743Skarels * or if this is not the first fragment, 28533743Skarels * attempt reassembly; if it succeeds, proceed. 28633743Skarels */ 28733743Skarels if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 28833743Skarels ipstat.ips_fragments++; 28933743Skarels ip = ip_reass((struct ipasfrag *)ip, fp); 29033743Skarels if (ip == 0) 29133743Skarels goto next; 29233743Skarels m = dtom(ip); 29333743Skarels } else 29433743Skarels if (fp) 29533743Skarels ip_freef(fp); 2964640Swnj } else 29733743Skarels ip->ip_len -= hlen; 2984951Swnj 2994951Swnj /* 3004951Swnj * Switch out to protocol's input routine. 3014951Swnj */ 302*37319Skarels (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 3035084Swnj goto next; 3044951Swnj bad: 3054951Swnj m_freem(m); 3065084Swnj goto next; 3074640Swnj } 3084495Swnj 3094640Swnj /* 3104640Swnj * Take incoming datagram fragment and try to 3114951Swnj * reassemble it into whole datagram. If a chain for 3124640Swnj * reassembly of this datagram already exists, then it 3134640Swnj * is given as fp; otherwise have to make a chain. 3144640Swnj */ 3154640Swnj struct ip * 3164640Swnj ip_reass(ip, fp) 3174898Swnj register struct ipasfrag *ip; 3184640Swnj register struct ipq *fp; 3194640Swnj { 3204640Swnj register struct mbuf *m = dtom(ip); 3214898Swnj register struct ipasfrag *q; 3224640Swnj struct mbuf *t; 3234640Swnj int hlen = ip->ip_hl << 2; 3244640Swnj int i, next; 3254543Swnj 3264640Swnj /* 3274640Swnj * Presence of header sizes in mbufs 3284640Swnj * would confuse code below. 3294640Swnj */ 330*37319Skarels m->m_data += hlen; 3314640Swnj m->m_len -= hlen; 3324495Swnj 3334640Swnj /* 3344640Swnj * If first fragment to arrive, create a reassembly queue. 3354640Swnj */ 3364640Swnj if (fp == 0) { 33731201Skarels if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 3384640Swnj goto dropfrag; 3394640Swnj fp = mtod(t, struct ipq *); 3404640Swnj insque(fp, &ipq); 3414640Swnj fp->ipq_ttl = IPFRAGTTL; 3424640Swnj fp->ipq_p = ip->ip_p; 3434640Swnj fp->ipq_id = ip->ip_id; 3444898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 3454898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 3464898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 3475161Swnj q = (struct ipasfrag *)fp; 3485161Swnj goto insert; 3494640Swnj } 3504495Swnj 3514640Swnj /* 3524640Swnj * Find a segment which begins after this one does. 3534640Swnj */ 3544898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3554640Swnj if (q->ip_off > ip->ip_off) 3564640Swnj break; 3574495Swnj 3584640Swnj /* 3594640Swnj * If there is a preceding segment, it may provide some of 3604640Swnj * our data already. If so, drop the data from the incoming 3614640Swnj * segment. If it provides all of our data, drop us. 3624640Swnj */ 3634898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 3644898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 3654640Swnj if (i > 0) { 3664640Swnj if (i >= ip->ip_len) 3674640Swnj goto dropfrag; 3684640Swnj m_adj(dtom(ip), i); 3694640Swnj ip->ip_off += i; 3704640Swnj ip->ip_len -= i; 3714640Swnj } 3724640Swnj } 3734543Swnj 3744640Swnj /* 3754640Swnj * While we overlap succeeding segments trim them or, 3764640Swnj * if they are completely covered, dequeue them. 3774640Swnj */ 3784898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 3794640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 3804640Swnj if (i < q->ip_len) { 3814640Swnj q->ip_len -= i; 3826256Sroot q->ip_off += i; 3834640Swnj m_adj(dtom(q), i); 3844640Swnj break; 3854495Swnj } 3864898Swnj q = q->ipf_next; 3874898Swnj m_freem(dtom(q->ipf_prev)); 3884898Swnj ip_deq(q->ipf_prev); 3894543Swnj } 3904495Swnj 3915161Swnj insert: 3924640Swnj /* 3934640Swnj * Stick new segment in its place; 3944640Swnj * check for complete reassembly. 3954640Swnj */ 3964898Swnj ip_enq(ip, q->ipf_prev); 3974640Swnj next = 0; 3984898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 3994640Swnj if (q->ip_off != next) 4004640Swnj return (0); 4014640Swnj next += q->ip_len; 4024640Swnj } 4034898Swnj if (q->ipf_prev->ipf_mff) 4044640Swnj return (0); 4054495Swnj 4064640Swnj /* 4074640Swnj * Reassembly is complete; concatenate fragments. 4084640Swnj */ 4094640Swnj q = fp->ipq_next; 4104640Swnj m = dtom(q); 4114640Swnj t = m->m_next; 4124640Swnj m->m_next = 0; 4134640Swnj m_cat(m, t); 4146298Swnj q = q->ipf_next; 4156298Swnj while (q != (struct ipasfrag *)fp) { 4166298Swnj t = dtom(q); 4176298Swnj q = q->ipf_next; 4186298Swnj m_cat(m, t); 4196298Swnj } 4204495Swnj 4214640Swnj /* 4224640Swnj * Create header for new ip packet by 4234640Swnj * modifying header of first packet; 4244640Swnj * dequeue and discard fragment reassembly header. 4254640Swnj * Make header visible. 4264640Swnj */ 4274640Swnj ip = fp->ipq_next; 4284640Swnj ip->ip_len = next; 4294898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 4304898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 4314640Swnj remque(fp); 4324907Swnj (void) m_free(dtom(fp)); 4334640Swnj m = dtom(ip); 43424813Skarels m->m_len += (ip->ip_hl << 2); 435*37319Skarels m->m_data -= (ip->ip_hl << 2); 4364898Swnj return ((struct ip *)ip); 4374495Swnj 4384640Swnj dropfrag: 43924813Skarels ipstat.ips_fragdropped++; 4404640Swnj m_freem(m); 4414640Swnj return (0); 4424495Swnj } 4434495Swnj 4444640Swnj /* 4454640Swnj * Free a fragment reassembly header and all 4464640Swnj * associated datagrams. 4474640Swnj */ 4484640Swnj ip_freef(fp) 4494640Swnj struct ipq *fp; 4504495Swnj { 45110735Ssam register struct ipasfrag *q, *p; 4524495Swnj 45310735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 45410735Ssam p = q->ipf_next; 45510735Ssam ip_deq(q); 4564640Swnj m_freem(dtom(q)); 45710735Ssam } 45810735Ssam remque(fp); 45910735Ssam (void) m_free(dtom(fp)); 4604495Swnj } 4614495Swnj 4624640Swnj /* 4634640Swnj * Put an ip fragment on a reassembly chain. 4644640Swnj * Like insque, but pointers in middle of structure. 4654640Swnj */ 4664640Swnj ip_enq(p, prev) 4674898Swnj register struct ipasfrag *p, *prev; 4684495Swnj { 4694951Swnj 4704898Swnj p->ipf_prev = prev; 4714898Swnj p->ipf_next = prev->ipf_next; 4724898Swnj prev->ipf_next->ipf_prev = p; 4734898Swnj prev->ipf_next = p; 4744495Swnj } 4754495Swnj 4764640Swnj /* 4774640Swnj * To ip_enq as remque is to insque. 4784640Swnj */ 4794640Swnj ip_deq(p) 4804898Swnj register struct ipasfrag *p; 4814640Swnj { 4824951Swnj 4834898Swnj p->ipf_prev->ipf_next = p->ipf_next; 4844898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 4854495Swnj } 4864495Swnj 4874640Swnj /* 4884640Swnj * IP timer processing; 4894640Swnj * if a timer expires on a reassembly 4904640Swnj * queue, discard it. 4914640Swnj */ 4924801Swnj ip_slowtimo() 4934495Swnj { 4944495Swnj register struct ipq *fp; 4954640Swnj int s = splnet(); 4964951Swnj 4975243Sroot fp = ipq.next; 4985243Sroot if (fp == 0) { 4995243Sroot splx(s); 5005243Sroot return; 5015243Sroot } 50210735Ssam while (fp != &ipq) { 50310735Ssam --fp->ipq_ttl; 50410735Ssam fp = fp->next; 50524813Skarels if (fp->prev->ipq_ttl == 0) { 50624813Skarels ipstat.ips_fragtimeout++; 50710735Ssam ip_freef(fp->prev); 50824813Skarels } 50910735Ssam } 5104640Swnj splx(s); 5114495Swnj } 5124495Swnj 5134951Swnj /* 5144951Swnj * Drain off all datagram fragments. 5154951Swnj */ 5164801Swnj ip_drain() 5174801Swnj { 5184801Swnj 51924813Skarels while (ipq.next != &ipq) { 52024813Skarels ipstat.ips_fragdropped++; 52110735Ssam ip_freef(ipq.next); 52224813Skarels } 5234801Swnj } 5244923Swnj 52530925Skarels extern struct in_ifaddr *ifptoia(); 52624813Skarels struct in_ifaddr *ip_rtaddr(); 52724813Skarels 5284640Swnj /* 5294640Swnj * Do option processing on a datagram, 5304640Swnj * possibly discarding it if bad options 5314640Swnj * are encountered. 5324640Swnj */ 533*37319Skarels ip_dooptions(m) 53436814Skarels struct mbuf *m; 5354495Swnj { 53636814Skarels register struct ip *ip = mtod(m, struct ip *); 5374640Swnj register u_char *cp; 53824813Skarels register struct ip_timestamp *ipt; 53924813Skarels register struct in_ifaddr *ia; 54036814Skarels int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 5414923Swnj struct in_addr *sin; 54224813Skarels n_time ntime; 5434495Swnj 5444640Swnj cp = (u_char *)(ip + 1); 5454640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 5464640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 54724813Skarels opt = cp[IPOPT_OPTVAL]; 5484640Swnj if (opt == IPOPT_EOL) 5494640Swnj break; 5504640Swnj if (opt == IPOPT_NOP) 5514640Swnj optlen = 1; 55216392Ssam else { 55324813Skarels optlen = cp[IPOPT_OLEN]; 55424813Skarels if (optlen <= 0 || optlen > cnt) { 55524813Skarels code = &cp[IPOPT_OLEN] - (u_char *)ip; 55617551Skarels goto bad; 55724813Skarels } 55816392Ssam } 5594640Swnj switch (opt) { 5604495Swnj 5614640Swnj default: 5624640Swnj break; 5634495Swnj 5644951Swnj /* 5654951Swnj * Source routing with record. 5664951Swnj * Find interface with current destination address. 5674951Swnj * If none on this machine then drop if strictly routed, 5684951Swnj * or do nothing if loosely routed. 5694951Swnj * Record interface address and bring up next address 5704951Swnj * component. If strictly routed make sure next 5714951Swnj * address on directly accessible net. 5724951Swnj */ 5734640Swnj case IPOPT_LSRR: 5747508Sroot case IPOPT_SSRR: 57524813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 57624813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 57724813Skarels goto bad; 57824813Skarels } 57924813Skarels ipaddr.sin_addr = ip->ip_dst; 58024813Skarels ia = (struct in_ifaddr *) 58124813Skarels ifa_ifwithaddr((struct sockaddr *)&ipaddr); 58224813Skarels if (ia == 0) { 58324813Skarels if (opt == IPOPT_SSRR) { 58424813Skarels type = ICMP_UNREACH; 58524813Skarels code = ICMP_UNREACH_SRCFAIL; 5864951Swnj goto bad; 58724813Skarels } 58824813Skarels /* 58924813Skarels * Loose routing, and not at next destination 59024813Skarels * yet; nothing to do except forward. 59124813Skarels */ 5924951Swnj break; 5934640Swnj } 59424813Skarels off--; /* 0 origin */ 59524813Skarels if (off > optlen - sizeof(struct in_addr)) { 59624813Skarels /* 59724813Skarels * End of source route. Should be for us. 59824813Skarels */ 59924813Skarels save_rte(cp, ip->ip_src); 6004951Swnj break; 60124813Skarels } 60224813Skarels /* 60324813Skarels * locate outgoing interface 60424813Skarels */ 60526384Skarels bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 60624813Skarels sizeof(ipaddr.sin_addr)); 60724813Skarels if ((opt == IPOPT_SSRR && 60824813Skarels in_iaonnetof(in_netof(ipaddr.sin_addr)) == 0) || 60924813Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 61024813Skarels type = ICMP_UNREACH; 61124813Skarels code = ICMP_UNREACH_SRCFAIL; 6124951Swnj goto bad; 61324813Skarels } 61424813Skarels ip->ip_dst = ipaddr.sin_addr; 61526384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 61626384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 61724813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 61836814Skarels forward = 1; 6194640Swnj break; 6204495Swnj 62124813Skarels case IPOPT_RR: 62224813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 62324813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 62424813Skarels goto bad; 62524813Skarels } 62624813Skarels /* 62724813Skarels * If no space remains, ignore. 62824813Skarels */ 62924813Skarels off--; /* 0 origin */ 63024813Skarels if (off > optlen - sizeof(struct in_addr)) 63124813Skarels break; 63231393Skarels bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 63324813Skarels sizeof(ipaddr.sin_addr)); 63424813Skarels /* 635*37319Skarels * locate outgoing interface; if we're the destination, 636*37319Skarels * use the incoming interface (should be same). 63724813Skarels */ 638*37319Skarels if ((ia = 639*37319Skarels (struct in_ifaddr *)ifa_ifwithaddr( 640*37319Skarels (struct sockaddr *)&ipaddr)) == 0 && 641*37319Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 64224813Skarels type = ICMP_UNREACH; 64332113Skarels code = ICMP_UNREACH_HOST; 64424813Skarels goto bad; 64524813Skarels } 64626384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 64726384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 64824813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 64924813Skarels break; 65024813Skarels 6514640Swnj case IPOPT_TS: 6526583Ssam code = cp - (u_char *)ip; 6534801Swnj ipt = (struct ip_timestamp *)cp; 6544801Swnj if (ipt->ipt_len < 5) 6554640Swnj goto bad; 6564801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 6574801Swnj if (++ipt->ipt_oflw == 0) 6584640Swnj goto bad; 6594495Swnj break; 6604640Swnj } 66130925Skarels sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 6624801Swnj switch (ipt->ipt_flg) { 6634495Swnj 6644640Swnj case IPOPT_TS_TSONLY: 6654640Swnj break; 6664640Swnj 6674640Swnj case IPOPT_TS_TSANDADDR: 66824813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 66924813Skarels sizeof(struct in_addr) > ipt->ipt_len) 6704640Swnj goto bad; 671*37319Skarels ia = ifptoia(m->m_pkthdr.rcvif); 67230925Skarels bcopy((caddr_t)&IA_SIN(ia)->sin_addr, 67324813Skarels (caddr_t)sin, sizeof(struct in_addr)); 67430925Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6754640Swnj break; 6764640Swnj 6774640Swnj case IPOPT_TS_PRESPEC: 67830925Skarels if (ipt->ipt_ptr + sizeof(n_time) + 67930925Skarels sizeof(struct in_addr) > ipt->ipt_len) 68030925Skarels goto bad; 68124813Skarels bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 68224813Skarels sizeof(struct in_addr)); 68318376Skarels if (ifa_ifwithaddr((struct sockaddr *)&ipaddr) == 0) 6844951Swnj continue; 68524813Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6864640Swnj break; 6874640Swnj 6884495Swnj default: 6894640Swnj goto bad; 6904495Swnj } 69124813Skarels ntime = iptime(); 69230925Skarels bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 69330925Skarels sizeof(n_time)); 69424813Skarels ipt->ipt_ptr += sizeof(n_time); 6954640Swnj } 6964495Swnj } 69736814Skarels if (forward) { 698*37319Skarels ip_forward(m); 69936814Skarels return (1); 70036814Skarels } else 70136814Skarels return (0); 7024640Swnj bad: 703*37319Skarels icmp_error(m, type, code); 7046583Ssam return (1); 7054495Swnj } 7064495Swnj 7074640Swnj /* 70824813Skarels * Given address of next destination (final or next hop), 70924813Skarels * return internet address info of interface to be used to get there. 71024813Skarels */ 71124813Skarels struct in_ifaddr * 71224813Skarels ip_rtaddr(dst) 71324813Skarels struct in_addr dst; 71424813Skarels { 71524813Skarels register struct sockaddr_in *sin; 71624813Skarels register struct in_ifaddr *ia; 71724813Skarels 71824813Skarels sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 71924813Skarels 72024813Skarels if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 72124813Skarels if (ipforward_rt.ro_rt) { 72224813Skarels RTFREE(ipforward_rt.ro_rt); 72324813Skarels ipforward_rt.ro_rt = 0; 72424813Skarels } 72524813Skarels sin->sin_family = AF_INET; 726*37319Skarels sin->sin_len = sizeof(*sin); 72724813Skarels sin->sin_addr = dst; 72824813Skarels 72924813Skarels rtalloc(&ipforward_rt); 73024813Skarels } 73124813Skarels if (ipforward_rt.ro_rt == 0) 73224813Skarels return ((struct in_ifaddr *)0); 73324813Skarels /* 73424813Skarels * Find address associated with outgoing interface. 73524813Skarels */ 73624813Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) 73724813Skarels if (ia->ia_ifp == ipforward_rt.ro_rt->rt_ifp) 73824813Skarels break; 73924813Skarels return (ia); 74024813Skarels } 74124813Skarels 74224813Skarels /* 74324813Skarels * Save incoming source route for use in replies, 74424813Skarels * to be picked up later by ip_srcroute if the receiver is interested. 74524813Skarels */ 74624813Skarels save_rte(option, dst) 74726384Skarels u_char *option; 74824813Skarels struct in_addr dst; 74924813Skarels { 75026384Skarels unsigned olen; 75124813Skarels 75224813Skarels olen = option[IPOPT_OLEN]; 75336814Skarels if (ipprintfs) 75436814Skarels printf("save_rte: olen %d\n", olen); 75536814Skarels if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 75624813Skarels return; 75726384Skarels bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 75824813Skarels ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 75936814Skarels ip_srcrt.dst = dst; 76024813Skarels } 76124813Skarels 76224813Skarels /* 76324813Skarels * Retrieve incoming source route for use in replies, 76424813Skarels * in the same form used by setsockopt. 76524813Skarels * The first hop is placed before the options, will be removed later. 76624813Skarels */ 76724813Skarels struct mbuf * 76824813Skarels ip_srcroute() 76924813Skarels { 77024813Skarels register struct in_addr *p, *q; 77124813Skarels register struct mbuf *m; 77224813Skarels 77324813Skarels if (ip_nhops == 0) 77424813Skarels return ((struct mbuf *)0); 77531201Skarels m = m_get(M_DONTWAIT, MT_SOOPTS); 77631201Skarels if (m == 0) 77731201Skarels return ((struct mbuf *)0); 77824813Skarels 77936814Skarels #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 78036814Skarels 78136814Skarels /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 78236814Skarels m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 78336814Skarels OPTSIZ; 78436814Skarels if (ipprintfs) 78536814Skarels printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 78636814Skarels 78724813Skarels /* 78824813Skarels * First save first hop for return route 78924813Skarels */ 79024813Skarels p = &ip_srcrt.route[ip_nhops - 1]; 79124813Skarels *(mtod(m, struct in_addr *)) = *p--; 79236814Skarels if (ipprintfs) 79336814Skarels printf(" hops %X", ntohl(*mtod(m, struct in_addr *))); 79424813Skarels 79524813Skarels /* 79624813Skarels * Copy option fields and padding (nop) to mbuf. 79724813Skarels */ 79824813Skarels ip_srcrt.nop = IPOPT_NOP; 79936814Skarels ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 80036814Skarels bcopy((caddr_t)&ip_srcrt.nop, 80136814Skarels mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 80224813Skarels q = (struct in_addr *)(mtod(m, caddr_t) + 80336814Skarels sizeof(struct in_addr) + OPTSIZ); 80436814Skarels #undef OPTSIZ 80524813Skarels /* 80624813Skarels * Record return path as an IP source route, 80724813Skarels * reversing the path (pointers are now aligned). 80824813Skarels */ 80936814Skarels while (p >= ip_srcrt.route) { 81036814Skarels if (ipprintfs) 81136814Skarels printf(" %X", ntohl(*q)); 81224813Skarels *q++ = *p--; 81336814Skarels } 81436814Skarels /* 81536814Skarels * Last hop goes to final destination. 81636814Skarels */ 81736814Skarels *q = ip_srcrt.dst; 81836814Skarels if (ipprintfs) 81936814Skarels printf(" %X\n", ntohl(*q)); 82024813Skarels return (m); 82124813Skarels } 82224813Skarels 82324813Skarels /* 8244951Swnj * Strip out IP options, at higher 8254951Swnj * level protocol in the kernel. 8264951Swnj * Second argument is buffer to which options 8274951Swnj * will be moved, and return value is their length. 82836814Skarels * XXX should be deleted; last arg currently ignored. 8294640Swnj */ 830*37319Skarels ip_stripoptions(m, mopt) 831*37319Skarels register struct mbuf *m; 8325217Swnj struct mbuf *mopt; 8334495Swnj { 8344640Swnj register int i; 835*37319Skarels struct ip *ip = mtod(m, struct ip *); 83624813Skarels register caddr_t opts; 8374640Swnj int olen; 8384640Swnj 8394640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 84024813Skarels opts = (caddr_t)(ip + 1); 8414640Swnj i = m->m_len - (sizeof (struct ip) + olen); 84224813Skarels bcopy(opts + olen, opts, (unsigned)i); 8435243Sroot m->m_len -= olen; 844*37319Skarels if (m->m_flags & M_PKTHDR) 845*37319Skarels m->m_pkthdr.len -= olen; 84624813Skarels ip->ip_hl = sizeof(struct ip) >> 2; 8474495Swnj } 8486583Ssam 84914670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 85024813Skarels 0, 0, 0, 0, 85114670Ssam 0, 0, EHOSTDOWN, EHOSTUNREACH, 85214670Ssam ENETUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 85324813Skarels EMSGSIZE, EHOSTUNREACH, 0, 0, 85424813Skarels 0, 0, 0, 0, 85524813Skarels ENOPROTOOPT 8566583Ssam }; 8576583Ssam 8586583Ssam /* 8596583Ssam * Forward a packet. If some error occurs return the sender 86018376Skarels * an icmp packet. Note we can't always generate a meaningful 86124813Skarels * icmp message because icmp doesn't have a large enough repertoire 8626583Ssam * of codes and types. 86326308Skarels * 86426308Skarels * If not forwarding (possibly because we have only a single external 86526308Skarels * network), just drop the packet. This could be confusing if ipforwarding 86626308Skarels * was zero but some routing protocol was advancing us as a gateway 86726308Skarels * to somewhere. However, we must let the routing protocol deal with that. 8686583Ssam */ 869*37319Skarels ip_forward(m) 87036814Skarels struct mbuf *m; 8716583Ssam { 87236814Skarels register struct ip *ip = mtod(m, struct ip *); 87324813Skarels register int error, type = 0, code; 87424813Skarels register struct sockaddr_in *sin; 87518376Skarels struct mbuf *mcopy; 87624813Skarels struct in_addr dest; 8776583Ssam 87824813Skarels dest.s_addr = 0; 8796583Ssam if (ipprintfs) 8806583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 8816583Ssam ip->ip_dst, ip->ip_ttl); 882*37319Skarels if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 88326308Skarels ipstat.ips_cantforward++; 884*37319Skarels m_freem(m); 88526308Skarels return; 8866583Ssam } 88736814Skarels ip->ip_id = htons(ip->ip_id); 88831393Skarels if (ip->ip_ttl <= IPTTLDEC) { 8896583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 8906583Ssam goto sendicmp; 8916583Ssam } 8926583Ssam ip->ip_ttl -= IPTTLDEC; 8936609Ssam 8946609Ssam /* 8956609Ssam * Save at most 64 bytes of the packet in case 8966609Ssam * we need to generate an ICMP message to the src. 8976609Ssam */ 898*37319Skarels mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 8996583Ssam 90024813Skarels sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 90124813Skarels if (ipforward_rt.ro_rt == 0 || 90224813Skarels ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 90324813Skarels if (ipforward_rt.ro_rt) { 90424813Skarels RTFREE(ipforward_rt.ro_rt); 90524813Skarels ipforward_rt.ro_rt = 0; 90624813Skarels } 90724813Skarels sin->sin_family = AF_INET; 908*37319Skarels sin->sin_len = sizeof(*sin); 90924813Skarels sin->sin_addr = ip->ip_dst; 91024813Skarels 91124813Skarels rtalloc(&ipforward_rt); 91224813Skarels } 91324813Skarels /* 91424813Skarels * If forwarding packet using same interface that it came in on, 91524813Skarels * perhaps should send a redirect to sender to shortcut a hop. 91624813Skarels * Only send redirect if source is sending directly to us, 91724813Skarels * and if packet was not source routed (or has any options). 91830447Skarels * Also, don't send redirect if forwarding using a default route 91930447Skarels * or a route modfied by a redirect. 92024813Skarels */ 92130447Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 922*37319Skarels if (ipforward_rt.ro_rt && 923*37319Skarels ipforward_rt.ro_rt->rt_ifp == m->m_pkthdr.rcvif && 92436814Skarels (ipforward_rt.ro_rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 925*37319Skarels satosin(rt_key(ipforward_rt.ro_rt))->sin_addr.s_addr != 0 && 92624813Skarels ipsendredirects && ip->ip_hl == (sizeof(struct ip) >> 2)) { 92724813Skarels struct in_ifaddr *ia; 92824813Skarels u_long src = ntohl(ip->ip_src.s_addr); 92924813Skarels u_long dst = ntohl(ip->ip_dst.s_addr); 93024813Skarels 931*37319Skarels if ((ia = ifptoia(m->m_pkthdr.rcvif)) && 93224813Skarels (src & ia->ia_subnetmask) == ia->ia_subnet) { 93324813Skarels if (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) 934*37319Skarels dest = satosin(ipforward_rt.ro_rt->rt_gateway)->sin_addr; 93524813Skarels else 93624813Skarels dest = ip->ip_dst; 93724813Skarels /* 93824813Skarels * If the destination is reached by a route to host, 93927145Skarels * is on a subnet of a local net, or is directly 94027145Skarels * on the attached net (!), use host redirect. 94124813Skarels * (We may be the correct first hop for other subnets.) 94224813Skarels */ 94324813Skarels type = ICMP_REDIRECT; 94424813Skarels code = ICMP_REDIRECT_NET; 94524813Skarels if ((ipforward_rt.ro_rt->rt_flags & RTF_HOST) || 94624813Skarels (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) == 0) 94724813Skarels code = ICMP_REDIRECT_HOST; 94824813Skarels else for (ia = in_ifaddr; ia = ia->ia_next; ) 94924813Skarels if ((dst & ia->ia_netmask) == ia->ia_net) { 95027145Skarels if (ia->ia_subnetmask != ia->ia_netmask) 95127145Skarels code = ICMP_REDIRECT_HOST; 95224813Skarels break; 95324813Skarels } 95424813Skarels if (ipprintfs) 95524813Skarels printf("redirect (%d) to %x\n", code, dest); 95624813Skarels } 95724813Skarels } 95824813Skarels 959*37319Skarels error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING); 96024813Skarels if (error) 96124813Skarels ipstat.ips_cantforward++; 96224813Skarels else if (type) 96324813Skarels ipstat.ips_redirectsent++; 96424813Skarels else { 9656609Ssam if (mcopy) 9666609Ssam m_freem(mcopy); 96721117Skarels ipstat.ips_forward++; 9686583Ssam return; 9696609Ssam } 97011540Ssam if (mcopy == NULL) 97111540Ssam return; 9726609Ssam ip = mtod(mcopy, struct ip *); 97324813Skarels type = ICMP_UNREACH; 9746609Ssam switch (error) { 9756609Ssam 97624813Skarels case 0: /* forwarded, but need redirect */ 97724813Skarels type = ICMP_REDIRECT; 97824813Skarels /* code set above */ 97924813Skarels break; 98024813Skarels 9816609Ssam case ENETUNREACH: 9826609Ssam case ENETDOWN: 98332572Skarels if (in_localaddr(ip->ip_dst)) 98432572Skarels code = ICMP_UNREACH_HOST; 98532572Skarels else 98632572Skarels code = ICMP_UNREACH_NET; 9876609Ssam break; 9886609Ssam 9896609Ssam case EMSGSIZE: 9906583Ssam code = ICMP_UNREACH_NEEDFRAG; 9916609Ssam break; 9926609Ssam 9936609Ssam case EPERM: 9946609Ssam code = ICMP_UNREACH_PORT; 9956609Ssam break; 9966609Ssam 9976609Ssam case ENOBUFS: 9986609Ssam type = ICMP_SOURCEQUENCH; 999*37319Skarels code = 0; 10006609Ssam break; 10016609Ssam 10026609Ssam case EHOSTDOWN: 10036609Ssam case EHOSTUNREACH: 10046609Ssam code = ICMP_UNREACH_HOST; 10056609Ssam break; 10066609Ssam } 10076583Ssam sendicmp: 1008*37319Skarels icmp_error(m, type, code, dest); 10096583Ssam } 1010