]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kpi_socket.c
165d9b64aa147eefa7bd3dc90b8d3ef9bb0cf033
2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <sys/kernel.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/socketvar.h>
29 #include <sys/param.h>
31 #include <sys/errno.h>
32 #include <sys/malloc.h>
33 #include <sys/protosw.h>
34 #include <sys/domain.h>
36 #include <sys/fcntl.h>
37 #include <sys/filio.h>
38 #include <sys/uio_internal.h>
39 #include <kern/lock.h>
41 extern void *memcpy(void *, const void *, size_t);
42 extern int soclose_locked(struct socket
*so
);
44 errno_t
sock_send_internal(
46 const struct msghdr
*msg
,
56 struct sockaddr
*from
,
64 struct socket
*new_so
;
65 lck_mtx_t
*mutex_held
;
69 if (sock
== NULL
|| new_sock
== NULL
) return EINVAL
;
71 if ((sock
->so_options
& SO_ACCEPTCONN
) == 0) {
72 socket_unlock(sock
, 1);
75 if ((flags
& ~(MSG_DONTWAIT
)) != 0) {
76 socket_unlock(sock
, 1);
79 if (((flags
& MSG_DONTWAIT
) != 0 || (sock
->so_state
& SS_NBIO
) != 0) &&
80 sock
->so_comp
.tqh_first
== NULL
) {
81 socket_unlock(sock
, 1);
85 if (sock
->so_proto
->pr_getlock
!= NULL
) {
86 mutex_held
= (*sock
->so_proto
->pr_getlock
)(sock
, 0);
90 mutex_held
= sock
->so_proto
->pr_domain
->dom_mtx
;
94 while (TAILQ_EMPTY(&sock
->so_comp
) && sock
->so_error
== 0) {
95 if (sock
->so_state
& SS_CANTRCVMORE
) {
96 sock
->so_error
= ECONNABORTED
;
99 error
= msleep((caddr_t
)&sock
->so_timeo
, mutex_held
, PSOCK
| PCATCH
, "sock_accept", 0);
101 socket_unlock(sock
, 1);
105 if (sock
->so_error
) {
106 error
= sock
->so_error
;
108 socket_unlock(sock
, 1);
112 new_so
= TAILQ_FIRST(&sock
->so_comp
);
113 TAILQ_REMOVE(&sock
->so_comp
, new_so
, so_list
);
115 socket_unlock(sock
, 1); /* release the head */
118 lck_mtx_assert(new_so
->so_proto
->pr_getlock(new_so
, 0),
119 LCK_MTX_ASSERT_NOTOWNED
);
120 socket_lock(new_so
, 1);
123 new_so
->so_state
&= ~SS_COMP
;
124 new_so
->so_head
= NULL
;
125 soacceptlock(new_so
, &sa
, 0);
128 new_so
->so_upcall
= callback
;
129 new_so
->so_upcallarg
= cookie
;
130 new_so
->so_rcv
.sb_flags
|= SB_UPCALL
;
135 if (fromlen
> sa
->sa_len
) fromlen
= sa
->sa_len
;
136 memcpy(from
, sa
, fromlen
);
138 if (sa
) FREE(sa
, M_SONAME
);
141 socket_unlock(new_so
, 1);
148 const struct sockaddr
*to
)
150 if (sock
== NULL
|| to
== NULL
) return EINVAL
;
152 return sobind(sock
, (struct sockaddr
*)to
);
158 const struct sockaddr
*to
,
162 lck_mtx_t
*mutex_held
;
164 if (sock
== NULL
|| to
== NULL
) return EINVAL
;
166 socket_lock(sock
, 1);
168 if ((sock
->so_state
& SS_ISCONNECTING
) &&
169 ((sock
->so_state
& SS_NBIO
) != 0 ||
170 (flags
& MSG_DONTWAIT
) != 0)) {
171 socket_unlock(sock
, 1);
174 error
= soconnectlock(sock
, (struct sockaddr
*)to
, 0);
176 if ((sock
->so_state
& SS_ISCONNECTING
) &&
177 ((sock
->so_state
& SS_NBIO
) != 0 || (flags
& MSG_DONTWAIT
) != 0)) {
178 socket_unlock(sock
, 1);
182 if (sock
->so_proto
->pr_getlock
!= NULL
)
183 mutex_held
= (*sock
->so_proto
->pr_getlock
)(sock
, 0);
185 mutex_held
= sock
->so_proto
->pr_domain
->dom_mtx
;
187 while ((sock
->so_state
& SS_ISCONNECTING
) && sock
->so_error
== 0) {
188 error
= msleep((caddr_t
)&sock
->so_timeo
, mutex_held
, PSOCK
| PCATCH
,
195 error
= sock
->so_error
;
200 sock
->so_state
&= ~SS_ISCONNECTING
;
202 socket_unlock(sock
, 1);
209 const struct timeval
*tv
)
211 lck_mtx_t
* mutex_held
;
215 socket_lock(sock
, 1);
217 // Check if we're already connected or if we've already errored out
218 if ((sock
->so_state
& SS_ISCONNECTING
) == 0 || sock
->so_error
) {
219 if (sock
->so_error
) {
220 retval
= sock
->so_error
;
224 if ((sock
->so_state
& SS_ISCONNECTED
) != 0)
232 // copied translation from timeval to hertz from SO_RCVTIMEO handling
233 if (tv
->tv_sec
< 0 || tv
->tv_sec
> SHRT_MAX
/ hz
||
234 tv
->tv_usec
< 0 || tv
->tv_usec
>= 1000000) {
239 ts
.tv_sec
= tv
->tv_sec
;
240 ts
.tv_nsec
= (tv
->tv_usec
* NSEC_PER_USEC
);
241 if ( (ts
.tv_sec
+ (ts
.tv_nsec
/NSEC_PER_SEC
))/100 > SHRT_MAX
) {
246 if (sock
->so_proto
->pr_getlock
!= NULL
)
247 mutex_held
= (*sock
->so_proto
->pr_getlock
)(sock
, 0);
249 mutex_held
= sock
->so_proto
->pr_domain
->dom_mtx
;
251 msleep((caddr_t
)&sock
->so_timeo
, mutex_held
, PSOCK
, "sock_connectwait", &ts
);
253 // Check if we're still waiting to connect
254 if ((sock
->so_state
& SS_ISCONNECTING
) && sock
->so_error
== 0) {
255 retval
= EINPROGRESS
;
259 if (sock
->so_error
) {
260 retval
= sock
->so_error
;
265 socket_unlock(sock
, 1);
274 socket_lock(sock
, 1);
277 sock
->so_rcv
.sb_flags
|= SB_NOINTR
; // This isn't safe
278 sock
->so_snd
.sb_flags
|= SB_NOINTR
; // This isn't safe
281 sock
->so_rcv
.sb_flags
&= ~SB_NOINTR
; // This isn't safe
282 sock
->so_snd
.sb_flags
&= ~SB_NOINTR
; // This isn't safe
285 socket_unlock(sock
, 1);
293 struct sockaddr
*peername
,
297 struct sockaddr
*sa
= NULL
;
299 if (sock
== NULL
|| peername
== NULL
|| peernamelen
< 0) return EINVAL
;
300 socket_lock(sock
, 1);
301 if ((sock
->so_state
& (SS_ISCONNECTED
|SS_ISCONFIRMING
)) == 0) {
302 socket_unlock(sock
, 1);
305 error
= sock
->so_proto
->pr_usrreqs
->pru_peeraddr(sock
, &sa
);
308 if (peernamelen
> sa
->sa_len
) peernamelen
= sa
->sa_len
;
309 memcpy(peername
, sa
, peernamelen
);
311 if (sa
) FREE(sa
, M_SONAME
);
312 socket_unlock(sock
, 1);
319 struct sockaddr
*sockname
,
323 struct sockaddr
*sa
= NULL
;
325 if (sock
== NULL
|| sockname
== NULL
|| socknamelen
< 0) return EINVAL
;
326 socket_lock(sock
, 1);
327 error
= sock
->so_proto
->pr_usrreqs
->pru_sockaddr(sock
, &sa
);
330 if (socknamelen
> sa
->sa_len
) socknamelen
= sa
->sa_len
;
331 memcpy(sockname
, sa
, socknamelen
);
333 if (sa
) FREE(sa
, M_SONAME
);
334 socket_unlock(sock
, 1);
349 if (sock
== NULL
|| optval
== NULL
|| optlen
== NULL
) return EINVAL
;
350 sopt
.sopt_dir
= SOPT_GET
;
351 sopt
.sopt_level
= level
;
352 sopt
.sopt_name
= optname
;
353 sopt
.sopt_val
= CAST_USER_ADDR_T(optval
);
354 sopt
.sopt_valsize
= *optlen
;
356 error
= sogetopt(sock
, &sopt
); /* will lock socket */
357 if (error
== 0) *optlen
= sopt
.sopt_valsize
;
364 unsigned long request
,
367 return soioctl(sock
, request
, argp
, NULL
); /* will lock socket */
380 if (sock
== NULL
|| optval
== NULL
) return EINVAL
;
381 sopt
.sopt_dir
= SOPT_SET
;
382 sopt
.sopt_level
= level
;
383 sopt
.sopt_name
= optname
;
384 sopt
.sopt_val
= CAST_USER_ADDR_T(optval
);
385 sopt
.sopt_valsize
= optlen
;
387 return sosetopt(sock
, &sopt
); /* will lock socket */
395 if (sock
== NULL
) return EINVAL
;
396 return solisten(sock
, backlog
); /* will lock socket */
400 sock_receive_internal(
408 struct mbuf
*control
= NULL
;
411 struct sockaddr
*fromsa
;
412 char uio_buf
[ UIO_SIZEOF((msg
!= NULL
) ? msg
->msg_iovlen
: 0) ];
414 if (sock
== NULL
) return EINVAL
;
416 auio
= uio_createwithbuffer(((msg
!= NULL
) ? msg
->msg_iovlen
: 0),
417 0, UIO_SYSSPACE
, UIO_READ
,
418 &uio_buf
[0], sizeof(uio_buf
));
419 if (msg
&& data
== NULL
) {
421 struct iovec_32
*tempp
= (struct iovec_32
*) msg
->msg_iov
;
423 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
424 uio_addiov(auio
, CAST_USER_ADDR_T((tempp
+ i
)->iov_base
), (tempp
+ i
)->iov_len
);
426 if (uio_resid(auio
) < 0) return EINVAL
;
429 uio_setresid(auio
, (uio_resid(auio
) + *recvdlen
));
431 length
= uio_resid(auio
);
436 if (msg
&& msg
->msg_control
) {
437 if ((size_t)msg
->msg_controllen
< sizeof(struct cmsghdr
)) return EINVAL
;
438 if ((size_t)msg
->msg_controllen
> MLEN
) return EINVAL
;
439 control
= m_get(M_NOWAIT
, MT_CONTROL
);
440 if (control
== NULL
) return ENOMEM
;
441 memcpy(mtod(control
, caddr_t
), msg
->msg_control
, msg
->msg_controllen
);
442 control
->m_len
= msg
->msg_controllen
;
445 /* let pru_soreceive handle the socket locking */
446 error
= sock
->so_proto
->pr_usrreqs
->pru_soreceive(sock
, &fromsa
, auio
,
447 data
, control
? &control
: NULL
, &flags
);
448 if (error
) goto cleanup
;
451 *recvdlen
= length
- uio_resid(auio
);
453 msg
->msg_flags
= flags
;
458 salen
= msg
->msg_namelen
;
459 if (msg
->msg_namelen
> 0 && fromsa
!= 0)
461 salen
= MIN(salen
, fromsa
->sa_len
);
462 memcpy(msg
->msg_name
, fromsa
,
463 msg
->msg_namelen
> fromsa
->sa_len
? fromsa
->sa_len
: msg
->msg_namelen
);
467 if (msg
->msg_control
)
469 struct mbuf
* m
= control
;
470 u_char
* ctlbuf
= msg
->msg_control
;
471 int clen
= msg
->msg_controllen
;
472 msg
->msg_controllen
= 0;
474 while (m
&& clen
> 0)
477 if (clen
>= m
->m_len
)
483 msg
->msg_flags
|= MSG_CTRUNC
;
486 memcpy(ctlbuf
, mtod(m
, caddr_t
), tocopy
);
491 msg
->msg_controllen
= (u_int32_t
)ctlbuf
- (u_int32_t
)msg
->msg_control
;
496 if (control
) m_freem(control
);
497 if (fromsa
) FREE(fromsa
, M_SONAME
);
509 (msg
->msg_iovlen
< 1) ||
510 (msg
->msg_iov
[0].iov_len
== 0) ||
511 (msg
->msg_iov
[0].iov_base
== NULL
))
513 return sock_receive_internal(sock
, msg
, NULL
, flags
, recvdlen
);
524 if (data
== NULL
|| recvlen
== 0 || *recvlen
<= 0 || (msg
&&
525 (msg
->msg_iov
!= NULL
|| msg
->msg_iovlen
!= 0)))
527 return sock_receive_internal(sock
, msg
, data
, flags
, recvlen
);
533 const struct msghdr
*msg
,
539 struct mbuf
*control
= NULL
;
542 char uio_buf
[ UIO_SIZEOF((msg
!= NULL
? msg
->msg_iovlen
: 1)) ];
549 if (data
== 0 && msg
!= NULL
) {
550 struct iovec_32
*tempp
= (struct iovec_32
*) msg
->msg_iov
;
552 auio
= uio_createwithbuffer(msg
->msg_iovlen
, 0, UIO_SYSSPACE
, UIO_WRITE
,
553 &uio_buf
[0], sizeof(uio_buf
));
558 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
559 uio_addiov(auio
, CAST_USER_ADDR_T((tempp
+ i
)->iov_base
), (tempp
+ i
)->iov_len
);
562 if (uio_resid(auio
) < 0) {
573 datalen
= uio_resid(auio
);
575 datalen
= data
->m_pkthdr
.len
;
577 if (msg
&& msg
->msg_control
)
579 if ((size_t)msg
->msg_controllen
< sizeof(struct cmsghdr
)) return EINVAL
;
580 if ((size_t)msg
->msg_controllen
> MLEN
) return EINVAL
;
581 control
= m_get(M_NOWAIT
, MT_CONTROL
);
582 if (control
== NULL
) {
586 memcpy(mtod(control
, caddr_t
), msg
->msg_control
, msg
->msg_controllen
);
587 control
->m_len
= msg
->msg_controllen
;
590 error
= sock
->so_proto
->pr_usrreqs
->pru_sosend(sock
, msg
? (struct sockaddr
*)msg
->msg_name
: 0,
591 auio
, data
, control
, flags
);
592 if (error
== 0 && sentlen
) {
594 *sentlen
= datalen
- uio_resid(auio
);
602 * In cases where we detect an error before returning, we need to
603 * free the mbuf chain if there is one. sosend (and pru_sosend) will
604 * free the mbuf chain if they encounter an error.
619 const struct msghdr
*msg
,
623 if (msg
== NULL
|| msg
->msg_iov
== NULL
|| msg
->msg_iovlen
< 1)
625 return sock_send_internal(sock
, msg
, NULL
, flags
, sentlen
);
631 const struct msghdr
*msg
,
636 if (data
== NULL
|| (msg
&&
637 (msg
->msg_iov
!= NULL
|| msg
->msg_iovlen
!= 0))) {
642 return sock_send_internal(sock
, msg
, data
, flags
, sentlen
);
650 if (sock
== NULL
) return EINVAL
;
651 return soshutdown(sock
, how
);
654 typedef void (*so_upcall
)(struct socket
*sock
, void* arg
, int waitf
);
661 sock_upcall callback
,
666 if (new_so
== NULL
) return EINVAL
;
667 /* socreate will create an initial so_count */
668 error
= socreate(domain
, new_so
, type
, protocol
);
669 if (error
== 0 && callback
)
671 (*new_so
)->so_rcv
.sb_flags
|= SB_UPCALL
;
672 (*new_so
)->so_upcall
= (so_upcall
)callback
;
673 (*new_so
)->so_upcallarg
= context
;
682 if (sock
== NULL
) return;
686 /* Do we want this to be APPLE_PRIVATE API?: YES (LD 12/23/04)*/
691 if (sock
== NULL
) return;
692 socket_lock(sock
, 1);
693 sock
->so_retaincnt
++;
694 sock
->so_usecount
++; /* add extra reference for holding the socket */
695 socket_unlock(sock
, 1);
698 /* Do we want this to be APPLE_PRIVATE API? */
703 if (sock
== NULL
) return;
704 socket_lock(sock
, 1);
705 sock
->so_retaincnt
--;
706 if (sock
->so_retaincnt
< 0)
707 panic("sock_release: negative retain count for sock=%x cnt=%x\n",
708 sock
, sock
->so_retaincnt
);
709 if ((sock
->so_retaincnt
== 0) && (sock
->so_usecount
== 2))
710 soclose_locked(sock
); /* close socket only if the FD is not holding it */
712 sock
->so_usecount
--; /* remove extra reference holding the socket */
713 socket_unlock(sock
, 1);
721 if (sock
== NULL
) return EINVAL
;
722 socket_lock(sock
, 1);
725 sock
->so_state
|= SS_PRIV
;
729 sock
->so_state
&= ~SS_PRIV
;
731 socket_unlock(sock
, 1);
740 socket_lock(sock
, 1);
741 retval
= (sock
->so_state
& SS_ISCONNECTED
) != 0;
742 socket_unlock(sock
, 1);
751 socket_lock(sock
, 1);
752 retval
= (sock
->so_state
& SS_NBIO
) != 0;
753 socket_unlock(sock
, 1);
764 socket_lock(sock
, 1);
766 *outDomain
= sock
->so_proto
->pr_domain
->dom_family
;
768 *outType
= sock
->so_type
;
770 *outProtocol
= sock
->so_proto
->pr_protocol
;
771 socket_unlock(sock
, 1);