123184Smckusick /* 229143Smckusick * Copyright (c) 1982, 1986 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*30925Skarels * @(#)ip_input.c 7.3 (Berkeley) 04/18/87 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 491*30925Skarels extern struct in_ifaddr *ifptoia(); 49224813Skarels struct in_ifaddr *ip_rtaddr(); 49324813Skarels 4944640Swnj /* 4954640Swnj * Do option processing on a datagram, 4964640Swnj * possibly discarding it if bad options 4974640Swnj * are encountered. 4984640Swnj */ 49926031Skarels ip_dooptions(ip, ifp) 50026031Skarels register struct ip *ip; 50126031Skarels struct ifnet *ifp; 5024495Swnj { 5034640Swnj register u_char *cp; 50424813Skarels int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB; 50524813Skarels register struct ip_timestamp *ipt; 50624813Skarels register struct in_ifaddr *ia; 5074923Swnj struct in_addr *sin; 50824813Skarels n_time ntime; 5094495Swnj 5104640Swnj cp = (u_char *)(ip + 1); 5114640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 5124640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 51324813Skarels opt = cp[IPOPT_OPTVAL]; 5144640Swnj if (opt == IPOPT_EOL) 5154640Swnj break; 5164640Swnj if (opt == IPOPT_NOP) 5174640Swnj optlen = 1; 51816392Ssam else { 51924813Skarels optlen = cp[IPOPT_OLEN]; 52024813Skarels if (optlen <= 0 || optlen > cnt) { 52124813Skarels code = &cp[IPOPT_OLEN] - (u_char *)ip; 52217551Skarels goto bad; 52324813Skarels } 52416392Ssam } 5254640Swnj switch (opt) { 5264495Swnj 5274640Swnj default: 5284640Swnj break; 5294495Swnj 5304951Swnj /* 5314951Swnj * Source routing with record. 5324951Swnj * Find interface with current destination address. 5334951Swnj * If none on this machine then drop if strictly routed, 5344951Swnj * or do nothing if loosely routed. 5354951Swnj * Record interface address and bring up next address 5364951Swnj * component. If strictly routed make sure next 5374951Swnj * address on directly accessible net. 5384951Swnj */ 5394640Swnj case IPOPT_LSRR: 5407508Sroot case IPOPT_SSRR: 54124813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 54224813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 54324813Skarels goto bad; 54424813Skarels } 54524813Skarels ipaddr.sin_addr = ip->ip_dst; 54624813Skarels ia = (struct in_ifaddr *) 54724813Skarels ifa_ifwithaddr((struct sockaddr *)&ipaddr); 54824813Skarels if (ia == 0) { 54924813Skarels if (opt == IPOPT_SSRR) { 55024813Skarels type = ICMP_UNREACH; 55124813Skarels code = ICMP_UNREACH_SRCFAIL; 5524951Swnj goto bad; 55324813Skarels } 55424813Skarels /* 55524813Skarels * Loose routing, and not at next destination 55624813Skarels * yet; nothing to do except forward. 55724813Skarels */ 5584951Swnj break; 5594640Swnj } 56024813Skarels off--; /* 0 origin */ 56124813Skarels if (off > optlen - sizeof(struct in_addr)) { 56224813Skarels /* 56324813Skarels * End of source route. Should be for us. 56424813Skarels */ 56524813Skarels save_rte(cp, ip->ip_src); 5664951Swnj break; 56724813Skarels } 56824813Skarels /* 56924813Skarels * locate outgoing interface 57024813Skarels */ 57126384Skarels bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 57224813Skarels sizeof(ipaddr.sin_addr)); 57324813Skarels if ((opt == IPOPT_SSRR && 57424813Skarels in_iaonnetof(in_netof(ipaddr.sin_addr)) == 0) || 57524813Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 57624813Skarels type = ICMP_UNREACH; 57724813Skarels code = ICMP_UNREACH_SRCFAIL; 5784951Swnj goto bad; 57924813Skarels } 58024813Skarels ip->ip_dst = ipaddr.sin_addr; 58126384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 58226384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 58324813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 5844640Swnj break; 5854495Swnj 58624813Skarels case IPOPT_RR: 58724813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 58824813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 58924813Skarels goto bad; 59024813Skarels } 59124813Skarels /* 59224813Skarels * If no space remains, ignore. 59324813Skarels */ 59424813Skarels off--; /* 0 origin */ 59524813Skarels if (off > optlen - sizeof(struct in_addr)) 59624813Skarels break; 59726384Skarels bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 59824813Skarels sizeof(ipaddr.sin_addr)); 59924813Skarels /* 60024813Skarels * locate outgoing interface 60124813Skarels */ 60224813Skarels if ((ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 60324813Skarels type = ICMP_UNREACH; 60424813Skarels code = ICMP_UNREACH_SRCFAIL; 60524813Skarels goto bad; 60624813Skarels } 60726384Skarels bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 60826384Skarels (caddr_t)(cp + off), sizeof(struct in_addr)); 60924813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 61024813Skarels break; 61124813Skarels 6124640Swnj case IPOPT_TS: 6136583Ssam code = cp - (u_char *)ip; 6144801Swnj ipt = (struct ip_timestamp *)cp; 6154801Swnj if (ipt->ipt_len < 5) 6164640Swnj goto bad; 6174801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 6184801Swnj if (++ipt->ipt_oflw == 0) 6194640Swnj goto bad; 6204495Swnj break; 6214640Swnj } 622*30925Skarels sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 6234801Swnj switch (ipt->ipt_flg) { 6244495Swnj 6254640Swnj case IPOPT_TS_TSONLY: 6264640Swnj break; 6274640Swnj 6284640Swnj case IPOPT_TS_TSANDADDR: 62924813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 63024813Skarels sizeof(struct in_addr) > ipt->ipt_len) 6314640Swnj goto bad; 632*30925Skarels ia = ifptoia(ifp); 633*30925Skarels bcopy((caddr_t)&IA_SIN(ia)->sin_addr, 63424813Skarels (caddr_t)sin, sizeof(struct in_addr)); 635*30925Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6364640Swnj break; 6374640Swnj 6384640Swnj case IPOPT_TS_PRESPEC: 639*30925Skarels if (ipt->ipt_ptr + sizeof(n_time) + 640*30925Skarels sizeof(struct in_addr) > ipt->ipt_len) 641*30925Skarels goto bad; 64224813Skarels bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 64324813Skarels sizeof(struct in_addr)); 64418376Skarels if (ifa_ifwithaddr((struct sockaddr *)&ipaddr) == 0) 6454951Swnj continue; 64624813Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6474640Swnj break; 6484640Swnj 6494495Swnj default: 6504640Swnj goto bad; 6514495Swnj } 65224813Skarels ntime = iptime(); 653*30925Skarels bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 654*30925Skarels sizeof(n_time)); 65524813Skarels ipt->ipt_ptr += sizeof(n_time); 6564640Swnj } 6574495Swnj } 6586583Ssam return (0); 6594640Swnj bad: 66026031Skarels icmp_error(ip, type, code, ifp); 6616583Ssam return (1); 6624495Swnj } 6634495Swnj 6644640Swnj /* 66524813Skarels * Given address of next destination (final or next hop), 66624813Skarels * return internet address info of interface to be used to get there. 66724813Skarels */ 66824813Skarels struct in_ifaddr * 66924813Skarels ip_rtaddr(dst) 67024813Skarels struct in_addr dst; 67124813Skarels { 67224813Skarels register struct sockaddr_in *sin; 67324813Skarels register struct in_ifaddr *ia; 67424813Skarels 67524813Skarels sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 67624813Skarels 67724813Skarels if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 67824813Skarels if (ipforward_rt.ro_rt) { 67924813Skarels RTFREE(ipforward_rt.ro_rt); 68024813Skarels ipforward_rt.ro_rt = 0; 68124813Skarels } 68224813Skarels sin->sin_family = AF_INET; 68324813Skarels sin->sin_addr = dst; 68424813Skarels 68524813Skarels rtalloc(&ipforward_rt); 68624813Skarels } 68724813Skarels if (ipforward_rt.ro_rt == 0) 68824813Skarels return ((struct in_ifaddr *)0); 68924813Skarels /* 69024813Skarels * Find address associated with outgoing interface. 69124813Skarels */ 69224813Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) 69324813Skarels if (ia->ia_ifp == ipforward_rt.ro_rt->rt_ifp) 69424813Skarels break; 69524813Skarels return (ia); 69624813Skarels } 69724813Skarels 69824813Skarels /* 69924813Skarels * Save incoming source route for use in replies, 70024813Skarels * to be picked up later by ip_srcroute if the receiver is interested. 70124813Skarels */ 70224813Skarels save_rte(option, dst) 70326384Skarels u_char *option; 70424813Skarels struct in_addr dst; 70524813Skarels { 70626384Skarels unsigned olen; 70724813Skarels extern ipprintfs; 70824813Skarels 70924813Skarels olen = option[IPOPT_OLEN]; 71024813Skarels if (olen > sizeof(ip_srcrt) - 1) { 71124813Skarels if (ipprintfs) 71224813Skarels printf("save_rte: olen %d\n", olen); 71324813Skarels return; 71424813Skarels } 71526384Skarels bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 71624813Skarels ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 71724813Skarels ip_srcrt.route[ip_nhops++] = dst; 71824813Skarels } 71924813Skarels 72024813Skarels /* 72124813Skarels * Retrieve incoming source route for use in replies, 72224813Skarels * in the same form used by setsockopt. 72324813Skarels * The first hop is placed before the options, will be removed later. 72424813Skarels */ 72524813Skarels struct mbuf * 72624813Skarels ip_srcroute() 72724813Skarels { 72824813Skarels register struct in_addr *p, *q; 72924813Skarels register struct mbuf *m; 73024813Skarels 73124813Skarels if (ip_nhops == 0) 73224813Skarels return ((struct mbuf *)0); 73324813Skarels m = m_get(M_WAIT, MT_SOOPTS); 73424813Skarels m->m_len = ip_nhops * sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1; 73524813Skarels 73624813Skarels /* 73724813Skarels * First save first hop for return route 73824813Skarels */ 73924813Skarels p = &ip_srcrt.route[ip_nhops - 1]; 74024813Skarels *(mtod(m, struct in_addr *)) = *p--; 74124813Skarels 74224813Skarels /* 74324813Skarels * Copy option fields and padding (nop) to mbuf. 74424813Skarels */ 74524813Skarels ip_srcrt.nop = IPOPT_NOP; 74624813Skarels bcopy((caddr_t)&ip_srcrt, mtod(m, caddr_t) + sizeof(struct in_addr), 74724813Skarels IPOPT_OFFSET + 1 + 1); 74824813Skarels q = (struct in_addr *)(mtod(m, caddr_t) + 74924813Skarels sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1); 75024813Skarels /* 75124813Skarels * Record return path as an IP source route, 75224813Skarels * reversing the path (pointers are now aligned). 75324813Skarels */ 75424813Skarels while (p >= ip_srcrt.route) 75524813Skarels *q++ = *p--; 75624813Skarels return (m); 75724813Skarels } 75824813Skarels 75924813Skarels /* 7604951Swnj * Strip out IP options, at higher 7614951Swnj * level protocol in the kernel. 7624951Swnj * Second argument is buffer to which options 7634951Swnj * will be moved, and return value is their length. 7644640Swnj */ 7655217Swnj ip_stripoptions(ip, mopt) 7664640Swnj struct ip *ip; 7675217Swnj struct mbuf *mopt; 7684495Swnj { 7694640Swnj register int i; 7704640Swnj register struct mbuf *m; 77124813Skarels register caddr_t opts; 7724640Swnj int olen; 7734640Swnj 7744640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 7754951Swnj m = dtom(ip); 77624813Skarels opts = (caddr_t)(ip + 1); 7775217Swnj if (mopt) { 7785217Swnj mopt->m_len = olen; 7795217Swnj mopt->m_off = MMINOFF; 78024813Skarels bcopy(opts, mtod(mopt, caddr_t), (unsigned)olen); 7815217Swnj } 7824640Swnj i = m->m_len - (sizeof (struct ip) + olen); 78324813Skarels bcopy(opts + olen, opts, (unsigned)i); 7845243Sroot m->m_len -= olen; 78524813Skarels ip->ip_hl = sizeof(struct ip) >> 2; 7864495Swnj } 7876583Ssam 78814670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 78924813Skarels 0, 0, 0, 0, 79014670Ssam 0, 0, EHOSTDOWN, EHOSTUNREACH, 79114670Ssam ENETUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 79224813Skarels EMSGSIZE, EHOSTUNREACH, 0, 0, 79324813Skarels 0, 0, 0, 0, 79424813Skarels ENOPROTOOPT 7956583Ssam }; 7966583Ssam 79724813Skarels #ifndef IPFORWARDING 79824813Skarels #define IPFORWARDING 1 79924813Skarels #endif 80024813Skarels #ifndef IPSENDREDIRECTS 80124813Skarels #define IPSENDREDIRECTS 1 80224813Skarels #endif 8036583Ssam int ipprintfs = 0; 80424813Skarels int ipforwarding = IPFORWARDING; 80524813Skarels extern int in_interfaces; 80624813Skarels int ipsendredirects = IPSENDREDIRECTS; 80724813Skarels 8086583Ssam /* 8096583Ssam * Forward a packet. If some error occurs return the sender 81018376Skarels * an icmp packet. Note we can't always generate a meaningful 81124813Skarels * icmp message because icmp doesn't have a large enough repertoire 8126583Ssam * of codes and types. 81326308Skarels * 81426308Skarels * If not forwarding (possibly because we have only a single external 81526308Skarels * network), just drop the packet. This could be confusing if ipforwarding 81626308Skarels * was zero but some routing protocol was advancing us as a gateway 81726308Skarels * to somewhere. However, we must let the routing protocol deal with that. 8186583Ssam */ 81924813Skarels ip_forward(ip, ifp) 8206583Ssam register struct ip *ip; 82124813Skarels struct ifnet *ifp; 8226583Ssam { 82324813Skarels register int error, type = 0, code; 82424813Skarels register struct sockaddr_in *sin; 82518376Skarels struct mbuf *mcopy; 82624813Skarels struct in_addr dest; 8276583Ssam 82824813Skarels dest.s_addr = 0; 8296583Ssam if (ipprintfs) 8306583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 8316583Ssam ip->ip_dst, ip->ip_ttl); 83218376Skarels ip->ip_id = htons(ip->ip_id); 83324813Skarels if (ipforwarding == 0 || in_interfaces <= 1) { 83426308Skarels ipstat.ips_cantforward++; 83526985Skarels #ifdef GATEWAY 83626985Skarels type = ICMP_UNREACH, code = ICMP_UNREACH_NET; 83726985Skarels goto sendicmp; 83826985Skarels #else 83926308Skarels m_freem(dtom(ip)); 84026308Skarels return; 84126985Skarels #endif 8426583Ssam } 8436583Ssam if (ip->ip_ttl < IPTTLDEC) { 8446583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 8456583Ssam goto sendicmp; 8466583Ssam } 8476583Ssam ip->ip_ttl -= IPTTLDEC; 8486609Ssam 8496609Ssam /* 8506609Ssam * Save at most 64 bytes of the packet in case 8516609Ssam * we need to generate an ICMP message to the src. 8526609Ssam */ 85328944Skarels mcopy = m_copy(dtom(ip), 0, imin((int)ip->ip_len, 64)); 8546583Ssam 85524813Skarels sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 85624813Skarels if (ipforward_rt.ro_rt == 0 || 85724813Skarels ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 85824813Skarels if (ipforward_rt.ro_rt) { 85924813Skarels RTFREE(ipforward_rt.ro_rt); 86024813Skarels ipforward_rt.ro_rt = 0; 86124813Skarels } 86224813Skarels sin->sin_family = AF_INET; 86324813Skarels sin->sin_addr = ip->ip_dst; 86424813Skarels 86524813Skarels rtalloc(&ipforward_rt); 86624813Skarels } 86724813Skarels /* 86824813Skarels * If forwarding packet using same interface that it came in on, 86924813Skarels * perhaps should send a redirect to sender to shortcut a hop. 87024813Skarels * Only send redirect if source is sending directly to us, 87124813Skarels * and if packet was not source routed (or has any options). 87230447Skarels * Also, don't send redirect if forwarding using a default route 87330447Skarels * or a route modfied by a redirect. 87424813Skarels */ 87530447Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 87624813Skarels if (ipforward_rt.ro_rt && ipforward_rt.ro_rt->rt_ifp == ifp && 87730447Skarels (ipforward_rt.ro_rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 87830447Skarels satosin(&ipforward_rt.ro_rt->rt_dst)->sin_addr.s_addr != 0 && 87924813Skarels ipsendredirects && ip->ip_hl == (sizeof(struct ip) >> 2)) { 88024813Skarels struct in_ifaddr *ia; 88124813Skarels u_long src = ntohl(ip->ip_src.s_addr); 88224813Skarels u_long dst = ntohl(ip->ip_dst.s_addr); 88324813Skarels 88424813Skarels if ((ia = ifptoia(ifp)) && 88524813Skarels (src & ia->ia_subnetmask) == ia->ia_subnet) { 88624813Skarels if (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) 88724813Skarels dest = satosin(&ipforward_rt.ro_rt->rt_gateway)->sin_addr; 88824813Skarels else 88924813Skarels dest = ip->ip_dst; 89024813Skarels /* 89124813Skarels * If the destination is reached by a route to host, 89227145Skarels * is on a subnet of a local net, or is directly 89327145Skarels * on the attached net (!), use host redirect. 89424813Skarels * (We may be the correct first hop for other subnets.) 89524813Skarels */ 89624813Skarels type = ICMP_REDIRECT; 89724813Skarels code = ICMP_REDIRECT_NET; 89824813Skarels if ((ipforward_rt.ro_rt->rt_flags & RTF_HOST) || 89924813Skarels (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) == 0) 90024813Skarels code = ICMP_REDIRECT_HOST; 90124813Skarels else for (ia = in_ifaddr; ia = ia->ia_next; ) 90224813Skarels if ((dst & ia->ia_netmask) == ia->ia_net) { 90327145Skarels if (ia->ia_subnetmask != ia->ia_netmask) 90427145Skarels code = ICMP_REDIRECT_HOST; 90524813Skarels break; 90624813Skarels } 90724813Skarels if (ipprintfs) 90824813Skarels printf("redirect (%d) to %x\n", code, dest); 90924813Skarels } 91024813Skarels } 91124813Skarels 91224813Skarels error = ip_output(dtom(ip), (struct mbuf *)0, &ipforward_rt, 91318376Skarels IP_FORWARDING); 91424813Skarels if (error) 91524813Skarels ipstat.ips_cantforward++; 91624813Skarels else if (type) 91724813Skarels ipstat.ips_redirectsent++; 91824813Skarels else { 9196609Ssam if (mcopy) 9206609Ssam m_freem(mcopy); 92121117Skarels ipstat.ips_forward++; 9226583Ssam return; 9236609Ssam } 92411540Ssam if (mcopy == NULL) 92511540Ssam return; 9266609Ssam ip = mtod(mcopy, struct ip *); 92724813Skarels type = ICMP_UNREACH; 9286609Ssam switch (error) { 9296609Ssam 93024813Skarels case 0: /* forwarded, but need redirect */ 93124813Skarels type = ICMP_REDIRECT; 93224813Skarels /* code set above */ 93324813Skarels break; 93424813Skarels 9356609Ssam case ENETUNREACH: 9366609Ssam case ENETDOWN: 9376583Ssam code = ICMP_UNREACH_NET; 9386609Ssam break; 9396609Ssam 9406609Ssam case EMSGSIZE: 9416583Ssam code = ICMP_UNREACH_NEEDFRAG; 9426609Ssam break; 9436609Ssam 9446609Ssam case EPERM: 9456609Ssam code = ICMP_UNREACH_PORT; 9466609Ssam break; 9476609Ssam 9486609Ssam case ENOBUFS: 9496609Ssam type = ICMP_SOURCEQUENCH; 9506609Ssam break; 9516609Ssam 9526609Ssam case EHOSTDOWN: 9536609Ssam case EHOSTUNREACH: 9546609Ssam code = ICMP_UNREACH_HOST; 9556609Ssam break; 9566609Ssam } 9576583Ssam sendicmp: 95826031Skarels icmp_error(ip, type, code, ifp, dest); 9596583Ssam } 960