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
522eb7cb5Sgd78059 * Common Development and Distribution License (the "License").
622eb7cb5Sgd78059 * 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*09b0d01cSGary Mills * Copyright 2014 Gary Mills
2322eb7cb5Sgd78059 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate * The Regents of the University of California
337c478bd9Sstevel@tonic-gate * All Rights Reserved
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate * contributors.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate * Kernel TLI-like function to read a datagram off of a
427c478bd9Sstevel@tonic-gate * transport endpoints stream head.
437c478bd9Sstevel@tonic-gate *
447c478bd9Sstevel@tonic-gate * Returns:
457c478bd9Sstevel@tonic-gate * 0 On success or positive error code.
467c478bd9Sstevel@tonic-gate * On sucess, type is set to:
477c478bd9Sstevel@tonic-gate * T_DATA If normal data has been received
487c478bd9Sstevel@tonic-gate * T_UDERR If an error indication has been received,
497c478bd9Sstevel@tonic-gate * in which case uderr contains the unitdata
507c478bd9Sstevel@tonic-gate * error number.
517c478bd9Sstevel@tonic-gate */
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #include <sys/param.h>
547c478bd9Sstevel@tonic-gate #include <sys/types.h>
557c478bd9Sstevel@tonic-gate #include <sys/user.h>
567c478bd9Sstevel@tonic-gate #include <sys/file.h>
577c478bd9Sstevel@tonic-gate #include <sys/errno.h>
587c478bd9Sstevel@tonic-gate #include <sys/stream.h>
597c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
607c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
617c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
627c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
637c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
647c478bd9Sstevel@tonic-gate #include <sys/timod.h>
657c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
667c478bd9Sstevel@tonic-gate #include <sys/t_kuser.h>
677c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
6822eb7cb5Sgd78059 #include <sys/strsun.h>
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate int
t_krcvudata(TIUSER * tiptr,struct t_kunitdata * unitdata,int * type,int * uderr)727c478bd9Sstevel@tonic-gate t_krcvudata(TIUSER *tiptr, struct t_kunitdata *unitdata, int *type, int *uderr)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate int len;
757c478bd9Sstevel@tonic-gate size_t hdrsz;
767c478bd9Sstevel@tonic-gate union T_primitives *pptr;
777c478bd9Sstevel@tonic-gate struct file *fp;
787c478bd9Sstevel@tonic-gate mblk_t *bp;
797c478bd9Sstevel@tonic-gate mblk_t *nbp;
807c478bd9Sstevel@tonic-gate mblk_t *mp;
817c478bd9Sstevel@tonic-gate mblk_t *tmp;
827c478bd9Sstevel@tonic-gate int error;
837c478bd9Sstevel@tonic-gate int flag;
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate fp = tiptr->fp;
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate if (type == NULL || uderr == NULL)
887c478bd9Sstevel@tonic-gate return (EINVAL);
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate error = 0;
917c478bd9Sstevel@tonic-gate unitdata->udata.buf = NULL;
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate if (unitdata->udata.udata_mp) {
947c478bd9Sstevel@tonic-gate KTLILOG(2, "t_krcvudata: freeing existing message block\n", 0);
957c478bd9Sstevel@tonic-gate freemsg(unitdata->udata.udata_mp);
967c478bd9Sstevel@tonic-gate unitdata->udata.udata_mp = NULL;
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate * XXX Grabbing a mutex to do an atomic operation seems pointless
1017c478bd9Sstevel@tonic-gate */
1027c478bd9Sstevel@tonic-gate mutex_enter(&fp->f_tlock);
1037c478bd9Sstevel@tonic-gate flag = fp->f_flag;
1047c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock);
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate if ((error = tli_recv(tiptr, &bp, flag)) != 0)
1077c478bd9Sstevel@tonic-gate return (error);
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * Got something
1117c478bd9Sstevel@tonic-gate */
1127c478bd9Sstevel@tonic-gate switch (bp->b_datap->db_type) {
1137c478bd9Sstevel@tonic-gate case M_PROTO:
1147c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
1157c478bd9Sstevel@tonic-gate pptr = (union T_primitives *)bp->b_rptr;
1167c478bd9Sstevel@tonic-gate switch (pptr->type) {
1177c478bd9Sstevel@tonic-gate case T_UNITDATA_IND:
1187c478bd9Sstevel@tonic-gate KTLILOG(2, "t_krcvudata: Got T_UNITDATA_IND\n", 0);
11922eb7cb5Sgd78059 hdrsz = MBLKL(bp);
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate * check everything for consistency
1237c478bd9Sstevel@tonic-gate */
1247c478bd9Sstevel@tonic-gate if (hdrsz < TUNITDATAINDSZ ||
1257c478bd9Sstevel@tonic-gate hdrsz < (pptr->unitdata_ind.OPT_length +
1267c478bd9Sstevel@tonic-gate pptr->unitdata_ind.OPT_offset) ||
1277c478bd9Sstevel@tonic-gate hdrsz < (pptr->unitdata_ind.SRC_length +
1287c478bd9Sstevel@tonic-gate pptr->unitdata_ind.SRC_offset)) {
1297c478bd9Sstevel@tonic-gate error = EPROTO;
1307c478bd9Sstevel@tonic-gate freemsg(bp);
1317c478bd9Sstevel@tonic-gate break;
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate * okay, so now we copy them
1367c478bd9Sstevel@tonic-gate */
1377c478bd9Sstevel@tonic-gate len = MIN(pptr->unitdata_ind.SRC_length,
1387c478bd9Sstevel@tonic-gate unitdata->addr.maxlen);
1397c478bd9Sstevel@tonic-gate bcopy(bp->b_rptr + pptr->unitdata_ind.SRC_offset,
1407c478bd9Sstevel@tonic-gate unitdata->addr.buf, len);
1417c478bd9Sstevel@tonic-gate unitdata->addr.len = len;
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate len = MIN(pptr->unitdata_ind.OPT_length,
1447c478bd9Sstevel@tonic-gate unitdata->opt.maxlen);
1457c478bd9Sstevel@tonic-gate bcopy(bp->b_rptr + pptr->unitdata_ind.OPT_offset,
1467c478bd9Sstevel@tonic-gate unitdata->opt.buf, len);
1477c478bd9Sstevel@tonic-gate unitdata->opt.len = len;
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate bp->b_rptr += hdrsz;
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate * we assume that the client knows how to deal
1537c478bd9Sstevel@tonic-gate * with a set of linked mblks, so all we do is
1547c478bd9Sstevel@tonic-gate * make a pass and remove any that are zero
1557c478bd9Sstevel@tonic-gate * length.
1567c478bd9Sstevel@tonic-gate */
1577c478bd9Sstevel@tonic-gate nbp = NULL;
1587c478bd9Sstevel@tonic-gate mp = bp;
1597c478bd9Sstevel@tonic-gate while (mp) {
16022eb7cb5Sgd78059 if (bp->b_wptr == bp->b_rptr) {
1617c478bd9Sstevel@tonic-gate KTLILOG(2,
1627c478bd9Sstevel@tonic-gate "t_krcvudata: zero length block\n",
1637c478bd9Sstevel@tonic-gate 0);
1647c478bd9Sstevel@tonic-gate tmp = mp->b_cont;
1657c478bd9Sstevel@tonic-gate if (nbp)
1667c478bd9Sstevel@tonic-gate nbp->b_cont = tmp;
1677c478bd9Sstevel@tonic-gate else
1687c478bd9Sstevel@tonic-gate bp = tmp;
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate freeb(mp);
1717c478bd9Sstevel@tonic-gate mp = tmp;
1727c478bd9Sstevel@tonic-gate } else {
1737c478bd9Sstevel@tonic-gate nbp = mp;
1747c478bd9Sstevel@tonic-gate mp = mp->b_cont;
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate #ifdef KTLIDEBUG
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate mblk_t *tp;
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate tp = bp;
1827c478bd9Sstevel@tonic-gate while (tp) {
1837c478bd9Sstevel@tonic-gate struct datab *dbp = tp->b_datap;
1847c478bd9Sstevel@tonic-gate frtn_t *frp = dbp->db_frtnp;
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate KTLILOG(2, "t_krcvudata: bp %x, ", tp);
1877c478bd9Sstevel@tonic-gate KTLILOG(2, "db_size %x, ", dbp->db_lim - dbp->db_base);
1887c478bd9Sstevel@tonic-gate KTLILOG(2, "db_ref %x", dbp->db_ref);
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate if (frp != NULL)
1917c478bd9Sstevel@tonic-gate KTLILOG(2, ", func: %x", frp->free_func);
1927c478bd9Sstevel@tonic-gate KTLILOG(2, ", arg %x\n", frp->free_arg);
1937c478bd9Sstevel@tonic-gate } else
1947c478bd9Sstevel@tonic-gate KTLILOG(2, "\n", 0);
1957c478bd9Sstevel@tonic-gate tp = tp->b_cont;
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate #endif /* KTLIDEBUG */
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate * now just point the users mblk
2017c478bd9Sstevel@tonic-gate * pointer to what we received.
2027c478bd9Sstevel@tonic-gate */
2037c478bd9Sstevel@tonic-gate if (bp == NULL) {
2047c478bd9Sstevel@tonic-gate KTLILOG(2, "t_krcvudata: No data\n", 0);
2057c478bd9Sstevel@tonic-gate error = EPROTO;
2067c478bd9Sstevel@tonic-gate break;
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate if (bp->b_wptr != bp->b_rptr) {
2097c478bd9Sstevel@tonic-gate if (!IS_P2ALIGNED(bp->b_rptr, sizeof (uint32_t)))
21022eb7cb5Sgd78059 if (!pullupmsg(bp, MBLKL(bp))) {
2117c478bd9Sstevel@tonic-gate KTLILOG(1,
2127c478bd9Sstevel@tonic-gate "t_krcvudata: pullupmsg failed\n", 0);
2137c478bd9Sstevel@tonic-gate error = EIO;
2147c478bd9Sstevel@tonic-gate freemsg(bp);
2157c478bd9Sstevel@tonic-gate break;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate unitdata->udata.buf = (char *)bp->b_rptr;
21822eb7cb5Sgd78059 unitdata->udata.len = (uint_t)MBLKL(bp);
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate KTLILOG(2, "t_krcvudata: got %d bytes\n",
2217c478bd9Sstevel@tonic-gate unitdata->udata.len);
2227c478bd9Sstevel@tonic-gate unitdata->udata.udata_mp = bp;
2237c478bd9Sstevel@tonic-gate } else {
2247c478bd9Sstevel@tonic-gate KTLILOG(2,
2257c478bd9Sstevel@tonic-gate "t_krcvudata: 0 length data message\n", 0);
2267c478bd9Sstevel@tonic-gate freemsg(bp);
2277c478bd9Sstevel@tonic-gate unitdata->udata.len = 0;
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate *type = T_DATA;
2307c478bd9Sstevel@tonic-gate break;
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate case T_UDERROR_IND:
2337c478bd9Sstevel@tonic-gate KTLILOG(2, "t_krcvudata: Got T_UDERROR_IND\n", 0);
23422eb7cb5Sgd78059 hdrsz = MBLKL(bp);
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate * check everything for consistency
2387c478bd9Sstevel@tonic-gate */
2397c478bd9Sstevel@tonic-gate if (hdrsz < TUDERRORINDSZ ||
2407c478bd9Sstevel@tonic-gate hdrsz < (pptr->uderror_ind.OPT_length +
2417c478bd9Sstevel@tonic-gate pptr->uderror_ind.OPT_offset) ||
2427c478bd9Sstevel@tonic-gate hdrsz < (pptr->uderror_ind.DEST_length +
2437c478bd9Sstevel@tonic-gate pptr->uderror_ind.DEST_offset)) {
2447c478bd9Sstevel@tonic-gate error = EPROTO;
2457c478bd9Sstevel@tonic-gate freemsg(bp);
2467c478bd9Sstevel@tonic-gate break;
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate if (pptr->uderror_ind.DEST_length >
2507c478bd9Sstevel@tonic-gate (int)unitdata->addr.maxlen ||
2517c478bd9Sstevel@tonic-gate pptr->uderror_ind.OPT_length >
2527c478bd9Sstevel@tonic-gate (int)unitdata->opt.maxlen) {
2537c478bd9Sstevel@tonic-gate error = EMSGSIZE;
2547c478bd9Sstevel@tonic-gate freemsg(bp);
2557c478bd9Sstevel@tonic-gate break;
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate * okay, so now we copy them
2607c478bd9Sstevel@tonic-gate */
2617c478bd9Sstevel@tonic-gate bcopy(bp->b_rptr + pptr->uderror_ind.DEST_offset,
2627c478bd9Sstevel@tonic-gate unitdata->addr.buf,
2637c478bd9Sstevel@tonic-gate (size_t)pptr->uderror_ind.DEST_length);
2647c478bd9Sstevel@tonic-gate unitdata->addr.len = pptr->uderror_ind.DEST_length;
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate bcopy(bp->b_rptr + pptr->uderror_ind.OPT_offset,
2677c478bd9Sstevel@tonic-gate unitdata->opt.buf,
2687c478bd9Sstevel@tonic-gate (size_t)pptr->uderror_ind.OPT_length);
2697c478bd9Sstevel@tonic-gate unitdata->opt.len = pptr->uderror_ind.OPT_length;
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate *uderr = pptr->uderror_ind.ERROR_type;
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate unitdata->udata.buf = NULL;
2747c478bd9Sstevel@tonic-gate unitdata->udata.udata_mp = NULL;
2757c478bd9Sstevel@tonic-gate unitdata->udata.len = 0;
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate freemsg(bp);
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate *type = T_UDERR;
2807c478bd9Sstevel@tonic-gate break;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate default:
2837c478bd9Sstevel@tonic-gate KTLILOG(1,
2847c478bd9Sstevel@tonic-gate "t_krcvudata: Unknown transport primitive %d\n",
2857c478bd9Sstevel@tonic-gate pptr->type);
2867c478bd9Sstevel@tonic-gate error = EPROTO;
2877c478bd9Sstevel@tonic-gate freemsg(bp);
2887c478bd9Sstevel@tonic-gate break;
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate break;
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate case M_FLUSH:
2937c478bd9Sstevel@tonic-gate KTLILOG(1, "t_krcvudata: tli_recv returned M_FLUSH\n", 0);
2947c478bd9Sstevel@tonic-gate freemsg(bp);
295*09b0d01cSGary Mills error = EBADMSG;
2967c478bd9Sstevel@tonic-gate break;
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate default:
2997c478bd9Sstevel@tonic-gate KTLILOG(1, "t_krcvudata: unknown message type %x\n",
3007c478bd9Sstevel@tonic-gate bp->b_datap->db_type);
3017c478bd9Sstevel@tonic-gate freemsg(bp);
302*09b0d01cSGary Mills error = EBADMSG;
3037c478bd9Sstevel@tonic-gate break;
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate return (error);
3077c478bd9Sstevel@tonic-gate }
308