123184Smckusick /* 257968Sandrew * Copyright (c) 1982, 1986, 1988, 1993 Regents of the University of California. 332787Sbostic * All rights reserved. 423184Smckusick * 544480Sbostic * %sccs.include.redist.c% 632787Sbostic * 7*59136Smckusick * @(#)ip_input.c 7.27 (Berkeley) 04/18/93 823184Smckusick */ 94571Swnj 1056531Sbostic #include <sys/param.h> 1156531Sbostic #include <sys/systm.h> 1256531Sbostic #include <sys/malloc.h> 1356531Sbostic #include <sys/mbuf.h> 1456531Sbostic #include <sys/domain.h> 1556531Sbostic #include <sys/protosw.h> 1656531Sbostic #include <sys/socket.h> 1756531Sbostic #include <sys/errno.h> 1856531Sbostic #include <sys/time.h> 1956531Sbostic #include <sys/kernel.h> 208695Sroot 2156531Sbostic #include <net/if.h> 2256531Sbostic #include <net/route.h> 2310892Ssam 2456531Sbostic #include <netinet/in.h> 2556531Sbostic #include <netinet/in_systm.h> 2656531Sbostic #include <netinet/ip.h> 2756531Sbostic #include <netinet/in_pcb.h> 2856531Sbostic #include <netinet/in_var.h> 2956531Sbostic #include <netinet/ip_var.h> 3056531Sbostic #include <netinet/ip_icmp.h> 314495Swnj 3236814Skarels #ifndef IPFORWARDING 3336814Skarels #ifdef GATEWAY 3440689Skarels #define IPFORWARDING 1 /* forward IP packets not for us */ 3536814Skarels #else /* GATEWAY */ 3640689Skarels #define IPFORWARDING 0 /* don't forward IP packets not for us */ 3736814Skarels #endif /* GATEWAY */ 3836814Skarels #endif /* IPFORWARDING */ 3936814Skarels #ifndef IPSENDREDIRECTS 4036814Skarels #define IPSENDREDIRECTS 1 4136814Skarels #endif 4236814Skarels int ipforwarding = IPFORWARDING; 4336814Skarels int ipsendredirects = IPSENDREDIRECTS; 44*59136Smckusick int ip_defttl = IPDEFTTL; 4549042Ssklower #ifdef DIAGNOSTIC 4640689Skarels int ipprintfs = 0; 4740689Skarels #endif 4836814Skarels 4949042Ssklower extern struct domain inetdomain; 5049042Ssklower extern struct protosw inetsw[]; 514898Swnj u_char ip_protox[IPPROTO_MAX]; 526210Swnj int ipqmaxlen = IFQ_MAXLEN; 5318376Skarels struct in_ifaddr *in_ifaddr; /* first inet address */ 5458998Ssklower struct ifqueue ipintrq; 554898Swnj 564801Swnj /* 5724813Skarels * We need to save the IP options in case a protocol wants to respond 5824813Skarels * to an incoming packet over the same route if the packet got here 5924813Skarels * using IP source routing. This allows connection establishment and 6024813Skarels * maintenance when the remote end is on a network that is not known 6124813Skarels * to us. 6224813Skarels */ 6324813Skarels int ip_nhops = 0; 6424813Skarels static struct ip_srcrt { 6536814Skarels struct in_addr dst; /* final destination */ 6624813Skarels char nop; /* one NOP to align */ 6724813Skarels char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 6836814Skarels struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 6924813Skarels } ip_srcrt; 7024813Skarels 7140689Skarels #ifdef GATEWAY 7240689Skarels extern int if_index; 7340689Skarels u_long *ip_ifmatrix; 7440689Skarels #endif 7540689Skarels 7624813Skarels /* 775172Swnj * IP initialization: fill in IP protocol switch table. 785161Swnj * All protocols not implemented in kernel go to raw IP protocol handler. 794801Swnj */ 804801Swnj ip_init() 814801Swnj { 824898Swnj register struct protosw *pr; 834898Swnj register int i; 844495Swnj 8524813Skarels pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 864898Swnj if (pr == 0) 874898Swnj panic("ip_init"); 884898Swnj for (i = 0; i < IPPROTO_MAX; i++) 899030Sroot ip_protox[i] = pr - inetsw; 909030Sroot for (pr = inetdomain.dom_protosw; 9117551Skarels pr < inetdomain.dom_protoswNPROTOSW; pr++) 9216990Skarels if (pr->pr_domain->dom_family == PF_INET && 934898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 949030Sroot ip_protox[pr->pr_protocol] = pr - inetsw; 954801Swnj ipq.next = ipq.prev = &ipq; 968172Sroot ip_id = time.tv_sec & 0xffff; 976210Swnj ipintrq.ifq_maxlen = ipqmaxlen; 9840689Skarels #ifdef GATEWAY 9940689Skarels i = (if_index + 1) * (if_index + 1) * sizeof (u_long); 10050425Smckusick ip_ifmatrix = (u_long *) malloc(i, M_RTABLE, M_WAITOK); 10150425Smckusick bzero((char *)ip_ifmatrix, i); 10240689Skarels #endif 1034801Swnj } 1044801Swnj 1054640Swnj struct ip *ip_reass(); 10637319Skarels struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 10724813Skarels struct route ipforward_rt; 1084640Swnj 1094640Swnj /* 1104640Swnj * Ip input routine. Checksum and byte swap header. If fragmented 11140689Skarels * try to reassemble. Process options. Pass to next level. 1124640Swnj */ 1135084Swnj ipintr() 1144495Swnj { 1154923Swnj register struct ip *ip; 1165084Swnj register struct mbuf *m; 1174495Swnj register struct ipq *fp; 11818376Skarels register struct in_ifaddr *ia; 1195084Swnj int hlen, s; 1204495Swnj 1215084Swnj next: 1224640Swnj /* 1235084Swnj * Get next datagram off input queue and get IP header 1245084Swnj * in first mbuf. 1254640Swnj */ 1265084Swnj s = splimp(); 12737319Skarels IF_DEQUEUE(&ipintrq, m); 1285084Swnj splx(s); 1295218Swnj if (m == 0) 1305084Swnj return; 13144967Skarels #ifdef DIAGNOSTIC 13244967Skarels if ((m->m_flags & M_PKTHDR) == 0) 13344967Skarels panic("ipintr no HDR"); 13444967Skarels #endif 13526001Skarels /* 13626001Skarels * If no IP addresses have been set yet but the interfaces 13726001Skarels * are receiving, can't do anything with incoming packets yet. 13826001Skarels */ 13926001Skarels if (in_ifaddr == NULL) 14026001Skarels goto bad; 14125920Skarels ipstat.ips_total++; 14240689Skarels if (m->m_len < sizeof (struct ip) && 14311232Ssam (m = m_pullup(m, sizeof (struct ip))) == 0) { 14411232Ssam ipstat.ips_toosmall++; 14511232Ssam goto next; 14611232Ssam } 1474640Swnj ip = mtod(m, struct ip *); 14857968Sandrew if (ip->ip_v != IPVERSION) { 14957968Sandrew ipstat.ips_badvers++; 15057968Sandrew goto bad; 15157968Sandrew } 15218376Skarels hlen = ip->ip_hl << 2; 15324813Skarels if (hlen < sizeof(struct ip)) { /* minimum header length */ 15418376Skarels ipstat.ips_badhlen++; 15521117Skarels goto bad; 15618376Skarels } 15718376Skarels if (hlen > m->m_len) { 15811232Ssam if ((m = m_pullup(m, hlen)) == 0) { 15911232Ssam ipstat.ips_badhlen++; 16011232Ssam goto next; 16111232Ssam } 1625161Swnj ip = mtod(m, struct ip *); 1635161Swnj } 16437319Skarels if (ip->ip_sum = in_cksum(m, hlen)) { 16537319Skarels ipstat.ips_badsum++; 16637319Skarels goto bad; 16737319Skarels } 1684951Swnj 1694951Swnj /* 1704951Swnj * Convert fields to host representation. 1714951Swnj */ 17240689Skarels NTOHS(ip->ip_len); 17311232Ssam if (ip->ip_len < hlen) { 17411232Ssam ipstat.ips_badlen++; 17511232Ssam goto bad; 17611232Ssam } 17740689Skarels NTOHS(ip->ip_id); 17840689Skarels NTOHS(ip->ip_off); 1794495Swnj 1804543Swnj /* 1814640Swnj * Check that the amount of data in the buffers 1824640Swnj * is as at least much as the IP header would have us expect. 1834640Swnj * Trim mbufs if longer than we expect. 1844640Swnj * Drop packet if shorter than we expect. 1854543Swnj */ 18637319Skarels if (m->m_pkthdr.len < ip->ip_len) { 18737319Skarels ipstat.ips_tooshort++; 18837319Skarels goto bad; 1896088Sroot } 19037319Skarels if (m->m_pkthdr.len > ip->ip_len) { 19137319Skarels if (m->m_len == m->m_pkthdr.len) { 19237319Skarels m->m_len = ip->ip_len; 19337319Skarels m->m_pkthdr.len = ip->ip_len; 19437319Skarels } else 19537319Skarels m_adj(m, ip->ip_len - m->m_pkthdr.len); 1964495Swnj } 1974495Swnj 1984640Swnj /* 1994640Swnj * Process options and, if not destined for us, 2006583Ssam * ship it on. ip_dooptions returns 1 when an 2016583Ssam * error was detected (causing an icmp message 20221117Skarels * to be sent and the original packet to be freed). 2034640Swnj */ 20424813Skarels ip_nhops = 0; /* for source routed packets */ 20537319Skarels if (hlen > sizeof (struct ip) && ip_dooptions(m)) 2066583Ssam goto next; 2076210Swnj 2086338Ssam /* 20918376Skarels * Check our list of addresses, to see if the packet is for us. 2106338Ssam */ 21118376Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) { 21218376Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 2136338Ssam 21418376Skarels if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 21524813Skarels goto ours; 21625195Skarels if ( 21725195Skarels #ifdef DIRECTED_BROADCAST 21837319Skarels ia->ia_ifp == m->m_pkthdr.rcvif && 21925195Skarels #endif 22025195Skarels (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 22126247Skarels u_long t; 22225195Skarels 22325195Skarels if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 22425195Skarels ip->ip_dst.s_addr) 22525195Skarels goto ours; 22625195Skarels if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 22725195Skarels goto ours; 22825195Skarels /* 22925195Skarels * Look for all-0's host part (old broadcast addr), 23025195Skarels * either for subnet or net. 23125195Skarels */ 23226247Skarels t = ntohl(ip->ip_dst.s_addr); 23326247Skarels if (t == ia->ia_subnet) 23425195Skarels goto ours; 23526247Skarels if (t == ia->ia_net) 23625195Skarels goto ours; 23725195Skarels } 2386338Ssam } 23954716Ssklower if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 24054716Ssklower struct in_multi *inm; 24154716Ssklower #ifdef MROUTING 24254716Ssklower extern struct socket *ip_mrouter; 24354716Ssklower 24454716Ssklower if (ip_mrouter) { 24554716Ssklower /* 24654716Ssklower * If we are acting as a multicast router, all 24754716Ssklower * incoming multicast packets are passed to the 24854716Ssklower * kernel-level multicast forwarding function. 24954716Ssklower * The packet is returned (relatively) intact; if 25054716Ssklower * ip_mforward() returns a non-zero value, the packet 25154716Ssklower * must be discarded, else it may be accepted below. 25254716Ssklower * 25354716Ssklower * (The IP ident field is put in the same byte order 25454716Ssklower * as expected when ip_mforward() is called from 25554716Ssklower * ip_output().) 25654716Ssklower */ 25754716Ssklower ip->ip_id = htons(ip->ip_id); 25854716Ssklower if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) { 25957433Sandrew ipstat.ips_cantforward++; 26054716Ssklower m_freem(m); 26154716Ssklower goto next; 26254716Ssklower } 26354716Ssklower ip->ip_id = ntohs(ip->ip_id); 26454716Ssklower 26554716Ssklower /* 26654716Ssklower * The process-level routing demon needs to receive 26754716Ssklower * all multicast IGMP packets, whether or not this 26854716Ssklower * host belongs to their destination groups. 26954716Ssklower */ 27054716Ssklower if (ip->ip_p == IPPROTO_IGMP) 27154716Ssklower goto ours; 27257433Sandrew ipstat.ips_forward++; 27354716Ssklower } 27454716Ssklower #endif 27554716Ssklower /* 27654716Ssklower * See if we belong to the destination multicast group on the 27754716Ssklower * arrival interface. 27854716Ssklower */ 27954716Ssklower IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 28054716Ssklower if (inm == NULL) { 28157433Sandrew ipstat.ips_cantforward++; 28254716Ssklower m_freem(m); 28354716Ssklower goto next; 28454716Ssklower } 28554716Ssklower goto ours; 28654716Ssklower } 28724813Skarels if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 28824813Skarels goto ours; 28924813Skarels if (ip->ip_dst.s_addr == INADDR_ANY) 29024813Skarels goto ours; 2914495Swnj 2924640Swnj /* 29324813Skarels * Not for us; forward if possible and desirable. 29424813Skarels */ 29540689Skarels if (ipforwarding == 0) { 29636814Skarels ipstat.ips_cantforward++; 29736814Skarels m_freem(m); 29836814Skarels } else 29940689Skarels ip_forward(m, 0); 30024813Skarels goto next; 30124813Skarels 30224813Skarels ours: 30324813Skarels /* 30433743Skarels * If offset or IP_MF are set, must reassemble. 30533743Skarels * Otherwise, nothing need be done. 30633743Skarels * (We could look in the reassembly queue to see 30733743Skarels * if the packet was previously fragmented, 30833743Skarels * but it's not worth the time; just let them time out.) 3094640Swnj */ 31033743Skarels if (ip->ip_off &~ IP_DF) { 31140689Skarels if (m->m_flags & M_EXT) { /* XXX */ 31240689Skarels if ((m = m_pullup(m, sizeof (struct ip))) == 0) { 31340689Skarels ipstat.ips_toosmall++; 31440689Skarels goto next; 31540689Skarels } 31640689Skarels ip = mtod(m, struct ip *); 31740689Skarels } 31833743Skarels /* 31933743Skarels * Look for queue of fragments 32033743Skarels * of this datagram. 32133743Skarels */ 32233743Skarels for (fp = ipq.next; fp != &ipq; fp = fp->next) 32333743Skarels if (ip->ip_id == fp->ipq_id && 32433743Skarels ip->ip_src.s_addr == fp->ipq_src.s_addr && 32533743Skarels ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 32633743Skarels ip->ip_p == fp->ipq_p) 32733743Skarels goto found; 32833743Skarels fp = 0; 3294640Swnj found: 3304495Swnj 33133743Skarels /* 33233743Skarels * Adjust ip_len to not reflect header, 33333743Skarels * set ip_mff if more fragments are expected, 33433743Skarels * convert offset of this to bytes. 33533743Skarels */ 33633743Skarels ip->ip_len -= hlen; 33757968Sandrew ((struct ipasfrag *)ip)->ipf_mff &= ~1; 33833743Skarels if (ip->ip_off & IP_MF) 33957968Sandrew ((struct ipasfrag *)ip)->ipf_mff |= 1; 34033743Skarels ip->ip_off <<= 3; 3414495Swnj 34233743Skarels /* 34333743Skarels * If datagram marked as having more fragments 34433743Skarels * or if this is not the first fragment, 34533743Skarels * attempt reassembly; if it succeeds, proceed. 34633743Skarels */ 34757968Sandrew if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) { 34833743Skarels ipstat.ips_fragments++; 34933743Skarels ip = ip_reass((struct ipasfrag *)ip, fp); 35033743Skarels if (ip == 0) 35133743Skarels goto next; 35257433Sandrew ipstat.ips_reassembled++; 35333743Skarels m = dtom(ip); 35433743Skarels } else 35533743Skarels if (fp) 35633743Skarels ip_freef(fp); 3574640Swnj } else 35833743Skarels ip->ip_len -= hlen; 3594951Swnj 3604951Swnj /* 3614951Swnj * Switch out to protocol's input routine. 3624951Swnj */ 36339185Ssklower ipstat.ips_delivered++; 36437319Skarels (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 3655084Swnj goto next; 3664951Swnj bad: 3674951Swnj m_freem(m); 3685084Swnj goto next; 3694640Swnj } 3704495Swnj 3714640Swnj /* 3724640Swnj * Take incoming datagram fragment and try to 3734951Swnj * reassemble it into whole datagram. If a chain for 3744640Swnj * reassembly of this datagram already exists, then it 3754640Swnj * is given as fp; otherwise have to make a chain. 3764640Swnj */ 3774640Swnj struct ip * 3784640Swnj ip_reass(ip, fp) 3794898Swnj register struct ipasfrag *ip; 3804640Swnj register struct ipq *fp; 3814640Swnj { 3824640Swnj register struct mbuf *m = dtom(ip); 3834898Swnj register struct ipasfrag *q; 3844640Swnj struct mbuf *t; 3854640Swnj int hlen = ip->ip_hl << 2; 3864640Swnj int i, next; 3874543Swnj 3884640Swnj /* 3894640Swnj * Presence of header sizes in mbufs 3904640Swnj * would confuse code below. 3914640Swnj */ 39237319Skarels m->m_data += hlen; 3934640Swnj m->m_len -= hlen; 3944495Swnj 3954640Swnj /* 3964640Swnj * If first fragment to arrive, create a reassembly queue. 3974640Swnj */ 3984640Swnj if (fp == 0) { 39931201Skarels if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 4004640Swnj goto dropfrag; 4014640Swnj fp = mtod(t, struct ipq *); 4024640Swnj insque(fp, &ipq); 4034640Swnj fp->ipq_ttl = IPFRAGTTL; 4044640Swnj fp->ipq_p = ip->ip_p; 4054640Swnj fp->ipq_id = ip->ip_id; 4064898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 4074898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 4084898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 4095161Swnj q = (struct ipasfrag *)fp; 4105161Swnj goto insert; 4114640Swnj } 4124495Swnj 4134640Swnj /* 4144640Swnj * Find a segment which begins after this one does. 4154640Swnj */ 4164898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 4174640Swnj if (q->ip_off > ip->ip_off) 4184640Swnj break; 4194495Swnj 4204640Swnj /* 4214640Swnj * If there is a preceding segment, it may provide some of 4224640Swnj * our data already. If so, drop the data from the incoming 4234640Swnj * segment. If it provides all of our data, drop us. 4244640Swnj */ 4254898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 4264898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 4274640Swnj if (i > 0) { 4284640Swnj if (i >= ip->ip_len) 4294640Swnj goto dropfrag; 4304640Swnj m_adj(dtom(ip), i); 4314640Swnj ip->ip_off += i; 4324640Swnj ip->ip_len -= i; 4334640Swnj } 4344640Swnj } 4354543Swnj 4364640Swnj /* 4374640Swnj * While we overlap succeeding segments trim them or, 4384640Swnj * if they are completely covered, dequeue them. 4394640Swnj */ 4404898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 4414640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 4424640Swnj if (i < q->ip_len) { 4434640Swnj q->ip_len -= i; 4446256Sroot q->ip_off += i; 4454640Swnj m_adj(dtom(q), i); 4464640Swnj break; 4474495Swnj } 4484898Swnj q = q->ipf_next; 4494898Swnj m_freem(dtom(q->ipf_prev)); 4504898Swnj ip_deq(q->ipf_prev); 4514543Swnj } 4524495Swnj 4535161Swnj insert: 4544640Swnj /* 4554640Swnj * Stick new segment in its place; 4564640Swnj * check for complete reassembly. 4574640Swnj */ 4584898Swnj ip_enq(ip, q->ipf_prev); 4594640Swnj next = 0; 4604898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 4614640Swnj if (q->ip_off != next) 4624640Swnj return (0); 4634640Swnj next += q->ip_len; 4644640Swnj } 46557968Sandrew if (q->ipf_prev->ipf_mff & 1) 4664640Swnj return (0); 4674495Swnj 4684640Swnj /* 4694640Swnj * Reassembly is complete; concatenate fragments. 4704640Swnj */ 4714640Swnj q = fp->ipq_next; 4724640Swnj m = dtom(q); 4734640Swnj t = m->m_next; 4744640Swnj m->m_next = 0; 4754640Swnj m_cat(m, t); 4766298Swnj q = q->ipf_next; 4776298Swnj while (q != (struct ipasfrag *)fp) { 4786298Swnj t = dtom(q); 4796298Swnj q = q->ipf_next; 4806298Swnj m_cat(m, t); 4816298Swnj } 4824495Swnj 4834640Swnj /* 4844640Swnj * Create header for new ip packet by 4854640Swnj * modifying header of first packet; 4864640Swnj * dequeue and discard fragment reassembly header. 4874640Swnj * Make header visible. 4884640Swnj */ 4894640Swnj ip = fp->ipq_next; 4904640Swnj ip->ip_len = next; 49157968Sandrew ip->ipf_mff &= ~1; 4924898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 4934898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 4944640Swnj remque(fp); 4954907Swnj (void) m_free(dtom(fp)); 4964640Swnj m = dtom(ip); 49724813Skarels m->m_len += (ip->ip_hl << 2); 49837319Skarels m->m_data -= (ip->ip_hl << 2); 49949042Ssklower /* some debugging cruft by sklower, below, will go away soon */ 50049042Ssklower if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 50149042Ssklower register int plen = 0; 50249042Ssklower for (t = m; m; m = m->m_next) 50349042Ssklower plen += m->m_len; 50449042Ssklower t->m_pkthdr.len = plen; 50549042Ssklower } 5064898Swnj return ((struct ip *)ip); 5074495Swnj 5084640Swnj dropfrag: 50924813Skarels ipstat.ips_fragdropped++; 5104640Swnj m_freem(m); 5114640Swnj return (0); 5124495Swnj } 5134495Swnj 5144640Swnj /* 5154640Swnj * Free a fragment reassembly header and all 5164640Swnj * associated datagrams. 5174640Swnj */ 5184640Swnj ip_freef(fp) 5194640Swnj struct ipq *fp; 5204495Swnj { 52110735Ssam register struct ipasfrag *q, *p; 5224495Swnj 52310735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 52410735Ssam p = q->ipf_next; 52510735Ssam ip_deq(q); 5264640Swnj m_freem(dtom(q)); 52710735Ssam } 52810735Ssam remque(fp); 52910735Ssam (void) m_free(dtom(fp)); 5304495Swnj } 5314495Swnj 5324640Swnj /* 5334640Swnj * Put an ip fragment on a reassembly chain. 5344640Swnj * Like insque, but pointers in middle of structure. 5354640Swnj */ 5364640Swnj ip_enq(p, prev) 5374898Swnj register struct ipasfrag *p, *prev; 5384495Swnj { 5394951Swnj 5404898Swnj p->ipf_prev = prev; 5414898Swnj p->ipf_next = prev->ipf_next; 5424898Swnj prev->ipf_next->ipf_prev = p; 5434898Swnj prev->ipf_next = p; 5444495Swnj } 5454495Swnj 5464640Swnj /* 5474640Swnj * To ip_enq as remque is to insque. 5484640Swnj */ 5494640Swnj ip_deq(p) 5504898Swnj register struct ipasfrag *p; 5514640Swnj { 5524951Swnj 5534898Swnj p->ipf_prev->ipf_next = p->ipf_next; 5544898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 5554495Swnj } 5564495Swnj 5574640Swnj /* 5584640Swnj * IP timer processing; 5594640Swnj * if a timer expires on a reassembly 5604640Swnj * queue, discard it. 5614640Swnj */ 5624801Swnj ip_slowtimo() 5634495Swnj { 5644495Swnj register struct ipq *fp; 5654640Swnj int s = splnet(); 5664951Swnj 5675243Sroot fp = ipq.next; 5685243Sroot if (fp == 0) { 5695243Sroot splx(s); 5705243Sroot return; 5715243Sroot } 57210735Ssam while (fp != &ipq) { 57310735Ssam --fp->ipq_ttl; 57410735Ssam fp = fp->next; 57524813Skarels if (fp->prev->ipq_ttl == 0) { 57624813Skarels ipstat.ips_fragtimeout++; 57710735Ssam ip_freef(fp->prev); 57824813Skarels } 57910735Ssam } 5804640Swnj splx(s); 5814495Swnj } 5824495Swnj 5834951Swnj /* 5844951Swnj * Drain off all datagram fragments. 5854951Swnj */ 5864801Swnj ip_drain() 5874801Swnj { 5884801Swnj 58924813Skarels while (ipq.next != &ipq) { 59024813Skarels ipstat.ips_fragdropped++; 59110735Ssam ip_freef(ipq.next); 59224813Skarels } 5934801Swnj } 5944923Swnj 59524813Skarels struct in_ifaddr *ip_rtaddr(); 59624813Skarels 5974640Swnj /* 5984640Swnj * Do option processing on a datagram, 59940689Skarels * possibly discarding it if bad options are encountered, 60040689Skarels * or forwarding it if source-routed. 60140689Skarels * Returns 1 if packet has been forwarded/freed, 60240689Skarels * 0 if the packet should be processed further. 6034640Swnj */ 60437319Skarels ip_dooptions(m) 60536814Skarels struct mbuf *m; 6064495Swnj { 60736814Skarels register struct ip *ip = mtod(m, struct ip *); 6084640Swnj register u_char *cp; 60924813Skarels register struct ip_timestamp *ipt; 61024813Skarels register struct in_ifaddr *ia; 61136814Skarels int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 61258998Ssklower struct in_addr *sin, dst; 61324813Skarels n_time ntime; 6144495Swnj 61558998Ssklower dst = ip->ip_dst; 6164640Swnj cp = (u_char *)(ip + 1); 6174640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 6184640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 61924813Skarels opt = cp[IPOPT_OPTVAL]; 6204640Swnj if (opt == IPOPT_EOL) 6214640Swnj break; 6224640Swnj if (opt == IPOPT_NOP) 6234640Swnj optlen = 1; 62416392Ssam else { 62524813Skarels optlen = cp[IPOPT_OLEN]; 62624813Skarels if (optlen <= 0 || optlen > cnt) { 62724813Skarels code = &cp[IPOPT_OLEN] - (u_char *)ip; 62817551Skarels goto bad; 62924813Skarels } 63016392Ssam } 6314640Swnj switch (opt) { 6324495Swnj 6334640Swnj default: 6344640Swnj break; 6354495Swnj 6364951Swnj /* 6374951Swnj * Source routing with record. 6384951Swnj * Find interface with current destination address. 6394951Swnj * If none on this machine then drop if strictly routed, 6404951Swnj * or do nothing if loosely routed. 6414951Swnj * Record interface address and bring up next address 6424951Swnj * component. If strictly routed make sure next 64340689Skarels * address is on directly accessible net. 6444951Swnj */ 6454640Swnj case IPOPT_LSRR: 6467508Sroot case IPOPT_SSRR: 64724813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 64824813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 64924813Skarels goto bad; 65024813Skarels } 65124813Skarels ipaddr.sin_addr = ip->ip_dst; 65224813Skarels ia = (struct in_ifaddr *) 65324813Skarels ifa_ifwithaddr((struct sockaddr *)&ipaddr); 65424813Skarels if (ia == 0) { 65524813Skarels if (opt == IPOPT_SSRR) { 65624813Skarels type = ICMP_UNREACH; 65724813Skarels code = ICMP_UNREACH_SRCFAIL; 6584951Swnj goto bad; 65924813Skarels } 66024813Skarels /* 66124813Skarels * Loose routing, and not at next destination 66224813Skarels * yet; nothing to do except forward. 66324813Skarels */ 6644951Swnj break; 6654640Swnj } 66624813Skarels off--; /* 0 origin */ 66724813Skarels if (off > optlen - sizeof(struct in_addr)) { 66824813Skarels /* 66924813Skarels * End of source route. Should be for us. 67024813Skarels */ 67124813Skarels save_rte(cp, ip->ip_src); 6724951Swnj break; 67324813Skarels } 67424813Skarels /* 67524813Skarels * locate outgoing interface 67624813Skarels */ 67726384Skarels bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 67824813Skarels sizeof(ipaddr.sin_addr)); 67940689Skarels if (opt == IPOPT_SSRR) { 68040689Skarels #define INA struct in_ifaddr * 68140689Skarels #define SA struct sockaddr * 68240689Skarels if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 68358998Ssklower ia = (INA)ifa_ifwithnet((SA)&ipaddr); 68440689Skarels } else 68540689Skarels ia = ip_rtaddr(ipaddr.sin_addr); 68640689Skarels if (ia == 0) { 68724813Skarels type = ICMP_UNREACH; 68824813Skarels code = ICMP_UNREACH_SRCFAIL; 6894951Swnj goto bad; 69024813Skarels } 69124813Skarels ip->ip_dst = ipaddr.sin_addr; 69226384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 69326384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 69424813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 69557433Sandrew /* 69657433Sandrew * Let ip_intr's mcast routing check handle mcast pkts 69757433Sandrew */ 69857433Sandrew forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 6994640Swnj break; 7004495Swnj 70124813Skarels case IPOPT_RR: 70224813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 70324813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 70424813Skarels goto bad; 70524813Skarels } 70624813Skarels /* 70724813Skarels * If no space remains, ignore. 70824813Skarels */ 70924813Skarels off--; /* 0 origin */ 71024813Skarels if (off > optlen - sizeof(struct in_addr)) 71124813Skarels break; 71231393Skarels bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 71324813Skarels sizeof(ipaddr.sin_addr)); 71424813Skarels /* 71537319Skarels * locate outgoing interface; if we're the destination, 71637319Skarels * use the incoming interface (should be same). 71724813Skarels */ 71840689Skarels if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 71937319Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 72024813Skarels type = ICMP_UNREACH; 72132113Skarels code = ICMP_UNREACH_HOST; 72224813Skarels goto bad; 72324813Skarels } 72426384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 72526384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 72624813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 72724813Skarels break; 72824813Skarels 7294640Swnj case IPOPT_TS: 7306583Ssam code = cp - (u_char *)ip; 7314801Swnj ipt = (struct ip_timestamp *)cp; 7324801Swnj if (ipt->ipt_len < 5) 7334640Swnj goto bad; 7344801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 7354801Swnj if (++ipt->ipt_oflw == 0) 7364640Swnj goto bad; 7374495Swnj break; 7384640Swnj } 73930925Skarels sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 7404801Swnj switch (ipt->ipt_flg) { 7414495Swnj 7424640Swnj case IPOPT_TS_TSONLY: 7434640Swnj break; 7444640Swnj 7454640Swnj case IPOPT_TS_TSANDADDR: 74624813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 74724813Skarels sizeof(struct in_addr) > ipt->ipt_len) 7484640Swnj goto bad; 74958998Ssklower ipaddr.sin_addr = dst; 75058998Ssklower ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 75158998Ssklower m->m_pkthdr.rcvif); 75258998Ssklower if (ia == 0) 75358998Ssklower continue; 75430925Skarels bcopy((caddr_t)&IA_SIN(ia)->sin_addr, 75524813Skarels (caddr_t)sin, sizeof(struct in_addr)); 75630925Skarels ipt->ipt_ptr += sizeof(struct in_addr); 7574640Swnj break; 7584640Swnj 7594640Swnj case IPOPT_TS_PRESPEC: 76030925Skarels if (ipt->ipt_ptr + sizeof(n_time) + 76130925Skarels sizeof(struct in_addr) > ipt->ipt_len) 76230925Skarels goto bad; 76324813Skarels bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 76424813Skarels sizeof(struct in_addr)); 76540689Skarels if (ifa_ifwithaddr((SA)&ipaddr) == 0) 7664951Swnj continue; 76724813Skarels ipt->ipt_ptr += sizeof(struct in_addr); 7684640Swnj break; 7694640Swnj 7704495Swnj default: 7714640Swnj goto bad; 7724495Swnj } 77324813Skarels ntime = iptime(); 77430925Skarels bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 77530925Skarels sizeof(n_time)); 77624813Skarels ipt->ipt_ptr += sizeof(n_time); 7774640Swnj } 7784495Swnj } 77936814Skarels if (forward) { 78040689Skarels ip_forward(m, 1); 78136814Skarels return (1); 78257433Sandrew } 78357433Sandrew return (0); 7844640Swnj bad: 78557433Sandrew ip->ip_len -= ip->ip_hl << 2; /* XXX icmp_error adds in hdr length */ 78658998Ssklower icmp_error(m, type, code, 0, 0); 78757433Sandrew ipstat.ips_badoptions++; 7886583Ssam return (1); 7894495Swnj } 7904495Swnj 7914640Swnj /* 79224813Skarels * Given address of next destination (final or next hop), 79324813Skarels * return internet address info of interface to be used to get there. 79424813Skarels */ 79524813Skarels struct in_ifaddr * 79624813Skarels ip_rtaddr(dst) 79724813Skarels struct in_addr dst; 79824813Skarels { 79924813Skarels register struct sockaddr_in *sin; 80024813Skarels 80124813Skarels sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 80224813Skarels 80324813Skarels if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 80424813Skarels if (ipforward_rt.ro_rt) { 80524813Skarels RTFREE(ipforward_rt.ro_rt); 80624813Skarels ipforward_rt.ro_rt = 0; 80724813Skarels } 80824813Skarels sin->sin_family = AF_INET; 80937319Skarels sin->sin_len = sizeof(*sin); 81024813Skarels sin->sin_addr = dst; 81124813Skarels 81224813Skarels rtalloc(&ipforward_rt); 81324813Skarels } 81424813Skarels if (ipforward_rt.ro_rt == 0) 81524813Skarels return ((struct in_ifaddr *)0); 81640689Skarels return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 81724813Skarels } 81824813Skarels 81924813Skarels /* 82024813Skarels * Save incoming source route for use in replies, 82124813Skarels * to be picked up later by ip_srcroute if the receiver is interested. 82224813Skarels */ 82324813Skarels save_rte(option, dst) 82426384Skarels u_char *option; 82524813Skarels struct in_addr dst; 82624813Skarels { 82726384Skarels unsigned olen; 82824813Skarels 82924813Skarels olen = option[IPOPT_OLEN]; 83049042Ssklower #ifdef DIAGNOSTIC 83136814Skarels if (ipprintfs) 83236814Skarels printf("save_rte: olen %d\n", olen); 83340689Skarels #endif 83436814Skarels if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 83524813Skarels return; 83626384Skarels bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 83724813Skarels ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 83836814Skarels ip_srcrt.dst = dst; 83924813Skarels } 84024813Skarels 84124813Skarels /* 84224813Skarels * Retrieve incoming source route for use in replies, 84324813Skarels * in the same form used by setsockopt. 84424813Skarels * The first hop is placed before the options, will be removed later. 84524813Skarels */ 84624813Skarels struct mbuf * 84724813Skarels ip_srcroute() 84824813Skarels { 84924813Skarels register struct in_addr *p, *q; 85024813Skarels register struct mbuf *m; 85124813Skarels 85224813Skarels if (ip_nhops == 0) 85324813Skarels return ((struct mbuf *)0); 85431201Skarels m = m_get(M_DONTWAIT, MT_SOOPTS); 85531201Skarels if (m == 0) 85631201Skarels return ((struct mbuf *)0); 85724813Skarels 85836814Skarels #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 85936814Skarels 86036814Skarels /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 86136814Skarels m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 86236814Skarels OPTSIZ; 86349042Ssklower #ifdef DIAGNOSTIC 86436814Skarels if (ipprintfs) 86536814Skarels printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 86640689Skarels #endif 86736814Skarels 86824813Skarels /* 86924813Skarels * First save first hop for return route 87024813Skarels */ 87124813Skarels p = &ip_srcrt.route[ip_nhops - 1]; 87224813Skarels *(mtod(m, struct in_addr *)) = *p--; 87349042Ssklower #ifdef DIAGNOSTIC 87436814Skarels if (ipprintfs) 87549882Sbostic printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr)); 87640689Skarels #endif 87724813Skarels 87824813Skarels /* 87924813Skarels * Copy option fields and padding (nop) to mbuf. 88024813Skarels */ 88124813Skarels ip_srcrt.nop = IPOPT_NOP; 88236814Skarels ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 88336814Skarels bcopy((caddr_t)&ip_srcrt.nop, 88436814Skarels mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 88524813Skarels q = (struct in_addr *)(mtod(m, caddr_t) + 88636814Skarels sizeof(struct in_addr) + OPTSIZ); 88736814Skarels #undef OPTSIZ 88824813Skarels /* 88924813Skarels * Record return path as an IP source route, 89024813Skarels * reversing the path (pointers are now aligned). 89124813Skarels */ 89236814Skarels while (p >= ip_srcrt.route) { 89349042Ssklower #ifdef DIAGNOSTIC 89436814Skarels if (ipprintfs) 89549882Sbostic printf(" %lx", ntohl(q->s_addr)); 89640689Skarels #endif 89724813Skarels *q++ = *p--; 89836814Skarels } 89936814Skarels /* 90036814Skarels * Last hop goes to final destination. 90136814Skarels */ 90236814Skarels *q = ip_srcrt.dst; 90349042Ssklower #ifdef DIAGNOSTIC 90436814Skarels if (ipprintfs) 90549882Sbostic printf(" %lx\n", ntohl(q->s_addr)); 90640689Skarels #endif 90724813Skarels return (m); 90824813Skarels } 90924813Skarels 91024813Skarels /* 9114951Swnj * Strip out IP options, at higher 9124951Swnj * level protocol in the kernel. 9134951Swnj * Second argument is buffer to which options 9144951Swnj * will be moved, and return value is their length. 91536814Skarels * XXX should be deleted; last arg currently ignored. 9164640Swnj */ 91737319Skarels ip_stripoptions(m, mopt) 91837319Skarels register struct mbuf *m; 9195217Swnj struct mbuf *mopt; 9204495Swnj { 9214640Swnj register int i; 92237319Skarels struct ip *ip = mtod(m, struct ip *); 92324813Skarels register caddr_t opts; 9244640Swnj int olen; 9254640Swnj 9264640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 92724813Skarels opts = (caddr_t)(ip + 1); 9284640Swnj i = m->m_len - (sizeof (struct ip) + olen); 92924813Skarels bcopy(opts + olen, opts, (unsigned)i); 9305243Sroot m->m_len -= olen; 93137319Skarels if (m->m_flags & M_PKTHDR) 93237319Skarels m->m_pkthdr.len -= olen; 93324813Skarels ip->ip_hl = sizeof(struct ip) >> 2; 9344495Swnj } 9356583Ssam 93614670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 93724813Skarels 0, 0, 0, 0, 93840689Skarels 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 93940689Skarels EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 94024813Skarels EMSGSIZE, EHOSTUNREACH, 0, 0, 94124813Skarels 0, 0, 0, 0, 94224813Skarels ENOPROTOOPT 9436583Ssam }; 9446583Ssam 9456583Ssam /* 9466583Ssam * Forward a packet. If some error occurs return the sender 94718376Skarels * an icmp packet. Note we can't always generate a meaningful 94824813Skarels * icmp message because icmp doesn't have a large enough repertoire 9496583Ssam * of codes and types. 95026308Skarels * 95140689Skarels * If not forwarding, just drop the packet. This could be confusing 95240689Skarels * if ipforwarding was zero but some routing protocol was advancing 95340689Skarels * us as a gateway to somewhere. However, we must let the routing 95440689Skarels * protocol deal with that. 95540689Skarels * 95640689Skarels * The srcrt parameter indicates whether the packet is being forwarded 95740689Skarels * via a source route. 9586583Ssam */ 95940689Skarels ip_forward(m, srcrt) 96036814Skarels struct mbuf *m; 96140689Skarels int srcrt; 9626583Ssam { 96336814Skarels register struct ip *ip = mtod(m, struct ip *); 96424813Skarels register struct sockaddr_in *sin; 96540689Skarels register struct rtentry *rt; 96640689Skarels int error, type = 0, code; 96718376Skarels struct mbuf *mcopy; 96824813Skarels struct in_addr dest; 96957433Sandrew struct ifnet *destifp; 9706583Ssam 97124813Skarels dest.s_addr = 0; 97249042Ssklower #ifdef DIAGNOSTIC 9736583Ssam if (ipprintfs) 9746583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 9756583Ssam ip->ip_dst, ip->ip_ttl); 97640689Skarels #endif 97737319Skarels if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 97826308Skarels ipstat.ips_cantforward++; 97937319Skarels m_freem(m); 98026308Skarels return; 9816583Ssam } 98240689Skarels HTONS(ip->ip_id); 98331393Skarels if (ip->ip_ttl <= IPTTLDEC) { 98440689Skarels icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest); 98540689Skarels return; 9866583Ssam } 9876583Ssam ip->ip_ttl -= IPTTLDEC; 9886609Ssam 98924813Skarels sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 99040689Skarels if ((rt = ipforward_rt.ro_rt) == 0 || 99124813Skarels ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 99224813Skarels if (ipforward_rt.ro_rt) { 99324813Skarels RTFREE(ipforward_rt.ro_rt); 99424813Skarels ipforward_rt.ro_rt = 0; 99524813Skarels } 99624813Skarels sin->sin_family = AF_INET; 99737319Skarels sin->sin_len = sizeof(*sin); 99824813Skarels sin->sin_addr = ip->ip_dst; 99924813Skarels 100024813Skarels rtalloc(&ipforward_rt); 100140689Skarels if (ipforward_rt.ro_rt == 0) { 100240689Skarels icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest); 100340689Skarels return; 100440689Skarels } 100540689Skarels rt = ipforward_rt.ro_rt; 100624813Skarels } 100740689Skarels 100824813Skarels /* 100940689Skarels * Save at most 64 bytes of the packet in case 101040689Skarels * we need to generate an ICMP message to the src. 101140689Skarels */ 101240689Skarels mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 101340689Skarels 101440689Skarels #ifdef GATEWAY 101540689Skarels ip_ifmatrix[rt->rt_ifp->if_index + 101640689Skarels if_index * m->m_pkthdr.rcvif->if_index]++; 101740689Skarels #endif 101840689Skarels /* 101924813Skarels * If forwarding packet using same interface that it came in on, 102024813Skarels * perhaps should send a redirect to sender to shortcut a hop. 102124813Skarels * Only send redirect if source is sending directly to us, 102224813Skarels * and if packet was not source routed (or has any options). 102330447Skarels * Also, don't send redirect if forwarding using a default route 102440689Skarels * or a route modified by a redirect. 102524813Skarels */ 102630447Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 102740689Skarels if (rt->rt_ifp == m->m_pkthdr.rcvif && 102840689Skarels (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 102940689Skarels satosin(rt_key(rt))->sin_addr.s_addr != 0 && 103040689Skarels ipsendredirects && !srcrt) { 103152554Ssklower #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 103224813Skarels u_long src = ntohl(ip->ip_src.s_addr); 103324813Skarels u_long dst = ntohl(ip->ip_dst.s_addr); 103424813Skarels 103552554Ssklower if (RTA(rt) && 103652554Ssklower (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 103740689Skarels if (rt->rt_flags & RTF_GATEWAY) 103840689Skarels dest = satosin(rt->rt_gateway)->sin_addr; 103924813Skarels else 104024813Skarels dest = ip->ip_dst; 104158998Ssklower /* Router requirements says to only send host redirects */ 104224813Skarels type = ICMP_REDIRECT; 104358998Ssklower code = ICMP_REDIRECT_HOST; 104449042Ssklower #ifdef DIAGNOSTIC 104524813Skarels if (ipprintfs) 104640689Skarels printf("redirect (%d) to %x\n", code, dest.s_addr); 104740689Skarels #endif 104824813Skarels } 104924813Skarels } 105024813Skarels 105158998Ssklower error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING 105258998Ssklower #ifdef DIRECTED_BROADCAST 105358998Ssklower | IP_ALLOWBROADCAST 105458998Ssklower #endif 105558998Ssklower , 0); 105624813Skarels if (error) 105724813Skarels ipstat.ips_cantforward++; 105824813Skarels else { 105921117Skarels ipstat.ips_forward++; 106040689Skarels if (type) 106140689Skarels ipstat.ips_redirectsent++; 106240689Skarels else { 106340689Skarels if (mcopy) 106440689Skarels m_freem(mcopy); 106540689Skarels return; 106640689Skarels } 10676609Ssam } 106811540Ssam if (mcopy == NULL) 106911540Ssam return; 107057433Sandrew destifp = NULL; 107157433Sandrew 10726609Ssam switch (error) { 10736609Ssam 107424813Skarels case 0: /* forwarded, but need redirect */ 107540689Skarels /* type, code set above */ 107624813Skarels break; 107724813Skarels 107840689Skarels case ENETUNREACH: /* shouldn't happen, checked above */ 107940689Skarels case EHOSTUNREACH: 10806609Ssam case ENETDOWN: 108140689Skarels case EHOSTDOWN: 108240689Skarels default: 108340689Skarels type = ICMP_UNREACH; 108440689Skarels code = ICMP_UNREACH_HOST; 10856609Ssam break; 10866609Ssam 10876609Ssam case EMSGSIZE: 108840689Skarels type = ICMP_UNREACH; 10896583Ssam code = ICMP_UNREACH_NEEDFRAG; 109057433Sandrew if (ipforward_rt.ro_rt) 109157433Sandrew destifp = ipforward_rt.ro_rt->rt_ifp; 109239185Ssklower ipstat.ips_cantfrag++; 10936609Ssam break; 10946609Ssam 10956609Ssam case ENOBUFS: 10966609Ssam type = ICMP_SOURCEQUENCH; 109737319Skarels code = 0; 10986609Ssam break; 10996609Ssam } 110057433Sandrew icmp_error(mcopy, type, code, dest, destifp); 11016583Ssam } 1102*59136Smckusick 1103*59136Smckusick ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 1104*59136Smckusick int *name; 1105*59136Smckusick u_int namelen; 1106*59136Smckusick void *oldp; 1107*59136Smckusick size_t *oldlenp; 1108*59136Smckusick void *newp; 1109*59136Smckusick size_t newlen; 1110*59136Smckusick { 1111*59136Smckusick extern int ip_ttl; 1112*59136Smckusick 1113*59136Smckusick /* all sysctl names at this level are terminal */ 1114*59136Smckusick if (namelen != 1) 1115*59136Smckusick return (ENOTDIR); 1116*59136Smckusick 1117*59136Smckusick switch (name[0]) { 1118*59136Smckusick case IPCTL_FORWARDING: 1119*59136Smckusick return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding)); 1120*59136Smckusick case IPCTL_SENDREDIRECTS: 1121*59136Smckusick return (sysctl_int(oldp, oldlenp, newp, newlen, 1122*59136Smckusick &ipsendredirects)); 1123*59136Smckusick case IPCTL_DEFTTL: 1124*59136Smckusick return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl)); 1125*59136Smckusick #ifdef notyet 1126*59136Smckusick case IPCTL_DEFMTU: 1127*59136Smckusick return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu)); 1128*59136Smckusick #endif 1129*59136Smckusick default: 1130*59136Smckusick return (EOPNOTSUPP); 1131*59136Smckusick } 1132*59136Smckusick /* NOTREACHED */ 1133*59136Smckusick } 1134