]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/udp_usrreq.c
xnu-3789.31.2.tar.gz
[apple/xnu.git] / bsd / netinet / udp_usrreq.c
1 /*
2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
61 */
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67 #include <sys/mbuf.h>
68 #include <sys/domain.h>
69 #include <sys/protosw.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/sysctl.h>
73 #include <sys/syslog.h>
74 #include <sys/mcache.h>
75 #include <net/ntstat.h>
76
77 #include <kern/zalloc.h>
78 #include <mach/boolean.h>
79
80 #include <net/if.h>
81 #include <net/if_types.h>
82 #include <net/route.h>
83 #include <net/dlil.h>
84
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/in_tclass.h>
88 #include <netinet/ip.h>
89 #if INET6
90 #include <netinet/ip6.h>
91 #endif /* INET6 */
92 #include <netinet/in_pcb.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip_var.h>
95 #if INET6
96 #include <netinet6/in6_pcb.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/udp6_var.h>
99 #endif /* INET6 */
100 #include <netinet/ip_icmp.h>
101 #include <netinet/icmp_var.h>
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>
108 #include <netinet6/esp.h>
109 extern int ipsec_bypass;
110 extern int esp_udp_encap_port;
111 #endif /* IPSEC */
112
113 #if NECP
114 #include <net/necp.h>
115 #endif /* NECP */
116
117 #if FLOW_DIVERT
118 #include <netinet/flow_divert.h>
119 #endif /* FLOW_DIVERT */
120
121 #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)
127
128 /*
129 * UDP protocol implementation.
130 * Per RFC 768, August, 1980.
131 */
132 #ifndef COMPAT_42
133 static int udpcksum = 1;
134 #else
135 static int udpcksum = 0; /* XXX */
136 #endif
137 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum,
138 CTLFLAG_RW | CTLFLAG_LOCKED, &udpcksum, 0, "");
139
140 int udp_log_in_vain = 0;
141 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW | CTLFLAG_LOCKED,
142 &udp_log_in_vain, 0, "Log all incoming UDP packets");
143
144 static int blackhole = 0;
145 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW | CTLFLAG_LOCKED,
146 &blackhole, 0, "Do not send port unreachables for refused connects");
147
148 struct inpcbhead udb; /* from udp_var.h */
149 #define udb6 udb /* for KAME src sync over BSD*'s */
150 struct inpcbinfo udbinfo;
151
152 #ifndef UDBHASHSIZE
153 #define UDBHASHSIZE 16
154 #endif
155
156 /* Garbage collection performed during most recent udp_gc() run */
157 static boolean_t udp_gc_done = FALSE;
158
159 #if IPFIREWALL
160 extern int fw_verbose;
161 extern void ipfwsyslog(int level, const char *format, ...);
162 extern void ipfw_stealth_stats_incr_udp(void);
163
164 /* Apple logging, log to ipfw.log */
165 #define log_in_vain_log(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)) { \
169 ipfw_stealth_stats_incr_udp(); \
170 } else { \
171 log a; \
172 } \
173 }
174 #else /* !IPFIREWALL */
175 #define log_in_vain_log(a) { log a; }
176 #endif /* !IPFIREWALL */
177
178 static int udp_getstat SYSCTL_HANDLER_ARGS;
179 struct udpstat udpstat; /* from udp_var.h */
180 SYSCTL_PROC(_net_inet_udp, UDPCTL_STATS, stats,
181 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
182 0, 0, udp_getstat, "S,udpstat",
183 "UDP statistics (struct udpstat, netinet/udp_var.h)");
184
185 SYSCTL_INT(_net_inet_udp, OID_AUTO, pcbcount,
186 CTLFLAG_RD | CTLFLAG_LOCKED, &udbinfo.ipi_count, 0,
187 "Number of active PCBs");
188
189 __private_extern__ int udp_use_randomport = 1;
190 SYSCTL_INT(_net_inet_udp, OID_AUTO, randomize_ports,
191 CTLFLAG_RW | CTLFLAG_LOCKED, &udp_use_randomport, 0,
192 "Randomize UDP port numbers");
193
194 #if INET6
195 struct udp_in6 {
196 struct sockaddr_in6 uin6_sin;
197 u_char uin6_init_done : 1;
198 };
199 struct udp_ip6 {
200 struct ip6_hdr uip6_ip6;
201 u_char uip6_init_done : 1;
202 };
203
204 static int udp_abort(struct socket *);
205 static int udp_attach(struct socket *, int, struct proc *);
206 static int udp_bind(struct socket *, struct sockaddr *, struct proc *);
207 static int udp_connect(struct socket *, struct sockaddr *, struct proc *);
208 static int udp_connectx(struct socket *, struct sockaddr_list **,
209 struct sockaddr_list **, struct proc *, uint32_t, sae_associd_t,
210 sae_connid_t *, uint32_t, void *, uint32_t, struct uio *, user_ssize_t *);
211 static int udp_detach(struct socket *);
212 static int udp_disconnect(struct socket *);
213 static int udp_disconnectx(struct socket *, sae_associd_t, sae_connid_t);
214 static int udp_send(struct socket *, int, struct mbuf *, struct sockaddr *,
215 struct mbuf *, struct proc *);
216 static 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 */
219 static void udp_append(struct inpcb *, struct ip *, struct mbuf *, int,
220 struct sockaddr_in *, struct ifnet *);
221 #endif /* !INET6 */
222 static int udp_input_checksum(struct mbuf *, struct udphdr *, int, int);
223 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
224 struct mbuf *, struct proc *);
225 static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
226 static void udp_gc(struct inpcbinfo *);
227
228 struct 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,
244 .pru_soreceive_list = soreceive_list,
245 };
246
247 void
248 udp_init(struct protosw *pp, struct domain *dp)
249 {
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;
260
261 LIST_INIT(&udb);
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;
271 /*
272 * allocate lock group attribute and group for udp pcb mutexes
273 */
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 }
283
284 udbinfo.ipi_gc = udp_gc;
285 in_pcbinfo_attach(&udbinfo);
286 }
287
288 void
289 udp_input(struct mbuf *m, int iphlen)
290 {
291 struct ip *ip;
292 struct udphdr *uh;
293 struct inpcb *inp;
294 struct mbuf *opts = NULL;
295 int len, isbroadcast;
296 struct ip save_ip;
297 struct sockaddr *append_sa;
298 struct inpcbinfo *pcbinfo = &udbinfo;
299 struct sockaddr_in udp_in;
300 struct ip_moptions *imo = NULL;
301 int foundmembership = 0, ret = 0;
302 #if INET6
303 struct udp_in6 udp_in6;
304 struct udp_ip6 udp_ip6;
305 #endif /* INET6 */
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));
309 boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp));
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 */
319
320 udpstat.udps_ipackets++;
321
322 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_START, 0, 0, 0, 0, 0);
323
324 /* Expect 32-bit aligned data pointer on strict-align platforms */
325 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
326
327 /*
328 * Strip IP options, if any; should skip this,
329 * make available to user, and use on returned packets,
330 * but we don't yet have a way to check the checksum
331 * with options still present.
332 */
333 if (iphlen > sizeof (struct ip)) {
334 ip_stripoptions(m, (struct mbuf *)0);
335 iphlen = sizeof (struct ip);
336 }
337
338 /*
339 * Get IP and UDP header together in first mbuf.
340 */
341 ip = mtod(m, struct ip *);
342 if (m->m_len < iphlen + sizeof (struct udphdr)) {
343 m = m_pullup(m, iphlen + sizeof (struct udphdr));
344 if (m == NULL) {
345 udpstat.udps_hdrops++;
346 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
347 0, 0, 0, 0, 0);
348 return;
349 }
350 ip = mtod(m, struct ip *);
351 }
352 uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
353
354 /* destination port of 0 is illegal, based on RFC768. */
355 if (uh->uh_dport == 0) {
356 IF_UDP_STATINC(ifp, port0);
357 goto bad;
358 }
359
360 KERNEL_DEBUG(DBG_LAYER_IN_BEG, uh->uh_dport, uh->uh_sport,
361 ip->ip_src.s_addr, ip->ip_dst.s_addr, uh->uh_ulen);
362
363 /*
364 * Make mbuf data length reflect UDP length.
365 * If not enough data to reflect UDP length, drop.
366 */
367 len = ntohs((u_short)uh->uh_ulen);
368 if (ip->ip_len != len) {
369 if (len > ip->ip_len || len < sizeof (struct udphdr)) {
370 udpstat.udps_badlen++;
371 IF_UDP_STATINC(ifp, badlength);
372 goto bad;
373 }
374 m_adj(m, len - ip->ip_len);
375 /* ip->ip_len = len; */
376 }
377 /*
378 * Save a copy of the IP header in case we want restore it
379 * for sending an ICMP error message in response.
380 */
381 save_ip = *ip;
382
383 /*
384 * Checksum extended UDP header and data.
385 */
386 if (udp_input_checksum(m, uh, iphlen, len))
387 goto bad;
388
389 isbroadcast = in_broadcast(ip->ip_dst, ifp);
390
391 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || isbroadcast) {
392 int reuse_sock = 0, mcast_delivered = 0;
393
394 lck_rw_lock_shared(pcbinfo->ipi_lock);
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 */
420 #if INET6
421 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
422 #endif /* INET6 */
423 LIST_FOREACH(inp, &udb, inp_list) {
424 #if IPSEC
425 int skipit;
426 #endif /* IPSEC */
427
428 if (inp->inp_socket == NULL)
429 continue;
430 if (inp != sotoinpcb(inp->inp_socket)) {
431 panic("%s: bad so back ptr inp=%p\n",
432 __func__, inp);
433 /* NOTREACHED */
434 }
435 #if INET6
436 if ((inp->inp_vflag & INP_IPV4) == 0)
437 continue;
438 #endif /* INET6 */
439 if (inp_restricted_recv(inp, ifp))
440 continue;
441
442 if ((inp->inp_moptions == NULL) &&
443 (ntohl(ip->ip_dst.s_addr) !=
444 INADDR_ALLHOSTS_GROUP) && (isbroadcast == 0))
445 continue;
446
447 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) ==
448 WNT_STOPUSING)
449 continue;
450
451 udp_lock(inp->inp_socket, 1, 0);
452
453 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) ==
454 WNT_STOPUSING) {
455 udp_unlock(inp->inp_socket, 1, 0);
456 continue;
457 }
458
459 if (inp->inp_lport != uh->uh_dport) {
460 udp_unlock(inp->inp_socket, 1, 0);
461 continue;
462 }
463 if (inp->inp_laddr.s_addr != INADDR_ANY) {
464 if (inp->inp_laddr.s_addr !=
465 ip->ip_dst.s_addr) {
466 udp_unlock(inp->inp_socket, 1, 0);
467 continue;
468 }
469 }
470 if (inp->inp_faddr.s_addr != INADDR_ANY) {
471 if (inp->inp_faddr.s_addr !=
472 ip->ip_src.s_addr ||
473 inp->inp_fport != uh->uh_sport) {
474 udp_unlock(inp->inp_socket, 1, 0);
475 continue;
476 }
477 }
478
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) {
485 udp_unlock(inp->inp_socket, 1, 0);
486 continue;
487 }
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;
510 }
511
512 reuse_sock = (inp->inp_socket->so_options &
513 (SO_REUSEPORT|SO_REUSEADDR));
514
515 #if NECP
516 skipit = 0;
517 if (!necp_socket_is_allowed_to_send_recv_v4(inp,
518 uh->uh_dport, uh->uh_sport, &ip->ip_dst,
519 &ip->ip_src, ifp, NULL, NULL)) {
520 /* do not inject data to pcb */
521 skipit = 1;
522 }
523 if (skipit == 0)
524 #endif /* NECP */
525 {
526 struct mbuf *n = NULL;
527
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;
542 }
543 udp_unlock(inp->inp_socket, 1, 0);
544
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
550 * port. It assumes that an application will never
551 * clear these options after setting them.
552 */
553 if (reuse_sock == 0 || m == NULL)
554 break;
555
556 /*
557 * Expect 32-bit aligned data pointer on strict-align
558 * platforms.
559 */
560 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
561 /*
562 * Recompute IP and UDP header pointers for new mbuf
563 */
564 ip = mtod(m, struct ip *);
565 uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
566 }
567 lck_rw_done(pcbinfo->ipi_lock);
568
569 if (mcast_delivered == 0) {
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++;
576 IF_UDP_STATINC(ifp, port_unreach);
577 goto bad;
578 }
579
580 /* free the extra copy of mbuf or skipped by IPSec */
581 if (m != NULL)
582 m_freem(m);
583 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
584 return;
585 }
586
587 #if IPSEC
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 &&
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) {
603 udpstat.udps_hdrops++;
604 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
605 0, 0, 0, 0, 0);
606 return;
607 }
608 /*
609 * Expect 32-bit aligned data pointer on strict-align
610 * platforms.
611 */
612 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
613
614 ip = mtod(m, struct ip *);
615 uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen);
616 }
617 /* Check for NAT keepalive packet */
618 if (payload_len == 1 && *(u_int8_t *)
619 ((caddr_t)uh + sizeof (struct udphdr)) == 0xFF) {
620 m_freem(m);
621 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
622 0, 0, 0, 0, 0);
623 return;
624 } else if (payload_len == 4 && *(u_int32_t *)(void *)
625 ((caddr_t)uh + sizeof (struct udphdr)) != 0) {
626 /* UDP encapsulated IPSec packet to pass through NAT */
627 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END,
628 0, 0, 0, 0, 0);
629 /* preserve the udp header */
630 esp4_input(m, iphlen + sizeof (struct udphdr));
631 return;
632 }
633 }
634 #endif /* IPSEC */
635
636 /*
637 * Locate pcb for datagram.
638 */
639 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
640 ip->ip_dst, uh->uh_dport, 1, ifp);
641 if (inp == NULL) {
642 IF_UDP_STATINC(ifp, port_unreach);
643
644 if (udp_log_in_vain) {
645 char buf[MAX_IPv4_STR_LEN];
646 char buf2[MAX_IPv4_STR_LEN];
647
648 /* check src and dst address */
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,
652 &ip->ip_dst, buf, sizeof (buf)),
653 ntohs(uh->uh_dport), inet_ntop(AF_INET,
654 &ip->ip_src, buf2, sizeof (buf2)),
655 ntohs(uh->uh_sport));
656 } else if (!(m->m_flags & (M_BCAST | M_MCAST)) &&
657 ip->ip_dst.s_addr != ip->ip_src.s_addr) {
658 log_in_vain_log((LOG_INFO,
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 }
666 }
667 udpstat.udps_noport++;
668 if (m->m_flags & (M_BCAST | M_MCAST)) {
669 udpstat.udps_noportbcast++;
670 goto bad;
671 }
672 #if ICMP_BANDLIM
673 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
674 goto bad;
675 #endif /* ICMP_BANDLIM */
676 if (blackhole)
677 if (ifp && ifp->if_type != IFT_LOOP)
678 goto bad;
679 *ip = save_ip;
680 ip->ip_len += iphlen;
681 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
682 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
683 return;
684 }
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);
689 IF_UDP_STATINC(ifp, cleanup);
690 goto bad;
691 }
692 #if NECP
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)) {
695 udp_unlock(inp->inp_socket, 1, 0);
696 IF_UDP_STATINC(ifp, badipsec);
697 goto bad;
698 }
699 #endif /* NECP */
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;
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) {
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;
717 ret = ip6_savecontrol(inp, m, &opts);
718 inp->inp_flags = savedflags;
719 } else
720 #endif /* INET6 */
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 }
728 }
729 m_adj(m, iphlen + sizeof (struct udphdr));
730
731 KERNEL_DEBUG(DBG_LAYER_IN_END, uh->uh_dport, uh->uh_sport,
732 save_ip.ip_src.s_addr, save_ip.ip_dst.s_addr, uh->uh_ulen);
733
734 #if INET6
735 if (inp->inp_vflag & INP_IPV6) {
736 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
737 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
738 } else
739 #endif /* INET6 */
740 {
741 append_sa = (struct sockaddr *)&udp_in;
742 }
743 if (nstat_collect) {
744 INP_ADD_STAT(inp, cell, wifi, wired, rxpackets, 1);
745 INP_ADD_STAT(inp, cell, wifi, wired, rxbytes, m->m_pkthdr.len);
746 }
747 so_recv_data_stat(inp->inp_socket, m, 0);
748 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa,
749 m, opts, NULL) == 0) {
750 udpstat.udps_fullsock++;
751 } else {
752 sorwakeup(inp->inp_socket);
753 }
754 udp_unlock(inp->inp_socket, 1, 0);
755 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
756 return;
757 bad:
758 m_freem(m);
759 if (opts)
760 m_freem(opts);
761 KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0);
762 }
763
764 #if INET6
765 static void
766 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
767 {
768 bzero(ip6, sizeof (*ip6));
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;
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 }
782 }
783 #endif /* INET6 */
784
785 /*
786 * subroutine of udp_input(), mainly for source code readability.
787 */
788 static void
789 #if INET6
790 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
791 struct sockaddr_in *pudp_in, struct udp_in6 *pudp_in6,
792 struct udp_ip6 *pudp_ip6, struct ifnet *ifp)
793 #else /* !INET6 */
794 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
795 struct sockaddr_in *pudp_in, struct ifnet *ifp)
796 #endif /* !INET6 */
797 {
798 struct sockaddr *append_sa;
799 struct mbuf *opts = 0;
800 boolean_t cell = IFNET_IS_CELLULAR(ifp);
801 boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp));
802 boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp));
803 int ret = 0;
804
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 }
810 #endif /* CONFIG_MACF_NET */
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) {
814 #if INET6
815 if (last->inp_vflag & INP_IPV6) {
816 int savedflags;
817
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;
821 }
822 savedflags = last->inp_flags;
823 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
824 ret = ip6_savecontrol(last, n, &opts);
825 if (ret != 0) {
826 last->inp_flags = savedflags;
827 goto error;
828 }
829 last->inp_flags = savedflags;
830 } else
831 #endif /* INET6 */
832 {
833 ret = ip_savecontrol(last, &opts, ip, n);
834 if (ret != 0) {
835 goto error;
836 }
837 }
838 }
839 #if INET6
840 if (last->inp_vflag & INP_IPV6) {
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;
844 }
845 append_sa = (struct sockaddr *)&pudp_in6->uin6_sin;
846 } else
847 #endif /* INET6 */
848 append_sa = (struct sockaddr *)pudp_in;
849 if (nstat_collect) {
850 INP_ADD_STAT(last, cell, wifi, wired, rxpackets, 1);
851 INP_ADD_STAT(last, cell, wifi, wired, rxbytes,
852 n->m_pkthdr.len);
853 }
854 so_recv_data_stat(last->inp_socket, n, 0);
855 m_adj(n, off);
856 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa,
857 n, opts, NULL) == 0) {
858 udpstat.udps_fullsock++;
859 } else {
860 sorwakeup(last->inp_socket);
861 }
862 return;
863 error:
864 m_freem(n);
865 m_freem(opts);
866 }
867
868 /*
869 * Notify a udp user of an asynchronous error;
870 * just wake up so that he can collect error status.
871 */
872 void
873 udp_notify(struct inpcb *inp, int errno)
874 {
875 inp->inp_socket->so_error = errno;
876 sorwakeup(inp->inp_socket);
877 sowwakeup(inp->inp_socket);
878 }
879
880 void
881 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
882 {
883 struct ip *ip = vip;
884 void (*notify)(struct inpcb *, int) = udp_notify;
885 struct in_addr faddr;
886 struct inpcb *inp;
887
888 faddr = ((struct sockaddr_in *)(void *)sa)->sin_addr;
889 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
890 return;
891
892 if (PRC_IS_REDIRECT(cmd)) {
893 ip = 0;
894 notify = in_rtchange;
895 } else if (cmd == PRC_HOSTDEAD) {
896 ip = 0;
897 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
898 return;
899 }
900 if (ip) {
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,
905 ip->ip_src, uh.uh_sport, 0, NULL);
906 if (inp != NULL && inp->inp_socket != NULL) {
907 udp_lock(inp->inp_socket, 1, 0);
908 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) ==
909 WNT_STOPUSING) {
910 udp_unlock(inp->inp_socket, 1, 0);
911 return;
912 }
913 (*notify)(inp, inetctlerrmap[cmd]);
914 udp_unlock(inp->inp_socket, 1, 0);
915 }
916 } else {
917 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
918 }
919 }
920
921 int
922 udp_ctloutput(struct socket *so, struct sockopt *sopt)
923 {
924 int error, optval;
925 struct inpcb *inp;
926
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))
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;
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 }
998
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,
1004 u_int8_t *,
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 }
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
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
1057 static int
1058 udp_pcblist SYSCTL_HANDLER_ARGS
1059 {
1060 #pragma unused(oidp, arg1, arg2)
1061 int error, i, n;
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 */
1070 lck_rw_lock_exclusive(udbinfo.ipi_lock);
1071 if (req->oldptr == USER_ADDR_NULL) {
1072 n = udbinfo.ipi_count;
1073 req->oldidx = 2 * (sizeof (xig))
1074 + (n + n/8) * sizeof (struct xinpcb);
1075 lck_rw_done(udbinfo.ipi_lock);
1076 return (0);
1077 }
1078
1079 if (req->newptr != USER_ADDR_NULL) {
1080 lck_rw_done(udbinfo.ipi_lock);
1081 return (EPERM);
1082 }
1083
1084 /*
1085 * OK, now we're committed to doing something.
1086 */
1087 gencnt = udbinfo.ipi_gencnt;
1088 n = udbinfo.ipi_count;
1089
1090 bzero(&xig, sizeof (xig));
1091 xig.xig_len = sizeof (xig);
1092 xig.xig_count = n;
1093 xig.xig_gen = gencnt;
1094 xig.xig_sogen = so_gencnt;
1095 error = SYSCTL_OUT(req, &xig, sizeof (xig));
1096 if (error) {
1097 lck_rw_done(udbinfo.ipi_lock);
1098 return (error);
1099 }
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);
1109 if (inp_list == 0) {
1110 lck_rw_done(udbinfo.ipi_lock);
1111 return (ENOMEM);
1112 }
1113
1114 for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n;
1115 inp = LIST_NEXT(inp, inp_list)) {
1116 if (inp->inp_gencnt <= gencnt &&
1117 inp->inp_state != INPCB_STATE_DEAD)
1118 inp_list[i++] = inp;
1119 }
1120 n = i;
1121
1122 error = 0;
1123 for (i = 0; i < n; i++) {
1124 struct xinpcb xi;
1125
1126 inp = inp_list[i];
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;
1138 }
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));
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 */
1159 bzero(&xig, sizeof (xig));
1160 xig.xig_len = sizeof (xig);
1161 xig.xig_gen = udbinfo.ipi_gencnt;
1162 xig.xig_sogen = so_gencnt;
1163 xig.xig_count = udbinfo.ipi_count;
1164 error = SYSCTL_OUT(req, &xig, sizeof (xig));
1165 }
1166 FREE(inp_list, M_TEMP);
1167 lck_rw_done(udbinfo.ipi_lock);
1168 return (error);
1169 }
1170
1171 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
1172 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist,
1173 "S,xinpcb", "List of active UDP sockets");
1174
1175
1176 static int
1177 udp_pcblist64 SYSCTL_HANDLER_ARGS
1178 {
1179 #pragma unused(oidp, arg1, arg2)
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 =
1193 2 * (sizeof (xig)) + (n + n/8) * sizeof (struct xinpcb64);
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 }
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
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 }
1232
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 &&
1236 inp->inp_state != INPCB_STATE_DEAD)
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);
1287 }
1288
1289 SYSCTL_PROC(_net_inet_udp, OID_AUTO, pcblist64,
1290 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist64,
1291 "S,xinpcb64", "List of active UDP sockets");
1292
1293
1294 static int
1295 udp_pcblist_n SYSCTL_HANDLER_ARGS
1296 {
1297 #pragma unused(oidp, arg1, arg2)
1298 return (get_pcblist_n(IPPROTO_UDP, req, &udbinfo));
1299 }
1300
1301 SYSCTL_PROC(_net_inet_udp, OID_AUTO, pcblist_n,
1302 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist_n,
1303 "S,xinpcb_n", "List of active UDP sockets");
1304
1305 __private_extern__ void
1306 udp_get_ports_used(uint32_t ifindex, int protocol, uint32_t flags,
1307 bitstr_t *bitfield)
1308 {
1309 inpcb_get_ports_used(ifindex, protocol, flags, bitfield, &udbinfo);
1310 }
1311
1312 __private_extern__ uint32_t
1313 udp_count_opportunistic(unsigned int ifindex, u_int32_t flags)
1314 {
1315 return (inpcb_count_opportunistic(ifindex, &udbinfo, flags));
1316 }
1317
1318 __private_extern__ uint32_t
1319 udp_find_anypcb_byaddr(struct ifaddr *ifa)
1320 {
1321 return (inpcb_find_anypcb_byaddr(ifa, &udbinfo));
1322 }
1323
1324 static int
1325 udp_check_pktinfo(struct mbuf *control, struct ifnet **outif,
1326 struct in_addr *laddr)
1327 {
1328 struct cmsghdr *cm = 0;
1329 struct in_pktinfo *pktinfo;
1330 struct ifnet *ifp;
1331
1332 if (outif != NULL)
1333 *outif = NULL;
1334
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
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)
1349 return (EINVAL);
1350
1351 if (cm->cmsg_level != IPPROTO_IP || cm->cmsg_type != IP_PKTINFO)
1352 continue;
1353
1354 if (cm->cmsg_len != CMSG_LEN(sizeof (struct in_pktinfo)))
1355 return (EINVAL);
1356
1357 pktinfo = (struct in_pktinfo *)(void *)CMSG_DATA(cm);
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
1367 /*
1368 * If ipi_ifindex is specified it takes precedence
1369 * over ipi_spec_dst.
1370 */
1371 if (pktinfo->ipi_ifindex) {
1372 ifp = ifindex2ifnet[pktinfo->ipi_ifindex];
1373 if (ifp == NULL) {
1374 ifnet_head_done();
1375 return (ENXIO);
1376 }
1377 if (outif != NULL) {
1378 ifnet_reference(ifp);
1379 *outif = ifp;
1380 }
1381 ifnet_head_done();
1382 laddr->s_addr = INADDR_ANY;
1383 break;
1384 }
1385
1386 ifnet_head_done();
1387
1388 /*
1389 * Use the provided ipi_spec_dst address for temp
1390 * source address.
1391 */
1392 *laddr = pktinfo->ipi_spec_dst;
1393 break;
1394 }
1395 return (0);
1396 }
1397
1398 static int
1399 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
1400 struct mbuf *control, struct proc *p)
1401 {
1402 struct udpiphdr *ui;
1403 int len = m->m_pkthdr.len;
1404 struct sockaddr_in *sin;
1405 struct in_addr origladdr, laddr, faddr, pi_laddr;
1406 u_short lport, fport;
1407 int error = 0, udp_dodisconnect = 0, pktinfo = 0;
1408 struct socket *so = inp->inp_socket;
1409 int soopts = 0;
1410 struct mbuf *inpopts;
1411 struct ip_moptions *mopts;
1412 struct route ro;
1413 struct ip_out_args ipoa =
1414 { IFSCOPE_NONE, { 0 }, IPOAF_SELECT_SRCIF, 0, 0, 0 };
1415 struct ifnet *outif = NULL;
1416 struct flowadv *adv = &ipoa.ipoa_flowadv;
1417 int sotc = SO_TC_UNSPEC;
1418 int netsvctype = _NET_SERVICE_TYPE_UNSPEC;
1419 struct ifnet *origoutifp = NULL;
1420 int flowadv = 0;
1421
1422 /* Enable flow advisory only when connected */
1423 flowadv = (so->so_state & SS_ISCONNECTED) ? 1 : 0;
1424 pi_laddr.s_addr = INADDR_ANY;
1425
1426 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_START, 0, 0, 0, 0, 0);
1427
1428 lck_mtx_assert(&inp->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
1429 if (control != NULL) {
1430 sotc = so_tc_from_control(control, &netsvctype);
1431 VERIFY(outif == NULL);
1432 error = udp_check_pktinfo(control, &outif, &pi_laddr);
1433 m_freem(control);
1434 control = NULL;
1435 if (error)
1436 goto release;
1437 pktinfo++;
1438 if (outif != NULL)
1439 ipoa.ipoa_boundif = outif->if_index;
1440 }
1441 if (sotc == SO_TC_UNSPEC) {
1442 sotc = so->so_traffic_class;
1443 netsvctype = so->so_netsvctype;
1444 }
1445
1446 KERNEL_DEBUG(DBG_LAYER_OUT_BEG, inp->inp_fport, inp->inp_lport,
1447 inp->inp_laddr.s_addr, inp->inp_faddr.s_addr,
1448 (htons((u_short)len + sizeof (struct udphdr))));
1449
1450 if (len + sizeof (struct udpiphdr) > IP_MAXPACKET) {
1451 error = EMSGSIZE;
1452 goto release;
1453 }
1454
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 }
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 */
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);
1474 outif = inp->inp_boundifp;
1475 ipoa.ipoa_boundif = outif->if_index;
1476 }
1477 if (INP_NO_CELLULAR(inp))
1478 ipoa.ipoa_flags |= IPOAF_NO_CELLULAR;
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;
1483 ipoa.ipoa_sotc = sotc;
1484 ipoa.ipoa_netsvctype = netsvctype;
1485 soopts |= IP_OUTARGS;
1486
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.
1491 */
1492 if (ROUTE_UNUSABLE(&inp->inp_route)) {
1493 struct in_ifaddr *ia = NULL;
1494
1495 ROUTE_RELEASE(&inp->inp_route);
1496
1497 /* src address is gone? */
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) ||
1501 (so->so_state & SS_ISCONNECTED)) {
1502 /*
1503 * Rdar://5448998
1504 * If the source address is gone, return an
1505 * error if:
1506 * - the source was specified
1507 * - the socket was already connected
1508 */
1509 soevent(so, (SO_FILT_HINT_LOCKED |
1510 SO_FILT_HINT_NOSRCADDR));
1511 error = EADDRNOTAVAIL;
1512 goto release;
1513 } else {
1514 /* new src will be set later */
1515 inp->inp_laddr.s_addr = INADDR_ANY;
1516 inp->inp_last_outifp = NULL;
1517 }
1518 }
1519 if (ia != NULL)
1520 IFA_REMREF(&ia->ia_ifa);
1521 }
1522
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.
1527 */
1528 if (pi_laddr.s_addr != INADDR_ANY ||
1529 (ipoa.ipoa_boundif != IFSCOPE_NONE && pktinfo)) {
1530 /* temp src address for this datagram only */
1531 laddr = pi_laddr;
1532 origladdr.s_addr = INADDR_ANY;
1533 /* we don't want to keep the laddr or route */
1534 udp_dodisconnect = 1;
1535 /* remember we don't care about src addr */
1536 inp->inp_flags |= INP_INADDR_ANY;
1537 } else {
1538 origladdr = laddr = inp->inp_laddr;
1539 }
1540
1541 origoutifp = inp->inp_last_outifp;
1542 faddr = inp->inp_faddr;
1543 lport = inp->inp_lport;
1544 fport = inp->inp_fport;
1545
1546 if (addr) {
1547 sin = (struct sockaddr_in *)(void *)addr;
1548 if (faddr.s_addr != INADDR_ANY) {
1549 error = EISCONN;
1550 goto release;
1551 }
1552 if (lport == 0) {
1553 /*
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.
1558 */
1559 /* if we have a source address specified, use that */
1560 if (pi_laddr.s_addr != INADDR_ANY)
1561 inp->inp_laddr = pi_laddr;
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)
1570 goto release;
1571
1572 laddr = inp->inp_laddr;
1573 lport = inp->inp_lport;
1574 faddr = inp->inp_faddr;
1575 fport = inp->inp_fport;
1576 udp_dodisconnect = 1;
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;
1581 } else {
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.
1591 */
1592 if (laddr.s_addr == INADDR_ANY) {
1593 if ((error = in_pcbladdr(inp, addr, &laddr,
1594 ipoa.ipoa_boundif, &outif, 0)) != 0)
1595 goto release;
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;
1606 }
1607
1608 faddr = sin->sin_addr;
1609 fport = sin->sin_port;
1610 }
1611 } else {
1612 if (faddr.s_addr == INADDR_ANY) {
1613 error = ENOTCONN;
1614 goto release;
1615 }
1616 }
1617
1618 #if CONFIG_MACF_NET
1619 mac_mbuf_label_associate_inpcb(inp, m);
1620 #endif /* CONFIG_MACF_NET */
1621
1622 if (inp->inp_flowhash == 0)
1623 inp->inp_flowhash = inp_calc_flowhash(inp);
1624
1625 /*
1626 * Calculate data length and get a mbuf
1627 * for UDP and IP headers.
1628 */
1629 M_PREPEND(m, sizeof (struct udpiphdr), M_DONTWAIT, 1);
1630 if (m == 0) {
1631 error = ENOBUFS;
1632 goto abort;
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 *);
1640 bzero(ui->ui_x1, sizeof (ui->ui_x1)); /* XXX still needed? */
1641 ui->ui_pr = IPPROTO_UDP;
1642 ui->ui_src = laddr;
1643 ui->ui_dst = faddr;
1644 ui->ui_sport = lport;
1645 ui->ui_dport = fport;
1646 ui->ui_ulen = htons((u_short)len + sizeof (struct udphdr));
1647
1648 /*
1649 * Set up checksum and output datagram.
1650 */
1651 if (udpcksum && !(inp->inp_flags & INP_UDP_NOCKSUM)) {
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));
1654 m->m_pkthdr.csum_flags = CSUM_UDP;
1655 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1656 } else {
1657 ui->ui_sum = 0;
1658 }
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,
1665 ui->ui_src.s_addr, ui->ui_dst.s_addr, ui->ui_ulen);
1666
1667 #if NECP
1668 {
1669 necp_kernel_policy_id policy_id;
1670 u_int32_t route_rule_id;
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
1704 if (!necp_socket_is_allowed_to_send_recv_v4(inp, lport, fport,
1705 &laddr, &faddr, NULL, &policy_id, &route_rule_id)) {
1706 error = EHOSTUNREACH;
1707 goto abort;
1708 }
1709
1710 necp_mark_packet_from_socket(m, inp, policy_id, route_rule_id);
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 }
1716 }
1717 #endif /* NECP */
1718 if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED))
1719 ipoa.ipoa_flags |= IPOAF_QOSMARKING_ALLOWED;
1720
1721 #if IPSEC
1722 if (inp->inp_sp != NULL && ipsec_setsocket(m, inp->inp_socket) != 0) {
1723 error = ENOBUFS;
1724 goto abort;
1725 }
1726 #endif /* IPSEC */
1727
1728 inpopts = inp->inp_options;
1729 soopts |= (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST));
1730 mopts = inp->inp_moptions;
1731 if (mopts != NULL) {
1732 IMO_LOCK(mopts);
1733 IMO_ADDREF_LOCKED(mopts);
1734 if (IN_MULTICAST(ntohl(ui->ui_dst.s_addr)) &&
1735 mopts->imo_multicast_ifp != NULL) {
1736 /* no reference needed */
1737 inp->inp_last_outifp = mopts->imo_multicast_ifp;
1738 }
1739 IMO_UNLOCK(mopts);
1740 }
1741
1742 /* Copy the cached route and take an extra reference */
1743 inp_route_copyout(inp, &ro);
1744
1745 set_packet_service_class(m, so, sotc, 0);
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);
1750 if (flowadv)
1751 m->m_pkthdr.pkt_flags |= PKTF_FLOW_ADV;
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++;
1760
1761 socket_unlock(so, 0);
1762 error = ip_output(m, inpopts, &ro, soopts, mopts, &ipoa);
1763 m = NULL;
1764 socket_lock(so, 0);
1765 if (mopts != NULL)
1766 IMO_REMREF(mopts);
1767
1768 if (error == 0 && nstat_collect) {
1769 boolean_t cell, wifi, wired;
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));
1774 wired = (!wifi && IFNET_IS_WIRED(ro.ro_rt->rt_ifp));
1775 } else {
1776 cell = wifi = wired = FALSE;
1777 }
1778 INP_ADD_STAT(inp, cell, wifi, wired, txpackets, 1);
1779 INP_ADD_STAT(inp, cell, wifi, wired, txbytes, len);
1780 }
1781
1782 if (flowadv && (adv->code == FADV_FLOW_CONTROLLED ||
1783 adv->code == FADV_SUSPENDED)) {
1784 /*
1785 * return a hint to the application that
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
1796 /* Synchronize PCB cached route */
1797 inp_route_copyin(inp, &ro);
1798
1799 abort:
1800 if (udp_dodisconnect) {
1801 /* Always discard the cached route for unconnected socket */
1802 ROUTE_RELEASE(&inp->inp_route);
1803 in_pcbdisconnect(inp);
1804 inp->inp_laddr = origladdr; /* XXX rehash? */
1805 /* no reference needed */
1806 inp->inp_last_outifp = origoutifp;
1807 } else if (inp->inp_route.ro_rt != NULL) {
1808 struct rtentry *rt = inp->inp_route.ro_rt;
1809 struct ifnet *outifp;
1810
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 */
1816 if (rt == NULL)
1817 ROUTE_RELEASE(&inp->inp_route);
1818
1819 /*
1820 * If the destination route is unicast, update outifp with
1821 * that of the route interface used by IP.
1822 */
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 }
1834 } else {
1835 ROUTE_RELEASE(&inp->inp_route);
1836 }
1837
1838 /*
1839 * If output interface was cellular/expensive, and this socket is
1840 * denied access to it, generate an event.
1841 */
1842 if (error != 0 && (ipoa.ipoa_retflags & IPOARF_IFDENIED) &&
1843 (INP_NO_CELLULAR(inp) || INP_NO_EXPENSIVE(inp)))
1844 soevent(so, (SO_FILT_HINT_LOCKED|SO_FILT_HINT_IFDENIED));
1845
1846 release:
1847 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_END, error, 0, 0, 0, 0);
1848
1849 if (m != NULL)
1850 m_freem(m);
1851
1852 if (outif != NULL)
1853 ifnet_release(outif);
1854
1855 return (error);
1856 }
1857
1858 u_int32_t udp_sendspace = 9216; /* really max datagram size */
1859 /* 187 1K datagrams (approx 192 KB) */
1860 u_int32_t udp_recvspace = 187 * (1024 +
1861 #if INET6
1862 sizeof (struct sockaddr_in6)
1863 #else /* !INET6 */
1864 sizeof (struct sockaddr_in)
1865 #endif /* !INET6 */
1866 );
1867
1868 /* Check that the values of udp send and recv space do not exceed sb_max */
1869 static int
1870 sysctl_udp_sospace(struct sysctl_oid *oidp, void *arg1, int arg2,
1871 struct sysctl_req *req)
1872 {
1873 #pragma unused(arg1, arg2)
1874 u_int32_t new_value = 0, *space_p = NULL;
1875 int changed = 0, error = 0;
1876 u_quad_t sb_effective_max = (sb_max/(MSIZE+MCLBYTES)) * MCLBYTES;
1877
1878 switch (oidp->oid_number) {
1879 case UDPCTL_RECVSPACE:
1880 space_p = &udp_recvspace;
1881 break;
1882 case UDPCTL_MAXDGRAM:
1883 space_p = &udp_sendspace;
1884 break;
1885 default:
1886 return (EINVAL);
1887 }
1888 error = sysctl_io_number(req, *space_p, sizeof (u_int32_t),
1889 &new_value, &changed);
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);
1897 }
1898
1899 SYSCTL_PROC(_net_inet_udp, UDPCTL_RECVSPACE, recvspace,
1900 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &udp_recvspace, 0,
1901 &sysctl_udp_sospace, "IU", "Maximum incoming UDP datagram size");
1902
1903 SYSCTL_PROC(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram,
1904 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &udp_sendspace, 0,
1905 &sysctl_udp_sospace, "IU", "Maximum outgoing UDP datagram size");
1906
1907 static int
1908 udp_abort(struct socket *so)
1909 {
1910 struct inpcb *inp;
1911
1912 inp = sotoinpcb(so);
1913 if (inp == NULL) {
1914 panic("%s: so=%p null inp\n", __func__, so);
1915 /* NOTREACHED */
1916 }
1917 soisdisconnected(so);
1918 in_pcbdetach(inp);
1919 return (0);
1920 }
1921
1922 static int
1923 udp_attach(struct socket *so, int proto, struct proc *p)
1924 {
1925 #pragma unused(proto)
1926 struct inpcb *inp;
1927 int error;
1928
1929 inp = sotoinpcb(so);
1930 if (inp != NULL) {
1931 panic("%s so=%p inp=%p\n", __func__, so, inp);
1932 /* NOTREACHED */
1933 }
1934 error = in_pcballoc(so, &udbinfo, p);
1935 if (error != 0)
1936 return (error);
1937 error = soreserve(so, udp_sendspace, udp_recvspace);
1938 if (error != 0)
1939 return (error);
1940 inp = (struct inpcb *)so->so_pcb;
1941 inp->inp_vflag |= INP_IPV4;
1942 inp->inp_ip_ttl = ip_defttl;
1943 if (nstat_collect)
1944 nstat_udp_new_pcb(inp);
1945 return (0);
1946 }
1947
1948 static int
1949 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
1950 {
1951 struct inpcb *inp;
1952 int error;
1953
1954 if (nam->sa_family != 0 && nam->sa_family != AF_INET &&
1955 nam->sa_family != AF_INET6)
1956 return (EAFNOSUPPORT);
1957
1958 inp = sotoinpcb(so);
1959 if (inp == NULL)
1960 return (EINVAL);
1961 error = in_pcbbind(inp, nam, p);
1962 return (error);
1963 }
1964
1965 static int
1966 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
1967 {
1968 struct inpcb *inp;
1969 int error;
1970
1971 inp = sotoinpcb(so);
1972 if (inp == NULL)
1973 return (EINVAL);
1974 if (inp->inp_faddr.s_addr != INADDR_ANY)
1975 return (EISCONN);
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
1995 error = in_pcbconnect(inp, nam, p, IFSCOPE_NONE, NULL);
1996 if (error == 0) {
1997 soisconnected(so);
1998 if (inp->inp_flowhash == 0)
1999 inp->inp_flowhash = inp_calc_flowhash(inp);
2000 }
2001 return (error);
2002 }
2003
2004 int
2005 udp_connectx_common(struct socket *so, int af,
2006 struct sockaddr_list **src_sl, struct sockaddr_list **dst_sl,
2007 struct proc *p, uint32_t ifscope, sae_associd_t aid, sae_connid_t *pcid,
2008 uint32_t flags, void *arg, uint32_t arglen,
2009 struct uio *uio, user_ssize_t *bytes_written)
2010 {
2011 #pragma unused(aid, flags, arg, arglen)
2012 struct sockaddr_entry *src_se = NULL, *dst_se = NULL;
2013 struct inpcb *inp = sotoinpcb(so);
2014 int error;
2015 user_ssize_t datalen = 0;
2016
2017 if (inp == NULL)
2018 return (EINVAL);
2019
2020 VERIFY(dst_sl != NULL);
2021
2022 /* select source (if specified) and destination addresses */
2023 error = in_selectaddrs(af, src_sl, &src_se, dst_sl, &dst_se);
2024 if (error != 0)
2025 return (error);
2026
2027 VERIFY(*dst_sl != NULL && dst_se != NULL);
2028 VERIFY(src_se == NULL || *src_sl != NULL);
2029 VERIFY(dst_se->se_addr->sa_family == af);
2030 VERIFY(src_se == NULL || src_se->se_addr->sa_family == af);
2031
2032 #if NECP
2033 inp_update_necp_policy(inp, src_se ? src_se->se_addr : NULL,
2034 dst_se ? dst_se->se_addr : NULL, ifscope);
2035 #endif /* NECP */
2036
2037 /* bind socket to the specified interface, if requested */
2038 if (ifscope != IFSCOPE_NONE &&
2039 (error = inp_bindif(inp, ifscope, NULL)) != 0)
2040 return (error);
2041
2042 /* if source address and/or port is specified, bind to it */
2043 if (src_se != NULL) {
2044 struct sockaddr *sa = src_se->se_addr;
2045 error = sobindlock(so, sa, 0); /* already locked */
2046 if (error != 0)
2047 return (error);
2048 }
2049
2050 switch (af) {
2051 case AF_INET:
2052 error = udp_connect(so, dst_se->se_addr, p);
2053 break;
2054 #if INET6
2055 case AF_INET6:
2056 error = udp6_connect(so, dst_se->se_addr, p);
2057 break;
2058 #endif /* INET6 */
2059 default:
2060 VERIFY(0);
2061 /* NOTREACHED */
2062 }
2063
2064 if (error != 0)
2065 return (error);
2066
2067 /*
2068 * If there is data, copy it. DATA_IDEMPOTENT is ignored.
2069 * CONNECT_RESUME_ON_READ_WRITE is ignored.
2070 */
2071 if (uio != NULL) {
2072 socket_unlock(so, 0);
2073
2074 VERIFY(bytes_written != NULL);
2075
2076 datalen = uio_resid(uio);
2077 error = so->so_proto->pr_usrreqs->pru_sosend(so, NULL,
2078 (uio_t)uio, NULL, NULL, 0);
2079 socket_lock(so, 0);
2080
2081 /* If error returned is EMSGSIZE, for example, disconnect */
2082 if (error == 0 || error == EWOULDBLOCK)
2083 *bytes_written = datalen - uio_resid(uio);
2084 else
2085 (void) so->so_proto->pr_usrreqs->pru_disconnectx(so,
2086 SAE_ASSOCID_ANY, SAE_CONNID_ANY);
2087 /*
2088 * mask the EWOULDBLOCK error so that the caller
2089 * knows that atleast the connect was successful.
2090 */
2091 if (error == EWOULDBLOCK)
2092 error = 0;
2093 }
2094
2095 if (error == 0 && pcid != NULL)
2096 *pcid = 1; /* there is only 1 connection for UDP */
2097
2098 return (error);
2099 }
2100
2101 static int
2102 udp_connectx(struct socket *so, struct sockaddr_list **src_sl,
2103 struct sockaddr_list **dst_sl, struct proc *p, uint32_t ifscope,
2104 sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg,
2105 uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written)
2106 {
2107 return (udp_connectx_common(so, AF_INET, src_sl, dst_sl,
2108 p, ifscope, aid, pcid, flags, arg, arglen, uio, bytes_written));
2109 }
2110
2111 static int
2112 udp_detach(struct socket *so)
2113 {
2114 struct inpcb *inp;
2115
2116 inp = sotoinpcb(so);
2117 if (inp == NULL) {
2118 panic("%s: so=%p null inp\n", __func__, so);
2119 /* NOTREACHED */
2120 }
2121
2122 /*
2123 * If this is a socket that does not want to wakeup the device
2124 * for it's traffic, the application might be waiting for
2125 * close to complete before going to sleep. Send a notification
2126 * for this kind of sockets
2127 */
2128 if (so->so_options & SO_NOWAKEFROMSLEEP)
2129 socket_post_kev_msg_closed(so);
2130
2131 in_pcbdetach(inp);
2132 inp->inp_state = INPCB_STATE_DEAD;
2133 return (0);
2134 }
2135
2136 static int
2137 udp_disconnect(struct socket *so)
2138 {
2139 struct inpcb *inp;
2140
2141 inp = sotoinpcb(so);
2142 if (inp == NULL
2143 #if NECP
2144 || (necp_socket_should_use_flow_divert(inp))
2145 #endif /* NECP */
2146 )
2147 return (inp == NULL ? EINVAL : EPROTOTYPE);
2148 if (inp->inp_faddr.s_addr == INADDR_ANY)
2149 return (ENOTCONN);
2150
2151 in_pcbdisconnect(inp);
2152
2153 /* reset flow controlled state, just in case */
2154 inp_reset_fc_state(inp);
2155
2156 inp->inp_laddr.s_addr = INADDR_ANY;
2157 so->so_state &= ~SS_ISCONNECTED; /* XXX */
2158 inp->inp_last_outifp = NULL;
2159 return (0);
2160 }
2161
2162 static int
2163 udp_disconnectx(struct socket *so, sae_associd_t aid, sae_connid_t cid)
2164 {
2165 #pragma unused(cid)
2166 if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL)
2167 return (EINVAL);
2168
2169 return (udp_disconnect(so));
2170 }
2171
2172 static int
2173 udp_send(struct socket *so, int flags, struct mbuf *m,
2174 struct sockaddr *addr, struct mbuf *control, struct proc *p)
2175 {
2176 #ifndef FLOW_DIVERT
2177 #pragma unused(flags)
2178 #endif /* !(FLOW_DIVERT) */
2179 struct inpcb *inp;
2180
2181 inp = sotoinpcb(so);
2182 if (inp == NULL) {
2183 if (m != NULL)
2184 m_freem(m);
2185 if (control != NULL)
2186 m_freem(control);
2187 return (EINVAL);
2188 }
2189
2190 #if NECP
2191 #if FLOW_DIVERT
2192 if (necp_socket_should_use_flow_divert(inp)) {
2193 /* Implicit connect */
2194 return (flow_divert_implicit_data_out(so, flags, m, addr,
2195 control, p));
2196 }
2197 #endif /* FLOW_DIVERT */
2198 #endif /* NECP */
2199
2200 return (udp_output(inp, m, addr, control, p));
2201 }
2202
2203 int
2204 udp_shutdown(struct socket *so)
2205 {
2206 struct inpcb *inp;
2207
2208 inp = sotoinpcb(so);
2209 if (inp == NULL)
2210 return (EINVAL);
2211 socantsendmore(so);
2212 return (0);
2213 }
2214
2215 int
2216 udp_lock(struct socket *so, int refcount, void *debug)
2217 {
2218 void *lr_saved;
2219
2220 if (debug == NULL)
2221 lr_saved = __builtin_return_address(0);
2222 else
2223 lr_saved = debug;
2224
2225 if (so->so_pcb != NULL) {
2226 lck_mtx_assert(&((struct inpcb *)so->so_pcb)->inpcb_mtx,
2227 LCK_MTX_ASSERT_NOTOWNED);
2228 lck_mtx_lock(&((struct inpcb *)so->so_pcb)->inpcb_mtx);
2229 } else {
2230 panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__,
2231 so, lr_saved, solockhistory_nr(so));
2232 /* NOTREACHED */
2233 }
2234 if (refcount)
2235 so->so_usecount++;
2236
2237 so->lock_lr[so->next_lock_lr] = lr_saved;
2238 so->next_lock_lr = (so->next_lock_lr+1) % SO_LCKDBG_MAX;
2239 return (0);
2240 }
2241
2242 int
2243 udp_unlock(struct socket *so, int refcount, void *debug)
2244 {
2245 void *lr_saved;
2246
2247 if (debug == NULL)
2248 lr_saved = __builtin_return_address(0);
2249 else
2250 lr_saved = debug;
2251
2252 if (refcount) {
2253 VERIFY(so->so_usecount > 0);
2254 so->so_usecount--;
2255 }
2256 if (so->so_pcb == NULL) {
2257 panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__,
2258 so, lr_saved, solockhistory_nr(so));
2259 /* NOTREACHED */
2260 } else {
2261 lck_mtx_assert(&((struct inpcb *)so->so_pcb)->inpcb_mtx,
2262 LCK_MTX_ASSERT_OWNED);
2263 so->unlock_lr[so->next_unlock_lr] = lr_saved;
2264 so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX;
2265 lck_mtx_unlock(&((struct inpcb *)so->so_pcb)->inpcb_mtx);
2266 }
2267 return (0);
2268 }
2269
2270 lck_mtx_t *
2271 udp_getlock(struct socket *so, int locktype)
2272 {
2273 #pragma unused(locktype)
2274 struct inpcb *inp = sotoinpcb(so);
2275
2276 if (so->so_pcb == NULL) {
2277 panic("%s: so=%p NULL so_pcb lrh= %s\n", __func__,
2278 so, solockhistory_nr(so));
2279 /* NOTREACHED */
2280 }
2281 return (&inp->inpcb_mtx);
2282 }
2283
2284 /*
2285 * UDP garbage collector callback (inpcb_timer_func_t).
2286 *
2287 * Returns > 0 to keep timer active.
2288 */
2289 static void
2290 udp_gc(struct inpcbinfo *ipi)
2291 {
2292 struct inpcb *inp, *inpnxt;
2293 struct socket *so;
2294
2295 if (lck_rw_try_lock_exclusive(ipi->ipi_lock) == FALSE) {
2296 if (udp_gc_done == TRUE) {
2297 udp_gc_done = FALSE;
2298 /* couldn't get the lock, must lock next time */
2299 atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1);
2300 return;
2301 }
2302 lck_rw_lock_exclusive(ipi->ipi_lock);
2303 }
2304
2305 udp_gc_done = TRUE;
2306
2307 for (inp = udb.lh_first; inp != NULL; inp = inpnxt) {
2308 inpnxt = inp->inp_list.le_next;
2309
2310 /*
2311 * Skip unless it's STOPUSING; garbage collector will
2312 * be triggered by in_pcb_checkstate() upon setting
2313 * wantcnt to that value. If the PCB is already dead,
2314 * keep gc active to anticipate wantcnt changing.
2315 */
2316 if (inp->inp_wantcnt != WNT_STOPUSING)
2317 continue;
2318
2319 /*
2320 * Skip if busy, no hurry for cleanup. Keep gc active
2321 * and try the lock again during next round.
2322 */
2323 if (!lck_mtx_try_lock(&inp->inpcb_mtx)) {
2324 atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1);
2325 continue;
2326 }
2327
2328 /*
2329 * Keep gc active unless usecount is 0.
2330 */
2331 so = inp->inp_socket;
2332 if (so->so_usecount == 0) {
2333 if (inp->inp_state != INPCB_STATE_DEAD) {
2334 #if INET6
2335 if (SOCK_CHECK_DOM(so, PF_INET6))
2336 in6_pcbdetach(inp);
2337 else
2338 #endif /* INET6 */
2339 in_pcbdetach(inp);
2340 }
2341 in_pcbdispose(inp);
2342 } else {
2343 lck_mtx_unlock(&inp->inpcb_mtx);
2344 atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1);
2345 }
2346 }
2347 lck_rw_done(ipi->ipi_lock);
2348 }
2349
2350 static int
2351 udp_getstat SYSCTL_HANDLER_ARGS
2352 {
2353 #pragma unused(oidp, arg1, arg2)
2354 if (req->oldptr == USER_ADDR_NULL)
2355 req->oldlen = (size_t)sizeof (struct udpstat);
2356
2357 return (SYSCTL_OUT(req, &udpstat, MIN(sizeof (udpstat), req->oldlen)));
2358 }
2359
2360 void
2361 udp_in_cksum_stats(u_int32_t len)
2362 {
2363 udpstat.udps_rcv_swcsum++;
2364 udpstat.udps_rcv_swcsum_bytes += len;
2365 }
2366
2367 void
2368 udp_out_cksum_stats(u_int32_t len)
2369 {
2370 udpstat.udps_snd_swcsum++;
2371 udpstat.udps_snd_swcsum_bytes += len;
2372 }
2373
2374 #if INET6
2375 void
2376 udp_in6_cksum_stats(u_int32_t len)
2377 {
2378 udpstat.udps_rcv6_swcsum++;
2379 udpstat.udps_rcv6_swcsum_bytes += len;
2380 }
2381
2382 void
2383 udp_out6_cksum_stats(u_int32_t len)
2384 {
2385 udpstat.udps_snd6_swcsum++;
2386 udpstat.udps_snd6_swcsum_bytes += len;
2387 }
2388 #endif /* INET6 */
2389
2390 /*
2391 * Checksum extended UDP header and data.
2392 */
2393 static int
2394 udp_input_checksum(struct mbuf *m, struct udphdr *uh, int off, int ulen)
2395 {
2396 struct ifnet *ifp = m->m_pkthdr.rcvif;
2397 struct ip *ip = mtod(m, struct ip *);
2398 struct ipovly *ipov = (struct ipovly *)ip;
2399
2400 if (uh->uh_sum == 0) {
2401 udpstat.udps_nosum++;
2402 return (0);
2403 }
2404
2405 if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) ||
2406 (m->m_pkthdr.pkt_flags & PKTF_LOOP)) &&
2407 (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) {
2408 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) {
2409 uh->uh_sum = m->m_pkthdr.csum_rx_val;
2410 } else {
2411 uint16_t sum = m->m_pkthdr.csum_rx_val;
2412 uint16_t start = m->m_pkthdr.csum_rx_start;
2413
2414 /*
2415 * Perform 1's complement adjustment of octets
2416 * that got included/excluded in the hardware-
2417 * calculated checksum value. Ignore cases
2418 * where the value includes or excludes the
2419 * IP header span, as the sum for those octets
2420 * would already be 0xffff and thus no-op.
2421 */
2422 if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) &&
2423 start != 0 && (off - start) != off) {
2424 #if BYTE_ORDER != BIG_ENDIAN
2425 if (start < off) {
2426 HTONS(ip->ip_len);
2427 HTONS(ip->ip_off);
2428 }
2429 #endif /* BYTE_ORDER != BIG_ENDIAN */
2430 /* callee folds in sum */
2431 sum = m_adj_sum16(m, start, off, sum);
2432 #if BYTE_ORDER != BIG_ENDIAN
2433 if (start < off) {
2434 NTOHS(ip->ip_off);
2435 NTOHS(ip->ip_len);
2436 }
2437 #endif /* BYTE_ORDER != BIG_ENDIAN */
2438 }
2439
2440 /* callee folds in sum */
2441 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
2442 ip->ip_dst.s_addr, sum + htonl(ulen + IPPROTO_UDP));
2443 }
2444 uh->uh_sum ^= 0xffff;
2445 } else {
2446 uint16_t ip_sum;
2447 char b[9];
2448
2449 bcopy(ipov->ih_x1, b, sizeof (ipov->ih_x1));
2450 bzero(ipov->ih_x1, sizeof (ipov->ih_x1));
2451 ip_sum = ipov->ih_len;
2452 ipov->ih_len = uh->uh_ulen;
2453 uh->uh_sum = in_cksum(m, ulen + sizeof (struct ip));
2454 bcopy(b, ipov->ih_x1, sizeof (ipov->ih_x1));
2455 ipov->ih_len = ip_sum;
2456
2457 udp_in_cksum_stats(ulen);
2458 }
2459
2460 if (uh->uh_sum != 0) {
2461 udpstat.udps_badsum++;
2462 IF_UDP_STATINC(ifp, badchksum);
2463 return (-1);
2464 }
2465
2466 return (0);
2467 }
2468
2469 void
2470 udp_fill_keepalive_offload_frames(ifnet_t ifp,
2471 struct ifnet_keepalive_offload_frame *frames_array,
2472 u_int32_t frames_array_count, size_t frame_data_offset,
2473 u_int32_t *used_frames_count)
2474 {
2475 struct inpcb *inp;
2476 inp_gen_t gencnt;
2477 u_int32_t frame_index = *used_frames_count;
2478
2479 if (ifp == NULL || frames_array == NULL ||
2480 frames_array_count == 0 ||
2481 frame_index >= frames_array_count ||
2482 frame_data_offset >= IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE)
2483 return;
2484
2485 lck_rw_lock_shared(udbinfo.ipi_lock);
2486 gencnt = udbinfo.ipi_gencnt;
2487 LIST_FOREACH(inp, udbinfo.ipi_listhead, inp_list) {
2488 struct socket *so;
2489 u_int8_t *data;
2490 struct ifnet_keepalive_offload_frame *frame;
2491 struct mbuf *m = NULL;
2492
2493 if (frame_index >= frames_array_count)
2494 break;
2495
2496 if (inp->inp_gencnt > gencnt ||
2497 inp->inp_state == INPCB_STATE_DEAD)
2498 continue;
2499
2500 if ((so = inp->inp_socket) == NULL ||
2501 (so->so_state & SS_DEFUNCT))
2502 continue;
2503 /*
2504 * check for keepalive offload flag without socket
2505 * lock to avoid a deadlock
2506 */
2507 if (!(inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD)) {
2508 continue;
2509 }
2510
2511 udp_lock(so, 1, 0);
2512 if (!(inp->inp_vflag & (INP_IPV4 | INP_IPV6))) {
2513 udp_unlock(so, 1, 0);
2514 continue;
2515 }
2516 if ((inp->inp_vflag & INP_IPV4) &&
2517 (inp->inp_laddr.s_addr == INADDR_ANY ||
2518 inp->inp_faddr.s_addr == INADDR_ANY)) {
2519 udp_unlock(so, 1, 0);
2520 continue;
2521 }
2522 if ((inp->inp_vflag & INP_IPV6) &&
2523 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ||
2524 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))) {
2525 udp_unlock(so, 1, 0);
2526 continue;
2527 }
2528 if (inp->inp_lport == 0 || inp->inp_fport == 0) {
2529 udp_unlock(so, 1, 0);
2530 continue;
2531 }
2532 if (inp->inp_last_outifp == NULL ||
2533 inp->inp_last_outifp->if_index != ifp->if_index) {
2534 udp_unlock(so, 1, 0);
2535 continue;
2536 }
2537 if ((inp->inp_vflag & INP_IPV4)) {
2538 if ((frame_data_offset + sizeof(struct udpiphdr) +
2539 inp->inp_keepalive_datalen) >
2540 IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) {
2541 udp_unlock(so, 1, 0);
2542 continue;
2543 }
2544 if ((sizeof(struct udpiphdr) +
2545 inp->inp_keepalive_datalen) > _MHLEN) {
2546 udp_unlock(so, 1, 0);
2547 continue;
2548 }
2549 } else {
2550 if ((frame_data_offset + sizeof(struct ip6_hdr) +
2551 sizeof(struct udphdr) +
2552 inp->inp_keepalive_datalen) >
2553 IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) {
2554 udp_unlock(so, 1, 0);
2555 continue;
2556 }
2557 if ((sizeof(struct ip6_hdr) + sizeof(struct udphdr) +
2558 inp->inp_keepalive_datalen) > _MHLEN) {
2559 udp_unlock(so, 1, 0);
2560 continue;
2561 }
2562 }
2563 MGETHDR(m, M_WAIT, MT_HEADER);
2564 if (m == NULL) {
2565 udp_unlock(so, 1, 0);
2566 continue;
2567 }
2568 /*
2569 * This inp has all the information that is needed to
2570 * generate an offload frame.
2571 */
2572 if (inp->inp_vflag & INP_IPV4) {
2573 struct ip *ip;
2574 struct udphdr *udp;
2575
2576 frame = &frames_array[frame_index];
2577 frame->length = frame_data_offset +
2578 sizeof(struct udpiphdr) +
2579 inp->inp_keepalive_datalen;
2580 frame->ether_type =
2581 IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV4;
2582 frame->interval = inp->inp_keepalive_interval;
2583 switch (inp->inp_keepalive_type) {
2584 case UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY:
2585 frame->type =
2586 IFNET_KEEPALIVE_OFFLOAD_FRAME_AIRPLAY;
2587 break;
2588 default:
2589 break;
2590 }
2591 data = mtod(m, u_int8_t *);
2592 bzero(data, sizeof(struct udpiphdr));
2593 ip = (__typeof__(ip))(void *)data;
2594 udp = (__typeof__(udp))(void *) (data +
2595 sizeof(struct ip));
2596 m->m_len = sizeof(struct udpiphdr);
2597 data = data + sizeof(struct udpiphdr);
2598 if (inp->inp_keepalive_datalen > 0 &&
2599 inp->inp_keepalive_data != NULL) {
2600 bcopy(inp->inp_keepalive_data, data,
2601 inp->inp_keepalive_datalen);
2602 m->m_len += inp->inp_keepalive_datalen;
2603 }
2604 m->m_pkthdr.len = m->m_len;
2605
2606 ip->ip_v = IPVERSION;
2607 ip->ip_hl = (sizeof(struct ip) >> 2);
2608 ip->ip_p = IPPROTO_UDP;
2609 ip->ip_len = htons(sizeof(struct udpiphdr) +
2610 (u_short)inp->inp_keepalive_datalen);
2611 ip->ip_ttl = inp->inp_ip_ttl;
2612 ip->ip_tos = inp->inp_ip_tos;
2613 ip->ip_src = inp->inp_laddr;
2614 ip->ip_dst = inp->inp_faddr;
2615 ip->ip_sum = in_cksum_hdr_opt(ip);
2616
2617 udp->uh_sport = inp->inp_lport;
2618 udp->uh_dport = inp->inp_fport;
2619 udp->uh_ulen = htons(sizeof(struct udphdr) +
2620 (u_short)inp->inp_keepalive_datalen);
2621
2622 if (!(inp->inp_flags & INP_UDP_NOCKSUM)) {
2623 udp->uh_sum = in_pseudo(ip->ip_src.s_addr,
2624 ip->ip_dst.s_addr,
2625 htons(sizeof(struct udphdr) +
2626 (u_short)inp->inp_keepalive_datalen +
2627 IPPROTO_UDP));
2628 m->m_pkthdr.csum_flags = CSUM_UDP;
2629 m->m_pkthdr.csum_data = offsetof(struct udphdr,
2630 uh_sum);
2631 }
2632 m->m_pkthdr.pkt_proto = IPPROTO_UDP;
2633 in_delayed_cksum(m);
2634 bcopy(m->m_data, frame->data + frame_data_offset,
2635 m->m_len);
2636 } else {
2637 struct ip6_hdr *ip6;
2638 struct udphdr *udp6;
2639
2640 VERIFY(inp->inp_vflag & INP_IPV6);
2641 frame = &frames_array[frame_index];
2642 frame->length = frame_data_offset +
2643 sizeof(struct ip6_hdr) +
2644 sizeof(struct udphdr) +
2645 inp->inp_keepalive_datalen;
2646 frame->ether_type =
2647 IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV6;
2648 frame->interval = inp->inp_keepalive_interval;
2649 switch (inp->inp_keepalive_type) {
2650 case UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY:
2651 frame->type =
2652 IFNET_KEEPALIVE_OFFLOAD_FRAME_AIRPLAY;
2653 break;
2654 default:
2655 break;
2656 }
2657 data = mtod(m, u_int8_t *);
2658 bzero(data, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
2659 ip6 = (__typeof__(ip6))(void *)data;
2660 udp6 = (__typeof__(udp6))(void *)(data +
2661 sizeof(struct ip6_hdr));
2662 m->m_len = sizeof(struct ip6_hdr) +
2663 sizeof(struct udphdr);
2664 data = data + (sizeof(struct ip6_hdr) +
2665 sizeof(struct udphdr));
2666 if (inp->inp_keepalive_datalen > 0 &&
2667 inp->inp_keepalive_data != NULL) {
2668 bcopy(inp->inp_keepalive_data, data,
2669 inp->inp_keepalive_datalen);
2670 m->m_len += inp->inp_keepalive_datalen;
2671 }
2672 m->m_pkthdr.len = m->m_len;
2673 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
2674 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2675 ip6->ip6_vfc |= IPV6_VERSION;
2676 ip6->ip6_nxt = IPPROTO_UDP;
2677 ip6->ip6_hlim = ip6_defhlim;
2678 ip6->ip6_plen = htons(sizeof(struct udphdr) +
2679 (u_short)inp->inp_keepalive_datalen);
2680 ip6->ip6_src = inp->in6p_laddr;
2681 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
2682 ip6->ip6_src.s6_addr16[1] = 0;
2683
2684 ip6->ip6_dst = inp->in6p_faddr;
2685 if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
2686 ip6->ip6_dst.s6_addr16[1] = 0;
2687
2688 udp6->uh_sport = inp->in6p_lport;
2689 udp6->uh_dport = inp->in6p_fport;
2690 udp6->uh_ulen = htons(sizeof(struct udphdr) +
2691 (u_short)inp->inp_keepalive_datalen);
2692 if (!(inp->inp_flags & INP_UDP_NOCKSUM)) {
2693 udp6->uh_sum = in6_pseudo(&ip6->ip6_src,
2694 &ip6->ip6_dst,
2695 htonl(sizeof(struct udphdr) +
2696 (u_short)inp->inp_keepalive_datalen +
2697 IPPROTO_UDP));
2698 m->m_pkthdr.csum_flags = CSUM_UDPIPV6;
2699 m->m_pkthdr.csum_data = offsetof(struct udphdr,
2700 uh_sum);
2701 }
2702 m->m_pkthdr.pkt_proto = IPPROTO_UDP;
2703 in6_delayed_cksum(m);
2704 bcopy(m->m_data, frame->data + frame_data_offset,
2705 m->m_len);
2706 }
2707 if (m != NULL) {
2708 m_freem(m);
2709 m = NULL;
2710 }
2711 frame_index++;
2712 udp_unlock(so, 1, 0);
2713 }
2714 lck_rw_done(udbinfo.ipi_lock);
2715 *used_frames_count = frame_index;
2716 }