123184Smckusick /* 236814Skarels * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 332787Sbostic * All rights reserved. 423184Smckusick * 544480Sbostic * %sccs.include.redist.c% 632787Sbostic * 7*49882Sbostic * @(#)ip_input.c 7.19 (Berkeley) 05/25/91 823184Smckusick */ 94571Swnj 1017060Sbloom #include "param.h" 1117060Sbloom #include "systm.h" 1237319Skarels #include "malloc.h" 1317060Sbloom #include "mbuf.h" 1417060Sbloom #include "domain.h" 1517060Sbloom #include "protosw.h" 1617060Sbloom #include "socket.h" 1717060Sbloom #include "errno.h" 1817060Sbloom #include "time.h" 1917060Sbloom #include "kernel.h" 208695Sroot 218695Sroot #include "../net/if.h" 228695Sroot #include "../net/route.h" 2310892Ssam 2417060Sbloom #include "in.h" 2540689Skarels #include "in_systm.h" 2640689Skarels #include "ip.h" 2717060Sbloom #include "in_pcb.h" 2818376Skarels #include "in_var.h" 2917060Sbloom #include "ip_var.h" 3017060Sbloom #include "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); 9840689Skarels if ((ip_ifmatrix = (u_long *) malloc(i, M_RTABLE, M_WAITOK)) == 0) 9940689Skarels panic("no memory for ip_ifmatrix"); 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 *); 14618376Skarels hlen = ip->ip_hl << 2; 14724813Skarels if (hlen < sizeof(struct ip)) { /* minimum header length */ 14818376Skarels ipstat.ips_badhlen++; 14921117Skarels goto bad; 15018376Skarels } 15118376Skarels if (hlen > m->m_len) { 15211232Ssam if ((m = m_pullup(m, hlen)) == 0) { 15311232Ssam ipstat.ips_badhlen++; 15411232Ssam goto next; 15511232Ssam } 1565161Swnj ip = mtod(m, struct ip *); 1575161Swnj } 15837319Skarels if (ip->ip_sum = in_cksum(m, hlen)) { 15937319Skarels ipstat.ips_badsum++; 16037319Skarels goto bad; 16137319Skarels } 1624951Swnj 1634951Swnj /* 1644951Swnj * Convert fields to host representation. 1654951Swnj */ 16640689Skarels NTOHS(ip->ip_len); 16711232Ssam if (ip->ip_len < hlen) { 16811232Ssam ipstat.ips_badlen++; 16911232Ssam goto bad; 17011232Ssam } 17140689Skarels NTOHS(ip->ip_id); 17240689Skarels NTOHS(ip->ip_off); 1734495Swnj 1744543Swnj /* 1754640Swnj * Check that the amount of data in the buffers 1764640Swnj * is as at least much as the IP header would have us expect. 1774640Swnj * Trim mbufs if longer than we expect. 1784640Swnj * Drop packet if shorter than we expect. 1794543Swnj */ 18037319Skarels if (m->m_pkthdr.len < ip->ip_len) { 18137319Skarels ipstat.ips_tooshort++; 18237319Skarels goto bad; 1836088Sroot } 18437319Skarels if (m->m_pkthdr.len > ip->ip_len) { 18537319Skarels if (m->m_len == m->m_pkthdr.len) { 18637319Skarels m->m_len = ip->ip_len; 18737319Skarels m->m_pkthdr.len = ip->ip_len; 18837319Skarels } else 18937319Skarels m_adj(m, ip->ip_len - m->m_pkthdr.len); 1904495Swnj } 1914495Swnj 1924640Swnj /* 1934640Swnj * Process options and, if not destined for us, 1946583Ssam * ship it on. ip_dooptions returns 1 when an 1956583Ssam * error was detected (causing an icmp message 19621117Skarels * to be sent and the original packet to be freed). 1974640Swnj */ 19824813Skarels ip_nhops = 0; /* for source routed packets */ 19937319Skarels if (hlen > sizeof (struct ip) && ip_dooptions(m)) 2006583Ssam goto next; 2016210Swnj 2026338Ssam /* 20318376Skarels * Check our list of addresses, to see if the packet is for us. 2046338Ssam */ 20518376Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) { 20618376Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 2076338Ssam 20818376Skarels if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 20924813Skarels goto ours; 21025195Skarels if ( 21125195Skarels #ifdef DIRECTED_BROADCAST 21237319Skarels ia->ia_ifp == m->m_pkthdr.rcvif && 21325195Skarels #endif 21425195Skarels (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 21526247Skarels u_long t; 21625195Skarels 21725195Skarels if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 21825195Skarels ip->ip_dst.s_addr) 21925195Skarels goto ours; 22025195Skarels if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 22125195Skarels goto ours; 22225195Skarels /* 22325195Skarels * Look for all-0's host part (old broadcast addr), 22425195Skarels * either for subnet or net. 22525195Skarels */ 22626247Skarels t = ntohl(ip->ip_dst.s_addr); 22726247Skarels if (t == ia->ia_subnet) 22825195Skarels goto ours; 22926247Skarels if (t == ia->ia_net) 23025195Skarels goto ours; 23125195Skarels } 2326338Ssam } 23324813Skarels if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 23424813Skarels goto ours; 23524813Skarels if (ip->ip_dst.s_addr == INADDR_ANY) 23624813Skarels goto ours; 2374495Swnj 2384640Swnj /* 23924813Skarels * Not for us; forward if possible and desirable. 24024813Skarels */ 24140689Skarels if (ipforwarding == 0) { 24236814Skarels ipstat.ips_cantforward++; 24336814Skarels m_freem(m); 24436814Skarels } else 24540689Skarels ip_forward(m, 0); 24624813Skarels goto next; 24724813Skarels 24824813Skarels ours: 24924813Skarels /* 25033743Skarels * If offset or IP_MF are set, must reassemble. 25133743Skarels * Otherwise, nothing need be done. 25233743Skarels * (We could look in the reassembly queue to see 25333743Skarels * if the packet was previously fragmented, 25433743Skarels * but it's not worth the time; just let them time out.) 2554640Swnj */ 25633743Skarels if (ip->ip_off &~ IP_DF) { 25740689Skarels if (m->m_flags & M_EXT) { /* XXX */ 25840689Skarels if ((m = m_pullup(m, sizeof (struct ip))) == 0) { 25940689Skarels ipstat.ips_toosmall++; 26040689Skarels goto next; 26140689Skarels } 26240689Skarels ip = mtod(m, struct ip *); 26340689Skarels } 26433743Skarels /* 26533743Skarels * Look for queue of fragments 26633743Skarels * of this datagram. 26733743Skarels */ 26833743Skarels for (fp = ipq.next; fp != &ipq; fp = fp->next) 26933743Skarels if (ip->ip_id == fp->ipq_id && 27033743Skarels ip->ip_src.s_addr == fp->ipq_src.s_addr && 27133743Skarels ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 27233743Skarels ip->ip_p == fp->ipq_p) 27333743Skarels goto found; 27433743Skarels fp = 0; 2754640Swnj found: 2764495Swnj 27733743Skarels /* 27833743Skarels * Adjust ip_len to not reflect header, 27933743Skarels * set ip_mff if more fragments are expected, 28033743Skarels * convert offset of this to bytes. 28133743Skarels */ 28233743Skarels ip->ip_len -= hlen; 28333743Skarels ((struct ipasfrag *)ip)->ipf_mff = 0; 28433743Skarels if (ip->ip_off & IP_MF) 28533743Skarels ((struct ipasfrag *)ip)->ipf_mff = 1; 28633743Skarels ip->ip_off <<= 3; 2874495Swnj 28833743Skarels /* 28933743Skarels * If datagram marked as having more fragments 29033743Skarels * or if this is not the first fragment, 29133743Skarels * attempt reassembly; if it succeeds, proceed. 29233743Skarels */ 29333743Skarels if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 29433743Skarels ipstat.ips_fragments++; 29533743Skarels ip = ip_reass((struct ipasfrag *)ip, fp); 29633743Skarels if (ip == 0) 29733743Skarels goto next; 29839185Ssklower else 29939185Ssklower ipstat.ips_reassembled++; 30033743Skarels m = dtom(ip); 30133743Skarels } else 30233743Skarels if (fp) 30333743Skarels ip_freef(fp); 3044640Swnj } else 30533743Skarels ip->ip_len -= hlen; 3064951Swnj 3074951Swnj /* 3084951Swnj * Switch out to protocol's input routine. 3094951Swnj */ 31039185Ssklower ipstat.ips_delivered++; 31137319Skarels (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 3125084Swnj goto next; 3134951Swnj bad: 3144951Swnj m_freem(m); 3155084Swnj goto next; 3164640Swnj } 3174495Swnj 3184640Swnj /* 3194640Swnj * Take incoming datagram fragment and try to 3204951Swnj * reassemble it into whole datagram. If a chain for 3214640Swnj * reassembly of this datagram already exists, then it 3224640Swnj * is given as fp; otherwise have to make a chain. 3234640Swnj */ 3244640Swnj struct ip * 3254640Swnj ip_reass(ip, fp) 3264898Swnj register struct ipasfrag *ip; 3274640Swnj register struct ipq *fp; 3284640Swnj { 3294640Swnj register struct mbuf *m = dtom(ip); 3304898Swnj register struct ipasfrag *q; 3314640Swnj struct mbuf *t; 3324640Swnj int hlen = ip->ip_hl << 2; 3334640Swnj int i, next; 3344543Swnj 3354640Swnj /* 3364640Swnj * Presence of header sizes in mbufs 3374640Swnj * would confuse code below. 3384640Swnj */ 33937319Skarels m->m_data += hlen; 3404640Swnj m->m_len -= hlen; 3414495Swnj 3424640Swnj /* 3434640Swnj * If first fragment to arrive, create a reassembly queue. 3444640Swnj */ 3454640Swnj if (fp == 0) { 34631201Skarels if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 3474640Swnj goto dropfrag; 3484640Swnj fp = mtod(t, struct ipq *); 3494640Swnj insque(fp, &ipq); 3504640Swnj fp->ipq_ttl = IPFRAGTTL; 3514640Swnj fp->ipq_p = ip->ip_p; 3524640Swnj fp->ipq_id = ip->ip_id; 3534898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 3544898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 3554898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 3565161Swnj q = (struct ipasfrag *)fp; 3575161Swnj goto insert; 3584640Swnj } 3594495Swnj 3604640Swnj /* 3614640Swnj * Find a segment which begins after this one does. 3624640Swnj */ 3634898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3644640Swnj if (q->ip_off > ip->ip_off) 3654640Swnj break; 3664495Swnj 3674640Swnj /* 3684640Swnj * If there is a preceding segment, it may provide some of 3694640Swnj * our data already. If so, drop the data from the incoming 3704640Swnj * segment. If it provides all of our data, drop us. 3714640Swnj */ 3724898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 3734898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 3744640Swnj if (i > 0) { 3754640Swnj if (i >= ip->ip_len) 3764640Swnj goto dropfrag; 3774640Swnj m_adj(dtom(ip), i); 3784640Swnj ip->ip_off += i; 3794640Swnj ip->ip_len -= i; 3804640Swnj } 3814640Swnj } 3824543Swnj 3834640Swnj /* 3844640Swnj * While we overlap succeeding segments trim them or, 3854640Swnj * if they are completely covered, dequeue them. 3864640Swnj */ 3874898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 3884640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 3894640Swnj if (i < q->ip_len) { 3904640Swnj q->ip_len -= i; 3916256Sroot q->ip_off += i; 3924640Swnj m_adj(dtom(q), i); 3934640Swnj break; 3944495Swnj } 3954898Swnj q = q->ipf_next; 3964898Swnj m_freem(dtom(q->ipf_prev)); 3974898Swnj ip_deq(q->ipf_prev); 3984543Swnj } 3994495Swnj 4005161Swnj insert: 4014640Swnj /* 4024640Swnj * Stick new segment in its place; 4034640Swnj * check for complete reassembly. 4044640Swnj */ 4054898Swnj ip_enq(ip, q->ipf_prev); 4064640Swnj next = 0; 4074898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 4084640Swnj if (q->ip_off != next) 4094640Swnj return (0); 4104640Swnj next += q->ip_len; 4114640Swnj } 4124898Swnj if (q->ipf_prev->ipf_mff) 4134640Swnj return (0); 4144495Swnj 4154640Swnj /* 4164640Swnj * Reassembly is complete; concatenate fragments. 4174640Swnj */ 4184640Swnj q = fp->ipq_next; 4194640Swnj m = dtom(q); 4204640Swnj t = m->m_next; 4214640Swnj m->m_next = 0; 4224640Swnj m_cat(m, t); 4236298Swnj q = q->ipf_next; 4246298Swnj while (q != (struct ipasfrag *)fp) { 4256298Swnj t = dtom(q); 4266298Swnj q = q->ipf_next; 4276298Swnj m_cat(m, t); 4286298Swnj } 4294495Swnj 4304640Swnj /* 4314640Swnj * Create header for new ip packet by 4324640Swnj * modifying header of first packet; 4334640Swnj * dequeue and discard fragment reassembly header. 4344640Swnj * Make header visible. 4354640Swnj */ 4364640Swnj ip = fp->ipq_next; 4374640Swnj ip->ip_len = next; 4384898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 4394898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 4404640Swnj remque(fp); 4414907Swnj (void) m_free(dtom(fp)); 4424640Swnj m = dtom(ip); 44324813Skarels m->m_len += (ip->ip_hl << 2); 44437319Skarels m->m_data -= (ip->ip_hl << 2); 44549042Ssklower /* some debugging cruft by sklower, below, will go away soon */ 44649042Ssklower if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 44749042Ssklower register int plen = 0; 44849042Ssklower for (t = m; m; m = m->m_next) 44949042Ssklower plen += m->m_len; 45049042Ssklower t->m_pkthdr.len = plen; 45149042Ssklower } 4524898Swnj return ((struct ip *)ip); 4534495Swnj 4544640Swnj dropfrag: 45524813Skarels ipstat.ips_fragdropped++; 4564640Swnj m_freem(m); 4574640Swnj return (0); 4584495Swnj } 4594495Swnj 4604640Swnj /* 4614640Swnj * Free a fragment reassembly header and all 4624640Swnj * associated datagrams. 4634640Swnj */ 4644640Swnj ip_freef(fp) 4654640Swnj struct ipq *fp; 4664495Swnj { 46710735Ssam register struct ipasfrag *q, *p; 4684495Swnj 46910735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 47010735Ssam p = q->ipf_next; 47110735Ssam ip_deq(q); 4724640Swnj m_freem(dtom(q)); 47310735Ssam } 47410735Ssam remque(fp); 47510735Ssam (void) m_free(dtom(fp)); 4764495Swnj } 4774495Swnj 4784640Swnj /* 4794640Swnj * Put an ip fragment on a reassembly chain. 4804640Swnj * Like insque, but pointers in middle of structure. 4814640Swnj */ 4824640Swnj ip_enq(p, prev) 4834898Swnj register struct ipasfrag *p, *prev; 4844495Swnj { 4854951Swnj 4864898Swnj p->ipf_prev = prev; 4874898Swnj p->ipf_next = prev->ipf_next; 4884898Swnj prev->ipf_next->ipf_prev = p; 4894898Swnj prev->ipf_next = p; 4904495Swnj } 4914495Swnj 4924640Swnj /* 4934640Swnj * To ip_enq as remque is to insque. 4944640Swnj */ 4954640Swnj ip_deq(p) 4964898Swnj register struct ipasfrag *p; 4974640Swnj { 4984951Swnj 4994898Swnj p->ipf_prev->ipf_next = p->ipf_next; 5004898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 5014495Swnj } 5024495Swnj 5034640Swnj /* 5044640Swnj * IP timer processing; 5054640Swnj * if a timer expires on a reassembly 5064640Swnj * queue, discard it. 5074640Swnj */ 5084801Swnj ip_slowtimo() 5094495Swnj { 5104495Swnj register struct ipq *fp; 5114640Swnj int s = splnet(); 5124951Swnj 5135243Sroot fp = ipq.next; 5145243Sroot if (fp == 0) { 5155243Sroot splx(s); 5165243Sroot return; 5175243Sroot } 51810735Ssam while (fp != &ipq) { 51910735Ssam --fp->ipq_ttl; 52010735Ssam fp = fp->next; 52124813Skarels if (fp->prev->ipq_ttl == 0) { 52224813Skarels ipstat.ips_fragtimeout++; 52310735Ssam ip_freef(fp->prev); 52424813Skarels } 52510735Ssam } 5264640Swnj splx(s); 5274495Swnj } 5284495Swnj 5294951Swnj /* 5304951Swnj * Drain off all datagram fragments. 5314951Swnj */ 5324801Swnj ip_drain() 5334801Swnj { 5344801Swnj 53524813Skarels while (ipq.next != &ipq) { 53624813Skarels ipstat.ips_fragdropped++; 53710735Ssam ip_freef(ipq.next); 53824813Skarels } 5394801Swnj } 5404923Swnj 54130925Skarels extern struct in_ifaddr *ifptoia(); 54224813Skarels struct in_ifaddr *ip_rtaddr(); 54324813Skarels 5444640Swnj /* 5454640Swnj * Do option processing on a datagram, 54640689Skarels * possibly discarding it if bad options are encountered, 54740689Skarels * or forwarding it if source-routed. 54840689Skarels * Returns 1 if packet has been forwarded/freed, 54940689Skarels * 0 if the packet should be processed further. 5504640Swnj */ 55137319Skarels ip_dooptions(m) 55236814Skarels struct mbuf *m; 5534495Swnj { 55436814Skarels register struct ip *ip = mtod(m, struct ip *); 5554640Swnj register u_char *cp; 55624813Skarels register struct ip_timestamp *ipt; 55724813Skarels register struct in_ifaddr *ia; 55836814Skarels int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 5594923Swnj struct in_addr *sin; 56024813Skarels n_time ntime; 5614495Swnj 5624640Swnj cp = (u_char *)(ip + 1); 5634640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 5644640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 56524813Skarels opt = cp[IPOPT_OPTVAL]; 5664640Swnj if (opt == IPOPT_EOL) 5674640Swnj break; 5684640Swnj if (opt == IPOPT_NOP) 5694640Swnj optlen = 1; 57016392Ssam else { 57124813Skarels optlen = cp[IPOPT_OLEN]; 57224813Skarels if (optlen <= 0 || optlen > cnt) { 57324813Skarels code = &cp[IPOPT_OLEN] - (u_char *)ip; 57417551Skarels goto bad; 57524813Skarels } 57616392Ssam } 5774640Swnj switch (opt) { 5784495Swnj 5794640Swnj default: 5804640Swnj break; 5814495Swnj 5824951Swnj /* 5834951Swnj * Source routing with record. 5844951Swnj * Find interface with current destination address. 5854951Swnj * If none on this machine then drop if strictly routed, 5864951Swnj * or do nothing if loosely routed. 5874951Swnj * Record interface address and bring up next address 5884951Swnj * component. If strictly routed make sure next 58940689Skarels * address is on directly accessible net. 5904951Swnj */ 5914640Swnj case IPOPT_LSRR: 5927508Sroot case IPOPT_SSRR: 59324813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 59424813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 59524813Skarels goto bad; 59624813Skarels } 59724813Skarels ipaddr.sin_addr = ip->ip_dst; 59824813Skarels ia = (struct in_ifaddr *) 59924813Skarels ifa_ifwithaddr((struct sockaddr *)&ipaddr); 60024813Skarels if (ia == 0) { 60124813Skarels if (opt == IPOPT_SSRR) { 60224813Skarels type = ICMP_UNREACH; 60324813Skarels code = ICMP_UNREACH_SRCFAIL; 6044951Swnj goto bad; 60524813Skarels } 60624813Skarels /* 60724813Skarels * Loose routing, and not at next destination 60824813Skarels * yet; nothing to do except forward. 60924813Skarels */ 6104951Swnj break; 6114640Swnj } 61224813Skarels off--; /* 0 origin */ 61324813Skarels if (off > optlen - sizeof(struct in_addr)) { 61424813Skarels /* 61524813Skarels * End of source route. Should be for us. 61624813Skarels */ 61724813Skarels save_rte(cp, ip->ip_src); 6184951Swnj break; 61924813Skarels } 62024813Skarels /* 62124813Skarels * locate outgoing interface 62224813Skarels */ 62326384Skarels bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 62424813Skarels sizeof(ipaddr.sin_addr)); 62540689Skarels if (opt == IPOPT_SSRR) { 62640689Skarels #define INA struct in_ifaddr * 62740689Skarels #define SA struct sockaddr * 62840689Skarels if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 62940689Skarels ia = in_iaonnetof(in_netof(ipaddr.sin_addr)); 63040689Skarels } else 63140689Skarels ia = ip_rtaddr(ipaddr.sin_addr); 63240689Skarels if (ia == 0) { 63324813Skarels type = ICMP_UNREACH; 63424813Skarels code = ICMP_UNREACH_SRCFAIL; 6354951Swnj goto bad; 63624813Skarels } 63724813Skarels ip->ip_dst = ipaddr.sin_addr; 63826384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 63926384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 64024813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 64136814Skarels forward = 1; 6424640Swnj break; 6434495Swnj 64424813Skarels case IPOPT_RR: 64524813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 64624813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 64724813Skarels goto bad; 64824813Skarels } 64924813Skarels /* 65024813Skarels * If no space remains, ignore. 65124813Skarels */ 65224813Skarels off--; /* 0 origin */ 65324813Skarels if (off > optlen - sizeof(struct in_addr)) 65424813Skarels break; 65531393Skarels bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 65624813Skarels sizeof(ipaddr.sin_addr)); 65724813Skarels /* 65837319Skarels * locate outgoing interface; if we're the destination, 65937319Skarels * use the incoming interface (should be same). 66024813Skarels */ 66140689Skarels if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 66237319Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 66324813Skarels type = ICMP_UNREACH; 66432113Skarels code = ICMP_UNREACH_HOST; 66524813Skarels goto bad; 66624813Skarels } 66726384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 66826384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 66924813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 67024813Skarels break; 67124813Skarels 6724640Swnj case IPOPT_TS: 6736583Ssam code = cp - (u_char *)ip; 6744801Swnj ipt = (struct ip_timestamp *)cp; 6754801Swnj if (ipt->ipt_len < 5) 6764640Swnj goto bad; 6774801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 6784801Swnj if (++ipt->ipt_oflw == 0) 6794640Swnj goto bad; 6804495Swnj break; 6814640Swnj } 68230925Skarels sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 6834801Swnj switch (ipt->ipt_flg) { 6844495Swnj 6854640Swnj case IPOPT_TS_TSONLY: 6864640Swnj break; 6874640Swnj 6884640Swnj case IPOPT_TS_TSANDADDR: 68924813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 69024813Skarels sizeof(struct in_addr) > ipt->ipt_len) 6914640Swnj goto bad; 69237319Skarels ia = ifptoia(m->m_pkthdr.rcvif); 69330925Skarels bcopy((caddr_t)&IA_SIN(ia)->sin_addr, 69424813Skarels (caddr_t)sin, sizeof(struct in_addr)); 69530925Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6964640Swnj break; 6974640Swnj 6984640Swnj case IPOPT_TS_PRESPEC: 69930925Skarels if (ipt->ipt_ptr + sizeof(n_time) + 70030925Skarels sizeof(struct in_addr) > ipt->ipt_len) 70130925Skarels goto bad; 70224813Skarels bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 70324813Skarels sizeof(struct in_addr)); 70440689Skarels if (ifa_ifwithaddr((SA)&ipaddr) == 0) 7054951Swnj continue; 70624813Skarels ipt->ipt_ptr += sizeof(struct in_addr); 7074640Swnj break; 7084640Swnj 7094495Swnj default: 7104640Swnj goto bad; 7114495Swnj } 71224813Skarels ntime = iptime(); 71330925Skarels bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 71430925Skarels sizeof(n_time)); 71524813Skarels ipt->ipt_ptr += sizeof(n_time); 7164640Swnj } 7174495Swnj } 71836814Skarels if (forward) { 71940689Skarels ip_forward(m, 1); 72036814Skarels return (1); 72136814Skarels } else 72236814Skarels return (0); 7234640Swnj bad: 72437319Skarels icmp_error(m, type, code); 7256583Ssam return (1); 7264495Swnj } 7274495Swnj 7284640Swnj /* 72924813Skarels * Given address of next destination (final or next hop), 73024813Skarels * return internet address info of interface to be used to get there. 73124813Skarels */ 73224813Skarels struct in_ifaddr * 73324813Skarels ip_rtaddr(dst) 73424813Skarels struct in_addr dst; 73524813Skarels { 73624813Skarels register struct sockaddr_in *sin; 73724813Skarels 73824813Skarels sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 73924813Skarels 74024813Skarels if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 74124813Skarels if (ipforward_rt.ro_rt) { 74224813Skarels RTFREE(ipforward_rt.ro_rt); 74324813Skarels ipforward_rt.ro_rt = 0; 74424813Skarels } 74524813Skarels sin->sin_family = AF_INET; 74637319Skarels sin->sin_len = sizeof(*sin); 74724813Skarels sin->sin_addr = dst; 74824813Skarels 74924813Skarels rtalloc(&ipforward_rt); 75024813Skarels } 75124813Skarels if (ipforward_rt.ro_rt == 0) 75224813Skarels return ((struct in_ifaddr *)0); 75340689Skarels return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 75424813Skarels } 75524813Skarels 75624813Skarels /* 75724813Skarels * Save incoming source route for use in replies, 75824813Skarels * to be picked up later by ip_srcroute if the receiver is interested. 75924813Skarels */ 76024813Skarels save_rte(option, dst) 76126384Skarels u_char *option; 76224813Skarels struct in_addr dst; 76324813Skarels { 76426384Skarels unsigned olen; 76524813Skarels 76624813Skarels olen = option[IPOPT_OLEN]; 76749042Ssklower #ifdef DIAGNOSTIC 76836814Skarels if (ipprintfs) 76936814Skarels printf("save_rte: olen %d\n", olen); 77040689Skarels #endif 77136814Skarels if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 77224813Skarels return; 77326384Skarels bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 77424813Skarels ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 77536814Skarels ip_srcrt.dst = dst; 77624813Skarels } 77724813Skarels 77824813Skarels /* 77924813Skarels * Retrieve incoming source route for use in replies, 78024813Skarels * in the same form used by setsockopt. 78124813Skarels * The first hop is placed before the options, will be removed later. 78224813Skarels */ 78324813Skarels struct mbuf * 78424813Skarels ip_srcroute() 78524813Skarels { 78624813Skarels register struct in_addr *p, *q; 78724813Skarels register struct mbuf *m; 78824813Skarels 78924813Skarels if (ip_nhops == 0) 79024813Skarels return ((struct mbuf *)0); 79131201Skarels m = m_get(M_DONTWAIT, MT_SOOPTS); 79231201Skarels if (m == 0) 79331201Skarels return ((struct mbuf *)0); 79424813Skarels 79536814Skarels #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 79636814Skarels 79736814Skarels /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 79836814Skarels m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 79936814Skarels OPTSIZ; 80049042Ssklower #ifdef DIAGNOSTIC 80136814Skarels if (ipprintfs) 80236814Skarels printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 80340689Skarels #endif 80436814Skarels 80524813Skarels /* 80624813Skarels * First save first hop for return route 80724813Skarels */ 80824813Skarels p = &ip_srcrt.route[ip_nhops - 1]; 80924813Skarels *(mtod(m, struct in_addr *)) = *p--; 81049042Ssklower #ifdef DIAGNOSTIC 81136814Skarels if (ipprintfs) 812*49882Sbostic printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr)); 81340689Skarels #endif 81424813Skarels 81524813Skarels /* 81624813Skarels * Copy option fields and padding (nop) to mbuf. 81724813Skarels */ 81824813Skarels ip_srcrt.nop = IPOPT_NOP; 81936814Skarels ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 82036814Skarels bcopy((caddr_t)&ip_srcrt.nop, 82136814Skarels mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 82224813Skarels q = (struct in_addr *)(mtod(m, caddr_t) + 82336814Skarels sizeof(struct in_addr) + OPTSIZ); 82436814Skarels #undef OPTSIZ 82524813Skarels /* 82624813Skarels * Record return path as an IP source route, 82724813Skarels * reversing the path (pointers are now aligned). 82824813Skarels */ 82936814Skarels while (p >= ip_srcrt.route) { 83049042Ssklower #ifdef DIAGNOSTIC 83136814Skarels if (ipprintfs) 832*49882Sbostic printf(" %lx", ntohl(q->s_addr)); 83340689Skarels #endif 83424813Skarels *q++ = *p--; 83536814Skarels } 83636814Skarels /* 83736814Skarels * Last hop goes to final destination. 83836814Skarels */ 83936814Skarels *q = ip_srcrt.dst; 84049042Ssklower #ifdef DIAGNOSTIC 84136814Skarels if (ipprintfs) 842*49882Sbostic printf(" %lx\n", ntohl(q->s_addr)); 84340689Skarels #endif 84424813Skarels return (m); 84524813Skarels } 84624813Skarels 84724813Skarels /* 8484951Swnj * Strip out IP options, at higher 8494951Swnj * level protocol in the kernel. 8504951Swnj * Second argument is buffer to which options 8514951Swnj * will be moved, and return value is their length. 85236814Skarels * XXX should be deleted; last arg currently ignored. 8534640Swnj */ 85437319Skarels ip_stripoptions(m, mopt) 85537319Skarels register struct mbuf *m; 8565217Swnj struct mbuf *mopt; 8574495Swnj { 8584640Swnj register int i; 85937319Skarels struct ip *ip = mtod(m, struct ip *); 86024813Skarels register caddr_t opts; 8614640Swnj int olen; 8624640Swnj 8634640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 86424813Skarels opts = (caddr_t)(ip + 1); 8654640Swnj i = m->m_len - (sizeof (struct ip) + olen); 86624813Skarels bcopy(opts + olen, opts, (unsigned)i); 8675243Sroot m->m_len -= olen; 86837319Skarels if (m->m_flags & M_PKTHDR) 86937319Skarels m->m_pkthdr.len -= olen; 87024813Skarels ip->ip_hl = sizeof(struct ip) >> 2; 8714495Swnj } 8726583Ssam 87314670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 87424813Skarels 0, 0, 0, 0, 87540689Skarels 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 87640689Skarels EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 87724813Skarels EMSGSIZE, EHOSTUNREACH, 0, 0, 87824813Skarels 0, 0, 0, 0, 87924813Skarels ENOPROTOOPT 8806583Ssam }; 8816583Ssam 8826583Ssam /* 8836583Ssam * Forward a packet. If some error occurs return the sender 88418376Skarels * an icmp packet. Note we can't always generate a meaningful 88524813Skarels * icmp message because icmp doesn't have a large enough repertoire 8866583Ssam * of codes and types. 88726308Skarels * 88840689Skarels * If not forwarding, just drop the packet. This could be confusing 88940689Skarels * if ipforwarding was zero but some routing protocol was advancing 89040689Skarels * us as a gateway to somewhere. However, we must let the routing 89140689Skarels * protocol deal with that. 89240689Skarels * 89340689Skarels * The srcrt parameter indicates whether the packet is being forwarded 89440689Skarels * via a source route. 8956583Ssam */ 89640689Skarels ip_forward(m, srcrt) 89736814Skarels struct mbuf *m; 89840689Skarels int srcrt; 8996583Ssam { 90036814Skarels register struct ip *ip = mtod(m, struct ip *); 90124813Skarels register struct sockaddr_in *sin; 90240689Skarels register struct rtentry *rt; 90340689Skarels int error, type = 0, code; 90418376Skarels struct mbuf *mcopy; 90524813Skarels struct in_addr dest; 9066583Ssam 90724813Skarels dest.s_addr = 0; 90849042Ssklower #ifdef DIAGNOSTIC 9096583Ssam if (ipprintfs) 9106583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 9116583Ssam ip->ip_dst, ip->ip_ttl); 91240689Skarels #endif 91337319Skarels if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 91426308Skarels ipstat.ips_cantforward++; 91537319Skarels m_freem(m); 91626308Skarels return; 9176583Ssam } 91840689Skarels HTONS(ip->ip_id); 91931393Skarels if (ip->ip_ttl <= IPTTLDEC) { 92040689Skarels icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest); 92140689Skarels return; 9226583Ssam } 9236583Ssam ip->ip_ttl -= IPTTLDEC; 9246609Ssam 92524813Skarels sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 92640689Skarels if ((rt = ipforward_rt.ro_rt) == 0 || 92724813Skarels ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 92824813Skarels if (ipforward_rt.ro_rt) { 92924813Skarels RTFREE(ipforward_rt.ro_rt); 93024813Skarels ipforward_rt.ro_rt = 0; 93124813Skarels } 93224813Skarels sin->sin_family = AF_INET; 93337319Skarels sin->sin_len = sizeof(*sin); 93424813Skarels sin->sin_addr = ip->ip_dst; 93524813Skarels 93624813Skarels rtalloc(&ipforward_rt); 93740689Skarels if (ipforward_rt.ro_rt == 0) { 93840689Skarels icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest); 93940689Skarels return; 94040689Skarels } 94140689Skarels rt = ipforward_rt.ro_rt; 94224813Skarels } 94340689Skarels 94424813Skarels /* 94540689Skarels * Save at most 64 bytes of the packet in case 94640689Skarels * we need to generate an ICMP message to the src. 94740689Skarels */ 94840689Skarels mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 94940689Skarels 95040689Skarels #ifdef GATEWAY 95140689Skarels ip_ifmatrix[rt->rt_ifp->if_index + 95240689Skarels if_index * m->m_pkthdr.rcvif->if_index]++; 95340689Skarels #endif 95440689Skarels /* 95524813Skarels * If forwarding packet using same interface that it came in on, 95624813Skarels * perhaps should send a redirect to sender to shortcut a hop. 95724813Skarels * Only send redirect if source is sending directly to us, 95824813Skarels * and if packet was not source routed (or has any options). 95930447Skarels * Also, don't send redirect if forwarding using a default route 96040689Skarels * or a route modified by a redirect. 96124813Skarels */ 96230447Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 96340689Skarels if (rt->rt_ifp == m->m_pkthdr.rcvif && 96440689Skarels (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 96540689Skarels satosin(rt_key(rt))->sin_addr.s_addr != 0 && 96640689Skarels ipsendredirects && !srcrt) { 96724813Skarels struct in_ifaddr *ia; 96824813Skarels u_long src = ntohl(ip->ip_src.s_addr); 96924813Skarels u_long dst = ntohl(ip->ip_dst.s_addr); 97024813Skarels 97137319Skarels if ((ia = ifptoia(m->m_pkthdr.rcvif)) && 97224813Skarels (src & ia->ia_subnetmask) == ia->ia_subnet) { 97340689Skarels if (rt->rt_flags & RTF_GATEWAY) 97440689Skarels dest = satosin(rt->rt_gateway)->sin_addr; 97524813Skarels else 97624813Skarels dest = ip->ip_dst; 97724813Skarels /* 97824813Skarels * If the destination is reached by a route to host, 97927145Skarels * is on a subnet of a local net, or is directly 98027145Skarels * on the attached net (!), use host redirect. 98124813Skarels * (We may be the correct first hop for other subnets.) 98224813Skarels */ 98340689Skarels #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 98424813Skarels type = ICMP_REDIRECT; 98540689Skarels if ((rt->rt_flags & RTF_HOST) || 98640689Skarels (rt->rt_flags & RTF_GATEWAY) == 0) 98740689Skarels code = ICMP_REDIRECT_HOST; 98840689Skarels else if (RTA(rt)->ia_subnetmask != RTA(rt)->ia_netmask && 98940689Skarels (dst & RTA(rt)->ia_netmask) == RTA(rt)->ia_net) 99040689Skarels code = ICMP_REDIRECT_HOST; 99140689Skarels else 99240689Skarels code = ICMP_REDIRECT_NET; 99349042Ssklower #ifdef DIAGNOSTIC 99424813Skarels if (ipprintfs) 99540689Skarels printf("redirect (%d) to %x\n", code, dest.s_addr); 99640689Skarels #endif 99724813Skarels } 99824813Skarels } 99924813Skarels 100037319Skarels error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING); 100124813Skarels if (error) 100224813Skarels ipstat.ips_cantforward++; 100324813Skarels else { 100421117Skarels ipstat.ips_forward++; 100540689Skarels if (type) 100640689Skarels ipstat.ips_redirectsent++; 100740689Skarels else { 100840689Skarels if (mcopy) 100940689Skarels m_freem(mcopy); 101040689Skarels return; 101140689Skarels } 10126609Ssam } 101311540Ssam if (mcopy == NULL) 101411540Ssam return; 10156609Ssam switch (error) { 10166609Ssam 101724813Skarels case 0: /* forwarded, but need redirect */ 101840689Skarels /* type, code set above */ 101924813Skarels break; 102024813Skarels 102140689Skarels case ENETUNREACH: /* shouldn't happen, checked above */ 102240689Skarels case EHOSTUNREACH: 10236609Ssam case ENETDOWN: 102440689Skarels case EHOSTDOWN: 102540689Skarels default: 102640689Skarels type = ICMP_UNREACH; 102740689Skarels code = ICMP_UNREACH_HOST; 10286609Ssam break; 10296609Ssam 10306609Ssam case EMSGSIZE: 103140689Skarels type = ICMP_UNREACH; 10326583Ssam code = ICMP_UNREACH_NEEDFRAG; 103339185Ssklower ipstat.ips_cantfrag++; 10346609Ssam break; 10356609Ssam 10366609Ssam case ENOBUFS: 10376609Ssam type = ICMP_SOURCEQUENCH; 103837319Skarels code = 0; 10396609Ssam break; 10406609Ssam } 104138795Skarels icmp_error(mcopy, type, code, dest); 10426583Ssam } 1043