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
5*de8c4a14SErik Nordmark  * Common Development and Distribution License (the "License").
6*de8c4a14SErik Nordmark  * 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  * TLI-like function to send datagrams over a specified
417c478bd9Sstevel@tonic-gate  * transport endpoint.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * Returns:
447c478bd9Sstevel@tonic-gate  * 	0 on success or positive error code.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <sys/param.h>
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <sys/user.h>
507c478bd9Sstevel@tonic-gate #include <sys/file.h>
517c478bd9Sstevel@tonic-gate #include <sys/errno.h>
527c478bd9Sstevel@tonic-gate #include <sys/stream.h>
537c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
547c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
557c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
567c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
577c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
587c478bd9Sstevel@tonic-gate #include <sys/timod.h>
597c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
607c478bd9Sstevel@tonic-gate #include <sys/t_kuser.h>
617c478bd9Sstevel@tonic-gate #include <sys/debug.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate int
t_ksndudata(TIUSER * tiptr,struct t_kunitdata * unitdata,frtn_t * frtn)657c478bd9Sstevel@tonic-gate t_ksndudata(TIUSER *tiptr, struct t_kunitdata *unitdata, frtn_t *frtn)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	int			msgsz;
687c478bd9Sstevel@tonic-gate 	file_t			*fp;
697c478bd9Sstevel@tonic-gate 	mblk_t			*bp;
707c478bd9Sstevel@tonic-gate 	mblk_t			*dbp;
717c478bd9Sstevel@tonic-gate 	struct T_unitdata_req	*udreq;
727c478bd9Sstevel@tonic-gate 	int			error;
737c478bd9Sstevel@tonic-gate 	int			flag;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	error = 0;
767c478bd9Sstevel@tonic-gate 	fp = tiptr->fp;
777c478bd9Sstevel@tonic-gate 	msgsz = unitdata->udata.len;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/*
807c478bd9Sstevel@tonic-gate 	 * See if Class 0 is required
817c478bd9Sstevel@tonic-gate 	 */
827c478bd9Sstevel@tonic-gate 	if (frtn != NULL) {
837c478bd9Sstevel@tonic-gate 		ASSERT(unitdata->udata.udata_mp == NULL);
847c478bd9Sstevel@tonic-gate 		ASSERT(unitdata->udata.buf != NULL);
857c478bd9Sstevel@tonic-gate 		/*
867c478bd9Sstevel@tonic-gate 		 * user has supplied their own buffer, all we have to
877c478bd9Sstevel@tonic-gate 		 * do is allocate a class 0 streams buffer and set it
887c478bd9Sstevel@tonic-gate 		 * up.
897c478bd9Sstevel@tonic-gate 		 */
907c478bd9Sstevel@tonic-gate 		if ((dbp = (mblk_t *)esballoc((uchar_t *)unitdata->udata.buf,
917c478bd9Sstevel@tonic-gate 		    (size_t)msgsz, BPRI_LO, frtn)) == NULL)
927c478bd9Sstevel@tonic-gate 			return (ENOSR);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 		dbp->b_datap->db_type = M_DATA;
957c478bd9Sstevel@tonic-gate 		KTLILOG(2, "t_ksndudata: bp %x, ", dbp);
967c478bd9Sstevel@tonic-gate 		KTLILOG(2, "len %d, ", msgsz);
977c478bd9Sstevel@tonic-gate 		KTLILOG(2, "free func %x\n", frtn->free_func);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	} else if (unitdata->udata.buf) {
1007c478bd9Sstevel@tonic-gate 		ASSERT(unitdata->udata.udata_mp == NULL);
1017c478bd9Sstevel@tonic-gate 		while (!(dbp = allocb(msgsz, BPRI_LO)))
1027c478bd9Sstevel@tonic-gate 			if (strwaitbuf((size_t)msgsz, BPRI_LO))
1037c478bd9Sstevel@tonic-gate 				return (ENOSR);
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		bcopy(unitdata->udata.buf, dbp->b_wptr, unitdata->udata.len);
1067c478bd9Sstevel@tonic-gate 		dbp->b_datap->db_type = M_DATA;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	} else if (unitdata->udata.udata_mp) {
1097c478bd9Sstevel@tonic-gate 		ASSERT(unitdata->udata.buf == NULL);
1107c478bd9Sstevel@tonic-gate 		/*
1117c478bd9Sstevel@tonic-gate 		 * user has done it all
1127c478bd9Sstevel@tonic-gate 		 */
1137c478bd9Sstevel@tonic-gate 		dbp = unitdata->udata.udata_mp;
1147c478bd9Sstevel@tonic-gate 		goto gotdp;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	} else {
1177c478bd9Sstevel@tonic-gate 		/*
1187c478bd9Sstevel@tonic-gate 		 * zero length message.
1197c478bd9Sstevel@tonic-gate 		 */
1207c478bd9Sstevel@tonic-gate 		dbp = NULL;
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (dbp)
1247c478bd9Sstevel@tonic-gate 		dbp->b_wptr += msgsz;		/* on behalf of the user */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/*
1277c478bd9Sstevel@tonic-gate 	 * Okay, put the control part in
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate gotdp:
1307c478bd9Sstevel@tonic-gate 	msgsz = (int)TUNITDATAREQSZ;
131*de8c4a14SErik Nordmark 	/*
132*de8c4a14SErik Nordmark 	 * Usually sendto()s are performed with the credential of the caller;
133*de8c4a14SErik Nordmark 	 * in this particular case we specifically use the credential of
134*de8c4a14SErik Nordmark 	 * the opener as this call is typically done in the context of a user
135*de8c4a14SErik Nordmark 	 * process but on behalf of the kernel, e.g., a client connection
136*de8c4a14SErik Nordmark 	 * to a server which is later shared by different users.
137*de8c4a14SErik Nordmark 	 * At open time, we make sure to set fp->f_cred to kcred if such is
138*de8c4a14SErik Nordmark 	 * the case.
139*de8c4a14SErik Nordmark 	 *
140*de8c4a14SErik Nordmark 	 * Note: if the receiver uses SCM_UCRED/getpeerucred the pid will
141*de8c4a14SErik Nordmark 	 * appear as -1.
142*de8c4a14SErik Nordmark 	 */
143*de8c4a14SErik Nordmark 	while (!(bp = allocb_cred(msgsz + unitdata->addr.len +
144*de8c4a14SErik Nordmark 	    unitdata->opt.len, fp->f_cred, NOPID))) {
1457c478bd9Sstevel@tonic-gate 		if (strwaitbuf(msgsz + unitdata->addr.len + unitdata->opt.len,
1467c478bd9Sstevel@tonic-gate 		    BPRI_LO)) {
1477c478bd9Sstevel@tonic-gate 			if (dbp && (dbp != unitdata->udata.udata_mp))
1487c478bd9Sstevel@tonic-gate 				freeb(dbp);
1497c478bd9Sstevel@tonic-gate 			return (ENOSR);
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
1547c478bd9Sstevel@tonic-gate 	udreq = (struct T_unitdata_req *)bp->b_wptr;
1557c478bd9Sstevel@tonic-gate 	udreq->PRIM_type = T_UNITDATA_REQ;
1567c478bd9Sstevel@tonic-gate 	udreq->DEST_length = unitdata->addr.len;
1577c478bd9Sstevel@tonic-gate 	if (unitdata->addr.len) {
1587c478bd9Sstevel@tonic-gate 		bcopy(unitdata->addr.buf, bp->b_wptr + msgsz,
1597c478bd9Sstevel@tonic-gate 		    unitdata->addr.len);
1607c478bd9Sstevel@tonic-gate 		udreq->DEST_offset = (t_scalar_t)msgsz;
1617c478bd9Sstevel@tonic-gate 		msgsz += unitdata->addr.len;
1627c478bd9Sstevel@tonic-gate 	} else
1637c478bd9Sstevel@tonic-gate 		udreq->DEST_offset = 0;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	udreq->OPT_length = unitdata->opt.len;
1667c478bd9Sstevel@tonic-gate 	if (unitdata->opt.len) {
1677c478bd9Sstevel@tonic-gate 		bcopy(unitdata->opt.buf, bp->b_wptr + msgsz, unitdata->opt.len);
1687c478bd9Sstevel@tonic-gate 		udreq->OPT_offset = (t_scalar_t)msgsz;
1697c478bd9Sstevel@tonic-gate 		msgsz += unitdata->opt.len;
1707c478bd9Sstevel@tonic-gate 	} else
1717c478bd9Sstevel@tonic-gate 		udreq->OPT_offset = 0;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	bp->b_datap->db_type = M_PROTO;
1747c478bd9Sstevel@tonic-gate 	bp->b_wptr += msgsz;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * link the two.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	linkb(bp, dbp);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * Put it to the transport provider.
1837c478bd9Sstevel@tonic-gate 	 * tli_send() always consumes the message.
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 	flag = fp->f_flag;
1867c478bd9Sstevel@tonic-gate 	error = tli_send(tiptr, bp, flag);
1877c478bd9Sstevel@tonic-gate 	unitdata->udata.udata_mp = NULL;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	return (error);
1907c478bd9Sstevel@tonic-gate }
191