xref: /xnu-10002.41.9/bsd/netinet/udp_usrreq.c (revision 699cd48037512bf4380799317ca44ca453c82f57)
1 /*
2  * Copyright (c) 2000-2021 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 abort:
2054 	if (udp_dodisconnect) {
2055 		/* Always discard the cached route for unconnected socket */
2056 		ROUTE_RELEASE(&inp->inp_route);
2057 		in_pcbdisconnect(inp);
2058 		inp->inp_laddr = origladdr;     /* XXX rehash? */
2059 		/* no reference needed */
2060 		inp->inp_last_outifp = origoutifp;
2061 #if SKYWALK
2062 		if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
2063 			netns_set_ifnet(&inp->inp_netns_token,
2064 			    inp->inp_last_outifp);
2065 		}
2066 #endif /* SKYWALK */
2067 	} else if (inp->inp_route.ro_rt != NULL) {
2068 		struct rtentry *rt = inp->inp_route.ro_rt;
2069 		struct ifnet *outifp;
2070 
2071 		if (rt->rt_flags & (RTF_MULTICAST | RTF_BROADCAST)) {
2072 			rt = NULL;      /* unusable */
2073 		}
2074 #if CONTENT_FILTER
2075 		/*
2076 		 * Discard temporary route for cfil case
2077 		 */
2078 		if (cfil_faddr_use) {
2079 			rt = NULL;      /* unusable */
2080 		}
2081 #endif
2082 
2083 		/*
2084 		 * Always discard if it is a multicast or broadcast route.
2085 		 */
2086 		if (rt == NULL) {
2087 			ROUTE_RELEASE(&inp->inp_route);
2088 		}
2089 
2090 		/*
2091 		 * If the destination route is unicast, update outifp with
2092 		 * that of the route interface used by IP.
2093 		 */
2094 		if (rt != NULL &&
2095 		    (outifp = rt->rt_ifp) != inp->inp_last_outifp) {
2096 			inp->inp_last_outifp = outifp; /* no reference needed */
2097 #if SKYWALK
2098 			if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
2099 				netns_set_ifnet(&inp->inp_netns_token,
2100 				    inp->inp_last_outifp);
2101 			}
2102 #endif /* SKYWALK */
2103 
2104 			so->so_pktheadroom = (uint16_t)P2ROUNDUP(
2105 				sizeof(struct udphdr) +
2106 				sizeof(struct ip) +
2107 				ifnet_hdrlen(outifp) +
2108 				ifnet_mbuf_packetpreamblelen(outifp),
2109 				sizeof(u_int32_t));
2110 		}
2111 	} else {
2112 		ROUTE_RELEASE(&inp->inp_route);
2113 	}
2114 
2115 	/*
2116 	 * If output interface was cellular/expensive, and this socket is
2117 	 * denied access to it, generate an event.
2118 	 */
2119 	if (error != 0 && (ipoa.ipoa_flags & IPOAF_R_IFDENIED) &&
2120 	    (INP_NO_CELLULAR(inp) || INP_NO_EXPENSIVE(inp) || INP_NO_CONSTRAINED(inp))) {
2121 		soevent(so, (SO_FILT_HINT_LOCKED | SO_FILT_HINT_IFDENIED));
2122 	}
2123 
2124 release:
2125 	KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_END, error, 0, 0, 0, 0);
2126 
2127 	if (m != NULL) {
2128 		m_freem(m);
2129 	}
2130 
2131 	if (outif != NULL) {
2132 		ifnet_release(outif);
2133 	}
2134 
2135 #if CONTENT_FILTER
2136 	if (cfil_tag) {
2137 		m_tag_free(cfil_tag);
2138 	}
2139 #endif
2140 	if (sndinprog_cnt_used) {
2141 		VERIFY(inp->inp_sndinprog_cnt > 0);
2142 		if (--inp->inp_sndinprog_cnt == 0) {
2143 			inp->inp_flags &= ~(INP_FC_FEEDBACK);
2144 			if (inp->inp_sndingprog_waiters > 0) {
2145 				wakeup(&inp->inp_sndinprog_cnt);
2146 			}
2147 		}
2148 		sndinprog_cnt_used = false;
2149 	}
2150 
2151 	return error;
2152 }
2153 
2154 u_int32_t       udp_sendspace = 9216;           /* really max datagram size */
2155 /* 187 1K datagrams (approx 192 KB) */
2156 u_int32_t       udp_recvspace = 187 * (1024 + sizeof(struct sockaddr_in6));
2157 
2158 /* Check that the values of udp send and recv space do not exceed sb_max */
2159 static int
sysctl_udp_sospace(struct sysctl_oid * oidp,void * arg1,int arg2,struct sysctl_req * req)2160 sysctl_udp_sospace(struct sysctl_oid *oidp, void *arg1, int arg2,
2161     struct sysctl_req *req)
2162 {
2163 #pragma unused(arg1, arg2)
2164 	u_int32_t new_value = 0, *space_p = NULL;
2165 	int changed = 0, error = 0;
2166 	u_quad_t sb_effective_max = (sb_max / (SB_MSIZE_ADJ + MCLBYTES)) * MCLBYTES;
2167 
2168 	switch (oidp->oid_number) {
2169 	case UDPCTL_RECVSPACE:
2170 		space_p = &udp_recvspace;
2171 		break;
2172 	case UDPCTL_MAXDGRAM:
2173 		space_p = &udp_sendspace;
2174 		break;
2175 	default:
2176 		return EINVAL;
2177 	}
2178 	error = sysctl_io_number(req, *space_p, sizeof(u_int32_t),
2179 	    &new_value, &changed);
2180 	if (changed) {
2181 		if (new_value > 0 && new_value <= sb_effective_max) {
2182 			*space_p = new_value;
2183 		} else {
2184 			error = ERANGE;
2185 		}
2186 	}
2187 	return error;
2188 }
2189 
2190 SYSCTL_PROC(_net_inet_udp, UDPCTL_RECVSPACE, recvspace,
2191     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &udp_recvspace, 0,
2192     &sysctl_udp_sospace, "IU", "Maximum incoming UDP datagram size");
2193 
2194 SYSCTL_PROC(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram,
2195     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &udp_sendspace, 0,
2196     &sysctl_udp_sospace, "IU", "Maximum outgoing UDP datagram size");
2197 
2198 int
udp_abort(struct socket * so)2199 udp_abort(struct socket *so)
2200 {
2201 	struct inpcb *inp;
2202 
2203 	inp = sotoinpcb(so);
2204 	if (inp == NULL) {
2205 		panic("%s: so=%p null inp", __func__, so);
2206 		/* NOTREACHED */
2207 	}
2208 	soisdisconnected(so);
2209 	in_pcbdetach(inp);
2210 	return 0;
2211 }
2212 
2213 int
udp_attach(struct socket * so,int proto,struct proc * p)2214 udp_attach(struct socket *so, int proto, struct proc *p)
2215 {
2216 #pragma unused(proto)
2217 	struct inpcb *inp;
2218 	int error;
2219 
2220 	error = soreserve(so, udp_sendspace, udp_recvspace);
2221 	if (error != 0) {
2222 		return error;
2223 	}
2224 	inp = sotoinpcb(so);
2225 	if (inp != NULL) {
2226 		panic("%s so=%p inp=%p", __func__, so, inp);
2227 		/* NOTREACHED */
2228 	}
2229 	error = in_pcballoc(so, &udbinfo, p);
2230 	if (error != 0) {
2231 		return error;
2232 	}
2233 	inp = (struct inpcb *)so->so_pcb;
2234 	inp->inp_vflag |= INP_IPV4;
2235 	inp->inp_ip_ttl = (uint8_t)ip_defttl;
2236 	if (nstat_collect) {
2237 		nstat_udp_new_pcb(inp);
2238 	}
2239 	return 0;
2240 }
2241 
2242 int
udp_bind(struct socket * so,struct sockaddr * nam,struct proc * p)2243 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
2244 {
2245 	struct inpcb *inp;
2246 	int error;
2247 
2248 	if (nam->sa_family != 0 && nam->sa_family != AF_INET &&
2249 	    nam->sa_family != AF_INET6) {
2250 		return EAFNOSUPPORT;
2251 	}
2252 
2253 	inp = sotoinpcb(so);
2254 	if (inp == NULL) {
2255 		return EINVAL;
2256 	}
2257 	error = in_pcbbind(inp, nam, p);
2258 
2259 #if NECP
2260 	/* Update NECP client with bind result if not in middle of connect */
2261 	if (error == 0 &&
2262 	    (inp->inp_flags2 & INP2_CONNECT_IN_PROGRESS) &&
2263 	    !uuid_is_null(inp->necp_client_uuid)) {
2264 		socket_unlock(so, 0);
2265 		necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
2266 		socket_lock(so, 0);
2267 	}
2268 #endif /* NECP */
2269 
2270 	UDP_LOG_BIND(inp, error);
2271 
2272 	return error;
2273 }
2274 
2275 int
udp_connect(struct socket * so,struct sockaddr * nam,struct proc * p)2276 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
2277 {
2278 	struct inpcb *inp;
2279 	int error;
2280 
2281 	inp = sotoinpcb(so);
2282 	if (inp == NULL) {
2283 		return EINVAL;
2284 	}
2285 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
2286 		return EISCONN;
2287 	}
2288 
2289 	if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) {
2290 		so->so_flags1 |= SOF1_CONNECT_COUNTED;
2291 		INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_connected);
2292 	}
2293 
2294 #if NECP
2295 #if FLOW_DIVERT
2296 	if (necp_socket_should_use_flow_divert(inp)) {
2297 		error = flow_divert_pcb_init(so);
2298 		if (error == 0) {
2299 			error = flow_divert_connect_out(so, nam, p);
2300 		}
2301 		UDP_LOG_CONNECT(inp, error);
2302 		return error;
2303 	}
2304 #endif /* FLOW_DIVERT */
2305 #endif /* NECP */
2306 
2307 	error = in_pcbconnect(inp, nam, p, IFSCOPE_NONE, NULL);
2308 	if (error == 0) {
2309 #if NECP
2310 		/* Update NECP client with connected five-tuple */
2311 		if (!uuid_is_null(inp->necp_client_uuid)) {
2312 			socket_unlock(so, 0);
2313 			necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
2314 			socket_lock(so, 0);
2315 		}
2316 #endif /* NECP */
2317 
2318 		soisconnected(so);
2319 		if (inp->inp_flowhash == 0) {
2320 			inp_calc_flowhash(inp);
2321 			ASSERT(inp->inp_flowhash != 0);
2322 		}
2323 		inp->inp_connect_timestamp = mach_continuous_time();
2324 	}
2325 	UDP_LOG_CONNECT(inp, error);
2326 	return error;
2327 }
2328 
2329 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)2330 udp_connectx_common(struct socket *so, int af, struct sockaddr *src, struct sockaddr *dst,
2331     struct proc *p, uint32_t ifscope, sae_associd_t aid, sae_connid_t *pcid,
2332     uint32_t flags, void *arg, uint32_t arglen,
2333     struct uio *uio, user_ssize_t *bytes_written)
2334 {
2335 #pragma unused(aid, flags, arg, arglen)
2336 	struct inpcb *inp = sotoinpcb(so);
2337 	int error = 0;
2338 	user_ssize_t datalen = 0;
2339 
2340 	if (inp == NULL) {
2341 		return EINVAL;
2342 	}
2343 
2344 	VERIFY(dst != NULL);
2345 
2346 	ASSERT(!(inp->inp_flags2 & INP2_CONNECT_IN_PROGRESS));
2347 	inp->inp_flags2 |= INP2_CONNECT_IN_PROGRESS;
2348 
2349 #if NECP
2350 	inp_update_necp_policy(inp, src, dst, ifscope);
2351 #endif /* NECP */
2352 
2353 	/* bind socket to the specified interface, if requested */
2354 	if (ifscope != IFSCOPE_NONE &&
2355 	    (error = inp_bindif(inp, ifscope, NULL)) != 0) {
2356 		goto done;
2357 	}
2358 
2359 	/* if source address and/or port is specified, bind to it */
2360 	if (src != NULL) {
2361 		error = sobindlock(so, src, 0); /* already locked */
2362 		if (error != 0) {
2363 			goto done;
2364 		}
2365 	}
2366 
2367 	switch (af) {
2368 	case AF_INET:
2369 		error = udp_connect(so, dst, p);
2370 		break;
2371 	case AF_INET6:
2372 		error = udp6_connect(so, dst, p);
2373 		break;
2374 	default:
2375 		VERIFY(0);
2376 		/* NOTREACHED */
2377 	}
2378 
2379 	if (error != 0) {
2380 		goto done;
2381 	}
2382 
2383 	/*
2384 	 * If there is data, copy it. DATA_IDEMPOTENT is ignored.
2385 	 * CONNECT_RESUME_ON_READ_WRITE is ignored.
2386 	 */
2387 	if (uio != NULL) {
2388 		socket_unlock(so, 0);
2389 
2390 		VERIFY(bytes_written != NULL);
2391 
2392 		datalen = uio_resid(uio);
2393 		error = so->so_proto->pr_usrreqs->pru_sosend(so, NULL,
2394 		    (uio_t)uio, NULL, NULL, 0);
2395 		socket_lock(so, 0);
2396 
2397 		/* If error returned is EMSGSIZE, for example, disconnect */
2398 		if (error == 0 || error == EWOULDBLOCK) {
2399 			*bytes_written = datalen - uio_resid(uio);
2400 		} else {
2401 			(void) so->so_proto->pr_usrreqs->pru_disconnectx(so,
2402 			    SAE_ASSOCID_ANY, SAE_CONNID_ANY);
2403 		}
2404 		/*
2405 		 * mask the EWOULDBLOCK error so that the caller
2406 		 * knows that atleast the connect was successful.
2407 		 */
2408 		if (error == EWOULDBLOCK) {
2409 			error = 0;
2410 		}
2411 	}
2412 
2413 	if (error == 0 && pcid != NULL) {
2414 		*pcid = 1;      /* there is only 1 connection for UDP */
2415 	}
2416 done:
2417 	inp->inp_flags2 &= ~INP2_CONNECT_IN_PROGRESS;
2418 	return error;
2419 }
2420 
2421 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)2422 udp_connectx(struct socket *so, struct sockaddr *src,
2423     struct sockaddr *dst, struct proc *p, uint32_t ifscope,
2424     sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg,
2425     uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written)
2426 {
2427 	return udp_connectx_common(so, AF_INET, src, dst,
2428 	           p, ifscope, aid, pcid, flags, arg, arglen, uio, bytes_written);
2429 }
2430 
2431 int
udp_detach(struct socket * so)2432 udp_detach(struct socket *so)
2433 {
2434 	struct inpcb *inp;
2435 
2436 	inp = sotoinpcb(so);
2437 	if (inp == NULL) {
2438 		panic("%s: so=%p null inp", __func__, so);
2439 		/* NOTREACHED */
2440 	}
2441 
2442 	/*
2443 	 * If this is a socket that does not want to wakeup the device
2444 	 * for it's traffic, the application might be waiting for
2445 	 * close to complete before going to sleep. Send a notification
2446 	 * for this kind of sockets
2447 	 */
2448 	if (so->so_options & SO_NOWAKEFROMSLEEP) {
2449 		socket_post_kev_msg_closed(so);
2450 	}
2451 
2452 	UDP_LOG_CONNECTION_SUMMARY(inp);
2453 
2454 	in_pcbdetach(inp);
2455 	inp->inp_state = INPCB_STATE_DEAD;
2456 	return 0;
2457 }
2458 
2459 int
udp_disconnect(struct socket * so)2460 udp_disconnect(struct socket *so)
2461 {
2462 	struct inpcb *inp;
2463 
2464 	inp = sotoinpcb(so);
2465 	if (inp == NULL) {
2466 		return EINVAL;
2467 	}
2468 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
2469 		return ENOTCONN;
2470 	}
2471 
2472 	UDP_LOG_CONNECTION_SUMMARY(inp);
2473 
2474 	in_pcbdisconnect(inp);
2475 
2476 	/* reset flow controlled state, just in case */
2477 	inp_reset_fc_state(inp);
2478 
2479 	inp->inp_laddr.s_addr = INADDR_ANY;
2480 	so->so_state &= ~SS_ISCONNECTED;                /* XXX */
2481 	inp->inp_last_outifp = NULL;
2482 #if SKYWALK
2483 	if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
2484 		netns_set_ifnet(&inp->inp_netns_token, NULL);
2485 	}
2486 #endif /* SKYWALK */
2487 
2488 	return 0;
2489 }
2490 
2491 int
udp_disconnectx(struct socket * so,sae_associd_t aid,sae_connid_t cid)2492 udp_disconnectx(struct socket *so, sae_associd_t aid, sae_connid_t cid)
2493 {
2494 #pragma unused(cid)
2495 	if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) {
2496 		return EINVAL;
2497 	}
2498 
2499 	return udp_disconnect(so);
2500 }
2501 
2502 int
udp_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct proc * p)2503 udp_send(struct socket *so, int flags, struct mbuf *m,
2504     struct sockaddr *addr, struct mbuf *control, struct proc *p)
2505 {
2506 #ifndef FLOW_DIVERT
2507 #pragma unused(flags)
2508 #endif /* !(FLOW_DIVERT) */
2509 	struct inpcb *inp;
2510 	int error;
2511 
2512 	inp = sotoinpcb(so);
2513 	if (inp == NULL) {
2514 		if (m != NULL) {
2515 			m_freem(m);
2516 		}
2517 		if (control != NULL) {
2518 			m_freem(control);
2519 		}
2520 		return EINVAL;
2521 	}
2522 
2523 #if NECP
2524 #if FLOW_DIVERT
2525 	if (necp_socket_should_use_flow_divert(inp)) {
2526 		/* Implicit connect */
2527 		return flow_divert_implicit_data_out(so, flags, m, addr,
2528 		           control, p);
2529 	}
2530 #endif /* FLOW_DIVERT */
2531 #endif /* NECP */
2532 
2533 #if SKYWALK
2534 	sk_protect_t protect = sk_async_transmit_protect();
2535 #endif /* SKYWALK */
2536 	error = udp_output(inp, m, addr, control, p);
2537 #if SKYWALK
2538 	sk_async_transmit_unprotect(protect);
2539 #endif /* SKYWALK */
2540 
2541 	return error;
2542 }
2543 
2544 int
udp_shutdown(struct socket * so)2545 udp_shutdown(struct socket *so)
2546 {
2547 	struct inpcb *inp;
2548 
2549 	inp = sotoinpcb(so);
2550 	if (inp == NULL) {
2551 		return EINVAL;
2552 	}
2553 	socantsendmore(so);
2554 	return 0;
2555 }
2556 
2557 int
udp_lock(struct socket * so,int refcount,void * debug)2558 udp_lock(struct socket *so, int refcount, void *debug)
2559 {
2560 	void *lr_saved;
2561 
2562 	if (debug == NULL) {
2563 		lr_saved = __builtin_return_address(0);
2564 	} else {
2565 		lr_saved = debug;
2566 	}
2567 
2568 	if (so->so_pcb != NULL) {
2569 		LCK_MTX_ASSERT(&((struct inpcb *)so->so_pcb)->inpcb_mtx,
2570 		    LCK_MTX_ASSERT_NOTOWNED);
2571 		lck_mtx_lock(&((struct inpcb *)so->so_pcb)->inpcb_mtx);
2572 	} else {
2573 		panic("%s: so=%p NO PCB! lr=%p lrh= %s", __func__,
2574 		    so, lr_saved, solockhistory_nr(so));
2575 		/* NOTREACHED */
2576 	}
2577 	if (refcount) {
2578 		so->so_usecount++;
2579 	}
2580 
2581 	so->lock_lr[so->next_lock_lr] = lr_saved;
2582 	so->next_lock_lr = (so->next_lock_lr + 1) % SO_LCKDBG_MAX;
2583 	return 0;
2584 }
2585 
2586 int
udp_unlock(struct socket * so,int refcount,void * debug)2587 udp_unlock(struct socket *so, int refcount, void *debug)
2588 {
2589 	void *lr_saved;
2590 
2591 	if (debug == NULL) {
2592 		lr_saved = __builtin_return_address(0);
2593 	} else {
2594 		lr_saved = debug;
2595 	}
2596 
2597 	if (refcount) {
2598 		VERIFY(so->so_usecount > 0);
2599 		so->so_usecount--;
2600 	}
2601 	if (so->so_pcb == NULL) {
2602 		panic("%s: so=%p NO PCB! lr=%p lrh= %s", __func__,
2603 		    so, lr_saved, solockhistory_nr(so));
2604 		/* NOTREACHED */
2605 	} else {
2606 		LCK_MTX_ASSERT(&((struct inpcb *)so->so_pcb)->inpcb_mtx,
2607 		    LCK_MTX_ASSERT_OWNED);
2608 		so->unlock_lr[so->next_unlock_lr] = lr_saved;
2609 		so->next_unlock_lr = (so->next_unlock_lr + 1) % SO_LCKDBG_MAX;
2610 		lck_mtx_unlock(&((struct inpcb *)so->so_pcb)->inpcb_mtx);
2611 	}
2612 	return 0;
2613 }
2614 
2615 lck_mtx_t *
udp_getlock(struct socket * so,int flags)2616 udp_getlock(struct socket *so, int flags)
2617 {
2618 #pragma unused(flags)
2619 	struct inpcb *inp = sotoinpcb(so);
2620 
2621 	if (so->so_pcb == NULL) {
2622 		panic("%s: so=%p NULL so_pcb lrh= %s", __func__,
2623 		    so, solockhistory_nr(so));
2624 		/* NOTREACHED */
2625 	}
2626 	return &inp->inpcb_mtx;
2627 }
2628 
2629 /*
2630  * UDP garbage collector callback (inpcb_timer_func_t).
2631  *
2632  * Returns > 0 to keep timer active.
2633  */
2634 static void
udp_gc(struct inpcbinfo * ipi)2635 udp_gc(struct inpcbinfo *ipi)
2636 {
2637 	struct inpcb *inp, *inpnxt;
2638 	struct socket *so;
2639 
2640 	if (lck_rw_try_lock_exclusive(&ipi->ipi_lock) == FALSE) {
2641 		if (udp_gc_done == TRUE) {
2642 			udp_gc_done = FALSE;
2643 			/* couldn't get the lock, must lock next time */
2644 			os_atomic_inc(&ipi->ipi_gc_req.intimer_fast, relaxed);
2645 			return;
2646 		}
2647 		lck_rw_lock_exclusive(&ipi->ipi_lock);
2648 	}
2649 
2650 	udp_gc_done = TRUE;
2651 
2652 	for (inp = udb.lh_first; inp != NULL; inp = inpnxt) {
2653 		inpnxt = inp->inp_list.le_next;
2654 
2655 		/*
2656 		 * Skip unless it's STOPUSING; garbage collector will
2657 		 * be triggered by in_pcb_checkstate() upon setting
2658 		 * wantcnt to that value.  If the PCB is already dead,
2659 		 * keep gc active to anticipate wantcnt changing.
2660 		 */
2661 		if (inp->inp_wantcnt != WNT_STOPUSING) {
2662 			continue;
2663 		}
2664 
2665 		/*
2666 		 * Skip if busy, no hurry for cleanup.  Keep gc active
2667 		 * and try the lock again during next round.
2668 		 */
2669 		if (!socket_try_lock(inp->inp_socket)) {
2670 			os_atomic_inc(&ipi->ipi_gc_req.intimer_fast, relaxed);
2671 			continue;
2672 		}
2673 
2674 		/*
2675 		 * Keep gc active unless usecount is 0.
2676 		 */
2677 		so = inp->inp_socket;
2678 		if (so->so_usecount == 0) {
2679 			if (inp->inp_state != INPCB_STATE_DEAD) {
2680 				if (SOCK_CHECK_DOM(so, PF_INET6)) {
2681 					in6_pcbdetach(inp);
2682 				} else {
2683 					in_pcbdetach(inp);
2684 				}
2685 			}
2686 			in_pcbdispose(inp);
2687 		} else {
2688 			socket_unlock(so, 0);
2689 			os_atomic_inc(&ipi->ipi_gc_req.intimer_fast, relaxed);
2690 		}
2691 	}
2692 	lck_rw_done(&ipi->ipi_lock);
2693 }
2694 
2695 static int
2696 udp_getstat SYSCTL_HANDLER_ARGS
2697 {
2698 #pragma unused(oidp, arg1, arg2)
2699 	if (req->oldptr == USER_ADDR_NULL) {
2700 		req->oldlen = (size_t)sizeof(struct udpstat);
2701 	}
2702 
2703 	return SYSCTL_OUT(req, &udpstat, MIN(sizeof(udpstat), req->oldlen));
2704 }
2705 
2706 void
udp_in_cksum_stats(u_int32_t len)2707 udp_in_cksum_stats(u_int32_t len)
2708 {
2709 	udpstat.udps_rcv_swcsum++;
2710 	udpstat.udps_rcv_swcsum_bytes += len;
2711 }
2712 
2713 void
udp_out_cksum_stats(u_int32_t len)2714 udp_out_cksum_stats(u_int32_t len)
2715 {
2716 	udpstat.udps_snd_swcsum++;
2717 	udpstat.udps_snd_swcsum_bytes += len;
2718 }
2719 
2720 void
udp_in6_cksum_stats(u_int32_t len)2721 udp_in6_cksum_stats(u_int32_t len)
2722 {
2723 	udpstat.udps_rcv6_swcsum++;
2724 	udpstat.udps_rcv6_swcsum_bytes += len;
2725 }
2726 
2727 void
udp_out6_cksum_stats(u_int32_t len)2728 udp_out6_cksum_stats(u_int32_t len)
2729 {
2730 	udpstat.udps_snd6_swcsum++;
2731 	udpstat.udps_snd6_swcsum_bytes += len;
2732 }
2733 
2734 /*
2735  * Checksum extended UDP header and data.
2736  */
2737 static int
udp_input_checksum(struct mbuf * m,struct udphdr * uh,int off,int ulen)2738 udp_input_checksum(struct mbuf *m, struct udphdr *uh, int off, int ulen)
2739 {
2740 	struct ifnet *ifp = m->m_pkthdr.rcvif;
2741 	struct ip *ip = mtod(m, struct ip *);
2742 	struct ipovly *ipov = (struct ipovly *)ip;
2743 
2744 	if (uh->uh_sum == 0) {
2745 		udpstat.udps_nosum++;
2746 		return 0;
2747 	}
2748 
2749 	/* ip_stripoptions() must have been called before we get here */
2750 	ASSERT((ip->ip_hl << 2) == sizeof(*ip));
2751 
2752 	if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) ||
2753 	    (m->m_pkthdr.pkt_flags & PKTF_LOOP)) &&
2754 	    (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) {
2755 		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
2756 			uh->uh_sum = m->m_pkthdr.csum_rx_val;
2757 		} else {
2758 			uint32_t sum = m->m_pkthdr.csum_rx_val;
2759 			uint32_t start = m->m_pkthdr.csum_rx_start;
2760 			int32_t trailer = (m_pktlen(m) - (off + ulen));
2761 
2762 			/*
2763 			 * Perform 1's complement adjustment of octets
2764 			 * that got included/excluded in the hardware-
2765 			 * calculated checksum value.  Ignore cases
2766 			 * where the value already includes the entire
2767 			 * IP header span, as the sum for those octets
2768 			 * would already be 0 by the time we get here;
2769 			 * IP has already performed its header checksum
2770 			 * checks.  If we do need to adjust, restore
2771 			 * the original fields in the IP header when
2772 			 * computing the adjustment value.  Also take
2773 			 * care of any trailing bytes and subtract out
2774 			 * their partial sum.
2775 			 */
2776 			ASSERT(trailer >= 0);
2777 			if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) &&
2778 			    ((start != 0 && start != off) || trailer != 0)) {
2779 				uint32_t swbytes = (uint32_t)trailer;
2780 
2781 				if (start < off) {
2782 					ip->ip_len += sizeof(*ip);
2783 #if BYTE_ORDER != BIG_ENDIAN
2784 					HTONS(ip->ip_len);
2785 					HTONS(ip->ip_off);
2786 #endif /* BYTE_ORDER != BIG_ENDIAN */
2787 				}
2788 				/* callee folds in sum */
2789 				sum = m_adj_sum16(m, start, off, ulen, sum);
2790 				if (off > start) {
2791 					swbytes += (off - start);
2792 				} else {
2793 					swbytes += (start - off);
2794 				}
2795 
2796 				if (start < off) {
2797 #if BYTE_ORDER != BIG_ENDIAN
2798 					NTOHS(ip->ip_off);
2799 					NTOHS(ip->ip_len);
2800 #endif /* BYTE_ORDER != BIG_ENDIAN */
2801 					ip->ip_len -= sizeof(*ip);
2802 				}
2803 
2804 				if (swbytes != 0) {
2805 					udp_in_cksum_stats(swbytes);
2806 				}
2807 				if (trailer != 0) {
2808 					m_adj(m, -trailer);
2809 				}
2810 			}
2811 
2812 			/* callee folds in sum */
2813 			uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
2814 			    ip->ip_dst.s_addr, sum + htonl(ulen + IPPROTO_UDP));
2815 		}
2816 		uh->uh_sum ^= 0xffff;
2817 	} else {
2818 		uint16_t ip_sum;
2819 		char b[9];
2820 
2821 		bcopy(ipov->ih_x1, b, sizeof(ipov->ih_x1));
2822 		bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
2823 		ip_sum = ipov->ih_len;
2824 		ipov->ih_len = uh->uh_ulen;
2825 		uh->uh_sum = in_cksum(m, ulen + sizeof(struct ip));
2826 		bcopy(b, ipov->ih_x1, sizeof(ipov->ih_x1));
2827 		ipov->ih_len = ip_sum;
2828 
2829 		udp_in_cksum_stats(ulen);
2830 	}
2831 
2832 	if (uh->uh_sum != 0) {
2833 		udpstat.udps_badsum++;
2834 		IF_UDP_STATINC(ifp, badchksum);
2835 		return -1;
2836 	}
2837 
2838 	return 0;
2839 }
2840 
2841 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)2842 udp_fill_keepalive_offload_frames(ifnet_t ifp,
2843     struct ifnet_keepalive_offload_frame *frames_array,
2844     u_int32_t frames_array_count, size_t frame_data_offset,
2845     u_int32_t *used_frames_count)
2846 {
2847 	struct inpcb *inp;
2848 	inp_gen_t gencnt;
2849 	u_int32_t frame_index = *used_frames_count;
2850 
2851 	if (ifp == NULL || frames_array == NULL ||
2852 	    frames_array_count == 0 ||
2853 	    frame_index >= frames_array_count ||
2854 	    frame_data_offset >= IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) {
2855 		return;
2856 	}
2857 
2858 	lck_rw_lock_shared(&udbinfo.ipi_lock);
2859 	gencnt = udbinfo.ipi_gencnt;
2860 	LIST_FOREACH(inp, udbinfo.ipi_listhead, inp_list) {
2861 		struct socket *so;
2862 		u_int8_t *data;
2863 		struct ifnet_keepalive_offload_frame *frame;
2864 		struct mbuf *m = NULL;
2865 
2866 		if (frame_index >= frames_array_count) {
2867 			break;
2868 		}
2869 
2870 		if (inp->inp_gencnt > gencnt ||
2871 		    inp->inp_state == INPCB_STATE_DEAD) {
2872 			continue;
2873 		}
2874 
2875 		if ((so = inp->inp_socket) == NULL ||
2876 		    (so->so_state & SS_DEFUNCT)) {
2877 			continue;
2878 		}
2879 		/*
2880 		 * check for keepalive offload flag without socket
2881 		 * lock to avoid a deadlock
2882 		 */
2883 		if (!(inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD)) {
2884 			continue;
2885 		}
2886 
2887 		udp_lock(so, 1, 0);
2888 		if (!(inp->inp_vflag & (INP_IPV4 | INP_IPV6))) {
2889 			udp_unlock(so, 1, 0);
2890 			continue;
2891 		}
2892 		if ((inp->inp_vflag & INP_IPV4) &&
2893 		    (inp->inp_laddr.s_addr == INADDR_ANY ||
2894 		    inp->inp_faddr.s_addr == INADDR_ANY)) {
2895 			udp_unlock(so, 1, 0);
2896 			continue;
2897 		}
2898 		if ((inp->inp_vflag & INP_IPV6) &&
2899 		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ||
2900 		    IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))) {
2901 			udp_unlock(so, 1, 0);
2902 			continue;
2903 		}
2904 		if (inp->inp_lport == 0 || inp->inp_fport == 0) {
2905 			udp_unlock(so, 1, 0);
2906 			continue;
2907 		}
2908 		if (inp->inp_last_outifp == NULL ||
2909 		    inp->inp_last_outifp->if_index != ifp->if_index) {
2910 			udp_unlock(so, 1, 0);
2911 			continue;
2912 		}
2913 		if ((inp->inp_vflag & INP_IPV4)) {
2914 			if ((frame_data_offset + sizeof(struct udpiphdr) +
2915 			    inp->inp_keepalive_datalen) >
2916 			    IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) {
2917 				udp_unlock(so, 1, 0);
2918 				continue;
2919 			}
2920 			if ((sizeof(struct udpiphdr) +
2921 			    inp->inp_keepalive_datalen) > _MHLEN) {
2922 				udp_unlock(so, 1, 0);
2923 				continue;
2924 			}
2925 		} else {
2926 			if ((frame_data_offset + sizeof(struct ip6_hdr) +
2927 			    sizeof(struct udphdr) +
2928 			    inp->inp_keepalive_datalen) >
2929 			    IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) {
2930 				udp_unlock(so, 1, 0);
2931 				continue;
2932 			}
2933 			if ((sizeof(struct ip6_hdr) + sizeof(struct udphdr) +
2934 			    inp->inp_keepalive_datalen) > _MHLEN) {
2935 				udp_unlock(so, 1, 0);
2936 				continue;
2937 			}
2938 		}
2939 		MGETHDR(m, M_WAIT, MT_HEADER);
2940 		if (m == NULL) {
2941 			udp_unlock(so, 1, 0);
2942 			continue;
2943 		}
2944 		/*
2945 		 * This inp has all the information that is needed to
2946 		 * generate an offload frame.
2947 		 */
2948 		if (inp->inp_vflag & INP_IPV4) {
2949 			struct ip *ip;
2950 			struct udphdr *udp;
2951 
2952 			frame = &frames_array[frame_index];
2953 			frame->length = (uint8_t)(frame_data_offset +
2954 			    sizeof(struct udpiphdr) +
2955 			    inp->inp_keepalive_datalen);
2956 			frame->ether_type =
2957 			    IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV4;
2958 			frame->interval = inp->inp_keepalive_interval;
2959 			switch (inp->inp_keepalive_type) {
2960 			case UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY:
2961 				frame->type =
2962 				    IFNET_KEEPALIVE_OFFLOAD_FRAME_AIRPLAY;
2963 				break;
2964 			default:
2965 				break;
2966 			}
2967 			data = mtod(m, u_int8_t *);
2968 			bzero(data, sizeof(struct udpiphdr));
2969 			ip = (__typeof__(ip))(void *)data;
2970 			udp = (__typeof__(udp))(void *) (data +
2971 			    sizeof(struct ip));
2972 			m->m_len = sizeof(struct udpiphdr);
2973 			data = data + sizeof(struct udpiphdr);
2974 			if (inp->inp_keepalive_datalen > 0 &&
2975 			    inp->inp_keepalive_data != NULL) {
2976 				bcopy(inp->inp_keepalive_data, data,
2977 				    inp->inp_keepalive_datalen);
2978 				m->m_len += inp->inp_keepalive_datalen;
2979 			}
2980 			m->m_pkthdr.len = m->m_len;
2981 
2982 			ip->ip_v = IPVERSION;
2983 			ip->ip_hl = (sizeof(struct ip) >> 2);
2984 			ip->ip_p = IPPROTO_UDP;
2985 			ip->ip_len = htons(sizeof(struct udpiphdr) +
2986 			    (u_short)inp->inp_keepalive_datalen);
2987 			ip->ip_ttl = inp->inp_ip_ttl;
2988 			ip->ip_tos |= (inp->inp_ip_tos & ~IPTOS_ECN_MASK);
2989 			ip->ip_src = inp->inp_laddr;
2990 			ip->ip_dst = inp->inp_faddr;
2991 			ip->ip_sum = in_cksum_hdr_opt(ip);
2992 
2993 			udp->uh_sport = inp->inp_lport;
2994 			udp->uh_dport = inp->inp_fport;
2995 			udp->uh_ulen = htons(sizeof(struct udphdr) +
2996 			    (u_short)inp->inp_keepalive_datalen);
2997 
2998 			if (!(inp->inp_flags & INP_UDP_NOCKSUM)) {
2999 				udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
3000 				    ip->ip_dst.s_addr,
3001 				    htons(sizeof(struct udphdr) +
3002 				    (u_short)inp->inp_keepalive_datalen +
3003 				    IPPROTO_UDP));
3004 				m->m_pkthdr.csum_flags =
3005 				    (CSUM_UDP | CSUM_ZERO_INVERT);
3006 				m->m_pkthdr.csum_data = offsetof(struct udphdr,
3007 				    uh_sum);
3008 			}
3009 			m->m_pkthdr.pkt_proto = IPPROTO_UDP;
3010 			in_delayed_cksum(m);
3011 			bcopy(m->m_data, frame->data + frame_data_offset,
3012 			    m->m_len);
3013 		} else {
3014 			struct ip6_hdr *ip6;
3015 			struct udphdr *udp6;
3016 
3017 			VERIFY(inp->inp_vflag & INP_IPV6);
3018 			frame = &frames_array[frame_index];
3019 			frame->length = (uint8_t)(frame_data_offset +
3020 			    sizeof(struct ip6_hdr) +
3021 			    sizeof(struct udphdr) +
3022 			    inp->inp_keepalive_datalen);
3023 			frame->ether_type =
3024 			    IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV6;
3025 			frame->interval = inp->inp_keepalive_interval;
3026 			switch (inp->inp_keepalive_type) {
3027 			case UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY:
3028 				frame->type =
3029 				    IFNET_KEEPALIVE_OFFLOAD_FRAME_AIRPLAY;
3030 				break;
3031 			default:
3032 				break;
3033 			}
3034 			data = mtod(m, u_int8_t *);
3035 			bzero(data, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
3036 			ip6 = (__typeof__(ip6))(void *)data;
3037 			udp6 = (__typeof__(udp6))(void *)(data +
3038 			    sizeof(struct ip6_hdr));
3039 			m->m_len = sizeof(struct ip6_hdr) +
3040 			    sizeof(struct udphdr);
3041 			data = data + (sizeof(struct ip6_hdr) +
3042 			    sizeof(struct udphdr));
3043 			if (inp->inp_keepalive_datalen > 0 &&
3044 			    inp->inp_keepalive_data != NULL) {
3045 				bcopy(inp->inp_keepalive_data, data,
3046 				    inp->inp_keepalive_datalen);
3047 				m->m_len += inp->inp_keepalive_datalen;
3048 			}
3049 			m->m_pkthdr.len = m->m_len;
3050 			ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
3051 			ip6->ip6_flow = ip6->ip6_flow & ~IPV6_FLOW_ECN_MASK;
3052 			ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
3053 			ip6->ip6_vfc |= IPV6_VERSION;
3054 			ip6->ip6_nxt = IPPROTO_UDP;
3055 			ip6->ip6_hlim = (uint8_t)ip6_defhlim;
3056 			ip6->ip6_plen = htons(sizeof(struct udphdr) +
3057 			    (u_short)inp->inp_keepalive_datalen);
3058 			ip6->ip6_src = inp->in6p_laddr;
3059 			if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
3060 				ip6->ip6_src.s6_addr16[1] = 0;
3061 			}
3062 
3063 			ip6->ip6_dst = inp->in6p_faddr;
3064 			if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
3065 				ip6->ip6_dst.s6_addr16[1] = 0;
3066 			}
3067 
3068 			udp6->uh_sport = inp->in6p_lport;
3069 			udp6->uh_dport = inp->in6p_fport;
3070 			udp6->uh_ulen = htons(sizeof(struct udphdr) +
3071 			    (u_short)inp->inp_keepalive_datalen);
3072 			if (!(inp->inp_flags & INP_UDP_NOCKSUM)) {
3073 				udp6->uh_sum = in6_pseudo(&ip6->ip6_src,
3074 				    &ip6->ip6_dst,
3075 				    htonl(sizeof(struct udphdr) +
3076 				    (u_short)inp->inp_keepalive_datalen +
3077 				    IPPROTO_UDP));
3078 				m->m_pkthdr.csum_flags =
3079 				    (CSUM_UDPIPV6 | CSUM_ZERO_INVERT);
3080 				m->m_pkthdr.csum_data = offsetof(struct udphdr,
3081 				    uh_sum);
3082 			}
3083 			m->m_pkthdr.pkt_proto = IPPROTO_UDP;
3084 			in6_delayed_cksum(m);
3085 			bcopy(m->m_data, frame->data + frame_data_offset,
3086 			    m->m_len);
3087 		}
3088 		if (m != NULL) {
3089 			m_freem(m);
3090 			m = NULL;
3091 		}
3092 		frame_index++;
3093 		udp_unlock(so, 1, 0);
3094 	}
3095 	lck_rw_done(&udbinfo.ipi_lock);
3096 	*used_frames_count = frame_index;
3097 }
3098 
3099 int
udp_defunct(struct socket * so)3100 udp_defunct(struct socket *so)
3101 {
3102 	struct ip_moptions *imo;
3103 	struct inpcb *inp;
3104 
3105 	inp = sotoinpcb(so);
3106 	if (inp == NULL) {
3107 		return EINVAL;
3108 	}
3109 
3110 	imo = inp->inp_moptions;
3111 	if (imo != NULL) {
3112 		struct proc *p = current_proc();
3113 
3114 		SODEFUNCTLOG("%s[%d, %s]: defuncting so 0x%llu drop multicast memberships",
3115 		    __func__, proc_pid(p), proc_best_name(p),
3116 		    so->so_gencnt);
3117 
3118 		inp->inp_moptions = NULL;
3119 
3120 		IMO_REMREF(imo);
3121 	}
3122 
3123 	return 0;
3124 }
3125