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