ip_input.c revision 254889
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/netinet/ip_input.c 254889 2013-08-25 21:54:41Z markj $");
34
35#include "opt_bootp.h"
36#include "opt_ipfw.h"
37#include "opt_ipstealth.h"
38#include "opt_ipsec.h"
39#include "opt_kdtrace.h"
40#include "opt_route.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/mbuf.h>
45#include <sys/malloc.h>
46#include <sys/domain.h>
47#include <sys/protosw.h>
48#include <sys/socket.h>
49#include <sys/time.h>
50#include <sys/kernel.h>
51#include <sys/lock.h>
52#include <sys/rwlock.h>
53#include <sys/sdt.h>
54#include <sys/syslog.h>
55#include <sys/sysctl.h>
56
57#include <net/pfil.h>
58#include <net/if.h>
59#include <net/if_types.h>
60#include <net/if_var.h>
61#include <net/if_dl.h>
62#include <net/route.h>
63#include <net/netisr.h>
64#include <net/vnet.h>
65#include <net/flowtable.h>
66
67#include <netinet/in.h>
68#include <netinet/in_kdtrace.h>
69#include <netinet/in_systm.h>
70#include <netinet/in_var.h>
71#include <netinet/ip.h>
72#include <netinet/in_pcb.h>
73#include <netinet/ip_var.h>
74#include <netinet/ip_fw.h>
75#include <netinet/ip_icmp.h>
76#include <netinet/ip_options.h>
77#include <machine/in_cksum.h>
78#include <netinet/ip_carp.h>
79#ifdef IPSEC
80#include <netinet/ip_ipsec.h>
81#endif /* IPSEC */
82
83#include <sys/socketvar.h>
84
85#include <security/mac/mac_framework.h>
86
87#ifdef CTASSERT
88CTASSERT(sizeof(struct ip) == 20);
89#endif
90
91struct	rwlock in_ifaddr_lock;
92RW_SYSINIT(in_ifaddr_lock, &in_ifaddr_lock, "in_ifaddr_lock");
93
94VNET_DEFINE(int, rsvp_on);
95
96VNET_DEFINE(int, ipforwarding);
97SYSCTL_VNET_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
98    &VNET_NAME(ipforwarding), 0,
99    "Enable IP forwarding between interfaces");
100
101static VNET_DEFINE(int, ipsendredirects) = 1;	/* XXX */
102#define	V_ipsendredirects	VNET(ipsendredirects)
103SYSCTL_VNET_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
104    &VNET_NAME(ipsendredirects), 0,
105    "Enable sending IP redirects");
106
107static VNET_DEFINE(int, ip_keepfaith);
108#define	V_ip_keepfaith		VNET(ip_keepfaith)
109SYSCTL_VNET_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
110    &VNET_NAME(ip_keepfaith), 0,
111    "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
112
113static VNET_DEFINE(int, ip_sendsourcequench);
114#define	V_ip_sendsourcequench	VNET(ip_sendsourcequench)
115SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
116    &VNET_NAME(ip_sendsourcequench), 0,
117    "Enable the transmission of source quench packets");
118
119VNET_DEFINE(int, ip_do_randomid);
120SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
121    &VNET_NAME(ip_do_randomid), 0,
122    "Assign random ip_id values");
123
124/*
125 * XXX - Setting ip_checkinterface mostly implements the receive side of
126 * the Strong ES model described in RFC 1122, but since the routing table
127 * and transmit implementation do not implement the Strong ES model,
128 * setting this to 1 results in an odd hybrid.
129 *
130 * XXX - ip_checkinterface currently must be disabled if you use ipnat
131 * to translate the destination address to another local interface.
132 *
133 * XXX - ip_checkinterface must be disabled if you add IP aliases
134 * to the loopback interface instead of the interface where the
135 * packets for those addresses are received.
136 */
137static VNET_DEFINE(int, ip_checkinterface);
138#define	V_ip_checkinterface	VNET(ip_checkinterface)
139SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
140    &VNET_NAME(ip_checkinterface), 0,
141    "Verify packet arrives on correct interface");
142
143VNET_DEFINE(struct pfil_head, inet_pfil_hook);	/* Packet filter hooks */
144
145static struct netisr_handler ip_nh = {
146	.nh_name = "ip",
147	.nh_handler = ip_input,
148	.nh_proto = NETISR_IP,
149	.nh_policy = NETISR_POLICY_FLOW,
150};
151
152extern	struct domain inetdomain;
153extern	struct protosw inetsw[];
154u_char	ip_protox[IPPROTO_MAX];
155VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead);  /* first inet address */
156VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table  */
157VNET_DEFINE(u_long, in_ifaddrhmask);		/* mask for hash table */
158
159static VNET_DEFINE(uma_zone_t, ipq_zone);
160static VNET_DEFINE(TAILQ_HEAD(ipqhead, ipq), ipq[IPREASS_NHASH]);
161static struct mtx ipqlock;
162
163#define	V_ipq_zone		VNET(ipq_zone)
164#define	V_ipq			VNET(ipq)
165
166#define	IPQ_LOCK()	mtx_lock(&ipqlock)
167#define	IPQ_UNLOCK()	mtx_unlock(&ipqlock)
168#define	IPQ_LOCK_INIT()	mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
169#define	IPQ_LOCK_ASSERT()	mtx_assert(&ipqlock, MA_OWNED)
170
171static void	maxnipq_update(void);
172static void	ipq_zone_change(void *);
173static void	ip_drain_locked(void);
174
175static VNET_DEFINE(int, maxnipq);  /* Administrative limit on # reass queues. */
176static VNET_DEFINE(int, nipq);			/* Total # of reass queues */
177#define	V_maxnipq		VNET(maxnipq)
178#define	V_nipq			VNET(nipq)
179SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD,
180    &VNET_NAME(nipq), 0,
181    "Current number of IPv4 fragment reassembly queue entries");
182
183static VNET_DEFINE(int, maxfragsperpacket);
184#define	V_maxfragsperpacket	VNET(maxfragsperpacket)
185SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
186    &VNET_NAME(maxfragsperpacket), 0,
187    "Maximum number of IPv4 fragments allowed per packet");
188
189#ifdef IPCTL_DEFMTU
190SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
191    &ip_mtu, 0, "Default MTU");
192#endif
193
194#ifdef IPSTEALTH
195VNET_DEFINE(int, ipstealth);
196SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
197    &VNET_NAME(ipstealth), 0,
198    "IP stealth mode, no TTL decrementation on forwarding");
199#endif
200
201#ifdef FLOWTABLE
202static VNET_DEFINE(int, ip_output_flowtable_size) = 2048;
203VNET_DEFINE(struct flowtable *, ip_ft);
204#define	V_ip_output_flowtable_size	VNET(ip_output_flowtable_size)
205
206SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, output_flowtable_size, CTLFLAG_RDTUN,
207    &VNET_NAME(ip_output_flowtable_size), 2048,
208    "number of entries in the per-cpu output flow caches");
209#endif
210
211static void	ip_freef(struct ipqhead *, struct ipq *);
212
213/*
214 * IP statistics are stored in the "array" of counter(9)s.
215 */
216VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat);
217VNET_PCPUSTAT_SYSINIT(ipstat);
218SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat,
219    "IP statistics (struct ipstat, netinet/ip_var.h)");
220
221#ifdef VIMAGE
222VNET_PCPUSTAT_SYSUNINIT(ipstat);
223#endif /* VIMAGE */
224
225/*
226 * Kernel module interface for updating ipstat.  The argument is an index
227 * into ipstat treated as an array.
228 */
229void
230kmod_ipstat_inc(int statnum)
231{
232
233	counter_u64_add(VNET(ipstat)[statnum], 1);
234}
235
236void
237kmod_ipstat_dec(int statnum)
238{
239
240	counter_u64_add(VNET(ipstat)[statnum], -1);
241}
242
243static int
244sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
245{
246	int error, qlimit;
247
248	netisr_getqlimit(&ip_nh, &qlimit);
249	error = sysctl_handle_int(oidp, &qlimit, 0, req);
250	if (error || !req->newptr)
251		return (error);
252	if (qlimit < 1)
253		return (EINVAL);
254	return (netisr_setqlimit(&ip_nh, qlimit));
255}
256SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen,
257    CTLTYPE_INT|CTLFLAG_RW, 0, 0, sysctl_netinet_intr_queue_maxlen, "I",
258    "Maximum size of the IP input queue");
259
260static int
261sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
262{
263	u_int64_t qdrops_long;
264	int error, qdrops;
265
266	netisr_getqdrops(&ip_nh, &qdrops_long);
267	qdrops = qdrops_long;
268	error = sysctl_handle_int(oidp, &qdrops, 0, req);
269	if (error || !req->newptr)
270		return (error);
271	if (qdrops != 0)
272		return (EINVAL);
273	netisr_clearqdrops(&ip_nh);
274	return (0);
275}
276
277SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops,
278    CTLTYPE_INT|CTLFLAG_RD, 0, 0, sysctl_netinet_intr_queue_drops, "I",
279    "Number of packets dropped from the IP input queue");
280
281/*
282 * IP initialization: fill in IP protocol switch table.
283 * All protocols not implemented in kernel go to raw IP protocol handler.
284 */
285void
286ip_init(void)
287{
288	struct protosw *pr;
289	int i;
290
291	V_ip_id = time_second & 0xffff;
292
293	TAILQ_INIT(&V_in_ifaddrhead);
294	V_in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &V_in_ifaddrhmask);
295
296	/* Initialize IP reassembly queue. */
297	for (i = 0; i < IPREASS_NHASH; i++)
298		TAILQ_INIT(&V_ipq[i]);
299	V_maxnipq = nmbclusters / 32;
300	V_maxfragsperpacket = 16;
301	V_ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
302	    NULL, UMA_ALIGN_PTR, 0);
303	maxnipq_update();
304
305	/* Initialize packet filter hooks. */
306	V_inet_pfil_hook.ph_type = PFIL_TYPE_AF;
307	V_inet_pfil_hook.ph_af = AF_INET;
308	if ((i = pfil_head_register(&V_inet_pfil_hook)) != 0)
309		printf("%s: WARNING: unable to register pfil hook, "
310			"error %d\n", __func__, i);
311
312#ifdef FLOWTABLE
313	if (TUNABLE_INT_FETCH("net.inet.ip.output_flowtable_size",
314		&V_ip_output_flowtable_size)) {
315		if (V_ip_output_flowtable_size < 256)
316			V_ip_output_flowtable_size = 256;
317		if (!powerof2(V_ip_output_flowtable_size)) {
318			printf("flowtable must be power of 2 size\n");
319			V_ip_output_flowtable_size = 2048;
320		}
321	} else {
322		/*
323		 * round up to the next power of 2
324		 */
325		V_ip_output_flowtable_size = 1 << fls((1024 + maxusers * 64)-1);
326	}
327	V_ip_ft = flowtable_alloc("ipv4", V_ip_output_flowtable_size, FL_PCPU);
328#endif
329
330	/* Skip initialization of globals for non-default instances. */
331	if (!IS_DEFAULT_VNET(curvnet))
332		return;
333
334	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
335	if (pr == NULL)
336		panic("ip_init: PF_INET not found");
337
338	/* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
339	for (i = 0; i < IPPROTO_MAX; i++)
340		ip_protox[i] = pr - inetsw;
341	/*
342	 * Cycle through IP protocols and put them into the appropriate place
343	 * in ip_protox[].
344	 */
345	for (pr = inetdomain.dom_protosw;
346	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
347		if (pr->pr_domain->dom_family == PF_INET &&
348		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
349			/* Be careful to only index valid IP protocols. */
350			if (pr->pr_protocol < IPPROTO_MAX)
351				ip_protox[pr->pr_protocol] = pr - inetsw;
352		}
353
354	EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
355		NULL, EVENTHANDLER_PRI_ANY);
356
357	/* Initialize various other remaining things. */
358	IPQ_LOCK_INIT();
359	netisr_register(&ip_nh);
360}
361
362#ifdef VIMAGE
363void
364ip_destroy(void)
365{
366
367	/* Cleanup in_ifaddr hash table; should be empty. */
368	hashdestroy(V_in_ifaddrhashtbl, M_IFADDR, V_in_ifaddrhmask);
369
370	IPQ_LOCK();
371	ip_drain_locked();
372	IPQ_UNLOCK();
373
374	uma_zdestroy(V_ipq_zone);
375}
376#endif
377
378/*
379 * Ip input routine.  Checksum and byte swap header.  If fragmented
380 * try to reassemble.  Process options.  Pass to next level.
381 */
382void
383ip_input(struct mbuf *m)
384{
385	struct ip *ip = NULL;
386	struct in_ifaddr *ia = NULL;
387	struct ifaddr *ifa;
388	struct ifnet *ifp;
389	int    checkif, hlen = 0;
390	uint16_t sum, ip_len;
391	int dchg = 0;				/* dest changed after fw */
392	struct in_addr odst;			/* original dst address */
393
394	M_ASSERTPKTHDR(m);
395
396	if (m->m_flags & M_FASTFWD_OURS) {
397		m->m_flags &= ~M_FASTFWD_OURS;
398		/* Set up some basics that will be used later. */
399		ip = mtod(m, struct ip *);
400		hlen = ip->ip_hl << 2;
401		ip_len = ntohs(ip->ip_len);
402		goto ours;
403	}
404
405	IPSTAT_INC(ips_total);
406
407	if (m->m_pkthdr.len < sizeof(struct ip))
408		goto tooshort;
409
410	if (m->m_len < sizeof (struct ip) &&
411	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
412		IPSTAT_INC(ips_toosmall);
413		return;
414	}
415	ip = mtod(m, struct ip *);
416
417	if (ip->ip_v != IPVERSION) {
418		IPSTAT_INC(ips_badvers);
419		goto bad;
420	}
421
422	hlen = ip->ip_hl << 2;
423	if (hlen < sizeof(struct ip)) {	/* minimum header length */
424		IPSTAT_INC(ips_badhlen);
425		goto bad;
426	}
427	if (hlen > m->m_len) {
428		if ((m = m_pullup(m, hlen)) == NULL) {
429			IPSTAT_INC(ips_badhlen);
430			return;
431		}
432		ip = mtod(m, struct ip *);
433	}
434
435	IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL);
436
437	/* 127/8 must not appear on wire - RFC1122 */
438	ifp = m->m_pkthdr.rcvif;
439	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
440	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
441		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
442			IPSTAT_INC(ips_badaddr);
443			goto bad;
444		}
445	}
446
447	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
448		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
449	} else {
450		if (hlen == sizeof(struct ip)) {
451			sum = in_cksum_hdr(ip);
452		} else {
453			sum = in_cksum(m, hlen);
454		}
455	}
456	if (sum) {
457		IPSTAT_INC(ips_badsum);
458		goto bad;
459	}
460
461#ifdef ALTQ
462	if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
463		/* packet is dropped by traffic conditioner */
464		return;
465#endif
466
467	ip_len = ntohs(ip->ip_len);
468	if (ip_len < hlen) {
469		IPSTAT_INC(ips_badlen);
470		goto bad;
471	}
472
473	/*
474	 * Check that the amount of data in the buffers
475	 * is as at least much as the IP header would have us expect.
476	 * Trim mbufs if longer than we expect.
477	 * Drop packet if shorter than we expect.
478	 */
479	if (m->m_pkthdr.len < ip_len) {
480tooshort:
481		IPSTAT_INC(ips_tooshort);
482		goto bad;
483	}
484	if (m->m_pkthdr.len > ip_len) {
485		if (m->m_len == m->m_pkthdr.len) {
486			m->m_len = ip_len;
487			m->m_pkthdr.len = ip_len;
488		} else
489			m_adj(m, ip_len - m->m_pkthdr.len);
490	}
491#ifdef IPSEC
492	/*
493	 * Bypass packet filtering for packets previously handled by IPsec.
494	 */
495	if (ip_ipsec_filtertunnel(m))
496		goto passin;
497#endif /* IPSEC */
498
499	/*
500	 * Run through list of hooks for input packets.
501	 *
502	 * NB: Beware of the destination address changing (e.g.
503	 *     by NAT rewriting).  When this happens, tell
504	 *     ip_forward to do the right thing.
505	 */
506
507	/* Jump over all PFIL processing if hooks are not active. */
508	if (!PFIL_HOOKED(&V_inet_pfil_hook))
509		goto passin;
510
511	odst = ip->ip_dst;
512	if (pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_IN, NULL) != 0)
513		return;
514	if (m == NULL)			/* consumed by filter */
515		return;
516
517	ip = mtod(m, struct ip *);
518	dchg = (odst.s_addr != ip->ip_dst.s_addr);
519	ifp = m->m_pkthdr.rcvif;
520
521	if (m->m_flags & M_FASTFWD_OURS) {
522		m->m_flags &= ~M_FASTFWD_OURS;
523		goto ours;
524	}
525	if (m->m_flags & M_IP_NEXTHOP) {
526		dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL);
527		if (dchg != 0) {
528			/*
529			 * Directly ship the packet on.  This allows
530			 * forwarding packets originally destined to us
531			 * to some other directly connected host.
532			 */
533			ip_forward(m, 1);
534			return;
535		}
536	}
537passin:
538
539	/*
540	 * Process options and, if not destined for us,
541	 * ship it on.  ip_dooptions returns 1 when an
542	 * error was detected (causing an icmp message
543	 * to be sent and the original packet to be freed).
544	 */
545	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
546		return;
547
548        /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
549         * matter if it is destined to another node, or whether it is
550         * a multicast one, RSVP wants it! and prevents it from being forwarded
551         * anywhere else. Also checks if the rsvp daemon is running before
552	 * grabbing the packet.
553         */
554	if (V_rsvp_on && ip->ip_p==IPPROTO_RSVP)
555		goto ours;
556
557	/*
558	 * Check our list of addresses, to see if the packet is for us.
559	 * If we don't have any addresses, assume any unicast packet
560	 * we receive might be for us (and let the upper layers deal
561	 * with it).
562	 */
563	if (TAILQ_EMPTY(&V_in_ifaddrhead) &&
564	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
565		goto ours;
566
567	/*
568	 * Enable a consistency check between the destination address
569	 * and the arrival interface for a unicast packet (the RFC 1122
570	 * strong ES model) if IP forwarding is disabled and the packet
571	 * is not locally generated and the packet is not subject to
572	 * 'ipfw fwd'.
573	 *
574	 * XXX - Checking also should be disabled if the destination
575	 * address is ipnat'ed to a different interface.
576	 *
577	 * XXX - Checking is incompatible with IP aliases added
578	 * to the loopback interface instead of the interface where
579	 * the packets are received.
580	 *
581	 * XXX - This is the case for carp vhost IPs as well so we
582	 * insert a workaround. If the packet got here, we already
583	 * checked with carp_iamatch() and carp_forus().
584	 */
585	checkif = V_ip_checkinterface && (V_ipforwarding == 0) &&
586	    ifp != NULL && ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
587	    ifp->if_carp == NULL && (dchg == 0);
588
589	/*
590	 * Check for exact addresses in the hash bucket.
591	 */
592	/* IN_IFADDR_RLOCK(); */
593	LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
594		/*
595		 * If the address matches, verify that the packet
596		 * arrived via the correct interface if checking is
597		 * enabled.
598		 */
599		if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
600		    (!checkif || ia->ia_ifp == ifp)) {
601			ifa_ref(&ia->ia_ifa);
602			/* IN_IFADDR_RUNLOCK(); */
603			goto ours;
604		}
605	}
606	/* IN_IFADDR_RUNLOCK(); */
607
608	/*
609	 * Check for broadcast addresses.
610	 *
611	 * Only accept broadcast packets that arrive via the matching
612	 * interface.  Reception of forwarded directed broadcasts would
613	 * be handled via ip_forward() and ether_output() with the loopback
614	 * into the stack for SIMPLEX interfaces handled by ether_output().
615	 */
616	if (ifp != NULL && ifp->if_flags & IFF_BROADCAST) {
617		IF_ADDR_RLOCK(ifp);
618	        TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
619			if (ifa->ifa_addr->sa_family != AF_INET)
620				continue;
621			ia = ifatoia(ifa);
622			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
623			    ip->ip_dst.s_addr) {
624				ifa_ref(ifa);
625				IF_ADDR_RUNLOCK(ifp);
626				goto ours;
627			}
628#ifdef BOOTP_COMPAT
629			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
630				ifa_ref(ifa);
631				IF_ADDR_RUNLOCK(ifp);
632				goto ours;
633			}
634#endif
635		}
636		IF_ADDR_RUNLOCK(ifp);
637		ia = NULL;
638	}
639	/* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
640	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
641		IPSTAT_INC(ips_cantforward);
642		m_freem(m);
643		return;
644	}
645	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
646		if (V_ip_mrouter) {
647			/*
648			 * If we are acting as a multicast router, all
649			 * incoming multicast packets are passed to the
650			 * kernel-level multicast forwarding function.
651			 * The packet is returned (relatively) intact; if
652			 * ip_mforward() returns a non-zero value, the packet
653			 * must be discarded, else it may be accepted below.
654			 */
655			if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
656				IPSTAT_INC(ips_cantforward);
657				m_freem(m);
658				return;
659			}
660
661			/*
662			 * The process-level routing daemon needs to receive
663			 * all multicast IGMP packets, whether or not this
664			 * host belongs to their destination groups.
665			 */
666			if (ip->ip_p == IPPROTO_IGMP)
667				goto ours;
668			IPSTAT_INC(ips_forward);
669		}
670		/*
671		 * Assume the packet is for us, to avoid prematurely taking
672		 * a lock on the in_multi hash. Protocols must perform
673		 * their own filtering and update statistics accordingly.
674		 */
675		goto ours;
676	}
677	if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
678		goto ours;
679	if (ip->ip_dst.s_addr == INADDR_ANY)
680		goto ours;
681
682	/*
683	 * FAITH(Firewall Aided Internet Translator)
684	 */
685	if (ifp && ifp->if_type == IFT_FAITH) {
686		if (V_ip_keepfaith) {
687			if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
688				goto ours;
689		}
690		m_freem(m);
691		return;
692	}
693
694	/*
695	 * Not for us; forward if possible and desirable.
696	 */
697	if (V_ipforwarding == 0) {
698		IPSTAT_INC(ips_cantforward);
699		m_freem(m);
700	} else {
701#ifdef IPSEC
702		if (ip_ipsec_fwd(m))
703			goto bad;
704#endif /* IPSEC */
705		ip_forward(m, dchg);
706	}
707	return;
708
709ours:
710#ifdef IPSTEALTH
711	/*
712	 * IPSTEALTH: Process non-routing options only
713	 * if the packet is destined for us.
714	 */
715	if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1)) {
716		if (ia != NULL)
717			ifa_free(&ia->ia_ifa);
718		return;
719	}
720#endif /* IPSTEALTH */
721
722	/* Count the packet in the ip address stats */
723	if (ia != NULL) {
724		ia->ia_ifa.if_ipackets++;
725		ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
726		ifa_free(&ia->ia_ifa);
727	}
728
729	/*
730	 * Attempt reassembly; if it succeeds, proceed.
731	 * ip_reass() will return a different mbuf.
732	 */
733	if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
734		m = ip_reass(m);
735		if (m == NULL)
736			return;
737		ip = mtod(m, struct ip *);
738		/* Get the header length of the reassembled packet */
739		hlen = ip->ip_hl << 2;
740	}
741
742#ifdef IPSEC
743	/*
744	 * enforce IPsec policy checking if we are seeing last header.
745	 * note that we do not visit this with protocols with pcb layer
746	 * code - like udp/tcp/raw ip.
747	 */
748	if (ip_ipsec_input(m))
749		goto bad;
750#endif /* IPSEC */
751
752	/*
753	 * Switch out to protocol's input routine.
754	 */
755	IPSTAT_INC(ips_delivered);
756
757	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
758	return;
759bad:
760	m_freem(m);
761}
762
763/*
764 * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
765 * max has slightly different semantics than the sysctl, for historical
766 * reasons.
767 */
768static void
769maxnipq_update(void)
770{
771
772	/*
773	 * -1 for unlimited allocation.
774	 */
775	if (V_maxnipq < 0)
776		uma_zone_set_max(V_ipq_zone, 0);
777	/*
778	 * Positive number for specific bound.
779	 */
780	if (V_maxnipq > 0)
781		uma_zone_set_max(V_ipq_zone, V_maxnipq);
782	/*
783	 * Zero specifies no further fragment queue allocation -- set the
784	 * bound very low, but rely on implementation elsewhere to actually
785	 * prevent allocation and reclaim current queues.
786	 */
787	if (V_maxnipq == 0)
788		uma_zone_set_max(V_ipq_zone, 1);
789}
790
791static void
792ipq_zone_change(void *tag)
793{
794
795	if (V_maxnipq > 0 && V_maxnipq < (nmbclusters / 32)) {
796		V_maxnipq = nmbclusters / 32;
797		maxnipq_update();
798	}
799}
800
801static int
802sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
803{
804	int error, i;
805
806	i = V_maxnipq;
807	error = sysctl_handle_int(oidp, &i, 0, req);
808	if (error || !req->newptr)
809		return (error);
810
811	/*
812	 * XXXRW: Might be a good idea to sanity check the argument and place
813	 * an extreme upper bound.
814	 */
815	if (i < -1)
816		return (EINVAL);
817	V_maxnipq = i;
818	maxnipq_update();
819	return (0);
820}
821
822SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
823    NULL, 0, sysctl_maxnipq, "I",
824    "Maximum number of IPv4 fragment reassembly queue entries");
825
826/*
827 * Take incoming datagram fragment and try to reassemble it into
828 * whole datagram.  If the argument is the first fragment or one
829 * in between the function will return NULL and store the mbuf
830 * in the fragment chain.  If the argument is the last fragment
831 * the packet will be reassembled and the pointer to the new
832 * mbuf returned for further processing.  Only m_tags attached
833 * to the first packet/fragment are preserved.
834 * The IP header is *NOT* adjusted out of iplen.
835 */
836struct mbuf *
837ip_reass(struct mbuf *m)
838{
839	struct ip *ip;
840	struct mbuf *p, *q, *nq, *t;
841	struct ipq *fp = NULL;
842	struct ipqhead *head;
843	int i, hlen, next;
844	u_int8_t ecn, ecn0;
845	u_short hash;
846
847	/* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
848	if (V_maxnipq == 0 || V_maxfragsperpacket == 0) {
849		IPSTAT_INC(ips_fragments);
850		IPSTAT_INC(ips_fragdropped);
851		m_freem(m);
852		return (NULL);
853	}
854
855	ip = mtod(m, struct ip *);
856	hlen = ip->ip_hl << 2;
857
858	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
859	head = &V_ipq[hash];
860	IPQ_LOCK();
861
862	/*
863	 * Look for queue of fragments
864	 * of this datagram.
865	 */
866	TAILQ_FOREACH(fp, head, ipq_list)
867		if (ip->ip_id == fp->ipq_id &&
868		    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
869		    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
870#ifdef MAC
871		    mac_ipq_match(m, fp) &&
872#endif
873		    ip->ip_p == fp->ipq_p)
874			goto found;
875
876	fp = NULL;
877
878	/*
879	 * Attempt to trim the number of allocated fragment queues if it
880	 * exceeds the administrative limit.
881	 */
882	if ((V_nipq > V_maxnipq) && (V_maxnipq > 0)) {
883		/*
884		 * drop something from the tail of the current queue
885		 * before proceeding further
886		 */
887		struct ipq *q = TAILQ_LAST(head, ipqhead);
888		if (q == NULL) {   /* gak */
889			for (i = 0; i < IPREASS_NHASH; i++) {
890				struct ipq *r = TAILQ_LAST(&V_ipq[i], ipqhead);
891				if (r) {
892					IPSTAT_ADD(ips_fragtimeout,
893					    r->ipq_nfrags);
894					ip_freef(&V_ipq[i], r);
895					break;
896				}
897			}
898		} else {
899			IPSTAT_ADD(ips_fragtimeout, q->ipq_nfrags);
900			ip_freef(head, q);
901		}
902	}
903
904found:
905	/*
906	 * Adjust ip_len to not reflect header,
907	 * convert offset of this to bytes.
908	 */
909	ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
910	if (ip->ip_off & htons(IP_MF)) {
911		/*
912		 * Make sure that fragments have a data length
913		 * that's a non-zero multiple of 8 bytes.
914		 */
915		if (ip->ip_len == htons(0) || (ntohs(ip->ip_len) & 0x7) != 0) {
916			IPSTAT_INC(ips_toosmall); /* XXX */
917			goto dropfrag;
918		}
919		m->m_flags |= M_IP_FRAG;
920	} else
921		m->m_flags &= ~M_IP_FRAG;
922	ip->ip_off = htons(ntohs(ip->ip_off) << 3);
923
924	/*
925	 * Attempt reassembly; if it succeeds, proceed.
926	 * ip_reass() will return a different mbuf.
927	 */
928	IPSTAT_INC(ips_fragments);
929	m->m_pkthdr.PH_loc.ptr = ip;
930
931	/* Previous ip_reass() started here. */
932	/*
933	 * Presence of header sizes in mbufs
934	 * would confuse code below.
935	 */
936	m->m_data += hlen;
937	m->m_len -= hlen;
938
939	/*
940	 * If first fragment to arrive, create a reassembly queue.
941	 */
942	if (fp == NULL) {
943		fp = uma_zalloc(V_ipq_zone, M_NOWAIT);
944		if (fp == NULL)
945			goto dropfrag;
946#ifdef MAC
947		if (mac_ipq_init(fp, M_NOWAIT) != 0) {
948			uma_zfree(V_ipq_zone, fp);
949			fp = NULL;
950			goto dropfrag;
951		}
952		mac_ipq_create(m, fp);
953#endif
954		TAILQ_INSERT_HEAD(head, fp, ipq_list);
955		V_nipq++;
956		fp->ipq_nfrags = 1;
957		fp->ipq_ttl = IPFRAGTTL;
958		fp->ipq_p = ip->ip_p;
959		fp->ipq_id = ip->ip_id;
960		fp->ipq_src = ip->ip_src;
961		fp->ipq_dst = ip->ip_dst;
962		fp->ipq_frags = m;
963		m->m_nextpkt = NULL;
964		goto done;
965	} else {
966		fp->ipq_nfrags++;
967#ifdef MAC
968		mac_ipq_update(m, fp);
969#endif
970	}
971
972#define GETIP(m)	((struct ip*)((m)->m_pkthdr.PH_loc.ptr))
973
974	/*
975	 * Handle ECN by comparing this segment with the first one;
976	 * if CE is set, do not lose CE.
977	 * drop if CE and not-ECT are mixed for the same packet.
978	 */
979	ecn = ip->ip_tos & IPTOS_ECN_MASK;
980	ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
981	if (ecn == IPTOS_ECN_CE) {
982		if (ecn0 == IPTOS_ECN_NOTECT)
983			goto dropfrag;
984		if (ecn0 != IPTOS_ECN_CE)
985			GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
986	}
987	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
988		goto dropfrag;
989
990	/*
991	 * Find a segment which begins after this one does.
992	 */
993	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
994		if (ntohs(GETIP(q)->ip_off) > ntohs(ip->ip_off))
995			break;
996
997	/*
998	 * If there is a preceding segment, it may provide some of
999	 * our data already.  If so, drop the data from the incoming
1000	 * segment.  If it provides all of our data, drop us, otherwise
1001	 * stick new segment in the proper place.
1002	 *
1003	 * If some of the data is dropped from the preceding
1004	 * segment, then it's checksum is invalidated.
1005	 */
1006	if (p) {
1007		i = ntohs(GETIP(p)->ip_off) + ntohs(GETIP(p)->ip_len) -
1008		    ntohs(ip->ip_off);
1009		if (i > 0) {
1010			if (i >= ntohs(ip->ip_len))
1011				goto dropfrag;
1012			m_adj(m, i);
1013			m->m_pkthdr.csum_flags = 0;
1014			ip->ip_off = htons(ntohs(ip->ip_off) + i);
1015			ip->ip_len = htons(ntohs(ip->ip_len) - i);
1016		}
1017		m->m_nextpkt = p->m_nextpkt;
1018		p->m_nextpkt = m;
1019	} else {
1020		m->m_nextpkt = fp->ipq_frags;
1021		fp->ipq_frags = m;
1022	}
1023
1024	/*
1025	 * While we overlap succeeding segments trim them or,
1026	 * if they are completely covered, dequeue them.
1027	 */
1028	for (; q != NULL && ntohs(ip->ip_off) + ntohs(ip->ip_len) >
1029	    ntohs(GETIP(q)->ip_off); q = nq) {
1030		i = (ntohs(ip->ip_off) + ntohs(ip->ip_len)) -
1031		    ntohs(GETIP(q)->ip_off);
1032		if (i < ntohs(GETIP(q)->ip_len)) {
1033			GETIP(q)->ip_len = htons(ntohs(GETIP(q)->ip_len) - i);
1034			GETIP(q)->ip_off = htons(ntohs(GETIP(q)->ip_off) + i);
1035			m_adj(q, i);
1036			q->m_pkthdr.csum_flags = 0;
1037			break;
1038		}
1039		nq = q->m_nextpkt;
1040		m->m_nextpkt = nq;
1041		IPSTAT_INC(ips_fragdropped);
1042		fp->ipq_nfrags--;
1043		m_freem(q);
1044	}
1045
1046	/*
1047	 * Check for complete reassembly and perform frag per packet
1048	 * limiting.
1049	 *
1050	 * Frag limiting is performed here so that the nth frag has
1051	 * a chance to complete the packet before we drop the packet.
1052	 * As a result, n+1 frags are actually allowed per packet, but
1053	 * only n will ever be stored. (n = maxfragsperpacket.)
1054	 *
1055	 */
1056	next = 0;
1057	for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1058		if (ntohs(GETIP(q)->ip_off) != next) {
1059			if (fp->ipq_nfrags > V_maxfragsperpacket) {
1060				IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1061				ip_freef(head, fp);
1062			}
1063			goto done;
1064		}
1065		next += ntohs(GETIP(q)->ip_len);
1066	}
1067	/* Make sure the last packet didn't have the IP_MF flag */
1068	if (p->m_flags & M_IP_FRAG) {
1069		if (fp->ipq_nfrags > V_maxfragsperpacket) {
1070			IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1071			ip_freef(head, fp);
1072		}
1073		goto done;
1074	}
1075
1076	/*
1077	 * Reassembly is complete.  Make sure the packet is a sane size.
1078	 */
1079	q = fp->ipq_frags;
1080	ip = GETIP(q);
1081	if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
1082		IPSTAT_INC(ips_toolong);
1083		IPSTAT_ADD(ips_fragdropped, fp->ipq_nfrags);
1084		ip_freef(head, fp);
1085		goto done;
1086	}
1087
1088	/*
1089	 * Concatenate fragments.
1090	 */
1091	m = q;
1092	t = m->m_next;
1093	m->m_next = NULL;
1094	m_cat(m, t);
1095	nq = q->m_nextpkt;
1096	q->m_nextpkt = NULL;
1097	for (q = nq; q != NULL; q = nq) {
1098		nq = q->m_nextpkt;
1099		q->m_nextpkt = NULL;
1100		m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1101		m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1102		m_cat(m, q);
1103	}
1104	/*
1105	 * In order to do checksumming faster we do 'end-around carry' here
1106	 * (and not in for{} loop), though it implies we are not going to
1107	 * reassemble more than 64k fragments.
1108	 */
1109	m->m_pkthdr.csum_data =
1110	    (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
1111#ifdef MAC
1112	mac_ipq_reassemble(fp, m);
1113	mac_ipq_destroy(fp);
1114#endif
1115
1116	/*
1117	 * Create header for new ip packet by modifying header of first
1118	 * packet;  dequeue and discard fragment reassembly header.
1119	 * Make header visible.
1120	 */
1121	ip->ip_len = htons((ip->ip_hl << 2) + next);
1122	ip->ip_src = fp->ipq_src;
1123	ip->ip_dst = fp->ipq_dst;
1124	TAILQ_REMOVE(head, fp, ipq_list);
1125	V_nipq--;
1126	uma_zfree(V_ipq_zone, fp);
1127	m->m_len += (ip->ip_hl << 2);
1128	m->m_data -= (ip->ip_hl << 2);
1129	/* some debugging cruft by sklower, below, will go away soon */
1130	if (m->m_flags & M_PKTHDR)	/* XXX this should be done elsewhere */
1131		m_fixhdr(m);
1132	IPSTAT_INC(ips_reassembled);
1133	IPQ_UNLOCK();
1134	return (m);
1135
1136dropfrag:
1137	IPSTAT_INC(ips_fragdropped);
1138	if (fp != NULL)
1139		fp->ipq_nfrags--;
1140	m_freem(m);
1141done:
1142	IPQ_UNLOCK();
1143	return (NULL);
1144
1145#undef GETIP
1146}
1147
1148/*
1149 * Free a fragment reassembly header and all
1150 * associated datagrams.
1151 */
1152static void
1153ip_freef(struct ipqhead *fhp, struct ipq *fp)
1154{
1155	struct mbuf *q;
1156
1157	IPQ_LOCK_ASSERT();
1158
1159	while (fp->ipq_frags) {
1160		q = fp->ipq_frags;
1161		fp->ipq_frags = q->m_nextpkt;
1162		m_freem(q);
1163	}
1164	TAILQ_REMOVE(fhp, fp, ipq_list);
1165	uma_zfree(V_ipq_zone, fp);
1166	V_nipq--;
1167}
1168
1169/*
1170 * IP timer processing;
1171 * if a timer expires on a reassembly
1172 * queue, discard it.
1173 */
1174void
1175ip_slowtimo(void)
1176{
1177	VNET_ITERATOR_DECL(vnet_iter);
1178	struct ipq *fp;
1179	int i;
1180
1181	VNET_LIST_RLOCK_NOSLEEP();
1182	IPQ_LOCK();
1183	VNET_FOREACH(vnet_iter) {
1184		CURVNET_SET(vnet_iter);
1185		for (i = 0; i < IPREASS_NHASH; i++) {
1186			for(fp = TAILQ_FIRST(&V_ipq[i]); fp;) {
1187				struct ipq *fpp;
1188
1189				fpp = fp;
1190				fp = TAILQ_NEXT(fp, ipq_list);
1191				if(--fpp->ipq_ttl == 0) {
1192					IPSTAT_ADD(ips_fragtimeout,
1193					    fpp->ipq_nfrags);
1194					ip_freef(&V_ipq[i], fpp);
1195				}
1196			}
1197		}
1198		/*
1199		 * If we are over the maximum number of fragments
1200		 * (due to the limit being lowered), drain off
1201		 * enough to get down to the new limit.
1202		 */
1203		if (V_maxnipq >= 0 && V_nipq > V_maxnipq) {
1204			for (i = 0; i < IPREASS_NHASH; i++) {
1205				while (V_nipq > V_maxnipq &&
1206				    !TAILQ_EMPTY(&V_ipq[i])) {
1207					IPSTAT_ADD(ips_fragdropped,
1208					    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
1209					ip_freef(&V_ipq[i],
1210					    TAILQ_FIRST(&V_ipq[i]));
1211				}
1212			}
1213		}
1214		CURVNET_RESTORE();
1215	}
1216	IPQ_UNLOCK();
1217	VNET_LIST_RUNLOCK_NOSLEEP();
1218}
1219
1220/*
1221 * Drain off all datagram fragments.
1222 */
1223static void
1224ip_drain_locked(void)
1225{
1226	int     i;
1227
1228	IPQ_LOCK_ASSERT();
1229
1230	for (i = 0; i < IPREASS_NHASH; i++) {
1231		while(!TAILQ_EMPTY(&V_ipq[i])) {
1232			IPSTAT_ADD(ips_fragdropped,
1233			    TAILQ_FIRST(&V_ipq[i])->ipq_nfrags);
1234			ip_freef(&V_ipq[i], TAILQ_FIRST(&V_ipq[i]));
1235		}
1236	}
1237}
1238
1239void
1240ip_drain(void)
1241{
1242	VNET_ITERATOR_DECL(vnet_iter);
1243
1244	VNET_LIST_RLOCK_NOSLEEP();
1245	IPQ_LOCK();
1246	VNET_FOREACH(vnet_iter) {
1247		CURVNET_SET(vnet_iter);
1248		ip_drain_locked();
1249		CURVNET_RESTORE();
1250	}
1251	IPQ_UNLOCK();
1252	VNET_LIST_RUNLOCK_NOSLEEP();
1253	in_rtqdrain();
1254}
1255
1256/*
1257 * The protocol to be inserted into ip_protox[] must be already registered
1258 * in inetsw[], either statically or through pf_proto_register().
1259 */
1260int
1261ipproto_register(short ipproto)
1262{
1263	struct protosw *pr;
1264
1265	/* Sanity checks. */
1266	if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1267		return (EPROTONOSUPPORT);
1268
1269	/*
1270	 * The protocol slot must not be occupied by another protocol
1271	 * already.  An index pointing to IPPROTO_RAW is unused.
1272	 */
1273	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1274	if (pr == NULL)
1275		return (EPFNOSUPPORT);
1276	if (ip_protox[ipproto] != pr - inetsw)	/* IPPROTO_RAW */
1277		return (EEXIST);
1278
1279	/* Find the protocol position in inetsw[] and set the index. */
1280	for (pr = inetdomain.dom_protosw;
1281	     pr < inetdomain.dom_protoswNPROTOSW; pr++) {
1282		if (pr->pr_domain->dom_family == PF_INET &&
1283		    pr->pr_protocol && pr->pr_protocol == ipproto) {
1284			ip_protox[pr->pr_protocol] = pr - inetsw;
1285			return (0);
1286		}
1287	}
1288	return (EPROTONOSUPPORT);
1289}
1290
1291int
1292ipproto_unregister(short ipproto)
1293{
1294	struct protosw *pr;
1295
1296	/* Sanity checks. */
1297	if (ipproto <= 0 || ipproto >= IPPROTO_MAX)
1298		return (EPROTONOSUPPORT);
1299
1300	/* Check if the protocol was indeed registered. */
1301	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
1302	if (pr == NULL)
1303		return (EPFNOSUPPORT);
1304	if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
1305		return (ENOENT);
1306
1307	/* Reset the protocol slot to IPPROTO_RAW. */
1308	ip_protox[ipproto] = pr - inetsw;
1309	return (0);
1310}
1311
1312/*
1313 * Given address of next destination (final or next hop), return (referenced)
1314 * internet address info of interface to be used to get there.
1315 */
1316struct in_ifaddr *
1317ip_rtaddr(struct in_addr dst, u_int fibnum)
1318{
1319	struct route sro;
1320	struct sockaddr_in *sin;
1321	struct in_ifaddr *ia;
1322
1323	bzero(&sro, sizeof(sro));
1324	sin = (struct sockaddr_in *)&sro.ro_dst;
1325	sin->sin_family = AF_INET;
1326	sin->sin_len = sizeof(*sin);
1327	sin->sin_addr = dst;
1328	in_rtalloc_ign(&sro, 0, fibnum);
1329
1330	if (sro.ro_rt == NULL)
1331		return (NULL);
1332
1333	ia = ifatoia(sro.ro_rt->rt_ifa);
1334	ifa_ref(&ia->ia_ifa);
1335	RTFREE(sro.ro_rt);
1336	return (ia);
1337}
1338
1339u_char inetctlerrmap[PRC_NCMDS] = {
1340	0,		0,		0,		0,
1341	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1342	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1343	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1344	0,		0,		EHOSTUNREACH,	0,
1345	ENOPROTOOPT,	ECONNREFUSED
1346};
1347
1348/*
1349 * Forward a packet.  If some error occurs return the sender
1350 * an icmp packet.  Note we can't always generate a meaningful
1351 * icmp message because icmp doesn't have a large enough repertoire
1352 * of codes and types.
1353 *
1354 * If not forwarding, just drop the packet.  This could be confusing
1355 * if ipforwarding was zero but some routing protocol was advancing
1356 * us as a gateway to somewhere.  However, we must let the routing
1357 * protocol deal with that.
1358 *
1359 * The srcrt parameter indicates whether the packet is being forwarded
1360 * via a source route.
1361 */
1362void
1363ip_forward(struct mbuf *m, int srcrt)
1364{
1365	struct ip *ip = mtod(m, struct ip *);
1366	struct in_ifaddr *ia;
1367	struct mbuf *mcopy;
1368	struct in_addr dest;
1369	struct route ro;
1370	int error, type = 0, code = 0, mtu = 0;
1371
1372	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1373		IPSTAT_INC(ips_cantforward);
1374		m_freem(m);
1375		return;
1376	}
1377#ifdef IPSTEALTH
1378	if (!V_ipstealth) {
1379#endif
1380		if (ip->ip_ttl <= IPTTLDEC) {
1381			icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1382			    0, 0);
1383			return;
1384		}
1385#ifdef IPSTEALTH
1386	}
1387#endif
1388
1389	ia = ip_rtaddr(ip->ip_dst, M_GETFIB(m));
1390#ifndef IPSEC
1391	/*
1392	 * 'ia' may be NULL if there is no route for this destination.
1393	 * In case of IPsec, Don't discard it just yet, but pass it to
1394	 * ip_output in case of outgoing IPsec policy.
1395	 */
1396	if (!srcrt && ia == NULL) {
1397		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1398		return;
1399	}
1400#endif
1401
1402	/*
1403	 * Save the IP header and at most 8 bytes of the payload,
1404	 * in case we need to generate an ICMP message to the src.
1405	 *
1406	 * XXX this can be optimized a lot by saving the data in a local
1407	 * buffer on the stack (72 bytes at most), and only allocating the
1408	 * mbuf if really necessary. The vast majority of the packets
1409	 * are forwarded without having to send an ICMP back (either
1410	 * because unnecessary, or because rate limited), so we are
1411	 * really we are wasting a lot of work here.
1412	 *
1413	 * We don't use m_copy() because it might return a reference
1414	 * to a shared cluster. Both this function and ip_output()
1415	 * assume exclusive access to the IP header in `m', so any
1416	 * data in a cluster may change before we reach icmp_error().
1417	 */
1418	mcopy = m_gethdr(M_NOWAIT, m->m_type);
1419	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
1420		/*
1421		 * It's probably ok if the pkthdr dup fails (because
1422		 * the deep copy of the tag chain failed), but for now
1423		 * be conservative and just discard the copy since
1424		 * code below may some day want the tags.
1425		 */
1426		m_free(mcopy);
1427		mcopy = NULL;
1428	}
1429	if (mcopy != NULL) {
1430		mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
1431		mcopy->m_pkthdr.len = mcopy->m_len;
1432		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1433	}
1434
1435#ifdef IPSTEALTH
1436	if (!V_ipstealth) {
1437#endif
1438		ip->ip_ttl -= IPTTLDEC;
1439#ifdef IPSTEALTH
1440	}
1441#endif
1442
1443	/*
1444	 * If forwarding packet using same interface that it came in on,
1445	 * perhaps should send a redirect to sender to shortcut a hop.
1446	 * Only send redirect if source is sending directly to us,
1447	 * and if packet was not source routed (or has any options).
1448	 * Also, don't send redirect if forwarding using a default route
1449	 * or a route modified by a redirect.
1450	 */
1451	dest.s_addr = 0;
1452	if (!srcrt && V_ipsendredirects &&
1453	    ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
1454		struct sockaddr_in *sin;
1455		struct rtentry *rt;
1456
1457		bzero(&ro, sizeof(ro));
1458		sin = (struct sockaddr_in *)&ro.ro_dst;
1459		sin->sin_family = AF_INET;
1460		sin->sin_len = sizeof(*sin);
1461		sin->sin_addr = ip->ip_dst;
1462		in_rtalloc_ign(&ro, 0, M_GETFIB(m));
1463
1464		rt = ro.ro_rt;
1465
1466		if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1467		    satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1468#define	RTA(rt)	((struct in_ifaddr *)(rt->rt_ifa))
1469			u_long src = ntohl(ip->ip_src.s_addr);
1470
1471			if (RTA(rt) &&
1472			    (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1473				if (rt->rt_flags & RTF_GATEWAY)
1474					dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1475				else
1476					dest.s_addr = ip->ip_dst.s_addr;
1477				/* Router requirements says to only send host redirects */
1478				type = ICMP_REDIRECT;
1479				code = ICMP_REDIRECT_HOST;
1480			}
1481		}
1482		if (rt)
1483			RTFREE(rt);
1484	}
1485
1486	/*
1487	 * Try to cache the route MTU from ip_output so we can consider it for
1488	 * the ICMP_UNREACH_NEEDFRAG "Next-Hop MTU" field described in RFC1191.
1489	 */
1490	bzero(&ro, sizeof(ro));
1491
1492	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1493
1494	if (error == EMSGSIZE && ro.ro_rt)
1495		mtu = ro.ro_rt->rt_rmx.rmx_mtu;
1496	RO_RTFREE(&ro);
1497
1498	if (error)
1499		IPSTAT_INC(ips_cantforward);
1500	else {
1501		IPSTAT_INC(ips_forward);
1502		if (type)
1503			IPSTAT_INC(ips_redirectsent);
1504		else {
1505			if (mcopy)
1506				m_freem(mcopy);
1507			if (ia != NULL)
1508				ifa_free(&ia->ia_ifa);
1509			return;
1510		}
1511	}
1512	if (mcopy == NULL) {
1513		if (ia != NULL)
1514			ifa_free(&ia->ia_ifa);
1515		return;
1516	}
1517
1518	switch (error) {
1519
1520	case 0:				/* forwarded, but need redirect */
1521		/* type, code set above */
1522		break;
1523
1524	case ENETUNREACH:
1525	case EHOSTUNREACH:
1526	case ENETDOWN:
1527	case EHOSTDOWN:
1528	default:
1529		type = ICMP_UNREACH;
1530		code = ICMP_UNREACH_HOST;
1531		break;
1532
1533	case EMSGSIZE:
1534		type = ICMP_UNREACH;
1535		code = ICMP_UNREACH_NEEDFRAG;
1536
1537#ifdef IPSEC
1538		/*
1539		 * If IPsec is configured for this path,
1540		 * override any possibly mtu value set by ip_output.
1541		 */
1542		mtu = ip_ipsec_mtu(mcopy, mtu);
1543#endif /* IPSEC */
1544		/*
1545		 * If the MTU was set before make sure we are below the
1546		 * interface MTU.
1547		 * If the MTU wasn't set before use the interface mtu or
1548		 * fall back to the next smaller mtu step compared to the
1549		 * current packet size.
1550		 */
1551		if (mtu != 0) {
1552			if (ia != NULL)
1553				mtu = min(mtu, ia->ia_ifp->if_mtu);
1554		} else {
1555			if (ia != NULL)
1556				mtu = ia->ia_ifp->if_mtu;
1557			else
1558				mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
1559		}
1560		IPSTAT_INC(ips_cantfrag);
1561		break;
1562
1563	case ENOBUFS:
1564		/*
1565		 * A router should not generate ICMP_SOURCEQUENCH as
1566		 * required in RFC1812 Requirements for IP Version 4 Routers.
1567		 * Source quench could be a big problem under DoS attacks,
1568		 * or if the underlying interface is rate-limited.
1569		 * Those who need source quench packets may re-enable them
1570		 * via the net.inet.ip.sendsourcequench sysctl.
1571		 */
1572		if (V_ip_sendsourcequench == 0) {
1573			m_freem(mcopy);
1574			if (ia != NULL)
1575				ifa_free(&ia->ia_ifa);
1576			return;
1577		} else {
1578			type = ICMP_SOURCEQUENCH;
1579			code = 0;
1580		}
1581		break;
1582
1583	case EACCES:			/* ipfw denied packet */
1584		m_freem(mcopy);
1585		if (ia != NULL)
1586			ifa_free(&ia->ia_ifa);
1587		return;
1588	}
1589	if (ia != NULL)
1590		ifa_free(&ia->ia_ifa);
1591	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1592}
1593
1594void
1595ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1596    struct mbuf *m)
1597{
1598
1599	if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1600		struct bintime bt;
1601
1602		bintime(&bt);
1603		if (inp->inp_socket->so_options & SO_BINTIME) {
1604			*mp = sbcreatecontrol((caddr_t)&bt, sizeof(bt),
1605			    SCM_BINTIME, SOL_SOCKET);
1606			if (*mp)
1607				mp = &(*mp)->m_next;
1608		}
1609		if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1610			struct timeval tv;
1611
1612			bintime2timeval(&bt, &tv);
1613			*mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv),
1614			    SCM_TIMESTAMP, SOL_SOCKET);
1615			if (*mp)
1616				mp = &(*mp)->m_next;
1617		}
1618	}
1619	if (inp->inp_flags & INP_RECVDSTADDR) {
1620		*mp = sbcreatecontrol((caddr_t)&ip->ip_dst,
1621		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1622		if (*mp)
1623			mp = &(*mp)->m_next;
1624	}
1625	if (inp->inp_flags & INP_RECVTTL) {
1626		*mp = sbcreatecontrol((caddr_t)&ip->ip_ttl,
1627		    sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
1628		if (*mp)
1629			mp = &(*mp)->m_next;
1630	}
1631#ifdef notyet
1632	/* XXX
1633	 * Moving these out of udp_input() made them even more broken
1634	 * than they already were.
1635	 */
1636	/* options were tossed already */
1637	if (inp->inp_flags & INP_RECVOPTS) {
1638		*mp = sbcreatecontrol((caddr_t)opts_deleted_above,
1639		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1640		if (*mp)
1641			mp = &(*mp)->m_next;
1642	}
1643	/* ip_srcroute doesn't do what we want here, need to fix */
1644	if (inp->inp_flags & INP_RECVRETOPTS) {
1645		*mp = sbcreatecontrol((caddr_t)ip_srcroute(m),
1646		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1647		if (*mp)
1648			mp = &(*mp)->m_next;
1649	}
1650#endif
1651	if (inp->inp_flags & INP_RECVIF) {
1652		struct ifnet *ifp;
1653		struct sdlbuf {
1654			struct sockaddr_dl sdl;
1655			u_char	pad[32];
1656		} sdlbuf;
1657		struct sockaddr_dl *sdp;
1658		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1659
1660		if ((ifp = m->m_pkthdr.rcvif) &&
1661		    ifp->if_index && ifp->if_index <= V_if_index) {
1662			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1663			/*
1664			 * Change our mind and don't try copy.
1665			 */
1666			if (sdp->sdl_family != AF_LINK ||
1667			    sdp->sdl_len > sizeof(sdlbuf)) {
1668				goto makedummy;
1669			}
1670			bcopy(sdp, sdl2, sdp->sdl_len);
1671		} else {
1672makedummy:
1673			sdl2->sdl_len =
1674			    offsetof(struct sockaddr_dl, sdl_data[0]);
1675			sdl2->sdl_family = AF_LINK;
1676			sdl2->sdl_index = 0;
1677			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1678		}
1679		*mp = sbcreatecontrol((caddr_t)sdl2, sdl2->sdl_len,
1680		    IP_RECVIF, IPPROTO_IP);
1681		if (*mp)
1682			mp = &(*mp)->m_next;
1683	}
1684	if (inp->inp_flags & INP_RECVTOS) {
1685		*mp = sbcreatecontrol((caddr_t)&ip->ip_tos,
1686		    sizeof(u_char), IP_RECVTOS, IPPROTO_IP);
1687		if (*mp)
1688			mp = &(*mp)->m_next;
1689	}
1690}
1691
1692/*
1693 * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
1694 * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
1695 * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
1696 * compiled.
1697 */
1698static VNET_DEFINE(int, ip_rsvp_on);
1699VNET_DEFINE(struct socket *, ip_rsvpd);
1700
1701#define	V_ip_rsvp_on		VNET(ip_rsvp_on)
1702
1703int
1704ip_rsvp_init(struct socket *so)
1705{
1706
1707	if (so->so_type != SOCK_RAW ||
1708	    so->so_proto->pr_protocol != IPPROTO_RSVP)
1709		return EOPNOTSUPP;
1710
1711	if (V_ip_rsvpd != NULL)
1712		return EADDRINUSE;
1713
1714	V_ip_rsvpd = so;
1715	/*
1716	 * This may seem silly, but we need to be sure we don't over-increment
1717	 * the RSVP counter, in case something slips up.
1718	 */
1719	if (!V_ip_rsvp_on) {
1720		V_ip_rsvp_on = 1;
1721		V_rsvp_on++;
1722	}
1723
1724	return 0;
1725}
1726
1727int
1728ip_rsvp_done(void)
1729{
1730
1731	V_ip_rsvpd = NULL;
1732	/*
1733	 * This may seem silly, but we need to be sure we don't over-decrement
1734	 * the RSVP counter, in case something slips up.
1735	 */
1736	if (V_ip_rsvp_on) {
1737		V_ip_rsvp_on = 0;
1738		V_rsvp_on--;
1739	}
1740	return 0;
1741}
1742
1743void
1744rsvp_input(struct mbuf *m, int off)	/* XXX must fixup manually */
1745{
1746
1747	if (rsvp_input_p) { /* call the real one if loaded */
1748		rsvp_input_p(m, off);
1749		return;
1750	}
1751
1752	/* Can still get packets with rsvp_on = 0 if there is a local member
1753	 * of the group to which the RSVP packet is addressed.  But in this
1754	 * case we want to throw the packet away.
1755	 */
1756
1757	if (!V_rsvp_on) {
1758		m_freem(m);
1759		return;
1760	}
1761
1762	if (V_ip_rsvpd != NULL) {
1763		rip_input(m, off);
1764		return;
1765	}
1766	/* Drop the packet */
1767	m_freem(m);
1768}
1769