xref: /src/sys/netinet/ip_input.c (revision adba114dfbfb02c6f332237abf070cbcc480f914)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "opt_bootp.h"
33 #include "opt_inet.h"
34 #include "opt_ipstealth.h"
35 #include "opt_ipsec.h"
36 #include "opt_rss.h"
37 #include "opt_sctp.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/hhook.h>
42 #include <sys/mbuf.h>
43 #include <sys/malloc.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/time.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/rmlock.h>
51 #include <sys/rwlock.h>
52 #include <sys/sdt.h>
53 #include <sys/syslog.h>
54 #include <sys/sysctl.h>
55 #include <sys/hash.h>
56 
57 #include <net/if.h>
58 #include <net/if_types.h>
59 #include <net/if_var.h>
60 #include <net/if_dl.h>
61 #include <net/if_private.h>
62 #include <net/pfil.h>
63 #include <net/route.h>
64 #include <net/route/nhop.h>
65 #include <net/netisr.h>
66 #include <net/rss_config.h>
67 #include <net/vnet.h>
68 
69 #include <netinet/in.h>
70 #include <netinet/in_kdtrace.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/in_var.h>
73 #include <netinet/ip.h>
74 #include <netinet/in_fib.h>
75 #include <netinet/in_pcb.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/ip_encap.h>
78 #include <netinet/ip_fw.h>
79 #include <netinet/ip_icmp.h>
80 #include <netinet/igmp_var.h>
81 #include <netinet/ip_options.h>
82 #include <machine/in_cksum.h>
83 #include <netinet/ip_carp.h>
84 #include <netinet/in_rss.h>
85 #include <netinet/ip_mroute.h>
86 #ifdef SCTP
87 #include <netinet/sctp_var.h>
88 #endif
89 
90 #include <netipsec/ipsec_support.h>
91 
92 #include <sys/socketvar.h>
93 
94 #include <security/mac/mac_framework.h>
95 
96 #ifdef CTASSERT
97 CTASSERT(sizeof(struct ip) == 20);
98 #endif
99 
100 /* IP reassembly functions are defined in ip_reass.c. */
101 extern void ipreass_init(void);
102 extern void ipreass_vnet_init(void);
103 #ifdef VIMAGE
104 extern void ipreass_destroy(void);
105 #endif
106 
107 VNET_DEFINE(int, rsvp_on);
108 
109 VNET_DEFINE(int, ipforwarding);
110 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_VNET | CTLFLAG_RW,
111     &VNET_NAME(ipforwarding), 0,
112     "Enable IP forwarding between interfaces");
113 
114 /*
115  * Respond with an ICMP host redirect when we forward a packet out of
116  * the same interface on which it was received.  See RFC 792.
117  */
118 VNET_DEFINE(int, ipsendredirects) = 1;
119 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_VNET | CTLFLAG_RW,
120     &VNET_NAME(ipsendredirects), 0,
121     "Enable sending IP redirects");
122 
123 VNET_DEFINE_STATIC(bool, ip_strong_es) = false;
124 #define	V_ip_strong_es	VNET(ip_strong_es)
125 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, rfc1122_strong_es,
126     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_strong_es), false,
127     "Packet's IP destination address must match address on arrival interface");
128 
129 VNET_DEFINE_STATIC(bool, ip_sav) = true;
130 #define	V_ip_sav	VNET(ip_sav)
131 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, source_address_validation,
132     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_sav), true,
133     "Drop incoming packets with source address that is a local address");
134 
135 /* Packet filter hooks */
136 VNET_DEFINE(pfil_head_t, inet_pfil_head);
137 VNET_DEFINE(pfil_head_t, inet_local_pfil_head);
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_v4,
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_soft_m2cpuid_v4,
165 	.nh_policy = NETISR_POLICY_CPU,
166 	.nh_dispatch = NETISR_DISPATCH_HYBRID,
167 };
168 #endif
169 
170 ipproto_input_t		*ip_protox[IPPROTO_MAX] = {
171 			    [0 ... IPPROTO_MAX - 1] = rip_input };
172 ipproto_ctlinput_t	*ip_ctlprotox[IPPROTO_MAX] = {
173 			    [0 ... IPPROTO_MAX - 1] = rip_ctlinput };
174 
175 VNET_DEFINE(struct in_ifaddrhead, in_ifaddrhead);  /* first inet address */
176 VNET_DEFINE(struct in_ifaddrhashhead *, in_ifaddrhashtbl); /* inet addr hash table  */
177 VNET_DEFINE(u_long, in_ifaddrhmask);		/* mask for hash table */
178 
179 #ifdef IPCTL_DEFMTU
180 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
181     &ip_mtu, 0, "Default MTU");
182 #endif
183 
184 #ifdef IPSTEALTH
185 VNET_DEFINE(int, ipstealth);
186 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_VNET | CTLFLAG_RW,
187     &VNET_NAME(ipstealth), 0,
188     "IP stealth mode, no TTL decrementation on forwarding");
189 #endif
190 
191 /*
192  * IP statistics are stored in the "array" of counter(9)s.
193  */
194 VNET_PCPUSTAT_DEFINE(struct ipstat, ipstat);
195 VNET_PCPUSTAT_SYSINIT(ipstat);
196 SYSCTL_VNET_PCPUSTAT(_net_inet_ip, IPCTL_STATS, stats, struct ipstat, ipstat,
197     "IP statistics (struct ipstat, netinet/ip_var.h)");
198 
199 #ifdef VIMAGE
200 VNET_PCPUSTAT_SYSUNINIT(ipstat);
201 #endif /* VIMAGE */
202 
203 /*
204  * Kernel module interface for updating ipstat.  The argument is an index
205  * into ipstat treated as an array.
206  */
207 void
kmod_ipstat_inc(int statnum)208 kmod_ipstat_inc(int statnum)
209 {
210 
211 	counter_u64_add(VNET(ipstat)[statnum], 1);
212 }
213 
214 void
kmod_ipstat_dec(int statnum)215 kmod_ipstat_dec(int statnum)
216 {
217 
218 	counter_u64_add(VNET(ipstat)[statnum], -1);
219 }
220 
221 static int
sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)222 sysctl_netinet_intr_queue_maxlen(SYSCTL_HANDLER_ARGS)
223 {
224 	int error, qlimit;
225 
226 	netisr_getqlimit(&ip_nh, &qlimit);
227 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
228 	if (error || !req->newptr)
229 		return (error);
230 	if (qlimit < 1)
231 		return (EINVAL);
232 	return (netisr_setqlimit(&ip_nh, qlimit));
233 }
234 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen,
235     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
236     sysctl_netinet_intr_queue_maxlen, "I",
237     "Maximum size of the IP input queue");
238 
239 static int
sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)240 sysctl_netinet_intr_queue_drops(SYSCTL_HANDLER_ARGS)
241 {
242 	u_int64_t qdrops_long;
243 	int error, qdrops;
244 
245 	netisr_getqdrops(&ip_nh, &qdrops_long);
246 	qdrops = qdrops_long;
247 	error = sysctl_handle_int(oidp, &qdrops, 0, req);
248 	if (error || !req->newptr)
249 		return (error);
250 	if (qdrops != 0)
251 		return (EINVAL);
252 	netisr_clearqdrops(&ip_nh);
253 	return (0);
254 }
255 
256 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops,
257     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
258     0, 0, sysctl_netinet_intr_queue_drops, "I",
259     "Number of packets dropped from the IP input queue");
260 
261 #ifdef	RSS
262 static int
sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)263 sysctl_netinet_intr_direct_queue_maxlen(SYSCTL_HANDLER_ARGS)
264 {
265 	int error, qlimit;
266 
267 	netisr_getqlimit(&ip_direct_nh, &qlimit);
268 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
269 	if (error || !req->newptr)
270 		return (error);
271 	if (qlimit < 1)
272 		return (EINVAL);
273 	return (netisr_setqlimit(&ip_direct_nh, qlimit));
274 }
275 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRDQMAXLEN, intr_direct_queue_maxlen,
276     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
277     0, 0, sysctl_netinet_intr_direct_queue_maxlen,
278     "I", "Maximum size of the IP direct input queue");
279 
280 static int
sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS)281 sysctl_netinet_intr_direct_queue_drops(SYSCTL_HANDLER_ARGS)
282 {
283 	u_int64_t qdrops_long;
284 	int error, qdrops;
285 
286 	netisr_getqdrops(&ip_direct_nh, &qdrops_long);
287 	qdrops = qdrops_long;
288 	error = sysctl_handle_int(oidp, &qdrops, 0, req);
289 	if (error || !req->newptr)
290 		return (error);
291 	if (qdrops != 0)
292 		return (EINVAL);
293 	netisr_clearqdrops(&ip_direct_nh);
294 	return (0);
295 }
296 
297 SYSCTL_PROC(_net_inet_ip, IPCTL_INTRDQDROPS, intr_direct_queue_drops,
298     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
299     sysctl_netinet_intr_direct_queue_drops, "I",
300     "Number of packets dropped from the IP direct input queue");
301 #endif	/* RSS */
302 
303 /*
304  * IP initialization: fill in IP protocol switch table.
305  * All protocols not implemented in kernel go to raw IP protocol handler.
306  */
307 static void
ip_vnet_init(void * arg __unused)308 ip_vnet_init(void *arg __unused)
309 {
310 	CK_STAILQ_INIT(&V_in_ifaddrhead);
311 
312 	struct hashalloc_args ha = {
313 		.size = INADDR_NHASH,
314 		.mtype = M_IFADDR,
315 		.mflags = M_WAITOK,
316 		.head = HASH_HEAD_CK_LIST,
317 	};
318 	V_in_ifaddrhashtbl = hashalloc(&ha);
319 	V_in_ifaddrhmask = ha.size - 1;
320 
321 	/* Initialize IP reassembly queue. */
322 	ipreass_vnet_init();
323 
324 	/* Initialize packet filter hooks. */
325 	struct pfil_head_args pa = {
326 		.pa_version = PFIL_VERSION,
327 		.pa_flags = PFIL_IN | PFIL_OUT,
328 		.pa_type = PFIL_TYPE_IP4,
329 		.pa_headname = PFIL_INET_NAME,
330 	};
331 	V_inet_pfil_head = pfil_head_register(&pa);
332 
333 	pa.pa_flags = PFIL_OUT;
334 	pa.pa_headname = PFIL_INET_LOCAL_NAME;
335 	V_inet_local_pfil_head = pfil_head_register(&pa);
336 
337 	if (hhook_head_register(HHOOK_TYPE_IPSEC_IN, AF_INET,
338 	    &V_ipsec_hhh_in[HHOOK_IPSEC_INET],
339 	    HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
340 		printf("%s: WARNING: unable to register input helper hook\n",
341 		    __func__);
342 	if (hhook_head_register(HHOOK_TYPE_IPSEC_OUT, AF_INET,
343 	    &V_ipsec_hhh_out[HHOOK_IPSEC_INET],
344 	    HHOOK_WAITOK | HHOOK_HEADISINVNET) != 0)
345 		printf("%s: WARNING: unable to register output helper hook\n",
346 		    __func__);
347 
348 #ifdef VIMAGE
349 	netisr_register_vnet(&ip_nh);
350 #ifdef	RSS
351 	netisr_register_vnet(&ip_direct_nh);
352 #endif
353 #endif
354 }
355 VNET_SYSINIT(ip_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
356     ip_vnet_init, NULL);
357 
358 static void
ip_init(const void * unused __unused)359 ip_init(const void *unused __unused)
360 {
361 	struct ifnet *ifp;
362 
363 	ipreass_init();
364 
365 	/*
366 	 * Register statically compiled protocols, that are unlikely to
367 	 * ever become dynamic.
368 	 */
369 	IPPROTO_REGISTER(IPPROTO_ICMP, icmp_input, NULL);
370 	IPPROTO_REGISTER(IPPROTO_IGMP, igmp_input, NULL);
371 	IPPROTO_REGISTER(IPPROTO_RSVP, rsvp_input, NULL);
372 	IPPROTO_REGISTER(IPPROTO_IPV4, encap4_input, NULL);
373 	IPPROTO_REGISTER(IPPROTO_MOBILE, encap4_input, NULL);
374 	IPPROTO_REGISTER(IPPROTO_ETHERIP, encap4_input, NULL);
375 	IPPROTO_REGISTER(IPPROTO_GRE, encap4_input, NULL);
376 	IPPROTO_REGISTER(IPPROTO_IPV6, encap4_input, NULL);
377 	IPPROTO_REGISTER(IPPROTO_PIM, encap4_input, NULL);
378 #ifdef SCTP	/* XXX: has a loadable & static version */
379 	IPPROTO_REGISTER(IPPROTO_SCTP, sctp_input, sctp_ctlinput);
380 #endif
381 
382 	netisr_register(&ip_nh);
383 #ifdef	RSS
384 	netisr_register(&ip_direct_nh);
385 #endif
386 	/*
387 	 * XXXGL: we use SYSINIT() here, but go over V_ifnet.  It was the same
388 	 * way before dom_ifattach removal.  This worked because when any
389 	 * non-default vnet is created, there are no interfaces inside.
390 	 * Eventually this needs to be fixed.
391 	 */
392 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link)
393 		in_ifattach(NULL, ifp);
394 }
395 SYSINIT(ip_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip_init, NULL);
396 
397 #ifdef VIMAGE
398 static void
ip_destroy(void * unused __unused)399 ip_destroy(void *unused __unused)
400 {
401 	int error;
402 
403 #ifdef	RSS
404 	netisr_unregister_vnet(&ip_direct_nh);
405 #endif
406 	netisr_unregister_vnet(&ip_nh);
407 
408 	pfil_head_unregister(V_inet_pfil_head);
409 	error = hhook_head_deregister(V_ipsec_hhh_in[HHOOK_IPSEC_INET]);
410 	if (error != 0) {
411 		printf("%s: WARNING: unable to deregister input helper hook "
412 		    "type HHOOK_TYPE_IPSEC_IN, id HHOOK_IPSEC_INET: "
413 		    "error %d returned\n", __func__, error);
414 	}
415 	error = hhook_head_deregister(V_ipsec_hhh_out[HHOOK_IPSEC_INET]);
416 	if (error != 0) {
417 		printf("%s: WARNING: unable to deregister output helper hook "
418 		    "type HHOOK_TYPE_IPSEC_OUT, id HHOOK_IPSEC_INET: "
419 		    "error %d returned\n", __func__, error);
420 	}
421 
422 	/* Remove the IPv4 addresses from all interfaces. */
423 	in_ifscrub_all();
424 
425 	/* Make sure the IPv4 routes are gone as well. */
426 	rib_flush_routes_family(AF_INET);
427 
428 	/* Destroy IP reassembly queue. */
429 	ipreass_destroy();
430 
431 	/* Cleanup in_ifaddr hash table; should be empty. */
432 	struct hashalloc_args ha = {
433 		.mtype = M_IFADDR,
434 		.head = HASH_HEAD_CK_LIST,
435 		.size = V_in_ifaddrhmask + 1,
436 	};
437 	hashfree(V_in_ifaddrhashtbl, &ha);
438 }
439 
440 VNET_SYSUNINIT(ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, ip_destroy, NULL);
441 #endif
442 
443 #ifdef	RSS
444 /*
445  * IP direct input routine.
446  *
447  * This is called when reinjecting completed fragments where
448  * all of the previous checking and book-keeping has been done.
449  */
450 void
ip_direct_input(struct mbuf * m)451 ip_direct_input(struct mbuf *m)
452 {
453 	struct ip *ip;
454 	int hlen;
455 
456 	ip = mtod(m, struct ip *);
457 	hlen = ip->ip_hl << 2;
458 
459 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
460 	if (IPSEC_ENABLED(ipv4)) {
461 		if (IPSEC_INPUT(ipv4, m, hlen, ip->ip_p) != 0)
462 			return;
463 	}
464 #endif /* IPSEC */
465 	IPSTAT_INC(ips_delivered);
466 	ip_protox[ip->ip_p](&m, &hlen, ip->ip_p);
467 }
468 #endif
469 
470 /*
471  * Ip input routine.  Checksum and byte swap header.  If fragmented
472  * try to reassemble.  Process options.  Pass to next level.
473  */
474 void
ip_input(struct mbuf * m)475 ip_input(struct mbuf *m)
476 {
477 	struct ip *ip = NULL;
478 	struct in_ifaddr *ia = NULL;
479 	struct ifaddr *ifa;
480 	struct ifnet *ifp;
481 	int hlen = 0;
482 	uint16_t sum, ip_len;
483 	int dchg = 0;				/* dest changed after fw */
484 	struct in_addr odst;			/* original dst address */
485 	bool strong_es;
486 
487 	M_ASSERTPKTHDR(m);
488 	NET_EPOCH_ASSERT();
489 
490 	if (m->m_flags & M_FASTFWD_OURS) {
491 		m->m_flags &= ~M_FASTFWD_OURS;
492 		/* Set up some basics that will be used later. */
493 		ip = mtod(m, struct ip *);
494 		hlen = ip->ip_hl << 2;
495 		ip_len = ntohs(ip->ip_len);
496 		goto ours;
497 	}
498 
499 	IPSTAT_INC(ips_total);
500 
501 	if (__predict_false(m->m_pkthdr.len < sizeof(struct ip)))
502 		goto tooshort;
503 
504 	if (m->m_len < sizeof(struct ip)) {
505 		m = m_pullup(m, sizeof(struct ip));
506 		if (__predict_false(m == NULL)) {
507 			IPSTAT_INC(ips_toosmall);
508 			return;
509 		}
510 	}
511 	ip = mtod(m, struct ip *);
512 
513 	if (__predict_false(ip->ip_v != IPVERSION)) {
514 		IPSTAT_INC(ips_badvers);
515 		goto bad;
516 	}
517 
518 	hlen = ip->ip_hl << 2;
519 	if (__predict_false(hlen < sizeof(struct ip))) {	/* minimum header length */
520 		IPSTAT_INC(ips_badhlen);
521 		goto bad;
522 	}
523 	if (hlen > m->m_len) {
524 		m = m_pullup(m, hlen);
525 		if (__predict_false(m == NULL)) {
526 			IPSTAT_INC(ips_badhlen);
527 			return;
528 		}
529 		ip = mtod(m, struct ip *);
530 	}
531 
532 	IP_PROBE(receive, NULL, NULL, ip, m->m_pkthdr.rcvif, ip, NULL);
533 
534 	/* IN_LOOPBACK must not appear on the wire - RFC1122 */
535 	ifp = m->m_pkthdr.rcvif;
536 	if (IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) ||
537 	    IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) {
538 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
539 			IPSTAT_INC(ips_badaddr);
540 			goto bad;
541 		}
542 	}
543 
544 	if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
545 		sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
546 	} else if (m->m_pkthdr.csum_flags & CSUM_IP) {
547 		/*
548 		 * Packet from local host that offloaded checksum computation.
549 		 * Checksum not required since the packet wasn't on the wire.
550 		 */
551 		sum = 0;
552 	} else {
553 		if (hlen == sizeof(struct ip)) {
554 			sum = in_cksum_hdr(ip);
555 		} else {
556 			sum = in_cksum(m, hlen);
557 		}
558 	}
559 	if (__predict_false(sum)) {
560 		IPSTAT_INC(ips_badsum);
561 		goto bad;
562 	}
563 
564 	ip_len = ntohs(ip->ip_len);
565 	if (__predict_false(ip_len < hlen)) {
566 		IPSTAT_INC(ips_badlen);
567 		goto bad;
568 	}
569 
570 	/*
571 	 * Check that the amount of data in the buffers
572 	 * is as at least much as the IP header would have us expect.
573 	 * Trim mbufs if longer than we expect.
574 	 * Drop packet if shorter than we expect.
575 	 */
576 	if (__predict_false(m->m_pkthdr.len < ip_len)) {
577 tooshort:
578 		IPSTAT_INC(ips_tooshort);
579 		goto bad;
580 	}
581 	if (m->m_pkthdr.len > ip_len) {
582 		if (m->m_len == m->m_pkthdr.len) {
583 			m->m_len = ip_len;
584 			m->m_pkthdr.len = ip_len;
585 		} else
586 			m_adj(m, ip_len - m->m_pkthdr.len);
587 	}
588 
589 	/*
590 	 * Try to forward the packet, but if we fail continue.
591 	 * ip_tryforward() may generate redirects these days.
592 	 * XXX the logic below falling through to normal processing
593 	 * if redirects are required should be revisited as well.
594 	 * ip_tryforward() does inbound and outbound packet firewall
595 	 * processing. If firewall has decided that destination becomes
596 	 * our local address, it sets M_FASTFWD_OURS flag. In this
597 	 * case skip another inbound firewall processing and update
598 	 * ip pointer.
599 	 */
600 	if (V_ipforwarding != 0
601 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
602 	    && (!IPSEC_ENABLED(ipv4) ||
603 	    IPSEC_CAPS(ipv4, m, IPSEC_CAP_OPERABLE) == 0)
604 #endif
605 	    ) {
606 		/*
607 		 * ip_dooptions() was run so we can ignore the source route (or
608 		 * any IP options case) case for redirects in ip_tryforward().
609 		 */
610 		if ((m = ip_tryforward(m)) == NULL)
611 			return;
612 		if (m->m_flags & M_FASTFWD_OURS) {
613 			m->m_flags &= ~M_FASTFWD_OURS;
614 			ip = mtod(m, struct ip *);
615 			goto ours;
616 		}
617 	}
618 
619 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
620 	/*
621 	 * Bypass packet filtering for packets previously handled by IPsec.
622 	 */
623 	if (IPSEC_ENABLED(ipv4) &&
624 	    IPSEC_CAPS(ipv4, m, IPSEC_CAP_BYPASS_FILTER) != 0)
625 			goto passin;
626 #endif
627 
628 	/*
629 	 * Run through list of hooks for input packets.
630 	 *
631 	 * NB: Beware of the destination address changing (e.g.
632 	 *     by NAT rewriting).  When this happens, tell
633 	 *     ip_forward to do the right thing.
634 	 */
635 
636 	/* Jump over all PFIL processing if hooks are not active. */
637 	if (!PFIL_HOOKED_IN(V_inet_pfil_head))
638 		goto passin;
639 
640 	odst = ip->ip_dst;
641 	if (pfil_mbuf_in(V_inet_pfil_head, &m, ifp, NULL) !=
642 	    PFIL_PASS)
643 		return;
644 
645 	ip = mtod(m, struct ip *);
646 	dchg = (odst.s_addr != ip->ip_dst.s_addr);
647 
648 	if (m->m_flags & M_FASTFWD_OURS) {
649 		m->m_flags &= ~M_FASTFWD_OURS;
650 		goto ours;
651 	}
652 	if (m->m_flags & M_IP_NEXTHOP) {
653 		if (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL) {
654 			/*
655 			 * Directly ship the packet on.  This allows
656 			 * forwarding packets originally destined to us
657 			 * to some other directly connected host.
658 			 */
659 			ip_forward(m, 1);
660 			return;
661 		}
662 	}
663 passin:
664 	/*
665 	 * The unspecified address can appear only as a src address - RFC1122.
666 	 *
667 	 * The check is deferred to here to give firewalls a chance to block
668 	 * (and log) such packets.  ip_tryforward() will not process such
669 	 * packets.
670 	 */
671 	if (__predict_false(ntohl(ip->ip_dst.s_addr) == INADDR_ANY)) {
672 		IPSTAT_INC(ips_badaddr);
673 		goto bad;
674 	}
675 
676 	/*
677 	 * Process options and, if not destined for us,
678 	 * ship it on.  ip_dooptions returns 1 when an
679 	 * error was detected (causing an icmp message
680 	 * to be sent and the original packet to be freed).
681 	 */
682 	if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
683 		return;
684 
685         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
686          * matter if it is destined to another node, or whether it is
687          * a multicast one, RSVP wants it! and prevents it from being forwarded
688          * anywhere else. Also checks if the rsvp daemon is running before
689 	 * grabbing the packet.
690          */
691 	if (ip->ip_p == IPPROTO_RSVP && V_rsvp_on)
692 		goto ours;
693 
694 	/*
695 	 * Check our list of addresses, to see if the packet is for us.
696 	 * If we don't have any addresses, assume any unicast packet
697 	 * we receive might be for us (and let the upper layers deal
698 	 * with it).
699 	 */
700 	if (CK_STAILQ_EMPTY(&V_in_ifaddrhead) &&
701 	    (m->m_flags & (M_MCAST|M_BCAST)) == 0)
702 		goto ours;
703 
704 	/*
705 	 * Enable a consistency check between the destination address
706 	 * and the arrival interface for a unicast packet (the RFC 1122
707 	 * strong ES model) with a list of additional predicates:
708 	 * - if IP forwarding is disabled
709 	 * - the packet is not locally generated
710 	 * - the packet is not subject to 'ipfw fwd'
711 	 * - Interface is not running CARP. If the packet got here, we already
712 	 *   checked it with carp_iamatch() and carp_forus().
713 	 */
714 	strong_es = V_ip_strong_es && (V_ipforwarding == 0) &&
715 	    ((ifp->if_flags & IFF_LOOPBACK) == 0) &&
716 	    ifp->if_carp == NULL && (dchg == 0);
717 
718 	/*
719 	 * Check for exact addresses in the hash bucket.
720 	 */
721 	CK_LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
722 		if (IA_SIN(ia)->sin_addr.s_addr != ip->ip_dst.s_addr)
723 			continue;
724 
725 		/*
726 		 * net.inet.ip.rfc1122_strong_es: the address matches, verify
727 		 * that the packet arrived via the correct interface.
728 		 */
729 		if (__predict_false(strong_es && ia->ia_ifp != ifp)) {
730 			IPSTAT_INC(ips_badaddr);
731 			goto bad;
732 		}
733 
734 		/*
735 		 * net.inet.ip.source_address_validation: drop incoming
736 		 * packets that pretend to be ours.
737 		 */
738 		if (V_ip_sav && !(ifp->if_flags & IFF_LOOPBACK) &&
739 		    __predict_false(in_localip_fib(ip->ip_src, ifp->if_fib))) {
740 			IPSTAT_INC(ips_badaddr);
741 			goto bad;
742 		}
743 
744 		counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
745 		counter_u64_add(ia->ia_ifa.ifa_ibytes, m->m_pkthdr.len);
746 		goto ours;
747 	}
748 
749 	/*
750 	 * Check for broadcast addresses.
751 	 *
752 	 * Only accept broadcast packets that arrive via the matching
753 	 * interface.  Reception of forwarded directed broadcasts would
754 	 * be handled via ip_forward() and ether_output() with the loopback
755 	 * into the stack for SIMPLEX interfaces handled by ether_output().
756 	 */
757 	if (ifp->if_flags & IFF_BROADCAST) {
758 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
759 			if (ifa->ifa_addr->sa_family != AF_INET)
760 				continue;
761 			ia = ifatoia(ifa);
762 			if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
763 			    ip->ip_dst.s_addr) {
764 				counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
765 				counter_u64_add(ia->ia_ifa.ifa_ibytes,
766 				    m->m_pkthdr.len);
767 				goto ours;
768 			}
769 #ifdef BOOTP_COMPAT
770 			if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) {
771 				counter_u64_add(ia->ia_ifa.ifa_ipackets, 1);
772 				counter_u64_add(ia->ia_ifa.ifa_ibytes,
773 				    m->m_pkthdr.len);
774 				goto ours;
775 			}
776 #endif
777 		}
778 		ia = NULL;
779 	}
780 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
781 		/*
782 		 * RFC 3927 2.7: Do not forward multicast packets from
783 		 * IN_LINKLOCAL.
784 		 */
785 		if (V_ip_mrouting_enabled &&
786 		    !IN_LINKLOCAL(ntohl(ip->ip_src.s_addr))) {
787 			/*
788 			 * If we are acting as a multicast router, all
789 			 * incoming multicast packets are passed to the
790 			 * kernel-level multicast forwarding function.
791 			 * The packet is returned (relatively) intact; if
792 			 * ip_mforward() returns a non-zero value, the packet
793 			 * must be discarded, else it may be accepted below.
794 			 */
795 			if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) {
796 				IPSTAT_INC(ips_cantforward);
797 				m_freem(m);
798 				return;
799 			}
800 
801 			/*
802 			 * The process-level routing daemon needs to receive
803 			 * all multicast IGMP packets, whether or not this
804 			 * host belongs to their destination groups.
805 			 */
806 			if (ip->ip_p == IPPROTO_IGMP) {
807 				goto ours;
808 			}
809 			IPSTAT_INC(ips_forward);
810 		}
811 		/*
812 		 * Assume the packet is for us, to avoid prematurely taking
813 		 * a lock on the in_multi hash. Protocols must perform
814 		 * their own filtering and update statistics accordingly.
815 		 */
816 		goto ours;
817 	}
818 	if (in_broadcast(ip->ip_dst))
819 		goto ours;
820 	/* RFC 3927 2.7: Do not forward packets to or from IN_LINKLOCAL. */
821 	if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
822 	    IN_LINKLOCAL(ntohl(ip->ip_src.s_addr))) {
823 		IPSTAT_INC(ips_cantforward);
824 		m_freem(m);
825 		return;
826 	}
827 
828 	/*
829 	 * Not for us; forward if possible and desirable.
830 	 */
831 	if (V_ipforwarding == 0) {
832 		IPSTAT_INC(ips_cantforward);
833 		m_freem(m);
834 	} else {
835 		ip_forward(m, dchg);
836 	}
837 	return;
838 
839 ours:
840 #ifdef IPSTEALTH
841 	/*
842 	 * IPSTEALTH: Process non-routing options only
843 	 * if the packet is destined for us.
844 	 */
845 	if (V_ipstealth && hlen > sizeof (struct ip) && ip_dooptions(m, 1))
846 		return;
847 #endif /* IPSTEALTH */
848 
849 	/*
850 	 * We are going to ship the packet to the local protocol stack. Call the
851 	 * filter again for this 'output' action, allowing redirect-like rules
852 	 * to adjust the source address.
853 	 */
854 	if (PFIL_HOOKED_OUT(V_inet_local_pfil_head)) {
855 		if (pfil_mbuf_out(V_inet_local_pfil_head, &m, V_loif, NULL) !=
856 		    PFIL_PASS)
857 			return;
858 		ip = mtod(m, struct ip *);
859 	}
860 
861 	/*
862 	 * Attempt reassembly; if it succeeds, proceed.
863 	 * ip_reass() will return a different mbuf.
864 	 */
865 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) {
866 		/* XXXGL: shouldn't we save & set m_flags? */
867 		m = ip_reass(m);
868 		if (m == NULL)
869 			return;
870 		ip = mtod(m, struct ip *);
871 		/* Get the header length of the reassembled packet */
872 		hlen = ip->ip_hl << 2;
873 	}
874 
875 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
876 	if (IPSEC_ENABLED(ipv4)) {
877 		if (IPSEC_INPUT(ipv4, m, hlen, ip->ip_p) != 0)
878 			return;
879 	}
880 #endif /* IPSEC */
881 
882 	/*
883 	 * Switch out to protocol's input routine.
884 	 */
885 	IPSTAT_INC(ips_delivered);
886 
887 	ip_protox[ip->ip_p](&m, &hlen, ip->ip_p);
888 	return;
889 bad:
890 	m_freem(m);
891 }
892 
893 int
ipproto_register(uint8_t proto,ipproto_input_t input,ipproto_ctlinput_t ctl)894 ipproto_register(uint8_t proto, ipproto_input_t input, ipproto_ctlinput_t ctl)
895 {
896 
897 	MPASS(proto > 0);
898 
899 	/*
900 	 * The protocol slot must not be occupied by another protocol
901 	 * already.  An index pointing to rip_input() is unused.
902 	 */
903 	if (ip_protox[proto] == rip_input) {
904 		ip_protox[proto] = input;
905 		ip_ctlprotox[proto] = ctl;
906 		return (0);
907 	} else
908 		return (EEXIST);
909 }
910 
911 int
ipproto_unregister(uint8_t proto)912 ipproto_unregister(uint8_t proto)
913 {
914 
915 	MPASS(proto > 0);
916 
917 	if (ip_protox[proto] != rip_input) {
918 		ip_protox[proto] = rip_input;
919 		ip_ctlprotox[proto] = rip_ctlinput;
920 		return (0);
921 	} else
922 		return (ENOENT);
923 }
924 
925 /*
926  * Forward a packet.  If some error occurs return the sender
927  * an icmp packet.  Note we can't always generate a meaningful
928  * icmp message because icmp doesn't have a large enough repertoire
929  * of codes and types.
930  *
931  * If not forwarding, just drop the packet.  This could be confusing
932  * if ipforwarding was zero but some routing protocol was advancing
933  * us as a gateway to somewhere.  However, we must let the routing
934  * protocol deal with that.
935  *
936  * The srcrt parameter indicates whether the packet is being forwarded
937  * via a source route.
938  */
939 void
ip_forward(struct mbuf * m,int srcrt)940 ip_forward(struct mbuf *m, int srcrt)
941 {
942 	struct ip *ip = mtod(m, struct ip *);
943 	struct in_ifaddr *ia;
944 	struct mbuf *mcopy;
945 	struct sockaddr_in *sin;
946 	struct in_addr dest;
947 	struct route ro;
948 	uint32_t flowid;
949 	int error, type = 0, code = 0, mtu = 0;
950 
951 	NET_EPOCH_ASSERT();
952 
953 	if (m->m_flags & (M_BCAST|M_MCAST) || !in_canforward(ip->ip_dst)) {
954 		IPSTAT_INC(ips_cantforward);
955 		m_freem(m);
956 		return;
957 	}
958 	if (
959 #ifdef IPSTEALTH
960 	    V_ipstealth == 0 &&
961 #endif
962 	    ip->ip_ttl <= IPTTLDEC) {
963 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 0, 0);
964 		return;
965 	}
966 
967 	bzero(&ro, sizeof(ro));
968 	sin = (struct sockaddr_in *)&ro.ro_dst;
969 	sin->sin_family = AF_INET;
970 	sin->sin_len = sizeof(*sin);
971 	sin->sin_addr = ip->ip_dst;
972 	flowid = m->m_pkthdr.flowid;
973 	ro.ro_nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_REF, flowid);
974 	if (ro.ro_nh != NULL) {
975 		if (ro.ro_nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) {
976 			IPSTAT_INC(ips_cantforward);
977 			m_freem(m);
978 			NH_FREE(ro.ro_nh);
979 			return;
980 		}
981 		if (ro.ro_nh->nh_flags & NHF_REJECT) {
982 			IPSTAT_INC(ips_cantforward);
983 			NH_FREE(ro.ro_nh);
984 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
985 			return;
986 		}
987 		ia = ifatoia(ro.ro_nh->nh_ifa);
988 	} else
989 		ia = NULL;
990 	/*
991 	 * Save the IP header and at most 8 bytes of the payload,
992 	 * in case we need to generate an ICMP message to the src.
993 	 *
994 	 * XXX this can be optimized a lot by saving the data in a local
995 	 * buffer on the stack (72 bytes at most), and only allocating the
996 	 * mbuf if really necessary. The vast majority of the packets
997 	 * are forwarded without having to send an ICMP back (either
998 	 * because unnecessary, or because rate limited), so we are
999 	 * really we are wasting a lot of work here.
1000 	 *
1001 	 * We don't use m_copym() because it might return a reference
1002 	 * to a shared cluster. Both this function and ip_output()
1003 	 * assume exclusive access to the IP header in `m', so any
1004 	 * data in a cluster may change before we reach icmp_error().
1005 	 */
1006 	mcopy = m_gethdr(M_NOWAIT, m->m_type);
1007 	if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_NOWAIT)) {
1008 		/*
1009 		 * It's probably ok if the pkthdr dup fails (because
1010 		 * the deep copy of the tag chain failed), but for now
1011 		 * be conservative and just discard the copy since
1012 		 * code below may some day want the tags.
1013 		 */
1014 		m_free(mcopy);
1015 		mcopy = NULL;
1016 	}
1017 	if (mcopy != NULL) {
1018 		mcopy->m_len = min(ntohs(ip->ip_len), M_TRAILINGSPACE(mcopy));
1019 		mcopy->m_pkthdr.len = mcopy->m_len;
1020 		m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1021 	}
1022 #ifdef IPSTEALTH
1023 	if (V_ipstealth == 0)
1024 #endif
1025 		ip->ip_ttl -= IPTTLDEC;
1026 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1027 	if (IPSEC_ENABLED(ipv4)) {
1028 		if ((error = IPSEC_FORWARD(ipv4, m)) != 0) {
1029 			/* mbuf consumed by IPsec */
1030 			RO_NHFREE(&ro);
1031 			m_freem(mcopy);
1032 			if (error != EINPROGRESS)
1033 				IPSTAT_INC(ips_cantforward);
1034 			return;
1035 		}
1036 		/* No IPsec processing required */
1037 	}
1038 #endif /* IPSEC */
1039 	/*
1040 	 * If forwarding packet using same interface that it came in on,
1041 	 * perhaps should send a redirect to sender to shortcut a hop.
1042 	 * Only send redirect if source is sending directly to us,
1043 	 * and if packet was not source routed (or has any options).
1044 	 * Also, don't send redirect if forwarding using a default route
1045 	 * or a route modified by a redirect.
1046 	 */
1047 	dest.s_addr = 0;
1048 	if (!srcrt && V_ipsendredirects &&
1049 	    ia != NULL && ia->ia_ifp == m->m_pkthdr.rcvif) {
1050 		struct nhop_object *nh;
1051 
1052 		nh = ro.ro_nh;
1053 
1054 		if (nh != NULL && ((nh->nh_flags & (NHF_REDIRECT|NHF_DEFAULT)) == 0)) {
1055 			struct in_ifaddr *nh_ia = (struct in_ifaddr *)(nh->nh_ifa);
1056 			u_long src = ntohl(ip->ip_src.s_addr);
1057 
1058 			if (nh_ia != NULL &&
1059 			    (src & nh_ia->ia_subnetmask) == nh_ia->ia_subnet) {
1060 				/* Router requirements says to only send host redirects */
1061 				type = ICMP_REDIRECT;
1062 				code = ICMP_REDIRECT_HOST;
1063 				if (nh->nh_flags & NHF_GATEWAY) {
1064 				    if (nh->gw_sa.sa_family == AF_INET)
1065 					dest.s_addr = nh->gw4_sa.sin_addr.s_addr;
1066 				    else /* Do not redirect in case gw is AF_INET6 */
1067 					type = 0;
1068 				} else
1069 					dest.s_addr = ip->ip_dst.s_addr;
1070 			}
1071 		}
1072 	}
1073 
1074 	error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL, NULL);
1075 
1076 	if (error == EMSGSIZE && ro.ro_nh)
1077 		mtu = ro.ro_nh->nh_mtu;
1078 	RO_NHFREE(&ro);
1079 
1080 	if (error)
1081 		IPSTAT_INC(ips_cantforward);
1082 	else {
1083 		IPSTAT_INC(ips_forward);
1084 		if (type)
1085 			IPSTAT_INC(ips_redirectsent);
1086 		else {
1087 			if (mcopy)
1088 				m_freem(mcopy);
1089 			return;
1090 		}
1091 	}
1092 	if (mcopy == NULL)
1093 		return;
1094 
1095 	switch (error) {
1096 	case 0:				/* forwarded, but need redirect */
1097 		/* type, code set above */
1098 		break;
1099 
1100 	case ENETUNREACH:
1101 	case EHOSTUNREACH:
1102 	case ENETDOWN:
1103 	case EHOSTDOWN:
1104 	default:
1105 		type = ICMP_UNREACH;
1106 		code = ICMP_UNREACH_HOST;
1107 		break;
1108 
1109 	case EMSGSIZE:
1110 		type = ICMP_UNREACH;
1111 		code = ICMP_UNREACH_NEEDFRAG;
1112 		/*
1113 		 * If the MTU was set before make sure we are below the
1114 		 * interface MTU.
1115 		 * If the MTU wasn't set before use the interface mtu or
1116 		 * fall back to the next smaller mtu step compared to the
1117 		 * current packet size.
1118 		 */
1119 		if (mtu != 0) {
1120 			if (ia != NULL)
1121 				mtu = min(mtu, ia->ia_ifp->if_mtu);
1122 		} else {
1123 			if (ia != NULL)
1124 				mtu = ia->ia_ifp->if_mtu;
1125 			else
1126 				mtu = ip_next_mtu(ntohs(ip->ip_len), 0);
1127 		}
1128 		IPSTAT_INC(ips_cantfrag);
1129 		break;
1130 
1131 	case ENOBUFS:
1132 	case EACCES:			/* ipfw denied packet */
1133 		m_freem(mcopy);
1134 		return;
1135 	}
1136 	icmp_error(mcopy, type, code, dest.s_addr, mtu);
1137 }
1138 
1139 #define	CHECK_SO_CT(sp, ct) \
1140     (((sp->so_options & SO_TIMESTAMP) && (sp->so_ts_clock == ct)) ? 1 : 0)
1141 
1142 void
ip_savecontrol(struct inpcb * inp,struct mbuf ** mp,struct ip * ip,struct mbuf * m)1143 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1144     struct mbuf *m)
1145 {
1146 	bool stamped;
1147 
1148 	stamped = false;
1149 	if ((inp->inp_socket->so_options & SO_BINTIME) ||
1150 	    CHECK_SO_CT(inp->inp_socket, SO_TS_BINTIME)) {
1151 		struct bintime boottimebin, bt;
1152 		struct timespec ts1;
1153 
1154 		if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1155 		    M_TSTMP)) {
1156 			mbuf_tstmp2timespec(m, &ts1);
1157 			timespec2bintime(&ts1, &bt);
1158 			getboottimebin(&boottimebin);
1159 			bintime_add(&bt, &boottimebin);
1160 		} else {
1161 			bintime(&bt);
1162 		}
1163 		*mp = sbcreatecontrol(&bt, sizeof(bt), SCM_BINTIME,
1164 		    SOL_SOCKET, M_NOWAIT);
1165 		if (*mp != NULL) {
1166 			mp = &(*mp)->m_next;
1167 			stamped = true;
1168 		}
1169 	}
1170 	if (CHECK_SO_CT(inp->inp_socket, SO_TS_REALTIME_MICRO)) {
1171 		struct bintime boottimebin, bt1;
1172 		struct timespec ts1;
1173 		struct timeval tv;
1174 
1175 		if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1176 		    M_TSTMP)) {
1177 			mbuf_tstmp2timespec(m, &ts1);
1178 			timespec2bintime(&ts1, &bt1);
1179 			getboottimebin(&boottimebin);
1180 			bintime_add(&bt1, &boottimebin);
1181 			bintime2timeval(&bt1, &tv);
1182 		} else {
1183 			microtime(&tv);
1184 		}
1185 		*mp = sbcreatecontrol((caddr_t)&tv, sizeof(tv), SCM_TIMESTAMP,
1186 		    SOL_SOCKET, M_NOWAIT);
1187 		if (*mp != NULL) {
1188 			mp = &(*mp)->m_next;
1189 			stamped = true;
1190 		}
1191 	} else if (CHECK_SO_CT(inp->inp_socket, SO_TS_REALTIME)) {
1192 		struct bintime boottimebin;
1193 		struct timespec ts, ts1;
1194 
1195 		if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1196 		    M_TSTMP)) {
1197 			mbuf_tstmp2timespec(m, &ts);
1198 			getboottimebin(&boottimebin);
1199 			bintime2timespec(&boottimebin, &ts1);
1200 			timespecadd(&ts, &ts1, &ts);
1201 		} else {
1202 			nanotime(&ts);
1203 		}
1204 		*mp = sbcreatecontrol(&ts, sizeof(ts), SCM_REALTIME,
1205 		    SOL_SOCKET, M_NOWAIT);
1206 		if (*mp != NULL) {
1207 			mp = &(*mp)->m_next;
1208 			stamped = true;
1209 		}
1210 	} else if (CHECK_SO_CT(inp->inp_socket, SO_TS_MONOTONIC)) {
1211 		struct timespec ts;
1212 
1213 		if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1214 		    M_TSTMP))
1215 			mbuf_tstmp2timespec(m, &ts);
1216 		else
1217 			nanouptime(&ts);
1218 		*mp = sbcreatecontrol(&ts, sizeof(ts), SCM_MONOTONIC,
1219 		    SOL_SOCKET, M_NOWAIT);
1220 		if (*mp != NULL) {
1221 			mp = &(*mp)->m_next;
1222 			stamped = true;
1223 		}
1224 	}
1225 	if (stamped && (m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR |
1226 	    M_TSTMP)) {
1227 		struct sock_timestamp_info sti;
1228 
1229 		bzero(&sti, sizeof(sti));
1230 		sti.st_info_flags = ST_INFO_HW;
1231 		if ((m->m_flags & M_TSTMP_HPREC) != 0)
1232 			sti.st_info_flags |= ST_INFO_HW_HPREC;
1233 		*mp = sbcreatecontrol(&sti, sizeof(sti), SCM_TIME_INFO,
1234 		    SOL_SOCKET, M_NOWAIT);
1235 		if (*mp != NULL)
1236 			mp = &(*mp)->m_next;
1237 	}
1238 	if (inp->inp_flags & INP_RECVDSTADDR) {
1239 		*mp = sbcreatecontrol(&ip->ip_dst, sizeof(struct in_addr),
1240 		    IP_RECVDSTADDR, IPPROTO_IP, M_NOWAIT);
1241 		if (*mp)
1242 			mp = &(*mp)->m_next;
1243 	}
1244 	if (inp->inp_flags & INP_RECVTTL) {
1245 		*mp = sbcreatecontrol(&ip->ip_ttl, sizeof(u_char), IP_RECVTTL,
1246 		    IPPROTO_IP, M_NOWAIT);
1247 		if (*mp)
1248 			mp = &(*mp)->m_next;
1249 	}
1250 #ifdef notyet
1251 	/* XXX
1252 	 * Moving these out of udp_input() made them even more broken
1253 	 * than they already were.
1254 	 */
1255 	/* options were tossed already */
1256 	if (inp->inp_flags & INP_RECVOPTS) {
1257 		*mp = sbcreatecontrol(opts_deleted_above,
1258 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP, M_NOWAIT);
1259 		if (*mp)
1260 			mp = &(*mp)->m_next;
1261 	}
1262 	/* ip_srcroute doesn't do what we want here, need to fix */
1263 	if (inp->inp_flags & INP_RECVRETOPTS) {
1264 		*mp = sbcreatecontrol(ip_srcroute(m), sizeof(struct in_addr),
1265 		    IP_RECVRETOPTS, IPPROTO_IP, M_NOWAIT);
1266 		if (*mp)
1267 			mp = &(*mp)->m_next;
1268 	}
1269 #endif
1270 	if (inp->inp_flags & INP_RECVIF) {
1271 		struct ifnet *ifp;
1272 		struct sdlbuf {
1273 			struct sockaddr_dl sdl;
1274 			u_char	pad[32];
1275 		} sdlbuf;
1276 		struct sockaddr_dl *sdp;
1277 		struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1278 
1279 		if ((ifp = m->m_pkthdr.rcvif)) {
1280 			sdp = (struct sockaddr_dl *)ifp->if_addr->ifa_addr;
1281 			/*
1282 			 * Change our mind and don't try copy.
1283 			 */
1284 			if (sdp->sdl_family != AF_LINK ||
1285 			    sdp->sdl_len > sizeof(sdlbuf)) {
1286 				goto makedummy;
1287 			}
1288 			bcopy(sdp, sdl2, sdp->sdl_len);
1289 		} else {
1290 makedummy:
1291 			sdl2->sdl_len =
1292 			    offsetof(struct sockaddr_dl, sdl_data[0]);
1293 			sdl2->sdl_family = AF_LINK;
1294 			sdl2->sdl_index = 0;
1295 			sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1296 		}
1297 		*mp = sbcreatecontrol(sdl2, sdl2->sdl_len, IP_RECVIF,
1298 		    IPPROTO_IP, M_NOWAIT);
1299 		if (*mp)
1300 			mp = &(*mp)->m_next;
1301 	}
1302 	if (inp->inp_flags & INP_RECVTOS) {
1303 		*mp = sbcreatecontrol(&ip->ip_tos, sizeof(u_char), IP_RECVTOS,
1304 		    IPPROTO_IP, M_NOWAIT);
1305 		if (*mp)
1306 			mp = &(*mp)->m_next;
1307 	}
1308 
1309 	if (inp->inp_flags2 & INP_RECVFLOWID) {
1310 		uint32_t flowid, flow_type;
1311 
1312 		flowid = m->m_pkthdr.flowid;
1313 		flow_type = M_HASHTYPE_GET(m);
1314 
1315 		/*
1316 		 * XXX should handle the failure of one or the
1317 		 * other - don't populate both?
1318 		 */
1319 		*mp = sbcreatecontrol(&flowid, sizeof(uint32_t), IP_FLOWID,
1320 		    IPPROTO_IP, M_NOWAIT);
1321 		if (*mp)
1322 			mp = &(*mp)->m_next;
1323 		*mp = sbcreatecontrol(&flow_type, sizeof(uint32_t),
1324 		    IP_FLOWTYPE, IPPROTO_IP, M_NOWAIT);
1325 		if (*mp)
1326 			mp = &(*mp)->m_next;
1327 	}
1328 
1329 #ifdef	RSS
1330 	if (inp->inp_flags2 & INP_RECVRSSBUCKETID) {
1331 		uint32_t flowid, flow_type;
1332 		uint32_t rss_bucketid;
1333 
1334 		flowid = m->m_pkthdr.flowid;
1335 		flow_type = M_HASHTYPE_GET(m);
1336 
1337 		if (rss_hash2bucket(flowid, flow_type, &rss_bucketid) == 0) {
1338 			*mp = sbcreatecontrol(&rss_bucketid, sizeof(uint32_t),
1339 			    IP_RSSBUCKETID, IPPROTO_IP, M_NOWAIT);
1340 			if (*mp)
1341 				mp = &(*mp)->m_next;
1342 		}
1343 	}
1344 #endif
1345 }
1346 
1347 /*
1348  * XXXRW: Multicast routing code in ip_mroute.c is generally MPSAFE, but the
1349  * ip_rsvp and ip_rsvp_on variables need to be interlocked with rsvp_on
1350  * locking.  This code remains in ip_input.c as ip_mroute.c is optionally
1351  * compiled.
1352  */
1353 VNET_DEFINE_STATIC(int, ip_rsvp_on);
1354 VNET_DEFINE(struct socket *, ip_rsvpd);
1355 
1356 #define	V_ip_rsvp_on		VNET(ip_rsvp_on)
1357 
1358 int
ip_rsvp_init(struct socket * so)1359 ip_rsvp_init(struct socket *so)
1360 {
1361 
1362 	if (V_ip_rsvpd != NULL)
1363 		return EADDRINUSE;
1364 
1365 	V_ip_rsvpd = so;
1366 	/*
1367 	 * This may seem silly, but we need to be sure we don't over-increment
1368 	 * the RSVP counter, in case something slips up.
1369 	 */
1370 	if (!V_ip_rsvp_on) {
1371 		V_ip_rsvp_on = 1;
1372 		V_rsvp_on++;
1373 	}
1374 
1375 	return 0;
1376 }
1377 
1378 int
ip_rsvp_done(void)1379 ip_rsvp_done(void)
1380 {
1381 
1382 	V_ip_rsvpd = NULL;
1383 	/*
1384 	 * This may seem silly, but we need to be sure we don't over-decrement
1385 	 * the RSVP counter, in case something slips up.
1386 	 */
1387 	if (V_ip_rsvp_on) {
1388 		V_ip_rsvp_on = 0;
1389 		V_rsvp_on--;
1390 	}
1391 	return 0;
1392 }
1393 
1394 int
rsvp_input(struct mbuf ** mp,int * offp,int proto)1395 rsvp_input(struct mbuf **mp, int *offp, int proto)
1396 {
1397 	struct mbuf *m;
1398 
1399 	m = *mp;
1400 	*mp = NULL;
1401 
1402 	if (rsvp_input_p) { /* call the real one if loaded */
1403 		*mp = m;
1404 		rsvp_input_p(mp, offp, proto);
1405 		return (IPPROTO_DONE);
1406 	}
1407 
1408 	/* Can still get packets with rsvp_on = 0 if there is a local member
1409 	 * of the group to which the RSVP packet is addressed.  But in this
1410 	 * case we want to throw the packet away.
1411 	 */
1412 
1413 	if (!V_rsvp_on) {
1414 		m_freem(m);
1415 		return (IPPROTO_DONE);
1416 	}
1417 
1418 	if (V_ip_rsvpd != NULL) {
1419 		*mp = m;
1420 		rip_input(mp, offp, proto);
1421 		return (IPPROTO_DONE);
1422 	}
1423 	/* Drop the packet */
1424 	m_freem(m);
1425 	return (IPPROTO_DONE);
1426 }
1427