4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
29 #include <sys/stropts.h>
30 #include <sys/socket.h>
31 #include <sys/socketvar.h>
32 #include <sys/sysmacros.h>
33 #include <sys/filio.h> /* FIO* ioctls */
34 #include <sys/sockio.h> /* SIOC* ioctls */
35 #include <sys/poll_impl.h>
36 #include <sys/cmn_err.h>
37 #include <sys/ksocket.h>
38 #include <io/ksocket/ksocket_impl.h>
39 #include <../../../../kernel/fs/sockfs/sockcommon.h>
41 #define SOCKETMOD_TCP "tcp"
42 #define SOCKETMOD_UDP "udp"
46 * Mostly a wrapper around the private socket_* functions.
49 ksocket_socket(ksocket_t
*ksp
, int domain
, int type
, int protocol
, int flags
,
56 /* All Solaris components should pass a cred for this operation. */
59 ASSERT(flags
== KSOCKET_SLEEP
|| flags
== KSOCKET_NOSLEEP
);
60 so
= socket_create(domain
, type
, protocol
, NULL
, NULL
, flags
,
63 if (error
== EAFNOSUPPORT
) {
67 * Could be that root file system is not loaded or
68 * soconfig has not run yet.
70 if (type
== SOCK_STREAM
&& (domain
== AF_INET
||
71 domain
== AF_INET6
) && (protocol
== 0 ||
72 protocol
== IPPROTO_TCP
)) {
74 } else if (type
== SOCK_DGRAM
&& (domain
== AF_INET
||
75 domain
== AF_INET6
) && (protocol
== 0 ||
76 protocol
== IPPROTO_UDP
)) {
79 return (EAFNOSUPPORT
);
82 so
= socket_create(domain
, type
, protocol
, NULL
,
83 mod
, flags
, cr
, &error
);
91 so
->so_mode
|= SM_KERNEL
;
98 ksocket_bind(ksocket_t ks
, struct sockaddr
*addr
, socklen_t addrlen
,
103 /* All Solaris components should pass a cred for this operation. */
106 if (!KSOCKET_VALID(ks
))
109 error
= socket_bind(KSTOSO(ks
), addr
, addrlen
, 0, cr
);
115 ksocket_listen(ksocket_t ks
, int backlog
, struct cred
*cr
)
117 /* All Solaris components should pass a cred for this operation. */
120 if (!KSOCKET_VALID(ks
))
123 return (socket_listen(KSTOSO(ks
), backlog
, cr
));
127 ksocket_accept(ksocket_t ks
, struct sockaddr
*addr
,
128 socklen_t
*addrlenp
, ksocket_t
*nks
, struct cred
*cr
)
131 struct sonode
*nso
= NULL
;
133 /* All Solaris components should pass a cred for this operation. */
138 if (!KSOCKET_VALID(ks
))
141 if (addr
!= NULL
&& addrlenp
== NULL
)
144 error
= socket_accept(KSTOSO(ks
), KSOCKET_FMODE(ks
), cr
, &nso
);
150 nso
->so_mode
|= SM_KERNEL
;
152 if (addr
!= NULL
&& addrlenp
!= NULL
) {
153 error
= socket_getpeername(nso
, addr
, addrlenp
, B_TRUE
, cr
);
155 (void) socket_close(nso
, 0, cr
);
157 return ((error
== ENOTCONN
) ? ECONNABORTED
: error
);
167 ksocket_connect(ksocket_t ks
, struct sockaddr
*addr
, socklen_t addrlen
,
170 /* All Solaris components should pass a cred for this operation. */
173 if (!KSOCKET_VALID(ks
))
176 return (socket_connect(KSTOSO(ks
), addr
, addrlen
,
177 KSOCKET_FMODE(ks
), 0, cr
));
181 ksocket_send(ksocket_t ks
, void *msg
, size_t msglen
, int flags
,
182 size_t *sent
, struct cred
*cr
)
185 struct msghdr msghdr
;
189 /* All Solaris components should pass a cred for this operation. */
192 if (!KSOCKET_VALID(ks
)) {
199 iov
.iov_len
= msglen
;
201 bzero(&auio
, sizeof (struct uio
));
202 auio
.uio_loffset
= 0;
205 auio
.uio_resid
= msglen
;
206 if (flags
& MSG_USERSPACE
)
207 auio
.uio_segflg
= UIO_USERSPACE
;
209 auio
.uio_segflg
= UIO_SYSSPACE
;
210 auio
.uio_extflg
= UIO_COPY_DEFAULT
;
212 auio
.uio_fmode
= KSOCKET_FMODE(ks
);
214 msghdr
.msg_name
= NULL
;
215 msghdr
.msg_namelen
= 0;
216 msghdr
.msg_control
= NULL
;
217 msghdr
.msg_controllen
= 0;
218 msghdr
.msg_flags
= flags
| MSG_EOR
;
220 error
= socket_sendmsg(KSTOSO(ks
), &msghdr
, &auio
, cr
);
228 *sent
= msglen
- auio
.uio_resid
;
233 ksocket_sendto(ksocket_t ks
, void *msg
, size_t msglen
, int flags
,
234 struct sockaddr
*name
, socklen_t namelen
, size_t *sent
, struct cred
*cr
)
237 struct msghdr msghdr
;
241 /* All Solaris components should pass a cred for this operation. */
244 if (!KSOCKET_VALID(ks
)) {
251 iov
.iov_len
= msglen
;
253 bzero(&auio
, sizeof (struct uio
));
254 auio
.uio_loffset
= 0;
257 auio
.uio_resid
= msglen
;
258 if (flags
& MSG_USERSPACE
)
259 auio
.uio_segflg
= UIO_USERSPACE
;
261 auio
.uio_segflg
= UIO_SYSSPACE
;
262 auio
.uio_extflg
= UIO_COPY_DEFAULT
;
264 auio
.uio_fmode
= KSOCKET_FMODE(ks
);
266 msghdr
.msg_iov
= &iov
;
267 msghdr
.msg_iovlen
= 1;
268 msghdr
.msg_name
= (char *)name
;
269 msghdr
.msg_namelen
= namelen
;
270 msghdr
.msg_control
= NULL
;
271 msghdr
.msg_controllen
= 0;
272 msghdr
.msg_flags
= flags
| MSG_EOR
;
274 error
= socket_sendmsg(KSTOSO(ks
), &msghdr
, &auio
, cr
);
281 *sent
= msglen
- auio
.uio_resid
;
286 ksocket_sendmsg(ksocket_t ks
, struct msghdr
*msg
, int flags
,
287 size_t *sent
, struct cred
*cr
)
294 /* All Solaris components should pass a cred for this operation. */
297 if (!KSOCKET_VALID(ks
)) {
303 bzero(&auio
, sizeof (struct uio
));
304 auio
.uio_loffset
= 0;
305 auio
.uio_iov
= msg
->msg_iov
;
306 auio
.uio_iovcnt
= msg
->msg_iovlen
;
307 if (flags
& MSG_USERSPACE
)
308 auio
.uio_segflg
= UIO_USERSPACE
;
310 auio
.uio_segflg
= UIO_SYSSPACE
;
311 auio
.uio_extflg
= UIO_COPY_DEFAULT
;
313 auio
.uio_fmode
= KSOCKET_FMODE(ks
);
315 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
317 iovlen
= (msg
->msg_iov
)[i
].iov_len
;
319 if (len
< 0 || iovlen
< 0)
322 auio
.uio_resid
= len
;
324 msg
->msg_flags
= flags
| MSG_EOR
;
326 error
= socket_sendmsg(KSTOSO(ks
), msg
, &auio
, cr
);
334 *sent
= len
- auio
.uio_resid
;
340 ksocket_recv(ksocket_t ks
, void *msg
, size_t msglen
, int flags
,
341 size_t *recv
, struct cred
*cr
)
344 struct msghdr msghdr
;
348 /* All Solaris components should pass a cred for this operation. */
351 if (!KSOCKET_VALID(ks
)) {
358 iov
.iov_len
= msglen
;
360 bzero(&auio
, sizeof (struct uio
));
361 auio
.uio_loffset
= 0;
364 auio
.uio_resid
= msglen
;
365 if (flags
& MSG_USERSPACE
)
366 auio
.uio_segflg
= UIO_USERSPACE
;
368 auio
.uio_segflg
= UIO_SYSSPACE
;
369 auio
.uio_extflg
= UIO_COPY_DEFAULT
;
371 auio
.uio_fmode
= KSOCKET_FMODE(ks
);
373 msghdr
.msg_name
= NULL
;
374 msghdr
.msg_namelen
= 0;
375 msghdr
.msg_control
= NULL
;
376 msghdr
.msg_controllen
= 0;
377 msghdr
.msg_flags
= flags
& (MSG_OOB
| MSG_PEEK
| MSG_WAITALL
|
378 MSG_DONTWAIT
| MSG_USERSPACE
);
380 error
= socket_recvmsg(KSTOSO(ks
), &msghdr
, &auio
, cr
);
388 *recv
= msglen
- auio
.uio_resid
;
393 ksocket_recvfrom(ksocket_t ks
, void *msg
, size_t msglen
, int flags
,
394 struct sockaddr
*name
, socklen_t
*namelen
, size_t *recv
, struct cred
*cr
)
397 struct msghdr msghdr
;
401 /* All Solaris components should pass a cred for this operation. */
404 if (!KSOCKET_VALID(ks
)) {
411 iov
.iov_len
= msglen
;
413 bzero(&auio
, sizeof (struct uio
));
414 auio
.uio_loffset
= 0;
417 auio
.uio_resid
= msglen
;
418 if (flags
& MSG_USERSPACE
)
419 auio
.uio_segflg
= UIO_USERSPACE
;
421 auio
.uio_segflg
= UIO_SYSSPACE
;
422 auio
.uio_extflg
= UIO_COPY_DEFAULT
;
424 auio
.uio_fmode
= KSOCKET_FMODE(ks
);
426 msghdr
.msg_name
= (char *)name
;
427 msghdr
.msg_namelen
= *namelen
;
428 msghdr
.msg_control
= NULL
;
429 msghdr
.msg_controllen
= 0;
430 msghdr
.msg_flags
= flags
& (MSG_OOB
| MSG_PEEK
| MSG_WAITALL
|
431 MSG_DONTWAIT
| MSG_USERSPACE
);
433 error
= socket_recvmsg(KSTOSO(ks
), &msghdr
, &auio
, cr
);
440 *recv
= msglen
- auio
.uio_resid
;
442 bcopy(msghdr
.msg_name
, name
, msghdr
.msg_namelen
);
443 bcopy(&msghdr
.msg_namelen
, namelen
, sizeof (msghdr
.msg_namelen
));
448 ksocket_recvmsg(ksocket_t ks
, struct msghdr
*msg
, int flags
, size_t *recv
,
456 /* All Solaris components should pass a cred for this operation. */
459 if (!KSOCKET_VALID(ks
)) {
465 bzero(&auio
, sizeof (struct uio
));
466 auio
.uio_loffset
= 0;
467 auio
.uio_iov
= msg
->msg_iov
;
468 auio
.uio_iovcnt
= msg
->msg_iovlen
;
469 if (msg
->msg_flags
& MSG_USERSPACE
)
470 auio
.uio_segflg
= UIO_USERSPACE
;
472 auio
.uio_segflg
= UIO_SYSSPACE
;
473 auio
.uio_extflg
= UIO_COPY_DEFAULT
;
475 auio
.uio_fmode
= KSOCKET_FMODE(ks
);
478 for (i
= 0; i
< msg
->msg_iovlen
; i
++) {
480 iovlen
= (msg
->msg_iov
)[i
].iov_len
;
482 if (len
< 0 || iovlen
< 0)
485 auio
.uio_resid
= len
;
487 msg
->msg_flags
= flags
& (MSG_OOB
| MSG_PEEK
| MSG_WAITALL
|
488 MSG_DONTWAIT
| MSG_USERSPACE
);
490 error
= socket_recvmsg(KSTOSO(ks
), msg
, &auio
, cr
);
497 *recv
= len
- auio
.uio_resid
;
503 ksocket_shutdown(ksocket_t ks
, int how
, struct cred
*cr
)
507 /* All Solaris components should pass a cred for this operation. */
510 if (!KSOCKET_VALID(ks
))
515 return (socket_shutdown(so
, how
, cr
));
519 ksocket_close(ksocket_t ks
, struct cred
*cr
)
524 /* All Solaris components should pass a cred for this operation. */
527 mutex_enter(&so
->so_lock
);
529 if (!KSOCKET_VALID(ks
)) {
530 mutex_exit(&so
->so_lock
);
534 so
->so_state
|= SS_CLOSING
;
536 if (so
->so_count
> 1) {
537 mutex_enter(&so
->so_acceptq_lock
);
538 cv_broadcast(&so
->so_acceptq_cv
);
539 mutex_exit(&so
->so_acceptq_lock
);
540 cv_broadcast(&so
->so_rcv_cv
);
541 cv_broadcast(&so
->so_state_cv
);
542 cv_broadcast(&so
->so_single_cv
);
543 cv_broadcast(&so
->so_read_cv
);
544 cv_broadcast(&so
->so_snd_cv
);
545 cv_broadcast(&so
->so_copy_cv
);
547 while (so
->so_count
> 1)
548 cv_wait(&so
->so_closing_cv
, &so
->so_lock
);
550 mutex_exit(&so
->so_lock
);
551 /* Remove callbacks, if any */
552 (void) ksocket_setcallbacks(ks
, NULL
, NULL
, cr
);
554 (void) socket_close(so
, 0, cr
);
561 ksocket_getsockname(ksocket_t ks
, struct sockaddr
*addr
, socklen_t
*addrlen
,
566 /* All Solaris components should pass a cred for this operation. */
569 if (!KSOCKET_VALID(ks
))
574 if (addrlen
== NULL
|| (addr
== NULL
&& *addrlen
!= 0))
577 return (socket_getsockname(so
, addr
, addrlen
, cr
));
581 ksocket_getpeername(ksocket_t ks
, struct sockaddr
*addr
, socklen_t
*addrlen
,
586 /* All Solaris components should pass a cred for this operation. */
589 if (!KSOCKET_VALID(ks
))
594 if (addrlen
== NULL
|| (addr
== NULL
&& *addrlen
!= 0))
597 return (socket_getpeername(so
, addr
, addrlen
, B_FALSE
, cr
));
601 ksocket_getsockopt(ksocket_t ks
, int level
, int optname
, void *optval
,
602 int *optlen
, struct cred
*cr
)
606 /* All Solaris components should pass a cred for this operation. */
609 if (!KSOCKET_VALID(ks
))
616 if (*optlen
> SO_MAXARGSIZE
)
619 return (socket_getsockopt(so
, level
, optname
, optval
,
620 (socklen_t
*)optlen
, 0, cr
));
624 ksocket_setsockopt(ksocket_t ks
, int level
, int optname
, const void *optval
,
625 int optlen
, struct cred
*cr
)
629 /* All Solaris components should pass a cred for this operation. */
632 if (!KSOCKET_VALID(ks
))
640 return (socket_setsockopt(so
, level
, optname
, optval
,
641 (t_uscalar_t
)optlen
, cr
));
646 ksocket_setcallbacks(ksocket_t ks
, ksocket_callbacks_t
*cb
, void *arg
,
651 /* All Solaris components should pass a cred for this operation. */
654 if (!KSOCKET_VALID(ks
))
659 if (cb
== NULL
&& arg
!= NULL
)
662 mutex_enter(&so
->so_lock
);
663 bzero(&(so
->so_ksock_callbacks
), sizeof (ksocket_callbacks_t
));
664 so
->so_ksock_cb_arg
= NULL
;
665 mutex_exit(&so
->so_lock
);
667 mutex_enter(&so
->so_lock
);
668 SETCALLBACK(so
, cb
, connected
, KSOCKET_CB_CONNECTED
)
669 SETCALLBACK(so
, cb
, connectfailed
, KSOCKET_CB_CONNECTFAILED
)
670 SETCALLBACK(so
, cb
, disconnected
, KSOCKET_CB_DISCONNECTED
)
671 SETCALLBACK(so
, cb
, newdata
, KSOCKET_CB_NEWDATA
)
672 SETCALLBACK(so
, cb
, newconn
, KSOCKET_CB_NEWCONN
)
673 SETCALLBACK(so
, cb
, cansend
, KSOCKET_CB_CANSEND
)
674 SETCALLBACK(so
, cb
, oobdata
, KSOCKET_CB_OOBDATA
)
675 SETCALLBACK(so
, cb
, cantsendmore
, KSOCKET_CB_CANTSENDMORE
)
676 SETCALLBACK(so
, cb
, cantrecvmore
, KSOCKET_CB_CANTRECVMORE
)
677 so
->so_ksock_cb_arg
= arg
;
678 mutex_exit(&so
->so_lock
);
684 ksocket_ioctl(ksocket_t ks
, int cmd
, intptr_t arg
, int *rvalp
, struct cred
*cr
)
689 /* All Solaris components should pass a cred for this operation. */
692 if (!KSOCKET_VALID(ks
))
699 /* STREAM iotcls are not supported */
700 if ((cmd
& 0xffffff00U
) == STR
) {
703 rval
= socket_ioctl(so
, cmd
, arg
,
704 KSOCKET_FMODE(ks
) | FKIOCTL
, cr
, rvalp
);
720 * Wait for an input event, similar to t_kspoll().
721 * Ideas and code borrowed from ../devpoll.c
722 * Basically, setup just enough poll data structures so
723 * we can block on a CV until timeout or pollwakeup().
726 ksocket_spoll(ksocket_t ks
, int timo
, short events
, short *revents
,
730 pollhead_t
*php
, *php2
;
737 /* All Solaris components should pass a cred for this operation. */
739 ASSERT(curthread
->t_pollcache
== NULL
);
743 if (!KSOCKET_VALID(ks
))
748 * Check if there are any events already pending.
749 * If we're not willing to block, (timo == 0) then
750 * pass "anyyet">0 to socket_poll so it can skip
751 * some work. Othewise pass "anyyet"=0 and if
752 * there are no events pending, it will fill in
753 * the pollhead pointer we need for pollwakeup().
755 * XXX - pollrelock() logic needs to know which
756 * which pollcache lock to grab. It'd be a
757 * cleaner solution if we could pass pcp as
758 * an arguement in fop_poll interface instead
759 * of implicitly passing it using thread_t
760 * struct. On the other hand, changing fop_poll
761 * interface will require all driver/file system
762 * poll routine to change. May want to revisit
763 * the tradeoff later.
767 pcp
= pcache_alloc();
768 pcache_create(pcp
, 1);
770 mutex_enter(&pcp
->pc_lock
);
771 curthread
->t_pollcache
= pcp
;
772 error
= socket_poll(so
, (short)events
, (timo
== 0),
774 curthread
->t_pollcache
= NULL
;
775 mutex_exit(&pcp
->pc_lock
);
777 if (error
!= 0 || *revents
!= 0 || timo
== 0)
781 * Need to block. Did not get *revents, so the
782 * php should be non-NULL, but let's verify.
783 * Also compute when our sleep expires.
790 expires
= ddi_get_lbolt() +
791 MSEC_TO_TICK_ROUNDUP(timo
);
794 * Setup: pollhead -> polldat -> pollcache
795 * needed for pollwakeup()
796 * pdp should be freed by pcache_destroy
798 pdp
= kmem_zalloc(sizeof (*pdp
), KM_SLEEP
);
800 pdp
->pd_events
= events
;
801 pdp
->pd_pcache
= pcp
;
802 pcache_insert_fd(pcp
, pdp
, 1);
803 pollhead_insert(php
, pdp
);
806 mutex_enter(&pcp
->pc_lock
);
807 while (!(so
->so_state
& SS_CLOSING
)) {
810 /* Ditto pcp comment above. */
811 curthread
->t_pollcache
= pcp
;
812 error
= socket_poll(so
, (short)events
, 0,
814 curthread
->t_pollcache
= NULL
;
817 if (error
!= 0 || *revents
!= 0)
820 if (pcp
->pc_flag
& PC_POLLWAKE
)
824 rval
= cv_wait_sig(&pcp
->pc_cv
, &pcp
->pc_lock
);
826 rval
= cv_timedwait_sig(&pcp
->pc_cv
, &pcp
->pc_lock
,
835 mutex_exit(&pcp
->pc_lock
);
837 if (pdp
->pd_php
!= NULL
) {
838 pollhead_delete(pdp
->pd_php
, pdp
);
844 * pollwakeup() may still interact with this pollcache. Wait until
847 mutex_enter(&pcp
->pc_no_exit
);
848 ASSERT(pcp
->pc_busy
>= 0);
849 while (pcp
->pc_busy
> 0)
850 cv_wait(&pcp
->pc_busy_cv
, &pcp
->pc_no_exit
);
851 mutex_exit(&pcp
->pc_no_exit
);
858 ksocket_sendmblk(ksocket_t ks
, struct msghdr
*msg
, int flags
,
859 mblk_t
**mpp
, cred_t
*cr
)
867 /* All Solaris components should pass a cred for this operation. */
870 if (!KSOCKET_VALID(ks
))
875 if (flags
& MSG_MBLK_QUICKRELE
) {
876 error
= socket_getsockopt(so
, SOL_SOCKET
, SO_SND_COPYAVOID
,
877 &i_val
, &val_len
, 0, cr
);
881 /* Zero copy is not enable */
885 for (; mp
!= NULL
; mp
= mp
->b_cont
)
886 mp
->b_datap
->db_struioflag
|= STRUIO_ZC
;
889 error
= socket_sendmblk(so
, msg
, flags
, cr
, mpp
);
896 ksocket_hold(ksocket_t ks
)
901 if (!mutex_owned(&so
->so_lock
)) {
902 mutex_enter(&so
->so_lock
);
904 mutex_exit(&so
->so_lock
);
910 ksocket_rele(ksocket_t ks
)
916 * When so_count equals 1 means no thread working on this ksocket
918 if (so
->so_count
< 2)
919 cmn_err(CE_PANIC
, "ksocket_rele: sonode ref count 0 or 1");
921 if (!mutex_owned(&so
->so_lock
)) {
922 mutex_enter(&so
->so_lock
);
923 if (--so
->so_count
== 1)
924 cv_signal(&so
->so_closing_cv
);
925 mutex_exit(&so
->so_lock
);
927 if (--so
->so_count
== 1)
928 cv_signal(&so
->so_closing_cv
);