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