xref: /onnv-gate/usr/src/uts/common/ktli/t_kconnect.c (revision 8778:b4169d2ab299)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56990Sgd78059  * Common Development and Distribution License (the "License").
66990Sgd78059  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8778SErik.Nordmark@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * Kernel TLI-like function to allow a trasnport endpoint to initiate a
410Sstevel@tonic-gate  * connection to another transport endpoint.  This function will wait for
420Sstevel@tonic-gate  * an ack and a T_CONN_CON before returning.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * Returns:
450Sstevel@tonic-gate  * 	0 on success, and if rcvcall is non-NULL it shall be
460Sstevel@tonic-gate  * 	filled with the connection confirm data.
470Sstevel@tonic-gate  * 	Otherwise a positive error code.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <sys/param.h>
510Sstevel@tonic-gate #include <sys/types.h>
520Sstevel@tonic-gate #include <sys/user.h>
530Sstevel@tonic-gate #include <sys/file.h>
540Sstevel@tonic-gate #include <sys/vnode.h>
550Sstevel@tonic-gate #include <sys/errno.h>
560Sstevel@tonic-gate #include <sys/stream.h>
570Sstevel@tonic-gate #include <sys/ioctl.h>
580Sstevel@tonic-gate #include <sys/stropts.h>
590Sstevel@tonic-gate #include <sys/tihdr.h>
600Sstevel@tonic-gate #include <sys/timod.h>
610Sstevel@tonic-gate #include <sys/tiuser.h>
620Sstevel@tonic-gate #include <sys/t_kuser.h>
630Sstevel@tonic-gate #include <sys/strsubr.h>
640Sstevel@tonic-gate #include <sys/sysmacros.h>
656990Sgd78059 #include <sys/strsun.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 
680Sstevel@tonic-gate int
t_kconnect(TIUSER * tiptr,struct t_call * sndcall,struct t_call * rcvcall)690Sstevel@tonic-gate t_kconnect(TIUSER *tiptr, struct t_call *sndcall, struct t_call *rcvcall)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	int			len;
720Sstevel@tonic-gate 	int			msgsz;
730Sstevel@tonic-gate 	size_t			hdrsz;
740Sstevel@tonic-gate 	struct T_conn_req	*creq;
750Sstevel@tonic-gate 	union T_primitives	*pptr;
760Sstevel@tonic-gate 	mblk_t			*nbp;
770Sstevel@tonic-gate 	file_t			*fp;
780Sstevel@tonic-gate 	mblk_t			*bp;
790Sstevel@tonic-gate 	int			error;
800Sstevel@tonic-gate 	int			flag;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	error = 0;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	fp = tiptr->fp;
850Sstevel@tonic-gate 	msgsz = (int)TCONNREQSZ;
86*8778SErik.Nordmark@Sun.COM 	/*
87*8778SErik.Nordmark@Sun.COM 	 * Usually connect()s are performed with the credential of the caller;
88*8778SErik.Nordmark@Sun.COM 	 * in this particular case we specifically use the credential of
89*8778SErik.Nordmark@Sun.COM 	 * the opener as this call is typically done in the context of a user
90*8778SErik.Nordmark@Sun.COM 	 * process but on behalf of the kernel, e.g., a client connection
91*8778SErik.Nordmark@Sun.COM 	 * to a server which is later shared by different users.
92*8778SErik.Nordmark@Sun.COM 	 * At open time, we make sure to set fp->f_cred to kcred if such is
93*8778SErik.Nordmark@Sun.COM 	 * the case.
94*8778SErik.Nordmark@Sun.COM 	 *
95*8778SErik.Nordmark@Sun.COM 	 * Note: if the receiver uses SCM_UCRED/getpeerucred the pid will
96*8778SErik.Nordmark@Sun.COM 	 * appear as -1.
97*8778SErik.Nordmark@Sun.COM 	 */
98*8778SErik.Nordmark@Sun.COM 	while (!(bp = allocb_cred(msgsz + sndcall->addr.len + sndcall->opt.len,
99*8778SErik.Nordmark@Sun.COM 	    fp->f_cred, NOPID))) {
1000Sstevel@tonic-gate 		if (strwaitbuf(msgsz + sndcall->addr.len + sndcall->opt.len,
1010Sstevel@tonic-gate 		    BPRI_LO))
1020Sstevel@tonic-gate 			return (ENOSR);
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	/* LINTED pointer alignment */
1060Sstevel@tonic-gate 	creq = (struct T_conn_req *)bp->b_wptr;
1070Sstevel@tonic-gate 	creq->PRIM_type = T_CONN_REQ;
1080Sstevel@tonic-gate 	creq->DEST_length = (t_scalar_t)sndcall->addr.len;
1090Sstevel@tonic-gate 	creq->OPT_length = (t_scalar_t)sndcall->opt.len;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	if (sndcall->addr.len) {
1120Sstevel@tonic-gate 		bcopy(sndcall->addr.buf, (bp->b_wptr+msgsz), sndcall->addr.len);
1130Sstevel@tonic-gate 		creq->DEST_offset = (t_scalar_t)msgsz;
1140Sstevel@tonic-gate 		msgsz += sndcall->addr.len;
1150Sstevel@tonic-gate 	} else
1160Sstevel@tonic-gate 		creq->DEST_offset = (t_scalar_t)0;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (sndcall->opt.len) {
1190Sstevel@tonic-gate 		bcopy(sndcall->opt.buf, (bp->b_wptr+msgsz), sndcall->opt.len);
1200Sstevel@tonic-gate 		creq->OPT_offset = (t_scalar_t)msgsz;
1210Sstevel@tonic-gate 		msgsz += sndcall->opt.len;
1220Sstevel@tonic-gate 	} else
1230Sstevel@tonic-gate 		creq->OPT_offset = (t_scalar_t)0;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	bp->b_datap->db_type = M_PROTO;
1260Sstevel@tonic-gate 	bp->b_wptr += msgsz;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	/*
1290Sstevel@tonic-gate 	 * copy the users data, if any.
1300Sstevel@tonic-gate 	 */
1310Sstevel@tonic-gate 	if (sndcall->udata.len) {
1320Sstevel@tonic-gate 		/*
1330Sstevel@tonic-gate 		 * if CO then we would allocate a data block and
1340Sstevel@tonic-gate 		 * put the users connect data into it.
1350Sstevel@tonic-gate 		 */
1360Sstevel@tonic-gate 		KTLILOG(1,
1370Sstevel@tonic-gate 		    "Attempt to send connectionless data on T_CONN_REQ\n", 0);
1380Sstevel@tonic-gate 		freemsg(bp);
1390Sstevel@tonic-gate 		return (EPROTO);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	flag = fp->f_flag;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	/*
1450Sstevel@tonic-gate 	 * send it
1460Sstevel@tonic-gate 	 */
1470Sstevel@tonic-gate 	if ((error = tli_send(tiptr, bp, flag)) != 0)
1480Sstevel@tonic-gate 		return (error);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	/*
1510Sstevel@tonic-gate 	 * wait for acknowledgment
1520Sstevel@tonic-gate 	 */
1530Sstevel@tonic-gate 	if ((error = get_ok_ack(tiptr, T_CONN_REQ, flag)) != 0)
1540Sstevel@tonic-gate 		return (error);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	bp = NULL;
1570Sstevel@tonic-gate 	/*
1580Sstevel@tonic-gate 	 * wait for CONfirm
1590Sstevel@tonic-gate 	 */
1600Sstevel@tonic-gate 	if ((error = tli_recv(tiptr, &bp, flag)) != 0)
1610Sstevel@tonic-gate 		return (error);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (bp->b_datap->db_type != M_PROTO) {
1640Sstevel@tonic-gate 		freemsg(bp);
1650Sstevel@tonic-gate 		return (EPROTO);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	/* LINTED pointer alignment */
1690Sstevel@tonic-gate 	pptr = (union T_primitives *)bp->b_rptr;
1700Sstevel@tonic-gate 	switch (pptr->type) {
1710Sstevel@tonic-gate 	case T_CONN_CON:
1726990Sgd78059 		hdrsz = MBLKL(bp);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 		/*
1750Sstevel@tonic-gate 		 * check everything for consistency
1760Sstevel@tonic-gate 		 */
1770Sstevel@tonic-gate 		if (hdrsz < TCONNCONSZ ||
1780Sstevel@tonic-gate 		    hdrsz < (pptr->conn_con.OPT_length +
1790Sstevel@tonic-gate 		    pptr->conn_con.OPT_offset) ||
1800Sstevel@tonic-gate 		    hdrsz < (pptr->conn_con.RES_length +
1810Sstevel@tonic-gate 		    pptr->conn_con.RES_offset)) {
1820Sstevel@tonic-gate 			error = EPROTO;
1830Sstevel@tonic-gate 			freemsg(bp);
1840Sstevel@tonic-gate 			break;
1850Sstevel@tonic-gate 		}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 		if (rcvcall != NULL) {
1880Sstevel@tonic-gate 			/*
1890Sstevel@tonic-gate 			 * okay, so now we copy them
1900Sstevel@tonic-gate 			 */
1910Sstevel@tonic-gate 			len = MIN(pptr->conn_con.RES_length,
1920Sstevel@tonic-gate 			    rcvcall->addr.maxlen);
1930Sstevel@tonic-gate 			bcopy(bp->b_rptr + pptr->conn_con.RES_offset,
1940Sstevel@tonic-gate 			    rcvcall->addr.buf, len);
1950Sstevel@tonic-gate 			rcvcall->addr.len = len;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 			len = MIN(pptr->conn_con.OPT_length,
1980Sstevel@tonic-gate 			    rcvcall->opt.maxlen);
1990Sstevel@tonic-gate 			bcopy(bp->b_rptr + pptr->conn_con.OPT_offset,
2000Sstevel@tonic-gate 			    rcvcall->opt.buf, len);
2010Sstevel@tonic-gate 			rcvcall->opt.len = len;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 			if (bp->b_cont) {
2040Sstevel@tonic-gate 				nbp = bp;
2050Sstevel@tonic-gate 				bp = bp->b_cont;
2066990Sgd78059 				msgsz = (int)MBLKL(bp);
2070Sstevel@tonic-gate 				len = MIN(msgsz, rcvcall->udata.maxlen);
2080Sstevel@tonic-gate 				bcopy(bp->b_rptr, rcvcall->udata.buf, len);
2090Sstevel@tonic-gate 				rcvcall->udata.len = len;
2100Sstevel@tonic-gate 				freemsg(nbp);
2110Sstevel@tonic-gate 			}
2120Sstevel@tonic-gate 		} else {
2130Sstevel@tonic-gate 			freemsg(bp);
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate 		break;
2160Sstevel@tonic-gate 
2177010Sgwr 	case T_DISCON_IND:
2187010Sgwr 		/*
2197010Sgwr 		 * TCP puts the errno here, i.e.
2207010Sgwr 		 * ETIMEDOUT, ECONNREFUSED
2217010Sgwr 		 */
2227010Sgwr 		error = pptr->discon_ind.DISCON_reason;
2237010Sgwr 		freemsg(bp);
2247010Sgwr 		break;
2257010Sgwr 
2260Sstevel@tonic-gate 	default:
2270Sstevel@tonic-gate 		error = EPROTO;
2280Sstevel@tonic-gate 		freemsg(bp);
2290Sstevel@tonic-gate 		break;
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 	return (error);
2320Sstevel@tonic-gate }
233