ip_input.c revision 37433
1141104Sharti/*
21590Srgrimes * Copyright (c) 1982, 1986, 1988, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes *
331590Srgrimes *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
341590Srgrimes * $Id: ip_input.c,v 1.93 1998/07/06 05:00:52 julian Exp $
351590Srgrimes *	$ANA: ip_input.c,v 1.5 1996/09/18 14:34:59 wollman Exp $
361590Srgrimes */
3762833Swsanchez
3862833Swsanchez#define	_IP_VHL
391590Srgrimes
401590Srgrimes#include "opt_bootp.h"
4162833Swsanchez#include "opt_ipfw.h"
4294587Sobrien#include "opt_ipdivert.h"
431590Srgrimes#include "opt_ipfilter.h"
441590Srgrimes
451590Srgrimes#include <stddef.h>
461590Srgrimes
471590Srgrimes#include <sys/param.h>
481590Srgrimes#include <sys/systm.h>
491590Srgrimes#include <sys/mbuf.h>
501590Srgrimes#include <sys/domain.h>
511590Srgrimes#include <sys/protosw.h>
521590Srgrimes#include <sys/socket.h>
531590Srgrimes#include <sys/time.h>
541590Srgrimes#include <sys/kernel.h>
55144026Sharti#include <sys/syslog.h>
56144026Sharti#include <sys/sysctl.h>
57144026Sharti
58144026Sharti#include <net/if.h>
591590Srgrimes#include <net/if_var.h>
60144026Sharti#include <net/if_dl.h>
61144026Sharti#include <net/route.h>
62144026Sharti#include <net/netisr.h>
63144026Sharti
64144026Sharti#include <netinet/in.h>
651590Srgrimes#include <netinet/in_systm.h>
66144026Sharti#include <netinet/in_var.h>
67144026Sharti#include <netinet/ip.h>
68144026Sharti#include <netinet/in_pcb.h>
69144026Sharti#include <netinet/ip_var.h>
70144026Sharti#include <netinet/ip_icmp.h>
711590Srgrimes#include <machine/in_cksum.h>
721590Srgrimes
73144341Sharti#include <sys/socketvar.h>
74141104Sharti
751590Srgrimes#ifdef IPFIREWALL
76141104Sharti#include <netinet/ip_fw.h>
77141104Sharti#endif
7827644Scharnier
79141104Shartiint rsvp_on = 0;
80141104Shartistatic int ip_rsvp_on;
81141104Shartistruct socket *ip_rsvpd;
82141104Sharti
83141104Shartiint	ipforwarding = 0;
841590SrgrimesSYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
85145612Sharti	&ipforwarding, 0, "");
86141104Sharti
87141104Shartistatic int	ipsendredirects = 1; /* XXX */
88141104ShartiSYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
891590Srgrimes	&ipsendredirects, 0, "");
90141104Sharti
91141104Shartiint	ip_defttl = IPDEFTTL;
92141104ShartiSYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
931590Srgrimes	&ip_defttl, 0, "");
94141104Sharti
95141104Shartistatic int	ip_dosourceroute = 0;
96141104ShartiSYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
97141104Sharti	&ip_dosourceroute, 0, "");
98141104Sharti
991590Srgrimesstatic int	ip_acceptsourceroute = 0;
1001590SrgrimesSYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
101144341Sharti	CTLFLAG_RW, &ip_acceptsourceroute, 0, "");
1021590Srgrimes#ifdef DIAGNOSTIC
1031590Srgrimesstatic int	ipprintfs = 0;
1041590Srgrimes#endif
1051590Srgrimes
1061590Srgrimesextern	struct domain inetdomain;
107138916Shartiextern	struct protosw inetsw[];
108138916Shartiu_char	ip_protox[IPPROTO_MAX];
109138916Shartistatic int	ipqmaxlen = IFQ_MAXLEN;
110138916Shartistruct	in_ifaddrhead in_ifaddrhead; /* first inet address */
111144026Shartistruct	ifqueue ipintrq;
112144026ShartiSYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RD,
1131590Srgrimes	&ipintrq.ifq_maxlen, 0, "");
114144026ShartiSYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
1151590Srgrimes	&ipintrq.ifq_drops, 0, "");
116144026Sharti
117144026Shartistruct ipstat ipstat;
118144026ShartiSYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD,
119144026Sharti	&ipstat, ipstat, "");
120144026Sharti
121126824Sru/* Packet reassembly stuff */
122144341Sharti#define IPREASS_NHASH_LOG2      6
123144341Sharti#define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
124144341Sharti#define IPREASS_HMASK           (IPREASS_NHASH - 1)
125144341Sharti#define IPREASS_HASH(x,y) \
126144341Sharti	((((x) & 0xF | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
127144341Sharti
128144341Shartistatic struct ipq ipq[IPREASS_NHASH];
129144341Shartistatic int    nipq = 0;         /* total # of reass queues */
130144341Shartistatic int    maxnipq;
131144341Sharti
132144341Sharti#ifdef IPCTL_DEFMTU
133144026ShartiSYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
134138916Sharti	&ip_mtu, 0, "");
135144341Sharti#endif
1361590Srgrimes
137144341Sharti#if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
138144341Sharti#undef COMPAT_IPFW
139144341Sharti#define COMPAT_IPFW 1
140138916Sharti#else
141144020Sharti#undef COMPAT_IPFW
142138916Sharti#endif
143138916Sharti
144144020Sharti#ifdef COMPAT_IPFW
145138916Sharti/* Firewall hooks */
146144026Shartiip_fw_chk_t *ip_fw_chk_ptr;
1471590Srgrimesip_fw_ctl_t *ip_fw_ctl_ptr;
1481590Srgrimes
1491590Srgrimes/* IP Network Address Translation (NAT) hooks */
1501590Srgrimesip_nat_t *ip_nat_ptr;
1511590Srgrimesip_nat_ctl_t *ip_nat_ctl_ptr;
1521590Srgrimes#endif
153144026Sharti
154144026Sharti#if defined(IPFILTER_LKM) || defined(IPFILTER)
155144026Shartiint iplattach __P((void));
156144026Shartiint (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)) = NULL;
157144026Sharti#endif
158144026Sharti
159144026Sharti
160144026Sharti/*
161144026Sharti * We need to save the IP options in case a protocol wants to respond
162144026Sharti * to an incoming packet over the same route if the packet got here
163144026Sharti * using IP source routing.  This allows connection establishment and
164144026Sharti * maintenance when the remote end is on a network that is not known
165144026Sharti * to us.
166144026Sharti */
167144026Shartistatic int	ip_nhops = 0;
168144026Shartistatic	struct ip_srcrt {
169144026Sharti	struct	in_addr dst;			/* final destination */
170144026Sharti	char	nop;				/* one NOP to align */
171144026Sharti	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
172144026Sharti	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
173144026Sharti} ip_srcrt;
174144026Sharti
175144026Sharti#ifdef IPDIVERT
176144026Sharti/*
177144026Sharti * Shared variable between ip_input() and ip_reass() to communicate
1781590Srgrimes * about which packets, once assembled from fragments, get diverted,
1791590Srgrimes * and to which port.
1801590Srgrimes */
18118730Sstevestatic u_short	frag_divert_port;
1821590Srgrimes#endif
1831590Srgrimes
18469527Swillstruct sockaddr_in *ip_fw_fwd_addr;
1851590Srgrimes
1861590Srgrimesstatic void save_rte __P((u_char *, struct in_addr));
187144026Shartistatic void	 ip_deq __P((struct ipasfrag *));
1881590Srgrimesstatic int	 ip_dooptions __P((struct mbuf *));
1891590Srgrimesstatic void	 ip_enq __P((struct ipasfrag *, struct ipasfrag *));
1901590Srgrimesstatic void	 ip_forward __P((struct mbuf *, int));
1911590Srgrimesstatic void	 ip_freef __P((struct ipq *));
1921590Srgrimesstatic struct ip *
1931590Srgrimes	 ip_reass __P((struct ipasfrag *, struct ipq *, struct ipq *));
1941590Srgrimesstatic struct in_ifaddr *
1951590Srgrimes	 ip_rtaddr __P((struct in_addr));
196145616Shartistatic void	ipintr __P((void));
197144026Sharti/*
198144026Sharti * IP initialization: fill in IP protocol switch table.
199144026Sharti * All protocols not implemented in kernel go to raw IP protocol handler.
2001590Srgrimes */
201145616Shartivoid
202144026Shartiip_init()
203144026Sharti{
204144026Sharti	register struct protosw *pr;
205144026Sharti	register int i;
206144026Sharti
207144026Sharti	TAILQ_INIT(&in_ifaddrhead);
208144026Sharti	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
209144026Sharti	if (pr == 0)
210144026Sharti		panic("ip_init");
211144026Sharti	for (i = 0; i < IPPROTO_MAX; i++)
212144026Sharti		ip_protox[i] = pr - inetsw;
213144026Sharti	for (pr = inetdomain.dom_protosw;
214144026Sharti	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
215144026Sharti		if (pr->pr_domain->dom_family == PF_INET &&
216144026Sharti		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
217144026Sharti			ip_protox[pr->pr_protocol] = pr - inetsw;
218144026Sharti
219144026Sharti	for (i = 0; i < IPREASS_NHASH; i++)
220144026Sharti	    ipq[i].next = ipq[i].prev = &ipq[i];
221144026Sharti
222144026Sharti	maxnipq = nmbclusters/4;
223144026Sharti
224144026Sharti	ip_id = time_second & 0xffff;
225144026Sharti	ipintrq.ifq_maxlen = ipqmaxlen;
226144026Sharti#ifdef IPFIREWALL
227144026Sharti	ip_fw_init();
228144026Sharti#endif
229144026Sharti#ifdef IPNAT
230144026Sharti        ip_nat_init();
231144026Sharti#endif
232144026Sharti#ifdef IPFILTER
233144026Sharti        iplattach();
234145616Sharti#endif
2351590Srgrimes
236145616Sharti}
2371590Srgrimes
238144894Shartistatic struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
239144894Shartistatic struct	route ipforward_rt;
240144894Sharti
241144894Sharti/*
242144894Sharti * Ip input routine.  Checksum and byte swap header.  If fragmented
243144894Sharti * try to reassemble.  Process options.  Pass to next level.
244144894Sharti */
245144894Shartivoid
246144894Shartiip_input(struct mbuf *m)
247144894Sharti{
248144894Sharti	struct ip *ip;
249144894Sharti	struct ipq *fp;
250145612Sharti	struct in_ifaddr *ia;
251144894Sharti	int    i, hlen;
252144894Sharti	u_short sum;
253144894Sharti
254144894Sharti#ifdef	DIAGNOSTIC
255144894Sharti	if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
256144894Sharti		panic("ip_input no HDR");
257144894Sharti#endif
258144894Sharti	/*
259144894Sharti	 * If no IP addresses have been set yet but the interfaces
260144894Sharti	 * are receiving, can't do anything with incoming packets yet.
261144894Sharti	 * XXX This is broken! We should be able to receive broadcasts
262144894Sharti	 * and multicasts even without any local addresses configured.
263144894Sharti	 */
264144894Sharti	if (TAILQ_EMPTY(&in_ifaddrhead))
265144894Sharti		goto bad;
266144894Sharti	ipstat.ips_total++;
267144894Sharti
268144894Sharti	if (m->m_pkthdr.len < sizeof(struct ip))
269145612Sharti		goto tooshort;
270144894Sharti
271144894Sharti	if (m->m_len < sizeof (struct ip) &&
272144894Sharti	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
2731590Srgrimes		ipstat.ips_toosmall++;
274145616Sharti		return;
2751590Srgrimes	}
2761590Srgrimes	ip = mtod(m, struct ip *);
2771590Srgrimes
278145616Sharti	if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
2791590Srgrimes		ipstat.ips_badvers++;
280145616Sharti		goto bad;
281145616Sharti	}
2821590Srgrimes
283145616Sharti	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
2848874Srgrimes	if (hlen < sizeof(struct ip)) {	/* minimum header length */
285145616Sharti		ipstat.ips_badhlen++;
286145616Sharti		goto bad;
287145616Sharti	}
288145616Sharti	if (hlen > m->m_len) {
289145616Sharti		if ((m = m_pullup(m, hlen)) == 0) {
2901590Srgrimes			ipstat.ips_badhlen++;
2911590Srgrimes			return;
2921590Srgrimes		}
2931590Srgrimes		ip = mtod(m, struct ip *);
2941590Srgrimes	}
2951590Srgrimes	if (hlen == sizeof(struct ip)) {
2961590Srgrimes		sum = in_cksum_hdr(ip);
2971590Srgrimes	} else {
2981590Srgrimes		sum = in_cksum(m, hlen);
2991590Srgrimes	}
3001590Srgrimes	if (sum) {
3011590Srgrimes		ipstat.ips_badsum++;
3021590Srgrimes		goto bad;
3031590Srgrimes	}
3041590Srgrimes
3051590Srgrimes	/*
30698136Sjmallett	 * Convert fields to host representation.
3071590Srgrimes	 */
3081590Srgrimes	NTOHS(ip->ip_len);
30993056Simp	if (ip->ip_len < hlen) {
3101590Srgrimes		ipstat.ips_badlen++;
311144745Sharti		goto bad;
312144745Sharti	}
313144745Sharti	NTOHS(ip->ip_id);
3141590Srgrimes	NTOHS(ip->ip_off);
315138232Sharti
316138232Sharti	/*
3171590Srgrimes	 * Check that the amount of data in the buffers
318138232Sharti	 * is as at least much as the IP header would have us expect.
319138232Sharti	 * Trim mbufs if longer than we expect.
3201590Srgrimes	 * Drop packet if shorter than we expect.
3211590Srgrimes	 */
3221590Srgrimes	if (m->m_pkthdr.len < ip->ip_len) {
3231590Srgrimestooshort:
324144341Sharti		ipstat.ips_tooshort++;
325144341Sharti		goto bad;
326144341Sharti	}
327144341Sharti	if (m->m_pkthdr.len > ip->ip_len) {
328144341Sharti		if (m->m_len == m->m_pkthdr.len) {
329144341Sharti			m->m_len = ip->ip_len;
330144341Sharti			m->m_pkthdr.len = ip->ip_len;
331144341Sharti		} else
332144341Sharti			m_adj(m, ip->ip_len - m->m_pkthdr.len);
333144341Sharti	}
334144341Sharti	/*
335144341Sharti	 * IpHack's section.
336144341Sharti	 * Right now when no processing on packet has done
337144341Sharti	 * and it is still fresh out of network we do our black
338144341Sharti	 * deals with it.
339144341Sharti	 * - Firewall: deny/allow/divert
340144341Sharti	 * - Xlate: translate packet's addr/port (NAT).
341144341Sharti	 * - Wrap: fake packet's addr/port <unimpl.>
342144341Sharti	 * - Encapsulate: put it in another IP and send out. <unimp.>
343144341Sharti 	 */
344144341Sharti#if defined(IPFILTER) || defined(IPFILTER_LKM)
345144341Sharti	/*
346144341Sharti	 * Check if we want to allow this packet to be processed.
347144341Sharti	 * Consider it to be bad if not.
348144341Sharti	 */
349144341Sharti	if (fr_checkp) {
350144341Sharti		struct	mbuf	*m1 = m;
351144341Sharti
352144341Sharti		if ((*fr_checkp)(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1)
353144341Sharti			return;
354144341Sharti		ip = mtod(m = m1, struct ip *);
355144341Sharti	}
356144341Sharti#endif
357144341Sharti#ifdef COMPAT_IPFW
358144341Sharti	if (ip_fw_chk_ptr) {
359144341Sharti		u_int16_t	port;
360144341Sharti
361144341Sharti#ifdef IPFIREWALL_FORWARD
362144341Sharti		/*
363144341Sharti		 * If we've been forwarded from the output side, then
364144341Sharti		 * skip the firewall a second time
365144341Sharti		 */
366144341Sharti		if (ip_fw_fwd_addr)
367144341Sharti			goto ours;
368144341Sharti#endif	/* IPFIREWALL_FORWARD */
369144341Sharti#ifdef IPDIVERT
370144341Sharti		port = (*ip_fw_chk_ptr)(&ip, hlen, NULL, &ip_divert_cookie,
371144341Sharti					&m, &ip_fw_fwd_addr);
372144341Sharti		if (port) {
373144341Sharti			/* Divert packet */
374144341Sharti			frag_divert_port = port;
375144341Sharti			goto ours;
376144341Sharti		}
377144341Sharti#else	/* !DIVERT */
378144341Sharti		/*
379144341Sharti		 * If ipfw says divert, we have to just drop packet */
380144341Sharti		 *  Use port as a dummy argument.
381144341Sharti		 */
382144341Sharti		port = 0;
383144341Sharti		if ((*ip_fw_chk_ptr)(&ip, hlen, NULL, &port,
384144341Sharti					&m, &ip_fw_fwd_addr)) {
385144341Sharti			m_freem(m);
386144341Sharti			m = NULL;
387144341Sharti		}
388144341Sharti#endif	/* !DIVERT */
389144341Sharti		if (!m)
390144341Sharti			return;
391144341Sharti	}
392144341Sharti
393144341Sharti        if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, m->m_pkthdr.rcvif, IP_NAT_IN))
394144341Sharti		return;
395144341Sharti#endif	/* !COMPAT_IPFW */
396144341Sharti
397144341Sharti	/*
398144341Sharti	 * Process options and, if not destined for us,
3991590Srgrimes	 * ship it on.  ip_dooptions returns 1 when an
4001590Srgrimes	 * error was detected (causing an icmp message
4011590Srgrimes	 * to be sent and the original packet to be freed).
402143100Sharti	 */
4031590Srgrimes	ip_nhops = 0;		/* for source routed packets */
4041590Srgrimes	if (hlen > sizeof (struct ip) && ip_dooptions(m))
4051590Srgrimes		return;
4061590Srgrimes
407143100Sharti        /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
4081590Srgrimes         * matter if it is destined to another node, or whether it is
4091590Srgrimes         * a multicast one, RSVP wants it! and prevents it from being forwarded
4101590Srgrimes         * anywhere else. Also checks if the rsvp daemon is running before
4111590Srgrimes	 * grabbing the packet.
412143027Sharti         */
413143027Sharti	if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
4141590Srgrimes		goto ours;
415143027Sharti
416143027Sharti	/*
417138232Sharti	 * Check our list of addresses, to see if the packet is for us.
418143027Sharti	 */
419143027Sharti	for (ia = TAILQ_FIRST(&in_ifaddrhead); ia;
420143027Sharti					ia = TAILQ_NEXT(ia, ia_link)) {
421143027Sharti#define	satosin(sa)	((struct sockaddr_in *)(sa))
422143027Sharti
423143027Sharti		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
424143027Sharti			goto ours;
425143027Sharti#ifdef BOOTP_COMPAT
426143027Sharti		if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
4271590Srgrimes			goto ours;
4281590Srgrimes#endif
4291590Srgrimes#ifdef IPFIREWALL_FORWARD
4301590Srgrimes		/*
4311590Srgrimes		 * If the addr to forward to is one of ours, we pretend to
4321590Srgrimes		 * be the destination for this packet.
433143684Sharti		 */
434143684Sharti		if (ip_fw_fwd_addr != NULL &&
435143684Sharti			IA_SIN(ia)->sin_addr.s_addr ==
436143684Sharti					 ip_fw_fwd_addr->sin_addr.s_addr)
4371590Srgrimes			goto ours;
4381590Srgrimes#endif
4391590Srgrimes		if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
4401590Srgrimes			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
4411590Srgrimes			    ip->ip_dst.s_addr)
4421590Srgrimes				goto ours;
443143684Sharti			if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
444143684Sharti				goto ours;
4451590Srgrimes		}
446143684Sharti	}
447143684Sharti	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
448143684Sharti		struct in_multi *inm;
449138232Sharti		if (ip_mrouter) {
450143684Sharti			/*
451143684Sharti			 * If we are acting as a multicast router, all
4521590Srgrimes			 * incoming multicast packets are passed to the
453143684Sharti			 * kernel-level multicast forwarding function.
454143684Sharti			 * The packet is returned (relatively) intact; if
455143684Sharti			 * ip_mforward() returns a non-zero value, the packet
456143684Sharti			 * must be discarded, else it may be accepted below.
457143684Sharti			 *
458143684Sharti			 * (The IP ident field is put in the same byte order
459143684Sharti			 * as expected when ip_mforward() is called from
460143684Sharti			 * ip_output().)
461143684Sharti			 */
462143684Sharti			ip->ip_id = htons(ip->ip_id);
463143684Sharti			if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
464143684Sharti				ipstat.ips_cantforward++;
4658874Srgrimes				m_freem(m);
466143684Sharti				return;
467143684Sharti			}
468143684Sharti			ip->ip_id = ntohs(ip->ip_id);
469143684Sharti
470143684Sharti			/*
471143684Sharti			 * The process-level routing demon needs to receive
472143684Sharti			 * all multicast IGMP packets, whether or not this
473143684Sharti			 * host belongs to their destination groups.
474143684Sharti			 */
475143684Sharti			if (ip->ip_p == IPPROTO_IGMP)
476143684Sharti				goto ours;
477143684Sharti			ipstat.ips_forward++;
4781590Srgrimes		}
479143684Sharti		/*
480143684Sharti		 * See if we belong to the destination multicast group on the
481143684Sharti		 * arrival interface.
482143684Sharti		 */
483143684Sharti		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
484143684Sharti		if (inm == NULL) {
485143684Sharti			ipstat.ips_notmember++;
486143684Sharti			m_freem(m);
487143684Sharti			return;
488143684Sharti		}
489143684Sharti		goto ours;
490143684Sharti	}
4911590Srgrimes	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
492143684Sharti		goto ours;
493143684Sharti	if (ip->ip_dst.s_addr == INADDR_ANY)
494143684Sharti		goto ours;
495143684Sharti
496143684Sharti	/*
497143684Sharti	 * Not for us; forward if possible and desirable.
498143684Sharti	 */
499143684Sharti	if (ipforwarding == 0) {
500143684Sharti		ipstat.ips_cantforward++;
501143684Sharti		m_freem(m);
502143684Sharti	} else
503143684Sharti		ip_forward(m, 0);
504143684Sharti	return;
5051590Srgrimes
5061590Srgrimesours:
5071590Srgrimes
5081590Srgrimes	/*
5091590Srgrimes	 * If offset or IP_MF are set, must reassemble.
5101590Srgrimes	 * Otherwise, nothing need be done.
5111590Srgrimes	 * (We could look in the reassembly queue to see
5121590Srgrimes	 * if the packet was previously fragmented,
5131590Srgrimes	 * but it's not worth the time; just let them time out.)
5141590Srgrimes	 */
5151590Srgrimes	if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) {
5161590Srgrimes		if (m->m_flags & M_EXT) {		/* XXX */
5171590Srgrimes			if ((m = m_pullup(m, sizeof (struct ip))) == 0) {
5181590Srgrimes				ipstat.ips_toosmall++;
5191590Srgrimes#ifdef IPDIVERT
5201590Srgrimes				frag_divert_port = 0;
5211590Srgrimes				ip_divert_cookie = 0;
5221590Srgrimes#endif
5231590Srgrimes				return;
5241590Srgrimes			}
525138512Sharti			ip = mtod(m, struct ip *);
5261590Srgrimes		}
527144026Sharti		sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
528145616Sharti		/*
5291590Srgrimes		 * Look for queue of fragments
530145616Sharti		 * of this datagram.
531145616Sharti		 */
532145616Sharti		for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
533145616Sharti			if (ip->ip_id == fp->ipq_id &&
534144026Sharti			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
535144026Sharti			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
536145616Sharti			    ip->ip_p == fp->ipq_p)
537144026Sharti				goto found;
538144026Sharti
539144026Sharti		fp = 0;
540144026Sharti
5411590Srgrimes		/* check if there's a place for the new queue */
54218730Ssteve		if (nipq > maxnipq) {
543144026Sharti		    /*
544144026Sharti		     * drop something from the tail of the current queue
545144026Sharti		     * before proceeding further
546144026Sharti		     */
547144026Sharti		    if (ipq[sum].prev == &ipq[sum]) {   /* gak */
548144026Sharti			for (i = 0; i < IPREASS_NHASH; i++) {
549144026Sharti			    if (ipq[i].prev != &ipq[i]) {
550144026Sharti				ip_freef(ipq[i].prev);
551144026Sharti				break;
552144026Sharti			    }
553144026Sharti			}
554144026Sharti		    } else
555144026Sharti			ip_freef(ipq[sum].prev);
556144026Sharti		}
557144026Shartifound:
558144026Sharti		/*
559144026Sharti		 * Adjust ip_len to not reflect header,
56018730Ssteve		 * set ip_mff if more fragments are expected,
561144026Sharti		 * convert offset of this to bytes.
562144026Sharti		 */
563144026Sharti		ip->ip_len -= hlen;
564144026Sharti		((struct ipasfrag *)ip)->ipf_mff &= ~1;
565144026Sharti		if (ip->ip_off & IP_MF)
566144026Sharti			((struct ipasfrag *)ip)->ipf_mff |= 1;
567144026Sharti		ip->ip_off <<= 3;
568144026Sharti
569144026Sharti		/*
570144026Sharti		 * If datagram marked as having more fragments
571144026Sharti		 * or if this is not the first fragment,
572144026Sharti		 * attempt reassembly; if it succeeds, proceed.
573144026Sharti		 */
574144026Sharti		if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) {
575144026Sharti			ipstat.ips_fragments++;
576144026Sharti			ip = ip_reass((struct ipasfrag *)ip, fp, &ipq[sum]);
57718730Ssteve			if (ip == 0) {
578144026Sharti#ifdef	IPFIREWALL_FORWARD
579144026Sharti				ip_fw_fwd_addr = NULL;
580144026Sharti#endif
581144026Sharti				return;
582144026Sharti			}
583144026Sharti			/* Get the length of the reassembled packets header */
584144026Sharti			hlen = IP_VHL_HL(ip->ip_vhl) << 2;
585144026Sharti			ipstat.ips_reassembled++;
586144026Sharti			m = dtom(ip);
587144026Sharti#ifdef IPDIVERT
588144026Sharti			if (frag_divert_port) {
589144026Sharti				ip->ip_len += hlen;
590144026Sharti				HTONS(ip->ip_len);
5911590Srgrimes				HTONS(ip->ip_off);
592144026Sharti				HTONS(ip->ip_id);
5931590Srgrimes				ip->ip_sum = 0;
594144026Sharti				ip->ip_sum = in_cksum_hdr(ip);
5951590Srgrimes				NTOHS(ip->ip_id);
596144026Sharti				NTOHS(ip->ip_off);
597144026Sharti				NTOHS(ip->ip_len);
598144026Sharti				ip->ip_len -= hlen;
599144026Sharti			}
600144026Sharti#endif
601144026Sharti		} else
602144026Sharti			if (fp)
603144026Sharti				ip_freef(fp);
604144026Sharti	} else
605144026Sharti		ip->ip_len -= hlen;
606144026Sharti
607144026Sharti#ifdef IPDIVERT
608144026Sharti	/*
609144026Sharti	 * Divert reassembled packets to the divert protocol if required
610144026Sharti	 *  If divert port is null then cookie should be too,
6111590Srgrimes	 * so we shouldn't need to clear them here. Assume ip_divert does so.
61218730Ssteve	 */
613144026Sharti	if (frag_divert_port) {
614144026Sharti		ipstat.ips_delivered++;
615144026Sharti		ip_divert_port = frag_divert_port;
616144026Sharti		frag_divert_port = 0;
617144026Sharti		(*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, hlen);
6181590Srgrimes		return;
619143684Sharti	}
620144026Sharti
621144026Sharti	/* Don't let packets divert themselves */
622144026Sharti	if (ip->ip_p == IPPROTO_DIVERT) {
623143684Sharti		ipstat.ips_noproto++;
624144026Sharti		goto bad;
625144026Sharti	}
626144026Sharti
627144026Sharti#endif
628144026Sharti
629144026Sharti	/*
630144026Sharti	 * Switch out to protocol's input routine.
631144026Sharti	 */
632144026Sharti	ipstat.ips_delivered++;
633144026Sharti	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
634144026Sharti#ifdef	IPFIREWALL_FORWARD
635144026Sharti	ip_fw_fwd_addr = NULL;	/* tcp needed it */
636144026Sharti#endif
637143684Sharti	return;
6381590Srgrimesbad:
6391590Srgrimes#ifdef	IPFIREWALL_FORWARD
640138232Sharti	ip_fw_fwd_addr = NULL;
6411590Srgrimes#endif
6421590Srgrimes	m_freem(m);
6431590Srgrimes}
6441590Srgrimes
6451590Srgrimes/*
6461590Srgrimes * IP software interrupt routine - to go away sometime soon
6471590Srgrimes */
6481590Srgrimesstatic void
6491590Srgrimesipintr(void)
6501590Srgrimes{
6511590Srgrimes	int s;
6521590Srgrimes	struct mbuf *m;
6531590Srgrimes
6541590Srgrimes	while(1) {
6551590Srgrimes		s = splimp();
6561590Srgrimes		IF_DEQUEUE(&ipintrq, m);
6571590Srgrimes		splx(s);
658144026Sharti		if (m == 0)
6591590Srgrimes			return;
6601590Srgrimes		ip_input(m);
6611590Srgrimes	}
6621590Srgrimes}
6631590Srgrimes
6641590SrgrimesNETISR_SET(NETISR_IP, ipintr);
6651590Srgrimes
6661590Srgrimes/*
6671590Srgrimes * Take incoming datagram fragment and try to
6681590Srgrimes * reassemble it into whole datagram.  If a chain for
6691590Srgrimes * reassembly of this datagram already exists, then it
6701590Srgrimes * is given as fp; otherwise have to make a chain.
6711590Srgrimes */
6721590Srgrimesstatic struct ip *
6731590Srgrimesip_reass(ip, fp, where)
6741590Srgrimes	register struct ipasfrag *ip;
6751590Srgrimes	register struct ipq *fp;
676141270Sharti	struct   ipq    *where;
6771590Srgrimes{
678144026Sharti	register struct mbuf *m = dtom(ip);
679144026Sharti	register struct ipasfrag *q;
680144026Sharti	struct mbuf *t;
681144026Sharti	int hlen = ip->ip_hl << 2;
682144026Sharti	int i, next;
683144026Sharti
684144026Sharti	/*
685145616Sharti	 * Presence of header sizes in mbufs
6861590Srgrimes	 * would confuse code below.
687144026Sharti	 */
6881590Srgrimes	m->m_data += hlen;
689144026Sharti	m->m_len -= hlen;
690144026Sharti
691144026Sharti	/*
6921590Srgrimes	 * If first fragment to arrive, create a reassembly queue.
693144026Sharti	 */
694144026Sharti	if (fp == 0) {
695144026Sharti		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
696144026Sharti			goto dropfrag;
697144026Sharti		fp = mtod(t, struct ipq *);
698144026Sharti		insque(fp, where);
699144026Sharti		nipq++;
700144026Sharti		fp->ipq_ttl = IPFRAGTTL;
701144026Sharti		fp->ipq_p = ip->ip_p;
702144026Sharti		fp->ipq_id = ip->ip_id;
703144026Sharti		fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
704144026Sharti		fp->ipq_src = ((struct ip *)ip)->ip_src;
705144026Sharti		fp->ipq_dst = ((struct ip *)ip)->ip_dst;
706144026Sharti#ifdef IPDIVERT
707144026Sharti		fp->ipq_divert = 0;
708144026Sharti		fp->ipq_div_cookie = 0;
7091590Srgrimes#endif
710144026Sharti		q = (struct ipasfrag *)fp;
711144026Sharti		goto insert;
7121590Srgrimes	}
713144026Sharti
714144026Sharti	/*
715144026Sharti	 * Find a segment which begins after this one does.
716144026Sharti	 */
717102178Sru	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
718144026Sharti		if (q->ip_off > ip->ip_off)
719144026Sharti			break;
720144026Sharti
721144026Sharti	/*
722144026Sharti	 * If there is a preceding segment, it may provide some of
723144026Sharti	 * our data already.  If so, drop the data from the incoming
724144026Sharti	 * segment.  If it provides all of our data, drop us.
725144026Sharti	 */
726144026Sharti	if (q->ipf_prev != (struct ipasfrag *)fp) {
727144026Sharti		i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
728144026Sharti		if (i > 0) {
729144026Sharti			if (i >= ip->ip_len)
730144026Sharti				goto dropfrag;
731144026Sharti			m_adj(dtom(ip), i);
732144026Sharti			ip->ip_off += i;
733144026Sharti			ip->ip_len -= i;
734102178Sru		}
735144026Sharti	}
736144026Sharti
737102178Sru	/*
738144026Sharti	 * While we overlap succeeding segments trim them or,
739144026Sharti	 * if they are completely covered, dequeue them.
740144026Sharti	 */
741102178Sru	while (q != (struct ipasfrag *)fp &&
742144026Sharti	    ip->ip_off + ip->ip_len > q->ip_off) {
7438874Srgrimes		struct mbuf *m0;
744144026Sharti
745144026Sharti		i = (ip->ip_off + ip->ip_len) - q->ip_off;
746144026Sharti		if (i < q->ip_len) {
747144026Sharti			q->ip_len -= i;
748144026Sharti			q->ip_off += i;
7491590Srgrimes			m_adj(dtom(q), i);
750144026Sharti			break;
751144026Sharti		}
752144026Sharti		m0 = dtom(q);
753144026Sharti		q = q->ipf_next;
754144026Sharti		ip_deq(q->ipf_prev);
755144026Sharti		m_freem(m0);
756144026Sharti	}
757144026Sharti
758144026Shartiinsert:
759144026Sharti
760144026Sharti#ifdef IPDIVERT
761144026Sharti	/*
762144026Sharti	 * Any fragment diverting causes the whole packet to divert
763144026Sharti	 */
764144026Sharti	if (frag_divert_port) {
765144026Sharti		fp->ipq_divert = frag_divert_port;
766144026Sharti		fp->ipq_div_cookie = ip_divert_cookie;
767144026Sharti	}
768144026Sharti	frag_divert_port = 0;
769144026Sharti	ip_divert_cookie = 0;
770144026Sharti#endif
771144026Sharti
772144026Sharti	/*
773144026Sharti	 * Stick new segment in its place;
774144026Sharti	 * check for complete reassembly.
7758874Srgrimes	 */
776144026Sharti	ip_enq(ip, q->ipf_prev);
777144026Sharti	next = 0;
778144026Sharti	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
779144026Sharti		if (q->ip_off != next)
780144026Sharti			return (0);
781144026Sharti		next += q->ip_len;
782144026Sharti	}
783144026Sharti	if (q->ipf_prev->ipf_mff & 1)
784144026Sharti		return (0);
785144026Sharti
786144026Sharti	/*
787144026Sharti	 * Reassembly is complete.  Make sure the packet is a sane size.
788144026Sharti	 */
789144026Sharti	if (next + (IP_VHL_HL(((struct ip *)fp->ipq_next)->ip_vhl) << 2)
790144026Sharti							> IP_MAXPACKET) {
791144026Sharti		ipstat.ips_toolong++;
792144026Sharti		ip_freef(fp);
7931590Srgrimes		return (0);
794144026Sharti	}
795144026Sharti
7961590Srgrimes	/*
797144026Sharti	 * Concatenate fragments.
798144026Sharti	 */
799144026Sharti	q = fp->ipq_next;
800144026Sharti	m = dtom(q);
801144026Sharti	t = m->m_next;
802145616Sharti	m->m_next = 0;
803145616Sharti	m_cat(m, t);
804144026Sharti	q = q->ipf_next;
805144026Sharti	while (q != (struct ipasfrag *)fp) {
806144026Sharti		t = dtom(q);
807144026Sharti		q = q->ipf_next;
808144026Sharti		m_cat(m, t);
809145616Sharti	}
810145616Sharti
811144026Sharti#ifdef IPDIVERT
812144026Sharti	/*
813144026Sharti	 * extract divert port for packet, if any
814144026Sharti	 */
815144026Sharti	frag_divert_port = fp->ipq_divert;
816144026Sharti	ip_divert_cookie = fp->ipq_div_cookie;
817144026Sharti#endif
818144026Sharti
819144026Sharti	/*
820144026Sharti	 * Create header for new ip packet by
821144026Sharti	 * modifying header of first packet;
822144026Sharti	 * dequeue and discard fragment reassembly header.
823144026Sharti	 * Make header visible.
824144026Sharti	 */
825144026Sharti	ip = fp->ipq_next;
826144026Sharti	ip->ip_len = next;
827144026Sharti	ip->ipf_mff &= ~1;
828144026Sharti	((struct ip *)ip)->ip_src = fp->ipq_src;
829144026Sharti	((struct ip *)ip)->ip_dst = fp->ipq_dst;
830144026Sharti	remque(fp);
831144026Sharti	nipq--;
832144026Sharti	(void) m_free(dtom(fp));
833144026Sharti	m = dtom(ip);
834144026Sharti	m->m_len += (ip->ip_hl << 2);
835144026Sharti	m->m_data -= (ip->ip_hl << 2);
836144026Sharti	/* some debugging cruft by sklower, below, will go away soon */
837144026Sharti	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
838144026Sharti		register int plen = 0;
839144026Sharti		for (t = m; m; m = m->m_next)
840144026Sharti			plen += m->m_len;
841144026Sharti		t->m_pkthdr.len = plen;
842144026Sharti	}
843144026Sharti	return ((struct ip *)ip);
844144026Sharti
845144026Shartidropfrag:
846144026Sharti#ifdef IPDIVERT
847144026Sharti	frag_divert_port = 0;
848144026Sharti	ip_divert_cookie = 0;
849144026Sharti#endif
850144026Sharti	ipstat.ips_fragdropped++;
851144026Sharti	m_freem(m);
852144026Sharti	return (0);
853144026Sharti}
854144026Sharti
855144026Sharti/*
856144026Sharti * Free a fragment reassembly header and all
857144026Sharti * associated datagrams.
858144026Sharti */
859144026Shartistatic void
860144026Shartiip_freef(fp)
861144026Sharti	struct ipq *fp;
862144026Sharti{
863144026Sharti	register struct ipasfrag *q, *p;
864144026Sharti
865144026Sharti	for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {
866144026Sharti		p = q->ipf_next;
867144026Sharti		ip_deq(q);
868144026Sharti		m_freem(dtom(q));
869144026Sharti	}
870144026Sharti	remque(fp);
871144026Sharti	(void) m_free(dtom(fp));
872144026Sharti	nipq--;
873144026Sharti}
874144026Sharti
875144026Sharti/*
876144026Sharti * Put an ip fragment on a reassembly chain.
877144026Sharti * Like insque, but pointers in middle of structure.
878144026Sharti */
879144026Shartistatic void
880144026Shartiip_enq(p, prev)
881144026Sharti	register struct ipasfrag *p, *prev;
882144026Sharti{
883144026Sharti
884144026Sharti	p->ipf_prev = prev;
885144026Sharti	p->ipf_next = prev->ipf_next;
886144026Sharti	prev->ipf_next->ipf_prev = p;
887144026Sharti	prev->ipf_next = p;
888144026Sharti}
889144026Sharti
890144026Sharti/*
891144026Sharti * To ip_enq as remque is to insque.
892144026Sharti */
893144026Shartistatic void
8941590Srgrimesip_deq(p)
8951590Srgrimes	register struct ipasfrag *p;
896144026Sharti{
8971590Srgrimes
898144026Sharti	p->ipf_prev->ipf_next = p->ipf_next;
899144026Sharti	p->ipf_next->ipf_prev = p->ipf_prev;
9001590Srgrimes}
901144029Sharti
9028874Srgrimes/*
903144026Sharti * IP timer processing;
904144026Sharti * if a timer expires on a reassembly
9058874Srgrimes * queue, discard it.
906144026Sharti */
907144026Shartivoid
908144026Shartiip_slowtimo()
909144026Sharti{
910144026Sharti	register struct ipq *fp;
911144026Sharti	int s = splnet();
912144026Sharti	int i;
913144026Sharti
914144026Sharti	for (i = 0; i < IPREASS_NHASH; i++) {
915144026Sharti		fp = ipq[i].next;
916138916Sharti		if (fp == 0)
917144026Sharti			continue;
918144026Sharti		while (fp != &ipq[i]) {
9198874Srgrimes			--fp->ipq_ttl;
920144026Sharti			fp = fp->next;
921144026Sharti			if (fp->prev->ipq_ttl == 0) {
922144026Sharti				ipstat.ips_fragtimeout++;
923144026Sharti				ip_freef(fp->prev);
924144026Sharti			}
925144026Sharti		}
926144026Sharti	}
927144026Sharti	ipflow_slowtimo();
9288874Srgrimes	splx(s);
929144026Sharti}
930144026Sharti
931144026Sharti/*
932144026Sharti * Drain off all datagram fragments.
933144026Sharti */
934144026Shartivoid
935144026Shartiip_drain()
936144026Sharti{
937144026Sharti	int     i;
938144026Sharti
939144026Sharti	for (i = 0; i < IPREASS_NHASH; i++) {
940144026Sharti		while (ipq[i].next != &ipq[i]) {
941144026Sharti			ipstat.ips_fragdropped++;
942144026Sharti			ip_freef(ipq[i].next);
943144026Sharti		}
944144026Sharti	}
945144026Sharti	in_rtqdrain();
946144026Sharti}
9471590Srgrimes
948144026Sharti/*
949144026Sharti * Do option processing on a datagram,
9501590Srgrimes * possibly discarding it if bad options are encountered,
951144026Sharti * or forwarding it if source-routed.
952144026Sharti * Returns 1 if packet has been forwarded/freed,
9538874Srgrimes * 0 if the packet should be processed further.
954144029Sharti */
955144026Shartistatic int
956144026Shartiip_dooptions(m)
957144026Sharti	struct mbuf *m;
958144026Sharti{
959144026Sharti	register struct ip *ip = mtod(m, struct ip *);
960144026Sharti	register u_char *cp;
961144026Sharti	register struct ip_timestamp *ipt;
962144026Sharti	register struct in_ifaddr *ia;
963144026Sharti	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
9641590Srgrimes	struct in_addr *sin, dst;
965144026Sharti	n_time ntime;
966144026Sharti
967144026Sharti	dst = ip->ip_dst;
9681590Srgrimes	cp = (u_char *)(ip + 1);
969144026Sharti	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
970144029Sharti	for (; cnt > 0; cnt -= optlen, cp += optlen) {
9718874Srgrimes		opt = cp[IPOPT_OPTVAL];
972144026Sharti		if (opt == IPOPT_EOL)
973144026Sharti			break;
974144026Sharti		if (opt == IPOPT_NOP)
975144026Sharti			optlen = 1;
976144026Sharti		else {
977144026Sharti			optlen = cp[IPOPT_OLEN];
978144026Sharti			if (optlen <= 0 || optlen > cnt) {
979144026Sharti				code = &cp[IPOPT_OLEN] - (u_char *)ip;
980144026Sharti				goto bad;
981144026Sharti			}
982144026Sharti		}
983144026Sharti		switch (opt) {
984144026Sharti
985144026Sharti		default:
986144026Sharti			break;
987144026Sharti
988144026Sharti		/*
989144026Sharti		 * Source routing with record.
990144026Sharti		 * Find interface with current destination address.
991144026Sharti		 * If none on this machine then drop if strictly routed,
992144026Sharti		 * or do nothing if loosely routed.
9931590Srgrimes		 * Record interface address and bring up next address
9948874Srgrimes		 * component.  If strictly routed make sure next
9951590Srgrimes		 * address is on directly accessible net.
996144026Sharti		 */
997144026Sharti		case IPOPT_LSRR:
9981590Srgrimes		case IPOPT_SSRR:
999144026Sharti			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1000144026Sharti				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1001144026Sharti				goto bad;
1002144026Sharti			}
1003144026Sharti			ipaddr.sin_addr = ip->ip_dst;
1004144026Sharti			ia = (struct in_ifaddr *)
1005144026Sharti				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1006144026Sharti			if (ia == 0) {
10071590Srgrimes				if (opt == IPOPT_SSRR) {
10081590Srgrimes					type = ICMP_UNREACH;
1009144026Sharti					code = ICMP_UNREACH_SRCFAIL;
1010144026Sharti					goto bad;
1011144026Sharti				}
1012144026Sharti				if (!ip_dosourceroute)
1013144026Sharti					goto nosourcerouting;
1014144026Sharti				/*
1015144026Sharti				 * Loose routing, and not at next destination
1016144026Sharti				 * yet; nothing to do except forward.
1017144026Sharti				 */
1018144026Sharti				break;
1019144026Sharti			}
1020144026Sharti			off--;			/* 0 origin */
10211590Srgrimes			if (off > optlen - sizeof(struct in_addr)) {
10221590Srgrimes				/*
10231590Srgrimes				 * End of source route.  Should be for us.
10241590Srgrimes				 */
1025144026Sharti				if (!ip_acceptsourceroute)
1026144026Sharti					goto nosourcerouting;
1027144026Sharti				save_rte(cp, ip->ip_src);
1028144026Sharti				break;
1029144026Sharti			}
1030144026Sharti
1031144026Sharti			if (!ip_dosourceroute) {
1032144026Sharti				char buf[4*sizeof "123"];
1033144026Sharti
1034144026Shartinosourcerouting:
1035144026Sharti				strcpy(buf, inet_ntoa(ip->ip_dst));
1036144026Sharti				log(LOG_WARNING,
1037144026Sharti				    "attempted source route from %s to %s\n",
1038144026Sharti				    inet_ntoa(ip->ip_src), buf);
1039144026Sharti				type = ICMP_UNREACH;
1040144026Sharti				code = ICMP_UNREACH_SRCFAIL;
1041144026Sharti				goto bad;
1042144026Sharti			}
1043144026Sharti
1044144026Sharti			/*
1045144026Sharti			 * locate outgoing interface
1046144026Sharti			 */
1047144026Sharti			(void)memcpy(&ipaddr.sin_addr, cp + off,
1048144026Sharti			    sizeof(ipaddr.sin_addr));
1049144026Sharti
1050144026Sharti			if (opt == IPOPT_SSRR) {
1051144026Sharti#define	INA	struct in_ifaddr *
1052144026Sharti#define	SA	struct sockaddr *
1053144026Sharti			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1054144026Sharti				ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1055144026Sharti			} else
1056144026Sharti				ia = ip_rtaddr(ipaddr.sin_addr);
1057144026Sharti			if (ia == 0) {
1058144026Sharti				type = ICMP_UNREACH;
1059144026Sharti				code = ICMP_UNREACH_SRCFAIL;
10601590Srgrimes				goto bad;
1061144026Sharti			}
1062144026Sharti			ip->ip_dst = ipaddr.sin_addr;
1063144026Sharti			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
10641590Srgrimes			    sizeof(struct in_addr));
1065144026Sharti			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1066144026Sharti			/*
10671590Srgrimes			 * Let ip_intr's mcast routing check handle mcast pkts
1068144026Sharti			 */
1069144026Sharti			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1070144026Sharti			break;
1071144026Sharti
1072144026Sharti		case IPOPT_RR:
1073144026Sharti			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1074144026Sharti				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1075144026Sharti				goto bad;
1076144029Sharti			}
1077144026Sharti			/*
10781590Srgrimes			 * If no space remains, ignore.
10791590Srgrimes			 */
1080144026Sharti			off--;			/* 0 origin */
1081144026Sharti			if (off > optlen - sizeof(struct in_addr))
1082144026Sharti				break;
1083144029Sharti			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1084144029Sharti			    sizeof(ipaddr.sin_addr));
1085144029Sharti			/*
1086144026Sharti			 * locate outgoing interface; if we're the destination,
1087144026Sharti			 * use the incoming interface (should be same).
1088144026Sharti			 */
1089144026Sharti			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1090144026Sharti			    (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
1091144026Sharti				type = ICMP_UNREACH;
1092144026Sharti				code = ICMP_UNREACH_HOST;
1093144026Sharti				goto bad;
1094144026Sharti			}
1095144026Sharti			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1096144026Sharti			    sizeof(struct in_addr));
1097144026Sharti			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1098144026Sharti			break;
1099144026Sharti
1100144026Sharti		case IPOPT_TS:
1101144026Sharti			code = cp - (u_char *)ip;
1102144026Sharti			ipt = (struct ip_timestamp *)cp;
1103144026Sharti			if (ipt->ipt_len < 5)
1104144026Sharti				goto bad;
1105144026Sharti			if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
1106144026Sharti				if (++ipt->ipt_oflw == 0)
1107144026Sharti					goto bad;
1108144026Sharti				break;
1109144026Sharti			}
1110144026Sharti			sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
1111144026Sharti			switch (ipt->ipt_flg) {
1112144026Sharti
1113144026Sharti			case IPOPT_TS_TSONLY:
1114144026Sharti				break;
1115144026Sharti
1116144026Sharti			case IPOPT_TS_TSANDADDR:
1117144026Sharti				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1118144026Sharti				    sizeof(struct in_addr) > ipt->ipt_len)
1119144026Sharti					goto bad;
1120144026Sharti				ipaddr.sin_addr = dst;
1121144026Sharti				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1122144026Sharti							    m->m_pkthdr.rcvif);
1123144026Sharti				if (ia == 0)
1124144026Sharti					continue;
1125144026Sharti				(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1126144026Sharti				    sizeof(struct in_addr));
1127144026Sharti				ipt->ipt_ptr += sizeof(struct in_addr);
1128144026Sharti				break;
1129144026Sharti
1130144026Sharti			case IPOPT_TS_PRESPEC:
1131144026Sharti				if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1132144026Sharti				    sizeof(struct in_addr) > ipt->ipt_len)
1133144026Sharti					goto bad;
1134144026Sharti				(void)memcpy(&ipaddr.sin_addr, sin,
1135144026Sharti				    sizeof(struct in_addr));
1136144026Sharti				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1137144026Sharti					continue;
1138144026Sharti				ipt->ipt_ptr += sizeof(struct in_addr);
1139144026Sharti				break;
1140144026Sharti
1141144026Sharti			default:
1142144026Sharti				goto bad;
1143144026Sharti			}
1144144026Sharti			ntime = iptime();
1145144026Sharti			(void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
1146144026Sharti			    sizeof(n_time));
1147144026Sharti			ipt->ipt_ptr += sizeof(n_time);
1148144026Sharti		}
11491590Srgrimes	}
1150144026Sharti	if (forward && ipforwarding) {
1151144026Sharti		ip_forward(m, 1);
1152144026Sharti		return (1);
11531590Srgrimes	}
1154144026Sharti	return (0);
1155144026Shartibad:
1156144026Sharti	ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2;   /* XXX icmp_error adds in hdr length */
1157144026Sharti	icmp_error(m, type, code, 0, 0);
1158144026Sharti	ipstat.ips_badoptions++;
1159144026Sharti	return (1);
1160144026Sharti}
1161144026Sharti
1162144026Sharti/*
1163144026Sharti * Given address of next destination (final or next hop),
1164144026Sharti * return internet address info of interface to be used to get there.
1165144026Sharti */
1166144026Shartistatic struct in_ifaddr *
1167144026Shartiip_rtaddr(dst)
1168144026Sharti	 struct in_addr dst;
1169144026Sharti{
1170144026Sharti	register struct sockaddr_in *sin;
1171144026Sharti
1172144026Sharti	sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
1173144026Sharti
11741590Srgrimes	if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
1175144026Sharti		if (ipforward_rt.ro_rt) {
1176144026Sharti			RTFREE(ipforward_rt.ro_rt);
11778874Srgrimes			ipforward_rt.ro_rt = 0;
1178144026Sharti		}
1179144026Sharti		sin->sin_family = AF_INET;
1180138916Sharti		sin->sin_len = sizeof(*sin);
1181144026Sharti		sin->sin_addr = dst;
1182144026Sharti
1183144026Sharti		rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1184144026Sharti	}
1185144026Sharti	if (ipforward_rt.ro_rt == 0)
1186144026Sharti		return ((struct in_ifaddr *)0);
1187141270Sharti	return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
1188144026Sharti}
1189144026Sharti
1190144026Sharti/*
1191144026Sharti * Save incoming source route for use in replies,
1192144026Sharti * to be picked up later by ip_srcroute if the receiver is interested.
1193144026Sharti */
1194144026Shartivoid
1195144026Shartisave_rte(option, dst)
1196144026Sharti	u_char *option;
1197144026Sharti	struct in_addr dst;
11981590Srgrimes{
1199144026Sharti	unsigned olen;
1200144026Sharti
1201144026Sharti	olen = option[IPOPT_OLEN];
1202144026Sharti#ifdef DIAGNOSTIC
1203144026Sharti	if (ipprintfs)
1204144026Sharti		printf("save_rte: olen %d\n", olen);
12051590Srgrimes#endif
1206144026Sharti	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1207144026Sharti		return;
12081590Srgrimes	bcopy(option, ip_srcrt.srcopt, olen);
1209144026Sharti	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1210144026Sharti	ip_srcrt.dst = dst;
1211144026Sharti}
1212144026Sharti
1213144026Sharti/*
1214144026Sharti * Retrieve incoming source route for use in replies,
1215144026Sharti * in the same form used by setsockopt.
1216144026Sharti * The first hop is placed before the options, will be removed later.
1217144026Sharti */
1218144026Shartistruct mbuf *
1219144026Shartiip_srcroute()
1220144026Sharti{
1221144026Sharti	register struct in_addr *p, *q;
1222144026Sharti	register struct mbuf *m;
1223144026Sharti
12241590Srgrimes	if (ip_nhops == 0)
12251590Srgrimes		return ((struct mbuf *)0);
12261590Srgrimes	m = m_get(M_DONTWAIT, MT_SOOPTS);
12271590Srgrimes	if (m == 0)
12281590Srgrimes		return ((struct mbuf *)0);
12291590Srgrimes
12301590Srgrimes#define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
12311590Srgrimes
12321590Srgrimes	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
12331590Srgrimes	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
12341590Srgrimes	    OPTSIZ;
12351590Srgrimes#ifdef DIAGNOSTIC
12361590Srgrimes	if (ipprintfs)
12371590Srgrimes		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
12381590Srgrimes#endif
12391590Srgrimes
12401590Srgrimes	/*
12411590Srgrimes	 * First save first hop for return route
12421590Srgrimes	 */
12431590Srgrimes	p = &ip_srcrt.route[ip_nhops - 1];
12441590Srgrimes	*(mtod(m, struct in_addr *)) = *p--;
1245138232Sharti#ifdef DIAGNOSTIC
12461590Srgrimes	if (ipprintfs)
1247144026Sharti		printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr));
1248144026Sharti#endif
1249138232Sharti
1250144026Sharti	/*
1251103503Sjmallett	 * Copy option fields and padding (nop) to mbuf.
1252144029Sharti	 */
12531590Srgrimes	ip_srcrt.nop = IPOPT_NOP;
1254144026Sharti	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1255144026Sharti	(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1256144026Sharti	    &ip_srcrt.nop, OPTSIZ);
1257144029Sharti	q = (struct in_addr *)(mtod(m, caddr_t) +
1258144026Sharti	    sizeof(struct in_addr) + OPTSIZ);
12591590Srgrimes#undef OPTSIZ
1260144026Sharti	/*
1261144026Sharti	 * Record return path as an IP source route,
1262144026Sharti	 * reversing the path (pointers are now aligned).
1263144026Sharti	 */
1264144026Sharti	while (p >= ip_srcrt.route) {
1265144026Sharti#ifdef DIAGNOSTIC
1266144026Sharti		if (ipprintfs)
126718730Ssteve			printf(" %lx", ntohl(q->s_addr));
1268144026Sharti#endif
1269144026Sharti		*q++ = *p--;
1270144026Sharti	}
1271144026Sharti	/*
1272144026Sharti	 * Last hop goes to final destination.
1273144026Sharti	 */
1274144026Sharti	*q = ip_srcrt.dst;
1275144026Sharti#ifdef DIAGNOSTIC
127618730Ssteve	if (ipprintfs)
1277144026Sharti		printf(" %lx\n", ntohl(q->s_addr));
1278144026Sharti#endif
1279144026Sharti	return (m);
1280144026Sharti}
128118730Ssteve
1282144026Sharti/*
1283144026Sharti * Strip out IP options, at higher
1284144026Sharti * level protocol in the kernel.
1285144026Sharti * Second argument is buffer to which options
128618730Ssteve * will be moved, and return value is their length.
1287144026Sharti * XXX should be deleted; last arg currently ignored.
1288144026Sharti */
1289144026Shartivoid
1290144026Shartiip_stripoptions(m, mopt)
1291144026Sharti	register struct mbuf *m;
1292144026Sharti	struct mbuf *mopt;
1293144026Sharti{
1294144026Sharti	register int i;
129518730Ssteve	struct ip *ip = mtod(m, struct ip *);
1296144026Sharti	register caddr_t opts;
1297144026Sharti	int olen;
1298144026Sharti
1299144026Sharti	olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1300144026Sharti	opts = (caddr_t)(ip + 1);
1301144026Sharti	i = m->m_len - (sizeof (struct ip) + olen);
1302144026Sharti	bcopy(opts + olen, opts, (unsigned)i);
130318730Ssteve	m->m_len -= olen;
1304144026Sharti	if (m->m_flags & M_PKTHDR)
1305144026Sharti		m->m_pkthdr.len -= olen;
1306144026Sharti	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1307144026Sharti}
1308144026Sharti
130918730Ssteveu_char inetctlerrmap[PRC_NCMDS] = {
1310144026Sharti	0,		0,		0,		0,
1311144026Sharti	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1312144026Sharti	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1313144026Sharti	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1314144026Sharti	0,		0,		0,		0,
1315144026Sharti	ENOPROTOOPT
1316144026Sharti};
1317144026Sharti
1318144026Sharti/*
1319144026Sharti * Forward a packet.  If some error occurs return the sender
1320144026Sharti * an icmp packet.  Note we can't always generate a meaningful
1321144026Sharti * icmp message because icmp doesn't have a large enough repertoire
1322144026Sharti * of codes and types.
13231590Srgrimes *
13241590Srgrimes * If not forwarding, just drop the packet.  This could be confusing
1325144026Sharti * if ipforwarding was zero but some routing protocol was advancing
13261590Srgrimes * us as a gateway to somewhere.  However, we must let the routing
13271590Srgrimes * protocol deal with that.
13281590Srgrimes *
13291590Srgrimes * The srcrt parameter indicates whether the packet is being forwarded
13301590Srgrimes * via a source route.
13311590Srgrimes */
13321590Srgrimesstatic void
13331590Srgrimesip_forward(m, srcrt)
13341590Srgrimes	struct mbuf *m;
13351590Srgrimes	int srcrt;
13361590Srgrimes{
13371590Srgrimes	register struct ip *ip = mtod(m, struct ip *);
13381590Srgrimes	register struct sockaddr_in *sin;
13391590Srgrimes	register struct rtentry *rt;
13401590Srgrimes	int error, type = 0, code = 0;
13411590Srgrimes	struct mbuf *mcopy;
13421590Srgrimes	n_long dest;
13431590Srgrimes	struct ifnet *destifp;
13441590Srgrimes
13451590Srgrimes	dest = 0;
13461590Srgrimes#ifdef DIAGNOSTIC
13471590Srgrimes	if (ipprintfs)
13481590Srgrimes		printf("forward: src %lx dst %lx ttl %x\n",
1349138232Sharti			ip->ip_src.s_addr, ip->ip_dst.s_addr, ip->ip_ttl);
13501590Srgrimes#endif
1351144026Sharti
1352144026Sharti
1353144026Sharti	if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
1354144026Sharti		ipstat.ips_cantforward++;
1355144026Sharti		m_freem(m);
1356144026Sharti		return;
1357144026Sharti	}
1358144026Sharti	HTONS(ip->ip_id);
1359144026Sharti	if (ip->ip_ttl <= IPTTLDEC) {
1360144026Sharti		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1361144026Sharti		return;
1362144026Sharti	}
1363144026Sharti	ip->ip_ttl -= IPTTLDEC;
1364144026Sharti
13651590Srgrimes	sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
1366144026Sharti	if ((rt = ipforward_rt.ro_rt) == 0 ||
1367144026Sharti	    ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
13685814Sjkh		if (ipforward_rt.ro_rt) {
13691590Srgrimes			RTFREE(ipforward_rt.ro_rt);
1370144026Sharti			ipforward_rt.ro_rt = 0;
1371144026Sharti		}
1372144026Sharti		sin->sin_family = AF_INET;
1373144029Sharti		sin->sin_len = sizeof(*sin);
1374144026Sharti		sin->sin_addr = ip->ip_dst;
1375144026Sharti
13761590Srgrimes		rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1377144026Sharti		if (ipforward_rt.ro_rt == 0) {
1378144026Sharti			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1379144026Sharti			return;
1380144026Sharti		}
1381144026Sharti		rt = ipforward_rt.ro_rt;
1382144026Sharti	}
1383144026Sharti
13841590Srgrimes	/*
1385144026Sharti	 * Save at most 64 bytes of the packet in case
1386144026Sharti	 * we need to generate an ICMP message to the src.
13871590Srgrimes	 */
1388144026Sharti	mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
1389144026Sharti
1390144026Sharti	/*
1391144026Sharti	 * If forwarding packet using same interface that it came in on,
1392144026Sharti	 * perhaps should send a redirect to sender to shortcut a hop.
1393144026Sharti	 * Only send redirect if source is sending directly to us,
1394144026Sharti	 * and if packet was not source routed (or has any options).
1395144026Sharti	 * Also, don't send redirect if forwarding using a default route
13961590Srgrimes	 * or a route modified by a redirect.
1397144026Sharti	 */
1398144026Sharti#define	satosin(sa)	((struct sockaddr_in *)(sa))
1399144026Sharti	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1400144026Sharti	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1401144026Sharti	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1402144026Sharti	    ipsendredirects && !srcrt) {
1403144026Sharti#define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1404144026Sharti		u_long src = ntohl(ip->ip_src.s_addr);
1405144026Sharti
1406144026Sharti		if (RTA(rt) &&
1407144026Sharti		    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
14081590Srgrimes		    if (rt->rt_flags & RTF_GATEWAY)
1409144026Sharti			dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1410144026Sharti		    else
1411144026Sharti			dest = ip->ip_dst.s_addr;
1412144026Sharti		    /* Router requirements says to only send host redirects */
14131590Srgrimes		    type = ICMP_REDIRECT;
1414144026Sharti		    code = ICMP_REDIRECT_HOST;
1415144026Sharti#ifdef DIAGNOSTIC
1416144026Sharti		    if (ipprintfs)
1417144026Sharti		        printf("redirect (%d) to %lx\n", code, (u_long)dest);
14181590Srgrimes#endif
1419144026Sharti		}
142018730Ssteve	}
1421144026Sharti
1422144026Sharti	error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1423144026Sharti			  IP_FORWARDING, 0);
1424144026Sharti	if (error)
1425144026Sharti		ipstat.ips_cantforward++;
1426144026Sharti	else {
142718730Ssteve		ipstat.ips_forward++;
1428144026Sharti		if (type)
1429144026Sharti			ipstat.ips_redirectsent++;
1430144026Sharti		else {
1431144026Sharti			if (mcopy) {
1432144026Sharti				ipflow_create(&ipforward_rt, mcopy);
1433144026Sharti				m_freem(mcopy);
1434144026Sharti			}
143518730Ssteve			return;
1436144026Sharti		}
14371590Srgrimes	}
1438144026Sharti	if (mcopy == NULL)
1439144026Sharti		return;
1440144026Sharti	destifp = NULL;
14411590Srgrimes
1442144026Sharti	switch (error) {
1443144026Sharti
14441590Srgrimes	case 0:				/* forwarded, but need redirect */
1445144026Sharti		/* type, code set above */
1446144026Sharti		break;
1447144026Sharti
1448144026Sharti	case ENETUNREACH:		/* shouldn't happen, checked above */
1449144026Sharti	case EHOSTUNREACH:
1450144026Sharti	case ENETDOWN:
1451144026Sharti	case EHOSTDOWN:
1452144026Sharti	default:
1453144026Sharti		type = ICMP_UNREACH;
1454144026Sharti		code = ICMP_UNREACH_HOST;
1455144026Sharti		break;
1456144026Sharti
1457144026Sharti	case EMSGSIZE:
1458144026Sharti		type = ICMP_UNREACH;
145998509Sjmallett		code = ICMP_UNREACH_NEEDFRAG;
1460144026Sharti		if (ipforward_rt.ro_rt)
146198509Sjmallett			destifp = ipforward_rt.ro_rt->rt_ifp;
1462144026Sharti		ipstat.ips_cantfrag++;
1463144026Sharti		break;
1464144026Sharti
1465144026Sharti	case ENOBUFS:
1466144026Sharti		type = ICMP_SOURCEQUENCH;
1467144026Sharti		code = 0;
1468142457Sharti		break;
1469144026Sharti	}
14701590Srgrimes	icmp_error(mcopy, type, code, dest, destifp);
1471144026Sharti}
14721590Srgrimes
1473144026Shartivoid
1474144026Shartiip_savecontrol(inp, mp, ip, m)
14751590Srgrimes	register struct inpcb *inp;
1476144026Sharti	register struct mbuf **mp;
1477144026Sharti	register struct ip *ip;
1478144026Sharti	register struct mbuf *m;
1479144026Sharti{
1480144026Sharti	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1481144026Sharti		struct timeval tv;
1482144026Sharti
1483144026Sharti		microtime(&tv);
14841590Srgrimes		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1485144026Sharti			SCM_TIMESTAMP, SOL_SOCKET);
1486144026Sharti		if (*mp)
1487144026Sharti			mp = &(*mp)->m_next;
1488144026Sharti	}
1489144026Sharti	if (inp->inp_flags & INP_RECVDSTADDR) {
1490144026Sharti		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1491144026Sharti		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1492144026Sharti		if (*mp)
1493144026Sharti			mp = &(*mp)->m_next;
1494144026Sharti	}
14951590Srgrimes#ifdef notyet
1496144026Sharti	/* XXX
1497144026Sharti	 * Moving these out of udp_input() made them even more broken
1498144026Sharti	 * than they already were.
1499144026Sharti	 */
1500144026Sharti	/* options were tossed already */
1501144026Sharti	if (inp->inp_flags & INP_RECVOPTS) {
1502144026Sharti		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1503144026Sharti		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1504144026Sharti		if (*mp)
1505144026Sharti			mp = &(*mp)->m_next;
1506144026Sharti	}
1507144026Sharti	/* ip_srcroute doesn't do what we want here, need to fix */
1508144026Sharti	if (inp->inp_flags & INP_RECVRETOPTS) {
1509144026Sharti		*mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1510144026Sharti		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1511144026Sharti		if (*mp)
15121590Srgrimes			mp = &(*mp)->m_next;
15131590Srgrimes	}
15141590Srgrimes#endif
15151590Srgrimes	if (inp->inp_flags & INP_RECVIF) {
15161590Srgrimes		struct ifnet *ifp;
15171590Srgrimes		struct sdlbuf {
15181590Srgrimes			struct sockaddr_dl sdl;
15191590Srgrimes			u_char	pad[32];
15201590Srgrimes		} sdlbuf;
15211590Srgrimes		struct sockaddr_dl *sdp;
15221590Srgrimes		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
15235814Sjkh
15241590Srgrimes		if (((ifp = m->m_pkthdr.rcvif))
15251590Srgrimes		&& ( ifp->if_index && (ifp->if_index <= if_index))) {
15261590Srgrimes			sdp = (struct sockaddr_dl *)(ifnet_addrs
15271590Srgrimes					[ifp->if_index - 1]->ifa_addr);
15281590Srgrimes			/*
15291590Srgrimes			 * Change our mind and don't try copy.
15305814Sjkh			 */
1531104696Sjmallett			if ((sdp->sdl_family != AF_LINK)
15321590Srgrimes			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1533144026Sharti				goto makedummy;
1534138232Sharti			}
1535144026Sharti			bcopy(sdp, sdl2, sdp->sdl_len);
1536144026Sharti		} else {
1537144026Shartimakedummy:
15381590Srgrimes			sdl2->sdl_len
15391590Srgrimes				= offsetof(struct sockaddr_dl, sdl_data[0]);
15401590Srgrimes			sdl2->sdl_family = AF_LINK;
15411590Srgrimes			sdl2->sdl_index = 0;
15421590Srgrimes			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
15431590Srgrimes		}
15441590Srgrimes		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
15451590Srgrimes			IP_RECVIF, IPPROTO_IP);
15461590Srgrimes		if (*mp)
15471590Srgrimes			mp = &(*mp)->m_next;
15481590Srgrimes	}
15491590Srgrimes}
15501590Srgrimes
15511590Srgrimesint
15521590Srgrimesip_rsvp_init(struct socket *so)
15531590Srgrimes{
15541590Srgrimes	if (so->so_type != SOCK_RAW ||
1555138232Sharti	    so->so_proto->pr_protocol != IPPROTO_RSVP)
15561590Srgrimes	  return EOPNOTSUPP;
1557138232Sharti
1558144026Sharti	if (ip_rsvpd != NULL)
15591590Srgrimes	  return EADDRINUSE;
15601590Srgrimes
15611590Srgrimes	ip_rsvpd = so;
15621590Srgrimes	/*
15631590Srgrimes	 * This may seem silly, but we need to be sure we don't over-increment
15641590Srgrimes	 * the RSVP counter, in case something slips up.
15658874Srgrimes	 */
15661590Srgrimes	if (!ip_rsvp_on) {
15671590Srgrimes		ip_rsvp_on = 1;
15681590Srgrimes		rsvp_on++;
15691590Srgrimes	}
1570126824Sru
1571126824Sru	return 0;
15721590Srgrimes}
15731590Srgrimes
15741590Srgrimesint
1575126824Sruip_rsvp_done(void)
15761590Srgrimes{
15771590Srgrimes	ip_rsvpd = NULL;
1578144026Sharti	/*
15791590Srgrimes	 * This may seem silly, but we need to be sure we don't over-decrement
1580144341Sharti	 * the RSVP counter, in case something slips up.
15811590Srgrimes	 */
15821590Srgrimes	if (ip_rsvp_on) {
15831590Srgrimes		ip_rsvp_on = 0;
15841590Srgrimes		rsvp_on--;
15851590Srgrimes	}
15861590Srgrimes	return 0;
15871590Srgrimes}
15888874Srgrimes