123184Smckusick /* 2*63218Sbostic * Copyright (c) 1982, 1986, 1988, 1993 3*63218Sbostic * The Regents of the University of California. All rights reserved. 423184Smckusick * 544480Sbostic * %sccs.include.redist.c% 632787Sbostic * 7*63218Sbostic * @(#)ip_input.c 8.1 (Berkeley) 06/10/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; 4459136Smckusick 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 7661335Sbostic static void save_rte __P((u_char *, struct in_addr)); 7724813Skarels /* 785172Swnj * IP initialization: fill in IP protocol switch table. 795161Swnj * All protocols not implemented in kernel go to raw IP protocol handler. 804801Swnj */ 8161335Sbostic void 824801Swnj ip_init() 834801Swnj { 844898Swnj register struct protosw *pr; 854898Swnj register int i; 864495Swnj 8724813Skarels pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 884898Swnj if (pr == 0) 894898Swnj panic("ip_init"); 904898Swnj for (i = 0; i < IPPROTO_MAX; i++) 919030Sroot ip_protox[i] = pr - inetsw; 929030Sroot for (pr = inetdomain.dom_protosw; 9317551Skarels pr < inetdomain.dom_protoswNPROTOSW; pr++) 9416990Skarels if (pr->pr_domain->dom_family == PF_INET && 954898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 969030Sroot ip_protox[pr->pr_protocol] = pr - inetsw; 974801Swnj ipq.next = ipq.prev = &ipq; 988172Sroot ip_id = time.tv_sec & 0xffff; 996210Swnj ipintrq.ifq_maxlen = ipqmaxlen; 10040689Skarels #ifdef GATEWAY 10140689Skarels i = (if_index + 1) * (if_index + 1) * sizeof (u_long); 10250425Smckusick ip_ifmatrix = (u_long *) malloc(i, M_RTABLE, M_WAITOK); 10350425Smckusick bzero((char *)ip_ifmatrix, i); 10440689Skarels #endif 1054801Swnj } 1064801Swnj 10737319Skarels struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 10824813Skarels struct route ipforward_rt; 1094640Swnj 1104640Swnj /* 1114640Swnj * Ip input routine. Checksum and byte swap header. If fragmented 11240689Skarels * try to reassemble. Process options. Pass to next level. 1134640Swnj */ 11461335Sbostic void 1155084Swnj ipintr() 1164495Swnj { 1174923Swnj register struct ip *ip; 1185084Swnj register struct mbuf *m; 1194495Swnj register struct ipq *fp; 12018376Skarels register struct in_ifaddr *ia; 1215084Swnj int hlen, s; 1224495Swnj 1235084Swnj next: 1244640Swnj /* 1255084Swnj * Get next datagram off input queue and get IP header 1265084Swnj * in first mbuf. 1274640Swnj */ 1285084Swnj s = splimp(); 12937319Skarels IF_DEQUEUE(&ipintrq, m); 1305084Swnj splx(s); 1315218Swnj if (m == 0) 1325084Swnj return; 13344967Skarels #ifdef DIAGNOSTIC 13444967Skarels if ((m->m_flags & M_PKTHDR) == 0) 13544967Skarels panic("ipintr no HDR"); 13644967Skarels #endif 13726001Skarels /* 13826001Skarels * If no IP addresses have been set yet but the interfaces 13926001Skarels * are receiving, can't do anything with incoming packets yet. 14026001Skarels */ 14126001Skarels if (in_ifaddr == NULL) 14226001Skarels goto bad; 14325920Skarels ipstat.ips_total++; 14440689Skarels if (m->m_len < sizeof (struct ip) && 14511232Ssam (m = m_pullup(m, sizeof (struct ip))) == 0) { 14611232Ssam ipstat.ips_toosmall++; 14711232Ssam goto next; 14811232Ssam } 1494640Swnj ip = mtod(m, struct ip *); 15057968Sandrew if (ip->ip_v != IPVERSION) { 15157968Sandrew ipstat.ips_badvers++; 15257968Sandrew goto bad; 15357968Sandrew } 15418376Skarels hlen = ip->ip_hl << 2; 15524813Skarels if (hlen < sizeof(struct ip)) { /* minimum header length */ 15618376Skarels ipstat.ips_badhlen++; 15721117Skarels goto bad; 15818376Skarels } 15918376Skarels if (hlen > m->m_len) { 16011232Ssam if ((m = m_pullup(m, hlen)) == 0) { 16111232Ssam ipstat.ips_badhlen++; 16211232Ssam goto next; 16311232Ssam } 1645161Swnj ip = mtod(m, struct ip *); 1655161Swnj } 16637319Skarels if (ip->ip_sum = in_cksum(m, hlen)) { 16737319Skarels ipstat.ips_badsum++; 16837319Skarels goto bad; 16937319Skarels } 1704951Swnj 1714951Swnj /* 1724951Swnj * Convert fields to host representation. 1734951Swnj */ 17440689Skarels NTOHS(ip->ip_len); 17511232Ssam if (ip->ip_len < hlen) { 17611232Ssam ipstat.ips_badlen++; 17711232Ssam goto bad; 17811232Ssam } 17940689Skarels NTOHS(ip->ip_id); 18040689Skarels NTOHS(ip->ip_off); 1814495Swnj 1824543Swnj /* 1834640Swnj * Check that the amount of data in the buffers 1844640Swnj * is as at least much as the IP header would have us expect. 1854640Swnj * Trim mbufs if longer than we expect. 1864640Swnj * Drop packet if shorter than we expect. 1874543Swnj */ 18837319Skarels if (m->m_pkthdr.len < ip->ip_len) { 18937319Skarels ipstat.ips_tooshort++; 19037319Skarels goto bad; 1916088Sroot } 19237319Skarels if (m->m_pkthdr.len > ip->ip_len) { 19337319Skarels if (m->m_len == m->m_pkthdr.len) { 19437319Skarels m->m_len = ip->ip_len; 19537319Skarels m->m_pkthdr.len = ip->ip_len; 19637319Skarels } else 19737319Skarels m_adj(m, ip->ip_len - m->m_pkthdr.len); 1984495Swnj } 1994495Swnj 2004640Swnj /* 2014640Swnj * Process options and, if not destined for us, 2026583Ssam * ship it on. ip_dooptions returns 1 when an 2036583Ssam * error was detected (causing an icmp message 20421117Skarels * to be sent and the original packet to be freed). 2054640Swnj */ 20624813Skarels ip_nhops = 0; /* for source routed packets */ 20737319Skarels if (hlen > sizeof (struct ip) && ip_dooptions(m)) 2086583Ssam goto next; 2096210Swnj 2106338Ssam /* 21118376Skarels * Check our list of addresses, to see if the packet is for us. 2126338Ssam */ 21318376Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) { 21418376Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 2156338Ssam 21618376Skarels if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 21724813Skarels goto ours; 21825195Skarels if ( 21925195Skarels #ifdef DIRECTED_BROADCAST 22037319Skarels ia->ia_ifp == m->m_pkthdr.rcvif && 22125195Skarels #endif 22225195Skarels (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 22326247Skarels u_long t; 22425195Skarels 22525195Skarels if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 22625195Skarels ip->ip_dst.s_addr) 22725195Skarels goto ours; 22825195Skarels if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 22925195Skarels goto ours; 23025195Skarels /* 23125195Skarels * Look for all-0's host part (old broadcast addr), 23225195Skarels * either for subnet or net. 23325195Skarels */ 23426247Skarels t = ntohl(ip->ip_dst.s_addr); 23526247Skarels if (t == ia->ia_subnet) 23625195Skarels goto ours; 23726247Skarels if (t == ia->ia_net) 23825195Skarels goto ours; 23925195Skarels } 2406338Ssam } 24154716Ssklower if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 24254716Ssklower struct in_multi *inm; 24354716Ssklower #ifdef MROUTING 24454716Ssklower extern struct socket *ip_mrouter; 24554716Ssklower 24654716Ssklower if (ip_mrouter) { 24754716Ssklower /* 24854716Ssklower * If we are acting as a multicast router, all 24954716Ssklower * incoming multicast packets are passed to the 25054716Ssklower * kernel-level multicast forwarding function. 25154716Ssklower * The packet is returned (relatively) intact; if 25254716Ssklower * ip_mforward() returns a non-zero value, the packet 25354716Ssklower * must be discarded, else it may be accepted below. 25454716Ssklower * 25554716Ssklower * (The IP ident field is put in the same byte order 25654716Ssklower * as expected when ip_mforward() is called from 25754716Ssklower * ip_output().) 25854716Ssklower */ 25954716Ssklower ip->ip_id = htons(ip->ip_id); 26054716Ssklower if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) { 26157433Sandrew ipstat.ips_cantforward++; 26254716Ssklower m_freem(m); 26354716Ssklower goto next; 26454716Ssklower } 26554716Ssklower ip->ip_id = ntohs(ip->ip_id); 26654716Ssklower 26754716Ssklower /* 26854716Ssklower * The process-level routing demon needs to receive 26954716Ssklower * all multicast IGMP packets, whether or not this 27054716Ssklower * host belongs to their destination groups. 27154716Ssklower */ 27254716Ssklower if (ip->ip_p == IPPROTO_IGMP) 27354716Ssklower goto ours; 27457433Sandrew ipstat.ips_forward++; 27554716Ssklower } 27654716Ssklower #endif 27754716Ssklower /* 27854716Ssklower * See if we belong to the destination multicast group on the 27954716Ssklower * arrival interface. 28054716Ssklower */ 28154716Ssklower IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 28254716Ssklower if (inm == NULL) { 28357433Sandrew ipstat.ips_cantforward++; 28454716Ssklower m_freem(m); 28554716Ssklower goto next; 28654716Ssklower } 28754716Ssklower goto ours; 28854716Ssklower } 28924813Skarels if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 29024813Skarels goto ours; 29124813Skarels if (ip->ip_dst.s_addr == INADDR_ANY) 29224813Skarels goto ours; 2934495Swnj 2944640Swnj /* 29524813Skarels * Not for us; forward if possible and desirable. 29624813Skarels */ 29740689Skarels if (ipforwarding == 0) { 29836814Skarels ipstat.ips_cantforward++; 29936814Skarels m_freem(m); 30036814Skarels } else 30140689Skarels ip_forward(m, 0); 30224813Skarels goto next; 30324813Skarels 30424813Skarels ours: 30524813Skarels /* 30633743Skarels * If offset or IP_MF are set, must reassemble. 30733743Skarels * Otherwise, nothing need be done. 30833743Skarels * (We could look in the reassembly queue to see 30933743Skarels * if the packet was previously fragmented, 31033743Skarels * but it's not worth the time; just let them time out.) 3114640Swnj */ 31233743Skarels if (ip->ip_off &~ IP_DF) { 31340689Skarels if (m->m_flags & M_EXT) { /* XXX */ 31440689Skarels if ((m = m_pullup(m, sizeof (struct ip))) == 0) { 31540689Skarels ipstat.ips_toosmall++; 31640689Skarels goto next; 31740689Skarels } 31840689Skarels ip = mtod(m, struct ip *); 31940689Skarels } 32033743Skarels /* 32133743Skarels * Look for queue of fragments 32233743Skarels * of this datagram. 32333743Skarels */ 32433743Skarels for (fp = ipq.next; fp != &ipq; fp = fp->next) 32533743Skarels if (ip->ip_id == fp->ipq_id && 32633743Skarels ip->ip_src.s_addr == fp->ipq_src.s_addr && 32733743Skarels ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 32833743Skarels ip->ip_p == fp->ipq_p) 32933743Skarels goto found; 33033743Skarels fp = 0; 3314640Swnj found: 3324495Swnj 33333743Skarels /* 33433743Skarels * Adjust ip_len to not reflect header, 33533743Skarels * set ip_mff if more fragments are expected, 33633743Skarels * convert offset of this to bytes. 33733743Skarels */ 33833743Skarels ip->ip_len -= hlen; 33957968Sandrew ((struct ipasfrag *)ip)->ipf_mff &= ~1; 34033743Skarels if (ip->ip_off & IP_MF) 34157968Sandrew ((struct ipasfrag *)ip)->ipf_mff |= 1; 34233743Skarels ip->ip_off <<= 3; 3434495Swnj 34433743Skarels /* 34533743Skarels * If datagram marked as having more fragments 34633743Skarels * or if this is not the first fragment, 34733743Skarels * attempt reassembly; if it succeeds, proceed. 34833743Skarels */ 34957968Sandrew if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) { 35033743Skarels ipstat.ips_fragments++; 35133743Skarels ip = ip_reass((struct ipasfrag *)ip, fp); 35233743Skarels if (ip == 0) 35333743Skarels goto next; 35457433Sandrew ipstat.ips_reassembled++; 35533743Skarels m = dtom(ip); 35633743Skarels } else 35733743Skarels if (fp) 35833743Skarels ip_freef(fp); 3594640Swnj } else 36033743Skarels ip->ip_len -= hlen; 3614951Swnj 3624951Swnj /* 3634951Swnj * Switch out to protocol's input routine. 3644951Swnj */ 36539185Ssklower ipstat.ips_delivered++; 36637319Skarels (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 3675084Swnj goto next; 3684951Swnj bad: 3694951Swnj m_freem(m); 3705084Swnj goto next; 3714640Swnj } 3724495Swnj 3734640Swnj /* 3744640Swnj * Take incoming datagram fragment and try to 3754951Swnj * reassemble it into whole datagram. If a chain for 3764640Swnj * reassembly of this datagram already exists, then it 3774640Swnj * is given as fp; otherwise have to make a chain. 3784640Swnj */ 3794640Swnj struct ip * 3804640Swnj ip_reass(ip, fp) 3814898Swnj register struct ipasfrag *ip; 3824640Swnj register struct ipq *fp; 3834640Swnj { 3844640Swnj register struct mbuf *m = dtom(ip); 3854898Swnj register struct ipasfrag *q; 3864640Swnj struct mbuf *t; 3874640Swnj int hlen = ip->ip_hl << 2; 3884640Swnj int i, next; 3894543Swnj 3904640Swnj /* 3914640Swnj * Presence of header sizes in mbufs 3924640Swnj * would confuse code below. 3934640Swnj */ 39437319Skarels m->m_data += hlen; 3954640Swnj m->m_len -= hlen; 3964495Swnj 3974640Swnj /* 3984640Swnj * If first fragment to arrive, create a reassembly queue. 3994640Swnj */ 4004640Swnj if (fp == 0) { 40131201Skarels if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 4024640Swnj goto dropfrag; 4034640Swnj fp = mtod(t, struct ipq *); 4044640Swnj insque(fp, &ipq); 4054640Swnj fp->ipq_ttl = IPFRAGTTL; 4064640Swnj fp->ipq_p = ip->ip_p; 4074640Swnj fp->ipq_id = ip->ip_id; 4084898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 4094898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 4104898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 4115161Swnj q = (struct ipasfrag *)fp; 4125161Swnj goto insert; 4134640Swnj } 4144495Swnj 4154640Swnj /* 4164640Swnj * Find a segment which begins after this one does. 4174640Swnj */ 4184898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 4194640Swnj if (q->ip_off > ip->ip_off) 4204640Swnj break; 4214495Swnj 4224640Swnj /* 4234640Swnj * If there is a preceding segment, it may provide some of 4244640Swnj * our data already. If so, drop the data from the incoming 4254640Swnj * segment. If it provides all of our data, drop us. 4264640Swnj */ 4274898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 4284898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 4294640Swnj if (i > 0) { 4304640Swnj if (i >= ip->ip_len) 4314640Swnj goto dropfrag; 4324640Swnj m_adj(dtom(ip), i); 4334640Swnj ip->ip_off += i; 4344640Swnj ip->ip_len -= i; 4354640Swnj } 4364640Swnj } 4374543Swnj 4384640Swnj /* 4394640Swnj * While we overlap succeeding segments trim them or, 4404640Swnj * if they are completely covered, dequeue them. 4414640Swnj */ 4424898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 4434640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 4444640Swnj if (i < q->ip_len) { 4454640Swnj q->ip_len -= i; 4466256Sroot q->ip_off += i; 4474640Swnj m_adj(dtom(q), i); 4484640Swnj break; 4494495Swnj } 4504898Swnj q = q->ipf_next; 4514898Swnj m_freem(dtom(q->ipf_prev)); 4524898Swnj ip_deq(q->ipf_prev); 4534543Swnj } 4544495Swnj 4555161Swnj insert: 4564640Swnj /* 4574640Swnj * Stick new segment in its place; 4584640Swnj * check for complete reassembly. 4594640Swnj */ 4604898Swnj ip_enq(ip, q->ipf_prev); 4614640Swnj next = 0; 4624898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 4634640Swnj if (q->ip_off != next) 4644640Swnj return (0); 4654640Swnj next += q->ip_len; 4664640Swnj } 46757968Sandrew if (q->ipf_prev->ipf_mff & 1) 4684640Swnj return (0); 4694495Swnj 4704640Swnj /* 4714640Swnj * Reassembly is complete; concatenate fragments. 4724640Swnj */ 4734640Swnj q = fp->ipq_next; 4744640Swnj m = dtom(q); 4754640Swnj t = m->m_next; 4764640Swnj m->m_next = 0; 4774640Swnj m_cat(m, t); 4786298Swnj q = q->ipf_next; 4796298Swnj while (q != (struct ipasfrag *)fp) { 4806298Swnj t = dtom(q); 4816298Swnj q = q->ipf_next; 4826298Swnj m_cat(m, t); 4836298Swnj } 4844495Swnj 4854640Swnj /* 4864640Swnj * Create header for new ip packet by 4874640Swnj * modifying header of first packet; 4884640Swnj * dequeue and discard fragment reassembly header. 4894640Swnj * Make header visible. 4904640Swnj */ 4914640Swnj ip = fp->ipq_next; 4924640Swnj ip->ip_len = next; 49357968Sandrew ip->ipf_mff &= ~1; 4944898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 4954898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 4964640Swnj remque(fp); 4974907Swnj (void) m_free(dtom(fp)); 4984640Swnj m = dtom(ip); 49924813Skarels m->m_len += (ip->ip_hl << 2); 50037319Skarels m->m_data -= (ip->ip_hl << 2); 50149042Ssklower /* some debugging cruft by sklower, below, will go away soon */ 50249042Ssklower if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 50349042Ssklower register int plen = 0; 50449042Ssklower for (t = m; m; m = m->m_next) 50549042Ssklower plen += m->m_len; 50649042Ssklower t->m_pkthdr.len = plen; 50749042Ssklower } 5084898Swnj return ((struct ip *)ip); 5094495Swnj 5104640Swnj dropfrag: 51124813Skarels ipstat.ips_fragdropped++; 5124640Swnj m_freem(m); 5134640Swnj return (0); 5144495Swnj } 5154495Swnj 5164640Swnj /* 5174640Swnj * Free a fragment reassembly header and all 5184640Swnj * associated datagrams. 5194640Swnj */ 52061335Sbostic void 5214640Swnj ip_freef(fp) 5224640Swnj struct ipq *fp; 5234495Swnj { 52410735Ssam register struct ipasfrag *q, *p; 5254495Swnj 52610735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 52710735Ssam p = q->ipf_next; 52810735Ssam ip_deq(q); 5294640Swnj m_freem(dtom(q)); 53010735Ssam } 53110735Ssam remque(fp); 53210735Ssam (void) m_free(dtom(fp)); 5334495Swnj } 5344495Swnj 5354640Swnj /* 5364640Swnj * Put an ip fragment on a reassembly chain. 5374640Swnj * Like insque, but pointers in middle of structure. 5384640Swnj */ 53961335Sbostic void 5404640Swnj ip_enq(p, prev) 5414898Swnj register struct ipasfrag *p, *prev; 5424495Swnj { 5434951Swnj 5444898Swnj p->ipf_prev = prev; 5454898Swnj p->ipf_next = prev->ipf_next; 5464898Swnj prev->ipf_next->ipf_prev = p; 5474898Swnj prev->ipf_next = p; 5484495Swnj } 5494495Swnj 5504640Swnj /* 5514640Swnj * To ip_enq as remque is to insque. 5524640Swnj */ 55361335Sbostic void 5544640Swnj ip_deq(p) 5554898Swnj register struct ipasfrag *p; 5564640Swnj { 5574951Swnj 5584898Swnj p->ipf_prev->ipf_next = p->ipf_next; 5594898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 5604495Swnj } 5614495Swnj 5624640Swnj /* 5634640Swnj * IP timer processing; 5644640Swnj * if a timer expires on a reassembly 5654640Swnj * queue, discard it. 5664640Swnj */ 56761335Sbostic void 5684801Swnj ip_slowtimo() 5694495Swnj { 5704495Swnj register struct ipq *fp; 5714640Swnj int s = splnet(); 5724951Swnj 5735243Sroot fp = ipq.next; 5745243Sroot if (fp == 0) { 5755243Sroot splx(s); 5765243Sroot return; 5775243Sroot } 57810735Ssam while (fp != &ipq) { 57910735Ssam --fp->ipq_ttl; 58010735Ssam fp = fp->next; 58124813Skarels if (fp->prev->ipq_ttl == 0) { 58224813Skarels ipstat.ips_fragtimeout++; 58310735Ssam ip_freef(fp->prev); 58424813Skarels } 58510735Ssam } 5864640Swnj splx(s); 5874495Swnj } 5884495Swnj 5894951Swnj /* 5904951Swnj * Drain off all datagram fragments. 5914951Swnj */ 59261335Sbostic void 5934801Swnj ip_drain() 5944801Swnj { 5954801Swnj 59624813Skarels while (ipq.next != &ipq) { 59724813Skarels ipstat.ips_fragdropped++; 59810735Ssam ip_freef(ipq.next); 59924813Skarels } 6004801Swnj } 6014923Swnj 6024640Swnj /* 6034640Swnj * Do option processing on a datagram, 60440689Skarels * possibly discarding it if bad options are encountered, 60540689Skarels * or forwarding it if source-routed. 60640689Skarels * Returns 1 if packet has been forwarded/freed, 60740689Skarels * 0 if the packet should be processed further. 6084640Swnj */ 60961335Sbostic int 61037319Skarels ip_dooptions(m) 61136814Skarels struct mbuf *m; 6124495Swnj { 61336814Skarels register struct ip *ip = mtod(m, struct ip *); 6144640Swnj register u_char *cp; 61524813Skarels register struct ip_timestamp *ipt; 61624813Skarels register struct in_ifaddr *ia; 61736814Skarels int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 61858998Ssklower struct in_addr *sin, dst; 61924813Skarels n_time ntime; 6204495Swnj 62158998Ssklower dst = ip->ip_dst; 6224640Swnj cp = (u_char *)(ip + 1); 6234640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 6244640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 62524813Skarels opt = cp[IPOPT_OPTVAL]; 6264640Swnj if (opt == IPOPT_EOL) 6274640Swnj break; 6284640Swnj if (opt == IPOPT_NOP) 6294640Swnj optlen = 1; 63016392Ssam else { 63124813Skarels optlen = cp[IPOPT_OLEN]; 63224813Skarels if (optlen <= 0 || optlen > cnt) { 63324813Skarels code = &cp[IPOPT_OLEN] - (u_char *)ip; 63417551Skarels goto bad; 63524813Skarels } 63616392Ssam } 6374640Swnj switch (opt) { 6384495Swnj 6394640Swnj default: 6404640Swnj break; 6414495Swnj 6424951Swnj /* 6434951Swnj * Source routing with record. 6444951Swnj * Find interface with current destination address. 6454951Swnj * If none on this machine then drop if strictly routed, 6464951Swnj * or do nothing if loosely routed. 6474951Swnj * Record interface address and bring up next address 6484951Swnj * component. If strictly routed make sure next 64940689Skarels * address is on directly accessible net. 6504951Swnj */ 6514640Swnj case IPOPT_LSRR: 6527508Sroot case IPOPT_SSRR: 65324813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 65424813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 65524813Skarels goto bad; 65624813Skarels } 65724813Skarels ipaddr.sin_addr = ip->ip_dst; 65824813Skarels ia = (struct in_ifaddr *) 65924813Skarels ifa_ifwithaddr((struct sockaddr *)&ipaddr); 66024813Skarels if (ia == 0) { 66124813Skarels if (opt == IPOPT_SSRR) { 66224813Skarels type = ICMP_UNREACH; 66324813Skarels code = ICMP_UNREACH_SRCFAIL; 6644951Swnj goto bad; 66524813Skarels } 66624813Skarels /* 66724813Skarels * Loose routing, and not at next destination 66824813Skarels * yet; nothing to do except forward. 66924813Skarels */ 6704951Swnj break; 6714640Swnj } 67224813Skarels off--; /* 0 origin */ 67324813Skarels if (off > optlen - sizeof(struct in_addr)) { 67424813Skarels /* 67524813Skarels * End of source route. Should be for us. 67624813Skarels */ 67724813Skarels save_rte(cp, ip->ip_src); 6784951Swnj break; 67924813Skarels } 68024813Skarels /* 68124813Skarels * locate outgoing interface 68224813Skarels */ 68326384Skarels bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 68424813Skarels sizeof(ipaddr.sin_addr)); 68540689Skarels if (opt == IPOPT_SSRR) { 68640689Skarels #define INA struct in_ifaddr * 68740689Skarels #define SA struct sockaddr * 68840689Skarels if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 68958998Ssklower ia = (INA)ifa_ifwithnet((SA)&ipaddr); 69040689Skarels } else 69140689Skarels ia = ip_rtaddr(ipaddr.sin_addr); 69240689Skarels if (ia == 0) { 69324813Skarels type = ICMP_UNREACH; 69424813Skarels code = ICMP_UNREACH_SRCFAIL; 6954951Swnj goto bad; 69624813Skarels } 69724813Skarels ip->ip_dst = ipaddr.sin_addr; 69826384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 69926384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 70024813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 70157433Sandrew /* 70257433Sandrew * Let ip_intr's mcast routing check handle mcast pkts 70357433Sandrew */ 70457433Sandrew forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 7054640Swnj break; 7064495Swnj 70724813Skarels case IPOPT_RR: 70824813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 70924813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 71024813Skarels goto bad; 71124813Skarels } 71224813Skarels /* 71324813Skarels * If no space remains, ignore. 71424813Skarels */ 71524813Skarels off--; /* 0 origin */ 71624813Skarels if (off > optlen - sizeof(struct in_addr)) 71724813Skarels break; 71831393Skarels bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 71924813Skarels sizeof(ipaddr.sin_addr)); 72024813Skarels /* 72137319Skarels * locate outgoing interface; if we're the destination, 72237319Skarels * use the incoming interface (should be same). 72324813Skarels */ 72440689Skarels if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 72537319Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 72624813Skarels type = ICMP_UNREACH; 72732113Skarels code = ICMP_UNREACH_HOST; 72824813Skarels goto bad; 72924813Skarels } 73026384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 73126384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 73224813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 73324813Skarels break; 73424813Skarels 7354640Swnj case IPOPT_TS: 7366583Ssam code = cp - (u_char *)ip; 7374801Swnj ipt = (struct ip_timestamp *)cp; 7384801Swnj if (ipt->ipt_len < 5) 7394640Swnj goto bad; 7404801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 7414801Swnj if (++ipt->ipt_oflw == 0) 7424640Swnj goto bad; 7434495Swnj break; 7444640Swnj } 74530925Skarels sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 7464801Swnj switch (ipt->ipt_flg) { 7474495Swnj 7484640Swnj case IPOPT_TS_TSONLY: 7494640Swnj break; 7504640Swnj 7514640Swnj case IPOPT_TS_TSANDADDR: 75224813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 75324813Skarels sizeof(struct in_addr) > ipt->ipt_len) 7544640Swnj goto bad; 75558998Ssklower ipaddr.sin_addr = dst; 75658998Ssklower ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 75758998Ssklower m->m_pkthdr.rcvif); 75858998Ssklower if (ia == 0) 75958998Ssklower continue; 76030925Skarels bcopy((caddr_t)&IA_SIN(ia)->sin_addr, 76124813Skarels (caddr_t)sin, sizeof(struct in_addr)); 76230925Skarels ipt->ipt_ptr += sizeof(struct in_addr); 7634640Swnj break; 7644640Swnj 7654640Swnj case IPOPT_TS_PRESPEC: 76630925Skarels if (ipt->ipt_ptr + sizeof(n_time) + 76730925Skarels sizeof(struct in_addr) > ipt->ipt_len) 76830925Skarels goto bad; 76924813Skarels bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 77024813Skarels sizeof(struct in_addr)); 77140689Skarels if (ifa_ifwithaddr((SA)&ipaddr) == 0) 7724951Swnj continue; 77324813Skarels ipt->ipt_ptr += sizeof(struct in_addr); 7744640Swnj break; 7754640Swnj 7764495Swnj default: 7774640Swnj goto bad; 7784495Swnj } 77924813Skarels ntime = iptime(); 78030925Skarels bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 78130925Skarels sizeof(n_time)); 78224813Skarels ipt->ipt_ptr += sizeof(n_time); 7834640Swnj } 7844495Swnj } 78536814Skarels if (forward) { 78640689Skarels ip_forward(m, 1); 78736814Skarels return (1); 78857433Sandrew } 78957433Sandrew return (0); 7904640Swnj bad: 79157433Sandrew ip->ip_len -= ip->ip_hl << 2; /* XXX icmp_error adds in hdr length */ 79258998Ssklower icmp_error(m, type, code, 0, 0); 79357433Sandrew ipstat.ips_badoptions++; 7946583Ssam return (1); 7954495Swnj } 7964495Swnj 7974640Swnj /* 79824813Skarels * Given address of next destination (final or next hop), 79924813Skarels * return internet address info of interface to be used to get there. 80024813Skarels */ 80124813Skarels struct in_ifaddr * 80224813Skarels ip_rtaddr(dst) 80324813Skarels struct in_addr dst; 80424813Skarels { 80524813Skarels register struct sockaddr_in *sin; 80624813Skarels 80724813Skarels sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 80824813Skarels 80924813Skarels if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 81024813Skarels if (ipforward_rt.ro_rt) { 81124813Skarels RTFREE(ipforward_rt.ro_rt); 81224813Skarels ipforward_rt.ro_rt = 0; 81324813Skarels } 81424813Skarels sin->sin_family = AF_INET; 81537319Skarels sin->sin_len = sizeof(*sin); 81624813Skarels sin->sin_addr = dst; 81724813Skarels 81824813Skarels rtalloc(&ipforward_rt); 81924813Skarels } 82024813Skarels if (ipforward_rt.ro_rt == 0) 82124813Skarels return ((struct in_ifaddr *)0); 82240689Skarels return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 82324813Skarels } 82424813Skarels 82524813Skarels /* 82624813Skarels * Save incoming source route for use in replies, 82724813Skarels * to be picked up later by ip_srcroute if the receiver is interested. 82824813Skarels */ 82961335Sbostic void 83024813Skarels save_rte(option, dst) 83126384Skarels u_char *option; 83224813Skarels struct in_addr dst; 83324813Skarels { 83426384Skarels unsigned olen; 83524813Skarels 83624813Skarels olen = option[IPOPT_OLEN]; 83749042Ssklower #ifdef DIAGNOSTIC 83836814Skarels if (ipprintfs) 83936814Skarels printf("save_rte: olen %d\n", olen); 84040689Skarels #endif 84136814Skarels if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 84224813Skarels return; 84326384Skarels bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 84424813Skarels ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 84536814Skarels ip_srcrt.dst = dst; 84624813Skarels } 84724813Skarels 84824813Skarels /* 84924813Skarels * Retrieve incoming source route for use in replies, 85024813Skarels * in the same form used by setsockopt. 85124813Skarels * The first hop is placed before the options, will be removed later. 85224813Skarels */ 85324813Skarels struct mbuf * 85424813Skarels ip_srcroute() 85524813Skarels { 85624813Skarels register struct in_addr *p, *q; 85724813Skarels register struct mbuf *m; 85824813Skarels 85924813Skarels if (ip_nhops == 0) 86024813Skarels return ((struct mbuf *)0); 86131201Skarels m = m_get(M_DONTWAIT, MT_SOOPTS); 86231201Skarels if (m == 0) 86331201Skarels return ((struct mbuf *)0); 86424813Skarels 86536814Skarels #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 86636814Skarels 86736814Skarels /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 86836814Skarels m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 86936814Skarels OPTSIZ; 87049042Ssklower #ifdef DIAGNOSTIC 87136814Skarels if (ipprintfs) 87236814Skarels printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 87340689Skarels #endif 87436814Skarels 87524813Skarels /* 87624813Skarels * First save first hop for return route 87724813Skarels */ 87824813Skarels p = &ip_srcrt.route[ip_nhops - 1]; 87924813Skarels *(mtod(m, struct in_addr *)) = *p--; 88049042Ssklower #ifdef DIAGNOSTIC 88136814Skarels if (ipprintfs) 88249882Sbostic printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr)); 88340689Skarels #endif 88424813Skarels 88524813Skarels /* 88624813Skarels * Copy option fields and padding (nop) to mbuf. 88724813Skarels */ 88824813Skarels ip_srcrt.nop = IPOPT_NOP; 88936814Skarels ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 89036814Skarels bcopy((caddr_t)&ip_srcrt.nop, 89136814Skarels mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 89224813Skarels q = (struct in_addr *)(mtod(m, caddr_t) + 89336814Skarels sizeof(struct in_addr) + OPTSIZ); 89436814Skarels #undef OPTSIZ 89524813Skarels /* 89624813Skarels * Record return path as an IP source route, 89724813Skarels * reversing the path (pointers are now aligned). 89824813Skarels */ 89936814Skarels while (p >= ip_srcrt.route) { 90049042Ssklower #ifdef DIAGNOSTIC 90136814Skarels if (ipprintfs) 90249882Sbostic printf(" %lx", ntohl(q->s_addr)); 90340689Skarels #endif 90424813Skarels *q++ = *p--; 90536814Skarels } 90636814Skarels /* 90736814Skarels * Last hop goes to final destination. 90836814Skarels */ 90936814Skarels *q = ip_srcrt.dst; 91049042Ssklower #ifdef DIAGNOSTIC 91136814Skarels if (ipprintfs) 91249882Sbostic printf(" %lx\n", ntohl(q->s_addr)); 91340689Skarels #endif 91424813Skarels return (m); 91524813Skarels } 91624813Skarels 91724813Skarels /* 9184951Swnj * Strip out IP options, at higher 9194951Swnj * level protocol in the kernel. 9204951Swnj * Second argument is buffer to which options 9214951Swnj * will be moved, and return value is their length. 92236814Skarels * XXX should be deleted; last arg currently ignored. 9234640Swnj */ 92461335Sbostic void 92537319Skarels ip_stripoptions(m, mopt) 92637319Skarels register struct mbuf *m; 9275217Swnj struct mbuf *mopt; 9284495Swnj { 9294640Swnj register int i; 93037319Skarels struct ip *ip = mtod(m, struct ip *); 93124813Skarels register caddr_t opts; 9324640Swnj int olen; 9334640Swnj 9344640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 93524813Skarels opts = (caddr_t)(ip + 1); 9364640Swnj i = m->m_len - (sizeof (struct ip) + olen); 93724813Skarels bcopy(opts + olen, opts, (unsigned)i); 9385243Sroot m->m_len -= olen; 93937319Skarels if (m->m_flags & M_PKTHDR) 94037319Skarels m->m_pkthdr.len -= olen; 94124813Skarels ip->ip_hl = sizeof(struct ip) >> 2; 9424495Swnj } 9436583Ssam 94414670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 94524813Skarels 0, 0, 0, 0, 94640689Skarels 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 94740689Skarels EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 94824813Skarels EMSGSIZE, EHOSTUNREACH, 0, 0, 94924813Skarels 0, 0, 0, 0, 95024813Skarels ENOPROTOOPT 9516583Ssam }; 9526583Ssam 9536583Ssam /* 9546583Ssam * Forward a packet. If some error occurs return the sender 95518376Skarels * an icmp packet. Note we can't always generate a meaningful 95624813Skarels * icmp message because icmp doesn't have a large enough repertoire 9576583Ssam * of codes and types. 95826308Skarels * 95940689Skarels * If not forwarding, just drop the packet. This could be confusing 96040689Skarels * if ipforwarding was zero but some routing protocol was advancing 96140689Skarels * us as a gateway to somewhere. However, we must let the routing 96240689Skarels * protocol deal with that. 96340689Skarels * 96440689Skarels * The srcrt parameter indicates whether the packet is being forwarded 96540689Skarels * via a source route. 9666583Ssam */ 96761335Sbostic void 96840689Skarels ip_forward(m, srcrt) 96936814Skarels struct mbuf *m; 97040689Skarels int srcrt; 9716583Ssam { 97236814Skarels register struct ip *ip = mtod(m, struct ip *); 97324813Skarels register struct sockaddr_in *sin; 97440689Skarels register struct rtentry *rt; 97540689Skarels int error, type = 0, code; 97618376Skarels struct mbuf *mcopy; 97760637Smckusick n_long dest; 97857433Sandrew struct ifnet *destifp; 9796583Ssam 98060637Smckusick dest = 0; 98149042Ssklower #ifdef DIAGNOSTIC 9826583Ssam if (ipprintfs) 9836583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 9846583Ssam ip->ip_dst, ip->ip_ttl); 98540689Skarels #endif 98637319Skarels if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 98726308Skarels ipstat.ips_cantforward++; 98837319Skarels m_freem(m); 98926308Skarels return; 9906583Ssam } 99140689Skarels HTONS(ip->ip_id); 99231393Skarels if (ip->ip_ttl <= IPTTLDEC) { 99360637Smckusick icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0); 99440689Skarels return; 9956583Ssam } 9966583Ssam ip->ip_ttl -= IPTTLDEC; 9976609Ssam 99824813Skarels sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 99940689Skarels if ((rt = ipforward_rt.ro_rt) == 0 || 100024813Skarels ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 100124813Skarels if (ipforward_rt.ro_rt) { 100224813Skarels RTFREE(ipforward_rt.ro_rt); 100324813Skarels ipforward_rt.ro_rt = 0; 100424813Skarels } 100524813Skarels sin->sin_family = AF_INET; 100637319Skarels sin->sin_len = sizeof(*sin); 100724813Skarels sin->sin_addr = ip->ip_dst; 100824813Skarels 100924813Skarels rtalloc(&ipforward_rt); 101040689Skarels if (ipforward_rt.ro_rt == 0) { 101160637Smckusick icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 101240689Skarels return; 101340689Skarels } 101440689Skarels rt = ipforward_rt.ro_rt; 101524813Skarels } 101640689Skarels 101724813Skarels /* 101840689Skarels * Save at most 64 bytes of the packet in case 101940689Skarels * we need to generate an ICMP message to the src. 102040689Skarels */ 102140689Skarels mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 102240689Skarels 102340689Skarels #ifdef GATEWAY 102440689Skarels ip_ifmatrix[rt->rt_ifp->if_index + 102540689Skarels if_index * m->m_pkthdr.rcvif->if_index]++; 102640689Skarels #endif 102740689Skarels /* 102824813Skarels * If forwarding packet using same interface that it came in on, 102924813Skarels * perhaps should send a redirect to sender to shortcut a hop. 103024813Skarels * Only send redirect if source is sending directly to us, 103124813Skarels * and if packet was not source routed (or has any options). 103230447Skarels * Also, don't send redirect if forwarding using a default route 103340689Skarels * or a route modified by a redirect. 103424813Skarels */ 103530447Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 103640689Skarels if (rt->rt_ifp == m->m_pkthdr.rcvif && 103740689Skarels (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 103840689Skarels satosin(rt_key(rt))->sin_addr.s_addr != 0 && 103940689Skarels ipsendredirects && !srcrt) { 104052554Ssklower #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 104124813Skarels u_long src = ntohl(ip->ip_src.s_addr); 104224813Skarels u_long dst = ntohl(ip->ip_dst.s_addr); 104324813Skarels 104452554Ssklower if (RTA(rt) && 104552554Ssklower (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 104640689Skarels if (rt->rt_flags & RTF_GATEWAY) 104760637Smckusick dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 104824813Skarels else 104960637Smckusick dest = ip->ip_dst.s_addr; 105058998Ssklower /* Router requirements says to only send host redirects */ 105124813Skarels type = ICMP_REDIRECT; 105258998Ssklower code = ICMP_REDIRECT_HOST; 105349042Ssklower #ifdef DIAGNOSTIC 105424813Skarels if (ipprintfs) 105560637Smckusick printf("redirect (%d) to %lx\n", code, (u_long)dest); 105640689Skarels #endif 105724813Skarels } 105824813Skarels } 105924813Skarels 106058998Ssklower error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING 106158998Ssklower #ifdef DIRECTED_BROADCAST 106258998Ssklower | IP_ALLOWBROADCAST 106358998Ssklower #endif 106458998Ssklower , 0); 106524813Skarels if (error) 106624813Skarels ipstat.ips_cantforward++; 106724813Skarels else { 106821117Skarels ipstat.ips_forward++; 106940689Skarels if (type) 107040689Skarels ipstat.ips_redirectsent++; 107140689Skarels else { 107240689Skarels if (mcopy) 107340689Skarels m_freem(mcopy); 107440689Skarels return; 107540689Skarels } 10766609Ssam } 107711540Ssam if (mcopy == NULL) 107811540Ssam return; 107957433Sandrew destifp = NULL; 108057433Sandrew 10816609Ssam switch (error) { 10826609Ssam 108324813Skarels case 0: /* forwarded, but need redirect */ 108440689Skarels /* type, code set above */ 108524813Skarels break; 108624813Skarels 108740689Skarels case ENETUNREACH: /* shouldn't happen, checked above */ 108840689Skarels case EHOSTUNREACH: 10896609Ssam case ENETDOWN: 109040689Skarels case EHOSTDOWN: 109140689Skarels default: 109240689Skarels type = ICMP_UNREACH; 109340689Skarels code = ICMP_UNREACH_HOST; 10946609Ssam break; 10956609Ssam 10966609Ssam case EMSGSIZE: 109740689Skarels type = ICMP_UNREACH; 10986583Ssam code = ICMP_UNREACH_NEEDFRAG; 109957433Sandrew if (ipforward_rt.ro_rt) 110057433Sandrew destifp = ipforward_rt.ro_rt->rt_ifp; 110139185Ssklower ipstat.ips_cantfrag++; 11026609Ssam break; 11036609Ssam 11046609Ssam case ENOBUFS: 11056609Ssam type = ICMP_SOURCEQUENCH; 110637319Skarels code = 0; 11076609Ssam break; 11086609Ssam } 110957433Sandrew icmp_error(mcopy, type, code, dest, destifp); 11106583Ssam } 111159136Smckusick 111261335Sbostic int 111359136Smckusick ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 111459136Smckusick int *name; 111559136Smckusick u_int namelen; 111659136Smckusick void *oldp; 111759136Smckusick size_t *oldlenp; 111859136Smckusick void *newp; 111959136Smckusick size_t newlen; 112059136Smckusick { 112159136Smckusick extern int ip_ttl; 112259136Smckusick 112359136Smckusick /* all sysctl names at this level are terminal */ 112459136Smckusick if (namelen != 1) 112559136Smckusick return (ENOTDIR); 112659136Smckusick 112759136Smckusick switch (name[0]) { 112859136Smckusick case IPCTL_FORWARDING: 112959136Smckusick return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding)); 113059136Smckusick case IPCTL_SENDREDIRECTS: 113159136Smckusick return (sysctl_int(oldp, oldlenp, newp, newlen, 113259136Smckusick &ipsendredirects)); 113359136Smckusick case IPCTL_DEFTTL: 113459136Smckusick return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl)); 113559136Smckusick #ifdef notyet 113659136Smckusick case IPCTL_DEFMTU: 113759136Smckusick return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu)); 113859136Smckusick #endif 113959136Smckusick default: 114059136Smckusick return (EOPNOTSUPP); 114159136Smckusick } 114259136Smckusick /* NOTREACHED */ 114359136Smckusick } 1144