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