2  * Copyright (c) 2003-2012 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@ 
  30 #include <sys/systm.h> 
  31 #include <sys/kernel.h> 
  32 #include <sys/types.h> 
  33 #include <sys/socket.h> 
  34 #include <sys/socketvar.h> 
  35 #include <sys/param.h> 
  37 #include <sys/errno.h> 
  38 #include <sys/malloc.h> 
  39 #include <sys/protosw.h> 
  40 #include <sys/domain.h> 
  42 #include <sys/fcntl.h> 
  43 #include <sys/filio.h> 
  44 #include <sys/uio_internal.h> 
  45 #include <kern/locks.h> 
  46 #include <netinet/in.h> 
  47 #include <libkern/OSAtomic.h> 
  49 static errno_t 
sock_send_internal(socket_t
, const struct msghdr 
*, 
  50     mbuf_t
, int, size_t *); 
  51 static void sock_setupcalls_common(socket_t
, sock_upcall
, void *, 
  55 sock_accept(socket_t sock
, struct sockaddr 
*from
, int fromlen
, int flags
, 
  56     sock_upcall callback
, void *cookie
, socket_t 
*new_sock
) 
  59         struct socket 
*new_so
; 
  60         lck_mtx_t 
*mutex_held
; 
  64         if (sock 
== NULL 
|| new_sock 
== NULL
) 
  68         if ((sock
->so_options 
& SO_ACCEPTCONN
) == 0) { 
  69                 socket_unlock(sock
, 1); 
  72         if ((flags 
& ~(MSG_DONTWAIT
)) != 0) { 
  73                 socket_unlock(sock
, 1); 
  76         if (((flags 
& MSG_DONTWAIT
) != 0 || (sock
->so_state 
& SS_NBIO
) != 0) && 
  77             sock
->so_comp
.tqh_first 
== NULL
) { 
  78                 socket_unlock(sock
, 1); 
  82         if (sock
->so_proto
->pr_getlock 
!= NULL
)  { 
  83                 mutex_held 
= (*sock
->so_proto
->pr_getlock
)(sock
, 0); 
  86                 mutex_held 
= sock
->so_proto
->pr_domain
->dom_mtx
; 
  90         while (TAILQ_EMPTY(&sock
->so_comp
) && sock
->so_error 
== 0) { 
  91                 if (sock
->so_state 
& SS_CANTRCVMORE
) { 
  92                         sock
->so_error 
= ECONNABORTED
; 
  95                 error 
= msleep((caddr_t
)&sock
->so_timeo
, mutex_held
, 
  96                     PSOCK 
| PCATCH
, "sock_accept", NULL
); 
  98                         socket_unlock(sock
, 1); 
 102         if (sock
->so_error 
!= 0) { 
 103                 error 
= sock
->so_error
; 
 105                 socket_unlock(sock
, 1); 
 109         new_so 
= TAILQ_FIRST(&sock
->so_comp
); 
 110         TAILQ_REMOVE(&sock
->so_comp
, new_so
, so_list
); 
 114          * Pass the pre-accepted socket to any interested socket filter(s). 
 115          * Upon failure, the socket would have been closed by the callee. 
 117         if (new_so
->so_filt 
!= NULL
) { 
 119                  * Temporarily drop the listening socket's lock before we 
 120                  * hand off control over to the socket filter(s), but keep 
 121                  * a reference so that it won't go away.  We'll grab it 
 122                  * again once we're done with the filter(s). 
 124                 socket_unlock(sock
, 0); 
 125                 if ((error 
= soacceptfilter(new_so
)) != 0) { 
 126                         /* Drop reference on listening socket */ 
 130                 socket_lock(sock
, 0); 
 134                 lck_mtx_assert(new_so
->so_proto
->pr_getlock(new_so
, 0), 
 135                     LCK_MTX_ASSERT_NOTOWNED
); 
 136                 socket_lock(new_so
, 1); 
 139         new_so
->so_state 
&= ~SS_COMP
; 
 140         new_so
->so_head 
= NULL
; 
 141         (void) soacceptlock(new_so
, &sa
, 0); 
 143         socket_unlock(sock
, 1); /* release the head */ 
 145         /* see comments in sock_setupcall() */ 
 146         if (callback 
!= NULL
) { 
 147                 sock_setupcalls_common(new_so
, callback
, cookie
, NULL
, NULL
); 
 150         if (sa 
!= NULL 
&& from 
!= NULL
) { 
 151                 if (fromlen 
> sa
->sa_len
) 
 152                         fromlen 
= sa
->sa_len
; 
 153                 memcpy(from
, sa
, fromlen
); 
 159          * If the socket has been marked as inactive by sosetdefunct(), 
 160          * disallow further operations on it. 
 162         if (new_so
->so_flags 
& SOF_DEFUNCT
) { 
 163                 (void) sodefunct(current_proc(), new_so
, 
 164                     SHUTDOWN_SOCKET_LEVEL_DISCONNECT_INTERNAL
); 
 168                 socket_unlock(new_so
, 1); 
 173 sock_bind(socket_t sock
, const struct sockaddr 
*to
) 
 176         struct sockaddr 
*sa 
= NULL
; 
 177         struct sockaddr_storage ss
; 
 178         boolean_t want_free 
= TRUE
; 
 180         if (sock 
== NULL 
|| to 
== NULL
) 
 183         if (to
->sa_len 
> sizeof (ss
)) { 
 184                 MALLOC(sa
, struct sockaddr 
*, to
->sa_len
, M_SONAME
, M_WAITOK
); 
 188                 sa 
= (struct sockaddr 
*)&ss
; 
 191         memcpy(sa
, to
, to
->sa_len
); 
 193         error 
= sobindlock(sock
, sa
, 1);        /* will lock socket */ 
 195         if (sa 
!= NULL 
&& want_free 
== TRUE
) 
 202 sock_connect(socket_t sock
, const struct sockaddr 
*to
, int flags
) 
 205         lck_mtx_t 
*mutex_held
; 
 206         struct sockaddr 
*sa 
= NULL
; 
 207         struct sockaddr_storage ss
; 
 208         boolean_t want_free 
= TRUE
; 
 210         if (sock 
== NULL 
|| to 
== NULL
) 
 213         if (to
->sa_len 
> sizeof (ss
)) { 
 214                 MALLOC(sa
, struct sockaddr 
*, to
->sa_len
, M_SONAME
, 
 215                     (flags 
& MSG_DONTWAIT
) ? M_NOWAIT 
: M_WAITOK
); 
 219                 sa 
= (struct sockaddr 
*)&ss
; 
 222         memcpy(sa
, to
, to
->sa_len
); 
 224         socket_lock(sock
, 1); 
 226         if ((sock
->so_state 
& SS_ISCONNECTING
) && 
 227             ((sock
->so_state 
& SS_NBIO
) != 0 || (flags 
& MSG_DONTWAIT
) != 0)) { 
 231         error 
= soconnectlock(sock
, sa
, 0); 
 233                 if ((sock
->so_state 
& SS_ISCONNECTING
) && 
 234                     ((sock
->so_state 
& SS_NBIO
) != 0 || 
 235                     (flags 
& MSG_DONTWAIT
) != 0)) { 
 240                 if (sock
->so_proto
->pr_getlock 
!= NULL
) 
 241                         mutex_held 
= (*sock
->so_proto
->pr_getlock
)(sock
, 0); 
 243                         mutex_held 
= sock
->so_proto
->pr_domain
->dom_mtx
; 
 245                 while ((sock
->so_state 
& SS_ISCONNECTING
) && 
 246                     sock
->so_error 
== 0) { 
 247                         error 
= msleep((caddr_t
)&sock
->so_timeo
, 
 248                             mutex_held
, PSOCK 
| PCATCH
, "sock_connect", NULL
); 
 254                         error 
= sock
->so_error
; 
 258                 sock
->so_state 
&= ~SS_ISCONNECTING
; 
 261         socket_unlock(sock
, 1); 
 263         if (sa 
!= NULL 
&& want_free 
== TRUE
) 
 270 sock_connectwait(socket_t sock
, const struct timeval 
*tv
) 
 272         lck_mtx_t 
*mutex_held
; 
 276         socket_lock(sock
, 1); 
 278         /* Check if we're already connected or if we've already errored out */ 
 279         if ((sock
->so_state 
& SS_ISCONNECTING
) == 0 || sock
->so_error 
!= 0) { 
 280                 if (sock
->so_error 
!= 0) { 
 281                         retval 
= sock
->so_error
; 
 284                         if ((sock
->so_state 
& SS_ISCONNECTED
) != 0) 
 292         /* copied translation from timeval to hertz from SO_RCVTIMEO handling */ 
 293         if (tv
->tv_sec 
< 0 || tv
->tv_sec 
> SHRT_MAX 
/ hz 
|| 
 294             tv
->tv_usec 
< 0 || tv
->tv_usec 
>= 1000000) { 
 299         ts
.tv_sec 
= tv
->tv_sec
; 
 300         ts
.tv_nsec 
= (tv
->tv_usec 
* (integer_t
)NSEC_PER_USEC
); 
 301         if ((ts
.tv_sec 
+ (ts
.tv_nsec
/(long)NSEC_PER_SEC
))/100  >  SHRT_MAX
)  { 
 306         if (sock
->so_proto
->pr_getlock 
!= NULL
) 
 307                 mutex_held 
= (*sock
->so_proto
->pr_getlock
)(sock
, 0); 
 309                 mutex_held 
= sock
->so_proto
->pr_domain
->dom_mtx
; 
 311         msleep((caddr_t
)&sock
->so_timeo
, mutex_held
, 
 312             PSOCK
, "sock_connectwait", &ts
); 
 314         /* Check if we're still waiting to connect */ 
 315         if ((sock
->so_state 
& SS_ISCONNECTING
) && sock
->so_error 
== 0) { 
 316                 retval 
= EINPROGRESS
; 
 320         if (sock
->so_error 
!= 0) { 
 321                 retval 
= sock
->so_error
; 
 326         socket_unlock(sock
, 1); 
 331 sock_nointerrupt(socket_t sock
, int on
) 
 333         socket_lock(sock
, 1); 
 336                 sock
->so_rcv
.sb_flags 
|= SB_NOINTR
;     /* This isn't safe */ 
 337                 sock
->so_snd
.sb_flags 
|= SB_NOINTR
;     /* This isn't safe */ 
 339                 sock
->so_rcv
.sb_flags 
&= ~SB_NOINTR
;    /* This isn't safe */ 
 340                 sock
->so_snd
.sb_flags 
&= ~SB_NOINTR
;    /* This isn't safe */ 
 343         socket_unlock(sock
, 1); 
 349 sock_getpeername(socket_t sock
, struct sockaddr 
*peername
, int peernamelen
) 
 352         struct sockaddr 
*sa 
= NULL
; 
 354         if (sock 
== NULL 
|| peername 
== NULL 
|| peernamelen 
< 0) 
 357         socket_lock(sock
, 1); 
 358         if (!(sock
->so_state 
& (SS_ISCONNECTED
|SS_ISCONFIRMING
))) { 
 359                 socket_unlock(sock
, 1); 
 362         error 
= sogetaddr_locked(sock
, &sa
, 1); 
 363         socket_unlock(sock
, 1); 
 365                 if (peernamelen 
> sa
->sa_len
) 
 366                         peernamelen 
= sa
->sa_len
; 
 367                 memcpy(peername
, sa
, peernamelen
); 
 374 sock_getsockname(socket_t sock
, struct sockaddr 
*sockname
, int socknamelen
) 
 377         struct sockaddr 
*sa 
= NULL
; 
 379         if (sock 
== NULL 
|| sockname 
== NULL 
|| socknamelen 
< 0) 
 382         socket_lock(sock
, 1); 
 383         error 
= sogetaddr_locked(sock
, &sa
, 0); 
 384         socket_unlock(sock
, 1); 
 386                 if (socknamelen 
> sa
->sa_len
) 
 387                         socknamelen 
= sa
->sa_len
; 
 388                 memcpy(sockname
, sa
, socknamelen
); 
 394 __private_extern__ 
int 
 395 sogetaddr_locked(struct socket 
*so
, struct sockaddr 
**psa
, int peer
) 
 399         if (so 
== NULL 
|| psa 
== NULL
) 
 403         error 
= peer 
? so
->so_proto
->pr_usrreqs
->pru_peeraddr(so
, psa
) : 
 404             so
->so_proto
->pr_usrreqs
->pru_sockaddr(so
, psa
); 
 406         if (error 
== 0 && *psa 
== NULL
) { 
 408         } else if (error 
!= 0 && *psa 
!= NULL
) { 
 409                 FREE(*psa
, M_SONAME
); 
 416 sock_getaddr(socket_t sock
, struct sockaddr 
**psa
, int peer
) 
 420         if (sock 
== NULL 
|| psa 
== NULL
) 
 423         socket_lock(sock
, 1); 
 424         error 
= sogetaddr_locked(sock
, psa
, peer
); 
 425         socket_unlock(sock
, 1); 
 431 sock_freeaddr(struct sockaddr 
*sa
) 
 438 sock_getsockopt(socket_t sock
, int level
, int optname
, void *optval
, 
 444         if (sock 
== NULL 
|| optval 
== NULL 
|| optlen 
== NULL
) 
 447         sopt
.sopt_dir 
= SOPT_GET
; 
 448         sopt
.sopt_level 
= level
; 
 449         sopt
.sopt_name 
= optname
; 
 450         sopt
.sopt_val 
= CAST_USER_ADDR_T(optval
); 
 451         sopt
.sopt_valsize 
= *optlen
; 
 452         sopt
.sopt_p 
= kernproc
; 
 453         error 
= sogetoptlock(sock
, &sopt
, 1);   /* will lock socket */ 
 455                 *optlen 
= sopt
.sopt_valsize
; 
 460 sock_ioctl(socket_t sock
, unsigned long request
, void *argp
) 
 462         return (soioctl(sock
, request
, argp
, kernproc
)); /* will lock socket */ 
 466 sock_setsockopt(socket_t sock
, int level
, int optname
, const void *optval
, 
 471         if (sock 
== NULL 
|| optval 
== NULL
) 
 474         sopt
.sopt_dir 
= SOPT_SET
; 
 475         sopt
.sopt_level 
= level
; 
 476         sopt
.sopt_name 
= optname
; 
 477         sopt
.sopt_val 
= CAST_USER_ADDR_T(optval
); 
 478         sopt
.sopt_valsize 
= optlen
; 
 479         sopt
.sopt_p 
= kernproc
; 
 480         return (sosetoptlock(sock
, &sopt
, 1)); /* will lock socket */ 
 484  * This follows the recommended mappings between DSCP code points 
 485  * and WMM access classes. 
 487 static u_int32_t 
so_tc_from_dscp(u_int8_t dscp
); 
 489 so_tc_from_dscp(u_int8_t dscp
) 
 493         if (dscp 
>= 0x30 && dscp 
<= 0x3f) 
 495         else if (dscp 
>= 0x20 && dscp 
<= 0x2f) 
 497         else if (dscp 
>= 0x08 && dscp 
<= 0x17) 
 506 sock_settclassopt(socket_t sock
, const void *optval
, size_t optlen
) 
 512         if (sock 
== NULL 
|| optval 
== NULL 
|| optlen 
!= sizeof (int)) 
 515         socket_lock(sock
, 1); 
 516         if (!(sock
->so_state 
& SS_ISCONNECTED
)) { 
 518                  * If the socket is not connected then we don't know 
 519                  * if the destination is on LAN  or not. Skip 
 520                  * setting traffic class in this case 
 526         if (sock
->so_proto 
== NULL 
|| sock
->so_proto
->pr_domain 
== NULL 
|| 
 527             sock
->so_pcb 
== NULL
) { 
 533          * Set the socket traffic class based on the passed DSCP code point 
 534          * regardless of the scope of the destination 
 536         sotc 
= so_tc_from_dscp((*(const int *)optval
) >> 2); 
 538         sopt
.sopt_dir 
= SOPT_SET
; 
 539         sopt
.sopt_val 
= CAST_USER_ADDR_T(&sotc
); 
 540         sopt
.sopt_valsize 
= sizeof (sotc
); 
 541         sopt
.sopt_p 
= kernproc
; 
 542         sopt
.sopt_level 
= SOL_SOCKET
; 
 543         sopt
.sopt_name 
= SO_TRAFFIC_CLASS
; 
 545         error 
= sosetoptlock(sock
, &sopt
, 0);   /* already locked */ 
 548                 printf("%s: sosetopt SO_TRAFFIC_CLASS failed %d\n", 
 554          * Check if the destination address is LAN or link local address. 
 555          * We do not want to set traffic class bits if the destination 
 558         if (!so_isdstlocal(sock
)) 
 561         sopt
.sopt_dir 
= SOPT_SET
; 
 562         sopt
.sopt_val 
= CAST_USER_ADDR_T(optval
); 
 563         sopt
.sopt_valsize 
= optlen
; 
 564         sopt
.sopt_p 
= kernproc
; 
 566         switch (SOCK_DOM(sock
)) { 
 568                 sopt
.sopt_level 
= IPPROTO_IP
; 
 569                 sopt
.sopt_name 
= IP_TOS
; 
 572                 sopt
.sopt_level 
= IPPROTO_IPV6
; 
 573                 sopt
.sopt_name 
= IPV6_TCLASS
; 
 580         error 
= sosetoptlock(sock
, &sopt
, 0);   /* already locked */ 
 581         socket_unlock(sock
, 1); 
 584         socket_unlock(sock
, 1); 
 589 sock_gettclassopt(socket_t sock
, void *optval
, size_t *optlen
) 
 594         if (sock 
== NULL 
|| optval 
== NULL 
|| optlen 
== NULL
) 
 597         sopt
.sopt_dir 
= SOPT_GET
; 
 598         sopt
.sopt_val 
= CAST_USER_ADDR_T(optval
); 
 599         sopt
.sopt_valsize 
= *optlen
; 
 600         sopt
.sopt_p 
= kernproc
; 
 602         socket_lock(sock
, 1); 
 603         if (sock
->so_proto 
== NULL 
|| sock
->so_proto
->pr_domain 
== NULL
) { 
 604                 socket_unlock(sock
, 1); 
 608         switch (SOCK_DOM(sock
)) { 
 610                 sopt
.sopt_level 
= IPPROTO_IP
; 
 611                 sopt
.sopt_name 
= IP_TOS
; 
 614                 sopt
.sopt_level 
= IPPROTO_IPV6
; 
 615                 sopt
.sopt_name 
= IPV6_TCLASS
; 
 618                 socket_unlock(sock
, 1); 
 622         error 
= sogetoptlock(sock
, &sopt
, 0);   /* already locked */ 
 623         socket_unlock(sock
, 1); 
 625                 *optlen 
= sopt
.sopt_valsize
; 
 630 sock_listen(socket_t sock
, int backlog
) 
 635         return (solisten(sock
, backlog
)); /* will lock socket */ 
 639 sock_receive_internal(socket_t sock
, struct msghdr 
*msg
, mbuf_t 
*data
, 
 640     int flags
, size_t *recvdlen
) 
 643         struct mbuf 
*control 
= NULL
; 
 646         struct sockaddr 
*fromsa 
= NULL
; 
 647         char uio_buf
[ UIO_SIZEOF((msg 
!= NULL
) ? msg
->msg_iovlen 
: 0) ]; 
 652         auio 
= uio_createwithbuffer(((msg 
!= NULL
) ? msg
->msg_iovlen 
: 0), 
 653             0, UIO_SYSSPACE
, UIO_READ
, &uio_buf
[0], sizeof (uio_buf
)); 
 654         if (msg 
!= NULL 
&& data 
== NULL
) { 
 656                 struct iovec 
*tempp 
= msg
->msg_iov
; 
 658                 for (i 
= 0; i 
< msg
->msg_iovlen
; i
++) { 
 660                             CAST_USER_ADDR_T((tempp 
+ i
)->iov_base
), 
 661                             (tempp 
+ i
)->iov_len
); 
 663                 if (uio_resid(auio
) < 0) 
 665         } else if (recvdlen 
!= NULL
) { 
 666                 uio_setresid(auio
, (uio_resid(auio
) + *recvdlen
)); 
 668         length 
= uio_resid(auio
); 
 670         if (recvdlen 
!= NULL
) 
 673         /* let pru_soreceive handle the socket locking */ 
 674         error 
= sock
->so_proto
->pr_usrreqs
->pru_soreceive(sock
, &fromsa
, auio
, 
 675             data
, (msg 
&& msg
->msg_control
) ? &control 
: NULL
, &flags
); 
 679         if (recvdlen 
!= NULL
) 
 680                 *recvdlen 
= length 
- uio_resid(auio
); 
 682                 msg
->msg_flags 
= flags
; 
 684                 if (msg
->msg_name 
!= NULL
) { 
 686                         salen 
= msg
->msg_namelen
; 
 687                         if (msg
->msg_namelen 
> 0 && fromsa 
!= NULL
) { 
 688                                 salen 
= MIN(salen
, fromsa
->sa_len
); 
 689                                 memcpy(msg
->msg_name
, fromsa
, 
 690                                     msg
->msg_namelen 
> fromsa
->sa_len 
? 
 691                                     fromsa
->sa_len 
: msg
->msg_namelen
); 
 695                 if (msg
->msg_control 
!= NULL
) { 
 696                         struct mbuf 
*m 
= control
; 
 697                         u_char 
*ctlbuf 
= msg
->msg_control
; 
 698                         int clen 
= msg
->msg_controllen
; 
 700                         msg
->msg_controllen 
= 0; 
 702                         while (m 
!= NULL 
&& clen 
> 0) { 
 705                                 if (clen 
>= m
->m_len
) { 
 708                                         msg
->msg_flags 
|= MSG_CTRUNC
; 
 711                                 memcpy(ctlbuf
, mtod(m
, caddr_t
), tocopy
); 
 716                         msg
->msg_controllen 
= 
 717                             (uintptr_t)ctlbuf 
- (uintptr_t)msg
->msg_control
; 
 725                 FREE(fromsa
, M_SONAME
); 
 730 sock_receive(socket_t sock
, struct msghdr 
*msg
, int flags
, size_t *recvdlen
) 
 732         if ((msg 
== NULL
) || (msg
->msg_iovlen 
< 1) || 
 733             (msg
->msg_iov
[0].iov_len 
== 0) || 
 734             (msg
->msg_iov
[0].iov_base 
== NULL
)) 
 737         return (sock_receive_internal(sock
, msg
, NULL
, flags
, recvdlen
)); 
 741 sock_receivembuf(socket_t sock
, struct msghdr 
*msg
, mbuf_t 
*data
, int flags
, 
 744         if (data 
== NULL 
|| recvlen 
== 0 || *recvlen 
<= 0 || (msg 
!= NULL 
&& 
 745             (msg
->msg_iov 
!= NULL 
|| msg
->msg_iovlen 
!= 0))) 
 748         return (sock_receive_internal(sock
, msg
, data
, flags
, recvlen
)); 
 752 sock_send_internal(socket_t sock
, const struct msghdr 
*msg
, mbuf_t data
, 
 753     int flags
, size_t *sentlen
) 
 756         struct mbuf 
*control 
= NULL
; 
 759         char uio_buf
[ UIO_SIZEOF((msg 
!= NULL 
? msg
->msg_iovlen 
: 1)) ]; 
 766         if (data 
== NULL 
&& msg 
!= NULL
) { 
 767                 struct iovec 
*tempp 
= msg
->msg_iov
; 
 769                 auio 
= uio_createwithbuffer(msg
->msg_iovlen
, 0, 
 770                     UIO_SYSSPACE
, UIO_WRITE
, &uio_buf
[0], sizeof (uio_buf
)); 
 774                         for (i 
= 0; i 
< msg
->msg_iovlen
; i
++) { 
 776                                     CAST_USER_ADDR_T((tempp 
+ i
)->iov_base
), 
 777                                     (tempp 
+ i
)->iov_len
); 
 780                         if (uio_resid(auio
) < 0) { 
 791                 datalen 
= uio_resid(auio
); 
 793                 datalen 
= data
->m_pkthdr
.len
; 
 795         if (msg 
!= NULL 
&& msg
->msg_control
) { 
 796                 if ((size_t)msg
->msg_controllen 
< sizeof (struct cmsghdr
)) { 
 801                 if ((size_t)msg
->msg_controllen 
> MLEN
) { 
 806                 control 
= m_get(M_NOWAIT
, MT_CONTROL
); 
 807                 if (control 
== NULL
) { 
 811                 memcpy(mtod(control
, caddr_t
), msg
->msg_control
, 
 812                     msg
->msg_controllen
); 
 813                 control
->m_len 
= msg
->msg_controllen
; 
 816         error 
= sock
->so_proto
->pr_usrreqs
->pru_sosend(sock
, msg 
!= NULL 
? 
 817             (struct sockaddr 
*)msg
->msg_name 
: NULL
, auio
, data
, 
 821          * Residual data is possible in the case of IO vectors but not 
 822          * in the mbuf case since the latter is treated as atomic send. 
 823          * If pru_sosend() consumed a portion of the iovecs data and 
 824          * the error returned is transient, treat it as success; this 
 825          * is consistent with sendit() behavior. 
 827         if (auio 
!= NULL 
&& uio_resid(auio
) != datalen 
&& 
 828             (error 
== ERESTART 
|| error 
== EINTR 
|| error 
== EWOULDBLOCK
)) 
 831         if (error 
== 0 && sentlen 
!= NULL
) { 
 833                         *sentlen 
= datalen 
- uio_resid(auio
); 
 841  * In cases where we detect an error before returning, we need to 
 842  * free the mbuf chain if there is one. sosend (and pru_sosend) will 
 843  * free the mbuf chain if they encounter an error. 
 856 sock_send(socket_t sock
, const struct msghdr 
*msg
, int flags
, size_t *sentlen
) 
 858         if (msg 
== NULL 
|| msg
->msg_iov 
== NULL 
|| msg
->msg_iovlen 
< 1) 
 861         return (sock_send_internal(sock
, msg
, NULL
, flags
, sentlen
)); 
 865 sock_sendmbuf(socket_t sock
, const struct msghdr 
*msg
, mbuf_t data
, 
 866     int flags
, size_t *sentlen
) 
 868         if (data 
== NULL 
|| (msg 
!= NULL 
&& (msg
->msg_iov 
!= NULL 
|| 
 869             msg
->msg_iovlen 
!= 0))) { 
 874         return (sock_send_internal(sock
, msg
, data
, flags
, sentlen
)); 
 878 sock_shutdown(socket_t sock
, int how
) 
 883         return (soshutdown(sock
, how
)); 
 888 sock_socket(int domain
, int type
, int protocol
, sock_upcall callback
, 
 889     void *context
, socket_t 
*new_so
) 
 896         /* socreate will create an initial so_count */ 
 897         error 
= socreate(domain
, new_so
, type
, protocol
); 
 899                 /* see comments in sock_setupcall() */ 
 900                 if (callback 
!= NULL
) { 
 901                         sock_setupcalls_common(*new_so
, callback
, context
, 
 905                  * last_pid and last_upid should be zero for sockets 
 906                  * created using sock_socket 
 908                 (*new_so
)->last_pid 
= 0; 
 909                 (*new_so
)->last_upid 
= 0; 
 915 sock_close(socket_t sock
) 
 923 /* Do we want this to be APPLE_PRIVATE API?: YES (LD 12/23/04) */ 
 925 sock_retain(socket_t sock
) 
 930         socket_lock(sock
, 1); 
 931         sock
->so_retaincnt
++; 
 932         sock
->so_usecount
++;    /* add extra reference for holding the socket */ 
 933         socket_unlock(sock
, 1); 
 936 /* Do we want this to be APPLE_PRIVATE API? */ 
 938 sock_release(socket_t sock
) 
 943         socket_lock(sock
, 1); 
 944         if (sock
->so_upcallusecount 
> 0) 
 945                 soclose_wait_locked(sock
); 
 947         sock
->so_retaincnt
--; 
 948         if (sock
->so_retaincnt 
< 0) { 
 949                 panic("%s: negative retain count (%d) for sock=%p\n", 
 950                     __func__
, sock
->so_retaincnt
, sock
); 
 953         if ((sock
->so_retaincnt 
== 0) && (sock
->so_usecount 
== 2)) { 
 954                 /* close socket only if the FD is not holding it */ 
 955                 soclose_locked(sock
); 
 957                 /* remove extra reference holding the socket */ 
 960         socket_unlock(sock
, 1); 
 964 sock_setpriv(socket_t sock
, int on
) 
 969         socket_lock(sock
, 1); 
 971                 sock
->so_state 
|= SS_PRIV
; 
 973                 sock
->so_state 
&= ~SS_PRIV
; 
 974         socket_unlock(sock
, 1); 
 979 sock_isconnected(socket_t sock
) 
 983         socket_lock(sock
, 1); 
 984         retval 
= ((sock
->so_state 
& SS_ISCONNECTED
) ? 1 : 0); 
 985         socket_unlock(sock
, 1); 
 990 sock_isnonblocking(socket_t sock
) 
 994         socket_lock(sock
, 1); 
 995         retval 
= ((sock
->so_state 
& SS_NBIO
) ? 1 : 0); 
 996         socket_unlock(sock
, 1); 
1001 sock_gettype(socket_t sock
, int *outDomain
, int *outType
, int *outProtocol
) 
1003         socket_lock(sock
, 1); 
1004         if (outDomain 
!= NULL
) 
1005                 *outDomain 
= SOCK_DOM(sock
); 
1006         if (outType 
!= NULL
) 
1007                 *outType 
= sock
->so_type
; 
1008         if (outProtocol 
!= NULL
) 
1009                 *outProtocol 
= SOCK_PROTO(sock
); 
1010         socket_unlock(sock
, 1); 
1015  * Return the listening socket of a pre-accepted socket.  It returns the 
1016  * listener (so_head) value of a given socket.  This is intended to be 
1017  * called by a socket filter during a filter attach (sf_attach) callback. 
1018  * The value returned by this routine is safe to be used only in the 
1019  * context of that callback, because we hold the listener's lock across 
1020  * the sflt_initsock() call. 
1023 sock_getlistener(socket_t sock
) 
1025         return (sock
->so_head
); 
1029 sock_set_tcp_stream_priority(socket_t sock
) 
1031         if ((SOCK_DOM(sock
) == PF_INET 
|| SOCK_DOM(sock
) == PF_INET6
) && 
1032             SOCK_TYPE(sock
) == SOCK_STREAM
) { 
1033                 set_tcp_stream_priority(sock
); 
1038  * Caller must have ensured socket is valid and won't be going away. 
1041 socket_set_traffic_mgt_flags_locked(socket_t sock
, u_int32_t flags
) 
1043         (void) OSBitOrAtomic(flags
, &sock
->so_traffic_mgt_flags
); 
1044         sock_set_tcp_stream_priority(sock
); 
1048 socket_set_traffic_mgt_flags(socket_t sock
, u_int32_t flags
) 
1050         socket_lock(sock
, 1); 
1051         socket_set_traffic_mgt_flags_locked(sock
, flags
); 
1052         socket_unlock(sock
, 1); 
1056  * Caller must have ensured socket is valid and won't be going away. 
1059 socket_clear_traffic_mgt_flags_locked(socket_t sock
, u_int32_t flags
) 
1061         (void) OSBitAndAtomic(~flags
, &sock
->so_traffic_mgt_flags
); 
1062         sock_set_tcp_stream_priority(sock
); 
1066 socket_clear_traffic_mgt_flags(socket_t sock
, u_int32_t flags
) 
1068         socket_lock(sock
, 1); 
1069         socket_clear_traffic_mgt_flags_locked(sock
, flags
); 
1070         socket_unlock(sock
, 1); 
1075  * Caller must have ensured socket is valid and won't be going away. 
1078 socket_defunct(struct proc 
*p
, socket_t so
, int level
) 
1082         if (level 
!= SHUTDOWN_SOCKET_LEVEL_DISCONNECT_SVC 
&& 
1083             level 
!= SHUTDOWN_SOCKET_LEVEL_DISCONNECT_ALL
) 
1088          * SHUTDOWN_SOCKET_LEVEL_DISCONNECT_SVC level is meant to tear down 
1089          * all of mDNSResponder IPC sockets, currently those of AF_UNIX; note 
1090          * that this is an implementation artifact of mDNSResponder.  We do 
1091          * a quick test against the socket buffers for SB_UNIX, since that 
1092          * would have been set by unp_attach() at socket creation time. 
1094         if (level 
== SHUTDOWN_SOCKET_LEVEL_DISCONNECT_SVC 
&& 
1095             (so
->so_rcv
.sb_flags 
& so
->so_snd
.sb_flags 
& SB_UNIX
) != SB_UNIX
) { 
1096                 socket_unlock(so
, 1); 
1097                 return (EOPNOTSUPP
); 
1099         retval 
= sosetdefunct(p
, so
, level
, TRUE
); 
1101                 retval 
= sodefunct(p
, so
, level
); 
1102         socket_unlock(so
, 1); 
1107 sock_setupcalls_common(socket_t sock
, sock_upcall rcallback
, void *rcontext
, 
1108     sock_upcall wcallback
, void *wcontext
) 
1110         if (rcallback 
!= NULL
) { 
1111                 sock
->so_rcv
.sb_flags 
|= SB_UPCALL
; 
1112                 sock
->so_rcv
.sb_upcall 
= rcallback
; 
1113                 sock
->so_rcv
.sb_upcallarg 
= rcontext
; 
1115                 sock
->so_rcv
.sb_flags 
&= ~SB_UPCALL
; 
1116                 sock
->so_rcv
.sb_upcall 
= NULL
; 
1117                 sock
->so_rcv
.sb_upcallarg 
= NULL
; 
1120         if (wcallback 
!= NULL
) { 
1121                 sock
->so_snd
.sb_flags 
|= SB_UPCALL
; 
1122                 sock
->so_snd
.sb_upcall 
= wcallback
; 
1123                 sock
->so_snd
.sb_upcallarg 
= wcontext
; 
1125                 sock
->so_snd
.sb_flags 
&= ~SB_UPCALL
; 
1126                 sock
->so_snd
.sb_upcall 
= NULL
; 
1127                 sock
->so_snd
.sb_upcallarg 
= NULL
; 
1132 sock_setupcall(socket_t sock
, sock_upcall callback
, void *context
) 
1138          * Note that we don't wait for any in progress upcall to complete. 
1139          * On embedded, sock_setupcall() causes both read and write 
1140          * callbacks to be set; on desktop, only read callback is set 
1141          * to maintain legacy KPI behavior. 
1143          * The newer sock_setupcalls() KPI should be used instead to set 
1144          * the read and write callbacks and their respective parameters. 
1146         socket_lock(sock
, 1); 
1147         sock_setupcalls_common(sock
, callback
, context
, NULL
, NULL
); 
1148         socket_unlock(sock
, 1); 
1154 sock_setupcalls(socket_t sock
, sock_upcall rcallback
, void *rcontext
, 
1155     sock_upcall wcallback
, void *wcontext
) 
1161          * Note that we don't wait for any in progress upcall to complete. 
1163         socket_lock(sock
, 1); 
1164         sock_setupcalls_common(sock
, rcallback
, rcontext
, wcallback
, wcontext
); 
1165         socket_unlock(sock
, 1); 
1171 sock_catchevents(socket_t sock
, sock_evupcall ecallback
, void *econtext
, 
1178          * Note that we don't wait for any in progress upcall to complete. 
1180         socket_lock(sock
, 1); 
1181         if (ecallback 
!= NULL
) { 
1182                 sock
->so_event 
= ecallback
; 
1183                 sock
->so_eventarg 
= econtext
; 
1184                 sock
->so_eventmask 
= emask
; 
1186                 sock
->so_event 
= sonullevent
; 
1187                 sock
->so_eventarg 
= NULL
; 
1188                 sock
->so_eventmask 
= 0; 
1190         socket_unlock(sock
, 1); 
1196  * Returns true whether or not a socket belongs to the kernel. 
1199 sock_iskernel(socket_t so
) 
1201         return (so 
&& so
->last_pid 
== 0);