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 23 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 24 /* All Rights Reserved */ 25 26 /* 27 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 28 * Use is subject to license terms. 29 */ 30 31 #include "mt.h" 32 #include <stdlib.h> 33 #include <errno.h> 34 #include <unistd.h> 35 #include <stropts.h> 36 #include <sys/stream.h> 37 #define _SUN_TPI_VERSION 2 38 #include <sys/tihdr.h> 39 #include <sys/timod.h> 40 #include <xti.h> 41 #include <signal.h> 42 #include <syslog.h> 43 #include <assert.h> 44 #include "tx.h" 45 46 int 47 _tx_accept( 48 int fd, 49 int resfd, 50 const struct t_call *call, 51 int api_semantics 52 ) 53 { 54 struct T_conn_res *cres; 55 struct strfdinsert strfdinsert; 56 int size, retval, sv_errno; 57 struct _ti_user *tiptr; 58 struct _ti_user *restiptr; 59 sigset_t mask; 60 struct strbuf ctlbuf; 61 int didalloc; 62 t_scalar_t conn_res_prim; 63 64 if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL) 65 return (-1); 66 if ((restiptr = _t_checkfd(resfd, 0, api_semantics)) == NULL) 67 return (-1); 68 69 /* 70 * We need to block signals to perform the I_FDINSERT operation 71 * (sending T_CONN_RES downstream) which is non-idempotent. 72 * Note that sig_mutex_lock() only defers signals, it does not 73 * block them, so interruptible syscalls could still get EINTR. 74 */ 75 (void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask); 76 sig_mutex_lock(&tiptr->ti_lock); 77 78 if (tiptr->ti_servtype == T_CLTS) { 79 t_errno = TNOTSUPPORT; 80 sig_mutex_unlock(&tiptr->ti_lock); 81 (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL); 82 return (-1); 83 } 84 85 if (_T_IS_XTI(api_semantics)) { 86 /* 87 * User level state verification only done for XTI 88 * because doing for TLI may break existing applications 89 * 90 * For fd == resfd, state should be T_INCON 91 * For fd != resfd, 92 * fd state should be T_INCON 93 * resfd state should be T_IDLE (bound endpoint) or 94 * it can be T_UNBND. The T_UNBND case is not (yet?) 95 * allowed in the published XTI spec but fixed by the 96 * corrigenda. 97 */ 98 if ((fd == resfd && tiptr->ti_state != T_INCON) || 99 (fd != resfd && 100 ((tiptr->ti_state != T_INCON) || 101 !(restiptr->ti_state == T_IDLE || 102 restiptr->ti_state == T_UNBND)))) { 103 t_errno = TOUTSTATE; 104 sig_mutex_unlock(&tiptr->ti_lock); 105 (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL); 106 return (-1); 107 } 108 109 /* 110 * XTI says: 111 * If fd != resfd, and a resfd bound to a protocol address is 112 * passed, then it better not have a qlen > 0. 113 * That is, an endpoint bound as if it will be a listener 114 * cannot be used as an acceptor. 115 */ 116 if (fd != resfd && restiptr->ti_state == T_IDLE && 117 restiptr->ti_qlen > 0) { 118 t_errno = TRESQLEN; 119 sig_mutex_unlock(&tiptr->ti_lock); 120 (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL); 121 return (-1); 122 } 123 124 if (fd == resfd && tiptr->ti_ocnt > 1) { 125 t_errno = TINDOUT; 126 sig_mutex_unlock(&tiptr->ti_lock); 127 (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL); 128 return (-1); 129 } 130 131 /* 132 * Note: TRESADDR error is specified by XTI. It happens 133 * when resfd is bound and fd and resfd are not BOUND to 134 * the same protocol address. TCP obviously does allow 135 * two endpoints to bind to the same address. Why is the 136 * need for this error considering there is an address switch 137 * that can be done for the endpoint at accept time ? Go 138 * figure and ask the XTI folks. 139 * We interpret this to be a transport specific error condition 140 * to be be coveyed by the transport provider in T_ERROR_ACK 141 * to T_CONN_RES on transports that allow two endpoints to 142 * be bound to the same address and have trouble with the 143 * idea of accepting connections on a resfd that has a qlen > 0 144 */ 145 } 146 147 if (fd != resfd) { 148 if ((retval = ioctl(resfd, I_NREAD, &size)) < 0) { 149 sv_errno = errno; 150 151 t_errno = TSYSERR; 152 sig_mutex_unlock(&tiptr->ti_lock); 153 (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL); 154 errno = sv_errno; 155 return (-1); 156 } 157 if (retval > 0) { 158 t_errno = TBADF; 159 sig_mutex_unlock(&tiptr->ti_lock); 160 (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL); 161 return (-1); 162 } 163 } 164 165 /* 166 * Acquire ctlbuf for use in sending/receiving control part 167 * of the message. 168 */ 169 if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) { 170 sv_errno = errno; 171 sig_mutex_unlock(&tiptr->ti_lock); 172 (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL); 173 errno = sv_errno; 174 return (-1); 175 } 176 177 /* 178 * In Unix98 t_accept() need not return [TLOOK] if connect/disconnect 179 * indications are present. TLI and Unix95 need to return error. 180 */ 181 if (_T_API_VER_LT(api_semantics, TX_XTI_XNS5_API)) { 182 if (_t_is_event(fd, tiptr) < 0) 183 goto err_out; 184 } 185 186 /* LINTED pointer cast */ 187 cres = (struct T_conn_res *)ctlbuf.buf; 188 cres->OPT_length = call->opt.len; 189 cres->OPT_offset = 0; 190 cres->SEQ_number = call->sequence; 191 if ((restiptr->ti_flags & V_ACCEPTOR_ID) != 0) { 192 cres->ACCEPTOR_id = restiptr->acceptor_id; 193 cres->PRIM_type = conn_res_prim = T_CONN_RES; 194 } else { 195 /* I_FDINSERT should use O_T_CONN_RES. */ 196 cres->ACCEPTOR_id = 0; 197 cres->PRIM_type = conn_res_prim = O_T_CONN_RES; 198 } 199 200 size = (int)sizeof (struct T_conn_res); 201 202 if (call->opt.len) { 203 if (_t_aligned_copy(&ctlbuf, call->opt.len, size, 204 call->opt.buf, &cres->OPT_offset) < 0) { 205 /* 206 * Aligned copy will overflow buffer allocated based 207 * transport maximum options length. 208 * return error. 209 */ 210 t_errno = TBADOPT; 211 goto err_out; 212 } 213 size = cres->OPT_offset + cres->OPT_length; 214 } 215 216 if (call->udata.len) { 217 if ((tiptr->ti_cdatasize == T_INVALID /* -2 */) || 218 ((tiptr->ti_cdatasize != T_INFINITE /* -1 */) && 219 (call->udata.len > (uint32_t)tiptr->ti_cdatasize))) { 220 /* 221 * user data not valid with connect or it 222 * exceeds the limits specified by the transport 223 * provider 224 */ 225 t_errno = TBADDATA; 226 goto err_out; 227 } 228 } 229 230 231 ctlbuf.len = size; 232 233 /* 234 * Assumes signals are blocked so putmsg() will not block 235 * indefinitely 236 */ 237 if ((restiptr->ti_flags & V_ACCEPTOR_ID) != 0) { 238 /* 239 * Assumes signals are blocked so putmsg() will not block 240 * indefinitely 241 */ 242 if (putmsg(fd, &ctlbuf, 243 (struct strbuf *)(call->udata.len? &call->udata: NULL), 0) < 244 0) { 245 if (errno == EAGAIN) 246 t_errno = TFLOW; 247 else 248 t_errno = TSYSERR; 249 goto err_out; 250 } 251 } else { 252 strfdinsert.ctlbuf.maxlen = ctlbuf.maxlen; 253 strfdinsert.ctlbuf.len = ctlbuf.len; 254 strfdinsert.ctlbuf.buf = ctlbuf.buf; 255 256 strfdinsert.databuf.maxlen = call->udata.maxlen; 257 strfdinsert.databuf.len = 258 (call->udata.len? call->udata.len: -1); 259 strfdinsert.databuf.buf = call->udata.buf; 260 strfdinsert.fildes = resfd; 261 strfdinsert.offset = (int)sizeof (t_scalar_t); 262 strfdinsert.flags = 0; /* could be EXPEDITED also */ 263 264 if (ioctl(fd, I_FDINSERT, &strfdinsert) < 0) { 265 if (errno == EAGAIN) 266 t_errno = TFLOW; 267 else 268 t_errno = TSYSERR; 269 goto err_out; 270 } 271 } 272 273 if (_t_is_ok(fd, tiptr, conn_res_prim) < 0) { 274 /* 275 * At the TPI level, the error returned in a T_ERROR_ACK 276 * received in response to a T_CONN_RES for a listener and 277 * acceptor endpoints not being the same kind of endpoints 278 * has changed to a new t_errno code introduced with 279 * XTI (TPROVMISMATCH). We need to adjust TLI error code 280 * to be same as before. 281 */ 282 if (_T_IS_TLI(api_semantics) && t_errno == TPROVMISMATCH) { 283 /* TLI only */ 284 t_errno = TBADF; 285 } 286 goto err_out; 287 } 288 289 if (tiptr->ti_ocnt == 1) { 290 if (fd == resfd) { 291 _T_TX_NEXTSTATE(T_ACCEPT1, tiptr, 292 "t_accept: invalid state event T_ACCEPT1"); 293 } else { 294 _T_TX_NEXTSTATE(T_ACCEPT2, tiptr, 295 "t_accept: invalid state event T_ACCEPT2"); 296 /* 297 * XXX Here we lock the resfd lock also. This 298 * is an instance of holding two locks without 299 * any enforcement of a locking hiararchy. 300 * There is potential for deadlock in incorrect 301 * or buggy programs here but this is the safer 302 * choice in this case. Correct programs will not 303 * deadlock. 304 */ 305 sig_mutex_lock(&restiptr->ti_lock); 306 _T_TX_NEXTSTATE(T_PASSCON, restiptr, 307 "t_accept: invalid state event T_PASSCON"); 308 sig_mutex_unlock(&restiptr->ti_lock); 309 } 310 } else { 311 _T_TX_NEXTSTATE(T_ACCEPT3, tiptr, 312 "t_accept: invalid state event T_ACCEPT3"); 313 if (fd != resfd) 314 sig_mutex_lock(&restiptr->ti_lock); 315 _T_TX_NEXTSTATE(T_PASSCON, restiptr, 316 "t_accept: invalid state event T_PASSCON"); 317 if (fd != resfd) 318 sig_mutex_unlock(&restiptr->ti_lock); 319 } 320 321 tiptr->ti_ocnt--; 322 tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED; 323 324 /* 325 * Update attributes which may have been negotiated during 326 * connection establishment for protocols where we suspect 327 * such negotiation is likely (e.g. OSI). We do not do it for 328 * all endpoints for performance reasons. Also, this code is 329 * deliberately done after user level state changes so even 330 * the (unlikely) failure case reflects a connected endpoint. 331 */ 332 if (restiptr->ti_tsdusize != 0) { 333 if (_t_do_postconn_sync(resfd, restiptr) < 0) 334 goto err_out; 335 } 336 337 if (didalloc) 338 free(ctlbuf.buf); 339 else 340 tiptr->ti_ctlbuf = ctlbuf.buf; 341 sig_mutex_unlock(&tiptr->ti_lock); 342 (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL); 343 return (0); 344 /* NOTREACHED */ 345 err_out: 346 sv_errno = errno; 347 if (didalloc) 348 free(ctlbuf.buf); 349 else 350 tiptr->ti_ctlbuf = ctlbuf.buf; 351 sig_mutex_unlock(&tiptr->ti_lock); 352 (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL); 353 errno = sv_errno; 354 return (-1); 355 } 356