ip_input.c revision 101095
1/*
2 * Copyright (c) 1982, 1986, 1988, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
34 * $FreeBSD: head/sys/netinet/ip_input.c 101095 2002-07-31 17:17:51Z rwatson $
35 */
36
37#define	_IP_VHL
38
39#include "opt_bootp.h"
40#include "opt_ipfw.h"
41#include "opt_ipdn.h"
42#include "opt_ipdivert.h"
43#include "opt_ipfilter.h"
44#include "opt_ipstealth.h"
45#include "opt_ipsec.h"
46#include "opt_mac.h"
47#include "opt_pfil_hooks.h"
48#include "opt_random_ip_id.h"
49
50#include <sys/param.h>
51#include <sys/systm.h>
52#include <sys/mac.h>
53#include <sys/mbuf.h>
54#include <sys/malloc.h>
55#include <sys/domain.h>
56#include <sys/protosw.h>
57#include <sys/socket.h>
58#include <sys/time.h>
59#include <sys/kernel.h>
60#include <sys/syslog.h>
61#include <sys/sysctl.h>
62
63#include <net/pfil.h>
64#include <net/if.h>
65#include <net/if_types.h>
66#include <net/if_var.h>
67#include <net/if_dl.h>
68#include <net/route.h>
69#include <net/netisr.h>
70#include <net/intrq.h>
71
72#include <netinet/in.h>
73#include <netinet/in_systm.h>
74#include <netinet/in_var.h>
75#include <netinet/ip.h>
76#include <netinet/in_pcb.h>
77#include <netinet/ip_var.h>
78#include <netinet/ip_icmp.h>
79#include <machine/in_cksum.h>
80
81#include <sys/socketvar.h>
82
83#include <netinet/ip_fw.h>
84#include <netinet/ip_dummynet.h>
85
86#ifdef IPSEC
87#include <netinet6/ipsec.h>
88#include <netkey/key.h>
89#endif
90
91int rsvp_on = 0;
92
93int	ipforwarding = 0;
94SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
95    &ipforwarding, 0, "Enable IP forwarding between interfaces");
96
97static int	ipsendredirects = 1; /* XXX */
98SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
99    &ipsendredirects, 0, "Enable sending IP redirects");
100
101int	ip_defttl = IPDEFTTL;
102SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
103    &ip_defttl, 0, "Maximum TTL on IP packets");
104
105static int	ip_dosourceroute = 0;
106SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
107    &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
108
109static int	ip_acceptsourceroute = 0;
110SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
111    CTLFLAG_RW, &ip_acceptsourceroute, 0,
112    "Enable accepting source routed IP packets");
113
114static int	ip_keepfaith = 0;
115SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
116	&ip_keepfaith,	0,
117	"Enable packet capture for FAITH IPv4->IPv6 translater daemon");
118
119static int	ip_nfragpackets = 0;
120static int	ip_maxfragpackets;	/* initialized in ip_init() */
121SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW,
122	&ip_maxfragpackets, 0,
123	"Maximum number of IPv4 fragment reassembly queue entries");
124
125/*
126 * XXX - Setting ip_checkinterface mostly implements the receive side of
127 * the Strong ES model described in RFC 1122, but since the routing table
128 * and transmit implementation do not implement the Strong ES model,
129 * setting this to 1 results in an odd hybrid.
130 *
131 * XXX - ip_checkinterface currently must be disabled if you use ipnat
132 * to translate the destination address to another local interface.
133 *
134 * XXX - ip_checkinterface must be disabled if you add IP aliases
135 * to the loopback interface instead of the interface where the
136 * packets for those addresses are received.
137 */
138static int	ip_checkinterface = 1;
139SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
140    &ip_checkinterface, 0, "Verify packet arrives on correct interface");
141
142#ifdef DIAGNOSTIC
143static int	ipprintfs = 0;
144#endif
145
146static int	ipqmaxlen = IFQ_MAXLEN;
147
148extern	struct domain inetdomain;
149extern	struct protosw inetsw[];
150u_char	ip_protox[IPPROTO_MAX];
151struct	in_ifaddrhead in_ifaddrhead; 		/* first inet address */
152struct	in_ifaddrhashhead *in_ifaddrhashtbl;	/* inet addr hash table  */
153u_long 	in_ifaddrhmask;				/* mask for hash table */
154
155SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
156    &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
157SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
158    &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
159
160struct ipstat ipstat;
161SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
162    &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
163
164/* Packet reassembly stuff */
165#define IPREASS_NHASH_LOG2      6
166#define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
167#define IPREASS_HMASK           (IPREASS_NHASH - 1)
168#define IPREASS_HASH(x,y) \
169	(((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
170
171static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH];
172static int    nipq = 0;         /* total # of reass queues */
173static int    maxnipq;
174
175#ifdef IPCTL_DEFMTU
176SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
177    &ip_mtu, 0, "Default MTU");
178#endif
179
180#ifdef IPSTEALTH
181static int	ipstealth = 0;
182SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
183    &ipstealth, 0, "");
184#endif
185
186
187/* Firewall hooks */
188ip_fw_chk_t *ip_fw_chk_ptr;
189int fw_enable = 1 ;
190
191/* Dummynet hooks */
192ip_dn_io_t *ip_dn_io_ptr;
193
194
195/*
196 * XXX this is ugly -- the following two global variables are
197 * used to store packet state while it travels through the stack.
198 * Note that the code even makes assumptions on the size and
199 * alignment of fields inside struct ip_srcrt so e.g. adding some
200 * fields will break the code. This needs to be fixed.
201 *
202 * We need to save the IP options in case a protocol wants to respond
203 * to an incoming packet over the same route if the packet got here
204 * using IP source routing.  This allows connection establishment and
205 * maintenance when the remote end is on a network that is not known
206 * to us.
207 */
208static int	ip_nhops = 0;
209static	struct ip_srcrt {
210	struct	in_addr dst;			/* final destination */
211	char	nop;				/* one NOP to align */
212	char	srcopt[IPOPT_OFFSET + 1];	/* OPTVAL, OLEN and OFFSET */
213	struct	in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
214} ip_srcrt;
215
216static void	save_rte(u_char *, struct in_addr);
217static int	ip_dooptions(struct mbuf *m, int,
218			struct sockaddr_in *next_hop);
219static void	ip_forward(struct mbuf *m, int srcrt,
220			struct sockaddr_in *next_hop);
221static void	ip_freef(struct ipqhead *, struct ipq *);
222static struct	mbuf *ip_reass(struct mbuf *, struct ipqhead *,
223		struct ipq *, u_int32_t *, u_int16_t *);
224static void	ipintr(void);
225
226/*
227 * IP initialization: fill in IP protocol switch table.
228 * All protocols not implemented in kernel go to raw IP protocol handler.
229 */
230void
231ip_init()
232{
233	register struct protosw *pr;
234	register int i;
235
236	TAILQ_INIT(&in_ifaddrhead);
237	in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &in_ifaddrhmask);
238	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
239	if (pr == 0)
240		panic("ip_init");
241	for (i = 0; i < IPPROTO_MAX; i++)
242		ip_protox[i] = pr - inetsw;
243	for (pr = inetdomain.dom_protosw;
244	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
245		if (pr->pr_domain->dom_family == PF_INET &&
246		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
247			ip_protox[pr->pr_protocol] = pr - inetsw;
248
249	for (i = 0; i < IPREASS_NHASH; i++)
250	    TAILQ_INIT(&ipq[i]);
251
252	maxnipq = nmbclusters / 4;
253	ip_maxfragpackets = nmbclusters / 4;
254
255#ifndef RANDOM_IP_ID
256	ip_id = time_second & 0xffff;
257#endif
258	ipintrq.ifq_maxlen = ipqmaxlen;
259	mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
260	ipintrq_present = 1;
261
262	register_netisr(NETISR_IP, ipintr);
263}
264
265/*
266 * XXX watch out this one. It is perhaps used as a cache for
267 * the most recently used route ? it is cleared in in_addroute()
268 * when a new route is successfully created.
269 */
270struct	route ipforward_rt;
271
272/*
273 * Ip input routine.  Checksum and byte swap header.  If fragmented
274 * try to reassemble.  Process options.  Pass to next level.
275 */
276void
277ip_input(struct mbuf *m)
278{
279	struct ip *ip;
280	struct ipq *fp;
281	struct in_ifaddr *ia = NULL;
282	struct ifaddr *ifa;
283	int    i, hlen, checkif;
284	u_short sum;
285	struct in_addr pkt_dst;
286	u_int32_t divert_info = 0;		/* packet divert/tee info */
287	struct ip_fw_args args;
288#ifdef PFIL_HOOKS
289	struct packet_filter_hook *pfh;
290	struct mbuf *m0;
291	int rv;
292#endif /* PFIL_HOOKS */
293
294	args.eh = NULL;
295	args.oif = NULL;
296	args.rule = NULL;
297	args.divert_rule = 0;			/* divert cookie */
298	args.next_hop = NULL;
299
300	/* Grab info from MT_TAG mbufs prepended to the chain.	*/
301	for (; m && m->m_type == MT_TAG; m = m->m_next) {
302		switch(m->m_tag_id) {
303		default:
304			printf("ip_input: unrecognised MT_TAG tag %d\n",
305			    m->m_tag_id);
306			break;
307
308		case PACKET_TAG_DUMMYNET:
309			args.rule = ((struct dn_pkt *)m)->rule;
310			break;
311
312		case PACKET_TAG_DIVERT:
313			args.divert_rule = (intptr_t)m->m_hdr.mh_data & 0xffff;
314			break;
315
316		case PACKET_TAG_IPFORWARD:
317			args.next_hop = (struct sockaddr_in *)m->m_hdr.mh_data;
318			break;
319		}
320	}
321
322	KASSERT(m != NULL && (m->m_flags & M_PKTHDR) != 0,
323	    ("ip_input: no HDR"));
324
325	if (args.rule) {	/* dummynet already filtered us */
326		ip = mtod(m, struct ip *);
327		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
328		goto iphack ;
329	}
330
331	ipstat.ips_total++;
332
333	if (m->m_pkthdr.len < sizeof(struct ip))
334		goto tooshort;
335
336	if (m->m_len < sizeof (struct ip) &&
337	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
338		ipstat.ips_toosmall++;
339		return;
340	}
341	ip = mtod(m, struct ip *);
342
343	if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
344		ipstat.ips_badvers++;
345		goto bad;
346	}
347
348	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
349	if (hlen < sizeof(struct ip)) {	/* minimum header length */
350		ipstat.ips_badhlen++;
351		goto bad;
352	}
353	if (hlen > m->m_len) {
354		if ((m = m_pullup(m, hlen)) == 0) {
355			ipstat.ips_badhlen++;
356			return;
357		}
358		ip = mtod(m, struct ip *);
359	}
360
361	/* 127/8 must not appear on wire - RFC1122 */
362	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
363	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
364		if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
365			ipstat.ips_badaddr++;
366			goto bad;
367		}
368	}
369
370	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
371		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
372	} else {
373		if (hlen == sizeof(struct ip)) {
374			sum = in_cksum_hdr(ip);
375		} else {
376			sum = in_cksum(m, hlen);
377		}
378	}
379	if (sum) {
380		ipstat.ips_badsum++;
381		goto bad;
382	}
383
384	/*
385	 * Convert fields to host representation.
386	 */
387	ip->ip_len = ntohs(ip->ip_len);
388	if (ip->ip_len < hlen) {
389		ipstat.ips_badlen++;
390		goto bad;
391	}
392	ip->ip_off = ntohs(ip->ip_off);
393
394	/*
395	 * Check that the amount of data in the buffers
396	 * is as at least much as the IP header would have us expect.
397	 * Trim mbufs if longer than we expect.
398	 * Drop packet if shorter than we expect.
399	 */
400	if (m->m_pkthdr.len < ip->ip_len) {
401tooshort:
402		ipstat.ips_tooshort++;
403		goto bad;
404	}
405	if (m->m_pkthdr.len > ip->ip_len) {
406		if (m->m_len == m->m_pkthdr.len) {
407			m->m_len = ip->ip_len;
408			m->m_pkthdr.len = ip->ip_len;
409		} else
410			m_adj(m, ip->ip_len - m->m_pkthdr.len);
411	}
412
413#ifdef IPSEC
414	if (ipsec_gethist(m, NULL))
415		goto pass;
416#endif
417
418	/*
419	 * IpHack's section.
420	 * Right now when no processing on packet has done
421	 * and it is still fresh out of network we do our black
422	 * deals with it.
423	 * - Firewall: deny/allow/divert
424	 * - Xlate: translate packet's addr/port (NAT).
425	 * - Pipe: pass pkt through dummynet.
426	 * - Wrap: fake packet's addr/port <unimpl.>
427	 * - Encapsulate: put it in another IP and send out. <unimp.>
428 	 */
429
430iphack:
431
432#ifdef PFIL_HOOKS
433	/*
434	 * Run through list of hooks for input packets.  If there are any
435	 * filters which require that additional packets in the flow are
436	 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
437	 * Note that filters must _never_ set this flag, as another filter
438	 * in the list may have previously cleared it.
439	 */
440	m0 = m;
441	pfh = pfil_hook_get(PFIL_IN, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
442	for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link))
443		if (pfh->pfil_func) {
444			rv = pfh->pfil_func(ip, hlen,
445					    m->m_pkthdr.rcvif, 0, &m0);
446			if (rv)
447				return;
448			m = m0;
449			if (m == NULL)
450				return;
451			ip = mtod(m, struct ip *);
452		}
453#endif /* PFIL_HOOKS */
454
455	if (fw_enable && IPFW_LOADED) {
456		/*
457		 * If we've been forwarded from the output side, then
458		 * skip the firewall a second time
459		 */
460		if (args.next_hop)
461			goto ours;
462
463		args.m = m;
464		i = ip_fw_chk_ptr(&args);
465		m = args.m;
466
467		if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) { /* drop */
468			if (m)
469				m_freem(m);
470			return;
471		}
472		ip = mtod(m, struct ip *); /* just in case m changed */
473		if (i == 0 && args.next_hop == NULL)	/* common case */
474			goto pass;
475                if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG) != 0) {
476			/* Send packet to the appropriate pipe */
477			ip_dn_io_ptr(m, i&0xffff, DN_TO_IP_IN, &args);
478			return;
479		}
480#ifdef IPDIVERT
481		if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) {
482			/* Divert or tee packet */
483			divert_info = i;
484			goto ours;
485		}
486#endif
487		if (i == 0 && args.next_hop != NULL)
488			goto pass;
489		/*
490		 * if we get here, the packet must be dropped
491		 */
492		m_freem(m);
493		return;
494	}
495pass:
496
497	/*
498	 * Process options and, if not destined for us,
499	 * ship it on.  ip_dooptions returns 1 when an
500	 * error was detected (causing an icmp message
501	 * to be sent and the original packet to be freed).
502	 */
503	ip_nhops = 0;		/* for source routed packets */
504	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0, args.next_hop))
505		return;
506
507        /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
508         * matter if it is destined to another node, or whether it is
509         * a multicast one, RSVP wants it! and prevents it from being forwarded
510         * anywhere else. Also checks if the rsvp daemon is running before
511	 * grabbing the packet.
512         */
513	if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
514		goto ours;
515
516	/*
517	 * Check our list of addresses, to see if the packet is for us.
518	 * If we don't have any addresses, assume any unicast packet
519	 * we receive might be for us (and let the upper layers deal
520	 * with it).
521	 */
522	if (TAILQ_EMPTY(&in_ifaddrhead) &&
523	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
524		goto ours;
525
526	/*
527	 * Cache the destination address of the packet; this may be
528	 * changed by use of 'ipfw fwd'.
529	 */
530	pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst;
531
532	/*
533	 * Enable a consistency check between the destination address
534	 * and the arrival interface for a unicast packet (the RFC 1122
535	 * strong ES model) if IP forwarding is disabled and the packet
536	 * is not locally generated and the packet is not subject to
537	 * 'ipfw fwd'.
538	 *
539	 * XXX - Checking also should be disabled if the destination
540	 * address is ipnat'ed to a different interface.
541	 *
542	 * XXX - Checking is incompatible with IP aliases added
543	 * to the loopback interface instead of the interface where
544	 * the packets are received.
545	 */
546	checkif = ip_checkinterface && (ipforwarding == 0) &&
547	    m->m_pkthdr.rcvif != NULL &&
548	    ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
549	    (args.next_hop == NULL);
550
551	/*
552	 * Check for exact addresses in the hash bucket.
553	 */
554	LIST_FOREACH(ia, INADDR_HASH(pkt_dst.s_addr), ia_hash) {
555		/*
556		 * If the address matches, verify that the packet
557		 * arrived via the correct interface if checking is
558		 * enabled.
559		 */
560		if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr &&
561		    (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
562			goto ours;
563	}
564	/*
565	 * Check for broadcast addresses.
566	 *
567	 * Only accept broadcast packets that arrive via the matching
568	 * interface.  Reception of forwarded directed broadcasts would
569	 * be handled via ip_forward() and ether_output() with the loopback
570	 * into the stack for SIMPLEX interfaces handled by ether_output().
571	 */
572	if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
573	        TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
574			if (ifa->ifa_addr->sa_family != AF_INET)
575				continue;
576			ia = ifatoia(ifa);
577			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
578			    pkt_dst.s_addr)
579				goto ours;
580			if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr)
581				goto ours;
582#ifdef BOOTP_COMPAT
583			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
584				goto ours;
585#endif
586		}
587	}
588	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
589		struct in_multi *inm;
590		if (ip_mrouter) {
591			/*
592			 * If we are acting as a multicast router, all
593			 * incoming multicast packets are passed to the
594			 * kernel-level multicast forwarding function.
595			 * The packet is returned (relatively) intact; if
596			 * ip_mforward() returns a non-zero value, the packet
597			 * must be discarded, else it may be accepted below.
598			 */
599			if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
600				ipstat.ips_cantforward++;
601				m_freem(m);
602				return;
603			}
604
605			/*
606			 * The process-level routing daemon needs to receive
607			 * all multicast IGMP packets, whether or not this
608			 * host belongs to their destination groups.
609			 */
610			if (ip->ip_p == IPPROTO_IGMP)
611				goto ours;
612			ipstat.ips_forward++;
613		}
614		/*
615		 * See if we belong to the destination multicast group on the
616		 * arrival interface.
617		 */
618		IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
619		if (inm == NULL) {
620			ipstat.ips_notmember++;
621			m_freem(m);
622			return;
623		}
624		goto ours;
625	}
626	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
627		goto ours;
628	if (ip->ip_dst.s_addr == INADDR_ANY)
629		goto ours;
630
631	/*
632	 * FAITH(Firewall Aided Internet Translator)
633	 */
634	if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
635		if (ip_keepfaith) {
636			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
637				goto ours;
638		}
639		m_freem(m);
640		return;
641	}
642
643	/*
644	 * Not for us; forward if possible and desirable.
645	 */
646	if (ipforwarding == 0) {
647		ipstat.ips_cantforward++;
648		m_freem(m);
649	} else {
650#ifdef IPSEC
651		/*
652		 * Enforce inbound IPsec SPD.
653		 */
654		if (ipsec4_in_reject(m, NULL)) {
655			ipsecstat.in_polvio++;
656			goto bad;
657		}
658#endif /* IPSEC */
659		ip_forward(m, 0, args.next_hop);
660	}
661	return;
662
663ours:
664#ifdef IPSTEALTH
665	/*
666	 * IPSTEALTH: Process non-routing options only
667	 * if the packet is destined for us.
668	 */
669	if (ipstealth && hlen > sizeof (struct ip) &&
670	    ip_dooptions(m, 1, args.next_hop))
671		return;
672#endif /* IPSTEALTH */
673
674	/* Count the packet in the ip address stats */
675	if (ia != NULL) {
676		ia->ia_ifa.if_ipackets++;
677		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
678	}
679
680	/*
681	 * If offset or IP_MF are set, must reassemble.
682	 * Otherwise, nothing need be done.
683	 * (We could look in the reassembly queue to see
684	 * if the packet was previously fragmented,
685	 * but it's not worth the time; just let them time out.)
686	 */
687	if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
688
689		sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
690		/*
691		 * Look for queue of fragments
692		 * of this datagram.
693		 */
694		TAILQ_FOREACH(fp, &ipq[sum], ipq_list)
695			if (ip->ip_id == fp->ipq_id &&
696			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
697			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
698#ifdef MAC
699			    mac_fragment_match(m, fp) &&
700#endif
701			    ip->ip_p == fp->ipq_p)
702				goto found;
703
704		fp = 0;
705
706		/* check if there's a place for the new queue */
707		if (nipq > maxnipq) {
708		    /*
709		     * drop something from the tail of the current queue
710		     * before proceeding further
711		     */
712		    struct ipq *q = TAILQ_LAST(&ipq[sum], ipqhead);
713		    if (q == NULL) {   /* gak */
714			for (i = 0; i < IPREASS_NHASH; i++) {
715			    struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead);
716			    if (r) {
717				ip_freef(&ipq[i], r);
718				break;
719			    }
720			}
721		    } else
722			ip_freef(&ipq[sum], q);
723		}
724found:
725		/*
726		 * Adjust ip_len to not reflect header,
727		 * convert offset of this to bytes.
728		 */
729		ip->ip_len -= hlen;
730		if (ip->ip_off & IP_MF) {
731		        /*
732		         * Make sure that fragments have a data length
733			 * that's a non-zero multiple of 8 bytes.
734		         */
735			if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
736				ipstat.ips_toosmall++; /* XXX */
737				goto bad;
738			}
739			m->m_flags |= M_FRAG;
740		}
741		ip->ip_off <<= 3;
742
743		/*
744		 * Attempt reassembly; if it succeeds, proceed.
745		 * ip_reass() will return a different mbuf, and update
746		 * the divert info in divert_info and args.divert_rule.
747		 */
748		ipstat.ips_fragments++;
749		m->m_pkthdr.header = ip;
750		m = ip_reass(m,
751		    &ipq[sum], fp, &divert_info, &args.divert_rule);
752		if (m == 0)
753			return;
754		ipstat.ips_reassembled++;
755		ip = mtod(m, struct ip *);
756		/* Get the header length of the reassembled packet */
757		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
758#ifdef IPDIVERT
759		/* Restore original checksum before diverting packet */
760		if (divert_info != 0) {
761			ip->ip_len += hlen;
762			ip->ip_len = htons(ip->ip_len);
763			ip->ip_off = htons(ip->ip_off);
764			ip->ip_sum = 0;
765			if (hlen == sizeof(struct ip))
766				ip->ip_sum = in_cksum_hdr(ip);
767			else
768				ip->ip_sum = in_cksum(m, hlen);
769			ip->ip_off = ntohs(ip->ip_off);
770			ip->ip_len = ntohs(ip->ip_len);
771			ip->ip_len -= hlen;
772		}
773#endif
774	} else
775		ip->ip_len -= hlen;
776
777#ifdef IPDIVERT
778	/*
779	 * Divert or tee packet to the divert protocol if required.
780	 */
781	if (divert_info != 0) {
782		struct mbuf *clone = NULL;
783
784		/* Clone packet if we're doing a 'tee' */
785		if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0)
786			clone = m_dup(m, M_DONTWAIT);
787
788		/* Restore packet header fields to original values */
789		ip->ip_len += hlen;
790		ip->ip_len = htons(ip->ip_len);
791		ip->ip_off = htons(ip->ip_off);
792
793		/* Deliver packet to divert input routine */
794		divert_packet(m, 1, divert_info & 0xffff, args.divert_rule);
795		ipstat.ips_delivered++;
796
797		/* If 'tee', continue with original packet */
798		if (clone == NULL)
799			return;
800		m = clone;
801		ip = mtod(m, struct ip *);
802		ip->ip_len += hlen;
803		/*
804		 * Jump backwards to complete processing of the
805		 * packet. But first clear divert_info to avoid
806		 * entering this block again.
807		 * We do not need to clear args.divert_rule
808		 * or args.next_hop as they will not be used.
809		 */
810		divert_info = 0;
811		goto pass;
812	}
813#endif
814
815#ifdef IPSEC
816	/*
817	 * enforce IPsec policy checking if we are seeing last header.
818	 * note that we do not visit this with protocols with pcb layer
819	 * code - like udp/tcp/raw ip.
820	 */
821	if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
822	    ipsec4_in_reject(m, NULL)) {
823		ipsecstat.in_polvio++;
824		goto bad;
825	}
826#endif
827
828	/*
829	 * Switch out to protocol's input routine.
830	 */
831	ipstat.ips_delivered++;
832	if (args.next_hop && ip->ip_p == IPPROTO_TCP) {
833		/* TCP needs IPFORWARD info if available */
834		struct m_hdr tag;
835
836		tag.mh_type = MT_TAG;
837		tag.mh_flags = PACKET_TAG_IPFORWARD;
838		tag.mh_data = (caddr_t)args.next_hop;
839		tag.mh_next = m;
840
841		(*inetsw[ip_protox[ip->ip_p]].pr_input)(
842			(struct mbuf *)&tag, hlen);
843	} else
844		(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
845	return;
846bad:
847	m_freem(m);
848}
849
850/*
851 * IP software interrupt routine - to go away sometime soon
852 */
853static void
854ipintr(void)
855{
856	struct mbuf *m;
857
858	while (1) {
859		IF_DEQUEUE(&ipintrq, m);
860		if (m == 0)
861			return;
862		ip_input(m);
863	}
864}
865
866/*
867 * Take incoming datagram fragment and try to reassemble it into
868 * whole datagram.  If a chain for reassembly of this datagram already
869 * exists, then it is given as fp; otherwise have to make a chain.
870 *
871 * When IPDIVERT enabled, keep additional state with each packet that
872 * tells us if we need to divert or tee the packet we're building.
873 * In particular, *divinfo includes the port and TEE flag,
874 * *divert_rule is the number of the matching rule.
875 */
876
877static struct mbuf *
878ip_reass(struct mbuf *m, struct ipqhead *head, struct ipq *fp,
879	u_int32_t *divinfo, u_int16_t *divert_rule)
880{
881	struct ip *ip = mtod(m, struct ip *);
882	register struct mbuf *p, *q, *nq;
883	struct mbuf *t;
884	int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
885	int i, next;
886
887	/*
888	 * Presence of header sizes in mbufs
889	 * would confuse code below.
890	 */
891	m->m_data += hlen;
892	m->m_len -= hlen;
893
894	/*
895	 * If first fragment to arrive, create a reassembly queue.
896	 */
897	if (fp == 0) {
898		/*
899		 * Enforce upper bound on number of fragmented packets
900		 * for which we attempt reassembly;
901		 * If maxfrag is 0, never accept fragments.
902		 * If maxfrag is -1, accept all fragments without limitation.
903		 */
904		if ((ip_maxfragpackets >= 0) && (ip_nfragpackets >= ip_maxfragpackets))
905			goto dropfrag;
906		ip_nfragpackets++;
907		if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
908			goto dropfrag;
909		fp = mtod(t, struct ipq *);
910#ifdef MAC
911		mac_init_ipq(fp);
912		mac_create_ipq(m, fp);
913#endif
914		TAILQ_INSERT_HEAD(head, fp, ipq_list);
915		nipq++;
916		fp->ipq_ttl = IPFRAGTTL;
917		fp->ipq_p = ip->ip_p;
918		fp->ipq_id = ip->ip_id;
919		fp->ipq_src = ip->ip_src;
920		fp->ipq_dst = ip->ip_dst;
921		fp->ipq_frags = m;
922		m->m_nextpkt = NULL;
923#ifdef IPDIVERT
924		fp->ipq_div_info = 0;
925		fp->ipq_div_cookie = 0;
926#endif
927		goto inserted;
928	} else {
929#ifdef MAC
930		mac_update_ipq(m, fp);
931#endif
932	}
933
934#define GETIP(m)	((struct ip*)((m)->m_pkthdr.header))
935
936	/*
937	 * Find a segment which begins after this one does.
938	 */
939	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
940		if (GETIP(q)->ip_off > ip->ip_off)
941			break;
942
943	/*
944	 * If there is a preceding segment, it may provide some of
945	 * our data already.  If so, drop the data from the incoming
946	 * segment.  If it provides all of our data, drop us, otherwise
947	 * stick new segment in the proper place.
948	 *
949	 * If some of the data is dropped from the the preceding
950	 * segment, then it's checksum is invalidated.
951	 */
952	if (p) {
953		i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
954		if (i > 0) {
955			if (i >= ip->ip_len)
956				goto dropfrag;
957			m_adj(m, i);
958			m->m_pkthdr.csum_flags = 0;
959			ip->ip_off += i;
960			ip->ip_len -= i;
961		}
962		m->m_nextpkt = p->m_nextpkt;
963		p->m_nextpkt = m;
964	} else {
965		m->m_nextpkt = fp->ipq_frags;
966		fp->ipq_frags = m;
967	}
968
969	/*
970	 * While we overlap succeeding segments trim them or,
971	 * if they are completely covered, dequeue them.
972	 */
973	for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
974	     q = nq) {
975		i = (ip->ip_off + ip->ip_len) -
976		    GETIP(q)->ip_off;
977		if (i < GETIP(q)->ip_len) {
978			GETIP(q)->ip_len -= i;
979			GETIP(q)->ip_off += i;
980			m_adj(q, i);
981			q->m_pkthdr.csum_flags = 0;
982			break;
983		}
984		nq = q->m_nextpkt;
985		m->m_nextpkt = nq;
986		m_freem(q);
987	}
988
989inserted:
990
991#ifdef IPDIVERT
992	/*
993	 * Transfer firewall instructions to the fragment structure.
994	 * Only trust info in the fragment at offset 0.
995	 */
996	if (ip->ip_off == 0) {
997		fp->ipq_div_info = *divinfo;
998		fp->ipq_div_cookie = *divert_rule;
999	}
1000	*divinfo = 0;
1001	*divert_rule = 0;
1002#endif
1003
1004	/*
1005	 * Check for complete reassembly.
1006	 */
1007	next = 0;
1008	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1009		if (GETIP(q)->ip_off != next)
1010			return (0);
1011		next += GETIP(q)->ip_len;
1012	}
1013	/* Make sure the last packet didn't have the IP_MF flag */
1014	if (p->m_flags & M_FRAG)
1015		return (0);
1016
1017	/*
1018	 * Reassembly is complete.  Make sure the packet is a sane size.
1019	 */
1020	q = fp->ipq_frags;
1021	ip = GETIP(q);
1022	if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
1023		ipstat.ips_toolong++;
1024		ip_freef(head, fp);
1025		return (0);
1026	}
1027
1028	/*
1029	 * Concatenate fragments.
1030	 */
1031	m = q;
1032	t = m->m_next;
1033	m->m_next = 0;
1034	m_cat(m, t);
1035	nq = q->m_nextpkt;
1036	q->m_nextpkt = 0;
1037	for (q = nq; q != NULL; q = nq) {
1038		nq = q->m_nextpkt;
1039		q->m_nextpkt = NULL;
1040		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1041		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1042		m_cat(m, q);
1043	}
1044#ifdef MAC
1045	mac_create_datagram_from_ipq(fp, m);
1046	mac_destroy_ipq(fp);
1047#endif
1048
1049#ifdef IPDIVERT
1050	/*
1051	 * Extract firewall instructions from the fragment structure.
1052	 */
1053	*divinfo = fp->ipq_div_info;
1054	*divert_rule = fp->ipq_div_cookie;
1055#endif
1056
1057	/*
1058	 * Create header for new ip packet by
1059	 * modifying header of first packet;
1060	 * dequeue and discard fragment reassembly header.
1061	 * Make header visible.
1062	 */
1063	ip->ip_len = next;
1064	ip->ip_src = fp->ipq_src;
1065	ip->ip_dst = fp->ipq_dst;
1066	TAILQ_REMOVE(head, fp, ipq_list);
1067	nipq--;
1068	(void) m_free(dtom(fp));
1069	ip_nfragpackets--;
1070	m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
1071	m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
1072	/* some debugging cruft by sklower, below, will go away soon */
1073	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
1074		register int plen = 0;
1075		for (t = m; t; t = t->m_next)
1076			plen += t->m_len;
1077		m->m_pkthdr.len = plen;
1078	}
1079	return (m);
1080
1081dropfrag:
1082#ifdef IPDIVERT
1083	*divinfo = 0;
1084	*divert_rule = 0;
1085#endif
1086	ipstat.ips_fragdropped++;
1087	m_freem(m);
1088	return (0);
1089
1090#undef GETIP
1091}
1092
1093/*
1094 * Free a fragment reassembly header and all
1095 * associated datagrams.
1096 */
1097static void
1098ip_freef(fhp, fp)
1099	struct ipqhead *fhp;
1100	struct ipq *fp;
1101{
1102	register struct mbuf *q;
1103
1104	while (fp->ipq_frags) {
1105		q = fp->ipq_frags;
1106		fp->ipq_frags = q->m_nextpkt;
1107		m_freem(q);
1108	}
1109	TAILQ_REMOVE(fhp, fp, ipq_list);
1110	(void) m_free(dtom(fp));
1111	ip_nfragpackets--;
1112	nipq--;
1113}
1114
1115/*
1116 * IP timer processing;
1117 * if a timer expires on a reassembly
1118 * queue, discard it.
1119 */
1120void
1121ip_slowtimo()
1122{
1123	register struct ipq *fp;
1124	int s = splnet();
1125	int i;
1126
1127	for (i = 0; i < IPREASS_NHASH; i++) {
1128		for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
1129			struct ipq *fpp;
1130
1131			fpp = fp;
1132			fp = TAILQ_NEXT(fp, ipq_list);
1133			if(--fpp->ipq_ttl == 0) {
1134				ipstat.ips_fragtimeout++;
1135				ip_freef(&ipq[i], fpp);
1136			}
1137		}
1138	}
1139	/*
1140	 * If we are over the maximum number of fragments
1141	 * (due to the limit being lowered), drain off
1142	 * enough to get down to the new limit.
1143	 */
1144	for (i = 0; i < IPREASS_NHASH; i++) {
1145		if (ip_maxfragpackets >= 0) {
1146			while (ip_nfragpackets > ip_maxfragpackets &&
1147				!TAILQ_EMPTY(&ipq[i])) {
1148				ipstat.ips_fragdropped++;
1149				ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1150			}
1151		}
1152	}
1153	ipflow_slowtimo();
1154	splx(s);
1155}
1156
1157/*
1158 * Drain off all datagram fragments.
1159 */
1160void
1161ip_drain()
1162{
1163	int     i;
1164
1165	for (i = 0; i < IPREASS_NHASH; i++) {
1166		while(!TAILQ_EMPTY(&ipq[i])) {
1167			ipstat.ips_fragdropped++;
1168			ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1169		}
1170	}
1171	in_rtqdrain();
1172}
1173
1174/*
1175 * Do option processing on a datagram,
1176 * possibly discarding it if bad options are encountered,
1177 * or forwarding it if source-routed.
1178 * The pass argument is used when operating in the IPSTEALTH
1179 * mode to tell what options to process:
1180 * [LS]SRR (pass 0) or the others (pass 1).
1181 * The reason for as many as two passes is that when doing IPSTEALTH,
1182 * non-routing options should be processed only if the packet is for us.
1183 * Returns 1 if packet has been forwarded/freed,
1184 * 0 if the packet should be processed further.
1185 */
1186static int
1187ip_dooptions(struct mbuf *m, int pass, struct sockaddr_in *next_hop)
1188{
1189	struct ip *ip = mtod(m, struct ip *);
1190	u_char *cp;
1191	struct in_ifaddr *ia;
1192	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1193	struct in_addr *sin, dst;
1194	n_time ntime;
1195	struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
1196
1197	dst = ip->ip_dst;
1198	cp = (u_char *)(ip + 1);
1199	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1200	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1201		opt = cp[IPOPT_OPTVAL];
1202		if (opt == IPOPT_EOL)
1203			break;
1204		if (opt == IPOPT_NOP)
1205			optlen = 1;
1206		else {
1207			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1208				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1209				goto bad;
1210			}
1211			optlen = cp[IPOPT_OLEN];
1212			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1213				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1214				goto bad;
1215			}
1216		}
1217		switch (opt) {
1218
1219		default:
1220			break;
1221
1222		/*
1223		 * Source routing with record.
1224		 * Find interface with current destination address.
1225		 * If none on this machine then drop if strictly routed,
1226		 * or do nothing if loosely routed.
1227		 * Record interface address and bring up next address
1228		 * component.  If strictly routed make sure next
1229		 * address is on directly accessible net.
1230		 */
1231		case IPOPT_LSRR:
1232		case IPOPT_SSRR:
1233#ifdef IPSTEALTH
1234			if (ipstealth && pass > 0)
1235				break;
1236#endif
1237			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1238				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1239				goto bad;
1240			}
1241			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1242				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1243				goto bad;
1244			}
1245			ipaddr.sin_addr = ip->ip_dst;
1246			ia = (struct in_ifaddr *)
1247				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1248			if (ia == 0) {
1249				if (opt == IPOPT_SSRR) {
1250					type = ICMP_UNREACH;
1251					code = ICMP_UNREACH_SRCFAIL;
1252					goto bad;
1253				}
1254				if (!ip_dosourceroute)
1255					goto nosourcerouting;
1256				/*
1257				 * Loose routing, and not at next destination
1258				 * yet; nothing to do except forward.
1259				 */
1260				break;
1261			}
1262			off--;			/* 0 origin */
1263			if (off > optlen - (int)sizeof(struct in_addr)) {
1264				/*
1265				 * End of source route.  Should be for us.
1266				 */
1267				if (!ip_acceptsourceroute)
1268					goto nosourcerouting;
1269				save_rte(cp, ip->ip_src);
1270				break;
1271			}
1272#ifdef IPSTEALTH
1273			if (ipstealth)
1274				goto dropit;
1275#endif
1276			if (!ip_dosourceroute) {
1277				if (ipforwarding) {
1278					char buf[16]; /* aaa.bbb.ccc.ddd\0 */
1279					/*
1280					 * Acting as a router, so generate ICMP
1281					 */
1282nosourcerouting:
1283					strcpy(buf, inet_ntoa(ip->ip_dst));
1284					log(LOG_WARNING,
1285					    "attempted source route from %s to %s\n",
1286					    inet_ntoa(ip->ip_src), buf);
1287					type = ICMP_UNREACH;
1288					code = ICMP_UNREACH_SRCFAIL;
1289					goto bad;
1290				} else {
1291					/*
1292					 * Not acting as a router, so silently drop.
1293					 */
1294#ifdef IPSTEALTH
1295dropit:
1296#endif
1297					ipstat.ips_cantforward++;
1298					m_freem(m);
1299					return (1);
1300				}
1301			}
1302
1303			/*
1304			 * locate outgoing interface
1305			 */
1306			(void)memcpy(&ipaddr.sin_addr, cp + off,
1307			    sizeof(ipaddr.sin_addr));
1308
1309			if (opt == IPOPT_SSRR) {
1310#define	INA	struct in_ifaddr *
1311#define	SA	struct sockaddr *
1312			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1313				ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1314			} else
1315				ia = ip_rtaddr(ipaddr.sin_addr, &ipforward_rt);
1316			if (ia == 0) {
1317				type = ICMP_UNREACH;
1318				code = ICMP_UNREACH_SRCFAIL;
1319				goto bad;
1320			}
1321			ip->ip_dst = ipaddr.sin_addr;
1322			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1323			    sizeof(struct in_addr));
1324			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1325			/*
1326			 * Let ip_intr's mcast routing check handle mcast pkts
1327			 */
1328			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1329			break;
1330
1331		case IPOPT_RR:
1332#ifdef IPSTEALTH
1333			if (ipstealth && pass == 0)
1334				break;
1335#endif
1336			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1337				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1338				goto bad;
1339			}
1340			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1341				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1342				goto bad;
1343			}
1344			/*
1345			 * If no space remains, ignore.
1346			 */
1347			off--;			/* 0 origin */
1348			if (off > optlen - (int)sizeof(struct in_addr))
1349				break;
1350			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1351			    sizeof(ipaddr.sin_addr));
1352			/*
1353			 * locate outgoing interface; if we're the destination,
1354			 * use the incoming interface (should be same).
1355			 */
1356			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1357			    (ia = ip_rtaddr(ipaddr.sin_addr,
1358			    &ipforward_rt)) == 0) {
1359				type = ICMP_UNREACH;
1360				code = ICMP_UNREACH_HOST;
1361				goto bad;
1362			}
1363			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1364			    sizeof(struct in_addr));
1365			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1366			break;
1367
1368		case IPOPT_TS:
1369#ifdef IPSTEALTH
1370			if (ipstealth && pass == 0)
1371				break;
1372#endif
1373			code = cp - (u_char *)ip;
1374			if (optlen < 4 || optlen > 40) {
1375				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1376				goto bad;
1377			}
1378			if ((off = cp[IPOPT_OFFSET]) < 5) {
1379				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1380				goto bad;
1381			}
1382			if (off > optlen - (int)sizeof(int32_t)) {
1383				cp[IPOPT_OFFSET + 1] += (1 << 4);
1384				if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
1385					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1386					goto bad;
1387				}
1388				break;
1389			}
1390			off--;				/* 0 origin */
1391			sin = (struct in_addr *)(cp + off);
1392			switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
1393
1394			case IPOPT_TS_TSONLY:
1395				break;
1396
1397			case IPOPT_TS_TSANDADDR:
1398				if (off + sizeof(n_time) +
1399				    sizeof(struct in_addr) > optlen) {
1400					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1401					goto bad;
1402				}
1403				ipaddr.sin_addr = dst;
1404				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1405							    m->m_pkthdr.rcvif);
1406				if (ia == 0)
1407					continue;
1408				(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1409				    sizeof(struct in_addr));
1410				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1411				break;
1412
1413			case IPOPT_TS_PRESPEC:
1414				if (off + sizeof(n_time) +
1415				    sizeof(struct in_addr) > optlen) {
1416					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1417					goto bad;
1418				}
1419				(void)memcpy(&ipaddr.sin_addr, sin,
1420				    sizeof(struct in_addr));
1421				if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1422					continue;
1423				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1424				break;
1425
1426			default:
1427				code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
1428				goto bad;
1429			}
1430			ntime = iptime();
1431			(void)memcpy(cp + off, &ntime, sizeof(n_time));
1432			cp[IPOPT_OFFSET] += sizeof(n_time);
1433		}
1434	}
1435	if (forward && ipforwarding) {
1436		ip_forward(m, 1, next_hop);
1437		return (1);
1438	}
1439	return (0);
1440bad:
1441	icmp_error(m, type, code, 0, 0);
1442	ipstat.ips_badoptions++;
1443	return (1);
1444}
1445
1446/*
1447 * Given address of next destination (final or next hop),
1448 * return internet address info of interface to be used to get there.
1449 */
1450struct in_ifaddr *
1451ip_rtaddr(dst, rt)
1452	struct in_addr dst;
1453	struct route *rt;
1454{
1455	register struct sockaddr_in *sin;
1456
1457	sin = (struct sockaddr_in *)&rt->ro_dst;
1458
1459	if (rt->ro_rt == 0 ||
1460	    !(rt->ro_rt->rt_flags & RTF_UP) ||
1461	    dst.s_addr != sin->sin_addr.s_addr) {
1462		if (rt->ro_rt) {
1463			RTFREE(rt->ro_rt);
1464			rt->ro_rt = 0;
1465		}
1466		sin->sin_family = AF_INET;
1467		sin->sin_len = sizeof(*sin);
1468		sin->sin_addr = dst;
1469
1470		rtalloc_ign(rt, RTF_PRCLONING);
1471	}
1472	if (rt->ro_rt == 0)
1473		return ((struct in_ifaddr *)0);
1474	return (ifatoia(rt->ro_rt->rt_ifa));
1475}
1476
1477/*
1478 * Save incoming source route for use in replies,
1479 * to be picked up later by ip_srcroute if the receiver is interested.
1480 */
1481void
1482save_rte(option, dst)
1483	u_char *option;
1484	struct in_addr dst;
1485{
1486	unsigned olen;
1487
1488	olen = option[IPOPT_OLEN];
1489#ifdef DIAGNOSTIC
1490	if (ipprintfs)
1491		printf("save_rte: olen %d\n", olen);
1492#endif
1493	if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1494		return;
1495	bcopy(option, ip_srcrt.srcopt, olen);
1496	ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1497	ip_srcrt.dst = dst;
1498}
1499
1500/*
1501 * Retrieve incoming source route for use in replies,
1502 * in the same form used by setsockopt.
1503 * The first hop is placed before the options, will be removed later.
1504 */
1505struct mbuf *
1506ip_srcroute()
1507{
1508	register struct in_addr *p, *q;
1509	register struct mbuf *m;
1510
1511	if (ip_nhops == 0)
1512		return ((struct mbuf *)0);
1513	m = m_get(M_DONTWAIT, MT_HEADER);
1514	if (m == 0)
1515		return ((struct mbuf *)0);
1516
1517#define OPTSIZ	(sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1518
1519	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1520	m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1521	    OPTSIZ;
1522#ifdef DIAGNOSTIC
1523	if (ipprintfs)
1524		printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1525#endif
1526
1527	/*
1528	 * First save first hop for return route
1529	 */
1530	p = &ip_srcrt.route[ip_nhops - 1];
1531	*(mtod(m, struct in_addr *)) = *p--;
1532#ifdef DIAGNOSTIC
1533	if (ipprintfs)
1534		printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1535#endif
1536
1537	/*
1538	 * Copy option fields and padding (nop) to mbuf.
1539	 */
1540	ip_srcrt.nop = IPOPT_NOP;
1541	ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1542	(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1543	    &ip_srcrt.nop, OPTSIZ);
1544	q = (struct in_addr *)(mtod(m, caddr_t) +
1545	    sizeof(struct in_addr) + OPTSIZ);
1546#undef OPTSIZ
1547	/*
1548	 * Record return path as an IP source route,
1549	 * reversing the path (pointers are now aligned).
1550	 */
1551	while (p >= ip_srcrt.route) {
1552#ifdef DIAGNOSTIC
1553		if (ipprintfs)
1554			printf(" %lx", (u_long)ntohl(q->s_addr));
1555#endif
1556		*q++ = *p--;
1557	}
1558	/*
1559	 * Last hop goes to final destination.
1560	 */
1561	*q = ip_srcrt.dst;
1562#ifdef DIAGNOSTIC
1563	if (ipprintfs)
1564		printf(" %lx\n", (u_long)ntohl(q->s_addr));
1565#endif
1566	return (m);
1567}
1568
1569/*
1570 * Strip out IP options, at higher
1571 * level protocol in the kernel.
1572 * Second argument is buffer to which options
1573 * will be moved, and return value is their length.
1574 * XXX should be deleted; last arg currently ignored.
1575 */
1576void
1577ip_stripoptions(m, mopt)
1578	register struct mbuf *m;
1579	struct mbuf *mopt;
1580{
1581	register int i;
1582	struct ip *ip = mtod(m, struct ip *);
1583	register caddr_t opts;
1584	int olen;
1585
1586	olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1587	opts = (caddr_t)(ip + 1);
1588	i = m->m_len - (sizeof (struct ip) + olen);
1589	bcopy(opts + olen, opts, (unsigned)i);
1590	m->m_len -= olen;
1591	if (m->m_flags & M_PKTHDR)
1592		m->m_pkthdr.len -= olen;
1593	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1594}
1595
1596u_char inetctlerrmap[PRC_NCMDS] = {
1597	0,		0,		0,		0,
1598	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1599	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1600	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1601	0,		0,		0,		0,
1602	ENOPROTOOPT,	ECONNREFUSED
1603};
1604
1605/*
1606 * Forward a packet.  If some error occurs return the sender
1607 * an icmp packet.  Note we can't always generate a meaningful
1608 * icmp message because icmp doesn't have a large enough repertoire
1609 * of codes and types.
1610 *
1611 * If not forwarding, just drop the packet.  This could be confusing
1612 * if ipforwarding was zero but some routing protocol was advancing
1613 * us as a gateway to somewhere.  However, we must let the routing
1614 * protocol deal with that.
1615 *
1616 * The srcrt parameter indicates whether the packet is being forwarded
1617 * via a source route.
1618 */
1619static void
1620ip_forward(struct mbuf *m, int srcrt, struct sockaddr_in *next_hop)
1621{
1622	struct ip *ip = mtod(m, struct ip *);
1623	struct rtentry *rt;
1624	int error, type = 0, code = 0;
1625	struct mbuf *mcopy;
1626	n_long dest;
1627	struct in_addr pkt_dst;
1628	struct ifnet *destifp;
1629#ifdef IPSEC
1630	struct ifnet dummyifp;
1631#endif
1632
1633	dest = 0;
1634	/*
1635	 * Cache the destination address of the packet; this may be
1636	 * changed by use of 'ipfw fwd'.
1637	 */
1638	pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst;
1639
1640#ifdef DIAGNOSTIC
1641	if (ipprintfs)
1642		printf("forward: src %lx dst %lx ttl %x\n",
1643		    (u_long)ip->ip_src.s_addr, (u_long)pkt_dst.s_addr,
1644		    ip->ip_ttl);
1645#endif
1646
1647
1648	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(pkt_dst) == 0) {
1649		ipstat.ips_cantforward++;
1650		m_freem(m);
1651		return;
1652	}
1653#ifdef IPSTEALTH
1654	if (!ipstealth) {
1655#endif
1656		if (ip->ip_ttl <= IPTTLDEC) {
1657			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1658			    dest, 0);
1659			return;
1660		}
1661#ifdef IPSTEALTH
1662	}
1663#endif
1664
1665	if (ip_rtaddr(pkt_dst, &ipforward_rt) == 0) {
1666		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1667		return;
1668	} else
1669		rt = ipforward_rt.ro_rt;
1670
1671	/*
1672	 * Save the IP header and at most 8 bytes of the payload,
1673	 * in case we need to generate an ICMP message to the src.
1674	 *
1675	 * XXX this can be optimized a lot by saving the data in a local
1676	 * buffer on the stack (72 bytes at most), and only allocating the
1677	 * mbuf if really necessary. The vast majority of the packets
1678	 * are forwarded without having to send an ICMP back (either
1679	 * because unnecessary, or because rate limited), so we are
1680	 * really we are wasting a lot of work here.
1681	 *
1682	 * We don't use m_copy() because it might return a reference
1683	 * to a shared cluster. Both this function and ip_output()
1684	 * assume exclusive access to the IP header in `m', so any
1685	 * data in a cluster may change before we reach icmp_error().
1686	 */
1687	MGET(mcopy, M_DONTWAIT, m->m_type);
1688	if (mcopy != NULL) {
1689		M_COPY_PKTHDR(mcopy, m);
1690		mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8,
1691		    (int)ip->ip_len);
1692		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1693	}
1694
1695#ifdef IPSTEALTH
1696	if (!ipstealth) {
1697#endif
1698		ip->ip_ttl -= IPTTLDEC;
1699#ifdef IPSTEALTH
1700	}
1701#endif
1702
1703	/*
1704	 * If forwarding packet using same interface that it came in on,
1705	 * perhaps should send a redirect to sender to shortcut a hop.
1706	 * Only send redirect if source is sending directly to us,
1707	 * and if packet was not source routed (or has any options).
1708	 * Also, don't send redirect if forwarding using a default route
1709	 * or a route modified by a redirect.
1710	 */
1711	if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1712	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1713	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1714	    ipsendredirects && !srcrt && !next_hop) {
1715#define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1716		u_long src = ntohl(ip->ip_src.s_addr);
1717
1718		if (RTA(rt) &&
1719		    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1720		    if (rt->rt_flags & RTF_GATEWAY)
1721			dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1722		    else
1723			dest = pkt_dst.s_addr;
1724		    /* Router requirements says to only send host redirects */
1725		    type = ICMP_REDIRECT;
1726		    code = ICMP_REDIRECT_HOST;
1727#ifdef DIAGNOSTIC
1728		    if (ipprintfs)
1729		        printf("redirect (%d) to %lx\n", code, (u_long)dest);
1730#endif
1731		}
1732	}
1733
1734	error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1735			  IP_FORWARDING, 0);
1736	if (error)
1737		ipstat.ips_cantforward++;
1738	else {
1739		ipstat.ips_forward++;
1740		if (type)
1741			ipstat.ips_redirectsent++;
1742		else {
1743			if (mcopy) {
1744				ipflow_create(&ipforward_rt, mcopy);
1745				m_freem(mcopy);
1746			}
1747			return;
1748		}
1749	}
1750	if (mcopy == NULL)
1751		return;
1752	destifp = NULL;
1753
1754	switch (error) {
1755
1756	case 0:				/* forwarded, but need redirect */
1757		/* type, code set above */
1758		break;
1759
1760	case ENETUNREACH:		/* shouldn't happen, checked above */
1761	case EHOSTUNREACH:
1762	case ENETDOWN:
1763	case EHOSTDOWN:
1764	default:
1765		type = ICMP_UNREACH;
1766		code = ICMP_UNREACH_HOST;
1767		break;
1768
1769	case EMSGSIZE:
1770		type = ICMP_UNREACH;
1771		code = ICMP_UNREACH_NEEDFRAG;
1772#ifndef IPSEC
1773		if (ipforward_rt.ro_rt)
1774			destifp = ipforward_rt.ro_rt->rt_ifp;
1775#else
1776		/*
1777		 * If the packet is routed over IPsec tunnel, tell the
1778		 * originator the tunnel MTU.
1779		 *	tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1780		 * XXX quickhack!!!
1781		 */
1782		if (ipforward_rt.ro_rt) {
1783			struct secpolicy *sp = NULL;
1784			int ipsecerror;
1785			int ipsechdr;
1786			struct route *ro;
1787
1788			sp = ipsec4_getpolicybyaddr(mcopy,
1789						    IPSEC_DIR_OUTBOUND,
1790			                            IP_FORWARDING,
1791			                            &ipsecerror);
1792
1793			if (sp == NULL)
1794				destifp = ipforward_rt.ro_rt->rt_ifp;
1795			else {
1796				/* count IPsec header size */
1797				ipsechdr = ipsec4_hdrsiz(mcopy,
1798							 IPSEC_DIR_OUTBOUND,
1799							 NULL);
1800
1801				/*
1802				 * find the correct route for outer IPv4
1803				 * header, compute tunnel MTU.
1804				 *
1805				 * XXX BUG ALERT
1806				 * The "dummyifp" code relies upon the fact
1807				 * that icmp_error() touches only ifp->if_mtu.
1808				 */
1809				/*XXX*/
1810				destifp = NULL;
1811				if (sp->req != NULL
1812				 && sp->req->sav != NULL
1813				 && sp->req->sav->sah != NULL) {
1814					ro = &sp->req->sav->sah->sa_route;
1815					if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1816						dummyifp.if_mtu =
1817						    ro->ro_rt->rt_ifp->if_mtu;
1818						dummyifp.if_mtu -= ipsechdr;
1819						destifp = &dummyifp;
1820					}
1821				}
1822
1823				key_freesp(sp);
1824			}
1825		}
1826#endif /*IPSEC*/
1827		ipstat.ips_cantfrag++;
1828		break;
1829
1830	case ENOBUFS:
1831		type = ICMP_SOURCEQUENCH;
1832		code = 0;
1833		break;
1834
1835	case EACCES:			/* ipfw denied packet */
1836		m_freem(mcopy);
1837		return;
1838	}
1839	icmp_error(mcopy, type, code, dest, destifp);
1840}
1841
1842void
1843ip_savecontrol(inp, mp, ip, m)
1844	register struct inpcb *inp;
1845	register struct mbuf **mp;
1846	register struct ip *ip;
1847	register struct mbuf *m;
1848{
1849	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1850		struct timeval tv;
1851
1852		microtime(&tv);
1853		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1854			SCM_TIMESTAMP, SOL_SOCKET);
1855		if (*mp)
1856			mp = &(*mp)->m_next;
1857	}
1858	if (inp->inp_flags & INP_RECVDSTADDR) {
1859		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1860		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1861		if (*mp)
1862			mp = &(*mp)->m_next;
1863	}
1864#ifdef notyet
1865	/* XXX
1866	 * Moving these out of udp_input() made them even more broken
1867	 * than they already were.
1868	 */
1869	/* options were tossed already */
1870	if (inp->inp_flags & INP_RECVOPTS) {
1871		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1872		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1873		if (*mp)
1874			mp = &(*mp)->m_next;
1875	}
1876	/* ip_srcroute doesn't do what we want here, need to fix */
1877	if (inp->inp_flags & INP_RECVRETOPTS) {
1878		*mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1879		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1880		if (*mp)
1881			mp = &(*mp)->m_next;
1882	}
1883#endif
1884	if (inp->inp_flags & INP_RECVIF) {
1885		struct ifnet *ifp;
1886		struct sdlbuf {
1887			struct sockaddr_dl sdl;
1888			u_char	pad[32];
1889		} sdlbuf;
1890		struct sockaddr_dl *sdp;
1891		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1892
1893		if (((ifp = m->m_pkthdr.rcvif))
1894		&& ( ifp->if_index && (ifp->if_index <= if_index))) {
1895			sdp = (struct sockaddr_dl *)
1896			    (ifaddr_byindex(ifp->if_index)->ifa_addr);
1897			/*
1898			 * Change our mind and don't try copy.
1899			 */
1900			if ((sdp->sdl_family != AF_LINK)
1901			|| (sdp->sdl_len > sizeof(sdlbuf))) {
1902				goto makedummy;
1903			}
1904			bcopy(sdp, sdl2, sdp->sdl_len);
1905		} else {
1906makedummy:
1907			sdl2->sdl_len
1908				= offsetof(struct sockaddr_dl, sdl_data[0]);
1909			sdl2->sdl_family = AF_LINK;
1910			sdl2->sdl_index = 0;
1911			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1912		}
1913		*mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
1914			IP_RECVIF, IPPROTO_IP);
1915		if (*mp)
1916			mp = &(*mp)->m_next;
1917	}
1918}
1919
1920/*
1921 * XXX these routines are called from the upper part of the kernel.
1922 * They need to be locked when we remove Giant.
1923 *
1924 * They could also be moved to ip_mroute.c, since all the RSVP
1925 *  handling is done there already.
1926 */
1927static int ip_rsvp_on;
1928struct socket *ip_rsvpd;
1929int
1930ip_rsvp_init(struct socket *so)
1931{
1932	if (so->so_type != SOCK_RAW ||
1933	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1934	  return EOPNOTSUPP;
1935
1936	if (ip_rsvpd != NULL)
1937	  return EADDRINUSE;
1938
1939	ip_rsvpd = so;
1940	/*
1941	 * This may seem silly, but we need to be sure we don't over-increment
1942	 * the RSVP counter, in case something slips up.
1943	 */
1944	if (!ip_rsvp_on) {
1945		ip_rsvp_on = 1;
1946		rsvp_on++;
1947	}
1948
1949	return 0;
1950}
1951
1952int
1953ip_rsvp_done(void)
1954{
1955	ip_rsvpd = NULL;
1956	/*
1957	 * This may seem silly, but we need to be sure we don't over-decrement
1958	 * the RSVP counter, in case something slips up.
1959	 */
1960	if (ip_rsvp_on) {
1961		ip_rsvp_on = 0;
1962		rsvp_on--;
1963	}
1964	return 0;
1965}
1966