xref: /csrg-svn/sys/netinet/ip_input.c (revision 56531)
123184Smckusick /*
236814Skarels  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
332787Sbostic  * All rights reserved.
423184Smckusick  *
544480Sbostic  * %sccs.include.redist.c%
632787Sbostic  *
7*56531Sbostic  *	@(#)ip_input.c	7.23 (Berkeley) 10/11/92
823184Smckusick  */
94571Swnj 
10*56531Sbostic #include <sys/param.h>
11*56531Sbostic #include <sys/systm.h>
12*56531Sbostic #include <sys/malloc.h>
13*56531Sbostic #include <sys/mbuf.h>
14*56531Sbostic #include <sys/domain.h>
15*56531Sbostic #include <sys/protosw.h>
16*56531Sbostic #include <sys/socket.h>
17*56531Sbostic #include <sys/errno.h>
18*56531Sbostic #include <sys/time.h>
19*56531Sbostic #include <sys/kernel.h>
208695Sroot 
21*56531Sbostic #include <net/if.h>
22*56531Sbostic #include <net/route.h>
2310892Ssam 
24*56531Sbostic #include <netinet/in.h>
25*56531Sbostic #include <netinet/in_systm.h>
26*56531Sbostic #include <netinet/ip.h>
27*56531Sbostic #include <netinet/in_pcb.h>
28*56531Sbostic #include <netinet/in_var.h>
29*56531Sbostic #include <netinet/ip_var.h>
30*56531Sbostic #include <netinet/ip_icmp.h>
314495Swnj 
3236814Skarels #ifndef	IPFORWARDING
3336814Skarels #ifdef GATEWAY
3440689Skarels #define	IPFORWARDING	1	/* forward IP packets not for us */
3536814Skarels #else /* GATEWAY */
3640689Skarels #define	IPFORWARDING	0	/* don't forward IP packets not for us */
3736814Skarels #endif /* GATEWAY */
3836814Skarels #endif /* IPFORWARDING */
3936814Skarels #ifndef	IPSENDREDIRECTS
4036814Skarels #define	IPSENDREDIRECTS	1
4136814Skarels #endif
4236814Skarels int	ipforwarding = IPFORWARDING;
4336814Skarels int	ipsendredirects = IPSENDREDIRECTS;
4449042Ssklower #ifdef DIAGNOSTIC
4540689Skarels int	ipprintfs = 0;
4640689Skarels #endif
4736814Skarels 
4849042Ssklower extern	struct domain inetdomain;
4949042Ssklower extern	struct protosw inetsw[];
504898Swnj u_char	ip_protox[IPPROTO_MAX];
516210Swnj int	ipqmaxlen = IFQ_MAXLEN;
5218376Skarels struct	in_ifaddr *in_ifaddr;			/* first inet address */
534898Swnj 
544801Swnj /*
5524813Skarels  * We need to save the IP options in case a protocol wants to respond
5624813Skarels  * to an incoming packet over the same route if the packet got here
5724813Skarels  * using IP source routing.  This allows connection establishment and
5824813Skarels  * maintenance when the remote end is on a network that is not known
5924813Skarels  * to us.
6024813Skarels  */
6124813Skarels int	ip_nhops = 0;
6224813Skarels static	struct ip_srcrt {
6336814Skarels 	struct	in_addr dst;			/* final destination */
6424813Skarels 	char	nop;				/* one NOP to align */
6524813Skarels 	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
6636814Skarels 	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
6724813Skarels } ip_srcrt;
6824813Skarels 
6940689Skarels #ifdef GATEWAY
7040689Skarels extern	int if_index;
7140689Skarels u_long	*ip_ifmatrix;
7240689Skarels #endif
7340689Skarels 
7424813Skarels /*
755172Swnj  * IP initialization: fill in IP protocol switch table.
765161Swnj  * All protocols not implemented in kernel go to raw IP protocol handler.
774801Swnj  */
784801Swnj ip_init()
794801Swnj {
804898Swnj 	register struct protosw *pr;
814898Swnj 	register int i;
824495Swnj 
8324813Skarels 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
844898Swnj 	if (pr == 0)
854898Swnj 		panic("ip_init");
864898Swnj 	for (i = 0; i < IPPROTO_MAX; i++)
879030Sroot 		ip_protox[i] = pr - inetsw;
889030Sroot 	for (pr = inetdomain.dom_protosw;
8917551Skarels 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
9016990Skarels 		if (pr->pr_domain->dom_family == PF_INET &&
914898Swnj 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
929030Sroot 			ip_protox[pr->pr_protocol] = pr - inetsw;
934801Swnj 	ipq.next = ipq.prev = &ipq;
948172Sroot 	ip_id = time.tv_sec & 0xffff;
956210Swnj 	ipintrq.ifq_maxlen = ipqmaxlen;
9640689Skarels #ifdef GATEWAY
9740689Skarels 	i = (if_index + 1) * (if_index + 1) * sizeof (u_long);
9850425Smckusick 	ip_ifmatrix = (u_long *) malloc(i, M_RTABLE, M_WAITOK);
9950425Smckusick 	bzero((char *)ip_ifmatrix, i);
10040689Skarels #endif
1014801Swnj }
1024801Swnj 
1034640Swnj struct	ip *ip_reass();
10437319Skarels struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
10524813Skarels struct	route ipforward_rt;
1064640Swnj 
1074640Swnj /*
1084640Swnj  * Ip input routine.  Checksum and byte swap header.  If fragmented
10940689Skarels  * try to reassemble.  Process options.  Pass to next level.
1104640Swnj  */
1115084Swnj ipintr()
1124495Swnj {
1134923Swnj 	register struct ip *ip;
1145084Swnj 	register struct mbuf *m;
1154495Swnj 	register struct ipq *fp;
11618376Skarels 	register struct in_ifaddr *ia;
1175084Swnj 	int hlen, s;
1184495Swnj 
1195084Swnj next:
1204640Swnj 	/*
1215084Swnj 	 * Get next datagram off input queue and get IP header
1225084Swnj 	 * in first mbuf.
1234640Swnj 	 */
1245084Swnj 	s = splimp();
12537319Skarels 	IF_DEQUEUE(&ipintrq, m);
1265084Swnj 	splx(s);
1275218Swnj 	if (m == 0)
1285084Swnj 		return;
12944967Skarels #ifdef	DIAGNOSTIC
13044967Skarels 	if ((m->m_flags & M_PKTHDR) == 0)
13144967Skarels 		panic("ipintr no HDR");
13244967Skarels #endif
13326001Skarels 	/*
13426001Skarels 	 * If no IP addresses have been set yet but the interfaces
13526001Skarels 	 * are receiving, can't do anything with incoming packets yet.
13626001Skarels 	 */
13726001Skarels 	if (in_ifaddr == NULL)
13826001Skarels 		goto bad;
13925920Skarels 	ipstat.ips_total++;
14040689Skarels 	if (m->m_len < sizeof (struct ip) &&
14111232Ssam 	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
14211232Ssam 		ipstat.ips_toosmall++;
14311232Ssam 		goto next;
14411232Ssam 	}
1454640Swnj 	ip = mtod(m, struct ip *);
14618376Skarels 	hlen = ip->ip_hl << 2;
14724813Skarels 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
14818376Skarels 		ipstat.ips_badhlen++;
14921117Skarels 		goto bad;
15018376Skarels 	}
15118376Skarels 	if (hlen > m->m_len) {
15211232Ssam 		if ((m = m_pullup(m, hlen)) == 0) {
15311232Ssam 			ipstat.ips_badhlen++;
15411232Ssam 			goto next;
15511232Ssam 		}
1565161Swnj 		ip = mtod(m, struct ip *);
1575161Swnj 	}
15837319Skarels 	if (ip->ip_sum = in_cksum(m, hlen)) {
15937319Skarels 		ipstat.ips_badsum++;
16037319Skarels 		goto bad;
16137319Skarels 	}
1624951Swnj 
1634951Swnj 	/*
1644951Swnj 	 * Convert fields to host representation.
1654951Swnj 	 */
16640689Skarels 	NTOHS(ip->ip_len);
16711232Ssam 	if (ip->ip_len < hlen) {
16811232Ssam 		ipstat.ips_badlen++;
16911232Ssam 		goto bad;
17011232Ssam 	}
17140689Skarels 	NTOHS(ip->ip_id);
17240689Skarels 	NTOHS(ip->ip_off);
1734495Swnj 
1744543Swnj 	/*
1754640Swnj 	 * Check that the amount of data in the buffers
1764640Swnj 	 * is as at least much as the IP header would have us expect.
1774640Swnj 	 * Trim mbufs if longer than we expect.
1784640Swnj 	 * Drop packet if shorter than we expect.
1794543Swnj 	 */
18037319Skarels 	if (m->m_pkthdr.len < ip->ip_len) {
18137319Skarels 		ipstat.ips_tooshort++;
18237319Skarels 		goto bad;
1836088Sroot 	}
18437319Skarels 	if (m->m_pkthdr.len > ip->ip_len) {
18537319Skarels 		if (m->m_len == m->m_pkthdr.len) {
18637319Skarels 			m->m_len = ip->ip_len;
18737319Skarels 			m->m_pkthdr.len = ip->ip_len;
18837319Skarels 		} else
18937319Skarels 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
1904495Swnj 	}
1914495Swnj 
1924640Swnj 	/*
1934640Swnj 	 * Process options and, if not destined for us,
1946583Ssam 	 * ship it on.  ip_dooptions returns 1 when an
1956583Ssam 	 * error was detected (causing an icmp message
19621117Skarels 	 * to be sent and the original packet to be freed).
1974640Swnj 	 */
19824813Skarels 	ip_nhops = 0;		/* for source routed packets */
19937319Skarels 	if (hlen > sizeof (struct ip) && ip_dooptions(m))
2006583Ssam 		goto next;
2016210Swnj 
2026338Ssam 	/*
20318376Skarels 	 * Check our list of addresses, to see if the packet is for us.
2046338Ssam 	 */
20518376Skarels 	for (ia = in_ifaddr; ia; ia = ia->ia_next) {
20618376Skarels #define	satosin(sa)	((struct sockaddr_in *)(sa))
2076338Ssam 
20818376Skarels 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
20924813Skarels 			goto ours;
21025195Skarels 		if (
21125195Skarels #ifdef	DIRECTED_BROADCAST
21237319Skarels 		    ia->ia_ifp == m->m_pkthdr.rcvif &&
21325195Skarels #endif
21425195Skarels 		    (ia->ia_ifp->if_flags & IFF_BROADCAST)) {
21526247Skarels 			u_long t;
21625195Skarels 
21725195Skarels 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
21825195Skarels 			    ip->ip_dst.s_addr)
21925195Skarels 				goto ours;
22025195Skarels 			if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
22125195Skarels 				goto ours;
22225195Skarels 			/*
22325195Skarels 			 * Look for all-0's host part (old broadcast addr),
22425195Skarels 			 * either for subnet or net.
22525195Skarels 			 */
22626247Skarels 			t = ntohl(ip->ip_dst.s_addr);
22726247Skarels 			if (t == ia->ia_subnet)
22825195Skarels 				goto ours;
22926247Skarels 			if (t == ia->ia_net)
23025195Skarels 				goto ours;
23125195Skarels 		}
2326338Ssam 	}
23354716Ssklower #ifdef MULTICAST
23454716Ssklower 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
23554716Ssklower 		struct in_multi *inm;
23654716Ssklower #ifdef MROUTING
23754716Ssklower 		extern struct socket *ip_mrouter;
23854716Ssklower 
23954716Ssklower 		if (ip_mrouter) {
24054716Ssklower 			/*
24154716Ssklower 			 * If we are acting as a multicast router, all
24254716Ssklower 			 * incoming multicast packets are passed to the
24354716Ssklower 			 * kernel-level multicast forwarding function.
24454716Ssklower 			 * The packet is returned (relatively) intact; if
24554716Ssklower 			 * ip_mforward() returns a non-zero value, the packet
24654716Ssklower 			 * must be discarded, else it may be accepted below.
24754716Ssklower 			 *
24854716Ssklower 			 * (The IP ident field is put in the same byte order
24954716Ssklower 			 * as expected when ip_mforward() is called from
25054716Ssklower 			 * ip_output().)
25154716Ssklower 			 */
25254716Ssklower 			ip->ip_id = htons(ip->ip_id);
25354716Ssklower 			if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
25454716Ssklower 				m_freem(m);
25554716Ssklower 				goto next;
25654716Ssklower 			}
25754716Ssklower 			ip->ip_id = ntohs(ip->ip_id);
25854716Ssklower 
25954716Ssklower 			/*
26054716Ssklower 			 * The process-level routing demon needs to receive
26154716Ssklower 			 * all multicast IGMP packets, whether or not this
26254716Ssklower 			 * host belongs to their destination groups.
26354716Ssklower 			 */
26454716Ssklower 			if (ip->ip_p == IPPROTO_IGMP)
26554716Ssklower 				goto ours;
26654716Ssklower 		}
26754716Ssklower #endif
26854716Ssklower 		/*
26954716Ssklower 		 * See if we belong to the destination multicast group on the
27054716Ssklower 		 * arrival interface.
27154716Ssklower 		 */
27254716Ssklower 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
27354716Ssklower 		if (inm == NULL) {
27454716Ssklower 			m_freem(m);
27554716Ssklower 			goto next;
27654716Ssklower 		}
27754716Ssklower 		goto ours;
27854716Ssklower 	}
27954716Ssklower #endif
28024813Skarels 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
28124813Skarels 		goto ours;
28224813Skarels 	if (ip->ip_dst.s_addr == INADDR_ANY)
28324813Skarels 		goto ours;
2844495Swnj 
2854640Swnj 	/*
28624813Skarels 	 * Not for us; forward if possible and desirable.
28724813Skarels 	 */
28840689Skarels 	if (ipforwarding == 0) {
28936814Skarels 		ipstat.ips_cantforward++;
29036814Skarels 		m_freem(m);
29136814Skarels 	} else
29240689Skarels 		ip_forward(m, 0);
29324813Skarels 	goto next;
29424813Skarels 
29524813Skarels ours:
29624813Skarels 	/*
29733743Skarels 	 * If offset or IP_MF are set, must reassemble.
29833743Skarels 	 * Otherwise, nothing need be done.
29933743Skarels 	 * (We could look in the reassembly queue to see
30033743Skarels 	 * if the packet was previously fragmented,
30133743Skarels 	 * but it's not worth the time; just let them time out.)
3024640Swnj 	 */
30333743Skarels 	if (ip->ip_off &~ IP_DF) {
30440689Skarels 		if (m->m_flags & M_EXT) {		/* XXX */
30540689Skarels 			if ((m = m_pullup(m, sizeof (struct ip))) == 0) {
30640689Skarels 				ipstat.ips_toosmall++;
30740689Skarels 				goto next;
30840689Skarels 			}
30940689Skarels 			ip = mtod(m, struct ip *);
31040689Skarels 		}
31133743Skarels 		/*
31233743Skarels 		 * Look for queue of fragments
31333743Skarels 		 * of this datagram.
31433743Skarels 		 */
31533743Skarels 		for (fp = ipq.next; fp != &ipq; fp = fp->next)
31633743Skarels 			if (ip->ip_id == fp->ipq_id &&
31733743Skarels 			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
31833743Skarels 			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
31933743Skarels 			    ip->ip_p == fp->ipq_p)
32033743Skarels 				goto found;
32133743Skarels 		fp = 0;
3224640Swnj found:
3234495Swnj 
32433743Skarels 		/*
32533743Skarels 		 * Adjust ip_len to not reflect header,
32633743Skarels 		 * set ip_mff if more fragments are expected,
32733743Skarels 		 * convert offset of this to bytes.
32833743Skarels 		 */
32933743Skarels 		ip->ip_len -= hlen;
33033743Skarels 		((struct ipasfrag *)ip)->ipf_mff = 0;
33133743Skarels 		if (ip->ip_off & IP_MF)
33233743Skarels 			((struct ipasfrag *)ip)->ipf_mff = 1;
33333743Skarels 		ip->ip_off <<= 3;
3344495Swnj 
33533743Skarels 		/*
33633743Skarels 		 * If datagram marked as having more fragments
33733743Skarels 		 * or if this is not the first fragment,
33833743Skarels 		 * attempt reassembly; if it succeeds, proceed.
33933743Skarels 		 */
34033743Skarels 		if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
34133743Skarels 			ipstat.ips_fragments++;
34233743Skarels 			ip = ip_reass((struct ipasfrag *)ip, fp);
34333743Skarels 			if (ip == 0)
34433743Skarels 				goto next;
34539185Ssklower 			else
34639185Ssklower 				ipstat.ips_reassembled++;
34733743Skarels 			m = dtom(ip);
34833743Skarels 		} else
34933743Skarels 			if (fp)
35033743Skarels 				ip_freef(fp);
3514640Swnj 	} else
35233743Skarels 		ip->ip_len -= hlen;
3534951Swnj 
3544951Swnj 	/*
3554951Swnj 	 * Switch out to protocol's input routine.
3564951Swnj 	 */
35739185Ssklower 	ipstat.ips_delivered++;
35837319Skarels 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
3595084Swnj 	goto next;
3604951Swnj bad:
3614951Swnj 	m_freem(m);
3625084Swnj 	goto next;
3634640Swnj }
3644495Swnj 
3654640Swnj /*
3664640Swnj  * Take incoming datagram fragment and try to
3674951Swnj  * reassemble it into whole datagram.  If a chain for
3684640Swnj  * reassembly of this datagram already exists, then it
3694640Swnj  * is given as fp; otherwise have to make a chain.
3704640Swnj  */
3714640Swnj struct ip *
3724640Swnj ip_reass(ip, fp)
3734898Swnj 	register struct ipasfrag *ip;
3744640Swnj 	register struct ipq *fp;
3754640Swnj {
3764640Swnj 	register struct mbuf *m = dtom(ip);
3774898Swnj 	register struct ipasfrag *q;
3784640Swnj 	struct mbuf *t;
3794640Swnj 	int hlen = ip->ip_hl << 2;
3804640Swnj 	int i, next;
3814543Swnj 
3824640Swnj 	/*
3834640Swnj 	 * Presence of header sizes in mbufs
3844640Swnj 	 * would confuse code below.
3854640Swnj 	 */
38637319Skarels 	m->m_data += hlen;
3874640Swnj 	m->m_len -= hlen;
3884495Swnj 
3894640Swnj 	/*
3904640Swnj 	 * If first fragment to arrive, create a reassembly queue.
3914640Swnj 	 */
3924640Swnj 	if (fp == 0) {
39331201Skarels 		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
3944640Swnj 			goto dropfrag;
3954640Swnj 		fp = mtod(t, struct ipq *);
3964640Swnj 		insque(fp, &ipq);
3974640Swnj 		fp->ipq_ttl = IPFRAGTTL;
3984640Swnj 		fp->ipq_p = ip->ip_p;
3994640Swnj 		fp->ipq_id = ip->ip_id;
4004898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
4014898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
4024898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
4035161Swnj 		q = (struct ipasfrag *)fp;
4045161Swnj 		goto insert;
4054640Swnj 	}
4064495Swnj 
4074640Swnj 	/*
4084640Swnj 	 * Find a segment which begins after this one does.
4094640Swnj 	 */
4104898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
4114640Swnj 		if (q->ip_off > ip->ip_off)
4124640Swnj 			break;
4134495Swnj 
4144640Swnj 	/*
4154640Swnj 	 * If there is a preceding segment, it may provide some of
4164640Swnj 	 * our data already.  If so, drop the data from the incoming
4174640Swnj 	 * segment.  If it provides all of our data, drop us.
4184640Swnj 	 */
4194898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
4204898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
4214640Swnj 		if (i > 0) {
4224640Swnj 			if (i >= ip->ip_len)
4234640Swnj 				goto dropfrag;
4244640Swnj 			m_adj(dtom(ip), i);
4254640Swnj 			ip->ip_off += i;
4264640Swnj 			ip->ip_len -= i;
4274640Swnj 		}
4284640Swnj 	}
4294543Swnj 
4304640Swnj 	/*
4314640Swnj 	 * While we overlap succeeding segments trim them or,
4324640Swnj 	 * if they are completely covered, dequeue them.
4334640Swnj 	 */
4344898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
4354640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
4364640Swnj 		if (i < q->ip_len) {
4374640Swnj 			q->ip_len -= i;
4386256Sroot 			q->ip_off += i;
4394640Swnj 			m_adj(dtom(q), i);
4404640Swnj 			break;
4414495Swnj 		}
4424898Swnj 		q = q->ipf_next;
4434898Swnj 		m_freem(dtom(q->ipf_prev));
4444898Swnj 		ip_deq(q->ipf_prev);
4454543Swnj 	}
4464495Swnj 
4475161Swnj insert:
4484640Swnj 	/*
4494640Swnj 	 * Stick new segment in its place;
4504640Swnj 	 * check for complete reassembly.
4514640Swnj 	 */
4524898Swnj 	ip_enq(ip, q->ipf_prev);
4534640Swnj 	next = 0;
4544898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
4554640Swnj 		if (q->ip_off != next)
4564640Swnj 			return (0);
4574640Swnj 		next += q->ip_len;
4584640Swnj 	}
4594898Swnj 	if (q->ipf_prev->ipf_mff)
4604640Swnj 		return (0);
4614495Swnj 
4624640Swnj 	/*
4634640Swnj 	 * Reassembly is complete; concatenate fragments.
4644640Swnj 	 */
4654640Swnj 	q = fp->ipq_next;
4664640Swnj 	m = dtom(q);
4674640Swnj 	t = m->m_next;
4684640Swnj 	m->m_next = 0;
4694640Swnj 	m_cat(m, t);
4706298Swnj 	q = q->ipf_next;
4716298Swnj 	while (q != (struct ipasfrag *)fp) {
4726298Swnj 		t = dtom(q);
4736298Swnj 		q = q->ipf_next;
4746298Swnj 		m_cat(m, t);
4756298Swnj 	}
4764495Swnj 
4774640Swnj 	/*
4784640Swnj 	 * Create header for new ip packet by
4794640Swnj 	 * modifying header of first packet;
4804640Swnj 	 * dequeue and discard fragment reassembly header.
4814640Swnj 	 * Make header visible.
4824640Swnj 	 */
4834640Swnj 	ip = fp->ipq_next;
4844640Swnj 	ip->ip_len = next;
4854898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
4864898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
4874640Swnj 	remque(fp);
4884907Swnj 	(void) m_free(dtom(fp));
4894640Swnj 	m = dtom(ip);
49024813Skarels 	m->m_len += (ip->ip_hl << 2);
49137319Skarels 	m->m_data -= (ip->ip_hl << 2);
49249042Ssklower 	/* some debugging cruft by sklower, below, will go away soon */
49349042Ssklower 	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
49449042Ssklower 		register int plen = 0;
49549042Ssklower 		for (t = m; m; m = m->m_next)
49649042Ssklower 			plen += m->m_len;
49749042Ssklower 		t->m_pkthdr.len = plen;
49849042Ssklower 	}
4994898Swnj 	return ((struct ip *)ip);
5004495Swnj 
5014640Swnj dropfrag:
50224813Skarels 	ipstat.ips_fragdropped++;
5034640Swnj 	m_freem(m);
5044640Swnj 	return (0);
5054495Swnj }
5064495Swnj 
5074640Swnj /*
5084640Swnj  * Free a fragment reassembly header and all
5094640Swnj  * associated datagrams.
5104640Swnj  */
5114640Swnj ip_freef(fp)
5124640Swnj 	struct ipq *fp;
5134495Swnj {
51410735Ssam 	register struct ipasfrag *q, *p;
5154495Swnj 
51610735Ssam 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {
51710735Ssam 		p = q->ipf_next;
51810735Ssam 		ip_deq(q);
5194640Swnj 		m_freem(dtom(q));
52010735Ssam 	}
52110735Ssam 	remque(fp);
52210735Ssam 	(void) m_free(dtom(fp));
5234495Swnj }
5244495Swnj 
5254640Swnj /*
5264640Swnj  * Put an ip fragment on a reassembly chain.
5274640Swnj  * Like insque, but pointers in middle of structure.
5284640Swnj  */
5294640Swnj ip_enq(p, prev)
5304898Swnj 	register struct ipasfrag *p, *prev;
5314495Swnj {
5324951Swnj 
5334898Swnj 	p->ipf_prev = prev;
5344898Swnj 	p->ipf_next = prev->ipf_next;
5354898Swnj 	prev->ipf_next->ipf_prev = p;
5364898Swnj 	prev->ipf_next = p;
5374495Swnj }
5384495Swnj 
5394640Swnj /*
5404640Swnj  * To ip_enq as remque is to insque.
5414640Swnj  */
5424640Swnj ip_deq(p)
5434898Swnj 	register struct ipasfrag *p;
5444640Swnj {
5454951Swnj 
5464898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
5474898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
5484495Swnj }
5494495Swnj 
5504640Swnj /*
5514640Swnj  * IP timer processing;
5524640Swnj  * if a timer expires on a reassembly
5534640Swnj  * queue, discard it.
5544640Swnj  */
5554801Swnj ip_slowtimo()
5564495Swnj {
5574495Swnj 	register struct ipq *fp;
5584640Swnj 	int s = splnet();
5594951Swnj 
5605243Sroot 	fp = ipq.next;
5615243Sroot 	if (fp == 0) {
5625243Sroot 		splx(s);
5635243Sroot 		return;
5645243Sroot 	}
56510735Ssam 	while (fp != &ipq) {
56610735Ssam 		--fp->ipq_ttl;
56710735Ssam 		fp = fp->next;
56824813Skarels 		if (fp->prev->ipq_ttl == 0) {
56924813Skarels 			ipstat.ips_fragtimeout++;
57010735Ssam 			ip_freef(fp->prev);
57124813Skarels 		}
57210735Ssam 	}
5734640Swnj 	splx(s);
5744495Swnj }
5754495Swnj 
5764951Swnj /*
5774951Swnj  * Drain off all datagram fragments.
5784951Swnj  */
5794801Swnj ip_drain()
5804801Swnj {
5814801Swnj 
58224813Skarels 	while (ipq.next != &ipq) {
58324813Skarels 		ipstat.ips_fragdropped++;
58410735Ssam 		ip_freef(ipq.next);
58524813Skarels 	}
5864801Swnj }
5874923Swnj 
58830925Skarels extern struct in_ifaddr *ifptoia();
58924813Skarels struct in_ifaddr *ip_rtaddr();
59024813Skarels 
5914640Swnj /*
5924640Swnj  * Do option processing on a datagram,
59340689Skarels  * possibly discarding it if bad options are encountered,
59440689Skarels  * or forwarding it if source-routed.
59540689Skarels  * Returns 1 if packet has been forwarded/freed,
59640689Skarels  * 0 if the packet should be processed further.
5974640Swnj  */
59837319Skarels ip_dooptions(m)
59936814Skarels 	struct mbuf *m;
6004495Swnj {
60136814Skarels 	register struct ip *ip = mtod(m, struct ip *);
6024640Swnj 	register u_char *cp;
60324813Skarels 	register struct ip_timestamp *ipt;
60424813Skarels 	register struct in_ifaddr *ia;
60536814Skarels 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
6064923Swnj 	struct in_addr *sin;
60724813Skarels 	n_time ntime;
6084495Swnj 
6094640Swnj 	cp = (u_char *)(ip + 1);
6104640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
6114640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
61224813Skarels 		opt = cp[IPOPT_OPTVAL];
6134640Swnj 		if (opt == IPOPT_EOL)
6144640Swnj 			break;
6154640Swnj 		if (opt == IPOPT_NOP)
6164640Swnj 			optlen = 1;
61716392Ssam 		else {
61824813Skarels 			optlen = cp[IPOPT_OLEN];
61924813Skarels 			if (optlen <= 0 || optlen > cnt) {
62024813Skarels 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
62117551Skarels 				goto bad;
62224813Skarels 			}
62316392Ssam 		}
6244640Swnj 		switch (opt) {
6254495Swnj 
6264640Swnj 		default:
6274640Swnj 			break;
6284495Swnj 
6294951Swnj 		/*
6304951Swnj 		 * Source routing with record.
6314951Swnj 		 * Find interface with current destination address.
6324951Swnj 		 * If none on this machine then drop if strictly routed,
6334951Swnj 		 * or do nothing if loosely routed.
6344951Swnj 		 * Record interface address and bring up next address
6354951Swnj 		 * component.  If strictly routed make sure next
63640689Skarels 		 * address is on directly accessible net.
6374951Swnj 		 */
6384640Swnj 		case IPOPT_LSRR:
6397508Sroot 		case IPOPT_SSRR:
64024813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
64124813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
64224813Skarels 				goto bad;
64324813Skarels 			}
64424813Skarels 			ipaddr.sin_addr = ip->ip_dst;
64524813Skarels 			ia = (struct in_ifaddr *)
64624813Skarels 				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
64724813Skarels 			if (ia == 0) {
64824813Skarels 				if (opt == IPOPT_SSRR) {
64924813Skarels 					type = ICMP_UNREACH;
65024813Skarels 					code = ICMP_UNREACH_SRCFAIL;
6514951Swnj 					goto bad;
65224813Skarels 				}
65324813Skarels 				/*
65424813Skarels 				 * Loose routing, and not at next destination
65524813Skarels 				 * yet; nothing to do except forward.
65624813Skarels 				 */
6574951Swnj 				break;
6584640Swnj 			}
65924813Skarels 			off--;			/* 0 origin */
66024813Skarels 			if (off > optlen - sizeof(struct in_addr)) {
66124813Skarels 				/*
66224813Skarels 				 * End of source route.  Should be for us.
66324813Skarels 				 */
66424813Skarels 				save_rte(cp, ip->ip_src);
6654951Swnj 				break;
66624813Skarels 			}
66724813Skarels 			/*
66824813Skarels 			 * locate outgoing interface
66924813Skarels 			 */
67026384Skarels 			bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
67124813Skarels 			    sizeof(ipaddr.sin_addr));
67240689Skarels 			if (opt == IPOPT_SSRR) {
67340689Skarels #define	INA	struct in_ifaddr *
67440689Skarels #define	SA	struct sockaddr *
67540689Skarels 			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
67640689Skarels 				ia = in_iaonnetof(in_netof(ipaddr.sin_addr));
67740689Skarels 			} else
67840689Skarels 				ia = ip_rtaddr(ipaddr.sin_addr);
67940689Skarels 			if (ia == 0) {
68024813Skarels 				type = ICMP_UNREACH;
68124813Skarels 				code = ICMP_UNREACH_SRCFAIL;
6824951Swnj 				goto bad;
68324813Skarels 			}
68424813Skarels 			ip->ip_dst = ipaddr.sin_addr;
68526384Skarels 			bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
68626384Skarels 			    (caddr_t)(cp + off), sizeof(struct in_addr));
68724813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
68836814Skarels 			forward = 1;
6894640Swnj 			break;
6904495Swnj 
69124813Skarels 		case IPOPT_RR:
69224813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
69324813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
69424813Skarels 				goto bad;
69524813Skarels 			}
69624813Skarels 			/*
69724813Skarels 			 * If no space remains, ignore.
69824813Skarels 			 */
69924813Skarels 			off--;			/* 0 origin */
70024813Skarels 			if (off > optlen - sizeof(struct in_addr))
70124813Skarels 				break;
70231393Skarels 			bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
70324813Skarels 			    sizeof(ipaddr.sin_addr));
70424813Skarels 			/*
70537319Skarels 			 * locate outgoing interface; if we're the destination,
70637319Skarels 			 * use the incoming interface (should be same).
70724813Skarels 			 */
70840689Skarels 			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
70937319Skarels 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
71024813Skarels 				type = ICMP_UNREACH;
71132113Skarels 				code = ICMP_UNREACH_HOST;
71224813Skarels 				goto bad;
71324813Skarels 			}
71426384Skarels 			bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
71526384Skarels 			    (caddr_t)(cp + off), sizeof(struct in_addr));
71624813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
71724813Skarels 			break;
71824813Skarels 
7194640Swnj 		case IPOPT_TS:
7206583Ssam 			code = cp - (u_char *)ip;
7214801Swnj 			ipt = (struct ip_timestamp *)cp;
7224801Swnj 			if (ipt->ipt_len < 5)
7234640Swnj 				goto bad;
7244801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
7254801Swnj 				if (++ipt->ipt_oflw == 0)
7264640Swnj 					goto bad;
7274495Swnj 				break;
7284640Swnj 			}
72930925Skarels 			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
7304801Swnj 			switch (ipt->ipt_flg) {
7314495Swnj 
7324640Swnj 			case IPOPT_TS_TSONLY:
7334640Swnj 				break;
7344640Swnj 
7354640Swnj 			case IPOPT_TS_TSANDADDR:
73624813Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
73724813Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
7384640Swnj 					goto bad;
73937319Skarels 				ia = ifptoia(m->m_pkthdr.rcvif);
74030925Skarels 				bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
74124813Skarels 				    (caddr_t)sin, sizeof(struct in_addr));
74230925Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
7434640Swnj 				break;
7444640Swnj 
7454640Swnj 			case IPOPT_TS_PRESPEC:
74630925Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
74730925Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
74830925Skarels 					goto bad;
74924813Skarels 				bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
75024813Skarels 				    sizeof(struct in_addr));
75140689Skarels 				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
7524951Swnj 					continue;
75324813Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
7544640Swnj 				break;
7554640Swnj 
7564495Swnj 			default:
7574640Swnj 				goto bad;
7584495Swnj 			}
75924813Skarels 			ntime = iptime();
76030925Skarels 			bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
76130925Skarels 			    sizeof(n_time));
76224813Skarels 			ipt->ipt_ptr += sizeof(n_time);
7634640Swnj 		}
7644495Swnj 	}
76536814Skarels 	if (forward) {
76640689Skarels 		ip_forward(m, 1);
76736814Skarels 		return (1);
76836814Skarels 	} else
76936814Skarels 		return (0);
7704640Swnj bad:
77137319Skarels 	icmp_error(m, type, code);
7726583Ssam 	return (1);
7734495Swnj }
7744495Swnj 
7754640Swnj /*
77624813Skarels  * Given address of next destination (final or next hop),
77724813Skarels  * return internet address info of interface to be used to get there.
77824813Skarels  */
77924813Skarels struct in_ifaddr *
78024813Skarels ip_rtaddr(dst)
78124813Skarels 	 struct in_addr dst;
78224813Skarels {
78324813Skarels 	register struct sockaddr_in *sin;
78424813Skarels 
78524813Skarels 	sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
78624813Skarels 
78724813Skarels 	if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
78824813Skarels 		if (ipforward_rt.ro_rt) {
78924813Skarels 			RTFREE(ipforward_rt.ro_rt);
79024813Skarels 			ipforward_rt.ro_rt = 0;
79124813Skarels 		}
79224813Skarels 		sin->sin_family = AF_INET;
79337319Skarels 		sin->sin_len = sizeof(*sin);
79424813Skarels 		sin->sin_addr = dst;
79524813Skarels 
79624813Skarels 		rtalloc(&ipforward_rt);
79724813Skarels 	}
79824813Skarels 	if (ipforward_rt.ro_rt == 0)
79924813Skarels 		return ((struct in_ifaddr *)0);
80040689Skarels 	return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
80124813Skarels }
80224813Skarels 
80324813Skarels /*
80424813Skarels  * Save incoming source route for use in replies,
80524813Skarels  * to be picked up later by ip_srcroute if the receiver is interested.
80624813Skarels  */
80724813Skarels save_rte(option, dst)
80826384Skarels 	u_char *option;
80924813Skarels 	struct in_addr dst;
81024813Skarels {
81126384Skarels 	unsigned olen;
81224813Skarels 
81324813Skarels 	olen = option[IPOPT_OLEN];
81449042Ssklower #ifdef DIAGNOSTIC
81536814Skarels 	if (ipprintfs)
81636814Skarels 		printf("save_rte: olen %d\n", olen);
81740689Skarels #endif
81836814Skarels 	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
81924813Skarels 		return;
82026384Skarels 	bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
82124813Skarels 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
82236814Skarels 	ip_srcrt.dst = dst;
82324813Skarels }
82424813Skarels 
82524813Skarels /*
82624813Skarels  * Retrieve incoming source route for use in replies,
82724813Skarels  * in the same form used by setsockopt.
82824813Skarels  * The first hop is placed before the options, will be removed later.
82924813Skarels  */
83024813Skarels struct mbuf *
83124813Skarels ip_srcroute()
83224813Skarels {
83324813Skarels 	register struct in_addr *p, *q;
83424813Skarels 	register struct mbuf *m;
83524813Skarels 
83624813Skarels 	if (ip_nhops == 0)
83724813Skarels 		return ((struct mbuf *)0);
83831201Skarels 	m = m_get(M_DONTWAIT, MT_SOOPTS);
83931201Skarels 	if (m == 0)
84031201Skarels 		return ((struct mbuf *)0);
84124813Skarels 
84236814Skarels #define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
84336814Skarels 
84436814Skarels 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
84536814Skarels 	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
84636814Skarels 	    OPTSIZ;
84749042Ssklower #ifdef DIAGNOSTIC
84836814Skarels 	if (ipprintfs)
84936814Skarels 		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
85040689Skarels #endif
85136814Skarels 
85224813Skarels 	/*
85324813Skarels 	 * First save first hop for return route
85424813Skarels 	 */
85524813Skarels 	p = &ip_srcrt.route[ip_nhops - 1];
85624813Skarels 	*(mtod(m, struct in_addr *)) = *p--;
85749042Ssklower #ifdef DIAGNOSTIC
85836814Skarels 	if (ipprintfs)
85949882Sbostic 		printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr));
86040689Skarels #endif
86124813Skarels 
86224813Skarels 	/*
86324813Skarels 	 * Copy option fields and padding (nop) to mbuf.
86424813Skarels 	 */
86524813Skarels 	ip_srcrt.nop = IPOPT_NOP;
86636814Skarels 	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
86736814Skarels 	bcopy((caddr_t)&ip_srcrt.nop,
86836814Skarels 	    mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
86924813Skarels 	q = (struct in_addr *)(mtod(m, caddr_t) +
87036814Skarels 	    sizeof(struct in_addr) + OPTSIZ);
87136814Skarels #undef OPTSIZ
87224813Skarels 	/*
87324813Skarels 	 * Record return path as an IP source route,
87424813Skarels 	 * reversing the path (pointers are now aligned).
87524813Skarels 	 */
87636814Skarels 	while (p >= ip_srcrt.route) {
87749042Ssklower #ifdef DIAGNOSTIC
87836814Skarels 		if (ipprintfs)
87949882Sbostic 			printf(" %lx", ntohl(q->s_addr));
88040689Skarels #endif
88124813Skarels 		*q++ = *p--;
88236814Skarels 	}
88336814Skarels 	/*
88436814Skarels 	 * Last hop goes to final destination.
88536814Skarels 	 */
88636814Skarels 	*q = ip_srcrt.dst;
88749042Ssklower #ifdef DIAGNOSTIC
88836814Skarels 	if (ipprintfs)
88949882Sbostic 		printf(" %lx\n", ntohl(q->s_addr));
89040689Skarels #endif
89124813Skarels 	return (m);
89224813Skarels }
89324813Skarels 
89424813Skarels /*
8954951Swnj  * Strip out IP options, at higher
8964951Swnj  * level protocol in the kernel.
8974951Swnj  * Second argument is buffer to which options
8984951Swnj  * will be moved, and return value is their length.
89936814Skarels  * XXX should be deleted; last arg currently ignored.
9004640Swnj  */
90137319Skarels ip_stripoptions(m, mopt)
90237319Skarels 	register struct mbuf *m;
9035217Swnj 	struct mbuf *mopt;
9044495Swnj {
9054640Swnj 	register int i;
90637319Skarels 	struct ip *ip = mtod(m, struct ip *);
90724813Skarels 	register caddr_t opts;
9084640Swnj 	int olen;
9094640Swnj 
9104640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
91124813Skarels 	opts = (caddr_t)(ip + 1);
9124640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
91324813Skarels 	bcopy(opts  + olen, opts, (unsigned)i);
9145243Sroot 	m->m_len -= olen;
91537319Skarels 	if (m->m_flags & M_PKTHDR)
91637319Skarels 		m->m_pkthdr.len -= olen;
91724813Skarels 	ip->ip_hl = sizeof(struct ip) >> 2;
9184495Swnj }
9196583Ssam 
92014670Ssam u_char inetctlerrmap[PRC_NCMDS] = {
92124813Skarels 	0,		0,		0,		0,
92240689Skarels 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
92340689Skarels 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
92424813Skarels 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
92524813Skarels 	0,		0,		0,		0,
92624813Skarels 	ENOPROTOOPT
9276583Ssam };
9286583Ssam 
9296583Ssam /*
9306583Ssam  * Forward a packet.  If some error occurs return the sender
93118376Skarels  * an icmp packet.  Note we can't always generate a meaningful
93224813Skarels  * icmp message because icmp doesn't have a large enough repertoire
9336583Ssam  * of codes and types.
93426308Skarels  *
93540689Skarels  * If not forwarding, just drop the packet.  This could be confusing
93640689Skarels  * if ipforwarding was zero but some routing protocol was advancing
93740689Skarels  * us as a gateway to somewhere.  However, we must let the routing
93840689Skarels  * protocol deal with that.
93940689Skarels  *
94040689Skarels  * The srcrt parameter indicates whether the packet is being forwarded
94140689Skarels  * via a source route.
9426583Ssam  */
94340689Skarels ip_forward(m, srcrt)
94436814Skarels 	struct mbuf *m;
94540689Skarels 	int srcrt;
9466583Ssam {
94736814Skarels 	register struct ip *ip = mtod(m, struct ip *);
94824813Skarels 	register struct sockaddr_in *sin;
94940689Skarels 	register struct rtentry *rt;
95040689Skarels 	int error, type = 0, code;
95118376Skarels 	struct mbuf *mcopy;
95224813Skarels 	struct in_addr dest;
9536583Ssam 
95424813Skarels 	dest.s_addr = 0;
95549042Ssklower #ifdef DIAGNOSTIC
9566583Ssam 	if (ipprintfs)
9576583Ssam 		printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
9586583Ssam 			ip->ip_dst, ip->ip_ttl);
95940689Skarels #endif
96037319Skarels 	if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
96126308Skarels 		ipstat.ips_cantforward++;
96237319Skarels 		m_freem(m);
96326308Skarels 		return;
9646583Ssam 	}
96540689Skarels 	HTONS(ip->ip_id);
96631393Skarels 	if (ip->ip_ttl <= IPTTLDEC) {
96740689Skarels 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest);
96840689Skarels 		return;
9696583Ssam 	}
9706583Ssam 	ip->ip_ttl -= IPTTLDEC;
9716609Ssam 
97224813Skarels 	sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
97340689Skarels 	if ((rt = ipforward_rt.ro_rt) == 0 ||
97424813Skarels 	    ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
97524813Skarels 		if (ipforward_rt.ro_rt) {
97624813Skarels 			RTFREE(ipforward_rt.ro_rt);
97724813Skarels 			ipforward_rt.ro_rt = 0;
97824813Skarels 		}
97924813Skarels 		sin->sin_family = AF_INET;
98037319Skarels 		sin->sin_len = sizeof(*sin);
98124813Skarels 		sin->sin_addr = ip->ip_dst;
98224813Skarels 
98324813Skarels 		rtalloc(&ipforward_rt);
98440689Skarels 		if (ipforward_rt.ro_rt == 0) {
98540689Skarels 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest);
98640689Skarels 			return;
98740689Skarels 		}
98840689Skarels 		rt = ipforward_rt.ro_rt;
98924813Skarels 	}
99040689Skarels 
99124813Skarels 	/*
99240689Skarels 	 * Save at most 64 bytes of the packet in case
99340689Skarels 	 * we need to generate an ICMP message to the src.
99440689Skarels 	 */
99540689Skarels 	mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
99640689Skarels 
99740689Skarels #ifdef GATEWAY
99840689Skarels 	ip_ifmatrix[rt->rt_ifp->if_index +
99940689Skarels 	     if_index * m->m_pkthdr.rcvif->if_index]++;
100040689Skarels #endif
100140689Skarels 	/*
100224813Skarels 	 * If forwarding packet using same interface that it came in on,
100324813Skarels 	 * perhaps should send a redirect to sender to shortcut a hop.
100424813Skarels 	 * Only send redirect if source is sending directly to us,
100524813Skarels 	 * and if packet was not source routed (or has any options).
100630447Skarels 	 * Also, don't send redirect if forwarding using a default route
100740689Skarels 	 * or a route modified by a redirect.
100824813Skarels 	 */
100930447Skarels #define	satosin(sa)	((struct sockaddr_in *)(sa))
101040689Skarels 	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
101140689Skarels 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
101240689Skarels 	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
101340689Skarels 	    ipsendredirects && !srcrt) {
101452554Ssklower #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
101524813Skarels 		u_long src = ntohl(ip->ip_src.s_addr);
101624813Skarels 		u_long dst = ntohl(ip->ip_dst.s_addr);
101724813Skarels 
101852554Ssklower 		if (RTA(rt) &&
101952554Ssklower 		    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
102040689Skarels 		    if (rt->rt_flags & RTF_GATEWAY)
102140689Skarels 			dest = satosin(rt->rt_gateway)->sin_addr;
102224813Skarels 		    else
102324813Skarels 			dest = ip->ip_dst;
102424813Skarels 		    /*
102524813Skarels 		     * If the destination is reached by a route to host,
102627145Skarels 		     * is on a subnet of a local net, or is directly
102727145Skarels 		     * on the attached net (!), use host redirect.
102824813Skarels 		     * (We may be the correct first hop for other subnets.)
102924813Skarels 		     */
103024813Skarels 		    type = ICMP_REDIRECT;
103140689Skarels 		    if ((rt->rt_flags & RTF_HOST) ||
103240689Skarels 		        (rt->rt_flags & RTF_GATEWAY) == 0)
103340689Skarels 			    code = ICMP_REDIRECT_HOST;
103440689Skarels 		    else if (RTA(rt)->ia_subnetmask != RTA(rt)->ia_netmask &&
103540689Skarels 		        (dst & RTA(rt)->ia_netmask) ==  RTA(rt)->ia_net)
103640689Skarels 			    code = ICMP_REDIRECT_HOST;
103740689Skarels 		    else
103840689Skarels 			    code = ICMP_REDIRECT_NET;
103949042Ssklower #ifdef DIAGNOSTIC
104024813Skarels 		    if (ipprintfs)
104140689Skarels 		        printf("redirect (%d) to %x\n", code, dest.s_addr);
104240689Skarels #endif
104324813Skarels 		}
104424813Skarels 	}
104524813Skarels 
104637319Skarels 	error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING);
104724813Skarels 	if (error)
104824813Skarels 		ipstat.ips_cantforward++;
104924813Skarels 	else {
105021117Skarels 		ipstat.ips_forward++;
105140689Skarels 		if (type)
105240689Skarels 			ipstat.ips_redirectsent++;
105340689Skarels 		else {
105440689Skarels 			if (mcopy)
105540689Skarels 				m_freem(mcopy);
105640689Skarels 			return;
105740689Skarels 		}
10586609Ssam 	}
105911540Ssam 	if (mcopy == NULL)
106011540Ssam 		return;
10616609Ssam 	switch (error) {
10626609Ssam 
106324813Skarels 	case 0:				/* forwarded, but need redirect */
106440689Skarels 		/* type, code set above */
106524813Skarels 		break;
106624813Skarels 
106740689Skarels 	case ENETUNREACH:		/* shouldn't happen, checked above */
106840689Skarels 	case EHOSTUNREACH:
10696609Ssam 	case ENETDOWN:
107040689Skarels 	case EHOSTDOWN:
107140689Skarels 	default:
107240689Skarels 		type = ICMP_UNREACH;
107340689Skarels 		code = ICMP_UNREACH_HOST;
10746609Ssam 		break;
10756609Ssam 
10766609Ssam 	case EMSGSIZE:
107740689Skarels 		type = ICMP_UNREACH;
10786583Ssam 		code = ICMP_UNREACH_NEEDFRAG;
107939185Ssklower 		ipstat.ips_cantfrag++;
10806609Ssam 		break;
10816609Ssam 
10826609Ssam 	case ENOBUFS:
10836609Ssam 		type = ICMP_SOURCEQUENCH;
108437319Skarels 		code = 0;
10856609Ssam 		break;
10866609Ssam 	}
108738795Skarels 	icmp_error(mcopy, type, code, dest);
10886583Ssam }
1089