17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
522eb7cb5Sgd78059 * Common Development and Distribution License (the "License").
622eb7cb5Sgd78059 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*de8c4a14SErik Nordmark * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate * The Regents of the University of California
327c478bd9Sstevel@tonic-gate * All Rights Reserved
337c478bd9Sstevel@tonic-gate *
347c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate * contributors.
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate * Kernel TLI-like function to allow a trasnport endpoint to initiate a
417c478bd9Sstevel@tonic-gate * connection to another transport endpoint. This function will wait for
427c478bd9Sstevel@tonic-gate * an ack and a T_CONN_CON before returning.
437c478bd9Sstevel@tonic-gate *
447c478bd9Sstevel@tonic-gate * Returns:
457c478bd9Sstevel@tonic-gate * 0 on success, and if rcvcall is non-NULL it shall be
467c478bd9Sstevel@tonic-gate * filled with the connection confirm data.
477c478bd9Sstevel@tonic-gate * Otherwise a positive error code.
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate #include <sys/param.h>
517c478bd9Sstevel@tonic-gate #include <sys/types.h>
527c478bd9Sstevel@tonic-gate #include <sys/user.h>
537c478bd9Sstevel@tonic-gate #include <sys/file.h>
547c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
557c478bd9Sstevel@tonic-gate #include <sys/errno.h>
567c478bd9Sstevel@tonic-gate #include <sys/stream.h>
577c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
587c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
597c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
607c478bd9Sstevel@tonic-gate #include <sys/timod.h>
617c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
627c478bd9Sstevel@tonic-gate #include <sys/t_kuser.h>
637c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
647c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
6522eb7cb5Sgd78059 #include <sys/strsun.h>
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate int
t_kconnect(TIUSER * tiptr,struct t_call * sndcall,struct t_call * rcvcall)697c478bd9Sstevel@tonic-gate t_kconnect(TIUSER *tiptr, struct t_call *sndcall, struct t_call *rcvcall)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate int len;
727c478bd9Sstevel@tonic-gate int msgsz;
737c478bd9Sstevel@tonic-gate size_t hdrsz;
747c478bd9Sstevel@tonic-gate struct T_conn_req *creq;
757c478bd9Sstevel@tonic-gate union T_primitives *pptr;
767c478bd9Sstevel@tonic-gate mblk_t *nbp;
777c478bd9Sstevel@tonic-gate file_t *fp;
787c478bd9Sstevel@tonic-gate mblk_t *bp;
797c478bd9Sstevel@tonic-gate int error;
807c478bd9Sstevel@tonic-gate int flag;
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate error = 0;
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate fp = tiptr->fp;
857c478bd9Sstevel@tonic-gate msgsz = (int)TCONNREQSZ;
86*de8c4a14SErik Nordmark /*
87*de8c4a14SErik Nordmark * Usually connect()s are performed with the credential of the caller;
88*de8c4a14SErik Nordmark * in this particular case we specifically use the credential of
89*de8c4a14SErik Nordmark * the opener as this call is typically done in the context of a user
90*de8c4a14SErik Nordmark * process but on behalf of the kernel, e.g., a client connection
91*de8c4a14SErik Nordmark * to a server which is later shared by different users.
92*de8c4a14SErik Nordmark * At open time, we make sure to set fp->f_cred to kcred if such is
93*de8c4a14SErik Nordmark * the case.
94*de8c4a14SErik Nordmark *
95*de8c4a14SErik Nordmark * Note: if the receiver uses SCM_UCRED/getpeerucred the pid will
96*de8c4a14SErik Nordmark * appear as -1.
97*de8c4a14SErik Nordmark */
98*de8c4a14SErik Nordmark while (!(bp = allocb_cred(msgsz + sndcall->addr.len + sndcall->opt.len,
99*de8c4a14SErik Nordmark fp->f_cred, NOPID))) {
1007c478bd9Sstevel@tonic-gate if (strwaitbuf(msgsz + sndcall->addr.len + sndcall->opt.len,
1017c478bd9Sstevel@tonic-gate BPRI_LO))
1027c478bd9Sstevel@tonic-gate return (ENOSR);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
1067c478bd9Sstevel@tonic-gate creq = (struct T_conn_req *)bp->b_wptr;
1077c478bd9Sstevel@tonic-gate creq->PRIM_type = T_CONN_REQ;
1087c478bd9Sstevel@tonic-gate creq->DEST_length = (t_scalar_t)sndcall->addr.len;
1097c478bd9Sstevel@tonic-gate creq->OPT_length = (t_scalar_t)sndcall->opt.len;
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate if (sndcall->addr.len) {
1127c478bd9Sstevel@tonic-gate bcopy(sndcall->addr.buf, (bp->b_wptr+msgsz), sndcall->addr.len);
1137c478bd9Sstevel@tonic-gate creq->DEST_offset = (t_scalar_t)msgsz;
1147c478bd9Sstevel@tonic-gate msgsz += sndcall->addr.len;
1157c478bd9Sstevel@tonic-gate } else
1167c478bd9Sstevel@tonic-gate creq->DEST_offset = (t_scalar_t)0;
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate if (sndcall->opt.len) {
1197c478bd9Sstevel@tonic-gate bcopy(sndcall->opt.buf, (bp->b_wptr+msgsz), sndcall->opt.len);
1207c478bd9Sstevel@tonic-gate creq->OPT_offset = (t_scalar_t)msgsz;
1217c478bd9Sstevel@tonic-gate msgsz += sndcall->opt.len;
1227c478bd9Sstevel@tonic-gate } else
1237c478bd9Sstevel@tonic-gate creq->OPT_offset = (t_scalar_t)0;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate bp->b_datap->db_type = M_PROTO;
1267c478bd9Sstevel@tonic-gate bp->b_wptr += msgsz;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate * copy the users data, if any.
1307c478bd9Sstevel@tonic-gate */
1317c478bd9Sstevel@tonic-gate if (sndcall->udata.len) {
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate * if CO then we would allocate a data block and
1347c478bd9Sstevel@tonic-gate * put the users connect data into it.
1357c478bd9Sstevel@tonic-gate */
1367c478bd9Sstevel@tonic-gate KTLILOG(1,
1377c478bd9Sstevel@tonic-gate "Attempt to send connectionless data on T_CONN_REQ\n", 0);
1387c478bd9Sstevel@tonic-gate freemsg(bp);
1397c478bd9Sstevel@tonic-gate return (EPROTO);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate flag = fp->f_flag;
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate * send it
1467c478bd9Sstevel@tonic-gate */
1477c478bd9Sstevel@tonic-gate if ((error = tli_send(tiptr, bp, flag)) != 0)
1487c478bd9Sstevel@tonic-gate return (error);
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate * wait for acknowledgment
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate if ((error = get_ok_ack(tiptr, T_CONN_REQ, flag)) != 0)
1547c478bd9Sstevel@tonic-gate return (error);
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate bp = NULL;
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate * wait for CONfirm
1597c478bd9Sstevel@tonic-gate */
1607c478bd9Sstevel@tonic-gate if ((error = tli_recv(tiptr, &bp, flag)) != 0)
1617c478bd9Sstevel@tonic-gate return (error);
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate if (bp->b_datap->db_type != M_PROTO) {
1647c478bd9Sstevel@tonic-gate freemsg(bp);
1657c478bd9Sstevel@tonic-gate return (EPROTO);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
1697c478bd9Sstevel@tonic-gate pptr = (union T_primitives *)bp->b_rptr;
1707c478bd9Sstevel@tonic-gate switch (pptr->type) {
1717c478bd9Sstevel@tonic-gate case T_CONN_CON:
17222eb7cb5Sgd78059 hdrsz = MBLKL(bp);
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate * check everything for consistency
1767c478bd9Sstevel@tonic-gate */
1777c478bd9Sstevel@tonic-gate if (hdrsz < TCONNCONSZ ||
1787c478bd9Sstevel@tonic-gate hdrsz < (pptr->conn_con.OPT_length +
1797c478bd9Sstevel@tonic-gate pptr->conn_con.OPT_offset) ||
1807c478bd9Sstevel@tonic-gate hdrsz < (pptr->conn_con.RES_length +
1817c478bd9Sstevel@tonic-gate pptr->conn_con.RES_offset)) {
1827c478bd9Sstevel@tonic-gate error = EPROTO;
1837c478bd9Sstevel@tonic-gate freemsg(bp);
1847c478bd9Sstevel@tonic-gate break;
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate if (rcvcall != NULL) {
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate * okay, so now we copy them
1907c478bd9Sstevel@tonic-gate */
1917c478bd9Sstevel@tonic-gate len = MIN(pptr->conn_con.RES_length,
1927c478bd9Sstevel@tonic-gate rcvcall->addr.maxlen);
1937c478bd9Sstevel@tonic-gate bcopy(bp->b_rptr + pptr->conn_con.RES_offset,
1947c478bd9Sstevel@tonic-gate rcvcall->addr.buf, len);
1957c478bd9Sstevel@tonic-gate rcvcall->addr.len = len;
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate len = MIN(pptr->conn_con.OPT_length,
1987c478bd9Sstevel@tonic-gate rcvcall->opt.maxlen);
1997c478bd9Sstevel@tonic-gate bcopy(bp->b_rptr + pptr->conn_con.OPT_offset,
2007c478bd9Sstevel@tonic-gate rcvcall->opt.buf, len);
2017c478bd9Sstevel@tonic-gate rcvcall->opt.len = len;
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate if (bp->b_cont) {
2047c478bd9Sstevel@tonic-gate nbp = bp;
2057c478bd9Sstevel@tonic-gate bp = bp->b_cont;
20622eb7cb5Sgd78059 msgsz = (int)MBLKL(bp);
2077c478bd9Sstevel@tonic-gate len = MIN(msgsz, rcvcall->udata.maxlen);
2087c478bd9Sstevel@tonic-gate bcopy(bp->b_rptr, rcvcall->udata.buf, len);
2097c478bd9Sstevel@tonic-gate rcvcall->udata.len = len;
2107c478bd9Sstevel@tonic-gate freemsg(nbp);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate } else {
2137c478bd9Sstevel@tonic-gate freemsg(bp);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate break;
2167c478bd9Sstevel@tonic-gate
2170fd77660Sgwr case T_DISCON_IND:
2180fd77660Sgwr /*
2190fd77660Sgwr * TCP puts the errno here, i.e.
2200fd77660Sgwr * ETIMEDOUT, ECONNREFUSED
2210fd77660Sgwr */
2220fd77660Sgwr error = pptr->discon_ind.DISCON_reason;
2230fd77660Sgwr freemsg(bp);
2240fd77660Sgwr break;
2250fd77660Sgwr
2267c478bd9Sstevel@tonic-gate default:
2277c478bd9Sstevel@tonic-gate error = EPROTO;
2287c478bd9Sstevel@tonic-gate freemsg(bp);
2297c478bd9Sstevel@tonic-gate break;
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate return (error);
2327c478bd9Sstevel@tonic-gate }
233