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