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 /*
270Sstevel@tonic-gate * Solaris DDI STREAMS utility routines (PSARC/2003/648).
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * Please see the appropriate section 9F manpage for documentation.
300Sstevel@tonic-gate */
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/errno.h>
350Sstevel@tonic-gate #include <sys/stream.h>
360Sstevel@tonic-gate #include <sys/stropts.h>
37741Smasputra #include <sys/strsubr.h>
380Sstevel@tonic-gate #include <sys/strsun.h>
39741Smasputra #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include <sys/cmn_err.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate void
merror(queue_t * wq,mblk_t * mp,int error)430Sstevel@tonic-gate merror(queue_t *wq, mblk_t *mp, int error)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate if ((mp = mexchange(wq, mp, 1, M_ERROR, -1)) == NULL)
460Sstevel@tonic-gate return;
470Sstevel@tonic-gate
480Sstevel@tonic-gate *mp->b_rptr = (uchar_t)error;
490Sstevel@tonic-gate qreply(wq, mp);
500Sstevel@tonic-gate }
510Sstevel@tonic-gate
520Sstevel@tonic-gate void
mioc2ack(mblk_t * mp,mblk_t * dp,size_t count,int rval)530Sstevel@tonic-gate mioc2ack(mblk_t *mp, mblk_t *dp, size_t count, int rval)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
560Sstevel@tonic-gate mblk_t *odp = mp->b_cont; /* allows freemsg() to be a tail call */
570Sstevel@tonic-gate
580Sstevel@tonic-gate DB_TYPE(mp) = M_IOCACK;
590Sstevel@tonic-gate iocp->ioc_count = count;
600Sstevel@tonic-gate iocp->ioc_error = 0;
610Sstevel@tonic-gate iocp->ioc_rval = rval;
620Sstevel@tonic-gate
630Sstevel@tonic-gate mp->b_cont = dp;
640Sstevel@tonic-gate if (dp != NULL)
650Sstevel@tonic-gate dp->b_wptr = dp->b_rptr + count;
660Sstevel@tonic-gate freemsg(odp);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate
690Sstevel@tonic-gate void
miocack(queue_t * wq,mblk_t * mp,int count,int rval)700Sstevel@tonic-gate miocack(queue_t *wq, mblk_t *mp, int count, int rval)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
730Sstevel@tonic-gate
740Sstevel@tonic-gate DB_TYPE(mp) = M_IOCACK;
750Sstevel@tonic-gate iocp->ioc_count = count;
760Sstevel@tonic-gate iocp->ioc_error = 0;
770Sstevel@tonic-gate iocp->ioc_rval = rval;
780Sstevel@tonic-gate qreply(wq, mp);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
810Sstevel@tonic-gate void
miocnak(queue_t * wq,mblk_t * mp,int count,int error)820Sstevel@tonic-gate miocnak(queue_t *wq, mblk_t *mp, int count, int error)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
850Sstevel@tonic-gate
860Sstevel@tonic-gate DB_TYPE(mp) = M_IOCNAK;
870Sstevel@tonic-gate iocp->ioc_count = count;
880Sstevel@tonic-gate iocp->ioc_error = error;
890Sstevel@tonic-gate qreply(wq, mp);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
920Sstevel@tonic-gate mblk_t *
mexchange(queue_t * wq,mblk_t * mp,size_t size,uchar_t type,int32_t primtype)930Sstevel@tonic-gate mexchange(queue_t *wq, mblk_t *mp, size_t size, uchar_t type, int32_t primtype)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate if (mp == NULL || MBLKSIZE(mp) < size || DB_REF(mp) > 1) {
960Sstevel@tonic-gate freemsg(mp);
970Sstevel@tonic-gate if ((mp = allocb(size, BPRI_LO)) == NULL) {
980Sstevel@tonic-gate if (wq != NULL) {
990Sstevel@tonic-gate if ((mp = allocb(1, BPRI_HI)) != NULL)
1000Sstevel@tonic-gate merror(wq, mp, ENOSR);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate return (NULL);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate DB_TYPE(mp) = type;
1070Sstevel@tonic-gate mp->b_rptr = DB_BASE(mp);
1080Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + size;
1090Sstevel@tonic-gate if (primtype >= 0)
1100Sstevel@tonic-gate *(int32_t *)mp->b_rptr = primtype;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate return (mp);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate size_t
msgsize(mblk_t * mp)1160Sstevel@tonic-gate msgsize(mblk_t *mp)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate size_t n = 0;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate for (; mp != NULL; mp = mp->b_cont)
1210Sstevel@tonic-gate n += MBLKL(mp);
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate return (n);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate void
mcopymsg(mblk_t * mp,void * bufp)1270Sstevel@tonic-gate mcopymsg(mblk_t *mp, void *bufp)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate caddr_t dest = bufp;
1300Sstevel@tonic-gate mblk_t *bp;
1310Sstevel@tonic-gate size_t n;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate for (bp = mp; bp != NULL; bp = bp->b_cont) {
1340Sstevel@tonic-gate n = MBLKL(bp);
1350Sstevel@tonic-gate bcopy(bp->b_rptr, dest, n);
1360Sstevel@tonic-gate dest += n;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate freemsg(mp);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate void
mcopyin(mblk_t * mp,void * private,size_t size,void * useraddr)1430Sstevel@tonic-gate mcopyin(mblk_t *mp, void *private, size_t size, void *useraddr)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate struct copyreq *cp = (struct copyreq *)mp->b_rptr;
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate if (useraddr != NULL) {
1480Sstevel@tonic-gate cp->cq_addr = (caddr_t)useraddr;
1490Sstevel@tonic-gate } else {
1500Sstevel@tonic-gate ASSERT(DB_TYPE(mp) == M_IOCTL);
1510Sstevel@tonic-gate ASSERT(mp->b_cont != NULL);
1520Sstevel@tonic-gate ASSERT(((struct iocblk *)mp->b_rptr)->ioc_count == TRANSPARENT);
1530Sstevel@tonic-gate cp->cq_addr = (caddr_t)*(uintptr_t *)mp->b_cont->b_rptr;
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate cp->cq_flag = 0;
1570Sstevel@tonic-gate cp->cq_size = size;
1580Sstevel@tonic-gate cp->cq_private = (mblk_t *)private;
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate DB_TYPE(mp) = M_COPYIN;
1610Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (struct copyreq);
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate if (mp->b_cont != NULL) {
1640Sstevel@tonic-gate freemsg(mp->b_cont);
1650Sstevel@tonic-gate mp->b_cont = NULL;
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate void
mcopyout(mblk_t * mp,void * private,size_t size,void * useraddr,mblk_t * dp)1700Sstevel@tonic-gate mcopyout(mblk_t *mp, void *private, size_t size, void *useraddr, mblk_t *dp)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate struct copyreq *cp = (struct copyreq *)mp->b_rptr;
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate if (useraddr != NULL)
1750Sstevel@tonic-gate cp->cq_addr = (caddr_t)useraddr;
1760Sstevel@tonic-gate else {
1770Sstevel@tonic-gate ASSERT(DB_TYPE(mp) == M_IOCTL);
1780Sstevel@tonic-gate ASSERT(mp->b_cont != NULL);
1790Sstevel@tonic-gate ASSERT(((struct iocblk *)mp->b_rptr)->ioc_count == TRANSPARENT);
1800Sstevel@tonic-gate cp->cq_addr = (caddr_t)*(uintptr_t *)mp->b_cont->b_rptr;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate cp->cq_flag = 0;
1840Sstevel@tonic-gate cp->cq_size = size;
1850Sstevel@tonic-gate cp->cq_private = (mblk_t *)private;
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate DB_TYPE(mp) = M_COPYOUT;
1880Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (struct copyreq);
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate if (dp != NULL) {
1910Sstevel@tonic-gate if (mp->b_cont != NULL)
1920Sstevel@tonic-gate freemsg(mp->b_cont);
1930Sstevel@tonic-gate mp->b_cont = dp;
1940Sstevel@tonic-gate mp->b_cont->b_wptr = mp->b_cont->b_rptr + size;
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate int
miocpullup(mblk_t * iocmp,size_t size)1990Sstevel@tonic-gate miocpullup(mblk_t *iocmp, size_t size)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate struct iocblk *iocp = (struct iocblk *)iocmp->b_rptr;
2020Sstevel@tonic-gate mblk_t *datamp = iocmp->b_cont;
2030Sstevel@tonic-gate mblk_t *newdatamp;
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate * We'd like to be sure that DB_TYPE(iocmp) == M_IOCTL, but some
2070Sstevel@tonic-gate * nitwit routines like ttycommon_ioctl() always reset the type of
2080Sstevel@tonic-gate * legitimate M_IOCTL messages to M_IOCACK as a "courtesy" to the
2090Sstevel@tonic-gate * caller, even when the routine does not understand the M_IOCTL.
2100Sstevel@tonic-gate * The ttycommon_ioctl() routine does us the additional favor of
2110Sstevel@tonic-gate * clearing ioc_count, so we cannot rely on it having a correct
2120Sstevel@tonic-gate * size either (blissfully, ttycommon_ioctl() does not screw with
2130Sstevel@tonic-gate * TRANSPARENT messages, so we can still sanity check for that).
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate ASSERT(MBLKL(iocmp) == sizeof (struct iocblk));
2160Sstevel@tonic-gate if (MBLKL(iocmp) != sizeof (struct iocblk)) {
2170Sstevel@tonic-gate cmn_err(CE_WARN, "miocpullup: passed mblk_t %p is not an ioctl"
2180Sstevel@tonic-gate " mblk_t", (void *)iocmp);
2190Sstevel@tonic-gate return (EINVAL);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate if (iocp->ioc_count == TRANSPARENT)
2230Sstevel@tonic-gate return (EINVAL);
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate if (size == 0)
2260Sstevel@tonic-gate return (0);
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate if (datamp == NULL)
2290Sstevel@tonic-gate return (EINVAL);
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate if (MBLKL(datamp) >= size)
2320Sstevel@tonic-gate return (0);
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate newdatamp = msgpullup(datamp, size);
2350Sstevel@tonic-gate if (newdatamp == NULL) {
2360Sstevel@tonic-gate if (msgdsize(datamp) < size)
2370Sstevel@tonic-gate return (EINVAL);
2380Sstevel@tonic-gate return (ENOMEM);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate iocmp->b_cont = newdatamp;
2420Sstevel@tonic-gate freemsg(datamp);
2430Sstevel@tonic-gate return (0);
2440Sstevel@tonic-gate }
245741Smasputra
246741Smasputra /* Copy userdata into a new mblk_t */
247741Smasputra mblk_t *
mcopyinuio(struct stdata * stp,uio_t * uiop,ssize_t iosize,ssize_t maxblk,int * errorp)248741Smasputra mcopyinuio(struct stdata *stp, uio_t *uiop, ssize_t iosize,
249741Smasputra ssize_t maxblk, int *errorp)
250741Smasputra {
251741Smasputra mblk_t *head = NULL, **tail = &head;
252741Smasputra size_t offset = stp->sd_wroff;
253898Skais size_t tail_len = stp->sd_tail;
254741Smasputra
255741Smasputra if (iosize == INFPSZ || iosize > uiop->uio_resid)
256741Smasputra iosize = uiop->uio_resid;
257741Smasputra
258741Smasputra if (maxblk == INFPSZ)
259741Smasputra maxblk = iosize;
260741Smasputra
261741Smasputra /* Nothing to do in these cases, so we're done */
262741Smasputra if (iosize < 0 || maxblk < 0 || (maxblk == 0 && iosize > 0))
263741Smasputra goto done;
264741Smasputra
265741Smasputra if (stp->sd_flag & STRCOPYCACHED)
266741Smasputra uiop->uio_extflg |= UIO_COPY_CACHED;
267741Smasputra
268741Smasputra /*
269741Smasputra * We will enter the loop below if iosize is 0; it will allocate an
270741Smasputra * empty message block and call uiomove(9F) which will just return.
271741Smasputra * We could avoid that with an extra check but would only slow
272741Smasputra * down the much more likely case where iosize is larger than 0.
273741Smasputra */
274741Smasputra do {
275741Smasputra ssize_t blocksize;
276741Smasputra mblk_t *mp;
277741Smasputra
278741Smasputra blocksize = MIN(iosize, maxblk);
279741Smasputra ASSERT(blocksize >= 0);
280898Skais if ((mp = allocb_cred(offset + blocksize + tail_len,
281*8778SErik.Nordmark@Sun.COM CRED(), curproc->p_pid)) == NULL) {
282741Smasputra *errorp = ENOMEM;
283741Smasputra return (head);
284741Smasputra }
285741Smasputra mp->b_rptr += offset;
286741Smasputra mp->b_wptr = mp->b_rptr + blocksize;
287741Smasputra
288741Smasputra *tail = mp;
289741Smasputra tail = &mp->b_cont;
290741Smasputra
291741Smasputra /* uiomove(9F) either returns 0 or EFAULT */
292741Smasputra if ((*errorp = uiomove(mp->b_rptr, (size_t)blocksize,
293741Smasputra UIO_WRITE, uiop)) != 0) {
294741Smasputra ASSERT(*errorp != ENOMEM);
295741Smasputra freemsg(head);
296741Smasputra return (NULL);
297741Smasputra }
298741Smasputra
299741Smasputra iosize -= blocksize;
300741Smasputra } while (iosize > 0);
301741Smasputra
302741Smasputra done:
303741Smasputra *errorp = 0;
304741Smasputra return (head);
305741Smasputra }
306