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*39185Ssklower * @(#)ip_input.c 7.14 (Berkeley) 09/20/89 1823184Smckusick */ 194571Swnj 2017060Sbloom #include "param.h" 2117060Sbloom #include "systm.h" 2237319Skarels #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(); 10237319Skarels 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(); 12437319Skarels IF_DEQUEUE(&ipintrq, m); 1255084Swnj splx(s); 1265218Swnj if (m == 0) 1275084Swnj return; 12837319Skarels if ((m->m_flags & M_PKTHDR) == 0) 12937319Skarels 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++; 13737319Skarels 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 } 15537319Skarels if (ip->ip_sum = in_cksum(m, hlen)) { 15637319Skarels ipstat.ips_badsum++; 15737319Skarels goto bad; 15837319Skarels } 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 */ 17737319Skarels if (m->m_pkthdr.len < ip->ip_len) { 17837319Skarels ipstat.ips_tooshort++; 17937319Skarels goto bad; 1806088Sroot } 18137319Skarels if (m->m_pkthdr.len > ip->ip_len) { 18237319Skarels if (m->m_len == m->m_pkthdr.len) { 18337319Skarels m->m_len = ip->ip_len; 18437319Skarels m->m_pkthdr.len = ip->ip_len; 18537319Skarels } else 18637319Skarels 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 */ 19637319Skarels 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 20937319Skarels 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 24637319Skarels 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; 292*39185Ssklower else 293*39185Ssklower ipstat.ips_reassembled++; 29433743Skarels m = dtom(ip); 29533743Skarels } else 29633743Skarels if (fp) 29733743Skarels ip_freef(fp); 2984640Swnj } else 29933743Skarels ip->ip_len -= hlen; 3004951Swnj 3014951Swnj /* 3024951Swnj * Switch out to protocol's input routine. 3034951Swnj */ 304*39185Ssklower ipstat.ips_delivered++; 30537319Skarels (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 3065084Swnj goto next; 3074951Swnj bad: 3084951Swnj m_freem(m); 3095084Swnj goto next; 3104640Swnj } 3114495Swnj 3124640Swnj /* 3134640Swnj * Take incoming datagram fragment and try to 3144951Swnj * reassemble it into whole datagram. If a chain for 3154640Swnj * reassembly of this datagram already exists, then it 3164640Swnj * is given as fp; otherwise have to make a chain. 3174640Swnj */ 3184640Swnj struct ip * 3194640Swnj ip_reass(ip, fp) 3204898Swnj register struct ipasfrag *ip; 3214640Swnj register struct ipq *fp; 3224640Swnj { 3234640Swnj register struct mbuf *m = dtom(ip); 3244898Swnj register struct ipasfrag *q; 3254640Swnj struct mbuf *t; 3264640Swnj int hlen = ip->ip_hl << 2; 3274640Swnj int i, next; 3284543Swnj 3294640Swnj /* 3304640Swnj * Presence of header sizes in mbufs 3314640Swnj * would confuse code below. 3324640Swnj */ 33337319Skarels m->m_data += hlen; 3344640Swnj m->m_len -= hlen; 3354495Swnj 3364640Swnj /* 3374640Swnj * If first fragment to arrive, create a reassembly queue. 3384640Swnj */ 3394640Swnj if (fp == 0) { 34031201Skarels if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 3414640Swnj goto dropfrag; 3424640Swnj fp = mtod(t, struct ipq *); 3434640Swnj insque(fp, &ipq); 3444640Swnj fp->ipq_ttl = IPFRAGTTL; 3454640Swnj fp->ipq_p = ip->ip_p; 3464640Swnj fp->ipq_id = ip->ip_id; 3474898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 3484898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 3494898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 3505161Swnj q = (struct ipasfrag *)fp; 3515161Swnj goto insert; 3524640Swnj } 3534495Swnj 3544640Swnj /* 3554640Swnj * Find a segment which begins after this one does. 3564640Swnj */ 3574898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3584640Swnj if (q->ip_off > ip->ip_off) 3594640Swnj break; 3604495Swnj 3614640Swnj /* 3624640Swnj * If there is a preceding segment, it may provide some of 3634640Swnj * our data already. If so, drop the data from the incoming 3644640Swnj * segment. If it provides all of our data, drop us. 3654640Swnj */ 3664898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 3674898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 3684640Swnj if (i > 0) { 3694640Swnj if (i >= ip->ip_len) 3704640Swnj goto dropfrag; 3714640Swnj m_adj(dtom(ip), i); 3724640Swnj ip->ip_off += i; 3734640Swnj ip->ip_len -= i; 3744640Swnj } 3754640Swnj } 3764543Swnj 3774640Swnj /* 3784640Swnj * While we overlap succeeding segments trim them or, 3794640Swnj * if they are completely covered, dequeue them. 3804640Swnj */ 3814898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 3824640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 3834640Swnj if (i < q->ip_len) { 3844640Swnj q->ip_len -= i; 3856256Sroot q->ip_off += i; 3864640Swnj m_adj(dtom(q), i); 3874640Swnj break; 3884495Swnj } 3894898Swnj q = q->ipf_next; 3904898Swnj m_freem(dtom(q->ipf_prev)); 3914898Swnj ip_deq(q->ipf_prev); 3924543Swnj } 3934495Swnj 3945161Swnj insert: 3954640Swnj /* 3964640Swnj * Stick new segment in its place; 3974640Swnj * check for complete reassembly. 3984640Swnj */ 3994898Swnj ip_enq(ip, q->ipf_prev); 4004640Swnj next = 0; 4014898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 4024640Swnj if (q->ip_off != next) 4034640Swnj return (0); 4044640Swnj next += q->ip_len; 4054640Swnj } 4064898Swnj if (q->ipf_prev->ipf_mff) 4074640Swnj return (0); 4084495Swnj 4094640Swnj /* 4104640Swnj * Reassembly is complete; concatenate fragments. 4114640Swnj */ 4124640Swnj q = fp->ipq_next; 4134640Swnj m = dtom(q); 4144640Swnj t = m->m_next; 4154640Swnj m->m_next = 0; 4164640Swnj m_cat(m, t); 4176298Swnj q = q->ipf_next; 4186298Swnj while (q != (struct ipasfrag *)fp) { 4196298Swnj t = dtom(q); 4206298Swnj q = q->ipf_next; 4216298Swnj m_cat(m, t); 4226298Swnj } 4234495Swnj 4244640Swnj /* 4254640Swnj * Create header for new ip packet by 4264640Swnj * modifying header of first packet; 4274640Swnj * dequeue and discard fragment reassembly header. 4284640Swnj * Make header visible. 4294640Swnj */ 4304640Swnj ip = fp->ipq_next; 4314640Swnj ip->ip_len = next; 4324898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 4334898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 4344640Swnj remque(fp); 4354907Swnj (void) m_free(dtom(fp)); 4364640Swnj m = dtom(ip); 43724813Skarels m->m_len += (ip->ip_hl << 2); 43837319Skarels m->m_data -= (ip->ip_hl << 2); 4394898Swnj return ((struct ip *)ip); 4404495Swnj 4414640Swnj dropfrag: 44224813Skarels ipstat.ips_fragdropped++; 4434640Swnj m_freem(m); 4444640Swnj return (0); 4454495Swnj } 4464495Swnj 4474640Swnj /* 4484640Swnj * Free a fragment reassembly header and all 4494640Swnj * associated datagrams. 4504640Swnj */ 4514640Swnj ip_freef(fp) 4524640Swnj struct ipq *fp; 4534495Swnj { 45410735Ssam register struct ipasfrag *q, *p; 4554495Swnj 45610735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 45710735Ssam p = q->ipf_next; 45810735Ssam ip_deq(q); 4594640Swnj m_freem(dtom(q)); 46010735Ssam } 46110735Ssam remque(fp); 46210735Ssam (void) m_free(dtom(fp)); 4634495Swnj } 4644495Swnj 4654640Swnj /* 4664640Swnj * Put an ip fragment on a reassembly chain. 4674640Swnj * Like insque, but pointers in middle of structure. 4684640Swnj */ 4694640Swnj ip_enq(p, prev) 4704898Swnj register struct ipasfrag *p, *prev; 4714495Swnj { 4724951Swnj 4734898Swnj p->ipf_prev = prev; 4744898Swnj p->ipf_next = prev->ipf_next; 4754898Swnj prev->ipf_next->ipf_prev = p; 4764898Swnj prev->ipf_next = p; 4774495Swnj } 4784495Swnj 4794640Swnj /* 4804640Swnj * To ip_enq as remque is to insque. 4814640Swnj */ 4824640Swnj ip_deq(p) 4834898Swnj register struct ipasfrag *p; 4844640Swnj { 4854951Swnj 4864898Swnj p->ipf_prev->ipf_next = p->ipf_next; 4874898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 4884495Swnj } 4894495Swnj 4904640Swnj /* 4914640Swnj * IP timer processing; 4924640Swnj * if a timer expires on a reassembly 4934640Swnj * queue, discard it. 4944640Swnj */ 4954801Swnj ip_slowtimo() 4964495Swnj { 4974495Swnj register struct ipq *fp; 4984640Swnj int s = splnet(); 4994951Swnj 5005243Sroot fp = ipq.next; 5015243Sroot if (fp == 0) { 5025243Sroot splx(s); 5035243Sroot return; 5045243Sroot } 50510735Ssam while (fp != &ipq) { 50610735Ssam --fp->ipq_ttl; 50710735Ssam fp = fp->next; 50824813Skarels if (fp->prev->ipq_ttl == 0) { 50924813Skarels ipstat.ips_fragtimeout++; 51010735Ssam ip_freef(fp->prev); 51124813Skarels } 51210735Ssam } 5134640Swnj splx(s); 5144495Swnj } 5154495Swnj 5164951Swnj /* 5174951Swnj * Drain off all datagram fragments. 5184951Swnj */ 5194801Swnj ip_drain() 5204801Swnj { 5214801Swnj 52224813Skarels while (ipq.next != &ipq) { 52324813Skarels ipstat.ips_fragdropped++; 52410735Ssam ip_freef(ipq.next); 52524813Skarels } 5264801Swnj } 5274923Swnj 52830925Skarels extern struct in_ifaddr *ifptoia(); 52924813Skarels struct in_ifaddr *ip_rtaddr(); 53024813Skarels 5314640Swnj /* 5324640Swnj * Do option processing on a datagram, 5334640Swnj * possibly discarding it if bad options 5344640Swnj * are encountered. 5354640Swnj */ 53637319Skarels ip_dooptions(m) 53736814Skarels struct mbuf *m; 5384495Swnj { 53936814Skarels register struct ip *ip = mtod(m, struct ip *); 5404640Swnj register u_char *cp; 54124813Skarels register struct ip_timestamp *ipt; 54224813Skarels register struct in_ifaddr *ia; 54336814Skarels int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 5444923Swnj struct in_addr *sin; 54524813Skarels n_time ntime; 5464495Swnj 5474640Swnj cp = (u_char *)(ip + 1); 5484640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 5494640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 55024813Skarels opt = cp[IPOPT_OPTVAL]; 5514640Swnj if (opt == IPOPT_EOL) 5524640Swnj break; 5534640Swnj if (opt == IPOPT_NOP) 5544640Swnj optlen = 1; 55516392Ssam else { 55624813Skarels optlen = cp[IPOPT_OLEN]; 55724813Skarels if (optlen <= 0 || optlen > cnt) { 55824813Skarels code = &cp[IPOPT_OLEN] - (u_char *)ip; 55917551Skarels goto bad; 56024813Skarels } 56116392Ssam } 5624640Swnj switch (opt) { 5634495Swnj 5644640Swnj default: 5654640Swnj break; 5664495Swnj 5674951Swnj /* 5684951Swnj * Source routing with record. 5694951Swnj * Find interface with current destination address. 5704951Swnj * If none on this machine then drop if strictly routed, 5714951Swnj * or do nothing if loosely routed. 5724951Swnj * Record interface address and bring up next address 5734951Swnj * component. If strictly routed make sure next 5744951Swnj * address on directly accessible net. 5754951Swnj */ 5764640Swnj case IPOPT_LSRR: 5777508Sroot case IPOPT_SSRR: 57824813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 57924813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 58024813Skarels goto bad; 58124813Skarels } 58224813Skarels ipaddr.sin_addr = ip->ip_dst; 58324813Skarels ia = (struct in_ifaddr *) 58424813Skarels ifa_ifwithaddr((struct sockaddr *)&ipaddr); 58524813Skarels if (ia == 0) { 58624813Skarels if (opt == IPOPT_SSRR) { 58724813Skarels type = ICMP_UNREACH; 58824813Skarels code = ICMP_UNREACH_SRCFAIL; 5894951Swnj goto bad; 59024813Skarels } 59124813Skarels /* 59224813Skarels * Loose routing, and not at next destination 59324813Skarels * yet; nothing to do except forward. 59424813Skarels */ 5954951Swnj break; 5964640Swnj } 59724813Skarels off--; /* 0 origin */ 59824813Skarels if (off > optlen - sizeof(struct in_addr)) { 59924813Skarels /* 60024813Skarels * End of source route. Should be for us. 60124813Skarels */ 60224813Skarels save_rte(cp, ip->ip_src); 6034951Swnj break; 60424813Skarels } 60524813Skarels /* 60624813Skarels * locate outgoing interface 60724813Skarels */ 60826384Skarels bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 60924813Skarels sizeof(ipaddr.sin_addr)); 61024813Skarels if ((opt == IPOPT_SSRR && 61124813Skarels in_iaonnetof(in_netof(ipaddr.sin_addr)) == 0) || 61224813Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 61324813Skarels type = ICMP_UNREACH; 61424813Skarels code = ICMP_UNREACH_SRCFAIL; 6154951Swnj goto bad; 61624813Skarels } 61724813Skarels ip->ip_dst = ipaddr.sin_addr; 61826384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 61926384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 62024813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 62136814Skarels forward = 1; 6224640Swnj break; 6234495Swnj 62424813Skarels case IPOPT_RR: 62524813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 62624813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 62724813Skarels goto bad; 62824813Skarels } 62924813Skarels /* 63024813Skarels * If no space remains, ignore. 63124813Skarels */ 63224813Skarels off--; /* 0 origin */ 63324813Skarels if (off > optlen - sizeof(struct in_addr)) 63424813Skarels break; 63531393Skarels bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 63624813Skarels sizeof(ipaddr.sin_addr)); 63724813Skarels /* 63837319Skarels * locate outgoing interface; if we're the destination, 63937319Skarels * use the incoming interface (should be same). 64024813Skarels */ 64137319Skarels if ((ia = 64237319Skarels (struct in_ifaddr *)ifa_ifwithaddr( 64337319Skarels (struct sockaddr *)&ipaddr)) == 0 && 64437319Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 64524813Skarels type = ICMP_UNREACH; 64632113Skarels code = ICMP_UNREACH_HOST; 64724813Skarels goto bad; 64824813Skarels } 64926384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 65026384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 65124813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 65224813Skarels break; 65324813Skarels 6544640Swnj case IPOPT_TS: 6556583Ssam code = cp - (u_char *)ip; 6564801Swnj ipt = (struct ip_timestamp *)cp; 6574801Swnj if (ipt->ipt_len < 5) 6584640Swnj goto bad; 6594801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 6604801Swnj if (++ipt->ipt_oflw == 0) 6614640Swnj goto bad; 6624495Swnj break; 6634640Swnj } 66430925Skarels sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 6654801Swnj switch (ipt->ipt_flg) { 6664495Swnj 6674640Swnj case IPOPT_TS_TSONLY: 6684640Swnj break; 6694640Swnj 6704640Swnj case IPOPT_TS_TSANDADDR: 67124813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 67224813Skarels sizeof(struct in_addr) > ipt->ipt_len) 6734640Swnj goto bad; 67437319Skarels ia = ifptoia(m->m_pkthdr.rcvif); 67530925Skarels bcopy((caddr_t)&IA_SIN(ia)->sin_addr, 67624813Skarels (caddr_t)sin, sizeof(struct in_addr)); 67730925Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6784640Swnj break; 6794640Swnj 6804640Swnj case IPOPT_TS_PRESPEC: 68130925Skarels if (ipt->ipt_ptr + sizeof(n_time) + 68230925Skarels sizeof(struct in_addr) > ipt->ipt_len) 68330925Skarels goto bad; 68424813Skarels bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 68524813Skarels sizeof(struct in_addr)); 68618376Skarels if (ifa_ifwithaddr((struct sockaddr *)&ipaddr) == 0) 6874951Swnj continue; 68824813Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6894640Swnj break; 6904640Swnj 6914495Swnj default: 6924640Swnj goto bad; 6934495Swnj } 69424813Skarels ntime = iptime(); 69530925Skarels bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 69630925Skarels sizeof(n_time)); 69724813Skarels ipt->ipt_ptr += sizeof(n_time); 6984640Swnj } 6994495Swnj } 70036814Skarels if (forward) { 70137319Skarels ip_forward(m); 70236814Skarels return (1); 70336814Skarels } else 70436814Skarels return (0); 7054640Swnj bad: 70637319Skarels icmp_error(m, type, code); 7076583Ssam return (1); 7084495Swnj } 7094495Swnj 7104640Swnj /* 71124813Skarels * Given address of next destination (final or next hop), 71224813Skarels * return internet address info of interface to be used to get there. 71324813Skarels */ 71424813Skarels struct in_ifaddr * 71524813Skarels ip_rtaddr(dst) 71624813Skarels struct in_addr dst; 71724813Skarels { 71824813Skarels register struct sockaddr_in *sin; 71924813Skarels register struct in_ifaddr *ia; 72024813Skarels 72124813Skarels sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 72224813Skarels 72324813Skarels if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 72424813Skarels if (ipforward_rt.ro_rt) { 72524813Skarels RTFREE(ipforward_rt.ro_rt); 72624813Skarels ipforward_rt.ro_rt = 0; 72724813Skarels } 72824813Skarels sin->sin_family = AF_INET; 72937319Skarels sin->sin_len = sizeof(*sin); 73024813Skarels sin->sin_addr = dst; 73124813Skarels 73224813Skarels rtalloc(&ipforward_rt); 73324813Skarels } 73424813Skarels if (ipforward_rt.ro_rt == 0) 73524813Skarels return ((struct in_ifaddr *)0); 73624813Skarels /* 73724813Skarels * Find address associated with outgoing interface. 73824813Skarels */ 73924813Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) 74024813Skarels if (ia->ia_ifp == ipforward_rt.ro_rt->rt_ifp) 74124813Skarels break; 74224813Skarels return (ia); 74324813Skarels } 74424813Skarels 74524813Skarels /* 74624813Skarels * Save incoming source route for use in replies, 74724813Skarels * to be picked up later by ip_srcroute if the receiver is interested. 74824813Skarels */ 74924813Skarels save_rte(option, dst) 75026384Skarels u_char *option; 75124813Skarels struct in_addr dst; 75224813Skarels { 75326384Skarels unsigned olen; 75424813Skarels 75524813Skarels olen = option[IPOPT_OLEN]; 75636814Skarels if (ipprintfs) 75736814Skarels printf("save_rte: olen %d\n", olen); 75836814Skarels if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 75924813Skarels return; 76026384Skarels bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 76124813Skarels ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 76236814Skarels ip_srcrt.dst = dst; 76324813Skarels } 76424813Skarels 76524813Skarels /* 76624813Skarels * Retrieve incoming source route for use in replies, 76724813Skarels * in the same form used by setsockopt. 76824813Skarels * The first hop is placed before the options, will be removed later. 76924813Skarels */ 77024813Skarels struct mbuf * 77124813Skarels ip_srcroute() 77224813Skarels { 77324813Skarels register struct in_addr *p, *q; 77424813Skarels register struct mbuf *m; 77524813Skarels 77624813Skarels if (ip_nhops == 0) 77724813Skarels return ((struct mbuf *)0); 77831201Skarels m = m_get(M_DONTWAIT, MT_SOOPTS); 77931201Skarels if (m == 0) 78031201Skarels return ((struct mbuf *)0); 78124813Skarels 78236814Skarels #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 78336814Skarels 78436814Skarels /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 78536814Skarels m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 78636814Skarels OPTSIZ; 78736814Skarels if (ipprintfs) 78836814Skarels printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 78936814Skarels 79024813Skarels /* 79124813Skarels * First save first hop for return route 79224813Skarels */ 79324813Skarels p = &ip_srcrt.route[ip_nhops - 1]; 79424813Skarels *(mtod(m, struct in_addr *)) = *p--; 79536814Skarels if (ipprintfs) 79636814Skarels printf(" hops %X", ntohl(*mtod(m, struct in_addr *))); 79724813Skarels 79824813Skarels /* 79924813Skarels * Copy option fields and padding (nop) to mbuf. 80024813Skarels */ 80124813Skarels ip_srcrt.nop = IPOPT_NOP; 80236814Skarels ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 80336814Skarels bcopy((caddr_t)&ip_srcrt.nop, 80436814Skarels mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 80524813Skarels q = (struct in_addr *)(mtod(m, caddr_t) + 80636814Skarels sizeof(struct in_addr) + OPTSIZ); 80736814Skarels #undef OPTSIZ 80824813Skarels /* 80924813Skarels * Record return path as an IP source route, 81024813Skarels * reversing the path (pointers are now aligned). 81124813Skarels */ 81236814Skarels while (p >= ip_srcrt.route) { 81336814Skarels if (ipprintfs) 81436814Skarels printf(" %X", ntohl(*q)); 81524813Skarels *q++ = *p--; 81636814Skarels } 81736814Skarels /* 81836814Skarels * Last hop goes to final destination. 81936814Skarels */ 82036814Skarels *q = ip_srcrt.dst; 82136814Skarels if (ipprintfs) 82236814Skarels printf(" %X\n", ntohl(*q)); 82324813Skarels return (m); 82424813Skarels } 82524813Skarels 82624813Skarels /* 8274951Swnj * Strip out IP options, at higher 8284951Swnj * level protocol in the kernel. 8294951Swnj * Second argument is buffer to which options 8304951Swnj * will be moved, and return value is their length. 83136814Skarels * XXX should be deleted; last arg currently ignored. 8324640Swnj */ 83337319Skarels ip_stripoptions(m, mopt) 83437319Skarels register struct mbuf *m; 8355217Swnj struct mbuf *mopt; 8364495Swnj { 8374640Swnj register int i; 83837319Skarels struct ip *ip = mtod(m, struct ip *); 83924813Skarels register caddr_t opts; 8404640Swnj int olen; 8414640Swnj 8424640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 84324813Skarels opts = (caddr_t)(ip + 1); 8444640Swnj i = m->m_len - (sizeof (struct ip) + olen); 84524813Skarels bcopy(opts + olen, opts, (unsigned)i); 8465243Sroot m->m_len -= olen; 84737319Skarels if (m->m_flags & M_PKTHDR) 84837319Skarels m->m_pkthdr.len -= olen; 84924813Skarels ip->ip_hl = sizeof(struct ip) >> 2; 8504495Swnj } 8516583Ssam 85214670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 85324813Skarels 0, 0, 0, 0, 85414670Ssam 0, 0, EHOSTDOWN, EHOSTUNREACH, 85514670Ssam ENETUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 85624813Skarels EMSGSIZE, EHOSTUNREACH, 0, 0, 85724813Skarels 0, 0, 0, 0, 85824813Skarels ENOPROTOOPT 8596583Ssam }; 8606583Ssam 8616583Ssam /* 8626583Ssam * Forward a packet. If some error occurs return the sender 86318376Skarels * an icmp packet. Note we can't always generate a meaningful 86424813Skarels * icmp message because icmp doesn't have a large enough repertoire 8656583Ssam * of codes and types. 86626308Skarels * 86726308Skarels * If not forwarding (possibly because we have only a single external 86826308Skarels * network), just drop the packet. This could be confusing if ipforwarding 86926308Skarels * was zero but some routing protocol was advancing us as a gateway 87026308Skarels * to somewhere. However, we must let the routing protocol deal with that. 8716583Ssam */ 87237319Skarels ip_forward(m) 87336814Skarels struct mbuf *m; 8746583Ssam { 87536814Skarels register struct ip *ip = mtod(m, struct ip *); 87624813Skarels register int error, type = 0, code; 87724813Skarels register struct sockaddr_in *sin; 87818376Skarels struct mbuf *mcopy; 87924813Skarels struct in_addr dest; 8806583Ssam 88124813Skarels dest.s_addr = 0; 8826583Ssam if (ipprintfs) 8836583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 8846583Ssam ip->ip_dst, ip->ip_ttl); 88537319Skarels if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 88626308Skarels ipstat.ips_cantforward++; 88737319Skarels m_freem(m); 88826308Skarels return; 8896583Ssam } 89036814Skarels ip->ip_id = htons(ip->ip_id); 89131393Skarels if (ip->ip_ttl <= IPTTLDEC) { 8926583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 8936583Ssam goto sendicmp; 8946583Ssam } 8956583Ssam ip->ip_ttl -= IPTTLDEC; 8966609Ssam 8976609Ssam /* 8986609Ssam * Save at most 64 bytes of the packet in case 8996609Ssam * we need to generate an ICMP message to the src. 9006609Ssam */ 90137319Skarels mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 9026583Ssam 90324813Skarels sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 90424813Skarels if (ipforward_rt.ro_rt == 0 || 90524813Skarels ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 90624813Skarels if (ipforward_rt.ro_rt) { 90724813Skarels RTFREE(ipforward_rt.ro_rt); 90824813Skarels ipforward_rt.ro_rt = 0; 90924813Skarels } 91024813Skarels sin->sin_family = AF_INET; 91137319Skarels sin->sin_len = sizeof(*sin); 91224813Skarels sin->sin_addr = ip->ip_dst; 91324813Skarels 91424813Skarels rtalloc(&ipforward_rt); 91524813Skarels } 91624813Skarels /* 91724813Skarels * If forwarding packet using same interface that it came in on, 91824813Skarels * perhaps should send a redirect to sender to shortcut a hop. 91924813Skarels * Only send redirect if source is sending directly to us, 92024813Skarels * and if packet was not source routed (or has any options). 92130447Skarels * Also, don't send redirect if forwarding using a default route 92230447Skarels * or a route modfied by a redirect. 92324813Skarels */ 92430447Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 92537319Skarels if (ipforward_rt.ro_rt && 92637319Skarels ipforward_rt.ro_rt->rt_ifp == m->m_pkthdr.rcvif && 92736814Skarels (ipforward_rt.ro_rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 92837319Skarels satosin(rt_key(ipforward_rt.ro_rt))->sin_addr.s_addr != 0 && 92924813Skarels ipsendredirects && ip->ip_hl == (sizeof(struct ip) >> 2)) { 93024813Skarels struct in_ifaddr *ia; 93124813Skarels u_long src = ntohl(ip->ip_src.s_addr); 93224813Skarels u_long dst = ntohl(ip->ip_dst.s_addr); 93324813Skarels 93437319Skarels if ((ia = ifptoia(m->m_pkthdr.rcvif)) && 93524813Skarels (src & ia->ia_subnetmask) == ia->ia_subnet) { 93624813Skarels if (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) 93737319Skarels dest = satosin(ipforward_rt.ro_rt->rt_gateway)->sin_addr; 93824813Skarels else 93924813Skarels dest = ip->ip_dst; 94024813Skarels /* 94124813Skarels * If the destination is reached by a route to host, 94227145Skarels * is on a subnet of a local net, or is directly 94327145Skarels * on the attached net (!), use host redirect. 94424813Skarels * (We may be the correct first hop for other subnets.) 94524813Skarels */ 94624813Skarels type = ICMP_REDIRECT; 94724813Skarels code = ICMP_REDIRECT_NET; 94824813Skarels if ((ipforward_rt.ro_rt->rt_flags & RTF_HOST) || 94924813Skarels (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) == 0) 95024813Skarels code = ICMP_REDIRECT_HOST; 95124813Skarels else for (ia = in_ifaddr; ia = ia->ia_next; ) 95224813Skarels if ((dst & ia->ia_netmask) == ia->ia_net) { 95327145Skarels if (ia->ia_subnetmask != ia->ia_netmask) 95427145Skarels code = ICMP_REDIRECT_HOST; 95524813Skarels break; 95624813Skarels } 95724813Skarels if (ipprintfs) 95824813Skarels printf("redirect (%d) to %x\n", code, dest); 95924813Skarels } 96024813Skarels } 96124813Skarels 96237319Skarels error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING); 96324813Skarels if (error) 96424813Skarels ipstat.ips_cantforward++; 96524813Skarels else if (type) 96624813Skarels ipstat.ips_redirectsent++; 96724813Skarels else { 9686609Ssam if (mcopy) 9696609Ssam m_freem(mcopy); 97021117Skarels ipstat.ips_forward++; 9716583Ssam return; 9726609Ssam } 97311540Ssam if (mcopy == NULL) 97411540Ssam return; 9756609Ssam ip = mtod(mcopy, struct ip *); 97624813Skarels type = ICMP_UNREACH; 9776609Ssam switch (error) { 9786609Ssam 97924813Skarels case 0: /* forwarded, but need redirect */ 98024813Skarels type = ICMP_REDIRECT; 98124813Skarels /* code set above */ 98224813Skarels break; 98324813Skarels 9846609Ssam case ENETUNREACH: 9856609Ssam case ENETDOWN: 98632572Skarels if (in_localaddr(ip->ip_dst)) 98732572Skarels code = ICMP_UNREACH_HOST; 98832572Skarels else 98932572Skarels code = ICMP_UNREACH_NET; 9906609Ssam break; 9916609Ssam 9926609Ssam case EMSGSIZE: 9936583Ssam code = ICMP_UNREACH_NEEDFRAG; 994*39185Ssklower ipstat.ips_cantfrag++; 9956609Ssam break; 9966609Ssam 9976609Ssam case EPERM: 9986609Ssam code = ICMP_UNREACH_PORT; 9996609Ssam break; 10006609Ssam 10016609Ssam case ENOBUFS: 10026609Ssam type = ICMP_SOURCEQUENCH; 100337319Skarels code = 0; 10046609Ssam break; 10056609Ssam 10066609Ssam case EHOSTDOWN: 10076609Ssam case EHOSTUNREACH: 10086609Ssam code = ICMP_UNREACH_HOST; 10096609Ssam break; 10106609Ssam } 10116583Ssam sendicmp: 101238795Skarels icmp_error(mcopy, type, code, dest); 10136583Ssam } 1014