xref: /csrg-svn/sys/netinet/ip_input.c (revision 40689)
123184Smckusick /*
236814Skarels  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
332787Sbostic  * All rights reserved.
423184Smckusick  *
532787Sbostic  * Redistribution and use in source and binary forms are permitted
634854Sbostic  * provided that the above copyright notice and this paragraph are
734854Sbostic  * duplicated in all such forms and that any documentation,
834854Sbostic  * advertising materials, and other materials related to such
934854Sbostic  * distribution and use acknowledge that the software was developed
1034854Sbostic  * by the University of California, Berkeley.  The name of the
1134854Sbostic  * University may not be used to endorse or promote products derived
1234854Sbostic  * from this software without specific prior written permission.
1334854Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434854Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534854Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1632787Sbostic  *
17*40689Skarels  *	@(#)ip_input.c	7.15 (Berkeley) 04/03/90
1823184Smckusick  */
194571Swnj 
2017060Sbloom #include "param.h"
2117060Sbloom #include "systm.h"
2237319Skarels #include "malloc.h"
2317060Sbloom #include "mbuf.h"
2417060Sbloom #include "domain.h"
2517060Sbloom #include "protosw.h"
2617060Sbloom #include "socket.h"
2717060Sbloom #include "errno.h"
2817060Sbloom #include "time.h"
2917060Sbloom #include "kernel.h"
308695Sroot 
318695Sroot #include "../net/if.h"
328695Sroot #include "../net/route.h"
3310892Ssam 
3417060Sbloom #include "in.h"
35*40689Skarels #include "in_systm.h"
36*40689Skarels #include "ip.h"
3717060Sbloom #include "in_pcb.h"
3818376Skarels #include "in_var.h"
3917060Sbloom #include "ip_var.h"
4017060Sbloom #include "ip_icmp.h"
414495Swnj 
4236814Skarels #ifndef	IPFORWARDING
4336814Skarels #ifdef GATEWAY
44*40689Skarels #define	IPFORWARDING	1	/* forward IP packets not for us */
4536814Skarels #else /* GATEWAY */
46*40689Skarels #define	IPFORWARDING	0	/* don't forward IP packets not for us */
4736814Skarels #endif /* GATEWAY */
4836814Skarels #endif /* IPFORWARDING */
4936814Skarels #ifndef	IPSENDREDIRECTS
5036814Skarels #define	IPSENDREDIRECTS	1
5136814Skarels #endif
5236814Skarels int	ipforwarding = IPFORWARDING;
5336814Skarels int	ipsendredirects = IPSENDREDIRECTS;
54*40689Skarels #ifdef DEBUG
55*40689Skarels int	ipprintfs = 0;
56*40689Skarels #endif
5736814Skarels 
584898Swnj u_char	ip_protox[IPPROTO_MAX];
596210Swnj int	ipqmaxlen = IFQ_MAXLEN;
6018376Skarels struct	in_ifaddr *in_ifaddr;			/* first inet address */
614898Swnj 
624801Swnj /*
6324813Skarels  * We need to save the IP options in case a protocol wants to respond
6424813Skarels  * to an incoming packet over the same route if the packet got here
6524813Skarels  * using IP source routing.  This allows connection establishment and
6624813Skarels  * maintenance when the remote end is on a network that is not known
6724813Skarels  * to us.
6824813Skarels  */
6924813Skarels int	ip_nhops = 0;
7024813Skarels static	struct ip_srcrt {
7136814Skarels 	struct	in_addr dst;			/* final destination */
7224813Skarels 	char	nop;				/* one NOP to align */
7324813Skarels 	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
7436814Skarels 	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
7524813Skarels } ip_srcrt;
7624813Skarels 
77*40689Skarels #ifdef GATEWAY
78*40689Skarels extern	int if_index;
79*40689Skarels u_long	*ip_ifmatrix;
80*40689Skarels #endif
81*40689Skarels 
8224813Skarels /*
835172Swnj  * IP initialization: fill in IP protocol switch table.
845161Swnj  * All protocols not implemented in kernel go to raw IP protocol handler.
854801Swnj  */
864801Swnj ip_init()
874801Swnj {
884898Swnj 	register struct protosw *pr;
894898Swnj 	register int i;
904495Swnj 
9124813Skarels 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
924898Swnj 	if (pr == 0)
934898Swnj 		panic("ip_init");
944898Swnj 	for (i = 0; i < IPPROTO_MAX; i++)
959030Sroot 		ip_protox[i] = pr - inetsw;
969030Sroot 	for (pr = inetdomain.dom_protosw;
9717551Skarels 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
9816990Skarels 		if (pr->pr_domain->dom_family == PF_INET &&
994898Swnj 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
1009030Sroot 			ip_protox[pr->pr_protocol] = pr - inetsw;
1014801Swnj 	ipq.next = ipq.prev = &ipq;
1028172Sroot 	ip_id = time.tv_sec & 0xffff;
1036210Swnj 	ipintrq.ifq_maxlen = ipqmaxlen;
104*40689Skarels #ifdef GATEWAY
105*40689Skarels 	i = (if_index + 1) * (if_index + 1) * sizeof (u_long);
106*40689Skarels 	if ((ip_ifmatrix = (u_long *) malloc(i, M_RTABLE, M_WAITOK)) == 0)
107*40689Skarels 		panic("no memory for ip_ifmatrix");
108*40689Skarels #endif
1094801Swnj }
1104801Swnj 
1114640Swnj struct	ip *ip_reass();
11237319Skarels struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
11324813Skarels struct	route ipforward_rt;
1144640Swnj 
1154640Swnj /*
1164640Swnj  * Ip input routine.  Checksum and byte swap header.  If fragmented
117*40689Skarels  * try to reassemble.  Process options.  Pass to next level.
1184640Swnj  */
1195084Swnj ipintr()
1204495Swnj {
1214923Swnj 	register struct ip *ip;
1225084Swnj 	register struct mbuf *m;
1234495Swnj 	register struct ipq *fp;
12418376Skarels 	register struct in_ifaddr *ia;
1255084Swnj 	int hlen, s;
1264495Swnj 
1275084Swnj next:
1284640Swnj 	/*
1295084Swnj 	 * Get next datagram off input queue and get IP header
1305084Swnj 	 * in first mbuf.
1314640Swnj 	 */
1325084Swnj 	s = splimp();
13337319Skarels 	IF_DEQUEUE(&ipintrq, m);
1345084Swnj 	splx(s);
1355218Swnj 	if (m == 0)
1365084Swnj 		return;
13737319Skarels if ((m->m_flags & M_PKTHDR) == 0)
13837319Skarels panic("ipintr no HDR");
13926001Skarels 	/*
14026001Skarels 	 * If no IP addresses have been set yet but the interfaces
14126001Skarels 	 * are receiving, can't do anything with incoming packets yet.
14226001Skarels 	 */
14326001Skarels 	if (in_ifaddr == NULL)
14426001Skarels 		goto bad;
14525920Skarels 	ipstat.ips_total++;
146*40689Skarels 	if (m->m_len < sizeof (struct ip) &&
14711232Ssam 	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
14811232Ssam 		ipstat.ips_toosmall++;
14911232Ssam 		goto next;
15011232Ssam 	}
1514640Swnj 	ip = mtod(m, struct ip *);
15218376Skarels 	hlen = ip->ip_hl << 2;
15324813Skarels 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
15418376Skarels 		ipstat.ips_badhlen++;
15521117Skarels 		goto bad;
15618376Skarels 	}
15718376Skarels 	if (hlen > m->m_len) {
15811232Ssam 		if ((m = m_pullup(m, hlen)) == 0) {
15911232Ssam 			ipstat.ips_badhlen++;
16011232Ssam 			goto next;
16111232Ssam 		}
1625161Swnj 		ip = mtod(m, struct ip *);
1635161Swnj 	}
16437319Skarels 	if (ip->ip_sum = in_cksum(m, hlen)) {
16537319Skarels 		ipstat.ips_badsum++;
16637319Skarels 		goto bad;
16737319Skarels 	}
1684951Swnj 
1694951Swnj 	/*
1704951Swnj 	 * Convert fields to host representation.
1714951Swnj 	 */
172*40689Skarels 	NTOHS(ip->ip_len);
17311232Ssam 	if (ip->ip_len < hlen) {
17411232Ssam 		ipstat.ips_badlen++;
17511232Ssam 		goto bad;
17611232Ssam 	}
177*40689Skarels 	NTOHS(ip->ip_id);
178*40689Skarels 	NTOHS(ip->ip_off);
1794495Swnj 
1804543Swnj 	/*
1814640Swnj 	 * Check that the amount of data in the buffers
1824640Swnj 	 * is as at least much as the IP header would have us expect.
1834640Swnj 	 * Trim mbufs if longer than we expect.
1844640Swnj 	 * Drop packet if shorter than we expect.
1854543Swnj 	 */
18637319Skarels 	if (m->m_pkthdr.len < ip->ip_len) {
18737319Skarels 		ipstat.ips_tooshort++;
18837319Skarels 		goto bad;
1896088Sroot 	}
19037319Skarels 	if (m->m_pkthdr.len > ip->ip_len) {
19137319Skarels 		if (m->m_len == m->m_pkthdr.len) {
19237319Skarels 			m->m_len = ip->ip_len;
19337319Skarels 			m->m_pkthdr.len = ip->ip_len;
19437319Skarels 		} else
19537319Skarels 			m_adj(m, ip->ip_len - m->m_pkthdr.len);
1964495Swnj 	}
1974495Swnj 
1984640Swnj 	/*
1994640Swnj 	 * Process options and, if not destined for us,
2006583Ssam 	 * ship it on.  ip_dooptions returns 1 when an
2016583Ssam 	 * error was detected (causing an icmp message
20221117Skarels 	 * to be sent and the original packet to be freed).
2034640Swnj 	 */
20424813Skarels 	ip_nhops = 0;		/* for source routed packets */
20537319Skarels 	if (hlen > sizeof (struct ip) && ip_dooptions(m))
2066583Ssam 		goto next;
2076210Swnj 
2086338Ssam 	/*
20918376Skarels 	 * Check our list of addresses, to see if the packet is for us.
2106338Ssam 	 */
21118376Skarels 	for (ia = in_ifaddr; ia; ia = ia->ia_next) {
21218376Skarels #define	satosin(sa)	((struct sockaddr_in *)(sa))
2136338Ssam 
21418376Skarels 		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
21524813Skarels 			goto ours;
21625195Skarels 		if (
21725195Skarels #ifdef	DIRECTED_BROADCAST
21837319Skarels 		    ia->ia_ifp == m->m_pkthdr.rcvif &&
21925195Skarels #endif
22025195Skarels 		    (ia->ia_ifp->if_flags & IFF_BROADCAST)) {
22126247Skarels 			u_long t;
22225195Skarels 
22325195Skarels 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
22425195Skarels 			    ip->ip_dst.s_addr)
22525195Skarels 				goto ours;
22625195Skarels 			if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
22725195Skarels 				goto ours;
22825195Skarels 			/*
22925195Skarels 			 * Look for all-0's host part (old broadcast addr),
23025195Skarels 			 * either for subnet or net.
23125195Skarels 			 */
23226247Skarels 			t = ntohl(ip->ip_dst.s_addr);
23326247Skarels 			if (t == ia->ia_subnet)
23425195Skarels 				goto ours;
23526247Skarels 			if (t == ia->ia_net)
23625195Skarels 				goto ours;
23725195Skarels 		}
2386338Ssam 	}
23924813Skarels 	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
24024813Skarels 		goto ours;
24124813Skarels 	if (ip->ip_dst.s_addr == INADDR_ANY)
24224813Skarels 		goto ours;
2434495Swnj 
2444640Swnj 	/*
24524813Skarels 	 * Not for us; forward if possible and desirable.
24624813Skarels 	 */
247*40689Skarels 	if (ipforwarding == 0) {
24836814Skarels 		ipstat.ips_cantforward++;
24936814Skarels 		m_freem(m);
25036814Skarels 	} else
251*40689Skarels 		ip_forward(m, 0);
25224813Skarels 	goto next;
25324813Skarels 
25424813Skarels ours:
25524813Skarels 	/*
25633743Skarels 	 * If offset or IP_MF are set, must reassemble.
25733743Skarels 	 * Otherwise, nothing need be done.
25833743Skarels 	 * (We could look in the reassembly queue to see
25933743Skarels 	 * if the packet was previously fragmented,
26033743Skarels 	 * but it's not worth the time; just let them time out.)
2614640Swnj 	 */
26233743Skarels 	if (ip->ip_off &~ IP_DF) {
263*40689Skarels 		if (m->m_flags & M_EXT) {		/* XXX */
264*40689Skarels 			if ((m = m_pullup(m, sizeof (struct ip))) == 0) {
265*40689Skarels 				ipstat.ips_toosmall++;
266*40689Skarels 				goto next;
267*40689Skarels 			}
268*40689Skarels 			ip = mtod(m, struct ip *);
269*40689Skarels 		}
27033743Skarels 		/*
27133743Skarels 		 * Look for queue of fragments
27233743Skarels 		 * of this datagram.
27333743Skarels 		 */
27433743Skarels 		for (fp = ipq.next; fp != &ipq; fp = fp->next)
27533743Skarels 			if (ip->ip_id == fp->ipq_id &&
27633743Skarels 			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
27733743Skarels 			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
27833743Skarels 			    ip->ip_p == fp->ipq_p)
27933743Skarels 				goto found;
28033743Skarels 		fp = 0;
2814640Swnj found:
2824495Swnj 
28333743Skarels 		/*
28433743Skarels 		 * Adjust ip_len to not reflect header,
28533743Skarels 		 * set ip_mff if more fragments are expected,
28633743Skarels 		 * convert offset of this to bytes.
28733743Skarels 		 */
28833743Skarels 		ip->ip_len -= hlen;
28933743Skarels 		((struct ipasfrag *)ip)->ipf_mff = 0;
29033743Skarels 		if (ip->ip_off & IP_MF)
29133743Skarels 			((struct ipasfrag *)ip)->ipf_mff = 1;
29233743Skarels 		ip->ip_off <<= 3;
2934495Swnj 
29433743Skarels 		/*
29533743Skarels 		 * If datagram marked as having more fragments
29633743Skarels 		 * or if this is not the first fragment,
29733743Skarels 		 * attempt reassembly; if it succeeds, proceed.
29833743Skarels 		 */
29933743Skarels 		if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
30033743Skarels 			ipstat.ips_fragments++;
30133743Skarels 			ip = ip_reass((struct ipasfrag *)ip, fp);
30233743Skarels 			if (ip == 0)
30333743Skarels 				goto next;
30439185Ssklower 			else
30539185Ssklower 				ipstat.ips_reassembled++;
30633743Skarels 			m = dtom(ip);
30733743Skarels 		} else
30833743Skarels 			if (fp)
30933743Skarels 				ip_freef(fp);
3104640Swnj 	} else
31133743Skarels 		ip->ip_len -= hlen;
3124951Swnj 
3134951Swnj 	/*
3144951Swnj 	 * Switch out to protocol's input routine.
3154951Swnj 	 */
31639185Ssklower 	ipstat.ips_delivered++;
31737319Skarels 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
3185084Swnj 	goto next;
3194951Swnj bad:
3204951Swnj 	m_freem(m);
3215084Swnj 	goto next;
3224640Swnj }
3234495Swnj 
3244640Swnj /*
3254640Swnj  * Take incoming datagram fragment and try to
3264951Swnj  * reassemble it into whole datagram.  If a chain for
3274640Swnj  * reassembly of this datagram already exists, then it
3284640Swnj  * is given as fp; otherwise have to make a chain.
3294640Swnj  */
3304640Swnj struct ip *
3314640Swnj ip_reass(ip, fp)
3324898Swnj 	register struct ipasfrag *ip;
3334640Swnj 	register struct ipq *fp;
3344640Swnj {
3354640Swnj 	register struct mbuf *m = dtom(ip);
3364898Swnj 	register struct ipasfrag *q;
3374640Swnj 	struct mbuf *t;
3384640Swnj 	int hlen = ip->ip_hl << 2;
3394640Swnj 	int i, next;
3404543Swnj 
3414640Swnj 	/*
3424640Swnj 	 * Presence of header sizes in mbufs
3434640Swnj 	 * would confuse code below.
3444640Swnj 	 */
34537319Skarels 	m->m_data += hlen;
3464640Swnj 	m->m_len -= hlen;
3474495Swnj 
3484640Swnj 	/*
3494640Swnj 	 * If first fragment to arrive, create a reassembly queue.
3504640Swnj 	 */
3514640Swnj 	if (fp == 0) {
35231201Skarels 		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
3534640Swnj 			goto dropfrag;
3544640Swnj 		fp = mtod(t, struct ipq *);
3554640Swnj 		insque(fp, &ipq);
3564640Swnj 		fp->ipq_ttl = IPFRAGTTL;
3574640Swnj 		fp->ipq_p = ip->ip_p;
3584640Swnj 		fp->ipq_id = ip->ip_id;
3594898Swnj 		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
3604898Swnj 		fp->ipq_src = ((struct ip *)ip)->ip_src;
3614898Swnj 		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
3625161Swnj 		q = (struct ipasfrag *)fp;
3635161Swnj 		goto insert;
3644640Swnj 	}
3654495Swnj 
3664640Swnj 	/*
3674640Swnj 	 * Find a segment which begins after this one does.
3684640Swnj 	 */
3694898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
3704640Swnj 		if (q->ip_off > ip->ip_off)
3714640Swnj 			break;
3724495Swnj 
3734640Swnj 	/*
3744640Swnj 	 * If there is a preceding segment, it may provide some of
3754640Swnj 	 * our data already.  If so, drop the data from the incoming
3764640Swnj 	 * segment.  If it provides all of our data, drop us.
3774640Swnj 	 */
3784898Swnj 	if (q->ipf_prev != (struct ipasfrag *)fp) {
3794898Swnj 		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
3804640Swnj 		if (i > 0) {
3814640Swnj 			if (i >= ip->ip_len)
3824640Swnj 				goto dropfrag;
3834640Swnj 			m_adj(dtom(ip), i);
3844640Swnj 			ip->ip_off += i;
3854640Swnj 			ip->ip_len -= i;
3864640Swnj 		}
3874640Swnj 	}
3884543Swnj 
3894640Swnj 	/*
3904640Swnj 	 * While we overlap succeeding segments trim them or,
3914640Swnj 	 * if they are completely covered, dequeue them.
3924640Swnj 	 */
3934898Swnj 	while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
3944640Swnj 		i = (ip->ip_off + ip->ip_len) - q->ip_off;
3954640Swnj 		if (i < q->ip_len) {
3964640Swnj 			q->ip_len -= i;
3976256Sroot 			q->ip_off += i;
3984640Swnj 			m_adj(dtom(q), i);
3994640Swnj 			break;
4004495Swnj 		}
4014898Swnj 		q = q->ipf_next;
4024898Swnj 		m_freem(dtom(q->ipf_prev));
4034898Swnj 		ip_deq(q->ipf_prev);
4044543Swnj 	}
4054495Swnj 
4065161Swnj insert:
4074640Swnj 	/*
4084640Swnj 	 * Stick new segment in its place;
4094640Swnj 	 * check for complete reassembly.
4104640Swnj 	 */
4114898Swnj 	ip_enq(ip, q->ipf_prev);
4124640Swnj 	next = 0;
4134898Swnj 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
4144640Swnj 		if (q->ip_off != next)
4154640Swnj 			return (0);
4164640Swnj 		next += q->ip_len;
4174640Swnj 	}
4184898Swnj 	if (q->ipf_prev->ipf_mff)
4194640Swnj 		return (0);
4204495Swnj 
4214640Swnj 	/*
4224640Swnj 	 * Reassembly is complete; concatenate fragments.
4234640Swnj 	 */
4244640Swnj 	q = fp->ipq_next;
4254640Swnj 	m = dtom(q);
4264640Swnj 	t = m->m_next;
4274640Swnj 	m->m_next = 0;
4284640Swnj 	m_cat(m, t);
4296298Swnj 	q = q->ipf_next;
4306298Swnj 	while (q != (struct ipasfrag *)fp) {
4316298Swnj 		t = dtom(q);
4326298Swnj 		q = q->ipf_next;
4336298Swnj 		m_cat(m, t);
4346298Swnj 	}
4354495Swnj 
4364640Swnj 	/*
4374640Swnj 	 * Create header for new ip packet by
4384640Swnj 	 * modifying header of first packet;
4394640Swnj 	 * dequeue and discard fragment reassembly header.
4404640Swnj 	 * Make header visible.
4414640Swnj 	 */
4424640Swnj 	ip = fp->ipq_next;
4434640Swnj 	ip->ip_len = next;
4444898Swnj 	((struct ip *)ip)->ip_src = fp->ipq_src;
4454898Swnj 	((struct ip *)ip)->ip_dst = fp->ipq_dst;
4464640Swnj 	remque(fp);
4474907Swnj 	(void) m_free(dtom(fp));
4484640Swnj 	m = dtom(ip);
44924813Skarels 	m->m_len += (ip->ip_hl << 2);
45037319Skarels 	m->m_data -= (ip->ip_hl << 2);
4514898Swnj 	return ((struct ip *)ip);
4524495Swnj 
4534640Swnj dropfrag:
45424813Skarels 	ipstat.ips_fragdropped++;
4554640Swnj 	m_freem(m);
4564640Swnj 	return (0);
4574495Swnj }
4584495Swnj 
4594640Swnj /*
4604640Swnj  * Free a fragment reassembly header and all
4614640Swnj  * associated datagrams.
4624640Swnj  */
4634640Swnj ip_freef(fp)
4644640Swnj 	struct ipq *fp;
4654495Swnj {
46610735Ssam 	register struct ipasfrag *q, *p;
4674495Swnj 
46810735Ssam 	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {
46910735Ssam 		p = q->ipf_next;
47010735Ssam 		ip_deq(q);
4714640Swnj 		m_freem(dtom(q));
47210735Ssam 	}
47310735Ssam 	remque(fp);
47410735Ssam 	(void) m_free(dtom(fp));
4754495Swnj }
4764495Swnj 
4774640Swnj /*
4784640Swnj  * Put an ip fragment on a reassembly chain.
4794640Swnj  * Like insque, but pointers in middle of structure.
4804640Swnj  */
4814640Swnj ip_enq(p, prev)
4824898Swnj 	register struct ipasfrag *p, *prev;
4834495Swnj {
4844951Swnj 
4854898Swnj 	p->ipf_prev = prev;
4864898Swnj 	p->ipf_next = prev->ipf_next;
4874898Swnj 	prev->ipf_next->ipf_prev = p;
4884898Swnj 	prev->ipf_next = p;
4894495Swnj }
4904495Swnj 
4914640Swnj /*
4924640Swnj  * To ip_enq as remque is to insque.
4934640Swnj  */
4944640Swnj ip_deq(p)
4954898Swnj 	register struct ipasfrag *p;
4964640Swnj {
4974951Swnj 
4984898Swnj 	p->ipf_prev->ipf_next = p->ipf_next;
4994898Swnj 	p->ipf_next->ipf_prev = p->ipf_prev;
5004495Swnj }
5014495Swnj 
5024640Swnj /*
5034640Swnj  * IP timer processing;
5044640Swnj  * if a timer expires on a reassembly
5054640Swnj  * queue, discard it.
5064640Swnj  */
5074801Swnj ip_slowtimo()
5084495Swnj {
5094495Swnj 	register struct ipq *fp;
5104640Swnj 	int s = splnet();
5114951Swnj 
5125243Sroot 	fp = ipq.next;
5135243Sroot 	if (fp == 0) {
5145243Sroot 		splx(s);
5155243Sroot 		return;
5165243Sroot 	}
51710735Ssam 	while (fp != &ipq) {
51810735Ssam 		--fp->ipq_ttl;
51910735Ssam 		fp = fp->next;
52024813Skarels 		if (fp->prev->ipq_ttl == 0) {
52124813Skarels 			ipstat.ips_fragtimeout++;
52210735Ssam 			ip_freef(fp->prev);
52324813Skarels 		}
52410735Ssam 	}
5254640Swnj 	splx(s);
5264495Swnj }
5274495Swnj 
5284951Swnj /*
5294951Swnj  * Drain off all datagram fragments.
5304951Swnj  */
5314801Swnj ip_drain()
5324801Swnj {
5334801Swnj 
53424813Skarels 	while (ipq.next != &ipq) {
53524813Skarels 		ipstat.ips_fragdropped++;
53610735Ssam 		ip_freef(ipq.next);
53724813Skarels 	}
5384801Swnj }
5394923Swnj 
54030925Skarels extern struct in_ifaddr *ifptoia();
54124813Skarels struct in_ifaddr *ip_rtaddr();
54224813Skarels 
5434640Swnj /*
5444640Swnj  * Do option processing on a datagram,
545*40689Skarels  * possibly discarding it if bad options are encountered,
546*40689Skarels  * or forwarding it if source-routed.
547*40689Skarels  * Returns 1 if packet has been forwarded/freed,
548*40689Skarels  * 0 if the packet should be processed further.
5494640Swnj  */
55037319Skarels ip_dooptions(m)
55136814Skarels 	struct mbuf *m;
5524495Swnj {
55336814Skarels 	register struct ip *ip = mtod(m, struct ip *);
5544640Swnj 	register u_char *cp;
55524813Skarels 	register struct ip_timestamp *ipt;
55624813Skarels 	register struct in_ifaddr *ia;
55736814Skarels 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
5584923Swnj 	struct in_addr *sin;
55924813Skarels 	n_time ntime;
5604495Swnj 
5614640Swnj 	cp = (u_char *)(ip + 1);
5624640Swnj 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
5634640Swnj 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
56424813Skarels 		opt = cp[IPOPT_OPTVAL];
5654640Swnj 		if (opt == IPOPT_EOL)
5664640Swnj 			break;
5674640Swnj 		if (opt == IPOPT_NOP)
5684640Swnj 			optlen = 1;
56916392Ssam 		else {
57024813Skarels 			optlen = cp[IPOPT_OLEN];
57124813Skarels 			if (optlen <= 0 || optlen > cnt) {
57224813Skarels 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
57317551Skarels 				goto bad;
57424813Skarels 			}
57516392Ssam 		}
5764640Swnj 		switch (opt) {
5774495Swnj 
5784640Swnj 		default:
5794640Swnj 			break;
5804495Swnj 
5814951Swnj 		/*
5824951Swnj 		 * Source routing with record.
5834951Swnj 		 * Find interface with current destination address.
5844951Swnj 		 * If none on this machine then drop if strictly routed,
5854951Swnj 		 * or do nothing if loosely routed.
5864951Swnj 		 * Record interface address and bring up next address
5874951Swnj 		 * component.  If strictly routed make sure next
588*40689Skarels 		 * address is on directly accessible net.
5894951Swnj 		 */
5904640Swnj 		case IPOPT_LSRR:
5917508Sroot 		case IPOPT_SSRR:
59224813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
59324813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
59424813Skarels 				goto bad;
59524813Skarels 			}
59624813Skarels 			ipaddr.sin_addr = ip->ip_dst;
59724813Skarels 			ia = (struct in_ifaddr *)
59824813Skarels 				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
59924813Skarels 			if (ia == 0) {
60024813Skarels 				if (opt == IPOPT_SSRR) {
60124813Skarels 					type = ICMP_UNREACH;
60224813Skarels 					code = ICMP_UNREACH_SRCFAIL;
6034951Swnj 					goto bad;
60424813Skarels 				}
60524813Skarels 				/*
60624813Skarels 				 * Loose routing, and not at next destination
60724813Skarels 				 * yet; nothing to do except forward.
60824813Skarels 				 */
6094951Swnj 				break;
6104640Swnj 			}
61124813Skarels 			off--;			/* 0 origin */
61224813Skarels 			if (off > optlen - sizeof(struct in_addr)) {
61324813Skarels 				/*
61424813Skarels 				 * End of source route.  Should be for us.
61524813Skarels 				 */
61624813Skarels 				save_rte(cp, ip->ip_src);
6174951Swnj 				break;
61824813Skarels 			}
61924813Skarels 			/*
62024813Skarels 			 * locate outgoing interface
62124813Skarels 			 */
62226384Skarels 			bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
62324813Skarels 			    sizeof(ipaddr.sin_addr));
624*40689Skarels 			if (opt == IPOPT_SSRR) {
625*40689Skarels #define	INA	struct in_ifaddr *
626*40689Skarels #define	SA	struct sockaddr *
627*40689Skarels 			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
628*40689Skarels 				ia = in_iaonnetof(in_netof(ipaddr.sin_addr));
629*40689Skarels 			} else
630*40689Skarels 				ia = ip_rtaddr(ipaddr.sin_addr);
631*40689Skarels 			if (ia == 0) {
63224813Skarels 				type = ICMP_UNREACH;
63324813Skarels 				code = ICMP_UNREACH_SRCFAIL;
6344951Swnj 				goto bad;
63524813Skarels 			}
63624813Skarels 			ip->ip_dst = ipaddr.sin_addr;
63726384Skarels 			bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
63826384Skarels 			    (caddr_t)(cp + off), sizeof(struct in_addr));
63924813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
64036814Skarels 			forward = 1;
6414640Swnj 			break;
6424495Swnj 
64324813Skarels 		case IPOPT_RR:
64424813Skarels 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
64524813Skarels 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
64624813Skarels 				goto bad;
64724813Skarels 			}
64824813Skarels 			/*
64924813Skarels 			 * If no space remains, ignore.
65024813Skarels 			 */
65124813Skarels 			off--;			/* 0 origin */
65224813Skarels 			if (off > optlen - sizeof(struct in_addr))
65324813Skarels 				break;
65431393Skarels 			bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
65524813Skarels 			    sizeof(ipaddr.sin_addr));
65624813Skarels 			/*
65737319Skarels 			 * locate outgoing interface; if we're the destination,
65837319Skarels 			 * use the incoming interface (should be same).
65924813Skarels 			 */
660*40689Skarels 			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
66137319Skarels 			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
66224813Skarels 				type = ICMP_UNREACH;
66332113Skarels 				code = ICMP_UNREACH_HOST;
66424813Skarels 				goto bad;
66524813Skarels 			}
66626384Skarels 			bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
66726384Skarels 			    (caddr_t)(cp + off), sizeof(struct in_addr));
66824813Skarels 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
66924813Skarels 			break;
67024813Skarels 
6714640Swnj 		case IPOPT_TS:
6726583Ssam 			code = cp - (u_char *)ip;
6734801Swnj 			ipt = (struct ip_timestamp *)cp;
6744801Swnj 			if (ipt->ipt_len < 5)
6754640Swnj 				goto bad;
6764801Swnj 			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
6774801Swnj 				if (++ipt->ipt_oflw == 0)
6784640Swnj 					goto bad;
6794495Swnj 				break;
6804640Swnj 			}
68130925Skarels 			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
6824801Swnj 			switch (ipt->ipt_flg) {
6834495Swnj 
6844640Swnj 			case IPOPT_TS_TSONLY:
6854640Swnj 				break;
6864640Swnj 
6874640Swnj 			case IPOPT_TS_TSANDADDR:
68824813Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
68924813Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
6904640Swnj 					goto bad;
69137319Skarels 				ia = ifptoia(m->m_pkthdr.rcvif);
69230925Skarels 				bcopy((caddr_t)&IA_SIN(ia)->sin_addr,
69324813Skarels 				    (caddr_t)sin, sizeof(struct in_addr));
69430925Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
6954640Swnj 				break;
6964640Swnj 
6974640Swnj 			case IPOPT_TS_PRESPEC:
69830925Skarels 				if (ipt->ipt_ptr + sizeof(n_time) +
69930925Skarels 				    sizeof(struct in_addr) > ipt->ipt_len)
70030925Skarels 					goto bad;
70124813Skarels 				bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
70224813Skarels 				    sizeof(struct in_addr));
703*40689Skarels 				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
7044951Swnj 					continue;
70524813Skarels 				ipt->ipt_ptr += sizeof(struct in_addr);
7064640Swnj 				break;
7074640Swnj 
7084495Swnj 			default:
7094640Swnj 				goto bad;
7104495Swnj 			}
71124813Skarels 			ntime = iptime();
71230925Skarels 			bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
71330925Skarels 			    sizeof(n_time));
71424813Skarels 			ipt->ipt_ptr += sizeof(n_time);
7154640Swnj 		}
7164495Swnj 	}
71736814Skarels 	if (forward) {
718*40689Skarels 		ip_forward(m, 1);
71936814Skarels 		return (1);
72036814Skarels 	} else
72136814Skarels 		return (0);
7224640Swnj bad:
72337319Skarels 	icmp_error(m, type, code);
7246583Ssam 	return (1);
7254495Swnj }
7264495Swnj 
7274640Swnj /*
72824813Skarels  * Given address of next destination (final or next hop),
72924813Skarels  * return internet address info of interface to be used to get there.
73024813Skarels  */
73124813Skarels struct in_ifaddr *
73224813Skarels ip_rtaddr(dst)
73324813Skarels 	 struct in_addr dst;
73424813Skarels {
73524813Skarels 	register struct sockaddr_in *sin;
73624813Skarels 
73724813Skarels 	sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
73824813Skarels 
73924813Skarels 	if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
74024813Skarels 		if (ipforward_rt.ro_rt) {
74124813Skarels 			RTFREE(ipforward_rt.ro_rt);
74224813Skarels 			ipforward_rt.ro_rt = 0;
74324813Skarels 		}
74424813Skarels 		sin->sin_family = AF_INET;
74537319Skarels 		sin->sin_len = sizeof(*sin);
74624813Skarels 		sin->sin_addr = dst;
74724813Skarels 
74824813Skarels 		rtalloc(&ipforward_rt);
74924813Skarels 	}
75024813Skarels 	if (ipforward_rt.ro_rt == 0)
75124813Skarels 		return ((struct in_ifaddr *)0);
752*40689Skarels 	return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
75324813Skarels }
75424813Skarels 
75524813Skarels /*
75624813Skarels  * Save incoming source route for use in replies,
75724813Skarels  * to be picked up later by ip_srcroute if the receiver is interested.
75824813Skarels  */
75924813Skarels save_rte(option, dst)
76026384Skarels 	u_char *option;
76124813Skarels 	struct in_addr dst;
76224813Skarels {
76326384Skarels 	unsigned olen;
76424813Skarels 
76524813Skarels 	olen = option[IPOPT_OLEN];
766*40689Skarels #ifdef DEBUG
76736814Skarels 	if (ipprintfs)
76836814Skarels 		printf("save_rte: olen %d\n", olen);
769*40689Skarels #endif
77036814Skarels 	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
77124813Skarels 		return;
77226384Skarels 	bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
77324813Skarels 	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
77436814Skarels 	ip_srcrt.dst = dst;
77524813Skarels }
77624813Skarels 
77724813Skarels /*
77824813Skarels  * Retrieve incoming source route for use in replies,
77924813Skarels  * in the same form used by setsockopt.
78024813Skarels  * The first hop is placed before the options, will be removed later.
78124813Skarels  */
78224813Skarels struct mbuf *
78324813Skarels ip_srcroute()
78424813Skarels {
78524813Skarels 	register struct in_addr *p, *q;
78624813Skarels 	register struct mbuf *m;
78724813Skarels 
78824813Skarels 	if (ip_nhops == 0)
78924813Skarels 		return ((struct mbuf *)0);
79031201Skarels 	m = m_get(M_DONTWAIT, MT_SOOPTS);
79131201Skarels 	if (m == 0)
79231201Skarels 		return ((struct mbuf *)0);
79324813Skarels 
79436814Skarels #define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
79536814Skarels 
79636814Skarels 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
79736814Skarels 	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
79836814Skarels 	    OPTSIZ;
799*40689Skarels #ifdef DEBUG
80036814Skarels 	if (ipprintfs)
80136814Skarels 		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
802*40689Skarels #endif
80336814Skarels 
80424813Skarels 	/*
80524813Skarels 	 * First save first hop for return route
80624813Skarels 	 */
80724813Skarels 	p = &ip_srcrt.route[ip_nhops - 1];
80824813Skarels 	*(mtod(m, struct in_addr *)) = *p--;
809*40689Skarels #ifdef DEBUG
81036814Skarels 	if (ipprintfs)
81136814Skarels 		printf(" hops %X", ntohl(*mtod(m, struct in_addr *)));
812*40689Skarels #endif
81324813Skarels 
81424813Skarels 	/*
81524813Skarels 	 * Copy option fields and padding (nop) to mbuf.
81624813Skarels 	 */
81724813Skarels 	ip_srcrt.nop = IPOPT_NOP;
81836814Skarels 	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
81936814Skarels 	bcopy((caddr_t)&ip_srcrt.nop,
82036814Skarels 	    mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
82124813Skarels 	q = (struct in_addr *)(mtod(m, caddr_t) +
82236814Skarels 	    sizeof(struct in_addr) + OPTSIZ);
82336814Skarels #undef OPTSIZ
82424813Skarels 	/*
82524813Skarels 	 * Record return path as an IP source route,
82624813Skarels 	 * reversing the path (pointers are now aligned).
82724813Skarels 	 */
82836814Skarels 	while (p >= ip_srcrt.route) {
829*40689Skarels #ifdef DEBUG
83036814Skarels 		if (ipprintfs)
83136814Skarels 			printf(" %X", ntohl(*q));
832*40689Skarels #endif
83324813Skarels 		*q++ = *p--;
83436814Skarels 	}
83536814Skarels 	/*
83636814Skarels 	 * Last hop goes to final destination.
83736814Skarels 	 */
83836814Skarels 	*q = ip_srcrt.dst;
839*40689Skarels #ifdef DEBUG
84036814Skarels 	if (ipprintfs)
84136814Skarels 		printf(" %X\n", ntohl(*q));
842*40689Skarels #endif
84324813Skarels 	return (m);
84424813Skarels }
84524813Skarels 
84624813Skarels /*
8474951Swnj  * Strip out IP options, at higher
8484951Swnj  * level protocol in the kernel.
8494951Swnj  * Second argument is buffer to which options
8504951Swnj  * will be moved, and return value is their length.
85136814Skarels  * XXX should be deleted; last arg currently ignored.
8524640Swnj  */
85337319Skarels ip_stripoptions(m, mopt)
85437319Skarels 	register struct mbuf *m;
8555217Swnj 	struct mbuf *mopt;
8564495Swnj {
8574640Swnj 	register int i;
85837319Skarels 	struct ip *ip = mtod(m, struct ip *);
85924813Skarels 	register caddr_t opts;
8604640Swnj 	int olen;
8614640Swnj 
8624640Swnj 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
86324813Skarels 	opts = (caddr_t)(ip + 1);
8644640Swnj 	i = m->m_len - (sizeof (struct ip) + olen);
86524813Skarels 	bcopy(opts  + olen, opts, (unsigned)i);
8665243Sroot 	m->m_len -= olen;
86737319Skarels 	if (m->m_flags & M_PKTHDR)
86837319Skarels 		m->m_pkthdr.len -= olen;
86924813Skarels 	ip->ip_hl = sizeof(struct ip) >> 2;
8704495Swnj }
8716583Ssam 
87214670Ssam u_char inetctlerrmap[PRC_NCMDS] = {
87324813Skarels 	0,		0,		0,		0,
874*40689Skarels 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
875*40689Skarels 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
87624813Skarels 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
87724813Skarels 	0,		0,		0,		0,
87824813Skarels 	ENOPROTOOPT
8796583Ssam };
8806583Ssam 
8816583Ssam /*
8826583Ssam  * Forward a packet.  If some error occurs return the sender
88318376Skarels  * an icmp packet.  Note we can't always generate a meaningful
88424813Skarels  * icmp message because icmp doesn't have a large enough repertoire
8856583Ssam  * of codes and types.
88626308Skarels  *
887*40689Skarels  * If not forwarding, just drop the packet.  This could be confusing
888*40689Skarels  * if ipforwarding was zero but some routing protocol was advancing
889*40689Skarels  * us as a gateway to somewhere.  However, we must let the routing
890*40689Skarels  * protocol deal with that.
891*40689Skarels  *
892*40689Skarels  * The srcrt parameter indicates whether the packet is being forwarded
893*40689Skarels  * via a source route.
8946583Ssam  */
895*40689Skarels ip_forward(m, srcrt)
89636814Skarels 	struct mbuf *m;
897*40689Skarels 	int srcrt;
8986583Ssam {
89936814Skarels 	register struct ip *ip = mtod(m, struct ip *);
90024813Skarels 	register struct sockaddr_in *sin;
901*40689Skarels 	register struct rtentry *rt;
902*40689Skarels 	int error, type = 0, code;
90318376Skarels 	struct mbuf *mcopy;
90424813Skarels 	struct in_addr dest;
9056583Ssam 
90624813Skarels 	dest.s_addr = 0;
907*40689Skarels #ifdef DEBUG
9086583Ssam 	if (ipprintfs)
9096583Ssam 		printf("forward: src %x dst %x ttl %x\n", ip->ip_src,
9106583Ssam 			ip->ip_dst, ip->ip_ttl);
911*40689Skarels #endif
91237319Skarels 	if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
91326308Skarels 		ipstat.ips_cantforward++;
91437319Skarels 		m_freem(m);
91526308Skarels 		return;
9166583Ssam 	}
917*40689Skarels 	HTONS(ip->ip_id);
91831393Skarels 	if (ip->ip_ttl <= IPTTLDEC) {
919*40689Skarels 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest);
920*40689Skarels 		return;
9216583Ssam 	}
9226583Ssam 	ip->ip_ttl -= IPTTLDEC;
9236609Ssam 
92424813Skarels 	sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
925*40689Skarels 	if ((rt = ipforward_rt.ro_rt) == 0 ||
92624813Skarels 	    ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
92724813Skarels 		if (ipforward_rt.ro_rt) {
92824813Skarels 			RTFREE(ipforward_rt.ro_rt);
92924813Skarels 			ipforward_rt.ro_rt = 0;
93024813Skarels 		}
93124813Skarels 		sin->sin_family = AF_INET;
93237319Skarels 		sin->sin_len = sizeof(*sin);
93324813Skarels 		sin->sin_addr = ip->ip_dst;
93424813Skarels 
93524813Skarels 		rtalloc(&ipforward_rt);
936*40689Skarels 		if (ipforward_rt.ro_rt == 0) {
937*40689Skarels 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest);
938*40689Skarels 			return;
939*40689Skarels 		}
940*40689Skarels 		rt = ipforward_rt.ro_rt;
94124813Skarels 	}
942*40689Skarels 
94324813Skarels 	/*
944*40689Skarels 	 * Save at most 64 bytes of the packet in case
945*40689Skarels 	 * we need to generate an ICMP message to the src.
946*40689Skarels 	 */
947*40689Skarels 	mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
948*40689Skarels 
949*40689Skarels #ifdef GATEWAY
950*40689Skarels 	ip_ifmatrix[rt->rt_ifp->if_index +
951*40689Skarels 	     if_index * m->m_pkthdr.rcvif->if_index]++;
952*40689Skarels #endif
953*40689Skarels 	/*
95424813Skarels 	 * If forwarding packet using same interface that it came in on,
95524813Skarels 	 * perhaps should send a redirect to sender to shortcut a hop.
95624813Skarels 	 * Only send redirect if source is sending directly to us,
95724813Skarels 	 * and if packet was not source routed (or has any options).
95830447Skarels 	 * Also, don't send redirect if forwarding using a default route
959*40689Skarels 	 * or a route modified by a redirect.
96024813Skarels 	 */
96130447Skarels #define	satosin(sa)	((struct sockaddr_in *)(sa))
962*40689Skarels 	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
963*40689Skarels 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
964*40689Skarels 	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
965*40689Skarels 	    ipsendredirects && !srcrt) {
96624813Skarels 		struct in_ifaddr *ia;
96724813Skarels 		u_long src = ntohl(ip->ip_src.s_addr);
96824813Skarels 		u_long dst = ntohl(ip->ip_dst.s_addr);
96924813Skarels 
97037319Skarels 		if ((ia = ifptoia(m->m_pkthdr.rcvif)) &&
97124813Skarels 		   (src & ia->ia_subnetmask) == ia->ia_subnet) {
972*40689Skarels 		    if (rt->rt_flags & RTF_GATEWAY)
973*40689Skarels 			dest = satosin(rt->rt_gateway)->sin_addr;
97424813Skarels 		    else
97524813Skarels 			dest = ip->ip_dst;
97624813Skarels 		    /*
97724813Skarels 		     * If the destination is reached by a route to host,
97827145Skarels 		     * is on a subnet of a local net, or is directly
97927145Skarels 		     * on the attached net (!), use host redirect.
98024813Skarels 		     * (We may be the correct first hop for other subnets.)
98124813Skarels 		     */
982*40689Skarels #define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
98324813Skarels 		    type = ICMP_REDIRECT;
984*40689Skarels 		    if ((rt->rt_flags & RTF_HOST) ||
985*40689Skarels 		        (rt->rt_flags & RTF_GATEWAY) == 0)
986*40689Skarels 			    code = ICMP_REDIRECT_HOST;
987*40689Skarels 		    else if (RTA(rt)->ia_subnetmask != RTA(rt)->ia_netmask &&
988*40689Skarels 		        (dst & RTA(rt)->ia_netmask) ==  RTA(rt)->ia_net)
989*40689Skarels 			    code = ICMP_REDIRECT_HOST;
990*40689Skarels 		    else
991*40689Skarels 			    code = ICMP_REDIRECT_NET;
992*40689Skarels #ifdef DEBUG
99324813Skarels 		    if (ipprintfs)
994*40689Skarels 		        printf("redirect (%d) to %x\n", code, dest.s_addr);
995*40689Skarels #endif
99624813Skarels 		}
99724813Skarels 	}
99824813Skarels 
99937319Skarels 	error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING);
100024813Skarels 	if (error)
100124813Skarels 		ipstat.ips_cantforward++;
100224813Skarels 	else {
100321117Skarels 		ipstat.ips_forward++;
1004*40689Skarels 		if (type)
1005*40689Skarels 			ipstat.ips_redirectsent++;
1006*40689Skarels 		else {
1007*40689Skarels 			if (mcopy)
1008*40689Skarels 				m_freem(mcopy);
1009*40689Skarels 			return;
1010*40689Skarels 		}
10116609Ssam 	}
101211540Ssam 	if (mcopy == NULL)
101311540Ssam 		return;
10146609Ssam 	switch (error) {
10156609Ssam 
101624813Skarels 	case 0:				/* forwarded, but need redirect */
1017*40689Skarels 		/* type, code set above */
101824813Skarels 		break;
101924813Skarels 
1020*40689Skarels 	case ENETUNREACH:		/* shouldn't happen, checked above */
1021*40689Skarels 	case EHOSTUNREACH:
10226609Ssam 	case ENETDOWN:
1023*40689Skarels 	case EHOSTDOWN:
1024*40689Skarels 	default:
1025*40689Skarels 		type = ICMP_UNREACH;
1026*40689Skarels 		code = ICMP_UNREACH_HOST;
10276609Ssam 		break;
10286609Ssam 
10296609Ssam 	case EMSGSIZE:
1030*40689Skarels 		type = ICMP_UNREACH;
10316583Ssam 		code = ICMP_UNREACH_NEEDFRAG;
103239185Ssklower 		ipstat.ips_cantfrag++;
10336609Ssam 		break;
10346609Ssam 
10356609Ssam 	case ENOBUFS:
10366609Ssam 		type = ICMP_SOURCEQUENCH;
103737319Skarels 		code = 0;
10386609Ssam 		break;
10396609Ssam 	}
104038795Skarels 	icmp_error(mcopy, type, code, dest);
10416583Ssam }
1042