2 * Copyright (c) 2000-2008 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>
76 #include <kern/zalloc.h>
79 #include <net/if_types.h>
80 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
86 #include <netinet/ip6.h>
88 #include <netinet/in_pcb.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip_var.h>
92 #include <netinet6/in6_pcb.h>
93 #include <netinet6/ip6_var.h>
95 #include <netinet/ip_icmp.h>
96 #include <netinet/icmp_var.h>
97 #include <netinet/udp.h>
98 #include <netinet/udp_var.h>
99 #include <sys/kdebug.h>
102 #include <netinet6/ipsec.h>
103 #include <netinet6/esp.h>
104 extern int ipsec_bypass
;
108 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETUDP, 0)
109 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETUDP, 2)
110 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETUDP, 1)
111 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETUDP, 3)
112 #define DBG_FNC_UDP_INPUT NETDBG_CODE(DBG_NETUDP, (5 << 8))
113 #define DBG_FNC_UDP_OUTPUT NETDBG_CODE(DBG_NETUDP, (6 << 8) | 1)
116 * UDP protocol implementation.
117 * Per RFC 768, August, 1980.
120 static int udpcksum
= 1;
122 static int udpcksum
= 0; /* XXX */
124 SYSCTL_INT(_net_inet_udp
, UDPCTL_CHECKSUM
, checksum
, CTLFLAG_RW
,
127 static u_int32_t udps_in_sw_cksum
;
128 SYSCTL_UINT(_net_inet_udp
, OID_AUTO
, in_sw_cksum
, CTLFLAG_RD
,
129 &udps_in_sw_cksum
, 0,
130 "Number of received packets checksummed in software");
132 static u_int64_t udps_in_sw_cksum_bytes
;
133 SYSCTL_QUAD(_net_inet_udp
, OID_AUTO
, in_sw_cksum_bytes
, CTLFLAG_RD
,
134 &udps_in_sw_cksum_bytes
,
135 "Amount of received data checksummed in software");
137 static u_int32_t udps_out_sw_cksum
;
138 SYSCTL_UINT(_net_inet_udp
, OID_AUTO
, out_sw_cksum
, CTLFLAG_RD
,
139 &udps_out_sw_cksum
, 0,
140 "Number of transmitted packets checksummed in software");
142 static u_int64_t udps_out_sw_cksum_bytes
;
143 SYSCTL_QUAD(_net_inet_udp
, OID_AUTO
, out_sw_cksum_bytes
, CTLFLAG_RD
,
144 &udps_out_sw_cksum_bytes
,
145 "Amount of transmitted data checksummed in software");
148 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
,
149 &log_in_vain
, 0, "Log all incoming UDP packets");
151 static int blackhole
= 0;
152 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, blackhole
, CTLFLAG_RW
,
153 &blackhole
, 0, "Do not send port unreachables for refused connects");
155 struct inpcbhead udb
; /* from udp_var.h */
156 #define udb6 udb /* for KAME src sync over BSD*'s */
157 struct inpcbinfo udbinfo
;
160 #define UDBHASHSIZE 16
163 extern int esp_udp_encap_port
;
165 extern void ipfwsyslog( int level
, const char *format
,...);
167 extern int fw_verbose
;
168 static int udp_gc_done
= FALSE
; /* Garbage collection performed last slowtimo */
171 #define log_in_vain_log( a ) { \
172 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
178 #define log_in_vain_log( a ) { log a; }
181 struct udpstat udpstat
; /* from udp_var.h */
182 SYSCTL_STRUCT(_net_inet_udp
, UDPCTL_STATS
, stats
, CTLFLAG_RD
,
183 &udpstat
, udpstat
, "UDP statistics (struct udpstat, netinet/udp_var.h)");
184 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, pcbcount
, CTLFLAG_RD
,
185 &udbinfo
.ipi_count
, 0, "Number of active PCBs");
187 __private_extern__
int udp_use_randomport
= 1;
188 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, randomize_ports
, CTLFLAG_RW
,
189 &udp_use_randomport
, 0, "Randomize UDP port numbers");
193 struct sockaddr_in6 uin6_sin
;
194 u_char uin6_init_done
: 1;
197 struct ip6_hdr uip6_ip6
;
198 u_char uip6_init_done
: 1;
200 static void ip_2_ip6_hdr(struct ip6_hdr
*ip6
, struct ip
*ip
);
201 static void udp_append(struct inpcb
*last
, struct ip
*ip
,
202 struct mbuf
*n
, int off
, struct sockaddr_in
*pudp_in
,
203 struct udp_in6
*pudp_in6
, struct udp_ip6
*pudp_ip6
);
205 static void udp_append(struct inpcb
*last
, struct ip
*ip
,
206 struct mbuf
*n
, int off
, struct sockaddr_in
*pudp_in
);
209 static int udp_detach(struct socket
*so
);
210 static int udp_output(struct inpcb
*, struct mbuf
*, struct sockaddr
*,
211 struct mbuf
*, struct proc
*);
212 extern int ChkAddressOK( __uint32_t dstaddr
, __uint32_t srcaddr
);
218 struct inpcbinfo
*pcbinfo
;
222 udbinfo
.listhead
= &udb
;
223 udbinfo
.hashbase
= hashinit(UDBHASHSIZE
, M_PCB
, &udbinfo
.hashmask
);
224 udbinfo
.porthashbase
= hashinit(UDBHASHSIZE
, M_PCB
,
225 &udbinfo
.porthashmask
);
227 str_size
= (vm_size_t
) sizeof(struct inpcb
);
228 udbinfo
.ipi_zone
= (void *) zinit(str_size
, 80000*str_size
, 8192, "udpcb");
232 * allocate lock group attribute and group for udp pcb mutexes
234 pcbinfo
->mtx_grp_attr
= lck_grp_attr_alloc_init();
236 pcbinfo
->mtx_grp
= lck_grp_alloc_init("udppcb", pcbinfo
->mtx_grp_attr
);
238 pcbinfo
->mtx_attr
= lck_attr_alloc_init();
240 if ((pcbinfo
->mtx
= lck_rw_alloc_init(pcbinfo
->mtx_grp
, pcbinfo
->mtx_attr
)) == NULL
)
241 return; /* pretty much dead if this fails... */
243 udbinfo
.ipi_zone
= zinit("udpcb", sizeof(struct inpcb
), maxsockets
,
250 register struct mbuf
*m
;
253 register struct ip
*ip
;
254 register struct udphdr
*uh
;
255 register struct inpcb
*inp
;
256 struct mbuf
*opts
= 0;
259 struct sockaddr
*append_sa
;
260 struct inpcbinfo
*pcbinfo
= &udbinfo
;
261 struct sockaddr_in udp_in
= {
262 sizeof (udp_in
), AF_INET
, 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
265 struct udp_in6 udp_in6
= {
266 { sizeof (udp_in6
.uin6_sin
), AF_INET6
, 0, 0,
267 IN6ADDR_ANY_INIT
, 0 },
270 struct udp_ip6 udp_ip6
;
273 udpstat
.udps_ipackets
++;
275 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_START
, 0,0,0,0,0);
276 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
277 m
->m_pkthdr
.csum_flags
= 0; /* invalidate hwcksum for UDP */
280 * Strip IP options, if any; should skip this,
281 * make available to user, and use on returned packets,
282 * but we don't yet have a way to check the checksum
283 * with options still present.
285 if (iphlen
> sizeof (struct ip
)) {
286 ip_stripoptions(m
, (struct mbuf
*)0);
287 iphlen
= sizeof(struct ip
);
291 * Get IP and UDP header together in first mbuf.
293 ip
= mtod(m
, struct ip
*);
294 if (m
->m_len
< iphlen
+ sizeof(struct udphdr
)) {
295 if ((m
= m_pullup(m
, iphlen
+ sizeof(struct udphdr
))) == 0) {
296 udpstat
.udps_hdrops
++;
297 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
300 ip
= mtod(m
, struct ip
*);
302 uh
= (struct udphdr
*)((caddr_t
)ip
+ iphlen
);
304 /* destination port of 0 is illegal, based on RFC768. */
305 if (uh
->uh_dport
== 0)
308 KERNEL_DEBUG(DBG_LAYER_IN_BEG
, uh
->uh_dport
, uh
->uh_sport
,
309 ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, uh
->uh_ulen
);
312 * Make mbuf data length reflect UDP length.
313 * If not enough data to reflect UDP length, drop.
315 len
= ntohs((u_short
)uh
->uh_ulen
);
316 if (ip
->ip_len
!= len
) {
317 if (len
> ip
->ip_len
|| len
< sizeof(struct udphdr
)) {
318 udpstat
.udps_badlen
++;
321 m_adj(m
, len
- ip
->ip_len
);
322 /* ip->ip_len = len; */
325 * Save a copy of the IP header in case we want restore it
326 * for sending an ICMP error message in response.
331 * Checksum extended UDP header and data.
334 if (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) {
335 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
)
336 uh
->uh_sum
= m
->m_pkthdr
.csum_data
;
339 uh
->uh_sum
^= 0xffff;
343 *(uint32_t*)&b
[0] = *(uint32_t*)&((struct ipovly
*)ip
)->ih_x1
[0];
344 *(uint32_t*)&b
[4] = *(uint32_t*)&((struct ipovly
*)ip
)->ih_x1
[4];
345 *(uint8_t*)&b
[8] = *(uint8_t*)&((struct ipovly
*)ip
)->ih_x1
[8];
347 bzero(((struct ipovly
*)ip
)->ih_x1
, 9);
348 ((struct ipovly
*)ip
)->ih_len
= uh
->uh_ulen
;
349 uh
->uh_sum
= in_cksum(m
, len
+ sizeof (struct ip
));
351 *(uint32_t*)&((struct ipovly
*)ip
)->ih_x1
[0] = *(uint32_t*)&b
[0];
352 *(uint32_t*)&((struct ipovly
*)ip
)->ih_x1
[4] = *(uint32_t*)&b
[4];
353 *(uint8_t*)&((struct ipovly
*)ip
)->ih_x1
[8] = *(uint8_t*)&b
[8];
354 udp_in_cksum_stats(len
);
357 udpstat
.udps_badsum
++;
359 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
365 udpstat
.udps_nosum
++;
368 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
369 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
)) {
371 int reuse_sock
= 0, mcast_delivered
= 0;
373 lck_rw_lock_shared(pcbinfo
->mtx
);
375 * Deliver a multicast or broadcast datagram to *all* sockets
376 * for which the local and remote addresses and ports match
377 * those of the incoming datagram. This allows more than
378 * one process to receive multi/broadcasts on the same port.
379 * (This really ought to be done for unicast datagrams as
380 * well, but that would cause problems with existing
381 * applications that open both address-specific sockets and
382 * a wildcard socket listening to the same port -- they would
383 * end up receiving duplicates of every unicast datagram.
384 * Those applications open the multiple sockets to overcome an
385 * inadequacy of the UDP socket interface, but for backwards
386 * compatibility we avoid the problem here rather than
387 * fixing the interface. Maybe 4.5BSD will remedy this?)
392 * Construct sockaddr format source address.
394 udp_in
.sin_port
= uh
->uh_sport
;
395 udp_in
.sin_addr
= ip
->ip_src
;
397 * Locate pcb(s) for datagram.
398 * (Algorithm copied from raw_intr().)
401 udp_in6
.uin6_init_done
= udp_ip6
.uip6_init_done
= 0;
403 LIST_FOREACH(inp
, &udb
, inp_list
) {
404 if (inp
->inp_socket
== NULL
)
406 if (inp
!= sotoinpcb(inp
->inp_socket
))
407 panic("udp_input: bad so back ptr inp=%p\n", inp
);
409 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
413 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
) {
417 udp_lock(inp
->inp_socket
, 1, 0);
419 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
420 udp_unlock(inp
->inp_socket
, 1, 0);
424 if (inp
->inp_lport
!= uh
->uh_dport
) {
425 udp_unlock(inp
->inp_socket
, 1, 0);
428 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
429 if (inp
->inp_laddr
.s_addr
!=
431 udp_unlock(inp
->inp_socket
, 1, 0);
435 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
436 if (inp
->inp_faddr
.s_addr
!=
438 inp
->inp_fport
!= uh
->uh_sport
) {
439 udp_unlock(inp
->inp_socket
, 1, 0);
444 reuse_sock
= inp
->inp_socket
->so_options
& (SO_REUSEPORT
|SO_REUSEADDR
);
448 /* check AH/ESP integrity. */
449 if (ipsec_bypass
== 0) {
450 if (ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
451 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
452 /* do not inject data to pcb */
459 struct mbuf
*n
= NULL
;
462 n
= m_copy(m
, 0, M_COPYALL
);
464 udp_append(inp
, ip
, m
,
465 iphlen
+ sizeof(struct udphdr
),
466 &udp_in
, &udp_in6
, &udp_ip6
);
468 udp_append(inp
, ip
, m
,
469 iphlen
+ sizeof(struct udphdr
),
476 udp_unlock(inp
->inp_socket
, 1, 0);
479 * Don't look for additional matches if this one does
480 * not have either the SO_REUSEPORT or SO_REUSEADDR
481 * socket options set. This heuristic avoids searching
482 * through all pcbs in the common case of a non-shared
483 * port. It assumes that an application will never
484 * clear these options after setting them.
486 if (reuse_sock
== 0 || m
== NULL
)
489 * Recompute IP and UDP header pointers for new mbuf
491 ip
= mtod(m
, struct ip
*);
492 uh
= (struct udphdr
*)((caddr_t
)ip
+ iphlen
);
494 lck_rw_done(pcbinfo
->mtx
);
496 if (mcast_delivered
== 0) {
498 * No matching pcb found; discard datagram.
499 * (No need to send an ICMP Port Unreachable
500 * for a broadcast or multicast datgram.)
502 udpstat
.udps_noportbcast
++;
506 if (m
!= NULL
) /* free the extra copy of mbuf or skipped by IPSec */
508 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
514 * UDP to port 4500 with a payload where the first four bytes are
515 * not zero is a UDP encapsulated IPSec packet. Packets where
516 * the payload is one byte and that byte is 0xFF are NAT keepalive
517 * packets. Decapsulate the ESP packet and carry on with IPSec input
518 * or discard the NAT keep-alive.
520 if (ipsec_bypass
== 0 && (esp_udp_encap_port
& 0xFFFF) != 0 &&
521 uh
->uh_dport
== ntohs((u_short
)esp_udp_encap_port
)) {
522 int payload_len
= len
- sizeof(struct udphdr
) > 4 ? 4 : len
- sizeof(struct udphdr
);
523 if (m
->m_len
< iphlen
+ sizeof(struct udphdr
) + payload_len
) {
524 if ((m
= m_pullup(m
, iphlen
+ sizeof(struct udphdr
) + payload_len
)) == 0) {
525 udpstat
.udps_hdrops
++;
526 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
529 ip
= mtod(m
, struct ip
*);
530 uh
= (struct udphdr
*)((caddr_t
)ip
+ iphlen
);
532 /* Check for NAT keepalive packet */
533 if (payload_len
== 1 && *(u_int8_t
*)((caddr_t
)uh
+ sizeof(struct udphdr
)) == 0xFF) {
535 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
538 else if (payload_len
== 4 && *(u_int32_t
*)((caddr_t
)uh
+ sizeof(struct udphdr
)) != 0) {
539 /* UDP encapsulated IPSec packet to pass through NAT */
542 stripsiz
= sizeof(struct udphdr
);
544 ip
= mtod(m
, struct ip
*);
545 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), iphlen
);
546 m
->m_data
+= stripsiz
;
547 m
->m_len
-= stripsiz
;
548 m
->m_pkthdr
.len
-= stripsiz
;
549 ip
= mtod(m
, struct ip
*);
550 ip
->ip_len
= ip
->ip_len
- stripsiz
;
551 ip
->ip_p
= IPPROTO_ESP
;
553 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
554 esp4_input(m
, iphlen
);
561 * Locate pcb for datagram.
563 inp
= in_pcblookup_hash(&udbinfo
, ip
->ip_src
, uh
->uh_sport
,
564 ip
->ip_dst
, uh
->uh_dport
, 1, m
->m_pkthdr
.rcvif
);
567 char buf
[MAX_IPv4_STR_LEN
];
568 char buf2
[MAX_IPv4_STR_LEN
];
570 /* check src and dst address */
571 if (log_in_vain
!= 3)
573 "Connection attempt to UDP %s:%d from %s:%d\n",
574 inet_ntop(AF_INET
, &ip
->ip_dst
, buf
, sizeof(buf
)),
576 inet_ntop(AF_INET
, &ip
->ip_src
, buf2
, sizeof(buf2
)),
577 ntohs(uh
->uh_sport
));
578 else if (!(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
579 ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
)
580 log_in_vain_log((LOG_INFO
,
581 "Stealth Mode connection attempt to UDP %s:%d from %s:%d\n",
582 inet_ntop(AF_INET
, &ip
->ip_dst
, buf
, sizeof(buf
)),
584 inet_ntop(AF_INET
, &ip
->ip_src
, buf2
, sizeof(buf2
)),
585 ntohs(uh
->uh_sport
)))
587 udpstat
.udps_noport
++;
588 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
589 udpstat
.udps_noportbcast
++;
593 if (badport_bandlim(BANDLIM_ICMP_UNREACH
) < 0)
597 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
)
600 ip
->ip_len
+= iphlen
;
601 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_PORT
, 0, 0);
602 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
605 udp_lock(inp
->inp_socket
, 1, 0);
607 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
608 udp_unlock(inp
->inp_socket
, 1, 0);
612 if (ipsec_bypass
== 0 && inp
!= NULL
) {
613 if (ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
614 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
615 udp_unlock(inp
->inp_socket
, 1, 0);
622 * Construct sockaddr format source address.
623 * Stuff source address and datagram in user buffer.
625 udp_in
.sin_port
= uh
->uh_sport
;
626 udp_in
.sin_addr
= ip
->ip_src
;
627 if (inp
->inp_flags
& INP_CONTROLOPTS
628 || inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
630 if (inp
->inp_vflag
& INP_IPV6
) {
633 ip_2_ip6_hdr(&udp_ip6
.uip6_ip6
, ip
);
634 savedflags
= inp
->inp_flags
;
635 inp
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
636 ip6_savecontrol(inp
, &opts
, &udp_ip6
.uip6_ip6
, m
);
637 inp
->inp_flags
= savedflags
;
640 ip_savecontrol(inp
, &opts
, ip
, m
);
642 m_adj(m
, iphlen
+ sizeof(struct udphdr
));
644 KERNEL_DEBUG(DBG_LAYER_IN_END
, uh
->uh_dport
, uh
->uh_sport
,
645 save_ip
.ip_src
.s_addr
, save_ip
.ip_dst
.s_addr
, uh
->uh_ulen
);
648 if (inp
->inp_vflag
& INP_IPV6
) {
649 in6_sin_2_v4mapsin6(&udp_in
, &udp_in6
.uin6_sin
);
650 append_sa
= (struct sockaddr
*)&udp_in6
.uin6_sin
;
653 append_sa
= (struct sockaddr
*)&udp_in
;
654 if (sbappendaddr(&inp
->inp_socket
->so_rcv
, append_sa
, m
, opts
, NULL
) == 0) {
655 udpstat
.udps_fullsock
++;
658 sorwakeup(inp
->inp_socket
);
660 udp_unlock(inp
->inp_socket
, 1, 0);
661 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
667 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
673 ip_2_ip6_hdr(ip6
, ip
)
677 bzero(ip6
, sizeof(*ip6
));
679 ip6
->ip6_vfc
= IPV6_VERSION
;
680 ip6
->ip6_plen
= ip
->ip_len
;
681 ip6
->ip6_nxt
= ip
->ip_p
;
682 ip6
->ip6_hlim
= ip
->ip_ttl
;
683 ip6
->ip6_src
.s6_addr32
[2] = ip6
->ip6_dst
.s6_addr32
[2] =
685 ip6
->ip6_src
.s6_addr32
[3] = ip
->ip_src
.s_addr
;
686 ip6
->ip6_dst
.s6_addr32
[3] = ip
->ip_dst
.s_addr
;
691 * subroutine of udp_input(), mainly for source code readability.
695 udp_append(struct inpcb
*last
, struct ip
*ip
, struct mbuf
*n
, int off
,
696 struct sockaddr_in
*pudp_in
, struct udp_in6
*pudp_in6
,
697 struct udp_ip6
*pudp_ip6
)
699 udp_append(struct inpcb
*last
, struct ip
*ip
, struct mbuf
*n
, int off
,
700 struct sockaddr_in
*pudp_in
)
703 struct sockaddr
*append_sa
;
704 struct mbuf
*opts
= 0;
707 if (mac_inpcb_check_deliver(last
, n
, AF_INET
, SOCK_DGRAM
) != 0) {
712 if (last
->inp_flags
& INP_CONTROLOPTS
||
713 last
->inp_socket
->so_options
& SO_TIMESTAMP
) {
715 if (last
->inp_vflag
& INP_IPV6
) {
718 if (pudp_ip6
->uip6_init_done
== 0) {
719 ip_2_ip6_hdr(&pudp_ip6
->uip6_ip6
, ip
);
720 pudp_ip6
->uip6_init_done
= 1;
722 savedflags
= last
->inp_flags
;
723 last
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
724 ip6_savecontrol(last
, &opts
, &pudp_ip6
->uip6_ip6
, n
);
725 last
->inp_flags
= savedflags
;
728 ip_savecontrol(last
, &opts
, ip
, n
);
731 if (last
->inp_vflag
& INP_IPV6
) {
732 if (pudp_in6
->uin6_init_done
== 0) {
733 in6_sin_2_v4mapsin6(pudp_in
, &pudp_in6
->uin6_sin
);
734 pudp_in6
->uin6_init_done
= 1;
736 append_sa
= (struct sockaddr
*)&pudp_in6
->uin6_sin
;
739 append_sa
= (struct sockaddr
*)pudp_in
;
741 if (sbappendaddr(&last
->inp_socket
->so_rcv
, append_sa
, n
, opts
, NULL
) == 0) {
742 udpstat
.udps_fullsock
++;
744 sorwakeup(last
->inp_socket
);
748 * Notify a udp user of an asynchronous error;
749 * just wake up so that he can collect error status.
752 udp_notify(inp
, errno
)
753 register struct inpcb
*inp
;
756 inp
->inp_socket
->so_error
= errno
;
757 sorwakeup(inp
->inp_socket
);
758 sowwakeup(inp
->inp_socket
);
762 udp_ctlinput(cmd
, sa
, vip
)
769 void (*notify
)(struct inpcb
*, int) = udp_notify
;
770 struct in_addr faddr
;
773 faddr
= ((struct sockaddr_in
*)sa
)->sin_addr
;
774 if (sa
->sa_family
!= AF_INET
|| faddr
.s_addr
== INADDR_ANY
)
777 if (PRC_IS_REDIRECT(cmd
)) {
779 notify
= in_rtchange
;
780 } else if (cmd
== PRC_HOSTDEAD
)
782 else if ((unsigned)cmd
>= PRC_NCMDS
|| inetctlerrmap
[cmd
] == 0)
785 uh
= (struct udphdr
*)((caddr_t
)ip
+ (ip
->ip_hl
<< 2));
786 inp
= in_pcblookup_hash(&udbinfo
, faddr
, uh
->uh_dport
,
787 ip
->ip_src
, uh
->uh_sport
, 0, NULL
);
788 if (inp
!= NULL
&& inp
->inp_socket
!= NULL
) {
789 udp_lock(inp
->inp_socket
, 1, 0);
790 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
791 udp_unlock(inp
->inp_socket
, 1, 0);
794 (*notify
)(inp
, inetctlerrmap
[cmd
]);
795 udp_unlock(inp
->inp_socket
, 1, 0);
798 in_pcbnotifyall(&udbinfo
, faddr
, inetctlerrmap
[cmd
], notify
);
802 udp_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
807 if (sopt
->sopt_level
!= IPPROTO_UDP
)
808 return (ip_ctloutput(so
, sopt
));
813 switch (sopt
->sopt_dir
) {
815 switch (sopt
->sopt_name
) {
817 /* This option is settable only for UDP over IPv4 */
818 if (!(inp
->inp_vflag
& INP_IPV4
)) {
823 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
824 sizeof (optval
))) != 0)
828 inp
->inp_flags
|= INP_UDP_NOCKSUM
;
830 inp
->inp_flags
&= ~INP_UDP_NOCKSUM
;
840 switch (sopt
->sopt_name
) {
842 optval
= inp
->inp_flags
& INP_UDP_NOCKSUM
;
850 error
= sooptcopyout(sopt
, &optval
, sizeof (optval
));
857 udp_pcblist SYSCTL_HANDLER_ARGS
859 #pragma unused(oidp, arg1, arg2)
861 struct inpcb
*inp
, **inp_list
;
866 * The process of preparing the TCB list is too time-consuming and
867 * resource-intensive to repeat twice on every request.
869 lck_rw_lock_exclusive(udbinfo
.mtx
);
870 if (req
->oldptr
== USER_ADDR_NULL
) {
871 n
= udbinfo
.ipi_count
;
872 req
->oldidx
= 2 * (sizeof xig
)
873 + (n
+ n
/8) * sizeof(struct xinpcb
);
874 lck_rw_done(udbinfo
.mtx
);
878 if (req
->newptr
!= USER_ADDR_NULL
) {
879 lck_rw_done(udbinfo
.mtx
);
884 * OK, now we're committed to doing something.
886 gencnt
= udbinfo
.ipi_gencnt
;
887 n
= udbinfo
.ipi_count
;
889 bzero(&xig
, sizeof(xig
));
890 xig
.xig_len
= sizeof xig
;
892 xig
.xig_gen
= gencnt
;
893 xig
.xig_sogen
= so_gencnt
;
894 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
896 lck_rw_done(udbinfo
.mtx
);
900 * We are done if there is no pcb
903 lck_rw_done(udbinfo
.mtx
);
907 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
909 lck_rw_done(udbinfo
.mtx
);
913 for (inp
= LIST_FIRST(udbinfo
.listhead
), i
= 0; inp
&& i
< n
;
914 inp
= LIST_NEXT(inp
, inp_list
)) {
915 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
)
921 for (i
= 0; i
< n
; i
++) {
923 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
926 bzero(&xi
, sizeof(xi
));
927 xi
.xi_len
= sizeof xi
;
928 /* XXX should avoid extra copy */
929 inpcb_to_compat(inp
, &xi
.xi_inp
);
931 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
932 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
937 * Give the user an updated idea of our state.
938 * If the generation differs from what we told
939 * her before, she knows that something happened
940 * while we were processing this request, and it
941 * might be necessary to retry.
943 bzero(&xig
, sizeof(xig
));
944 xig
.xig_len
= sizeof xig
;
945 xig
.xig_gen
= udbinfo
.ipi_gencnt
;
946 xig
.xig_sogen
= so_gencnt
;
947 xig
.xig_count
= udbinfo
.ipi_count
;
948 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
950 FREE(inp_list
, M_TEMP
);
951 lck_rw_done(udbinfo
.mtx
);
955 SYSCTL_PROC(_net_inet_udp
, UDPCTL_PCBLIST
, pcblist
, CTLFLAG_RD
, 0, 0,
956 udp_pcblist
, "S,xinpcb", "List of active UDP sockets");
961 udp_pcblist64 SYSCTL_HANDLER_ARGS
963 #pragma unused(oidp, arg1, arg2)
965 struct inpcb
*inp
, **inp_list
;
970 * The process of preparing the TCB list is too time-consuming and
971 * resource-intensive to repeat twice on every request.
973 lck_rw_lock_shared(udbinfo
.mtx
);
974 if (req
->oldptr
== USER_ADDR_NULL
) {
975 n
= udbinfo
.ipi_count
;
976 req
->oldidx
= 2 * (sizeof xig
)
977 + (n
+ n
/8) * sizeof(struct xinpcb64
);
978 lck_rw_done(udbinfo
.mtx
);
982 if (req
->newptr
!= USER_ADDR_NULL
) {
983 lck_rw_done(udbinfo
.mtx
);
988 * OK, now we're committed to doing something.
990 gencnt
= udbinfo
.ipi_gencnt
;
991 n
= udbinfo
.ipi_count
;
993 bzero(&xig
, sizeof(xig
));
994 xig
.xig_len
= sizeof xig
;
996 xig
.xig_gen
= gencnt
;
997 xig
.xig_sogen
= so_gencnt
;
998 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1000 lck_rw_done(udbinfo
.mtx
);
1004 * We are done if there is no pcb
1007 lck_rw_done(udbinfo
.mtx
);
1011 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
1012 if (inp_list
== 0) {
1013 lck_rw_done(udbinfo
.mtx
);
1017 for (inp
= LIST_FIRST(udbinfo
.listhead
), i
= 0; inp
&& i
< n
;
1018 inp
= LIST_NEXT(inp
, inp_list
)) {
1019 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
)
1020 inp_list
[i
++] = inp
;
1025 for (i
= 0; i
< n
; i
++) {
1027 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
1030 bzero(&xi
, sizeof(xi
));
1031 xi
.xi_len
= sizeof xi
;
1032 inpcb_to_xinpcb64(inp
, &xi
);
1033 if (inp
->inp_socket
)
1034 sotoxsocket64(inp
->inp_socket
, &xi
.xi_socket
);
1035 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
1040 * Give the user an updated idea of our state.
1041 * If the generation differs from what we told
1042 * her before, she knows that something happened
1043 * while we were processing this request, and it
1044 * might be necessary to retry.
1046 bzero(&xig
, sizeof(xig
));
1047 xig
.xig_len
= sizeof xig
;
1048 xig
.xig_gen
= udbinfo
.ipi_gencnt
;
1049 xig
.xig_sogen
= so_gencnt
;
1050 xig
.xig_count
= udbinfo
.ipi_count
;
1051 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1053 FREE(inp_list
, M_TEMP
);
1054 lck_rw_done(udbinfo
.mtx
);
1058 SYSCTL_PROC(_net_inet_udp
, OID_AUTO
, pcblist64
, CTLFLAG_RD
, 0, 0,
1059 udp_pcblist64
, "S,xinpcb64", "List of active UDP sockets");
1061 #endif /* !CONFIG_EMBEDDED */
1063 static __inline__ u_int16_t
1064 get_socket_id(struct socket
* s
)
1071 val
= (u_int16_t
)(((uintptr_t)s
) / sizeof(struct socket
));
1079 udp_output(inp
, m
, addr
, control
, p
)
1080 register struct inpcb
*inp
;
1082 struct sockaddr
*addr
;
1083 struct mbuf
*control
;
1086 register struct udpiphdr
*ui
;
1087 register int len
= m
->m_pkthdr
.len
;
1088 struct sockaddr_in
*sin
;
1089 struct in_addr origladdr
, laddr
, faddr
;
1090 u_short lport
, fport
;
1091 struct sockaddr_in
*ifaddr
;
1092 int error
= 0, udp_dodisconnect
= 0;
1093 struct socket
*so
= inp
->inp_socket
;
1095 struct mbuf
*inpopts
;
1096 struct ip_moptions
*mopts
;
1098 struct ip_out_args ipoa
;
1100 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
1103 m_freem(control
); /* XXX */
1105 KERNEL_DEBUG(DBG_LAYER_OUT_BEG
, inp
->inp_fport
, inp
->inp_lport
,
1106 inp
->inp_laddr
.s_addr
, inp
->inp_faddr
.s_addr
,
1107 (htons((u_short
)len
+ sizeof (struct udphdr
))));
1109 if (len
+ sizeof(struct udpiphdr
) > IP_MAXPACKET
) {
1114 lck_mtx_assert(inp
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1116 /* If socket was bound to an ifindex, tell ip_output about it */
1117 ipoa
.ipoa_ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
1118 inp
->inp_boundif
: IFSCOPE_NONE
;
1119 soopts
|= IP_OUTARGS
;
1121 /* If there was a routing change, discard cached route and check
1122 * that we have a valid source address.
1123 * Reacquire a new source address if INADDR_ANY was specified
1125 if (inp
->inp_route
.ro_rt
!= NULL
&&
1126 inp
->inp_route
.ro_rt
->generation_id
!= route_generation
) {
1127 struct in_ifaddr
*ia
;
1129 /* src address is gone? */
1130 if ((ia
= ifa_foraddr(inp
->inp_laddr
.s_addr
)) == NULL
) {
1131 if (inp
->inp_flags
& INP_INADDR_ANY
) {
1132 /* new src will be set later */
1133 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
1135 error
= EADDRNOTAVAIL
;
1140 ifafree(&ia
->ia_ifa
);
1141 if (inp
->inp_route
.ro_rt
!= NULL
)
1142 rtfree(inp
->inp_route
.ro_rt
);
1143 inp
->inp_route
.ro_rt
= NULL
;
1146 origladdr
= laddr
= inp
->inp_laddr
;
1147 faddr
= inp
->inp_faddr
;
1148 lport
= inp
->inp_lport
;
1149 fport
= inp
->inp_fport
;
1152 sin
= (struct sockaddr_in
*)addr
;
1153 if (faddr
.s_addr
!= INADDR_ANY
) {
1159 * In case we don't have a local port set, go through the full connect.
1160 * We don't have a local port yet (ie, we can't be looked up),
1161 * so it's not an issue if the input runs at the same time we do this.
1163 error
= in_pcbconnect(inp
, addr
, p
);
1167 laddr
= inp
->inp_laddr
;
1168 lport
= inp
->inp_lport
;
1169 faddr
= inp
->inp_faddr
;
1170 fport
= inp
->inp_fport
;
1171 udp_dodisconnect
= 1;
1175 * we have a full address and a local port.
1176 * use those info to build the packet without changing the pcb
1177 * and interfering with the input path. See 3851370
1179 if (laddr
.s_addr
== INADDR_ANY
) {
1180 if ((error
= in_pcbladdr(inp
, addr
, &ifaddr
)) != 0)
1182 laddr
= ifaddr
->sin_addr
;
1183 inp
->inp_flags
|= INP_INADDR_ANY
; /* from pcbconnect: remember we don't care about src addr.*/
1186 faddr
= sin
->sin_addr
;
1187 fport
= sin
->sin_port
;
1190 if (faddr
.s_addr
== INADDR_ANY
) {
1197 mac_mbuf_label_associate_inpcb(inp
, m
);
1201 * Calculate data length and get a mbuf
1202 * for UDP and IP headers.
1204 M_PREPEND(m
, sizeof(struct udpiphdr
), M_DONTWAIT
);
1211 * Fill in mbuf with extended UDP header
1212 * and addresses and length put into network format.
1214 ui
= mtod(m
, struct udpiphdr
*);
1215 bzero(ui
->ui_x1
, sizeof(ui
->ui_x1
)); /* XXX still needed? */
1216 ui
->ui_pr
= IPPROTO_UDP
;
1219 ui
->ui_sport
= lport
;
1220 ui
->ui_dport
= fport
;
1221 ui
->ui_ulen
= htons((u_short
)len
+ sizeof(struct udphdr
));
1224 * Set up checksum and output datagram.
1226 if (udpcksum
&& !(inp
->inp_flags
& INP_UDP_NOCKSUM
)) {
1227 ui
->ui_sum
= in_pseudo(ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
,
1228 htons((u_short
)len
+ sizeof(struct udphdr
) + IPPROTO_UDP
));
1229 m
->m_pkthdr
.csum_flags
= CSUM_UDP
;
1230 m
->m_pkthdr
.csum_data
= offsetof(struct udphdr
, uh_sum
);
1234 ((struct ip
*)ui
)->ip_len
= sizeof (struct udpiphdr
) + len
;
1235 ((struct ip
*)ui
)->ip_ttl
= inp
->inp_ip_ttl
; /* XXX */
1236 ((struct ip
*)ui
)->ip_tos
= inp
->inp_ip_tos
; /* XXX */
1237 udpstat
.udps_opackets
++;
1239 KERNEL_DEBUG(DBG_LAYER_OUT_END
, ui
->ui_dport
, ui
->ui_sport
,
1240 ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
, ui
->ui_ulen
);
1243 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, inp
->inp_socket
) != 0) {
1248 m
->m_pkthdr
.socket_id
= get_socket_id(inp
->inp_socket
);
1250 inpopts
= inp
->inp_options
;
1251 soopts
|= (inp
->inp_socket
->so_options
& (SO_DONTROUTE
| SO_BROADCAST
));
1252 mopts
= inp
->inp_moptions
;
1254 /* Copy the cached route and take an extra reference */
1255 inp_route_copyout(inp
, &ro
);
1257 socket_unlock(so
, 0);
1258 /* XXX jgraessley please look at XXX */
1259 error
= ip_output_list(m
, 0, inpopts
, &ro
, soopts
, mopts
, &ipoa
);
1262 /* Synchronize PCB cached route */
1263 inp_route_copyin(inp
, &ro
);
1265 if (udp_dodisconnect
) {
1266 in_pcbdisconnect(inp
);
1267 inp
->inp_laddr
= origladdr
; /* XXX rehash? */
1269 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
1273 if (udp_dodisconnect
) {
1274 in_pcbdisconnect(inp
);
1275 inp
->inp_laddr
= origladdr
; /* XXX rehash? */
1280 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
1284 u_int32_t udp_sendspace
= 9216; /* really max datagram size */
1285 /* 40 1K datagrams */
1286 u_int32_t udp_recvspace
= 40 * (1024 +
1288 sizeof(struct sockaddr_in6
)
1290 sizeof(struct sockaddr_in
)
1294 /* Check that the values of udp send and recv space do not exceed sb_max */
1296 sysctl_udp_sospace(struct sysctl_oid
*oidp
, __unused
void *arg1
,
1297 __unused
int arg2
, struct sysctl_req
*req
) {
1298 u_int32_t new_value
= 0, *space_p
= NULL
;
1299 int changed
= 0, error
= 0;
1300 u_quad_t sb_effective_max
= (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
;
1302 switch (oidp
->oid_number
) {
1303 case UDPCTL_RECVSPACE
:
1304 space_p
= &udp_recvspace
;
1306 case UDPCTL_MAXDGRAM
:
1307 space_p
= &udp_sendspace
;
1312 error
= sysctl_io_number(req
, *space_p
, sizeof(u_int32_t
),
1313 &new_value
, &changed
);
1315 if (new_value
> 0 && new_value
<= sb_effective_max
) {
1316 *space_p
= new_value
;
1324 SYSCTL_PROC(_net_inet_udp
, UDPCTL_RECVSPACE
, recvspace
, CTLTYPE_INT
| CTLFLAG_RW
,
1325 &udp_recvspace
, 0, &sysctl_udp_sospace
, "IU", "Maximum incoming UDP datagram size");
1327 SYSCTL_PROC(_net_inet_udp
, UDPCTL_MAXDGRAM
, maxdgram
, CTLTYPE_INT
| CTLFLAG_RW
,
1328 &udp_sendspace
, 0, &sysctl_udp_sospace
, "IU", "Maximum outgoing UDP datagram size");
1331 udp_abort(struct socket
*so
)
1335 inp
= sotoinpcb(so
);
1337 panic("udp_abort: so=%p null inp\n", so
); /* ??? possible? panic instead? */
1338 soisdisconnected(so
);
1344 udp_attach(struct socket
*so
, __unused
int proto
, struct proc
*p
)
1349 inp
= sotoinpcb(so
);
1351 panic ("udp_attach so=%p inp=%p\n", so
, inp
);
1353 error
= in_pcballoc(so
, &udbinfo
, p
);
1356 error
= soreserve(so
, udp_sendspace
, udp_recvspace
);
1359 inp
= (struct inpcb
*)so
->so_pcb
;
1360 inp
->inp_vflag
|= INP_IPV4
;
1361 inp
->inp_ip_ttl
= ip_defttl
;
1366 udp_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1371 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
1372 && nam
->sa_family
!= AF_INET6
) {
1373 return EAFNOSUPPORT
;
1375 inp
= sotoinpcb(so
);
1378 error
= in_pcbbind(inp
, nam
, p
);
1383 udp_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1388 inp
= sotoinpcb(so
);
1391 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
1393 error
= in_pcbconnect(inp
, nam
, p
);
1400 udp_detach(struct socket
*so
)
1404 inp
= sotoinpcb(so
);
1406 panic("udp_detach: so=%p null inp\n", so
); /* ??? possible? panic instead? */
1408 inp
->inp_state
= INPCB_STATE_DEAD
;
1413 udp_disconnect(struct socket
*so
)
1417 inp
= sotoinpcb(so
);
1420 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
)
1423 in_pcbdisconnect(inp
);
1424 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
1425 so
->so_state
&= ~SS_ISCONNECTED
; /* XXX */
1430 udp_send(struct socket
*so
, __unused
int flags
, struct mbuf
*m
, struct sockaddr
*addr
,
1431 struct mbuf
*control
, struct proc
*p
)
1435 inp
= sotoinpcb(so
);
1441 return udp_output(inp
, m
, addr
, control
, p
);
1445 udp_shutdown(struct socket
*so
)
1449 inp
= sotoinpcb(so
);
1456 struct pr_usrreqs udp_usrreqs
= {
1457 udp_abort
, pru_accept_notsupp
, udp_attach
, udp_bind
, udp_connect
,
1458 pru_connect2_notsupp
, in_control
, udp_detach
, udp_disconnect
,
1459 pru_listen_notsupp
, in_setpeeraddr
, pru_rcvd_notsupp
,
1460 pru_rcvoob_notsupp
, udp_send
, pru_sense_null
, udp_shutdown
,
1461 in_setsockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
1466 udp_lock(struct socket
*so
, int refcount
, void *debug
)
1471 lr_saved
= __builtin_return_address(0);
1476 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
1477 LCK_MTX_ASSERT_NOTOWNED
);
1478 lck_mtx_lock(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
1480 panic("udp_lock: so=%p NO PCB! lr=%p lrh= %s\n",
1481 so
, lr_saved
, solockhistory_nr(so
));
1487 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
1488 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
1493 udp_unlock(struct socket
*so
, int refcount
, void *debug
)
1498 lr_saved
= __builtin_return_address(0);
1505 if (so
->so_pcb
== NULL
) {
1506 panic("udp_unlock: so=%p NO PCB! lr=%p lrh= %s\n",
1507 so
, lr_saved
, solockhistory_nr(so
));
1510 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
1511 LCK_MTX_ASSERT_OWNED
);
1512 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
1513 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
1514 lck_mtx_unlock(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
1522 udp_getlock(struct socket
*so
, __unused
int locktype
)
1524 struct inpcb
*inp
= sotoinpcb(so
);
1528 return(inp
->inpcb_mtx
);
1530 panic("udp_getlock: so=%p NULL so_pcb lrh= %s\n",
1531 so
, solockhistory_nr(so
));
1532 return (so
->so_proto
->pr_domain
->dom_mtx
);
1539 struct inpcb
*inp
, *inpnxt
;
1541 struct inpcbinfo
*pcbinfo
= &udbinfo
;
1543 if (lck_rw_try_lock_exclusive(pcbinfo
->mtx
) == FALSE
) {
1544 if (udp_gc_done
== TRUE
) {
1545 udp_gc_done
= FALSE
;
1546 return; /* couldn't get the lock, better lock next time */
1548 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1553 for (inp
= udb
.lh_first
; inp
!= NULL
; inp
= inpnxt
) {
1554 inpnxt
= inp
->inp_list
.le_next
;
1556 if (inp
->inp_wantcnt
!= WNT_STOPUSING
)
1559 so
= inp
->inp_socket
;
1560 if (!lck_mtx_try_lock(inp
->inpcb_mtx
)) /* skip if busy, no hurry for cleanup... */
1563 if (so
->so_usecount
== 0) {
1564 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
1566 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
1574 lck_mtx_unlock(inp
->inpcb_mtx
);
1577 lck_rw_done(pcbinfo
->mtx
);
1581 ChkAddressOK( __uint32_t dstaddr
, __uint32_t srcaddr
)
1583 if ( dstaddr
== srcaddr
){
1590 udp_in_cksum_stats(u_int32_t len
)
1593 udps_in_sw_cksum_bytes
+= len
;
1597 udp_out_cksum_stats(u_int32_t len
)
1599 udps_out_sw_cksum
++;
1600 udps_out_sw_cksum_bytes
+= len
;