xref: /csrg-svn/sys/netinet/ip_input.c (revision 58998)
123184Smckusick /*
257968Sandrew  * Copyright (c) 1982, 1986, 1988, 1993 Regents of the University of California.
332787Sbostic  * All rights reserved.
423184Smckusick  *
544480Sbostic  * %sccs.include.redist.c%
632787Sbostic  *
7*58998Ssklower  *	@(#)ip_input.c	7.26 (Berkeley) 04/07/93
823184Smckusick  */
94571Swnj 
1056531Sbostic #include <sys/param.h>
1156531Sbostic #include <sys/systm.h>
1256531Sbostic #include <sys/malloc.h>
1356531Sbostic #include <sys/mbuf.h>
1456531Sbostic #include <sys/domain.h>
1556531Sbostic #include <sys/protosw.h>
1656531Sbostic #include <sys/socket.h>
1756531Sbostic #include <sys/errno.h>
1856531Sbostic #include <sys/time.h>
1956531Sbostic #include <sys/kernel.h>
208695Sroot 
2156531Sbostic #include <net/if.h>
2256531Sbostic #include <net/route.h>
2310892Ssam 
2456531Sbostic #include <netinet/in.h>
2556531Sbostic #include <netinet/in_systm.h>
2656531Sbostic #include <netinet/ip.h>
2756531Sbostic #include <netinet/in_pcb.h>
2856531Sbostic #include <netinet/in_var.h>
2956531Sbostic #include <netinet/ip_var.h>
3056531Sbostic #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 */
53*58998Ssklower struct	ifqueue ipintrq;
544898Swnj 
554801Swnj /*
5624813Skarels  * We need to save the IP options in case a protocol wants to respond
5724813Skarels  * to an incoming packet over the same route if the packet got here
5824813Skarels  * using IP source routing.  This allows connection establishment and
5924813Skarels  * maintenance when the remote end is on a network that is not known
6024813Skarels  * to us.
6124813Skarels  */
6224813Skarels int	ip_nhops = 0;
6324813Skarels static	struct ip_srcrt {
6436814Skarels 	struct	in_addr dst;			/* final destination */
6524813Skarels 	char	nop;				/* one NOP to align */
6624813Skarels 	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
6736814Skarels 	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
6824813Skarels } ip_srcrt;
6924813Skarels 
7040689Skarels #ifdef GATEWAY
7140689Skarels extern	int if_index;
7240689Skarels u_long	*ip_ifmatrix;
7340689Skarels #endif
7440689Skarels 
7524813Skarels /*
765172Swnj  * IP initialization: fill in IP protocol switch table.
775161Swnj  * All protocols not implemented in kernel go to raw IP protocol handler.
784801Swnj  */
794801Swnj ip_init()
804801Swnj {
814898Swnj 	register struct protosw *pr;
824898Swnj 	register int i;
834495Swnj 
8424813Skarels 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
854898Swnj 	if (pr == 0)
864898Swnj 		panic("ip_init");
874898Swnj 	for (i = 0; i < IPPROTO_MAX; i++)
889030Sroot 		ip_protox[i] = pr - inetsw;
899030Sroot 	for (pr = inetdomain.dom_protosw;
9017551Skarels 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
9116990Skarels 		if (pr->pr_domain->dom_family == PF_INET &&
924898Swnj 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
939030Sroot 			ip_protox[pr->pr_protocol] = pr - inetsw;
944801Swnj 	ipq.next = ipq.prev = &ipq;
958172Sroot 	ip_id = time.tv_sec & 0xffff;
966210Swnj 	ipintrq.ifq_maxlen = ipqmaxlen;
9740689Skarels #ifdef GATEWAY
9840689Skarels 	i = (if_index + 1) * (if_index + 1) * sizeof (u_long);
9950425Smckusick 	ip_ifmatrix = (u_long *) malloc(i, M_RTABLE, M_WAITOK);
10050425Smckusick 	bzero((char *)ip_ifmatrix, i);
10140689Skarels #endif
1024801Swnj }
1034801Swnj 
1044640Swnj struct	ip *ip_reass();
10537319Skarels struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
10624813Skarels struct	route ipforward_rt;
1074640Swnj 
1084640Swnj /*
1094640Swnj  * Ip input routine.  Checksum and byte swap header.  If fragmented
11040689Skarels  * try to reassemble.  Process options.  Pass to next level.
1114640Swnj  */
1125084Swnj ipintr()
1134495Swnj {
1144923Swnj 	register struct ip *ip;
1155084Swnj 	register struct mbuf *m;
1164495Swnj 	register struct ipq *fp;
11718376Skarels 	register struct in_ifaddr *ia;
1185084Swnj 	int hlen, s;
1194495Swnj 
1205084Swnj next:
1214640Swnj 	/*
1225084Swnj 	 * Get next datagram off input queue and get IP header
1235084Swnj 	 * in first mbuf.
1244640Swnj 	 */
1255084Swnj 	s = splimp();
12637319Skarels 	IF_DEQUEUE(&ipintrq, m);
1275084Swnj 	splx(s);
1285218Swnj 	if (m == 0)
1295084Swnj 		return;
13044967Skarels #ifdef	DIAGNOSTIC
13144967Skarels 	if ((m->m_flags & M_PKTHDR) == 0)
13244967Skarels 		panic("ipintr no HDR");
13344967Skarels #endif
13426001Skarels 	/*
13526001Skarels 	 * If no IP addresses have been set yet but the interfaces
13626001Skarels 	 * are receiving, can't do anything with incoming packets yet.
13726001Skarels 	 */
13826001Skarels 	if (in_ifaddr == NULL)
13926001Skarels 		goto bad;
14025920Skarels 	ipstat.ips_total++;
14140689Skarels 	if (m->m_len < sizeof (struct ip) &&
14211232Ssam 	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
14311232Ssam 		ipstat.ips_toosmall++;
14411232Ssam 		goto next;
14511232Ssam 	}
1464640Swnj 	ip = mtod(m, struct ip *);
14757968Sandrew 	if (ip->ip_v != IPVERSION) {
14857968Sandrew 		ipstat.ips_badvers++;
14957968Sandrew 		goto bad;
15057968Sandrew 	}
15118376Skarels 	hlen = ip->ip_hl << 2;
15224813Skarels 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
15318376Skarels 		ipstat.ips_badhlen++;
15421117Skarels 		goto bad;
15518376Skarels 	}
15618376Skarels 	if (hlen > m->m_len) {
15711232Ssam 		if ((m = m_pullup(m, hlen)) == 0) {
15811232Ssam 			ipstat.ips_badhlen++;
15911232Ssam 			goto next;
16011232Ssam 		}
1615161Swnj 		ip = mtod(m, struct ip *);
1625161Swnj 	}
16337319Skarels 	if (ip->ip_sum = in_cksum(m, hlen)) {
16437319Skarels 		ipstat.ips_badsum++;
16537319Skarels 		goto bad;
16637319Skarels 	}
1674951Swnj 
1684951Swnj 	/*
1694951Swnj 	 * Convert fields to host representation.
1704951Swnj 	 */
17140689Skarels 	NTOHS(ip->ip_len);
17211232Ssam 	if (ip->ip_len < hlen) {
17311232Ssam 		ipstat.ips_badlen++;
17411232Ssam 		goto bad;
17511232Ssam 	}
17640689Skarels 	NTOHS(ip->ip_id);
17740689Skarels 	NTOHS(ip->ip_off);
1784495Swnj 
1794543Swnj 	/*
1804640Swnj 	 * Check that the amount of data in the buffers
1814640Swnj 	 * is as at least much as the IP header would have us expect.
1824640Swnj 	 * Trim mbufs if longer than we expect.
1834640Swnj 	 * Drop packet if shorter than we expect.
1844543Swnj 	 */
18537319Skarels 	if (m->m_pkthdr.len < ip->ip_len) {
18637319Skarels 		ipstat.ips_tooshort++;
18737319Skarels 		goto bad;
1886088Sroot 	}
18937319Skarels 	if (m->m_pkthdr.len > ip->ip_len) {
19037319Skarels 		if (m->m_len == m->m_pkthdr.len) {
19137319Skarels 			m->m_len = ip->ip_len;
19237319Skarels 			m->m_pkthdr.len = ip->ip_len;
19337319Skarels 		} else
19437319Skarels 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
1954495Swnj 	}
1964495Swnj 
1974640Swnj 	/*
1984640Swnj 	 * Process options and, if not destined for us,
1996583Ssam 	 * ship it on.  ip_dooptions returns 1 when an
2006583Ssam 	 * error was detected (causing an icmp message
20121117Skarels 	 * to be sent and the original packet to be freed).
2024640Swnj 	 */
20324813Skarels 	ip_nhops = 0;		/* for source routed packets */
20437319Skarels 	if (hlen > sizeof (struct ip) && ip_dooptions(m))
2056583Ssam 		goto next;
2066210Swnj 
2076338Ssam 	/*
20818376Skarels 	 * Check our list of addresses, to see if the packet is for us.
2096338Ssam 	 */
21018376Skarels 	for (ia = in_ifaddr; ia; ia = ia->ia_next) {
21118376Skarels #define	satosin(sa)	((struct sockaddr_in *)(sa))
2126338Ssam 
21318376Skarels 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
21424813Skarels 			goto ours;
21525195Skarels 		if (
21625195Skarels #ifdef	DIRECTED_BROADCAST
21737319Skarels 		    ia->ia_ifp == m->m_pkthdr.rcvif &&
21825195Skarels #endif
21925195Skarels 		    (ia->ia_ifp->if_flags & IFF_BROADCAST)) {
22026247Skarels 			u_long t;
22125195Skarels 
22225195Skarels 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
22325195Skarels 			    ip->ip_dst.s_addr)
22425195Skarels 				goto ours;
22525195Skarels 			if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
22625195Skarels 				goto ours;
22725195Skarels 			/*
22825195Skarels 			 * Look for all-0's host part (old broadcast addr),
22925195Skarels 			 * either for subnet or net.
23025195Skarels 			 */
23126247Skarels 			t = ntohl(ip->ip_dst.s_addr);
23226247Skarels 			if (t == ia->ia_subnet)
23325195Skarels 				goto ours;
23426247Skarels 			if (t == ia->ia_net)
23525195Skarels 				goto ours;
23625195Skarels 		}
2376338Ssam 	}
23854716Ssklower 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
23954716Ssklower 		struct in_multi *inm;
24054716Ssklower #ifdef MROUTING
24154716Ssklower 		extern struct socket *ip_mrouter;
24254716Ssklower 
24354716Ssklower 		if (ip_mrouter) {
24454716Ssklower 			/*
24554716Ssklower 			 * If we are acting as a multicast router, all
24654716Ssklower 			 * incoming multicast packets are passed to the
24754716Ssklower 			 * kernel-level multicast forwarding function.
24854716Ssklower 			 * The packet is returned (relatively) intact; if
24954716Ssklower 			 * ip_mforward() returns a non-zero value, the packet
25054716Ssklower 			 * must be discarded, else it may be accepted below.
25154716Ssklower 			 *
25254716Ssklower 			 * (The IP ident field is put in the same byte order
25354716Ssklower 			 * as expected when ip_mforward() is called from
25454716Ssklower 			 * ip_output().)
25554716Ssklower 			 */
25654716Ssklower 			ip->ip_id = htons(ip->ip_id);
25754716Ssklower 			if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
25857433Sandrew 				ipstat.ips_cantforward++;
25954716Ssklower 				m_freem(m);
26054716Ssklower 				goto next;
26154716Ssklower 			}
26254716Ssklower 			ip->ip_id = ntohs(ip->ip_id);
26354716Ssklower 
26454716Ssklower 			/*
26554716Ssklower 			 * The process-level routing demon needs to receive
26654716Ssklower 			 * all multicast IGMP packets, whether or not this
26754716Ssklower 			 * host belongs to their destination groups.
26854716Ssklower 			 */
26954716Ssklower 			if (ip->ip_p == IPPROTO_IGMP)
27054716Ssklower 				goto ours;
27157433Sandrew 			ipstat.ips_forward++;
27254716Ssklower 		}
27354716Ssklower #endif
27454716Ssklower 		/*
27554716Ssklower 		 * See if we belong to the destination multicast group on the
27654716Ssklower 		 * arrival interface.
27754716Ssklower 		 */
27854716Ssklower 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
27954716Ssklower 		if (inm == NULL) {
28057433Sandrew 			ipstat.ips_cantforward++;
28154716Ssklower 			m_freem(m);
28254716Ssklower 			goto next;
28354716Ssklower 		}
28454716Ssklower 		goto ours;
28554716Ssklower 	}
28624813Skarels 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
28724813Skarels 		goto ours;
28824813Skarels 	if (ip->ip_dst.s_addr == INADDR_ANY)
28924813Skarels 		goto ours;
2904495Swnj 
2914640Swnj 	/*
29224813Skarels 	 * Not for us; forward if possible and desirable.
29324813Skarels 	 */
29440689Skarels 	if (ipforwarding == 0) {
29536814Skarels 		ipstat.ips_cantforward++;
29636814Skarels 		m_freem(m);
29736814Skarels 	} else
29840689Skarels 		ip_forward(m, 0);
29924813Skarels 	goto next;
30024813Skarels 
30124813Skarels ours:
30224813Skarels 	/*
30333743Skarels 	 * If offset or IP_MF are set, must reassemble.
30433743Skarels 	 * Otherwise, nothing need be done.
30533743Skarels 	 * (We could look in the reassembly queue to see
30633743Skarels 	 * if the packet was previously fragmented,
30733743Skarels 	 * but it's not worth the time; just let them time out.)
3084640Swnj 	 */
30933743Skarels 	if (ip->ip_off &~ IP_DF) {
31040689Skarels 		if (m->m_flags & M_EXT) {		/* XXX */
31140689Skarels 			if ((m = m_pullup(m, sizeof (struct ip))) == 0) {
31240689Skarels 				ipstat.ips_toosmall++;
31340689Skarels 				goto next;
31440689Skarels 			}
31540689Skarels 			ip = mtod(m, struct ip *);
31640689Skarels 		}
31733743Skarels 		/*
31833743Skarels 		 * Look for queue of fragments
31933743Skarels 		 * of this datagram.
32033743Skarels 		 */
32133743Skarels 		for (fp = ipq.next; fp != &ipq; fp = fp->next)
32233743Skarels 			if (ip->ip_id == fp->ipq_id &&
32333743Skarels 			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
32433743Skarels 			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
32533743Skarels 			    ip->ip_p == fp->ipq_p)
32633743Skarels 				goto found;
32733743Skarels 		fp = 0;
3284640Swnj found:
3294495Swnj 
33033743Skarels 		/*
33133743Skarels 		 * Adjust ip_len to not reflect header,
33233743Skarels 		 * set ip_mff if more fragments are expected,
33333743Skarels 		 * convert offset of this to bytes.
33433743Skarels 		 */
33533743Skarels 		ip->ip_len -= hlen;
33657968Sandrew 		((struct ipasfrag *)ip)->ipf_mff &= ~1;
33733743Skarels 		if (ip->ip_off & IP_MF)
33857968Sandrew 			((struct ipasfrag *)ip)->ipf_mff |= 1;
33933743Skarels 		ip->ip_off <<= 3;
3404495Swnj 
34133743Skarels 		/*
34233743Skarels 		 * If datagram marked as having more fragments
34333743Skarels 		 * or if this is not the first fragment,
34433743Skarels 		 * attempt reassembly; if it succeeds, proceed.
34533743Skarels 		 */
34657968Sandrew 		if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) {
34733743Skarels 			ipstat.ips_fragments++;
34833743Skarels 			ip = ip_reass((struct ipasfrag *)ip, fp);
34933743Skarels 			if (ip == 0)
35033743Skarels 				goto next;
35157433Sandrew 			ipstat.ips_reassembled++;
35233743Skarels 			m = dtom(ip);
35333743Skarels 		} else
35433743Skarels 			if (fp)
35533743Skarels 				ip_freef(fp);
3564640Swnj 	} else
35733743Skarels 		ip->ip_len -= hlen;
3584951Swnj 
3594951Swnj 	/*
3604951Swnj 	 * Switch out to protocol's input routine.
3614951Swnj 	 */
36239185Ssklower 	ipstat.ips_delivered++;
36337319Skarels 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
3645084Swnj 	goto next;
3654951Swnj bad:
3664951Swnj 	m_freem(m);
3675084Swnj 	goto next;
3684640Swnj }
3694495Swnj 
3704640Swnj /*
3714640Swnj  * Take incoming datagram fragment and try to
3724951Swnj  * reassemble it into whole datagram.  If a chain for
3734640Swnj  * reassembly of this datagram already exists, then it
3744640Swnj  * is given as fp; otherwise have to make a chain.
3754640Swnj  */
3764640Swnj struct ip *
3774640Swnj ip_reass(ip, fp)
3784898Swnj 	register struct ipasfrag *ip;
3794640Swnj 	register struct ipq *fp;
3804640Swnj {
3814640Swnj 	register struct mbuf *m = dtom(ip);
3824898Swnj 	register struct ipasfrag *q;
3834640Swnj 	struct mbuf *t;
3844640Swnj 	int hlen = ip->ip_hl << 2;
3854640Swnj 	int i, next;
3864543Swnj 
3874640Swnj 	/*
3884640Swnj 	 * Presence of header sizes in mbufs
3894640Swnj 	 * would confuse code below.
3904640Swnj 	 */
39137319Skarels 	m->m_data += hlen;
3924640Swnj 	m->m_len -= hlen;
3934495Swnj 
3944640Swnj 	/*
3954640Swnj 	 * If first fragment to arrive, create a reassembly queue.
3964640Swnj 	 */
3974640Swnj 	if (fp == 0) {
39831201Skarels 		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
3994640Swnj 			goto dropfrag;
4004640Swnj 		fp = mtod(t, struct ipq *);
4014640Swnj 		insque(fp, &ipq);
4024640Swnj 		fp->ipq_ttl = IPFRAGTTL;
4034640Swnj 		fp->ipq_p = ip->ip_p;
4044640Swnj 		fp->ipq_id = ip->ip_id;
4054898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
4064898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
4074898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
4085161Swnj 		q = (struct ipasfrag *)fp;
4095161Swnj 		goto insert;
4104640Swnj 	}
4114495Swnj 
4124640Swnj 	/*
4134640Swnj 	 * Find a segment which begins after this one does.
4144640Swnj 	 */
4154898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
4164640Swnj 		if (q->ip_off > ip->ip_off)
4174640Swnj 			break;
4184495Swnj 
4194640Swnj 	/*
4204640Swnj 	 * If there is a preceding segment, it may provide some of
4214640Swnj 	 * our data already.  If so, drop the data from the incoming
4224640Swnj 	 * segment.  If it provides all of our data, drop us.
4234640Swnj 	 */
4244898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
4254898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
4264640Swnj 		if (i > 0) {
4274640Swnj 			if (i >= ip->ip_len)
4284640Swnj 				goto dropfrag;
4294640Swnj 			m_adj(dtom(ip), i);
4304640Swnj 			ip->ip_off += i;
4314640Swnj 			ip->ip_len -= i;
4324640Swnj 		}
4334640Swnj 	}
4344543Swnj 
4354640Swnj 	/*
4364640Swnj 	 * While we overlap succeeding segments trim them or,
4374640Swnj 	 * if they are completely covered, dequeue them.
4384640Swnj 	 */
4394898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
4404640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
4414640Swnj 		if (i < q->ip_len) {
4424640Swnj 			q->ip_len -= i;
4436256Sroot 			q->ip_off += i;
4444640Swnj 			m_adj(dtom(q), i);
4454640Swnj 			break;
4464495Swnj 		}
4474898Swnj 		q = q->ipf_next;
4484898Swnj 		m_freem(dtom(q->ipf_prev));
4494898Swnj 		ip_deq(q->ipf_prev);
4504543Swnj 	}
4514495Swnj 
4525161Swnj insert:
4534640Swnj 	/*
4544640Swnj 	 * Stick new segment in its place;
4554640Swnj 	 * check for complete reassembly.
4564640Swnj 	 */
4574898Swnj 	ip_enq(ip, q->ipf_prev);
4584640Swnj 	next = 0;
4594898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
4604640Swnj 		if (q->ip_off != next)
4614640Swnj 			return (0);
4624640Swnj 		next += q->ip_len;
4634640Swnj 	}
46457968Sandrew 	if (q->ipf_prev->ipf_mff & 1)
4654640Swnj 		return (0);
4664495Swnj 
4674640Swnj 	/*
4684640Swnj 	 * Reassembly is complete; concatenate fragments.
4694640Swnj 	 */
4704640Swnj 	q = fp->ipq_next;
4714640Swnj 	m = dtom(q);
4724640Swnj 	t = m->m_next;
4734640Swnj 	m->m_next = 0;
4744640Swnj 	m_cat(m, t);
4756298Swnj 	q = q->ipf_next;
4766298Swnj 	while (q != (struct ipasfrag *)fp) {
4776298Swnj 		t = dtom(q);
4786298Swnj 		q = q->ipf_next;
4796298Swnj 		m_cat(m, t);
4806298Swnj 	}
4814495Swnj 
4824640Swnj 	/*
4834640Swnj 	 * Create header for new ip packet by
4844640Swnj 	 * modifying header of first packet;
4854640Swnj 	 * dequeue and discard fragment reassembly header.
4864640Swnj 	 * Make header visible.
4874640Swnj 	 */
4884640Swnj 	ip = fp->ipq_next;
4894640Swnj 	ip->ip_len = next;
49057968Sandrew 	ip->ipf_mff &= ~1;
4914898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
4924898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
4934640Swnj 	remque(fp);
4944907Swnj 	(void) m_free(dtom(fp));
4954640Swnj 	m = dtom(ip);
49624813Skarels 	m->m_len += (ip->ip_hl << 2);
49737319Skarels 	m->m_data -= (ip->ip_hl << 2);
49849042Ssklower 	/* some debugging cruft by sklower, below, will go away soon */
49949042Ssklower 	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
50049042Ssklower 		register int plen = 0;
50149042Ssklower 		for (t = m; m; m = m->m_next)
50249042Ssklower 			plen += m->m_len;
50349042Ssklower 		t->m_pkthdr.len = plen;
50449042Ssklower 	}
5054898Swnj 	return ((struct ip *)ip);
5064495Swnj 
5074640Swnj dropfrag:
50824813Skarels 	ipstat.ips_fragdropped++;
5094640Swnj 	m_freem(m);
5104640Swnj 	return (0);
5114495Swnj }
5124495Swnj 
5134640Swnj /*
5144640Swnj  * Free a fragment reassembly header and all
5154640Swnj  * associated datagrams.
5164640Swnj  */
5174640Swnj ip_freef(fp)
5184640Swnj 	struct ipq *fp;
5194495Swnj {
52010735Ssam 	register struct ipasfrag *q, *p;
5214495Swnj 
52210735Ssam 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {
52310735Ssam 		p = q->ipf_next;
52410735Ssam 		ip_deq(q);
5254640Swnj 		m_freem(dtom(q));
52610735Ssam 	}
52710735Ssam 	remque(fp);
52810735Ssam 	(void) m_free(dtom(fp));
5294495Swnj }
5304495Swnj 
5314640Swnj /*
5324640Swnj  * Put an ip fragment on a reassembly chain.
5334640Swnj  * Like insque, but pointers in middle of structure.
5344640Swnj  */
5354640Swnj ip_enq(p, prev)
5364898Swnj 	register struct ipasfrag *p, *prev;
5374495Swnj {
5384951Swnj 
5394898Swnj 	p->ipf_prev = prev;
5404898Swnj 	p->ipf_next = prev->ipf_next;
5414898Swnj 	prev->ipf_next->ipf_prev = p;
5424898Swnj 	prev->ipf_next = p;
5434495Swnj }
5444495Swnj 
5454640Swnj /*
5464640Swnj  * To ip_enq as remque is to insque.
5474640Swnj  */
5484640Swnj ip_deq(p)
5494898Swnj 	register struct ipasfrag *p;
5504640Swnj {
5514951Swnj 
5524898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
5534898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
5544495Swnj }
5554495Swnj 
5564640Swnj /*
5574640Swnj  * IP timer processing;
5584640Swnj  * if a timer expires on a reassembly
5594640Swnj  * queue, discard it.
5604640Swnj  */
5614801Swnj ip_slowtimo()
5624495Swnj {
5634495Swnj 	register struct ipq *fp;
5644640Swnj 	int s = splnet();
5654951Swnj 
5665243Sroot 	fp = ipq.next;
5675243Sroot 	if (fp == 0) {
5685243Sroot 		splx(s);
5695243Sroot 		return;
5705243Sroot 	}
57110735Ssam 	while (fp != &ipq) {
57210735Ssam 		--fp->ipq_ttl;
57310735Ssam 		fp = fp->next;
57424813Skarels 		if (fp->prev->ipq_ttl == 0) {
57524813Skarels 			ipstat.ips_fragtimeout++;
57610735Ssam 			ip_freef(fp->prev);
57724813Skarels 		}
57810735Ssam 	}
5794640Swnj 	splx(s);
5804495Swnj }
5814495Swnj 
5824951Swnj /*
5834951Swnj  * Drain off all datagram fragments.
5844951Swnj  */
5854801Swnj ip_drain()
5864801Swnj {
5874801Swnj 
58824813Skarels 	while (ipq.next != &ipq) {
58924813Skarels 		ipstat.ips_fragdropped++;
59010735Ssam 		ip_freef(ipq.next);
59124813Skarels 	}
5924801Swnj }
5934923Swnj 
59424813Skarels struct in_ifaddr *ip_rtaddr();
59524813Skarels 
5964640Swnj /*
5974640Swnj  * Do option processing on a datagram,
59840689Skarels  * possibly discarding it if bad options are encountered,
59940689Skarels  * or forwarding it if source-routed.
60040689Skarels  * Returns 1 if packet has been forwarded/freed,
60140689Skarels  * 0 if the packet should be processed further.
6024640Swnj  */
60337319Skarels ip_dooptions(m)
60436814Skarels 	struct mbuf *m;
6054495Swnj {
60636814Skarels 	register struct ip *ip = mtod(m, struct ip *);
6074640Swnj 	register u_char *cp;
60824813Skarels 	register struct ip_timestamp *ipt;
60924813Skarels 	register struct in_ifaddr *ia;
61036814Skarels 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
611*58998Ssklower 	struct in_addr *sin, dst;
61224813Skarels 	n_time ntime;
6134495Swnj 
614*58998Ssklower 	dst = ip->ip_dst;
6154640Swnj 	cp = (u_char *)(ip + 1);
6164640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
6174640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
61824813Skarels 		opt = cp[IPOPT_OPTVAL];
6194640Swnj 		if (opt == IPOPT_EOL)
6204640Swnj 			break;
6214640Swnj 		if (opt == IPOPT_NOP)
6224640Swnj 			optlen = 1;
62316392Ssam 		else {
62424813Skarels 			optlen = cp[IPOPT_OLEN];
62524813Skarels 			if (optlen <= 0 || optlen > cnt) {
62624813Skarels 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
62717551Skarels 				goto bad;
62824813Skarels 			}
62916392Ssam 		}
6304640Swnj 		switch (opt) {
6314495Swnj 
6324640Swnj 		default:
6334640Swnj 			break;
6344495Swnj 
6354951Swnj 		/*
6364951Swnj 		 * Source routing with record.
6374951Swnj 		 * Find interface with current destination address.
6384951Swnj 		 * If none on this machine then drop if strictly routed,
6394951Swnj 		 * or do nothing if loosely routed.
6404951Swnj 		 * Record interface address and bring up next address
6414951Swnj 		 * component.  If strictly routed make sure next
64240689Skarels 		 * address is on directly accessible net.
6434951Swnj 		 */
6444640Swnj 		case IPOPT_LSRR:
6457508Sroot 		case IPOPT_SSRR:
64624813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
64724813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
64824813Skarels 				goto bad;
64924813Skarels 			}
65024813Skarels 			ipaddr.sin_addr = ip->ip_dst;
65124813Skarels 			ia = (struct in_ifaddr *)
65224813Skarels 				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
65324813Skarels 			if (ia == 0) {
65424813Skarels 				if (opt == IPOPT_SSRR) {
65524813Skarels 					type = ICMP_UNREACH;
65624813Skarels 					code = ICMP_UNREACH_SRCFAIL;
6574951Swnj 					goto bad;
65824813Skarels 				}
65924813Skarels 				/*
66024813Skarels 				 * Loose routing, and not at next destination
66124813Skarels 				 * yet; nothing to do except forward.
66224813Skarels 				 */
6634951Swnj 				break;
6644640Swnj 			}
66524813Skarels 			off--;			/* 0 origin */
66624813Skarels 			if (off > optlen - sizeof(struct in_addr)) {
66724813Skarels 				/*
66824813Skarels 				 * End of source route.  Should be for us.
66924813Skarels 				 */
67024813Skarels 				save_rte(cp, ip->ip_src);
6714951Swnj 				break;
67224813Skarels 			}
67324813Skarels 			/*
67424813Skarels 			 * locate outgoing interface
67524813Skarels 			 */
67626384Skarels 			bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
67724813Skarels 			    sizeof(ipaddr.sin_addr));
67840689Skarels 			if (opt == IPOPT_SSRR) {
67940689Skarels #define	INA	struct in_ifaddr *
68040689Skarels #define	SA	struct sockaddr *
68140689Skarels 			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
682*58998Ssklower 				ia = (INA)ifa_ifwithnet((SA)&ipaddr);
68340689Skarels 			} else
68440689Skarels 				ia = ip_rtaddr(ipaddr.sin_addr);
68540689Skarels 			if (ia == 0) {
68624813Skarels 				type = ICMP_UNREACH;
68724813Skarels 				code = ICMP_UNREACH_SRCFAIL;
6884951Swnj 				goto bad;
68924813Skarels 			}
69024813Skarels 			ip->ip_dst = ipaddr.sin_addr;
69126384Skarels 			bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
69226384Skarels 			    (caddr_t)(cp + off), sizeof(struct in_addr));
69324813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
69457433Sandrew 			/*
69557433Sandrew 			 * Let ip_intr's mcast routing check handle mcast pkts
69657433Sandrew 			 */
69757433Sandrew 			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
6984640Swnj 			break;
6994495Swnj 
70024813Skarels 		case IPOPT_RR:
70124813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
70224813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
70324813Skarels 				goto bad;
70424813Skarels 			}
70524813Skarels 			/*
70624813Skarels 			 * If no space remains, ignore.
70724813Skarels 			 */
70824813Skarels 			off--;			/* 0 origin */
70924813Skarels 			if (off > optlen - sizeof(struct in_addr))
71024813Skarels 				break;
71131393Skarels 			bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
71224813Skarels 			    sizeof(ipaddr.sin_addr));
71324813Skarels 			/*
71437319Skarels 			 * locate outgoing interface; if we're the destination,
71537319Skarels 			 * use the incoming interface (should be same).
71624813Skarels 			 */
71740689Skarels 			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
71837319Skarels 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
71924813Skarels 				type = ICMP_UNREACH;
72032113Skarels 				code = ICMP_UNREACH_HOST;
72124813Skarels 				goto bad;
72224813Skarels 			}
72326384Skarels 			bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
72426384Skarels 			    (caddr_t)(cp + off), sizeof(struct in_addr));
72524813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
72624813Skarels 			break;
72724813Skarels 
7284640Swnj 		case IPOPT_TS:
7296583Ssam 			code = cp - (u_char *)ip;
7304801Swnj 			ipt = (struct ip_timestamp *)cp;
7314801Swnj 			if (ipt->ipt_len < 5)
7324640Swnj 				goto bad;
7334801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
7344801Swnj 				if (++ipt->ipt_oflw == 0)
7354640Swnj 					goto bad;
7364495Swnj 				break;
7374640Swnj 			}
73830925Skarels 			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
7394801Swnj 			switch (ipt->ipt_flg) {
7404495Swnj 
7414640Swnj 			case IPOPT_TS_TSONLY:
7424640Swnj 				break;
7434640Swnj 
7444640Swnj 			case IPOPT_TS_TSANDADDR:
74524813Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
74624813Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
7474640Swnj 					goto bad;
748*58998Ssklower 				ipaddr.sin_addr = dst;
749*58998Ssklower 				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
750*58998Ssklower 							    m->m_pkthdr.rcvif);
751*58998Ssklower 				if (ia == 0)
752*58998Ssklower 					continue;
75330925Skarels 				bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
75424813Skarels 				    (caddr_t)sin, sizeof(struct in_addr));
75530925Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
7564640Swnj 				break;
7574640Swnj 
7584640Swnj 			case IPOPT_TS_PRESPEC:
75930925Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
76030925Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
76130925Skarels 					goto bad;
76224813Skarels 				bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
76324813Skarels 				    sizeof(struct in_addr));
76440689Skarels 				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
7654951Swnj 					continue;
76624813Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
7674640Swnj 				break;
7684640Swnj 
7694495Swnj 			default:
7704640Swnj 				goto bad;
7714495Swnj 			}
77224813Skarels 			ntime = iptime();
77330925Skarels 			bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
77430925Skarels 			    sizeof(n_time));
77524813Skarels 			ipt->ipt_ptr += sizeof(n_time);
7764640Swnj 		}
7774495Swnj 	}
77836814Skarels 	if (forward) {
77940689Skarels 		ip_forward(m, 1);
78036814Skarels 		return (1);
78157433Sandrew 	}
78257433Sandrew 	return (0);
7834640Swnj bad:
78457433Sandrew 	ip->ip_len -= ip->ip_hl << 2;   /* XXX icmp_error adds in hdr length */
785*58998Ssklower 	icmp_error(m, type, code, 0, 0);
78657433Sandrew 	ipstat.ips_badoptions++;
7876583Ssam 	return (1);
7884495Swnj }
7894495Swnj 
7904640Swnj /*
79124813Skarels  * Given address of next destination (final or next hop),
79224813Skarels  * return internet address info of interface to be used to get there.
79324813Skarels  */
79424813Skarels struct in_ifaddr *
79524813Skarels ip_rtaddr(dst)
79624813Skarels 	 struct in_addr dst;
79724813Skarels {
79824813Skarels 	register struct sockaddr_in *sin;
79924813Skarels 
80024813Skarels 	sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
80124813Skarels 
80224813Skarels 	if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
80324813Skarels 		if (ipforward_rt.ro_rt) {
80424813Skarels 			RTFREE(ipforward_rt.ro_rt);
80524813Skarels 			ipforward_rt.ro_rt = 0;
80624813Skarels 		}
80724813Skarels 		sin->sin_family = AF_INET;
80837319Skarels 		sin->sin_len = sizeof(*sin);
80924813Skarels 		sin->sin_addr = dst;
81024813Skarels 
81124813Skarels 		rtalloc(&ipforward_rt);
81224813Skarels 	}
81324813Skarels 	if (ipforward_rt.ro_rt == 0)
81424813Skarels 		return ((struct in_ifaddr *)0);
81540689Skarels 	return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
81624813Skarels }
81724813Skarels 
81824813Skarels /*
81924813Skarels  * Save incoming source route for use in replies,
82024813Skarels  * to be picked up later by ip_srcroute if the receiver is interested.
82124813Skarels  */
82224813Skarels save_rte(option, dst)
82326384Skarels 	u_char *option;
82424813Skarels 	struct in_addr dst;
82524813Skarels {
82626384Skarels 	unsigned olen;
82724813Skarels 
82824813Skarels 	olen = option[IPOPT_OLEN];
82949042Ssklower #ifdef DIAGNOSTIC
83036814Skarels 	if (ipprintfs)
83136814Skarels 		printf("save_rte: olen %d\n", olen);
83240689Skarels #endif
83336814Skarels 	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
83424813Skarels 		return;
83526384Skarels 	bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
83624813Skarels 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
83736814Skarels 	ip_srcrt.dst = dst;
83824813Skarels }
83924813Skarels 
84024813Skarels /*
84124813Skarels  * Retrieve incoming source route for use in replies,
84224813Skarels  * in the same form used by setsockopt.
84324813Skarels  * The first hop is placed before the options, will be removed later.
84424813Skarels  */
84524813Skarels struct mbuf *
84624813Skarels ip_srcroute()
84724813Skarels {
84824813Skarels 	register struct in_addr *p, *q;
84924813Skarels 	register struct mbuf *m;
85024813Skarels 
85124813Skarels 	if (ip_nhops == 0)
85224813Skarels 		return ((struct mbuf *)0);
85331201Skarels 	m = m_get(M_DONTWAIT, MT_SOOPTS);
85431201Skarels 	if (m == 0)
85531201Skarels 		return ((struct mbuf *)0);
85624813Skarels 
85736814Skarels #define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
85836814Skarels 
85936814Skarels 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
86036814Skarels 	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
86136814Skarels 	    OPTSIZ;
86249042Ssklower #ifdef DIAGNOSTIC
86336814Skarels 	if (ipprintfs)
86436814Skarels 		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
86540689Skarels #endif
86636814Skarels 
86724813Skarels 	/*
86824813Skarels 	 * First save first hop for return route
86924813Skarels 	 */
87024813Skarels 	p = &ip_srcrt.route[ip_nhops - 1];
87124813Skarels 	*(mtod(m, struct in_addr *)) = *p--;
87249042Ssklower #ifdef DIAGNOSTIC
87336814Skarels 	if (ipprintfs)
87449882Sbostic 		printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr));
87540689Skarels #endif
87624813Skarels 
87724813Skarels 	/*
87824813Skarels 	 * Copy option fields and padding (nop) to mbuf.
87924813Skarels 	 */
88024813Skarels 	ip_srcrt.nop = IPOPT_NOP;
88136814Skarels 	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
88236814Skarels 	bcopy((caddr_t)&ip_srcrt.nop,
88336814Skarels 	    mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
88424813Skarels 	q = (struct in_addr *)(mtod(m, caddr_t) +
88536814Skarels 	    sizeof(struct in_addr) + OPTSIZ);
88636814Skarels #undef OPTSIZ
88724813Skarels 	/*
88824813Skarels 	 * Record return path as an IP source route,
88924813Skarels 	 * reversing the path (pointers are now aligned).
89024813Skarels 	 */
89136814Skarels 	while (p >= ip_srcrt.route) {
89249042Ssklower #ifdef DIAGNOSTIC
89336814Skarels 		if (ipprintfs)
89449882Sbostic 			printf(" %lx", ntohl(q->s_addr));
89540689Skarels #endif
89624813Skarels 		*q++ = *p--;
89736814Skarels 	}
89836814Skarels 	/*
89936814Skarels 	 * Last hop goes to final destination.
90036814Skarels 	 */
90136814Skarels 	*q = ip_srcrt.dst;
90249042Ssklower #ifdef DIAGNOSTIC
90336814Skarels 	if (ipprintfs)
90449882Sbostic 		printf(" %lx\n", ntohl(q->s_addr));
90540689Skarels #endif
90624813Skarels 	return (m);
90724813Skarels }
90824813Skarels 
90924813Skarels /*
9104951Swnj  * Strip out IP options, at higher
9114951Swnj  * level protocol in the kernel.
9124951Swnj  * Second argument is buffer to which options
9134951Swnj  * will be moved, and return value is their length.
91436814Skarels  * XXX should be deleted; last arg currently ignored.
9154640Swnj  */
91637319Skarels ip_stripoptions(m, mopt)
91737319Skarels 	register struct mbuf *m;
9185217Swnj 	struct mbuf *mopt;
9194495Swnj {
9204640Swnj 	register int i;
92137319Skarels 	struct ip *ip = mtod(m, struct ip *);
92224813Skarels 	register caddr_t opts;
9234640Swnj 	int olen;
9244640Swnj 
9254640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
92624813Skarels 	opts = (caddr_t)(ip + 1);
9274640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
92824813Skarels 	bcopy(opts  + olen, opts, (unsigned)i);
9295243Sroot 	m->m_len -= olen;
93037319Skarels 	if (m->m_flags & M_PKTHDR)
93137319Skarels 		m->m_pkthdr.len -= olen;
93224813Skarels 	ip->ip_hl = sizeof(struct ip) >> 2;
9334495Swnj }
9346583Ssam 
93514670Ssam u_char inetctlerrmap[PRC_NCMDS] = {
93624813Skarels 	0,		0,		0,		0,
93740689Skarels 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
93840689Skarels 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
93924813Skarels 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
94024813Skarels 	0,		0,		0,		0,
94124813Skarels 	ENOPROTOOPT
9426583Ssam };
9436583Ssam 
9446583Ssam /*
9456583Ssam  * Forward a packet.  If some error occurs return the sender
94618376Skarels  * an icmp packet.  Note we can't always generate a meaningful
94724813Skarels  * icmp message because icmp doesn't have a large enough repertoire
9486583Ssam  * of codes and types.
94926308Skarels  *
95040689Skarels  * If not forwarding, just drop the packet.  This could be confusing
95140689Skarels  * if ipforwarding was zero but some routing protocol was advancing
95240689Skarels  * us as a gateway to somewhere.  However, we must let the routing
95340689Skarels  * protocol deal with that.
95440689Skarels  *
95540689Skarels  * The srcrt parameter indicates whether the packet is being forwarded
95640689Skarels  * via a source route.
9576583Ssam  */
95840689Skarels ip_forward(m, srcrt)
95936814Skarels 	struct mbuf *m;
96040689Skarels 	int srcrt;
9616583Ssam {
96236814Skarels 	register struct ip *ip = mtod(m, struct ip *);
96324813Skarels 	register struct sockaddr_in *sin;
96440689Skarels 	register struct rtentry *rt;
96540689Skarels 	int error, type = 0, code;
96618376Skarels 	struct mbuf *mcopy;
96724813Skarels 	struct in_addr dest;
96857433Sandrew 	struct ifnet *destifp;
9696583Ssam 
97024813Skarels 	dest.s_addr = 0;
97149042Ssklower #ifdef DIAGNOSTIC
9726583Ssam 	if (ipprintfs)
9736583Ssam 		printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
9746583Ssam 			ip->ip_dst, ip->ip_ttl);
97540689Skarels #endif
97637319Skarels 	if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
97726308Skarels 		ipstat.ips_cantforward++;
97837319Skarels 		m_freem(m);
97926308Skarels 		return;
9806583Ssam 	}
98140689Skarels 	HTONS(ip->ip_id);
98231393Skarels 	if (ip->ip_ttl <= IPTTLDEC) {
98340689Skarels 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest);
98440689Skarels 		return;
9856583Ssam 	}
9866583Ssam 	ip->ip_ttl -= IPTTLDEC;
9876609Ssam 
98824813Skarels 	sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
98940689Skarels 	if ((rt = ipforward_rt.ro_rt) == 0 ||
99024813Skarels 	    ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
99124813Skarels 		if (ipforward_rt.ro_rt) {
99224813Skarels 			RTFREE(ipforward_rt.ro_rt);
99324813Skarels 			ipforward_rt.ro_rt = 0;
99424813Skarels 		}
99524813Skarels 		sin->sin_family = AF_INET;
99637319Skarels 		sin->sin_len = sizeof(*sin);
99724813Skarels 		sin->sin_addr = ip->ip_dst;
99824813Skarels 
99924813Skarels 		rtalloc(&ipforward_rt);
100040689Skarels 		if (ipforward_rt.ro_rt == 0) {
100140689Skarels 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest);
100240689Skarels 			return;
100340689Skarels 		}
100440689Skarels 		rt = ipforward_rt.ro_rt;
100524813Skarels 	}
100640689Skarels 
100724813Skarels 	/*
100840689Skarels 	 * Save at most 64 bytes of the packet in case
100940689Skarels 	 * we need to generate an ICMP message to the src.
101040689Skarels 	 */
101140689Skarels 	mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
101240689Skarels 
101340689Skarels #ifdef GATEWAY
101440689Skarels 	ip_ifmatrix[rt->rt_ifp->if_index +
101540689Skarels 	     if_index * m->m_pkthdr.rcvif->if_index]++;
101640689Skarels #endif
101740689Skarels 	/*
101824813Skarels 	 * If forwarding packet using same interface that it came in on,
101924813Skarels 	 * perhaps should send a redirect to sender to shortcut a hop.
102024813Skarels 	 * Only send redirect if source is sending directly to us,
102124813Skarels 	 * and if packet was not source routed (or has any options).
102230447Skarels 	 * Also, don't send redirect if forwarding using a default route
102340689Skarels 	 * or a route modified by a redirect.
102424813Skarels 	 */
102530447Skarels #define	satosin(sa)	((struct sockaddr_in *)(sa))
102640689Skarels 	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
102740689Skarels 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
102840689Skarels 	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
102940689Skarels 	    ipsendredirects && !srcrt) {
103052554Ssklower #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
103124813Skarels 		u_long src = ntohl(ip->ip_src.s_addr);
103224813Skarels 		u_long dst = ntohl(ip->ip_dst.s_addr);
103324813Skarels 
103452554Ssklower 		if (RTA(rt) &&
103552554Ssklower 		    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
103640689Skarels 		    if (rt->rt_flags & RTF_GATEWAY)
103740689Skarels 			dest = satosin(rt->rt_gateway)->sin_addr;
103824813Skarels 		    else
103924813Skarels 			dest = ip->ip_dst;
1040*58998Ssklower 		    /* Router requirements says to only send host redirects */
104124813Skarels 		    type = ICMP_REDIRECT;
1042*58998Ssklower 		    code = ICMP_REDIRECT_HOST;
104349042Ssklower #ifdef DIAGNOSTIC
104424813Skarels 		    if (ipprintfs)
104540689Skarels 		        printf("redirect (%d) to %x\n", code, dest.s_addr);
104640689Skarels #endif
104724813Skarels 		}
104824813Skarels 	}
104924813Skarels 
1050*58998Ssklower 	error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING
1051*58998Ssklower #ifdef DIRECTED_BROADCAST
1052*58998Ssklower 			    | IP_ALLOWBROADCAST
1053*58998Ssklower #endif
1054*58998Ssklower 						, 0);
105524813Skarels 	if (error)
105624813Skarels 		ipstat.ips_cantforward++;
105724813Skarels 	else {
105821117Skarels 		ipstat.ips_forward++;
105940689Skarels 		if (type)
106040689Skarels 			ipstat.ips_redirectsent++;
106140689Skarels 		else {
106240689Skarels 			if (mcopy)
106340689Skarels 				m_freem(mcopy);
106440689Skarels 			return;
106540689Skarels 		}
10666609Ssam 	}
106711540Ssam 	if (mcopy == NULL)
106811540Ssam 		return;
106957433Sandrew 	destifp = NULL;
107057433Sandrew 
10716609Ssam 	switch (error) {
10726609Ssam 
107324813Skarels 	case 0:				/* forwarded, but need redirect */
107440689Skarels 		/* type, code set above */
107524813Skarels 		break;
107624813Skarels 
107740689Skarels 	case ENETUNREACH:		/* shouldn't happen, checked above */
107840689Skarels 	case EHOSTUNREACH:
10796609Ssam 	case ENETDOWN:
108040689Skarels 	case EHOSTDOWN:
108140689Skarels 	default:
108240689Skarels 		type = ICMP_UNREACH;
108340689Skarels 		code = ICMP_UNREACH_HOST;
10846609Ssam 		break;
10856609Ssam 
10866609Ssam 	case EMSGSIZE:
108740689Skarels 		type = ICMP_UNREACH;
10886583Ssam 		code = ICMP_UNREACH_NEEDFRAG;
108957433Sandrew 		if (ipforward_rt.ro_rt)
109057433Sandrew 			destifp = ipforward_rt.ro_rt->rt_ifp;
109139185Ssklower 		ipstat.ips_cantfrag++;
10926609Ssam 		break;
10936609Ssam 
10946609Ssam 	case ENOBUFS:
10956609Ssam 		type = ICMP_SOURCEQUENCH;
109637319Skarels 		code = 0;
10976609Ssam 		break;
10986609Ssam 	}
109957433Sandrew 	icmp_error(mcopy, type, code, dest, destifp);
11006583Ssam }
1101