123184Smckusick /* 223184Smckusick * Copyright (c) 1982 Regents of the University of California. 323184Smckusick * All rights reserved. The Berkeley software License Agreement 423184Smckusick * specifies the terms and conditions for redistribution. 523184Smckusick * 6*26384Skarels * @(#)ip_input.c 6.20 (Berkeley) 02/23/86 723184Smckusick */ 84571Swnj 917060Sbloom #include "param.h" 1017060Sbloom #include "systm.h" 1117060Sbloom #include "mbuf.h" 1217060Sbloom #include "domain.h" 1317060Sbloom #include "protosw.h" 1417060Sbloom #include "socket.h" 1517060Sbloom #include "errno.h" 1617060Sbloom #include "time.h" 1717060Sbloom #include "kernel.h" 188695Sroot 198695Sroot #include "../net/if.h" 208695Sroot #include "../net/route.h" 2110892Ssam 2217060Sbloom #include "in.h" 2317060Sbloom #include "in_pcb.h" 2417060Sbloom #include "in_systm.h" 2518376Skarels #include "in_var.h" 2617060Sbloom #include "ip.h" 2717060Sbloom #include "ip_var.h" 2817060Sbloom #include "ip_icmp.h" 2917060Sbloom #include "tcp.h" 304495Swnj 314898Swnj u_char ip_protox[IPPROTO_MAX]; 326210Swnj int ipqmaxlen = IFQ_MAXLEN; 3318376Skarels struct in_ifaddr *in_ifaddr; /* first inet address */ 344898Swnj 354801Swnj /* 3624813Skarels * We need to save the IP options in case a protocol wants to respond 3724813Skarels * to an incoming packet over the same route if the packet got here 3824813Skarels * using IP source routing. This allows connection establishment and 3924813Skarels * maintenance when the remote end is on a network that is not known 4024813Skarels * to us. 4124813Skarels */ 4224813Skarels int ip_nhops = 0; 4324813Skarels static struct ip_srcrt { 4424813Skarels char nop; /* one NOP to align */ 4524813Skarels char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 4624813Skarels struct in_addr route[MAX_IPOPTLEN]; 4724813Skarels } ip_srcrt; 4824813Skarels 4924813Skarels /* 505172Swnj * IP initialization: fill in IP protocol switch table. 515161Swnj * All protocols not implemented in kernel go to raw IP protocol handler. 524801Swnj */ 534801Swnj ip_init() 544801Swnj { 554898Swnj register struct protosw *pr; 564898Swnj register int i; 574495Swnj 5824813Skarels pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 594898Swnj if (pr == 0) 604898Swnj panic("ip_init"); 614898Swnj for (i = 0; i < IPPROTO_MAX; i++) 629030Sroot ip_protox[i] = pr - inetsw; 639030Sroot for (pr = inetdomain.dom_protosw; 6417551Skarels pr < inetdomain.dom_protoswNPROTOSW; pr++) 6516990Skarels if (pr->pr_domain->dom_family == PF_INET && 664898Swnj pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 679030Sroot ip_protox[pr->pr_protocol] = pr - inetsw; 684801Swnj ipq.next = ipq.prev = &ipq; 698172Sroot ip_id = time.tv_sec & 0xffff; 706210Swnj ipintrq.ifq_maxlen = ipqmaxlen; 714801Swnj } 724801Swnj 734898Swnj u_char ipcksum = 1; 744640Swnj struct ip *ip_reass(); 756338Ssam struct sockaddr_in ipaddr = { AF_INET }; 7624813Skarels struct route ipforward_rt; 774640Swnj 784640Swnj /* 794640Swnj * Ip input routine. Checksum and byte swap header. If fragmented 804640Swnj * try to reassamble. If complete and fragment queue exists, discard. 814640Swnj * Process options. Pass to next level. 824640Swnj */ 835084Swnj ipintr() 844495Swnj { 854923Swnj register struct ip *ip; 865084Swnj register struct mbuf *m; 878597Sroot struct mbuf *m0; 884640Swnj register int i; 894495Swnj register struct ipq *fp; 9018376Skarels register struct in_ifaddr *ia; 9124813Skarels struct ifnet *ifp; 925084Swnj int hlen, s; 934495Swnj 945084Swnj next: 954640Swnj /* 965084Swnj * Get next datagram off input queue and get IP header 975084Swnj * in first mbuf. 984640Swnj */ 995084Swnj s = splimp(); 10024813Skarels IF_DEQUEUEIF(&ipintrq, m, ifp); 1015084Swnj splx(s); 1025218Swnj if (m == 0) 1035084Swnj return; 10426001Skarels /* 10526001Skarels * If no IP addresses have been set yet but the interfaces 10626001Skarels * are receiving, can't do anything with incoming packets yet. 10726001Skarels */ 10826001Skarels if (in_ifaddr == NULL) 10926001Skarels goto bad; 11025920Skarels ipstat.ips_total++; 1115306Sroot if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) && 11211232Ssam (m = m_pullup(m, sizeof (struct ip))) == 0) { 11311232Ssam ipstat.ips_toosmall++; 11411232Ssam goto next; 11511232Ssam } 1164640Swnj ip = mtod(m, struct ip *); 11718376Skarels hlen = ip->ip_hl << 2; 11824813Skarels if (hlen < sizeof(struct ip)) { /* minimum header length */ 11918376Skarels ipstat.ips_badhlen++; 12021117Skarels goto bad; 12118376Skarels } 12218376Skarels if (hlen > m->m_len) { 12311232Ssam if ((m = m_pullup(m, hlen)) == 0) { 12411232Ssam ipstat.ips_badhlen++; 12511232Ssam goto next; 12611232Ssam } 1275161Swnj ip = mtod(m, struct ip *); 1285161Swnj } 1294951Swnj if (ipcksum) 1305217Swnj if (ip->ip_sum = in_cksum(m, hlen)) { 1314951Swnj ipstat.ips_badsum++; 1324951Swnj goto bad; 1334495Swnj } 1344951Swnj 1354951Swnj /* 1364951Swnj * Convert fields to host representation. 1374951Swnj */ 1384907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 13911232Ssam if (ip->ip_len < hlen) { 14011232Ssam ipstat.ips_badlen++; 14111232Ssam goto bad; 14211232Ssam } 1434640Swnj ip->ip_id = ntohs(ip->ip_id); 1444951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 1454495Swnj 1464543Swnj /* 1474640Swnj * Check that the amount of data in the buffers 1484640Swnj * is as at least much as the IP header would have us expect. 1494640Swnj * Trim mbufs if longer than we expect. 1504640Swnj * Drop packet if shorter than we expect. 1514543Swnj */ 15224813Skarels i = -(u_short)ip->ip_len; 1535161Swnj m0 = m; 1546475Sroot for (;;) { 1554495Swnj i += m->m_len; 1566475Sroot if (m->m_next == 0) 1576475Sroot break; 1586475Sroot m = m->m_next; 1596088Sroot } 1606475Sroot if (i != 0) { 1616475Sroot if (i < 0) { 1625161Swnj ipstat.ips_tooshort++; 16317358Skarels m = m0; 1644951Swnj goto bad; 1655161Swnj } 1666475Sroot if (i <= m->m_len) 1676475Sroot m->m_len -= i; 1686475Sroot else 1696475Sroot m_adj(m0, -i); 1704495Swnj } 1716475Sroot m = m0; 1724495Swnj 1734640Swnj /* 1744640Swnj * Process options and, if not destined for us, 1756583Ssam * ship it on. ip_dooptions returns 1 when an 1766583Ssam * error was detected (causing an icmp message 17721117Skarels * to be sent and the original packet to be freed). 1784640Swnj */ 17924813Skarels ip_nhops = 0; /* for source routed packets */ 18026031Skarels if (hlen > sizeof (struct ip) && ip_dooptions(ip, ifp)) 1816583Ssam goto next; 1826210Swnj 1836338Ssam /* 18418376Skarels * Check our list of addresses, to see if the packet is for us. 1856338Ssam */ 18618376Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) { 18718376Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 1886338Ssam 18918376Skarels if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 19024813Skarels goto ours; 19125195Skarels if ( 19225195Skarels #ifdef DIRECTED_BROADCAST 19325195Skarels ia->ia_ifp == ifp && 19425195Skarels #endif 19525195Skarels (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 19626247Skarels u_long t; 19725195Skarels 19825195Skarels if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 19925195Skarels ip->ip_dst.s_addr) 20025195Skarels goto ours; 20125195Skarels if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 20225195Skarels goto ours; 20325195Skarels /* 20425195Skarels * Look for all-0's host part (old broadcast addr), 20525195Skarels * either for subnet or net. 20625195Skarels */ 20726247Skarels t = ntohl(ip->ip_dst.s_addr); 20826247Skarels if (t == ia->ia_subnet) 20925195Skarels goto ours; 21026247Skarels if (t == ia->ia_net) 21125195Skarels goto ours; 21225195Skarels } 2136338Ssam } 21424813Skarels if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 21524813Skarels goto ours; 21624813Skarels if (ip->ip_dst.s_addr == INADDR_ANY) 21724813Skarels goto ours; 2184495Swnj 2194640Swnj /* 22024813Skarels * Not for us; forward if possible and desirable. 22124813Skarels */ 22224813Skarels ip_forward(ip, ifp); 22324813Skarels goto next; 22424813Skarels 22524813Skarels ours: 22624813Skarels /* 2274640Swnj * Look for queue of fragments 2284640Swnj * of this datagram. 2294640Swnj */ 2304640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 2314640Swnj if (ip->ip_id == fp->ipq_id && 2324640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 2334640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 2344640Swnj ip->ip_p == fp->ipq_p) 2354640Swnj goto found; 2364640Swnj fp = 0; 2374640Swnj found: 2384495Swnj 2394640Swnj /* 2404640Swnj * Adjust ip_len to not reflect header, 2414640Swnj * set ip_mff if more fragments are expected, 2424640Swnj * convert offset of this to bytes. 2434640Swnj */ 2444640Swnj ip->ip_len -= hlen; 2454898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 2464640Swnj if (ip->ip_off & IP_MF) 2474898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 2484640Swnj ip->ip_off <<= 3; 2494495Swnj 2504640Swnj /* 2514640Swnj * If datagram marked as having more fragments 2524640Swnj * or if this is not the first fragment, 2534640Swnj * attempt reassembly; if it succeeds, proceed. 2544640Swnj */ 2554898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 25624813Skarels ipstat.ips_fragments++; 2574898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 2584640Swnj if (ip == 0) 2595084Swnj goto next; 2604640Swnj m = dtom(ip); 2614640Swnj } else 2624640Swnj if (fp) 26310735Ssam ip_freef(fp); 2644951Swnj 2654951Swnj /* 2664951Swnj * Switch out to protocol's input routine. 2674951Swnj */ 26824813Skarels (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, ifp); 2695084Swnj goto next; 2704951Swnj bad: 2714951Swnj m_freem(m); 2725084Swnj goto next; 2734640Swnj } 2744495Swnj 2754640Swnj /* 2764640Swnj * Take incoming datagram fragment and try to 2774951Swnj * reassemble it into whole datagram. If a chain for 2784640Swnj * reassembly of this datagram already exists, then it 2794640Swnj * is given as fp; otherwise have to make a chain. 2804640Swnj */ 2814640Swnj struct ip * 2824640Swnj ip_reass(ip, fp) 2834898Swnj register struct ipasfrag *ip; 2844640Swnj register struct ipq *fp; 2854640Swnj { 2864640Swnj register struct mbuf *m = dtom(ip); 2874898Swnj register struct ipasfrag *q; 2884640Swnj struct mbuf *t; 2894640Swnj int hlen = ip->ip_hl << 2; 2904640Swnj int i, next; 2914543Swnj 2924640Swnj /* 2934640Swnj * Presence of header sizes in mbufs 2944640Swnj * would confuse code below. 2954640Swnj */ 2964640Swnj m->m_off += hlen; 2974640Swnj m->m_len -= hlen; 2984495Swnj 2994640Swnj /* 3004640Swnj * If first fragment to arrive, create a reassembly queue. 3014640Swnj */ 3024640Swnj if (fp == 0) { 3039641Ssam if ((t = m_get(M_WAIT, MT_FTABLE)) == NULL) 3044640Swnj goto dropfrag; 3054640Swnj fp = mtod(t, struct ipq *); 3064640Swnj insque(fp, &ipq); 3074640Swnj fp->ipq_ttl = IPFRAGTTL; 3084640Swnj fp->ipq_p = ip->ip_p; 3094640Swnj fp->ipq_id = ip->ip_id; 3104898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 3114898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 3124898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 3135161Swnj q = (struct ipasfrag *)fp; 3145161Swnj goto insert; 3154640Swnj } 3164495Swnj 3174640Swnj /* 3184640Swnj * Find a segment which begins after this one does. 3194640Swnj */ 3204898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3214640Swnj if (q->ip_off > ip->ip_off) 3224640Swnj break; 3234495Swnj 3244640Swnj /* 3254640Swnj * If there is a preceding segment, it may provide some of 3264640Swnj * our data already. If so, drop the data from the incoming 3274640Swnj * segment. If it provides all of our data, drop us. 3284640Swnj */ 3294898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 3304898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 3314640Swnj if (i > 0) { 3324640Swnj if (i >= ip->ip_len) 3334640Swnj goto dropfrag; 3344640Swnj m_adj(dtom(ip), i); 3354640Swnj ip->ip_off += i; 3364640Swnj ip->ip_len -= i; 3374640Swnj } 3384640Swnj } 3394543Swnj 3404640Swnj /* 3414640Swnj * While we overlap succeeding segments trim them or, 3424640Swnj * if they are completely covered, dequeue them. 3434640Swnj */ 3444898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 3454640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 3464640Swnj if (i < q->ip_len) { 3474640Swnj q->ip_len -= i; 3486256Sroot q->ip_off += i; 3494640Swnj m_adj(dtom(q), i); 3504640Swnj break; 3514495Swnj } 3524898Swnj q = q->ipf_next; 3534898Swnj m_freem(dtom(q->ipf_prev)); 3544898Swnj ip_deq(q->ipf_prev); 3554543Swnj } 3564495Swnj 3575161Swnj insert: 3584640Swnj /* 3594640Swnj * Stick new segment in its place; 3604640Swnj * check for complete reassembly. 3614640Swnj */ 3624898Swnj ip_enq(ip, q->ipf_prev); 3634640Swnj next = 0; 3644898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 3654640Swnj if (q->ip_off != next) 3664640Swnj return (0); 3674640Swnj next += q->ip_len; 3684640Swnj } 3694898Swnj if (q->ipf_prev->ipf_mff) 3704640Swnj return (0); 3714495Swnj 3724640Swnj /* 3734640Swnj * Reassembly is complete; concatenate fragments. 3744640Swnj */ 3754640Swnj q = fp->ipq_next; 3764640Swnj m = dtom(q); 3774640Swnj t = m->m_next; 3784640Swnj m->m_next = 0; 3794640Swnj m_cat(m, t); 3806298Swnj q = q->ipf_next; 3816298Swnj while (q != (struct ipasfrag *)fp) { 3826298Swnj t = dtom(q); 3836298Swnj q = q->ipf_next; 3846298Swnj m_cat(m, t); 3856298Swnj } 3864495Swnj 3874640Swnj /* 3884640Swnj * Create header for new ip packet by 3894640Swnj * modifying header of first packet; 3904640Swnj * dequeue and discard fragment reassembly header. 3914640Swnj * Make header visible. 3924640Swnj */ 3934640Swnj ip = fp->ipq_next; 3944640Swnj ip->ip_len = next; 3954898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 3964898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 3974640Swnj remque(fp); 3984907Swnj (void) m_free(dtom(fp)); 3994640Swnj m = dtom(ip); 40024813Skarels m->m_len += (ip->ip_hl << 2); 40124813Skarels m->m_off -= (ip->ip_hl << 2); 4024898Swnj return ((struct ip *)ip); 4034495Swnj 4044640Swnj dropfrag: 40524813Skarels ipstat.ips_fragdropped++; 4064640Swnj m_freem(m); 4074640Swnj return (0); 4084495Swnj } 4094495Swnj 4104640Swnj /* 4114640Swnj * Free a fragment reassembly header and all 4124640Swnj * associated datagrams. 4134640Swnj */ 4144640Swnj ip_freef(fp) 4154640Swnj struct ipq *fp; 4164495Swnj { 41710735Ssam register struct ipasfrag *q, *p; 4184495Swnj 41910735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 42010735Ssam p = q->ipf_next; 42110735Ssam ip_deq(q); 4224640Swnj m_freem(dtom(q)); 42310735Ssam } 42410735Ssam remque(fp); 42510735Ssam (void) m_free(dtom(fp)); 4264495Swnj } 4274495Swnj 4284640Swnj /* 4294640Swnj * Put an ip fragment on a reassembly chain. 4304640Swnj * Like insque, but pointers in middle of structure. 4314640Swnj */ 4324640Swnj ip_enq(p, prev) 4334898Swnj register struct ipasfrag *p, *prev; 4344495Swnj { 4354951Swnj 4364898Swnj p->ipf_prev = prev; 4374898Swnj p->ipf_next = prev->ipf_next; 4384898Swnj prev->ipf_next->ipf_prev = p; 4394898Swnj prev->ipf_next = p; 4404495Swnj } 4414495Swnj 4424640Swnj /* 4434640Swnj * To ip_enq as remque is to insque. 4444640Swnj */ 4454640Swnj ip_deq(p) 4464898Swnj register struct ipasfrag *p; 4474640Swnj { 4484951Swnj 4494898Swnj p->ipf_prev->ipf_next = p->ipf_next; 4504898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 4514495Swnj } 4524495Swnj 4534640Swnj /* 4544640Swnj * IP timer processing; 4554640Swnj * if a timer expires on a reassembly 4564640Swnj * queue, discard it. 4574640Swnj */ 4584801Swnj ip_slowtimo() 4594495Swnj { 4604495Swnj register struct ipq *fp; 4614640Swnj int s = splnet(); 4624951Swnj 4635243Sroot fp = ipq.next; 4645243Sroot if (fp == 0) { 4655243Sroot splx(s); 4665243Sroot return; 4675243Sroot } 46810735Ssam while (fp != &ipq) { 46910735Ssam --fp->ipq_ttl; 47010735Ssam fp = fp->next; 47124813Skarels if (fp->prev->ipq_ttl == 0) { 47224813Skarels ipstat.ips_fragtimeout++; 47310735Ssam ip_freef(fp->prev); 47424813Skarels } 47510735Ssam } 4764640Swnj splx(s); 4774495Swnj } 4784495Swnj 4794951Swnj /* 4804951Swnj * Drain off all datagram fragments. 4814951Swnj */ 4824801Swnj ip_drain() 4834801Swnj { 4844801Swnj 48524813Skarels while (ipq.next != &ipq) { 48624813Skarels ipstat.ips_fragdropped++; 48710735Ssam ip_freef(ipq.next); 48824813Skarels } 4894801Swnj } 4904923Swnj 49124813Skarels struct in_ifaddr *ip_rtaddr(); 49224813Skarels 4934640Swnj /* 4944640Swnj * Do option processing on a datagram, 4954640Swnj * possibly discarding it if bad options 4964640Swnj * are encountered. 4974640Swnj */ 49826031Skarels ip_dooptions(ip, ifp) 49926031Skarels register struct ip *ip; 50026031Skarels struct ifnet *ifp; 5014495Swnj { 5024640Swnj register u_char *cp; 50324813Skarels int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB; 50424813Skarels register struct ip_timestamp *ipt; 50524813Skarels register struct in_ifaddr *ia; 5064923Swnj struct in_addr *sin; 50724813Skarels n_time ntime; 5084495Swnj 5094640Swnj cp = (u_char *)(ip + 1); 5104640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 5114640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 51224813Skarels opt = cp[IPOPT_OPTVAL]; 5134640Swnj if (opt == IPOPT_EOL) 5144640Swnj break; 5154640Swnj if (opt == IPOPT_NOP) 5164640Swnj optlen = 1; 51716392Ssam else { 51824813Skarels optlen = cp[IPOPT_OLEN]; 51924813Skarels if (optlen <= 0 || optlen > cnt) { 52024813Skarels code = &cp[IPOPT_OLEN] - (u_char *)ip; 52117551Skarels goto bad; 52224813Skarels } 52316392Ssam } 5244640Swnj switch (opt) { 5254495Swnj 5264640Swnj default: 5274640Swnj break; 5284495Swnj 5294951Swnj /* 5304951Swnj * Source routing with record. 5314951Swnj * Find interface with current destination address. 5324951Swnj * If none on this machine then drop if strictly routed, 5334951Swnj * or do nothing if loosely routed. 5344951Swnj * Record interface address and bring up next address 5354951Swnj * component. If strictly routed make sure next 5364951Swnj * address on directly accessible net. 5374951Swnj */ 5384640Swnj case IPOPT_LSRR: 5397508Sroot case IPOPT_SSRR: 54024813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 54124813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 54224813Skarels goto bad; 54324813Skarels } 54424813Skarels ipaddr.sin_addr = ip->ip_dst; 54524813Skarels ia = (struct in_ifaddr *) 54624813Skarels ifa_ifwithaddr((struct sockaddr *)&ipaddr); 54724813Skarels if (ia == 0) { 54824813Skarels if (opt == IPOPT_SSRR) { 54924813Skarels type = ICMP_UNREACH; 55024813Skarels code = ICMP_UNREACH_SRCFAIL; 5514951Swnj goto bad; 55224813Skarels } 55324813Skarels /* 55424813Skarels * Loose routing, and not at next destination 55524813Skarels * yet; nothing to do except forward. 55624813Skarels */ 5574951Swnj break; 5584640Swnj } 55924813Skarels off--; /* 0 origin */ 56024813Skarels if (off > optlen - sizeof(struct in_addr)) { 56124813Skarels /* 56224813Skarels * End of source route. Should be for us. 56324813Skarels */ 56424813Skarels save_rte(cp, ip->ip_src); 5654951Swnj break; 56624813Skarels } 56724813Skarels /* 56824813Skarels * locate outgoing interface 56924813Skarels */ 570*26384Skarels bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 57124813Skarels sizeof(ipaddr.sin_addr)); 57224813Skarels if ((opt == IPOPT_SSRR && 57324813Skarels in_iaonnetof(in_netof(ipaddr.sin_addr)) == 0) || 57424813Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 57524813Skarels type = ICMP_UNREACH; 57624813Skarels code = ICMP_UNREACH_SRCFAIL; 5774951Swnj goto bad; 57824813Skarels } 57924813Skarels ip->ip_dst = ipaddr.sin_addr; 580*26384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 581*26384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 58224813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 5834640Swnj break; 5844495Swnj 58524813Skarels case IPOPT_RR: 58624813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 58724813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 58824813Skarels goto bad; 58924813Skarels } 59024813Skarels /* 59124813Skarels * If no space remains, ignore. 59224813Skarels */ 59324813Skarels off--; /* 0 origin */ 59424813Skarels if (off > optlen - sizeof(struct in_addr)) 59524813Skarels break; 596*26384Skarels bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 59724813Skarels sizeof(ipaddr.sin_addr)); 59824813Skarels /* 59924813Skarels * locate outgoing interface 60024813Skarels */ 60124813Skarels if ((ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 60224813Skarels type = ICMP_UNREACH; 60324813Skarels code = ICMP_UNREACH_SRCFAIL; 60424813Skarels goto bad; 60524813Skarels } 606*26384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 607*26384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 60824813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 60924813Skarels break; 61024813Skarels 6114640Swnj case IPOPT_TS: 6126583Ssam code = cp - (u_char *)ip; 6134801Swnj ipt = (struct ip_timestamp *)cp; 6144801Swnj if (ipt->ipt_len < 5) 6154640Swnj goto bad; 6164801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 6174801Swnj if (++ipt->ipt_oflw == 0) 6184640Swnj goto bad; 6194495Swnj break; 6204640Swnj } 62124813Skarels sin = (struct in_addr *)(cp+cp[IPOPT_OFFSET]-1); 6224801Swnj switch (ipt->ipt_flg) { 6234495Swnj 6244640Swnj case IPOPT_TS_TSONLY: 6254640Swnj break; 6264640Swnj 6274640Swnj case IPOPT_TS_TSANDADDR: 62824813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 62924813Skarels sizeof(struct in_addr) > ipt->ipt_len) 6304640Swnj goto bad; 63118376Skarels if (in_ifaddr == 0) 6326338Ssam goto bad; /* ??? */ 63324813Skarels bcopy((caddr_t)&IA_SIN(in_ifaddr)->sin_addr, 63424813Skarels (caddr_t)sin, sizeof(struct in_addr)); 63524813Skarels sin++; 6364640Swnj break; 6374640Swnj 6384640Swnj case IPOPT_TS_PRESPEC: 63924813Skarels bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 64024813Skarels sizeof(struct in_addr)); 64118376Skarels if (ifa_ifwithaddr((struct sockaddr *)&ipaddr) == 0) 6424951Swnj continue; 64324813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 64424813Skarels sizeof(struct in_addr) > ipt->ipt_len) 6454640Swnj goto bad; 64624813Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6474640Swnj break; 6484640Swnj 6494495Swnj default: 6504640Swnj goto bad; 6514495Swnj } 65224813Skarels ntime = iptime(); 65324813Skarels bcopy((caddr_t)&ntime, (caddr_t)sin, sizeof(n_time)); 65424813Skarels ipt->ipt_ptr += sizeof(n_time); 6554640Swnj } 6564495Swnj } 6576583Ssam return (0); 6584640Swnj bad: 65926031Skarels icmp_error(ip, type, code, ifp); 6606583Ssam return (1); 6614495Swnj } 6624495Swnj 6634640Swnj /* 66424813Skarels * Given address of next destination (final or next hop), 66524813Skarels * return internet address info of interface to be used to get there. 66624813Skarels */ 66724813Skarels struct in_ifaddr * 66824813Skarels ip_rtaddr(dst) 66924813Skarels struct in_addr dst; 67024813Skarels { 67124813Skarels register struct sockaddr_in *sin; 67224813Skarels register struct in_ifaddr *ia; 67324813Skarels 67424813Skarels sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 67524813Skarels 67624813Skarels if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 67724813Skarels if (ipforward_rt.ro_rt) { 67824813Skarels RTFREE(ipforward_rt.ro_rt); 67924813Skarels ipforward_rt.ro_rt = 0; 68024813Skarels } 68124813Skarels sin->sin_family = AF_INET; 68224813Skarels sin->sin_addr = dst; 68324813Skarels 68424813Skarels rtalloc(&ipforward_rt); 68524813Skarels } 68624813Skarels if (ipforward_rt.ro_rt == 0) 68724813Skarels return ((struct in_ifaddr *)0); 68824813Skarels /* 68924813Skarels * Find address associated with outgoing interface. 69024813Skarels */ 69124813Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) 69224813Skarels if (ia->ia_ifp == ipforward_rt.ro_rt->rt_ifp) 69324813Skarels break; 69424813Skarels return (ia); 69524813Skarels } 69624813Skarels 69724813Skarels /* 69824813Skarels * Save incoming source route for use in replies, 69924813Skarels * to be picked up later by ip_srcroute if the receiver is interested. 70024813Skarels */ 70124813Skarels save_rte(option, dst) 702*26384Skarels u_char *option; 70324813Skarels struct in_addr dst; 70424813Skarels { 705*26384Skarels unsigned olen; 70624813Skarels extern ipprintfs; 70724813Skarels 70824813Skarels olen = option[IPOPT_OLEN]; 70924813Skarels if (olen > sizeof(ip_srcrt) - 1) { 71024813Skarels if (ipprintfs) 71124813Skarels printf("save_rte: olen %d\n", olen); 71224813Skarels return; 71324813Skarels } 714*26384Skarels bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 71524813Skarels ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 71624813Skarels ip_srcrt.route[ip_nhops++] = dst; 71724813Skarels } 71824813Skarels 71924813Skarels /* 72024813Skarels * Retrieve incoming source route for use in replies, 72124813Skarels * in the same form used by setsockopt. 72224813Skarels * The first hop is placed before the options, will be removed later. 72324813Skarels */ 72424813Skarels struct mbuf * 72524813Skarels ip_srcroute() 72624813Skarels { 72724813Skarels register struct in_addr *p, *q; 72824813Skarels register struct mbuf *m; 72924813Skarels 73024813Skarels if (ip_nhops == 0) 73124813Skarels return ((struct mbuf *)0); 73224813Skarels m = m_get(M_WAIT, MT_SOOPTS); 73324813Skarels m->m_len = ip_nhops * sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1; 73424813Skarels 73524813Skarels /* 73624813Skarels * First save first hop for return route 73724813Skarels */ 73824813Skarels p = &ip_srcrt.route[ip_nhops - 1]; 73924813Skarels *(mtod(m, struct in_addr *)) = *p--; 74024813Skarels 74124813Skarels /* 74224813Skarels * Copy option fields and padding (nop) to mbuf. 74324813Skarels */ 74424813Skarels ip_srcrt.nop = IPOPT_NOP; 74524813Skarels bcopy((caddr_t)&ip_srcrt, mtod(m, caddr_t) + sizeof(struct in_addr), 74624813Skarels IPOPT_OFFSET + 1 + 1); 74724813Skarels q = (struct in_addr *)(mtod(m, caddr_t) + 74824813Skarels sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1); 74924813Skarels /* 75024813Skarels * Record return path as an IP source route, 75124813Skarels * reversing the path (pointers are now aligned). 75224813Skarels */ 75324813Skarels while (p >= ip_srcrt.route) 75424813Skarels *q++ = *p--; 75524813Skarels return (m); 75624813Skarels } 75724813Skarels 75824813Skarels /* 7594951Swnj * Strip out IP options, at higher 7604951Swnj * level protocol in the kernel. 7614951Swnj * Second argument is buffer to which options 7624951Swnj * will be moved, and return value is their length. 7634640Swnj */ 7645217Swnj ip_stripoptions(ip, mopt) 7654640Swnj struct ip *ip; 7665217Swnj struct mbuf *mopt; 7674495Swnj { 7684640Swnj register int i; 7694640Swnj register struct mbuf *m; 77024813Skarels register caddr_t opts; 7714640Swnj int olen; 7724640Swnj 7734640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 7744951Swnj m = dtom(ip); 77524813Skarels opts = (caddr_t)(ip + 1); 7765217Swnj if (mopt) { 7775217Swnj mopt->m_len = olen; 7785217Swnj mopt->m_off = MMINOFF; 77924813Skarels bcopy(opts, mtod(mopt, caddr_t), (unsigned)olen); 7805217Swnj } 7814640Swnj i = m->m_len - (sizeof (struct ip) + olen); 78224813Skarels bcopy(opts + olen, opts, (unsigned)i); 7835243Sroot m->m_len -= olen; 78424813Skarels ip->ip_hl = sizeof(struct ip) >> 2; 7854495Swnj } 7866583Ssam 78714670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 78824813Skarels 0, 0, 0, 0, 78914670Ssam 0, 0, EHOSTDOWN, EHOSTUNREACH, 79014670Ssam ENETUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 79124813Skarels EMSGSIZE, EHOSTUNREACH, 0, 0, 79224813Skarels 0, 0, 0, 0, 79324813Skarels ENOPROTOOPT 7946583Ssam }; 7956583Ssam 79624813Skarels #ifndef IPFORWARDING 79724813Skarels #define IPFORWARDING 1 79824813Skarels #endif 79924813Skarels #ifndef IPSENDREDIRECTS 80024813Skarels #define IPSENDREDIRECTS 1 80124813Skarels #endif 8026583Ssam int ipprintfs = 0; 80324813Skarels int ipforwarding = IPFORWARDING; 80424813Skarels extern int in_interfaces; 80524813Skarels int ipsendredirects = IPSENDREDIRECTS; 80624813Skarels 8076583Ssam /* 8086583Ssam * Forward a packet. If some error occurs return the sender 80918376Skarels * an icmp packet. Note we can't always generate a meaningful 81024813Skarels * icmp message because icmp doesn't have a large enough repertoire 8116583Ssam * of codes and types. 81226308Skarels * 81326308Skarels * If not forwarding (possibly because we have only a single external 81426308Skarels * network), just drop the packet. This could be confusing if ipforwarding 81526308Skarels * was zero but some routing protocol was advancing us as a gateway 81626308Skarels * to somewhere. However, we must let the routing protocol deal with that. 8176583Ssam */ 81824813Skarels ip_forward(ip, ifp) 8196583Ssam register struct ip *ip; 82024813Skarels struct ifnet *ifp; 8216583Ssam { 82224813Skarels register int error, type = 0, code; 82324813Skarels register struct sockaddr_in *sin; 82418376Skarels struct mbuf *mcopy; 82524813Skarels struct in_addr dest; 8266583Ssam 82724813Skarels #ifdef lint 82824813Skarels dest.s_addr = 0; 82924813Skarels #endif 8306583Ssam if (ipprintfs) 8316583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 8326583Ssam ip->ip_dst, ip->ip_ttl); 83318376Skarels ip->ip_id = htons(ip->ip_id); 83424813Skarels if (ipforwarding == 0 || in_interfaces <= 1) { 83526308Skarels ipstat.ips_cantforward++; 83626308Skarels m_freem(dtom(ip)); 83726308Skarels return; 8386583Ssam } 8396583Ssam if (ip->ip_ttl < IPTTLDEC) { 8406583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 8416583Ssam goto sendicmp; 8426583Ssam } 8436583Ssam ip->ip_ttl -= IPTTLDEC; 8446609Ssam 8456609Ssam /* 8466609Ssam * Save at most 64 bytes of the packet in case 8476609Ssam * we need to generate an ICMP message to the src. 8486609Ssam */ 8497843Sroot mcopy = m_copy(dtom(ip), 0, imin(ip->ip_len, 64)); 8506583Ssam 85124813Skarels sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 85224813Skarels if (ipforward_rt.ro_rt == 0 || 85324813Skarels ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 85424813Skarels if (ipforward_rt.ro_rt) { 85524813Skarels RTFREE(ipforward_rt.ro_rt); 85624813Skarels ipforward_rt.ro_rt = 0; 85724813Skarels } 85824813Skarels sin->sin_family = AF_INET; 85924813Skarels sin->sin_addr = ip->ip_dst; 86024813Skarels 86124813Skarels rtalloc(&ipforward_rt); 86224813Skarels } 86324813Skarels /* 86424813Skarels * If forwarding packet using same interface that it came in on, 86524813Skarels * perhaps should send a redirect to sender to shortcut a hop. 86624813Skarels * Only send redirect if source is sending directly to us, 86724813Skarels * and if packet was not source routed (or has any options). 86824813Skarels */ 86924813Skarels if (ipforward_rt.ro_rt && ipforward_rt.ro_rt->rt_ifp == ifp && 87024813Skarels ipsendredirects && ip->ip_hl == (sizeof(struct ip) >> 2)) { 87124813Skarels struct in_ifaddr *ia; 87224813Skarels extern struct in_ifaddr *ifptoia(); 87324813Skarels u_long src = ntohl(ip->ip_src.s_addr); 87424813Skarels u_long dst = ntohl(ip->ip_dst.s_addr); 87524813Skarels 87624813Skarels if ((ia = ifptoia(ifp)) && 87724813Skarels (src & ia->ia_subnetmask) == ia->ia_subnet) { 87824813Skarels if (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) 87924813Skarels dest = satosin(&ipforward_rt.ro_rt->rt_gateway)->sin_addr; 88024813Skarels else 88124813Skarels dest = ip->ip_dst; 88224813Skarels /* 88324813Skarels * If the destination is reached by a route to host, 88424813Skarels * is directly on the attached net (!), 88524813Skarels * or if the destination is on a subnet of a local net 88624813Skarels * not known to the source net, use host redirect. 88724813Skarels * (We may be the correct first hop for other subnets.) 88824813Skarels */ 88924813Skarels type = ICMP_REDIRECT; 89024813Skarels code = ICMP_REDIRECT_NET; 89124813Skarels if ((ipforward_rt.ro_rt->rt_flags & RTF_HOST) || 89224813Skarels (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) == 0) 89324813Skarels code = ICMP_REDIRECT_HOST; 89424813Skarels else for (ia = in_ifaddr; ia = ia->ia_next; ) 89524813Skarels if ((dst & ia->ia_netmask) == ia->ia_net) { 89624813Skarels if ((src & ia->ia_netmask) != ia->ia_net) 89724813Skarels code = ICMP_REDIRECT_HOST; 89824813Skarels break; 89924813Skarels } 90024813Skarels if (ipprintfs) 90124813Skarels printf("redirect (%d) to %x\n", code, dest); 90224813Skarels } 90324813Skarels } 90424813Skarels 90524813Skarels error = ip_output(dtom(ip), (struct mbuf *)0, &ipforward_rt, 90618376Skarels IP_FORWARDING); 90724813Skarels if (error) 90824813Skarels ipstat.ips_cantforward++; 90924813Skarels else if (type) 91024813Skarels ipstat.ips_redirectsent++; 91124813Skarels else { 9126609Ssam if (mcopy) 9136609Ssam m_freem(mcopy); 91421117Skarels ipstat.ips_forward++; 9156583Ssam return; 9166609Ssam } 91711540Ssam if (mcopy == NULL) 91811540Ssam return; 9196609Ssam ip = mtod(mcopy, struct ip *); 92024813Skarels type = ICMP_UNREACH; 9216609Ssam switch (error) { 9226609Ssam 92324813Skarels case 0: /* forwarded, but need redirect */ 92424813Skarels type = ICMP_REDIRECT; 92524813Skarels /* code set above */ 92624813Skarels break; 92724813Skarels 9286609Ssam case ENETUNREACH: 9296609Ssam case ENETDOWN: 9306583Ssam code = ICMP_UNREACH_NET; 9316609Ssam break; 9326609Ssam 9336609Ssam case EMSGSIZE: 9346583Ssam code = ICMP_UNREACH_NEEDFRAG; 9356609Ssam break; 9366609Ssam 9376609Ssam case EPERM: 9386609Ssam code = ICMP_UNREACH_PORT; 9396609Ssam break; 9406609Ssam 9416609Ssam case ENOBUFS: 9426609Ssam type = ICMP_SOURCEQUENCH; 9436609Ssam break; 9446609Ssam 9456609Ssam case EHOSTDOWN: 9466609Ssam case EHOSTUNREACH: 9476609Ssam code = ICMP_UNREACH_HOST; 9486609Ssam break; 9496609Ssam } 9506583Ssam sendicmp: 95126031Skarels icmp_error(ip, type, code, ifp, dest); 9526583Ssam } 953