2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
60 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
61 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.13 2001/08/08 18:59:54 ghelmer Exp $
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/malloc.h>
69 #include <sys/domain.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/sysctl.h>
74 #include <sys/syslog.h>
75 #include <sys/mcache.h>
76 #include <net/ntstat.h>
78 #include <kern/zalloc.h>
79 #include <mach/boolean.h>
82 #include <net/if_types.h>
83 #include <net/route.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
90 #include <netinet/ip6.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip_var.h>
96 #include <netinet6/in6_pcb.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/udp6_var.h>
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>
107 #include <netinet6/ipsec.h>
108 #include <netinet6/esp.h>
109 extern int ipsec_bypass
;
110 extern int esp_udp_encap_port
;
114 #include <net/necp.h>
118 #include <netinet/flow_divert.h>
119 #endif /* FLOW_DIVERT */
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)
129 * UDP protocol implementation.
130 * Per RFC 768, August, 1980.
133 static int udpcksum
= 1;
135 static int udpcksum
= 0; /* XXX */
137 SYSCTL_INT(_net_inet_udp
, UDPCTL_CHECKSUM
, checksum
,
138 CTLFLAG_RW
| CTLFLAG_LOCKED
, &udpcksum
, 0, "");
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");
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");
148 struct inpcbhead udb
; /* from udp_var.h */
149 #define udb6 udb /* for KAME src sync over BSD*'s */
150 struct inpcbinfo udbinfo
;
153 #define UDBHASHSIZE 16
156 /* Garbage collection performed during most recent udp_gc() run */
157 static boolean_t udp_gc_done
= FALSE
;
160 extern int fw_verbose
;
161 extern void ipfwsyslog( int level
, const char *format
,...);
162 extern void ipfw_stealth_stats_incr_udp(void);
164 /* Apple logging, log to ipfw.log */
165 #define log_in_vain_log(a) { \
166 if ((udp_log_in_vain == 3) && (fw_verbose == 2)) { \
168 } else if ((udp_log_in_vain == 4) && (fw_verbose == 2)) { \
169 ipfw_stealth_stats_incr_udp(); \
174 #else /* !IPFIREWALL */
175 #define log_in_vain_log( a ) { log a; }
176 #endif /* !IPFIREWALL */
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)");
185 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, pcbcount
,
186 CTLFLAG_RD
| CTLFLAG_LOCKED
, &udbinfo
.ipi_count
, 0,
187 "Number of active PCBs");
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");
196 struct sockaddr_in6 uin6_sin
;
197 u_char uin6_init_done
: 1;
200 struct ip6_hdr uip6_ip6
;
201 u_char uip6_init_done
: 1;
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
*);
219 static void udp_append(struct inpcb
*, struct ip
*, struct mbuf
*, int,
220 struct sockaddr_in
*, struct ifnet
*);
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
*);
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
,
248 udp_init(struct protosw
*pp
, struct domain
*dp
)
251 static int udp_initialized
= 0;
253 struct inpcbinfo
*pcbinfo
;
255 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
|PR_ATTACHED
)) == PR_ATTACHED
);
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");
272 * allocate lock group attribute and group for udp pcb mutexes
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__
);
284 udbinfo
.ipi_gc
= udp_gc
;
285 in_pcbinfo_attach(&udbinfo
);
289 udp_input(struct mbuf
*m
, int iphlen
)
294 struct mbuf
*opts
= NULL
;
295 int len
, isbroadcast
;
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;
303 struct udp_in6 udp_in6
;
304 struct udp_ip6 udp_ip6
;
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
));
311 bzero(&udp_in
, sizeof (udp_in
));
312 udp_in
.sin_len
= sizeof (struct sockaddr_in
);
313 udp_in
.sin_family
= AF_INET
;
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
;
320 udpstat
.udps_ipackets
++;
322 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_START
, 0,0,0,0,0);
324 /* Expect 32-bit aligned data pointer on strict-align platforms */
325 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
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.
333 if (iphlen
> sizeof (struct ip
)) {
334 ip_stripoptions(m
, (struct mbuf
*)0);
335 iphlen
= sizeof (struct ip
);
339 * Get IP and UDP header together in first mbuf.
341 ip
= mtod(m
, struct ip
*);
342 if (m
->m_len
< iphlen
+ sizeof (struct udphdr
)) {
343 m
= m_pullup(m
, iphlen
+ sizeof (struct udphdr
));
345 udpstat
.udps_hdrops
++;
346 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
,
350 ip
= mtod(m
, struct ip
*);
352 uh
= (struct udphdr
*)(void *)((caddr_t
)ip
+ iphlen
);
354 /* destination port of 0 is illegal, based on RFC768. */
355 if (uh
->uh_dport
== 0) {
356 IF_UDP_STATINC(ifp
, port0
);
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
);
364 * Make mbuf data length reflect UDP length.
365 * If not enough data to reflect UDP length, drop.
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
);
374 m_adj(m
, len
- ip
->ip_len
);
375 /* ip->ip_len = len; */
378 * Save a copy of the IP header in case we want restore it
379 * for sending an ICMP error message in response.
384 * Checksum extended UDP header and data.
386 if (udp_input_checksum(m
, uh
, iphlen
, len
))
389 isbroadcast
= in_broadcast(ip
->ip_dst
, ifp
);
391 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) || isbroadcast
) {
392 int reuse_sock
= 0, mcast_delivered
= 0;
394 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
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?)
412 * Construct sockaddr format source address.
414 udp_in
.sin_port
= uh
->uh_sport
;
415 udp_in
.sin_addr
= ip
->ip_src
;
417 * Locate pcb(s) for datagram.
418 * (Algorithm copied from raw_intr().)
421 udp_in6
.uin6_init_done
= udp_ip6
.uip6_init_done
= 0;
423 LIST_FOREACH(inp
, &udb
, inp_list
) {
428 if (inp
->inp_socket
== NULL
)
430 if (inp
!= sotoinpcb(inp
->inp_socket
)) {
431 panic("%s: bad so back ptr inp=%p\n",
436 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
439 if (inp_restricted_recv(inp
, ifp
))
442 if ((inp
->inp_moptions
== NULL
) &&
443 (ntohl(ip
->ip_dst
.s_addr
) !=
444 INADDR_ALLHOSTS_GROUP
) && (isbroadcast
== 0))
447 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) ==
451 udp_lock(inp
->inp_socket
, 1, 0);
453 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) ==
455 udp_unlock(inp
->inp_socket
, 1, 0);
459 if (inp
->inp_lport
!= uh
->uh_dport
) {
460 udp_unlock(inp
->inp_socket
, 1, 0);
463 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
464 if (inp
->inp_laddr
.s_addr
!=
466 udp_unlock(inp
->inp_socket
, 1, 0);
470 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
471 if (inp
->inp_faddr
.s_addr
!=
473 inp
->inp_fport
!= uh
->uh_sport
) {
474 udp_unlock(inp
->inp_socket
, 1, 0);
479 if (isbroadcast
== 0 && (ntohl(ip
->ip_dst
.s_addr
) !=
480 INADDR_ALLHOSTS_GROUP
)) {
481 struct sockaddr_in group
;
484 if ((imo
= inp
->inp_moptions
) == NULL
) {
485 udp_unlock(inp
->inp_socket
, 1, 0);
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
;
495 blocked
= imo_multi_filter(imo
, ifp
,
496 (struct sockaddr
*)&group
,
497 (struct sockaddr
*)&udp_in
);
498 if (blocked
== MCAST_PASS
)
502 if (!foundmembership
) {
503 udp_unlock(inp
->inp_socket
, 1, 0);
504 if (blocked
== MCAST_NOTSMEMBER
||
505 blocked
== MCAST_MUTED
)
506 udpstat
.udps_filtermcast
++;
512 reuse_sock
= (inp
->inp_socket
->so_options
&
513 (SO_REUSEPORT
|SO_REUSEADDR
));
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 */
526 struct mbuf
*n
= NULL
;
529 n
= m_copy(m
, 0, M_COPYALL
);
531 udp_append(inp
, ip
, m
,
532 iphlen
+ sizeof (struct udphdr
),
533 &udp_in
, &udp_in6
, &udp_ip6
, ifp
);
535 udp_append(inp
, ip
, m
,
536 iphlen
+ sizeof (struct udphdr
),
543 udp_unlock(inp
->inp_socket
, 1, 0);
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.
553 if (reuse_sock
== 0 || m
== NULL
)
557 * Expect 32-bit aligned data pointer on strict-align
560 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
562 * Recompute IP and UDP header pointers for new mbuf
564 ip
= mtod(m
, struct ip
*);
565 uh
= (struct udphdr
*)(void *)((caddr_t
)ip
+ iphlen
);
567 lck_rw_done(pcbinfo
->ipi_lock
);
569 if (mcast_delivered
== 0) {
571 * No matching pcb found; discard datagram.
572 * (No need to send an ICMP Port Unreachable
573 * for a broadcast or multicast datgram.)
575 udpstat
.udps_noportbcast
++;
576 IF_UDP_STATINC(ifp
, port_unreach
);
580 /* free the extra copy of mbuf or skipped by IPSec */
583 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
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.
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
);
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
,
609 * Expect 32-bit aligned data pointer on strict-align
612 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
614 ip
= mtod(m
, struct ip
*);
615 uh
= (struct udphdr
*)(void *)((caddr_t
)ip
+ iphlen
);
617 /* Check for NAT keepalive packet */
618 if (payload_len
== 1 && *(u_int8_t
*)
619 ((caddr_t
)uh
+ sizeof (struct udphdr
)) == 0xFF) {
621 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
,
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
,
629 /* preserve the udp header */
630 esp4_input(m
, iphlen
+ sizeof (struct udphdr
));
637 * Locate pcb for datagram.
639 inp
= in_pcblookup_hash(&udbinfo
, ip
->ip_src
, uh
->uh_sport
,
640 ip
->ip_dst
, uh
->uh_dport
, 1, ifp
);
642 IF_UDP_STATINC(ifp
, port_unreach
);
644 if (udp_log_in_vain
) {
645 char buf
[MAX_IPv4_STR_LEN
];
646 char buf2
[MAX_IPv4_STR_LEN
];
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
)))
667 udpstat
.udps_noport
++;
668 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
669 udpstat
.udps_noportbcast
++;
673 if (badport_bandlim(BANDLIM_ICMP_UNREACH
) < 0)
675 #endif /* ICMP_BANDLIM */
677 if (ifp
&& ifp
->if_type
!= IFT_LOOP
)
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);
685 udp_lock(inp
->inp_socket
, 1, 0);
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
);
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
);
702 * Construct sockaddr format source address.
703 * Stuff source address and datagram in user buffer.
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) {
711 if (inp
->inp_vflag
& INP_IPV6
) {
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
;
722 ret
= ip_savecontrol(inp
, &opts
, ip
, m
);
725 udp_unlock(inp
->inp_socket
, 1, 0);
729 m_adj(m
, iphlen
+ sizeof (struct udphdr
));
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
);
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
;
741 append_sa
= (struct sockaddr
*)&udp_in
;
744 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
745 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, m
->m_pkthdr
.len
);
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
++;
752 sorwakeup(inp
->inp_socket
);
754 udp_unlock(inp
->inp_socket
, 1, 0);
755 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
761 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
766 ip_2_ip6_hdr(struct ip6_hdr
*ip6
, struct ip
*ip
)
768 bzero(ip6
, sizeof (*ip6
));
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
;
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
;
786 * subroutine of udp_input(), mainly for source code readability.
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
)
794 udp_append(struct inpcb
*last
, struct ip
*ip
, struct mbuf
*n
, int off
,
795 struct sockaddr_in
*pudp_in
, struct ifnet
*ifp
)
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
));
806 if (mac_inpcb_check_deliver(last
, n
, AF_INET
, SOCK_DGRAM
) != 0) {
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) {
815 if (last
->inp_vflag
& INP_IPV6
) {
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;
822 savedflags
= last
->inp_flags
;
823 last
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
824 ret
= ip6_savecontrol(last
, n
, &opts
);
826 last
->inp_flags
= savedflags
;
829 last
->inp_flags
= savedflags
;
833 ret
= ip_savecontrol(last
, &opts
, ip
, n
);
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;
845 append_sa
= (struct sockaddr
*)&pudp_in6
->uin6_sin
;
848 append_sa
= (struct sockaddr
*)pudp_in
;
850 INP_ADD_STAT(last
, cell
, wifi
, wired
, rxpackets
, 1);
851 INP_ADD_STAT(last
, cell
, wifi
, wired
, rxbytes
,
854 so_recv_data_stat(last
->inp_socket
, n
, 0);
856 if (sbappendaddr(&last
->inp_socket
->so_rcv
, append_sa
,
857 n
, opts
, NULL
) == 0) {
858 udpstat
.udps_fullsock
++;
860 sorwakeup(last
->inp_socket
);
870 * Notify a udp user of an asynchronous error;
871 * just wake up so that he can collect error status.
874 udp_notify(struct inpcb
*inp
, int errno
)
876 inp
->inp_socket
->so_error
= errno
;
877 sorwakeup(inp
->inp_socket
);
878 sowwakeup(inp
->inp_socket
);
882 udp_ctlinput(int cmd
, struct sockaddr
*sa
, void *vip
)
885 void (*notify
)(struct inpcb
*, int) = udp_notify
;
886 struct in_addr faddr
;
889 faddr
= ((struct sockaddr_in
*)(void *)sa
)->sin_addr
;
890 if (sa
->sa_family
!= AF_INET
|| faddr
.s_addr
== INADDR_ANY
)
893 if (PRC_IS_REDIRECT(cmd
)) {
895 notify
= in_rtchange
;
896 } else if (cmd
== PRC_HOSTDEAD
) {
898 } else if ((unsigned)cmd
>= PRC_NCMDS
|| inetctlerrmap
[cmd
] == 0) {
904 bcopy(((caddr_t
)ip
+ (ip
->ip_hl
<< 2)), &uh
, sizeof (uh
));
905 inp
= in_pcblookup_hash(&udbinfo
, faddr
, uh
.uh_dport
,
906 ip
->ip_src
, uh
.uh_sport
, 0, NULL
);
907 if (inp
!= NULL
&& inp
->inp_socket
!= NULL
) {
908 udp_lock(inp
->inp_socket
, 1, 0);
909 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) ==
911 udp_unlock(inp
->inp_socket
, 1, 0);
914 (*notify
)(inp
, inetctlerrmap
[cmd
]);
915 udp_unlock(inp
->inp_socket
, 1, 0);
918 in_pcbnotifyall(&udbinfo
, faddr
, inetctlerrmap
[cmd
], notify
);
923 udp_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
928 /* Allow <SOL_SOCKET,SO_FLUSH> at this level */
929 if (sopt
->sopt_level
!= IPPROTO_UDP
&&
930 !(sopt
->sopt_level
== SOL_SOCKET
&& sopt
->sopt_name
== SO_FLUSH
))
931 return (ip_ctloutput(so
, sopt
));
936 switch (sopt
->sopt_dir
) {
938 switch (sopt
->sopt_name
) {
940 /* This option is settable only for UDP over IPv4 */
941 if (!(inp
->inp_vflag
& INP_IPV4
)) {
946 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
947 sizeof (optval
))) != 0)
951 inp
->inp_flags
|= INP_UDP_NOCKSUM
;
953 inp
->inp_flags
&= ~INP_UDP_NOCKSUM
;
955 case UDP_KEEPALIVE_OFFLOAD
:
957 struct udp_keepalive_offload ka
;
959 * If the socket is not connected, the stack will
960 * not know the destination address to put in the
961 * keepalive datagram. Return an error now instead
964 if (!(so
->so_state
& SS_ISCONNECTED
)) {
968 if (sopt
->sopt_valsize
!= sizeof(ka
)) {
972 if ((error
= sooptcopyin(sopt
, &ka
, sizeof(ka
),
976 /* application should specify the type */
980 if (ka
.ka_interval
== 0) {
982 * if interval is 0, disable the offload
985 if (inp
->inp_keepalive_data
!= NULL
)
986 FREE(inp
->inp_keepalive_data
,
988 inp
->inp_keepalive_data
= NULL
;
989 inp
->inp_keepalive_datalen
= 0;
990 inp
->inp_keepalive_interval
= 0;
991 inp
->inp_keepalive_type
= 0;
992 inp
->inp_flags2
&= ~INP2_KEEPALIVE_OFFLOAD
;
994 if (inp
->inp_keepalive_data
!= NULL
) {
995 FREE(inp
->inp_keepalive_data
,
997 inp
->inp_keepalive_data
= NULL
;
1000 inp
->inp_keepalive_datalen
= min(
1002 UDP_KEEPALIVE_OFFLOAD_DATA_SIZE
);
1003 if (inp
->inp_keepalive_datalen
> 0) {
1004 MALLOC(inp
->inp_keepalive_data
,
1006 inp
->inp_keepalive_datalen
,
1008 if (inp
->inp_keepalive_data
== NULL
) {
1009 inp
->inp_keepalive_datalen
= 0;
1014 inp
->inp_keepalive_data
,
1015 inp
->inp_keepalive_datalen
);
1017 inp
->inp_keepalive_datalen
= 0;
1019 inp
->inp_keepalive_interval
=
1020 min(UDP_KEEPALIVE_INTERVAL_MAX_SECONDS
,
1022 inp
->inp_keepalive_type
= ka
.ka_type
;
1023 inp
->inp_flags2
|= INP2_KEEPALIVE_OFFLOAD
;
1028 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
1029 sizeof (optval
))) != 0)
1032 error
= inp_flush(inp
, optval
);
1036 error
= ENOPROTOOPT
;
1042 switch (sopt
->sopt_name
) {
1044 optval
= inp
->inp_flags
& INP_UDP_NOCKSUM
;
1048 error
= ENOPROTOOPT
;
1052 error
= sooptcopyout(sopt
, &optval
, sizeof (optval
));
1059 udp_pcblist SYSCTL_HANDLER_ARGS
1061 #pragma unused(oidp, arg1, arg2)
1063 struct inpcb
*inp
, **inp_list
;
1068 * The process of preparing the TCB list is too time-consuming and
1069 * resource-intensive to repeat twice on every request.
1071 lck_rw_lock_exclusive(udbinfo
.ipi_lock
);
1072 if (req
->oldptr
== USER_ADDR_NULL
) {
1073 n
= udbinfo
.ipi_count
;
1074 req
->oldidx
= 2 * (sizeof (xig
))
1075 + (n
+ n
/8) * sizeof (struct xinpcb
);
1076 lck_rw_done(udbinfo
.ipi_lock
);
1080 if (req
->newptr
!= USER_ADDR_NULL
) {
1081 lck_rw_done(udbinfo
.ipi_lock
);
1086 * OK, now we're committed to doing something.
1088 gencnt
= udbinfo
.ipi_gencnt
;
1089 n
= udbinfo
.ipi_count
;
1091 bzero(&xig
, sizeof (xig
));
1092 xig
.xig_len
= sizeof (xig
);
1094 xig
.xig_gen
= gencnt
;
1095 xig
.xig_sogen
= so_gencnt
;
1096 error
= SYSCTL_OUT(req
, &xig
, sizeof (xig
));
1098 lck_rw_done(udbinfo
.ipi_lock
);
1102 * We are done if there is no pcb
1105 lck_rw_done(udbinfo
.ipi_lock
);
1109 inp_list
= _MALLOC(n
* sizeof (*inp_list
), M_TEMP
, M_WAITOK
);
1110 if (inp_list
== 0) {
1111 lck_rw_done(udbinfo
.ipi_lock
);
1115 for (inp
= LIST_FIRST(udbinfo
.ipi_listhead
), i
= 0; inp
&& i
< n
;
1116 inp
= LIST_NEXT(inp
, inp_list
)) {
1117 if (inp
->inp_gencnt
<= gencnt
&&
1118 inp
->inp_state
!= INPCB_STATE_DEAD
)
1119 inp_list
[i
++] = inp
;
1124 for (i
= 0; i
< n
; i
++) {
1126 if (inp
->inp_gencnt
<= gencnt
&&
1127 inp
->inp_state
!= INPCB_STATE_DEAD
) {
1130 bzero(&xi
, sizeof (xi
));
1131 xi
.xi_len
= sizeof (xi
);
1132 /* XXX should avoid extra copy */
1133 inpcb_to_compat(inp
, &xi
.xi_inp
);
1134 if (inp
->inp_socket
)
1135 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
1136 error
= SYSCTL_OUT(req
, &xi
, sizeof (xi
));
1141 * Give the user an updated idea of our state.
1142 * If the generation differs from what we told
1143 * her before, she knows that something happened
1144 * while we were processing this request, and it
1145 * might be necessary to retry.
1147 bzero(&xig
, sizeof (xig
));
1148 xig
.xig_len
= sizeof (xig
);
1149 xig
.xig_gen
= udbinfo
.ipi_gencnt
;
1150 xig
.xig_sogen
= so_gencnt
;
1151 xig
.xig_count
= udbinfo
.ipi_count
;
1152 error
= SYSCTL_OUT(req
, &xig
, sizeof (xig
));
1154 FREE(inp_list
, M_TEMP
);
1155 lck_rw_done(udbinfo
.ipi_lock
);
1159 SYSCTL_PROC(_net_inet_udp
, UDPCTL_PCBLIST
, pcblist
,
1160 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, udp_pcblist
,
1161 "S,xinpcb", "List of active UDP sockets");
1165 udp_pcblist64 SYSCTL_HANDLER_ARGS
1167 #pragma unused(oidp, arg1, arg2)
1169 struct inpcb
*inp
, **inp_list
;
1174 * The process of preparing the TCB list is too time-consuming and
1175 * resource-intensive to repeat twice on every request.
1177 lck_rw_lock_shared(udbinfo
.ipi_lock
);
1178 if (req
->oldptr
== USER_ADDR_NULL
) {
1179 n
= udbinfo
.ipi_count
;
1181 2 * (sizeof (xig
)) + (n
+ n
/8) * sizeof (struct xinpcb64
);
1182 lck_rw_done(udbinfo
.ipi_lock
);
1186 if (req
->newptr
!= USER_ADDR_NULL
) {
1187 lck_rw_done(udbinfo
.ipi_lock
);
1192 * OK, now we're committed to doing something.
1194 gencnt
= udbinfo
.ipi_gencnt
;
1195 n
= udbinfo
.ipi_count
;
1197 bzero(&xig
, sizeof (xig
));
1198 xig
.xig_len
= sizeof (xig
);
1200 xig
.xig_gen
= gencnt
;
1201 xig
.xig_sogen
= so_gencnt
;
1202 error
= SYSCTL_OUT(req
, &xig
, sizeof (xig
));
1204 lck_rw_done(udbinfo
.ipi_lock
);
1208 * We are done if there is no pcb
1211 lck_rw_done(udbinfo
.ipi_lock
);
1215 inp_list
= _MALLOC(n
* sizeof (*inp_list
), M_TEMP
, M_WAITOK
);
1216 if (inp_list
== 0) {
1217 lck_rw_done(udbinfo
.ipi_lock
);
1221 for (inp
= LIST_FIRST(udbinfo
.ipi_listhead
), i
= 0; inp
&& i
< n
;
1222 inp
= LIST_NEXT(inp
, inp_list
)) {
1223 if (inp
->inp_gencnt
<= gencnt
&&
1224 inp
->inp_state
!= INPCB_STATE_DEAD
)
1225 inp_list
[i
++] = inp
;
1230 for (i
= 0; i
< n
; i
++) {
1232 if (inp
->inp_gencnt
<= gencnt
&&
1233 inp
->inp_state
!= INPCB_STATE_DEAD
) {
1236 bzero(&xi
, sizeof (xi
));
1237 xi
.xi_len
= sizeof (xi
);
1238 inpcb_to_xinpcb64(inp
, &xi
);
1239 if (inp
->inp_socket
)
1240 sotoxsocket64(inp
->inp_socket
, &xi
.xi_socket
);
1241 error
= SYSCTL_OUT(req
, &xi
, sizeof (xi
));
1246 * Give the user an updated idea of our state.
1247 * If the generation differs from what we told
1248 * her before, she knows that something happened
1249 * while we were processing this request, and it
1250 * might be necessary to retry.
1252 bzero(&xig
, sizeof (xig
));
1253 xig
.xig_len
= sizeof (xig
);
1254 xig
.xig_gen
= udbinfo
.ipi_gencnt
;
1255 xig
.xig_sogen
= so_gencnt
;
1256 xig
.xig_count
= udbinfo
.ipi_count
;
1257 error
= SYSCTL_OUT(req
, &xig
, sizeof (xig
));
1259 FREE(inp_list
, M_TEMP
);
1260 lck_rw_done(udbinfo
.ipi_lock
);
1264 SYSCTL_PROC(_net_inet_udp
, OID_AUTO
, pcblist64
,
1265 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, udp_pcblist64
,
1266 "S,xinpcb64", "List of active UDP sockets");
1270 udp_pcblist_n SYSCTL_HANDLER_ARGS
1272 #pragma unused(oidp, arg1, arg2)
1273 return (get_pcblist_n(IPPROTO_UDP
, req
, &udbinfo
));
1276 SYSCTL_PROC(_net_inet_udp
, OID_AUTO
, pcblist_n
,
1277 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, udp_pcblist_n
,
1278 "S,xinpcb_n", "List of active UDP sockets");
1280 __private_extern__
void
1281 udp_get_ports_used(uint32_t ifindex
, int protocol
, uint32_t flags
,
1284 inpcb_get_ports_used(ifindex
, protocol
, flags
, bitfield
, &udbinfo
);
1287 __private_extern__
uint32_t
1288 udp_count_opportunistic(unsigned int ifindex
, u_int32_t flags
)
1290 return (inpcb_count_opportunistic(ifindex
, &udbinfo
, flags
));
1293 __private_extern__
uint32_t
1294 udp_find_anypcb_byaddr(struct ifaddr
*ifa
)
1296 return (inpcb_find_anypcb_byaddr(ifa
, &udbinfo
));
1300 udp_check_pktinfo(struct mbuf
*control
, struct ifnet
**outif
,
1301 struct in_addr
*laddr
)
1303 struct cmsghdr
*cm
= 0;
1304 struct in_pktinfo
*pktinfo
;
1311 * XXX: Currently, we assume all the optional information is stored
1314 if (control
->m_next
)
1317 if (control
->m_len
< CMSG_LEN(0))
1320 for (cm
= M_FIRST_CMSGHDR(control
); cm
;
1321 cm
= M_NXT_CMSGHDR(control
, cm
)) {
1322 if (cm
->cmsg_len
< sizeof (struct cmsghdr
) ||
1323 cm
->cmsg_len
> control
->m_len
)
1326 if (cm
->cmsg_level
!= IPPROTO_IP
|| cm
->cmsg_type
!= IP_PKTINFO
)
1329 if (cm
->cmsg_len
!= CMSG_LEN(sizeof (struct in_pktinfo
)))
1332 pktinfo
= (struct in_pktinfo
*)(void *)CMSG_DATA(cm
);
1334 /* Check for a valid ifindex in pktinfo */
1335 ifnet_head_lock_shared();
1337 if (pktinfo
->ipi_ifindex
> if_index
) {
1343 * If ipi_ifindex is specified it takes precedence
1344 * over ipi_spec_dst.
1346 if (pktinfo
->ipi_ifindex
) {
1347 ifp
= ifindex2ifnet
[pktinfo
->ipi_ifindex
];
1352 if (outif
!= NULL
) {
1353 ifnet_reference(ifp
);
1357 laddr
->s_addr
= INADDR_ANY
;
1364 * Use the provided ipi_spec_dst address for temp
1367 *laddr
= pktinfo
->ipi_spec_dst
;
1374 udp_output(struct inpcb
*inp
, struct mbuf
*m
, struct sockaddr
*addr
,
1375 struct mbuf
*control
, struct proc
*p
)
1377 struct udpiphdr
*ui
;
1378 int len
= m
->m_pkthdr
.len
;
1379 struct sockaddr_in
*sin
;
1380 struct in_addr origladdr
, laddr
, faddr
, pi_laddr
;
1381 u_short lport
, fport
;
1382 int error
= 0, udp_dodisconnect
= 0, pktinfo
= 0;
1383 struct socket
*so
= inp
->inp_socket
;
1385 struct mbuf
*inpopts
;
1386 struct ip_moptions
*mopts
;
1388 struct ip_out_args ipoa
=
1389 { IFSCOPE_NONE
, { 0 }, IPOAF_SELECT_SRCIF
, 0 };
1390 struct ifnet
*outif
= NULL
;
1391 struct flowadv
*adv
= &ipoa
.ipoa_flowadv
;
1392 mbuf_svc_class_t msc
= MBUF_SC_UNSPEC
;
1393 struct ifnet
*origoutifp
= NULL
;
1396 /* Enable flow advisory only when connected */
1397 flowadv
= (so
->so_state
& SS_ISCONNECTED
) ? 1 : 0;
1398 pi_laddr
.s_addr
= INADDR_ANY
;
1400 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
1402 lck_mtx_assert(&inp
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1403 if (control
!= NULL
) {
1404 msc
= mbuf_service_class_from_control(control
);
1405 VERIFY(outif
== NULL
);
1406 error
= udp_check_pktinfo(control
, &outif
, &pi_laddr
);
1413 ipoa
.ipoa_boundif
= outif
->if_index
;
1416 KERNEL_DEBUG(DBG_LAYER_OUT_BEG
, inp
->inp_fport
, inp
->inp_lport
,
1417 inp
->inp_laddr
.s_addr
, inp
->inp_faddr
.s_addr
,
1418 (htons((u_short
)len
+ sizeof (struct udphdr
))));
1420 if (len
+ sizeof (struct udpiphdr
) > IP_MAXPACKET
) {
1425 if (flowadv
&& INP_WAIT_FOR_IF_FEEDBACK(inp
)) {
1427 * The socket is flow-controlled, drop the packets
1428 * until the inp is not flow controlled
1434 * If socket was bound to an ifindex, tell ip_output about it.
1435 * If the ancillary IP_PKTINFO option contains an interface index,
1436 * it takes precedence over the one specified by IP_BOUND_IF.
1438 if (ipoa
.ipoa_boundif
== IFSCOPE_NONE
&&
1439 (inp
->inp_flags
& INP_BOUND_IF
)) {
1440 VERIFY(inp
->inp_boundifp
!= NULL
);
1441 ifnet_reference(inp
->inp_boundifp
); /* for this routine */
1443 ifnet_release(outif
);
1444 outif
= inp
->inp_boundifp
;
1445 ipoa
.ipoa_boundif
= outif
->if_index
;
1447 if (INP_NO_CELLULAR(inp
))
1448 ipoa
.ipoa_flags
|= IPOAF_NO_CELLULAR
;
1449 if (INP_NO_EXPENSIVE(inp
))
1450 ipoa
.ipoa_flags
|= IPOAF_NO_EXPENSIVE
;
1451 if (INP_AWDL_UNRESTRICTED(inp
))
1452 ipoa
.ipoa_flags
|= IPOAF_AWDL_UNRESTRICTED
;
1453 soopts
|= IP_OUTARGS
;
1456 * If there was a routing change, discard cached route and check
1457 * that we have a valid source address. Reacquire a new source
1458 * address if INADDR_ANY was specified.
1460 if (ROUTE_UNUSABLE(&inp
->inp_route
)) {
1461 struct in_ifaddr
*ia
= NULL
;
1463 ROUTE_RELEASE(&inp
->inp_route
);
1465 /* src address is gone? */
1466 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
&&
1467 (ia
= ifa_foraddr(inp
->inp_laddr
.s_addr
)) == NULL
) {
1468 if (!(inp
->inp_flags
& INP_INADDR_ANY
) ||
1469 (so
->so_state
& SS_ISCONNECTED
)) {
1472 * If the source address is gone, return an
1474 * - the source was specified
1475 * - the socket was already connected
1477 soevent(so
, (SO_FILT_HINT_LOCKED
|
1478 SO_FILT_HINT_NOSRCADDR
));
1479 error
= EADDRNOTAVAIL
;
1482 /* new src will be set later */
1483 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
1484 inp
->inp_last_outifp
= NULL
;
1488 IFA_REMREF(&ia
->ia_ifa
);
1492 * IP_PKTINFO option check. If a temporary scope or src address
1493 * is provided, use it for this packet only and make sure we forget
1494 * it after sending this datagram.
1496 if (pi_laddr
.s_addr
!= INADDR_ANY
||
1497 (ipoa
.ipoa_boundif
!= IFSCOPE_NONE
&& pktinfo
)) {
1498 /* temp src address for this datagram only */
1500 origladdr
.s_addr
= INADDR_ANY
;
1501 /* we don't want to keep the laddr or route */
1502 udp_dodisconnect
= 1;
1503 /* remember we don't care about src addr.*/
1504 inp
->inp_flags
|= INP_INADDR_ANY
;
1506 origladdr
= laddr
= inp
->inp_laddr
;
1509 origoutifp
= inp
->inp_last_outifp
;
1510 faddr
= inp
->inp_faddr
;
1511 lport
= inp
->inp_lport
;
1512 fport
= inp
->inp_fport
;
1515 sin
= (struct sockaddr_in
*)(void *)addr
;
1516 if (faddr
.s_addr
!= INADDR_ANY
) {
1522 * In case we don't have a local port set, go through
1523 * the full connect. We don't have a local port yet
1524 * (i.e., we can't be looked up), so it's not an issue
1525 * if the input runs at the same time we do this.
1527 /* if we have a source address specified, use that */
1528 if (pi_laddr
.s_addr
!= INADDR_ANY
)
1529 inp
->inp_laddr
= pi_laddr
;
1531 * If a scope is specified, use it. Scope from
1532 * IP_PKTINFO takes precendence over the the scope
1533 * set via INP_BOUND_IF.
1535 error
= in_pcbconnect(inp
, addr
, p
, ipoa
.ipoa_boundif
,
1540 laddr
= inp
->inp_laddr
;
1541 lport
= inp
->inp_lport
;
1542 faddr
= inp
->inp_faddr
;
1543 fport
= inp
->inp_fport
;
1544 udp_dodisconnect
= 1;
1546 /* synch up in case in_pcbladdr() overrides */
1547 if (outif
!= NULL
&& ipoa
.ipoa_boundif
!= IFSCOPE_NONE
)
1548 ipoa
.ipoa_boundif
= outif
->if_index
;
1554 * We have a full address and a local port; use those
1555 * info to build the packet without changing the pcb
1556 * and interfering with the input path. See 3851370.
1558 * Scope from IP_PKTINFO takes precendence over the
1559 * the scope set via INP_BOUND_IF.
1561 if (laddr
.s_addr
== INADDR_ANY
) {
1562 if ((error
= in_pcbladdr(inp
, addr
, &laddr
,
1563 ipoa
.ipoa_boundif
, &outif
)) != 0)
1566 * from pcbconnect: remember we don't
1567 * care about src addr.
1569 inp
->inp_flags
|= INP_INADDR_ANY
;
1571 /* synch up in case in_pcbladdr() overrides */
1572 if (outif
!= NULL
&&
1573 ipoa
.ipoa_boundif
!= IFSCOPE_NONE
)
1574 ipoa
.ipoa_boundif
= outif
->if_index
;
1577 faddr
= sin
->sin_addr
;
1578 fport
= sin
->sin_port
;
1581 if (faddr
.s_addr
== INADDR_ANY
) {
1588 mac_mbuf_label_associate_inpcb(inp
, m
);
1589 #endif /* CONFIG_MACF_NET */
1591 if (inp
->inp_flowhash
== 0)
1592 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1595 * Calculate data length and get a mbuf
1596 * for UDP and IP headers.
1598 M_PREPEND(m
, sizeof (struct udpiphdr
), M_DONTWAIT
, 1);
1605 * Fill in mbuf with extended UDP header
1606 * and addresses and length put into network format.
1608 ui
= mtod(m
, struct udpiphdr
*);
1609 bzero(ui
->ui_x1
, sizeof (ui
->ui_x1
)); /* XXX still needed? */
1610 ui
->ui_pr
= IPPROTO_UDP
;
1613 ui
->ui_sport
= lport
;
1614 ui
->ui_dport
= fport
;
1615 ui
->ui_ulen
= htons((u_short
)len
+ sizeof (struct udphdr
));
1618 * Set up checksum and output datagram.
1620 if (udpcksum
&& !(inp
->inp_flags
& INP_UDP_NOCKSUM
)) {
1621 ui
->ui_sum
= in_pseudo(ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
,
1622 htons((u_short
)len
+ sizeof (struct udphdr
) + IPPROTO_UDP
));
1623 m
->m_pkthdr
.csum_flags
= CSUM_UDP
;
1624 m
->m_pkthdr
.csum_data
= offsetof(struct udphdr
, uh_sum
);
1628 ((struct ip
*)ui
)->ip_len
= sizeof (struct udpiphdr
) + len
;
1629 ((struct ip
*)ui
)->ip_ttl
= inp
->inp_ip_ttl
; /* XXX */
1630 ((struct ip
*)ui
)->ip_tos
= inp
->inp_ip_tos
; /* XXX */
1631 udpstat
.udps_opackets
++;
1633 KERNEL_DEBUG(DBG_LAYER_OUT_END
, ui
->ui_dport
, ui
->ui_sport
,
1634 ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
, ui
->ui_ulen
);
1638 necp_kernel_policy_id policy_id
;
1639 u_int32_t route_rule_id
;
1640 if (!necp_socket_is_allowed_to_send_recv_v4(inp
, lport
, fport
,
1641 &laddr
, &faddr
, NULL
, &policy_id
, &route_rule_id
)) {
1642 error
= EHOSTUNREACH
;
1646 necp_mark_packet_from_socket(m
, inp
, policy_id
, route_rule_id
);
1651 if (inp
->inp_sp
!= NULL
&& ipsec_setsocket(m
, inp
->inp_socket
) != 0) {
1657 inpopts
= inp
->inp_options
;
1658 soopts
|= (inp
->inp_socket
->so_options
& (SO_DONTROUTE
| SO_BROADCAST
));
1659 mopts
= inp
->inp_moptions
;
1660 if (mopts
!= NULL
) {
1662 IMO_ADDREF_LOCKED(mopts
);
1663 if (IN_MULTICAST(ntohl(ui
->ui_dst
.s_addr
)) &&
1664 mopts
->imo_multicast_ifp
!= NULL
) {
1665 /* no reference needed */
1666 inp
->inp_last_outifp
= mopts
->imo_multicast_ifp
;
1671 /* Copy the cached route and take an extra reference */
1672 inp_route_copyout(inp
, &ro
);
1674 set_packet_service_class(m
, so
, msc
, 0);
1675 m
->m_pkthdr
.pkt_flowsrc
= FLOWSRC_INPCB
;
1676 m
->m_pkthdr
.pkt_flowid
= inp
->inp_flowhash
;
1677 m
->m_pkthdr
.pkt_proto
= IPPROTO_UDP
;
1678 m
->m_pkthdr
.pkt_flags
|= (PKTF_FLOW_ID
| PKTF_FLOW_LOCALSRC
);
1680 m
->m_pkthdr
.pkt_flags
|= PKTF_FLOW_ADV
;
1682 if (ipoa
.ipoa_boundif
!= IFSCOPE_NONE
)
1683 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
1685 if (laddr
.s_addr
!= INADDR_ANY
)
1686 ipoa
.ipoa_flags
|= IPOAF_BOUND_SRCADDR
;
1688 inp
->inp_sndinprog_cnt
++;
1690 socket_unlock(so
, 0);
1691 error
= ip_output(m
, inpopts
, &ro
, soopts
, mopts
, &ipoa
);
1697 if (error
== 0 && nstat_collect
) {
1698 boolean_t cell
, wifi
, wired
;
1700 if (ro
.ro_rt
!= NULL
) {
1701 cell
= IFNET_IS_CELLULAR(ro
.ro_rt
->rt_ifp
);
1702 wifi
= (!cell
&& IFNET_IS_WIFI(ro
.ro_rt
->rt_ifp
));
1703 wired
= (!wifi
&& IFNET_IS_WIRED(ro
.ro_rt
->rt_ifp
));
1705 cell
= wifi
= wired
= FALSE
;
1707 INP_ADD_STAT(inp
, cell
, wifi
, wired
, txpackets
, 1);
1708 INP_ADD_STAT(inp
, cell
, wifi
, wired
, txbytes
, len
);
1711 if (flowadv
&& (adv
->code
== FADV_FLOW_CONTROLLED
||
1712 adv
->code
== FADV_SUSPENDED
)) {
1713 /* return a hint to the application that
1714 * the packet has been dropped
1717 inp_set_fc_state(inp
, adv
->code
);
1720 VERIFY(inp
->inp_sndinprog_cnt
> 0);
1721 if ( --inp
->inp_sndinprog_cnt
== 0)
1722 inp
->inp_flags
&= ~(INP_FC_FEEDBACK
);
1724 /* Synchronize PCB cached route */
1725 inp_route_copyin(inp
, &ro
);
1728 if (udp_dodisconnect
) {
1729 /* Always discard the cached route for unconnected socket */
1730 ROUTE_RELEASE(&inp
->inp_route
);
1731 in_pcbdisconnect(inp
);
1732 inp
->inp_laddr
= origladdr
; /* XXX rehash? */
1733 /* no reference needed */
1734 inp
->inp_last_outifp
= origoutifp
;
1735 } else if (inp
->inp_route
.ro_rt
!= NULL
) {
1736 struct rtentry
*rt
= inp
->inp_route
.ro_rt
;
1737 struct ifnet
*outifp
;
1739 if (rt
->rt_flags
& (RTF_MULTICAST
|RTF_BROADCAST
))
1740 rt
= NULL
; /* unusable */
1742 * Always discard if it is a multicast or broadcast route.
1745 ROUTE_RELEASE(&inp
->inp_route
);
1748 * If the destination route is unicast, update outifp with
1749 * that of the route interface used by IP.
1752 (outifp
= rt
->rt_ifp
) != inp
->inp_last_outifp
) {
1753 inp
->inp_last_outifp
= outifp
; /* no reference needed */
1755 so
->so_pktheadroom
= P2ROUNDUP(
1756 sizeof(struct udphdr
) +
1758 ifnet_hdrlen(outifp
) +
1759 ifnet_packetpreamblelen(outifp
),
1763 ROUTE_RELEASE(&inp
->inp_route
);
1767 * If output interface was cellular/expensive, and this socket is
1768 * denied access to it, generate an event.
1770 if (error
!= 0 && (ipoa
.ipoa_retflags
& IPOARF_IFDENIED
) &&
1771 (INP_NO_CELLULAR(inp
) || INP_NO_EXPENSIVE(inp
)))
1772 soevent(so
, (SO_FILT_HINT_LOCKED
|SO_FILT_HINT_IFDENIED
));
1775 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0, 0, 0, 0);
1781 ifnet_release(outif
);
1786 u_int32_t udp_sendspace
= 9216; /* really max datagram size */
1787 /* 187 1K datagrams (approx 192 KB) */
1788 u_int32_t udp_recvspace
= 187 * (1024 +
1790 sizeof (struct sockaddr_in6
)
1792 sizeof (struct sockaddr_in
)
1796 /* Check that the values of udp send and recv space do not exceed sb_max */
1798 sysctl_udp_sospace(struct sysctl_oid
*oidp
, void *arg1
, int arg2
,
1799 struct sysctl_req
*req
)
1801 #pragma unused(arg1, arg2)
1802 u_int32_t new_value
= 0, *space_p
= NULL
;
1803 int changed
= 0, error
= 0;
1804 u_quad_t sb_effective_max
= (sb_max
/(MSIZE
+MCLBYTES
)) * MCLBYTES
;
1806 switch (oidp
->oid_number
) {
1807 case UDPCTL_RECVSPACE
:
1808 space_p
= &udp_recvspace
;
1810 case UDPCTL_MAXDGRAM
:
1811 space_p
= &udp_sendspace
;
1816 error
= sysctl_io_number(req
, *space_p
, sizeof (u_int32_t
),
1817 &new_value
, &changed
);
1819 if (new_value
> 0 && new_value
<= sb_effective_max
)
1820 *space_p
= new_value
;
1827 SYSCTL_PROC(_net_inet_udp
, UDPCTL_RECVSPACE
, recvspace
,
1828 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &udp_recvspace
, 0,
1829 &sysctl_udp_sospace
, "IU", "Maximum incoming UDP datagram size");
1831 SYSCTL_PROC(_net_inet_udp
, UDPCTL_MAXDGRAM
, maxdgram
,
1832 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &udp_sendspace
, 0,
1833 &sysctl_udp_sospace
, "IU", "Maximum outgoing UDP datagram size");
1836 udp_abort(struct socket
*so
)
1840 inp
= sotoinpcb(so
);
1842 panic("%s: so=%p null inp\n", __func__
, so
);
1845 soisdisconnected(so
);
1851 udp_attach(struct socket
*so
, int proto
, struct proc
*p
)
1853 #pragma unused(proto)
1857 inp
= sotoinpcb(so
);
1859 panic ("%s so=%p inp=%p\n", __func__
, so
, inp
);
1862 error
= in_pcballoc(so
, &udbinfo
, p
);
1865 error
= soreserve(so
, udp_sendspace
, udp_recvspace
);
1868 inp
= (struct inpcb
*)so
->so_pcb
;
1869 inp
->inp_vflag
|= INP_IPV4
;
1870 inp
->inp_ip_ttl
= ip_defttl
;
1872 nstat_udp_new_pcb(inp
);
1877 udp_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1882 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
&&
1883 nam
->sa_family
!= AF_INET6
)
1884 return (EAFNOSUPPORT
);
1886 inp
= sotoinpcb(so
);
1889 error
= in_pcbbind(inp
, nam
, p
);
1894 udp_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1899 inp
= sotoinpcb(so
);
1902 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
1907 if (necp_socket_should_use_flow_divert(inp
)) {
1908 uint32_t fd_ctl_unit
=
1909 necp_socket_get_flow_divert_control_unit(inp
);
1910 if (fd_ctl_unit
> 0) {
1911 error
= flow_divert_pcb_init(so
, fd_ctl_unit
);
1913 error
= flow_divert_connect_out(so
, nam
, p
);
1920 #endif /* FLOW_DIVERT */
1923 error
= in_pcbconnect(inp
, nam
, p
, IFSCOPE_NONE
, NULL
);
1926 if (inp
->inp_flowhash
== 0)
1927 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1933 udp_connectx_common(struct socket
*so
, int af
,
1934 struct sockaddr_list
**src_sl
, struct sockaddr_list
**dst_sl
,
1935 struct proc
*p
, uint32_t ifscope
, sae_associd_t aid
, sae_connid_t
*pcid
,
1936 uint32_t flags
, void *arg
, uint32_t arglen
,
1937 struct uio
*uio
, user_ssize_t
*bytes_written
)
1939 #pragma unused(aid, flags, arg, arglen)
1940 struct sockaddr_entry
*src_se
= NULL
, *dst_se
= NULL
;
1941 struct inpcb
*inp
= sotoinpcb(so
);
1943 user_ssize_t datalen
= 0;
1948 VERIFY(dst_sl
!= NULL
);
1950 /* select source (if specified) and destination addresses */
1951 error
= in_selectaddrs(af
, src_sl
, &src_se
, dst_sl
, &dst_se
);
1955 VERIFY(*dst_sl
!= NULL
&& dst_se
!= NULL
);
1956 VERIFY(src_se
== NULL
|| *src_sl
!= NULL
);
1957 VERIFY(dst_se
->se_addr
->sa_family
== af
);
1958 VERIFY(src_se
== NULL
|| src_se
->se_addr
->sa_family
== af
);
1961 inp_update_necp_policy(inp
, src_se
? src_se
->se_addr
: NULL
,
1962 dst_se
? dst_se
->se_addr
: NULL
, ifscope
);
1965 /* bind socket to the specified interface, if requested */
1966 if (ifscope
!= IFSCOPE_NONE
&&
1967 (error
= inp_bindif(inp
, ifscope
, NULL
)) != 0)
1970 /* if source address and/or port is specified, bind to it */
1971 if (src_se
!= NULL
) {
1972 struct sockaddr
*sa
= src_se
->se_addr
;
1973 error
= sobindlock(so
, sa
, 0); /* already locked */
1980 error
= udp_connect(so
, dst_se
->se_addr
, p
);
1984 error
= udp6_connect(so
, dst_se
->se_addr
, p
);
1996 * If there is data, copy it. DATA_IDEMPOTENT is ignored.
1997 * CONNECT_RESUME_ON_READ_WRITE is ignored.
2000 socket_unlock(so
, 0);
2002 VERIFY(bytes_written
!= NULL
);
2004 datalen
= uio_resid(uio
);
2005 error
= so
->so_proto
->pr_usrreqs
->pru_sosend(so
, NULL
,
2006 (uio_t
)uio
, NULL
, NULL
, 0);
2009 /* If error returned is EMSGSIZE, for example, disconnect */
2010 if (error
== 0 || error
== EWOULDBLOCK
)
2011 *bytes_written
= datalen
- uio_resid(uio
);
2013 (void)so
->so_proto
->pr_usrreqs
->pru_disconnectx(so
,
2014 SAE_ASSOCID_ANY
, SAE_CONNID_ANY
);
2016 * mask the EWOULDBLOCK error so that the caller
2017 * knows that atleast the connect was successful.
2019 if (error
== EWOULDBLOCK
)
2023 if (error
== 0 && pcid
!= NULL
)
2024 *pcid
= 1; /* there is only 1 connection for UDP */
2030 udp_connectx(struct socket
*so
, struct sockaddr_list
**src_sl
,
2031 struct sockaddr_list
**dst_sl
, struct proc
*p
, uint32_t ifscope
,
2032 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
2033 uint32_t arglen
, struct uio
*uio
, user_ssize_t
*bytes_written
)
2035 return (udp_connectx_common(so
, AF_INET
, src_sl
, dst_sl
,
2036 p
, ifscope
, aid
, pcid
, flags
, arg
, arglen
, uio
, bytes_written
));
2040 udp_detach(struct socket
*so
)
2044 inp
= sotoinpcb(so
);
2046 panic("%s: so=%p null inp\n", __func__
, so
);
2051 * If this is a socket that does not want to wakeup the device
2052 * for it's traffic, the application might be waiting for
2053 * close to complete before going to sleep. Send a notification
2054 * for this kind of sockets
2056 if (so
->so_options
& SO_NOWAKEFROMSLEEP
)
2057 socket_post_kev_msg_closed(so
);
2060 inp
->inp_state
= INPCB_STATE_DEAD
;
2065 udp_disconnect(struct socket
*so
)
2069 inp
= sotoinpcb(so
);
2072 || (necp_socket_should_use_flow_divert(inp
))
2075 return (inp
== NULL
? EINVAL
: EPROTOTYPE
);
2076 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
)
2079 in_pcbdisconnect(inp
);
2081 /* reset flow controlled state, just in case */
2082 inp_reset_fc_state(inp
);
2084 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
2085 so
->so_state
&= ~SS_ISCONNECTED
; /* XXX */
2086 inp
->inp_last_outifp
= NULL
;
2091 udp_disconnectx(struct socket
*so
, sae_associd_t aid
, sae_connid_t cid
)
2094 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
)
2097 return (udp_disconnect(so
));
2101 udp_send(struct socket
*so
, int flags
, struct mbuf
*m
,
2102 struct sockaddr
*addr
, struct mbuf
*control
, struct proc
*p
)
2105 #pragma unused(flags)
2106 #endif /* !(FLOW_DIVERT) */
2109 inp
= sotoinpcb(so
);
2113 if (control
!= NULL
)
2120 if (necp_socket_should_use_flow_divert(inp
)) {
2121 /* Implicit connect */
2122 return (flow_divert_implicit_data_out(so
, flags
, m
, addr
, control
, p
));
2124 #endif /* FLOW_DIVERT */
2127 return (udp_output(inp
, m
, addr
, control
, p
));
2131 udp_shutdown(struct socket
*so
)
2135 inp
= sotoinpcb(so
);
2143 udp_lock(struct socket
*so
, int refcount
, void *debug
)
2148 lr_saved
= __builtin_return_address(0);
2152 if (so
->so_pcb
!= NULL
) {
2153 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
2154 LCK_MTX_ASSERT_NOTOWNED
);
2155 lck_mtx_lock(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
2157 panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__
,
2158 so
, lr_saved
, solockhistory_nr(so
));
2164 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
2165 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
2170 udp_unlock(struct socket
*so
, int refcount
, void *debug
)
2175 lr_saved
= __builtin_return_address(0);
2182 if (so
->so_pcb
== NULL
) {
2183 panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__
,
2184 so
, lr_saved
, solockhistory_nr(so
));
2187 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
2188 LCK_MTX_ASSERT_OWNED
);
2189 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
2190 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
2191 lck_mtx_unlock(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
2197 udp_getlock(struct socket
*so
, int locktype
)
2199 #pragma unused(locktype)
2200 struct inpcb
*inp
= sotoinpcb(so
);
2202 if (so
->so_pcb
== NULL
) {
2203 panic("%s: so=%p NULL so_pcb lrh= %s\n", __func__
,
2204 so
, solockhistory_nr(so
));
2207 return (&inp
->inpcb_mtx
);
2211 * UDP garbage collector callback (inpcb_timer_func_t).
2213 * Returns > 0 to keep timer active.
2216 udp_gc(struct inpcbinfo
*ipi
)
2218 struct inpcb
*inp
, *inpnxt
;
2221 if (lck_rw_try_lock_exclusive(ipi
->ipi_lock
) == FALSE
) {
2222 if (udp_gc_done
== TRUE
) {
2223 udp_gc_done
= FALSE
;
2224 /* couldn't get the lock, must lock next time */
2225 atomic_add_32(&ipi
->ipi_gc_req
.intimer_fast
, 1);
2228 lck_rw_lock_exclusive(ipi
->ipi_lock
);
2233 for (inp
= udb
.lh_first
; inp
!= NULL
; inp
= inpnxt
) {
2234 inpnxt
= inp
->inp_list
.le_next
;
2237 * Skip unless it's STOPUSING; garbage collector will
2238 * be triggered by in_pcb_checkstate() upon setting
2239 * wantcnt to that value. If the PCB is already dead,
2240 * keep gc active to anticipate wantcnt changing.
2242 if (inp
->inp_wantcnt
!= WNT_STOPUSING
)
2246 * Skip if busy, no hurry for cleanup. Keep gc active
2247 * and try the lock again during next round.
2249 if (!lck_mtx_try_lock(&inp
->inpcb_mtx
)) {
2250 atomic_add_32(&ipi
->ipi_gc_req
.intimer_fast
, 1);
2255 * Keep gc active unless usecount is 0.
2257 so
= inp
->inp_socket
;
2258 if (so
->so_usecount
== 0) {
2259 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
2261 if (SOCK_CHECK_DOM(so
, PF_INET6
))
2269 lck_mtx_unlock(&inp
->inpcb_mtx
);
2270 atomic_add_32(&ipi
->ipi_gc_req
.intimer_fast
, 1);
2273 lck_rw_done(ipi
->ipi_lock
);
2279 udp_getstat SYSCTL_HANDLER_ARGS
2281 #pragma unused(oidp, arg1, arg2)
2282 if (req
->oldptr
== USER_ADDR_NULL
)
2283 req
->oldlen
= (size_t)sizeof (struct udpstat
);
2285 return (SYSCTL_OUT(req
, &udpstat
, MIN(sizeof (udpstat
), req
->oldlen
)));
2289 udp_in_cksum_stats(u_int32_t len
)
2291 udpstat
.udps_rcv_swcsum
++;
2292 udpstat
.udps_rcv_swcsum_bytes
+= len
;
2296 udp_out_cksum_stats(u_int32_t len
)
2298 udpstat
.udps_snd_swcsum
++;
2299 udpstat
.udps_snd_swcsum_bytes
+= len
;
2304 udp_in6_cksum_stats(u_int32_t len
)
2306 udpstat
.udps_rcv6_swcsum
++;
2307 udpstat
.udps_rcv6_swcsum_bytes
+= len
;
2311 udp_out6_cksum_stats(u_int32_t len
)
2313 udpstat
.udps_snd6_swcsum
++;
2314 udpstat
.udps_snd6_swcsum_bytes
+= len
;
2319 * Checksum extended UDP header and data.
2322 udp_input_checksum(struct mbuf
*m
, struct udphdr
*uh
, int off
, int ulen
)
2324 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2325 struct ip
*ip
= mtod(m
, struct ip
*);
2326 struct ipovly
*ipov
= (struct ipovly
*)ip
;
2328 if (uh
->uh_sum
== 0) {
2329 udpstat
.udps_nosum
++;
2333 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
2334 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
2335 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
2336 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
2337 uh
->uh_sum
= m
->m_pkthdr
.csum_rx_val
;
2339 uint16_t sum
= m
->m_pkthdr
.csum_rx_val
;
2340 uint16_t start
= m
->m_pkthdr
.csum_rx_start
;
2343 * Perform 1's complement adjustment of octets
2344 * that got included/excluded in the hardware-
2345 * calculated checksum value. Ignore cases
2346 * where the value includes or excludes the
2347 * IP header span, as the sum for those octets
2348 * would already be 0xffff and thus no-op.
2350 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
2351 start
!= 0 && (off
- start
) != off
) {
2352 #if BYTE_ORDER != BIG_ENDIAN
2357 #endif /* BYTE_ORDER != BIG_ENDIAN */
2358 /* callee folds in sum */
2359 sum
= m_adj_sum16(m
, start
, off
, sum
);
2360 #if BYTE_ORDER != BIG_ENDIAN
2365 #endif /* BYTE_ORDER != BIG_ENDIAN */
2368 /* callee folds in sum */
2369 uh
->uh_sum
= in_pseudo(ip
->ip_src
.s_addr
,
2370 ip
->ip_dst
.s_addr
, sum
+ htonl(ulen
+ IPPROTO_UDP
));
2372 uh
->uh_sum
^= 0xffff;
2377 bcopy(ipov
->ih_x1
, b
, sizeof (ipov
->ih_x1
));
2378 bzero(ipov
->ih_x1
, sizeof (ipov
->ih_x1
));
2379 ip_sum
= ipov
->ih_len
;
2380 ipov
->ih_len
= uh
->uh_ulen
;
2381 uh
->uh_sum
= in_cksum(m
, ulen
+ sizeof (struct ip
));
2382 bcopy(b
, ipov
->ih_x1
, sizeof (ipov
->ih_x1
));
2383 ipov
->ih_len
= ip_sum
;
2385 udp_in_cksum_stats(ulen
);
2388 if (uh
->uh_sum
!= 0) {
2389 udpstat
.udps_badsum
++;
2390 IF_UDP_STATINC(ifp
, badchksum
);
2398 udp_fill_keepalive_offload_frames(ifnet_t ifp
,
2399 struct ifnet_keepalive_offload_frame
*frames_array
,
2400 u_int32_t frames_array_count
, size_t frame_data_offset
,
2401 u_int32_t
*used_frames_count
);
2404 udp_fill_keepalive_offload_frames(ifnet_t ifp
,
2405 struct ifnet_keepalive_offload_frame
*frames_array
,
2406 u_int32_t frames_array_count
, size_t frame_data_offset
,
2407 u_int32_t
*used_frames_count
)
2411 u_int32_t frame_index
= *used_frames_count
;
2413 if (ifp
== NULL
|| frames_array
== NULL
||
2414 frames_array_count
== 0 ||
2415 frame_index
>= frames_array_count
||
2416 frame_data_offset
>= IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE
)
2419 lck_rw_lock_shared(udbinfo
.ipi_lock
);
2420 gencnt
= udbinfo
.ipi_gencnt
;
2421 LIST_FOREACH(inp
, udbinfo
.ipi_listhead
, inp_list
) {
2424 struct ifnet_keepalive_offload_frame
*frame
;
2425 struct mbuf
*m
= NULL
;
2427 if (frame_index
>= frames_array_count
)
2430 if (inp
->inp_gencnt
> gencnt
||
2431 inp
->inp_state
== INPCB_STATE_DEAD
)
2434 if ((so
= inp
->inp_socket
) == NULL
||
2435 (so
->so_state
& SS_DEFUNCT
))
2438 * check for keepalive offload flag without socket
2439 * lock to avoid a deadlock
2441 if (!(inp
->inp_flags2
& INP2_KEEPALIVE_OFFLOAD
)) {
2446 if (!(inp
->inp_vflag
& (INP_IPV4
| INP_IPV6
))) {
2447 udp_unlock(so
, 1, 0);
2450 if ((inp
->inp_vflag
& INP_IPV4
) &&
2451 (inp
->inp_laddr
.s_addr
== INADDR_ANY
||
2452 inp
->inp_faddr
.s_addr
== INADDR_ANY
)) {
2453 udp_unlock(so
, 1, 0);
2456 if ((inp
->inp_vflag
& INP_IPV6
) &&
2457 (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
) ||
2458 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
))) {
2459 udp_unlock(so
, 1, 0);
2462 if (inp
->inp_lport
== 0 || inp
->inp_fport
== 0) {
2463 udp_unlock(so
, 1, 0);
2466 if (inp
->inp_last_outifp
== NULL
||
2467 inp
->inp_last_outifp
->if_index
!= ifp
->if_index
) {
2468 udp_unlock(so
, 1, 0);
2471 if ((inp
->inp_vflag
& INP_IPV4
)) {
2472 if ((frame_data_offset
+ sizeof(struct udpiphdr
) +
2473 inp
->inp_keepalive_datalen
) >
2474 IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE
) {
2475 udp_unlock(so
, 1, 0);
2478 if ((sizeof(struct udpiphdr
) +
2479 inp
->inp_keepalive_datalen
) > _MHLEN
) {
2480 udp_unlock(so
, 1, 0);
2484 if ((frame_data_offset
+ sizeof(struct ip6_hdr
) +
2485 sizeof(struct udphdr
) +
2486 inp
->inp_keepalive_datalen
) >
2487 IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE
) {
2488 udp_unlock(so
, 1, 0);
2491 if ((sizeof(struct ip6_hdr
) + sizeof(struct udphdr
) +
2492 inp
->inp_keepalive_datalen
) > _MHLEN
) {
2493 udp_unlock(so
, 1, 0);
2497 MGETHDR(m
, M_WAIT
, MT_HEADER
);
2499 udp_unlock(so
, 1, 0);
2503 * This inp has all the information that is needed to
2504 * generate an offload frame.
2506 if (inp
->inp_vflag
& INP_IPV4
) {
2510 frame
= &frames_array
[frame_index
];
2511 frame
->length
= frame_data_offset
+
2512 sizeof(struct udpiphdr
) +
2513 inp
->inp_keepalive_datalen
;
2515 IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV4
;
2516 frame
->interval
= inp
->inp_keepalive_interval
;
2517 switch (inp
->inp_keepalive_type
) {
2518 case UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY
:
2520 IFNET_KEEPALIVE_OFFLOAD_FRAME_AIRPLAY
;
2525 data
= mtod(m
, u_int8_t
*);
2526 bzero(data
, sizeof(struct udpiphdr
));
2527 ip
= (__typeof__(ip
))(void *)data
;
2528 udp
= (__typeof__(udp
))(void *) (data
+
2530 m
->m_len
= sizeof(struct udpiphdr
);
2531 data
= data
+ sizeof(struct udpiphdr
);
2532 if (inp
->inp_keepalive_datalen
> 0 &&
2533 inp
->inp_keepalive_data
!= NULL
) {
2534 bcopy(inp
->inp_keepalive_data
, data
,
2535 inp
->inp_keepalive_datalen
);
2536 m
->m_len
+= inp
->inp_keepalive_datalen
;
2538 m
->m_pkthdr
.len
= m
->m_len
;
2540 ip
->ip_v
= IPVERSION
;
2541 ip
->ip_hl
= (sizeof(struct ip
) >> 2);
2542 ip
->ip_p
= IPPROTO_UDP
;
2543 ip
->ip_len
= htons(sizeof(struct udpiphdr
) +
2544 (u_short
)inp
->inp_keepalive_datalen
);
2545 ip
->ip_ttl
= inp
->inp_ip_ttl
;
2546 ip
->ip_tos
= inp
->inp_ip_tos
;
2547 ip
->ip_src
= inp
->inp_laddr
;
2548 ip
->ip_dst
= inp
->inp_faddr
;
2549 ip
->ip_sum
= in_cksum_hdr_opt(ip
);
2551 udp
->uh_sport
= inp
->inp_lport
;
2552 udp
->uh_dport
= inp
->inp_fport
;
2553 udp
->uh_ulen
= htons(sizeof(struct udphdr
) +
2554 (u_short
)inp
->inp_keepalive_datalen
);
2556 if (!(inp
->inp_flags
& INP_UDP_NOCKSUM
)) {
2557 udp
->uh_sum
= in_pseudo(ip
->ip_src
.s_addr
,
2559 htons(sizeof(struct udphdr
) +
2560 (u_short
)inp
->inp_keepalive_datalen
+
2562 m
->m_pkthdr
.csum_flags
= CSUM_UDP
;
2563 m
->m_pkthdr
.csum_data
= offsetof(struct udphdr
,
2566 m
->m_pkthdr
.pkt_proto
= IPPROTO_UDP
;
2567 in_delayed_cksum(m
);
2568 bcopy(m
->m_data
, frame
->data
+ frame_data_offset
,
2571 struct ip6_hdr
*ip6
;
2572 struct udphdr
*udp6
;
2574 VERIFY(inp
->inp_vflag
& INP_IPV6
);
2575 frame
= &frames_array
[frame_index
];
2576 frame
->length
= frame_data_offset
+
2577 sizeof(struct ip6_hdr
) +
2578 sizeof(struct udphdr
) +
2579 inp
->inp_keepalive_datalen
;
2581 IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV6
;
2582 frame
->interval
= inp
->inp_keepalive_interval
;
2583 switch (inp
->inp_keepalive_type
) {
2584 case UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY
:
2586 IFNET_KEEPALIVE_OFFLOAD_FRAME_AIRPLAY
;
2591 data
= mtod(m
, u_int8_t
*);
2592 bzero(data
, sizeof(struct ip6_hdr
) + sizeof(struct udphdr
));
2593 ip6
= (__typeof__(ip6
))(void *)data
;
2594 udp6
= (__typeof__(udp6
))(void *)(data
+
2595 sizeof(struct ip6_hdr
));
2596 m
->m_len
= sizeof(struct ip6_hdr
) +
2597 sizeof(struct udphdr
);
2598 data
= data
+ (sizeof(struct ip6_hdr
) +
2599 sizeof(struct udphdr
));
2600 if (inp
->inp_keepalive_datalen
> 0 &&
2601 inp
->inp_keepalive_data
!= NULL
) {
2602 bcopy(inp
->inp_keepalive_data
, data
,
2603 inp
->inp_keepalive_datalen
);
2604 m
->m_len
+= inp
->inp_keepalive_datalen
;
2606 m
->m_pkthdr
.len
= m
->m_len
;
2607 ip6
->ip6_flow
= inp
->inp_flow
& IPV6_FLOWINFO_MASK
;
2608 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
2609 ip6
->ip6_vfc
|= IPV6_VERSION
;
2610 ip6
->ip6_nxt
= IPPROTO_UDP
;
2611 ip6
->ip6_hlim
= ip6_defhlim
;
2612 ip6
->ip6_plen
= htons(sizeof(struct udphdr
) +
2613 (u_short
)inp
->inp_keepalive_datalen
);
2614 ip6
->ip6_src
= inp
->in6p_laddr
;
2615 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
))
2616 ip6
->ip6_src
.s6_addr16
[1] = 0;
2618 ip6
->ip6_dst
= inp
->in6p_faddr
;
2619 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
))
2620 ip6
->ip6_dst
.s6_addr16
[1] = 0;
2622 udp6
->uh_sport
= inp
->in6p_lport
;
2623 udp6
->uh_dport
= inp
->in6p_fport
;
2624 udp6
->uh_ulen
= htons(sizeof(struct udphdr
) +
2625 (u_short
)inp
->inp_keepalive_datalen
);
2626 if (!(inp
->inp_flags
& INP_UDP_NOCKSUM
)) {
2627 udp6
->uh_sum
= in6_pseudo(&ip6
->ip6_src
,
2629 htonl(sizeof(struct udphdr
) +
2630 (u_short
)inp
->inp_keepalive_datalen
+
2632 m
->m_pkthdr
.csum_flags
= CSUM_UDPIPV6
;
2633 m
->m_pkthdr
.csum_data
= offsetof(struct udphdr
,
2636 m
->m_pkthdr
.pkt_proto
= IPPROTO_UDP
;
2637 in6_delayed_cksum(m
);
2638 bcopy(m
->m_data
, frame
->data
+ frame_data_offset
,
2646 udp_unlock(so
, 1, 0);
2648 lck_rw_done(udbinfo
.ipi_lock
);
2649 *used_frames_count
= frame_index
;