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 /*
277c478bd9Sstevel@tonic-gate * Solaris DDI STREAMS utility routines (PSARC/2003/648).
287c478bd9Sstevel@tonic-gate *
297c478bd9Sstevel@tonic-gate * Please see the appropriate section 9F manpage for documentation.
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/stream.h>
367c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
37ff550d0eSmasputra #include <sys/strsubr.h>
387c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
39ff550d0eSmasputra #include <sys/sysmacros.h>
407c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate void
merror(queue_t * wq,mblk_t * mp,int error)437c478bd9Sstevel@tonic-gate merror(queue_t *wq, mblk_t *mp, int error)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate if ((mp = mexchange(wq, mp, 1, M_ERROR, -1)) == NULL)
467c478bd9Sstevel@tonic-gate return;
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate *mp->b_rptr = (uchar_t)error;
497c478bd9Sstevel@tonic-gate qreply(wq, mp);
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate void
mioc2ack(mblk_t * mp,mblk_t * dp,size_t count,int rval)537c478bd9Sstevel@tonic-gate mioc2ack(mblk_t *mp, mblk_t *dp, size_t count, int rval)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
567c478bd9Sstevel@tonic-gate mblk_t *odp = mp->b_cont; /* allows freemsg() to be a tail call */
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate DB_TYPE(mp) = M_IOCACK;
597c478bd9Sstevel@tonic-gate iocp->ioc_count = count;
607c478bd9Sstevel@tonic-gate iocp->ioc_error = 0;
617c478bd9Sstevel@tonic-gate iocp->ioc_rval = rval;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate mp->b_cont = dp;
647c478bd9Sstevel@tonic-gate if (dp != NULL)
657c478bd9Sstevel@tonic-gate dp->b_wptr = dp->b_rptr + count;
667c478bd9Sstevel@tonic-gate freemsg(odp);
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate void
miocack(queue_t * wq,mblk_t * mp,int count,int rval)707c478bd9Sstevel@tonic-gate miocack(queue_t *wq, mblk_t *mp, int count, int rval)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate DB_TYPE(mp) = M_IOCACK;
757c478bd9Sstevel@tonic-gate iocp->ioc_count = count;
767c478bd9Sstevel@tonic-gate iocp->ioc_error = 0;
777c478bd9Sstevel@tonic-gate iocp->ioc_rval = rval;
787c478bd9Sstevel@tonic-gate qreply(wq, mp);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate void
miocnak(queue_t * wq,mblk_t * mp,int count,int error)827c478bd9Sstevel@tonic-gate miocnak(queue_t *wq, mblk_t *mp, int count, int error)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate DB_TYPE(mp) = M_IOCNAK;
877c478bd9Sstevel@tonic-gate iocp->ioc_count = count;
887c478bd9Sstevel@tonic-gate iocp->ioc_error = error;
897c478bd9Sstevel@tonic-gate qreply(wq, mp);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate mblk_t *
mexchange(queue_t * wq,mblk_t * mp,size_t size,uchar_t type,int32_t primtype)937c478bd9Sstevel@tonic-gate mexchange(queue_t *wq, mblk_t *mp, size_t size, uchar_t type, int32_t primtype)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate if (mp == NULL || MBLKSIZE(mp) < size || DB_REF(mp) > 1) {
967c478bd9Sstevel@tonic-gate freemsg(mp);
977c478bd9Sstevel@tonic-gate if ((mp = allocb(size, BPRI_LO)) == NULL) {
987c478bd9Sstevel@tonic-gate if (wq != NULL) {
997c478bd9Sstevel@tonic-gate if ((mp = allocb(1, BPRI_HI)) != NULL)
1007c478bd9Sstevel@tonic-gate merror(wq, mp, ENOSR);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate return (NULL);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate DB_TYPE(mp) = type;
1077c478bd9Sstevel@tonic-gate mp->b_rptr = DB_BASE(mp);
1087c478bd9Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + size;
1097c478bd9Sstevel@tonic-gate if (primtype >= 0)
1107c478bd9Sstevel@tonic-gate *(int32_t *)mp->b_rptr = primtype;
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate return (mp);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate size_t
msgsize(mblk_t * mp)1167c478bd9Sstevel@tonic-gate msgsize(mblk_t *mp)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate size_t n = 0;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate for (; mp != NULL; mp = mp->b_cont)
1217c478bd9Sstevel@tonic-gate n += MBLKL(mp);
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate return (n);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate void
mcopymsg(mblk_t * mp,void * bufp)1277c478bd9Sstevel@tonic-gate mcopymsg(mblk_t *mp, void *bufp)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate caddr_t dest = bufp;
1307c478bd9Sstevel@tonic-gate mblk_t *bp;
1317c478bd9Sstevel@tonic-gate size_t n;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate for (bp = mp; bp != NULL; bp = bp->b_cont) {
1347c478bd9Sstevel@tonic-gate n = MBLKL(bp);
1357c478bd9Sstevel@tonic-gate bcopy(bp->b_rptr, dest, n);
1367c478bd9Sstevel@tonic-gate dest += n;
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate freemsg(mp);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate void
mcopyin(mblk_t * mp,void * private,size_t size,void * useraddr)1437c478bd9Sstevel@tonic-gate mcopyin(mblk_t *mp, void *private, size_t size, void *useraddr)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate struct copyreq *cp = (struct copyreq *)mp->b_rptr;
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate if (useraddr != NULL) {
1487c478bd9Sstevel@tonic-gate cp->cq_addr = (caddr_t)useraddr;
1497c478bd9Sstevel@tonic-gate } else {
1507c478bd9Sstevel@tonic-gate ASSERT(DB_TYPE(mp) == M_IOCTL);
1517c478bd9Sstevel@tonic-gate ASSERT(mp->b_cont != NULL);
1527c478bd9Sstevel@tonic-gate ASSERT(((struct iocblk *)mp->b_rptr)->ioc_count == TRANSPARENT);
1537c478bd9Sstevel@tonic-gate cp->cq_addr = (caddr_t)*(uintptr_t *)mp->b_cont->b_rptr;
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate cp->cq_flag = 0;
1577c478bd9Sstevel@tonic-gate cp->cq_size = size;
1587c478bd9Sstevel@tonic-gate cp->cq_private = (mblk_t *)private;
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate DB_TYPE(mp) = M_COPYIN;
1617c478bd9Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (struct copyreq);
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate if (mp->b_cont != NULL) {
1647c478bd9Sstevel@tonic-gate freemsg(mp->b_cont);
1657c478bd9Sstevel@tonic-gate mp->b_cont = NULL;
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate void
mcopyout(mblk_t * mp,void * private,size_t size,void * useraddr,mblk_t * dp)1707c478bd9Sstevel@tonic-gate mcopyout(mblk_t *mp, void *private, size_t size, void *useraddr, mblk_t *dp)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate struct copyreq *cp = (struct copyreq *)mp->b_rptr;
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate if (useraddr != NULL)
1757c478bd9Sstevel@tonic-gate cp->cq_addr = (caddr_t)useraddr;
1767c478bd9Sstevel@tonic-gate else {
1777c478bd9Sstevel@tonic-gate ASSERT(DB_TYPE(mp) == M_IOCTL);
1787c478bd9Sstevel@tonic-gate ASSERT(mp->b_cont != NULL);
1797c478bd9Sstevel@tonic-gate ASSERT(((struct iocblk *)mp->b_rptr)->ioc_count == TRANSPARENT);
1807c478bd9Sstevel@tonic-gate cp->cq_addr = (caddr_t)*(uintptr_t *)mp->b_cont->b_rptr;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate cp->cq_flag = 0;
1847c478bd9Sstevel@tonic-gate cp->cq_size = size;
1857c478bd9Sstevel@tonic-gate cp->cq_private = (mblk_t *)private;
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate DB_TYPE(mp) = M_COPYOUT;
1887c478bd9Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (struct copyreq);
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate if (dp != NULL) {
1917c478bd9Sstevel@tonic-gate if (mp->b_cont != NULL)
1927c478bd9Sstevel@tonic-gate freemsg(mp->b_cont);
1937c478bd9Sstevel@tonic-gate mp->b_cont = dp;
1947c478bd9Sstevel@tonic-gate mp->b_cont->b_wptr = mp->b_cont->b_rptr + size;
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate int
miocpullup(mblk_t * iocmp,size_t size)1997c478bd9Sstevel@tonic-gate miocpullup(mblk_t *iocmp, size_t size)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate struct iocblk *iocp = (struct iocblk *)iocmp->b_rptr;
2027c478bd9Sstevel@tonic-gate mblk_t *datamp = iocmp->b_cont;
2037c478bd9Sstevel@tonic-gate mblk_t *newdatamp;
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate * We'd like to be sure that DB_TYPE(iocmp) == M_IOCTL, but some
2077c478bd9Sstevel@tonic-gate * nitwit routines like ttycommon_ioctl() always reset the type of
2087c478bd9Sstevel@tonic-gate * legitimate M_IOCTL messages to M_IOCACK as a "courtesy" to the
2097c478bd9Sstevel@tonic-gate * caller, even when the routine does not understand the M_IOCTL.
2107c478bd9Sstevel@tonic-gate * The ttycommon_ioctl() routine does us the additional favor of
2117c478bd9Sstevel@tonic-gate * clearing ioc_count, so we cannot rely on it having a correct
2127c478bd9Sstevel@tonic-gate * size either (blissfully, ttycommon_ioctl() does not screw with
2137c478bd9Sstevel@tonic-gate * TRANSPARENT messages, so we can still sanity check for that).
2147c478bd9Sstevel@tonic-gate */
2157c478bd9Sstevel@tonic-gate ASSERT(MBLKL(iocmp) == sizeof (struct iocblk));
2167c478bd9Sstevel@tonic-gate if (MBLKL(iocmp) != sizeof (struct iocblk)) {
2177c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "miocpullup: passed mblk_t %p is not an ioctl"
2187c478bd9Sstevel@tonic-gate " mblk_t", (void *)iocmp);
2197c478bd9Sstevel@tonic-gate return (EINVAL);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate if (iocp->ioc_count == TRANSPARENT)
2237c478bd9Sstevel@tonic-gate return (EINVAL);
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate if (size == 0)
2267c478bd9Sstevel@tonic-gate return (0);
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate if (datamp == NULL)
2297c478bd9Sstevel@tonic-gate return (EINVAL);
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate if (MBLKL(datamp) >= size)
2327c478bd9Sstevel@tonic-gate return (0);
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate newdatamp = msgpullup(datamp, size);
2357c478bd9Sstevel@tonic-gate if (newdatamp == NULL) {
2367c478bd9Sstevel@tonic-gate if (msgdsize(datamp) < size)
2377c478bd9Sstevel@tonic-gate return (EINVAL);
2387c478bd9Sstevel@tonic-gate return (ENOMEM);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate iocmp->b_cont = newdatamp;
2427c478bd9Sstevel@tonic-gate freemsg(datamp);
2437c478bd9Sstevel@tonic-gate return (0);
2447c478bd9Sstevel@tonic-gate }
245ff550d0eSmasputra
246ff550d0eSmasputra /* Copy userdata into a new mblk_t */
247ff550d0eSmasputra mblk_t *
mcopyinuio(struct stdata * stp,uio_t * uiop,ssize_t iosize,ssize_t maxblk,int * errorp)248ff550d0eSmasputra mcopyinuio(struct stdata *stp, uio_t *uiop, ssize_t iosize,
249ff550d0eSmasputra ssize_t maxblk, int *errorp)
250ff550d0eSmasputra {
251ff550d0eSmasputra mblk_t *head = NULL, **tail = &head;
252ff550d0eSmasputra size_t offset = stp->sd_wroff;
253c28749e9Skais size_t tail_len = stp->sd_tail;
254ff550d0eSmasputra
255ff550d0eSmasputra if (iosize == INFPSZ || iosize > uiop->uio_resid)
256ff550d0eSmasputra iosize = uiop->uio_resid;
257ff550d0eSmasputra
258ff550d0eSmasputra if (maxblk == INFPSZ)
259ff550d0eSmasputra maxblk = iosize;
260ff550d0eSmasputra
261ff550d0eSmasputra /* Nothing to do in these cases, so we're done */
262ff550d0eSmasputra if (iosize < 0 || maxblk < 0 || (maxblk == 0 && iosize > 0))
263ff550d0eSmasputra goto done;
264ff550d0eSmasputra
265ff550d0eSmasputra if (stp->sd_flag & STRCOPYCACHED)
266ff550d0eSmasputra uiop->uio_extflg |= UIO_COPY_CACHED;
267ff550d0eSmasputra
268ff550d0eSmasputra /*
269ff550d0eSmasputra * We will enter the loop below if iosize is 0; it will allocate an
270ff550d0eSmasputra * empty message block and call uiomove(9F) which will just return.
271ff550d0eSmasputra * We could avoid that with an extra check but would only slow
272ff550d0eSmasputra * down the much more likely case where iosize is larger than 0.
273ff550d0eSmasputra */
274ff550d0eSmasputra do {
275ff550d0eSmasputra ssize_t blocksize;
276ff550d0eSmasputra mblk_t *mp;
277ff550d0eSmasputra
278ff550d0eSmasputra blocksize = MIN(iosize, maxblk);
279ff550d0eSmasputra ASSERT(blocksize >= 0);
280c28749e9Skais if ((mp = allocb_cred(offset + blocksize + tail_len,
281*de8c4a14SErik Nordmark CRED(), curproc->p_pid)) == NULL) {
282ff550d0eSmasputra *errorp = ENOMEM;
283ff550d0eSmasputra return (head);
284ff550d0eSmasputra }
285ff550d0eSmasputra mp->b_rptr += offset;
286ff550d0eSmasputra mp->b_wptr = mp->b_rptr + blocksize;
287ff550d0eSmasputra
288ff550d0eSmasputra *tail = mp;
289ff550d0eSmasputra tail = &mp->b_cont;
290ff550d0eSmasputra
291ff550d0eSmasputra /* uiomove(9F) either returns 0 or EFAULT */
292ff550d0eSmasputra if ((*errorp = uiomove(mp->b_rptr, (size_t)blocksize,
293ff550d0eSmasputra UIO_WRITE, uiop)) != 0) {
294ff550d0eSmasputra ASSERT(*errorp != ENOMEM);
295ff550d0eSmasputra freemsg(head);
296ff550d0eSmasputra return (NULL);
297ff550d0eSmasputra }
298ff550d0eSmasputra
299ff550d0eSmasputra iosize -= blocksize;
300ff550d0eSmasputra } while (iosize > 0);
301ff550d0eSmasputra
302ff550d0eSmasputra done:
303ff550d0eSmasputra *errorp = 0;
304ff550d0eSmasputra return (head);
305ff550d0eSmasputra }
306