xref: /onnv-gate/usr/src/uts/common/ktli/t_kconnect.c (revision 7010:827c1f26c430)
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 /*
226990Sgd78059  * Copyright 2008 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * Kernel TLI-like function to allow a trasnport endpoint to initiate a
430Sstevel@tonic-gate  * connection to another transport endpoint.  This function will wait for
440Sstevel@tonic-gate  * an ack and a T_CONN_CON before returning.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * Returns:
470Sstevel@tonic-gate  * 	0 on success, and if rcvcall is non-NULL it shall be
480Sstevel@tonic-gate  * 	filled with the connection confirm data.
490Sstevel@tonic-gate  * 	Otherwise a positive error code.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <sys/param.h>
530Sstevel@tonic-gate #include <sys/types.h>
540Sstevel@tonic-gate #include <sys/user.h>
550Sstevel@tonic-gate #include <sys/file.h>
560Sstevel@tonic-gate #include <sys/vnode.h>
570Sstevel@tonic-gate #include <sys/errno.h>
580Sstevel@tonic-gate #include <sys/stream.h>
590Sstevel@tonic-gate #include <sys/ioctl.h>
600Sstevel@tonic-gate #include <sys/stropts.h>
610Sstevel@tonic-gate #include <sys/tihdr.h>
620Sstevel@tonic-gate #include <sys/timod.h>
630Sstevel@tonic-gate #include <sys/tiuser.h>
640Sstevel@tonic-gate #include <sys/t_kuser.h>
650Sstevel@tonic-gate #include <sys/strsubr.h>
660Sstevel@tonic-gate #include <sys/sysmacros.h>
676990Sgd78059 #include <sys/strsun.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 
700Sstevel@tonic-gate int
710Sstevel@tonic-gate t_kconnect(TIUSER *tiptr, struct t_call *sndcall, struct t_call *rcvcall)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	int			len;
740Sstevel@tonic-gate 	int			msgsz;
750Sstevel@tonic-gate 	size_t			hdrsz;
760Sstevel@tonic-gate 	struct T_conn_req	*creq;
770Sstevel@tonic-gate 	union T_primitives	*pptr;
780Sstevel@tonic-gate 	mblk_t			*nbp;
790Sstevel@tonic-gate 	file_t			*fp;
800Sstevel@tonic-gate 	mblk_t			*bp;
810Sstevel@tonic-gate 	int			error;
820Sstevel@tonic-gate 	int			flag;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	error = 0;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	fp = tiptr->fp;
870Sstevel@tonic-gate 	msgsz = (int)TCONNREQSZ;
880Sstevel@tonic-gate 	while (!(bp = allocb(msgsz + sndcall->addr.len + sndcall->opt.len,
890Sstevel@tonic-gate 	    BPRI_LO))) {
900Sstevel@tonic-gate 		if (strwaitbuf(msgsz + sndcall->addr.len + sndcall->opt.len,
910Sstevel@tonic-gate 		    BPRI_LO))
920Sstevel@tonic-gate 			return (ENOSR);
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	/* LINTED pointer alignment */
960Sstevel@tonic-gate 	creq = (struct T_conn_req *)bp->b_wptr;
970Sstevel@tonic-gate 	creq->PRIM_type = T_CONN_REQ;
980Sstevel@tonic-gate 	creq->DEST_length = (t_scalar_t)sndcall->addr.len;
990Sstevel@tonic-gate 	creq->OPT_length = (t_scalar_t)sndcall->opt.len;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (sndcall->addr.len) {
1020Sstevel@tonic-gate 		bcopy(sndcall->addr.buf, (bp->b_wptr+msgsz), sndcall->addr.len);
1030Sstevel@tonic-gate 		creq->DEST_offset = (t_scalar_t)msgsz;
1040Sstevel@tonic-gate 		msgsz += sndcall->addr.len;
1050Sstevel@tonic-gate 	} else
1060Sstevel@tonic-gate 		creq->DEST_offset = (t_scalar_t)0;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if (sndcall->opt.len) {
1090Sstevel@tonic-gate 		bcopy(sndcall->opt.buf, (bp->b_wptr+msgsz), sndcall->opt.len);
1100Sstevel@tonic-gate 		creq->OPT_offset = (t_scalar_t)msgsz;
1110Sstevel@tonic-gate 		msgsz += sndcall->opt.len;
1120Sstevel@tonic-gate 	} else
1130Sstevel@tonic-gate 		creq->OPT_offset = (t_scalar_t)0;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	bp->b_datap->db_type = M_PROTO;
1160Sstevel@tonic-gate 	bp->b_wptr += msgsz;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	/*
1190Sstevel@tonic-gate 	 * copy the users data, if any.
1200Sstevel@tonic-gate 	 */
1210Sstevel@tonic-gate 	if (sndcall->udata.len) {
1220Sstevel@tonic-gate 		/*
1230Sstevel@tonic-gate 		 * if CO then we would allocate a data block and
1240Sstevel@tonic-gate 		 * put the users connect data into it.
1250Sstevel@tonic-gate 		 */
1260Sstevel@tonic-gate 		KTLILOG(1,
1270Sstevel@tonic-gate 		    "Attempt to send connectionless data on T_CONN_REQ\n", 0);
1280Sstevel@tonic-gate 		freemsg(bp);
1290Sstevel@tonic-gate 		return (EPROTO);
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	flag = fp->f_flag;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	/*
1350Sstevel@tonic-gate 	 * send it
1360Sstevel@tonic-gate 	 */
1370Sstevel@tonic-gate 	if ((error = tli_send(tiptr, bp, flag)) != 0)
1380Sstevel@tonic-gate 		return (error);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	/*
1410Sstevel@tonic-gate 	 * wait for acknowledgment
1420Sstevel@tonic-gate 	 */
1430Sstevel@tonic-gate 	if ((error = get_ok_ack(tiptr, T_CONN_REQ, flag)) != 0)
1440Sstevel@tonic-gate 		return (error);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	bp = NULL;
1470Sstevel@tonic-gate 	/*
1480Sstevel@tonic-gate 	 * wait for CONfirm
1490Sstevel@tonic-gate 	 */
1500Sstevel@tonic-gate 	if ((error = tli_recv(tiptr, &bp, flag)) != 0)
1510Sstevel@tonic-gate 		return (error);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if (bp->b_datap->db_type != M_PROTO) {
1540Sstevel@tonic-gate 		freemsg(bp);
1550Sstevel@tonic-gate 		return (EPROTO);
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/* LINTED pointer alignment */
1590Sstevel@tonic-gate 	pptr = (union T_primitives *)bp->b_rptr;
1600Sstevel@tonic-gate 	switch (pptr->type) {
1610Sstevel@tonic-gate 	case T_CONN_CON:
1626990Sgd78059 		hdrsz = MBLKL(bp);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 		/*
1650Sstevel@tonic-gate 		 * check everything for consistency
1660Sstevel@tonic-gate 		 */
1670Sstevel@tonic-gate 		if (hdrsz < TCONNCONSZ ||
1680Sstevel@tonic-gate 		    hdrsz < (pptr->conn_con.OPT_length +
1690Sstevel@tonic-gate 		    pptr->conn_con.OPT_offset) ||
1700Sstevel@tonic-gate 		    hdrsz < (pptr->conn_con.RES_length +
1710Sstevel@tonic-gate 		    pptr->conn_con.RES_offset)) {
1720Sstevel@tonic-gate 			error = EPROTO;
1730Sstevel@tonic-gate 			freemsg(bp);
1740Sstevel@tonic-gate 			break;
1750Sstevel@tonic-gate 		}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 		if (rcvcall != NULL) {
1780Sstevel@tonic-gate 			/*
1790Sstevel@tonic-gate 			 * okay, so now we copy them
1800Sstevel@tonic-gate 			 */
1810Sstevel@tonic-gate 			len = MIN(pptr->conn_con.RES_length,
1820Sstevel@tonic-gate 			    rcvcall->addr.maxlen);
1830Sstevel@tonic-gate 			bcopy(bp->b_rptr + pptr->conn_con.RES_offset,
1840Sstevel@tonic-gate 			    rcvcall->addr.buf, len);
1850Sstevel@tonic-gate 			rcvcall->addr.len = len;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 			len = MIN(pptr->conn_con.OPT_length,
1880Sstevel@tonic-gate 			    rcvcall->opt.maxlen);
1890Sstevel@tonic-gate 			bcopy(bp->b_rptr + pptr->conn_con.OPT_offset,
1900Sstevel@tonic-gate 			    rcvcall->opt.buf, len);
1910Sstevel@tonic-gate 			rcvcall->opt.len = len;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 			if (bp->b_cont) {
1940Sstevel@tonic-gate 				nbp = bp;
1950Sstevel@tonic-gate 				bp = bp->b_cont;
1966990Sgd78059 				msgsz = (int)MBLKL(bp);
1970Sstevel@tonic-gate 				len = MIN(msgsz, rcvcall->udata.maxlen);
1980Sstevel@tonic-gate 				bcopy(bp->b_rptr, rcvcall->udata.buf, len);
1990Sstevel@tonic-gate 				rcvcall->udata.len = len;
2000Sstevel@tonic-gate 				freemsg(nbp);
2010Sstevel@tonic-gate 			}
2020Sstevel@tonic-gate 		} else {
2030Sstevel@tonic-gate 			freemsg(bp);
2040Sstevel@tonic-gate 		}
2050Sstevel@tonic-gate 		break;
2060Sstevel@tonic-gate 
207*7010Sgwr 	case T_DISCON_IND:
208*7010Sgwr 		/*
209*7010Sgwr 		 * TCP puts the errno here, i.e.
210*7010Sgwr 		 * ETIMEDOUT, ECONNREFUSED
211*7010Sgwr 		 */
212*7010Sgwr 		error = pptr->discon_ind.DISCON_reason;
213*7010Sgwr 		freemsg(bp);
214*7010Sgwr 		break;
215*7010Sgwr 
2160Sstevel@tonic-gate 	default:
2170Sstevel@tonic-gate 		error = EPROTO;
2180Sstevel@tonic-gate 		freemsg(bp);
2190Sstevel@tonic-gate 		break;
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 	return (error);
2220Sstevel@tonic-gate }
223