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*24813Skarels * @(#)ip_input.c 6.12 (Berkeley) 09/16/85 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 /* 36*24813Skarels * We need to save the IP options in case a protocol wants to respond 37*24813Skarels * to an incoming packet over the same route if the packet got here 38*24813Skarels * using IP source routing. This allows connection establishment and 39*24813Skarels * maintenance when the remote end is on a network that is not known 40*24813Skarels * to us. 41*24813Skarels */ 42*24813Skarels int ip_nhops = 0; 43*24813Skarels static struct ip_srcrt { 44*24813Skarels char nop; /* one NOP to align */ 45*24813Skarels char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 46*24813Skarels struct in_addr route[MAX_IPOPTLEN]; 47*24813Skarels } ip_srcrt; 48*24813Skarels 49*24813Skarels /* 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 58*24813Skarels 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 }; 76*24813Skarels 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; 91*24813Skarels 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(); 100*24813Skarels IF_DEQUEUEIF(&ipintrq, m, ifp); 1015084Swnj splx(s); 1025218Swnj if (m == 0) 1035084Swnj return; 1045306Sroot if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct ip)) && 10511232Ssam (m = m_pullup(m, sizeof (struct ip))) == 0) { 10611232Ssam ipstat.ips_toosmall++; 10711232Ssam goto next; 10811232Ssam } 1094640Swnj ip = mtod(m, struct ip *); 11018376Skarels hlen = ip->ip_hl << 2; 111*24813Skarels if (hlen < sizeof(struct ip)) { /* minimum header length */ 11218376Skarels ipstat.ips_badhlen++; 11321117Skarels goto bad; 11418376Skarels } 11518376Skarels if (hlen > m->m_len) { 11611232Ssam if ((m = m_pullup(m, hlen)) == 0) { 11711232Ssam ipstat.ips_badhlen++; 11811232Ssam goto next; 11911232Ssam } 1205161Swnj ip = mtod(m, struct ip *); 1215161Swnj } 1224951Swnj if (ipcksum) 1235217Swnj if (ip->ip_sum = in_cksum(m, hlen)) { 1244951Swnj ipstat.ips_badsum++; 1254951Swnj goto bad; 1264495Swnj } 1274951Swnj 1284951Swnj /* 1294951Swnj * Convert fields to host representation. 1304951Swnj */ 1314907Swnj ip->ip_len = ntohs((u_short)ip->ip_len); 13211232Ssam if (ip->ip_len < hlen) { 13311232Ssam ipstat.ips_badlen++; 13411232Ssam goto bad; 13511232Ssam } 1364640Swnj ip->ip_id = ntohs(ip->ip_id); 1374951Swnj ip->ip_off = ntohs((u_short)ip->ip_off); 1384495Swnj 1394543Swnj /* 1404640Swnj * Check that the amount of data in the buffers 1414640Swnj * is as at least much as the IP header would have us expect. 1424640Swnj * Trim mbufs if longer than we expect. 1434640Swnj * Drop packet if shorter than we expect. 1444543Swnj */ 145*24813Skarels i = -(u_short)ip->ip_len; 1465161Swnj m0 = m; 1476475Sroot for (;;) { 1484495Swnj i += m->m_len; 1496475Sroot if (m->m_next == 0) 1506475Sroot break; 1516475Sroot m = m->m_next; 1526088Sroot } 1536475Sroot if (i != 0) { 1546475Sroot if (i < 0) { 1555161Swnj ipstat.ips_tooshort++; 15617358Skarels m = m0; 1574951Swnj goto bad; 1585161Swnj } 1596475Sroot if (i <= m->m_len) 1606475Sroot m->m_len -= i; 1616475Sroot else 1626475Sroot m_adj(m0, -i); 1634495Swnj } 1646475Sroot m = m0; 1654495Swnj 1664640Swnj /* 1674640Swnj * Process options and, if not destined for us, 1686583Ssam * ship it on. ip_dooptions returns 1 when an 1696583Ssam * error was detected (causing an icmp message 17021117Skarels * to be sent and the original packet to be freed). 1714640Swnj */ 172*24813Skarels ip_nhops = 0; /* for source routed packets */ 1736583Ssam if (hlen > sizeof (struct ip) && ip_dooptions(ip)) 1746583Ssam goto next; 1756210Swnj 1766338Ssam /* 17718376Skarels * Check our list of addresses, to see if the packet is for us. 1786338Ssam */ 17918376Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) { 18018376Skarels #define satosin(sa) ((struct sockaddr_in *)(sa)) 1816338Ssam 18218376Skarels if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 183*24813Skarels goto ours; 18418376Skarels if ((ia->ia_ifp->if_flags & IFF_BROADCAST) && 18518376Skarels satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 18618376Skarels ip->ip_dst.s_addr) 187*24813Skarels goto ours; 18818376Skarels /* 18918376Skarels * Look for all-0's host part (old broadcast addr). 19018376Skarels */ 19118376Skarels if ((ia->ia_ifp->if_flags & IFF_BROADCAST) && 19218376Skarels ia->ia_subnet == ntohl(ip->ip_dst.s_addr)) 193*24813Skarels goto ours; 1946338Ssam } 195*24813Skarels if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 196*24813Skarels goto ours; 197*24813Skarels if (ip->ip_dst.s_addr == INADDR_ANY) 198*24813Skarels goto ours; 1994495Swnj 2004640Swnj /* 201*24813Skarels * Not for us; forward if possible and desirable. 202*24813Skarels */ 203*24813Skarels ip_forward(ip, ifp); 204*24813Skarels goto next; 205*24813Skarels 206*24813Skarels ours: 207*24813Skarels /* 2084640Swnj * Look for queue of fragments 2094640Swnj * of this datagram. 2104640Swnj */ 2114640Swnj for (fp = ipq.next; fp != &ipq; fp = fp->next) 2124640Swnj if (ip->ip_id == fp->ipq_id && 2134640Swnj ip->ip_src.s_addr == fp->ipq_src.s_addr && 2144640Swnj ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 2154640Swnj ip->ip_p == fp->ipq_p) 2164640Swnj goto found; 2174640Swnj fp = 0; 2184640Swnj found: 2194495Swnj 2204640Swnj /* 2214640Swnj * Adjust ip_len to not reflect header, 2224640Swnj * set ip_mff if more fragments are expected, 2234640Swnj * convert offset of this to bytes. 2244640Swnj */ 2254640Swnj ip->ip_len -= hlen; 2264898Swnj ((struct ipasfrag *)ip)->ipf_mff = 0; 2274640Swnj if (ip->ip_off & IP_MF) 2284898Swnj ((struct ipasfrag *)ip)->ipf_mff = 1; 2294640Swnj ip->ip_off <<= 3; 2304495Swnj 2314640Swnj /* 2324640Swnj * If datagram marked as having more fragments 2334640Swnj * or if this is not the first fragment, 2344640Swnj * attempt reassembly; if it succeeds, proceed. 2354640Swnj */ 2364898Swnj if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 237*24813Skarels ipstat.ips_fragments++; 2384898Swnj ip = ip_reass((struct ipasfrag *)ip, fp); 2394640Swnj if (ip == 0) 2405084Swnj goto next; 2414640Swnj m = dtom(ip); 2424640Swnj } else 2434640Swnj if (fp) 24410735Ssam ip_freef(fp); 2454951Swnj 2464951Swnj /* 2474951Swnj * Switch out to protocol's input routine. 2484951Swnj */ 249*24813Skarels (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, ifp); 2505084Swnj goto next; 2514951Swnj bad: 2524951Swnj m_freem(m); 2535084Swnj goto next; 2544640Swnj } 2554495Swnj 2564640Swnj /* 2574640Swnj * Take incoming datagram fragment and try to 2584951Swnj * reassemble it into whole datagram. If a chain for 2594640Swnj * reassembly of this datagram already exists, then it 2604640Swnj * is given as fp; otherwise have to make a chain. 2614640Swnj */ 2624640Swnj struct ip * 2634640Swnj ip_reass(ip, fp) 2644898Swnj register struct ipasfrag *ip; 2654640Swnj register struct ipq *fp; 2664640Swnj { 2674640Swnj register struct mbuf *m = dtom(ip); 2684898Swnj register struct ipasfrag *q; 2694640Swnj struct mbuf *t; 2704640Swnj int hlen = ip->ip_hl << 2; 2714640Swnj int i, next; 2724543Swnj 2734640Swnj /* 2744640Swnj * Presence of header sizes in mbufs 2754640Swnj * would confuse code below. 2764640Swnj */ 2774640Swnj m->m_off += hlen; 2784640Swnj m->m_len -= hlen; 2794495Swnj 2804640Swnj /* 2814640Swnj * If first fragment to arrive, create a reassembly queue. 2824640Swnj */ 2834640Swnj if (fp == 0) { 2849641Ssam if ((t = m_get(M_WAIT, MT_FTABLE)) == NULL) 2854640Swnj goto dropfrag; 2864640Swnj fp = mtod(t, struct ipq *); 2874640Swnj insque(fp, &ipq); 2884640Swnj fp->ipq_ttl = IPFRAGTTL; 2894640Swnj fp->ipq_p = ip->ip_p; 2904640Swnj fp->ipq_id = ip->ip_id; 2914898Swnj fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 2924898Swnj fp->ipq_src = ((struct ip *)ip)->ip_src; 2934898Swnj fp->ipq_dst = ((struct ip *)ip)->ip_dst; 2945161Swnj q = (struct ipasfrag *)fp; 2955161Swnj goto insert; 2964640Swnj } 2974495Swnj 2984640Swnj /* 2994640Swnj * Find a segment which begins after this one does. 3004640Swnj */ 3014898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 3024640Swnj if (q->ip_off > ip->ip_off) 3034640Swnj break; 3044495Swnj 3054640Swnj /* 3064640Swnj * If there is a preceding segment, it may provide some of 3074640Swnj * our data already. If so, drop the data from the incoming 3084640Swnj * segment. If it provides all of our data, drop us. 3094640Swnj */ 3104898Swnj if (q->ipf_prev != (struct ipasfrag *)fp) { 3114898Swnj i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 3124640Swnj if (i > 0) { 3134640Swnj if (i >= ip->ip_len) 3144640Swnj goto dropfrag; 3154640Swnj m_adj(dtom(ip), i); 3164640Swnj ip->ip_off += i; 3174640Swnj ip->ip_len -= i; 3184640Swnj } 3194640Swnj } 3204543Swnj 3214640Swnj /* 3224640Swnj * While we overlap succeeding segments trim them or, 3234640Swnj * if they are completely covered, dequeue them. 3244640Swnj */ 3254898Swnj while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 3264640Swnj i = (ip->ip_off + ip->ip_len) - q->ip_off; 3274640Swnj if (i < q->ip_len) { 3284640Swnj q->ip_len -= i; 3296256Sroot q->ip_off += i; 3304640Swnj m_adj(dtom(q), i); 3314640Swnj break; 3324495Swnj } 3334898Swnj q = q->ipf_next; 3344898Swnj m_freem(dtom(q->ipf_prev)); 3354898Swnj ip_deq(q->ipf_prev); 3364543Swnj } 3374495Swnj 3385161Swnj insert: 3394640Swnj /* 3404640Swnj * Stick new segment in its place; 3414640Swnj * check for complete reassembly. 3424640Swnj */ 3434898Swnj ip_enq(ip, q->ipf_prev); 3444640Swnj next = 0; 3454898Swnj for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 3464640Swnj if (q->ip_off != next) 3474640Swnj return (0); 3484640Swnj next += q->ip_len; 3494640Swnj } 3504898Swnj if (q->ipf_prev->ipf_mff) 3514640Swnj return (0); 3524495Swnj 3534640Swnj /* 3544640Swnj * Reassembly is complete; concatenate fragments. 3554640Swnj */ 3564640Swnj q = fp->ipq_next; 3574640Swnj m = dtom(q); 3584640Swnj t = m->m_next; 3594640Swnj m->m_next = 0; 3604640Swnj m_cat(m, t); 3616298Swnj q = q->ipf_next; 3626298Swnj while (q != (struct ipasfrag *)fp) { 3636298Swnj t = dtom(q); 3646298Swnj q = q->ipf_next; 3656298Swnj m_cat(m, t); 3666298Swnj } 3674495Swnj 3684640Swnj /* 3694640Swnj * Create header for new ip packet by 3704640Swnj * modifying header of first packet; 3714640Swnj * dequeue and discard fragment reassembly header. 3724640Swnj * Make header visible. 3734640Swnj */ 3744640Swnj ip = fp->ipq_next; 3754640Swnj ip->ip_len = next; 3764898Swnj ((struct ip *)ip)->ip_src = fp->ipq_src; 3774898Swnj ((struct ip *)ip)->ip_dst = fp->ipq_dst; 3784640Swnj remque(fp); 3794907Swnj (void) m_free(dtom(fp)); 3804640Swnj m = dtom(ip); 381*24813Skarels m->m_len += (ip->ip_hl << 2); 382*24813Skarels m->m_off -= (ip->ip_hl << 2); 3834898Swnj return ((struct ip *)ip); 3844495Swnj 3854640Swnj dropfrag: 386*24813Skarels ipstat.ips_fragdropped++; 3874640Swnj m_freem(m); 3884640Swnj return (0); 3894495Swnj } 3904495Swnj 3914640Swnj /* 3924640Swnj * Free a fragment reassembly header and all 3934640Swnj * associated datagrams. 3944640Swnj */ 3954640Swnj ip_freef(fp) 3964640Swnj struct ipq *fp; 3974495Swnj { 39810735Ssam register struct ipasfrag *q, *p; 3994495Swnj 40010735Ssam for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 40110735Ssam p = q->ipf_next; 40210735Ssam ip_deq(q); 4034640Swnj m_freem(dtom(q)); 40410735Ssam } 40510735Ssam remque(fp); 40610735Ssam (void) m_free(dtom(fp)); 4074495Swnj } 4084495Swnj 4094640Swnj /* 4104640Swnj * Put an ip fragment on a reassembly chain. 4114640Swnj * Like insque, but pointers in middle of structure. 4124640Swnj */ 4134640Swnj ip_enq(p, prev) 4144898Swnj register struct ipasfrag *p, *prev; 4154495Swnj { 4164951Swnj 4174898Swnj p->ipf_prev = prev; 4184898Swnj p->ipf_next = prev->ipf_next; 4194898Swnj prev->ipf_next->ipf_prev = p; 4204898Swnj prev->ipf_next = p; 4214495Swnj } 4224495Swnj 4234640Swnj /* 4244640Swnj * To ip_enq as remque is to insque. 4254640Swnj */ 4264640Swnj ip_deq(p) 4274898Swnj register struct ipasfrag *p; 4284640Swnj { 4294951Swnj 4304898Swnj p->ipf_prev->ipf_next = p->ipf_next; 4314898Swnj p->ipf_next->ipf_prev = p->ipf_prev; 4324495Swnj } 4334495Swnj 4344640Swnj /* 4354640Swnj * IP timer processing; 4364640Swnj * if a timer expires on a reassembly 4374640Swnj * queue, discard it. 4384640Swnj */ 4394801Swnj ip_slowtimo() 4404495Swnj { 4414495Swnj register struct ipq *fp; 4424640Swnj int s = splnet(); 4434951Swnj 4445243Sroot fp = ipq.next; 4455243Sroot if (fp == 0) { 4465243Sroot splx(s); 4475243Sroot return; 4485243Sroot } 44910735Ssam while (fp != &ipq) { 45010735Ssam --fp->ipq_ttl; 45110735Ssam fp = fp->next; 452*24813Skarels if (fp->prev->ipq_ttl == 0) { 453*24813Skarels ipstat.ips_fragtimeout++; 45410735Ssam ip_freef(fp->prev); 455*24813Skarels } 45610735Ssam } 4574640Swnj splx(s); 4584495Swnj } 4594495Swnj 4604951Swnj /* 4614951Swnj * Drain off all datagram fragments. 4624951Swnj */ 4634801Swnj ip_drain() 4644801Swnj { 4654801Swnj 466*24813Skarels while (ipq.next != &ipq) { 467*24813Skarels ipstat.ips_fragdropped++; 46810735Ssam ip_freef(ipq.next); 469*24813Skarels } 4704801Swnj } 4714923Swnj 472*24813Skarels struct in_ifaddr *ip_rtaddr(); 473*24813Skarels 4744640Swnj /* 4754640Swnj * Do option processing on a datagram, 4764640Swnj * possibly discarding it if bad options 4774640Swnj * are encountered. 4784640Swnj */ 4794640Swnj ip_dooptions(ip) 4804640Swnj struct ip *ip; 4814495Swnj { 4824640Swnj register u_char *cp; 483*24813Skarels int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB; 484*24813Skarels register struct ip_timestamp *ipt; 485*24813Skarels register struct in_ifaddr *ia; 4864923Swnj struct in_addr *sin; 487*24813Skarels n_time ntime; 4884495Swnj 4894640Swnj cp = (u_char *)(ip + 1); 4904640Swnj cnt = (ip->ip_hl << 2) - sizeof (struct ip); 4914640Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 492*24813Skarels opt = cp[IPOPT_OPTVAL]; 4934640Swnj if (opt == IPOPT_EOL) 4944640Swnj break; 4954640Swnj if (opt == IPOPT_NOP) 4964640Swnj optlen = 1; 49716392Ssam else { 498*24813Skarels optlen = cp[IPOPT_OLEN]; 499*24813Skarels if (optlen <= 0 || optlen > cnt) { 500*24813Skarels code = &cp[IPOPT_OLEN] - (u_char *)ip; 50117551Skarels goto bad; 502*24813Skarels } 50316392Ssam } 5044640Swnj switch (opt) { 5054495Swnj 5064640Swnj default: 5074640Swnj break; 5084495Swnj 5094951Swnj /* 5104951Swnj * Source routing with record. 5114951Swnj * Find interface with current destination address. 5124951Swnj * If none on this machine then drop if strictly routed, 5134951Swnj * or do nothing if loosely routed. 5144951Swnj * Record interface address and bring up next address 5154951Swnj * component. If strictly routed make sure next 5164951Swnj * address on directly accessible net. 5174951Swnj */ 5184640Swnj case IPOPT_LSRR: 5197508Sroot case IPOPT_SSRR: 520*24813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 521*24813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 522*24813Skarels goto bad; 523*24813Skarels } 524*24813Skarels ipaddr.sin_addr = ip->ip_dst; 525*24813Skarels ia = (struct in_ifaddr *) 526*24813Skarels ifa_ifwithaddr((struct sockaddr *)&ipaddr); 527*24813Skarels if (ia == 0) { 528*24813Skarels if (opt == IPOPT_SSRR) { 529*24813Skarels type = ICMP_UNREACH; 530*24813Skarels code = ICMP_UNREACH_SRCFAIL; 5314951Swnj goto bad; 532*24813Skarels } 533*24813Skarels /* 534*24813Skarels * Loose routing, and not at next destination 535*24813Skarels * yet; nothing to do except forward. 536*24813Skarels */ 5374951Swnj break; 5384640Swnj } 539*24813Skarels off--; /* 0 origin */ 540*24813Skarels if (off > optlen - sizeof(struct in_addr)) { 541*24813Skarels /* 542*24813Skarels * End of source route. Should be for us. 543*24813Skarels */ 544*24813Skarels save_rte(cp, ip->ip_src); 5454951Swnj break; 546*24813Skarels } 547*24813Skarels /* 548*24813Skarels * locate outgoing interface 549*24813Skarels */ 550*24813Skarels bcopy(cp + off, (caddr_t)&ipaddr.sin_addr, 551*24813Skarels sizeof(ipaddr.sin_addr)); 552*24813Skarels if ((opt == IPOPT_SSRR && 553*24813Skarels in_iaonnetof(in_netof(ipaddr.sin_addr)) == 0) || 554*24813Skarels (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 555*24813Skarels type = ICMP_UNREACH; 556*24813Skarels code = ICMP_UNREACH_SRCFAIL; 5574951Swnj goto bad; 558*24813Skarels } 559*24813Skarels ip->ip_dst = ipaddr.sin_addr; 560*24813Skarels bcopy(&(IA_SIN(ia)->sin_addr), cp + off, 561*24813Skarels sizeof(struct in_addr)); 562*24813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 5634640Swnj break; 5644495Swnj 565*24813Skarels case IPOPT_RR: 566*24813Skarels if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 567*24813Skarels code = &cp[IPOPT_OFFSET] - (u_char *)ip; 568*24813Skarels goto bad; 569*24813Skarels } 570*24813Skarels /* 571*24813Skarels * If no space remains, ignore. 572*24813Skarels */ 573*24813Skarels off--; /* 0 origin */ 574*24813Skarels if (off > optlen - sizeof(struct in_addr)) 575*24813Skarels break; 576*24813Skarels bcopy(cp + off, (caddr_t)ipaddr.sin_addr, 577*24813Skarels sizeof(ipaddr.sin_addr)); 578*24813Skarels /* 579*24813Skarels * locate outgoing interface 580*24813Skarels */ 581*24813Skarels if ((ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 582*24813Skarels type = ICMP_UNREACH; 583*24813Skarels code = ICMP_UNREACH_SRCFAIL; 584*24813Skarels goto bad; 585*24813Skarels } 586*24813Skarels bcopy(&(IA_SIN(ia)->sin_addr), cp + off, 587*24813Skarels sizeof(struct in_addr)); 588*24813Skarels cp[IPOPT_OFFSET] += sizeof(struct in_addr); 589*24813Skarels break; 590*24813Skarels 5914640Swnj case IPOPT_TS: 5926583Ssam code = cp - (u_char *)ip; 5934801Swnj ipt = (struct ip_timestamp *)cp; 5944801Swnj if (ipt->ipt_len < 5) 5954640Swnj goto bad; 5964801Swnj if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 5974801Swnj if (++ipt->ipt_oflw == 0) 5984640Swnj goto bad; 5994495Swnj break; 6004640Swnj } 601*24813Skarels sin = (struct in_addr *)(cp+cp[IPOPT_OFFSET]-1); 6024801Swnj switch (ipt->ipt_flg) { 6034495Swnj 6044640Swnj case IPOPT_TS_TSONLY: 6054640Swnj break; 6064640Swnj 6074640Swnj case IPOPT_TS_TSANDADDR: 608*24813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 609*24813Skarels sizeof(struct in_addr) > ipt->ipt_len) 6104640Swnj goto bad; 61118376Skarels if (in_ifaddr == 0) 6126338Ssam goto bad; /* ??? */ 613*24813Skarels bcopy((caddr_t)&IA_SIN(in_ifaddr)->sin_addr, 614*24813Skarels (caddr_t)sin, sizeof(struct in_addr)); 615*24813Skarels sin++; 6164640Swnj break; 6174640Swnj 6184640Swnj case IPOPT_TS_PRESPEC: 619*24813Skarels bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 620*24813Skarels sizeof(struct in_addr)); 62118376Skarels if (ifa_ifwithaddr((struct sockaddr *)&ipaddr) == 0) 6224951Swnj continue; 623*24813Skarels if (ipt->ipt_ptr + sizeof(n_time) + 624*24813Skarels sizeof(struct in_addr) > ipt->ipt_len) 6254640Swnj goto bad; 626*24813Skarels ipt->ipt_ptr += sizeof(struct in_addr); 6274640Swnj break; 6284640Swnj 6294495Swnj default: 6304640Swnj goto bad; 6314495Swnj } 632*24813Skarels ntime = iptime(); 633*24813Skarels bcopy((caddr_t)&ntime, (caddr_t)sin, sizeof(n_time)); 634*24813Skarels ipt->ipt_ptr += sizeof(n_time); 6354640Swnj } 6364495Swnj } 6376583Ssam return (0); 6384640Swnj bad: 6396583Ssam icmp_error(ip, type, code); 6406583Ssam return (1); 6414495Swnj } 6424495Swnj 6434640Swnj /* 644*24813Skarels * Given address of next destination (final or next hop), 645*24813Skarels * return internet address info of interface to be used to get there. 646*24813Skarels */ 647*24813Skarels struct in_ifaddr * 648*24813Skarels ip_rtaddr(dst) 649*24813Skarels struct in_addr dst; 650*24813Skarels { 651*24813Skarels register struct sockaddr_in *sin; 652*24813Skarels register struct in_ifaddr *ia; 653*24813Skarels 654*24813Skarels sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 655*24813Skarels 656*24813Skarels if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 657*24813Skarels if (ipforward_rt.ro_rt) { 658*24813Skarels RTFREE(ipforward_rt.ro_rt); 659*24813Skarels ipforward_rt.ro_rt = 0; 660*24813Skarels } 661*24813Skarels sin->sin_family = AF_INET; 662*24813Skarels sin->sin_addr = dst; 663*24813Skarels 664*24813Skarels rtalloc(&ipforward_rt); 665*24813Skarels } 666*24813Skarels if (ipforward_rt.ro_rt == 0) 667*24813Skarels return ((struct in_ifaddr *)0); 668*24813Skarels /* 669*24813Skarels * Find address associated with outgoing interface. 670*24813Skarels */ 671*24813Skarels for (ia = in_ifaddr; ia; ia = ia->ia_next) 672*24813Skarels if (ia->ia_ifp == ipforward_rt.ro_rt->rt_ifp) 673*24813Skarels break; 674*24813Skarels return (ia); 675*24813Skarels } 676*24813Skarels 677*24813Skarels /* 678*24813Skarels * Save incoming source route for use in replies, 679*24813Skarels * to be picked up later by ip_srcroute if the receiver is interested. 680*24813Skarels */ 681*24813Skarels save_rte(option, dst) 682*24813Skarels caddr_t option; 683*24813Skarels struct in_addr dst; 684*24813Skarels { 685*24813Skarels int olen; 686*24813Skarels extern ipprintfs; 687*24813Skarels 688*24813Skarels olen = option[IPOPT_OLEN]; 689*24813Skarels if (olen > sizeof(ip_srcrt) - 1) { 690*24813Skarels if (ipprintfs) 691*24813Skarels printf("save_rte: olen %d\n", olen); 692*24813Skarels return; 693*24813Skarels } 694*24813Skarels bcopy(option, (caddr_t)ip_srcrt.srcopt, olen); 695*24813Skarels ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 696*24813Skarels ip_srcrt.route[ip_nhops++] = dst; 697*24813Skarels } 698*24813Skarels 699*24813Skarels /* 700*24813Skarels * Retrieve incoming source route for use in replies, 701*24813Skarels * in the same form used by setsockopt. 702*24813Skarels * The first hop is placed before the options, will be removed later. 703*24813Skarels */ 704*24813Skarels struct mbuf * 705*24813Skarels ip_srcroute() 706*24813Skarels { 707*24813Skarels register struct in_addr *p, *q; 708*24813Skarels register struct mbuf *m; 709*24813Skarels 710*24813Skarels if (ip_nhops == 0) 711*24813Skarels return ((struct mbuf *)0); 712*24813Skarels m = m_get(M_WAIT, MT_SOOPTS); 713*24813Skarels m->m_len = ip_nhops * sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1; 714*24813Skarels 715*24813Skarels /* 716*24813Skarels * First save first hop for return route 717*24813Skarels */ 718*24813Skarels p = &ip_srcrt.route[ip_nhops - 1]; 719*24813Skarels *(mtod(m, struct in_addr *)) = *p--; 720*24813Skarels 721*24813Skarels /* 722*24813Skarels * Copy option fields and padding (nop) to mbuf. 723*24813Skarels */ 724*24813Skarels ip_srcrt.nop = IPOPT_NOP; 725*24813Skarels bcopy((caddr_t)&ip_srcrt, mtod(m, caddr_t) + sizeof(struct in_addr), 726*24813Skarels IPOPT_OFFSET + 1 + 1); 727*24813Skarels q = (struct in_addr *)(mtod(m, caddr_t) + 728*24813Skarels sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1); 729*24813Skarels /* 730*24813Skarels * Record return path as an IP source route, 731*24813Skarels * reversing the path (pointers are now aligned). 732*24813Skarels */ 733*24813Skarels while (p >= ip_srcrt.route) 734*24813Skarels *q++ = *p--; 735*24813Skarels return (m); 736*24813Skarels } 737*24813Skarels 738*24813Skarels /* 7394951Swnj * Strip out IP options, at higher 7404951Swnj * level protocol in the kernel. 7414951Swnj * Second argument is buffer to which options 7424951Swnj * will be moved, and return value is their length. 7434640Swnj */ 7445217Swnj ip_stripoptions(ip, mopt) 7454640Swnj struct ip *ip; 7465217Swnj struct mbuf *mopt; 7474495Swnj { 7484640Swnj register int i; 7494640Swnj register struct mbuf *m; 750*24813Skarels register caddr_t opts; 7514640Swnj int olen; 7524640Swnj 7534640Swnj olen = (ip->ip_hl<<2) - sizeof (struct ip); 7544951Swnj m = dtom(ip); 755*24813Skarels opts = (caddr_t)(ip + 1); 7565217Swnj if (mopt) { 7575217Swnj mopt->m_len = olen; 7585217Swnj mopt->m_off = MMINOFF; 759*24813Skarels bcopy(opts, mtod(mopt, caddr_t), (unsigned)olen); 7605217Swnj } 7614640Swnj i = m->m_len - (sizeof (struct ip) + olen); 762*24813Skarels bcopy(opts + olen, opts, (unsigned)i); 7635243Sroot m->m_len -= olen; 764*24813Skarels ip->ip_hl = sizeof(struct ip) >> 2; 7654495Swnj } 7666583Ssam 76714670Ssam u_char inetctlerrmap[PRC_NCMDS] = { 768*24813Skarels 0, 0, 0, 0, 76914670Ssam 0, 0, EHOSTDOWN, EHOSTUNREACH, 77014670Ssam ENETUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 771*24813Skarels EMSGSIZE, EHOSTUNREACH, 0, 0, 772*24813Skarels 0, 0, 0, 0, 773*24813Skarels ENOPROTOOPT 7746583Ssam }; 7756583Ssam 776*24813Skarels #ifndef IPFORWARDING 777*24813Skarels #define IPFORWARDING 1 778*24813Skarels #endif 779*24813Skarels #ifndef IPSENDREDIRECTS 780*24813Skarels #define IPSENDREDIRECTS 1 781*24813Skarels #endif 7826583Ssam int ipprintfs = 0; 783*24813Skarels int ipforwarding = IPFORWARDING; 784*24813Skarels extern int in_interfaces; 785*24813Skarels int ipsendredirects = IPSENDREDIRECTS; 786*24813Skarels 7876583Ssam /* 7886583Ssam * Forward a packet. If some error occurs return the sender 78918376Skarels * an icmp packet. Note we can't always generate a meaningful 790*24813Skarels * icmp message because icmp doesn't have a large enough repertoire 7916583Ssam * of codes and types. 7926583Ssam */ 793*24813Skarels ip_forward(ip, ifp) 7946583Ssam register struct ip *ip; 795*24813Skarels struct ifnet *ifp; 7966583Ssam { 797*24813Skarels register int error, type = 0, code; 798*24813Skarels register struct sockaddr_in *sin; 79918376Skarels struct mbuf *mcopy; 800*24813Skarels struct in_addr dest; 8016583Ssam 802*24813Skarels #ifdef lint 803*24813Skarels dest.s_addr = 0; 804*24813Skarels #endif 8056583Ssam if (ipprintfs) 8066583Ssam printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 8076583Ssam ip->ip_dst, ip->ip_ttl); 80818376Skarels ip->ip_id = htons(ip->ip_id); 809*24813Skarels if (ipforwarding == 0 || in_interfaces <= 1) { 8106583Ssam /* can't tell difference between net and host */ 8116583Ssam type = ICMP_UNREACH, code = ICMP_UNREACH_NET; 8126583Ssam goto sendicmp; 8136583Ssam } 8146583Ssam if (ip->ip_ttl < IPTTLDEC) { 8156583Ssam type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS; 8166583Ssam goto sendicmp; 8176583Ssam } 8186583Ssam ip->ip_ttl -= IPTTLDEC; 8196609Ssam 8206609Ssam /* 8216609Ssam * Save at most 64 bytes of the packet in case 8226609Ssam * we need to generate an ICMP message to the src. 8236609Ssam */ 8247843Sroot mcopy = m_copy(dtom(ip), 0, imin(ip->ip_len, 64)); 8256583Ssam 826*24813Skarels sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 827*24813Skarels if (ipforward_rt.ro_rt == 0 || 828*24813Skarels ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 829*24813Skarels if (ipforward_rt.ro_rt) { 830*24813Skarels RTFREE(ipforward_rt.ro_rt); 831*24813Skarels ipforward_rt.ro_rt = 0; 832*24813Skarels } 833*24813Skarels sin->sin_family = AF_INET; 834*24813Skarels sin->sin_addr = ip->ip_dst; 835*24813Skarels 836*24813Skarels rtalloc(&ipforward_rt); 837*24813Skarels } 838*24813Skarels /* 839*24813Skarels * If forwarding packet using same interface that it came in on, 840*24813Skarels * perhaps should send a redirect to sender to shortcut a hop. 841*24813Skarels * Only send redirect if source is sending directly to us, 842*24813Skarels * and if packet was not source routed (or has any options). 843*24813Skarels */ 844*24813Skarels if (ipforward_rt.ro_rt && ipforward_rt.ro_rt->rt_ifp == ifp && 845*24813Skarels ipsendredirects && ip->ip_hl == (sizeof(struct ip) >> 2)) { 846*24813Skarels struct in_ifaddr *ia; 847*24813Skarels extern struct in_ifaddr *ifptoia(); 848*24813Skarels u_long src = ntohl(ip->ip_src.s_addr); 849*24813Skarels u_long dst = ntohl(ip->ip_dst.s_addr); 850*24813Skarels 851*24813Skarels if ((ia = ifptoia(ifp)) && 852*24813Skarels (src & ia->ia_subnetmask) == ia->ia_subnet) { 853*24813Skarels if (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) 854*24813Skarels dest = satosin(&ipforward_rt.ro_rt->rt_gateway)->sin_addr; 855*24813Skarels else 856*24813Skarels dest = ip->ip_dst; 857*24813Skarels /* 858*24813Skarels * If the destination is reached by a route to host, 859*24813Skarels * is directly on the attached net (!), 860*24813Skarels * or if the destination is on a subnet of a local net 861*24813Skarels * not known to the source net, use host redirect. 862*24813Skarels * (We may be the correct first hop for other subnets.) 863*24813Skarels */ 864*24813Skarels type = ICMP_REDIRECT; 865*24813Skarels code = ICMP_REDIRECT_NET; 866*24813Skarels if ((ipforward_rt.ro_rt->rt_flags & RTF_HOST) || 867*24813Skarels (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) == 0) 868*24813Skarels code = ICMP_REDIRECT_HOST; 869*24813Skarels else for (ia = in_ifaddr; ia = ia->ia_next; ) 870*24813Skarels if ((dst & ia->ia_netmask) == ia->ia_net) { 871*24813Skarels if ((src & ia->ia_netmask) != ia->ia_net) 872*24813Skarels code = ICMP_REDIRECT_HOST; 873*24813Skarels break; 874*24813Skarels } 875*24813Skarels if (ipprintfs) 876*24813Skarels printf("redirect (%d) to %x\n", code, dest); 877*24813Skarels } 878*24813Skarels } 879*24813Skarels 880*24813Skarels error = ip_output(dtom(ip), (struct mbuf *)0, &ipforward_rt, 88118376Skarels IP_FORWARDING); 882*24813Skarels if (error) 883*24813Skarels ipstat.ips_cantforward++; 884*24813Skarels else if (type) 885*24813Skarels ipstat.ips_redirectsent++; 886*24813Skarels else { 8876609Ssam if (mcopy) 8886609Ssam m_freem(mcopy); 88921117Skarels ipstat.ips_forward++; 8906583Ssam return; 8916609Ssam } 89211540Ssam if (mcopy == NULL) 89311540Ssam return; 8946609Ssam ip = mtod(mcopy, struct ip *); 895*24813Skarels type = ICMP_UNREACH; 8966609Ssam switch (error) { 8976609Ssam 898*24813Skarels case 0: /* forwarded, but need redirect */ 899*24813Skarels type = ICMP_REDIRECT; 900*24813Skarels /* code set above */ 901*24813Skarels break; 902*24813Skarels 9036609Ssam case ENETUNREACH: 9046609Ssam case ENETDOWN: 9056583Ssam code = ICMP_UNREACH_NET; 9066609Ssam break; 9076609Ssam 9086609Ssam case EMSGSIZE: 9096583Ssam code = ICMP_UNREACH_NEEDFRAG; 9106609Ssam break; 9116609Ssam 9126609Ssam case EPERM: 9136609Ssam code = ICMP_UNREACH_PORT; 9146609Ssam break; 9156609Ssam 9166609Ssam case ENOBUFS: 9176609Ssam type = ICMP_SOURCEQUENCH; 9186609Ssam break; 9196609Ssam 9206609Ssam case EHOSTDOWN: 9216609Ssam case EHOSTUNREACH: 9226609Ssam code = ICMP_UNREACH_HOST; 9236609Ssam break; 9246609Ssam } 9256583Ssam sendicmp: 926*24813Skarels icmp_error(ip, type, code, dest); 9276583Ssam } 928