123184Smckusick /* 2*57968Sandrew * Copyright (c) 1982, 1986, 1988, 1993 Regents of the University of California. 332787Sbostic * All rights reserved. 423184Smckusick * 544480Sbostic * %sccs.include.redist.c% 632787Sbostic * 7*57968Sandrew * @(#)ip_input.c 7.25 (Berkeley) 02/12/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; 4449042Ssklower #ifdef DIAGNOSTIC 4540689Skarels int ipprintfs = 0; 4640689Skarels #endif 4736814Skarels 4849042Ssklower extern struct domain inetdomain; 4949042Ssklower extern struct protosw inetsw[]; 504898Swnj u_char ip_protox[IPPROTO_MAX]; 516210Swnj int ipqmaxlen = IFQ_MAXLEN; 5218376Skarels struct in_ifaddr *in_ifaddr; /* first inet address */ 534898Swnj 544801Swnj /* 5524813Skarels * We need to save the IP options in case a protocol wants to respond 5624813Skarels * to an incoming packet over the same route if the packet got here 5724813Skarels * using IP source routing. This allows connection establishment and 5824813Skarels * maintenance when the remote end is on a network that is not known 5924813Skarels * to us. 6024813Skarels */ 6124813Skarels int ip_nhops = 0; 6224813Skarels static struct ip_srcrt { 6336814Skarels struct in_addr dst; /* final destination */ 6424813Skarels char nop; /* one NOP to align */ 6524813Skarels char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 6636814Skarels struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 6724813Skarels } ip_srcrt; 6824813Skarels 6940689Skarels #ifdef GATEWAY 7040689Skarels extern int if_index; 7140689Skarels u_long *ip_ifmatrix; 7240689Skarels #endif 7340689Skarels 7424813Skarels /* 755172Swnj * IP initialization: fill in IP protocol switch table. 765161Swnj * All protocols not implemented in kernel go to raw IP protocol handler. 774801Swnj */ 784801Swnj ip_init() 794801Swnj { 804898Swnj register struct protosw *pr; 814898Swnj register int i; 824495Swnj 8324813Skarels pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 844898Swnj if (pr == 0) 854898Swnj panic("ip_init"); 864898Swnj for (i = 0; i < IPPROTO_MAX; i++) 879030Sroot ip_protox[i] = pr - inetsw; 889030Sroot for (pr = inetdomain.dom_protosw; 8917551Skarels pr < inetdomain.dom_protoswNPROTOSW; pr++) 9016990Skarels if (pr->pr_domain->dom_family == PF_INET && 914898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 929030Sroot ip_protox[pr->pr_protocol] = pr - inetsw; 934801Swnj ipq.next = ipq.prev = &ipq; 948172Sroot ip_id = time.tv_sec & 0xffff; 956210Swnj ipintrq.ifq_maxlen = ipqmaxlen; 9640689Skarels #ifdef GATEWAY 9740689Skarels i = (if_index + 1) * (if_index + 1) * sizeof (u_long); 9850425Smckusick ip_ifmatrix = (u_long *) malloc(i, M_RTABLE, M_WAITOK); 9950425Smckusick bzero((char *)ip_ifmatrix, i); 10040689Skarels #endif 1014801Swnj } 1024801Swnj 1034640Swnj struct ip *ip_reass(); 10437319Skarels struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 10524813Skarels struct route ipforward_rt; 1064640Swnj 1074640Swnj /* 1084640Swnj * Ip input routine. Checksum and byte swap header. If fragmented 10940689Skarels * try to reassemble. Process options. Pass to next level. 1104640Swnj */ 1115084Swnj ipintr() 1124495Swnj { 1134923Swnj register struct ip *ip; 1145084Swnj register struct mbuf *m; 1154495Swnj register struct ipq *fp; 11618376Skarels register struct in_ifaddr *ia; 1175084Swnj int hlen, s; 1184495Swnj 1195084Swnj next: 1204640Swnj /* 1215084Swnj * Get next datagram off input queue and get IP header 1225084Swnj * in first mbuf. 1234640Swnj */ 1245084Swnj s = splimp(); 12537319Skarels IF_DEQUEUE(&ipintrq, m); 1265084Swnj splx(s); 1275218Swnj if (m == 0) 1285084Swnj return; 12944967Skarels #ifdef DIAGNOSTIC 13044967Skarels if ((m->m_flags & M_PKTHDR) == 0) 13144967Skarels panic("ipintr no HDR"); 13244967Skarels #endif 13326001Skarels /* 13426001Skarels * If no IP addresses have been set yet but the interfaces 13526001Skarels * are receiving, can't do anything with incoming packets yet. 13626001Skarels */ 13726001Skarels if (in_ifaddr == NULL) 13826001Skarels goto bad; 13925920Skarels ipstat.ips_total++; 14040689Skarels if (m->m_len < sizeof (struct ip) && 14111232Ssam (m = m_pullup(m, sizeof (struct ip))) == 0) { 14211232Ssam ipstat.ips_toosmall++; 14311232Ssam goto next; 14411232Ssam } 1454640Swnj ip = mtod(m, struct ip *); 146*57968Sandrew if (ip->ip_v != IPVERSION) { 147*57968Sandrew ipstat.ips_badvers++; 148*57968Sandrew goto bad; 149*57968Sandrew } 15018376Skarels hlen = ip->ip_hl << 2; 15124813Skarels if (hlen < sizeof(struct ip)) { /* minimum header length */ 15218376Skarels ipstat.ips_badhlen++; 15321117Skarels goto bad; 15418376Skarels } 15518376Skarels if (hlen > m->m_len) { 15611232Ssam if ((m = m_pullup(m, hlen)) == 0) { 15711232Ssam ipstat.ips_badhlen++; 15811232Ssam goto next; 15911232Ssam } 1605161Swnj ip = mtod(m, struct ip *); 1615161Swnj } 16237319Skarels if (ip->ip_sum = in_cksum(m, hlen)) { 16337319Skarels ipstat.ips_badsum++; 16437319Skarels goto bad; 16537319Skarels } 1664951Swnj 1674951Swnj /* 1684951Swnj * Convert fields to host representation. 1694951Swnj */ 17040689Skarels NTOHS(ip->ip_len); 17111232Ssam if (ip->ip_len < hlen) { 17211232Ssam ipstat.ips_badlen++; 17311232Ssam goto bad; 17411232Ssam } 17540689Skarels NTOHS(ip->ip_id); 17640689Skarels NTOHS(ip->ip_off); 1774495Swnj 1784543Swnj /* 1794640Swnj * Check that the amount of data in the buffers 1804640Swnj * is as at least much as the IP header would have us expect. 1814640Swnj * Trim mbufs if longer than we expect. 1824640Swnj * Drop packet if shorter than we expect. 1834543Swnj */ 18437319Skarels if (m->m_pkthdr.len < ip->ip_len) { 18537319Skarels ipstat.ips_tooshort++; 18637319Skarels goto bad; 1876088Sroot } 18837319Skarels if (m->m_pkthdr.len > ip->ip_len) { 18937319Skarels if (m->m_len == m->m_pkthdr.len) { 19037319Skarels m->m_len = ip->ip_len; 19137319Skarels m->m_pkthdr.len = ip->ip_len; 19237319Skarels } else 19337319Skarels m_adj(m, ip->ip_len - m->m_pkthdr.len); 1944495Swnj } 1954495Swnj 1964640Swnj /* 1974640Swnj * Process options and, if not destined for us, 1986583Ssam * ship it on. ip_dooptions returns 1 when an 1996583Ssam * error was detected (causing an icmp message 20021117Skarels * to be sent and the original packet to be freed). 2014640Swnj */ 20224813Skarels ip_nhops = 0; /* for source routed packets */ 20337319Skarels if (hlen > sizeof (struct ip) && ip_dooptions(m)) 2046583Ssam goto next; 2056210Swnj 2066338Ssam /* 20718376Skarels * Check our list of addresses, to see if the packet is for us. 2086338Ssam */ 20918376Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) { 21018376Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 2116338Ssam 21218376Skarels if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 21324813Skarels goto ours; 21425195Skarels if ( 21525195Skarels #ifdef DIRECTED_BROADCAST 21637319Skarels ia->ia_ifp == m->m_pkthdr.rcvif && 21725195Skarels #endif 21825195Skarels (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 21926247Skarels u_long t; 22025195Skarels 22125195Skarels if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 22225195Skarels ip->ip_dst.s_addr) 22325195Skarels goto ours; 22425195Skarels if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 22525195Skarels goto ours; 22625195Skarels /* 22725195Skarels * Look for all-0's host part (old broadcast addr), 22825195Skarels * either for subnet or net. 22925195Skarels */ 23026247Skarels t = ntohl(ip->ip_dst.s_addr); 23126247Skarels if (t == ia->ia_subnet) 23225195Skarels goto ours; 23326247Skarels if (t == ia->ia_net) 23425195Skarels goto ours; 23525195Skarels } 2366338Ssam } 23754716Ssklower if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 23854716Ssklower struct in_multi *inm; 23954716Ssklower #ifdef MROUTING 24054716Ssklower extern struct socket *ip_mrouter; 24154716Ssklower 24254716Ssklower if (ip_mrouter) { 24354716Ssklower /* 24454716Ssklower * If we are acting as a multicast router, all 24554716Ssklower * incoming multicast packets are passed to the 24654716Ssklower * kernel-level multicast forwarding function. 24754716Ssklower * The packet is returned (relatively) intact; if 24854716Ssklower * ip_mforward() returns a non-zero value, the packet 24954716Ssklower * must be discarded, else it may be accepted below. 25054716Ssklower * 25154716Ssklower * (The IP ident field is put in the same byte order 25254716Ssklower * as expected when ip_mforward() is called from 25354716Ssklower * ip_output().) 25454716Ssklower */ 25554716Ssklower ip->ip_id = htons(ip->ip_id); 25654716Ssklower if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) { 25757433Sandrew ipstat.ips_cantforward++; 25854716Ssklower m_freem(m); 25954716Ssklower goto next; 26054716Ssklower } 26154716Ssklower ip->ip_id = ntohs(ip->ip_id); 26254716Ssklower 26354716Ssklower /* 26454716Ssklower * The process-level routing demon needs to receive 26554716Ssklower * all multicast IGMP packets, whether or not this 26654716Ssklower * host belongs to their destination groups. 26754716Ssklower */ 26854716Ssklower if (ip->ip_p == IPPROTO_IGMP) 26954716Ssklower goto ours; 27057433Sandrew ipstat.ips_forward++; 27154716Ssklower } 27254716Ssklower #endif 27354716Ssklower /* 27454716Ssklower * See if we belong to the destination multicast group on the 27554716Ssklower * arrival interface. 27654716Ssklower */ 27754716Ssklower IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 27854716Ssklower if (inm == NULL) { 27957433Sandrew ipstat.ips_cantforward++; 28054716Ssklower m_freem(m); 28154716Ssklower goto next; 28254716Ssklower } 28354716Ssklower goto ours; 28454716Ssklower } 28524813Skarels if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 28624813Skarels goto ours; 28724813Skarels if (ip->ip_dst.s_addr == INADDR_ANY) 28824813Skarels goto ours; 2894495Swnj 2904640Swnj /* 29124813Skarels * Not for us; forward if possible and desirable. 29224813Skarels */ 29340689Skarels if (ipforwarding == 0) { 29436814Skarels ipstat.ips_cantforward++; 29536814Skarels m_freem(m); 29636814Skarels } else 29740689Skarels ip_forward(m, 0); 29824813Skarels goto next; 29924813Skarels 30024813Skarels ours: 30124813Skarels /* 30233743Skarels * If offset or IP_MF are set, must reassemble. 30333743Skarels * Otherwise, nothing need be done. 30433743Skarels * (We could look in the reassembly queue to see 30533743Skarels * if the packet was previously fragmented, 30633743Skarels * but it's not worth the time; just let them time out.) 3074640Swnj */ 30833743Skarels if (ip->ip_off &~ IP_DF) { 30940689Skarels if (m->m_flags & M_EXT) { /* XXX */ 31040689Skarels if ((m = m_pullup(m, sizeof (struct ip))) == 0) { 31140689Skarels ipstat.ips_toosmall++; 31240689Skarels goto next; 31340689Skarels } 31440689Skarels ip = mtod(m, struct ip *); 31540689Skarels } 31633743Skarels /* 31733743Skarels * Look for queue of fragments 31833743Skarels * of this datagram. 31933743Skarels */ 32033743Skarels for (fp = ipq.next; fp != &ipq; fp = fp->next) 32133743Skarels if (ip->ip_id == fp->ipq_id && 32233743Skarels ip->ip_src.s_addr == fp->ipq_src.s_addr && 32333743Skarels ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 32433743Skarels ip->ip_p == fp->ipq_p) 32533743Skarels goto found; 32633743Skarels fp = 0; 3274640Swnj found: 3284495Swnj 32933743Skarels /* 33033743Skarels * Adjust ip_len to not reflect header, 33133743Skarels * set ip_mff if more fragments are expected, 33233743Skarels * convert offset of this to bytes. 33333743Skarels */ 33433743Skarels ip->ip_len -= hlen; 335*57968Sandrew ((struct ipasfrag *)ip)->ipf_mff &= ~1; 33633743Skarels if (ip->ip_off & IP_MF) 337*57968Sandrew ((struct ipasfrag *)ip)->ipf_mff |= 1; 33833743Skarels ip->ip_off <<= 3; 3394495Swnj 34033743Skarels /* 34133743Skarels * If datagram marked as having more fragments 34233743Skarels * or if this is not the first fragment, 34333743Skarels * attempt reassembly; if it succeeds, proceed. 34433743Skarels */ 345*57968Sandrew if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) { 34633743Skarels ipstat.ips_fragments++; 34733743Skarels ip = ip_reass((struct ipasfrag *)ip, fp); 34833743Skarels if (ip == 0) 34933743Skarels goto next; 35057433Sandrew ipstat.ips_reassembled++; 35133743Skarels m = dtom(ip); 35233743Skarels } else 35333743Skarels if (fp) 35433743Skarels ip_freef(fp); 3554640Swnj } else 35633743Skarels ip->ip_len -= hlen; 3574951Swnj 3584951Swnj /* 3594951Swnj * Switch out to protocol's input routine. 3604951Swnj */ 36139185Ssklower ipstat.ips_delivered++; 36237319Skarels (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 3635084Swnj goto next; 3644951Swnj bad: 3654951Swnj m_freem(m); 3665084Swnj goto next; 3674640Swnj } 3684495Swnj 3694640Swnj /* 3704640Swnj * Take incoming datagram fragment and try to 3714951Swnj * reassemble it into whole datagram. If a chain for 3724640Swnj * reassembly of this datagram already exists, then it 3734640Swnj * is given as fp; otherwise have to make a chain. 3744640Swnj */ 3754640Swnj struct ip * 3764640Swnj ip_reass(ip, fp) 3774898Swnj register struct ipasfrag *ip; 3784640Swnj register struct ipq *fp; 3794640Swnj { 3804640Swnj register struct mbuf *m = dtom(ip); 3814898Swnj register struct ipasfrag *q; 3824640Swnj struct mbuf *t; 3834640Swnj int hlen = ip->ip_hl << 2; 3844640Swnj int i, next; 3854543Swnj 3864640Swnj /* 3874640Swnj * Presence of header sizes in mbufs 3884640Swnj * would confuse code below. 3894640Swnj */ 39037319Skarels m->m_data += hlen; 3914640Swnj m->m_len -= hlen; 3924495Swnj 3934640Swnj /* 3944640Swnj * If first fragment to arrive, create a reassembly queue. 3954640Swnj */ 3964640Swnj if (fp == 0) { 39731201Skarels if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 3984640Swnj goto dropfrag; 3994640Swnj fp = mtod(t, struct ipq *); 4004640Swnj insque(fp, &ipq); 4014640Swnj fp->ipq_ttl = IPFRAGTTL; 4024640Swnj fp->ipq_p = ip->ip_p; 4034640Swnj fp->ipq_id = ip->ip_id; 4044898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 4054898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 4064898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 4075161Swnj q = (struct ipasfrag *)fp; 4085161Swnj goto insert; 4094640Swnj } 4104495Swnj 4114640Swnj /* 4124640Swnj * Find a segment which begins after this one does. 4134640Swnj */ 4144898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 4154640Swnj if (q->ip_off > ip->ip_off) 4164640Swnj break; 4174495Swnj 4184640Swnj /* 4194640Swnj * If there is a preceding segment, it may provide some of 4204640Swnj * our data already. If so, drop the data from the incoming 4214640Swnj * segment. If it provides all of our data, drop us. 4224640Swnj */ 4234898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 4244898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 4254640Swnj if (i > 0) { 4264640Swnj if (i >= ip->ip_len) 4274640Swnj goto dropfrag; 4284640Swnj m_adj(dtom(ip), i); 4294640Swnj ip->ip_off += i; 4304640Swnj ip->ip_len -= i; 4314640Swnj } 4324640Swnj } 4334543Swnj 4344640Swnj /* 4354640Swnj * While we overlap succeeding segments trim them or, 4364640Swnj * if they are completely covered, dequeue them. 4374640Swnj */ 4384898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 4394640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 4404640Swnj if (i < q->ip_len) { 4414640Swnj q->ip_len -= i; 4426256Sroot q->ip_off += i; 4434640Swnj m_adj(dtom(q), i); 4444640Swnj break; 4454495Swnj } 4464898Swnj q = q->ipf_next; 4474898Swnj m_freem(dtom(q->ipf_prev)); 4484898Swnj ip_deq(q->ipf_prev); 4494543Swnj } 4504495Swnj 4515161Swnj insert: 4524640Swnj /* 4534640Swnj * Stick new segment in its place; 4544640Swnj * check for complete reassembly. 4554640Swnj */ 4564898Swnj ip_enq(ip, q->ipf_prev); 4574640Swnj next = 0; 4584898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 4594640Swnj if (q->ip_off != next) 4604640Swnj return (0); 4614640Swnj next += q->ip_len; 4624640Swnj } 463*57968Sandrew if (q->ipf_prev->ipf_mff & 1) 4644640Swnj return (0); 4654495Swnj 4664640Swnj /* 4674640Swnj * Reassembly is complete; concatenate fragments. 4684640Swnj */ 4694640Swnj q = fp->ipq_next; 4704640Swnj m = dtom(q); 4714640Swnj t = m->m_next; 4724640Swnj m->m_next = 0; 4734640Swnj m_cat(m, t); 4746298Swnj q = q->ipf_next; 4756298Swnj while (q != (struct ipasfrag *)fp) { 4766298Swnj t = dtom(q); 4776298Swnj q = q->ipf_next; 4786298Swnj m_cat(m, t); 4796298Swnj } 4804495Swnj 4814640Swnj /* 4824640Swnj * Create header for new ip packet by 4834640Swnj * modifying header of first packet; 4844640Swnj * dequeue and discard fragment reassembly header. 4854640Swnj * Make header visible. 4864640Swnj */ 4874640Swnj ip = fp->ipq_next; 4884640Swnj ip->ip_len = next; 489*57968Sandrew ip->ipf_mff &= ~1; 4904898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 4914898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 4924640Swnj remque(fp); 4934907Swnj (void) m_free(dtom(fp)); 4944640Swnj m = dtom(ip); 49524813Skarels m->m_len += (ip->ip_hl << 2); 49637319Skarels m->m_data -= (ip->ip_hl << 2); 49749042Ssklower /* some debugging cruft by sklower, below, will go away soon */ 49849042Ssklower if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 49949042Ssklower register int plen = 0; 50049042Ssklower for (t = m; m; m = m->m_next) 50149042Ssklower plen += m->m_len; 50249042Ssklower t->m_pkthdr.len = plen; 50349042Ssklower } 5044898Swnj return ((struct ip *)ip); 5054495Swnj 5064640Swnj dropfrag: 50724813Skarels ipstat.ips_fragdropped++; 5084640Swnj m_freem(m); 5094640Swnj return (0); 5104495Swnj } 5114495Swnj 5124640Swnj /* 5134640Swnj * Free a fragment reassembly header and all 5144640Swnj * associated datagrams. 5154640Swnj */ 5164640Swnj ip_freef(fp) 5174640Swnj struct ipq *fp; 5184495Swnj { 51910735Ssam register struct ipasfrag *q, *p; 5204495Swnj 52110735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 52210735Ssam p = q->ipf_next; 52310735Ssam ip_deq(q); 5244640Swnj m_freem(dtom(q)); 52510735Ssam } 52610735Ssam remque(fp); 52710735Ssam (void) m_free(dtom(fp)); 5284495Swnj } 5294495Swnj 5304640Swnj /* 5314640Swnj * Put an ip fragment on a reassembly chain. 5324640Swnj * Like insque, but pointers in middle of structure. 5334640Swnj */ 5344640Swnj ip_enq(p, prev) 5354898Swnj register struct ipasfrag *p, *prev; 5364495Swnj { 5374951Swnj 5384898Swnj p->ipf_prev = prev; 5394898Swnj p->ipf_next = prev->ipf_next; 5404898Swnj prev->ipf_next->ipf_prev = p; 5414898Swnj prev->ipf_next = p; 5424495Swnj } 5434495Swnj 5444640Swnj /* 5454640Swnj * To ip_enq as remque is to insque. 5464640Swnj */ 5474640Swnj ip_deq(p) 5484898Swnj register struct ipasfrag *p; 5494640Swnj { 5504951Swnj 5514898Swnj p->ipf_prev->ipf_next = p->ipf_next; 5524898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 5534495Swnj } 5544495Swnj 5554640Swnj /* 5564640Swnj * IP timer processing; 5574640Swnj * if a timer expires on a reassembly 5584640Swnj * queue, discard it. 5594640Swnj */ 5604801Swnj ip_slowtimo() 5614495Swnj { 5624495Swnj register struct ipq *fp; 5634640Swnj int s = splnet(); 5644951Swnj 5655243Sroot fp = ipq.next; 5665243Sroot if (fp == 0) { 5675243Sroot splx(s); 5685243Sroot return; 5695243Sroot } 57010735Ssam while (fp != &ipq) { 57110735Ssam --fp->ipq_ttl; 57210735Ssam fp = fp->next; 57324813Skarels if (fp->prev->ipq_ttl == 0) { 57424813Skarels ipstat.ips_fragtimeout++; 57510735Ssam ip_freef(fp->prev); 57624813Skarels } 57710735Ssam } 5784640Swnj splx(s); 5794495Swnj } 5804495Swnj 5814951Swnj /* 5824951Swnj * Drain off all datagram fragments. 5834951Swnj */ 5844801Swnj ip_drain() 5854801Swnj { 5864801Swnj 58724813Skarels while (ipq.next != &ipq) { 58824813Skarels ipstat.ips_fragdropped++; 58910735Ssam ip_freef(ipq.next); 59024813Skarels } 5914801Swnj } 5924923Swnj 59330925Skarels extern struct in_ifaddr *ifptoia(); 59424813Skarels struct in_ifaddr *ip_rtaddr(); 59524813Skarels 5964640Swnj /* 5974640Swnj * Do option processing on a datagram, 59840689Skarels * possibly discarding it if bad options are encountered, 59940689Skarels * or forwarding it if source-routed. 60040689Skarels * Returns 1 if packet has been forwarded/freed, 60140689Skarels * 0 if the packet should be processed further. 6024640Swnj */ 60337319Skarels ip_dooptions(m) 60436814Skarels struct mbuf *m; 6054495Swnj { 60636814Skarels register struct ip *ip = mtod(m, struct ip *); 6074640Swnj register u_char *cp; 60824813Skarels register struct ip_timestamp *ipt; 60924813Skarels register struct in_ifaddr *ia; 61036814Skarels int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 6114923Swnj struct in_addr *sin; 61224813Skarels n_time ntime; 6134495Swnj 6144640Swnj cp = (u_char *)(ip + 1); 6154640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 6164640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 61724813Skarels opt = cp[IPOPT_OPTVAL]; 6184640Swnj if (opt == IPOPT_EOL) 6194640Swnj break; 6204640Swnj if (opt == IPOPT_NOP) 6214640Swnj optlen = 1; 62216392Ssam else { 62324813Skarels optlen = cp[IPOPT_OLEN]; 62424813Skarels if (optlen <= 0 || optlen > cnt) { 62524813Skarels code = &cp[IPOPT_OLEN] - (u_char *)ip; 62617551Skarels goto bad; 62724813Skarels } 62816392Ssam } 6294640Swnj switch (opt) { 6304495Swnj 6314640Swnj default: 6324640Swnj break; 6334495Swnj 6344951Swnj /* 6354951Swnj * Source routing with record. 6364951Swnj * Find interface with current destination address. 6374951Swnj * If none on this machine then drop if strictly routed, 6384951Swnj * or do nothing if loosely routed. 6394951Swnj * Record interface address and bring up next address 6404951Swnj * component. If strictly routed make sure next 64140689Skarels * address is on directly accessible net. 6424951Swnj */ 6434640Swnj case IPOPT_LSRR: 6447508Sroot case IPOPT_SSRR: 64524813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 64624813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 64724813Skarels goto bad; 64824813Skarels } 64924813Skarels ipaddr.sin_addr = ip->ip_dst; 65024813Skarels ia = (struct in_ifaddr *) 65124813Skarels ifa_ifwithaddr((struct sockaddr *)&ipaddr); 65224813Skarels if (ia == 0) { 65324813Skarels if (opt == IPOPT_SSRR) { 65424813Skarels type = ICMP_UNREACH; 65524813Skarels code = ICMP_UNREACH_SRCFAIL; 6564951Swnj goto bad; 65724813Skarels } 65824813Skarels /* 65924813Skarels * Loose routing, and not at next destination 66024813Skarels * yet; nothing to do except forward. 66124813Skarels */ 6624951Swnj break; 6634640Swnj } 66424813Skarels off--; /* 0 origin */ 66524813Skarels if (off > optlen - sizeof(struct in_addr)) { 66624813Skarels /* 66724813Skarels * End of source route. Should be for us. 66824813Skarels */ 66924813Skarels save_rte(cp, ip->ip_src); 6704951Swnj break; 67124813Skarels } 67224813Skarels /* 67324813Skarels * locate outgoing interface 67424813Skarels */ 67526384Skarels bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 67624813Skarels sizeof(ipaddr.sin_addr)); 67740689Skarels if (opt == IPOPT_SSRR) { 67840689Skarels #define INA struct in_ifaddr * 67940689Skarels #define SA struct sockaddr * 68040689Skarels if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 68140689Skarels ia = in_iaonnetof(in_netof(ipaddr.sin_addr)); 68240689Skarels } else 68340689Skarels ia = ip_rtaddr(ipaddr.sin_addr); 68440689Skarels if (ia == 0) { 68524813Skarels type = ICMP_UNREACH; 68624813Skarels code = ICMP_UNREACH_SRCFAIL; 6874951Swnj goto bad; 68824813Skarels } 68924813Skarels ip->ip_dst = ipaddr.sin_addr; 69026384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 69126384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 69224813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 69357433Sandrew /* 69457433Sandrew * Let ip_intr's mcast routing check handle mcast pkts 69557433Sandrew */ 69657433Sandrew forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 6974640Swnj break; 6984495Swnj 69924813Skarels case IPOPT_RR: 70024813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 70124813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 70224813Skarels goto bad; 70324813Skarels } 70424813Skarels /* 70524813Skarels * If no space remains, ignore. 70624813Skarels */ 70724813Skarels off--; /* 0 origin */ 70824813Skarels if (off > optlen - sizeof(struct in_addr)) 70924813Skarels break; 71031393Skarels bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 71124813Skarels sizeof(ipaddr.sin_addr)); 71224813Skarels /* 71337319Skarels * locate outgoing interface; if we're the destination, 71437319Skarels * use the incoming interface (should be same). 71524813Skarels */ 71640689Skarels if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 71737319Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 71824813Skarels type = ICMP_UNREACH; 71932113Skarels code = ICMP_UNREACH_HOST; 72024813Skarels goto bad; 72124813Skarels } 72226384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 72326384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 72424813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 72524813Skarels break; 72624813Skarels 7274640Swnj case IPOPT_TS: 7286583Ssam code = cp - (u_char *)ip; 7294801Swnj ipt = (struct ip_timestamp *)cp; 7304801Swnj if (ipt->ipt_len < 5) 7314640Swnj goto bad; 7324801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 7334801Swnj if (++ipt->ipt_oflw == 0) 7344640Swnj goto bad; 7354495Swnj break; 7364640Swnj } 73730925Skarels sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 7384801Swnj switch (ipt->ipt_flg) { 7394495Swnj 7404640Swnj case IPOPT_TS_TSONLY: 7414640Swnj break; 7424640Swnj 7434640Swnj case IPOPT_TS_TSANDADDR: 74424813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 74524813Skarels sizeof(struct in_addr) > ipt->ipt_len) 7464640Swnj goto bad; 74737319Skarels ia = ifptoia(m->m_pkthdr.rcvif); 74830925Skarels bcopy((caddr_t)&IA_SIN(ia)->sin_addr, 74924813Skarels (caddr_t)sin, sizeof(struct in_addr)); 75030925Skarels ipt->ipt_ptr += sizeof(struct in_addr); 7514640Swnj break; 7524640Swnj 7534640Swnj case IPOPT_TS_PRESPEC: 75430925Skarels if (ipt->ipt_ptr + sizeof(n_time) + 75530925Skarels sizeof(struct in_addr) > ipt->ipt_len) 75630925Skarels goto bad; 75724813Skarels bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 75824813Skarels sizeof(struct in_addr)); 75940689Skarels if (ifa_ifwithaddr((SA)&ipaddr) == 0) 7604951Swnj continue; 76124813Skarels ipt->ipt_ptr += sizeof(struct in_addr); 7624640Swnj break; 7634640Swnj 7644495Swnj default: 7654640Swnj goto bad; 7664495Swnj } 76724813Skarels ntime = iptime(); 76830925Skarels bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 76930925Skarels sizeof(n_time)); 77024813Skarels ipt->ipt_ptr += sizeof(n_time); 7714640Swnj } 7724495Swnj } 77336814Skarels if (forward) { 77440689Skarels ip_forward(m, 1); 77536814Skarels return (1); 77657433Sandrew } 77757433Sandrew return (0); 7784640Swnj bad: 77957433Sandrew ip->ip_len -= ip->ip_hl << 2; /* XXX icmp_error adds in hdr length */ 78037319Skarels icmp_error(m, type, code); 78157433Sandrew ipstat.ips_badoptions++; 7826583Ssam return (1); 7834495Swnj } 7844495Swnj 7854640Swnj /* 78624813Skarels * Given address of next destination (final or next hop), 78724813Skarels * return internet address info of interface to be used to get there. 78824813Skarels */ 78924813Skarels struct in_ifaddr * 79024813Skarels ip_rtaddr(dst) 79124813Skarels struct in_addr dst; 79224813Skarels { 79324813Skarels register struct sockaddr_in *sin; 79424813Skarels 79524813Skarels sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 79624813Skarels 79724813Skarels if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 79824813Skarels if (ipforward_rt.ro_rt) { 79924813Skarels RTFREE(ipforward_rt.ro_rt); 80024813Skarels ipforward_rt.ro_rt = 0; 80124813Skarels } 80224813Skarels sin->sin_family = AF_INET; 80337319Skarels sin->sin_len = sizeof(*sin); 80424813Skarels sin->sin_addr = dst; 80524813Skarels 80624813Skarels rtalloc(&ipforward_rt); 80724813Skarels } 80824813Skarels if (ipforward_rt.ro_rt == 0) 80924813Skarels return ((struct in_ifaddr *)0); 81040689Skarels return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 81124813Skarels } 81224813Skarels 81324813Skarels /* 81424813Skarels * Save incoming source route for use in replies, 81524813Skarels * to be picked up later by ip_srcroute if the receiver is interested. 81624813Skarels */ 81724813Skarels save_rte(option, dst) 81826384Skarels u_char *option; 81924813Skarels struct in_addr dst; 82024813Skarels { 82126384Skarels unsigned olen; 82224813Skarels 82324813Skarels olen = option[IPOPT_OLEN]; 82449042Ssklower #ifdef DIAGNOSTIC 82536814Skarels if (ipprintfs) 82636814Skarels printf("save_rte: olen %d\n", olen); 82740689Skarels #endif 82836814Skarels if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 82924813Skarels return; 83026384Skarels bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 83124813Skarels ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 83236814Skarels ip_srcrt.dst = dst; 83324813Skarels } 83424813Skarels 83524813Skarels /* 83624813Skarels * Retrieve incoming source route for use in replies, 83724813Skarels * in the same form used by setsockopt. 83824813Skarels * The first hop is placed before the options, will be removed later. 83924813Skarels */ 84024813Skarels struct mbuf * 84124813Skarels ip_srcroute() 84224813Skarels { 84324813Skarels register struct in_addr *p, *q; 84424813Skarels register struct mbuf *m; 84524813Skarels 84624813Skarels if (ip_nhops == 0) 84724813Skarels return ((struct mbuf *)0); 84831201Skarels m = m_get(M_DONTWAIT, MT_SOOPTS); 84931201Skarels if (m == 0) 85031201Skarels return ((struct mbuf *)0); 85124813Skarels 85236814Skarels #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 85336814Skarels 85436814Skarels /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 85536814Skarels m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 85636814Skarels OPTSIZ; 85749042Ssklower #ifdef DIAGNOSTIC 85836814Skarels if (ipprintfs) 85936814Skarels printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 86040689Skarels #endif 86136814Skarels 86224813Skarels /* 86324813Skarels * First save first hop for return route 86424813Skarels */ 86524813Skarels p = &ip_srcrt.route[ip_nhops - 1]; 86624813Skarels *(mtod(m, struct in_addr *)) = *p--; 86749042Ssklower #ifdef DIAGNOSTIC 86836814Skarels if (ipprintfs) 86949882Sbostic printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr)); 87040689Skarels #endif 87124813Skarels 87224813Skarels /* 87324813Skarels * Copy option fields and padding (nop) to mbuf. 87424813Skarels */ 87524813Skarels ip_srcrt.nop = IPOPT_NOP; 87636814Skarels ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 87736814Skarels bcopy((caddr_t)&ip_srcrt.nop, 87836814Skarels mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 87924813Skarels q = (struct in_addr *)(mtod(m, caddr_t) + 88036814Skarels sizeof(struct in_addr) + OPTSIZ); 88136814Skarels #undef OPTSIZ 88224813Skarels /* 88324813Skarels * Record return path as an IP source route, 88424813Skarels * reversing the path (pointers are now aligned). 88524813Skarels */ 88636814Skarels while (p >= ip_srcrt.route) { 88749042Ssklower #ifdef DIAGNOSTIC 88836814Skarels if (ipprintfs) 88949882Sbostic printf(" %lx", ntohl(q->s_addr)); 89040689Skarels #endif 89124813Skarels *q++ = *p--; 89236814Skarels } 89336814Skarels /* 89436814Skarels * Last hop goes to final destination. 89536814Skarels */ 89636814Skarels *q = ip_srcrt.dst; 89749042Ssklower #ifdef DIAGNOSTIC 89836814Skarels if (ipprintfs) 89949882Sbostic printf(" %lx\n", ntohl(q->s_addr)); 90040689Skarels #endif 90124813Skarels return (m); 90224813Skarels } 90324813Skarels 90424813Skarels /* 9054951Swnj * Strip out IP options, at higher 9064951Swnj * level protocol in the kernel. 9074951Swnj * Second argument is buffer to which options 9084951Swnj * will be moved, and return value is their length. 90936814Skarels * XXX should be deleted; last arg currently ignored. 9104640Swnj */ 91137319Skarels ip_stripoptions(m, mopt) 91237319Skarels register struct mbuf *m; 9135217Swnj struct mbuf *mopt; 9144495Swnj { 9154640Swnj register int i; 91637319Skarels struct ip *ip = mtod(m, struct ip *); 91724813Skarels register caddr_t opts; 9184640Swnj int olen; 9194640Swnj 9204640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 92124813Skarels opts = (caddr_t)(ip + 1); 9224640Swnj i = m->m_len - (sizeof (struct ip) + olen); 92324813Skarels bcopy(opts + olen, opts, (unsigned)i); 9245243Sroot m->m_len -= olen; 92537319Skarels if (m->m_flags & M_PKTHDR) 92637319Skarels m->m_pkthdr.len -= olen; 92724813Skarels ip->ip_hl = sizeof(struct ip) >> 2; 9284495Swnj } 9296583Ssam 93014670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 93124813Skarels 0, 0, 0, 0, 93240689Skarels 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 93340689Skarels EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 93424813Skarels EMSGSIZE, EHOSTUNREACH, 0, 0, 93524813Skarels 0, 0, 0, 0, 93624813Skarels ENOPROTOOPT 9376583Ssam }; 9386583Ssam 9396583Ssam /* 9406583Ssam * Forward a packet. If some error occurs return the sender 94118376Skarels * an icmp packet. Note we can't always generate a meaningful 94224813Skarels * icmp message because icmp doesn't have a large enough repertoire 9436583Ssam * of codes and types. 94426308Skarels * 94540689Skarels * If not forwarding, just drop the packet. This could be confusing 94640689Skarels * if ipforwarding was zero but some routing protocol was advancing 94740689Skarels * us as a gateway to somewhere. However, we must let the routing 94840689Skarels * protocol deal with that. 94940689Skarels * 95040689Skarels * The srcrt parameter indicates whether the packet is being forwarded 95140689Skarels * via a source route. 9526583Ssam */ 95340689Skarels ip_forward(m, srcrt) 95436814Skarels struct mbuf *m; 95540689Skarels int srcrt; 9566583Ssam { 95736814Skarels register struct ip *ip = mtod(m, struct ip *); 95824813Skarels register struct sockaddr_in *sin; 95940689Skarels register struct rtentry *rt; 96040689Skarels int error, type = 0, code; 96118376Skarels struct mbuf *mcopy; 96224813Skarels struct in_addr dest; 96357433Sandrew struct ifnet *destifp; 9646583Ssam 96524813Skarels dest.s_addr = 0; 96649042Ssklower #ifdef DIAGNOSTIC 9676583Ssam if (ipprintfs) 9686583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 9696583Ssam ip->ip_dst, ip->ip_ttl); 97040689Skarels #endif 97137319Skarels if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 97226308Skarels ipstat.ips_cantforward++; 97337319Skarels m_freem(m); 97426308Skarels return; 9756583Ssam } 97640689Skarels HTONS(ip->ip_id); 97731393Skarels if (ip->ip_ttl <= IPTTLDEC) { 97840689Skarels icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest); 97940689Skarels return; 9806583Ssam } 9816583Ssam ip->ip_ttl -= IPTTLDEC; 9826609Ssam 98324813Skarels sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 98440689Skarels if ((rt = ipforward_rt.ro_rt) == 0 || 98524813Skarels ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 98624813Skarels if (ipforward_rt.ro_rt) { 98724813Skarels RTFREE(ipforward_rt.ro_rt); 98824813Skarels ipforward_rt.ro_rt = 0; 98924813Skarels } 99024813Skarels sin->sin_family = AF_INET; 99137319Skarels sin->sin_len = sizeof(*sin); 99224813Skarels sin->sin_addr = ip->ip_dst; 99324813Skarels 99424813Skarels rtalloc(&ipforward_rt); 99540689Skarels if (ipforward_rt.ro_rt == 0) { 99640689Skarels icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest); 99740689Skarels return; 99840689Skarels } 99940689Skarels rt = ipforward_rt.ro_rt; 100024813Skarels } 100140689Skarels 100224813Skarels /* 100340689Skarels * Save at most 64 bytes of the packet in case 100440689Skarels * we need to generate an ICMP message to the src. 100540689Skarels */ 100640689Skarels mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 100740689Skarels 100840689Skarels #ifdef GATEWAY 100940689Skarels ip_ifmatrix[rt->rt_ifp->if_index + 101040689Skarels if_index * m->m_pkthdr.rcvif->if_index]++; 101140689Skarels #endif 101240689Skarels /* 101324813Skarels * If forwarding packet using same interface that it came in on, 101424813Skarels * perhaps should send a redirect to sender to shortcut a hop. 101524813Skarels * Only send redirect if source is sending directly to us, 101624813Skarels * and if packet was not source routed (or has any options). 101730447Skarels * Also, don't send redirect if forwarding using a default route 101840689Skarels * or a route modified by a redirect. 101924813Skarels */ 102030447Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 102140689Skarels if (rt->rt_ifp == m->m_pkthdr.rcvif && 102240689Skarels (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 102340689Skarels satosin(rt_key(rt))->sin_addr.s_addr != 0 && 102440689Skarels ipsendredirects && !srcrt) { 102552554Ssklower #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 102624813Skarels u_long src = ntohl(ip->ip_src.s_addr); 102724813Skarels u_long dst = ntohl(ip->ip_dst.s_addr); 102824813Skarels 102952554Ssklower if (RTA(rt) && 103052554Ssklower (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 103140689Skarels if (rt->rt_flags & RTF_GATEWAY) 103240689Skarels dest = satosin(rt->rt_gateway)->sin_addr; 103324813Skarels else 103424813Skarels dest = ip->ip_dst; 103524813Skarels /* 103624813Skarels * If the destination is reached by a route to host, 103727145Skarels * is on a subnet of a local net, or is directly 103827145Skarels * on the attached net (!), use host redirect. 103924813Skarels * (We may be the correct first hop for other subnets.) 104024813Skarels */ 104124813Skarels type = ICMP_REDIRECT; 104240689Skarels if ((rt->rt_flags & RTF_HOST) || 104340689Skarels (rt->rt_flags & RTF_GATEWAY) == 0) 104440689Skarels code = ICMP_REDIRECT_HOST; 104540689Skarels else if (RTA(rt)->ia_subnetmask != RTA(rt)->ia_netmask && 104640689Skarels (dst & RTA(rt)->ia_netmask) == RTA(rt)->ia_net) 104740689Skarels code = ICMP_REDIRECT_HOST; 104840689Skarels else 104940689Skarels code = ICMP_REDIRECT_NET; 105049042Ssklower #ifdef DIAGNOSTIC 105124813Skarels if (ipprintfs) 105240689Skarels printf("redirect (%d) to %x\n", code, dest.s_addr); 105340689Skarels #endif 105424813Skarels } 105524813Skarels } 105624813Skarels 105757433Sandrew error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING, 0); 105824813Skarels if (error) 105924813Skarels ipstat.ips_cantforward++; 106024813Skarels else { 106121117Skarels ipstat.ips_forward++; 106240689Skarels if (type) 106340689Skarels ipstat.ips_redirectsent++; 106440689Skarels else { 106540689Skarels if (mcopy) 106640689Skarels m_freem(mcopy); 106740689Skarels return; 106840689Skarels } 10696609Ssam } 107011540Ssam if (mcopy == NULL) 107111540Ssam return; 107257433Sandrew destifp = NULL; 107357433Sandrew 10746609Ssam switch (error) { 10756609Ssam 107624813Skarels case 0: /* forwarded, but need redirect */ 107740689Skarels /* type, code set above */ 107824813Skarels break; 107924813Skarels 108040689Skarels case ENETUNREACH: /* shouldn't happen, checked above */ 108140689Skarels case EHOSTUNREACH: 10826609Ssam case ENETDOWN: 108340689Skarels case EHOSTDOWN: 108440689Skarels default: 108540689Skarels type = ICMP_UNREACH; 108640689Skarels code = ICMP_UNREACH_HOST; 10876609Ssam break; 10886609Ssam 10896609Ssam case EMSGSIZE: 109040689Skarels type = ICMP_UNREACH; 10916583Ssam code = ICMP_UNREACH_NEEDFRAG; 109257433Sandrew if (ipforward_rt.ro_rt) 109357433Sandrew destifp = ipforward_rt.ro_rt->rt_ifp; 109439185Ssklower ipstat.ips_cantfrag++; 10956609Ssam break; 10966609Ssam 10976609Ssam case ENOBUFS: 10986609Ssam type = ICMP_SOURCEQUENCH; 109937319Skarels code = 0; 11006609Ssam break; 11016609Ssam } 110257433Sandrew icmp_error(mcopy, type, code, dest, destifp); 11036583Ssam } 1104