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