]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kpi_socket.c
357e1f40436a4dda94e33de060be4ba3c2266085
2 * Copyright (c) 2003-2004 Apple Computer, 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/kernel.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/socketvar.h>
34 #include <sys/param.h>
36 #include <sys/errno.h>
37 #include <sys/malloc.h>
38 #include <sys/protosw.h>
39 #include <sys/domain.h>
41 #include <sys/fcntl.h>
42 #include <sys/filio.h>
43 #include <sys/uio_internal.h>
44 #include <kern/lock.h>
46 extern void *memcpy(void *, const void *, size_t);
47 extern int soclose_locked(struct socket
*so
);
49 errno_t
sock_send_internal(
51 const struct msghdr
*msg
,
61 struct sockaddr
*from
,
69 struct socket
*new_so
;
70 lck_mtx_t
*mutex_held
;
74 if (sock
== NULL
|| new_sock
== NULL
) return EINVAL
;
76 if ((sock
->so_options
& SO_ACCEPTCONN
) == 0) {
77 socket_unlock(sock
, 1);
80 if ((flags
& ~(MSG_DONTWAIT
)) != 0) {
81 socket_unlock(sock
, 1);
84 if (((flags
& MSG_DONTWAIT
) != 0 || (sock
->so_state
& SS_NBIO
) != 0) &&
85 sock
->so_comp
.tqh_first
== NULL
) {
86 socket_unlock(sock
, 1);
90 if (sock
->so_proto
->pr_getlock
!= NULL
) {
91 mutex_held
= (*sock
->so_proto
->pr_getlock
)(sock
, 0);
95 mutex_held
= sock
->so_proto
->pr_domain
->dom_mtx
;
99 while (TAILQ_EMPTY(&sock
->so_comp
) && sock
->so_error
== 0) {
100 if (sock
->so_state
& SS_CANTRCVMORE
) {
101 sock
->so_error
= ECONNABORTED
;
104 error
= msleep((caddr_t
)&sock
->so_timeo
, mutex_held
, PSOCK
| PCATCH
, "sock_accept", 0);
106 socket_unlock(sock
, 1);
110 if (sock
->so_error
) {
111 error
= sock
->so_error
;
113 socket_unlock(sock
, 1);
117 new_so
= TAILQ_FIRST(&sock
->so_comp
);
118 TAILQ_REMOVE(&sock
->so_comp
, new_so
, so_list
);
120 socket_unlock(sock
, 1); /* release the head */
123 lck_mtx_assert(new_so
->so_proto
->pr_getlock(new_so
, 0),
124 LCK_MTX_ASSERT_NOTOWNED
);
125 socket_lock(new_so
, 1);
128 new_so
->so_state
&= ~SS_COMP
;
129 new_so
->so_head
= NULL
;
130 soacceptlock(new_so
, &sa
, 0);
133 new_so
->so_upcall
= callback
;
134 new_so
->so_upcallarg
= cookie
;
135 new_so
->so_rcv
.sb_flags
|= SB_UPCALL
;
140 if (fromlen
> sa
->sa_len
) fromlen
= sa
->sa_len
;
141 memcpy(from
, sa
, fromlen
);
143 if (sa
) FREE(sa
, M_SONAME
);
146 socket_unlock(new_so
, 1);
153 const struct sockaddr
*to
)
155 if (sock
== NULL
|| to
== NULL
) return EINVAL
;
157 return sobind(sock
, (struct sockaddr
*)to
);
163 const struct sockaddr
*to
,
167 lck_mtx_t
*mutex_held
;
169 if (sock
== NULL
|| to
== NULL
) return EINVAL
;
171 socket_lock(sock
, 1);
173 if ((sock
->so_state
& SS_ISCONNECTING
) &&
174 ((sock
->so_state
& SS_NBIO
) != 0 ||
175 (flags
& MSG_DONTWAIT
) != 0)) {
176 socket_unlock(sock
, 1);
179 error
= soconnectlock(sock
, (struct sockaddr
*)to
, 0);
181 if ((sock
->so_state
& SS_ISCONNECTING
) &&
182 ((sock
->so_state
& SS_NBIO
) != 0 || (flags
& MSG_DONTWAIT
) != 0)) {
183 socket_unlock(sock
, 1);
187 if (sock
->so_proto
->pr_getlock
!= NULL
)
188 mutex_held
= (*sock
->so_proto
->pr_getlock
)(sock
, 0);
190 mutex_held
= sock
->so_proto
->pr_domain
->dom_mtx
;
192 while ((sock
->so_state
& SS_ISCONNECTING
) && sock
->so_error
== 0) {
193 error
= msleep((caddr_t
)&sock
->so_timeo
, mutex_held
, PSOCK
| PCATCH
,
200 error
= sock
->so_error
;
205 sock
->so_state
&= ~SS_ISCONNECTING
;
207 socket_unlock(sock
, 1);
214 const struct timeval
*tv
)
216 lck_mtx_t
* mutex_held
;
220 socket_lock(sock
, 1);
222 // Check if we're already connected or if we've already errored out
223 if ((sock
->so_state
& SS_ISCONNECTING
) == 0 || sock
->so_error
) {
224 if (sock
->so_error
) {
225 retval
= sock
->so_error
;
229 if ((sock
->so_state
& SS_ISCONNECTED
) != 0)
237 // copied translation from timeval to hertz from SO_RCVTIMEO handling
238 if (tv
->tv_sec
< 0 || tv
->tv_sec
> SHRT_MAX
/ hz
||
239 tv
->tv_usec
< 0 || tv
->tv_usec
>= 1000000) {
244 ts
.tv_sec
= tv
->tv_sec
;
245 ts
.tv_nsec
= (tv
->tv_usec
* NSEC_PER_USEC
);
246 if ( (ts
.tv_sec
+ (ts
.tv_nsec
/NSEC_PER_SEC
))/100 > SHRT_MAX
) {
251 if (sock
->so_proto
->pr_getlock
!= NULL
)
252 mutex_held
= (*sock
->so_proto
->pr_getlock
)(sock
, 0);
254 mutex_held
= sock
->so_proto
->pr_domain
->dom_mtx
;
256 msleep((caddr_t
)&sock
->so_timeo
, mutex_held
, PSOCK
, "sock_connectwait", &ts
);
258 // Check if we're still waiting to connect
259 if ((sock
->so_state
& SS_ISCONNECTING
) && sock
->so_error
== 0) {
260 retval
= EINPROGRESS
;
264 if (sock
->so_error
) {
265 retval
= sock
->so_error
;
270 socket_unlock(sock
, 1);
279 socket_lock(sock
, 1);
282 sock
->so_rcv
.sb_flags
|= SB_NOINTR
; // This isn't safe
283 sock
->so_snd
.sb_flags
|= SB_NOINTR
; // This isn't safe
286 sock
->so_rcv
.sb_flags
&= ~SB_NOINTR
; // This isn't safe
287 sock
->so_snd
.sb_flags
&= ~SB_NOINTR
; // This isn't safe
290 socket_unlock(sock
, 1);
298 struct sockaddr
*peername
,
302 struct sockaddr
*sa
= NULL
;
304 if (sock
== NULL
|| peername
== NULL
|| peernamelen
< 0) return EINVAL
;
305 socket_lock(sock
, 1);
306 if ((sock
->so_state
& (SS_ISCONNECTED
|SS_ISCONFIRMING
)) == 0) {
307 socket_unlock(sock
, 1);
310 error
= sock
->so_proto
->pr_usrreqs
->pru_peeraddr(sock
, &sa
);
313 if (peernamelen
> sa
->sa_len
) peernamelen
= sa
->sa_len
;
314 memcpy(peername
, sa
, peernamelen
);
316 if (sa
) FREE(sa
, M_SONAME
);
317 socket_unlock(sock
, 1);
324 struct sockaddr
*sockname
,
328 struct sockaddr
*sa
= NULL
;
330 if (sock
== NULL
|| sockname
== NULL
|| socknamelen
< 0) return EINVAL
;
331 socket_lock(sock
, 1);
332 error
= sock
->so_proto
->pr_usrreqs
->pru_sockaddr(sock
, &sa
);
335 if (socknamelen
> sa
->sa_len
) socknamelen
= sa
->sa_len
;
336 memcpy(sockname
, sa
, socknamelen
);
338 if (sa
) FREE(sa
, M_SONAME
);
339 socket_unlock(sock
, 1);
354 if (sock
== NULL
|| optval
== NULL
|| optlen
== NULL
) return EINVAL
;
355 sopt
.sopt_dir
= SOPT_GET
;
356 sopt
.sopt_level
= level
;
357 sopt
.sopt_name
= optname
;
358 sopt
.sopt_val
= CAST_USER_ADDR_T(optval
);
359 sopt
.sopt_valsize
= *optlen
;
361 error
= sogetopt(sock
, &sopt
); /* will lock socket */
362 if (error
== 0) *optlen
= sopt
.sopt_valsize
;
369 unsigned long request
,
372 return soioctl(sock
, request
, argp
, NULL
); /* will lock socket */
385 if (sock
== NULL
|| optval
== NULL
) return EINVAL
;
386 sopt
.sopt_dir
= SOPT_SET
;
387 sopt
.sopt_level
= level
;
388 sopt
.sopt_name
= optname
;
389 sopt
.sopt_val
= CAST_USER_ADDR_T(optval
);
390 sopt
.sopt_valsize
= optlen
;
392 return sosetopt(sock
, &sopt
); /* will lock socket */
400 if (sock
== NULL
) return EINVAL
;
401 return solisten(sock
, backlog
); /* will lock socket */
405 sock_receive_internal(
413 struct mbuf
*control
= NULL
;
416 struct sockaddr
*fromsa
;
417 char uio_buf
[ UIO_SIZEOF((msg
!= NULL
) ? msg
->msg_iovlen
: 0) ];
419 if (sock
== NULL
) return EINVAL
;
421 auio
= uio_createwithbuffer(((msg
!= NULL
) ? msg
->msg_iovlen
: 0),
422 0, UIO_SYSSPACE
, UIO_READ
,
423 &uio_buf
[0], sizeof(uio_buf
));
424 if (msg
&& data
== NULL
) {
426 struct iovec_32
*tempp
= (struct iovec_32
*) msg
->msg_iov
;
428 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
429 uio_addiov(auio
, CAST_USER_ADDR_T((tempp
+ i
)->iov_base
), (tempp
+ i
)->iov_len
);
431 if (uio_resid(auio
) < 0) return EINVAL
;
434 uio_setresid(auio
, (uio_resid(auio
) + *recvdlen
));
436 length
= uio_resid(auio
);
441 if (msg
&& msg
->msg_control
) {
442 if ((size_t)msg
->msg_controllen
< sizeof(struct cmsghdr
)) return EINVAL
;
443 if ((size_t)msg
->msg_controllen
> MLEN
) return EINVAL
;
444 control
= m_get(M_NOWAIT
, MT_CONTROL
);
445 if (control
== NULL
) return ENOMEM
;
446 memcpy(mtod(control
, caddr_t
), msg
->msg_control
, msg
->msg_controllen
);
447 control
->m_len
= msg
->msg_controllen
;
450 /* let pru_soreceive handle the socket locking */
451 error
= sock
->so_proto
->pr_usrreqs
->pru_soreceive(sock
, &fromsa
, auio
,
452 data
, control
? &control
: NULL
, &flags
);
453 if (error
) goto cleanup
;
456 *recvdlen
= length
- uio_resid(auio
);
458 msg
->msg_flags
= flags
;
463 salen
= msg
->msg_namelen
;
464 if (msg
->msg_namelen
> 0 && fromsa
!= 0)
466 salen
= MIN(salen
, fromsa
->sa_len
);
467 memcpy(msg
->msg_name
, fromsa
,
468 msg
->msg_namelen
> fromsa
->sa_len
? fromsa
->sa_len
: msg
->msg_namelen
);
472 if (msg
->msg_control
)
474 struct mbuf
* m
= control
;
475 u_char
* ctlbuf
= msg
->msg_control
;
476 int clen
= msg
->msg_controllen
;
477 msg
->msg_controllen
= 0;
479 while (m
&& clen
> 0)
482 if (clen
>= m
->m_len
)
488 msg
->msg_flags
|= MSG_CTRUNC
;
491 memcpy(ctlbuf
, mtod(m
, caddr_t
), tocopy
);
496 msg
->msg_controllen
= (u_int32_t
)ctlbuf
- (u_int32_t
)msg
->msg_control
;
501 if (control
) m_freem(control
);
502 if (fromsa
) FREE(fromsa
, M_SONAME
);
514 (msg
->msg_iovlen
< 1) ||
515 (msg
->msg_iov
[0].iov_len
== 0) ||
516 (msg
->msg_iov
[0].iov_base
== NULL
))
518 return sock_receive_internal(sock
, msg
, NULL
, flags
, recvdlen
);
529 if (data
== NULL
|| recvlen
== 0 || *recvlen
<= 0 || (msg
&&
530 (msg
->msg_iov
!= NULL
|| msg
->msg_iovlen
!= 0)))
532 return sock_receive_internal(sock
, msg
, data
, flags
, recvlen
);
538 const struct msghdr
*msg
,
544 struct mbuf
*control
= NULL
;
547 char uio_buf
[ UIO_SIZEOF((msg
!= NULL
? msg
->msg_iovlen
: 1)) ];
554 if (data
== 0 && msg
!= NULL
) {
555 struct iovec_32
*tempp
= (struct iovec_32
*) msg
->msg_iov
;
557 auio
= uio_createwithbuffer(msg
->msg_iovlen
, 0, UIO_SYSSPACE
, UIO_WRITE
,
558 &uio_buf
[0], sizeof(uio_buf
));
563 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
564 uio_addiov(auio
, CAST_USER_ADDR_T((tempp
+ i
)->iov_base
), (tempp
+ i
)->iov_len
);
567 if (uio_resid(auio
) < 0) {
578 datalen
= uio_resid(auio
);
580 datalen
= data
->m_pkthdr
.len
;
582 if (msg
&& msg
->msg_control
)
584 if ((size_t)msg
->msg_controllen
< sizeof(struct cmsghdr
)) return EINVAL
;
585 if ((size_t)msg
->msg_controllen
> MLEN
) return EINVAL
;
586 control
= m_get(M_NOWAIT
, MT_CONTROL
);
587 if (control
== NULL
) {
591 memcpy(mtod(control
, caddr_t
), msg
->msg_control
, msg
->msg_controllen
);
592 control
->m_len
= msg
->msg_controllen
;
595 error
= sock
->so_proto
->pr_usrreqs
->pru_sosend(sock
, msg
!= NULL
?
596 (struct sockaddr
*)msg
->msg_name
: NULL
, auio
, data
, control
, flags
);
599 * Residual data is possible in the case of IO vectors but not
600 * in the mbuf case since the latter is treated as atomic send.
601 * If pru_sosend() consumed a portion of the iovecs data and
602 * the error returned is transient, treat it as success; this
603 * is consistent with sendit() behavior.
605 if (auio
!= NULL
&& uio_resid(auio
) != datalen
&&
606 (error
== ERESTART
|| error
== EINTR
|| error
== EWOULDBLOCK
))
609 if (error
== 0 && sentlen
!= NULL
) {
611 *sentlen
= datalen
- uio_resid(auio
);
619 * In cases where we detect an error before returning, we need to
620 * free the mbuf chain if there is one. sosend (and pru_sosend) will
621 * free the mbuf chain if they encounter an error.
636 const struct msghdr
*msg
,
640 if (msg
== NULL
|| msg
->msg_iov
== NULL
|| msg
->msg_iovlen
< 1)
642 return sock_send_internal(sock
, msg
, NULL
, flags
, sentlen
);
648 const struct msghdr
*msg
,
653 if (data
== NULL
|| (msg
&&
654 (msg
->msg_iov
!= NULL
|| msg
->msg_iovlen
!= 0))) {
659 return sock_send_internal(sock
, msg
, data
, flags
, sentlen
);
667 if (sock
== NULL
) return EINVAL
;
668 return soshutdown(sock
, how
);
671 typedef void (*so_upcall
)(struct socket
*sock
, void* arg
, int waitf
);
678 sock_upcall callback
,
683 if (new_so
== NULL
) return EINVAL
;
684 /* socreate will create an initial so_count */
685 error
= socreate(domain
, new_so
, type
, protocol
);
686 if (error
== 0 && callback
)
688 (*new_so
)->so_rcv
.sb_flags
|= SB_UPCALL
;
689 (*new_so
)->so_upcall
= (so_upcall
)callback
;
690 (*new_so
)->so_upcallarg
= context
;
699 if (sock
== NULL
) return;
703 /* Do we want this to be APPLE_PRIVATE API?: YES (LD 12/23/04)*/
708 if (sock
== NULL
) return;
709 socket_lock(sock
, 1);
710 sock
->so_retaincnt
++;
711 sock
->so_usecount
++; /* add extra reference for holding the socket */
712 socket_unlock(sock
, 1);
715 /* Do we want this to be APPLE_PRIVATE API? */
720 if (sock
== NULL
) return;
721 socket_lock(sock
, 1);
722 sock
->so_retaincnt
--;
723 if (sock
->so_retaincnt
< 0)
724 panic("sock_release: negative retain count for sock=%x cnt=%x\n",
725 sock
, sock
->so_retaincnt
);
726 if ((sock
->so_retaincnt
== 0) && (sock
->so_usecount
== 2))
727 soclose_locked(sock
); /* close socket only if the FD is not holding it */
729 sock
->so_usecount
--; /* remove extra reference holding the socket */
730 socket_unlock(sock
, 1);
738 if (sock
== NULL
) return EINVAL
;
739 socket_lock(sock
, 1);
742 sock
->so_state
|= SS_PRIV
;
746 sock
->so_state
&= ~SS_PRIV
;
748 socket_unlock(sock
, 1);
757 socket_lock(sock
, 1);
758 retval
= (sock
->so_state
& SS_ISCONNECTED
) != 0;
759 socket_unlock(sock
, 1);
768 socket_lock(sock
, 1);
769 retval
= (sock
->so_state
& SS_NBIO
) != 0;
770 socket_unlock(sock
, 1);
781 socket_lock(sock
, 1);
783 *outDomain
= sock
->so_proto
->pr_domain
->dom_family
;
785 *outType
= sock
->so_type
;
787 *outProtocol
= sock
->so_proto
->pr_protocol
;
788 socket_unlock(sock
, 1);