5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/stream.h>
29 #include <sys/strsubr.h>
30 #include <sys/strsun.h>
31 #include <sys/stropts.h>
32 #include <sys/vnode.h>
33 #include <sys/zone.h>
34 #include <sys/strlog.h>
35 #include <sys/sysmacros.h>
36 #define _SUN_TPI_VERSION 2
37 #include <sys/tihdr.h>
38 #include <sys/timod.h>
39 #include <sys/tiuser.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/sunldi.h>
43 #include <sys/file.h>
44 #include <sys/modctl.h>
875 case SO_RCVBUF:
876 if (*i1 > keystack->keystack_max_buf) {
877 errno = ENOBUFS;
878 } else {
879 RD(q)->q_hiwat = *i1;
880 (void) proto_set_rx_hiwat(RD(q), NULL, *i1);
881 }
882 break;
883 default:
884 errno = EINVAL;
885 }
886 mutex_exit(&ks->keysock_lock);
887 break;
888 default:
889 errno = EINVAL;
890 }
891 return (errno);
892 }
893
894 /*
895 * Handle STREAMS messages.
896 */
897 static void
898 keysock_wput_other(queue_t *q, mblk_t *mp)
899 {
900 struct iocblk *iocp;
901 int error;
902 keysock_t *ks = (keysock_t *)q->q_ptr;
903 keysock_stack_t *keystack = ks->keysock_keystack;
904 cred_t *cr;
905
906 switch (mp->b_datap->db_type) {
907 case M_PROTO:
908 case M_PCPROTO:
909 if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) {
910 ks3dbg(keystack, (
911 "keysock_wput_other: Not big enough M_PROTO\n"));
912 freemsg(mp);
913 return;
914 }
937 if (((union T_primitives *)mp->b_rptr)->type ==
938 T_SVR4_OPTMGMT_REQ) {
939 svr4_optcom_req(q, mp, cr, &keysock_opt_obj);
940 } else {
941 tpi_optcom_req(q, mp, cr, &keysock_opt_obj);
942 }
943 break;
944 case T_DATA_REQ:
945 case T_EXDATA_REQ:
946 case T_ORDREL_REQ:
947 /* Illegal for keysock. */
948 freemsg(mp);
949 (void) putnextctl1(RD(q), M_ERROR, EPROTO);
950 break;
951 default:
952 /* Not supported by keysock. */
953 keysock_err_ack(q, mp, TNOTSUPPORT, 0);
954 break;
955 }
956 return;
957 case M_IOCTL:
958 iocp = (struct iocblk *)mp->b_rptr;
959 error = EINVAL;
960
961 switch (iocp->ioc_cmd) {
962 case ND_SET:
963 case ND_GET:
964 if (nd_getset(q, keystack->keystack_g_nd, mp)) {
965 qreply(q, mp);
966 return;
967 } else
968 error = ENOENT;
969 /* FALLTHRU */
970 default:
971 miocnak(q, mp, 0, error);
972 return;
973 }
974 case M_FLUSH:
975 if (*mp->b_rptr & FLUSHW) {
976 flushq(q, FLUSHALL);
977 *mp->b_rptr &= ~FLUSHW;
978 }
979 if (*mp->b_rptr & FLUSHR) {
980 qreply(q, mp);
981 return;
|
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25 /*
26 * Copyright 2017 Joyent, Inc.
27 */
28
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/stream.h>
32 #include <sys/strsubr.h>
33 #include <sys/strsun.h>
34 #include <sys/stropts.h>
35 #include <sys/vnode.h>
36 #include <sys/zone.h>
37 #include <sys/strlog.h>
38 #include <sys/sysmacros.h>
39 #define _SUN_TPI_VERSION 2
40 #include <sys/tihdr.h>
41 #include <sys/timod.h>
42 #include <sys/tiuser.h>
43 #include <sys/ddi.h>
44 #include <sys/sunddi.h>
45 #include <sys/sunldi.h>
46 #include <sys/file.h>
47 #include <sys/modctl.h>
878 case SO_RCVBUF:
879 if (*i1 > keystack->keystack_max_buf) {
880 errno = ENOBUFS;
881 } else {
882 RD(q)->q_hiwat = *i1;
883 (void) proto_set_rx_hiwat(RD(q), NULL, *i1);
884 }
885 break;
886 default:
887 errno = EINVAL;
888 }
889 mutex_exit(&ks->keysock_lock);
890 break;
891 default:
892 errno = EINVAL;
893 }
894 return (errno);
895 }
896
897 /*
898 * Handle STREAMS ioctl copyin for getsockname() for both PF_KEY and
899 * PF_POLICY.
900 */
901 void
902 keysock_spdsock_wput_iocdata(queue_t *q, mblk_t *mp, sa_family_t family)
903 {
904 mblk_t *mp1;
905 STRUCT_HANDLE(strbuf, sb);
906 /* What size of sockaddr do we need? */
907 const uint_t addrlen = sizeof (struct sockaddr);
908
909 /* We only handle TI_GET{MY,PEER}NAME (get{sock,peer}name()). */
910 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
911 case TI_GETMYNAME:
912 case TI_GETPEERNAME:
913 break;
914 default:
915 freemsg(mp);
916 return;
917 }
918
919 switch (mi_copy_state(q, mp, &mp1)) {
920 case -1:
921 return;
922 case MI_COPY_CASE(MI_COPY_IN, 1):
923 break;
924 case MI_COPY_CASE(MI_COPY_OUT, 1):
925 /*
926 * The address has been copied out, so now
927 * copyout the strbuf.
928 */
929 mi_copyout(q, mp);
930 return;
931 case MI_COPY_CASE(MI_COPY_OUT, 2):
932 /*
933 * The address and strbuf have been copied out.
934 * We're done, so just acknowledge the original
935 * M_IOCTL.
936 */
937 mi_copy_done(q, mp, 0);
938 return;
939 default:
940 /*
941 * Something strange has happened, so acknowledge
942 * the original M_IOCTL with an EPROTO error.
943 */
944 mi_copy_done(q, mp, EPROTO);
945 return;
946 }
947
948 /*
949 * Now we have the strbuf structure for TI_GET{MY,PEER}NAME. Next we
950 * copyout the requested address and then we'll copyout the strbuf.
951 * Regardless of sockname or peername, we just return a sockaddr with
952 * sa_family set.
953 */
954 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag,
955 (void *)mp1->b_rptr);
956
957 if (STRUCT_FGET(sb, maxlen) < addrlen) {
958 mi_copy_done(q, mp, EINVAL);
959 return;
960 }
961
962 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
963 if (mp1 == NULL)
964 return;
965
966 STRUCT_FSET(sb, len, addrlen);
967 ((struct sockaddr *)mp1->b_wptr)->sa_family = family;
968 mp1->b_wptr += addrlen;
969 mi_copyout(q, mp);
970 }
971
972 /*
973 * Handle STREAMS messages.
974 */
975 static void
976 keysock_wput_other(queue_t *q, mblk_t *mp)
977 {
978 struct iocblk *iocp;
979 int error;
980 keysock_t *ks = (keysock_t *)q->q_ptr;
981 keysock_stack_t *keystack = ks->keysock_keystack;
982 cred_t *cr;
983
984 switch (mp->b_datap->db_type) {
985 case M_PROTO:
986 case M_PCPROTO:
987 if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) {
988 ks3dbg(keystack, (
989 "keysock_wput_other: Not big enough M_PROTO\n"));
990 freemsg(mp);
991 return;
992 }
1015 if (((union T_primitives *)mp->b_rptr)->type ==
1016 T_SVR4_OPTMGMT_REQ) {
1017 svr4_optcom_req(q, mp, cr, &keysock_opt_obj);
1018 } else {
1019 tpi_optcom_req(q, mp, cr, &keysock_opt_obj);
1020 }
1021 break;
1022 case T_DATA_REQ:
1023 case T_EXDATA_REQ:
1024 case T_ORDREL_REQ:
1025 /* Illegal for keysock. */
1026 freemsg(mp);
1027 (void) putnextctl1(RD(q), M_ERROR, EPROTO);
1028 break;
1029 default:
1030 /* Not supported by keysock. */
1031 keysock_err_ack(q, mp, TNOTSUPPORT, 0);
1032 break;
1033 }
1034 return;
1035 case M_IOCDATA:
1036 keysock_spdsock_wput_iocdata(q, mp, PF_KEY);
1037 return;
1038 case M_IOCTL:
1039 iocp = (struct iocblk *)mp->b_rptr;
1040 error = EINVAL;
1041
1042 switch (iocp->ioc_cmd) {
1043 case TI_GETMYNAME:
1044 case TI_GETPEERNAME:
1045 /*
1046 * For pfiles(1) observability with getsockname().
1047 * See keysock_spdsock_wput_iocdata() for the rest of
1048 * this.
1049 */
1050 mi_copyin(q, mp, NULL,
1051 SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
1052 return;
1053 case ND_SET:
1054 case ND_GET:
1055 if (nd_getset(q, keystack->keystack_g_nd, mp)) {
1056 qreply(q, mp);
1057 return;
1058 } else
1059 error = ENOENT;
1060 /* FALLTHRU */
1061 default:
1062 miocnak(q, mp, 0, error);
1063 return;
1064 }
1065 case M_FLUSH:
1066 if (*mp->b_rptr & FLUSHW) {
1067 flushq(q, FLUSHALL);
1068 *mp->b_rptr &= ~FLUSHW;
1069 }
1070 if (*mp->b_rptr & FLUSHR) {
1071 qreply(q, mp);
1072 return;
|