xref: /csrg-svn/sys/netinet/ip_input.c (revision 25195)
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*25195Skarels  *	@(#)ip_input.c	6.13 (Berkeley) 10/14/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 /*
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;
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;
11124813Skarels 	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 	 */
14524813Skarels 	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 	 */
17224813Skarels 	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)
18324813Skarels 			goto ours;
184*25195Skarels 		if (
185*25195Skarels #ifdef	DIRECTED_BROADCAST
186*25195Skarels 		    ia->ia_ifp == ifp &&
187*25195Skarels #endif
188*25195Skarels 		    (ia->ia_ifp->if_flags & IFF_BROADCAST)) {
189*25195Skarels 			u_long i;
190*25195Skarels 
191*25195Skarels 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
192*25195Skarels 			    ip->ip_dst.s_addr)
193*25195Skarels 				goto ours;
194*25195Skarels 			if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
195*25195Skarels 				goto ours;
196*25195Skarels 			/*
197*25195Skarels 			 * Look for all-0's host part (old broadcast addr),
198*25195Skarels 			 * either for subnet or net.
199*25195Skarels 			 */
200*25195Skarels 			i = ntohl(ip->ip_dst.s_addr);
201*25195Skarels 			if (i == ia->ia_subnet)
202*25195Skarels 				goto ours;
203*25195Skarels 			if (i == ia->ia_net)
204*25195Skarels 				goto ours;
205*25195Skarels 		}
2066338Ssam 	}
20724813Skarels 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
20824813Skarels 		goto ours;
20924813Skarels 	if (ip->ip_dst.s_addr == INADDR_ANY)
21024813Skarels 		goto ours;
2114495Swnj 
2124640Swnj 	/*
21324813Skarels 	 * Not for us; forward if possible and desirable.
21424813Skarels 	 */
21524813Skarels 	ip_forward(ip, ifp);
21624813Skarels 	goto next;
21724813Skarels 
21824813Skarels ours:
21924813Skarels 	/*
2204640Swnj 	 * Look for queue of fragments
2214640Swnj 	 * of this datagram.
2224640Swnj 	 */
2234640Swnj 	for (fp = ipq.next; fp != &ipq; fp = fp->next)
2244640Swnj 		if (ip->ip_id == fp->ipq_id &&
2254640Swnj 		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
2264640Swnj 		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
2274640Swnj 		    ip->ip_p == fp->ipq_p)
2284640Swnj 			goto found;
2294640Swnj 	fp = 0;
2304640Swnj found:
2314495Swnj 
2324640Swnj 	/*
2334640Swnj 	 * Adjust ip_len to not reflect header,
2344640Swnj 	 * set ip_mff if more fragments are expected,
2354640Swnj 	 * convert offset of this to bytes.
2364640Swnj 	 */
2374640Swnj 	ip->ip_len -= hlen;
2384898Swnj 	((struct ipasfrag *)ip)->ipf_mff = 0;
2394640Swnj 	if (ip->ip_off & IP_MF)
2404898Swnj 		((struct ipasfrag *)ip)->ipf_mff = 1;
2414640Swnj 	ip->ip_off <<= 3;
2424495Swnj 
2434640Swnj 	/*
2444640Swnj 	 * If datagram marked as having more fragments
2454640Swnj 	 * or if this is not the first fragment,
2464640Swnj 	 * attempt reassembly; if it succeeds, proceed.
2474640Swnj 	 */
2484898Swnj 	if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
24924813Skarels 		ipstat.ips_fragments++;
2504898Swnj 		ip = ip_reass((struct ipasfrag *)ip, fp);
2514640Swnj 		if (ip == 0)
2525084Swnj 			goto next;
2534640Swnj 		m = dtom(ip);
2544640Swnj 	} else
2554640Swnj 		if (fp)
25610735Ssam 			ip_freef(fp);
2574951Swnj 
2584951Swnj 	/*
2594951Swnj 	 * Switch out to protocol's input routine.
2604951Swnj 	 */
26124813Skarels 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, ifp);
2625084Swnj 	goto next;
2634951Swnj bad:
2644951Swnj 	m_freem(m);
2655084Swnj 	goto next;
2664640Swnj }
2674495Swnj 
2684640Swnj /*
2694640Swnj  * Take incoming datagram fragment and try to
2704951Swnj  * reassemble it into whole datagram.  If a chain for
2714640Swnj  * reassembly of this datagram already exists, then it
2724640Swnj  * is given as fp; otherwise have to make a chain.
2734640Swnj  */
2744640Swnj struct ip *
2754640Swnj ip_reass(ip, fp)
2764898Swnj 	register struct ipasfrag *ip;
2774640Swnj 	register struct ipq *fp;
2784640Swnj {
2794640Swnj 	register struct mbuf *m = dtom(ip);
2804898Swnj 	register struct ipasfrag *q;
2814640Swnj 	struct mbuf *t;
2824640Swnj 	int hlen = ip->ip_hl << 2;
2834640Swnj 	int i, next;
2844543Swnj 
2854640Swnj 	/*
2864640Swnj 	 * Presence of header sizes in mbufs
2874640Swnj 	 * would confuse code below.
2884640Swnj 	 */
2894640Swnj 	m->m_off += hlen;
2904640Swnj 	m->m_len -= hlen;
2914495Swnj 
2924640Swnj 	/*
2934640Swnj 	 * If first fragment to arrive, create a reassembly queue.
2944640Swnj 	 */
2954640Swnj 	if (fp == 0) {
2969641Ssam 		if ((t = m_get(M_WAIT, MT_FTABLE)) == NULL)
2974640Swnj 			goto dropfrag;
2984640Swnj 		fp = mtod(t, struct ipq *);
2994640Swnj 		insque(fp, &ipq);
3004640Swnj 		fp->ipq_ttl = IPFRAGTTL;
3014640Swnj 		fp->ipq_p = ip->ip_p;
3024640Swnj 		fp->ipq_id = ip->ip_id;
3034898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
3044898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
3054898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
3065161Swnj 		q = (struct ipasfrag *)fp;
3075161Swnj 		goto insert;
3084640Swnj 	}
3094495Swnj 
3104640Swnj 	/*
3114640Swnj 	 * Find a segment which begins after this one does.
3124640Swnj 	 */
3134898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
3144640Swnj 		if (q->ip_off > ip->ip_off)
3154640Swnj 			break;
3164495Swnj 
3174640Swnj 	/*
3184640Swnj 	 * If there is a preceding segment, it may provide some of
3194640Swnj 	 * our data already.  If so, drop the data from the incoming
3204640Swnj 	 * segment.  If it provides all of our data, drop us.
3214640Swnj 	 */
3224898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
3234898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
3244640Swnj 		if (i > 0) {
3254640Swnj 			if (i >= ip->ip_len)
3264640Swnj 				goto dropfrag;
3274640Swnj 			m_adj(dtom(ip), i);
3284640Swnj 			ip->ip_off += i;
3294640Swnj 			ip->ip_len -= i;
3304640Swnj 		}
3314640Swnj 	}
3324543Swnj 
3334640Swnj 	/*
3344640Swnj 	 * While we overlap succeeding segments trim them or,
3354640Swnj 	 * if they are completely covered, dequeue them.
3364640Swnj 	 */
3374898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
3384640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
3394640Swnj 		if (i < q->ip_len) {
3404640Swnj 			q->ip_len -= i;
3416256Sroot 			q->ip_off += i;
3424640Swnj 			m_adj(dtom(q), i);
3434640Swnj 			break;
3444495Swnj 		}
3454898Swnj 		q = q->ipf_next;
3464898Swnj 		m_freem(dtom(q->ipf_prev));
3474898Swnj 		ip_deq(q->ipf_prev);
3484543Swnj 	}
3494495Swnj 
3505161Swnj insert:
3514640Swnj 	/*
3524640Swnj 	 * Stick new segment in its place;
3534640Swnj 	 * check for complete reassembly.
3544640Swnj 	 */
3554898Swnj 	ip_enq(ip, q->ipf_prev);
3564640Swnj 	next = 0;
3574898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
3584640Swnj 		if (q->ip_off != next)
3594640Swnj 			return (0);
3604640Swnj 		next += q->ip_len;
3614640Swnj 	}
3624898Swnj 	if (q->ipf_prev->ipf_mff)
3634640Swnj 		return (0);
3644495Swnj 
3654640Swnj 	/*
3664640Swnj 	 * Reassembly is complete; concatenate fragments.
3674640Swnj 	 */
3684640Swnj 	q = fp->ipq_next;
3694640Swnj 	m = dtom(q);
3704640Swnj 	t = m->m_next;
3714640Swnj 	m->m_next = 0;
3724640Swnj 	m_cat(m, t);
3736298Swnj 	q = q->ipf_next;
3746298Swnj 	while (q != (struct ipasfrag *)fp) {
3756298Swnj 		t = dtom(q);
3766298Swnj 		q = q->ipf_next;
3776298Swnj 		m_cat(m, t);
3786298Swnj 	}
3794495Swnj 
3804640Swnj 	/*
3814640Swnj 	 * Create header for new ip packet by
3824640Swnj 	 * modifying header of first packet;
3834640Swnj 	 * dequeue and discard fragment reassembly header.
3844640Swnj 	 * Make header visible.
3854640Swnj 	 */
3864640Swnj 	ip = fp->ipq_next;
3874640Swnj 	ip->ip_len = next;
3884898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
3894898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
3904640Swnj 	remque(fp);
3914907Swnj 	(void) m_free(dtom(fp));
3924640Swnj 	m = dtom(ip);
39324813Skarels 	m->m_len += (ip->ip_hl << 2);
39424813Skarels 	m->m_off -= (ip->ip_hl << 2);
3954898Swnj 	return ((struct ip *)ip);
3964495Swnj 
3974640Swnj dropfrag:
39824813Skarels 	ipstat.ips_fragdropped++;
3994640Swnj 	m_freem(m);
4004640Swnj 	return (0);
4014495Swnj }
4024495Swnj 
4034640Swnj /*
4044640Swnj  * Free a fragment reassembly header and all
4054640Swnj  * associated datagrams.
4064640Swnj  */
4074640Swnj ip_freef(fp)
4084640Swnj 	struct ipq *fp;
4094495Swnj {
41010735Ssam 	register struct ipasfrag *q, *p;
4114495Swnj 
41210735Ssam 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {
41310735Ssam 		p = q->ipf_next;
41410735Ssam 		ip_deq(q);
4154640Swnj 		m_freem(dtom(q));
41610735Ssam 	}
41710735Ssam 	remque(fp);
41810735Ssam 	(void) m_free(dtom(fp));
4194495Swnj }
4204495Swnj 
4214640Swnj /*
4224640Swnj  * Put an ip fragment on a reassembly chain.
4234640Swnj  * Like insque, but pointers in middle of structure.
4244640Swnj  */
4254640Swnj ip_enq(p, prev)
4264898Swnj 	register struct ipasfrag *p, *prev;
4274495Swnj {
4284951Swnj 
4294898Swnj 	p->ipf_prev = prev;
4304898Swnj 	p->ipf_next = prev->ipf_next;
4314898Swnj 	prev->ipf_next->ipf_prev = p;
4324898Swnj 	prev->ipf_next = p;
4334495Swnj }
4344495Swnj 
4354640Swnj /*
4364640Swnj  * To ip_enq as remque is to insque.
4374640Swnj  */
4384640Swnj ip_deq(p)
4394898Swnj 	register struct ipasfrag *p;
4404640Swnj {
4414951Swnj 
4424898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
4434898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
4444495Swnj }
4454495Swnj 
4464640Swnj /*
4474640Swnj  * IP timer processing;
4484640Swnj  * if a timer expires on a reassembly
4494640Swnj  * queue, discard it.
4504640Swnj  */
4514801Swnj ip_slowtimo()
4524495Swnj {
4534495Swnj 	register struct ipq *fp;
4544640Swnj 	int s = splnet();
4554951Swnj 
4565243Sroot 	fp = ipq.next;
4575243Sroot 	if (fp == 0) {
4585243Sroot 		splx(s);
4595243Sroot 		return;
4605243Sroot 	}
46110735Ssam 	while (fp != &ipq) {
46210735Ssam 		--fp->ipq_ttl;
46310735Ssam 		fp = fp->next;
46424813Skarels 		if (fp->prev->ipq_ttl == 0) {
46524813Skarels 			ipstat.ips_fragtimeout++;
46610735Ssam 			ip_freef(fp->prev);
46724813Skarels 		}
46810735Ssam 	}
4694640Swnj 	splx(s);
4704495Swnj }
4714495Swnj 
4724951Swnj /*
4734951Swnj  * Drain off all datagram fragments.
4744951Swnj  */
4754801Swnj ip_drain()
4764801Swnj {
4774801Swnj 
47824813Skarels 	while (ipq.next != &ipq) {
47924813Skarels 		ipstat.ips_fragdropped++;
48010735Ssam 		ip_freef(ipq.next);
48124813Skarels 	}
4824801Swnj }
4834923Swnj 
48424813Skarels struct in_ifaddr *ip_rtaddr();
48524813Skarels 
4864640Swnj /*
4874640Swnj  * Do option processing on a datagram,
4884640Swnj  * possibly discarding it if bad options
4894640Swnj  * are encountered.
4904640Swnj  */
4914640Swnj ip_dooptions(ip)
4924640Swnj 	struct ip *ip;
4934495Swnj {
4944640Swnj 	register u_char *cp;
49524813Skarels 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB;
49624813Skarels 	register struct ip_timestamp *ipt;
49724813Skarels 	register struct in_ifaddr *ia;
4984923Swnj 	struct in_addr *sin;
49924813Skarels 	n_time ntime;
5004495Swnj 
5014640Swnj 	cp = (u_char *)(ip + 1);
5024640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
5034640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
50424813Skarels 		opt = cp[IPOPT_OPTVAL];
5054640Swnj 		if (opt == IPOPT_EOL)
5064640Swnj 			break;
5074640Swnj 		if (opt == IPOPT_NOP)
5084640Swnj 			optlen = 1;
50916392Ssam 		else {
51024813Skarels 			optlen = cp[IPOPT_OLEN];
51124813Skarels 			if (optlen <= 0 || optlen > cnt) {
51224813Skarels 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
51317551Skarels 				goto bad;
51424813Skarels 			}
51516392Ssam 		}
5164640Swnj 		switch (opt) {
5174495Swnj 
5184640Swnj 		default:
5194640Swnj 			break;
5204495Swnj 
5214951Swnj 		/*
5224951Swnj 		 * Source routing with record.
5234951Swnj 		 * Find interface with current destination address.
5244951Swnj 		 * If none on this machine then drop if strictly routed,
5254951Swnj 		 * or do nothing if loosely routed.
5264951Swnj 		 * Record interface address and bring up next address
5274951Swnj 		 * component.  If strictly routed make sure next
5284951Swnj 		 * address on directly accessible net.
5294951Swnj 		 */
5304640Swnj 		case IPOPT_LSRR:
5317508Sroot 		case IPOPT_SSRR:
53224813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
53324813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
53424813Skarels 				goto bad;
53524813Skarels 			}
53624813Skarels 			ipaddr.sin_addr = ip->ip_dst;
53724813Skarels 			ia = (struct in_ifaddr *)
53824813Skarels 				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
53924813Skarels 			if (ia == 0) {
54024813Skarels 				if (opt == IPOPT_SSRR) {
54124813Skarels 					type = ICMP_UNREACH;
54224813Skarels 					code = ICMP_UNREACH_SRCFAIL;
5434951Swnj 					goto bad;
54424813Skarels 				}
54524813Skarels 				/*
54624813Skarels 				 * Loose routing, and not at next destination
54724813Skarels 				 * yet; nothing to do except forward.
54824813Skarels 				 */
5494951Swnj 				break;
5504640Swnj 			}
55124813Skarels 			off--;			/* 0 origin */
55224813Skarels 			if (off > optlen - sizeof(struct in_addr)) {
55324813Skarels 				/*
55424813Skarels 				 * End of source route.  Should be for us.
55524813Skarels 				 */
55624813Skarels 				save_rte(cp, ip->ip_src);
5574951Swnj 				break;
55824813Skarels 			}
55924813Skarels 			/*
56024813Skarels 			 * locate outgoing interface
56124813Skarels 			 */
56224813Skarels 			bcopy(cp + off, (caddr_t)&ipaddr.sin_addr,
56324813Skarels 			    sizeof(ipaddr.sin_addr));
56424813Skarels 			if ((opt == IPOPT_SSRR &&
56524813Skarels 			    in_iaonnetof(in_netof(ipaddr.sin_addr)) == 0) ||
56624813Skarels 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
56724813Skarels 				type = ICMP_UNREACH;
56824813Skarels 				code = ICMP_UNREACH_SRCFAIL;
5694951Swnj 				goto bad;
57024813Skarels 			}
57124813Skarels 			ip->ip_dst = ipaddr.sin_addr;
57224813Skarels 			bcopy(&(IA_SIN(ia)->sin_addr), cp + off,
57324813Skarels 				sizeof(struct in_addr));
57424813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
5754640Swnj 			break;
5764495Swnj 
57724813Skarels 		case IPOPT_RR:
57824813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
57924813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
58024813Skarels 				goto bad;
58124813Skarels 			}
58224813Skarels 			/*
58324813Skarels 			 * If no space remains, ignore.
58424813Skarels 			 */
58524813Skarels 			off--;			/* 0 origin */
58624813Skarels 			if (off > optlen - sizeof(struct in_addr))
58724813Skarels 				break;
58824813Skarels 			bcopy(cp + off, (caddr_t)ipaddr.sin_addr,
58924813Skarels 			    sizeof(ipaddr.sin_addr));
59024813Skarels 			/*
59124813Skarels 			 * locate outgoing interface
59224813Skarels 			 */
59324813Skarels 			if ((ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
59424813Skarels 				type = ICMP_UNREACH;
59524813Skarels 				code = ICMP_UNREACH_SRCFAIL;
59624813Skarels 				goto bad;
59724813Skarels 			}
59824813Skarels 			bcopy(&(IA_SIN(ia)->sin_addr), cp + off,
59924813Skarels 				sizeof(struct in_addr));
60024813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
60124813Skarels 			break;
60224813Skarels 
6034640Swnj 		case IPOPT_TS:
6046583Ssam 			code = cp - (u_char *)ip;
6054801Swnj 			ipt = (struct ip_timestamp *)cp;
6064801Swnj 			if (ipt->ipt_len < 5)
6074640Swnj 				goto bad;
6084801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
6094801Swnj 				if (++ipt->ipt_oflw == 0)
6104640Swnj 					goto bad;
6114495Swnj 				break;
6124640Swnj 			}
61324813Skarels 			sin = (struct in_addr *)(cp+cp[IPOPT_OFFSET]-1);
6144801Swnj 			switch (ipt->ipt_flg) {
6154495Swnj 
6164640Swnj 			case IPOPT_TS_TSONLY:
6174640Swnj 				break;
6184640Swnj 
6194640Swnj 			case IPOPT_TS_TSANDADDR:
62024813Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
62124813Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
6224640Swnj 					goto bad;
62318376Skarels 				if (in_ifaddr == 0)
6246338Ssam 					goto bad;	/* ??? */
62524813Skarels 				bcopy((caddr_t)&IA_SIN(in_ifaddr)->sin_addr,
62624813Skarels 				    (caddr_t)sin, sizeof(struct in_addr));
62724813Skarels 				sin++;
6284640Swnj 				break;
6294640Swnj 
6304640Swnj 			case IPOPT_TS_PRESPEC:
63124813Skarels 				bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
63224813Skarels 				    sizeof(struct in_addr));
63318376Skarels 				if (ifa_ifwithaddr((struct sockaddr *)&ipaddr) == 0)
6344951Swnj 					continue;
63524813Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
63624813Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
6374640Swnj 					goto bad;
63824813Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
6394640Swnj 				break;
6404640Swnj 
6414495Swnj 			default:
6424640Swnj 				goto bad;
6434495Swnj 			}
64424813Skarels 			ntime = iptime();
64524813Skarels 			bcopy((caddr_t)&ntime, (caddr_t)sin, sizeof(n_time));
64624813Skarels 			ipt->ipt_ptr += sizeof(n_time);
6474640Swnj 		}
6484495Swnj 	}
6496583Ssam 	return (0);
6504640Swnj bad:
6516583Ssam 	icmp_error(ip, type, code);
6526583Ssam 	return (1);
6534495Swnj }
6544495Swnj 
6554640Swnj /*
65624813Skarels  * Given address of next destination (final or next hop),
65724813Skarels  * return internet address info of interface to be used to get there.
65824813Skarels  */
65924813Skarels struct in_ifaddr *
66024813Skarels ip_rtaddr(dst)
66124813Skarels 	 struct in_addr dst;
66224813Skarels {
66324813Skarels 	register struct sockaddr_in *sin;
66424813Skarels 	register struct in_ifaddr *ia;
66524813Skarels 
66624813Skarels 	sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
66724813Skarels 
66824813Skarels 	if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
66924813Skarels 		if (ipforward_rt.ro_rt) {
67024813Skarels 			RTFREE(ipforward_rt.ro_rt);
67124813Skarels 			ipforward_rt.ro_rt = 0;
67224813Skarels 		}
67324813Skarels 		sin->sin_family = AF_INET;
67424813Skarels 		sin->sin_addr = dst;
67524813Skarels 
67624813Skarels 		rtalloc(&ipforward_rt);
67724813Skarels 	}
67824813Skarels 	if (ipforward_rt.ro_rt == 0)
67924813Skarels 		return ((struct in_ifaddr *)0);
68024813Skarels 	/*
68124813Skarels 	 * Find address associated with outgoing interface.
68224813Skarels 	 */
68324813Skarels 	for (ia = in_ifaddr; ia; ia = ia->ia_next)
68424813Skarels 		if (ia->ia_ifp == ipforward_rt.ro_rt->rt_ifp)
68524813Skarels 			break;
68624813Skarels 	return (ia);
68724813Skarels }
68824813Skarels 
68924813Skarels /*
69024813Skarels  * Save incoming source route for use in replies,
69124813Skarels  * to be picked up later by ip_srcroute if the receiver is interested.
69224813Skarels  */
69324813Skarels save_rte(option, dst)
69424813Skarels 	caddr_t option;
69524813Skarels 	struct in_addr dst;
69624813Skarels {
69724813Skarels 	int olen;
69824813Skarels 	extern ipprintfs;
69924813Skarels 
70024813Skarels 	olen = option[IPOPT_OLEN];
70124813Skarels 	if (olen > sizeof(ip_srcrt) - 1) {
70224813Skarels 		if (ipprintfs)
70324813Skarels 			printf("save_rte: olen %d\n", olen);
70424813Skarels 		return;
70524813Skarels 	}
70624813Skarels 	bcopy(option, (caddr_t)ip_srcrt.srcopt, olen);
70724813Skarels 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
70824813Skarels 	ip_srcrt.route[ip_nhops++] = dst;
70924813Skarels }
71024813Skarels 
71124813Skarels /*
71224813Skarels  * Retrieve incoming source route for use in replies,
71324813Skarels  * in the same form used by setsockopt.
71424813Skarels  * The first hop is placed before the options, will be removed later.
71524813Skarels  */
71624813Skarels struct mbuf *
71724813Skarels ip_srcroute()
71824813Skarels {
71924813Skarels 	register struct in_addr *p, *q;
72024813Skarels 	register struct mbuf *m;
72124813Skarels 
72224813Skarels 	if (ip_nhops == 0)
72324813Skarels 		return ((struct mbuf *)0);
72424813Skarels 	m = m_get(M_WAIT, MT_SOOPTS);
72524813Skarels 	m->m_len = ip_nhops * sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1;
72624813Skarels 
72724813Skarels 	/*
72824813Skarels 	 * First save first hop for return route
72924813Skarels 	 */
73024813Skarels 	p = &ip_srcrt.route[ip_nhops - 1];
73124813Skarels 	*(mtod(m, struct in_addr *)) = *p--;
73224813Skarels 
73324813Skarels 	/*
73424813Skarels 	 * Copy option fields and padding (nop) to mbuf.
73524813Skarels 	 */
73624813Skarels 	ip_srcrt.nop = IPOPT_NOP;
73724813Skarels 	bcopy((caddr_t)&ip_srcrt, mtod(m, caddr_t) + sizeof(struct in_addr),
73824813Skarels 	    IPOPT_OFFSET + 1 + 1);
73924813Skarels 	q = (struct in_addr *)(mtod(m, caddr_t) +
74024813Skarels 	    sizeof(struct in_addr) + IPOPT_OFFSET + 1 + 1);
74124813Skarels 	/*
74224813Skarels 	 * Record return path as an IP source route,
74324813Skarels 	 * reversing the path (pointers are now aligned).
74424813Skarels 	 */
74524813Skarels 	while (p >= ip_srcrt.route)
74624813Skarels 		*q++ = *p--;
74724813Skarels 	return (m);
74824813Skarels }
74924813Skarels 
75024813Skarels /*
7514951Swnj  * Strip out IP options, at higher
7524951Swnj  * level protocol in the kernel.
7534951Swnj  * Second argument is buffer to which options
7544951Swnj  * will be moved, and return value is their length.
7554640Swnj  */
7565217Swnj ip_stripoptions(ip, mopt)
7574640Swnj 	struct ip *ip;
7585217Swnj 	struct mbuf *mopt;
7594495Swnj {
7604640Swnj 	register int i;
7614640Swnj 	register struct mbuf *m;
76224813Skarels 	register caddr_t opts;
7634640Swnj 	int olen;
7644640Swnj 
7654640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
7664951Swnj 	m = dtom(ip);
76724813Skarels 	opts = (caddr_t)(ip + 1);
7685217Swnj 	if (mopt) {
7695217Swnj 		mopt->m_len = olen;
7705217Swnj 		mopt->m_off = MMINOFF;
77124813Skarels 		bcopy(opts, mtod(mopt, caddr_t), (unsigned)olen);
7725217Swnj 	}
7734640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
77424813Skarels 	bcopy(opts  + olen, opts, (unsigned)i);
7755243Sroot 	m->m_len -= olen;
77624813Skarels 	ip->ip_hl = sizeof(struct ip) >> 2;
7774495Swnj }
7786583Ssam 
77914670Ssam u_char inetctlerrmap[PRC_NCMDS] = {
78024813Skarels 	0,		0,		0,		0,
78114670Ssam 	0,		0,		EHOSTDOWN,	EHOSTUNREACH,
78214670Ssam 	ENETUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
78324813Skarels 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
78424813Skarels 	0,		0,		0,		0,
78524813Skarels 	ENOPROTOOPT
7866583Ssam };
7876583Ssam 
78824813Skarels #ifndef	IPFORWARDING
78924813Skarels #define	IPFORWARDING	1
79024813Skarels #endif
79124813Skarels #ifndef	IPSENDREDIRECTS
79224813Skarels #define	IPSENDREDIRECTS	1
79324813Skarels #endif
7946583Ssam int	ipprintfs = 0;
79524813Skarels int	ipforwarding = IPFORWARDING;
79624813Skarels extern	int in_interfaces;
79724813Skarels int	ipsendredirects = IPSENDREDIRECTS;
79824813Skarels 
7996583Ssam /*
8006583Ssam  * Forward a packet.  If some error occurs return the sender
80118376Skarels  * an icmp packet.  Note we can't always generate a meaningful
80224813Skarels  * icmp message because icmp doesn't have a large enough repertoire
8036583Ssam  * of codes and types.
8046583Ssam  */
80524813Skarels ip_forward(ip, ifp)
8066583Ssam 	register struct ip *ip;
80724813Skarels 	struct ifnet *ifp;
8086583Ssam {
80924813Skarels 	register int error, type = 0, code;
81024813Skarels 	register struct sockaddr_in *sin;
81118376Skarels 	struct mbuf *mcopy;
81224813Skarels 	struct in_addr dest;
8136583Ssam 
81424813Skarels #ifdef lint
81524813Skarels 	dest.s_addr = 0;
81624813Skarels #endif
8176583Ssam 	if (ipprintfs)
8186583Ssam 		printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
8196583Ssam 			ip->ip_dst, ip->ip_ttl);
82018376Skarels 	ip->ip_id = htons(ip->ip_id);
82124813Skarels 	if (ipforwarding == 0 || in_interfaces <= 1) {
8226583Ssam 		/* can't tell difference between net and host */
8236583Ssam 		type = ICMP_UNREACH, code = ICMP_UNREACH_NET;
8246583Ssam 		goto sendicmp;
8256583Ssam 	}
8266583Ssam 	if (ip->ip_ttl < IPTTLDEC) {
8276583Ssam 		type = ICMP_TIMXCEED, code = ICMP_TIMXCEED_INTRANS;
8286583Ssam 		goto sendicmp;
8296583Ssam 	}
8306583Ssam 	ip->ip_ttl -= IPTTLDEC;
8316609Ssam 
8326609Ssam 	/*
8336609Ssam 	 * Save at most 64 bytes of the packet in case
8346609Ssam 	 * we need to generate an ICMP message to the src.
8356609Ssam 	 */
8367843Sroot 	mcopy = m_copy(dtom(ip), 0, imin(ip->ip_len, 64));
8376583Ssam 
83824813Skarels 	sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
83924813Skarels 	if (ipforward_rt.ro_rt == 0 ||
84024813Skarels 	    ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
84124813Skarels 		if (ipforward_rt.ro_rt) {
84224813Skarels 			RTFREE(ipforward_rt.ro_rt);
84324813Skarels 			ipforward_rt.ro_rt = 0;
84424813Skarels 		}
84524813Skarels 		sin->sin_family = AF_INET;
84624813Skarels 		sin->sin_addr = ip->ip_dst;
84724813Skarels 
84824813Skarels 		rtalloc(&ipforward_rt);
84924813Skarels 	}
85024813Skarels 	/*
85124813Skarels 	 * If forwarding packet using same interface that it came in on,
85224813Skarels 	 * perhaps should send a redirect to sender to shortcut a hop.
85324813Skarels 	 * Only send redirect if source is sending directly to us,
85424813Skarels 	 * and if packet was not source routed (or has any options).
85524813Skarels 	 */
85624813Skarels 	if (ipforward_rt.ro_rt && ipforward_rt.ro_rt->rt_ifp == ifp &&
85724813Skarels 	    ipsendredirects && ip->ip_hl == (sizeof(struct ip) >> 2)) {
85824813Skarels 		struct in_ifaddr *ia;
85924813Skarels 		extern struct in_ifaddr *ifptoia();
86024813Skarels 		u_long src = ntohl(ip->ip_src.s_addr);
86124813Skarels 		u_long dst = ntohl(ip->ip_dst.s_addr);
86224813Skarels 
86324813Skarels 		if ((ia = ifptoia(ifp)) &&
86424813Skarels 		   (src & ia->ia_subnetmask) == ia->ia_subnet) {
86524813Skarels 		    if (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY)
86624813Skarels 			dest = satosin(&ipforward_rt.ro_rt->rt_gateway)->sin_addr;
86724813Skarels 		    else
86824813Skarels 			dest = ip->ip_dst;
86924813Skarels 		    /*
87024813Skarels 		     * If the destination is reached by a route to host,
87124813Skarels 		     * is directly on the attached net (!),
87224813Skarels 		     * or if the destination is on a subnet of a local net
87324813Skarels 		     * not known to the source net, use host redirect.
87424813Skarels 		     * (We may be the correct first hop for other subnets.)
87524813Skarels 		     */
87624813Skarels 		    type = ICMP_REDIRECT;
87724813Skarels 		    code = ICMP_REDIRECT_NET;
87824813Skarels 		    if ((ipforward_rt.ro_rt->rt_flags & RTF_HOST) ||
87924813Skarels 		       (ipforward_rt.ro_rt->rt_flags & RTF_GATEWAY) == 0)
88024813Skarels 			code = ICMP_REDIRECT_HOST;
88124813Skarels 		    else for (ia = in_ifaddr; ia = ia->ia_next; )
88224813Skarels 			if ((dst & ia->ia_netmask) == ia->ia_net) {
88324813Skarels 			    if ((src & ia->ia_netmask) != ia->ia_net)
88424813Skarels 				code = ICMP_REDIRECT_HOST;
88524813Skarels 			    break;
88624813Skarels 			}
88724813Skarels 		    if (ipprintfs)
88824813Skarels 		        printf("redirect (%d) to %x\n", code, dest);
88924813Skarels 		}
89024813Skarels 	}
89124813Skarels 
89224813Skarels 	error = ip_output(dtom(ip), (struct mbuf *)0, &ipforward_rt,
89318376Skarels 		IP_FORWARDING);
89424813Skarels 	if (error)
89524813Skarels 		ipstat.ips_cantforward++;
89624813Skarels 	else if (type)
89724813Skarels 		ipstat.ips_redirectsent++;
89824813Skarels 	else {
8996609Ssam 		if (mcopy)
9006609Ssam 			m_freem(mcopy);
90121117Skarels 		ipstat.ips_forward++;
9026583Ssam 		return;
9036609Ssam 	}
90411540Ssam 	if (mcopy == NULL)
90511540Ssam 		return;
9066609Ssam 	ip = mtod(mcopy, struct ip *);
90724813Skarels 	type = ICMP_UNREACH;
9086609Ssam 	switch (error) {
9096609Ssam 
91024813Skarels 	case 0:				/* forwarded, but need redirect */
91124813Skarels 		type = ICMP_REDIRECT;
91224813Skarels 		/* code set above */
91324813Skarels 		break;
91424813Skarels 
9156609Ssam 	case ENETUNREACH:
9166609Ssam 	case ENETDOWN:
9176583Ssam 		code = ICMP_UNREACH_NET;
9186609Ssam 		break;
9196609Ssam 
9206609Ssam 	case EMSGSIZE:
9216583Ssam 		code = ICMP_UNREACH_NEEDFRAG;
9226609Ssam 		break;
9236609Ssam 
9246609Ssam 	case EPERM:
9256609Ssam 		code = ICMP_UNREACH_PORT;
9266609Ssam 		break;
9276609Ssam 
9286609Ssam 	case ENOBUFS:
9296609Ssam 		type = ICMP_SOURCEQUENCH;
9306609Ssam 		break;
9316609Ssam 
9326609Ssam 	case EHOSTDOWN:
9336609Ssam 	case EHOSTUNREACH:
9346609Ssam 		code = ICMP_UNREACH_HOST;
9356609Ssam 		break;
9366609Ssam 	}
9376583Ssam sendicmp:
93824813Skarels 	icmp_error(ip, type, code, dest);
9396583Ssam }
940