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