xref: /csrg-svn/sys/netinet/ip_input.c (revision 44967)
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*44967Skarels  *	@(#)ip_input.c	7.17 (Berkeley) 07/25/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;
127*44967Skarels #ifdef	DIAGNOSTIC
128*44967Skarels 	if ((m->m_flags & M_PKTHDR) == 0)
129*44967Skarels 		panic("ipintr no HDR");
130*44967Skarels #endif
13126001Skarels 	/*
13226001Skarels 	 * If no IP addresses have been set yet but the interfaces
13326001Skarels 	 * are receiving, can't do anything with incoming packets yet.
13426001Skarels 	 */
13526001Skarels 	if (in_ifaddr == NULL)
13626001Skarels 		goto bad;
13725920Skarels 	ipstat.ips_total++;
13840689Skarels 	if (m->m_len < sizeof (struct ip) &&
13911232Ssam 	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
14011232Ssam 		ipstat.ips_toosmall++;
14111232Ssam 		goto next;
14211232Ssam 	}
1434640Swnj 	ip = mtod(m, struct ip *);
14418376Skarels 	hlen = ip->ip_hl << 2;
14524813Skarels 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
14618376Skarels 		ipstat.ips_badhlen++;
14721117Skarels 		goto bad;
14818376Skarels 	}
14918376Skarels 	if (hlen > m->m_len) {
15011232Ssam 		if ((m = m_pullup(m, hlen)) == 0) {
15111232Ssam 			ipstat.ips_badhlen++;
15211232Ssam 			goto next;
15311232Ssam 		}
1545161Swnj 		ip = mtod(m, struct ip *);
1555161Swnj 	}
15637319Skarels 	if (ip->ip_sum = in_cksum(m, hlen)) {
15737319Skarels 		ipstat.ips_badsum++;
15837319Skarels 		goto bad;
15937319Skarels 	}
1604951Swnj 
1614951Swnj 	/*
1624951Swnj 	 * Convert fields to host representation.
1634951Swnj 	 */
16440689Skarels 	NTOHS(ip->ip_len);
16511232Ssam 	if (ip->ip_len < hlen) {
16611232Ssam 		ipstat.ips_badlen++;
16711232Ssam 		goto bad;
16811232Ssam 	}
16940689Skarels 	NTOHS(ip->ip_id);
17040689Skarels 	NTOHS(ip->ip_off);
1714495Swnj 
1724543Swnj 	/*
1734640Swnj 	 * Check that the amount of data in the buffers
1744640Swnj 	 * is as at least much as the IP header would have us expect.
1754640Swnj 	 * Trim mbufs if longer than we expect.
1764640Swnj 	 * Drop packet if shorter than we expect.
1774543Swnj 	 */
17837319Skarels 	if (m->m_pkthdr.len < ip->ip_len) {
17937319Skarels 		ipstat.ips_tooshort++;
18037319Skarels 		goto bad;
1816088Sroot 	}
18237319Skarels 	if (m->m_pkthdr.len > ip->ip_len) {
18337319Skarels 		if (m->m_len == m->m_pkthdr.len) {
18437319Skarels 			m->m_len = ip->ip_len;
18537319Skarels 			m->m_pkthdr.len = ip->ip_len;
18637319Skarels 		} else
18737319Skarels 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
1884495Swnj 	}
1894495Swnj 
1904640Swnj 	/*
1914640Swnj 	 * Process options and, if not destined for us,
1926583Ssam 	 * ship it on.  ip_dooptions returns 1 when an
1936583Ssam 	 * error was detected (causing an icmp message
19421117Skarels 	 * to be sent and the original packet to be freed).
1954640Swnj 	 */
19624813Skarels 	ip_nhops = 0;		/* for source routed packets */
19737319Skarels 	if (hlen > sizeof (struct ip) && ip_dooptions(m))
1986583Ssam 		goto next;
1996210Swnj 
2006338Ssam 	/*
20118376Skarels 	 * Check our list of addresses, to see if the packet is for us.
2026338Ssam 	 */
20318376Skarels 	for (ia = in_ifaddr; ia; ia = ia->ia_next) {
20418376Skarels #define	satosin(sa)	((struct sockaddr_in *)(sa))
2056338Ssam 
20618376Skarels 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
20724813Skarels 			goto ours;
20825195Skarels 		if (
20925195Skarels #ifdef	DIRECTED_BROADCAST
21037319Skarels 		    ia->ia_ifp == m->m_pkthdr.rcvif &&
21125195Skarels #endif
21225195Skarels 		    (ia->ia_ifp->if_flags & IFF_BROADCAST)) {
21326247Skarels 			u_long t;
21425195Skarels 
21525195Skarels 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
21625195Skarels 			    ip->ip_dst.s_addr)
21725195Skarels 				goto ours;
21825195Skarels 			if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
21925195Skarels 				goto ours;
22025195Skarels 			/*
22125195Skarels 			 * Look for all-0's host part (old broadcast addr),
22225195Skarels 			 * either for subnet or net.
22325195Skarels 			 */
22426247Skarels 			t = ntohl(ip->ip_dst.s_addr);
22526247Skarels 			if (t == ia->ia_subnet)
22625195Skarels 				goto ours;
22726247Skarels 			if (t == ia->ia_net)
22825195Skarels 				goto ours;
22925195Skarels 		}
2306338Ssam 	}
23124813Skarels 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
23224813Skarels 		goto ours;
23324813Skarels 	if (ip->ip_dst.s_addr == INADDR_ANY)
23424813Skarels 		goto ours;
2354495Swnj 
2364640Swnj 	/*
23724813Skarels 	 * Not for us; forward if possible and desirable.
23824813Skarels 	 */
23940689Skarels 	if (ipforwarding == 0) {
24036814Skarels 		ipstat.ips_cantforward++;
24136814Skarels 		m_freem(m);
24236814Skarels 	} else
24340689Skarels 		ip_forward(m, 0);
24424813Skarels 	goto next;
24524813Skarels 
24624813Skarels ours:
24724813Skarels 	/*
24833743Skarels 	 * If offset or IP_MF are set, must reassemble.
24933743Skarels 	 * Otherwise, nothing need be done.
25033743Skarels 	 * (We could look in the reassembly queue to see
25133743Skarels 	 * if the packet was previously fragmented,
25233743Skarels 	 * but it's not worth the time; just let them time out.)
2534640Swnj 	 */
25433743Skarels 	if (ip->ip_off &~ IP_DF) {
25540689Skarels 		if (m->m_flags & M_EXT) {		/* XXX */
25640689Skarels 			if ((m = m_pullup(m, sizeof (struct ip))) == 0) {
25740689Skarels 				ipstat.ips_toosmall++;
25840689Skarels 				goto next;
25940689Skarels 			}
26040689Skarels 			ip = mtod(m, struct ip *);
26140689Skarels 		}
26233743Skarels 		/*
26333743Skarels 		 * Look for queue of fragments
26433743Skarels 		 * of this datagram.
26533743Skarels 		 */
26633743Skarels 		for (fp = ipq.next; fp != &ipq; fp = fp->next)
26733743Skarels 			if (ip->ip_id == fp->ipq_id &&
26833743Skarels 			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
26933743Skarels 			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
27033743Skarels 			    ip->ip_p == fp->ipq_p)
27133743Skarels 				goto found;
27233743Skarels 		fp = 0;
2734640Swnj found:
2744495Swnj 
27533743Skarels 		/*
27633743Skarels 		 * Adjust ip_len to not reflect header,
27733743Skarels 		 * set ip_mff if more fragments are expected,
27833743Skarels 		 * convert offset of this to bytes.
27933743Skarels 		 */
28033743Skarels 		ip->ip_len -= hlen;
28133743Skarels 		((struct ipasfrag *)ip)->ipf_mff = 0;
28233743Skarels 		if (ip->ip_off & IP_MF)
28333743Skarels 			((struct ipasfrag *)ip)->ipf_mff = 1;
28433743Skarels 		ip->ip_off <<= 3;
2854495Swnj 
28633743Skarels 		/*
28733743Skarels 		 * If datagram marked as having more fragments
28833743Skarels 		 * or if this is not the first fragment,
28933743Skarels 		 * attempt reassembly; if it succeeds, proceed.
29033743Skarels 		 */
29133743Skarels 		if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
29233743Skarels 			ipstat.ips_fragments++;
29333743Skarels 			ip = ip_reass((struct ipasfrag *)ip, fp);
29433743Skarels 			if (ip == 0)
29533743Skarels 				goto next;
29639185Ssklower 			else
29739185Ssklower 				ipstat.ips_reassembled++;
29833743Skarels 			m = dtom(ip);
29933743Skarels 		} else
30033743Skarels 			if (fp)
30133743Skarels 				ip_freef(fp);
3024640Swnj 	} else
30333743Skarels 		ip->ip_len -= hlen;
3044951Swnj 
3054951Swnj 	/*
3064951Swnj 	 * Switch out to protocol's input routine.
3074951Swnj 	 */
30839185Ssklower 	ipstat.ips_delivered++;
30937319Skarels 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
3105084Swnj 	goto next;
3114951Swnj bad:
3124951Swnj 	m_freem(m);
3135084Swnj 	goto next;
3144640Swnj }
3154495Swnj 
3164640Swnj /*
3174640Swnj  * Take incoming datagram fragment and try to
3184951Swnj  * reassemble it into whole datagram.  If a chain for
3194640Swnj  * reassembly of this datagram already exists, then it
3204640Swnj  * is given as fp; otherwise have to make a chain.
3214640Swnj  */
3224640Swnj struct ip *
3234640Swnj ip_reass(ip, fp)
3244898Swnj 	register struct ipasfrag *ip;
3254640Swnj 	register struct ipq *fp;
3264640Swnj {
3274640Swnj 	register struct mbuf *m = dtom(ip);
3284898Swnj 	register struct ipasfrag *q;
3294640Swnj 	struct mbuf *t;
3304640Swnj 	int hlen = ip->ip_hl << 2;
3314640Swnj 	int i, next;
3324543Swnj 
3334640Swnj 	/*
3344640Swnj 	 * Presence of header sizes in mbufs
3354640Swnj 	 * would confuse code below.
3364640Swnj 	 */
33737319Skarels 	m->m_data += hlen;
3384640Swnj 	m->m_len -= hlen;
3394495Swnj 
3404640Swnj 	/*
3414640Swnj 	 * If first fragment to arrive, create a reassembly queue.
3424640Swnj 	 */
3434640Swnj 	if (fp == 0) {
34431201Skarels 		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
3454640Swnj 			goto dropfrag;
3464640Swnj 		fp = mtod(t, struct ipq *);
3474640Swnj 		insque(fp, &ipq);
3484640Swnj 		fp->ipq_ttl = IPFRAGTTL;
3494640Swnj 		fp->ipq_p = ip->ip_p;
3504640Swnj 		fp->ipq_id = ip->ip_id;
3514898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
3524898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
3534898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
3545161Swnj 		q = (struct ipasfrag *)fp;
3555161Swnj 		goto insert;
3564640Swnj 	}
3574495Swnj 
3584640Swnj 	/*
3594640Swnj 	 * Find a segment which begins after this one does.
3604640Swnj 	 */
3614898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
3624640Swnj 		if (q->ip_off > ip->ip_off)
3634640Swnj 			break;
3644495Swnj 
3654640Swnj 	/*
3664640Swnj 	 * If there is a preceding segment, it may provide some of
3674640Swnj 	 * our data already.  If so, drop the data from the incoming
3684640Swnj 	 * segment.  If it provides all of our data, drop us.
3694640Swnj 	 */
3704898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
3714898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
3724640Swnj 		if (i > 0) {
3734640Swnj 			if (i >= ip->ip_len)
3744640Swnj 				goto dropfrag;
3754640Swnj 			m_adj(dtom(ip), i);
3764640Swnj 			ip->ip_off += i;
3774640Swnj 			ip->ip_len -= i;
3784640Swnj 		}
3794640Swnj 	}
3804543Swnj 
3814640Swnj 	/*
3824640Swnj 	 * While we overlap succeeding segments trim them or,
3834640Swnj 	 * if they are completely covered, dequeue them.
3844640Swnj 	 */
3854898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
3864640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
3874640Swnj 		if (i < q->ip_len) {
3884640Swnj 			q->ip_len -= i;
3896256Sroot 			q->ip_off += i;
3904640Swnj 			m_adj(dtom(q), i);
3914640Swnj 			break;
3924495Swnj 		}
3934898Swnj 		q = q->ipf_next;
3944898Swnj 		m_freem(dtom(q->ipf_prev));
3954898Swnj 		ip_deq(q->ipf_prev);
3964543Swnj 	}
3974495Swnj 
3985161Swnj insert:
3994640Swnj 	/*
4004640Swnj 	 * Stick new segment in its place;
4014640Swnj 	 * check for complete reassembly.
4024640Swnj 	 */
4034898Swnj 	ip_enq(ip, q->ipf_prev);
4044640Swnj 	next = 0;
4054898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
4064640Swnj 		if (q->ip_off != next)
4074640Swnj 			return (0);
4084640Swnj 		next += q->ip_len;
4094640Swnj 	}
4104898Swnj 	if (q->ipf_prev->ipf_mff)
4114640Swnj 		return (0);
4124495Swnj 
4134640Swnj 	/*
4144640Swnj 	 * Reassembly is complete; concatenate fragments.
4154640Swnj 	 */
4164640Swnj 	q = fp->ipq_next;
4174640Swnj 	m = dtom(q);
4184640Swnj 	t = m->m_next;
4194640Swnj 	m->m_next = 0;
4204640Swnj 	m_cat(m, t);
4216298Swnj 	q = q->ipf_next;
4226298Swnj 	while (q != (struct ipasfrag *)fp) {
4236298Swnj 		t = dtom(q);
4246298Swnj 		q = q->ipf_next;
4256298Swnj 		m_cat(m, t);
4266298Swnj 	}
4274495Swnj 
4284640Swnj 	/*
4294640Swnj 	 * Create header for new ip packet by
4304640Swnj 	 * modifying header of first packet;
4314640Swnj 	 * dequeue and discard fragment reassembly header.
4324640Swnj 	 * Make header visible.
4334640Swnj 	 */
4344640Swnj 	ip = fp->ipq_next;
4354640Swnj 	ip->ip_len = next;
4364898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
4374898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
4384640Swnj 	remque(fp);
4394907Swnj 	(void) m_free(dtom(fp));
4404640Swnj 	m = dtom(ip);
44124813Skarels 	m->m_len += (ip->ip_hl << 2);
44237319Skarels 	m->m_data -= (ip->ip_hl << 2);
4434898Swnj 	return ((struct ip *)ip);
4444495Swnj 
4454640Swnj dropfrag:
44624813Skarels 	ipstat.ips_fragdropped++;
4474640Swnj 	m_freem(m);
4484640Swnj 	return (0);
4494495Swnj }
4504495Swnj 
4514640Swnj /*
4524640Swnj  * Free a fragment reassembly header and all
4534640Swnj  * associated datagrams.
4544640Swnj  */
4554640Swnj ip_freef(fp)
4564640Swnj 	struct ipq *fp;
4574495Swnj {
45810735Ssam 	register struct ipasfrag *q, *p;
4594495Swnj 
46010735Ssam 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {
46110735Ssam 		p = q->ipf_next;
46210735Ssam 		ip_deq(q);
4634640Swnj 		m_freem(dtom(q));
46410735Ssam 	}
46510735Ssam 	remque(fp);
46610735Ssam 	(void) m_free(dtom(fp));
4674495Swnj }
4684495Swnj 
4694640Swnj /*
4704640Swnj  * Put an ip fragment on a reassembly chain.
4714640Swnj  * Like insque, but pointers in middle of structure.
4724640Swnj  */
4734640Swnj ip_enq(p, prev)
4744898Swnj 	register struct ipasfrag *p, *prev;
4754495Swnj {
4764951Swnj 
4774898Swnj 	p->ipf_prev = prev;
4784898Swnj 	p->ipf_next = prev->ipf_next;
4794898Swnj 	prev->ipf_next->ipf_prev = p;
4804898Swnj 	prev->ipf_next = p;
4814495Swnj }
4824495Swnj 
4834640Swnj /*
4844640Swnj  * To ip_enq as remque is to insque.
4854640Swnj  */
4864640Swnj ip_deq(p)
4874898Swnj 	register struct ipasfrag *p;
4884640Swnj {
4894951Swnj 
4904898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
4914898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
4924495Swnj }
4934495Swnj 
4944640Swnj /*
4954640Swnj  * IP timer processing;
4964640Swnj  * if a timer expires on a reassembly
4974640Swnj  * queue, discard it.
4984640Swnj  */
4994801Swnj ip_slowtimo()
5004495Swnj {
5014495Swnj 	register struct ipq *fp;
5024640Swnj 	int s = splnet();
5034951Swnj 
5045243Sroot 	fp = ipq.next;
5055243Sroot 	if (fp == 0) {
5065243Sroot 		splx(s);
5075243Sroot 		return;
5085243Sroot 	}
50910735Ssam 	while (fp != &ipq) {
51010735Ssam 		--fp->ipq_ttl;
51110735Ssam 		fp = fp->next;
51224813Skarels 		if (fp->prev->ipq_ttl == 0) {
51324813Skarels 			ipstat.ips_fragtimeout++;
51410735Ssam 			ip_freef(fp->prev);
51524813Skarels 		}
51610735Ssam 	}
5174640Swnj 	splx(s);
5184495Swnj }
5194495Swnj 
5204951Swnj /*
5214951Swnj  * Drain off all datagram fragments.
5224951Swnj  */
5234801Swnj ip_drain()
5244801Swnj {
5254801Swnj 
52624813Skarels 	while (ipq.next != &ipq) {
52724813Skarels 		ipstat.ips_fragdropped++;
52810735Ssam 		ip_freef(ipq.next);
52924813Skarels 	}
5304801Swnj }
5314923Swnj 
53230925Skarels extern struct in_ifaddr *ifptoia();
53324813Skarels struct in_ifaddr *ip_rtaddr();
53424813Skarels 
5354640Swnj /*
5364640Swnj  * Do option processing on a datagram,
53740689Skarels  * possibly discarding it if bad options are encountered,
53840689Skarels  * or forwarding it if source-routed.
53940689Skarels  * Returns 1 if packet has been forwarded/freed,
54040689Skarels  * 0 if the packet should be processed further.
5414640Swnj  */
54237319Skarels ip_dooptions(m)
54336814Skarels 	struct mbuf *m;
5444495Swnj {
54536814Skarels 	register struct ip *ip = mtod(m, struct ip *);
5464640Swnj 	register u_char *cp;
54724813Skarels 	register struct ip_timestamp *ipt;
54824813Skarels 	register struct in_ifaddr *ia;
54936814Skarels 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
5504923Swnj 	struct in_addr *sin;
55124813Skarels 	n_time ntime;
5524495Swnj 
5534640Swnj 	cp = (u_char *)(ip + 1);
5544640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
5554640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
55624813Skarels 		opt = cp[IPOPT_OPTVAL];
5574640Swnj 		if (opt == IPOPT_EOL)
5584640Swnj 			break;
5594640Swnj 		if (opt == IPOPT_NOP)
5604640Swnj 			optlen = 1;
56116392Ssam 		else {
56224813Skarels 			optlen = cp[IPOPT_OLEN];
56324813Skarels 			if (optlen <= 0 || optlen > cnt) {
56424813Skarels 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
56517551Skarels 				goto bad;
56624813Skarels 			}
56716392Ssam 		}
5684640Swnj 		switch (opt) {
5694495Swnj 
5704640Swnj 		default:
5714640Swnj 			break;
5724495Swnj 
5734951Swnj 		/*
5744951Swnj 		 * Source routing with record.
5754951Swnj 		 * Find interface with current destination address.
5764951Swnj 		 * If none on this machine then drop if strictly routed,
5774951Swnj 		 * or do nothing if loosely routed.
5784951Swnj 		 * Record interface address and bring up next address
5794951Swnj 		 * component.  If strictly routed make sure next
58040689Skarels 		 * address is on directly accessible net.
5814951Swnj 		 */
5824640Swnj 		case IPOPT_LSRR:
5837508Sroot 		case IPOPT_SSRR:
58424813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
58524813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
58624813Skarels 				goto bad;
58724813Skarels 			}
58824813Skarels 			ipaddr.sin_addr = ip->ip_dst;
58924813Skarels 			ia = (struct in_ifaddr *)
59024813Skarels 				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
59124813Skarels 			if (ia == 0) {
59224813Skarels 				if (opt == IPOPT_SSRR) {
59324813Skarels 					type = ICMP_UNREACH;
59424813Skarels 					code = ICMP_UNREACH_SRCFAIL;
5954951Swnj 					goto bad;
59624813Skarels 				}
59724813Skarels 				/*
59824813Skarels 				 * Loose routing, and not at next destination
59924813Skarels 				 * yet; nothing to do except forward.
60024813Skarels 				 */
6014951Swnj 				break;
6024640Swnj 			}
60324813Skarels 			off--;			/* 0 origin */
60424813Skarels 			if (off > optlen - sizeof(struct in_addr)) {
60524813Skarels 				/*
60624813Skarels 				 * End of source route.  Should be for us.
60724813Skarels 				 */
60824813Skarels 				save_rte(cp, ip->ip_src);
6094951Swnj 				break;
61024813Skarels 			}
61124813Skarels 			/*
61224813Skarels 			 * locate outgoing interface
61324813Skarels 			 */
61426384Skarels 			bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
61524813Skarels 			    sizeof(ipaddr.sin_addr));
61640689Skarels 			if (opt == IPOPT_SSRR) {
61740689Skarels #define	INA	struct in_ifaddr *
61840689Skarels #define	SA	struct sockaddr *
61940689Skarels 			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
62040689Skarels 				ia = in_iaonnetof(in_netof(ipaddr.sin_addr));
62140689Skarels 			} else
62240689Skarels 				ia = ip_rtaddr(ipaddr.sin_addr);
62340689Skarels 			if (ia == 0) {
62424813Skarels 				type = ICMP_UNREACH;
62524813Skarels 				code = ICMP_UNREACH_SRCFAIL;
6264951Swnj 				goto bad;
62724813Skarels 			}
62824813Skarels 			ip->ip_dst = ipaddr.sin_addr;
62926384Skarels 			bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
63026384Skarels 			    (caddr_t)(cp + off), sizeof(struct in_addr));
63124813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
63236814Skarels 			forward = 1;
6334640Swnj 			break;
6344495Swnj 
63524813Skarels 		case IPOPT_RR:
63624813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
63724813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
63824813Skarels 				goto bad;
63924813Skarels 			}
64024813Skarels 			/*
64124813Skarels 			 * If no space remains, ignore.
64224813Skarels 			 */
64324813Skarels 			off--;			/* 0 origin */
64424813Skarels 			if (off > optlen - sizeof(struct in_addr))
64524813Skarels 				break;
64631393Skarels 			bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
64724813Skarels 			    sizeof(ipaddr.sin_addr));
64824813Skarels 			/*
64937319Skarels 			 * locate outgoing interface; if we're the destination,
65037319Skarels 			 * use the incoming interface (should be same).
65124813Skarels 			 */
65240689Skarels 			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
65337319Skarels 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
65424813Skarels 				type = ICMP_UNREACH;
65532113Skarels 				code = ICMP_UNREACH_HOST;
65624813Skarels 				goto bad;
65724813Skarels 			}
65826384Skarels 			bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
65926384Skarels 			    (caddr_t)(cp + off), sizeof(struct in_addr));
66024813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
66124813Skarels 			break;
66224813Skarels 
6634640Swnj 		case IPOPT_TS:
6646583Ssam 			code = cp - (u_char *)ip;
6654801Swnj 			ipt = (struct ip_timestamp *)cp;
6664801Swnj 			if (ipt->ipt_len < 5)
6674640Swnj 				goto bad;
6684801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
6694801Swnj 				if (++ipt->ipt_oflw == 0)
6704640Swnj 					goto bad;
6714495Swnj 				break;
6724640Swnj 			}
67330925Skarels 			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
6744801Swnj 			switch (ipt->ipt_flg) {
6754495Swnj 
6764640Swnj 			case IPOPT_TS_TSONLY:
6774640Swnj 				break;
6784640Swnj 
6794640Swnj 			case IPOPT_TS_TSANDADDR:
68024813Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
68124813Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
6824640Swnj 					goto bad;
68337319Skarels 				ia = ifptoia(m->m_pkthdr.rcvif);
68430925Skarels 				bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
68524813Skarels 				    (caddr_t)sin, sizeof(struct in_addr));
68630925Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
6874640Swnj 				break;
6884640Swnj 
6894640Swnj 			case IPOPT_TS_PRESPEC:
69030925Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
69130925Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
69230925Skarels 					goto bad;
69324813Skarels 				bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
69424813Skarels 				    sizeof(struct in_addr));
69540689Skarels 				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
6964951Swnj 					continue;
69724813Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
6984640Swnj 				break;
6994640Swnj 
7004495Swnj 			default:
7014640Swnj 				goto bad;
7024495Swnj 			}
70324813Skarels 			ntime = iptime();
70430925Skarels 			bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
70530925Skarels 			    sizeof(n_time));
70624813Skarels 			ipt->ipt_ptr += sizeof(n_time);
7074640Swnj 		}
7084495Swnj 	}
70936814Skarels 	if (forward) {
71040689Skarels 		ip_forward(m, 1);
71136814Skarels 		return (1);
71236814Skarels 	} else
71336814Skarels 		return (0);
7144640Swnj bad:
71537319Skarels 	icmp_error(m, type, code);
7166583Ssam 	return (1);
7174495Swnj }
7184495Swnj 
7194640Swnj /*
72024813Skarels  * Given address of next destination (final or next hop),
72124813Skarels  * return internet address info of interface to be used to get there.
72224813Skarels  */
72324813Skarels struct in_ifaddr *
72424813Skarels ip_rtaddr(dst)
72524813Skarels 	 struct in_addr dst;
72624813Skarels {
72724813Skarels 	register struct sockaddr_in *sin;
72824813Skarels 
72924813Skarels 	sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
73024813Skarels 
73124813Skarels 	if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
73224813Skarels 		if (ipforward_rt.ro_rt) {
73324813Skarels 			RTFREE(ipforward_rt.ro_rt);
73424813Skarels 			ipforward_rt.ro_rt = 0;
73524813Skarels 		}
73624813Skarels 		sin->sin_family = AF_INET;
73737319Skarels 		sin->sin_len = sizeof(*sin);
73824813Skarels 		sin->sin_addr = dst;
73924813Skarels 
74024813Skarels 		rtalloc(&ipforward_rt);
74124813Skarels 	}
74224813Skarels 	if (ipforward_rt.ro_rt == 0)
74324813Skarels 		return ((struct in_ifaddr *)0);
74440689Skarels 	return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
74524813Skarels }
74624813Skarels 
74724813Skarels /*
74824813Skarels  * Save incoming source route for use in replies,
74924813Skarels  * to be picked up later by ip_srcroute if the receiver is interested.
75024813Skarels  */
75124813Skarels save_rte(option, dst)
75226384Skarels 	u_char *option;
75324813Skarels 	struct in_addr dst;
75424813Skarels {
75526384Skarels 	unsigned olen;
75624813Skarels 
75724813Skarels 	olen = option[IPOPT_OLEN];
75840689Skarels #ifdef DEBUG
75936814Skarels 	if (ipprintfs)
76036814Skarels 		printf("save_rte: olen %d\n", olen);
76140689Skarels #endif
76236814Skarels 	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
76324813Skarels 		return;
76426384Skarels 	bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
76524813Skarels 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
76636814Skarels 	ip_srcrt.dst = dst;
76724813Skarels }
76824813Skarels 
76924813Skarels /*
77024813Skarels  * Retrieve incoming source route for use in replies,
77124813Skarels  * in the same form used by setsockopt.
77224813Skarels  * The first hop is placed before the options, will be removed later.
77324813Skarels  */
77424813Skarels struct mbuf *
77524813Skarels ip_srcroute()
77624813Skarels {
77724813Skarels 	register struct in_addr *p, *q;
77824813Skarels 	register struct mbuf *m;
77924813Skarels 
78024813Skarels 	if (ip_nhops == 0)
78124813Skarels 		return ((struct mbuf *)0);
78231201Skarels 	m = m_get(M_DONTWAIT, MT_SOOPTS);
78331201Skarels 	if (m == 0)
78431201Skarels 		return ((struct mbuf *)0);
78524813Skarels 
78636814Skarels #define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
78736814Skarels 
78836814Skarels 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
78936814Skarels 	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
79036814Skarels 	    OPTSIZ;
79140689Skarels #ifdef DEBUG
79236814Skarels 	if (ipprintfs)
79336814Skarels 		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
79440689Skarels #endif
79536814Skarels 
79624813Skarels 	/*
79724813Skarels 	 * First save first hop for return route
79824813Skarels 	 */
79924813Skarels 	p = &ip_srcrt.route[ip_nhops - 1];
80024813Skarels 	*(mtod(m, struct in_addr *)) = *p--;
80140689Skarels #ifdef DEBUG
80236814Skarels 	if (ipprintfs)
80336814Skarels 		printf(" hops %X", ntohl(*mtod(m, struct in_addr *)));
80440689Skarels #endif
80524813Skarels 
80624813Skarels 	/*
80724813Skarels 	 * Copy option fields and padding (nop) to mbuf.
80824813Skarels 	 */
80924813Skarels 	ip_srcrt.nop = IPOPT_NOP;
81036814Skarels 	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
81136814Skarels 	bcopy((caddr_t)&ip_srcrt.nop,
81236814Skarels 	    mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
81324813Skarels 	q = (struct in_addr *)(mtod(m, caddr_t) +
81436814Skarels 	    sizeof(struct in_addr) + OPTSIZ);
81536814Skarels #undef OPTSIZ
81624813Skarels 	/*
81724813Skarels 	 * Record return path as an IP source route,
81824813Skarels 	 * reversing the path (pointers are now aligned).
81924813Skarels 	 */
82036814Skarels 	while (p >= ip_srcrt.route) {
82140689Skarels #ifdef DEBUG
82236814Skarels 		if (ipprintfs)
82336814Skarels 			printf(" %X", ntohl(*q));
82440689Skarels #endif
82524813Skarels 		*q++ = *p--;
82636814Skarels 	}
82736814Skarels 	/*
82836814Skarels 	 * Last hop goes to final destination.
82936814Skarels 	 */
83036814Skarels 	*q = ip_srcrt.dst;
83140689Skarels #ifdef DEBUG
83236814Skarels 	if (ipprintfs)
83336814Skarels 		printf(" %X\n", ntohl(*q));
83440689Skarels #endif
83524813Skarels 	return (m);
83624813Skarels }
83724813Skarels 
83824813Skarels /*
8394951Swnj  * Strip out IP options, at higher
8404951Swnj  * level protocol in the kernel.
8414951Swnj  * Second argument is buffer to which options
8424951Swnj  * will be moved, and return value is their length.
84336814Skarels  * XXX should be deleted; last arg currently ignored.
8444640Swnj  */
84537319Skarels ip_stripoptions(m, mopt)
84637319Skarels 	register struct mbuf *m;
8475217Swnj 	struct mbuf *mopt;
8484495Swnj {
8494640Swnj 	register int i;
85037319Skarels 	struct ip *ip = mtod(m, struct ip *);
85124813Skarels 	register caddr_t opts;
8524640Swnj 	int olen;
8534640Swnj 
8544640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
85524813Skarels 	opts = (caddr_t)(ip + 1);
8564640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
85724813Skarels 	bcopy(opts  + olen, opts, (unsigned)i);
8585243Sroot 	m->m_len -= olen;
85937319Skarels 	if (m->m_flags & M_PKTHDR)
86037319Skarels 		m->m_pkthdr.len -= olen;
86124813Skarels 	ip->ip_hl = sizeof(struct ip) >> 2;
8624495Swnj }
8636583Ssam 
86414670Ssam u_char inetctlerrmap[PRC_NCMDS] = {
86524813Skarels 	0,		0,		0,		0,
86640689Skarels 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
86740689Skarels 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
86824813Skarels 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
86924813Skarels 	0,		0,		0,		0,
87024813Skarels 	ENOPROTOOPT
8716583Ssam };
8726583Ssam 
8736583Ssam /*
8746583Ssam  * Forward a packet.  If some error occurs return the sender
87518376Skarels  * an icmp packet.  Note we can't always generate a meaningful
87624813Skarels  * icmp message because icmp doesn't have a large enough repertoire
8776583Ssam  * of codes and types.
87826308Skarels  *
87940689Skarels  * If not forwarding, just drop the packet.  This could be confusing
88040689Skarels  * if ipforwarding was zero but some routing protocol was advancing
88140689Skarels  * us as a gateway to somewhere.  However, we must let the routing
88240689Skarels  * protocol deal with that.
88340689Skarels  *
88440689Skarels  * The srcrt parameter indicates whether the packet is being forwarded
88540689Skarels  * via a source route.
8866583Ssam  */
88740689Skarels ip_forward(m, srcrt)
88836814Skarels 	struct mbuf *m;
88940689Skarels 	int srcrt;
8906583Ssam {
89136814Skarels 	register struct ip *ip = mtod(m, struct ip *);
89224813Skarels 	register struct sockaddr_in *sin;
89340689Skarels 	register struct rtentry *rt;
89440689Skarels 	int error, type = 0, code;
89518376Skarels 	struct mbuf *mcopy;
89624813Skarels 	struct in_addr dest;
8976583Ssam 
89824813Skarels 	dest.s_addr = 0;
89940689Skarels #ifdef DEBUG
9006583Ssam 	if (ipprintfs)
9016583Ssam 		printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
9026583Ssam 			ip->ip_dst, ip->ip_ttl);
90340689Skarels #endif
90437319Skarels 	if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
90526308Skarels 		ipstat.ips_cantforward++;
90637319Skarels 		m_freem(m);
90726308Skarels 		return;
9086583Ssam 	}
90940689Skarels 	HTONS(ip->ip_id);
91031393Skarels 	if (ip->ip_ttl <= IPTTLDEC) {
91140689Skarels 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest);
91240689Skarels 		return;
9136583Ssam 	}
9146583Ssam 	ip->ip_ttl -= IPTTLDEC;
9156609Ssam 
91624813Skarels 	sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
91740689Skarels 	if ((rt = ipforward_rt.ro_rt) == 0 ||
91824813Skarels 	    ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
91924813Skarels 		if (ipforward_rt.ro_rt) {
92024813Skarels 			RTFREE(ipforward_rt.ro_rt);
92124813Skarels 			ipforward_rt.ro_rt = 0;
92224813Skarels 		}
92324813Skarels 		sin->sin_family = AF_INET;
92437319Skarels 		sin->sin_len = sizeof(*sin);
92524813Skarels 		sin->sin_addr = ip->ip_dst;
92624813Skarels 
92724813Skarels 		rtalloc(&ipforward_rt);
92840689Skarels 		if (ipforward_rt.ro_rt == 0) {
92940689Skarels 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest);
93040689Skarels 			return;
93140689Skarels 		}
93240689Skarels 		rt = ipforward_rt.ro_rt;
93324813Skarels 	}
93440689Skarels 
93524813Skarels 	/*
93640689Skarels 	 * Save at most 64 bytes of the packet in case
93740689Skarels 	 * we need to generate an ICMP message to the src.
93840689Skarels 	 */
93940689Skarels 	mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
94040689Skarels 
94140689Skarels #ifdef GATEWAY
94240689Skarels 	ip_ifmatrix[rt->rt_ifp->if_index +
94340689Skarels 	     if_index * m->m_pkthdr.rcvif->if_index]++;
94440689Skarels #endif
94540689Skarels 	/*
94624813Skarels 	 * If forwarding packet using same interface that it came in on,
94724813Skarels 	 * perhaps should send a redirect to sender to shortcut a hop.
94824813Skarels 	 * Only send redirect if source is sending directly to us,
94924813Skarels 	 * and if packet was not source routed (or has any options).
95030447Skarels 	 * Also, don't send redirect if forwarding using a default route
95140689Skarels 	 * or a route modified by a redirect.
95224813Skarels 	 */
95330447Skarels #define	satosin(sa)	((struct sockaddr_in *)(sa))
95440689Skarels 	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
95540689Skarels 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
95640689Skarels 	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
95740689Skarels 	    ipsendredirects && !srcrt) {
95824813Skarels 		struct in_ifaddr *ia;
95924813Skarels 		u_long src = ntohl(ip->ip_src.s_addr);
96024813Skarels 		u_long dst = ntohl(ip->ip_dst.s_addr);
96124813Skarels 
96237319Skarels 		if ((ia = ifptoia(m->m_pkthdr.rcvif)) &&
96324813Skarels 		   (src & ia->ia_subnetmask) == ia->ia_subnet) {
96440689Skarels 		    if (rt->rt_flags & RTF_GATEWAY)
96540689Skarels 			dest = satosin(rt->rt_gateway)->sin_addr;
96624813Skarels 		    else
96724813Skarels 			dest = ip->ip_dst;
96824813Skarels 		    /*
96924813Skarels 		     * If the destination is reached by a route to host,
97027145Skarels 		     * is on a subnet of a local net, or is directly
97127145Skarels 		     * on the attached net (!), use host redirect.
97224813Skarels 		     * (We may be the correct first hop for other subnets.)
97324813Skarels 		     */
97440689Skarels #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
97524813Skarels 		    type = ICMP_REDIRECT;
97640689Skarels 		    if ((rt->rt_flags & RTF_HOST) ||
97740689Skarels 		        (rt->rt_flags & RTF_GATEWAY) == 0)
97840689Skarels 			    code = ICMP_REDIRECT_HOST;
97940689Skarels 		    else if (RTA(rt)->ia_subnetmask != RTA(rt)->ia_netmask &&
98040689Skarels 		        (dst & RTA(rt)->ia_netmask) ==  RTA(rt)->ia_net)
98140689Skarels 			    code = ICMP_REDIRECT_HOST;
98240689Skarels 		    else
98340689Skarels 			    code = ICMP_REDIRECT_NET;
98440689Skarels #ifdef DEBUG
98524813Skarels 		    if (ipprintfs)
98640689Skarels 		        printf("redirect (%d) to %x\n", code, dest.s_addr);
98740689Skarels #endif
98824813Skarels 		}
98924813Skarels 	}
99024813Skarels 
99137319Skarels 	error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING);
99224813Skarels 	if (error)
99324813Skarels 		ipstat.ips_cantforward++;
99424813Skarels 	else {
99521117Skarels 		ipstat.ips_forward++;
99640689Skarels 		if (type)
99740689Skarels 			ipstat.ips_redirectsent++;
99840689Skarels 		else {
99940689Skarels 			if (mcopy)
100040689Skarels 				m_freem(mcopy);
100140689Skarels 			return;
100240689Skarels 		}
10036609Ssam 	}
100411540Ssam 	if (mcopy == NULL)
100511540Ssam 		return;
10066609Ssam 	switch (error) {
10076609Ssam 
100824813Skarels 	case 0:				/* forwarded, but need redirect */
100940689Skarels 		/* type, code set above */
101024813Skarels 		break;
101124813Skarels 
101240689Skarels 	case ENETUNREACH:		/* shouldn't happen, checked above */
101340689Skarels 	case EHOSTUNREACH:
10146609Ssam 	case ENETDOWN:
101540689Skarels 	case EHOSTDOWN:
101640689Skarels 	default:
101740689Skarels 		type = ICMP_UNREACH;
101840689Skarels 		code = ICMP_UNREACH_HOST;
10196609Ssam 		break;
10206609Ssam 
10216609Ssam 	case EMSGSIZE:
102240689Skarels 		type = ICMP_UNREACH;
10236583Ssam 		code = ICMP_UNREACH_NEEDFRAG;
102439185Ssklower 		ipstat.ips_cantfrag++;
10256609Ssam 		break;
10266609Ssam 
10276609Ssam 	case ENOBUFS:
10286609Ssam 		type = ICMP_SOURCEQUENCH;
102937319Skarels 		code = 0;
10306609Ssam 		break;
10316609Ssam 	}
103238795Skarels 	icmp_error(mcopy, type, code, dest);
10336583Ssam }
1034