xref: /csrg-svn/sys/netinet/ip_input.c (revision 57433)
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*57433Sandrew  *	@(#)ip_input.c	7.24 (Berkeley) 01/08/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 */
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 	}
162*57433Sandrew 	if (ip->ip_v != IPVERSION) {
163*57433Sandrew 		goto bad;
164*57433Sandrew 	}
1654951Swnj 
1664951Swnj 	/*
1674951Swnj 	 * Convert fields to host representation.
1684951Swnj 	 */
16940689Skarels 	NTOHS(ip->ip_len);
17011232Ssam 	if (ip->ip_len < hlen) {
17111232Ssam 		ipstat.ips_badlen++;
17211232Ssam 		goto bad;
17311232Ssam 	}
17440689Skarels 	NTOHS(ip->ip_id);
17540689Skarels 	NTOHS(ip->ip_off);
1764495Swnj 
1774543Swnj 	/*
1784640Swnj 	 * Check that the amount of data in the buffers
1794640Swnj 	 * is as at least much as the IP header would have us expect.
1804640Swnj 	 * Trim mbufs if longer than we expect.
1814640Swnj 	 * Drop packet if shorter than we expect.
1824543Swnj 	 */
18337319Skarels 	if (m->m_pkthdr.len < ip->ip_len) {
18437319Skarels 		ipstat.ips_tooshort++;
18537319Skarels 		goto bad;
1866088Sroot 	}
18737319Skarels 	if (m->m_pkthdr.len > ip->ip_len) {
18837319Skarels 		if (m->m_len == m->m_pkthdr.len) {
18937319Skarels 			m->m_len = ip->ip_len;
19037319Skarels 			m->m_pkthdr.len = ip->ip_len;
19137319Skarels 		} else
19237319Skarels 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
1934495Swnj 	}
1944495Swnj 
1954640Swnj 	/*
1964640Swnj 	 * Process options and, if not destined for us,
1976583Ssam 	 * ship it on.  ip_dooptions returns 1 when an
1986583Ssam 	 * error was detected (causing an icmp message
19921117Skarels 	 * to be sent and the original packet to be freed).
2004640Swnj 	 */
20124813Skarels 	ip_nhops = 0;		/* for source routed packets */
20237319Skarels 	if (hlen > sizeof (struct ip) && ip_dooptions(m))
2036583Ssam 		goto next;
2046210Swnj 
2056338Ssam 	/*
20618376Skarels 	 * Check our list of addresses, to see if the packet is for us.
2076338Ssam 	 */
20818376Skarels 	for (ia = in_ifaddr; ia; ia = ia->ia_next) {
20918376Skarels #define	satosin(sa)	((struct sockaddr_in *)(sa))
2106338Ssam 
21118376Skarels 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
21224813Skarels 			goto ours;
21325195Skarels 		if (
21425195Skarels #ifdef	DIRECTED_BROADCAST
21537319Skarels 		    ia->ia_ifp == m->m_pkthdr.rcvif &&
21625195Skarels #endif
21725195Skarels 		    (ia->ia_ifp->if_flags & IFF_BROADCAST)) {
21826247Skarels 			u_long t;
21925195Skarels 
22025195Skarels 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
22125195Skarels 			    ip->ip_dst.s_addr)
22225195Skarels 				goto ours;
22325195Skarels 			if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
22425195Skarels 				goto ours;
22525195Skarels 			/*
22625195Skarels 			 * Look for all-0's host part (old broadcast addr),
22725195Skarels 			 * either for subnet or net.
22825195Skarels 			 */
22926247Skarels 			t = ntohl(ip->ip_dst.s_addr);
23026247Skarels 			if (t == ia->ia_subnet)
23125195Skarels 				goto ours;
23226247Skarels 			if (t == ia->ia_net)
23325195Skarels 				goto ours;
23425195Skarels 		}
2356338Ssam 	}
23654716Ssklower 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
23754716Ssklower 		struct in_multi *inm;
23854716Ssklower #ifdef MROUTING
23954716Ssklower 		extern struct socket *ip_mrouter;
24054716Ssklower 
24154716Ssklower 		if (ip_mrouter) {
24254716Ssklower 			/*
24354716Ssklower 			 * If we are acting as a multicast router, all
24454716Ssklower 			 * incoming multicast packets are passed to the
24554716Ssklower 			 * kernel-level multicast forwarding function.
24654716Ssklower 			 * The packet is returned (relatively) intact; if
24754716Ssklower 			 * ip_mforward() returns a non-zero value, the packet
24854716Ssklower 			 * must be discarded, else it may be accepted below.
24954716Ssklower 			 *
25054716Ssklower 			 * (The IP ident field is put in the same byte order
25154716Ssklower 			 * as expected when ip_mforward() is called from
25254716Ssklower 			 * ip_output().)
25354716Ssklower 			 */
25454716Ssklower 			ip->ip_id = htons(ip->ip_id);
25554716Ssklower 			if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
256*57433Sandrew 				ipstat.ips_cantforward++;
25754716Ssklower 				m_freem(m);
25854716Ssklower 				goto next;
25954716Ssklower 			}
26054716Ssklower 			ip->ip_id = ntohs(ip->ip_id);
26154716Ssklower 
26254716Ssklower 			/*
26354716Ssklower 			 * The process-level routing demon needs to receive
26454716Ssklower 			 * all multicast IGMP packets, whether or not this
26554716Ssklower 			 * host belongs to their destination groups.
26654716Ssklower 			 */
26754716Ssklower 			if (ip->ip_p == IPPROTO_IGMP)
26854716Ssklower 				goto ours;
269*57433Sandrew 			ipstat.ips_forward++;
27054716Ssklower 		}
27154716Ssklower #endif
27254716Ssklower 		/*
27354716Ssklower 		 * See if we belong to the destination multicast group on the
27454716Ssklower 		 * arrival interface.
27554716Ssklower 		 */
27654716Ssklower 		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
27754716Ssklower 		if (inm == NULL) {
278*57433Sandrew 			ipstat.ips_cantforward++;
27954716Ssklower 			m_freem(m);
28054716Ssklower 			goto next;
28154716Ssklower 		}
28254716Ssklower 		goto ours;
28354716Ssklower 	}
28424813Skarels 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
28524813Skarels 		goto ours;
28624813Skarels 	if (ip->ip_dst.s_addr == INADDR_ANY)
28724813Skarels 		goto ours;
2884495Swnj 
2894640Swnj 	/*
29024813Skarels 	 * Not for us; forward if possible and desirable.
29124813Skarels 	 */
29240689Skarels 	if (ipforwarding == 0) {
29336814Skarels 		ipstat.ips_cantforward++;
29436814Skarels 		m_freem(m);
29536814Skarels 	} else
29640689Skarels 		ip_forward(m, 0);
29724813Skarels 	goto next;
29824813Skarels 
29924813Skarels ours:
30024813Skarels 	/*
30133743Skarels 	 * If offset or IP_MF are set, must reassemble.
30233743Skarels 	 * Otherwise, nothing need be done.
30333743Skarels 	 * (We could look in the reassembly queue to see
30433743Skarels 	 * if the packet was previously fragmented,
30533743Skarels 	 * but it's not worth the time; just let them time out.)
3064640Swnj 	 */
30733743Skarels 	if (ip->ip_off &~ IP_DF) {
30840689Skarels 		if (m->m_flags & M_EXT) {		/* XXX */
30940689Skarels 			if ((m = m_pullup(m, sizeof (struct ip))) == 0) {
31040689Skarels 				ipstat.ips_toosmall++;
31140689Skarels 				goto next;
31240689Skarels 			}
31340689Skarels 			ip = mtod(m, struct ip *);
31440689Skarels 		}
31533743Skarels 		/*
31633743Skarels 		 * Look for queue of fragments
31733743Skarels 		 * of this datagram.
31833743Skarels 		 */
31933743Skarels 		for (fp = ipq.next; fp != &ipq; fp = fp->next)
32033743Skarels 			if (ip->ip_id == fp->ipq_id &&
32133743Skarels 			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
32233743Skarels 			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
32333743Skarels 			    ip->ip_p == fp->ipq_p)
32433743Skarels 				goto found;
32533743Skarels 		fp = 0;
3264640Swnj found:
3274495Swnj 
32833743Skarels 		/*
32933743Skarels 		 * Adjust ip_len to not reflect header,
33033743Skarels 		 * set ip_mff if more fragments are expected,
33133743Skarels 		 * convert offset of this to bytes.
33233743Skarels 		 */
33333743Skarels 		ip->ip_len -= hlen;
33433743Skarels 		((struct ipasfrag *)ip)->ipf_mff = 0;
33533743Skarels 		if (ip->ip_off & IP_MF)
33633743Skarels 			((struct ipasfrag *)ip)->ipf_mff = 1;
33733743Skarels 		ip->ip_off <<= 3;
3384495Swnj 
33933743Skarels 		/*
34033743Skarels 		 * If datagram marked as having more fragments
34133743Skarels 		 * or if this is not the first fragment,
34233743Skarels 		 * attempt reassembly; if it succeeds, proceed.
34333743Skarels 		 */
34433743Skarels 		if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
34533743Skarels 			ipstat.ips_fragments++;
34633743Skarels 			ip = ip_reass((struct ipasfrag *)ip, fp);
34733743Skarels 			if (ip == 0)
34833743Skarels 				goto next;
349*57433Sandrew 			ipstat.ips_reassembled++;
35033743Skarels 			m = dtom(ip);
35133743Skarels 		} else
35233743Skarels 			if (fp)
35333743Skarels 				ip_freef(fp);
3544640Swnj 	} else
35533743Skarels 		ip->ip_len -= hlen;
3564951Swnj 
3574951Swnj 	/*
3584951Swnj 	 * Switch out to protocol's input routine.
3594951Swnj 	 */
36039185Ssklower 	ipstat.ips_delivered++;
36137319Skarels 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
3625084Swnj 	goto next;
3634951Swnj bad:
3644951Swnj 	m_freem(m);
3655084Swnj 	goto next;
3664640Swnj }
3674495Swnj 
3684640Swnj /*
3694640Swnj  * Take incoming datagram fragment and try to
3704951Swnj  * reassemble it into whole datagram.  If a chain for
3714640Swnj  * reassembly of this datagram already exists, then it
3724640Swnj  * is given as fp; otherwise have to make a chain.
3734640Swnj  */
3744640Swnj struct ip *
3754640Swnj ip_reass(ip, fp)
3764898Swnj 	register struct ipasfrag *ip;
3774640Swnj 	register struct ipq *fp;
3784640Swnj {
3794640Swnj 	register struct mbuf *m = dtom(ip);
3804898Swnj 	register struct ipasfrag *q;
3814640Swnj 	struct mbuf *t;
3824640Swnj 	int hlen = ip->ip_hl << 2;
3834640Swnj 	int i, next;
3844543Swnj 
3854640Swnj 	/*
3864640Swnj 	 * Presence of header sizes in mbufs
3874640Swnj 	 * would confuse code below.
3884640Swnj 	 */
38937319Skarels 	m->m_data += hlen;
3904640Swnj 	m->m_len -= hlen;
3914495Swnj 
3924640Swnj 	/*
3934640Swnj 	 * If first fragment to arrive, create a reassembly queue.
3944640Swnj 	 */
3954640Swnj 	if (fp == 0) {
39631201Skarels 		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
3974640Swnj 			goto dropfrag;
3984640Swnj 		fp = mtod(t, struct ipq *);
3994640Swnj 		insque(fp, &ipq);
4004640Swnj 		fp->ipq_ttl = IPFRAGTTL;
4014640Swnj 		fp->ipq_p = ip->ip_p;
4024640Swnj 		fp->ipq_id = ip->ip_id;
4034898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
4044898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
4054898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
4065161Swnj 		q = (struct ipasfrag *)fp;
4075161Swnj 		goto insert;
4084640Swnj 	}
4094495Swnj 
4104640Swnj 	/*
4114640Swnj 	 * Find a segment which begins after this one does.
4124640Swnj 	 */
4134898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
4144640Swnj 		if (q->ip_off > ip->ip_off)
4154640Swnj 			break;
4164495Swnj 
4174640Swnj 	/*
4184640Swnj 	 * If there is a preceding segment, it may provide some of
4194640Swnj 	 * our data already.  If so, drop the data from the incoming
4204640Swnj 	 * segment.  If it provides all of our data, drop us.
4214640Swnj 	 */
4224898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
4234898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
4244640Swnj 		if (i > 0) {
4254640Swnj 			if (i >= ip->ip_len)
4264640Swnj 				goto dropfrag;
4274640Swnj 			m_adj(dtom(ip), i);
4284640Swnj 			ip->ip_off += i;
4294640Swnj 			ip->ip_len -= i;
4304640Swnj 		}
4314640Swnj 	}
4324543Swnj 
4334640Swnj 	/*
4344640Swnj 	 * While we overlap succeeding segments trim them or,
4354640Swnj 	 * if they are completely covered, dequeue them.
4364640Swnj 	 */
4374898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
4384640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
4394640Swnj 		if (i < q->ip_len) {
4404640Swnj 			q->ip_len -= i;
4416256Sroot 			q->ip_off += i;
4424640Swnj 			m_adj(dtom(q), i);
4434640Swnj 			break;
4444495Swnj 		}
4454898Swnj 		q = q->ipf_next;
4464898Swnj 		m_freem(dtom(q->ipf_prev));
4474898Swnj 		ip_deq(q->ipf_prev);
4484543Swnj 	}
4494495Swnj 
4505161Swnj insert:
4514640Swnj 	/*
4524640Swnj 	 * Stick new segment in its place;
4534640Swnj 	 * check for complete reassembly.
4544640Swnj 	 */
4554898Swnj 	ip_enq(ip, q->ipf_prev);
4564640Swnj 	next = 0;
4574898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
4584640Swnj 		if (q->ip_off != next)
4594640Swnj 			return (0);
4604640Swnj 		next += q->ip_len;
4614640Swnj 	}
4624898Swnj 	if (q->ipf_prev->ipf_mff)
4634640Swnj 		return (0);
4644495Swnj 
4654640Swnj 	/*
4664640Swnj 	 * Reassembly is complete; concatenate fragments.
4674640Swnj 	 */
4684640Swnj 	q = fp->ipq_next;
4694640Swnj 	m = dtom(q);
4704640Swnj 	t = m->m_next;
4714640Swnj 	m->m_next = 0;
4724640Swnj 	m_cat(m, t);
4736298Swnj 	q = q->ipf_next;
4746298Swnj 	while (q != (struct ipasfrag *)fp) {
4756298Swnj 		t = dtom(q);
4766298Swnj 		q = q->ipf_next;
4776298Swnj 		m_cat(m, t);
4786298Swnj 	}
4794495Swnj 
4804640Swnj 	/*
4814640Swnj 	 * Create header for new ip packet by
4824640Swnj 	 * modifying header of first packet;
4834640Swnj 	 * dequeue and discard fragment reassembly header.
4844640Swnj 	 * Make header visible.
4854640Swnj 	 */
4864640Swnj 	ip = fp->ipq_next;
4874640Swnj 	ip->ip_len = next;
4884898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
4894898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
4904640Swnj 	remque(fp);
4914907Swnj 	(void) m_free(dtom(fp));
4924640Swnj 	m = dtom(ip);
49324813Skarels 	m->m_len += (ip->ip_hl << 2);
49437319Skarels 	m->m_data -= (ip->ip_hl << 2);
49549042Ssklower 	/* some debugging cruft by sklower, below, will go away soon */
49649042Ssklower 	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
49749042Ssklower 		register int plen = 0;
49849042Ssklower 		for (t = m; m; m = m->m_next)
49949042Ssklower 			plen += m->m_len;
50049042Ssklower 		t->m_pkthdr.len = plen;
50149042Ssklower 	}
5024898Swnj 	return ((struct ip *)ip);
5034495Swnj 
5044640Swnj dropfrag:
50524813Skarels 	ipstat.ips_fragdropped++;
5064640Swnj 	m_freem(m);
5074640Swnj 	return (0);
5084495Swnj }
5094495Swnj 
5104640Swnj /*
5114640Swnj  * Free a fragment reassembly header and all
5124640Swnj  * associated datagrams.
5134640Swnj  */
5144640Swnj ip_freef(fp)
5154640Swnj 	struct ipq *fp;
5164495Swnj {
51710735Ssam 	register struct ipasfrag *q, *p;
5184495Swnj 
51910735Ssam 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {
52010735Ssam 		p = q->ipf_next;
52110735Ssam 		ip_deq(q);
5224640Swnj 		m_freem(dtom(q));
52310735Ssam 	}
52410735Ssam 	remque(fp);
52510735Ssam 	(void) m_free(dtom(fp));
5264495Swnj }
5274495Swnj 
5284640Swnj /*
5294640Swnj  * Put an ip fragment on a reassembly chain.
5304640Swnj  * Like insque, but pointers in middle of structure.
5314640Swnj  */
5324640Swnj ip_enq(p, prev)
5334898Swnj 	register struct ipasfrag *p, *prev;
5344495Swnj {
5354951Swnj 
5364898Swnj 	p->ipf_prev = prev;
5374898Swnj 	p->ipf_next = prev->ipf_next;
5384898Swnj 	prev->ipf_next->ipf_prev = p;
5394898Swnj 	prev->ipf_next = p;
5404495Swnj }
5414495Swnj 
5424640Swnj /*
5434640Swnj  * To ip_enq as remque is to insque.
5444640Swnj  */
5454640Swnj ip_deq(p)
5464898Swnj 	register struct ipasfrag *p;
5474640Swnj {
5484951Swnj 
5494898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
5504898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
5514495Swnj }
5524495Swnj 
5534640Swnj /*
5544640Swnj  * IP timer processing;
5554640Swnj  * if a timer expires on a reassembly
5564640Swnj  * queue, discard it.
5574640Swnj  */
5584801Swnj ip_slowtimo()
5594495Swnj {
5604495Swnj 	register struct ipq *fp;
5614640Swnj 	int s = splnet();
5624951Swnj 
5635243Sroot 	fp = ipq.next;
5645243Sroot 	if (fp == 0) {
5655243Sroot 		splx(s);
5665243Sroot 		return;
5675243Sroot 	}
56810735Ssam 	while (fp != &ipq) {
56910735Ssam 		--fp->ipq_ttl;
57010735Ssam 		fp = fp->next;
57124813Skarels 		if (fp->prev->ipq_ttl == 0) {
57224813Skarels 			ipstat.ips_fragtimeout++;
57310735Ssam 			ip_freef(fp->prev);
57424813Skarels 		}
57510735Ssam 	}
5764640Swnj 	splx(s);
5774495Swnj }
5784495Swnj 
5794951Swnj /*
5804951Swnj  * Drain off all datagram fragments.
5814951Swnj  */
5824801Swnj ip_drain()
5834801Swnj {
5844801Swnj 
58524813Skarels 	while (ipq.next != &ipq) {
58624813Skarels 		ipstat.ips_fragdropped++;
58710735Ssam 		ip_freef(ipq.next);
58824813Skarels 	}
5894801Swnj }
5904923Swnj 
59130925Skarels extern struct in_ifaddr *ifptoia();
59224813Skarels struct in_ifaddr *ip_rtaddr();
59324813Skarels 
5944640Swnj /*
5954640Swnj  * Do option processing on a datagram,
59640689Skarels  * possibly discarding it if bad options are encountered,
59740689Skarels  * or forwarding it if source-routed.
59840689Skarels  * Returns 1 if packet has been forwarded/freed,
59940689Skarels  * 0 if the packet should be processed further.
6004640Swnj  */
60137319Skarels ip_dooptions(m)
60236814Skarels 	struct mbuf *m;
6034495Swnj {
60436814Skarels 	register struct ip *ip = mtod(m, struct ip *);
6054640Swnj 	register u_char *cp;
60624813Skarels 	register struct ip_timestamp *ipt;
60724813Skarels 	register struct in_ifaddr *ia;
60836814Skarels 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
6094923Swnj 	struct in_addr *sin;
61024813Skarels 	n_time ntime;
6114495Swnj 
6124640Swnj 	cp = (u_char *)(ip + 1);
6134640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
6144640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
61524813Skarels 		opt = cp[IPOPT_OPTVAL];
6164640Swnj 		if (opt == IPOPT_EOL)
6174640Swnj 			break;
6184640Swnj 		if (opt == IPOPT_NOP)
6194640Swnj 			optlen = 1;
62016392Ssam 		else {
62124813Skarels 			optlen = cp[IPOPT_OLEN];
62224813Skarels 			if (optlen <= 0 || optlen > cnt) {
62324813Skarels 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
62417551Skarels 				goto bad;
62524813Skarels 			}
62616392Ssam 		}
6274640Swnj 		switch (opt) {
6284495Swnj 
6294640Swnj 		default:
6304640Swnj 			break;
6314495Swnj 
6324951Swnj 		/*
6334951Swnj 		 * Source routing with record.
6344951Swnj 		 * Find interface with current destination address.
6354951Swnj 		 * If none on this machine then drop if strictly routed,
6364951Swnj 		 * or do nothing if loosely routed.
6374951Swnj 		 * Record interface address and bring up next address
6384951Swnj 		 * component.  If strictly routed make sure next
63940689Skarels 		 * address is on directly accessible net.
6404951Swnj 		 */
6414640Swnj 		case IPOPT_LSRR:
6427508Sroot 		case IPOPT_SSRR:
64324813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
64424813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
64524813Skarels 				goto bad;
64624813Skarels 			}
64724813Skarels 			ipaddr.sin_addr = ip->ip_dst;
64824813Skarels 			ia = (struct in_ifaddr *)
64924813Skarels 				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
65024813Skarels 			if (ia == 0) {
65124813Skarels 				if (opt == IPOPT_SSRR) {
65224813Skarels 					type = ICMP_UNREACH;
65324813Skarels 					code = ICMP_UNREACH_SRCFAIL;
6544951Swnj 					goto bad;
65524813Skarels 				}
65624813Skarels 				/*
65724813Skarels 				 * Loose routing, and not at next destination
65824813Skarels 				 * yet; nothing to do except forward.
65924813Skarels 				 */
6604951Swnj 				break;
6614640Swnj 			}
66224813Skarels 			off--;			/* 0 origin */
66324813Skarels 			if (off > optlen - sizeof(struct in_addr)) {
66424813Skarels 				/*
66524813Skarels 				 * End of source route.  Should be for us.
66624813Skarels 				 */
66724813Skarels 				save_rte(cp, ip->ip_src);
6684951Swnj 				break;
66924813Skarels 			}
67024813Skarels 			/*
67124813Skarels 			 * locate outgoing interface
67224813Skarels 			 */
67326384Skarels 			bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
67424813Skarels 			    sizeof(ipaddr.sin_addr));
67540689Skarels 			if (opt == IPOPT_SSRR) {
67640689Skarels #define	INA	struct in_ifaddr *
67740689Skarels #define	SA	struct sockaddr *
67840689Skarels 			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
67940689Skarels 				ia = in_iaonnetof(in_netof(ipaddr.sin_addr));
68040689Skarels 			} else
68140689Skarels 				ia = ip_rtaddr(ipaddr.sin_addr);
68240689Skarels 			if (ia == 0) {
68324813Skarels 				type = ICMP_UNREACH;
68424813Skarels 				code = ICMP_UNREACH_SRCFAIL;
6854951Swnj 				goto bad;
68624813Skarels 			}
68724813Skarels 			ip->ip_dst = ipaddr.sin_addr;
68826384Skarels 			bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
68926384Skarels 			    (caddr_t)(cp + off), sizeof(struct in_addr));
69024813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
691*57433Sandrew 			/*
692*57433Sandrew 			 * Let ip_intr's mcast routing check handle mcast pkts
693*57433Sandrew 			 */
694*57433Sandrew 			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
6954640Swnj 			break;
6964495Swnj 
69724813Skarels 		case IPOPT_RR:
69824813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
69924813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
70024813Skarels 				goto bad;
70124813Skarels 			}
70224813Skarels 			/*
70324813Skarels 			 * If no space remains, ignore.
70424813Skarels 			 */
70524813Skarels 			off--;			/* 0 origin */
70624813Skarels 			if (off > optlen - sizeof(struct in_addr))
70724813Skarels 				break;
70831393Skarels 			bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
70924813Skarels 			    sizeof(ipaddr.sin_addr));
71024813Skarels 			/*
71137319Skarels 			 * locate outgoing interface; if we're the destination,
71237319Skarels 			 * use the incoming interface (should be same).
71324813Skarels 			 */
71440689Skarels 			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
71537319Skarels 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
71624813Skarels 				type = ICMP_UNREACH;
71732113Skarels 				code = ICMP_UNREACH_HOST;
71824813Skarels 				goto bad;
71924813Skarels 			}
72026384Skarels 			bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
72126384Skarels 			    (caddr_t)(cp + off), sizeof(struct in_addr));
72224813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
72324813Skarels 			break;
72424813Skarels 
7254640Swnj 		case IPOPT_TS:
7266583Ssam 			code = cp - (u_char *)ip;
7274801Swnj 			ipt = (struct ip_timestamp *)cp;
7284801Swnj 			if (ipt->ipt_len < 5)
7294640Swnj 				goto bad;
7304801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
7314801Swnj 				if (++ipt->ipt_oflw == 0)
7324640Swnj 					goto bad;
7334495Swnj 				break;
7344640Swnj 			}
73530925Skarels 			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
7364801Swnj 			switch (ipt->ipt_flg) {
7374495Swnj 
7384640Swnj 			case IPOPT_TS_TSONLY:
7394640Swnj 				break;
7404640Swnj 
7414640Swnj 			case IPOPT_TS_TSANDADDR:
74224813Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
74324813Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
7444640Swnj 					goto bad;
74537319Skarels 				ia = ifptoia(m->m_pkthdr.rcvif);
74630925Skarels 				bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
74724813Skarels 				    (caddr_t)sin, sizeof(struct in_addr));
74830925Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
7494640Swnj 				break;
7504640Swnj 
7514640Swnj 			case IPOPT_TS_PRESPEC:
75230925Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
75330925Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
75430925Skarels 					goto bad;
75524813Skarels 				bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
75624813Skarels 				    sizeof(struct in_addr));
75740689Skarels 				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
7584951Swnj 					continue;
75924813Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
7604640Swnj 				break;
7614640Swnj 
7624495Swnj 			default:
7634640Swnj 				goto bad;
7644495Swnj 			}
76524813Skarels 			ntime = iptime();
76630925Skarels 			bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
76730925Skarels 			    sizeof(n_time));
76824813Skarels 			ipt->ipt_ptr += sizeof(n_time);
7694640Swnj 		}
7704495Swnj 	}
77136814Skarels 	if (forward) {
77240689Skarels 		ip_forward(m, 1);
77336814Skarels 		return (1);
774*57433Sandrew 	}
775*57433Sandrew 	return (0);
7764640Swnj bad:
777*57433Sandrew 	ip->ip_len -= ip->ip_hl << 2;   /* XXX icmp_error adds in hdr length */
77837319Skarels 	icmp_error(m, type, code);
779*57433Sandrew 	ipstat.ips_badoptions++;
7806583Ssam 	return (1);
7814495Swnj }
7824495Swnj 
7834640Swnj /*
78424813Skarels  * Given address of next destination (final or next hop),
78524813Skarels  * return internet address info of interface to be used to get there.
78624813Skarels  */
78724813Skarels struct in_ifaddr *
78824813Skarels ip_rtaddr(dst)
78924813Skarels 	 struct in_addr dst;
79024813Skarels {
79124813Skarels 	register struct sockaddr_in *sin;
79224813Skarels 
79324813Skarels 	sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
79424813Skarels 
79524813Skarels 	if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
79624813Skarels 		if (ipforward_rt.ro_rt) {
79724813Skarels 			RTFREE(ipforward_rt.ro_rt);
79824813Skarels 			ipforward_rt.ro_rt = 0;
79924813Skarels 		}
80024813Skarels 		sin->sin_family = AF_INET;
80137319Skarels 		sin->sin_len = sizeof(*sin);
80224813Skarels 		sin->sin_addr = dst;
80324813Skarels 
80424813Skarels 		rtalloc(&ipforward_rt);
80524813Skarels 	}
80624813Skarels 	if (ipforward_rt.ro_rt == 0)
80724813Skarels 		return ((struct in_ifaddr *)0);
80840689Skarels 	return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
80924813Skarels }
81024813Skarels 
81124813Skarels /*
81224813Skarels  * Save incoming source route for use in replies,
81324813Skarels  * to be picked up later by ip_srcroute if the receiver is interested.
81424813Skarels  */
81524813Skarels save_rte(option, dst)
81626384Skarels 	u_char *option;
81724813Skarels 	struct in_addr dst;
81824813Skarels {
81926384Skarels 	unsigned olen;
82024813Skarels 
82124813Skarels 	olen = option[IPOPT_OLEN];
82249042Ssklower #ifdef DIAGNOSTIC
82336814Skarels 	if (ipprintfs)
82436814Skarels 		printf("save_rte: olen %d\n", olen);
82540689Skarels #endif
82636814Skarels 	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
82724813Skarels 		return;
82826384Skarels 	bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
82924813Skarels 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
83036814Skarels 	ip_srcrt.dst = dst;
83124813Skarels }
83224813Skarels 
83324813Skarels /*
83424813Skarels  * Retrieve incoming source route for use in replies,
83524813Skarels  * in the same form used by setsockopt.
83624813Skarels  * The first hop is placed before the options, will be removed later.
83724813Skarels  */
83824813Skarels struct mbuf *
83924813Skarels ip_srcroute()
84024813Skarels {
84124813Skarels 	register struct in_addr *p, *q;
84224813Skarels 	register struct mbuf *m;
84324813Skarels 
84424813Skarels 	if (ip_nhops == 0)
84524813Skarels 		return ((struct mbuf *)0);
84631201Skarels 	m = m_get(M_DONTWAIT, MT_SOOPTS);
84731201Skarels 	if (m == 0)
84831201Skarels 		return ((struct mbuf *)0);
84924813Skarels 
85036814Skarels #define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
85136814Skarels 
85236814Skarels 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
85336814Skarels 	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
85436814Skarels 	    OPTSIZ;
85549042Ssklower #ifdef DIAGNOSTIC
85636814Skarels 	if (ipprintfs)
85736814Skarels 		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
85840689Skarels #endif
85936814Skarels 
86024813Skarels 	/*
86124813Skarels 	 * First save first hop for return route
86224813Skarels 	 */
86324813Skarels 	p = &ip_srcrt.route[ip_nhops - 1];
86424813Skarels 	*(mtod(m, struct in_addr *)) = *p--;
86549042Ssklower #ifdef DIAGNOSTIC
86636814Skarels 	if (ipprintfs)
86749882Sbostic 		printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr));
86840689Skarels #endif
86924813Skarels 
87024813Skarels 	/*
87124813Skarels 	 * Copy option fields and padding (nop) to mbuf.
87224813Skarels 	 */
87324813Skarels 	ip_srcrt.nop = IPOPT_NOP;
87436814Skarels 	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
87536814Skarels 	bcopy((caddr_t)&ip_srcrt.nop,
87636814Skarels 	    mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
87724813Skarels 	q = (struct in_addr *)(mtod(m, caddr_t) +
87836814Skarels 	    sizeof(struct in_addr) + OPTSIZ);
87936814Skarels #undef OPTSIZ
88024813Skarels 	/*
88124813Skarels 	 * Record return path as an IP source route,
88224813Skarels 	 * reversing the path (pointers are now aligned).
88324813Skarels 	 */
88436814Skarels 	while (p >= ip_srcrt.route) {
88549042Ssklower #ifdef DIAGNOSTIC
88636814Skarels 		if (ipprintfs)
88749882Sbostic 			printf(" %lx", ntohl(q->s_addr));
88840689Skarels #endif
88924813Skarels 		*q++ = *p--;
89036814Skarels 	}
89136814Skarels 	/*
89236814Skarels 	 * Last hop goes to final destination.
89336814Skarels 	 */
89436814Skarels 	*q = ip_srcrt.dst;
89549042Ssklower #ifdef DIAGNOSTIC
89636814Skarels 	if (ipprintfs)
89749882Sbostic 		printf(" %lx\n", ntohl(q->s_addr));
89840689Skarels #endif
89924813Skarels 	return (m);
90024813Skarels }
90124813Skarels 
90224813Skarels /*
9034951Swnj  * Strip out IP options, at higher
9044951Swnj  * level protocol in the kernel.
9054951Swnj  * Second argument is buffer to which options
9064951Swnj  * will be moved, and return value is their length.
90736814Skarels  * XXX should be deleted; last arg currently ignored.
9084640Swnj  */
90937319Skarels ip_stripoptions(m, mopt)
91037319Skarels 	register struct mbuf *m;
9115217Swnj 	struct mbuf *mopt;
9124495Swnj {
9134640Swnj 	register int i;
91437319Skarels 	struct ip *ip = mtod(m, struct ip *);
91524813Skarels 	register caddr_t opts;
9164640Swnj 	int olen;
9174640Swnj 
9184640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
91924813Skarels 	opts = (caddr_t)(ip + 1);
9204640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
92124813Skarels 	bcopy(opts  + olen, opts, (unsigned)i);
9225243Sroot 	m->m_len -= olen;
92337319Skarels 	if (m->m_flags & M_PKTHDR)
92437319Skarels 		m->m_pkthdr.len -= olen;
92524813Skarels 	ip->ip_hl = sizeof(struct ip) >> 2;
9264495Swnj }
9276583Ssam 
92814670Ssam u_char inetctlerrmap[PRC_NCMDS] = {
92924813Skarels 	0,		0,		0,		0,
93040689Skarels 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
93140689Skarels 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
93224813Skarels 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
93324813Skarels 	0,		0,		0,		0,
93424813Skarels 	ENOPROTOOPT
9356583Ssam };
9366583Ssam 
9376583Ssam /*
9386583Ssam  * Forward a packet.  If some error occurs return the sender
93918376Skarels  * an icmp packet.  Note we can't always generate a meaningful
94024813Skarels  * icmp message because icmp doesn't have a large enough repertoire
9416583Ssam  * of codes and types.
94226308Skarels  *
94340689Skarels  * If not forwarding, just drop the packet.  This could be confusing
94440689Skarels  * if ipforwarding was zero but some routing protocol was advancing
94540689Skarels  * us as a gateway to somewhere.  However, we must let the routing
94640689Skarels  * protocol deal with that.
94740689Skarels  *
94840689Skarels  * The srcrt parameter indicates whether the packet is being forwarded
94940689Skarels  * via a source route.
9506583Ssam  */
95140689Skarels ip_forward(m, srcrt)
95236814Skarels 	struct mbuf *m;
95340689Skarels 	int srcrt;
9546583Ssam {
95536814Skarels 	register struct ip *ip = mtod(m, struct ip *);
95624813Skarels 	register struct sockaddr_in *sin;
95740689Skarels 	register struct rtentry *rt;
95840689Skarels 	int error, type = 0, code;
95918376Skarels 	struct mbuf *mcopy;
96024813Skarels 	struct in_addr dest;
961*57433Sandrew 	struct ifnet *destifp;
9626583Ssam 
96324813Skarels 	dest.s_addr = 0;
96449042Ssklower #ifdef DIAGNOSTIC
9656583Ssam 	if (ipprintfs)
9666583Ssam 		printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
9676583Ssam 			ip->ip_dst, ip->ip_ttl);
96840689Skarels #endif
96937319Skarels 	if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
97026308Skarels 		ipstat.ips_cantforward++;
97137319Skarels 		m_freem(m);
97226308Skarels 		return;
9736583Ssam 	}
97440689Skarels 	HTONS(ip->ip_id);
97531393Skarels 	if (ip->ip_ttl <= IPTTLDEC) {
97640689Skarels 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest);
97740689Skarels 		return;
9786583Ssam 	}
9796583Ssam 	ip->ip_ttl -= IPTTLDEC;
9806609Ssam 
98124813Skarels 	sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
98240689Skarels 	if ((rt = ipforward_rt.ro_rt) == 0 ||
98324813Skarels 	    ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
98424813Skarels 		if (ipforward_rt.ro_rt) {
98524813Skarels 			RTFREE(ipforward_rt.ro_rt);
98624813Skarels 			ipforward_rt.ro_rt = 0;
98724813Skarels 		}
98824813Skarels 		sin->sin_family = AF_INET;
98937319Skarels 		sin->sin_len = sizeof(*sin);
99024813Skarels 		sin->sin_addr = ip->ip_dst;
99124813Skarels 
99224813Skarels 		rtalloc(&ipforward_rt);
99340689Skarels 		if (ipforward_rt.ro_rt == 0) {
99440689Skarels 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest);
99540689Skarels 			return;
99640689Skarels 		}
99740689Skarels 		rt = ipforward_rt.ro_rt;
99824813Skarels 	}
99940689Skarels 
100024813Skarels 	/*
100140689Skarels 	 * Save at most 64 bytes of the packet in case
100240689Skarels 	 * we need to generate an ICMP message to the src.
100340689Skarels 	 */
100440689Skarels 	mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
100540689Skarels 
100640689Skarels #ifdef GATEWAY
100740689Skarels 	ip_ifmatrix[rt->rt_ifp->if_index +
100840689Skarels 	     if_index * m->m_pkthdr.rcvif->if_index]++;
100940689Skarels #endif
101040689Skarels 	/*
101124813Skarels 	 * If forwarding packet using same interface that it came in on,
101224813Skarels 	 * perhaps should send a redirect to sender to shortcut a hop.
101324813Skarels 	 * Only send redirect if source is sending directly to us,
101424813Skarels 	 * and if packet was not source routed (or has any options).
101530447Skarels 	 * Also, don't send redirect if forwarding using a default route
101640689Skarels 	 * or a route modified by a redirect.
101724813Skarels 	 */
101830447Skarels #define	satosin(sa)	((struct sockaddr_in *)(sa))
101940689Skarels 	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
102040689Skarels 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
102140689Skarels 	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
102240689Skarels 	    ipsendredirects && !srcrt) {
102352554Ssklower #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
102424813Skarels 		u_long src = ntohl(ip->ip_src.s_addr);
102524813Skarels 		u_long dst = ntohl(ip->ip_dst.s_addr);
102624813Skarels 
102752554Ssklower 		if (RTA(rt) &&
102852554Ssklower 		    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
102940689Skarels 		    if (rt->rt_flags & RTF_GATEWAY)
103040689Skarels 			dest = satosin(rt->rt_gateway)->sin_addr;
103124813Skarels 		    else
103224813Skarels 			dest = ip->ip_dst;
103324813Skarels 		    /*
103424813Skarels 		     * If the destination is reached by a route to host,
103527145Skarels 		     * is on a subnet of a local net, or is directly
103627145Skarels 		     * on the attached net (!), use host redirect.
103724813Skarels 		     * (We may be the correct first hop for other subnets.)
103824813Skarels 		     */
103924813Skarels 		    type = ICMP_REDIRECT;
104040689Skarels 		    if ((rt->rt_flags & RTF_HOST) ||
104140689Skarels 		        (rt->rt_flags & RTF_GATEWAY) == 0)
104240689Skarels 			    code = ICMP_REDIRECT_HOST;
104340689Skarels 		    else if (RTA(rt)->ia_subnetmask != RTA(rt)->ia_netmask &&
104440689Skarels 		        (dst & RTA(rt)->ia_netmask) ==  RTA(rt)->ia_net)
104540689Skarels 			    code = ICMP_REDIRECT_HOST;
104640689Skarels 		    else
104740689Skarels 			    code = ICMP_REDIRECT_NET;
104849042Ssklower #ifdef DIAGNOSTIC
104924813Skarels 		    if (ipprintfs)
105040689Skarels 		        printf("redirect (%d) to %x\n", code, dest.s_addr);
105140689Skarels #endif
105224813Skarels 		}
105324813Skarels 	}
105424813Skarels 
1055*57433Sandrew 	error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING, 0);
105624813Skarels 	if (error)
105724813Skarels 		ipstat.ips_cantforward++;
105824813Skarels 	else {
105921117Skarels 		ipstat.ips_forward++;
106040689Skarels 		if (type)
106140689Skarels 			ipstat.ips_redirectsent++;
106240689Skarels 		else {
106340689Skarels 			if (mcopy)
106440689Skarels 				m_freem(mcopy);
106540689Skarels 			return;
106640689Skarels 		}
10676609Ssam 	}
106811540Ssam 	if (mcopy == NULL)
106911540Ssam 		return;
1070*57433Sandrew 	destifp = NULL;
1071*57433Sandrew 
10726609Ssam 	switch (error) {
10736609Ssam 
107424813Skarels 	case 0:				/* forwarded, but need redirect */
107540689Skarels 		/* type, code set above */
107624813Skarels 		break;
107724813Skarels 
107840689Skarels 	case ENETUNREACH:		/* shouldn't happen, checked above */
107940689Skarels 	case EHOSTUNREACH:
10806609Ssam 	case ENETDOWN:
108140689Skarels 	case EHOSTDOWN:
108240689Skarels 	default:
108340689Skarels 		type = ICMP_UNREACH;
108440689Skarels 		code = ICMP_UNREACH_HOST;
10856609Ssam 		break;
10866609Ssam 
10876609Ssam 	case EMSGSIZE:
108840689Skarels 		type = ICMP_UNREACH;
10896583Ssam 		code = ICMP_UNREACH_NEEDFRAG;
1090*57433Sandrew 		if (ipforward_rt.ro_rt)
1091*57433Sandrew 			destifp = ipforward_rt.ro_rt->rt_ifp;
109239185Ssklower 		ipstat.ips_cantfrag++;
10936609Ssam 		break;
10946609Ssam 
10956609Ssam 	case ENOBUFS:
10966609Ssam 		type = ICMP_SOURCEQUENCH;
109737319Skarels 		code = 0;
10986609Ssam 		break;
10996609Ssam 	}
1100*57433Sandrew 	icmp_error(mcopy, type, code, dest, destifp);
11016583Ssam }
1102