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*25920Skarels * @(#)ip_input.c 6.14 (Berkeley) 01/20/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; 104*25920Skarels ipstat.ips_total++; 1055306Sroot if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) && 10611232Ssam (m = m_pullup(m, sizeof (struct ip))) == 0) { 10711232Ssam ipstat.ips_toosmall++; 10811232Ssam goto next; 10911232Ssam } 1104640Swnj ip = mtod(m, struct ip *); 11118376Skarels hlen = ip->ip_hl << 2; 11224813Skarels if (hlen < sizeof(struct ip)) { /* minimum header length */ 11318376Skarels ipstat.ips_badhlen++; 11421117Skarels goto bad; 11518376Skarels } 11618376Skarels if (hlen > m->m_len) { 11711232Ssam if ((m = m_pullup(m, hlen)) == 0) { 11811232Ssam ipstat.ips_badhlen++; 11911232Ssam goto next; 12011232Ssam } 1215161Swnj ip = mtod(m, struct ip *); 1225161Swnj } 1234951Swnj if (ipcksum) 1245217Swnj if (ip->ip_sum = in_cksum(m, hlen)) { 1254951Swnj ipstat.ips_badsum++; 1264951Swnj goto bad; 1274495Swnj } 1284951Swnj 1294951Swnj /* 1304951Swnj * Convert fields to host representation. 1314951Swnj */ 1324907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 13311232Ssam if (ip->ip_len < hlen) { 13411232Ssam ipstat.ips_badlen++; 13511232Ssam goto bad; 13611232Ssam } 1374640Swnj ip->ip_id = ntohs(ip->ip_id); 1384951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 1394495Swnj 1404543Swnj /* 1414640Swnj * Check that the amount of data in the buffers 1424640Swnj * is as at least much as the IP header would have us expect. 1434640Swnj * Trim mbufs if longer than we expect. 1444640Swnj * Drop packet if shorter than we expect. 1454543Swnj */ 14624813Skarels i = -(u_short)ip->ip_len; 1475161Swnj m0 = m; 1486475Sroot for (;;) { 1494495Swnj i += m->m_len; 1506475Sroot if (m->m_next == 0) 1516475Sroot break; 1526475Sroot m = m->m_next; 1536088Sroot } 1546475Sroot if (i != 0) { 1556475Sroot if (i < 0) { 1565161Swnj ipstat.ips_tooshort++; 15717358Skarels m = m0; 1584951Swnj goto bad; 1595161Swnj } 1606475Sroot if (i <= m->m_len) 1616475Sroot m->m_len -= i; 1626475Sroot else 1636475Sroot m_adj(m0, -i); 1644495Swnj } 1656475Sroot m = m0; 1664495Swnj 1674640Swnj /* 1684640Swnj * Process options and, if not destined for us, 1696583Ssam * ship it on. ip_dooptions returns 1 when an 1706583Ssam * error was detected (causing an icmp message 17121117Skarels * to be sent and the original packet to be freed). 1724640Swnj */ 17324813Skarels ip_nhops = 0; /* for source routed packets */ 1746583Ssam if (hlen > sizeof (struct ip) && ip_dooptions(ip)) 1756583Ssam goto next; 1766210Swnj 1776338Ssam /* 17818376Skarels * Check our list of addresses, to see if the packet is for us. 1796338Ssam */ 18018376Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) { 18118376Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 1826338Ssam 18318376Skarels if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 18424813Skarels goto ours; 18525195Skarels if ( 18625195Skarels #ifdef DIRECTED_BROADCAST 18725195Skarels ia->ia_ifp == ifp && 18825195Skarels #endif 18925195Skarels (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 19025195Skarels u_long i; 19125195Skarels 19225195Skarels if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 19325195Skarels ip->ip_dst.s_addr) 19425195Skarels goto ours; 19525195Skarels if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 19625195Skarels goto ours; 19725195Skarels /* 19825195Skarels * Look for all-0's host part (old broadcast addr), 19925195Skarels * either for subnet or net. 20025195Skarels */ 20125195Skarels i = ntohl(ip->ip_dst.s_addr); 20225195Skarels if (i == ia->ia_subnet) 20325195Skarels goto ours; 20425195Skarels if (i == ia->ia_net) 20525195Skarels goto ours; 20625195Skarels } 2076338Ssam } 20824813Skarels if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 20924813Skarels goto ours; 21024813Skarels if (ip->ip_dst.s_addr == INADDR_ANY) 21124813Skarels goto ours; 2124495Swnj 2134640Swnj /* 21424813Skarels * Not for us; forward if possible and desirable. 21524813Skarels */ 21624813Skarels ip_forward(ip, ifp); 21724813Skarels goto next; 21824813Skarels 21924813Skarels ours: 22024813Skarels /* 2214640Swnj * Look for queue of fragments 2224640Swnj * of this datagram. 2234640Swnj */ 2244640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 2254640Swnj if (ip->ip_id == fp->ipq_id && 2264640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 2274640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 2284640Swnj ip->ip_p == fp->ipq_p) 2294640Swnj goto found; 2304640Swnj fp = 0; 2314640Swnj found: 2324495Swnj 2334640Swnj /* 2344640Swnj * Adjust ip_len to not reflect header, 2354640Swnj * set ip_mff if more fragments are expected, 2364640Swnj * convert offset of this to bytes. 2374640Swnj */ 2384640Swnj ip->ip_len -= hlen; 2394898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 2404640Swnj if (ip->ip_off & IP_MF) 2414898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 2424640Swnj ip->ip_off <<= 3; 2434495Swnj 2444640Swnj /* 2454640Swnj * If datagram marked as having more fragments 2464640Swnj * or if this is not the first fragment, 2474640Swnj * attempt reassembly; if it succeeds, proceed. 2484640Swnj */ 2494898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 25024813Skarels ipstat.ips_fragments++; 2514898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 2524640Swnj if (ip == 0) 2535084Swnj goto next; 2544640Swnj m = dtom(ip); 2554640Swnj } else 2564640Swnj if (fp) 25710735Ssam ip_freef(fp); 2584951Swnj 2594951Swnj /* 2604951Swnj * Switch out to protocol's input routine. 2614951Swnj */ 26224813Skarels (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, ifp); 2635084Swnj goto next; 2644951Swnj bad: 2654951Swnj m_freem(m); 2665084Swnj goto next; 2674640Swnj } 2684495Swnj 2694640Swnj /* 2704640Swnj * Take incoming datagram fragment and try to 2714951Swnj * reassemble it into whole datagram. If a chain for 2724640Swnj * reassembly of this datagram already exists, then it 2734640Swnj * is given as fp; otherwise have to make a chain. 2744640Swnj */ 2754640Swnj struct ip * 2764640Swnj ip_reass(ip, fp) 2774898Swnj register struct ipasfrag *ip; 2784640Swnj register struct ipq *fp; 2794640Swnj { 2804640Swnj register struct mbuf *m = dtom(ip); 2814898Swnj register struct ipasfrag *q; 2824640Swnj struct mbuf *t; 2834640Swnj int hlen = ip->ip_hl << 2; 2844640Swnj int i, next; 2854543Swnj 2864640Swnj /* 2874640Swnj * Presence of header sizes in mbufs 2884640Swnj * would confuse code below. 2894640Swnj */ 2904640Swnj m->m_off += hlen; 2914640Swnj m->m_len -= hlen; 2924495Swnj 2934640Swnj /* 2944640Swnj * If first fragment to arrive, create a reassembly queue. 2954640Swnj */ 2964640Swnj if (fp == 0) { 2979641Ssam if ((t = m_get(M_WAIT, MT_FTABLE)) == NULL) 2984640Swnj goto dropfrag; 2994640Swnj fp = mtod(t, struct ipq *); 3004640Swnj insque(fp, &ipq); 3014640Swnj fp->ipq_ttl = IPFRAGTTL; 3024640Swnj fp->ipq_p = ip->ip_p; 3034640Swnj fp->ipq_id = ip->ip_id; 3044898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 3054898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 3064898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 3075161Swnj q = (struct ipasfrag *)fp; 3085161Swnj goto insert; 3094640Swnj } 3104495Swnj 3114640Swnj /* 3124640Swnj * Find a segment which begins after this one does. 3134640Swnj */ 3144898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3154640Swnj if (q->ip_off > ip->ip_off) 3164640Swnj break; 3174495Swnj 3184640Swnj /* 3194640Swnj * If there is a preceding segment, it may provide some of 3204640Swnj * our data already. If so, drop the data from the incoming 3214640Swnj * segment. If it provides all of our data, drop us. 3224640Swnj */ 3234898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 3244898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 3254640Swnj if (i > 0) { 3264640Swnj if (i >= ip->ip_len) 3274640Swnj goto dropfrag; 3284640Swnj m_adj(dtom(ip), i); 3294640Swnj ip->ip_off += i; 3304640Swnj ip->ip_len -= i; 3314640Swnj } 3324640Swnj } 3334543Swnj 3344640Swnj /* 3354640Swnj * While we overlap succeeding segments trim them or, 3364640Swnj * if they are completely covered, dequeue them. 3374640Swnj */ 3384898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 3394640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 3404640Swnj if (i < q->ip_len) { 3414640Swnj q->ip_len -= i; 3426256Sroot q->ip_off += i; 3434640Swnj m_adj(dtom(q), i); 3444640Swnj break; 3454495Swnj } 3464898Swnj q = q->ipf_next; 3474898Swnj m_freem(dtom(q->ipf_prev)); 3484898Swnj ip_deq(q->ipf_prev); 3494543Swnj } 3504495Swnj 3515161Swnj insert: 3524640Swnj /* 3534640Swnj * Stick new segment in its place; 3544640Swnj * check for complete reassembly. 3554640Swnj */ 3564898Swnj ip_enq(ip, q->ipf_prev); 3574640Swnj next = 0; 3584898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 3594640Swnj if (q->ip_off != next) 3604640Swnj return (0); 3614640Swnj next += q->ip_len; 3624640Swnj } 3634898Swnj if (q->ipf_prev->ipf_mff) 3644640Swnj return (0); 3654495Swnj 3664640Swnj /* 3674640Swnj * Reassembly is complete; concatenate fragments. 3684640Swnj */ 3694640Swnj q = fp->ipq_next; 3704640Swnj m = dtom(q); 3714640Swnj t = m->m_next; 3724640Swnj m->m_next = 0; 3734640Swnj m_cat(m, t); 3746298Swnj q = q->ipf_next; 3756298Swnj while (q != (struct ipasfrag *)fp) { 3766298Swnj t = dtom(q); 3776298Swnj q = q->ipf_next; 3786298Swnj m_cat(m, t); 3796298Swnj } 3804495Swnj 3814640Swnj /* 3824640Swnj * Create header for new ip packet by 3834640Swnj * modifying header of first packet; 3844640Swnj * dequeue and discard fragment reassembly header. 3854640Swnj * Make header visible. 3864640Swnj */ 3874640Swnj ip = fp->ipq_next; 3884640Swnj ip->ip_len = next; 3894898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 3904898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 3914640Swnj remque(fp); 3924907Swnj (void) m_free(dtom(fp)); 3934640Swnj m = dtom(ip); 39424813Skarels m->m_len += (ip->ip_hl << 2); 39524813Skarels m->m_off -= (ip->ip_hl << 2); 3964898Swnj return ((struct ip *)ip); 3974495Swnj 3984640Swnj dropfrag: 39924813Skarels ipstat.ips_fragdropped++; 4004640Swnj m_freem(m); 4014640Swnj return (0); 4024495Swnj } 4034495Swnj 4044640Swnj /* 4054640Swnj * Free a fragment reassembly header and all 4064640Swnj * associated datagrams. 4074640Swnj */ 4084640Swnj ip_freef(fp) 4094640Swnj struct ipq *fp; 4104495Swnj { 41110735Ssam register struct ipasfrag *q, *p; 4124495Swnj 41310735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 41410735Ssam p = q->ipf_next; 41510735Ssam ip_deq(q); 4164640Swnj m_freem(dtom(q)); 41710735Ssam } 41810735Ssam remque(fp); 41910735Ssam (void) m_free(dtom(fp)); 4204495Swnj } 4214495Swnj 4224640Swnj /* 4234640Swnj * Put an ip fragment on a reassembly chain. 4244640Swnj * Like insque, but pointers in middle of structure. 4254640Swnj */ 4264640Swnj ip_enq(p, prev) 4274898Swnj register struct ipasfrag *p, *prev; 4284495Swnj { 4294951Swnj 4304898Swnj p->ipf_prev = prev; 4314898Swnj p->ipf_next = prev->ipf_next; 4324898Swnj prev->ipf_next->ipf_prev = p; 4334898Swnj prev->ipf_next = p; 4344495Swnj } 4354495Swnj 4364640Swnj /* 4374640Swnj * To ip_enq as remque is to insque. 4384640Swnj */ 4394640Swnj ip_deq(p) 4404898Swnj register struct ipasfrag *p; 4414640Swnj { 4424951Swnj 4434898Swnj p->ipf_prev->ipf_next = p->ipf_next; 4444898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 4454495Swnj } 4464495Swnj 4474640Swnj /* 4484640Swnj * IP timer processing; 4494640Swnj * if a timer expires on a reassembly 4504640Swnj * queue, discard it. 4514640Swnj */ 4524801Swnj ip_slowtimo() 4534495Swnj { 4544495Swnj register struct ipq *fp; 4554640Swnj int s = splnet(); 4564951Swnj 4575243Sroot fp = ipq.next; 4585243Sroot if (fp == 0) { 4595243Sroot splx(s); 4605243Sroot return; 4615243Sroot } 46210735Ssam while (fp != &ipq) { 46310735Ssam --fp->ipq_ttl; 46410735Ssam fp = fp->next; 46524813Skarels if (fp->prev->ipq_ttl == 0) { 46624813Skarels ipstat.ips_fragtimeout++; 46710735Ssam ip_freef(fp->prev); 46824813Skarels } 46910735Ssam } 4704640Swnj splx(s); 4714495Swnj } 4724495Swnj 4734951Swnj /* 4744951Swnj * Drain off all datagram fragments. 4754951Swnj */ 4764801Swnj ip_drain() 4774801Swnj { 4784801Swnj 47924813Skarels while (ipq.next != &ipq) { 48024813Skarels ipstat.ips_fragdropped++; 48110735Ssam ip_freef(ipq.next); 48224813Skarels } 4834801Swnj } 4844923Swnj 48524813Skarels struct in_ifaddr *ip_rtaddr(); 48624813Skarels 4874640Swnj /* 4884640Swnj * Do option processing on a datagram, 4894640Swnj * possibly discarding it if bad options 4904640Swnj * are encountered. 4914640Swnj */ 4924640Swnj ip_dooptions(ip) 4934640Swnj struct ip *ip; 4944495Swnj { 4954640Swnj register u_char *cp; 49624813Skarels int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB; 49724813Skarels register struct ip_timestamp *ipt; 49824813Skarels register struct in_ifaddr *ia; 4994923Swnj struct in_addr *sin; 50024813Skarels n_time ntime; 5014495Swnj 5024640Swnj cp = (u_char *)(ip + 1); 5034640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 5044640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 50524813Skarels opt = cp[IPOPT_OPTVAL]; 5064640Swnj if (opt == IPOPT_EOL) 5074640Swnj break; 5084640Swnj if (opt == IPOPT_NOP) 5094640Swnj optlen = 1; 51016392Ssam else { 51124813Skarels optlen = cp[IPOPT_OLEN]; 51224813Skarels if (optlen <= 0 || optlen > cnt) { 51324813Skarels code = &cp[IPOPT_OLEN] - (u_char *)ip; 51417551Skarels goto bad; 51524813Skarels } 51616392Ssam } 5174640Swnj switch (opt) { 5184495Swnj 5194640Swnj default: 5204640Swnj break; 5214495Swnj 5224951Swnj /* 5234951Swnj * Source routing with record. 5244951Swnj * Find interface with current destination address. 5254951Swnj * If none on this machine then drop if strictly routed, 5264951Swnj * or do nothing if loosely routed. 5274951Swnj * Record interface address and bring up next address 5284951Swnj * component. If strictly routed make sure next 5294951Swnj * address on directly accessible net. 5304951Swnj */ 5314640Swnj case IPOPT_LSRR: 5327508Sroot case IPOPT_SSRR: 53324813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 53424813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 53524813Skarels goto bad; 53624813Skarels } 53724813Skarels ipaddr.sin_addr = ip->ip_dst; 53824813Skarels ia = (struct in_ifaddr *) 53924813Skarels ifa_ifwithaddr((struct sockaddr *)&ipaddr); 54024813Skarels if (ia == 0) { 54124813Skarels if (opt == IPOPT_SSRR) { 54224813Skarels type = ICMP_UNREACH; 54324813Skarels code = ICMP_UNREACH_SRCFAIL; 5444951Swnj goto bad; 54524813Skarels } 54624813Skarels /* 54724813Skarels * Loose routing, and not at next destination 54824813Skarels * yet; nothing to do except forward. 54924813Skarels */ 5504951Swnj break; 5514640Swnj } 55224813Skarels off--; /* 0 origin */ 55324813Skarels if (off > optlen - sizeof(struct in_addr)) { 55424813Skarels /* 55524813Skarels * End of source route. Should be for us. 55624813Skarels */ 55724813Skarels save_rte(cp, ip->ip_src); 5584951Swnj break; 55924813Skarels } 56024813Skarels /* 56124813Skarels * locate outgoing interface 56224813Skarels */ 56324813Skarels bcopy(cp + off, (caddr_t)&ipaddr.sin_addr, 56424813Skarels sizeof(ipaddr.sin_addr)); 56524813Skarels if ((opt == IPOPT_SSRR && 56624813Skarels in_iaonnetof(in_netof(ipaddr.sin_addr)) == 0) || 56724813Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 56824813Skarels type = ICMP_UNREACH; 56924813Skarels code = ICMP_UNREACH_SRCFAIL; 5704951Swnj goto bad; 57124813Skarels } 57224813Skarels ip->ip_dst = ipaddr.sin_addr; 57324813Skarels bcopy(&(IA_SIN(ia)->sin_addr), cp + off, 57424813Skarels sizeof(struct in_addr)); 57524813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 5764640Swnj break; 5774495Swnj 57824813Skarels case IPOPT_RR: 57924813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 58024813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 58124813Skarels goto bad; 58224813Skarels } 58324813Skarels /* 58424813Skarels * If no space remains, ignore. 58524813Skarels */ 58624813Skarels off--; /* 0 origin */ 58724813Skarels if (off > optlen - sizeof(struct in_addr)) 58824813Skarels break; 58924813Skarels bcopy(cp + off, (caddr_t)ipaddr.sin_addr, 59024813Skarels sizeof(ipaddr.sin_addr)); 59124813Skarels /* 59224813Skarels * locate outgoing interface 59324813Skarels */ 59424813Skarels if ((ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 59524813Skarels type = ICMP_UNREACH; 59624813Skarels code = ICMP_UNREACH_SRCFAIL; 59724813Skarels goto bad; 59824813Skarels } 59924813Skarels bcopy(&(IA_SIN(ia)->sin_addr), cp + off, 60024813Skarels sizeof(struct in_addr)); 60124813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 60224813Skarels break; 60324813Skarels 6044640Swnj case IPOPT_TS: 6056583Ssam code = cp - (u_char *)ip; 6064801Swnj ipt = (struct ip_timestamp *)cp; 6074801Swnj if (ipt->ipt_len < 5) 6084640Swnj goto bad; 6094801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 6104801Swnj if (++ipt->ipt_oflw == 0) 6114640Swnj goto bad; 6124495Swnj break; 6134640Swnj } 61424813Skarels sin = (struct in_addr *)(cp+cp[IPOPT_OFFSET]-1); 6154801Swnj switch (ipt->ipt_flg) { 6164495Swnj 6174640Swnj case IPOPT_TS_TSONLY: 6184640Swnj break; 6194640Swnj 6204640Swnj case IPOPT_TS_TSANDADDR: 62124813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 62224813Skarels sizeof(struct in_addr) > ipt->ipt_len) 6234640Swnj goto bad; 62418376Skarels if (in_ifaddr == 0) 6256338Ssam goto bad; /* ??? */ 62624813Skarels bcopy((caddr_t)&IA_SIN(in_ifaddr)->sin_addr, 62724813Skarels (caddr_t)sin, sizeof(struct in_addr)); 62824813Skarels sin++; 6294640Swnj break; 6304640Swnj 6314640Swnj case IPOPT_TS_PRESPEC: 63224813Skarels bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 63324813Skarels sizeof(struct in_addr)); 63418376Skarels if (ifa_ifwithaddr((struct sockaddr *)&ipaddr) == 0) 6354951Swnj continue; 63624813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 63724813Skarels sizeof(struct in_addr) > ipt->ipt_len) 6384640Swnj goto bad; 63924813Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6404640Swnj break; 6414640Swnj 6424495Swnj default: 6434640Swnj goto bad; 6444495Swnj } 64524813Skarels ntime = iptime(); 64624813Skarels bcopy((caddr_t)&ntime, (caddr_t)sin, sizeof(n_time)); 64724813Skarels ipt->ipt_ptr += sizeof(n_time); 6484640Swnj } 6494495Swnj } 6506583Ssam return (0); 6514640Swnj bad: 6526583Ssam icmp_error(ip, type, code); 6536583Ssam return (1); 6544495Swnj } 6554495Swnj 6564640Swnj /* 65724813Skarels * Given address of next destination (final or next hop), 65824813Skarels * return internet address info of interface to be used to get there. 65924813Skarels */ 66024813Skarels struct in_ifaddr * 66124813Skarels ip_rtaddr(dst) 66224813Skarels struct in_addr dst; 66324813Skarels { 66424813Skarels register struct sockaddr_in *sin; 66524813Skarels register struct in_ifaddr *ia; 66624813Skarels 66724813Skarels sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 66824813Skarels 66924813Skarels if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 67024813Skarels if (ipforward_rt.ro_rt) { 67124813Skarels RTFREE(ipforward_rt.ro_rt); 67224813Skarels ipforward_rt.ro_rt = 0; 67324813Skarels } 67424813Skarels sin->sin_family = AF_INET; 67524813Skarels sin->sin_addr = dst; 67624813Skarels 67724813Skarels rtalloc(&ipforward_rt); 67824813Skarels } 67924813Skarels if (ipforward_rt.ro_rt == 0) 68024813Skarels return ((struct in_ifaddr *)0); 68124813Skarels /* 68224813Skarels * Find address associated with outgoing interface. 68324813Skarels */ 68424813Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) 68524813Skarels if (ia->ia_ifp == ipforward_rt.ro_rt->rt_ifp) 68624813Skarels break; 68724813Skarels return (ia); 68824813Skarels } 68924813Skarels 69024813Skarels /* 69124813Skarels * Save incoming source route for use in replies, 69224813Skarels * to be picked up later by ip_srcroute if the receiver is interested. 69324813Skarels */ 69424813Skarels save_rte(option, dst) 69524813Skarels caddr_t option; 69624813Skarels struct in_addr dst; 69724813Skarels { 69824813Skarels int olen; 69924813Skarels extern ipprintfs; 70024813Skarels 70124813Skarels olen = option[IPOPT_OLEN]; 70224813Skarels if (olen > sizeof(ip_srcrt) - 1) { 70324813Skarels if (ipprintfs) 70424813Skarels printf("save_rte: olen %d\n", olen); 70524813Skarels return; 70624813Skarels } 70724813Skarels bcopy(option, (caddr_t)ip_srcrt.srcopt, olen); 70824813Skarels ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 70924813Skarels ip_srcrt.route[ip_nhops++] = dst; 71024813Skarels } 71124813Skarels 71224813Skarels /* 71324813Skarels * Retrieve incoming source route for use in replies, 71424813Skarels * in the same form used by setsockopt. 71524813Skarels * The first hop is placed before the options, will be removed later. 71624813Skarels */ 71724813Skarels struct mbuf * 71824813Skarels ip_srcroute() 71924813Skarels { 72024813Skarels register struct in_addr *p, *q; 72124813Skarels register struct mbuf *m; 72224813Skarels 72324813Skarels if (ip_nhops == 0) 72424813Skarels return ((struct mbuf *)0); 72524813Skarels m = m_get(M_WAIT, MT_SOOPTS); 72624813Skarels m->m_len = ip_nhops * sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1; 72724813Skarels 72824813Skarels /* 72924813Skarels * First save first hop for return route 73024813Skarels */ 73124813Skarels p = &ip_srcrt.route[ip_nhops - 1]; 73224813Skarels *(mtod(m, struct in_addr *)) = *p--; 73324813Skarels 73424813Skarels /* 73524813Skarels * Copy option fields and padding (nop) to mbuf. 73624813Skarels */ 73724813Skarels ip_srcrt.nop = IPOPT_NOP; 73824813Skarels bcopy((caddr_t)&ip_srcrt, mtod(m, caddr_t) + sizeof(struct in_addr), 73924813Skarels IPOPT_OFFSET + 1 + 1); 74024813Skarels q = (struct in_addr *)(mtod(m, caddr_t) + 74124813Skarels sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1); 74224813Skarels /* 74324813Skarels * Record return path as an IP source route, 74424813Skarels * reversing the path (pointers are now aligned). 74524813Skarels */ 74624813Skarels while (p >= ip_srcrt.route) 74724813Skarels *q++ = *p--; 74824813Skarels return (m); 74924813Skarels } 75024813Skarels 75124813Skarels /* 7524951Swnj * Strip out IP options, at higher 7534951Swnj * level protocol in the kernel. 7544951Swnj * Second argument is buffer to which options 7554951Swnj * will be moved, and return value is their length. 7564640Swnj */ 7575217Swnj ip_stripoptions(ip, mopt) 7584640Swnj struct ip *ip; 7595217Swnj struct mbuf *mopt; 7604495Swnj { 7614640Swnj register int i; 7624640Swnj register struct mbuf *m; 76324813Skarels register caddr_t opts; 7644640Swnj int olen; 7654640Swnj 7664640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 7674951Swnj m = dtom(ip); 76824813Skarels opts = (caddr_t)(ip + 1); 7695217Swnj if (mopt) { 7705217Swnj mopt->m_len = olen; 7715217Swnj mopt->m_off = MMINOFF; 77224813Skarels bcopy(opts, mtod(mopt, caddr_t), (unsigned)olen); 7735217Swnj } 7744640Swnj i = m->m_len - (sizeof (struct ip) + olen); 77524813Skarels bcopy(opts + olen, opts, (unsigned)i); 7765243Sroot m->m_len -= olen; 77724813Skarels ip->ip_hl = sizeof(struct ip) >> 2; 7784495Swnj } 7796583Ssam 78014670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 78124813Skarels 0, 0, 0, 0, 78214670Ssam 0, 0, EHOSTDOWN, EHOSTUNREACH, 78314670Ssam ENETUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 78424813Skarels EMSGSIZE, EHOSTUNREACH, 0, 0, 78524813Skarels 0, 0, 0, 0, 78624813Skarels ENOPROTOOPT 7876583Ssam }; 7886583Ssam 78924813Skarels #ifndef IPFORWARDING 79024813Skarels #define IPFORWARDING 1 79124813Skarels #endif 79224813Skarels #ifndef IPSENDREDIRECTS 79324813Skarels #define IPSENDREDIRECTS 1 79424813Skarels #endif 7956583Ssam int ipprintfs = 0; 79624813Skarels int ipforwarding = IPFORWARDING; 79724813Skarels extern int in_interfaces; 79824813Skarels int ipsendredirects = IPSENDREDIRECTS; 79924813Skarels 8006583Ssam /* 8016583Ssam * Forward a packet. If some error occurs return the sender 80218376Skarels * an icmp packet. Note we can't always generate a meaningful 80324813Skarels * icmp message because icmp doesn't have a large enough repertoire 8046583Ssam * of codes and types. 8056583Ssam */ 80624813Skarels ip_forward(ip, ifp) 8076583Ssam register struct ip *ip; 80824813Skarels struct ifnet *ifp; 8096583Ssam { 81024813Skarels register int error, type = 0, code; 81124813Skarels register struct sockaddr_in *sin; 81218376Skarels struct mbuf *mcopy; 81324813Skarels struct in_addr dest; 8146583Ssam 81524813Skarels #ifdef lint 81624813Skarels dest.s_addr = 0; 81724813Skarels #endif 8186583Ssam if (ipprintfs) 8196583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 8206583Ssam ip->ip_dst, ip->ip_ttl); 82118376Skarels ip->ip_id = htons(ip->ip_id); 82224813Skarels if (ipforwarding == 0 || in_interfaces <= 1) { 8236583Ssam /* can't tell difference between net and host */ 8246583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_NET; 8256583Ssam goto sendicmp; 8266583Ssam } 8276583Ssam if (ip->ip_ttl < IPTTLDEC) { 8286583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 8296583Ssam goto sendicmp; 8306583Ssam } 8316583Ssam ip->ip_ttl -= IPTTLDEC; 8326609Ssam 8336609Ssam /* 8346609Ssam * Save at most 64 bytes of the packet in case 8356609Ssam * we need to generate an ICMP message to the src. 8366609Ssam */ 8377843Sroot mcopy = m_copy(dtom(ip), 0, imin(ip->ip_len, 64)); 8386583Ssam 83924813Skarels sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 84024813Skarels if (ipforward_rt.ro_rt == 0 || 84124813Skarels ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 84224813Skarels if (ipforward_rt.ro_rt) { 84324813Skarels RTFREE(ipforward_rt.ro_rt); 84424813Skarels ipforward_rt.ro_rt = 0; 84524813Skarels } 84624813Skarels sin->sin_family = AF_INET; 84724813Skarels sin->sin_addr = ip->ip_dst; 84824813Skarels 84924813Skarels rtalloc(&ipforward_rt); 85024813Skarels } 85124813Skarels /* 85224813Skarels * If forwarding packet using same interface that it came in on, 85324813Skarels * perhaps should send a redirect to sender to shortcut a hop. 85424813Skarels * Only send redirect if source is sending directly to us, 85524813Skarels * and if packet was not source routed (or has any options). 85624813Skarels */ 85724813Skarels if (ipforward_rt.ro_rt && ipforward_rt.ro_rt->rt_ifp == ifp && 85824813Skarels ipsendredirects && ip->ip_hl == (sizeof(struct ip) >> 2)) { 85924813Skarels struct in_ifaddr *ia; 86024813Skarels extern struct in_ifaddr *ifptoia(); 86124813Skarels u_long src = ntohl(ip->ip_src.s_addr); 86224813Skarels u_long dst = ntohl(ip->ip_dst.s_addr); 86324813Skarels 86424813Skarels if ((ia = ifptoia(ifp)) && 86524813Skarels (src & ia->ia_subnetmask) == ia->ia_subnet) { 86624813Skarels if (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) 86724813Skarels dest = satosin(&ipforward_rt.ro_rt->rt_gateway)->sin_addr; 86824813Skarels else 86924813Skarels dest = ip->ip_dst; 87024813Skarels /* 87124813Skarels * If the destination is reached by a route to host, 87224813Skarels * is directly on the attached net (!), 87324813Skarels * or if the destination is on a subnet of a local net 87424813Skarels * not known to the source net, use host redirect. 87524813Skarels * (We may be the correct first hop for other subnets.) 87624813Skarels */ 87724813Skarels type = ICMP_REDIRECT; 87824813Skarels code = ICMP_REDIRECT_NET; 87924813Skarels if ((ipforward_rt.ro_rt->rt_flags & RTF_HOST) || 88024813Skarels (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) == 0) 88124813Skarels code = ICMP_REDIRECT_HOST; 88224813Skarels else for (ia = in_ifaddr; ia = ia->ia_next; ) 88324813Skarels if ((dst & ia->ia_netmask) == ia->ia_net) { 88424813Skarels if ((src & ia->ia_netmask) != ia->ia_net) 88524813Skarels code = ICMP_REDIRECT_HOST; 88624813Skarels break; 88724813Skarels } 88824813Skarels if (ipprintfs) 88924813Skarels printf("redirect (%d) to %x\n", code, dest); 89024813Skarels } 89124813Skarels } 89224813Skarels 89324813Skarels error = ip_output(dtom(ip), (struct mbuf *)0, &ipforward_rt, 89418376Skarels IP_FORWARDING); 89524813Skarels if (error) 89624813Skarels ipstat.ips_cantforward++; 89724813Skarels else if (type) 89824813Skarels ipstat.ips_redirectsent++; 89924813Skarels else { 9006609Ssam if (mcopy) 9016609Ssam m_freem(mcopy); 90221117Skarels ipstat.ips_forward++; 9036583Ssam return; 9046609Ssam } 90511540Ssam if (mcopy == NULL) 90611540Ssam return; 9076609Ssam ip = mtod(mcopy, struct ip *); 90824813Skarels type = ICMP_UNREACH; 9096609Ssam switch (error) { 9106609Ssam 91124813Skarels case 0: /* forwarded, but need redirect */ 91224813Skarels type = ICMP_REDIRECT; 91324813Skarels /* code set above */ 91424813Skarels break; 91524813Skarels 9166609Ssam case ENETUNREACH: 9176609Ssam case ENETDOWN: 9186583Ssam code = ICMP_UNREACH_NET; 9196609Ssam break; 9206609Ssam 9216609Ssam case EMSGSIZE: 9226583Ssam code = ICMP_UNREACH_NEEDFRAG; 9236609Ssam break; 9246609Ssam 9256609Ssam case EPERM: 9266609Ssam code = ICMP_UNREACH_PORT; 9276609Ssam break; 9286609Ssam 9296609Ssam case ENOBUFS: 9306609Ssam type = ICMP_SOURCEQUENCH; 9316609Ssam break; 9326609Ssam 9336609Ssam case EHOSTDOWN: 9346609Ssam case EHOSTUNREACH: 9356609Ssam code = ICMP_UNREACH_HOST; 9366609Ssam break; 9376609Ssam } 9386583Ssam sendicmp: 93924813Skarels icmp_error(ip, type, code, dest); 9406583Ssam } 941