xref: /csrg-svn/sys/netinet/ip_input.c (revision 44480)
123184Smckusick /*
236814Skarels  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
332787Sbostic  * All rights reserved.
423184Smckusick  *
5*44480Sbostic  * %sccs.include.redist.c%
632787Sbostic  *
7*44480Sbostic  *	@(#)ip_input.c	7.16 (Berkeley) 06/28/90
823184Smckusick  */
94571Swnj 
1017060Sbloom #include "param.h"
1117060Sbloom #include "systm.h"
1237319Skarels #include "malloc.h"
1317060Sbloom #include "mbuf.h"
1417060Sbloom #include "domain.h"
1517060Sbloom #include "protosw.h"
1617060Sbloom #include "socket.h"
1717060Sbloom #include "errno.h"
1817060Sbloom #include "time.h"
1917060Sbloom #include "kernel.h"
208695Sroot 
218695Sroot #include "../net/if.h"
228695Sroot #include "../net/route.h"
2310892Ssam 
2417060Sbloom #include "in.h"
2540689Skarels #include "in_systm.h"
2640689Skarels #include "ip.h"
2717060Sbloom #include "in_pcb.h"
2818376Skarels #include "in_var.h"
2917060Sbloom #include "ip_var.h"
3017060Sbloom #include "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;
4440689Skarels #ifdef DEBUG
4540689Skarels int	ipprintfs = 0;
4640689Skarels #endif
4736814Skarels 
484898Swnj u_char	ip_protox[IPPROTO_MAX];
496210Swnj int	ipqmaxlen = IFQ_MAXLEN;
5018376Skarels struct	in_ifaddr *in_ifaddr;			/* first inet address */
514898Swnj 
524801Swnj /*
5324813Skarels  * We need to save the IP options in case a protocol wants to respond
5424813Skarels  * to an incoming packet over the same route if the packet got here
5524813Skarels  * using IP source routing.  This allows connection establishment and
5624813Skarels  * maintenance when the remote end is on a network that is not known
5724813Skarels  * to us.
5824813Skarels  */
5924813Skarels int	ip_nhops = 0;
6024813Skarels static	struct ip_srcrt {
6136814Skarels 	struct	in_addr dst;			/* final destination */
6224813Skarels 	char	nop;				/* one NOP to align */
6324813Skarels 	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
6436814Skarels 	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
6524813Skarels } ip_srcrt;
6624813Skarels 
6740689Skarels #ifdef GATEWAY
6840689Skarels extern	int if_index;
6940689Skarels u_long	*ip_ifmatrix;
7040689Skarels #endif
7140689Skarels 
7224813Skarels /*
735172Swnj  * IP initialization: fill in IP protocol switch table.
745161Swnj  * All protocols not implemented in kernel go to raw IP protocol handler.
754801Swnj  */
764801Swnj ip_init()
774801Swnj {
784898Swnj 	register struct protosw *pr;
794898Swnj 	register int i;
804495Swnj 
8124813Skarels 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
824898Swnj 	if (pr == 0)
834898Swnj 		panic("ip_init");
844898Swnj 	for (i = 0; i < IPPROTO_MAX; i++)
859030Sroot 		ip_protox[i] = pr - inetsw;
869030Sroot 	for (pr = inetdomain.dom_protosw;
8717551Skarels 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
8816990Skarels 		if (pr->pr_domain->dom_family == PF_INET &&
894898Swnj 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
909030Sroot 			ip_protox[pr->pr_protocol] = pr - inetsw;
914801Swnj 	ipq.next = ipq.prev = &ipq;
928172Sroot 	ip_id = time.tv_sec & 0xffff;
936210Swnj 	ipintrq.ifq_maxlen = ipqmaxlen;
9440689Skarels #ifdef GATEWAY
9540689Skarels 	i = (if_index + 1) * (if_index + 1) * sizeof (u_long);
9640689Skarels 	if ((ip_ifmatrix = (u_long *) malloc(i, M_RTABLE, M_WAITOK)) == 0)
9740689Skarels 		panic("no memory for ip_ifmatrix");
9840689Skarels #endif
994801Swnj }
1004801Swnj 
1014640Swnj struct	ip *ip_reass();
10237319Skarels struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
10324813Skarels struct	route ipforward_rt;
1044640Swnj 
1054640Swnj /*
1064640Swnj  * Ip input routine.  Checksum and byte swap header.  If fragmented
10740689Skarels  * try to reassemble.  Process options.  Pass to next level.
1084640Swnj  */
1095084Swnj ipintr()
1104495Swnj {
1114923Swnj 	register struct ip *ip;
1125084Swnj 	register struct mbuf *m;
1134495Swnj 	register struct ipq *fp;
11418376Skarels 	register struct in_ifaddr *ia;
1155084Swnj 	int hlen, s;
1164495Swnj 
1175084Swnj next:
1184640Swnj 	/*
1195084Swnj 	 * Get next datagram off input queue and get IP header
1205084Swnj 	 * in first mbuf.
1214640Swnj 	 */
1225084Swnj 	s = splimp();
12337319Skarels 	IF_DEQUEUE(&ipintrq, m);
1245084Swnj 	splx(s);
1255218Swnj 	if (m == 0)
1265084Swnj 		return;
12737319Skarels if ((m->m_flags & M_PKTHDR) == 0)
12837319Skarels panic("ipintr no HDR");
12926001Skarels 	/*
13026001Skarels 	 * If no IP addresses have been set yet but the interfaces
13126001Skarels 	 * are receiving, can't do anything with incoming packets yet.
13226001Skarels 	 */
13326001Skarels 	if (in_ifaddr == NULL)
13426001Skarels 		goto bad;
13525920Skarels 	ipstat.ips_total++;
13640689Skarels 	if (m->m_len < sizeof (struct ip) &&
13711232Ssam 	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
13811232Ssam 		ipstat.ips_toosmall++;
13911232Ssam 		goto next;
14011232Ssam 	}
1414640Swnj 	ip = mtod(m, struct ip *);
14218376Skarels 	hlen = ip->ip_hl << 2;
14324813Skarels 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
14418376Skarels 		ipstat.ips_badhlen++;
14521117Skarels 		goto bad;
14618376Skarels 	}
14718376Skarels 	if (hlen > m->m_len) {
14811232Ssam 		if ((m = m_pullup(m, hlen)) == 0) {
14911232Ssam 			ipstat.ips_badhlen++;
15011232Ssam 			goto next;
15111232Ssam 		}
1525161Swnj 		ip = mtod(m, struct ip *);
1535161Swnj 	}
15437319Skarels 	if (ip->ip_sum = in_cksum(m, hlen)) {
15537319Skarels 		ipstat.ips_badsum++;
15637319Skarels 		goto bad;
15737319Skarels 	}
1584951Swnj 
1594951Swnj 	/*
1604951Swnj 	 * Convert fields to host representation.
1614951Swnj 	 */
16240689Skarels 	NTOHS(ip->ip_len);
16311232Ssam 	if (ip->ip_len < hlen) {
16411232Ssam 		ipstat.ips_badlen++;
16511232Ssam 		goto bad;
16611232Ssam 	}
16740689Skarels 	NTOHS(ip->ip_id);
16840689Skarels 	NTOHS(ip->ip_off);
1694495Swnj 
1704543Swnj 	/*
1714640Swnj 	 * Check that the amount of data in the buffers
1724640Swnj 	 * is as at least much as the IP header would have us expect.
1734640Swnj 	 * Trim mbufs if longer than we expect.
1744640Swnj 	 * Drop packet if shorter than we expect.
1754543Swnj 	 */
17637319Skarels 	if (m->m_pkthdr.len < ip->ip_len) {
17737319Skarels 		ipstat.ips_tooshort++;
17837319Skarels 		goto bad;
1796088Sroot 	}
18037319Skarels 	if (m->m_pkthdr.len > ip->ip_len) {
18137319Skarels 		if (m->m_len == m->m_pkthdr.len) {
18237319Skarels 			m->m_len = ip->ip_len;
18337319Skarels 			m->m_pkthdr.len = ip->ip_len;
18437319Skarels 		} else
18537319Skarels 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
1864495Swnj 	}
1874495Swnj 
1884640Swnj 	/*
1894640Swnj 	 * Process options and, if not destined for us,
1906583Ssam 	 * ship it on.  ip_dooptions returns 1 when an
1916583Ssam 	 * error was detected (causing an icmp message
19221117Skarels 	 * to be sent and the original packet to be freed).
1934640Swnj 	 */
19424813Skarels 	ip_nhops = 0;		/* for source routed packets */
19537319Skarels 	if (hlen > sizeof (struct ip) && ip_dooptions(m))
1966583Ssam 		goto next;
1976210Swnj 
1986338Ssam 	/*
19918376Skarels 	 * Check our list of addresses, to see if the packet is for us.
2006338Ssam 	 */
20118376Skarels 	for (ia = in_ifaddr; ia; ia = ia->ia_next) {
20218376Skarels #define	satosin(sa)	((struct sockaddr_in *)(sa))
2036338Ssam 
20418376Skarels 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
20524813Skarels 			goto ours;
20625195Skarels 		if (
20725195Skarels #ifdef	DIRECTED_BROADCAST
20837319Skarels 		    ia->ia_ifp == m->m_pkthdr.rcvif &&
20925195Skarels #endif
21025195Skarels 		    (ia->ia_ifp->if_flags & IFF_BROADCAST)) {
21126247Skarels 			u_long t;
21225195Skarels 
21325195Skarels 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
21425195Skarels 			    ip->ip_dst.s_addr)
21525195Skarels 				goto ours;
21625195Skarels 			if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
21725195Skarels 				goto ours;
21825195Skarels 			/*
21925195Skarels 			 * Look for all-0's host part (old broadcast addr),
22025195Skarels 			 * either for subnet or net.
22125195Skarels 			 */
22226247Skarels 			t = ntohl(ip->ip_dst.s_addr);
22326247Skarels 			if (t == ia->ia_subnet)
22425195Skarels 				goto ours;
22526247Skarels 			if (t == ia->ia_net)
22625195Skarels 				goto ours;
22725195Skarels 		}
2286338Ssam 	}
22924813Skarels 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
23024813Skarels 		goto ours;
23124813Skarels 	if (ip->ip_dst.s_addr == INADDR_ANY)
23224813Skarels 		goto ours;
2334495Swnj 
2344640Swnj 	/*
23524813Skarels 	 * Not for us; forward if possible and desirable.
23624813Skarels 	 */
23740689Skarels 	if (ipforwarding == 0) {
23836814Skarels 		ipstat.ips_cantforward++;
23936814Skarels 		m_freem(m);
24036814Skarels 	} else
24140689Skarels 		ip_forward(m, 0);
24224813Skarels 	goto next;
24324813Skarels 
24424813Skarels ours:
24524813Skarels 	/*
24633743Skarels 	 * If offset or IP_MF are set, must reassemble.
24733743Skarels 	 * Otherwise, nothing need be done.
24833743Skarels 	 * (We could look in the reassembly queue to see
24933743Skarels 	 * if the packet was previously fragmented,
25033743Skarels 	 * but it's not worth the time; just let them time out.)
2514640Swnj 	 */
25233743Skarels 	if (ip->ip_off &~ IP_DF) {
25340689Skarels 		if (m->m_flags & M_EXT) {		/* XXX */
25440689Skarels 			if ((m = m_pullup(m, sizeof (struct ip))) == 0) {
25540689Skarels 				ipstat.ips_toosmall++;
25640689Skarels 				goto next;
25740689Skarels 			}
25840689Skarels 			ip = mtod(m, struct ip *);
25940689Skarels 		}
26033743Skarels 		/*
26133743Skarels 		 * Look for queue of fragments
26233743Skarels 		 * of this datagram.
26333743Skarels 		 */
26433743Skarels 		for (fp = ipq.next; fp != &ipq; fp = fp->next)
26533743Skarels 			if (ip->ip_id == fp->ipq_id &&
26633743Skarels 			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
26733743Skarels 			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
26833743Skarels 			    ip->ip_p == fp->ipq_p)
26933743Skarels 				goto found;
27033743Skarels 		fp = 0;
2714640Swnj found:
2724495Swnj 
27333743Skarels 		/*
27433743Skarels 		 * Adjust ip_len to not reflect header,
27533743Skarels 		 * set ip_mff if more fragments are expected,
27633743Skarels 		 * convert offset of this to bytes.
27733743Skarels 		 */
27833743Skarels 		ip->ip_len -= hlen;
27933743Skarels 		((struct ipasfrag *)ip)->ipf_mff = 0;
28033743Skarels 		if (ip->ip_off & IP_MF)
28133743Skarels 			((struct ipasfrag *)ip)->ipf_mff = 1;
28233743Skarels 		ip->ip_off <<= 3;
2834495Swnj 
28433743Skarels 		/*
28533743Skarels 		 * If datagram marked as having more fragments
28633743Skarels 		 * or if this is not the first fragment,
28733743Skarels 		 * attempt reassembly; if it succeeds, proceed.
28833743Skarels 		 */
28933743Skarels 		if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
29033743Skarels 			ipstat.ips_fragments++;
29133743Skarels 			ip = ip_reass((struct ipasfrag *)ip, fp);
29233743Skarels 			if (ip == 0)
29333743Skarels 				goto next;
29439185Ssklower 			else
29539185Ssklower 				ipstat.ips_reassembled++;
29633743Skarels 			m = dtom(ip);
29733743Skarels 		} else
29833743Skarels 			if (fp)
29933743Skarels 				ip_freef(fp);
3004640Swnj 	} else
30133743Skarels 		ip->ip_len -= hlen;
3024951Swnj 
3034951Swnj 	/*
3044951Swnj 	 * Switch out to protocol's input routine.
3054951Swnj 	 */
30639185Ssklower 	ipstat.ips_delivered++;
30737319Skarels 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
3085084Swnj 	goto next;
3094951Swnj bad:
3104951Swnj 	m_freem(m);
3115084Swnj 	goto next;
3124640Swnj }
3134495Swnj 
3144640Swnj /*
3154640Swnj  * Take incoming datagram fragment and try to
3164951Swnj  * reassemble it into whole datagram.  If a chain for
3174640Swnj  * reassembly of this datagram already exists, then it
3184640Swnj  * is given as fp; otherwise have to make a chain.
3194640Swnj  */
3204640Swnj struct ip *
3214640Swnj ip_reass(ip, fp)
3224898Swnj 	register struct ipasfrag *ip;
3234640Swnj 	register struct ipq *fp;
3244640Swnj {
3254640Swnj 	register struct mbuf *m = dtom(ip);
3264898Swnj 	register struct ipasfrag *q;
3274640Swnj 	struct mbuf *t;
3284640Swnj 	int hlen = ip->ip_hl << 2;
3294640Swnj 	int i, next;
3304543Swnj 
3314640Swnj 	/*
3324640Swnj 	 * Presence of header sizes in mbufs
3334640Swnj 	 * would confuse code below.
3344640Swnj 	 */
33537319Skarels 	m->m_data += hlen;
3364640Swnj 	m->m_len -= hlen;
3374495Swnj 
3384640Swnj 	/*
3394640Swnj 	 * If first fragment to arrive, create a reassembly queue.
3404640Swnj 	 */
3414640Swnj 	if (fp == 0) {
34231201Skarels 		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
3434640Swnj 			goto dropfrag;
3444640Swnj 		fp = mtod(t, struct ipq *);
3454640Swnj 		insque(fp, &ipq);
3464640Swnj 		fp->ipq_ttl = IPFRAGTTL;
3474640Swnj 		fp->ipq_p = ip->ip_p;
3484640Swnj 		fp->ipq_id = ip->ip_id;
3494898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
3504898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
3514898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
3525161Swnj 		q = (struct ipasfrag *)fp;
3535161Swnj 		goto insert;
3544640Swnj 	}
3554495Swnj 
3564640Swnj 	/*
3574640Swnj 	 * Find a segment which begins after this one does.
3584640Swnj 	 */
3594898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
3604640Swnj 		if (q->ip_off > ip->ip_off)
3614640Swnj 			break;
3624495Swnj 
3634640Swnj 	/*
3644640Swnj 	 * If there is a preceding segment, it may provide some of
3654640Swnj 	 * our data already.  If so, drop the data from the incoming
3664640Swnj 	 * segment.  If it provides all of our data, drop us.
3674640Swnj 	 */
3684898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
3694898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
3704640Swnj 		if (i > 0) {
3714640Swnj 			if (i >= ip->ip_len)
3724640Swnj 				goto dropfrag;
3734640Swnj 			m_adj(dtom(ip), i);
3744640Swnj 			ip->ip_off += i;
3754640Swnj 			ip->ip_len -= i;
3764640Swnj 		}
3774640Swnj 	}
3784543Swnj 
3794640Swnj 	/*
3804640Swnj 	 * While we overlap succeeding segments trim them or,
3814640Swnj 	 * if they are completely covered, dequeue them.
3824640Swnj 	 */
3834898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
3844640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
3854640Swnj 		if (i < q->ip_len) {
3864640Swnj 			q->ip_len -= i;
3876256Sroot 			q->ip_off += i;
3884640Swnj 			m_adj(dtom(q), i);
3894640Swnj 			break;
3904495Swnj 		}
3914898Swnj 		q = q->ipf_next;
3924898Swnj 		m_freem(dtom(q->ipf_prev));
3934898Swnj 		ip_deq(q->ipf_prev);
3944543Swnj 	}
3954495Swnj 
3965161Swnj insert:
3974640Swnj 	/*
3984640Swnj 	 * Stick new segment in its place;
3994640Swnj 	 * check for complete reassembly.
4004640Swnj 	 */
4014898Swnj 	ip_enq(ip, q->ipf_prev);
4024640Swnj 	next = 0;
4034898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
4044640Swnj 		if (q->ip_off != next)
4054640Swnj 			return (0);
4064640Swnj 		next += q->ip_len;
4074640Swnj 	}
4084898Swnj 	if (q->ipf_prev->ipf_mff)
4094640Swnj 		return (0);
4104495Swnj 
4114640Swnj 	/*
4124640Swnj 	 * Reassembly is complete; concatenate fragments.
4134640Swnj 	 */
4144640Swnj 	q = fp->ipq_next;
4154640Swnj 	m = dtom(q);
4164640Swnj 	t = m->m_next;
4174640Swnj 	m->m_next = 0;
4184640Swnj 	m_cat(m, t);
4196298Swnj 	q = q->ipf_next;
4206298Swnj 	while (q != (struct ipasfrag *)fp) {
4216298Swnj 		t = dtom(q);
4226298Swnj 		q = q->ipf_next;
4236298Swnj 		m_cat(m, t);
4246298Swnj 	}
4254495Swnj 
4264640Swnj 	/*
4274640Swnj 	 * Create header for new ip packet by
4284640Swnj 	 * modifying header of first packet;
4294640Swnj 	 * dequeue and discard fragment reassembly header.
4304640Swnj 	 * Make header visible.
4314640Swnj 	 */
4324640Swnj 	ip = fp->ipq_next;
4334640Swnj 	ip->ip_len = next;
4344898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
4354898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
4364640Swnj 	remque(fp);
4374907Swnj 	(void) m_free(dtom(fp));
4384640Swnj 	m = dtom(ip);
43924813Skarels 	m->m_len += (ip->ip_hl << 2);
44037319Skarels 	m->m_data -= (ip->ip_hl << 2);
4414898Swnj 	return ((struct ip *)ip);
4424495Swnj 
4434640Swnj dropfrag:
44424813Skarels 	ipstat.ips_fragdropped++;
4454640Swnj 	m_freem(m);
4464640Swnj 	return (0);
4474495Swnj }
4484495Swnj 
4494640Swnj /*
4504640Swnj  * Free a fragment reassembly header and all
4514640Swnj  * associated datagrams.
4524640Swnj  */
4534640Swnj ip_freef(fp)
4544640Swnj 	struct ipq *fp;
4554495Swnj {
45610735Ssam 	register struct ipasfrag *q, *p;
4574495Swnj 
45810735Ssam 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {
45910735Ssam 		p = q->ipf_next;
46010735Ssam 		ip_deq(q);
4614640Swnj 		m_freem(dtom(q));
46210735Ssam 	}
46310735Ssam 	remque(fp);
46410735Ssam 	(void) m_free(dtom(fp));
4654495Swnj }
4664495Swnj 
4674640Swnj /*
4684640Swnj  * Put an ip fragment on a reassembly chain.
4694640Swnj  * Like insque, but pointers in middle of structure.
4704640Swnj  */
4714640Swnj ip_enq(p, prev)
4724898Swnj 	register struct ipasfrag *p, *prev;
4734495Swnj {
4744951Swnj 
4754898Swnj 	p->ipf_prev = prev;
4764898Swnj 	p->ipf_next = prev->ipf_next;
4774898Swnj 	prev->ipf_next->ipf_prev = p;
4784898Swnj 	prev->ipf_next = p;
4794495Swnj }
4804495Swnj 
4814640Swnj /*
4824640Swnj  * To ip_enq as remque is to insque.
4834640Swnj  */
4844640Swnj ip_deq(p)
4854898Swnj 	register struct ipasfrag *p;
4864640Swnj {
4874951Swnj 
4884898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
4894898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
4904495Swnj }
4914495Swnj 
4924640Swnj /*
4934640Swnj  * IP timer processing;
4944640Swnj  * if a timer expires on a reassembly
4954640Swnj  * queue, discard it.
4964640Swnj  */
4974801Swnj ip_slowtimo()
4984495Swnj {
4994495Swnj 	register struct ipq *fp;
5004640Swnj 	int s = splnet();
5014951Swnj 
5025243Sroot 	fp = ipq.next;
5035243Sroot 	if (fp == 0) {
5045243Sroot 		splx(s);
5055243Sroot 		return;
5065243Sroot 	}
50710735Ssam 	while (fp != &ipq) {
50810735Ssam 		--fp->ipq_ttl;
50910735Ssam 		fp = fp->next;
51024813Skarels 		if (fp->prev->ipq_ttl == 0) {
51124813Skarels 			ipstat.ips_fragtimeout++;
51210735Ssam 			ip_freef(fp->prev);
51324813Skarels 		}
51410735Ssam 	}
5154640Swnj 	splx(s);
5164495Swnj }
5174495Swnj 
5184951Swnj /*
5194951Swnj  * Drain off all datagram fragments.
5204951Swnj  */
5214801Swnj ip_drain()
5224801Swnj {
5234801Swnj 
52424813Skarels 	while (ipq.next != &ipq) {
52524813Skarels 		ipstat.ips_fragdropped++;
52610735Ssam 		ip_freef(ipq.next);
52724813Skarels 	}
5284801Swnj }
5294923Swnj 
53030925Skarels extern struct in_ifaddr *ifptoia();
53124813Skarels struct in_ifaddr *ip_rtaddr();
53224813Skarels 
5334640Swnj /*
5344640Swnj  * Do option processing on a datagram,
53540689Skarels  * possibly discarding it if bad options are encountered,
53640689Skarels  * or forwarding it if source-routed.
53740689Skarels  * Returns 1 if packet has been forwarded/freed,
53840689Skarels  * 0 if the packet should be processed further.
5394640Swnj  */
54037319Skarels ip_dooptions(m)
54136814Skarels 	struct mbuf *m;
5424495Swnj {
54336814Skarels 	register struct ip *ip = mtod(m, struct ip *);
5444640Swnj 	register u_char *cp;
54524813Skarels 	register struct ip_timestamp *ipt;
54624813Skarels 	register struct in_ifaddr *ia;
54736814Skarels 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
5484923Swnj 	struct in_addr *sin;
54924813Skarels 	n_time ntime;
5504495Swnj 
5514640Swnj 	cp = (u_char *)(ip + 1);
5524640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
5534640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
55424813Skarels 		opt = cp[IPOPT_OPTVAL];
5554640Swnj 		if (opt == IPOPT_EOL)
5564640Swnj 			break;
5574640Swnj 		if (opt == IPOPT_NOP)
5584640Swnj 			optlen = 1;
55916392Ssam 		else {
56024813Skarels 			optlen = cp[IPOPT_OLEN];
56124813Skarels 			if (optlen <= 0 || optlen > cnt) {
56224813Skarels 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
56317551Skarels 				goto bad;
56424813Skarels 			}
56516392Ssam 		}
5664640Swnj 		switch (opt) {
5674495Swnj 
5684640Swnj 		default:
5694640Swnj 			break;
5704495Swnj 
5714951Swnj 		/*
5724951Swnj 		 * Source routing with record.
5734951Swnj 		 * Find interface with current destination address.
5744951Swnj 		 * If none on this machine then drop if strictly routed,
5754951Swnj 		 * or do nothing if loosely routed.
5764951Swnj 		 * Record interface address and bring up next address
5774951Swnj 		 * component.  If strictly routed make sure next
57840689Skarels 		 * address is on directly accessible net.
5794951Swnj 		 */
5804640Swnj 		case IPOPT_LSRR:
5817508Sroot 		case IPOPT_SSRR:
58224813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
58324813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
58424813Skarels 				goto bad;
58524813Skarels 			}
58624813Skarels 			ipaddr.sin_addr = ip->ip_dst;
58724813Skarels 			ia = (struct in_ifaddr *)
58824813Skarels 				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
58924813Skarels 			if (ia == 0) {
59024813Skarels 				if (opt == IPOPT_SSRR) {
59124813Skarels 					type = ICMP_UNREACH;
59224813Skarels 					code = ICMP_UNREACH_SRCFAIL;
5934951Swnj 					goto bad;
59424813Skarels 				}
59524813Skarels 				/*
59624813Skarels 				 * Loose routing, and not at next destination
59724813Skarels 				 * yet; nothing to do except forward.
59824813Skarels 				 */
5994951Swnj 				break;
6004640Swnj 			}
60124813Skarels 			off--;			/* 0 origin */
60224813Skarels 			if (off > optlen - sizeof(struct in_addr)) {
60324813Skarels 				/*
60424813Skarels 				 * End of source route.  Should be for us.
60524813Skarels 				 */
60624813Skarels 				save_rte(cp, ip->ip_src);
6074951Swnj 				break;
60824813Skarels 			}
60924813Skarels 			/*
61024813Skarels 			 * locate outgoing interface
61124813Skarels 			 */
61226384Skarels 			bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
61324813Skarels 			    sizeof(ipaddr.sin_addr));
61440689Skarels 			if (opt == IPOPT_SSRR) {
61540689Skarels #define	INA	struct in_ifaddr *
61640689Skarels #define	SA	struct sockaddr *
61740689Skarels 			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
61840689Skarels 				ia = in_iaonnetof(in_netof(ipaddr.sin_addr));
61940689Skarels 			} else
62040689Skarels 				ia = ip_rtaddr(ipaddr.sin_addr);
62140689Skarels 			if (ia == 0) {
62224813Skarels 				type = ICMP_UNREACH;
62324813Skarels 				code = ICMP_UNREACH_SRCFAIL;
6244951Swnj 				goto bad;
62524813Skarels 			}
62624813Skarels 			ip->ip_dst = ipaddr.sin_addr;
62726384Skarels 			bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
62826384Skarels 			    (caddr_t)(cp + off), sizeof(struct in_addr));
62924813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
63036814Skarels 			forward = 1;
6314640Swnj 			break;
6324495Swnj 
63324813Skarels 		case IPOPT_RR:
63424813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
63524813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
63624813Skarels 				goto bad;
63724813Skarels 			}
63824813Skarels 			/*
63924813Skarels 			 * If no space remains, ignore.
64024813Skarels 			 */
64124813Skarels 			off--;			/* 0 origin */
64224813Skarels 			if (off > optlen - sizeof(struct in_addr))
64324813Skarels 				break;
64431393Skarels 			bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
64524813Skarels 			    sizeof(ipaddr.sin_addr));
64624813Skarels 			/*
64737319Skarels 			 * locate outgoing interface; if we're the destination,
64837319Skarels 			 * use the incoming interface (should be same).
64924813Skarels 			 */
65040689Skarels 			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
65137319Skarels 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
65224813Skarels 				type = ICMP_UNREACH;
65332113Skarels 				code = ICMP_UNREACH_HOST;
65424813Skarels 				goto bad;
65524813Skarels 			}
65626384Skarels 			bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
65726384Skarels 			    (caddr_t)(cp + off), sizeof(struct in_addr));
65824813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
65924813Skarels 			break;
66024813Skarels 
6614640Swnj 		case IPOPT_TS:
6626583Ssam 			code = cp - (u_char *)ip;
6634801Swnj 			ipt = (struct ip_timestamp *)cp;
6644801Swnj 			if (ipt->ipt_len < 5)
6654640Swnj 				goto bad;
6664801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
6674801Swnj 				if (++ipt->ipt_oflw == 0)
6684640Swnj 					goto bad;
6694495Swnj 				break;
6704640Swnj 			}
67130925Skarels 			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
6724801Swnj 			switch (ipt->ipt_flg) {
6734495Swnj 
6744640Swnj 			case IPOPT_TS_TSONLY:
6754640Swnj 				break;
6764640Swnj 
6774640Swnj 			case IPOPT_TS_TSANDADDR:
67824813Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
67924813Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
6804640Swnj 					goto bad;
68137319Skarels 				ia = ifptoia(m->m_pkthdr.rcvif);
68230925Skarels 				bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
68324813Skarels 				    (caddr_t)sin, sizeof(struct in_addr));
68430925Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
6854640Swnj 				break;
6864640Swnj 
6874640Swnj 			case IPOPT_TS_PRESPEC:
68830925Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
68930925Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
69030925Skarels 					goto bad;
69124813Skarels 				bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
69224813Skarels 				    sizeof(struct in_addr));
69340689Skarels 				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
6944951Swnj 					continue;
69524813Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
6964640Swnj 				break;
6974640Swnj 
6984495Swnj 			default:
6994640Swnj 				goto bad;
7004495Swnj 			}
70124813Skarels 			ntime = iptime();
70230925Skarels 			bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
70330925Skarels 			    sizeof(n_time));
70424813Skarels 			ipt->ipt_ptr += sizeof(n_time);
7054640Swnj 		}
7064495Swnj 	}
70736814Skarels 	if (forward) {
70840689Skarels 		ip_forward(m, 1);
70936814Skarels 		return (1);
71036814Skarels 	} else
71136814Skarels 		return (0);
7124640Swnj bad:
71337319Skarels 	icmp_error(m, type, code);
7146583Ssam 	return (1);
7154495Swnj }
7164495Swnj 
7174640Swnj /*
71824813Skarels  * Given address of next destination (final or next hop),
71924813Skarels  * return internet address info of interface to be used to get there.
72024813Skarels  */
72124813Skarels struct in_ifaddr *
72224813Skarels ip_rtaddr(dst)
72324813Skarels 	 struct in_addr dst;
72424813Skarels {
72524813Skarels 	register struct sockaddr_in *sin;
72624813Skarels 
72724813Skarels 	sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
72824813Skarels 
72924813Skarels 	if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
73024813Skarels 		if (ipforward_rt.ro_rt) {
73124813Skarels 			RTFREE(ipforward_rt.ro_rt);
73224813Skarels 			ipforward_rt.ro_rt = 0;
73324813Skarels 		}
73424813Skarels 		sin->sin_family = AF_INET;
73537319Skarels 		sin->sin_len = sizeof(*sin);
73624813Skarels 		sin->sin_addr = dst;
73724813Skarels 
73824813Skarels 		rtalloc(&ipforward_rt);
73924813Skarels 	}
74024813Skarels 	if (ipforward_rt.ro_rt == 0)
74124813Skarels 		return ((struct in_ifaddr *)0);
74240689Skarels 	return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
74324813Skarels }
74424813Skarels 
74524813Skarels /*
74624813Skarels  * Save incoming source route for use in replies,
74724813Skarels  * to be picked up later by ip_srcroute if the receiver is interested.
74824813Skarels  */
74924813Skarels save_rte(option, dst)
75026384Skarels 	u_char *option;
75124813Skarels 	struct in_addr dst;
75224813Skarels {
75326384Skarels 	unsigned olen;
75424813Skarels 
75524813Skarels 	olen = option[IPOPT_OLEN];
75640689Skarels #ifdef DEBUG
75736814Skarels 	if (ipprintfs)
75836814Skarels 		printf("save_rte: olen %d\n", olen);
75940689Skarels #endif
76036814Skarels 	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
76124813Skarels 		return;
76226384Skarels 	bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
76324813Skarels 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
76436814Skarels 	ip_srcrt.dst = dst;
76524813Skarels }
76624813Skarels 
76724813Skarels /*
76824813Skarels  * Retrieve incoming source route for use in replies,
76924813Skarels  * in the same form used by setsockopt.
77024813Skarels  * The first hop is placed before the options, will be removed later.
77124813Skarels  */
77224813Skarels struct mbuf *
77324813Skarels ip_srcroute()
77424813Skarels {
77524813Skarels 	register struct in_addr *p, *q;
77624813Skarels 	register struct mbuf *m;
77724813Skarels 
77824813Skarels 	if (ip_nhops == 0)
77924813Skarels 		return ((struct mbuf *)0);
78031201Skarels 	m = m_get(M_DONTWAIT, MT_SOOPTS);
78131201Skarels 	if (m == 0)
78231201Skarels 		return ((struct mbuf *)0);
78324813Skarels 
78436814Skarels #define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
78536814Skarels 
78636814Skarels 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
78736814Skarels 	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
78836814Skarels 	    OPTSIZ;
78940689Skarels #ifdef DEBUG
79036814Skarels 	if (ipprintfs)
79136814Skarels 		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
79240689Skarels #endif
79336814Skarels 
79424813Skarels 	/*
79524813Skarels 	 * First save first hop for return route
79624813Skarels 	 */
79724813Skarels 	p = &ip_srcrt.route[ip_nhops - 1];
79824813Skarels 	*(mtod(m, struct in_addr *)) = *p--;
79940689Skarels #ifdef DEBUG
80036814Skarels 	if (ipprintfs)
80136814Skarels 		printf(" hops %X", ntohl(*mtod(m, struct in_addr *)));
80240689Skarels #endif
80324813Skarels 
80424813Skarels 	/*
80524813Skarels 	 * Copy option fields and padding (nop) to mbuf.
80624813Skarels 	 */
80724813Skarels 	ip_srcrt.nop = IPOPT_NOP;
80836814Skarels 	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
80936814Skarels 	bcopy((caddr_t)&ip_srcrt.nop,
81036814Skarels 	    mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
81124813Skarels 	q = (struct in_addr *)(mtod(m, caddr_t) +
81236814Skarels 	    sizeof(struct in_addr) + OPTSIZ);
81336814Skarels #undef OPTSIZ
81424813Skarels 	/*
81524813Skarels 	 * Record return path as an IP source route,
81624813Skarels 	 * reversing the path (pointers are now aligned).
81724813Skarels 	 */
81836814Skarels 	while (p >= ip_srcrt.route) {
81940689Skarels #ifdef DEBUG
82036814Skarels 		if (ipprintfs)
82136814Skarels 			printf(" %X", ntohl(*q));
82240689Skarels #endif
82324813Skarels 		*q++ = *p--;
82436814Skarels 	}
82536814Skarels 	/*
82636814Skarels 	 * Last hop goes to final destination.
82736814Skarels 	 */
82836814Skarels 	*q = ip_srcrt.dst;
82940689Skarels #ifdef DEBUG
83036814Skarels 	if (ipprintfs)
83136814Skarels 		printf(" %X\n", ntohl(*q));
83240689Skarels #endif
83324813Skarels 	return (m);
83424813Skarels }
83524813Skarels 
83624813Skarels /*
8374951Swnj  * Strip out IP options, at higher
8384951Swnj  * level protocol in the kernel.
8394951Swnj  * Second argument is buffer to which options
8404951Swnj  * will be moved, and return value is their length.
84136814Skarels  * XXX should be deleted; last arg currently ignored.
8424640Swnj  */
84337319Skarels ip_stripoptions(m, mopt)
84437319Skarels 	register struct mbuf *m;
8455217Swnj 	struct mbuf *mopt;
8464495Swnj {
8474640Swnj 	register int i;
84837319Skarels 	struct ip *ip = mtod(m, struct ip *);
84924813Skarels 	register caddr_t opts;
8504640Swnj 	int olen;
8514640Swnj 
8524640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
85324813Skarels 	opts = (caddr_t)(ip + 1);
8544640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
85524813Skarels 	bcopy(opts  + olen, opts, (unsigned)i);
8565243Sroot 	m->m_len -= olen;
85737319Skarels 	if (m->m_flags & M_PKTHDR)
85837319Skarels 		m->m_pkthdr.len -= olen;
85924813Skarels 	ip->ip_hl = sizeof(struct ip) >> 2;
8604495Swnj }
8616583Ssam 
86214670Ssam u_char inetctlerrmap[PRC_NCMDS] = {
86324813Skarels 	0,		0,		0,		0,
86440689Skarels 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
86540689Skarels 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
86624813Skarels 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
86724813Skarels 	0,		0,		0,		0,
86824813Skarels 	ENOPROTOOPT
8696583Ssam };
8706583Ssam 
8716583Ssam /*
8726583Ssam  * Forward a packet.  If some error occurs return the sender
87318376Skarels  * an icmp packet.  Note we can't always generate a meaningful
87424813Skarels  * icmp message because icmp doesn't have a large enough repertoire
8756583Ssam  * of codes and types.
87626308Skarels  *
87740689Skarels  * If not forwarding, just drop the packet.  This could be confusing
87840689Skarels  * if ipforwarding was zero but some routing protocol was advancing
87940689Skarels  * us as a gateway to somewhere.  However, we must let the routing
88040689Skarels  * protocol deal with that.
88140689Skarels  *
88240689Skarels  * The srcrt parameter indicates whether the packet is being forwarded
88340689Skarels  * via a source route.
8846583Ssam  */
88540689Skarels ip_forward(m, srcrt)
88636814Skarels 	struct mbuf *m;
88740689Skarels 	int srcrt;
8886583Ssam {
88936814Skarels 	register struct ip *ip = mtod(m, struct ip *);
89024813Skarels 	register struct sockaddr_in *sin;
89140689Skarels 	register struct rtentry *rt;
89240689Skarels 	int error, type = 0, code;
89318376Skarels 	struct mbuf *mcopy;
89424813Skarels 	struct in_addr dest;
8956583Ssam 
89624813Skarels 	dest.s_addr = 0;
89740689Skarels #ifdef DEBUG
8986583Ssam 	if (ipprintfs)
8996583Ssam 		printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
9006583Ssam 			ip->ip_dst, ip->ip_ttl);
90140689Skarels #endif
90237319Skarels 	if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
90326308Skarels 		ipstat.ips_cantforward++;
90437319Skarels 		m_freem(m);
90526308Skarels 		return;
9066583Ssam 	}
90740689Skarels 	HTONS(ip->ip_id);
90831393Skarels 	if (ip->ip_ttl <= IPTTLDEC) {
90940689Skarels 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest);
91040689Skarels 		return;
9116583Ssam 	}
9126583Ssam 	ip->ip_ttl -= IPTTLDEC;
9136609Ssam 
91424813Skarels 	sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
91540689Skarels 	if ((rt = ipforward_rt.ro_rt) == 0 ||
91624813Skarels 	    ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
91724813Skarels 		if (ipforward_rt.ro_rt) {
91824813Skarels 			RTFREE(ipforward_rt.ro_rt);
91924813Skarels 			ipforward_rt.ro_rt = 0;
92024813Skarels 		}
92124813Skarels 		sin->sin_family = AF_INET;
92237319Skarels 		sin->sin_len = sizeof(*sin);
92324813Skarels 		sin->sin_addr = ip->ip_dst;
92424813Skarels 
92524813Skarels 		rtalloc(&ipforward_rt);
92640689Skarels 		if (ipforward_rt.ro_rt == 0) {
92740689Skarels 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest);
92840689Skarels 			return;
92940689Skarels 		}
93040689Skarels 		rt = ipforward_rt.ro_rt;
93124813Skarels 	}
93240689Skarels 
93324813Skarels 	/*
93440689Skarels 	 * Save at most 64 bytes of the packet in case
93540689Skarels 	 * we need to generate an ICMP message to the src.
93640689Skarels 	 */
93740689Skarels 	mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
93840689Skarels 
93940689Skarels #ifdef GATEWAY
94040689Skarels 	ip_ifmatrix[rt->rt_ifp->if_index +
94140689Skarels 	     if_index * m->m_pkthdr.rcvif->if_index]++;
94240689Skarels #endif
94340689Skarels 	/*
94424813Skarels 	 * If forwarding packet using same interface that it came in on,
94524813Skarels 	 * perhaps should send a redirect to sender to shortcut a hop.
94624813Skarels 	 * Only send redirect if source is sending directly to us,
94724813Skarels 	 * and if packet was not source routed (or has any options).
94830447Skarels 	 * Also, don't send redirect if forwarding using a default route
94940689Skarels 	 * or a route modified by a redirect.
95024813Skarels 	 */
95130447Skarels #define	satosin(sa)	((struct sockaddr_in *)(sa))
95240689Skarels 	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
95340689Skarels 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
95440689Skarels 	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
95540689Skarels 	    ipsendredirects && !srcrt) {
95624813Skarels 		struct in_ifaddr *ia;
95724813Skarels 		u_long src = ntohl(ip->ip_src.s_addr);
95824813Skarels 		u_long dst = ntohl(ip->ip_dst.s_addr);
95924813Skarels 
96037319Skarels 		if ((ia = ifptoia(m->m_pkthdr.rcvif)) &&
96124813Skarels 		   (src & ia->ia_subnetmask) == ia->ia_subnet) {
96240689Skarels 		    if (rt->rt_flags & RTF_GATEWAY)
96340689Skarels 			dest = satosin(rt->rt_gateway)->sin_addr;
96424813Skarels 		    else
96524813Skarels 			dest = ip->ip_dst;
96624813Skarels 		    /*
96724813Skarels 		     * If the destination is reached by a route to host,
96827145Skarels 		     * is on a subnet of a local net, or is directly
96927145Skarels 		     * on the attached net (!), use host redirect.
97024813Skarels 		     * (We may be the correct first hop for other subnets.)
97124813Skarels 		     */
97240689Skarels #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
97324813Skarels 		    type = ICMP_REDIRECT;
97440689Skarels 		    if ((rt->rt_flags & RTF_HOST) ||
97540689Skarels 		        (rt->rt_flags & RTF_GATEWAY) == 0)
97640689Skarels 			    code = ICMP_REDIRECT_HOST;
97740689Skarels 		    else if (RTA(rt)->ia_subnetmask != RTA(rt)->ia_netmask &&
97840689Skarels 		        (dst & RTA(rt)->ia_netmask) ==  RTA(rt)->ia_net)
97940689Skarels 			    code = ICMP_REDIRECT_HOST;
98040689Skarels 		    else
98140689Skarels 			    code = ICMP_REDIRECT_NET;
98240689Skarels #ifdef DEBUG
98324813Skarels 		    if (ipprintfs)
98440689Skarels 		        printf("redirect (%d) to %x\n", code, dest.s_addr);
98540689Skarels #endif
98624813Skarels 		}
98724813Skarels 	}
98824813Skarels 
98937319Skarels 	error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING);
99024813Skarels 	if (error)
99124813Skarels 		ipstat.ips_cantforward++;
99224813Skarels 	else {
99321117Skarels 		ipstat.ips_forward++;
99440689Skarels 		if (type)
99540689Skarels 			ipstat.ips_redirectsent++;
99640689Skarels 		else {
99740689Skarels 			if (mcopy)
99840689Skarels 				m_freem(mcopy);
99940689Skarels 			return;
100040689Skarels 		}
10016609Ssam 	}
100211540Ssam 	if (mcopy == NULL)
100311540Ssam 		return;
10046609Ssam 	switch (error) {
10056609Ssam 
100624813Skarels 	case 0:				/* forwarded, but need redirect */
100740689Skarels 		/* type, code set above */
100824813Skarels 		break;
100924813Skarels 
101040689Skarels 	case ENETUNREACH:		/* shouldn't happen, checked above */
101140689Skarels 	case EHOSTUNREACH:
10126609Ssam 	case ENETDOWN:
101340689Skarels 	case EHOSTDOWN:
101440689Skarels 	default:
101540689Skarels 		type = ICMP_UNREACH;
101640689Skarels 		code = ICMP_UNREACH_HOST;
10176609Ssam 		break;
10186609Ssam 
10196609Ssam 	case EMSGSIZE:
102040689Skarels 		type = ICMP_UNREACH;
10216583Ssam 		code = ICMP_UNREACH_NEEDFRAG;
102239185Ssklower 		ipstat.ips_cantfrag++;
10236609Ssam 		break;
10246609Ssam 
10256609Ssam 	case ENOBUFS:
10266609Ssam 		type = ICMP_SOURCEQUENCH;
102737319Skarels 		code = 0;
10286609Ssam 		break;
10296609Ssam 	}
103038795Skarels 	icmp_error(mcopy, type, code, dest);
10316583Ssam }
1032