xref: /xnu-10002.61.3/bsd/netinet/udp_usrreq.c (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1 /*
2  * Copyright (c) 2000-2021, 2023 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30  *	The Regents of the University of California.  All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  * 1. Redistributions of source code must retain the above copyright
36  *    notice, this list of conditions and the following disclaimer.
37  * 2. Redistributions in binary form must reproduce the above copyright
38  *    notice, this list of conditions and the following disclaimer in the
39  *    documentation and/or other materials provided with the distribution.
40  * 3. All advertising materials mentioning features or use of this software
41  *    must display the following acknowledgement:
42  *	This product includes software developed by the University of
43  *	California, Berkeley and its contributors.
44  * 4. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
61  */
62 
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67 #include <sys/mbuf.h>
68 #include <sys/domain.h>
69 #include <sys/protosw.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/sysctl.h>
73 #include <sys/syslog.h>
74 #include <sys/mcache.h>
75 #include <net/ntstat.h>
76 
77 #include <kern/zalloc.h>
78 #include <mach/boolean.h>
79 #include <pexpert/pexpert.h>
80 
81 #include <net/if.h>
82 #include <net/if_types.h>
83 #include <net/route.h>
84 #include <net/dlil.h>
85 #include <net/net_api_stats.h>
86 
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/in_tclass.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip6.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip_var.h>
95 #include <netinet6/in6_pcb.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/udp6_var.h>
98 #include <netinet/ip_icmp.h>
99 #include <netinet/icmp_var.h>
100 #include <netinet/udp.h>
101 #include <netinet/udp_var.h>
102 #include <netinet/udp_log.h>
103 #include <sys/kdebug.h>
104 
105 #if IPSEC
106 #include <netinet6/ipsec.h>
107 #include <netinet6/esp.h>
108 #include <netkey/key.h>
109 extern int ipsec_bypass;
110 extern int esp_udp_encap_port;
111 #endif /* IPSEC */
112 
113 #if NECP
114 #include <net/necp.h>
115 #endif /* NECP */
116 
117 #if FLOW_DIVERT
118 #include <netinet/flow_divert.h>
119 #endif /* FLOW_DIVERT */
120 
121 #if CONTENT_FILTER
122 #include <net/content_filter.h>
123 #endif /* CONTENT_FILTER */
124 
125 #if SKYWALK
126 #include <skywalk/core/skywalk_var.h>
127 #endif /* SKYWALK */
128 
129 #define DBG_LAYER_IN_BEG        NETDBG_CODE(DBG_NETUDP, 0)
130 #define DBG_LAYER_IN_END        NETDBG_CODE(DBG_NETUDP, 2)
131 #define DBG_LAYER_OUT_BEG       NETDBG_CODE(DBG_NETUDP, 1)
132 #define DBG_LAYER_OUT_END       NETDBG_CODE(DBG_NETUDP, 3)
133 #define DBG_FNC_UDP_INPUT       NETDBG_CODE(DBG_NETUDP, (5 << 8))
134 #define DBG_FNC_UDP_OUTPUT      NETDBG_CODE(DBG_NETUDP, (6 << 8) | 1)
135 
136 /*
137  * UDP protocol implementation.
138  * Per RFC 768, August, 1980.
139  */
140 #ifndef COMPAT_42
141 static int udpcksum = 1;
142 #else
143 static int udpcksum = 0;                /* XXX */
144 #endif
145 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum,
146     CTLFLAG_RW | CTLFLAG_LOCKED, &udpcksum, 0, "");
147 
148 int udp_log_in_vain = 0;
149 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW | CTLFLAG_LOCKED,
150     &udp_log_in_vain, 0, "Log all incoming UDP packets");
151 
152 static int blackhole = 0;
153 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW | CTLFLAG_LOCKED,
154     &blackhole, 0, "Do not send port unreachables for refused connects");
155 
156 static KALLOC_TYPE_DEFINE(inpcbzone, struct inpcb, NET_KT_DEFAULT);
157 
158 struct inpcbhead udb;           /* from udp_var.h */
159 #define udb6    udb  /* for KAME src sync over BSD*'s */
160 struct inpcbinfo udbinfo;
161 
162 #ifndef UDBHASHSIZE
163 #define UDBHASHSIZE 16
164 #endif
165 
166 /* Garbage collection performed during most recent udp_gc() run */
167 static boolean_t udp_gc_done = FALSE;
168 
169 #define log_in_vain_log(a) { log a; }
170 
171 static int udp_getstat SYSCTL_HANDLER_ARGS;
172 struct  udpstat udpstat;        /* from udp_var.h */
173 SYSCTL_PROC(_net_inet_udp, UDPCTL_STATS, stats,
174     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
175     0, 0, udp_getstat, "S,udpstat",
176     "UDP statistics (struct udpstat, netinet/udp_var.h)");
177 
178 SYSCTL_INT(_net_inet_udp, OID_AUTO, pcbcount,
179     CTLFLAG_RD | CTLFLAG_LOCKED, &udbinfo.ipi_count, 0,
180     "Number of active PCBs");
181 
182 __private_extern__ int udp_use_randomport = 1;
183 SYSCTL_INT(_net_inet_udp, OID_AUTO, randomize_ports,
184     CTLFLAG_RW | CTLFLAG_LOCKED, &udp_use_randomport, 0,
185     "Randomize UDP port numbers");
186 
187 struct udp_in6 {
188 	struct sockaddr_in6     uin6_sin;
189 	u_char                  uin6_init_done : 1;
190 };
191 struct udp_ip6 {
192 	struct ip6_hdr          uip6_ip6;
193 	u_char                  uip6_init_done : 1;
194 };
195 
196 int udp_abort(struct socket *);
197 int udp_attach(struct socket *, int, struct proc *);
198 int udp_bind(struct socket *, struct sockaddr *, struct proc *);
199 int udp_connect(struct socket *, struct sockaddr *, struct proc *);
200 int udp_connectx(struct socket *, struct sockaddr *,
201     struct sockaddr *, struct proc *, uint32_t, sae_associd_t,
202     sae_connid_t *, uint32_t, void *, uint32_t, struct uio *, user_ssize_t *);
203 int udp_detach(struct socket *);
204 int udp_disconnect(struct socket *);
205 int udp_disconnectx(struct socket *, sae_associd_t, sae_connid_t);
206 int udp_send(struct socket *, int, struct mbuf *, struct sockaddr *,
207     struct mbuf *, struct proc *);
208 static void udp_append(struct inpcb *, struct ip *, struct mbuf *, int,
209     struct sockaddr_in *, struct udp_in6 *, struct udp_ip6 *, struct ifnet *);
210 static int udp_input_checksum(struct mbuf *, struct udphdr *, int, int);
211 int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
212     struct mbuf *, struct proc *);
213 static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
214 static void udp_gc(struct inpcbinfo *);
215 static int udp_defunct(struct socket *);
216 
217 struct pr_usrreqs udp_usrreqs = {
218 	.pru_abort =            udp_abort,
219 	.pru_attach =           udp_attach,
220 	.pru_bind =             udp_bind,
221 	.pru_connect =          udp_connect,
222 	.pru_connectx =         udp_connectx,
223 	.pru_control =          in_control,
224 	.pru_detach =           udp_detach,
225 	.pru_disconnect =       udp_disconnect,
226 	.pru_disconnectx =      udp_disconnectx,
227 	.pru_peeraddr =         in_getpeeraddr,
228 	.pru_send =             udp_send,
229 	.pru_shutdown =         udp_shutdown,
230 	.pru_sockaddr =         in_getsockaddr,
231 	.pru_sosend =           sosend,
232 	.pru_soreceive =        soreceive,
233 	.pru_defunct =          udp_defunct,
234 };
235 
236 void
udp_init(struct protosw * pp,struct domain * dp)237 udp_init(struct protosw *pp, struct domain *dp)
238 {
239 #pragma unused(dp)
240 	static int udp_initialized = 0;
241 	struct inpcbinfo        *pcbinfo;
242 
243 	VERIFY((pp->pr_flags & (PR_INITIALIZED | PR_ATTACHED)) == PR_ATTACHED);
244 
245 	if (udp_initialized) {
246 		return;
247 	}
248 	udp_initialized = 1;
249 	uint32_t pool_size = (nmbclusters << MCLSHIFT) >> MBSHIFT;
250 	if (pool_size >= 96) {
251 		/* Improves 10GbE UDP performance. */
252 		udp_recvspace = 786896;
253 	}
254 
255 	if (PE_parse_boot_argn("udp_log", &udp_log_enable_flags, sizeof(udp_log_enable_flags))) {
256 		os_log(OS_LOG_DEFAULT, "udp_init: set udp_log_enable_flags to 0x%x", udp_log_enable_flags);
257 	}
258 
259 	LIST_INIT(&udb);
260 	udbinfo.ipi_listhead = &udb;
261 	udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB,
262 	    &udbinfo.ipi_hashmask);
263 	udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB,
264 	    &udbinfo.ipi_porthashmask);
265 	udbinfo.ipi_zone = inpcbzone;
266 
267 	pcbinfo = &udbinfo;
268 	/*
269 	 * allocate lock group and attribute for udp pcb mutexes
270 	 */
271 	pcbinfo->ipi_lock_grp = lck_grp_alloc_init("udppcb",
272 	    LCK_GRP_ATTR_NULL);
273 	lck_attr_setdefault(&pcbinfo->ipi_lock_attr);
274 	lck_rw_init(&pcbinfo->ipi_lock, pcbinfo->ipi_lock_grp,
275 	    &pcbinfo->ipi_lock_attr);
276 
277 	udbinfo.ipi_gc = udp_gc;
278 	in_pcbinfo_attach(&udbinfo);
279 }
280 
281 void
udp_input(struct mbuf * m,int iphlen)282 udp_input(struct mbuf *m, int iphlen)
283 {
284 	struct ip *ip;
285 	struct udphdr *uh;
286 	struct inpcb *inp;
287 	struct mbuf *opts = NULL;
288 	int len, isbroadcast;
289 	struct ip save_ip;
290 	struct sockaddr *append_sa;
291 	struct inpcbinfo *pcbinfo = &udbinfo;
292 	struct sockaddr_in udp_in;
293 	struct ip_moptions *imo = NULL;
294 	int foundmembership = 0, ret = 0;
295 	struct udp_in6 udp_in6;
296 	struct udp_ip6 udp_ip6;
297 	struct ifnet *ifp = m->m_pkthdr.rcvif;
298 	boolean_t cell = IFNET_IS_CELLULAR(ifp);
299 	boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp));
300 	boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp));
301 	u_int16_t pf_tag = 0;
302 	boolean_t is_wake_pkt = false;
303 
304 	bzero(&udp_in, sizeof(udp_in));
305 	udp_in.sin_len = sizeof(struct sockaddr_in);
306 	udp_in.sin_family = AF_INET;
307 	bzero(&udp_in6, sizeof(udp_in6));
308 	udp_in6.uin6_sin.sin6_len = sizeof(struct sockaddr_in6);
309 	udp_in6.uin6_sin.sin6_family = AF_INET6;
310 
311 	if (m->m_flags & M_PKTHDR) {
312 		pf_tag = m_pftag(m)->pftag_tag;
313 		if (m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT) {
314 			is_wake_pkt = true;
315 		}
316 	}
317 
318 	udpstat.udps_ipackets++;
319 
320 	KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_START, 0, 0, 0, 0, 0);
321 
322 	/* Expect 32-bit aligned data pointer on strict-align platforms */
323 	MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
324 
325 	m_add_crumb(m, PKT_CRUMB_UDP_INPUT);
326 
327 	/*
328 	 * Strip IP options, if any; should skip this,
329 	 * make available to user, and use on returned packets,
330 	 * but we don't yet have a way to check the checksum
331 	 * with options still present.
332 	 */
333 	if (iphlen > sizeof(struct ip)) {
334 		ip_stripoptions(m);
335 		iphlen = sizeof(struct ip);
336 	}
337 
338 	/*
339 	 * Get IP and UDP header together in first mbuf.
340 	 */
341 	ip = mtod(m, struct ip *);
342 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
343 		m = m_pullup(m, iphlen + sizeof(struct udphdr));
344 		if (m == NULL) {
345 			udpstat.udps_hdrops++;
346 			KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
347 			    0, 0, 0, 0, 0);
348 			return;
349 		}
350 		ip = mtod(m, struct ip *);
351 	}
352 	uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
353 
354 	/* destination port of 0 is illegal, based on RFC768. */
355 	if (uh->uh_dport == 0) {
356 		IF_UDP_STATINC(ifp, port0);
357 		goto bad;
358 	}
359 
360 	KERNEL_DEBUG(DBG_LAYER_IN_BEG, uh->uh_dport, uh->uh_sport,
361 	    ip->ip_src.s_addr, ip->ip_dst.s_addr, uh->uh_ulen);
362 
363 	/*
364 	 * Make mbuf data length reflect UDP length.
365 	 * If not enough data to reflect UDP length, drop.
366 	 */
367 	len = ntohs((u_short)uh->uh_ulen);
368 	if (ip->ip_len != len) {
369 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
370 			udpstat.udps_badlen++;
371 			IF_UDP_STATINC(ifp, badlength);
372 			goto bad;
373 		}
374 		m_adj(m, len - ip->ip_len);
375 		/* ip->ip_len = len; */
376 	}
377 	/*
378 	 * Save a copy of the IP header in case we want restore it
379 	 * for sending an ICMP error message in response.
380 	 */
381 	save_ip = *ip;
382 
383 	/*
384 	 * Checksum extended UDP header and data.
385 	 */
386 	if (udp_input_checksum(m, uh, iphlen, len)) {
387 		goto bad;
388 	}
389 
390 	isbroadcast = in_broadcast(ip->ip_dst, ifp);
391 
392 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || isbroadcast) {
393 		int reuse_sock = 0, mcast_delivered = 0;
394 
395 		lck_rw_lock_shared(&pcbinfo->ipi_lock);
396 		/*
397 		 * Deliver a multicast or broadcast datagram to *all* sockets
398 		 * for which the local and remote addresses and ports match
399 		 * those of the incoming datagram.  This allows more than
400 		 * one process to receive multi/broadcasts on the same port.
401 		 * (This really ought to be done for unicast datagrams as
402 		 * well, but that would cause problems with existing
403 		 * applications that open both address-specific sockets and
404 		 * a wildcard socket listening to the same port -- they would
405 		 * end up receiving duplicates of every unicast datagram.
406 		 * Those applications open the multiple sockets to overcome an
407 		 * inadequacy of the UDP socket interface, but for backwards
408 		 * compatibility we avoid the problem here rather than
409 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
410 		 */
411 
412 		/*
413 		 * Construct sockaddr format source address.
414 		 */
415 		udp_in.sin_port = uh->uh_sport;
416 		udp_in.sin_addr = ip->ip_src;
417 		/*
418 		 * Locate pcb(s) for datagram.
419 		 * (Algorithm copied from raw_intr().)
420 		 */
421 		udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
422 		LIST_FOREACH(inp, &udb, inp_list) {
423 #if IPSEC
424 			int skipit;
425 #endif /* IPSEC */
426 
427 			if (inp->inp_socket == NULL) {
428 				continue;
429 			}
430 			if (inp != sotoinpcb(inp->inp_socket)) {
431 				panic("%s: bad so back ptr inp=%p",
432 				    __func__, inp);
433 				/* NOTREACHED */
434 			}
435 			if ((inp->inp_vflag & INP_IPV4) == 0) {
436 				continue;
437 			}
438 			if (inp_restricted_recv(inp, ifp)) {
439 				continue;
440 			}
441 
442 			if ((inp->inp_moptions == NULL) &&
443 			    (ntohl(ip->ip_dst.s_addr) !=
444 			    INADDR_ALLHOSTS_GROUP) && (isbroadcast == 0)) {
445 				continue;
446 			}
447 			/*
448 			 * Skip unbound sockets before taking the lock on the socket as
449 			 * the test with the destination port in the header will fail
450 			 */
451 			if (inp->inp_lport == 0) {
452 				continue;
453 			}
454 
455 			if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) ==
456 			    WNT_STOPUSING) {
457 				continue;
458 			}
459 
460 			udp_lock(inp->inp_socket, 1, 0);
461 
462 			if (in_pcb_checkstate(inp, WNT_RELEASE, 1) ==
463 			    WNT_STOPUSING) {
464 				udp_unlock(inp->inp_socket, 1, 0);
465 				continue;
466 			}
467 
468 			if (inp->inp_lport != uh->uh_dport) {
469 				udp_unlock(inp->inp_socket, 1, 0);
470 				continue;
471 			}
472 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
473 				if (inp->inp_laddr.s_addr !=
474 				    ip->ip_dst.s_addr) {
475 					udp_unlock(inp->inp_socket, 1, 0);
476 					continue;
477 				}
478 			}
479 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
480 				if (inp->inp_faddr.s_addr !=
481 				    ip->ip_src.s_addr ||
482 				    inp->inp_fport != uh->uh_sport) {
483 					udp_unlock(inp->inp_socket, 1, 0);
484 					continue;
485 				}
486 			}
487 
488 			if (isbroadcast == 0 && (ntohl(ip->ip_dst.s_addr) !=
489 			    INADDR_ALLHOSTS_GROUP)) {
490 				struct sockaddr_in group;
491 				int blocked;
492 
493 				if ((imo = inp->inp_moptions) == NULL) {
494 					udp_unlock(inp->inp_socket, 1, 0);
495 					continue;
496 				}
497 				IMO_LOCK(imo);
498 
499 				bzero(&group, sizeof(struct sockaddr_in));
500 				group.sin_len = sizeof(struct sockaddr_in);
501 				group.sin_family = AF_INET;
502 				group.sin_addr = ip->ip_dst;
503 
504 				blocked = imo_multi_filter(imo, ifp,
505 				    &group, &udp_in);
506 				if (blocked == MCAST_PASS) {
507 					foundmembership = 1;
508 				}
509 
510 				IMO_UNLOCK(imo);
511 				if (!foundmembership) {
512 					udp_unlock(inp->inp_socket, 1, 0);
513 					if (blocked == MCAST_NOTSMEMBER ||
514 					    blocked == MCAST_MUTED) {
515 						udpstat.udps_filtermcast++;
516 					}
517 					continue;
518 				}
519 				foundmembership = 0;
520 			}
521 
522 			reuse_sock = (inp->inp_socket->so_options &
523 			    (SO_REUSEPORT | SO_REUSEADDR));
524 
525 #if NECP
526 			skipit = 0;
527 			if (!necp_socket_is_allowed_to_send_recv_v4(inp,
528 			    uh->uh_dport, uh->uh_sport, &ip->ip_dst,
529 			    &ip->ip_src, ifp, pf_tag, NULL, NULL, NULL, NULL)) {
530 				/* do not inject data to pcb */
531 				skipit = 1;
532 			}
533 			if (skipit == 0)
534 #endif /* NECP */
535 			{
536 				struct mbuf *n = NULL;
537 
538 				if (reuse_sock) {
539 					n = m_copy(m, 0, M_COPYALL);
540 				}
541 				udp_append(inp, ip, m,
542 				    iphlen + sizeof(struct udphdr),
543 				    &udp_in, &udp_in6, &udp_ip6, ifp);
544 				mcast_delivered++;
545 
546 				m = n;
547 			}
548 			if (is_wake_pkt) {
549 				soevent(inp->inp_socket, SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
550 			}
551 
552 			udp_unlock(inp->inp_socket, 1, 0);
553 
554 
555 			/*
556 			 * Don't look for additional matches if this one does
557 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
558 			 * socket options set.  This heuristic avoids searching
559 			 * through all pcbs in the common case of a non-shared
560 			 * port.  It assumes that an application will never
561 			 * clear these options after setting them.
562 			 */
563 			if (reuse_sock == 0 || m == NULL) {
564 				break;
565 			}
566 
567 			/*
568 			 * Expect 32-bit aligned data pointer on strict-align
569 			 * platforms.
570 			 */
571 			MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
572 			/*
573 			 * Recompute IP and UDP header pointers for new mbuf
574 			 */
575 			ip = mtod(m, struct ip *);
576 			uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
577 		}
578 		lck_rw_done(&pcbinfo->ipi_lock);
579 
580 		if (mcast_delivered == 0) {
581 			/*
582 			 * No matching pcb found; discard datagram.
583 			 * (No need to send an ICMP Port Unreachable
584 			 * for a broadcast or multicast datgram.)
585 			 */
586 			udpstat.udps_noportbcast++;
587 			IF_UDP_STATINC(ifp, port_unreach);
588 			goto bad;
589 		}
590 
591 		/* free the extra copy of mbuf or skipped by IPsec */
592 		if (m != NULL) {
593 			m_freem(m);
594 		}
595 		KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
596 		return;
597 	}
598 
599 #if IPSEC
600 	/*
601 	 * UDP to port 4500 with a payload where the first four bytes are
602 	 * not zero is a UDP encapsulated IPsec packet. Packets where
603 	 * the payload is one byte and that byte is 0xFF are NAT keepalive
604 	 * packets. Decapsulate the ESP packet and carry on with IPsec input
605 	 * or discard the NAT keep-alive.
606 	 */
607 	if (ipsec_bypass == 0 && (esp_udp_encap_port & 0xFFFF) != 0 &&
608 	    (uh->uh_dport == ntohs((u_short)esp_udp_encap_port) ||
609 	    uh->uh_sport == ntohs((u_short)esp_udp_encap_port))) {
610 		/*
611 		 * Check if ESP or keepalive:
612 		 *      1. If the destination port of the incoming packet is 4500.
613 		 *      2. If the source port of the incoming packet is 4500,
614 		 *         then check the SADB to match IP address and port.
615 		 */
616 		bool check_esp = true;
617 		if (uh->uh_dport != ntohs((u_short)esp_udp_encap_port)) {
618 			check_esp = key_checksa_present(AF_INET, (caddr_t)&ip->ip_dst,
619 			    (caddr_t)&ip->ip_src, uh->uh_dport,
620 			    uh->uh_sport, IFSCOPE_NONE, IFSCOPE_NONE);
621 		}
622 
623 		if (check_esp) {
624 			int payload_len = len - sizeof(struct udphdr) > 4 ? 4 :
625 			    len - sizeof(struct udphdr);
626 
627 			if (m->m_len < iphlen + sizeof(struct udphdr) + payload_len) {
628 				if ((m = m_pullup(m, iphlen + sizeof(struct udphdr) +
629 				    payload_len)) == NULL) {
630 					udpstat.udps_hdrops++;
631 					KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
632 					    0, 0, 0, 0, 0);
633 					return;
634 				}
635 				/*
636 				 * Expect 32-bit aligned data pointer on strict-align
637 				 * platforms.
638 				 */
639 				MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
640 
641 				ip = mtod(m, struct ip *);
642 				uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
643 			}
644 			/* Check for NAT keepalive packet */
645 			if (payload_len == 1 && *(u_int8_t *)
646 			    ((caddr_t)uh + sizeof(struct udphdr)) == 0xFF) {
647 				m_freem(m);
648 				KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
649 				    0, 0, 0, 0, 0);
650 				return;
651 			} else if (payload_len == 4 && *(u_int32_t *)(void *)
652 			    ((caddr_t)uh + sizeof(struct udphdr)) != 0) {
653 				/* UDP encapsulated IPsec packet to pass through NAT */
654 				KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
655 				    0, 0, 0, 0, 0);
656 				/* preserve the udp header */
657 				esp4_input(m, iphlen + sizeof(struct udphdr));
658 				return;
659 			}
660 		}
661 	}
662 #endif /* IPSEC */
663 
664 	/*
665 	 * Locate pcb for datagram.
666 	 */
667 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
668 	    ip->ip_dst, uh->uh_dport, 1, ifp);
669 	if (inp == NULL) {
670 		IF_UDP_STATINC(ifp, port_unreach);
671 
672 		if (udp_log_in_vain) {
673 			char buf[MAX_IPv4_STR_LEN];
674 			char buf2[MAX_IPv4_STR_LEN];
675 
676 			/* check src and dst address */
677 			if (udp_log_in_vain < 3) {
678 				log(LOG_INFO, "Connection attempt to "
679 				    "UDP %s:%d from %s:%d\n", inet_ntop(AF_INET,
680 				    &ip->ip_dst, buf, sizeof(buf)),
681 				    ntohs(uh->uh_dport), inet_ntop(AF_INET,
682 				    &ip->ip_src, buf2, sizeof(buf2)),
683 				    ntohs(uh->uh_sport));
684 			} else if (!(m->m_flags & (M_BCAST | M_MCAST)) &&
685 			    ip->ip_dst.s_addr != ip->ip_src.s_addr) {
686 				log_in_vain_log((LOG_INFO,
687 				    "Stealth Mode connection attempt to "
688 				    "UDP %s:%d from %s:%d\n", inet_ntop(AF_INET,
689 				    &ip->ip_dst, buf, sizeof(buf)),
690 				    ntohs(uh->uh_dport), inet_ntop(AF_INET,
691 				    &ip->ip_src, buf2, sizeof(buf2)),
692 				    ntohs(uh->uh_sport)))
693 			}
694 		}
695 		udpstat.udps_noport++;
696 		if (m->m_flags & (M_BCAST | M_MCAST)) {
697 			udpstat.udps_noportbcast++;
698 			goto bad;
699 		}
700 		if (blackhole) {
701 			if (ifp && ifp->if_type != IFT_LOOP) {
702 				goto bad;
703 			}
704 		}
705 		*ip = save_ip;
706 		ip->ip_len += iphlen;
707 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
708 		KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
709 		return;
710 	}
711 	udp_lock(inp->inp_socket, 1, 0);
712 
713 	if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
714 		udp_unlock(inp->inp_socket, 1, 0);
715 		IF_UDP_STATINC(ifp, cleanup);
716 		goto bad;
717 	}
718 #if NECP
719 	if (!necp_socket_is_allowed_to_send_recv_v4(inp, uh->uh_dport,
720 	    uh->uh_sport, &ip->ip_dst, &ip->ip_src, ifp, pf_tag, NULL, NULL, NULL, NULL)) {
721 		udp_unlock(inp->inp_socket, 1, 0);
722 		IF_UDP_STATINC(ifp, badipsec);
723 		goto bad;
724 	}
725 #endif /* NECP */
726 
727 	/*
728 	 * Construct sockaddr format source address.
729 	 * Stuff source address and datagram in user buffer.
730 	 */
731 	udp_in.sin_port = uh->uh_sport;
732 	udp_in.sin_addr = ip->ip_src;
733 	if ((inp->inp_flags & INP_CONTROLOPTS) != 0 ||
734 	    SOFLOW_ENABLED(inp->inp_socket) ||
735 	    SO_RECV_CONTROL_OPTS(inp->inp_socket)) {
736 		if (inp->inp_vflag & INP_IPV6 || inp->inp_vflag & INP_V4MAPPEDV6) {
737 			int savedflags;
738 
739 			ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
740 			savedflags = inp->inp_flags;
741 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
742 			ret = ip6_savecontrol(inp, m, &opts);
743 			inp->inp_flags = savedflags;
744 		} else {
745 			ret = ip_savecontrol(inp, &opts, ip, m);
746 		}
747 		if (ret != 0) {
748 			udp_unlock(inp->inp_socket, 1, 0);
749 			goto bad;
750 		}
751 	}
752 	m_adj(m, iphlen + sizeof(struct udphdr));
753 
754 	KERNEL_DEBUG(DBG_LAYER_IN_END, uh->uh_dport, uh->uh_sport,
755 	    save_ip.ip_src.s_addr, save_ip.ip_dst.s_addr, uh->uh_ulen);
756 
757 	if (inp->inp_vflag & INP_IPV6) {
758 		in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
759 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
760 	} else {
761 		append_sa = (struct sockaddr *)&udp_in;
762 	}
763 	if (nstat_collect) {
764 		INP_ADD_STAT(inp, cell, wifi, wired, rxpackets, 1);
765 		INP_ADD_STAT(inp, cell, wifi, wired, rxbytes, m->m_pkthdr.len);
766 		inp_set_activity_bitmap(inp);
767 	}
768 	so_recv_data_stat(inp->inp_socket, m, 0);
769 	if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa,
770 	    m, opts, NULL) == 0) {
771 		udpstat.udps_fullsock++;
772 	} else {
773 		sorwakeup(inp->inp_socket);
774 	}
775 	if (is_wake_pkt) {
776 		soevent(inp->inp_socket, SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
777 	}
778 	udp_unlock(inp->inp_socket, 1, 0);
779 	KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
780 	return;
781 bad:
782 	m_freem(m);
783 	if (opts) {
784 		m_freem(opts);
785 	}
786 	KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
787 }
788 
789 static void
ip_2_ip6_hdr(struct ip6_hdr * ip6,struct ip * ip)790 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
791 {
792 	bzero(ip6, sizeof(*ip6));
793 
794 	ip6->ip6_vfc = IPV6_VERSION;
795 	ip6->ip6_plen = ip->ip_len;
796 	ip6->ip6_nxt = ip->ip_p;
797 	ip6->ip6_hlim = ip->ip_ttl;
798 	if (ip->ip_src.s_addr) {
799 		ip6->ip6_src.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
800 		ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
801 	}
802 	if (ip->ip_dst.s_addr) {
803 		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
804 		ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
805 	}
806 }
807 
808 /*
809  * subroutine of udp_input(), mainly for source code readability.
810  */
811 static void
udp_append(struct inpcb * last,struct ip * ip,struct mbuf * n,int off,struct sockaddr_in * pudp_in,struct udp_in6 * pudp_in6,struct udp_ip6 * pudp_ip6,struct ifnet * ifp)812 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
813     struct sockaddr_in *pudp_in, struct udp_in6 *pudp_in6,
814     struct udp_ip6 *pudp_ip6, struct ifnet *ifp)
815 {
816 	struct sockaddr *append_sa;
817 	struct mbuf *opts = 0;
818 	boolean_t cell = IFNET_IS_CELLULAR(ifp);
819 	boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp));
820 	boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp));
821 	int ret = 0;
822 
823 	if ((last->inp_flags & INP_CONTROLOPTS) != 0 ||
824 	    SOFLOW_ENABLED(last->inp_socket) ||
825 	    SO_RECV_CONTROL_OPTS(last->inp_socket)) {
826 		if (last->inp_vflag & INP_IPV6 || last->inp_vflag & INP_V4MAPPEDV6) {
827 			int savedflags;
828 
829 			if (pudp_ip6->uip6_init_done == 0) {
830 				ip_2_ip6_hdr(&pudp_ip6->uip6_ip6, ip);
831 				pudp_ip6->uip6_init_done = 1;
832 			}
833 			savedflags = last->inp_flags;
834 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
835 			ret = ip6_savecontrol(last, n, &opts);
836 			if (ret != 0) {
837 				last->inp_flags = savedflags;
838 				goto error;
839 			}
840 			last->inp_flags = savedflags;
841 		} else {
842 			ret = ip_savecontrol(last, &opts, ip, n);
843 			if (ret != 0) {
844 				goto error;
845 			}
846 		}
847 	}
848 	if (last->inp_vflag & INP_IPV6) {
849 		if (pudp_in6->uin6_init_done == 0) {
850 			in6_sin_2_v4mapsin6(pudp_in, &pudp_in6->uin6_sin);
851 			pudp_in6->uin6_init_done = 1;
852 		}
853 		append_sa = (struct sockaddr *)&pudp_in6->uin6_sin;
854 	} else {
855 		append_sa = (struct sockaddr *)pudp_in;
856 	}
857 	if (nstat_collect) {
858 		INP_ADD_STAT(last, cell, wifi, wired, rxpackets, 1);
859 		INP_ADD_STAT(last, cell, wifi, wired, rxbytes,
860 		    n->m_pkthdr.len);
861 		inp_set_activity_bitmap(last);
862 	}
863 	so_recv_data_stat(last->inp_socket, n, 0);
864 	m_adj(n, off);
865 	if (sbappendaddr(&last->inp_socket->so_rcv, append_sa,
866 	    n, opts, NULL) == 0) {
867 		udpstat.udps_fullsock++;
868 	} else {
869 		sorwakeup(last->inp_socket);
870 	}
871 	return;
872 error:
873 	m_freem(n);
874 	m_freem(opts);
875 }
876 
877 /*
878  * Notify a udp user of an asynchronous error;
879  * just wake up so that he can collect error status.
880  */
881 void
udp_notify(struct inpcb * inp,int errno)882 udp_notify(struct inpcb *inp, int errno)
883 {
884 	inp->inp_socket->so_error = (u_short)errno;
885 	sorwakeup(inp->inp_socket);
886 	sowwakeup(inp->inp_socket);
887 }
888 
889 void
udp_ctlinput(int cmd,struct sockaddr * sa,void * vip,__unused struct ifnet * ifp)890 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip, __unused struct ifnet * ifp)
891 {
892 	struct ipctlparam *ctl_param = vip;
893 	struct ip *ip = NULL;
894 	struct mbuf *m = NULL;
895 	void (*notify)(struct inpcb *, int) = udp_notify;
896 	struct in_addr faddr;
897 	struct inpcb *inp = NULL;
898 	struct icmp *icp = NULL;
899 	size_t off;
900 
901 	if (ctl_param != NULL) {
902 		ip = ctl_param->ipc_icmp_ip;
903 		icp = ctl_param->ipc_icmp;
904 		m = ctl_param->ipc_m;
905 		off = ctl_param->ipc_off;
906 	} else {
907 		ip = NULL;
908 		icp = NULL;
909 		m = NULL;
910 		off = 0;
911 	}
912 
913 	faddr = ((struct sockaddr_in *)(void *)sa)->sin_addr;
914 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) {
915 		return;
916 	}
917 
918 	if (PRC_IS_REDIRECT(cmd)) {
919 		ip = 0;
920 		notify = in_rtchange;
921 	} else if (cmd == PRC_HOSTDEAD) {
922 		ip = 0;
923 	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
924 		return;
925 	}
926 	if (ip) {
927 		struct udphdr uh;
928 
929 		/* Check if we can safely get the ports from the UDP header */
930 		if (m == NULL ||
931 		    (m->m_len < off + sizeof(uh))) {
932 			/* Insufficient length */
933 			return;
934 		}
935 
936 		bcopy(m->m_data + off, &uh, sizeof(uh));
937 		inp = in_pcblookup_hash(&udbinfo, faddr, uh.uh_dport,
938 		    ip->ip_src, uh.uh_sport, 0, NULL);
939 
940 		if (inp != NULL && inp->inp_socket != NULL) {
941 			udp_lock(inp->inp_socket, 1, 0);
942 			if (in_pcb_checkstate(inp, WNT_RELEASE, 1) ==
943 			    WNT_STOPUSING) {
944 				udp_unlock(inp->inp_socket, 1, 0);
945 				return;
946 			}
947 			if (cmd == PRC_MSGSIZE && !uuid_is_null(inp->necp_client_uuid)) {
948 				uuid_t null_uuid;
949 				uuid_clear(null_uuid);
950 				necp_update_flow_protoctl_event(null_uuid, inp->necp_client_uuid,
951 				    PRC_MSGSIZE, ntohs(icp->icmp_nextmtu), 0);
952 				/*
953 				 * Avoid calling udp_notify() to set so_error
954 				 * when using Network.framework since the notification
955 				 * of PRC_MSGSIZE has been delivered through NECP.
956 				 */
957 			} else {
958 				(*notify)(inp, inetctlerrmap[cmd]);
959 			}
960 			udp_unlock(inp->inp_socket, 1, 0);
961 		}
962 #if SKYWALK
963 		else {
964 			union sockaddr_in_4_6 sock_laddr;
965 			struct protoctl_ev_val prctl_ev_val;
966 			bzero(&prctl_ev_val, sizeof(prctl_ev_val));
967 			bzero(&sock_laddr, sizeof(sock_laddr));
968 
969 			if (cmd == PRC_MSGSIZE) {
970 				prctl_ev_val.val = ntohs(icp->icmp_nextmtu);
971 			}
972 
973 			sock_laddr.sin.sin_family = AF_INET;
974 			sock_laddr.sin.sin_len = sizeof(sock_laddr.sin);
975 			sock_laddr.sin.sin_addr = ip->ip_src;
976 
977 			protoctl_event_enqueue_nwk_wq_entry(ifp,
978 			    (struct sockaddr *)&sock_laddr, sa,
979 			    uh.uh_sport, uh.uh_dport, IPPROTO_UDP,
980 			    cmd, &prctl_ev_val);
981 		}
982 #endif /* SKYWALK */
983 	} else {
984 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
985 	}
986 }
987 
988 int
udp_ctloutput(struct socket * so,struct sockopt * sopt)989 udp_ctloutput(struct socket *so, struct sockopt *sopt)
990 {
991 	int     error = 0, optval = 0;
992 	struct  inpcb *inp;
993 
994 	/* Allow <SOL_SOCKET,SO_FLUSH> at this level */
995 	if (sopt->sopt_level != IPPROTO_UDP &&
996 	    !(sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_FLUSH)) {
997 		if (SOCK_CHECK_DOM(so, PF_INET6)) {
998 			error = ip6_ctloutput(so, sopt);
999 		} else {
1000 			error = ip_ctloutput(so, sopt);
1001 		}
1002 		return error;
1003 	}
1004 
1005 	inp = sotoinpcb(so);
1006 
1007 	switch (sopt->sopt_dir) {
1008 	case SOPT_SET:
1009 		switch (sopt->sopt_name) {
1010 		case UDP_NOCKSUM:
1011 			/* This option is settable only for UDP over IPv4 */
1012 			if (!(inp->inp_vflag & INP_IPV4)) {
1013 				error = EINVAL;
1014 				break;
1015 			}
1016 
1017 			if ((error = sooptcopyin(sopt, &optval, sizeof(optval),
1018 			    sizeof(optval))) != 0) {
1019 				break;
1020 			}
1021 
1022 			if (optval != 0) {
1023 				inp->inp_flags |= INP_UDP_NOCKSUM;
1024 			} else {
1025 				inp->inp_flags &= ~INP_UDP_NOCKSUM;
1026 			}
1027 			break;
1028 		case UDP_KEEPALIVE_OFFLOAD:
1029 		{
1030 			struct udp_keepalive_offload ka;
1031 			/*
1032 			 * If the socket is not connected, the stack will
1033 			 * not know the destination address to put in the
1034 			 * keepalive datagram. Return an error now instead
1035 			 * of failing later.
1036 			 */
1037 			if (!(so->so_state & SS_ISCONNECTED)) {
1038 				error = EINVAL;
1039 				break;
1040 			}
1041 			if (sopt->sopt_valsize != sizeof(ka)) {
1042 				error = EINVAL;
1043 				break;
1044 			}
1045 			if ((error = sooptcopyin(sopt, &ka, sizeof(ka),
1046 			    sizeof(ka))) != 0) {
1047 				break;
1048 			}
1049 
1050 			/* application should specify the type */
1051 			if (ka.ka_type == 0) {
1052 				return EINVAL;
1053 			}
1054 
1055 			if (ka.ka_interval == 0) {
1056 				/*
1057 				 * if interval is 0, disable the offload
1058 				 * mechanism
1059 				 */
1060 				if (inp->inp_keepalive_data != NULL) {
1061 					kfree_data(inp->inp_keepalive_data,
1062 					    inp->inp_keepalive_datalen);
1063 				}
1064 				inp->inp_keepalive_data = NULL;
1065 				inp->inp_keepalive_datalen = 0;
1066 				inp->inp_keepalive_interval = 0;
1067 				inp->inp_keepalive_type = 0;
1068 				inp->inp_flags2 &= ~INP2_KEEPALIVE_OFFLOAD;
1069 			} else {
1070 				if (inp->inp_keepalive_data != NULL) {
1071 					kfree_data(inp->inp_keepalive_data,
1072 					    inp->inp_keepalive_datalen);
1073 					inp->inp_keepalive_data = NULL;
1074 				}
1075 
1076 				inp->inp_keepalive_datalen = (uint8_t)min(
1077 					ka.ka_data_len,
1078 					UDP_KEEPALIVE_OFFLOAD_DATA_SIZE);
1079 				if (inp->inp_keepalive_datalen > 0) {
1080 					inp->inp_keepalive_data = (u_int8_t *)kalloc_data(
1081 						inp->inp_keepalive_datalen, Z_WAITOK);
1082 					if (inp->inp_keepalive_data == NULL) {
1083 						inp->inp_keepalive_datalen = 0;
1084 						error = ENOMEM;
1085 						break;
1086 					}
1087 					bcopy(ka.ka_data,
1088 					    inp->inp_keepalive_data,
1089 					    inp->inp_keepalive_datalen);
1090 				} else {
1091 					inp->inp_keepalive_datalen = 0;
1092 				}
1093 				inp->inp_keepalive_interval = (uint8_t)
1094 				    min(UDP_KEEPALIVE_INTERVAL_MAX_SECONDS,
1095 				    ka.ka_interval);
1096 				inp->inp_keepalive_type = ka.ka_type;
1097 				inp->inp_flags2 |= INP2_KEEPALIVE_OFFLOAD;
1098 			}
1099 			break;
1100 		}
1101 		case SO_FLUSH:
1102 			if ((error = sooptcopyin(sopt, &optval, sizeof(optval),
1103 			    sizeof(optval))) != 0) {
1104 				break;
1105 			}
1106 
1107 			error = inp_flush(inp, optval);
1108 			break;
1109 
1110 		default:
1111 			error = ENOPROTOOPT;
1112 			break;
1113 		}
1114 		break;
1115 
1116 	case SOPT_GET:
1117 		switch (sopt->sopt_name) {
1118 		case UDP_NOCKSUM:
1119 			optval = inp->inp_flags & INP_UDP_NOCKSUM;
1120 			break;
1121 
1122 		default:
1123 			error = ENOPROTOOPT;
1124 			break;
1125 		}
1126 		if (error == 0) {
1127 			error = sooptcopyout(sopt, &optval, sizeof(optval));
1128 		}
1129 		break;
1130 	}
1131 	return error;
1132 }
1133 
1134 static int
1135 udp_pcblist SYSCTL_HANDLER_ARGS
1136 {
1137 #pragma unused(oidp, arg1, arg2)
1138 	int error, i, n, sz;
1139 	struct inpcb *inp, **inp_list;
1140 	inp_gen_t gencnt;
1141 	struct xinpgen xig;
1142 
1143 	/*
1144 	 * The process of preparing the TCB list is too time-consuming and
1145 	 * resource-intensive to repeat twice on every request.
1146 	 */
1147 	lck_rw_lock_exclusive(&udbinfo.ipi_lock);
1148 	if (req->oldptr == USER_ADDR_NULL) {
1149 		n = udbinfo.ipi_count;
1150 		req->oldidx = 2 * (sizeof(xig))
1151 		    + (n + n / 8) * sizeof(struct xinpcb);
1152 		lck_rw_done(&udbinfo.ipi_lock);
1153 		return 0;
1154 	}
1155 
1156 	if (req->newptr != USER_ADDR_NULL) {
1157 		lck_rw_done(&udbinfo.ipi_lock);
1158 		return EPERM;
1159 	}
1160 
1161 	/*
1162 	 * OK, now we're committed to doing something.
1163 	 */
1164 	gencnt = udbinfo.ipi_gencnt;
1165 	sz = n = udbinfo.ipi_count;
1166 
1167 	bzero(&xig, sizeof(xig));
1168 	xig.xig_len = sizeof(xig);
1169 	xig.xig_count = n;
1170 	xig.xig_gen = gencnt;
1171 	xig.xig_sogen = so_gencnt;
1172 	error = SYSCTL_OUT(req, &xig, sizeof(xig));
1173 	if (error) {
1174 		lck_rw_done(&udbinfo.ipi_lock);
1175 		return error;
1176 	}
1177 	/*
1178 	 * We are done if there is no pcb
1179 	 */
1180 	if (n == 0) {
1181 		lck_rw_done(&udbinfo.ipi_lock);
1182 		return 0;
1183 	}
1184 
1185 	inp_list = kalloc_type(struct inpcb *, n, Z_WAITOK);
1186 	if (inp_list == NULL) {
1187 		lck_rw_done(&udbinfo.ipi_lock);
1188 		return ENOMEM;
1189 	}
1190 
1191 	for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n;
1192 	    inp = LIST_NEXT(inp, inp_list)) {
1193 		if (inp->inp_gencnt <= gencnt &&
1194 		    inp->inp_state != INPCB_STATE_DEAD) {
1195 			inp_list[i++] = inp;
1196 		}
1197 	}
1198 	n = i;
1199 
1200 	error = 0;
1201 	for (i = 0; i < n; i++) {
1202 		struct xinpcb xi;
1203 
1204 		inp = inp_list[i];
1205 
1206 		if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING) {
1207 			continue;
1208 		}
1209 		udp_lock(inp->inp_socket, 1, 0);
1210 		if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
1211 			udp_unlock(inp->inp_socket, 1, 0);
1212 			continue;
1213 		}
1214 		if (inp->inp_gencnt > gencnt) {
1215 			udp_unlock(inp->inp_socket, 1, 0);
1216 			continue;
1217 		}
1218 
1219 		bzero(&xi, sizeof(xi));
1220 		xi.xi_len = sizeof(xi);
1221 		/* XXX should avoid extra copy */
1222 		inpcb_to_compat(inp, &xi.xi_inp);
1223 		if (inp->inp_socket) {
1224 			sotoxsocket(inp->inp_socket, &xi.xi_socket);
1225 		}
1226 
1227 		udp_unlock(inp->inp_socket, 1, 0);
1228 
1229 		error = SYSCTL_OUT(req, &xi, sizeof(xi));
1230 	}
1231 	if (!error) {
1232 		/*
1233 		 * Give the user an updated idea of our state.
1234 		 * If the generation differs from what we told
1235 		 * her before, she knows that something happened
1236 		 * while we were processing this request, and it
1237 		 * might be necessary to retry.
1238 		 */
1239 		bzero(&xig, sizeof(xig));
1240 		xig.xig_len = sizeof(xig);
1241 		xig.xig_gen = udbinfo.ipi_gencnt;
1242 		xig.xig_sogen = so_gencnt;
1243 		xig.xig_count = udbinfo.ipi_count;
1244 		error = SYSCTL_OUT(req, &xig, sizeof(xig));
1245 	}
1246 
1247 	lck_rw_done(&udbinfo.ipi_lock);
1248 	kfree_type(struct inpcb *, sz, inp_list);
1249 	return error;
1250 }
1251 
1252 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
1253     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist,
1254     "S,xinpcb", "List of active UDP sockets");
1255 
1256 #if XNU_TARGET_OS_OSX
1257 
1258 static int
1259 udp_pcblist64 SYSCTL_HANDLER_ARGS
1260 {
1261 #pragma unused(oidp, arg1, arg2)
1262 	int error, i, n, sz;
1263 	struct inpcb *inp, **inp_list;
1264 	inp_gen_t gencnt;
1265 	struct xinpgen xig;
1266 
1267 	/*
1268 	 * The process of preparing the TCB list is too time-consuming and
1269 	 * resource-intensive to repeat twice on every request.
1270 	 */
1271 	lck_rw_lock_shared(&udbinfo.ipi_lock);
1272 	if (req->oldptr == USER_ADDR_NULL) {
1273 		n = udbinfo.ipi_count;
1274 		req->oldidx =
1275 		    2 * (sizeof(xig)) + (n + n / 8) * sizeof(struct xinpcb64);
1276 		lck_rw_done(&udbinfo.ipi_lock);
1277 		return 0;
1278 	}
1279 
1280 	if (req->newptr != USER_ADDR_NULL) {
1281 		lck_rw_done(&udbinfo.ipi_lock);
1282 		return EPERM;
1283 	}
1284 
1285 	/*
1286 	 * OK, now we're committed to doing something.
1287 	 */
1288 	gencnt = udbinfo.ipi_gencnt;
1289 	sz = n = udbinfo.ipi_count;
1290 
1291 	bzero(&xig, sizeof(xig));
1292 	xig.xig_len = sizeof(xig);
1293 	xig.xig_count = n;
1294 	xig.xig_gen = gencnt;
1295 	xig.xig_sogen = so_gencnt;
1296 	error = SYSCTL_OUT(req, &xig, sizeof(xig));
1297 	if (error) {
1298 		lck_rw_done(&udbinfo.ipi_lock);
1299 		return error;
1300 	}
1301 	/*
1302 	 * We are done if there is no pcb
1303 	 */
1304 	if (n == 0) {
1305 		lck_rw_done(&udbinfo.ipi_lock);
1306 		return 0;
1307 	}
1308 
1309 	inp_list = kalloc_type(struct inpcb *, n, Z_WAITOK);
1310 	if (inp_list == NULL) {
1311 		lck_rw_done(&udbinfo.ipi_lock);
1312 		return ENOMEM;
1313 	}
1314 
1315 	for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n;
1316 	    inp = LIST_NEXT(inp, inp_list)) {
1317 		if (inp->inp_gencnt <= gencnt &&
1318 		    inp->inp_state != INPCB_STATE_DEAD) {
1319 			inp_list[i++] = inp;
1320 		}
1321 	}
1322 	n = i;
1323 
1324 	error = 0;
1325 	for (i = 0; i < n; i++) {
1326 		struct xinpcb64 xi;
1327 
1328 		inp = inp_list[i];
1329 
1330 		if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING) {
1331 			continue;
1332 		}
1333 		udp_lock(inp->inp_socket, 1, 0);
1334 		if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
1335 			udp_unlock(inp->inp_socket, 1, 0);
1336 			continue;
1337 		}
1338 		if (inp->inp_gencnt > gencnt) {
1339 			udp_unlock(inp->inp_socket, 1, 0);
1340 			continue;
1341 		}
1342 
1343 		bzero(&xi, sizeof(xi));
1344 		xi.xi_len = sizeof(xi);
1345 		inpcb_to_xinpcb64(inp, &xi);
1346 		if (inp->inp_socket) {
1347 			sotoxsocket64(inp->inp_socket, &xi.xi_socket);
1348 		}
1349 
1350 		udp_unlock(inp->inp_socket, 1, 0);
1351 
1352 		error = SYSCTL_OUT(req, &xi, sizeof(xi));
1353 	}
1354 	if (!error) {
1355 		/*
1356 		 * Give the user an updated idea of our state.
1357 		 * If the generation differs from what we told
1358 		 * her before, she knows that something happened
1359 		 * while we were processing this request, and it
1360 		 * might be necessary to retry.
1361 		 */
1362 		bzero(&xig, sizeof(xig));
1363 		xig.xig_len = sizeof(xig);
1364 		xig.xig_gen = udbinfo.ipi_gencnt;
1365 		xig.xig_sogen = so_gencnt;
1366 		xig.xig_count = udbinfo.ipi_count;
1367 		error = SYSCTL_OUT(req, &xig, sizeof(xig));
1368 	}
1369 
1370 	lck_rw_done(&udbinfo.ipi_lock);
1371 	kfree_type(struct inpcb *, sz, inp_list);
1372 	return error;
1373 }
1374 
1375 SYSCTL_PROC(_net_inet_udp, OID_AUTO, pcblist64,
1376     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist64,
1377     "S,xinpcb64", "List of active UDP sockets");
1378 
1379 #endif /* XNU_TARGET_OS_OSX */
1380 
1381 static int
1382 udp_pcblist_n SYSCTL_HANDLER_ARGS
1383 {
1384 #pragma unused(oidp, arg1, arg2)
1385 	return get_pcblist_n(IPPROTO_UDP, req, &udbinfo);
1386 }
1387 
1388 SYSCTL_PROC(_net_inet_udp, OID_AUTO, pcblist_n,
1389     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist_n,
1390     "S,xinpcb_n", "List of active UDP sockets");
1391 
1392 __private_extern__ void
udp_get_ports_used(ifnet_t ifp,int protocol,uint32_t flags,bitstr_t * bitfield)1393 udp_get_ports_used(ifnet_t ifp, int protocol, uint32_t flags,
1394     bitstr_t *bitfield)
1395 {
1396 	inpcb_get_ports_used(ifp, protocol, flags, bitfield,
1397 	    &udbinfo);
1398 }
1399 
1400 __private_extern__ uint32_t
udp_count_opportunistic(unsigned int ifindex,u_int32_t flags)1401 udp_count_opportunistic(unsigned int ifindex, u_int32_t flags)
1402 {
1403 	return inpcb_count_opportunistic(ifindex, &udbinfo, flags);
1404 }
1405 
1406 __private_extern__ uint32_t
udp_find_anypcb_byaddr(struct ifaddr * ifa)1407 udp_find_anypcb_byaddr(struct ifaddr *ifa)
1408 {
1409 #if SKYWALK
1410 	if (netns_is_enabled()) {
1411 		return netns_find_anyres_byaddr(ifa, IPPROTO_UDP);
1412 	} else
1413 #endif /* SKYWALK */
1414 	return inpcb_find_anypcb_byaddr(ifa, &udbinfo);
1415 }
1416 
1417 static int
udp_check_pktinfo(struct mbuf * control,struct ifnet ** outif,struct in_addr * laddr)1418 udp_check_pktinfo(struct mbuf *control, struct ifnet **outif,
1419     struct in_addr *laddr)
1420 {
1421 	struct cmsghdr *cm = 0;
1422 	struct in_pktinfo *pktinfo;
1423 	struct ifnet *ifp;
1424 
1425 	if (outif != NULL) {
1426 		*outif = NULL;
1427 	}
1428 
1429 	/*
1430 	 * XXX: Currently, we assume all the optional information is stored
1431 	 * in a single mbuf.
1432 	 */
1433 	if (control->m_next) {
1434 		return EINVAL;
1435 	}
1436 
1437 	if (control->m_len < CMSG_LEN(0)) {
1438 		return EINVAL;
1439 	}
1440 
1441 	for (cm = M_FIRST_CMSGHDR(control);
1442 	    is_cmsg_valid(control, cm);
1443 	    cm = M_NXT_CMSGHDR(control, cm)) {
1444 		if (cm->cmsg_level != IPPROTO_IP ||
1445 		    cm->cmsg_type != IP_PKTINFO) {
1446 			continue;
1447 		}
1448 
1449 		if (cm->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo))) {
1450 			return EINVAL;
1451 		}
1452 
1453 		pktinfo =  (struct in_pktinfo *)(void *)CMSG_DATA(cm);
1454 
1455 		/* Check for a valid ifindex in pktinfo */
1456 		ifnet_head_lock_shared();
1457 
1458 		if (pktinfo->ipi_ifindex > if_index) {
1459 			ifnet_head_done();
1460 			return ENXIO;
1461 		}
1462 
1463 		/*
1464 		 * If ipi_ifindex is specified it takes precedence
1465 		 * over ipi_spec_dst.
1466 		 */
1467 		if (pktinfo->ipi_ifindex) {
1468 			ifp = ifindex2ifnet[pktinfo->ipi_ifindex];
1469 			if (ifp == NULL) {
1470 				ifnet_head_done();
1471 				return ENXIO;
1472 			}
1473 			if (outif != NULL) {
1474 				ifnet_reference(ifp);
1475 				*outif = ifp;
1476 			}
1477 			ifnet_head_done();
1478 			laddr->s_addr = INADDR_ANY;
1479 			break;
1480 		}
1481 
1482 		ifnet_head_done();
1483 
1484 		/*
1485 		 * Use the provided ipi_spec_dst address for temp
1486 		 * source address.
1487 		 */
1488 		*laddr = pktinfo->ipi_spec_dst;
1489 		break;
1490 	}
1491 	return 0;
1492 }
1493 
1494 int
udp_output(struct inpcb * inp,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct proc * p)1495 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
1496     struct mbuf *control, struct proc *p)
1497 {
1498 	struct udpiphdr *ui;
1499 	int len = m->m_pkthdr.len;
1500 	struct sockaddr_in *sin;
1501 	struct in_addr origladdr, laddr, faddr, pi_laddr;
1502 	u_short lport, fport;
1503 	int error = 0, udp_dodisconnect = 0, pktinfo = 0;
1504 	struct socket *so = inp->inp_socket;
1505 	int soopts = 0;
1506 	struct mbuf *inpopts;
1507 	struct ip_moptions *mopts;
1508 	struct route ro;
1509 	struct ip_out_args ipoa;
1510 	bool sndinprog_cnt_used = false;
1511 #if CONTENT_FILTER
1512 	struct m_tag *cfil_tag = NULL;
1513 	bool cfil_faddr_use = false;
1514 	uint32_t cfil_so_state_change_cnt = 0;
1515 	uint32_t cfil_so_options = 0;
1516 	struct sockaddr *cfil_faddr = NULL;
1517 #endif
1518 	bool check_qos_marking_again = (so->so_flags1 & SOF1_QOSMARKING_POLICY_OVERRIDE) ? FALSE : TRUE;
1519 
1520 	bzero(&ipoa, sizeof(ipoa));
1521 	ipoa.ipoa_boundif = IFSCOPE_NONE;
1522 	ipoa.ipoa_flags = IPOAF_SELECT_SRCIF;
1523 
1524 	struct ifnet *outif = NULL;
1525 	struct flowadv *adv = &ipoa.ipoa_flowadv;
1526 	int sotc = SO_TC_UNSPEC;
1527 	int netsvctype = _NET_SERVICE_TYPE_UNSPEC;
1528 	struct ifnet *origoutifp = NULL;
1529 	int flowadv = 0;
1530 	int tos = IPTOS_UNSPEC;
1531 
1532 	/* Enable flow advisory only when connected */
1533 	flowadv = (so->so_state & SS_ISCONNECTED) ? 1 : 0;
1534 	pi_laddr.s_addr = INADDR_ANY;
1535 
1536 	KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_START, 0, 0, 0, 0, 0);
1537 
1538 	socket_lock_assert_owned(so);
1539 
1540 #if CONTENT_FILTER
1541 	/*
1542 	 * If socket is subject to UDP Content Filter and no addr is passed in,
1543 	 * retrieve CFIL saved state from mbuf and use it if necessary.
1544 	 */
1545 	if (CFIL_DGRAM_FILTERED(so) && !addr) {
1546 		cfil_tag = cfil_dgram_get_socket_state(m, &cfil_so_state_change_cnt, &cfil_so_options, &cfil_faddr, NULL);
1547 		if (cfil_tag) {
1548 			sin = (struct sockaddr_in *)(void *)cfil_faddr;
1549 			if (inp && inp->inp_faddr.s_addr == INADDR_ANY) {
1550 				/*
1551 				 * Socket is unconnected, simply use the saved faddr as 'addr' to go through
1552 				 * the connect/disconnect logic.
1553 				 */
1554 				addr = (struct sockaddr *)cfil_faddr;
1555 			} else if ((so->so_state_change_cnt != cfil_so_state_change_cnt) &&
1556 			    (inp->inp_fport != sin->sin_port ||
1557 			    inp->inp_faddr.s_addr != sin->sin_addr.s_addr)) {
1558 				/*
1559 				 * Socket is connected but socket state and dest addr/port changed.
1560 				 * We need to use the saved faddr info.
1561 				 */
1562 				cfil_faddr_use = true;
1563 			}
1564 		}
1565 	}
1566 #endif
1567 
1568 	if (control != NULL) {
1569 		tos = so_tos_from_control(control);
1570 		sotc = so_tc_from_control(control, &netsvctype);
1571 		VERIFY(outif == NULL);
1572 		error = udp_check_pktinfo(control, &outif, &pi_laddr);
1573 		m_freem(control);
1574 		control = NULL;
1575 		if (error) {
1576 			goto release;
1577 		}
1578 		if (outif != NULL) {
1579 			pktinfo++;
1580 			ipoa.ipoa_boundif = outif->if_index;
1581 		}
1582 	}
1583 	if (sotc == SO_TC_UNSPEC) {
1584 		sotc = so->so_traffic_class;
1585 		netsvctype = so->so_netsvctype;
1586 	}
1587 
1588 	KERNEL_DEBUG(DBG_LAYER_OUT_BEG, inp->inp_fport, inp->inp_lport,
1589 	    inp->inp_laddr.s_addr, inp->inp_faddr.s_addr,
1590 	    (htons((u_short)len + sizeof(struct udphdr))));
1591 
1592 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1593 		error = EMSGSIZE;
1594 		goto release;
1595 	}
1596 
1597 	if (flowadv && INP_WAIT_FOR_IF_FEEDBACK(inp)) {
1598 		/*
1599 		 * The socket is flow-controlled, drop the packets
1600 		 * until the inp is not flow controlled
1601 		 */
1602 		error = ENOBUFS;
1603 		goto release;
1604 	}
1605 	/*
1606 	 * If socket was bound to an ifindex, tell ip_output about it.
1607 	 * If the ancillary IP_PKTINFO option contains an interface index,
1608 	 * it takes precedence over the one specified by IP_BOUND_IF.
1609 	 */
1610 	if (ipoa.ipoa_boundif == IFSCOPE_NONE &&
1611 	    (inp->inp_flags & INP_BOUND_IF)) {
1612 		VERIFY(inp->inp_boundifp != NULL);
1613 		ifnet_reference(inp->inp_boundifp);     /* for this routine */
1614 		if (outif != NULL) {
1615 			ifnet_release(outif);
1616 		}
1617 		outif = inp->inp_boundifp;
1618 		ipoa.ipoa_boundif = outif->if_index;
1619 	}
1620 	if (INP_NO_CELLULAR(inp)) {
1621 		ipoa.ipoa_flags |=  IPOAF_NO_CELLULAR;
1622 	}
1623 	if (INP_NO_EXPENSIVE(inp)) {
1624 		ipoa.ipoa_flags |=  IPOAF_NO_EXPENSIVE;
1625 	}
1626 	if (INP_NO_CONSTRAINED(inp)) {
1627 		ipoa.ipoa_flags |=  IPOAF_NO_CONSTRAINED;
1628 	}
1629 	if (INP_AWDL_UNRESTRICTED(inp)) {
1630 		ipoa.ipoa_flags |=  IPOAF_AWDL_UNRESTRICTED;
1631 	}
1632 	if (INP_MANAGEMENT_ALLOWED(inp)) {
1633 		ipoa.ipoa_flags |= IPOAF_MANAGEMENT_ALLOWED;
1634 	}
1635 	ipoa.ipoa_sotc = sotc;
1636 	ipoa.ipoa_netsvctype = netsvctype;
1637 	soopts |= IP_OUTARGS;
1638 
1639 	/*
1640 	 * If there was a routing change, discard cached route and check
1641 	 * that we have a valid source address.  Reacquire a new source
1642 	 * address if INADDR_ANY was specified.
1643 	 *
1644 	 * If we are using cfil saved state, go through this cache cleanup
1645 	 * so that we can get a new route.
1646 	 */
1647 	if (ROUTE_UNUSABLE(&inp->inp_route)
1648 #if CONTENT_FILTER
1649 	    || cfil_faddr_use
1650 #endif
1651 	    ) {
1652 		struct in_ifaddr *ia = NULL;
1653 
1654 		ROUTE_RELEASE(&inp->inp_route);
1655 
1656 		/* src address is gone? */
1657 		if (inp->inp_laddr.s_addr != INADDR_ANY &&
1658 		    (ia = ifa_foraddr(inp->inp_laddr.s_addr)) == NULL) {
1659 			if (!(inp->inp_flags & INP_INADDR_ANY) ||
1660 			    (so->so_state & SS_ISCONNECTED)) {
1661 				/*
1662 				 * Rdar://5448998
1663 				 * If the source address is gone, return an
1664 				 * error if:
1665 				 * - the source was specified
1666 				 * - the socket was already connected
1667 				 */
1668 				soevent(so, (SO_FILT_HINT_LOCKED |
1669 				    SO_FILT_HINT_NOSRCADDR));
1670 				error = EADDRNOTAVAIL;
1671 				goto release;
1672 			} else {
1673 				/* new src will be set later */
1674 				inp->inp_laddr.s_addr = INADDR_ANY;
1675 				inp->inp_last_outifp = NULL;
1676 #if SKYWALK
1677 				if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
1678 					netns_set_ifnet(&inp->inp_netns_token, NULL);
1679 				}
1680 #endif /* SKYWALK */
1681 			}
1682 		}
1683 		if (ia != NULL) {
1684 			IFA_REMREF(&ia->ia_ifa);
1685 		}
1686 	}
1687 
1688 	/*
1689 	 * IP_PKTINFO option check.  If a temporary scope or src address
1690 	 * is provided, use it for this packet only and make sure we forget
1691 	 * it after sending this datagram.
1692 	 */
1693 	if (pi_laddr.s_addr != INADDR_ANY ||
1694 	    (ipoa.ipoa_boundif != IFSCOPE_NONE && pktinfo)) {
1695 		/* temp src address for this datagram only */
1696 		laddr = pi_laddr;
1697 		origladdr.s_addr = INADDR_ANY;
1698 		/* we don't want to keep the laddr or route */
1699 		udp_dodisconnect = 1;
1700 		/* remember we don't care about src addr */
1701 		inp->inp_flags |= INP_INADDR_ANY;
1702 	} else {
1703 		origladdr = laddr = inp->inp_laddr;
1704 	}
1705 
1706 	origoutifp = inp->inp_last_outifp;
1707 	faddr = inp->inp_faddr;
1708 	lport = inp->inp_lport;
1709 	fport = inp->inp_fport;
1710 
1711 #if CONTENT_FILTER
1712 	if (cfil_faddr_use) {
1713 		faddr = ((struct sockaddr_in *)(void *)cfil_faddr)->sin_addr;
1714 		fport = ((struct sockaddr_in *)(void *)cfil_faddr)->sin_port;
1715 	}
1716 #endif
1717 	inp->inp_sndinprog_cnt++;
1718 	sndinprog_cnt_used = true;
1719 
1720 	if (addr) {
1721 		sin = (struct sockaddr_in *)(void *)addr;
1722 		if (faddr.s_addr != INADDR_ANY) {
1723 			error = EISCONN;
1724 			goto release;
1725 		}
1726 		if (lport == 0) {
1727 			/*
1728 			 * In case we don't have a local port set, go through
1729 			 * the full connect.  We don't have a local port yet
1730 			 * (i.e., we can't be looked up), so it's not an issue
1731 			 * if the input runs at the same time we do this.
1732 			 */
1733 			/* if we have a source address specified, use that */
1734 			if (pi_laddr.s_addr != INADDR_ANY) {
1735 				inp->inp_laddr = pi_laddr;
1736 			}
1737 			/*
1738 			 * If a scope is specified, use it.  Scope from
1739 			 * IP_PKTINFO takes precendence over the the scope
1740 			 * set via INP_BOUND_IF.
1741 			 */
1742 			error = in_pcbconnect(inp, addr, p, ipoa.ipoa_boundif,
1743 			    &outif);
1744 			if (error) {
1745 				goto release;
1746 			}
1747 
1748 			laddr = inp->inp_laddr;
1749 			lport = inp->inp_lport;
1750 			faddr = inp->inp_faddr;
1751 			fport = inp->inp_fport;
1752 			udp_dodisconnect = 1;
1753 
1754 			/* synch up in case in_pcbladdr() overrides */
1755 			if (outif != NULL && ipoa.ipoa_boundif != IFSCOPE_NONE) {
1756 				ipoa.ipoa_boundif = outif->if_index;
1757 			}
1758 		} else {
1759 			/*
1760 			 * Fast path case
1761 			 *
1762 			 * We have a full address and a local port; use those
1763 			 * info to build the packet without changing the pcb
1764 			 * and interfering with the input path. See 3851370.
1765 			 *
1766 			 * Scope from IP_PKTINFO takes precendence over the
1767 			 * the scope set via INP_BOUND_IF.
1768 			 */
1769 			if (laddr.s_addr == INADDR_ANY) {
1770 				if ((error = in_pcbladdr(inp, addr, &laddr,
1771 				    ipoa.ipoa_boundif, &outif, 0)) != 0) {
1772 					goto release;
1773 				}
1774 				/*
1775 				 * from pcbconnect: remember we don't
1776 				 * care about src addr.
1777 				 */
1778 				inp->inp_flags |= INP_INADDR_ANY;
1779 
1780 				/* synch up in case in_pcbladdr() overrides */
1781 				if (outif != NULL &&
1782 				    ipoa.ipoa_boundif != IFSCOPE_NONE) {
1783 					ipoa.ipoa_boundif = outif->if_index;
1784 				}
1785 			}
1786 
1787 			faddr = sin->sin_addr;
1788 			fport = sin->sin_port;
1789 		}
1790 	} else {
1791 		if (faddr.s_addr == INADDR_ANY) {
1792 			error = ENOTCONN;
1793 			goto release;
1794 		}
1795 	}
1796 
1797 	if (inp->inp_flowhash == 0) {
1798 		inp_calc_flowhash(inp);
1799 		ASSERT(inp->inp_flowhash != 0);
1800 	}
1801 
1802 	if (fport == htons(53) && !(so->so_flags1 & SOF1_DNS_COUNTED)) {
1803 		so->so_flags1 |= SOF1_DNS_COUNTED;
1804 		INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_dns);
1805 	}
1806 
1807 	/*
1808 	 * Calculate data length and get a mbuf
1809 	 * for UDP and IP headers.
1810 	 */
1811 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT, 1);
1812 	if (m == 0) {
1813 		error = ENOBUFS;
1814 		goto abort;
1815 	}
1816 
1817 	/*
1818 	 * Fill in mbuf with extended UDP header
1819 	 * and addresses and length put into network format.
1820 	 */
1821 	ui = mtod(m, struct udpiphdr *);
1822 	bzero(ui->ui_x1, sizeof(ui->ui_x1));    /* XXX still needed? */
1823 	ui->ui_pr = IPPROTO_UDP;
1824 	ui->ui_src = laddr;
1825 	ui->ui_dst = faddr;
1826 	ui->ui_sport = lport;
1827 	ui->ui_dport = fport;
1828 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1829 
1830 	/*
1831 	 * Set the Don't Fragment bit in the IP header.
1832 	 */
1833 	if (inp->inp_flags2 & INP2_DONTFRAG) {
1834 		struct ip *ip;
1835 
1836 		ip = (struct ip *)&ui->ui_i;
1837 		ip->ip_off |= IP_DF;
1838 	}
1839 
1840 	/*
1841 	 * Set up checksum to pseudo header checksum and output datagram.
1842 	 *
1843 	 * Treat flows to be CLAT46'd as IPv6 flow and compute checksum
1844 	 * no matter what, as IPv6 mandates checksum for UDP.
1845 	 *
1846 	 * Here we only compute the one's complement sum of the pseudo header.
1847 	 * The payload computation and final complement is delayed to much later
1848 	 * in IP processing to decide if remaining computation needs to be done
1849 	 * through offload.
1850 	 *
1851 	 * That is communicated by setting CSUM_UDP in csum_flags.
1852 	 * The offset of checksum from the start of ULP header is communicated
1853 	 * through csum_data.
1854 	 *
1855 	 * Note since this already contains the pseudo checksum header, any
1856 	 * later operation at IP layer that modify the values used here must
1857 	 * update the checksum as well (for example NAT etc).
1858 	 */
1859 	if ((inp->inp_flags2 & INP2_CLAT46_FLOW) ||
1860 	    (udpcksum && !(inp->inp_flags & INP_UDP_NOCKSUM))) {
1861 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
1862 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
1863 		m->m_pkthdr.csum_flags = (CSUM_UDP | CSUM_ZERO_INVERT);
1864 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1865 	} else {
1866 		ui->ui_sum = 0;
1867 	}
1868 	((struct ip *)ui)->ip_len = (uint16_t)(sizeof(struct udpiphdr) + len);
1869 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;    /* XXX */
1870 	if (tos != IPTOS_UNSPEC) {
1871 		((struct ip *)ui)->ip_tos = (uint8_t)(tos & IPTOS_MASK);
1872 	} else {
1873 		((struct ip *)ui)->ip_tos = inp->inp_ip_tos;    /* XXX */
1874 	}
1875 	udpstat.udps_opackets++;
1876 
1877 	KERNEL_DEBUG(DBG_LAYER_OUT_END, ui->ui_dport, ui->ui_sport,
1878 	    ui->ui_src.s_addr, ui->ui_dst.s_addr, ui->ui_ulen);
1879 
1880 #if NECP
1881 	{
1882 		necp_kernel_policy_id policy_id;
1883 		necp_kernel_policy_id skip_policy_id;
1884 		u_int32_t route_rule_id;
1885 		u_int32_t pass_flags;
1886 
1887 		/*
1888 		 * We need a route to perform NECP route rule checks
1889 		 */
1890 		if (net_qos_policy_restricted != 0 &&
1891 		    ROUTE_UNUSABLE(&inp->inp_route)) {
1892 			struct sockaddr_in to;
1893 			struct sockaddr_in from;
1894 
1895 			ROUTE_RELEASE(&inp->inp_route);
1896 
1897 			bzero(&from, sizeof(struct sockaddr_in));
1898 			from.sin_family = AF_INET;
1899 			from.sin_len = sizeof(struct sockaddr_in);
1900 			from.sin_addr = laddr;
1901 
1902 			bzero(&to, sizeof(struct sockaddr_in));
1903 			to.sin_family = AF_INET;
1904 			to.sin_len = sizeof(struct sockaddr_in);
1905 			to.sin_addr = faddr;
1906 
1907 			inp->inp_route.ro_dst.sa_family = AF_INET;
1908 			inp->inp_route.ro_dst.sa_len = sizeof(struct sockaddr_in);
1909 			((struct sockaddr_in *)(void *)&inp->inp_route.ro_dst)->sin_addr =
1910 			    faddr;
1911 
1912 			rtalloc_scoped(&inp->inp_route, ipoa.ipoa_boundif);
1913 
1914 			inp_update_necp_policy(inp, (struct sockaddr *)&from,
1915 			    (struct sockaddr *)&to, ipoa.ipoa_boundif);
1916 			inp->inp_policyresult.results.qos_marking_gencount = 0;
1917 		}
1918 
1919 		if (!necp_socket_is_allowed_to_send_recv_v4(inp, lport, fport,
1920 		    &laddr, &faddr, NULL, 0, &policy_id, &route_rule_id, &skip_policy_id, &pass_flags)) {
1921 			error = EHOSTUNREACH;
1922 			goto abort;
1923 		}
1924 
1925 		necp_mark_packet_from_socket(m, inp, policy_id, route_rule_id, skip_policy_id, pass_flags);
1926 
1927 		if (net_qos_policy_restricted != 0) {
1928 			necp_socket_update_qos_marking(inp, inp->inp_route.ro_rt, route_rule_id);
1929 		}
1930 	}
1931 #endif /* NECP */
1932 	if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) {
1933 		ipoa.ipoa_flags |= IPOAF_QOSMARKING_ALLOWED;
1934 	}
1935 	if (check_qos_marking_again) {
1936 		ipoa.ipoa_flags |= IPOAF_REDO_QOSMARKING_POLICY;
1937 	}
1938 	ipoa.qos_marking_gencount = inp->inp_policyresult.results.qos_marking_gencount;
1939 
1940 #if IPSEC
1941 	if (inp->inp_sp != NULL && ipsec_setsocket(m, inp->inp_socket) != 0) {
1942 		error = ENOBUFS;
1943 		goto abort;
1944 	}
1945 #endif /* IPSEC */
1946 
1947 	inpopts = inp->inp_options;
1948 #if CONTENT_FILTER
1949 	if (cfil_tag && (inp->inp_socket->so_options != cfil_so_options)) {
1950 		soopts |= (cfil_so_options & (SO_DONTROUTE | SO_BROADCAST));
1951 	} else
1952 #endif
1953 	soopts |= (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST));
1954 
1955 	mopts = inp->inp_moptions;
1956 	if (mopts != NULL) {
1957 		IMO_LOCK(mopts);
1958 		IMO_ADDREF_LOCKED(mopts);
1959 		if (IN_MULTICAST(ntohl(ui->ui_dst.s_addr)) &&
1960 		    mopts->imo_multicast_ifp != NULL) {
1961 			/* no reference needed */
1962 			inp->inp_last_outifp = mopts->imo_multicast_ifp;
1963 #if SKYWALK
1964 			if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
1965 				netns_set_ifnet(&inp->inp_netns_token,
1966 				    inp->inp_last_outifp);
1967 			}
1968 #endif /* SKYWALK */
1969 		}
1970 		IMO_UNLOCK(mopts);
1971 	}
1972 
1973 	/* Copy the cached route and take an extra reference */
1974 	inp_route_copyout(inp, &ro);
1975 
1976 	set_packet_service_class(m, so, sotc, 0);
1977 	m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
1978 	m->m_pkthdr.pkt_flowid = inp->inp_flowhash;
1979 	m->m_pkthdr.pkt_proto = IPPROTO_UDP;
1980 	m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC);
1981 	if (flowadv) {
1982 		m->m_pkthdr.pkt_flags |= PKTF_FLOW_ADV;
1983 	}
1984 	m->m_pkthdr.tx_udp_pid = so->last_pid;
1985 	if (so->so_flags & SOF_DELEGATED) {
1986 		m->m_pkthdr.tx_udp_e_pid = so->e_pid;
1987 	} else {
1988 		m->m_pkthdr.tx_udp_e_pid = 0;
1989 	}
1990 #if (DEBUG || DEVELOPMENT)
1991 	if (so->so_flags & SOF_MARK_WAKE_PKT) {
1992 		so->so_flags &= ~SOF_MARK_WAKE_PKT;
1993 		m->m_pkthdr.pkt_flags |= PKTF_WAKE_PKT;
1994 	}
1995 #endif /* (DEBUG || DEVELOPMENT) */
1996 
1997 	m_add_crumb(m, PKT_CRUMB_UDP_OUTPUT);
1998 
1999 	if (ipoa.ipoa_boundif != IFSCOPE_NONE) {
2000 		ipoa.ipoa_flags |= IPOAF_BOUND_IF;
2001 	}
2002 
2003 	if (laddr.s_addr != INADDR_ANY) {
2004 		ipoa.ipoa_flags |= IPOAF_BOUND_SRCADDR;
2005 	}
2006 
2007 	socket_unlock(so, 0);
2008 	error = ip_output(m, inpopts, &ro, soopts, mopts, &ipoa);
2009 	m = NULL;
2010 	socket_lock(so, 0);
2011 	if (mopts != NULL) {
2012 		IMO_REMREF(mopts);
2013 	}
2014 
2015 	if (check_qos_marking_again) {
2016 		inp->inp_policyresult.results.qos_marking_gencount = ipoa.qos_marking_gencount;
2017 
2018 		if (ipoa.ipoa_flags & IPOAF_QOSMARKING_ALLOWED) {
2019 			inp->inp_socket->so_flags1 |= SOF1_QOSMARKING_ALLOWED;
2020 		} else {
2021 			inp->inp_socket->so_flags1 &= ~SOF1_QOSMARKING_ALLOWED;
2022 		}
2023 	}
2024 
2025 	if (error == 0 && nstat_collect) {
2026 		boolean_t cell, wifi, wired;
2027 
2028 		if (ro.ro_rt != NULL) {
2029 			cell = IFNET_IS_CELLULAR(ro.ro_rt->rt_ifp);
2030 			wifi = (!cell && IFNET_IS_WIFI(ro.ro_rt->rt_ifp));
2031 			wired = (!wifi && IFNET_IS_WIRED(ro.ro_rt->rt_ifp));
2032 		} else {
2033 			cell = wifi = wired = FALSE;
2034 		}
2035 		INP_ADD_STAT(inp, cell, wifi, wired, txpackets, 1);
2036 		INP_ADD_STAT(inp, cell, wifi, wired, txbytes, len);
2037 		inp_set_activity_bitmap(inp);
2038 	}
2039 
2040 	if (flowadv && (adv->code == FADV_FLOW_CONTROLLED ||
2041 	    adv->code == FADV_SUSPENDED)) {
2042 		/*
2043 		 * return a hint to the application that
2044 		 * the packet has been dropped
2045 		 */
2046 		error = ENOBUFS;
2047 		inp_set_fc_state(inp, adv->code);
2048 	}
2049 
2050 	/* Synchronize PCB cached route */
2051 	inp_route_copyin(inp, &ro);
2052 
2053 	if (inp->inp_route.ro_rt != NULL) {
2054 		if (IS_LOCALNET_ROUTE(inp->inp_route.ro_rt)) {
2055 			inp->inp_flags2 |= INP2_LAST_ROUTE_LOCAL;
2056 		} else {
2057 			inp->inp_flags2 &= ~INP2_LAST_ROUTE_LOCAL;
2058 		}
2059 	}
2060 
2061 abort:
2062 	if (udp_dodisconnect) {
2063 		/* Always discard the cached route for unconnected socket */
2064 		ROUTE_RELEASE(&inp->inp_route);
2065 		in_pcbdisconnect(inp);
2066 		inp->inp_laddr = origladdr;     /* XXX rehash? */
2067 		/* no reference needed */
2068 		inp->inp_last_outifp = origoutifp;
2069 #if SKYWALK
2070 		if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
2071 			netns_set_ifnet(&inp->inp_netns_token,
2072 			    inp->inp_last_outifp);
2073 		}
2074 #endif /* SKYWALK */
2075 	} else if (inp->inp_route.ro_rt != NULL) {
2076 		struct rtentry *rt = inp->inp_route.ro_rt;
2077 		struct ifnet *outifp;
2078 
2079 		if (rt->rt_flags & (RTF_MULTICAST | RTF_BROADCAST)) {
2080 			rt = NULL;      /* unusable */
2081 		}
2082 #if CONTENT_FILTER
2083 		/*
2084 		 * Discard temporary route for cfil case
2085 		 */
2086 		if (cfil_faddr_use) {
2087 			rt = NULL;      /* unusable */
2088 		}
2089 #endif
2090 
2091 		/*
2092 		 * Always discard if it is a multicast or broadcast route.
2093 		 */
2094 		if (rt == NULL) {
2095 			ROUTE_RELEASE(&inp->inp_route);
2096 		}
2097 
2098 		/*
2099 		 * If the destination route is unicast, update outifp with
2100 		 * that of the route interface used by IP.
2101 		 */
2102 		if (rt != NULL &&
2103 		    (outifp = rt->rt_ifp) != inp->inp_last_outifp) {
2104 			inp->inp_last_outifp = outifp; /* no reference needed */
2105 #if SKYWALK
2106 			if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
2107 				netns_set_ifnet(&inp->inp_netns_token,
2108 				    inp->inp_last_outifp);
2109 			}
2110 #endif /* SKYWALK */
2111 
2112 			so->so_pktheadroom = (uint16_t)P2ROUNDUP(
2113 				sizeof(struct udphdr) +
2114 				sizeof(struct ip) +
2115 				ifnet_hdrlen(outifp) +
2116 				ifnet_mbuf_packetpreamblelen(outifp),
2117 				sizeof(u_int32_t));
2118 		}
2119 	} else {
2120 		ROUTE_RELEASE(&inp->inp_route);
2121 	}
2122 
2123 	/*
2124 	 * If output interface was cellular/expensive, and this socket is
2125 	 * denied access to it, generate an event.
2126 	 */
2127 	if (error != 0 && (ipoa.ipoa_flags & IPOAF_R_IFDENIED) &&
2128 	    (INP_NO_CELLULAR(inp) || INP_NO_EXPENSIVE(inp) || INP_NO_CONSTRAINED(inp))) {
2129 		soevent(so, (SO_FILT_HINT_LOCKED | SO_FILT_HINT_IFDENIED));
2130 	}
2131 
2132 release:
2133 	KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_END, error, 0, 0, 0, 0);
2134 
2135 	if (m != NULL) {
2136 		m_freem(m);
2137 	}
2138 
2139 	if (outif != NULL) {
2140 		ifnet_release(outif);
2141 	}
2142 
2143 #if CONTENT_FILTER
2144 	if (cfil_tag) {
2145 		m_tag_free(cfil_tag);
2146 	}
2147 #endif
2148 	if (sndinprog_cnt_used) {
2149 		VERIFY(inp->inp_sndinprog_cnt > 0);
2150 		if (--inp->inp_sndinprog_cnt == 0) {
2151 			inp->inp_flags &= ~(INP_FC_FEEDBACK);
2152 			if (inp->inp_sndingprog_waiters > 0) {
2153 				wakeup(&inp->inp_sndinprog_cnt);
2154 			}
2155 		}
2156 		sndinprog_cnt_used = false;
2157 	}
2158 
2159 	return error;
2160 }
2161 
2162 u_int32_t       udp_sendspace = 9216;           /* really max datagram size */
2163 /* 187 1K datagrams (approx 192 KB) */
2164 u_int32_t       udp_recvspace = 187 * (1024 + sizeof(struct sockaddr_in6));
2165 
2166 /* Check that the values of udp send and recv space do not exceed sb_max */
2167 static int
sysctl_udp_sospace(struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)2168 sysctl_udp_sospace(struct sysctl_oid *oidp, void *arg1, int arg2,
2169     struct sysctl_req *req)
2170 {
2171 #pragma unused(arg1, arg2)
2172 	u_int32_t new_value = 0, *space_p = NULL;
2173 	int changed = 0, error = 0;
2174 	u_quad_t sb_effective_max = (sb_max / (SB_MSIZE_ADJ + MCLBYTES)) * MCLBYTES;
2175 
2176 	switch (oidp->oid_number) {
2177 	case UDPCTL_RECVSPACE:
2178 		space_p = &udp_recvspace;
2179 		break;
2180 	case UDPCTL_MAXDGRAM:
2181 		space_p = &udp_sendspace;
2182 		break;
2183 	default:
2184 		return EINVAL;
2185 	}
2186 	error = sysctl_io_number(req, *space_p, sizeof(u_int32_t),
2187 	    &new_value, &changed);
2188 	if (changed) {
2189 		if (new_value > 0 && new_value <= sb_effective_max) {
2190 			*space_p = new_value;
2191 		} else {
2192 			error = ERANGE;
2193 		}
2194 	}
2195 	return error;
2196 }
2197 
2198 SYSCTL_PROC(_net_inet_udp, UDPCTL_RECVSPACE, recvspace,
2199     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &udp_recvspace, 0,
2200     &sysctl_udp_sospace, "IU", "Maximum incoming UDP datagram size");
2201 
2202 SYSCTL_PROC(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram,
2203     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &udp_sendspace, 0,
2204     &sysctl_udp_sospace, "IU", "Maximum outgoing UDP datagram size");
2205 
2206 int
udp_abort(struct socket * so)2207 udp_abort(struct socket *so)
2208 {
2209 	struct inpcb *inp;
2210 
2211 	inp = sotoinpcb(so);
2212 	if (inp == NULL) {
2213 		panic("%s: so=%p null inp", __func__, so);
2214 		/* NOTREACHED */
2215 	}
2216 	soisdisconnected(so);
2217 	in_pcbdetach(inp);
2218 	return 0;
2219 }
2220 
2221 int
udp_attach(struct socket * so,int proto,struct proc * p)2222 udp_attach(struct socket *so, int proto, struct proc *p)
2223 {
2224 #pragma unused(proto)
2225 	struct inpcb *inp;
2226 	int error;
2227 
2228 	error = soreserve(so, udp_sendspace, udp_recvspace);
2229 	if (error != 0) {
2230 		return error;
2231 	}
2232 	inp = sotoinpcb(so);
2233 	if (inp != NULL) {
2234 		panic("%s so=%p inp=%p", __func__, so, inp);
2235 		/* NOTREACHED */
2236 	}
2237 	error = in_pcballoc(so, &udbinfo, p);
2238 	if (error != 0) {
2239 		return error;
2240 	}
2241 	inp = (struct inpcb *)so->so_pcb;
2242 	inp->inp_vflag |= INP_IPV4;
2243 	inp->inp_ip_ttl = (uint8_t)ip_defttl;
2244 	if (nstat_collect) {
2245 		nstat_udp_new_pcb(inp);
2246 	}
2247 	return 0;
2248 }
2249 
2250 int
udp_bind(struct socket * so,struct sockaddr * nam,struct proc * p)2251 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
2252 {
2253 	struct inpcb *inp;
2254 	int error;
2255 
2256 	if (nam->sa_family != 0 && nam->sa_family != AF_INET &&
2257 	    nam->sa_family != AF_INET6) {
2258 		return EAFNOSUPPORT;
2259 	}
2260 
2261 	inp = sotoinpcb(so);
2262 	if (inp == NULL) {
2263 		return EINVAL;
2264 	}
2265 	error = in_pcbbind(inp, nam, p);
2266 
2267 #if NECP
2268 	/* Update NECP client with bind result if not in middle of connect */
2269 	if (error == 0 &&
2270 	    (inp->inp_flags2 & INP2_CONNECT_IN_PROGRESS) &&
2271 	    !uuid_is_null(inp->necp_client_uuid)) {
2272 		socket_unlock(so, 0);
2273 		necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
2274 		socket_lock(so, 0);
2275 	}
2276 #endif /* NECP */
2277 
2278 	UDP_LOG_BIND(inp, error);
2279 
2280 	return error;
2281 }
2282 
2283 int
udp_connect(struct socket * so,struct sockaddr * nam,struct proc * p)2284 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
2285 {
2286 	struct inpcb *inp;
2287 	int error;
2288 
2289 	inp = sotoinpcb(so);
2290 	if (inp == NULL) {
2291 		return EINVAL;
2292 	}
2293 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
2294 		return EISCONN;
2295 	}
2296 
2297 	if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) {
2298 		so->so_flags1 |= SOF1_CONNECT_COUNTED;
2299 		INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_connected);
2300 	}
2301 
2302 #if NECP
2303 #if FLOW_DIVERT
2304 	if (necp_socket_should_use_flow_divert(inp)) {
2305 		error = flow_divert_pcb_init(so);
2306 		if (error == 0) {
2307 			error = flow_divert_connect_out(so, nam, p);
2308 		}
2309 		UDP_LOG_CONNECT(inp, error);
2310 		return error;
2311 	} else {
2312 		so->so_flags1 |= SOF1_FLOW_DIVERT_SKIP;
2313 	}
2314 #endif /* FLOW_DIVERT */
2315 #endif /* NECP */
2316 
2317 	error = in_pcbconnect(inp, nam, p, IFSCOPE_NONE, NULL);
2318 	if (error == 0) {
2319 #if NECP
2320 		/* Update NECP client with connected five-tuple */
2321 		if (!uuid_is_null(inp->necp_client_uuid)) {
2322 			socket_unlock(so, 0);
2323 			necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
2324 			socket_lock(so, 0);
2325 		}
2326 #endif /* NECP */
2327 
2328 		soisconnected(so);
2329 		if (inp->inp_flowhash == 0) {
2330 			inp_calc_flowhash(inp);
2331 			ASSERT(inp->inp_flowhash != 0);
2332 		}
2333 		inp->inp_connect_timestamp = mach_continuous_time();
2334 	}
2335 	UDP_LOG_CONNECT(inp, error);
2336 	return error;
2337 }
2338 
2339 int
udp_connectx_common(struct socket * so,int af,struct sockaddr * src,struct sockaddr * dst,struct proc * p,uint32_t ifscope,sae_associd_t aid,sae_connid_t * pcid,uint32_t flags,void * arg,uint32_t arglen,struct uio * uio,user_ssize_t * bytes_written)2340 udp_connectx_common(struct socket *so, int af, struct sockaddr *src, struct sockaddr *dst,
2341     struct proc *p, uint32_t ifscope, sae_associd_t aid, sae_connid_t *pcid,
2342     uint32_t flags, void *arg, uint32_t arglen,
2343     struct uio *uio, user_ssize_t *bytes_written)
2344 {
2345 #pragma unused(aid, flags, arg, arglen)
2346 	struct inpcb *inp = sotoinpcb(so);
2347 	int error = 0;
2348 	user_ssize_t datalen = 0;
2349 
2350 	if (inp == NULL) {
2351 		return EINVAL;
2352 	}
2353 
2354 	VERIFY(dst != NULL);
2355 
2356 	ASSERT(!(inp->inp_flags2 & INP2_CONNECT_IN_PROGRESS));
2357 	inp->inp_flags2 |= INP2_CONNECT_IN_PROGRESS;
2358 
2359 #if NECP
2360 	inp_update_necp_policy(inp, src, dst, ifscope);
2361 #endif /* NECP */
2362 
2363 	/* bind socket to the specified interface, if requested */
2364 	if (ifscope != IFSCOPE_NONE &&
2365 	    (error = inp_bindif(inp, ifscope, NULL)) != 0) {
2366 		goto done;
2367 	}
2368 
2369 	/* if source address and/or port is specified, bind to it */
2370 	if (src != NULL) {
2371 		error = sobindlock(so, src, 0); /* already locked */
2372 		if (error != 0) {
2373 			goto done;
2374 		}
2375 	}
2376 
2377 	switch (af) {
2378 	case AF_INET:
2379 		error = udp_connect(so, dst, p);
2380 		break;
2381 	case AF_INET6:
2382 		error = udp6_connect(so, dst, p);
2383 		break;
2384 	default:
2385 		VERIFY(0);
2386 		/* NOTREACHED */
2387 	}
2388 
2389 	if (error != 0) {
2390 		goto done;
2391 	}
2392 
2393 	/*
2394 	 * If there is data, copy it. DATA_IDEMPOTENT is ignored.
2395 	 * CONNECT_RESUME_ON_READ_WRITE is ignored.
2396 	 */
2397 	if (uio != NULL) {
2398 		socket_unlock(so, 0);
2399 
2400 		VERIFY(bytes_written != NULL);
2401 
2402 		datalen = uio_resid(uio);
2403 		error = so->so_proto->pr_usrreqs->pru_sosend(so, NULL,
2404 		    (uio_t)uio, NULL, NULL, 0);
2405 		socket_lock(so, 0);
2406 
2407 		/* If error returned is EMSGSIZE, for example, disconnect */
2408 		if (error == 0 || error == EWOULDBLOCK) {
2409 			*bytes_written = datalen - uio_resid(uio);
2410 		} else {
2411 			(void) so->so_proto->pr_usrreqs->pru_disconnectx(so,
2412 			    SAE_ASSOCID_ANY, SAE_CONNID_ANY);
2413 		}
2414 		/*
2415 		 * mask the EWOULDBLOCK error so that the caller
2416 		 * knows that atleast the connect was successful.
2417 		 */
2418 		if (error == EWOULDBLOCK) {
2419 			error = 0;
2420 		}
2421 	}
2422 
2423 	if (error == 0 && pcid != NULL) {
2424 		*pcid = 1;      /* there is only 1 connection for UDP */
2425 	}
2426 done:
2427 	inp->inp_flags2 &= ~INP2_CONNECT_IN_PROGRESS;
2428 	return error;
2429 }
2430 
2431 int
udp_connectx(struct socket * so,struct sockaddr * src,struct sockaddr * dst,struct proc * p,uint32_t ifscope,sae_associd_t aid,sae_connid_t * pcid,uint32_t flags,void * arg,uint32_t arglen,struct uio * uio,user_ssize_t * bytes_written)2432 udp_connectx(struct socket *so, struct sockaddr *src,
2433     struct sockaddr *dst, struct proc *p, uint32_t ifscope,
2434     sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg,
2435     uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written)
2436 {
2437 	return udp_connectx_common(so, AF_INET, src, dst,
2438 	           p, ifscope, aid, pcid, flags, arg, arglen, uio, bytes_written);
2439 }
2440 
2441 int
udp_detach(struct socket * so)2442 udp_detach(struct socket *so)
2443 {
2444 	struct inpcb *inp;
2445 
2446 	inp = sotoinpcb(so);
2447 	if (inp == NULL) {
2448 		panic("%s: so=%p null inp", __func__, so);
2449 		/* NOTREACHED */
2450 	}
2451 
2452 	/*
2453 	 * If this is a socket that does not want to wakeup the device
2454 	 * for it's traffic, the application might be waiting for
2455 	 * close to complete before going to sleep. Send a notification
2456 	 * for this kind of sockets
2457 	 */
2458 	if (so->so_options & SO_NOWAKEFROMSLEEP) {
2459 		socket_post_kev_msg_closed(so);
2460 	}
2461 
2462 	UDP_LOG_CONNECTION_SUMMARY(inp);
2463 
2464 	in_pcbdetach(inp);
2465 	inp->inp_state = INPCB_STATE_DEAD;
2466 	return 0;
2467 }
2468 
2469 int
udp_disconnect(struct socket * so)2470 udp_disconnect(struct socket *so)
2471 {
2472 	struct inpcb *inp;
2473 
2474 	inp = sotoinpcb(so);
2475 	if (inp == NULL) {
2476 		return EINVAL;
2477 	}
2478 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
2479 		return ENOTCONN;
2480 	}
2481 
2482 	UDP_LOG_CONNECTION_SUMMARY(inp);
2483 
2484 	in_pcbdisconnect(inp);
2485 
2486 	/* reset flow controlled state, just in case */
2487 	inp_reset_fc_state(inp);
2488 
2489 	inp->inp_laddr.s_addr = INADDR_ANY;
2490 	so->so_state &= ~SS_ISCONNECTED;                /* XXX */
2491 	inp->inp_last_outifp = NULL;
2492 #if SKYWALK
2493 	if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
2494 		netns_set_ifnet(&inp->inp_netns_token, NULL);
2495 	}
2496 #endif /* SKYWALK */
2497 
2498 	return 0;
2499 }
2500 
2501 int
udp_disconnectx(struct socket * so,sae_associd_t aid,sae_connid_t cid)2502 udp_disconnectx(struct socket *so, sae_associd_t aid, sae_connid_t cid)
2503 {
2504 #pragma unused(cid)
2505 	if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) {
2506 		return EINVAL;
2507 	}
2508 
2509 	return udp_disconnect(so);
2510 }
2511 
2512 int
udp_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct proc * p)2513 udp_send(struct socket *so, int flags, struct mbuf *m,
2514     struct sockaddr *addr, struct mbuf *control, struct proc *p)
2515 {
2516 #ifndef FLOW_DIVERT
2517 #pragma unused(flags)
2518 #endif /* !(FLOW_DIVERT) */
2519 	struct inpcb *inp;
2520 	int error;
2521 
2522 	inp = sotoinpcb(so);
2523 	if (inp == NULL) {
2524 		if (m != NULL) {
2525 			m_freem(m);
2526 		}
2527 		if (control != NULL) {
2528 			m_freem(control);
2529 		}
2530 		return EINVAL;
2531 	}
2532 
2533 #if NECP
2534 #if FLOW_DIVERT
2535 	if (necp_socket_should_use_flow_divert(inp)) {
2536 		/* Implicit connect */
2537 		return flow_divert_implicit_data_out(so, flags, m, addr,
2538 		           control, p);
2539 	} else {
2540 		so->so_flags1 |= SOF1_FLOW_DIVERT_SKIP;
2541 	}
2542 #endif /* FLOW_DIVERT */
2543 #endif /* NECP */
2544 
2545 #if SKYWALK
2546 	sk_protect_t protect = sk_async_transmit_protect();
2547 #endif /* SKYWALK */
2548 	error = udp_output(inp, m, addr, control, p);
2549 #if SKYWALK
2550 	sk_async_transmit_unprotect(protect);
2551 #endif /* SKYWALK */
2552 
2553 	return error;
2554 }
2555 
2556 int
udp_shutdown(struct socket * so)2557 udp_shutdown(struct socket *so)
2558 {
2559 	struct inpcb *inp;
2560 
2561 	inp = sotoinpcb(so);
2562 	if (inp == NULL) {
2563 		return EINVAL;
2564 	}
2565 	socantsendmore(so);
2566 	return 0;
2567 }
2568 
2569 int
udp_lock(struct socket * so,int refcount,void * debug)2570 udp_lock(struct socket *so, int refcount, void *debug)
2571 {
2572 	void *lr_saved;
2573 
2574 	if (debug == NULL) {
2575 		lr_saved = __builtin_return_address(0);
2576 	} else {
2577 		lr_saved = debug;
2578 	}
2579 
2580 	if (so->so_pcb != NULL) {
2581 		LCK_MTX_ASSERT(&((struct inpcb *)so->so_pcb)->inpcb_mtx,
2582 		    LCK_MTX_ASSERT_NOTOWNED);
2583 		lck_mtx_lock(&((struct inpcb *)so->so_pcb)->inpcb_mtx);
2584 	} else {
2585 		panic("%s: so=%p NO PCB! lr=%p lrh= %s", __func__,
2586 		    so, lr_saved, solockhistory_nr(so));
2587 		/* NOTREACHED */
2588 	}
2589 	if (refcount) {
2590 		so->so_usecount++;
2591 	}
2592 
2593 	so->lock_lr[so->next_lock_lr] = lr_saved;
2594 	so->next_lock_lr = (so->next_lock_lr + 1) % SO_LCKDBG_MAX;
2595 	return 0;
2596 }
2597 
2598 int
udp_unlock(struct socket * so,int refcount,void * debug)2599 udp_unlock(struct socket *so, int refcount, void *debug)
2600 {
2601 	void *lr_saved;
2602 
2603 	if (debug == NULL) {
2604 		lr_saved = __builtin_return_address(0);
2605 	} else {
2606 		lr_saved = debug;
2607 	}
2608 
2609 	if (refcount) {
2610 		VERIFY(so->so_usecount > 0);
2611 		so->so_usecount--;
2612 	}
2613 	if (so->so_pcb == NULL) {
2614 		panic("%s: so=%p NO PCB! lr=%p lrh= %s", __func__,
2615 		    so, lr_saved, solockhistory_nr(so));
2616 		/* NOTREACHED */
2617 	} else {
2618 		LCK_MTX_ASSERT(&((struct inpcb *)so->so_pcb)->inpcb_mtx,
2619 		    LCK_MTX_ASSERT_OWNED);
2620 		so->unlock_lr[so->next_unlock_lr] = lr_saved;
2621 		so->next_unlock_lr = (so->next_unlock_lr + 1) % SO_LCKDBG_MAX;
2622 		lck_mtx_unlock(&((struct inpcb *)so->so_pcb)->inpcb_mtx);
2623 	}
2624 	return 0;
2625 }
2626 
2627 lck_mtx_t *
udp_getlock(struct socket * so,int flags)2628 udp_getlock(struct socket *so, int flags)
2629 {
2630 #pragma unused(flags)
2631 	struct inpcb *inp = sotoinpcb(so);
2632 
2633 	if (so->so_pcb == NULL) {
2634 		panic("%s: so=%p NULL so_pcb lrh= %s", __func__,
2635 		    so, solockhistory_nr(so));
2636 		/* NOTREACHED */
2637 	}
2638 	return &inp->inpcb_mtx;
2639 }
2640 
2641 /*
2642  * UDP garbage collector callback (inpcb_timer_func_t).
2643  *
2644  * Returns > 0 to keep timer active.
2645  */
2646 static void
udp_gc(struct inpcbinfo * ipi)2647 udp_gc(struct inpcbinfo *ipi)
2648 {
2649 	struct inpcb *inp, *inpnxt;
2650 	struct socket *so;
2651 
2652 	if (lck_rw_try_lock_exclusive(&ipi->ipi_lock) == FALSE) {
2653 		if (udp_gc_done == TRUE) {
2654 			udp_gc_done = FALSE;
2655 			/* couldn't get the lock, must lock next time */
2656 			os_atomic_inc(&ipi->ipi_gc_req.intimer_fast, relaxed);
2657 			return;
2658 		}
2659 		lck_rw_lock_exclusive(&ipi->ipi_lock);
2660 	}
2661 
2662 	udp_gc_done = TRUE;
2663 
2664 	for (inp = udb.lh_first; inp != NULL; inp = inpnxt) {
2665 		inpnxt = inp->inp_list.le_next;
2666 
2667 		/*
2668 		 * Skip unless it's STOPUSING; garbage collector will
2669 		 * be triggered by in_pcb_checkstate() upon setting
2670 		 * wantcnt to that value.  If the PCB is already dead,
2671 		 * keep gc active to anticipate wantcnt changing.
2672 		 */
2673 		if (inp->inp_wantcnt != WNT_STOPUSING) {
2674 			continue;
2675 		}
2676 
2677 		/*
2678 		 * Skip if busy, no hurry for cleanup.  Keep gc active
2679 		 * and try the lock again during next round.
2680 		 */
2681 		if (!socket_try_lock(inp->inp_socket)) {
2682 			os_atomic_inc(&ipi->ipi_gc_req.intimer_fast, relaxed);
2683 			continue;
2684 		}
2685 
2686 		/*
2687 		 * Keep gc active unless usecount is 0.
2688 		 */
2689 		so = inp->inp_socket;
2690 		if (so->so_usecount == 0) {
2691 			if (inp->inp_state != INPCB_STATE_DEAD) {
2692 				if (SOCK_CHECK_DOM(so, PF_INET6)) {
2693 					in6_pcbdetach(inp);
2694 				} else {
2695 					in_pcbdetach(inp);
2696 				}
2697 			}
2698 			in_pcbdispose(inp);
2699 		} else {
2700 			socket_unlock(so, 0);
2701 			os_atomic_inc(&ipi->ipi_gc_req.intimer_fast, relaxed);
2702 		}
2703 	}
2704 	lck_rw_done(&ipi->ipi_lock);
2705 }
2706 
2707 static int
2708 udp_getstat SYSCTL_HANDLER_ARGS
2709 {
2710 #pragma unused(oidp, arg1, arg2)
2711 	if (req->oldptr == USER_ADDR_NULL) {
2712 		req->oldlen = (size_t)sizeof(struct udpstat);
2713 	}
2714 
2715 	return SYSCTL_OUT(req, &udpstat, MIN(sizeof(udpstat), req->oldlen));
2716 }
2717 
2718 void
udp_in_cksum_stats(u_int32_t len)2719 udp_in_cksum_stats(u_int32_t len)
2720 {
2721 	udpstat.udps_rcv_swcsum++;
2722 	udpstat.udps_rcv_swcsum_bytes += len;
2723 }
2724 
2725 void
udp_out_cksum_stats(u_int32_t len)2726 udp_out_cksum_stats(u_int32_t len)
2727 {
2728 	udpstat.udps_snd_swcsum++;
2729 	udpstat.udps_snd_swcsum_bytes += len;
2730 }
2731 
2732 void
udp_in6_cksum_stats(u_int32_t len)2733 udp_in6_cksum_stats(u_int32_t len)
2734 {
2735 	udpstat.udps_rcv6_swcsum++;
2736 	udpstat.udps_rcv6_swcsum_bytes += len;
2737 }
2738 
2739 void
udp_out6_cksum_stats(u_int32_t len)2740 udp_out6_cksum_stats(u_int32_t len)
2741 {
2742 	udpstat.udps_snd6_swcsum++;
2743 	udpstat.udps_snd6_swcsum_bytes += len;
2744 }
2745 
2746 /*
2747  * Checksum extended UDP header and data.
2748  */
2749 static int
udp_input_checksum(struct mbuf * m,struct udphdr * uh,int off,int ulen)2750 udp_input_checksum(struct mbuf *m, struct udphdr *uh, int off, int ulen)
2751 {
2752 	struct ifnet *ifp = m->m_pkthdr.rcvif;
2753 	struct ip *ip = mtod(m, struct ip *);
2754 	struct ipovly *ipov = (struct ipovly *)ip;
2755 
2756 	if (uh->uh_sum == 0) {
2757 		udpstat.udps_nosum++;
2758 		return 0;
2759 	}
2760 
2761 	/* ip_stripoptions() must have been called before we get here */
2762 	ASSERT((ip->ip_hl << 2) == sizeof(*ip));
2763 
2764 	if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) ||
2765 	    (m->m_pkthdr.pkt_flags & PKTF_LOOP)) &&
2766 	    (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) {
2767 		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
2768 			uh->uh_sum = m->m_pkthdr.csum_rx_val;
2769 		} else {
2770 			uint32_t sum = m->m_pkthdr.csum_rx_val;
2771 			uint32_t start = m->m_pkthdr.csum_rx_start;
2772 			int32_t trailer = (m_pktlen(m) - (off + ulen));
2773 
2774 			/*
2775 			 * Perform 1's complement adjustment of octets
2776 			 * that got included/excluded in the hardware-
2777 			 * calculated checksum value.  Ignore cases
2778 			 * where the value already includes the entire
2779 			 * IP header span, as the sum for those octets
2780 			 * would already be 0 by the time we get here;
2781 			 * IP has already performed its header checksum
2782 			 * checks.  If we do need to adjust, restore
2783 			 * the original fields in the IP header when
2784 			 * computing the adjustment value.  Also take
2785 			 * care of any trailing bytes and subtract out
2786 			 * their partial sum.
2787 			 */
2788 			ASSERT(trailer >= 0);
2789 			if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) &&
2790 			    ((start != 0 && start != off) || trailer != 0)) {
2791 				uint32_t swbytes = (uint32_t)trailer;
2792 
2793 				if (start < off) {
2794 					ip->ip_len += sizeof(*ip);
2795 #if BYTE_ORDER != BIG_ENDIAN
2796 					HTONS(ip->ip_len);
2797 					HTONS(ip->ip_off);
2798 #endif /* BYTE_ORDER != BIG_ENDIAN */
2799 				}
2800 				/* callee folds in sum */
2801 				sum = m_adj_sum16(m, start, off, ulen, sum);
2802 				if (off > start) {
2803 					swbytes += (off - start);
2804 				} else {
2805 					swbytes += (start - off);
2806 				}
2807 
2808 				if (start < off) {
2809 #if BYTE_ORDER != BIG_ENDIAN
2810 					NTOHS(ip->ip_off);
2811 					NTOHS(ip->ip_len);
2812 #endif /* BYTE_ORDER != BIG_ENDIAN */
2813 					ip->ip_len -= sizeof(*ip);
2814 				}
2815 
2816 				if (swbytes != 0) {
2817 					udp_in_cksum_stats(swbytes);
2818 				}
2819 				if (trailer != 0) {
2820 					m_adj(m, -trailer);
2821 				}
2822 			}
2823 
2824 			/* callee folds in sum */
2825 			uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
2826 			    ip->ip_dst.s_addr, sum + htonl(ulen + IPPROTO_UDP));
2827 		}
2828 		uh->uh_sum ^= 0xffff;
2829 	} else {
2830 		uint16_t ip_sum;
2831 		char b[9];
2832 
2833 		bcopy(ipov->ih_x1, b, sizeof(ipov->ih_x1));
2834 		bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
2835 		ip_sum = ipov->ih_len;
2836 		ipov->ih_len = uh->uh_ulen;
2837 		uh->uh_sum = in_cksum(m, ulen + sizeof(struct ip));
2838 		bcopy(b, ipov->ih_x1, sizeof(ipov->ih_x1));
2839 		ipov->ih_len = ip_sum;
2840 
2841 		udp_in_cksum_stats(ulen);
2842 	}
2843 
2844 	if (uh->uh_sum != 0) {
2845 		udpstat.udps_badsum++;
2846 		IF_UDP_STATINC(ifp, badchksum);
2847 		return -1;
2848 	}
2849 
2850 	return 0;
2851 }
2852 
2853 void
udp_fill_keepalive_offload_frames(ifnet_t ifp,struct ifnet_keepalive_offload_frame * frames_array,u_int32_t frames_array_count,size_t frame_data_offset,u_int32_t * used_frames_count)2854 udp_fill_keepalive_offload_frames(ifnet_t ifp,
2855     struct ifnet_keepalive_offload_frame *frames_array,
2856     u_int32_t frames_array_count, size_t frame_data_offset,
2857     u_int32_t *used_frames_count)
2858 {
2859 	struct inpcb *inp;
2860 	inp_gen_t gencnt;
2861 	u_int32_t frame_index = *used_frames_count;
2862 
2863 	if (ifp == NULL || frames_array == NULL ||
2864 	    frames_array_count == 0 ||
2865 	    frame_index >= frames_array_count ||
2866 	    frame_data_offset >= IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) {
2867 		return;
2868 	}
2869 
2870 	lck_rw_lock_shared(&udbinfo.ipi_lock);
2871 	gencnt = udbinfo.ipi_gencnt;
2872 	LIST_FOREACH(inp, udbinfo.ipi_listhead, inp_list) {
2873 		struct socket *so;
2874 		u_int8_t *data;
2875 		struct ifnet_keepalive_offload_frame *frame;
2876 		struct mbuf *m = NULL;
2877 
2878 		if (frame_index >= frames_array_count) {
2879 			break;
2880 		}
2881 
2882 		if (inp->inp_gencnt > gencnt ||
2883 		    inp->inp_state == INPCB_STATE_DEAD) {
2884 			continue;
2885 		}
2886 
2887 		if ((so = inp->inp_socket) == NULL ||
2888 		    (so->so_state & SS_DEFUNCT)) {
2889 			continue;
2890 		}
2891 		/*
2892 		 * check for keepalive offload flag without socket
2893 		 * lock to avoid a deadlock
2894 		 */
2895 		if (!(inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD)) {
2896 			continue;
2897 		}
2898 
2899 		udp_lock(so, 1, 0);
2900 		if (!(inp->inp_vflag & (INP_IPV4 | INP_IPV6))) {
2901 			udp_unlock(so, 1, 0);
2902 			continue;
2903 		}
2904 		if ((inp->inp_vflag & INP_IPV4) &&
2905 		    (inp->inp_laddr.s_addr == INADDR_ANY ||
2906 		    inp->inp_faddr.s_addr == INADDR_ANY)) {
2907 			udp_unlock(so, 1, 0);
2908 			continue;
2909 		}
2910 		if ((inp->inp_vflag & INP_IPV6) &&
2911 		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ||
2912 		    IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))) {
2913 			udp_unlock(so, 1, 0);
2914 			continue;
2915 		}
2916 		if (inp->inp_lport == 0 || inp->inp_fport == 0) {
2917 			udp_unlock(so, 1, 0);
2918 			continue;
2919 		}
2920 		if (inp->inp_last_outifp == NULL ||
2921 		    inp->inp_last_outifp->if_index != ifp->if_index) {
2922 			udp_unlock(so, 1, 0);
2923 			continue;
2924 		}
2925 		if ((inp->inp_vflag & INP_IPV4)) {
2926 			if ((frame_data_offset + sizeof(struct udpiphdr) +
2927 			    inp->inp_keepalive_datalen) >
2928 			    IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) {
2929 				udp_unlock(so, 1, 0);
2930 				continue;
2931 			}
2932 			if ((sizeof(struct udpiphdr) +
2933 			    inp->inp_keepalive_datalen) > _MHLEN) {
2934 				udp_unlock(so, 1, 0);
2935 				continue;
2936 			}
2937 		} else {
2938 			if ((frame_data_offset + sizeof(struct ip6_hdr) +
2939 			    sizeof(struct udphdr) +
2940 			    inp->inp_keepalive_datalen) >
2941 			    IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) {
2942 				udp_unlock(so, 1, 0);
2943 				continue;
2944 			}
2945 			if ((sizeof(struct ip6_hdr) + sizeof(struct udphdr) +
2946 			    inp->inp_keepalive_datalen) > _MHLEN) {
2947 				udp_unlock(so, 1, 0);
2948 				continue;
2949 			}
2950 		}
2951 		MGETHDR(m, M_WAIT, MT_HEADER);
2952 		if (m == NULL) {
2953 			udp_unlock(so, 1, 0);
2954 			continue;
2955 		}
2956 		/*
2957 		 * This inp has all the information that is needed to
2958 		 * generate an offload frame.
2959 		 */
2960 		if (inp->inp_vflag & INP_IPV4) {
2961 			struct ip *ip;
2962 			struct udphdr *udp;
2963 
2964 			frame = &frames_array[frame_index];
2965 			frame->length = (uint8_t)(frame_data_offset +
2966 			    sizeof(struct udpiphdr) +
2967 			    inp->inp_keepalive_datalen);
2968 			frame->ether_type =
2969 			    IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV4;
2970 			frame->interval = inp->inp_keepalive_interval;
2971 			switch (inp->inp_keepalive_type) {
2972 			case UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY:
2973 				frame->type =
2974 				    IFNET_KEEPALIVE_OFFLOAD_FRAME_AIRPLAY;
2975 				break;
2976 			default:
2977 				break;
2978 			}
2979 			data = mtod(m, u_int8_t *);
2980 			bzero(data, sizeof(struct udpiphdr));
2981 			ip = (__typeof__(ip))(void *)data;
2982 			udp = (__typeof__(udp))(void *) (data +
2983 			    sizeof(struct ip));
2984 			m->m_len = sizeof(struct udpiphdr);
2985 			data = data + sizeof(struct udpiphdr);
2986 			if (inp->inp_keepalive_datalen > 0 &&
2987 			    inp->inp_keepalive_data != NULL) {
2988 				bcopy(inp->inp_keepalive_data, data,
2989 				    inp->inp_keepalive_datalen);
2990 				m->m_len += inp->inp_keepalive_datalen;
2991 			}
2992 			m->m_pkthdr.len = m->m_len;
2993 
2994 			ip->ip_v = IPVERSION;
2995 			ip->ip_hl = (sizeof(struct ip) >> 2);
2996 			ip->ip_p = IPPROTO_UDP;
2997 			ip->ip_len = htons(sizeof(struct udpiphdr) +
2998 			    (u_short)inp->inp_keepalive_datalen);
2999 			ip->ip_ttl = inp->inp_ip_ttl;
3000 			ip->ip_tos |= (inp->inp_ip_tos & ~IPTOS_ECN_MASK);
3001 			ip->ip_src = inp->inp_laddr;
3002 			ip->ip_dst = inp->inp_faddr;
3003 			ip->ip_sum = in_cksum_hdr_opt(ip);
3004 
3005 			udp->uh_sport = inp->inp_lport;
3006 			udp->uh_dport = inp->inp_fport;
3007 			udp->uh_ulen = htons(sizeof(struct udphdr) +
3008 			    (u_short)inp->inp_keepalive_datalen);
3009 
3010 			if (!(inp->inp_flags & INP_UDP_NOCKSUM)) {
3011 				udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
3012 				    ip->ip_dst.s_addr,
3013 				    htons(sizeof(struct udphdr) +
3014 				    (u_short)inp->inp_keepalive_datalen +
3015 				    IPPROTO_UDP));
3016 				m->m_pkthdr.csum_flags =
3017 				    (CSUM_UDP | CSUM_ZERO_INVERT);
3018 				m->m_pkthdr.csum_data = offsetof(struct udphdr,
3019 				    uh_sum);
3020 			}
3021 			m->m_pkthdr.pkt_proto = IPPROTO_UDP;
3022 			in_delayed_cksum(m);
3023 			bcopy(m->m_data, frame->data + frame_data_offset,
3024 			    m->m_len);
3025 		} else {
3026 			struct ip6_hdr *ip6;
3027 			struct udphdr *udp6;
3028 
3029 			VERIFY(inp->inp_vflag & INP_IPV6);
3030 			frame = &frames_array[frame_index];
3031 			frame->length = (uint8_t)(frame_data_offset +
3032 			    sizeof(struct ip6_hdr) +
3033 			    sizeof(struct udphdr) +
3034 			    inp->inp_keepalive_datalen);
3035 			frame->ether_type =
3036 			    IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV6;
3037 			frame->interval = inp->inp_keepalive_interval;
3038 			switch (inp->inp_keepalive_type) {
3039 			case UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY:
3040 				frame->type =
3041 				    IFNET_KEEPALIVE_OFFLOAD_FRAME_AIRPLAY;
3042 				break;
3043 			default:
3044 				break;
3045 			}
3046 			data = mtod(m, u_int8_t *);
3047 			bzero(data, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
3048 			ip6 = (__typeof__(ip6))(void *)data;
3049 			udp6 = (__typeof__(udp6))(void *)(data +
3050 			    sizeof(struct ip6_hdr));
3051 			m->m_len = sizeof(struct ip6_hdr) +
3052 			    sizeof(struct udphdr);
3053 			data = data + (sizeof(struct ip6_hdr) +
3054 			    sizeof(struct udphdr));
3055 			if (inp->inp_keepalive_datalen > 0 &&
3056 			    inp->inp_keepalive_data != NULL) {
3057 				bcopy(inp->inp_keepalive_data, data,
3058 				    inp->inp_keepalive_datalen);
3059 				m->m_len += inp->inp_keepalive_datalen;
3060 			}
3061 			m->m_pkthdr.len = m->m_len;
3062 			ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
3063 			ip6->ip6_flow = ip6->ip6_flow & ~IPV6_FLOW_ECN_MASK;
3064 			ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
3065 			ip6->ip6_vfc |= IPV6_VERSION;
3066 			ip6->ip6_nxt = IPPROTO_UDP;
3067 			ip6->ip6_hlim = (uint8_t)ip6_defhlim;
3068 			ip6->ip6_plen = htons(sizeof(struct udphdr) +
3069 			    (u_short)inp->inp_keepalive_datalen);
3070 			ip6->ip6_src = inp->in6p_laddr;
3071 			if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
3072 				ip6->ip6_src.s6_addr16[1] = 0;
3073 			}
3074 
3075 			ip6->ip6_dst = inp->in6p_faddr;
3076 			if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
3077 				ip6->ip6_dst.s6_addr16[1] = 0;
3078 			}
3079 
3080 			udp6->uh_sport = inp->in6p_lport;
3081 			udp6->uh_dport = inp->in6p_fport;
3082 			udp6->uh_ulen = htons(sizeof(struct udphdr) +
3083 			    (u_short)inp->inp_keepalive_datalen);
3084 			if (!(inp->inp_flags & INP_UDP_NOCKSUM)) {
3085 				udp6->uh_sum = in6_pseudo(&ip6->ip6_src,
3086 				    &ip6->ip6_dst,
3087 				    htonl(sizeof(struct udphdr) +
3088 				    (u_short)inp->inp_keepalive_datalen +
3089 				    IPPROTO_UDP));
3090 				m->m_pkthdr.csum_flags =
3091 				    (CSUM_UDPIPV6 | CSUM_ZERO_INVERT);
3092 				m->m_pkthdr.csum_data = offsetof(struct udphdr,
3093 				    uh_sum);
3094 			}
3095 			m->m_pkthdr.pkt_proto = IPPROTO_UDP;
3096 			in6_delayed_cksum(m);
3097 			bcopy(m->m_data, frame->data + frame_data_offset,
3098 			    m->m_len);
3099 		}
3100 		if (m != NULL) {
3101 			m_freem(m);
3102 			m = NULL;
3103 		}
3104 		frame_index++;
3105 		udp_unlock(so, 1, 0);
3106 	}
3107 	lck_rw_done(&udbinfo.ipi_lock);
3108 	*used_frames_count = frame_index;
3109 }
3110 
3111 int
udp_defunct(struct socket * so)3112 udp_defunct(struct socket *so)
3113 {
3114 	struct ip_moptions *imo;
3115 	struct inpcb *inp;
3116 
3117 	inp = sotoinpcb(so);
3118 	if (inp == NULL) {
3119 		return EINVAL;
3120 	}
3121 
3122 	imo = inp->inp_moptions;
3123 	if (imo != NULL) {
3124 		struct proc *p = current_proc();
3125 
3126 		SODEFUNCTLOG("%s[%d, %s]: defuncting so 0x%llu drop multicast memberships",
3127 		    __func__, proc_pid(p), proc_best_name(p),
3128 		    so->so_gencnt);
3129 
3130 		inp->inp_moptions = NULL;
3131 
3132 		IMO_REMREF(imo);
3133 	}
3134 
3135 	return 0;
3136 }
3137