1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23 /* All Rights Reserved */ 24 25 26 /* 27 * Copyright 1993-2003 Sun Microsystems, Inc. All rights reserved. 28 * Use is subject to license terms. 29 */ 30 31 32 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.5 */ 33 34 #include "mt.h" 35 #include <rpc/trace.h> 36 #include <errno.h> 37 #include <string.h> 38 #include <stdlib.h> 39 #include <stropts.h> 40 #include <sys/stream.h> 41 #define _SUN_TPI_VERSION 2 42 #include <sys/tihdr.h> 43 #include <sys/timod.h> 44 #include <xti.h> 45 #include <syslog.h> 46 #include <assert.h> 47 #include "tx.h" 48 49 int 50 _tx_rcvuderr(int fd, struct t_uderr *uderr, int api_semantics) 51 { 52 struct strbuf ctlbuf, databuf; 53 int flg; 54 int retval; 55 union T_primitives *pptr; 56 struct _ti_user *tiptr; 57 int sv_errno; 58 int didalloc; 59 int use_lookbufs = 0; 60 61 62 trace2(TR_t_rcvuderr, 0, fd); 63 if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL) { 64 sv_errno = errno; 65 trace2(TR_t_rcvuderr, 1, fd); 66 errno = sv_errno; 67 return (-1); 68 } 69 sig_mutex_lock(&tiptr->ti_lock); 70 71 if (tiptr->ti_servtype != T_CLTS) { 72 t_errno = TNOTSUPPORT; 73 sig_mutex_unlock(&tiptr->ti_lock); 74 trace2(TR_t_rcvuderr, 1, fd); 75 return (-1); 76 } 77 /* 78 * is there a unitdata error indication in look buffer 79 */ 80 if (tiptr->ti_lookcnt > 0) { 81 ctlbuf.len = tiptr->ti_lookbufs.tl_lookclen; 82 ctlbuf.buf = tiptr->ti_lookbufs.tl_lookcbuf; 83 /* Note: cltbuf.maxlen not used in this case */ 84 85 assert(((union T_primitives *)ctlbuf.buf)->type 86 == T_UDERROR_IND); 87 88 databuf.maxlen = 0; 89 databuf.len = 0; 90 databuf.buf = NULL; 91 92 use_lookbufs = 1; 93 94 } else { 95 if ((retval = _t_look_locked(fd, tiptr, 0, 96 api_semantics)) < 0) { 97 sv_errno = errno; 98 sig_mutex_unlock(&tiptr->ti_lock); 99 trace2(TR_t_rcvuderr, 1, fd); 100 errno = sv_errno; 101 return (-1); 102 } 103 if (retval != T_UDERR) { 104 t_errno = TNOUDERR; 105 sig_mutex_unlock(&tiptr->ti_lock); 106 trace2(TR_t_rcvuderr, 1, fd); 107 return (-1); 108 } 109 110 /* 111 * Acquire ctlbuf for use in sending/receiving control part 112 * of the message. 113 */ 114 if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) { 115 sv_errno = errno; 116 sig_mutex_unlock(&tiptr->ti_lock); 117 trace2(TR_t_rcvuderr, 1, fd); 118 errno = sv_errno; 119 return (-1); 120 } 121 122 databuf.maxlen = 0; 123 databuf.len = 0; 124 databuf.buf = NULL; 125 126 flg = 0; 127 128 /* 129 * Since we already verified that a unitdata error 130 * indication is pending, we assume that this getmsg() 131 * will not block indefinitely. 132 */ 133 if ((retval = getmsg(fd, &ctlbuf, &databuf, &flg)) < 0) { 134 135 t_errno = TSYSERR; 136 goto err_out; 137 } 138 /* 139 * did I get entire message? 140 */ 141 if (retval > 0) { 142 t_errno = TSYSERR; 143 errno = EIO; 144 goto err_out; 145 } 146 147 } 148 149 pptr = (union T_primitives *)ctlbuf.buf; 150 151 if ((ctlbuf.len < (int)sizeof (struct T_uderror_ind)) || 152 (pptr->type != T_UDERROR_IND)) { 153 t_errno = TSYSERR; 154 errno = EPROTO; 155 goto err_out; 156 } 157 158 if (uderr) { 159 if (_T_IS_TLI(api_semantics) || uderr->addr.maxlen > 0) { 160 if (TLEN_GT_NLEN(pptr->uderror_ind.DEST_length, 161 uderr->addr.maxlen)) { 162 t_errno = TBUFOVFLW; 163 goto err_out; 164 } 165 (void) memcpy(uderr->addr.buf, ctlbuf.buf + 166 pptr->uderror_ind.DEST_offset, 167 (size_t)pptr->uderror_ind.DEST_length); 168 uderr->addr.len = 169 (unsigned int)pptr->uderror_ind.DEST_length; 170 } 171 if (_T_IS_TLI(api_semantics) || uderr->addr.maxlen > 0) { 172 if (TLEN_GT_NLEN(pptr->uderror_ind.OPT_length, 173 uderr->opt.maxlen)) { 174 t_errno = TBUFOVFLW; 175 goto err_out; 176 } 177 (void) memcpy(uderr->opt.buf, ctlbuf.buf + 178 pptr->uderror_ind.OPT_offset, 179 (size_t)pptr->uderror_ind.OPT_length); 180 uderr->opt.len = 181 (unsigned int)pptr->uderror_ind.OPT_length; 182 } 183 uderr->error = pptr->uderror_ind.ERROR_type; 184 } 185 186 _T_TX_NEXTSTATE(T_RCVUDERR, tiptr, 187 "t_rcvuderr: invalid state event T_RCVUDERR"); 188 if (use_lookbufs) 189 _t_free_looklist_head(tiptr); 190 else { 191 if (didalloc) 192 free(ctlbuf.buf); 193 else 194 tiptr->ti_ctlbuf = ctlbuf.buf; 195 } 196 sig_mutex_unlock(&tiptr->ti_lock); 197 trace2(TR_t_rcvuderr, 1, fd); 198 return (0); 199 200 err_out: 201 sv_errno = errno; 202 203 if (use_lookbufs) 204 _t_free_looklist_head(tiptr); 205 else { 206 if (didalloc) 207 free(ctlbuf.buf); 208 else 209 tiptr->ti_ctlbuf = ctlbuf.buf; 210 } 211 sig_mutex_unlock(&tiptr->ti_lock); 212 trace2(TR_t_rcvuderr, 1, fd); 213 errno = sv_errno; 214 return (-1); 215 } 216