xref: /onnv-gate/usr/src/uts/common/ktli/t_ksndudat.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
5*8778SErik.Nordmark@Sun.COM  * Common Development and Distribution License (the "License").
6*8778SErik.Nordmark@Sun.COM  * 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  * TLI-like function to send datagrams over a specified
410Sstevel@tonic-gate  * transport endpoint.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  * Returns:
440Sstevel@tonic-gate  * 	0 on success or positive error code.
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include <sys/param.h>
480Sstevel@tonic-gate #include <sys/types.h>
490Sstevel@tonic-gate #include <sys/user.h>
500Sstevel@tonic-gate #include <sys/file.h>
510Sstevel@tonic-gate #include <sys/errno.h>
520Sstevel@tonic-gate #include <sys/stream.h>
530Sstevel@tonic-gate #include <sys/strsubr.h>
540Sstevel@tonic-gate #include <sys/vnode.h>
550Sstevel@tonic-gate #include <sys/ioctl.h>
560Sstevel@tonic-gate #include <sys/stropts.h>
570Sstevel@tonic-gate #include <sys/tihdr.h>
580Sstevel@tonic-gate #include <sys/timod.h>
590Sstevel@tonic-gate #include <sys/tiuser.h>
600Sstevel@tonic-gate #include <sys/t_kuser.h>
610Sstevel@tonic-gate #include <sys/debug.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 
640Sstevel@tonic-gate int
t_ksndudata(TIUSER * tiptr,struct t_kunitdata * unitdata,frtn_t * frtn)650Sstevel@tonic-gate t_ksndudata(TIUSER *tiptr, struct t_kunitdata *unitdata, frtn_t *frtn)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	int			msgsz;
680Sstevel@tonic-gate 	file_t			*fp;
690Sstevel@tonic-gate 	mblk_t			*bp;
700Sstevel@tonic-gate 	mblk_t			*dbp;
710Sstevel@tonic-gate 	struct T_unitdata_req	*udreq;
720Sstevel@tonic-gate 	int			error;
730Sstevel@tonic-gate 	int			flag;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	error = 0;
760Sstevel@tonic-gate 	fp = tiptr->fp;
770Sstevel@tonic-gate 	msgsz = unitdata->udata.len;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	/*
800Sstevel@tonic-gate 	 * See if Class 0 is required
810Sstevel@tonic-gate 	 */
820Sstevel@tonic-gate 	if (frtn != NULL) {
830Sstevel@tonic-gate 		ASSERT(unitdata->udata.udata_mp == NULL);
840Sstevel@tonic-gate 		ASSERT(unitdata->udata.buf != NULL);
850Sstevel@tonic-gate 		/*
860Sstevel@tonic-gate 		 * user has supplied their own buffer, all we have to
870Sstevel@tonic-gate 		 * do is allocate a class 0 streams buffer and set it
880Sstevel@tonic-gate 		 * up.
890Sstevel@tonic-gate 		 */
900Sstevel@tonic-gate 		if ((dbp = (mblk_t *)esballoc((uchar_t *)unitdata->udata.buf,
910Sstevel@tonic-gate 		    (size_t)msgsz, BPRI_LO, frtn)) == NULL)
920Sstevel@tonic-gate 			return (ENOSR);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 		dbp->b_datap->db_type = M_DATA;
950Sstevel@tonic-gate 		KTLILOG(2, "t_ksndudata: bp %x, ", dbp);
960Sstevel@tonic-gate 		KTLILOG(2, "len %d, ", msgsz);
970Sstevel@tonic-gate 		KTLILOG(2, "free func %x\n", frtn->free_func);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	} else if (unitdata->udata.buf) {
1000Sstevel@tonic-gate 		ASSERT(unitdata->udata.udata_mp == NULL);
1010Sstevel@tonic-gate 		while (!(dbp = allocb(msgsz, BPRI_LO)))
1020Sstevel@tonic-gate 			if (strwaitbuf((size_t)msgsz, BPRI_LO))
1030Sstevel@tonic-gate 				return (ENOSR);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 		bcopy(unitdata->udata.buf, dbp->b_wptr, unitdata->udata.len);
1060Sstevel@tonic-gate 		dbp->b_datap->db_type = M_DATA;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	} else if (unitdata->udata.udata_mp) {
1090Sstevel@tonic-gate 		ASSERT(unitdata->udata.buf == NULL);
1100Sstevel@tonic-gate 		/*
1110Sstevel@tonic-gate 		 * user has done it all
1120Sstevel@tonic-gate 		 */
1130Sstevel@tonic-gate 		dbp = unitdata->udata.udata_mp;
1140Sstevel@tonic-gate 		goto gotdp;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	} else {
1170Sstevel@tonic-gate 		/*
1180Sstevel@tonic-gate 		 * zero length message.
1190Sstevel@tonic-gate 		 */
1200Sstevel@tonic-gate 		dbp = NULL;
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if (dbp)
1240Sstevel@tonic-gate 		dbp->b_wptr += msgsz;		/* on behalf of the user */
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	/*
1270Sstevel@tonic-gate 	 * Okay, put the control part in
1280Sstevel@tonic-gate 	 */
1290Sstevel@tonic-gate gotdp:
1300Sstevel@tonic-gate 	msgsz = (int)TUNITDATAREQSZ;
131*8778SErik.Nordmark@Sun.COM 	/*
132*8778SErik.Nordmark@Sun.COM 	 * Usually sendto()s are performed with the credential of the caller;
133*8778SErik.Nordmark@Sun.COM 	 * in this particular case we specifically use the credential of
134*8778SErik.Nordmark@Sun.COM 	 * the opener as this call is typically done in the context of a user
135*8778SErik.Nordmark@Sun.COM 	 * process but on behalf of the kernel, e.g., a client connection
136*8778SErik.Nordmark@Sun.COM 	 * to a server which is later shared by different users.
137*8778SErik.Nordmark@Sun.COM 	 * At open time, we make sure to set fp->f_cred to kcred if such is
138*8778SErik.Nordmark@Sun.COM 	 * the case.
139*8778SErik.Nordmark@Sun.COM 	 *
140*8778SErik.Nordmark@Sun.COM 	 * Note: if the receiver uses SCM_UCRED/getpeerucred the pid will
141*8778SErik.Nordmark@Sun.COM 	 * appear as -1.
142*8778SErik.Nordmark@Sun.COM 	 */
143*8778SErik.Nordmark@Sun.COM 	while (!(bp = allocb_cred(msgsz + unitdata->addr.len +
144*8778SErik.Nordmark@Sun.COM 	    unitdata->opt.len, fp->f_cred, NOPID))) {
1450Sstevel@tonic-gate 		if (strwaitbuf(msgsz + unitdata->addr.len + unitdata->opt.len,
1460Sstevel@tonic-gate 		    BPRI_LO)) {
1470Sstevel@tonic-gate 			if (dbp && (dbp != unitdata->udata.udata_mp))
1480Sstevel@tonic-gate 				freeb(dbp);
1490Sstevel@tonic-gate 			return (ENOSR);
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/* LINTED pointer alignment */
1540Sstevel@tonic-gate 	udreq = (struct T_unitdata_req *)bp->b_wptr;
1550Sstevel@tonic-gate 	udreq->PRIM_type = T_UNITDATA_REQ;
1560Sstevel@tonic-gate 	udreq->DEST_length = unitdata->addr.len;
1570Sstevel@tonic-gate 	if (unitdata->addr.len) {
1580Sstevel@tonic-gate 		bcopy(unitdata->addr.buf, bp->b_wptr + msgsz,
1590Sstevel@tonic-gate 		    unitdata->addr.len);
1600Sstevel@tonic-gate 		udreq->DEST_offset = (t_scalar_t)msgsz;
1610Sstevel@tonic-gate 		msgsz += unitdata->addr.len;
1620Sstevel@tonic-gate 	} else
1630Sstevel@tonic-gate 		udreq->DEST_offset = 0;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	udreq->OPT_length = unitdata->opt.len;
1660Sstevel@tonic-gate 	if (unitdata->opt.len) {
1670Sstevel@tonic-gate 		bcopy(unitdata->opt.buf, bp->b_wptr + msgsz, unitdata->opt.len);
1680Sstevel@tonic-gate 		udreq->OPT_offset = (t_scalar_t)msgsz;
1690Sstevel@tonic-gate 		msgsz += unitdata->opt.len;
1700Sstevel@tonic-gate 	} else
1710Sstevel@tonic-gate 		udreq->OPT_offset = 0;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	bp->b_datap->db_type = M_PROTO;
1740Sstevel@tonic-gate 	bp->b_wptr += msgsz;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/*
1770Sstevel@tonic-gate 	 * link the two.
1780Sstevel@tonic-gate 	 */
1790Sstevel@tonic-gate 	linkb(bp, dbp);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * Put it to the transport provider.
1830Sstevel@tonic-gate 	 * tli_send() always consumes the message.
1840Sstevel@tonic-gate 	 */
1850Sstevel@tonic-gate 	flag = fp->f_flag;
1860Sstevel@tonic-gate 	error = tli_send(tiptr, bp, flag);
1870Sstevel@tonic-gate 	unitdata->udata.udata_mp = NULL;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	return (error);
1900Sstevel@tonic-gate }
191