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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright 2015, Joyent, Inc. 25 * Copyright 2016 Nexenta Systems, Inc. All rights reserved. 26 * Copyright 2022 Garrett D'Amore 27 */ 28 29 #include <sys/types.h> 30 #include <sys/t_lock.h> 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/buf.h> 34 #include <sys/conf.h> 35 #include <sys/cred.h> 36 #include <sys/kmem.h> 37 #include <sys/kmem_impl.h> 38 #include <sys/sysmacros.h> 39 #include <sys/vfs.h> 40 #include <sys/vnode.h> 41 #include <sys/debug.h> 42 #include <sys/errno.h> 43 #include <sys/time.h> 44 #include <sys/file.h> 45 #include <sys/open.h> 46 #include <sys/user.h> 47 #include <sys/termios.h> 48 #include <sys/stream.h> 49 #include <sys/strsubr.h> 50 #include <sys/strsun.h> 51 #include <sys/suntpi.h> 52 #include <sys/ddi.h> 53 #include <sys/esunddi.h> 54 #include <sys/flock.h> 55 #include <sys/modctl.h> 56 #include <sys/vtrace.h> 57 #include <sys/cmn_err.h> 58 #include <sys/pathname.h> 59 60 #include <sys/socket.h> 61 #include <sys/socketvar.h> 62 #include <sys/sockio.h> 63 #include <netinet/in.h> 64 #include <sys/un.h> 65 #include <sys/strsun.h> 66 67 #include <sys/tiuser.h> 68 #define _SUN_TPI_VERSION 2 69 #include <sys/tihdr.h> 70 #include <sys/timod.h> /* TI_GETMYNAME, TI_GETPEERNAME */ 71 72 #include <c2/audit.h> 73 74 #include <inet/common.h> 75 #include <inet/ip.h> 76 #include <inet/ip6.h> 77 #include <inet/tcp.h> 78 #include <inet/udp_impl.h> 79 80 #include <sys/zone.h> 81 82 #include <fs/sockfs/sockcommon.h> 83 #include <fs/sockfs/socktpi.h> 84 #include <fs/sockfs/socktpi_impl.h> 85 86 /* 87 * Possible failures when memory can't be allocated. The documented behavior: 88 * 89 * 5.5: 4.X: XNET: 90 * accept: ENOMEM/ENOSR/EINTR - (EINTR) ENOMEM/ENOBUFS/ENOSR/ 91 * EINTR 92 * (4.X does not document EINTR but returns it) 93 * bind: ENOSR - ENOBUFS/ENOSR 94 * connect: EINTR EINTR ENOBUFS/ENOSR/EINTR 95 * getpeername: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR 96 * getsockname: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR 97 * (4.X getpeername and getsockname do not fail in practice) 98 * getsockopt: ENOMEM/ENOSR - ENOBUFS/ENOSR 99 * listen: - - ENOBUFS 100 * recv: ENOMEM/ENOSR/EINTR EINTR ENOBUFS/ENOMEM/ENOSR/ 101 * EINTR 102 * send: ENOMEM/ENOSR/EINTR ENOBUFS/EINTR ENOBUFS/ENOMEM/ENOSR/ 103 * EINTR 104 * setsockopt: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR 105 * shutdown: ENOMEM/ENOSR - ENOBUFS/ENOSR 106 * socket: ENOMEM/ENOSR ENOBUFS ENOBUFS/ENOMEM/ENOSR 107 * socketpair: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR 108 * 109 * Resolution. When allocation fails: 110 * recv: return EINTR 111 * send: return EINTR 112 * connect, accept: EINTR 113 * bind, listen, shutdown (unbind, unix_close, disconnect): sleep 114 * socket, socketpair: ENOBUFS 115 * getpeername, getsockname: sleep 116 * getsockopt, setsockopt: sleep 117 */ 118 119 #ifdef SOCK_TEST 120 /* 121 * Variables that make sockfs do something other than the standard TPI 122 * for the AF_INET transports. 123 * 124 * solisten_tpi_tcp: 125 * TCP can handle a O_T_BIND_REQ with an increased backlog even though 126 * the transport is already bound. This is needed to avoid loosing the 127 * port number should listen() do a T_UNBIND_REQ followed by a 128 * O_T_BIND_REQ. 129 * 130 * soconnect_tpi_udp: 131 * UDP and ICMP can handle a T_CONN_REQ. 132 * This is needed to make the sequence of connect(), getsockname() 133 * return the local IP address used to send packets to the connected to 134 * destination. 135 * 136 * soconnect_tpi_tcp: 137 * TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ. 138 * Set this to non-zero to send TPI conformant messages to TCP in this 139 * respect. This is a performance optimization. 140 * 141 * soaccept_tpi_tcp: 142 * TCP can handle a T_CONN_REQ without the acceptor being bound. 143 * This is a performance optimization that has been picked up in XTI. 144 * 145 * soaccept_tpi_multioptions: 146 * When inheriting SOL_SOCKET options from the listener to the accepting 147 * socket send them as a single message for AF_INET{,6}. 148 */ 149 int solisten_tpi_tcp = 0; 150 int soconnect_tpi_udp = 0; 151 int soconnect_tpi_tcp = 0; 152 int soaccept_tpi_tcp = 0; 153 int soaccept_tpi_multioptions = 1; 154 #else /* SOCK_TEST */ 155 #define soconnect_tpi_tcp 0 156 #define soconnect_tpi_udp 0 157 #define solisten_tpi_tcp 0 158 #define soaccept_tpi_tcp 0 159 #define soaccept_tpi_multioptions 1 160 #endif /* SOCK_TEST */ 161 162 #ifdef SOCK_TEST 163 extern int do_useracc; 164 extern clock_t sock_test_timelimit; 165 #endif /* SOCK_TEST */ 166 167 extern uint32_t ucredsize; 168 169 /* 170 * Some X/Open added checks might have to be backed out to keep SunOS 4.X 171 * applications working. Turn on this flag to disable these checks. 172 */ 173 int xnet_skip_checks = 0; 174 int xnet_check_print = 0; 175 int xnet_truncate_print = 0; 176 177 static void sotpi_destroy(struct sonode *); 178 static struct sonode *sotpi_create(struct sockparams *, int, int, int, int, 179 int, int *, cred_t *cr); 180 181 static boolean_t sotpi_info_create(struct sonode *, int); 182 static void sotpi_info_init(struct sonode *); 183 static void sotpi_info_fini(struct sonode *); 184 static void sotpi_info_destroy(struct sonode *); 185 186 /* 187 * Do direct function call to the transport layer below; this would 188 * also allow the transport to utilize read-side synchronous stream 189 * interface if necessary. This is a /etc/system tunable that must 190 * not be modified on a running system. By default this is enabled 191 * for performance reasons and may be disabled for debugging purposes. 192 */ 193 boolean_t socktpi_direct = B_TRUE; 194 195 static struct kmem_cache *socktpi_cache, *socktpi_unix_cache; 196 197 extern void sigintr(k_sigset_t *, int); 198 extern void sigunintr(k_sigset_t *); 199 200 static int sotpi_unbind(struct sonode *, int); 201 202 /* TPI sockfs sonode operations */ 203 int sotpi_init(struct sonode *, struct sonode *, struct cred *, 204 int); 205 static int sotpi_accept(struct sonode *, int, struct cred *, 206 struct sonode **); 207 static int sotpi_bind(struct sonode *, struct sockaddr *, socklen_t, 208 int, struct cred *); 209 static int sotpi_listen(struct sonode *, int, struct cred *); 210 static int sotpi_connect(struct sonode *, struct sockaddr *, 211 socklen_t, int, int, struct cred *); 212 extern int sotpi_recvmsg(struct sonode *, struct nmsghdr *, 213 struct uio *, struct cred *); 214 static int sotpi_sendmsg(struct sonode *, struct nmsghdr *, 215 struct uio *, struct cred *); 216 static int sotpi_sendmblk(struct sonode *, struct nmsghdr *, int, 217 struct cred *, mblk_t **); 218 static int sosend_dgramcmsg(struct sonode *, struct sockaddr *, socklen_t, 219 struct uio *, void *, t_uscalar_t, int); 220 static int sodgram_direct(struct sonode *, struct sockaddr *, 221 socklen_t, struct uio *, int); 222 extern int sotpi_getpeername(struct sonode *, struct sockaddr *, 223 socklen_t *, boolean_t, struct cred *); 224 static int sotpi_getsockname(struct sonode *, struct sockaddr *, 225 socklen_t *, struct cred *); 226 static int sotpi_shutdown(struct sonode *, int, struct cred *); 227 extern int sotpi_getsockopt(struct sonode *, int, int, void *, 228 socklen_t *, int, struct cred *); 229 extern int sotpi_setsockopt(struct sonode *, int, int, const void *, 230 socklen_t, struct cred *); 231 static int sotpi_ioctl(struct sonode *, int, intptr_t, int, struct cred *, 232 int32_t *); 233 static int socktpi_plumbioctl(struct vnode *, int, intptr_t, int, 234 struct cred *, int32_t *); 235 static int sotpi_poll(struct sonode *, short, int, short *, 236 struct pollhead **); 237 static int sotpi_close(struct sonode *, int, struct cred *); 238 239 static int i_sotpi_info_constructor(sotpi_info_t *); 240 static void i_sotpi_info_destructor(sotpi_info_t *); 241 242 sonodeops_t sotpi_sonodeops = { 243 sotpi_init, /* sop_init */ 244 sotpi_accept, /* sop_accept */ 245 sotpi_bind, /* sop_bind */ 246 sotpi_listen, /* sop_listen */ 247 sotpi_connect, /* sop_connect */ 248 sotpi_recvmsg, /* sop_recvmsg */ 249 sotpi_sendmsg, /* sop_sendmsg */ 250 sotpi_sendmblk, /* sop_sendmblk */ 251 sotpi_getpeername, /* sop_getpeername */ 252 sotpi_getsockname, /* sop_getsockname */ 253 sotpi_shutdown, /* sop_shutdown */ 254 sotpi_getsockopt, /* sop_getsockopt */ 255 sotpi_setsockopt, /* sop_setsockopt */ 256 sotpi_ioctl, /* sop_ioctl */ 257 sotpi_poll, /* sop_poll */ 258 sotpi_close, /* sop_close */ 259 }; 260 261 /* 262 * Return a TPI socket vnode. 263 * 264 * Note that sockets assume that the driver will clone (either itself 265 * or by using the clone driver) i.e. a socket() call will always 266 * result in a new vnode being created. 267 */ 268 269 /* 270 * Common create code for socket and accept. If tso is set the values 271 * from that node is used instead of issuing a T_INFO_REQ. 272 */ 273 274 /* ARGSUSED */ 275 static struct sonode * 276 sotpi_create(struct sockparams *sp, int family, int type, int protocol, 277 int version, int sflags, int *errorp, cred_t *cr) 278 { 279 struct sonode *so; 280 kmem_cache_t *cp; 281 282 ASSERT(sp->sp_sdev_info.sd_vnode != NULL); 283 284 if (family == AF_NCA) { 285 /* 286 * The request is for an NCA socket so for NL7C use the 287 * INET domain instead and mark NL7C_AF_NCA below. 288 */ 289 family = AF_INET; 290 /* 291 * NL7C is not supported in the non-global zone, 292 * we enforce this restriction here. 293 */ 294 if (getzoneid() != GLOBAL_ZONEID) { 295 *errorp = ENOTSUP; 296 return (NULL); 297 } 298 } 299 300 /* 301 * to be compatible with old tpi socket implementation ignore 302 * sleep flag (sflags) passed in 303 */ 304 cp = (family == AF_UNIX) ? socktpi_unix_cache : socktpi_cache; 305 so = kmem_cache_alloc(cp, KM_SLEEP); 306 if (so == NULL) { 307 *errorp = ENOMEM; 308 return (NULL); 309 } 310 311 sonode_init(so, sp, family, type, protocol, &sotpi_sonodeops); 312 sotpi_info_init(so); 313 314 if (version == SOV_DEFAULT) 315 version = so_default_version; 316 317 so->so_version = (short)version; 318 *errorp = 0; 319 320 return (so); 321 } 322 323 static void 324 sotpi_destroy(struct sonode *so) 325 { 326 kmem_cache_t *cp; 327 struct sockparams *origsp; 328 329 /* 330 * If there is a new dealloc function (ie. smod_destroy_func), 331 * then it should check the correctness of the ops. 332 */ 333 334 ASSERT(so->so_ops == &sotpi_sonodeops); 335 336 origsp = SOTOTPI(so)->sti_orig_sp; 337 338 sotpi_info_fini(so); 339 340 if (so->so_state & SS_FALLBACK_COMP) { 341 /* 342 * A fallback happend, which means that a sotpi_info_t struct 343 * was allocated (as opposed to being allocated from the TPI 344 * sonode cache. Therefore we explicitly free the struct 345 * here. 346 */ 347 sotpi_info_destroy(so); 348 ASSERT(origsp != NULL); 349 350 origsp->sp_smod_info->smod_sock_destroy_func(so); 351 SOCKPARAMS_DEC_REF(origsp); 352 } else { 353 sonode_fini(so); 354 cp = (so->so_family == AF_UNIX) ? socktpi_unix_cache : 355 socktpi_cache; 356 kmem_cache_free(cp, so); 357 } 358 } 359 360 /* ARGSUSED1 */ 361 int 362 sotpi_init(struct sonode *so, struct sonode *tso, struct cred *cr, int flags) 363 { 364 major_t maj; 365 dev_t newdev; 366 struct vnode *vp; 367 int error = 0; 368 struct stdata *stp; 369 370 sotpi_info_t *sti = SOTOTPI(so); 371 372 dprint(1, ("sotpi_init()\n")); 373 374 /* 375 * over write the sleep flag passed in but that is ok 376 * as tpi socket does not honor sleep flag. 377 */ 378 flags |= FREAD|FWRITE; 379 380 /* 381 * Record in so_flag that it is a clone. 382 */ 383 if (getmajor(sti->sti_dev) == clone_major) 384 so->so_flag |= SOCLONE; 385 386 if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM) && 387 (so->so_family == AF_INET || so->so_family == AF_INET6) && 388 (so->so_protocol == IPPROTO_TCP || so->so_protocol == IPPROTO_UDP || 389 so->so_protocol == IPPROTO_IP)) { 390 /* Tell tcp or udp that it's talking to sockets */ 391 flags |= SO_SOCKSTR; 392 393 /* 394 * Here we indicate to socktpi_open() our attempt to 395 * make direct calls between sockfs and transport. 396 * The final decision is left to socktpi_open(). 397 */ 398 sti->sti_direct = 1; 399 400 ASSERT(so->so_type != SOCK_DGRAM || tso == NULL); 401 if (so->so_type == SOCK_STREAM && tso != NULL) { 402 if (SOTOTPI(tso)->sti_direct) { 403 /* 404 * Inherit sti_direct from listener and pass 405 * SO_ACCEPTOR open flag to tcp, indicating 406 * that this is an accept fast-path instance. 407 */ 408 flags |= SO_ACCEPTOR; 409 } else { 410 /* 411 * sti_direct is not set on listener, meaning 412 * that the listener has been converted from 413 * a socket to a stream. Ensure that the 414 * acceptor inherits these settings. 415 */ 416 sti->sti_direct = 0; 417 flags &= ~SO_SOCKSTR; 418 } 419 } 420 } 421 422 /* 423 * Tell local transport that it is talking to sockets. 424 */ 425 if (so->so_family == AF_UNIX) { 426 flags |= SO_SOCKSTR; 427 } 428 429 vp = SOTOV(so); 430 newdev = vp->v_rdev; 431 maj = getmajor(newdev); 432 ASSERT(STREAMSTAB(maj)); 433 434 error = stropen(vp, &newdev, flags, cr); 435 436 stp = vp->v_stream; 437 if (error == 0) { 438 if (so->so_flag & SOCLONE) 439 ASSERT(newdev != vp->v_rdev); 440 mutex_enter(&so->so_lock); 441 sti->sti_dev = newdev; 442 vp->v_rdev = newdev; 443 mutex_exit(&so->so_lock); 444 445 if (stp->sd_flag & STRISTTY) { 446 /* 447 * this is a post SVR4 tty driver - a socket can not 448 * be a controlling terminal. Fail the open. 449 */ 450 (void) sotpi_close(so, flags, cr); 451 return (ENOTTY); /* XXX */ 452 } 453 454 ASSERT(stp->sd_wrq != NULL); 455 sti->sti_provinfo = tpi_findprov(stp->sd_wrq); 456 457 /* 458 * If caller is interested in doing direct function call 459 * interface to/from transport module, probe the module 460 * directly beneath the streamhead to see if it qualifies. 461 * 462 * We turn off the direct interface when qualifications fail. 463 * In the acceptor case, we simply turn off the sti_direct 464 * flag on the socket. We do the fallback after the accept 465 * has completed, before the new socket is returned to the 466 * application. 467 */ 468 if (sti->sti_direct) { 469 queue_t *tq = stp->sd_wrq->q_next; 470 471 /* 472 * sti_direct is currently supported and tested 473 * only for tcp/udp; this is the main reason to 474 * have the following assertions. 475 */ 476 ASSERT(so->so_family == AF_INET || 477 so->so_family == AF_INET6); 478 ASSERT(so->so_protocol == IPPROTO_UDP || 479 so->so_protocol == IPPROTO_TCP || 480 so->so_protocol == IPPROTO_IP); 481 ASSERT(so->so_type == SOCK_DGRAM || 482 so->so_type == SOCK_STREAM); 483 484 /* 485 * Abort direct call interface if the module directly 486 * underneath the stream head is not defined with the 487 * _D_DIRECT flag. This could happen in the tcp or 488 * udp case, when some other module is autopushed 489 * above it, or for some reasons the expected module 490 * isn't purely D_MP (which is the main requirement). 491 */ 492 if (!socktpi_direct || !(tq->q_flag & _QDIRECT) || 493 !(_OTHERQ(tq)->q_flag & _QDIRECT)) { 494 int rval; 495 496 /* Continue on without direct calls */ 497 sti->sti_direct = 0; 498 499 /* 500 * Cannot issue ioctl on fallback socket since 501 * there is no conn associated with the queue. 502 * The fallback downcall will notify the proto 503 * of the change. 504 */ 505 if (!(flags & SO_ACCEPTOR) && 506 !(flags & SO_FALLBACK)) { 507 if ((error = strioctl(vp, 508 _SIOCSOCKFALLBACK, 0, 0, K_TO_K, 509 cr, &rval)) != 0) { 510 (void) sotpi_close(so, flags, 511 cr); 512 return (error); 513 } 514 } 515 } 516 } 517 518 if (flags & SO_FALLBACK) { 519 /* 520 * The stream created does not have a conn. 521 * do stream set up after conn has been assigned 522 */ 523 return (error); 524 } 525 error = so_strinit(so, tso); 526 if (error != 0) { 527 (void) sotpi_close(so, flags, cr); 528 return (error); 529 } 530 531 /* Enable sendfile() on AF_UNIX streams */ 532 if (so->so_family == AF_UNIX && so->so_type == SOCK_STREAM) { 533 mutex_enter(&so->so_lock); 534 so->so_mode |= SM_SENDFILESUPP; 535 mutex_exit(&so->so_lock); 536 } 537 538 /* Wildcard */ 539 if (so->so_protocol != so->so_sockparams->sp_protocol) { 540 int protocol = so->so_protocol; 541 /* 542 * Issue SO_PROTOTYPE setsockopt. 543 */ 544 error = sotpi_setsockopt(so, SOL_SOCKET, SO_PROTOTYPE, 545 &protocol, (t_uscalar_t)sizeof (protocol), cr); 546 if (error != 0) { 547 (void) sotpi_close(so, flags, cr); 548 /* 549 * Setsockopt often fails with ENOPROTOOPT but 550 * socket() should fail with 551 * EPROTONOSUPPORT/EPROTOTYPE. 552 */ 553 return (EPROTONOSUPPORT); 554 } 555 } 556 557 } else { 558 /* 559 * While the same socket can not be reopened (unlike specfs) 560 * the stream head sets STREOPENFAIL when the autopush fails. 561 */ 562 if ((stp != NULL) && 563 (stp->sd_flag & STREOPENFAIL)) { 564 /* 565 * Open failed part way through. 566 */ 567 mutex_enter(&stp->sd_lock); 568 stp->sd_flag &= ~STREOPENFAIL; 569 mutex_exit(&stp->sd_lock); 570 (void) sotpi_close(so, flags, cr); 571 return (error); 572 /*NOTREACHED*/ 573 } 574 ASSERT(stp == NULL); 575 } 576 TRACE_4(TR_FAC_SOCKFS, TR_SOCKFS_OPEN, 577 "sockfs open:maj %d vp %p so %p error %d", 578 maj, vp, so, error); 579 return (error); 580 } 581 582 /* 583 * Bind the socket to an unspecified address in sockfs only. 584 * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't 585 * required in all cases. 586 */ 587 static void 588 so_automatic_bind(struct sonode *so) 589 { 590 sotpi_info_t *sti = SOTOTPI(so); 591 ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6); 592 593 ASSERT(MUTEX_HELD(&so->so_lock)); 594 ASSERT(!(so->so_state & SS_ISBOUND)); 595 ASSERT(sti->sti_unbind_mp); 596 597 ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen); 598 bzero(sti->sti_laddr_sa, sti->sti_laddr_len); 599 sti->sti_laddr_sa->sa_family = so->so_family; 600 so->so_state |= SS_ISBOUND; 601 } 602 603 604 /* 605 * bind the socket. 606 * 607 * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2 608 * are passed in we allow rebinding. Note that for backwards compatibility 609 * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind. 610 * Thus the rebinding code is currently not executed. 611 * 612 * The constraints for rebinding are: 613 * - it is a SOCK_DGRAM, or 614 * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected 615 * and no listen() has been done. 616 * This rebinding code was added based on some language in the XNET book 617 * about not returning EINVAL it the protocol allows rebinding. However, 618 * this language is not present in the Posix socket draft. Thus maybe the 619 * rebinding logic should be deleted from the source. 620 * 621 * A null "name" can be used to unbind the socket if: 622 * - it is a SOCK_DGRAM, or 623 * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected 624 * and no listen() has been done. 625 */ 626 /* ARGSUSED */ 627 static int 628 sotpi_bindlisten(struct sonode *so, struct sockaddr *name, 629 socklen_t namelen, int backlog, int flags, struct cred *cr) 630 { 631 struct T_bind_req bind_req; 632 struct T_bind_ack *bind_ack; 633 int error = 0; 634 mblk_t *mp; 635 void *addr; 636 t_uscalar_t addrlen; 637 int unbind_on_err = 1; 638 boolean_t clear_acceptconn_on_err = B_FALSE; 639 boolean_t restore_backlog_on_err = B_FALSE; 640 int save_so_backlog; 641 t_scalar_t PRIM_type = O_T_BIND_REQ; 642 boolean_t tcp_udp_xport; 643 sotpi_info_t *sti = SOTOTPI(so); 644 645 dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n", 646 (void *)so, (void *)name, namelen, backlog, flags, 647 pr_state(so->so_state, so->so_mode))); 648 649 tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM; 650 651 if (!(flags & _SOBIND_LOCK_HELD)) { 652 mutex_enter(&so->so_lock); 653 so_lock_single(so); /* Set SOLOCKED */ 654 } else { 655 ASSERT(MUTEX_HELD(&so->so_lock)); 656 ASSERT(so->so_flag & SOLOCKED); 657 } 658 659 /* 660 * Make sure that there is a preallocated unbind_req message 661 * before binding. This message allocated when the socket is 662 * created but it might be have been consumed. 663 */ 664 if (sti->sti_unbind_mp == NULL) { 665 dprintso(so, 1, ("sobind: allocating unbind_req\n")); 666 /* NOTE: holding so_lock while sleeping */ 667 sti->sti_unbind_mp = 668 soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP, 669 cr); 670 } 671 672 if (flags & _SOBIND_REBIND) { 673 /* 674 * Called from solisten after doing an sotpi_unbind() or 675 * potentially without the unbind (latter for AF_INET{,6}). 676 */ 677 ASSERT(name == NULL && namelen == 0); 678 679 if (so->so_family == AF_UNIX) { 680 ASSERT(sti->sti_ux_bound_vp); 681 addr = &sti->sti_ux_laddr; 682 addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr); 683 dprintso(so, 1, ("sobind rebind UNIX: addrlen %d, " 684 "addr 0x%p, vp %p\n", 685 addrlen, 686 (void *)((struct so_ux_addr *)addr)->soua_vp, 687 (void *)sti->sti_ux_bound_vp)); 688 } else { 689 addr = sti->sti_laddr_sa; 690 addrlen = (t_uscalar_t)sti->sti_laddr_len; 691 } 692 } else if (flags & _SOBIND_UNSPEC) { 693 ASSERT(name == NULL && namelen == 0); 694 695 /* 696 * The caller checked SS_ISBOUND but not necessarily 697 * under so_lock 698 */ 699 if (so->so_state & SS_ISBOUND) { 700 /* No error */ 701 goto done; 702 } 703 704 /* Set an initial local address */ 705 switch (so->so_family) { 706 case AF_UNIX: 707 /* 708 * Use an address with same size as struct sockaddr 709 * just like BSD. 710 */ 711 sti->sti_laddr_len = 712 (socklen_t)sizeof (struct sockaddr); 713 ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen); 714 bzero(sti->sti_laddr_sa, sti->sti_laddr_len); 715 sti->sti_laddr_sa->sa_family = so->so_family; 716 717 /* 718 * Pass down an address with the implicit bind 719 * magic number and the rest all zeros. 720 * The transport will return a unique address. 721 */ 722 sti->sti_ux_laddr.soua_vp = NULL; 723 sti->sti_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT; 724 addr = &sti->sti_ux_laddr; 725 addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr); 726 break; 727 728 case AF_INET: 729 case AF_INET6: 730 /* 731 * An unspecified bind in TPI has a NULL address. 732 * Set the address in sockfs to have the sa_family. 733 */ 734 sti->sti_laddr_len = (so->so_family == AF_INET) ? 735 (socklen_t)sizeof (sin_t) : 736 (socklen_t)sizeof (sin6_t); 737 ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen); 738 bzero(sti->sti_laddr_sa, sti->sti_laddr_len); 739 sti->sti_laddr_sa->sa_family = so->so_family; 740 addr = NULL; 741 addrlen = 0; 742 break; 743 744 default: 745 /* 746 * An unspecified bind in TPI has a NULL address. 747 * Set the address in sockfs to be zero length. 748 * 749 * Can not assume there is a sa_family for all 750 * protocol families. For example, AF_X25 does not 751 * have a family field. 752 */ 753 bzero(sti->sti_laddr_sa, sti->sti_laddr_len); 754 sti->sti_laddr_len = 0; /* XXX correct? */ 755 addr = NULL; 756 addrlen = 0; 757 break; 758 } 759 760 } else { 761 if (so->so_state & SS_ISBOUND) { 762 /* 763 * If it is ok to rebind the socket, first unbind 764 * with the transport. A rebind to the NULL address 765 * is interpreted as an unbind. 766 * Note that a bind to NULL in BSD does unbind the 767 * socket but it fails with EINVAL. 768 * Note that regular sockets set SOV_SOCKBSD i.e. 769 * _SOBIND_SOCKBSD gets set here hence no type of 770 * socket does currently allow rebinding. 771 * 772 * If the name is NULL just do an unbind. 773 */ 774 if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) && 775 name != NULL) { 776 error = EINVAL; 777 unbind_on_err = 0; 778 eprintsoline(so, error); 779 goto done; 780 } 781 if ((so->so_mode & SM_CONNREQUIRED) && 782 (so->so_state & SS_CANTREBIND)) { 783 error = EINVAL; 784 unbind_on_err = 0; 785 eprintsoline(so, error); 786 goto done; 787 } 788 error = sotpi_unbind(so, 0); 789 if (error) { 790 eprintsoline(so, error); 791 goto done; 792 } 793 ASSERT(!(so->so_state & SS_ISBOUND)); 794 if (name == NULL) { 795 so->so_state &= 796 ~(SS_ISCONNECTED|SS_ISCONNECTING); 797 goto done; 798 } 799 } 800 801 /* X/Open requires this check */ 802 if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 803 if (xnet_check_print) { 804 printf("sockfs: X/Open bind state check " 805 "caused EINVAL\n"); 806 } 807 error = EINVAL; 808 goto done; 809 } 810 811 switch (so->so_family) { 812 case AF_UNIX: 813 /* 814 * All AF_UNIX addresses are nul terminated 815 * when copied (copyin_name) in so the minimum 816 * length is 3 bytes. 817 */ 818 if (name == NULL || 819 (ssize_t)namelen <= sizeof (short) + 1) { 820 error = EISDIR; 821 eprintsoline(so, error); 822 goto done; 823 } 824 /* 825 * Verify so_family matches the bound family. 826 * BSD does not check this for AF_UNIX resulting 827 * in funny mknods. 828 */ 829 if (name->sa_family != so->so_family) { 830 error = EAFNOSUPPORT; 831 goto done; 832 } 833 break; 834 case AF_INET: 835 if (name == NULL) { 836 error = EINVAL; 837 eprintsoline(so, error); 838 goto done; 839 } 840 if ((size_t)namelen != sizeof (sin_t)) { 841 error = name->sa_family != so->so_family ? 842 EAFNOSUPPORT : EINVAL; 843 eprintsoline(so, error); 844 goto done; 845 } 846 if ((flags & _SOBIND_XPG4_2) && 847 (name->sa_family != so->so_family)) { 848 /* 849 * This check has to be made for X/Open 850 * sockets however application failures have 851 * been observed when it is applied to 852 * all sockets. 853 */ 854 error = EAFNOSUPPORT; 855 eprintsoline(so, error); 856 goto done; 857 } 858 /* 859 * Force a zero sa_family to match so_family. 860 * 861 * Some programs like inetd(8) don't set the 862 * family field. Other programs leave 863 * sin_family set to garbage - SunOS 4.X does 864 * not check the family field on a bind. 865 * We use the family field that 866 * was passed in to the socket() call. 867 */ 868 name->sa_family = so->so_family; 869 break; 870 871 case AF_INET6: { 872 #ifdef DEBUG 873 sin6_t *sin6 = (sin6_t *)name; 874 #endif /* DEBUG */ 875 876 if (name == NULL) { 877 error = EINVAL; 878 eprintsoline(so, error); 879 goto done; 880 } 881 if ((size_t)namelen != sizeof (sin6_t)) { 882 error = name->sa_family != so->so_family ? 883 EAFNOSUPPORT : EINVAL; 884 eprintsoline(so, error); 885 goto done; 886 } 887 if (name->sa_family != so->so_family) { 888 /* 889 * With IPv6 we require the family to match 890 * unlike in IPv4. 891 */ 892 error = EAFNOSUPPORT; 893 eprintsoline(so, error); 894 goto done; 895 } 896 #ifdef DEBUG 897 /* 898 * Verify that apps don't forget to clear 899 * sin6_scope_id etc 900 */ 901 if (sin6->sin6_scope_id != 0 && 902 !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) { 903 zcmn_err(getzoneid(), CE_WARN, 904 "bind with uninitialized sin6_scope_id " 905 "(%d) on socket. Pid = %d\n", 906 (int)sin6->sin6_scope_id, 907 (int)curproc->p_pid); 908 } 909 if (sin6->__sin6_src_id != 0) { 910 zcmn_err(getzoneid(), CE_WARN, 911 "bind with uninitialized __sin6_src_id " 912 "(%d) on socket. Pid = %d\n", 913 (int)sin6->__sin6_src_id, 914 (int)curproc->p_pid); 915 } 916 #endif /* DEBUG */ 917 break; 918 } 919 default: 920 /* 921 * Don't do any length or sa_family check to allow 922 * non-sockaddr style addresses. 923 */ 924 if (name == NULL) { 925 error = EINVAL; 926 eprintsoline(so, error); 927 goto done; 928 } 929 break; 930 } 931 932 if (namelen > (t_uscalar_t)sti->sti_laddr_maxlen) { 933 error = ENAMETOOLONG; 934 eprintsoline(so, error); 935 goto done; 936 } 937 /* 938 * Save local address. 939 */ 940 sti->sti_laddr_len = (socklen_t)namelen; 941 ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen); 942 bcopy(name, sti->sti_laddr_sa, namelen); 943 944 addr = sti->sti_laddr_sa; 945 addrlen = (t_uscalar_t)sti->sti_laddr_len; 946 switch (so->so_family) { 947 case AF_INET6: 948 case AF_INET: 949 break; 950 case AF_UNIX: { 951 struct sockaddr_un *soun = 952 (struct sockaddr_un *)sti->sti_laddr_sa; 953 struct vnode *vp, *rvp; 954 struct vattr vattr; 955 956 ASSERT(sti->sti_ux_bound_vp == NULL); 957 /* 958 * Create vnode for the specified path name. 959 * Keep vnode held with a reference in sti_ux_bound_vp. 960 * Use the vnode pointer as the address used in the 961 * bind with the transport. 962 * 963 * Use the same mode as in BSD. In particular this does 964 * not observe the umask. 965 */ 966 /* MAXPATHLEN + soun_family + nul termination */ 967 if (sti->sti_laddr_len > 968 (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) { 969 error = ENAMETOOLONG; 970 eprintsoline(so, error); 971 goto done; 972 } 973 vattr.va_type = VSOCK; 974 vattr.va_mode = 0777 & ~PTOU(curproc)->u_cmask; 975 vattr.va_mask = AT_TYPE|AT_MODE; 976 /* NOTE: holding so_lock */ 977 error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr, 978 EXCL, 0, &vp, CRMKNOD, 0, 0); 979 if (error) { 980 if (error == EEXIST) 981 error = EADDRINUSE; 982 eprintsoline(so, error); 983 goto done; 984 } 985 /* 986 * Establish pointer from the underlying filesystem 987 * vnode to the socket node. 988 * sti_ux_bound_vp and v_stream->sd_vnode form the 989 * cross-linkage between the underlying filesystem 990 * node and the socket node. 991 */ 992 993 if ((VOP_REALVP(vp, &rvp, NULL) == 0) && (vp != rvp)) { 994 VN_HOLD(rvp); 995 VN_RELE(vp); 996 vp = rvp; 997 } 998 999 ASSERT(SOTOV(so)->v_stream); 1000 mutex_enter(&vp->v_lock); 1001 vp->v_stream = SOTOV(so)->v_stream; 1002 sti->sti_ux_bound_vp = vp; 1003 mutex_exit(&vp->v_lock); 1004 1005 /* 1006 * Use the vnode pointer value as a unique address 1007 * (together with the magic number to avoid conflicts 1008 * with implicit binds) in the transport provider. 1009 */ 1010 sti->sti_ux_laddr.soua_vp = 1011 (void *)sti->sti_ux_bound_vp; 1012 sti->sti_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT; 1013 addr = &sti->sti_ux_laddr; 1014 addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr); 1015 dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n", 1016 addrlen, 1017 (void *)((struct so_ux_addr *)addr)->soua_vp)); 1018 break; 1019 } 1020 } /* end switch (so->so_family) */ 1021 } 1022 1023 /* 1024 * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since 1025 * the transport can start passing up T_CONN_IND messages 1026 * as soon as it receives the bind req and strsock_proto() 1027 * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs. 1028 */ 1029 if (flags & _SOBIND_LISTEN) { 1030 if ((so->so_state & SS_ACCEPTCONN) == 0) 1031 clear_acceptconn_on_err = B_TRUE; 1032 save_so_backlog = so->so_backlog; 1033 restore_backlog_on_err = B_TRUE; 1034 so->so_state |= SS_ACCEPTCONN; 1035 so->so_backlog = backlog; 1036 } 1037 1038 /* 1039 * We send a T_BIND_REQ for TCP/UDP since we know it supports it, 1040 * for other transports we will send in a O_T_BIND_REQ. 1041 */ 1042 if (tcp_udp_xport && 1043 (so->so_family == AF_INET || so->so_family == AF_INET6)) 1044 PRIM_type = T_BIND_REQ; 1045 1046 bind_req.PRIM_type = PRIM_type; 1047 bind_req.ADDR_length = addrlen; 1048 bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req); 1049 bind_req.CONIND_number = backlog; 1050 /* NOTE: holding so_lock while sleeping */ 1051 mp = soallocproto2(&bind_req, sizeof (bind_req), 1052 addr, addrlen, 0, _ALLOC_SLEEP, cr); 1053 sti->sti_laddr_valid = 0; 1054 1055 /* Done using sti_laddr_sa - can drop the lock */ 1056 mutex_exit(&so->so_lock); 1057 1058 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 1059 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 1060 if (error) { 1061 eprintsoline(so, error); 1062 mutex_enter(&so->so_lock); 1063 goto done; 1064 } 1065 1066 mutex_enter(&so->so_lock); 1067 error = sowaitprim(so, PRIM_type, T_BIND_ACK, 1068 (t_uscalar_t)sizeof (*bind_ack), &mp, 0); 1069 if (error) { 1070 eprintsoline(so, error); 1071 goto done; 1072 } 1073 ASSERT(mp); 1074 /* 1075 * Even if some TPI message (e.g. T_DISCON_IND) was received in 1076 * strsock_proto while the lock was dropped above, the bind 1077 * is allowed to complete. 1078 */ 1079 1080 /* Mark as bound. This will be undone if we detect errors below. */ 1081 if (flags & _SOBIND_NOXLATE) { 1082 ASSERT(so->so_family == AF_UNIX); 1083 sti->sti_faddr_noxlate = 1; 1084 } 1085 ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND)); 1086 so->so_state |= SS_ISBOUND; 1087 ASSERT(sti->sti_unbind_mp); 1088 1089 /* note that we've already set SS_ACCEPTCONN above */ 1090 1091 /* 1092 * Recompute addrlen - an unspecied bind sent down an 1093 * address of length zero but we expect the appropriate length 1094 * in return. 1095 */ 1096 addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ? 1097 sizeof (sti->sti_ux_laddr) : sti->sti_laddr_len); 1098 1099 bind_ack = (struct T_bind_ack *)mp->b_rptr; 1100 /* 1101 * The alignment restriction is really too strict but 1102 * we want enough alignment to inspect the fields of 1103 * a sockaddr_in. 1104 */ 1105 addr = sogetoff(mp, bind_ack->ADDR_offset, 1106 bind_ack->ADDR_length, 1107 __TPI_ALIGN_SIZE); 1108 if (addr == NULL) { 1109 freemsg(mp); 1110 error = EPROTO; 1111 eprintsoline(so, error); 1112 goto done; 1113 } 1114 if (!(flags & _SOBIND_UNSPEC)) { 1115 /* 1116 * Verify that the transport didn't return something we 1117 * did not want e.g. an address other than what we asked for. 1118 * 1119 * NOTE: These checks would go away if/when we switch to 1120 * using the new TPI (in which the transport would fail 1121 * the request instead of assigning a different address). 1122 * 1123 * NOTE2: For protocols that we don't know (i.e. any 1124 * other than AF_INET6, AF_INET and AF_UNIX), we 1125 * cannot know if the transport should be expected to 1126 * return the same address as that requested. 1127 * 1128 * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send 1129 * down a T_BIND_REQ. We use O_T_BIND_REQ for others. 1130 * 1131 * For example, in the case of netatalk it may be 1132 * inappropriate for the transport to return the 1133 * requested address (as it may have allocated a local 1134 * port number in behaviour similar to that of an 1135 * AF_INET bind request with a port number of zero). 1136 * 1137 * Given the definition of O_T_BIND_REQ, where the 1138 * transport may bind to an address other than the 1139 * requested address, it's not possible to determine 1140 * whether a returned address that differs from the 1141 * requested address is a reason to fail (because the 1142 * requested address was not available) or succeed 1143 * (because the transport allocated an appropriate 1144 * address and/or port). 1145 * 1146 * sockfs currently requires that the transport return 1147 * the requested address in the T_BIND_ACK, unless 1148 * there is code here to allow for any discrepancy. 1149 * Such code exists for AF_INET and AF_INET6. 1150 * 1151 * Netatalk chooses to return the requested address 1152 * rather than the (correct) allocated address. This 1153 * means that netatalk violates the TPI specification 1154 * (and would not function correctly if used from a 1155 * TLI application), but it does mean that it works 1156 * with sockfs. 1157 * 1158 * As noted above, using the newer XTI bind primitive 1159 * (T_BIND_REQ) in preference to O_T_BIND_REQ would 1160 * allow sockfs to be more sure about whether or not 1161 * the bind request had succeeded (as transports are 1162 * not permitted to bind to a different address than 1163 * that requested - they must return failure). 1164 * Unfortunately, support for T_BIND_REQ may not be 1165 * present in all transport implementations (netatalk, 1166 * for example, doesn't have it), making the 1167 * transition difficult. 1168 */ 1169 if (bind_ack->ADDR_length != addrlen) { 1170 /* Assumes that the requested address was in use */ 1171 freemsg(mp); 1172 error = EADDRINUSE; 1173 eprintsoline(so, error); 1174 goto done; 1175 } 1176 1177 switch (so->so_family) { 1178 case AF_INET6: 1179 case AF_INET: { 1180 sin_t *rname, *aname; 1181 1182 rname = (sin_t *)addr; 1183 aname = (sin_t *)sti->sti_laddr_sa; 1184 1185 /* 1186 * Take advantage of the alignment 1187 * of sin_port and sin6_port which fall 1188 * in the same place in their data structures. 1189 * Just use sin_port for either address family. 1190 * 1191 * This may become a problem if (heaven forbid) 1192 * there's a separate ipv6port_reserved... :-P 1193 * 1194 * Binding to port 0 has the semantics of letting 1195 * the transport bind to any port. 1196 * 1197 * If the transport is TCP or UDP since we had sent 1198 * a T_BIND_REQ we would not get a port other than 1199 * what we asked for. 1200 */ 1201 if (tcp_udp_xport) { 1202 /* 1203 * Pick up the new port number if we bound to 1204 * port 0. 1205 */ 1206 if (aname->sin_port == 0) 1207 aname->sin_port = rname->sin_port; 1208 sti->sti_laddr_valid = 1; 1209 break; 1210 } 1211 if (aname->sin_port != 0 && 1212 aname->sin_port != rname->sin_port) { 1213 freemsg(mp); 1214 error = EADDRINUSE; 1215 eprintsoline(so, error); 1216 goto done; 1217 } 1218 /* 1219 * Pick up the new port number if we bound to port 0. 1220 */ 1221 aname->sin_port = rname->sin_port; 1222 1223 /* 1224 * Unfortunately, addresses aren't _quite_ the same. 1225 */ 1226 if (so->so_family == AF_INET) { 1227 if (aname->sin_addr.s_addr != 1228 rname->sin_addr.s_addr) { 1229 freemsg(mp); 1230 error = EADDRNOTAVAIL; 1231 eprintsoline(so, error); 1232 goto done; 1233 } 1234 } else { 1235 sin6_t *rname6 = (sin6_t *)rname; 1236 sin6_t *aname6 = (sin6_t *)aname; 1237 1238 if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr, 1239 &rname6->sin6_addr)) { 1240 freemsg(mp); 1241 error = EADDRNOTAVAIL; 1242 eprintsoline(so, error); 1243 goto done; 1244 } 1245 } 1246 break; 1247 } 1248 case AF_UNIX: 1249 if (bcmp(addr, &sti->sti_ux_laddr, addrlen) != 0) { 1250 freemsg(mp); 1251 error = EADDRINUSE; 1252 eprintsoline(so, error); 1253 eprintso(so, 1254 ("addrlen %d, addr 0x%x, vp %p\n", 1255 addrlen, *((int *)addr), 1256 (void *)sti->sti_ux_bound_vp)); 1257 goto done; 1258 } 1259 sti->sti_laddr_valid = 1; 1260 break; 1261 default: 1262 /* 1263 * NOTE: This assumes that addresses can be 1264 * byte-compared for equivalence. 1265 */ 1266 if (bcmp(addr, sti->sti_laddr_sa, addrlen) != 0) { 1267 freemsg(mp); 1268 error = EADDRINUSE; 1269 eprintsoline(so, error); 1270 goto done; 1271 } 1272 /* 1273 * Don't mark sti_laddr_valid, as we cannot be 1274 * sure that the returned address is the real 1275 * bound address when talking to an unknown 1276 * transport. 1277 */ 1278 break; 1279 } 1280 } else { 1281 /* 1282 * Save for returned address for getsockname. 1283 * Needed for unspecific bind unless transport supports 1284 * the TI_GETMYNAME ioctl. 1285 * Do this for AF_INET{,6} even though they do, as 1286 * caching info here is much better performance than 1287 * a TPI/STREAMS trip to the transport for getsockname. 1288 * Any which can't for some reason _must_ _not_ set 1289 * sti_laddr_valid here for the caching version of 1290 * getsockname to not break; 1291 */ 1292 switch (so->so_family) { 1293 case AF_UNIX: 1294 /* 1295 * Record the address bound with the transport 1296 * for use by socketpair. 1297 */ 1298 bcopy(addr, &sti->sti_ux_laddr, addrlen); 1299 sti->sti_laddr_valid = 1; 1300 break; 1301 case AF_INET: 1302 case AF_INET6: 1303 ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen); 1304 bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len); 1305 sti->sti_laddr_valid = 1; 1306 break; 1307 default: 1308 /* 1309 * Don't mark sti_laddr_valid, as we cannot be 1310 * sure that the returned address is the real 1311 * bound address when talking to an unknown 1312 * transport. 1313 */ 1314 break; 1315 } 1316 } 1317 1318 freemsg(mp); 1319 1320 done: 1321 if (error) { 1322 /* reset state & backlog to values held on entry */ 1323 if (clear_acceptconn_on_err == B_TRUE) 1324 so->so_state &= ~SS_ACCEPTCONN; 1325 if (restore_backlog_on_err == B_TRUE) 1326 so->so_backlog = save_so_backlog; 1327 1328 if (unbind_on_err && so->so_state & SS_ISBOUND) { 1329 int err; 1330 1331 err = sotpi_unbind(so, 0); 1332 /* LINTED - statement has no consequent: if */ 1333 if (err) { 1334 eprintsoline(so, error); 1335 } else { 1336 ASSERT(!(so->so_state & SS_ISBOUND)); 1337 } 1338 } 1339 } 1340 if (!(flags & _SOBIND_LOCK_HELD)) { 1341 so_unlock_single(so, SOLOCKED); 1342 mutex_exit(&so->so_lock); 1343 } else { 1344 ASSERT(MUTEX_HELD(&so->so_lock)); 1345 ASSERT(so->so_flag & SOLOCKED); 1346 } 1347 return (error); 1348 } 1349 1350 /* bind the socket */ 1351 static int 1352 sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen, 1353 int flags, struct cred *cr) 1354 { 1355 if ((flags & _SOBIND_SOCKETPAIR) == 0) 1356 return (sotpi_bindlisten(so, name, namelen, 0, flags, cr)); 1357 1358 flags &= ~_SOBIND_SOCKETPAIR; 1359 return (sotpi_bindlisten(so, name, namelen, 1, flags, cr)); 1360 } 1361 1362 /* 1363 * Unbind a socket - used when bind() fails, when bind() specifies a NULL 1364 * address, or when listen needs to unbind and bind. 1365 * If the _SOUNBIND_REBIND flag is specified the addresses are retained 1366 * so that a sobind can pick them up. 1367 */ 1368 static int 1369 sotpi_unbind(struct sonode *so, int flags) 1370 { 1371 struct T_unbind_req unbind_req; 1372 int error = 0; 1373 mblk_t *mp; 1374 sotpi_info_t *sti = SOTOTPI(so); 1375 1376 dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n", 1377 (void *)so, flags, pr_state(so->so_state, so->so_mode))); 1378 1379 ASSERT(MUTEX_HELD(&so->so_lock)); 1380 ASSERT(so->so_flag & SOLOCKED); 1381 1382 if (!(so->so_state & SS_ISBOUND)) { 1383 error = EINVAL; 1384 eprintsoline(so, error); 1385 goto done; 1386 } 1387 1388 mutex_exit(&so->so_lock); 1389 1390 /* 1391 * Flush the read and write side (except stream head read queue) 1392 * and send down T_UNBIND_REQ. 1393 */ 1394 (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW); 1395 1396 unbind_req.PRIM_type = T_UNBIND_REQ; 1397 mp = soallocproto1(&unbind_req, sizeof (unbind_req), 1398 0, _ALLOC_SLEEP, CRED()); 1399 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 1400 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 1401 mutex_enter(&so->so_lock); 1402 if (error) { 1403 eprintsoline(so, error); 1404 goto done; 1405 } 1406 1407 error = sowaitokack(so, T_UNBIND_REQ); 1408 if (error) { 1409 eprintsoline(so, error); 1410 goto done; 1411 } 1412 1413 /* 1414 * Even if some TPI message (e.g. T_DISCON_IND) was received in 1415 * strsock_proto while the lock was dropped above, the unbind 1416 * is allowed to complete. 1417 */ 1418 if (!(flags & _SOUNBIND_REBIND)) { 1419 /* 1420 * Clear out bound address. 1421 */ 1422 vnode_t *vp; 1423 1424 if ((vp = sti->sti_ux_bound_vp) != NULL) { 1425 sti->sti_ux_bound_vp = NULL; 1426 vn_rele_stream(vp); 1427 } 1428 /* Clear out address */ 1429 sti->sti_laddr_len = 0; 1430 } 1431 so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN); 1432 sti->sti_laddr_valid = 0; 1433 1434 done: 1435 1436 /* If the caller held the lock don't release it here */ 1437 ASSERT(MUTEX_HELD(&so->so_lock)); 1438 ASSERT(so->so_flag & SOLOCKED); 1439 1440 return (error); 1441 } 1442 1443 /* 1444 * listen on the socket. 1445 * For TPI conforming transports this has to first unbind with the transport 1446 * and then bind again using the new backlog. 1447 */ 1448 /* ARGSUSED */ 1449 int 1450 sotpi_listen(struct sonode *so, int backlog, struct cred *cr) 1451 { 1452 int error = 0; 1453 sotpi_info_t *sti = SOTOTPI(so); 1454 1455 dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n", 1456 (void *)so, backlog, pr_state(so->so_state, so->so_mode))); 1457 1458 if (sti->sti_serv_type == T_CLTS) 1459 return (EOPNOTSUPP); 1460 1461 /* 1462 * If the socket is ready to accept connections already, then 1463 * return without doing anything. This avoids a problem where 1464 * a second listen() call fails if a connection is pending and 1465 * leaves the socket unbound. Only when we are not unbinding 1466 * with the transport can we safely increase the backlog. 1467 */ 1468 if (so->so_state & SS_ACCEPTCONN && 1469 !((so->so_family == AF_INET || so->so_family == AF_INET6) && 1470 /*CONSTCOND*/ 1471 !solisten_tpi_tcp)) 1472 return (0); 1473 1474 if (so->so_state & SS_ISCONNECTED) 1475 return (EINVAL); 1476 1477 mutex_enter(&so->so_lock); 1478 so_lock_single(so); /* Set SOLOCKED */ 1479 1480 /* 1481 * If the listen doesn't change the backlog we do nothing. 1482 * This avoids an EPROTO error from the transport. 1483 */ 1484 if ((so->so_state & SS_ACCEPTCONN) && 1485 so->so_backlog == backlog) 1486 goto done; 1487 1488 if (!(so->so_state & SS_ISBOUND)) { 1489 /* 1490 * Must have been explicitly bound in the UNIX domain. 1491 */ 1492 if (so->so_family == AF_UNIX) { 1493 error = EINVAL; 1494 goto done; 1495 } 1496 error = sotpi_bindlisten(so, NULL, 0, backlog, 1497 _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr); 1498 } else if (backlog > 0) { 1499 /* 1500 * AF_INET{,6} hack to avoid losing the port. 1501 * Assumes that all AF_INET{,6} transports can handle a 1502 * O_T_BIND_REQ with a non-zero CONIND_number when the TPI 1503 * has already bound thus it is possible to avoid the unbind. 1504 */ 1505 if (!((so->so_family == AF_INET || so->so_family == AF_INET6) && 1506 /*CONSTCOND*/ 1507 !solisten_tpi_tcp)) { 1508 error = sotpi_unbind(so, _SOUNBIND_REBIND); 1509 if (error) 1510 goto done; 1511 } 1512 error = sotpi_bindlisten(so, NULL, 0, backlog, 1513 _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr); 1514 } else { 1515 so->so_state |= SS_ACCEPTCONN; 1516 so->so_backlog = backlog; 1517 } 1518 if (error) 1519 goto done; 1520 ASSERT(so->so_state & SS_ACCEPTCONN); 1521 done: 1522 so_unlock_single(so, SOLOCKED); 1523 mutex_exit(&so->so_lock); 1524 return (error); 1525 } 1526 1527 /* 1528 * Disconnect either a specified seqno or all (-1). 1529 * The former is used on listening sockets only. 1530 * 1531 * When seqno == -1 sodisconnect could call sotpi_unbind. However, 1532 * the current use of sodisconnect(seqno == -1) is only for shutdown 1533 * so there is no point (and potentially incorrect) to unbind. 1534 */ 1535 static int 1536 sodisconnect(struct sonode *so, t_scalar_t seqno, int flags) 1537 { 1538 struct T_discon_req discon_req; 1539 int error = 0; 1540 mblk_t *mp; 1541 1542 dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n", 1543 (void *)so, seqno, flags, pr_state(so->so_state, so->so_mode))); 1544 1545 if (!(flags & _SODISCONNECT_LOCK_HELD)) { 1546 mutex_enter(&so->so_lock); 1547 so_lock_single(so); /* Set SOLOCKED */ 1548 } else { 1549 ASSERT(MUTEX_HELD(&so->so_lock)); 1550 ASSERT(so->so_flag & SOLOCKED); 1551 } 1552 1553 if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) { 1554 error = EINVAL; 1555 eprintsoline(so, error); 1556 goto done; 1557 } 1558 1559 mutex_exit(&so->so_lock); 1560 /* 1561 * Flush the write side (unless this is a listener) 1562 * and then send down a T_DISCON_REQ. 1563 * (Don't flush on listener since it could flush {O_}T_CONN_RES 1564 * and other messages.) 1565 */ 1566 if (!(so->so_state & SS_ACCEPTCONN)) 1567 (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW); 1568 1569 discon_req.PRIM_type = T_DISCON_REQ; 1570 discon_req.SEQ_number = seqno; 1571 mp = soallocproto1(&discon_req, sizeof (discon_req), 1572 0, _ALLOC_SLEEP, CRED()); 1573 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 1574 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 1575 mutex_enter(&so->so_lock); 1576 if (error) { 1577 eprintsoline(so, error); 1578 goto done; 1579 } 1580 1581 error = sowaitokack(so, T_DISCON_REQ); 1582 if (error) { 1583 eprintsoline(so, error); 1584 goto done; 1585 } 1586 /* 1587 * Even if some TPI message (e.g. T_DISCON_IND) was received in 1588 * strsock_proto while the lock was dropped above, the disconnect 1589 * is allowed to complete. However, it is not possible to 1590 * assert that SS_ISCONNECTED|SS_ISCONNECTING are set. 1591 */ 1592 so->so_state &= ~(SS_ISCONNECTED|SS_ISCONNECTING); 1593 SOTOTPI(so)->sti_laddr_valid = 0; 1594 SOTOTPI(so)->sti_faddr_valid = 0; 1595 done: 1596 if (!(flags & _SODISCONNECT_LOCK_HELD)) { 1597 so_unlock_single(so, SOLOCKED); 1598 mutex_exit(&so->so_lock); 1599 } else { 1600 /* If the caller held the lock don't release it here */ 1601 ASSERT(MUTEX_HELD(&so->so_lock)); 1602 ASSERT(so->so_flag & SOLOCKED); 1603 } 1604 return (error); 1605 } 1606 1607 /* ARGSUSED */ 1608 int 1609 sotpi_accept(struct sonode *so, int fflag, struct cred *cr, 1610 struct sonode **nsop) 1611 { 1612 struct T_conn_ind *conn_ind; 1613 struct T_conn_res *conn_res; 1614 int error = 0; 1615 mblk_t *mp, *ack_mp; 1616 struct sonode *nso; 1617 vnode_t *nvp; 1618 void *src; 1619 t_uscalar_t srclen; 1620 void *opt; 1621 t_uscalar_t optlen; 1622 t_scalar_t PRIM_type; 1623 t_scalar_t SEQ_number; 1624 size_t sinlen; 1625 sotpi_info_t *sti = SOTOTPI(so); 1626 sotpi_info_t *nsti; 1627 1628 dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n", 1629 (void *)so, fflag, (void *)nsop, 1630 pr_state(so->so_state, so->so_mode))); 1631 1632 /* 1633 * Defer single-threading the accepting socket until 1634 * the T_CONN_IND has been received and parsed and the 1635 * new sonode has been opened. 1636 */ 1637 1638 /* Check that we are not already connected */ 1639 if ((so->so_state & SS_ACCEPTCONN) == 0) 1640 goto conn_bad; 1641 1642 if ((error = sowaitconnind(so, fflag, &mp)) != 0) 1643 goto e_bad; 1644 1645 ASSERT(mp != NULL); 1646 conn_ind = (struct T_conn_ind *)mp->b_rptr; 1647 1648 /* 1649 * Save SEQ_number for error paths. 1650 */ 1651 SEQ_number = conn_ind->SEQ_number; 1652 1653 srclen = conn_ind->SRC_length; 1654 src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1); 1655 if (src == NULL) { 1656 error = EPROTO; 1657 freemsg(mp); 1658 eprintsoline(so, error); 1659 goto disconnect_unlocked; 1660 } 1661 optlen = conn_ind->OPT_length; 1662 switch (so->so_family) { 1663 case AF_INET: 1664 case AF_INET6: 1665 if ((optlen == sizeof (intptr_t)) && (sti->sti_direct != 0)) { 1666 bcopy(mp->b_rptr + conn_ind->OPT_offset, 1667 &opt, conn_ind->OPT_length); 1668 } else { 1669 /* 1670 * The transport (in this case TCP) hasn't sent up 1671 * a pointer to an instance for the accept fast-path. 1672 * Disable fast-path completely because the call to 1673 * sotpi_create() below would otherwise create an 1674 * incomplete TCP instance, which would lead to 1675 * problems when sockfs sends a normal T_CONN_RES 1676 * message down the new stream. 1677 */ 1678 if (sti->sti_direct) { 1679 int rval; 1680 /* 1681 * For consistency we inform tcp to disable 1682 * direct interface on the listener, though 1683 * we can certainly live without doing this 1684 * because no data will ever travel upstream 1685 * on the listening socket. 1686 */ 1687 sti->sti_direct = 0; 1688 (void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK, 1689 0, 0, K_TO_K, cr, &rval); 1690 } 1691 opt = NULL; 1692 optlen = 0; 1693 } 1694 break; 1695 case AF_UNIX: 1696 default: 1697 if (optlen != 0) { 1698 opt = sogetoff(mp, conn_ind->OPT_offset, optlen, 1699 __TPI_ALIGN_SIZE); 1700 if (opt == NULL) { 1701 error = EPROTO; 1702 freemsg(mp); 1703 eprintsoline(so, error); 1704 goto disconnect_unlocked; 1705 } 1706 } 1707 if (so->so_family == AF_UNIX) { 1708 if (!sti->sti_faddr_noxlate) { 1709 src = NULL; 1710 srclen = 0; 1711 } 1712 /* Extract src address from options */ 1713 if (optlen != 0) 1714 so_getopt_srcaddr(opt, optlen, &src, &srclen); 1715 } 1716 break; 1717 } 1718 1719 /* 1720 * Create the new socket. 1721 */ 1722 nso = socket_newconn(so, NULL, NULL, SOCKET_SLEEP, &error); 1723 if (nso == NULL) { 1724 ASSERT(error != 0); 1725 /* 1726 * Accept can not fail with ENOBUFS. sotpi_create 1727 * sleeps waiting for memory until a signal is caught 1728 * so return EINTR. 1729 */ 1730 freemsg(mp); 1731 if (error == ENOBUFS) 1732 error = EINTR; 1733 goto e_disc_unl; 1734 } 1735 nvp = SOTOV(nso); 1736 nsti = SOTOTPI(nso); 1737 1738 #ifdef DEBUG 1739 /* 1740 * SO_DEBUG is used to trigger the dprint* and eprint* macros thus 1741 * it's inherited early to allow debugging of the accept code itself. 1742 */ 1743 nso->so_options |= so->so_options & SO_DEBUG; 1744 #endif /* DEBUG */ 1745 1746 /* 1747 * Save the SRC address from the T_CONN_IND 1748 * for getpeername to work on AF_UNIX and on transports that do not 1749 * support TI_GETPEERNAME. 1750 * 1751 * NOTE: AF_UNIX NUL termination is ensured by the sender's 1752 * copyin_name(). 1753 */ 1754 if (srclen > (t_uscalar_t)nsti->sti_faddr_maxlen) { 1755 error = EINVAL; 1756 freemsg(mp); 1757 eprintsoline(so, error); 1758 goto disconnect_vp_unlocked; 1759 } 1760 nsti->sti_faddr_len = (socklen_t)srclen; 1761 ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen); 1762 bcopy(src, nsti->sti_faddr_sa, srclen); 1763 nsti->sti_faddr_valid = 1; 1764 1765 /* 1766 * Record so_peercred and so_cpid from a cred in the T_CONN_IND. 1767 */ 1768 if ((DB_REF(mp) > 1) || MBLKSIZE(mp) < 1769 (sizeof (struct T_conn_res) + sizeof (intptr_t))) { 1770 cred_t *cr; 1771 pid_t cpid; 1772 1773 cr = msg_getcred(mp, &cpid); 1774 if (cr != NULL) { 1775 crhold(cr); 1776 nso->so_peercred = cr; 1777 nso->so_cpid = cpid; 1778 } 1779 freemsg(mp); 1780 1781 mp = soallocproto1(NULL, sizeof (struct T_conn_res) + 1782 sizeof (intptr_t), 0, _ALLOC_INTR, cr); 1783 if (mp == NULL) { 1784 /* 1785 * Accept can not fail with ENOBUFS. 1786 * A signal was caught so return EINTR. 1787 */ 1788 error = EINTR; 1789 eprintsoline(so, error); 1790 goto disconnect_vp_unlocked; 1791 } 1792 conn_res = (struct T_conn_res *)mp->b_rptr; 1793 } else { 1794 /* 1795 * For efficency reasons we use msg_extractcred; no crhold 1796 * needed since db_credp is cleared (i.e., we move the cred 1797 * from the message to so_peercred. 1798 */ 1799 nso->so_peercred = msg_extractcred(mp, &nso->so_cpid); 1800 1801 mp->b_rptr = DB_BASE(mp); 1802 conn_res = (struct T_conn_res *)mp->b_rptr; 1803 mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res); 1804 1805 mblk_setcred(mp, cr, curproc->p_pid); 1806 } 1807 1808 /* 1809 * New socket must be bound at least in sockfs and, except for AF_INET, 1810 * (or AF_INET6) it also has to be bound in the transport provider. 1811 * We set the local address in the sonode from the T_OK_ACK of the 1812 * T_CONN_RES. For this reason the address we bind to here isn't 1813 * important. 1814 */ 1815 if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) && 1816 /*CONSTCOND*/ 1817 nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) { 1818 /* 1819 * Optimization for AF_INET{,6} transports 1820 * that can handle a T_CONN_RES without being bound. 1821 */ 1822 mutex_enter(&nso->so_lock); 1823 so_automatic_bind(nso); 1824 mutex_exit(&nso->so_lock); 1825 } else { 1826 /* Perform NULL bind with the transport provider. */ 1827 if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC, 1828 cr)) != 0) { 1829 ASSERT(error != ENOBUFS); 1830 freemsg(mp); 1831 eprintsoline(nso, error); 1832 goto disconnect_vp_unlocked; 1833 } 1834 } 1835 1836 /* 1837 * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES 1838 * so that any data arriving on the new socket will cause the 1839 * appropriate signals to be delivered for the new socket. 1840 * 1841 * No other thread (except strsock_proto and strsock_misc) 1842 * can access the new socket thus we relax the locking. 1843 */ 1844 nso->so_pgrp = so->so_pgrp; 1845 nso->so_state |= so->so_state & SS_ASYNC; 1846 nsti->sti_faddr_noxlate = sti->sti_faddr_noxlate; 1847 1848 if (nso->so_pgrp != 0) { 1849 if ((error = so_set_events(nso, nvp, cr)) != 0) { 1850 eprintsoline(nso, error); 1851 error = 0; 1852 nso->so_pgrp = 0; 1853 } 1854 } 1855 1856 /* 1857 * Make note of the socket level options. TCP and IP level options 1858 * are already inherited. We could do all this after accept is 1859 * successful but doing it here simplifies code and no harm done 1860 * for error case. 1861 */ 1862 nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE| 1863 SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK| 1864 SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER); 1865 nso->so_sndbuf = so->so_sndbuf; 1866 nso->so_rcvbuf = so->so_rcvbuf; 1867 if (nso->so_options & SO_LINGER) 1868 nso->so_linger = so->so_linger; 1869 1870 /* 1871 * Note that the following sti_direct code path should be 1872 * removed once we are confident that the direct sockets 1873 * do not result in any degradation. 1874 */ 1875 if (sti->sti_direct) { 1876 1877 ASSERT(opt != NULL); 1878 1879 conn_res->OPT_length = optlen; 1880 conn_res->OPT_offset = MBLKL(mp); 1881 bcopy(&opt, mp->b_wptr, optlen); 1882 mp->b_wptr += optlen; 1883 conn_res->PRIM_type = T_CONN_RES; 1884 conn_res->ACCEPTOR_id = 0; 1885 PRIM_type = T_CONN_RES; 1886 1887 /* Send down the T_CONN_RES on acceptor STREAM */ 1888 error = kstrputmsg(SOTOV(nso), mp, NULL, 1889 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 1890 if (error) { 1891 mutex_enter(&so->so_lock); 1892 so_lock_single(so); 1893 eprintsoline(so, error); 1894 goto disconnect_vp; 1895 } 1896 mutex_enter(&nso->so_lock); 1897 error = sowaitprim(nso, T_CONN_RES, T_OK_ACK, 1898 (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0); 1899 if (error) { 1900 mutex_exit(&nso->so_lock); 1901 mutex_enter(&so->so_lock); 1902 so_lock_single(so); 1903 eprintsoline(so, error); 1904 goto disconnect_vp; 1905 } 1906 if (nso->so_family == AF_INET) { 1907 sin_t *sin; 1908 1909 sin = (sin_t *)(ack_mp->b_rptr + 1910 sizeof (struct T_ok_ack)); 1911 bcopy(sin, nsti->sti_laddr_sa, sizeof (sin_t)); 1912 nsti->sti_laddr_len = sizeof (sin_t); 1913 } else { 1914 sin6_t *sin6; 1915 1916 sin6 = (sin6_t *)(ack_mp->b_rptr + 1917 sizeof (struct T_ok_ack)); 1918 bcopy(sin6, nsti->sti_laddr_sa, sizeof (sin6_t)); 1919 nsti->sti_laddr_len = sizeof (sin6_t); 1920 } 1921 freemsg(ack_mp); 1922 1923 nso->so_state |= SS_ISCONNECTED; 1924 nso->so_proto_handle = (sock_lower_handle_t)opt; 1925 nsti->sti_laddr_valid = 1; 1926 1927 mutex_exit(&nso->so_lock); 1928 1929 /* 1930 * It's possible, through the use of autopush for example, 1931 * that the acceptor stream may not support sti_direct 1932 * semantics. If the new socket does not support sti_direct 1933 * we issue a _SIOCSOCKFALLBACK to inform the transport 1934 * as we would in the I_PUSH case. 1935 */ 1936 if (nsti->sti_direct == 0) { 1937 int rval; 1938 1939 if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK, 1940 0, 0, K_TO_K, cr, &rval)) != 0) { 1941 mutex_enter(&so->so_lock); 1942 so_lock_single(so); 1943 eprintsoline(so, error); 1944 goto disconnect_vp; 1945 } 1946 } 1947 1948 /* 1949 * Pass out new socket. 1950 */ 1951 if (nsop != NULL) 1952 *nsop = nso; 1953 1954 return (0); 1955 } 1956 1957 /* 1958 * This is the non-performance case for sockets (e.g. AF_UNIX sockets) 1959 * which don't support the FireEngine accept fast-path. It is also 1960 * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd 1961 * again. Neither sockfs nor TCP attempt to find out if some other 1962 * random module has been inserted in between (in which case we 1963 * should follow TLI accept behaviour). We blindly assume the worst 1964 * case and revert back to old behaviour i.e. TCP will not send us 1965 * any option (eager) and the accept should happen on the listener 1966 * queue. Any queued T_conn_ind have already got their options removed 1967 * by so_sock2_stream() when "sockmod" was I_POP'd. 1968 */ 1969 /* 1970 * Fill in the {O_}T_CONN_RES before getting SOLOCKED. 1971 */ 1972 if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) { 1973 #ifdef _ILP32 1974 queue_t *q; 1975 1976 /* 1977 * Find read queue in driver 1978 * Can safely do this since we "own" nso/nvp. 1979 */ 1980 q = strvp2wq(nvp)->q_next; 1981 while (SAMESTR(q)) 1982 q = q->q_next; 1983 q = RD(q); 1984 conn_res->ACCEPTOR_id = (t_uscalar_t)q; 1985 #else 1986 conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev); 1987 #endif /* _ILP32 */ 1988 conn_res->PRIM_type = O_T_CONN_RES; 1989 PRIM_type = O_T_CONN_RES; 1990 } else { 1991 conn_res->ACCEPTOR_id = nsti->sti_acceptor_id; 1992 conn_res->PRIM_type = T_CONN_RES; 1993 PRIM_type = T_CONN_RES; 1994 } 1995 conn_res->SEQ_number = SEQ_number; 1996 conn_res->OPT_length = 0; 1997 conn_res->OPT_offset = 0; 1998 1999 mutex_enter(&so->so_lock); 2000 so_lock_single(so); /* Set SOLOCKED */ 2001 mutex_exit(&so->so_lock); 2002 2003 error = kstrputmsg(SOTOV(so), mp, NULL, 2004 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 2005 mutex_enter(&so->so_lock); 2006 if (error) { 2007 eprintsoline(so, error); 2008 goto disconnect_vp; 2009 } 2010 error = sowaitprim(so, PRIM_type, T_OK_ACK, 2011 (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0); 2012 if (error) { 2013 eprintsoline(so, error); 2014 goto disconnect_vp; 2015 } 2016 mutex_exit(&so->so_lock); 2017 /* 2018 * If there is a sin/sin6 appended onto the T_OK_ACK use 2019 * that to set the local address. If this is not present 2020 * then we zero out the address and don't set the 2021 * sti_laddr_valid bit. For AF_UNIX endpoints we copy over 2022 * the pathname from the listening socket. 2023 * In the case where this is TCP or an AF_UNIX socket the 2024 * client side may have queued data or a T_ORDREL in the 2025 * transport. Having now sent the T_CONN_RES we may receive 2026 * those queued messages at any time. Hold the acceptor 2027 * so_lock until its state and laddr are finalized. 2028 */ 2029 mutex_enter(&nso->so_lock); 2030 sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t); 2031 if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) && 2032 MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) { 2033 ack_mp->b_rptr += sizeof (struct T_ok_ack); 2034 bcopy(ack_mp->b_rptr, nsti->sti_laddr_sa, sinlen); 2035 nsti->sti_laddr_len = sinlen; 2036 nsti->sti_laddr_valid = 1; 2037 } else if (nso->so_family == AF_UNIX) { 2038 ASSERT(so->so_family == AF_UNIX); 2039 nsti->sti_laddr_len = sti->sti_laddr_len; 2040 ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen); 2041 bcopy(sti->sti_laddr_sa, nsti->sti_laddr_sa, 2042 nsti->sti_laddr_len); 2043 nsti->sti_laddr_valid = 1; 2044 } else { 2045 nsti->sti_laddr_len = sti->sti_laddr_len; 2046 ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen); 2047 bzero(nsti->sti_laddr_sa, nsti->sti_addr_size); 2048 nsti->sti_laddr_sa->sa_family = nso->so_family; 2049 } 2050 nso->so_state |= SS_ISCONNECTED; 2051 mutex_exit(&nso->so_lock); 2052 2053 freemsg(ack_mp); 2054 2055 mutex_enter(&so->so_lock); 2056 so_unlock_single(so, SOLOCKED); 2057 mutex_exit(&so->so_lock); 2058 2059 /* 2060 * Pass out new socket. 2061 */ 2062 if (nsop != NULL) 2063 *nsop = nso; 2064 2065 return (0); 2066 2067 e_disc_unl: 2068 eprintsoline(so, error); 2069 goto disconnect_unlocked; 2070 2071 disconnect_vp_unlocked: 2072 (void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL); 2073 VN_RELE(nvp); 2074 disconnect_unlocked: 2075 (void) sodisconnect(so, SEQ_number, 0); 2076 return (error); 2077 2078 disconnect_vp: 2079 (void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD); 2080 so_unlock_single(so, SOLOCKED); 2081 mutex_exit(&so->so_lock); 2082 (void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL); 2083 VN_RELE(nvp); 2084 return (error); 2085 2086 conn_bad: /* Note: SunOS 4/BSD unconditionally returns EINVAL here */ 2087 error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW) 2088 ? EOPNOTSUPP : EINVAL; 2089 e_bad: 2090 eprintsoline(so, error); 2091 return (error); 2092 } 2093 2094 /* 2095 * connect a socket. 2096 * 2097 * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to 2098 * unconnect (by specifying a null address). 2099 */ 2100 int 2101 sotpi_connect(struct sonode *so, 2102 struct sockaddr *name, 2103 socklen_t namelen, 2104 int fflag, 2105 int flags, 2106 struct cred *cr) 2107 { 2108 struct T_conn_req conn_req; 2109 int error = 0; 2110 mblk_t *mp; 2111 void *src; 2112 socklen_t srclen; 2113 void *addr; 2114 socklen_t addrlen; 2115 boolean_t need_unlock; 2116 sotpi_info_t *sti = SOTOTPI(so); 2117 2118 dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n", 2119 (void *)so, (void *)name, namelen, fflag, flags, 2120 pr_state(so->so_state, so->so_mode))); 2121 2122 /* 2123 * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to 2124 * avoid sleeping for memory with SOLOCKED held. 2125 * We know that the T_CONN_REQ can't be larger than 2 * sti_faddr_maxlen 2126 * + sizeof (struct T_opthdr). 2127 * (the AF_UNIX so_ux_addr_xlate() does not make the address 2128 * exceed sti_faddr_maxlen). 2129 */ 2130 mp = soallocproto(sizeof (struct T_conn_req) + 2131 2 * sti->sti_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR, 2132 cr); 2133 if (mp == NULL) { 2134 /* 2135 * Connect can not fail with ENOBUFS. A signal was 2136 * caught so return EINTR. 2137 */ 2138 error = EINTR; 2139 eprintsoline(so, error); 2140 return (error); 2141 } 2142 2143 mutex_enter(&so->so_lock); 2144 /* 2145 * Make sure there is a preallocated T_unbind_req message 2146 * before any binding. This message is allocated when the 2147 * socket is created. Since another thread can consume 2148 * so_unbind_mp by the time we return from so_lock_single(), 2149 * we should check the availability of so_unbind_mp after 2150 * we return from so_lock_single(). 2151 */ 2152 2153 so_lock_single(so); /* Set SOLOCKED */ 2154 need_unlock = B_TRUE; 2155 2156 if (sti->sti_unbind_mp == NULL) { 2157 dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n")); 2158 /* NOTE: holding so_lock while sleeping */ 2159 sti->sti_unbind_mp = 2160 soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR, cr); 2161 if (sti->sti_unbind_mp == NULL) { 2162 error = EINTR; 2163 goto done; 2164 } 2165 } 2166 2167 /* 2168 * Can't have done a listen before connecting. 2169 */ 2170 if (so->so_state & SS_ACCEPTCONN) { 2171 error = EOPNOTSUPP; 2172 goto done; 2173 } 2174 2175 /* 2176 * Must be bound with the transport 2177 */ 2178 if (!(so->so_state & SS_ISBOUND)) { 2179 if ((so->so_family == AF_INET || so->so_family == AF_INET6) && 2180 /*CONSTCOND*/ 2181 so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) { 2182 /* 2183 * Optimization for AF_INET{,6} transports 2184 * that can handle a T_CONN_REQ without being bound. 2185 */ 2186 so_automatic_bind(so); 2187 } else { 2188 error = sotpi_bind(so, NULL, 0, 2189 _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr); 2190 if (error) 2191 goto done; 2192 } 2193 ASSERT(so->so_state & SS_ISBOUND); 2194 flags |= _SOCONNECT_DID_BIND; 2195 } 2196 2197 /* 2198 * Handle a connect to a name parameter of type AF_UNSPEC like a 2199 * connect to a null address. This is the portable method to 2200 * unconnect a socket. 2201 */ 2202 if ((namelen >= sizeof (sa_family_t)) && 2203 (name->sa_family == AF_UNSPEC)) { 2204 name = NULL; 2205 namelen = 0; 2206 } 2207 2208 /* 2209 * Check that we are not already connected. 2210 * A connection-oriented socket cannot be reconnected. 2211 * A connected connection-less socket can be 2212 * - connected to a different address by a subsequent connect 2213 * - "unconnected" by a connect to the NULL address 2214 */ 2215 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) { 2216 ASSERT(!(flags & _SOCONNECT_DID_BIND)); 2217 if (so->so_mode & SM_CONNREQUIRED) { 2218 /* Connection-oriented socket */ 2219 error = so->so_state & SS_ISCONNECTED ? 2220 EISCONN : EALREADY; 2221 goto done; 2222 } 2223 /* Connection-less socket */ 2224 if (name == NULL) { 2225 /* 2226 * Remove the connected state and clear SO_DGRAM_ERRIND 2227 * since it was set when the socket was connected. 2228 * If this is UDP also send down a T_DISCON_REQ. 2229 */ 2230 int val; 2231 2232 if ((so->so_family == AF_INET || 2233 so->so_family == AF_INET6) && 2234 (so->so_type == SOCK_DGRAM || 2235 so->so_type == SOCK_RAW) && 2236 /*CONSTCOND*/ 2237 !soconnect_tpi_udp) { 2238 /* XXX What about implicitly unbinding here? */ 2239 error = sodisconnect(so, -1, 2240 _SODISCONNECT_LOCK_HELD); 2241 } else { 2242 so->so_state &= 2243 ~(SS_ISCONNECTED | SS_ISCONNECTING); 2244 sti->sti_faddr_valid = 0; 2245 sti->sti_faddr_len = 0; 2246 } 2247 2248 /* Remove SOLOCKED since setsockopt will grab it */ 2249 so_unlock_single(so, SOLOCKED); 2250 mutex_exit(&so->so_lock); 2251 2252 val = 0; 2253 (void) sotpi_setsockopt(so, SOL_SOCKET, 2254 SO_DGRAM_ERRIND, &val, (t_uscalar_t)sizeof (val), 2255 cr); 2256 2257 mutex_enter(&so->so_lock); 2258 so_lock_single(so); /* Set SOLOCKED */ 2259 goto done; 2260 } 2261 } 2262 ASSERT(so->so_state & SS_ISBOUND); 2263 2264 if (name == NULL || namelen == 0) { 2265 error = EINVAL; 2266 goto done; 2267 } 2268 /* 2269 * Mark the socket if sti_faddr_sa represents the transport level 2270 * address. 2271 */ 2272 if (flags & _SOCONNECT_NOXLATE) { 2273 struct sockaddr_ux *soaddr_ux; 2274 2275 ASSERT(so->so_family == AF_UNIX); 2276 if (namelen != sizeof (struct sockaddr_ux)) { 2277 error = EINVAL; 2278 goto done; 2279 } 2280 soaddr_ux = (struct sockaddr_ux *)name; 2281 name = (struct sockaddr *)&soaddr_ux->sou_addr; 2282 namelen = sizeof (soaddr_ux->sou_addr); 2283 sti->sti_faddr_noxlate = 1; 2284 } 2285 2286 /* 2287 * Length and family checks. 2288 */ 2289 error = so_addr_verify(so, name, namelen); 2290 if (error) 2291 goto bad; 2292 2293 /* 2294 * Save foreign address. Needed for AF_UNIX as well as 2295 * transport providers that do not support TI_GETPEERNAME. 2296 * Also used for cached foreign address for TCP and UDP. 2297 */ 2298 if (namelen > (t_uscalar_t)sti->sti_faddr_maxlen) { 2299 error = EINVAL; 2300 goto done; 2301 } 2302 sti->sti_faddr_len = (socklen_t)namelen; 2303 ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen); 2304 bcopy(name, sti->sti_faddr_sa, namelen); 2305 sti->sti_faddr_valid = 1; 2306 2307 if (so->so_family == AF_UNIX) { 2308 if (sti->sti_faddr_noxlate) { 2309 /* 2310 * sti_faddr is a transport-level address, so 2311 * don't pass it as an option. Do save it in 2312 * sti_ux_faddr, used for connected DG send. 2313 */ 2314 src = NULL; 2315 srclen = 0; 2316 addr = sti->sti_faddr_sa; 2317 addrlen = (t_uscalar_t)sti->sti_faddr_len; 2318 bcopy(addr, &sti->sti_ux_faddr, 2319 sizeof (sti->sti_ux_faddr)); 2320 } else { 2321 /* 2322 * Pass the sockaddr_un source address as an option 2323 * and translate the remote address. 2324 * Holding so_lock thus sti_laddr_sa can not change. 2325 */ 2326 src = sti->sti_laddr_sa; 2327 srclen = (t_uscalar_t)sti->sti_laddr_len; 2328 dprintso(so, 1, 2329 ("sotpi_connect UNIX: srclen %d, src %p\n", 2330 srclen, src)); 2331 /* 2332 * Translate the destination address into our 2333 * internal form, and save it in sti_ux_faddr. 2334 * After this call, addr==&sti->sti_ux_taddr, 2335 * and we copy that to sti->sti_ux_faddr so 2336 * we save the connected peer address. 2337 */ 2338 error = so_ux_addr_xlate(so, 2339 sti->sti_faddr_sa, (socklen_t)sti->sti_faddr_len, 2340 (flags & _SOCONNECT_XPG4_2), 2341 &addr, &addrlen); 2342 if (error) 2343 goto bad; 2344 bcopy(&sti->sti_ux_taddr, &sti->sti_ux_faddr, 2345 sizeof (sti->sti_ux_faddr)); 2346 } 2347 } else { 2348 addr = sti->sti_faddr_sa; 2349 addrlen = (t_uscalar_t)sti->sti_faddr_len; 2350 src = NULL; 2351 srclen = 0; 2352 } 2353 /* 2354 * When connecting a datagram socket we issue the SO_DGRAM_ERRIND 2355 * option which asks the transport provider to send T_UDERR_IND 2356 * messages. These T_UDERR_IND messages are used to return connected 2357 * style errors (e.g. ECONNRESET) for connected datagram sockets. 2358 * 2359 * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets) 2360 * we send down a T_CONN_REQ. This is needed to let the 2361 * transport assign a local address that is consistent with 2362 * the remote address. Applications depend on a getsockname() 2363 * after a connect() to retrieve the "source" IP address for 2364 * the connected socket. Invalidate the cached local address 2365 * to force getsockname() to enquire of the transport. 2366 */ 2367 if (!(so->so_mode & SM_CONNREQUIRED)) { 2368 /* 2369 * Datagram socket. 2370 */ 2371 int32_t val; 2372 2373 so_unlock_single(so, SOLOCKED); 2374 mutex_exit(&so->so_lock); 2375 2376 val = 1; 2377 (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND, 2378 &val, (t_uscalar_t)sizeof (val), cr); 2379 2380 mutex_enter(&so->so_lock); 2381 so_lock_single(so); /* Set SOLOCKED */ 2382 if ((so->so_family != AF_INET && so->so_family != AF_INET6) || 2383 (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) || 2384 soconnect_tpi_udp) { 2385 soisconnected(so); 2386 goto done; 2387 } 2388 /* 2389 * Send down T_CONN_REQ etc. 2390 * Clear fflag to avoid returning EWOULDBLOCK. 2391 */ 2392 fflag = 0; 2393 ASSERT(so->so_family != AF_UNIX); 2394 sti->sti_laddr_valid = 0; 2395 } else if (sti->sti_laddr_len != 0) { 2396 /* 2397 * If the local address or port was "any" then it may be 2398 * changed by the transport as a result of the 2399 * connect. Invalidate the cached version if we have one. 2400 */ 2401 switch (so->so_family) { 2402 case AF_INET: 2403 ASSERT(sti->sti_laddr_len == (socklen_t)sizeof (sin_t)); 2404 if (((sin_t *)sti->sti_laddr_sa)->sin_addr.s_addr == 2405 INADDR_ANY || 2406 ((sin_t *)sti->sti_laddr_sa)->sin_port == 0) 2407 sti->sti_laddr_valid = 0; 2408 break; 2409 2410 case AF_INET6: 2411 ASSERT(sti->sti_laddr_len == 2412 (socklen_t)sizeof (sin6_t)); 2413 if (IN6_IS_ADDR_UNSPECIFIED( 2414 &((sin6_t *)sti->sti_laddr_sa) ->sin6_addr) || 2415 IN6_IS_ADDR_V4MAPPED_ANY( 2416 &((sin6_t *)sti->sti_laddr_sa)->sin6_addr) || 2417 ((sin6_t *)sti->sti_laddr_sa)->sin6_port == 0) 2418 sti->sti_laddr_valid = 0; 2419 break; 2420 2421 default: 2422 break; 2423 } 2424 } 2425 2426 /* 2427 * Check for failure of an earlier call 2428 */ 2429 if (so->so_error != 0) 2430 goto so_bad; 2431 2432 /* 2433 * Send down T_CONN_REQ. Message was allocated above. 2434 */ 2435 conn_req.PRIM_type = T_CONN_REQ; 2436 conn_req.DEST_length = addrlen; 2437 conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req); 2438 if (srclen == 0) { 2439 conn_req.OPT_length = 0; 2440 conn_req.OPT_offset = 0; 2441 soappendmsg(mp, &conn_req, sizeof (conn_req)); 2442 soappendmsg(mp, addr, addrlen); 2443 } else { 2444 /* 2445 * There is a AF_UNIX sockaddr_un to include as a source 2446 * address option. 2447 */ 2448 struct T_opthdr toh; 2449 2450 toh.level = SOL_SOCKET; 2451 toh.name = SO_SRCADDR; 2452 toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 2453 toh.status = 0; 2454 conn_req.OPT_length = 2455 (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen)); 2456 conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) + 2457 _TPI_ALIGN_TOPT(addrlen)); 2458 2459 soappendmsg(mp, &conn_req, sizeof (conn_req)); 2460 soappendmsg(mp, addr, addrlen); 2461 mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 2462 soappendmsg(mp, &toh, sizeof (toh)); 2463 soappendmsg(mp, src, srclen); 2464 mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 2465 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 2466 } 2467 /* 2468 * Set SS_ISCONNECTING before sending down the T_CONN_REQ 2469 * in order to have the right state when the T_CONN_CON shows up. 2470 */ 2471 soisconnecting(so); 2472 mutex_exit(&so->so_lock); 2473 2474 if (AU_AUDITING()) 2475 audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0); 2476 2477 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 2478 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 2479 mp = NULL; 2480 mutex_enter(&so->so_lock); 2481 if (error != 0) 2482 goto bad; 2483 2484 if ((error = sowaitokack(so, T_CONN_REQ)) != 0) 2485 goto bad; 2486 2487 /* Allow other threads to access the socket */ 2488 so_unlock_single(so, SOLOCKED); 2489 need_unlock = B_FALSE; 2490 2491 /* 2492 * Wait until we get a T_CONN_CON or an error 2493 */ 2494 if ((error = sowaitconnected(so, fflag, 0)) != 0) { 2495 so_lock_single(so); /* Set SOLOCKED */ 2496 need_unlock = B_TRUE; 2497 } 2498 2499 done: 2500 freemsg(mp); 2501 switch (error) { 2502 case EINPROGRESS: 2503 case EALREADY: 2504 case EISCONN: 2505 case EINTR: 2506 /* Non-fatal errors */ 2507 sti->sti_laddr_valid = 0; 2508 /* FALLTHRU */ 2509 case 0: 2510 break; 2511 default: 2512 ASSERT(need_unlock); 2513 /* 2514 * Fatal errors: clear SS_ISCONNECTING in case it was set, 2515 * and invalidate local-address cache 2516 */ 2517 so->so_state &= ~SS_ISCONNECTING; 2518 sti->sti_laddr_valid = 0; 2519 /* A discon_ind might have already unbound us */ 2520 if ((flags & _SOCONNECT_DID_BIND) && 2521 (so->so_state & SS_ISBOUND)) { 2522 int err; 2523 2524 err = sotpi_unbind(so, 0); 2525 /* LINTED - statement has no conseq */ 2526 if (err) { 2527 eprintsoline(so, err); 2528 } 2529 } 2530 break; 2531 } 2532 if (need_unlock) 2533 so_unlock_single(so, SOLOCKED); 2534 mutex_exit(&so->so_lock); 2535 return (error); 2536 2537 so_bad: error = sogeterr(so, B_TRUE); 2538 bad: eprintsoline(so, error); 2539 goto done; 2540 } 2541 2542 /* ARGSUSED */ 2543 int 2544 sotpi_shutdown(struct sonode *so, int how, struct cred *cr) 2545 { 2546 struct T_ordrel_req ordrel_req; 2547 mblk_t *mp; 2548 uint_t old_state, state_change; 2549 int error = 0; 2550 sotpi_info_t *sti = SOTOTPI(so); 2551 2552 dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n", 2553 (void *)so, how, pr_state(so->so_state, so->so_mode))); 2554 2555 mutex_enter(&so->so_lock); 2556 so_lock_single(so); /* Set SOLOCKED */ 2557 2558 /* 2559 * SunOS 4.X has no check for datagram sockets. 2560 * 5.X checks that it is connected (ENOTCONN) 2561 * X/Open requires that we check the connected state. 2562 */ 2563 if (!(so->so_state & SS_ISCONNECTED)) { 2564 if (!xnet_skip_checks) { 2565 error = ENOTCONN; 2566 if (xnet_check_print) { 2567 printf("sockfs: X/Open shutdown check " 2568 "caused ENOTCONN\n"); 2569 } 2570 } 2571 goto done; 2572 } 2573 /* 2574 * Record the current state and then perform any state changes. 2575 * Then use the difference between the old and new states to 2576 * determine which messages need to be sent. 2577 * This prevents e.g. duplicate T_ORDREL_REQ when there are 2578 * duplicate calls to shutdown(). 2579 */ 2580 old_state = so->so_state; 2581 2582 switch (how) { 2583 case 0: 2584 socantrcvmore(so); 2585 break; 2586 case 1: 2587 socantsendmore(so); 2588 break; 2589 case 2: 2590 socantsendmore(so); 2591 socantrcvmore(so); 2592 break; 2593 default: 2594 error = EINVAL; 2595 goto done; 2596 } 2597 2598 /* 2599 * Assumes that the SS_CANT* flags are never cleared in the above code. 2600 */ 2601 state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) - 2602 (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)); 2603 ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0); 2604 2605 switch (state_change) { 2606 case 0: 2607 dprintso(so, 1, 2608 ("sotpi_shutdown: nothing to send in state 0x%x\n", 2609 so->so_state)); 2610 goto done; 2611 2612 case SS_CANTRCVMORE: 2613 mutex_exit(&so->so_lock); 2614 strseteof(SOTOV(so), 1); 2615 /* 2616 * strseteof takes care of read side wakeups, 2617 * pollwakeups, and signals. 2618 */ 2619 /* 2620 * Get the read lock before flushing data to avoid problems 2621 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg. 2622 */ 2623 mutex_enter(&so->so_lock); 2624 (void) so_lock_read(so, 0); /* Set SOREADLOCKED */ 2625 mutex_exit(&so->so_lock); 2626 2627 /* Flush read side queue */ 2628 strflushrq(SOTOV(so), FLUSHALL); 2629 2630 mutex_enter(&so->so_lock); 2631 so_unlock_read(so); /* Clear SOREADLOCKED */ 2632 break; 2633 2634 case SS_CANTSENDMORE: 2635 mutex_exit(&so->so_lock); 2636 strsetwerror(SOTOV(so), 0, 0, sogetwrerr); 2637 mutex_enter(&so->so_lock); 2638 break; 2639 2640 case SS_CANTSENDMORE|SS_CANTRCVMORE: 2641 mutex_exit(&so->so_lock); 2642 strsetwerror(SOTOV(so), 0, 0, sogetwrerr); 2643 strseteof(SOTOV(so), 1); 2644 /* 2645 * strseteof takes care of read side wakeups, 2646 * pollwakeups, and signals. 2647 */ 2648 /* 2649 * Get the read lock before flushing data to avoid problems 2650 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg. 2651 */ 2652 mutex_enter(&so->so_lock); 2653 (void) so_lock_read(so, 0); /* Set SOREADLOCKED */ 2654 mutex_exit(&so->so_lock); 2655 2656 /* Flush read side queue */ 2657 strflushrq(SOTOV(so), FLUSHALL); 2658 2659 mutex_enter(&so->so_lock); 2660 so_unlock_read(so); /* Clear SOREADLOCKED */ 2661 break; 2662 } 2663 2664 ASSERT(MUTEX_HELD(&so->so_lock)); 2665 2666 /* 2667 * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them 2668 * was set due to this call and the new state has both of them set: 2669 * Send the AF_UNIX close indication 2670 * For T_COTS send a discon_ind 2671 * 2672 * If cantsend was set due to this call: 2673 * For T_COTSORD send an ordrel_ind 2674 * 2675 * Note that for T_CLTS there is no message sent here. 2676 */ 2677 if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) == 2678 (SS_CANTRCVMORE|SS_CANTSENDMORE)) { 2679 /* 2680 * For SunOS 4.X compatibility we tell the other end 2681 * that we are unable to receive at this point. 2682 */ 2683 if (so->so_family == AF_UNIX && sti->sti_serv_type != T_CLTS) 2684 so_unix_close(so); 2685 2686 if (sti->sti_serv_type == T_COTS) 2687 error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD); 2688 } 2689 if ((state_change & SS_CANTSENDMORE) && 2690 (sti->sti_serv_type == T_COTS_ORD)) { 2691 /* Send an orderly release */ 2692 ordrel_req.PRIM_type = T_ORDREL_REQ; 2693 2694 mutex_exit(&so->so_lock); 2695 mp = soallocproto1(&ordrel_req, sizeof (ordrel_req), 2696 0, _ALLOC_SLEEP, cr); 2697 /* 2698 * Send down the T_ORDREL_REQ even if there is flow control. 2699 * This prevents shutdown from blocking. 2700 * Note that there is no T_OK_ACK for ordrel_req. 2701 */ 2702 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 2703 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 2704 mutex_enter(&so->so_lock); 2705 if (error) { 2706 eprintsoline(so, error); 2707 goto done; 2708 } 2709 } 2710 2711 done: 2712 so_unlock_single(so, SOLOCKED); 2713 mutex_exit(&so->so_lock); 2714 return (error); 2715 } 2716 2717 /* 2718 * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send 2719 * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer 2720 * that we have closed. 2721 * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length 2722 * T_UNITDATA_REQ containing the same option. 2723 * 2724 * For SOCK_DGRAM half-connections (somebody connected to this end 2725 * but this end is not connect) we don't know where to send any 2726 * SO_UNIX_CLOSE. 2727 * 2728 * We have to ignore stream head errors just in case there has been 2729 * a shutdown(output). 2730 * Ignore any flow control to try to get the message more quickly to the peer. 2731 * While locally ignoring flow control solves the problem when there 2732 * is only the loopback transport on the stream it would not provide 2733 * the correct AF_UNIX socket semantics when one or more modules have 2734 * been pushed. 2735 */ 2736 void 2737 so_unix_close(struct sonode *so) 2738 { 2739 struct T_opthdr toh; 2740 mblk_t *mp; 2741 sotpi_info_t *sti = SOTOTPI(so); 2742 2743 ASSERT(MUTEX_HELD(&so->so_lock)); 2744 2745 ASSERT(so->so_family == AF_UNIX); 2746 2747 if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) != 2748 (SS_ISCONNECTED|SS_ISBOUND)) 2749 return; 2750 2751 dprintso(so, 1, ("so_unix_close(%p) %s\n", 2752 (void *)so, pr_state(so->so_state, so->so_mode))); 2753 2754 toh.level = SOL_SOCKET; 2755 toh.name = SO_UNIX_CLOSE; 2756 2757 /* zero length + header */ 2758 toh.len = (t_uscalar_t)sizeof (struct T_opthdr); 2759 toh.status = 0; 2760 2761 if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) { 2762 struct T_optdata_req tdr; 2763 2764 tdr.PRIM_type = T_OPTDATA_REQ; 2765 tdr.DATA_flag = 0; 2766 2767 tdr.OPT_length = (t_scalar_t)sizeof (toh); 2768 tdr.OPT_offset = (t_scalar_t)sizeof (tdr); 2769 2770 /* NOTE: holding so_lock while sleeping */ 2771 mp = soallocproto2(&tdr, sizeof (tdr), 2772 &toh, sizeof (toh), 0, _ALLOC_SLEEP, CRED()); 2773 } else { 2774 struct T_unitdata_req tudr; 2775 void *addr; 2776 socklen_t addrlen; 2777 void *src; 2778 socklen_t srclen; 2779 struct T_opthdr toh2; 2780 t_scalar_t size; 2781 2782 /* 2783 * We know this is an AF_UNIX connected DGRAM socket. 2784 * We therefore already have the destination address 2785 * in the internal form needed for this send. This is 2786 * similar to the sosend_dgram call later in this file 2787 * when there's no user-specified destination address. 2788 */ 2789 if (sti->sti_faddr_noxlate) { 2790 /* 2791 * Already have a transport internal address. Do not 2792 * pass any (transport internal) source address. 2793 */ 2794 addr = sti->sti_faddr_sa; 2795 addrlen = (t_uscalar_t)sti->sti_faddr_len; 2796 src = NULL; 2797 srclen = 0; 2798 } else { 2799 /* 2800 * Pass the sockaddr_un source address as an option 2801 * and translate the remote address. 2802 * Holding so_lock thus sti_laddr_sa can not change. 2803 */ 2804 src = sti->sti_laddr_sa; 2805 srclen = (socklen_t)sti->sti_laddr_len; 2806 dprintso(so, 1, 2807 ("so_ux_close: srclen %d, src %p\n", 2808 srclen, src)); 2809 /* 2810 * Use the destination address saved in connect. 2811 */ 2812 addr = &sti->sti_ux_faddr; 2813 addrlen = sizeof (sti->sti_ux_faddr); 2814 } 2815 tudr.PRIM_type = T_UNITDATA_REQ; 2816 tudr.DEST_length = addrlen; 2817 tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 2818 if (srclen == 0) { 2819 tudr.OPT_length = (t_scalar_t)sizeof (toh); 2820 tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 2821 _TPI_ALIGN_TOPT(addrlen)); 2822 2823 size = tudr.OPT_offset + tudr.OPT_length; 2824 /* NOTE: holding so_lock while sleeping */ 2825 mp = soallocproto2(&tudr, sizeof (tudr), 2826 addr, addrlen, size, _ALLOC_SLEEP, CRED()); 2827 mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen); 2828 soappendmsg(mp, &toh, sizeof (toh)); 2829 } else { 2830 /* 2831 * There is a AF_UNIX sockaddr_un to include as a 2832 * source address option. 2833 */ 2834 tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) + 2835 _TPI_ALIGN_TOPT(srclen)); 2836 tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 2837 _TPI_ALIGN_TOPT(addrlen)); 2838 2839 toh2.level = SOL_SOCKET; 2840 toh2.name = SO_SRCADDR; 2841 toh2.len = (t_uscalar_t)(srclen + 2842 sizeof (struct T_opthdr)); 2843 toh2.status = 0; 2844 2845 size = tudr.OPT_offset + tudr.OPT_length; 2846 2847 /* NOTE: holding so_lock while sleeping */ 2848 mp = soallocproto2(&tudr, sizeof (tudr), 2849 addr, addrlen, size, _ALLOC_SLEEP, CRED()); 2850 mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 2851 soappendmsg(mp, &toh, sizeof (toh)); 2852 soappendmsg(mp, &toh2, sizeof (toh2)); 2853 soappendmsg(mp, src, srclen); 2854 mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 2855 } 2856 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 2857 } 2858 mutex_exit(&so->so_lock); 2859 (void) kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 2860 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 2861 mutex_enter(&so->so_lock); 2862 } 2863 2864 /* 2865 * Called by sotpi_recvmsg when reading a non-zero amount of data. 2866 * In addition, the caller typically verifies that there is some 2867 * potential state to clear by checking 2868 * if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) 2869 * before calling this routine. 2870 * Note that such a check can be made without holding so_lock since 2871 * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg 2872 * decrements sti_oobsigcnt. 2873 * 2874 * When data is read *after* the point that all pending 2875 * oob data has been consumed the oob indication is cleared. 2876 * 2877 * This logic keeps select/poll returning POLLRDBAND and 2878 * SIOCATMARK returning true until we have read past 2879 * the mark. 2880 */ 2881 static void 2882 sorecv_update_oobstate(struct sonode *so) 2883 { 2884 sotpi_info_t *sti = SOTOTPI(so); 2885 2886 mutex_enter(&so->so_lock); 2887 ASSERT(so_verify_oobstate(so)); 2888 dprintso(so, 1, 2889 ("sorecv_update_oobstate: counts %d/%d state %s\n", 2890 sti->sti_oobsigcnt, 2891 sti->sti_oobcnt, pr_state(so->so_state, so->so_mode))); 2892 if (sti->sti_oobsigcnt == 0) { 2893 /* No more pending oob indications */ 2894 so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK); 2895 freemsg(so->so_oobmsg); 2896 so->so_oobmsg = NULL; 2897 } 2898 ASSERT(so_verify_oobstate(so)); 2899 mutex_exit(&so->so_lock); 2900 } 2901 2902 /* 2903 * Receive the next message on the queue. 2904 * If msg_controllen is non-zero when called the caller is interested in 2905 * any received control info (options). 2906 * If msg_namelen is non-zero when called the caller is interested in 2907 * any received source address. 2908 * The routine returns with msg_control and msg_name pointing to 2909 * kmem_alloc'ed memory which the caller has to free. 2910 */ 2911 /* ARGSUSED */ 2912 int 2913 sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, 2914 struct cred *cr) 2915 { 2916 union T_primitives *tpr; 2917 mblk_t *mp; 2918 uchar_t pri; 2919 int pflag, opflag; 2920 void *control; 2921 t_uscalar_t controllen; 2922 t_uscalar_t namelen; 2923 int so_state = so->so_state; /* Snapshot */ 2924 ssize_t saved_resid; 2925 rval_t rval; 2926 int flags; 2927 clock_t timout; 2928 int error = 0; 2929 sotpi_info_t *sti = SOTOTPI(so); 2930 2931 flags = msg->msg_flags; 2932 msg->msg_flags = 0; 2933 2934 dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n", 2935 (void *)so, (void *)msg, flags, 2936 pr_state(so->so_state, so->so_mode), so->so_error)); 2937 2938 if (so->so_version == SOV_STREAM) { 2939 so_update_attrs(so, SOACC); 2940 /* The imaginary "sockmod" has been popped - act as a stream */ 2941 return (strread(SOTOV(so), uiop, cr)); 2942 } 2943 2944 /* 2945 * If we are not connected because we have never been connected 2946 * we return ENOTCONN. If we have been connected (but are no longer 2947 * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return 2948 * the EOF. 2949 * 2950 * An alternative would be to post an ENOTCONN error in stream head 2951 * (read+write) and clear it when we're connected. However, that error 2952 * would cause incorrect poll/select behavior! 2953 */ 2954 if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 && 2955 (so->so_mode & SM_CONNREQUIRED)) { 2956 return (ENOTCONN); 2957 } 2958 2959 /* 2960 * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but 2961 * after checking that the read queue is empty) and returns zero. 2962 * This implementation will sleep (in kstrgetmsg) even if uio_resid 2963 * is zero. 2964 */ 2965 2966 if (flags & MSG_OOB) { 2967 /* Check that the transport supports OOB */ 2968 if (!(so->so_mode & SM_EXDATA)) 2969 return (EOPNOTSUPP); 2970 so_update_attrs(so, SOACC); 2971 return (sorecvoob(so, msg, uiop, flags, 2972 (so->so_options & SO_OOBINLINE))); 2973 } 2974 2975 so_update_attrs(so, SOACC); 2976 2977 /* 2978 * Set msg_controllen and msg_namelen to zero here to make it 2979 * simpler in the cases that no control or name is returned. 2980 */ 2981 controllen = msg->msg_controllen; 2982 namelen = msg->msg_namelen; 2983 msg->msg_controllen = 0; 2984 msg->msg_namelen = 0; 2985 2986 dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n", 2987 namelen, controllen)); 2988 2989 mutex_enter(&so->so_lock); 2990 /* 2991 * Only one reader is allowed at any given time. This is needed 2992 * for T_EXDATA handling and, in the future, MSG_WAITALL. 2993 * 2994 * This is slightly different that BSD behavior in that it fails with 2995 * EWOULDBLOCK when using nonblocking io. In BSD the read queue access 2996 * is single-threaded using sblock(), which is dropped while waiting 2997 * for data to appear. The difference shows up e.g. if one 2998 * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor 2999 * does use nonblocking io and different threads are reading each 3000 * file descriptor. In BSD there would never be an EWOULDBLOCK error 3001 * in this case as long as the read queue doesn't get empty. 3002 * In this implementation the thread using nonblocking io can 3003 * get an EWOULDBLOCK error due to the blocking thread executing 3004 * e.g. in the uiomove in kstrgetmsg. 3005 * This difference is not believed to be significant. 3006 */ 3007 /* Set SOREADLOCKED */ 3008 error = so_lock_read_intr(so, 3009 uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0)); 3010 mutex_exit(&so->so_lock); 3011 if (error) 3012 return (error); 3013 3014 /* 3015 * Tell kstrgetmsg to not inspect the stream head errors until all 3016 * queued data has been consumed. 3017 * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set. 3018 * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block. 3019 * 3020 * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and 3021 * to T_OPTDATA_IND that do not contain any user-visible control msg. 3022 * Note that MSG_WAITALL set with MSG_PEEK is a noop. 3023 */ 3024 pflag = MSG_ANY | MSG_DELAYERROR; 3025 if (flags & MSG_PEEK) { 3026 pflag |= MSG_IPEEK; 3027 flags &= ~MSG_WAITALL; 3028 } 3029 if (so->so_mode & SM_ATOMIC) 3030 pflag |= MSG_DISCARDTAIL; 3031 3032 if (flags & MSG_DONTWAIT) 3033 timout = 0; 3034 else if (so->so_rcvtimeo != 0) 3035 timout = TICK_TO_MSEC(so->so_rcvtimeo); 3036 else 3037 timout = -1; 3038 opflag = pflag; 3039 retry: 3040 saved_resid = uiop->uio_resid; 3041 pri = 0; 3042 mp = NULL; 3043 error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag, 3044 timout, &rval); 3045 if (error != 0) { 3046 /* kstrgetmsg returns ETIME when timeout expires */ 3047 if (error == ETIME) 3048 error = EWOULDBLOCK; 3049 goto out; 3050 } 3051 /* 3052 * For datagrams the MOREDATA flag is used to set MSG_TRUNC. 3053 * For non-datagrams MOREDATA is used to set MSG_EOR. 3054 */ 3055 ASSERT(!(rval.r_val1 & MORECTL)); 3056 if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC)) 3057 msg->msg_flags |= MSG_TRUNC; 3058 3059 if (mp == NULL) { 3060 dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n")); 3061 /* 3062 * 4.3BSD and 4.4BSD clears the mark when peeking across it. 3063 * The draft Posix socket spec states that the mark should 3064 * not be cleared when peeking. We follow the latter. 3065 */ 3066 if ((so->so_state & 3067 (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 3068 (uiop->uio_resid != saved_resid) && 3069 !(flags & MSG_PEEK)) { 3070 sorecv_update_oobstate(so); 3071 } 3072 3073 mutex_enter(&so->so_lock); 3074 /* Set MSG_EOR based on MOREDATA */ 3075 if (!(rval.r_val1 & MOREDATA)) { 3076 if (so->so_state & SS_SAVEDEOR) { 3077 msg->msg_flags |= MSG_EOR; 3078 so->so_state &= ~SS_SAVEDEOR; 3079 } 3080 } 3081 /* 3082 * If some data was received (i.e. not EOF) and the 3083 * read/recv* has not been satisfied wait for some more. 3084 */ 3085 if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 3086 uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 3087 mutex_exit(&so->so_lock); 3088 pflag = opflag | MSG_NOMARK; 3089 goto retry; 3090 } 3091 goto out_locked; 3092 } 3093 3094 /* strsock_proto has already verified length and alignment */ 3095 tpr = (union T_primitives *)mp->b_rptr; 3096 dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type)); 3097 3098 switch (tpr->type) { 3099 case T_DATA_IND: { 3100 if ((so->so_state & 3101 (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 3102 (uiop->uio_resid != saved_resid) && 3103 !(flags & MSG_PEEK)) { 3104 sorecv_update_oobstate(so); 3105 } 3106 3107 /* 3108 * Set msg_flags to MSG_EOR based on 3109 * MORE_flag and MOREDATA. 3110 */ 3111 mutex_enter(&so->so_lock); 3112 so->so_state &= ~SS_SAVEDEOR; 3113 if (!(tpr->data_ind.MORE_flag & 1)) { 3114 if (!(rval.r_val1 & MOREDATA)) 3115 msg->msg_flags |= MSG_EOR; 3116 else 3117 so->so_state |= SS_SAVEDEOR; 3118 } 3119 freemsg(mp); 3120 /* 3121 * If some data was received (i.e. not EOF) and the 3122 * read/recv* has not been satisfied wait for some more. 3123 */ 3124 if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 3125 uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 3126 mutex_exit(&so->so_lock); 3127 pflag = opflag | MSG_NOMARK; 3128 goto retry; 3129 } 3130 goto out_locked; 3131 } 3132 case T_UNITDATA_IND: { 3133 void *addr; 3134 t_uscalar_t addrlen; 3135 void *abuf; 3136 t_uscalar_t optlen; 3137 void *opt; 3138 3139 if ((so->so_state & 3140 (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 3141 (uiop->uio_resid != saved_resid) && 3142 !(flags & MSG_PEEK)) { 3143 sorecv_update_oobstate(so); 3144 } 3145 3146 if (namelen != 0) { 3147 /* Caller wants source address */ 3148 addrlen = tpr->unitdata_ind.SRC_length; 3149 addr = sogetoff(mp, 3150 tpr->unitdata_ind.SRC_offset, 3151 addrlen, 1); 3152 if (addr == NULL) { 3153 freemsg(mp); 3154 error = EPROTO; 3155 eprintsoline(so, error); 3156 goto out; 3157 } 3158 if (so->so_family == AF_UNIX) { 3159 /* 3160 * Can not use the transport level address. 3161 * If there is a SO_SRCADDR option carrying 3162 * the socket level address it will be 3163 * extracted below. 3164 */ 3165 addr = NULL; 3166 addrlen = 0; 3167 } 3168 } 3169 optlen = tpr->unitdata_ind.OPT_length; 3170 if (optlen != 0) { 3171 t_uscalar_t ncontrollen; 3172 3173 /* 3174 * Extract any source address option. 3175 * Determine how large cmsg buffer is needed. 3176 */ 3177 opt = sogetoff(mp, 3178 tpr->unitdata_ind.OPT_offset, 3179 optlen, __TPI_ALIGN_SIZE); 3180 3181 if (opt == NULL) { 3182 freemsg(mp); 3183 error = EPROTO; 3184 eprintsoline(so, error); 3185 goto out; 3186 } 3187 if (so->so_family == AF_UNIX) 3188 so_getopt_srcaddr(opt, optlen, &addr, &addrlen); 3189 ncontrollen = so_cmsglen(mp, opt, optlen, 3190 !(flags & MSG_XPG4_2)); 3191 if (controllen != 0) 3192 controllen = ncontrollen; 3193 else if (ncontrollen != 0) 3194 msg->msg_flags |= MSG_CTRUNC; 3195 } else { 3196 controllen = 0; 3197 } 3198 3199 if (namelen != 0) { 3200 /* 3201 * Return address to caller. 3202 * Caller handles truncation if length 3203 * exceeds msg_namelen. 3204 * NOTE: AF_UNIX NUL termination is ensured by 3205 * the sender's copyin_name(). 3206 */ 3207 abuf = kmem_alloc(addrlen, KM_SLEEP); 3208 3209 bcopy(addr, abuf, addrlen); 3210 msg->msg_name = abuf; 3211 msg->msg_namelen = addrlen; 3212 } 3213 3214 if (controllen != 0) { 3215 /* 3216 * Return control msg to caller. 3217 * Caller handles truncation if length 3218 * exceeds msg_controllen. 3219 */ 3220 control = kmem_zalloc(controllen, KM_SLEEP); 3221 3222 error = so_opt2cmsg(mp, opt, optlen, 3223 !(flags & MSG_XPG4_2), 3224 control, controllen); 3225 if (error) { 3226 freemsg(mp); 3227 if (msg->msg_namelen != 0) 3228 kmem_free(msg->msg_name, 3229 msg->msg_namelen); 3230 kmem_free(control, controllen); 3231 eprintsoline(so, error); 3232 goto out; 3233 } 3234 msg->msg_control = control; 3235 msg->msg_controllen = controllen; 3236 } 3237 3238 freemsg(mp); 3239 goto out; 3240 } 3241 case T_OPTDATA_IND: { 3242 struct T_optdata_req *tdr; 3243 void *opt; 3244 t_uscalar_t optlen; 3245 3246 if ((so->so_state & 3247 (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 3248 (uiop->uio_resid != saved_resid) && 3249 !(flags & MSG_PEEK)) { 3250 sorecv_update_oobstate(so); 3251 } 3252 3253 tdr = (struct T_optdata_req *)mp->b_rptr; 3254 optlen = tdr->OPT_length; 3255 if (optlen != 0) { 3256 t_uscalar_t ncontrollen; 3257 /* 3258 * Determine how large cmsg buffer is needed. 3259 */ 3260 opt = sogetoff(mp, 3261 tpr->optdata_ind.OPT_offset, 3262 optlen, __TPI_ALIGN_SIZE); 3263 3264 if (opt == NULL) { 3265 freemsg(mp); 3266 error = EPROTO; 3267 eprintsoline(so, error); 3268 goto out; 3269 } 3270 3271 ncontrollen = so_cmsglen(mp, opt, optlen, 3272 !(flags & MSG_XPG4_2)); 3273 if (controllen != 0) 3274 controllen = ncontrollen; 3275 else if (ncontrollen != 0) 3276 msg->msg_flags |= MSG_CTRUNC; 3277 } else { 3278 controllen = 0; 3279 } 3280 3281 if (controllen != 0) { 3282 /* 3283 * Return control msg to caller. 3284 * Caller handles truncation if length 3285 * exceeds msg_controllen. 3286 */ 3287 control = kmem_zalloc(controllen, KM_SLEEP); 3288 3289 error = so_opt2cmsg(mp, opt, optlen, 3290 !(flags & MSG_XPG4_2), 3291 control, controllen); 3292 if (error) { 3293 freemsg(mp); 3294 kmem_free(control, controllen); 3295 eprintsoline(so, error); 3296 goto out; 3297 } 3298 msg->msg_control = control; 3299 msg->msg_controllen = controllen; 3300 } 3301 3302 /* 3303 * Set msg_flags to MSG_EOR based on 3304 * DATA_flag and MOREDATA. 3305 */ 3306 mutex_enter(&so->so_lock); 3307 so->so_state &= ~SS_SAVEDEOR; 3308 if (!(tpr->data_ind.MORE_flag & 1)) { 3309 if (!(rval.r_val1 & MOREDATA)) 3310 msg->msg_flags |= MSG_EOR; 3311 else 3312 so->so_state |= SS_SAVEDEOR; 3313 } 3314 freemsg(mp); 3315 /* 3316 * If some data was received (i.e. not EOF) and the 3317 * read/recv* has not been satisfied wait for some more. 3318 * Not possible to wait if control info was received. 3319 */ 3320 if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 3321 controllen == 0 && 3322 uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 3323 mutex_exit(&so->so_lock); 3324 pflag = opflag | MSG_NOMARK; 3325 goto retry; 3326 } 3327 goto out_locked; 3328 } 3329 case T_EXDATA_IND: { 3330 dprintso(so, 1, 3331 ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld " 3332 "state %s\n", 3333 sti->sti_oobsigcnt, sti->sti_oobcnt, 3334 saved_resid - uiop->uio_resid, 3335 pr_state(so->so_state, so->so_mode))); 3336 /* 3337 * kstrgetmsg handles MSGMARK so there is nothing to 3338 * inspect in the T_EXDATA_IND. 3339 * strsock_proto makes the stream head queue the T_EXDATA_IND 3340 * as a separate message with no M_DATA component. Furthermore, 3341 * the stream head does not consolidate M_DATA messages onto 3342 * an MSGMARK'ed message ensuring that the T_EXDATA_IND 3343 * remains a message by itself. This is needed since MSGMARK 3344 * marks both the whole message as well as the last byte 3345 * of the message. 3346 */ 3347 freemsg(mp); 3348 ASSERT(uiop->uio_resid == saved_resid); /* No data */ 3349 if (flags & MSG_PEEK) { 3350 /* 3351 * Even though we are peeking we consume the 3352 * T_EXDATA_IND thereby moving the mark information 3353 * to SS_RCVATMARK. Then the oob code below will 3354 * retry the peeking kstrgetmsg. 3355 * Note that the stream head read queue is 3356 * never flushed without holding SOREADLOCKED 3357 * thus the T_EXDATA_IND can not disappear 3358 * underneath us. 3359 */ 3360 dprintso(so, 1, 3361 ("sotpi_recvmsg: consume EXDATA_IND " 3362 "counts %d/%d state %s\n", 3363 sti->sti_oobsigcnt, 3364 sti->sti_oobcnt, 3365 pr_state(so->so_state, so->so_mode))); 3366 3367 pflag = MSG_ANY | MSG_DELAYERROR; 3368 if (so->so_mode & SM_ATOMIC) 3369 pflag |= MSG_DISCARDTAIL; 3370 3371 pri = 0; 3372 mp = NULL; 3373 3374 error = kstrgetmsg(SOTOV(so), &mp, uiop, 3375 &pri, &pflag, (clock_t)-1, &rval); 3376 ASSERT(uiop->uio_resid == saved_resid); 3377 3378 if (error) { 3379 #ifdef SOCK_DEBUG 3380 if (error != EWOULDBLOCK && error != EINTR) { 3381 eprintsoline(so, error); 3382 } 3383 #endif /* SOCK_DEBUG */ 3384 goto out; 3385 } 3386 ASSERT(mp); 3387 tpr = (union T_primitives *)mp->b_rptr; 3388 ASSERT(tpr->type == T_EXDATA_IND); 3389 freemsg(mp); 3390 } /* end "if (flags & MSG_PEEK)" */ 3391 3392 /* 3393 * Decrement the number of queued and pending oob. 3394 * 3395 * SS_RCVATMARK is cleared when we read past a mark. 3396 * SS_HAVEOOBDATA is cleared when we've read past the 3397 * last mark. 3398 * SS_OOBPEND is cleared if we've read past the last 3399 * mark and no (new) SIGURG has been posted. 3400 */ 3401 mutex_enter(&so->so_lock); 3402 ASSERT(so_verify_oobstate(so)); 3403 ASSERT(sti->sti_oobsigcnt >= sti->sti_oobcnt); 3404 ASSERT(sti->sti_oobsigcnt > 0); 3405 sti->sti_oobsigcnt--; 3406 ASSERT(sti->sti_oobcnt > 0); 3407 sti->sti_oobcnt--; 3408 /* 3409 * Since the T_EXDATA_IND has been removed from the stream 3410 * head, but we have not read data past the mark, 3411 * sockfs needs to track that the socket is still at the mark. 3412 * 3413 * Since no data was received call kstrgetmsg again to wait 3414 * for data. 3415 */ 3416 so->so_state |= SS_RCVATMARK; 3417 mutex_exit(&so->so_lock); 3418 dprintso(so, 1, 3419 ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n", 3420 sti->sti_oobsigcnt, sti->sti_oobcnt, 3421 pr_state(so->so_state, so->so_mode))); 3422 pflag = opflag; 3423 goto retry; 3424 } 3425 default: 3426 cmn_err(CE_CONT, "sotpi_recvmsg: so %p prim %d mp %p\n", 3427 (void *)so, tpr->type, (void *)mp); 3428 ASSERT(0); 3429 freemsg(mp); 3430 error = EPROTO; 3431 eprintsoline(so, error); 3432 goto out; 3433 } 3434 /* NOTREACHED */ 3435 out: 3436 mutex_enter(&so->so_lock); 3437 out_locked: 3438 so_unlock_read(so); /* Clear SOREADLOCKED */ 3439 mutex_exit(&so->so_lock); 3440 return (error); 3441 } 3442 3443 /* 3444 * Sending data with options on a datagram socket. 3445 * Assumes caller has verified that SS_ISBOUND etc. are set. 3446 * 3447 * For AF_UNIX the destination address may be already in 3448 * internal form, as indicated by sti->sti_faddr_noxlate 3449 * or the MSG_SENDTO_NOXLATE flag. Otherwise we need to 3450 * translate the destination address to internal form. 3451 * 3452 * The source address is passed as an option. If passing 3453 * file descriptors, those are passed as file pointers in 3454 * another option. 3455 */ 3456 static int 3457 sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen, 3458 struct uio *uiop, void *control, t_uscalar_t controllen, int flags) 3459 { 3460 struct T_unitdata_req tudr; 3461 mblk_t *mp; 3462 int error; 3463 void *addr; 3464 socklen_t addrlen; 3465 void *src; 3466 socklen_t srclen; 3467 ssize_t len; 3468 int size; 3469 struct T_opthdr toh; 3470 struct fdbuf *fdbuf; 3471 t_uscalar_t optlen; 3472 void *fds; 3473 int fdlen; 3474 sotpi_info_t *sti = SOTOTPI(so); 3475 3476 ASSERT(name && namelen); 3477 ASSERT(control && controllen); 3478 3479 len = uiop->uio_resid; 3480 if (len > (ssize_t)sti->sti_tidu_size) { 3481 return (EMSGSIZE); 3482 } 3483 3484 if (sti->sti_faddr_noxlate == 0 && 3485 (flags & MSG_SENDTO_NOXLATE) == 0) { 3486 /* 3487 * Length and family checks. 3488 * Don't verify internal form. 3489 */ 3490 error = so_addr_verify(so, name, namelen); 3491 if (error) { 3492 eprintsoline(so, error); 3493 return (error); 3494 } 3495 } 3496 3497 if (so->so_family == AF_UNIX) { 3498 if (sti->sti_faddr_noxlate) { 3499 /* 3500 * Already have a transport internal address. Do not 3501 * pass any (transport internal) source address. 3502 */ 3503 addr = name; 3504 addrlen = namelen; 3505 src = NULL; 3506 srclen = 0; 3507 } else if (flags & MSG_SENDTO_NOXLATE) { 3508 /* 3509 * Have an internal form dest. address. 3510 * Pass the source address as usual. 3511 */ 3512 addr = name; 3513 addrlen = namelen; 3514 src = sti->sti_laddr_sa; 3515 srclen = (socklen_t)sti->sti_laddr_len; 3516 } else { 3517 /* 3518 * Pass the sockaddr_un source address as an option 3519 * and translate the remote address. 3520 * 3521 * Note that this code does not prevent sti_laddr_sa 3522 * from changing while it is being used. Thus 3523 * if an unbind+bind occurs concurrently with this 3524 * send the peer might see a partially new and a 3525 * partially old "from" address. 3526 */ 3527 src = sti->sti_laddr_sa; 3528 srclen = (socklen_t)sti->sti_laddr_len; 3529 dprintso(so, 1, 3530 ("sosend_dgramcmsg UNIX: srclen %d, src %p\n", 3531 srclen, src)); 3532 /* 3533 * The sendmsg caller specified a destination 3534 * address, which we must translate into our 3535 * internal form. addr = &sti->sti_ux_taddr 3536 */ 3537 error = so_ux_addr_xlate(so, name, namelen, 3538 (flags & MSG_XPG4_2), 3539 &addr, &addrlen); 3540 if (error) { 3541 eprintsoline(so, error); 3542 return (error); 3543 } 3544 } 3545 } else { 3546 addr = name; 3547 addrlen = namelen; 3548 src = NULL; 3549 srclen = 0; 3550 } 3551 optlen = so_optlen(control, controllen, 3552 !(flags & MSG_XPG4_2)); 3553 tudr.PRIM_type = T_UNITDATA_REQ; 3554 tudr.DEST_length = addrlen; 3555 tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 3556 if (srclen != 0) 3557 tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) + 3558 _TPI_ALIGN_TOPT(srclen)); 3559 else 3560 tudr.OPT_length = optlen; 3561 tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 3562 _TPI_ALIGN_TOPT(addrlen)); 3563 3564 size = tudr.OPT_offset + tudr.OPT_length; 3565 3566 /* 3567 * File descriptors only when SM_FDPASSING set. 3568 */ 3569 error = so_getfdopt(control, controllen, 3570 !(flags & MSG_XPG4_2), &fds, &fdlen); 3571 if (error) 3572 return (error); 3573 if (fdlen != -1) { 3574 if (!(so->so_mode & SM_FDPASSING)) 3575 return (EOPNOTSUPP); 3576 3577 error = fdbuf_create(fds, fdlen, &fdbuf); 3578 if (error) 3579 return (error); 3580 3581 /* 3582 * Pre-allocate enough additional space for lower level modules 3583 * to append an option (e.g. see tl_unitdata). The following 3584 * is enough extra space for the largest option we might append. 3585 */ 3586 size += sizeof (struct T_opthdr) + ucredsize; 3587 mp = fdbuf_allocmsg(size, fdbuf); 3588 } else { 3589 mp = soallocproto(size, _ALLOC_INTR, CRED()); 3590 if (mp == NULL) { 3591 /* 3592 * Caught a signal waiting for memory. 3593 * Let send* return EINTR. 3594 */ 3595 return (EINTR); 3596 } 3597 } 3598 soappendmsg(mp, &tudr, sizeof (tudr)); 3599 soappendmsg(mp, addr, addrlen); 3600 mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 3601 3602 if (fdlen != -1) { 3603 ASSERT(fdbuf != NULL); 3604 toh.level = SOL_SOCKET; 3605 toh.name = SO_FILEP; 3606 toh.len = fdbuf->fd_size + 3607 (t_uscalar_t)sizeof (struct T_opthdr); 3608 toh.status = 0; 3609 soappendmsg(mp, &toh, sizeof (toh)); 3610 soappendmsg(mp, fdbuf, fdbuf->fd_size); 3611 ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 3612 } 3613 if (srclen != 0) { 3614 /* 3615 * There is a AF_UNIX sockaddr_un to include as a source 3616 * address option. 3617 */ 3618 toh.level = SOL_SOCKET; 3619 toh.name = SO_SRCADDR; 3620 toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 3621 toh.status = 0; 3622 soappendmsg(mp, &toh, sizeof (toh)); 3623 soappendmsg(mp, src, srclen); 3624 mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 3625 ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 3626 } 3627 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 3628 so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp); 3629 /* 3630 * Normally at most 3 bytes left in the message, but we might have 3631 * allowed for extra space if we're passing fd's through. 3632 */ 3633 ASSERT(MBLKL(mp) <= (ssize_t)size); 3634 3635 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 3636 if (AU_AUDITING()) 3637 audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 3638 3639 error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 3640 #ifdef SOCK_DEBUG 3641 if (error) { 3642 eprintsoline(so, error); 3643 } 3644 #endif /* SOCK_DEBUG */ 3645 return (error); 3646 } 3647 3648 /* 3649 * Sending data with options on a connected stream socket. 3650 * Assumes caller has verified that SS_ISCONNECTED is set. 3651 */ 3652 static int 3653 sosend_svccmsg(struct sonode *so, struct uio *uiop, int more, void *control, 3654 t_uscalar_t controllen, int flags) 3655 { 3656 struct T_optdata_req tdr; 3657 mblk_t *mp; 3658 int error; 3659 ssize_t iosize; 3660 int size; 3661 struct fdbuf *fdbuf; 3662 t_uscalar_t optlen; 3663 void *fds; 3664 int fdlen; 3665 struct T_opthdr toh; 3666 sotpi_info_t *sti = SOTOTPI(so); 3667 3668 dprintso(so, 1, 3669 ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid)); 3670 3671 /* 3672 * Has to be bound and connected. However, since no locks are 3673 * held the state could have changed after sotpi_sendmsg checked it 3674 * thus it is not possible to ASSERT on the state. 3675 */ 3676 3677 /* Options on connection-oriented only when SM_OPTDATA set. */ 3678 if (!(so->so_mode & SM_OPTDATA)) 3679 return (EOPNOTSUPP); 3680 3681 do { 3682 /* 3683 * Set the MORE flag if uio_resid does not fit in this 3684 * message or if the caller passed in "more". 3685 * Error for transports with zero tidu_size. 3686 */ 3687 tdr.PRIM_type = T_OPTDATA_REQ; 3688 iosize = sti->sti_tidu_size; 3689 if (iosize <= 0) 3690 return (EMSGSIZE); 3691 if (uiop->uio_resid > iosize) { 3692 tdr.DATA_flag = 1; 3693 } else { 3694 if (more) 3695 tdr.DATA_flag = 1; 3696 else 3697 tdr.DATA_flag = 0; 3698 iosize = uiop->uio_resid; 3699 } 3700 dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n", 3701 tdr.DATA_flag, iosize)); 3702 3703 optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2)); 3704 tdr.OPT_length = optlen; 3705 tdr.OPT_offset = (t_scalar_t)sizeof (tdr); 3706 3707 size = (int)sizeof (tdr) + optlen; 3708 /* 3709 * File descriptors only when SM_FDPASSING set. 3710 */ 3711 error = so_getfdopt(control, controllen, 3712 !(flags & MSG_XPG4_2), &fds, &fdlen); 3713 if (error) 3714 return (error); 3715 if (fdlen != -1) { 3716 if (!(so->so_mode & SM_FDPASSING)) 3717 return (EOPNOTSUPP); 3718 3719 error = fdbuf_create(fds, fdlen, &fdbuf); 3720 if (error) 3721 return (error); 3722 3723 /* 3724 * Pre-allocate enough additional space for lower level 3725 * modules to append an option (e.g. see tl_unitdata). 3726 * The following is enough extra space for the largest 3727 * option we might append. 3728 */ 3729 size += sizeof (struct T_opthdr) + ucredsize; 3730 mp = fdbuf_allocmsg(size, fdbuf); 3731 } else { 3732 mp = soallocproto(size, _ALLOC_INTR, CRED()); 3733 if (mp == NULL) { 3734 /* 3735 * Caught a signal waiting for memory. 3736 * Let send* return EINTR. 3737 */ 3738 return (EINTR); 3739 } 3740 } 3741 soappendmsg(mp, &tdr, sizeof (tdr)); 3742 3743 if (fdlen != -1) { 3744 ASSERT(fdbuf != NULL); 3745 toh.level = SOL_SOCKET; 3746 toh.name = SO_FILEP; 3747 toh.len = fdbuf->fd_size + 3748 (t_uscalar_t)sizeof (struct T_opthdr); 3749 toh.status = 0; 3750 soappendmsg(mp, &toh, sizeof (toh)); 3751 soappendmsg(mp, fdbuf, fdbuf->fd_size); 3752 ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 3753 } 3754 so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp); 3755 /* 3756 * Normally at most 3 bytes left in the message, but we might 3757 * have allowed for extra space if we're passing fd's through. 3758 */ 3759 ASSERT(MBLKL(mp) <= (ssize_t)size); 3760 3761 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 3762 3763 error = kstrputmsg(SOTOV(so), mp, uiop, iosize, 3764 0, MSG_BAND, 0); 3765 if (error) { 3766 eprintsoline(so, error); 3767 return (error); 3768 } 3769 control = NULL; 3770 if (uiop->uio_resid > 0) { 3771 /* 3772 * Recheck for fatal errors. Fail write even though 3773 * some data have been written. This is consistent 3774 * with strwrite semantics and BSD sockets semantics. 3775 */ 3776 if (so->so_state & SS_CANTSENDMORE) { 3777 eprintsoline(so, error); 3778 return (EPIPE); 3779 } 3780 if (so->so_error != 0) { 3781 mutex_enter(&so->so_lock); 3782 error = sogeterr(so, B_TRUE); 3783 mutex_exit(&so->so_lock); 3784 if (error != 0) { 3785 eprintsoline(so, error); 3786 return (error); 3787 } 3788 } 3789 } 3790 } while (uiop->uio_resid > 0); 3791 return (0); 3792 } 3793 3794 /* 3795 * Sending data on a datagram socket. 3796 * Assumes caller has verified that SS_ISBOUND etc. are set. 3797 * 3798 * For AF_UNIX the destination address may be already in 3799 * internal form, as indicated by sti->sti_faddr_noxlate 3800 * or the MSG_SENDTO_NOXLATE flag. Otherwise we need to 3801 * translate the destination address to internal form. 3802 * 3803 * The source address is passed as an option. 3804 */ 3805 int 3806 sosend_dgram(struct sonode *so, struct sockaddr *name, socklen_t namelen, 3807 struct uio *uiop, int flags) 3808 { 3809 struct T_unitdata_req tudr; 3810 mblk_t *mp; 3811 int error; 3812 void *addr; 3813 socklen_t addrlen; 3814 void *src; 3815 socklen_t srclen; 3816 ssize_t len; 3817 sotpi_info_t *sti = SOTOTPI(so); 3818 3819 ASSERT(name != NULL && namelen != 0); 3820 3821 len = uiop->uio_resid; 3822 if (len > sti->sti_tidu_size) { 3823 error = EMSGSIZE; 3824 goto done; 3825 } 3826 3827 if (sti->sti_faddr_noxlate == 0 && 3828 (flags & MSG_SENDTO_NOXLATE) == 0) { 3829 /* 3830 * Length and family checks. 3831 * Don't verify internal form. 3832 */ 3833 error = so_addr_verify(so, name, namelen); 3834 if (error != 0) 3835 goto done; 3836 } 3837 3838 if (sti->sti_direct) /* Never on AF_UNIX */ 3839 return (sodgram_direct(so, name, namelen, uiop, flags)); 3840 3841 if (so->so_family == AF_UNIX) { 3842 if (sti->sti_faddr_noxlate) { 3843 /* 3844 * Already have a transport internal address. Do not 3845 * pass any (transport internal) source address. 3846 */ 3847 addr = name; 3848 addrlen = namelen; 3849 src = NULL; 3850 srclen = 0; 3851 } else if (flags & MSG_SENDTO_NOXLATE) { 3852 /* 3853 * Have an internal form dest. address. 3854 * Pass the source address as usual. 3855 */ 3856 addr = name; 3857 addrlen = namelen; 3858 src = sti->sti_laddr_sa; 3859 srclen = (socklen_t)sti->sti_laddr_len; 3860 } else { 3861 /* 3862 * Pass the sockaddr_un source address as an option 3863 * and translate the remote address. 3864 * 3865 * Note that this code does not prevent sti_laddr_sa 3866 * from changing while it is being used. Thus 3867 * if an unbind+bind occurs concurrently with this 3868 * send the peer might see a partially new and a 3869 * partially old "from" address. 3870 */ 3871 src = sti->sti_laddr_sa; 3872 srclen = (socklen_t)sti->sti_laddr_len; 3873 dprintso(so, 1, 3874 ("sosend_dgram UNIX: srclen %d, src %p\n", 3875 srclen, src)); 3876 /* 3877 * The sendmsg caller specified a destination 3878 * address, which we must translate into our 3879 * internal form. addr = &sti->sti_ux_taddr 3880 */ 3881 error = so_ux_addr_xlate(so, name, namelen, 3882 (flags & MSG_XPG4_2), 3883 &addr, &addrlen); 3884 if (error) { 3885 eprintsoline(so, error); 3886 goto done; 3887 } 3888 } 3889 } else { 3890 addr = name; 3891 addrlen = namelen; 3892 src = NULL; 3893 srclen = 0; 3894 } 3895 tudr.PRIM_type = T_UNITDATA_REQ; 3896 tudr.DEST_length = addrlen; 3897 tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 3898 if (srclen == 0) { 3899 tudr.OPT_length = 0; 3900 tudr.OPT_offset = 0; 3901 3902 mp = soallocproto2(&tudr, sizeof (tudr), 3903 addr, addrlen, 0, _ALLOC_INTR, CRED()); 3904 if (mp == NULL) { 3905 /* 3906 * Caught a signal waiting for memory. 3907 * Let send* return EINTR. 3908 */ 3909 error = EINTR; 3910 goto done; 3911 } 3912 } else { 3913 /* 3914 * There is a AF_UNIX sockaddr_un to include as a source 3915 * address option. 3916 */ 3917 struct T_opthdr toh; 3918 ssize_t size; 3919 3920 tudr.OPT_length = (t_scalar_t)(sizeof (toh) + 3921 _TPI_ALIGN_TOPT(srclen)); 3922 tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 3923 _TPI_ALIGN_TOPT(addrlen)); 3924 3925 toh.level = SOL_SOCKET; 3926 toh.name = SO_SRCADDR; 3927 toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 3928 toh.status = 0; 3929 3930 size = tudr.OPT_offset + tudr.OPT_length; 3931 mp = soallocproto2(&tudr, sizeof (tudr), 3932 addr, addrlen, size, _ALLOC_INTR, CRED()); 3933 if (mp == NULL) { 3934 /* 3935 * Caught a signal waiting for memory. 3936 * Let send* return EINTR. 3937 */ 3938 error = EINTR; 3939 goto done; 3940 } 3941 mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 3942 soappendmsg(mp, &toh, sizeof (toh)); 3943 soappendmsg(mp, src, srclen); 3944 mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 3945 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 3946 } 3947 3948 if (AU_AUDITING()) 3949 audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 3950 3951 error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 3952 done: 3953 #ifdef SOCK_DEBUG 3954 if (error) { 3955 eprintsoline(so, error); 3956 } 3957 #endif /* SOCK_DEBUG */ 3958 return (error); 3959 } 3960 3961 /* 3962 * Sending data on a connected stream socket. 3963 * Assumes caller has verified that SS_ISCONNECTED is set. 3964 */ 3965 int 3966 sosend_svc(struct sonode *so, struct uio *uiop, t_scalar_t prim, int more, 3967 int sflag) 3968 { 3969 struct T_data_req tdr; 3970 mblk_t *mp; 3971 int error; 3972 ssize_t iosize; 3973 sotpi_info_t *sti = SOTOTPI(so); 3974 3975 dprintso(so, 1, 3976 ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n", 3977 (void *)so, uiop->uio_resid, prim, sflag)); 3978 3979 /* 3980 * Has to be bound and connected. However, since no locks are 3981 * held the state could have changed after sotpi_sendmsg checked it 3982 * thus it is not possible to ASSERT on the state. 3983 */ 3984 3985 do { 3986 /* 3987 * Set the MORE flag if uio_resid does not fit in this 3988 * message or if the caller passed in "more". 3989 * Error for transports with zero tidu_size. 3990 */ 3991 tdr.PRIM_type = prim; 3992 iosize = sti->sti_tidu_size; 3993 if (iosize <= 0) 3994 return (EMSGSIZE); 3995 if (uiop->uio_resid > iosize) { 3996 tdr.MORE_flag = 1; 3997 } else { 3998 if (more) 3999 tdr.MORE_flag = 1; 4000 else 4001 tdr.MORE_flag = 0; 4002 iosize = uiop->uio_resid; 4003 } 4004 dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n", 4005 prim, tdr.MORE_flag, iosize)); 4006 mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR, CRED()); 4007 if (mp == NULL) { 4008 /* 4009 * Caught a signal waiting for memory. 4010 * Let send* return EINTR. 4011 */ 4012 return (EINTR); 4013 } 4014 4015 error = kstrputmsg(SOTOV(so), mp, uiop, iosize, 4016 0, sflag | MSG_BAND, 0); 4017 if (error) { 4018 eprintsoline(so, error); 4019 return (error); 4020 } 4021 if (uiop->uio_resid > 0) { 4022 /* 4023 * Recheck for fatal errors. Fail write even though 4024 * some data have been written. This is consistent 4025 * with strwrite semantics and BSD sockets semantics. 4026 */ 4027 if (so->so_state & SS_CANTSENDMORE) { 4028 eprintsoline(so, error); 4029 return (EPIPE); 4030 } 4031 if (so->so_error != 0) { 4032 mutex_enter(&so->so_lock); 4033 error = sogeterr(so, B_TRUE); 4034 mutex_exit(&so->so_lock); 4035 if (error != 0) { 4036 eprintsoline(so, error); 4037 return (error); 4038 } 4039 } 4040 } 4041 } while (uiop->uio_resid > 0); 4042 return (0); 4043 } 4044 4045 /* 4046 * Check the state for errors and call the appropriate send function. 4047 * 4048 * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set) 4049 * this function issues a setsockopt to toggle SO_DONTROUTE before and 4050 * after sending the message. 4051 * 4052 * The caller may optionally specify a destination address, for either 4053 * stream or datagram sockets. This table summarizes the cases: 4054 * 4055 * Socket type Dest. given Connected Result 4056 * ----------- ----------- --------- -------------- 4057 * Stream * Yes send to conn. addr. 4058 * Stream * No error ENOTCONN 4059 * Dgram yes * send to given addr. 4060 * Dgram no yes send to conn. addr. 4061 * Dgram no no error EDESTADDRREQ 4062 * 4063 * There are subtleties around the destination address when using 4064 * AF_UNIX datagram sockets. When the sendmsg call specifies the 4065 * destination address, it's in (struct sockaddr_un) form and we 4066 * need to translate it to our internal form (struct so_ux_addr). 4067 * 4068 * When the sendmsg call does not specify a destination address 4069 * we're using the peer address saved during sotpi_connect, and 4070 * that address is already in internal form. In this case, the 4071 * (internal only) flag MSG_SENDTO_NOXLATE is set in the flags 4072 * passed to sosend_dgram or sosend_dgramcmsg to indicate that 4073 * those functions should skip translation to internal form. 4074 * Avoiding that translation is not only more efficient, but it's 4075 * also necessary when a process does a connect on an AF_UNIX 4076 * datagram socket and then drops privileges. After the process 4077 * has dropped privileges, it may no longer be able to lookup the 4078 * the external name in the filesystem, but it should still be 4079 * able to send messages on the connected socket by leaving the 4080 * destination name unspecified. 4081 * 4082 * Yet more subtleties arise with sockets connected by socketpair(), 4083 * which puts internal form addresses in the fields where normally 4084 * the external form is found, and sets sti_faddr_noxlate=1, which 4085 * (like flag MSG_SENDTO_NOXLATE) causes the sosend_dgram functions 4086 * to skip translation of destination addresses to internal form. 4087 * However, beware that the flag sti_faddr_noxlate=1 also triggers 4088 * different behaviour almost everywhere AF_UNIX addresses appear. 4089 */ 4090 static int 4091 sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, 4092 struct cred *cr) 4093 { 4094 int so_state; 4095 int so_mode; 4096 int error; 4097 struct sockaddr *name; 4098 t_uscalar_t namelen; 4099 int dontroute; 4100 int flags; 4101 sotpi_info_t *sti = SOTOTPI(so); 4102 4103 dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n", 4104 (void *)so, (void *)msg, msg->msg_flags, 4105 pr_state(so->so_state, so->so_mode), so->so_error)); 4106 4107 if (so->so_version == SOV_STREAM) { 4108 /* The imaginary "sockmod" has been popped - act as a stream */ 4109 so_update_attrs(so, SOMOD); 4110 return (strwrite(SOTOV(so), uiop, cr)); 4111 } 4112 4113 mutex_enter(&so->so_lock); 4114 so_state = so->so_state; 4115 4116 if (so_state & SS_CANTSENDMORE) { 4117 mutex_exit(&so->so_lock); 4118 return (EPIPE); 4119 } 4120 4121 if (so->so_error != 0) { 4122 error = sogeterr(so, B_TRUE); 4123 if (error != 0) { 4124 mutex_exit(&so->so_lock); 4125 return (error); 4126 } 4127 } 4128 4129 name = (struct sockaddr *)msg->msg_name; 4130 namelen = msg->msg_namelen; 4131 flags = msg->msg_flags; 4132 4133 /* 4134 * Historically, this function does not validate the flags 4135 * passed in, and any errant bits are ignored. However, 4136 * we would not want any such errant flag bits accidently 4137 * being treated as one of the internal-only flags, so 4138 * clear the internal-only flag bits. 4139 */ 4140 flags &= ~MSG_SENDTO_NOXLATE; 4141 4142 so_mode = so->so_mode; 4143 4144 if (name == NULL) { 4145 if (!(so_state & SS_ISCONNECTED)) { 4146 mutex_exit(&so->so_lock); 4147 if (so_mode & SM_CONNREQUIRED) 4148 return (ENOTCONN); 4149 else 4150 return (EDESTADDRREQ); 4151 } 4152 /* 4153 * This is a connected socket. 4154 */ 4155 if (so_mode & SM_CONNREQUIRED) { 4156 /* 4157 * This is a connected STREAM socket, 4158 * destination not specified. 4159 */ 4160 name = NULL; 4161 namelen = 0; 4162 } else { 4163 /* 4164 * Datagram send on connected socket with 4165 * the destination name not specified. 4166 * Use the peer address from connect. 4167 */ 4168 if (so->so_family == AF_UNIX) { 4169 /* 4170 * Use the (internal form) address saved 4171 * in sotpi_connect. See above. 4172 */ 4173 name = (void *)&sti->sti_ux_faddr; 4174 namelen = sizeof (sti->sti_ux_faddr); 4175 flags |= MSG_SENDTO_NOXLATE; 4176 } else { 4177 ASSERT(sti->sti_faddr_sa); 4178 name = sti->sti_faddr_sa; 4179 namelen = (t_uscalar_t)sti->sti_faddr_len; 4180 } 4181 } 4182 } else { 4183 /* 4184 * Sendmsg specifies a destination name 4185 */ 4186 if (!(so_state & SS_ISCONNECTED) && 4187 (so_mode & SM_CONNREQUIRED)) { 4188 /* i.e. TCP not connected */ 4189 mutex_exit(&so->so_lock); 4190 return (ENOTCONN); 4191 } 4192 /* 4193 * Ignore the address on connection-oriented sockets. 4194 * Just like BSD this code does not generate an error for 4195 * TCP (a CONNREQUIRED socket) when sending to an address 4196 * passed in with sendto/sendmsg. Instead the data is 4197 * delivered on the connection as if no address had been 4198 * supplied. 4199 */ 4200 if ((so_state & SS_ISCONNECTED) && 4201 !(so_mode & SM_CONNREQUIRED)) { 4202 mutex_exit(&so->so_lock); 4203 return (EISCONN); 4204 } 4205 if (!(so_state & SS_ISBOUND)) { 4206 so_lock_single(so); /* Set SOLOCKED */ 4207 error = sotpi_bind(so, NULL, 0, 4208 _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr); 4209 so_unlock_single(so, SOLOCKED); 4210 if (error) { 4211 mutex_exit(&so->so_lock); 4212 eprintsoline(so, error); 4213 return (error); 4214 } 4215 } 4216 /* 4217 * Handle delayed datagram errors. These are only queued 4218 * when the application sets SO_DGRAM_ERRIND. 4219 * Return the error if we are sending to the address 4220 * that was returned in the last T_UDERROR_IND. 4221 * If sending to some other address discard the delayed 4222 * error indication. 4223 */ 4224 if (sti->sti_delayed_error) { 4225 struct T_uderror_ind *tudi; 4226 void *addr; 4227 t_uscalar_t addrlen; 4228 boolean_t match = B_FALSE; 4229 4230 ASSERT(sti->sti_eaddr_mp); 4231 error = sti->sti_delayed_error; 4232 sti->sti_delayed_error = 0; 4233 tudi = 4234 (struct T_uderror_ind *)sti->sti_eaddr_mp->b_rptr; 4235 addrlen = tudi->DEST_length; 4236 addr = sogetoff(sti->sti_eaddr_mp, 4237 tudi->DEST_offset, addrlen, 1); 4238 ASSERT(addr); /* Checked by strsock_proto */ 4239 switch (so->so_family) { 4240 case AF_INET: { 4241 /* Compare just IP address and port */ 4242 sin_t *sin1 = (sin_t *)name; 4243 sin_t *sin2 = (sin_t *)addr; 4244 4245 if (addrlen == sizeof (sin_t) && 4246 namelen == addrlen && 4247 sin1->sin_port == sin2->sin_port && 4248 sin1->sin_addr.s_addr == 4249 sin2->sin_addr.s_addr) 4250 match = B_TRUE; 4251 break; 4252 } 4253 case AF_INET6: { 4254 /* Compare just IP address and port. Not flow */ 4255 sin6_t *sin1 = (sin6_t *)name; 4256 sin6_t *sin2 = (sin6_t *)addr; 4257 4258 if (addrlen == sizeof (sin6_t) && 4259 namelen == addrlen && 4260 sin1->sin6_port == sin2->sin6_port && 4261 IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr, 4262 &sin2->sin6_addr)) 4263 match = B_TRUE; 4264 break; 4265 } 4266 case AF_UNIX: 4267 default: 4268 if (namelen == addrlen && 4269 bcmp(name, addr, namelen) == 0) 4270 match = B_TRUE; 4271 } 4272 if (match) { 4273 freemsg(sti->sti_eaddr_mp); 4274 sti->sti_eaddr_mp = NULL; 4275 mutex_exit(&so->so_lock); 4276 #ifdef DEBUG 4277 dprintso(so, 0, 4278 ("sockfs delayed error %d for %s\n", 4279 error, 4280 pr_addr(so->so_family, name, namelen))); 4281 #endif /* DEBUG */ 4282 return (error); 4283 } 4284 freemsg(sti->sti_eaddr_mp); 4285 sti->sti_eaddr_mp = NULL; 4286 } 4287 } 4288 mutex_exit(&so->so_lock); 4289 4290 dontroute = 0; 4291 if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) { 4292 uint32_t val; 4293 4294 val = 1; 4295 error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE, 4296 &val, (t_uscalar_t)sizeof (val), cr); 4297 if (error) 4298 return (error); 4299 dontroute = 1; 4300 } 4301 4302 if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) { 4303 error = EOPNOTSUPP; 4304 goto done; 4305 } 4306 if (msg->msg_controllen != 0) { 4307 if (!(so_mode & SM_CONNREQUIRED)) { 4308 so_update_attrs(so, SOMOD); 4309 error = sosend_dgramcmsg(so, name, namelen, uiop, 4310 msg->msg_control, msg->msg_controllen, flags); 4311 } else { 4312 if (flags & MSG_OOB) { 4313 /* Can't generate T_EXDATA_REQ with options */ 4314 error = EOPNOTSUPP; 4315 goto done; 4316 } 4317 so_update_attrs(so, SOMOD); 4318 error = sosend_svccmsg(so, uiop, 4319 !(flags & MSG_EOR), 4320 msg->msg_control, msg->msg_controllen, 4321 flags); 4322 } 4323 goto done; 4324 } 4325 4326 so_update_attrs(so, SOMOD); 4327 if (!(so_mode & SM_CONNREQUIRED)) { 4328 /* 4329 * If there is no SO_DONTROUTE to turn off return immediately 4330 * from send_dgram. This can allow tail-call optimizations. 4331 */ 4332 if (!dontroute) { 4333 return (sosend_dgram(so, name, namelen, uiop, flags)); 4334 } 4335 error = sosend_dgram(so, name, namelen, uiop, flags); 4336 } else { 4337 t_scalar_t prim; 4338 int sflag; 4339 4340 /* Ignore msg_name in the connected state */ 4341 if (flags & MSG_OOB) { 4342 prim = T_EXDATA_REQ; 4343 /* 4344 * Send down T_EXDATA_REQ even if there is flow 4345 * control for data. 4346 */ 4347 sflag = MSG_IGNFLOW; 4348 } else { 4349 if (so_mode & SM_BYTESTREAM) { 4350 /* Byte stream transport - use write */ 4351 dprintso(so, 1, ("sotpi_sendmsg: write\n")); 4352 4353 /* Send M_DATA messages */ 4354 /* 4355 * If there is no SO_DONTROUTE to turn off, 4356 * sti_direct is on, and there is no flow 4357 * control, we can take the fast path. 4358 */ 4359 if (!dontroute && sti->sti_direct != 0 && 4360 canputnext(SOTOV(so)->v_stream->sd_wrq)) { 4361 return (sostream_direct(so, uiop, 4362 NULL, cr)); 4363 } 4364 error = strwrite(SOTOV(so), uiop, cr); 4365 goto done; 4366 } 4367 prim = T_DATA_REQ; 4368 sflag = 0; 4369 } 4370 /* 4371 * If there is no SO_DONTROUTE to turn off return immediately 4372 * from sosend_svc. This can allow tail-call optimizations. 4373 */ 4374 if (!dontroute) 4375 return (sosend_svc(so, uiop, prim, 4376 !(flags & MSG_EOR), sflag)); 4377 error = sosend_svc(so, uiop, prim, 4378 !(flags & MSG_EOR), sflag); 4379 } 4380 ASSERT(dontroute); 4381 done: 4382 if (dontroute) { 4383 uint32_t val; 4384 4385 val = 0; 4386 (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE, 4387 &val, (t_uscalar_t)sizeof (val), cr); 4388 } 4389 return (error); 4390 } 4391 4392 /* 4393 * kstrwritemp() has very similar semantics as that of strwrite(). 4394 * The main difference is it obtains mblks from the caller and also 4395 * does not do any copy as done in strwrite() from user buffers to 4396 * kernel buffers. 4397 * 4398 * Currently, this routine is used by sendfile to send data allocated 4399 * within the kernel without any copying. This interface does not use the 4400 * synchronous stream interface as synch. stream interface implies 4401 * copying. 4402 */ 4403 int 4404 kstrwritemp(struct vnode *vp, mblk_t *mp, ushort_t fmode) 4405 { 4406 struct stdata *stp; 4407 struct queue *wqp; 4408 mblk_t *newmp; 4409 char waitflag; 4410 int tempmode; 4411 int error = 0; 4412 int done = 0; 4413 struct sonode *so; 4414 boolean_t direct; 4415 4416 ASSERT(vp->v_stream); 4417 stp = vp->v_stream; 4418 4419 so = VTOSO(vp); 4420 direct = _SOTOTPI(so)->sti_direct; 4421 4422 /* 4423 * This is the sockfs direct fast path. canputnext() need 4424 * not be accurate so we don't grab the sd_lock here. If 4425 * we get flow-controlled, we grab sd_lock just before the 4426 * do..while loop below to emulate what strwrite() does. 4427 */ 4428 wqp = stp->sd_wrq; 4429 if (canputnext(wqp) && direct && 4430 !(stp->sd_flag & (STWRERR|STRHUP|STPLEX))) { 4431 return (sostream_direct(so, NULL, mp, CRED())); 4432 } else if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) { 4433 /* Fast check of flags before acquiring the lock */ 4434 mutex_enter(&stp->sd_lock); 4435 error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0); 4436 mutex_exit(&stp->sd_lock); 4437 if (error != 0) { 4438 if (!(stp->sd_flag & STPLEX) && 4439 (stp->sd_wput_opt & SW_SIGPIPE)) { 4440 error = EPIPE; 4441 } 4442 return (error); 4443 } 4444 } 4445 4446 waitflag = WRITEWAIT; 4447 if (stp->sd_flag & OLDNDELAY) 4448 tempmode = fmode & ~FNDELAY; 4449 else 4450 tempmode = fmode; 4451 4452 mutex_enter(&stp->sd_lock); 4453 do { 4454 if (canputnext(wqp)) { 4455 mutex_exit(&stp->sd_lock); 4456 if (stp->sd_wputdatafunc != NULL) { 4457 newmp = (stp->sd_wputdatafunc)(vp, mp, NULL, 4458 NULL, NULL, NULL); 4459 if (newmp == NULL) { 4460 /* The caller will free mp */ 4461 return (ECOMM); 4462 } 4463 mp = newmp; 4464 } 4465 putnext(wqp, mp); 4466 return (0); 4467 } 4468 error = strwaitq(stp, waitflag, (ssize_t)0, tempmode, -1, 4469 &done); 4470 } while (error == 0 && !done); 4471 4472 mutex_exit(&stp->sd_lock); 4473 /* 4474 * EAGAIN tells the application to try again. ENOMEM 4475 * is returned only if the memory allocation size 4476 * exceeds the physical limits of the system. ENOMEM 4477 * can't be true here. 4478 */ 4479 if (error == ENOMEM) 4480 error = EAGAIN; 4481 return (error); 4482 } 4483 4484 /* ARGSUSED */ 4485 static int 4486 sotpi_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag, 4487 struct cred *cr, mblk_t **mpp) 4488 { 4489 int error; 4490 4491 switch (so->so_family) { 4492 case AF_INET: 4493 case AF_INET6: 4494 case AF_UNIX: 4495 break; 4496 default: 4497 return (EAFNOSUPPORT); 4498 4499 } 4500 4501 if (so->so_state & SS_CANTSENDMORE) 4502 return (EPIPE); 4503 4504 if (so->so_type != SOCK_STREAM) 4505 return (EOPNOTSUPP); 4506 4507 if ((so->so_state & SS_ISCONNECTED) == 0) 4508 return (ENOTCONN); 4509 4510 error = kstrwritemp(so->so_vnode, *mpp, fflag); 4511 if (error == 0) 4512 *mpp = NULL; 4513 return (error); 4514 } 4515 4516 /* 4517 * Sending data on a datagram socket. 4518 * Assumes caller has verified that SS_ISBOUND etc. are set. 4519 */ 4520 /* ARGSUSED */ 4521 static int 4522 sodgram_direct(struct sonode *so, struct sockaddr *name, 4523 socklen_t namelen, struct uio *uiop, int flags) 4524 { 4525 struct T_unitdata_req tudr; 4526 mblk_t *mp = NULL; 4527 int error = 0; 4528 void *addr; 4529 socklen_t addrlen; 4530 ssize_t len; 4531 struct stdata *stp = SOTOV(so)->v_stream; 4532 int so_state; 4533 queue_t *udp_wq; 4534 boolean_t connected; 4535 mblk_t *mpdata = NULL; 4536 sotpi_info_t *sti = SOTOTPI(so); 4537 uint32_t auditing = AU_AUDITING(); 4538 4539 ASSERT(name != NULL && namelen != 0); 4540 ASSERT(!(so->so_mode & SM_CONNREQUIRED)); 4541 ASSERT(!(so->so_mode & SM_EXDATA)); 4542 ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6); 4543 ASSERT(SOTOV(so)->v_type == VSOCK); 4544 4545 /* Caller checked for proper length */ 4546 len = uiop->uio_resid; 4547 ASSERT(len <= sti->sti_tidu_size); 4548 4549 /* Length and family checks have been done by caller */ 4550 ASSERT(name->sa_family == so->so_family); 4551 ASSERT(so->so_family == AF_INET || 4552 (namelen == (socklen_t)sizeof (struct sockaddr_in6))); 4553 ASSERT(so->so_family == AF_INET6 || 4554 (namelen == (socklen_t)sizeof (struct sockaddr_in))); 4555 4556 addr = name; 4557 addrlen = namelen; 4558 4559 if (stp->sd_sidp != NULL && 4560 (error = straccess(stp, JCWRITE)) != 0) 4561 goto done; 4562 4563 so_state = so->so_state; 4564 4565 connected = so_state & SS_ISCONNECTED; 4566 if (!connected) { 4567 tudr.PRIM_type = T_UNITDATA_REQ; 4568 tudr.DEST_length = addrlen; 4569 tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 4570 tudr.OPT_length = 0; 4571 tudr.OPT_offset = 0; 4572 4573 mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0, 4574 _ALLOC_INTR, CRED()); 4575 if (mp == NULL) { 4576 /* 4577 * Caught a signal waiting for memory. 4578 * Let send* return EINTR. 4579 */ 4580 error = EINTR; 4581 goto done; 4582 } 4583 } 4584 4585 /* 4586 * For UDP we don't break up the copyin into smaller pieces 4587 * as in the TCP case. That means if ENOMEM is returned by 4588 * mcopyinuio() then the uio vector has not been modified at 4589 * all and we fallback to either strwrite() or kstrputmsg() 4590 * below. Note also that we never generate priority messages 4591 * from here. 4592 */ 4593 udp_wq = stp->sd_wrq->q_next; 4594 if (canput(udp_wq) && 4595 (mpdata = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) { 4596 ASSERT(DB_TYPE(mpdata) == M_DATA); 4597 ASSERT(uiop->uio_resid == 0); 4598 if (!connected) 4599 linkb(mp, mpdata); 4600 else 4601 mp = mpdata; 4602 if (auditing) 4603 audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 4604 4605 /* Always returns 0... */ 4606 return (udp_wput(udp_wq, mp)); 4607 } 4608 4609 ASSERT(mpdata == NULL); 4610 if (error != 0 && error != ENOMEM) { 4611 freemsg(mp); 4612 return (error); 4613 } 4614 4615 /* 4616 * For connected, let strwrite() handle the blocking case. 4617 * Otherwise we fall thru and use kstrputmsg(). 4618 */ 4619 if (connected) 4620 return (strwrite(SOTOV(so), uiop, CRED())); 4621 4622 if (auditing) 4623 audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 4624 4625 error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 4626 done: 4627 #ifdef SOCK_DEBUG 4628 if (error != 0) { 4629 eprintsoline(so, error); 4630 } 4631 #endif /* SOCK_DEBUG */ 4632 return (error); 4633 } 4634 4635 int 4636 sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr) 4637 { 4638 struct stdata *stp = SOTOV(so)->v_stream; 4639 ssize_t iosize, rmax, maxblk; 4640 queue_t *tcp_wq = stp->sd_wrq->q_next; 4641 mblk_t *newmp; 4642 int error = 0, wflag = 0; 4643 4644 ASSERT(so->so_mode & SM_BYTESTREAM); 4645 ASSERT(SOTOV(so)->v_type == VSOCK); 4646 4647 if (stp->sd_sidp != NULL && 4648 (error = straccess(stp, JCWRITE)) != 0) 4649 return (error); 4650 4651 if (uiop == NULL) { 4652 /* 4653 * kstrwritemp() should have checked sd_flag and 4654 * flow-control before coming here. If we end up 4655 * here it means that we can simply pass down the 4656 * data to tcp. 4657 */ 4658 ASSERT(mp != NULL); 4659 if (stp->sd_wputdatafunc != NULL) { 4660 newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL, 4661 NULL, NULL, NULL); 4662 if (newmp == NULL) { 4663 /* The caller will free mp */ 4664 return (ECOMM); 4665 } 4666 mp = newmp; 4667 } 4668 /* Always returns 0... */ 4669 return (tcp_wput(tcp_wq, mp)); 4670 } 4671 4672 /* Fallback to strwrite() to do proper error handling */ 4673 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY)) 4674 return (strwrite(SOTOV(so), uiop, cr)); 4675 4676 rmax = stp->sd_qn_maxpsz; 4677 ASSERT(rmax >= 0 || rmax == INFPSZ); 4678 if (rmax == 0 || uiop->uio_resid <= 0) 4679 return (0); 4680 4681 if (rmax == INFPSZ) 4682 rmax = uiop->uio_resid; 4683 4684 maxblk = stp->sd_maxblk; 4685 4686 for (;;) { 4687 iosize = MIN(uiop->uio_resid, rmax); 4688 4689 mp = mcopyinuio(stp, uiop, iosize, maxblk, &error); 4690 if (mp == NULL) { 4691 /* 4692 * Fallback to strwrite() for ENOMEM; if this 4693 * is our first time in this routine and the uio 4694 * vector has not been modified, we will end up 4695 * calling strwrite() without any flag set. 4696 */ 4697 if (error == ENOMEM) 4698 goto slow_send; 4699 else 4700 return (error); 4701 } 4702 ASSERT(uiop->uio_resid >= 0); 4703 /* 4704 * If mp is non-NULL and ENOMEM is set, it means that 4705 * mcopyinuio() was able to break down some of the user 4706 * data into one or more mblks. Send the partial data 4707 * to tcp and let the rest be handled in strwrite(). 4708 */ 4709 ASSERT(error == 0 || error == ENOMEM); 4710 if (stp->sd_wputdatafunc != NULL) { 4711 newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL, 4712 NULL, NULL, NULL); 4713 if (newmp == NULL) { 4714 /* The caller will free mp */ 4715 return (ECOMM); 4716 } 4717 mp = newmp; 4718 } 4719 (void) tcp_wput(tcp_wq, mp); /* Always returns 0 anyway. */ 4720 4721 wflag |= NOINTR; 4722 4723 if (uiop->uio_resid == 0) { /* No more data; we're done */ 4724 ASSERT(error == 0); 4725 break; 4726 } else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag & 4727 (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) { 4728 slow_send: 4729 /* 4730 * We were able to send down partial data using 4731 * the direct call interface, but are now relying 4732 * on strwrite() to handle the non-fastpath cases. 4733 * If the socket is blocking we will sleep in 4734 * strwaitq() until write is permitted, otherwise, 4735 * we will need to return the amount of bytes 4736 * written so far back to the app. This is the 4737 * reason why we pass NOINTR flag to strwrite() 4738 * for non-blocking socket, because we don't want 4739 * to return EAGAIN when portion of the user data 4740 * has actually been sent down. 4741 */ 4742 return (strwrite_common(SOTOV(so), uiop, cr, wflag)); 4743 } 4744 } 4745 return (0); 4746 } 4747 4748 /* 4749 * Update sti_faddr by asking the transport (unless AF_UNIX). 4750 */ 4751 /* ARGSUSED */ 4752 int 4753 sotpi_getpeername(struct sonode *so, struct sockaddr *name, socklen_t *namelen, 4754 boolean_t accept, struct cred *cr) 4755 { 4756 struct strbuf strbuf; 4757 int error = 0, res; 4758 void *addr; 4759 t_uscalar_t addrlen; 4760 k_sigset_t smask; 4761 sotpi_info_t *sti = SOTOTPI(so); 4762 4763 dprintso(so, 1, ("sotpi_getpeername(%p) %s\n", 4764 (void *)so, pr_state(so->so_state, so->so_mode))); 4765 4766 ASSERT(*namelen > 0); 4767 mutex_enter(&so->so_lock); 4768 so_lock_single(so); /* Set SOLOCKED */ 4769 4770 if (accept) { 4771 bcopy(sti->sti_faddr_sa, name, 4772 MIN(*namelen, sti->sti_faddr_len)); 4773 *namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len; 4774 goto done; 4775 } 4776 4777 if (!(so->so_state & SS_ISCONNECTED)) { 4778 error = ENOTCONN; 4779 goto done; 4780 } 4781 /* Added this check for X/Open */ 4782 if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 4783 error = EINVAL; 4784 if (xnet_check_print) { 4785 printf("sockfs: X/Open getpeername check => EINVAL\n"); 4786 } 4787 goto done; 4788 } 4789 4790 if (sti->sti_faddr_valid) { 4791 bcopy(sti->sti_faddr_sa, name, 4792 MIN(*namelen, sti->sti_faddr_len)); 4793 *namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len; 4794 goto done; 4795 } 4796 4797 #ifdef DEBUG 4798 dprintso(so, 1, ("sotpi_getpeername (local): %s\n", 4799 pr_addr(so->so_family, sti->sti_faddr_sa, 4800 (t_uscalar_t)sti->sti_faddr_len))); 4801 #endif /* DEBUG */ 4802 4803 if (so->so_family == AF_UNIX) { 4804 /* Transport has different name space - return local info */ 4805 if (sti->sti_faddr_noxlate) 4806 *namelen = 0; 4807 error = 0; 4808 goto done; 4809 } 4810 4811 ASSERT(so->so_family != AF_UNIX && sti->sti_faddr_noxlate == 0); 4812 4813 ASSERT(sti->sti_faddr_sa); 4814 /* Allocate local buffer to use with ioctl */ 4815 addrlen = (t_uscalar_t)sti->sti_faddr_maxlen; 4816 mutex_exit(&so->so_lock); 4817 addr = kmem_alloc(addrlen, KM_SLEEP); 4818 4819 /* 4820 * Issue TI_GETPEERNAME with signals masked. 4821 * Put the result in sti_faddr_sa so that getpeername works after 4822 * a shutdown(output). 4823 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted 4824 * back to the socket. 4825 */ 4826 strbuf.buf = addr; 4827 strbuf.maxlen = addrlen; 4828 strbuf.len = 0; 4829 4830 sigintr(&smask, 0); 4831 res = 0; 4832 ASSERT(cr); 4833 error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf, 4834 0, K_TO_K, cr, &res); 4835 sigunintr(&smask); 4836 4837 mutex_enter(&so->so_lock); 4838 /* 4839 * If there is an error record the error in so_error put don't fail 4840 * the getpeername. Instead fallback on the recorded 4841 * sti->sti_faddr_sa. 4842 */ 4843 if (error) { 4844 /* 4845 * Various stream head errors can be returned to the ioctl. 4846 * However, it is impossible to determine which ones of 4847 * these are really socket level errors that were incorrectly 4848 * consumed by the ioctl. Thus this code silently ignores the 4849 * error - to code explicitly does not reinstate the error 4850 * using soseterror(). 4851 * Experiments have shows that at least this set of 4852 * errors are reported and should not be reinstated on the 4853 * socket: 4854 * EINVAL E.g. if an I_LINK was in effect when 4855 * getpeername was called. 4856 * EPIPE The ioctl error semantics prefer the write 4857 * side error over the read side error. 4858 * ENOTCONN The transport just got disconnected but 4859 * sockfs had not yet seen the T_DISCON_IND 4860 * when issuing the ioctl. 4861 */ 4862 error = 0; 4863 } else if (res == 0 && strbuf.len > 0 && 4864 (so->so_state & SS_ISCONNECTED)) { 4865 ASSERT(strbuf.len <= (int)sti->sti_faddr_maxlen); 4866 sti->sti_faddr_len = (socklen_t)strbuf.len; 4867 bcopy(addr, sti->sti_faddr_sa, sti->sti_faddr_len); 4868 sti->sti_faddr_valid = 1; 4869 4870 bcopy(addr, name, MIN(*namelen, sti->sti_faddr_len)); 4871 *namelen = sti->sti_faddr_len; 4872 } 4873 kmem_free(addr, addrlen); 4874 #ifdef DEBUG 4875 dprintso(so, 1, ("sotpi_getpeername (tp): %s\n", 4876 pr_addr(so->so_family, sti->sti_faddr_sa, 4877 (t_uscalar_t)sti->sti_faddr_len))); 4878 #endif /* DEBUG */ 4879 done: 4880 so_unlock_single(so, SOLOCKED); 4881 mutex_exit(&so->so_lock); 4882 return (error); 4883 } 4884 4885 /* 4886 * Update sti_laddr by asking the transport (unless AF_UNIX). 4887 */ 4888 int 4889 sotpi_getsockname(struct sonode *so, struct sockaddr *name, socklen_t *namelen, 4890 struct cred *cr) 4891 { 4892 struct strbuf strbuf; 4893 int error = 0, res; 4894 void *addr; 4895 t_uscalar_t addrlen; 4896 k_sigset_t smask; 4897 sotpi_info_t *sti = SOTOTPI(so); 4898 4899 dprintso(so, 1, ("sotpi_getsockname(%p) %s\n", 4900 (void *)so, pr_state(so->so_state, so->so_mode))); 4901 4902 ASSERT(*namelen > 0); 4903 mutex_enter(&so->so_lock); 4904 so_lock_single(so); /* Set SOLOCKED */ 4905 4906 #ifdef DEBUG 4907 4908 dprintso(so, 1, ("sotpi_getsockname (local): %s\n", 4909 pr_addr(so->so_family, sti->sti_laddr_sa, 4910 (t_uscalar_t)sti->sti_laddr_len))); 4911 #endif /* DEBUG */ 4912 if (sti->sti_laddr_valid) { 4913 bcopy(sti->sti_laddr_sa, name, 4914 MIN(*namelen, sti->sti_laddr_len)); 4915 *namelen = sti->sti_laddr_len; 4916 goto done; 4917 } 4918 4919 if (so->so_family == AF_UNIX) { 4920 /* 4921 * Transport has different name space - return local info. If we 4922 * have enough space, let consumers know the family. 4923 */ 4924 if (*namelen >= sizeof (sa_family_t)) { 4925 name->sa_family = AF_UNIX; 4926 *namelen = sizeof (sa_family_t); 4927 } else { 4928 *namelen = 0; 4929 } 4930 error = 0; 4931 goto done; 4932 } 4933 if (!(so->so_state & SS_ISBOUND)) { 4934 /* If not bound, then nothing to return. */ 4935 error = 0; 4936 goto done; 4937 } 4938 4939 /* Allocate local buffer to use with ioctl */ 4940 addrlen = (t_uscalar_t)sti->sti_laddr_maxlen; 4941 mutex_exit(&so->so_lock); 4942 addr = kmem_alloc(addrlen, KM_SLEEP); 4943 4944 /* 4945 * Issue TI_GETMYNAME with signals masked. 4946 * Put the result in sti_laddr_sa so that getsockname works after 4947 * a shutdown(output). 4948 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted 4949 * back to the socket. 4950 */ 4951 strbuf.buf = addr; 4952 strbuf.maxlen = addrlen; 4953 strbuf.len = 0; 4954 4955 sigintr(&smask, 0); 4956 res = 0; 4957 ASSERT(cr); 4958 error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf, 4959 0, K_TO_K, cr, &res); 4960 sigunintr(&smask); 4961 4962 mutex_enter(&so->so_lock); 4963 /* 4964 * If there is an error record the error in so_error put don't fail 4965 * the getsockname. Instead fallback on the recorded 4966 * sti->sti_laddr_sa. 4967 */ 4968 if (error) { 4969 /* 4970 * Various stream head errors can be returned to the ioctl. 4971 * However, it is impossible to determine which ones of 4972 * these are really socket level errors that were incorrectly 4973 * consumed by the ioctl. Thus this code silently ignores the 4974 * error - to code explicitly does not reinstate the error 4975 * using soseterror(). 4976 * Experiments have shows that at least this set of 4977 * errors are reported and should not be reinstated on the 4978 * socket: 4979 * EINVAL E.g. if an I_LINK was in effect when 4980 * getsockname was called. 4981 * EPIPE The ioctl error semantics prefer the write 4982 * side error over the read side error. 4983 */ 4984 error = 0; 4985 } else if (res == 0 && strbuf.len > 0 && 4986 (so->so_state & SS_ISBOUND)) { 4987 ASSERT(strbuf.len <= (int)sti->sti_laddr_maxlen); 4988 sti->sti_laddr_len = (socklen_t)strbuf.len; 4989 bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len); 4990 sti->sti_laddr_valid = 1; 4991 4992 bcopy(addr, name, MIN(sti->sti_laddr_len, *namelen)); 4993 *namelen = sti->sti_laddr_len; 4994 } 4995 kmem_free(addr, addrlen); 4996 #ifdef DEBUG 4997 dprintso(so, 1, ("sotpi_getsockname (tp): %s\n", 4998 pr_addr(so->so_family, sti->sti_laddr_sa, 4999 (t_uscalar_t)sti->sti_laddr_len))); 5000 #endif /* DEBUG */ 5001 done: 5002 so_unlock_single(so, SOLOCKED); 5003 mutex_exit(&so->so_lock); 5004 return (error); 5005 } 5006 5007 /* 5008 * Get socket options. For SOL_SOCKET options some options are handled 5009 * by the sockfs while others use the value recorded in the sonode as a 5010 * fallback should the T_SVR4_OPTMGMT_REQ fail. 5011 * 5012 * On the return most *optlenp bytes are copied to optval. 5013 */ 5014 /* ARGSUSED */ 5015 int 5016 sotpi_getsockopt(struct sonode *so, int level, int option_name, 5017 void *optval, socklen_t *optlenp, int flags, struct cred *cr) 5018 { 5019 struct T_optmgmt_req optmgmt_req; 5020 struct T_optmgmt_ack *optmgmt_ack; 5021 struct opthdr oh; 5022 struct opthdr *opt_res; 5023 mblk_t *mp = NULL; 5024 int error = 0; 5025 void *option = NULL; /* Set if fallback value */ 5026 t_uscalar_t maxlen = *optlenp; 5027 t_uscalar_t len; 5028 uint32_t value; 5029 struct timeval tmo_val; /* used for SO_RCVTIMEO, SO_SNDTIMEO */ 5030 struct timeval32 tmo_val32; 5031 struct so_snd_bufinfo snd_bufinfo; /* used for zero copy */ 5032 5033 dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n", 5034 (void *)so, level, option_name, optval, (void *)optlenp, 5035 pr_state(so->so_state, so->so_mode))); 5036 5037 mutex_enter(&so->so_lock); 5038 so_lock_single(so); /* Set SOLOCKED */ 5039 5040 len = (t_uscalar_t)sizeof (uint32_t); /* Default */ 5041 5042 /* 5043 * Check for SOL_SOCKET options. 5044 * Certain SOL_SOCKET options are returned directly whereas 5045 * others only provide a default (fallback) value should 5046 * the T_SVR4_OPTMGMT_REQ fail. 5047 */ 5048 if (level == SOL_SOCKET) { 5049 /* Check parameters */ 5050 switch (option_name) { 5051 case SO_TYPE: 5052 case SO_ERROR: 5053 case SO_DEBUG: 5054 case SO_ACCEPTCONN: 5055 case SO_REUSEADDR: 5056 case SO_KEEPALIVE: 5057 case SO_DONTROUTE: 5058 case SO_BROADCAST: 5059 case SO_USELOOPBACK: 5060 case SO_OOBINLINE: 5061 case SO_SNDBUF: 5062 case SO_RCVBUF: 5063 #ifdef notyet 5064 case SO_SNDLOWAT: 5065 case SO_RCVLOWAT: 5066 #endif /* notyet */ 5067 case SO_DOMAIN: 5068 case SO_DGRAM_ERRIND: 5069 if (maxlen < (t_uscalar_t)sizeof (int32_t)) { 5070 error = EINVAL; 5071 eprintsoline(so, error); 5072 goto done2; 5073 } 5074 break; 5075 case SO_RCVTIMEO: 5076 case SO_SNDTIMEO: 5077 if (get_udatamodel() == DATAMODEL_NONE || 5078 get_udatamodel() == DATAMODEL_NATIVE) { 5079 if (maxlen < sizeof (struct timeval)) { 5080 error = EINVAL; 5081 eprintsoline(so, error); 5082 goto done2; 5083 } 5084 } else { 5085 if (maxlen < sizeof (struct timeval32)) { 5086 error = EINVAL; 5087 eprintsoline(so, error); 5088 goto done2; 5089 } 5090 5091 } 5092 break; 5093 case SO_LINGER: 5094 if (maxlen < (t_uscalar_t)sizeof (struct linger)) { 5095 error = EINVAL; 5096 eprintsoline(so, error); 5097 goto done2; 5098 } 5099 break; 5100 case SO_SND_BUFINFO: 5101 if (maxlen < (t_uscalar_t) 5102 sizeof (struct so_snd_bufinfo)) { 5103 error = EINVAL; 5104 eprintsoline(so, error); 5105 goto done2; 5106 } 5107 break; 5108 } 5109 5110 switch (option_name) { 5111 case SO_TYPE: 5112 value = so->so_type; 5113 option = &value; 5114 goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 5115 5116 case SO_ERROR: 5117 value = sogeterr(so, B_TRUE); 5118 option = &value; 5119 goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 5120 5121 case SO_ACCEPTCONN: 5122 if (so->so_state & SS_ACCEPTCONN) 5123 value = SO_ACCEPTCONN; 5124 else 5125 value = 0; 5126 #ifdef DEBUG 5127 if (value) { 5128 dprintso(so, 1, 5129 ("sotpi_getsockopt: 0x%x is set\n", 5130 option_name)); 5131 } else { 5132 dprintso(so, 1, 5133 ("sotpi_getsockopt: 0x%x not set\n", 5134 option_name)); 5135 } 5136 #endif /* DEBUG */ 5137 option = &value; 5138 goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 5139 5140 case SO_DEBUG: 5141 case SO_REUSEADDR: 5142 case SO_KEEPALIVE: 5143 case SO_DONTROUTE: 5144 case SO_BROADCAST: 5145 case SO_USELOOPBACK: 5146 case SO_OOBINLINE: 5147 case SO_DGRAM_ERRIND: 5148 value = (so->so_options & option_name); 5149 #ifdef DEBUG 5150 if (value) { 5151 dprintso(so, 1, 5152 ("sotpi_getsockopt: 0x%x is set\n", 5153 option_name)); 5154 } else { 5155 dprintso(so, 1, 5156 ("sotpi_getsockopt: 0x%x not set\n", 5157 option_name)); 5158 } 5159 #endif /* DEBUG */ 5160 option = &value; 5161 goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 5162 5163 /* 5164 * The following options are only returned by sockfs when the 5165 * T_SVR4_OPTMGMT_REQ fails. 5166 */ 5167 case SO_LINGER: 5168 option = &so->so_linger; 5169 len = (t_uscalar_t)sizeof (struct linger); 5170 break; 5171 case SO_SNDBUF: { 5172 ssize_t lvalue; 5173 5174 /* 5175 * If the option has not been set then get a default 5176 * value from the read queue. This value is 5177 * returned if the transport fails 5178 * the T_SVR4_OPTMGMT_REQ. 5179 */ 5180 lvalue = so->so_sndbuf; 5181 if (lvalue == 0) { 5182 mutex_exit(&so->so_lock); 5183 (void) strqget(strvp2wq(SOTOV(so))->q_next, 5184 QHIWAT, 0, &lvalue); 5185 mutex_enter(&so->so_lock); 5186 dprintso(so, 1, 5187 ("got SO_SNDBUF %ld from q\n", lvalue)); 5188 } 5189 value = (int)lvalue; 5190 option = &value; 5191 len = (t_uscalar_t)sizeof (so->so_sndbuf); 5192 break; 5193 } 5194 case SO_RCVBUF: { 5195 ssize_t lvalue; 5196 5197 /* 5198 * If the option has not been set then get a default 5199 * value from the read queue. This value is 5200 * returned if the transport fails 5201 * the T_SVR4_OPTMGMT_REQ. 5202 * 5203 * XXX If SO_RCVBUF has been set and this is an 5204 * XPG 4.2 application then do not ask the transport 5205 * since the transport might adjust the value and not 5206 * return exactly what was set by the application. 5207 * For non-XPG 4.2 application we return the value 5208 * that the transport is actually using. 5209 */ 5210 lvalue = so->so_rcvbuf; 5211 if (lvalue == 0) { 5212 mutex_exit(&so->so_lock); 5213 (void) strqget(RD(strvp2wq(SOTOV(so))), 5214 QHIWAT, 0, &lvalue); 5215 mutex_enter(&so->so_lock); 5216 dprintso(so, 1, 5217 ("got SO_RCVBUF %ld from q\n", lvalue)); 5218 } else if (flags & _SOGETSOCKOPT_XPG4_2) { 5219 value = (int)lvalue; 5220 option = &value; 5221 goto copyout; /* skip asking transport */ 5222 } 5223 value = (int)lvalue; 5224 option = &value; 5225 len = (t_uscalar_t)sizeof (so->so_rcvbuf); 5226 break; 5227 } 5228 case SO_DOMAIN: 5229 value = so->so_family; 5230 option = &value; 5231 goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 5232 5233 #ifdef notyet 5234 /* 5235 * We do not implement the semantics of these options 5236 * thus we shouldn't implement the options either. 5237 */ 5238 case SO_SNDLOWAT: 5239 value = so->so_sndlowat; 5240 option = &value; 5241 break; 5242 case SO_RCVLOWAT: 5243 value = so->so_rcvlowat; 5244 option = &value; 5245 break; 5246 #endif /* notyet */ 5247 case SO_SNDTIMEO: 5248 case SO_RCVTIMEO: { 5249 clock_t val; 5250 5251 if (option_name == SO_RCVTIMEO) 5252 val = drv_hztousec(so->so_rcvtimeo); 5253 else 5254 val = drv_hztousec(so->so_sndtimeo); 5255 tmo_val.tv_sec = val / (1000 * 1000); 5256 tmo_val.tv_usec = val % (1000 * 1000); 5257 if (get_udatamodel() == DATAMODEL_NONE || 5258 get_udatamodel() == DATAMODEL_NATIVE) { 5259 option = &tmo_val; 5260 len = sizeof (struct timeval); 5261 } else { 5262 TIMEVAL_TO_TIMEVAL32(&tmo_val32, &tmo_val); 5263 option = &tmo_val32; 5264 len = sizeof (struct timeval32); 5265 } 5266 break; 5267 } 5268 case SO_SND_BUFINFO: { 5269 snd_bufinfo.sbi_wroff = 5270 (so->so_proto_props).sopp_wroff; 5271 snd_bufinfo.sbi_maxblk = 5272 (so->so_proto_props).sopp_maxblk; 5273 snd_bufinfo.sbi_maxpsz = 5274 (so->so_proto_props).sopp_maxpsz; 5275 snd_bufinfo.sbi_tail = 5276 (so->so_proto_props).sopp_tail; 5277 option = &snd_bufinfo; 5278 len = (t_uscalar_t)sizeof (struct so_snd_bufinfo); 5279 break; 5280 } 5281 } 5282 } 5283 5284 mutex_exit(&so->so_lock); 5285 5286 /* Send request */ 5287 optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ; 5288 optmgmt_req.MGMT_flags = T_CHECK; 5289 optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen); 5290 optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req); 5291 5292 oh.level = level; 5293 oh.name = option_name; 5294 oh.len = maxlen; 5295 5296 mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req), 5297 &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP, cr); 5298 /* Let option management work in the presence of data flow control */ 5299 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 5300 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 5301 mp = NULL; 5302 mutex_enter(&so->so_lock); 5303 if (error) { 5304 eprintsoline(so, error); 5305 goto done2; 5306 } 5307 error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK, 5308 (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0); 5309 if (error) { 5310 if (option != NULL) { 5311 /* We have a fallback value */ 5312 error = 0; 5313 goto copyout; 5314 } 5315 eprintsoline(so, error); 5316 goto done2; 5317 } 5318 ASSERT(mp); 5319 optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr; 5320 opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset, 5321 optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE); 5322 if (opt_res == NULL) { 5323 if (option != NULL) { 5324 /* We have a fallback value */ 5325 error = 0; 5326 goto copyout; 5327 } 5328 error = EPROTO; 5329 eprintsoline(so, error); 5330 goto done; 5331 } 5332 option = &opt_res[1]; 5333 5334 /* check to ensure that the option is within bounds */ 5335 if (((uintptr_t)option + opt_res->len < (uintptr_t)option) || 5336 (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) { 5337 if (option != NULL) { 5338 /* We have a fallback value */ 5339 error = 0; 5340 goto copyout; 5341 } 5342 error = EPROTO; 5343 eprintsoline(so, error); 5344 goto done; 5345 } 5346 5347 len = opt_res->len; 5348 5349 copyout: { 5350 t_uscalar_t size = MIN(len, maxlen); 5351 bcopy(option, optval, size); 5352 bcopy(&size, optlenp, sizeof (size)); 5353 } 5354 done: 5355 freemsg(mp); 5356 done2: 5357 so_unlock_single(so, SOLOCKED); 5358 mutex_exit(&so->so_lock); 5359 5360 return (error); 5361 } 5362 5363 /* 5364 * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ. 5365 * SOL_SOCKET options are also recorded in the sonode. A setsockopt for 5366 * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails - 5367 * setsockopt has to work even if the transport does not support the option. 5368 */ 5369 /* ARGSUSED */ 5370 int 5371 sotpi_setsockopt(struct sonode *so, int level, int option_name, 5372 const void *optval, t_uscalar_t optlen, struct cred *cr) 5373 { 5374 struct T_optmgmt_req optmgmt_req; 5375 struct opthdr oh; 5376 mblk_t *mp; 5377 int error = 0; 5378 boolean_t handled = B_FALSE; 5379 5380 dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n", 5381 (void *)so, level, option_name, optval, optlen, 5382 pr_state(so->so_state, so->so_mode))); 5383 5384 /* X/Open requires this check */ 5385 if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 5386 if (xnet_check_print) 5387 printf("sockfs: X/Open setsockopt check => EINVAL\n"); 5388 return (EINVAL); 5389 } 5390 5391 mutex_enter(&so->so_lock); 5392 so_lock_single(so); /* Set SOLOCKED */ 5393 mutex_exit(&so->so_lock); 5394 5395 optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ; 5396 optmgmt_req.MGMT_flags = T_NEGOTIATE; 5397 optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen; 5398 optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req); 5399 5400 oh.level = level; 5401 oh.name = option_name; 5402 oh.len = optlen; 5403 5404 mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req), 5405 &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP, cr); 5406 /* Let option management work in the presence of data flow control */ 5407 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 5408 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 5409 mp = NULL; 5410 mutex_enter(&so->so_lock); 5411 if (error) { 5412 eprintsoline(so, error); 5413 goto done2; 5414 } 5415 error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK, 5416 (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0); 5417 if (error) { 5418 eprintsoline(so, error); 5419 goto done; 5420 } 5421 ASSERT(mp); 5422 /* No need to verify T_optmgmt_ack */ 5423 freemsg(mp); 5424 done: 5425 /* 5426 * Check for SOL_SOCKET options and record their values. 5427 * If we know about a SOL_SOCKET parameter and the transport 5428 * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or 5429 * EPROTO) we let the setsockopt succeed. 5430 */ 5431 if (level == SOL_SOCKET) { 5432 /* Check parameters */ 5433 switch (option_name) { 5434 case SO_DEBUG: 5435 case SO_REUSEADDR: 5436 case SO_KEEPALIVE: 5437 case SO_DONTROUTE: 5438 case SO_BROADCAST: 5439 case SO_USELOOPBACK: 5440 case SO_OOBINLINE: 5441 case SO_SNDBUF: 5442 case SO_RCVBUF: 5443 #ifdef notyet 5444 case SO_SNDLOWAT: 5445 case SO_RCVLOWAT: 5446 #endif /* notyet */ 5447 case SO_DGRAM_ERRIND: 5448 if (optlen != (t_uscalar_t)sizeof (int32_t)) { 5449 error = EINVAL; 5450 eprintsoline(so, error); 5451 goto done2; 5452 } 5453 ASSERT(optval); 5454 handled = B_TRUE; 5455 break; 5456 case SO_SNDTIMEO: 5457 case SO_RCVTIMEO: 5458 if (get_udatamodel() == DATAMODEL_NONE || 5459 get_udatamodel() == DATAMODEL_NATIVE) { 5460 if (optlen != sizeof (struct timeval)) { 5461 error = EINVAL; 5462 eprintsoline(so, error); 5463 goto done2; 5464 } 5465 } else { 5466 if (optlen != sizeof (struct timeval32)) { 5467 error = EINVAL; 5468 eprintsoline(so, error); 5469 goto done2; 5470 } 5471 } 5472 ASSERT(optval); 5473 handled = B_TRUE; 5474 break; 5475 case SO_LINGER: 5476 if (optlen != (t_uscalar_t)sizeof (struct linger)) { 5477 error = EINVAL; 5478 eprintsoline(so, error); 5479 goto done2; 5480 } 5481 ASSERT(optval); 5482 handled = B_TRUE; 5483 break; 5484 } 5485 5486 #define intvalue (*(int32_t *)optval) 5487 5488 switch (option_name) { 5489 case SO_TYPE: 5490 case SO_ERROR: 5491 case SO_ACCEPTCONN: 5492 /* Can't be set */ 5493 error = ENOPROTOOPT; 5494 goto done2; 5495 case SO_LINGER: { 5496 struct linger *l = (struct linger *)optval; 5497 5498 so->so_linger.l_linger = l->l_linger; 5499 if (l->l_onoff) { 5500 so->so_linger.l_onoff = SO_LINGER; 5501 so->so_options |= SO_LINGER; 5502 } else { 5503 so->so_linger.l_onoff = 0; 5504 so->so_options &= ~SO_LINGER; 5505 } 5506 break; 5507 } 5508 5509 case SO_DEBUG: 5510 #ifdef SOCK_TEST 5511 if (intvalue & 2) 5512 sock_test_timelimit = 10 * hz; 5513 else 5514 sock_test_timelimit = 0; 5515 5516 if (intvalue & 4) 5517 do_useracc = 0; 5518 else 5519 do_useracc = 1; 5520 #endif /* SOCK_TEST */ 5521 /* FALLTHRU */ 5522 case SO_REUSEADDR: 5523 case SO_KEEPALIVE: 5524 case SO_DONTROUTE: 5525 case SO_BROADCAST: 5526 case SO_USELOOPBACK: 5527 case SO_OOBINLINE: 5528 case SO_DGRAM_ERRIND: 5529 if (intvalue != 0) { 5530 dprintso(so, 1, 5531 ("socket_setsockopt: setting 0x%x\n", 5532 option_name)); 5533 so->so_options |= option_name; 5534 } else { 5535 dprintso(so, 1, 5536 ("socket_setsockopt: clearing 0x%x\n", 5537 option_name)); 5538 so->so_options &= ~option_name; 5539 } 5540 break; 5541 /* 5542 * The following options are only returned by us when the 5543 * transport layer fails. 5544 * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs 5545 * since the transport might adjust the value and not 5546 * return exactly what was set by the application. 5547 */ 5548 case SO_SNDBUF: 5549 so->so_sndbuf = intvalue; 5550 break; 5551 case SO_RCVBUF: 5552 so->so_rcvbuf = intvalue; 5553 break; 5554 case SO_RCVPSH: 5555 so->so_rcv_timer_interval = intvalue; 5556 break; 5557 #ifdef notyet 5558 /* 5559 * We do not implement the semantics of these options 5560 * thus we shouldn't implement the options either. 5561 */ 5562 case SO_SNDLOWAT: 5563 so->so_sndlowat = intvalue; 5564 break; 5565 case SO_RCVLOWAT: 5566 so->so_rcvlowat = intvalue; 5567 break; 5568 #endif /* notyet */ 5569 case SO_SNDTIMEO: 5570 case SO_RCVTIMEO: { 5571 struct timeval tl; 5572 clock_t val; 5573 5574 if (get_udatamodel() == DATAMODEL_NONE || 5575 get_udatamodel() == DATAMODEL_NATIVE) 5576 bcopy(&tl, (struct timeval *)optval, 5577 sizeof (struct timeval)); 5578 else 5579 TIMEVAL32_TO_TIMEVAL(&tl, 5580 (struct timeval32 *)optval); 5581 val = tl.tv_sec * 1000 * 1000 + tl.tv_usec; 5582 if (option_name == SO_RCVTIMEO) 5583 so->so_rcvtimeo = drv_usectohz(val); 5584 else 5585 so->so_sndtimeo = drv_usectohz(val); 5586 break; 5587 } 5588 } 5589 #undef intvalue 5590 5591 if (error) { 5592 if ((error == ENOPROTOOPT || error == EPROTO || 5593 error == EINVAL) && handled) { 5594 dprintso(so, 1, 5595 ("setsockopt: ignoring error %d for 0x%x\n", 5596 error, option_name)); 5597 error = 0; 5598 } 5599 } 5600 } 5601 done2: 5602 so_unlock_single(so, SOLOCKED); 5603 mutex_exit(&so->so_lock); 5604 return (error); 5605 } 5606 5607 /* 5608 * sotpi_close() is called when the last open reference goes away. 5609 */ 5610 /* ARGSUSED */ 5611 int 5612 sotpi_close(struct sonode *so, int flag, struct cred *cr) 5613 { 5614 struct vnode *vp = SOTOV(so); 5615 dev_t dev; 5616 int error = 0; 5617 sotpi_info_t *sti = SOTOTPI(so); 5618 5619 dprintso(so, 1, ("sotpi_close(%p, %x) %s\n", 5620 (void *)vp, flag, pr_state(so->so_state, so->so_mode))); 5621 5622 dev = sti->sti_dev; 5623 5624 ASSERT(STREAMSTAB(getmajor(dev))); 5625 5626 mutex_enter(&so->so_lock); 5627 so_lock_single(so); /* Set SOLOCKED */ 5628 5629 ASSERT(so_verify_oobstate(so)); 5630 5631 if (vp->v_stream != NULL) { 5632 vnode_t *ux_vp; 5633 5634 if (so->so_family == AF_UNIX) { 5635 /* Could avoid this when CANTSENDMORE for !dgram */ 5636 so_unix_close(so); 5637 } 5638 5639 mutex_exit(&so->so_lock); 5640 /* 5641 * Disassemble the linkage from the AF_UNIX underlying file 5642 * system vnode to this socket (by atomically clearing 5643 * v_stream in vn_rele_stream) before strclose clears sd_vnode 5644 * and frees the stream head. 5645 */ 5646 if ((ux_vp = sti->sti_ux_bound_vp) != NULL) { 5647 ASSERT(ux_vp->v_stream); 5648 sti->sti_ux_bound_vp = NULL; 5649 vn_rele_stream(ux_vp); 5650 } 5651 error = strclose(vp, flag, cr); 5652 vp->v_stream = NULL; 5653 mutex_enter(&so->so_lock); 5654 } 5655 5656 /* 5657 * Flush the T_DISCON_IND on sti_discon_ind_mp. 5658 */ 5659 so_flush_discon_ind(so); 5660 5661 so_unlock_single(so, SOLOCKED); 5662 mutex_exit(&so->so_lock); 5663 5664 /* 5665 * Needed for STREAMs. 5666 * Decrement the device driver's reference count for streams 5667 * opened via the clone dip. The driver was held in clone_open(). 5668 * The absence of clone_close() forces this asymmetry. 5669 */ 5670 if (so->so_flag & SOCLONE) 5671 ddi_rele_driver(getmajor(dev)); 5672 5673 return (error); 5674 } 5675 5676 static int 5677 sotpi_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode, 5678 struct cred *cr, int32_t *rvalp) 5679 { 5680 struct vnode *vp = SOTOV(so); 5681 sotpi_info_t *sti = SOTOTPI(so); 5682 int error = 0; 5683 5684 dprintso(so, 0, ("sotpi_ioctl: cmd 0x%x, arg 0x%lx, state %s\n", 5685 cmd, arg, pr_state(so->so_state, so->so_mode))); 5686 5687 switch (cmd) { 5688 case SIOCSQPTR: 5689 /* 5690 * SIOCSQPTR is valid only when helper stream is created 5691 * by the protocol. 5692 */ 5693 case _I_INSERT: 5694 case _I_REMOVE: 5695 /* 5696 * Since there's no compelling reason to support these ioctls 5697 * on sockets, and doing so would increase the complexity 5698 * markedly, prevent it. 5699 */ 5700 return (EOPNOTSUPP); 5701 5702 case I_FIND: 5703 case I_LIST: 5704 case I_LOOK: 5705 case I_POP: 5706 case I_PUSH: 5707 /* 5708 * To prevent races and inconsistencies between the actual 5709 * state of the stream and the state according to the sonode, 5710 * we serialize all operations which modify or operate on the 5711 * list of modules on the socket's stream. 5712 */ 5713 mutex_enter(&sti->sti_plumb_lock); 5714 error = socktpi_plumbioctl(vp, cmd, arg, mode, cr, rvalp); 5715 mutex_exit(&sti->sti_plumb_lock); 5716 return (error); 5717 5718 default: 5719 if (so->so_version != SOV_STREAM) 5720 break; 5721 5722 /* 5723 * The imaginary "sockmod" has been popped; act as a stream. 5724 */ 5725 return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp)); 5726 } 5727 5728 ASSERT(so->so_version != SOV_STREAM); 5729 5730 /* 5731 * Process socket-specific ioctls. 5732 */ 5733 switch (cmd) { 5734 case FIONBIO: { 5735 int32_t value; 5736 5737 if (so_copyin((void *)arg, &value, sizeof (int32_t), 5738 (mode & (int)FKIOCTL))) 5739 return (EFAULT); 5740 5741 mutex_enter(&so->so_lock); 5742 if (value) { 5743 so->so_state |= SS_NDELAY; 5744 } else { 5745 so->so_state &= ~SS_NDELAY; 5746 } 5747 mutex_exit(&so->so_lock); 5748 return (0); 5749 } 5750 5751 case FIOASYNC: { 5752 int32_t value; 5753 5754 if (so_copyin((void *)arg, &value, sizeof (int32_t), 5755 (mode & (int)FKIOCTL))) 5756 return (EFAULT); 5757 5758 mutex_enter(&so->so_lock); 5759 /* 5760 * SS_ASYNC flag not already set correctly? 5761 * (!value != !(so->so_state & SS_ASYNC)) 5762 * but some engineers find that too hard to read. 5763 */ 5764 if ((value == 0 && (so->so_state & SS_ASYNC) != 0) || 5765 (value != 0 && (so->so_state & SS_ASYNC) == 0)) 5766 error = so_flip_async(so, vp, mode, cr); 5767 mutex_exit(&so->so_lock); 5768 return (error); 5769 } 5770 5771 case SIOCSPGRP: 5772 case FIOSETOWN: { 5773 pid_t pgrp; 5774 5775 if (so_copyin((void *)arg, &pgrp, sizeof (pid_t), 5776 (mode & (int)FKIOCTL))) 5777 return (EFAULT); 5778 5779 mutex_enter(&so->so_lock); 5780 dprintso(so, 1, ("setown: new %d old %d\n", pgrp, so->so_pgrp)); 5781 /* Any change? */ 5782 if (pgrp != so->so_pgrp) 5783 error = so_set_siggrp(so, vp, pgrp, mode, cr); 5784 mutex_exit(&so->so_lock); 5785 return (error); 5786 } 5787 case SIOCGPGRP: 5788 case FIOGETOWN: 5789 if (so_copyout(&so->so_pgrp, (void *)arg, 5790 sizeof (pid_t), (mode & (int)FKIOCTL))) 5791 return (EFAULT); 5792 return (0); 5793 5794 case SIOCATMARK: { 5795 int retval; 5796 uint_t so_state; 5797 5798 /* 5799 * strwaitmark has a finite timeout after which it 5800 * returns -1 if the mark state is undetermined. 5801 * In order to avoid any race between the mark state 5802 * in sockfs and the mark state in the stream head this 5803 * routine loops until the mark state can be determined 5804 * (or the urgent data indication has been removed by some 5805 * other thread). 5806 */ 5807 do { 5808 mutex_enter(&so->so_lock); 5809 so_state = so->so_state; 5810 mutex_exit(&so->so_lock); 5811 if (so_state & SS_RCVATMARK) { 5812 retval = 1; 5813 } else if (!(so_state & SS_OOBPEND)) { 5814 /* 5815 * No SIGURG has been generated -- there is no 5816 * pending or present urgent data. Thus can't 5817 * possibly be at the mark. 5818 */ 5819 retval = 0; 5820 } else { 5821 /* 5822 * Have the stream head wait until there is 5823 * either some messages on the read queue, or 5824 * STRATMARK or STRNOTATMARK gets set. The 5825 * STRNOTATMARK flag is used so that the 5826 * transport can send up a MSGNOTMARKNEXT 5827 * M_DATA to indicate that it is not 5828 * at the mark and additional data is not about 5829 * to be send upstream. 5830 * 5831 * If the mark state is undetermined this will 5832 * return -1 and we will loop rechecking the 5833 * socket state. 5834 */ 5835 retval = strwaitmark(vp); 5836 } 5837 } while (retval == -1); 5838 5839 if (so_copyout(&retval, (void *)arg, sizeof (int), 5840 (mode & (int)FKIOCTL))) 5841 return (EFAULT); 5842 return (0); 5843 } 5844 5845 case I_FDINSERT: 5846 case I_SENDFD: 5847 case I_RECVFD: 5848 case I_ATMARK: 5849 case _SIOCSOCKFALLBACK: 5850 /* 5851 * These ioctls do not apply to sockets. I_FDINSERT can be 5852 * used to send M_PROTO messages without modifying the socket 5853 * state. I_SENDFD/RECVFD should not be used for socket file 5854 * descriptor passing since they assume a twisted stream. 5855 * SIOCATMARK must be used instead of I_ATMARK. 5856 * 5857 * _SIOCSOCKFALLBACK from an application should never be 5858 * processed. It is only generated by socktpi_open() or 5859 * in response to I_POP or I_PUSH. 5860 */ 5861 #ifdef DEBUG 5862 zcmn_err(getzoneid(), CE_WARN, 5863 "Unsupported STREAMS ioctl 0x%x on socket. " 5864 "Pid = %d\n", cmd, curproc->p_pid); 5865 #endif /* DEBUG */ 5866 return (EOPNOTSUPP); 5867 5868 case _I_GETPEERCRED: 5869 if ((mode & FKIOCTL) == 0) 5870 return (EINVAL); 5871 5872 mutex_enter(&so->so_lock); 5873 if ((so->so_mode & SM_CONNREQUIRED) == 0) { 5874 error = ENOTSUP; 5875 } else if ((so->so_state & SS_ISCONNECTED) == 0) { 5876 error = ENOTCONN; 5877 } else if (so->so_peercred != NULL) { 5878 k_peercred_t *kp = (k_peercred_t *)arg; 5879 kp->pc_cr = so->so_peercred; 5880 kp->pc_cpid = so->so_cpid; 5881 crhold(so->so_peercred); 5882 } else { 5883 error = EINVAL; 5884 } 5885 mutex_exit(&so->so_lock); 5886 return (error); 5887 5888 default: 5889 /* 5890 * Do the higher-order bits of the ioctl cmd indicate 5891 * that it is an I_* streams ioctl? 5892 */ 5893 if ((cmd & 0xffffff00U) == STR && 5894 so->so_version == SOV_SOCKBSD) { 5895 #ifdef DEBUG 5896 zcmn_err(getzoneid(), CE_WARN, 5897 "Unsupported STREAMS ioctl 0x%x on socket. " 5898 "Pid = %d\n", cmd, curproc->p_pid); 5899 #endif /* DEBUG */ 5900 return (EOPNOTSUPP); 5901 } 5902 return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp)); 5903 } 5904 } 5905 5906 /* 5907 * Handle plumbing-related ioctls. 5908 */ 5909 static int 5910 socktpi_plumbioctl(struct vnode *vp, int cmd, intptr_t arg, int mode, 5911 struct cred *cr, int32_t *rvalp) 5912 { 5913 static const char sockmod_name[] = "sockmod"; 5914 struct sonode *so = VTOSO(vp); 5915 char mname[FMNAMESZ + 1]; 5916 int error; 5917 sotpi_info_t *sti = SOTOTPI(so); 5918 5919 ASSERT(MUTEX_HELD(&sti->sti_plumb_lock)); 5920 5921 if (so->so_version == SOV_SOCKBSD) 5922 return (EOPNOTSUPP); 5923 5924 if (so->so_version == SOV_STREAM) { 5925 /* 5926 * The imaginary "sockmod" has been popped - act as a stream. 5927 * If this is a push of sockmod then change back to a socket. 5928 */ 5929 if (cmd == I_PUSH) { 5930 error = ((mode & FKIOCTL) ? copystr : copyinstr)( 5931 (void *)arg, mname, sizeof (mname), NULL); 5932 5933 if (error == 0 && strcmp(mname, sockmod_name) == 0) { 5934 dprintso(so, 0, ("socktpi_ioctl: going to " 5935 "socket version\n")); 5936 so_stream2sock(so); 5937 return (0); 5938 } 5939 } 5940 return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp)); 5941 } 5942 5943 switch (cmd) { 5944 case I_PUSH: 5945 if (sti->sti_direct) { 5946 mutex_enter(&so->so_lock); 5947 so_lock_single(so); 5948 mutex_exit(&so->so_lock); 5949 5950 error = strioctl(vp, _SIOCSOCKFALLBACK, 0, 0, K_TO_K, 5951 cr, rvalp); 5952 5953 mutex_enter(&so->so_lock); 5954 if (error == 0) 5955 sti->sti_direct = 0; 5956 so_unlock_single(so, SOLOCKED); 5957 mutex_exit(&so->so_lock); 5958 5959 if (error != 0) 5960 return (error); 5961 } 5962 5963 error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp); 5964 if (error == 0) 5965 sti->sti_pushcnt++; 5966 return (error); 5967 5968 case I_POP: 5969 if (sti->sti_pushcnt == 0) { 5970 /* Emulate sockmod being popped */ 5971 dprintso(so, 0, 5972 ("socktpi_ioctl: going to STREAMS version\n")); 5973 return (so_sock2stream(so)); 5974 } 5975 5976 error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp); 5977 if (error == 0) 5978 sti->sti_pushcnt--; 5979 return (error); 5980 5981 case I_LIST: { 5982 struct str_mlist *kmlistp, *umlistp; 5983 struct str_list kstrlist; 5984 ssize_t kstrlistsize; 5985 int i, nmods; 5986 5987 STRUCT_DECL(str_list, ustrlist); 5988 STRUCT_INIT(ustrlist, mode); 5989 5990 if (arg == 0) { 5991 error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp); 5992 if (error == 0) 5993 (*rvalp)++; /* Add one for sockmod */ 5994 return (error); 5995 } 5996 5997 error = so_copyin((void *)arg, STRUCT_BUF(ustrlist), 5998 STRUCT_SIZE(ustrlist), mode & FKIOCTL); 5999 if (error != 0) 6000 return (error); 6001 6002 nmods = STRUCT_FGET(ustrlist, sl_nmods); 6003 if (nmods <= 0) 6004 return (EINVAL); 6005 /* 6006 * Ceiling nmods at nstrpush to prevent someone from 6007 * maliciously consuming lots of kernel memory. 6008 */ 6009 nmods = MIN(nmods, nstrpush); 6010 6011 kstrlistsize = (nmods + 1) * sizeof (struct str_mlist); 6012 kstrlist.sl_nmods = nmods; 6013 kstrlist.sl_modlist = kmem_zalloc(kstrlistsize, KM_SLEEP); 6014 6015 error = strioctl(vp, cmd, (intptr_t)&kstrlist, mode, K_TO_K, 6016 cr, rvalp); 6017 if (error != 0) 6018 goto done; 6019 6020 /* 6021 * Considering the module list as a 0-based array of sl_nmods 6022 * modules, sockmod should conceptually exist at slot 6023 * sti_pushcnt. Insert sockmod at this location by sliding all 6024 * of the module names after so_pushcnt over by one. We know 6025 * that there will be room to do this since we allocated 6026 * sl_modlist with an additional slot. 6027 */ 6028 for (i = kstrlist.sl_nmods; i > sti->sti_pushcnt; i--) 6029 kstrlist.sl_modlist[i] = kstrlist.sl_modlist[i - 1]; 6030 6031 (void) strcpy(kstrlist.sl_modlist[i].l_name, sockmod_name); 6032 kstrlist.sl_nmods++; 6033 6034 /* 6035 * Copy all of the entries out to ustrlist. 6036 */ 6037 kmlistp = kstrlist.sl_modlist; 6038 umlistp = STRUCT_FGETP(ustrlist, sl_modlist); 6039 for (i = 0; i < nmods && i < kstrlist.sl_nmods; i++) { 6040 error = so_copyout(kmlistp++, umlistp++, 6041 sizeof (struct str_mlist), mode & FKIOCTL); 6042 if (error != 0) 6043 goto done; 6044 } 6045 6046 error = so_copyout(&i, (void *)arg, sizeof (int32_t), 6047 mode & FKIOCTL); 6048 if (error == 0) 6049 *rvalp = 0; 6050 done: 6051 kmem_free(kstrlist.sl_modlist, kstrlistsize); 6052 return (error); 6053 } 6054 case I_LOOK: 6055 if (sti->sti_pushcnt == 0) { 6056 return (so_copyout(sockmod_name, (void *)arg, 6057 sizeof (sockmod_name), mode & FKIOCTL)); 6058 } 6059 return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp)); 6060 6061 case I_FIND: 6062 error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp); 6063 if (error && error != EINVAL) 6064 return (error); 6065 6066 /* if not found and string was sockmod return 1 */ 6067 if (*rvalp == 0 || error == EINVAL) { 6068 error = ((mode & FKIOCTL) ? copystr : copyinstr)( 6069 (void *)arg, mname, sizeof (mname), NULL); 6070 if (error == ENAMETOOLONG) 6071 error = EINVAL; 6072 6073 if (error == 0 && strcmp(mname, sockmod_name) == 0) 6074 *rvalp = 1; 6075 } 6076 return (error); 6077 6078 default: 6079 panic("socktpi_plumbioctl: unknown ioctl %d", cmd); 6080 break; 6081 } 6082 6083 return (0); 6084 } 6085 6086 /* 6087 * Wrapper around the streams poll routine that implements socket poll 6088 * semantics. 6089 * The sockfs never calls pollwakeup itself - the stream head take care 6090 * of all pollwakeups. Since sockfs never holds so_lock when calling the 6091 * stream head there can never be a deadlock due to holding so_lock across 6092 * pollwakeup and acquiring so_lock in this routine. 6093 * 6094 * However, since the performance of VOP_POLL is critical we avoid 6095 * acquiring so_lock here. This is based on two assumptions: 6096 * - The poll implementation holds locks to serialize the VOP_POLL call 6097 * and a pollwakeup for the same pollhead. This ensures that should 6098 * e.g. so_state change during a socktpi_poll call the pollwakeup 6099 * (which strsock_* and strrput conspire to issue) is issued after 6100 * the state change. Thus the pollwakeup will block until VOP_POLL has 6101 * returned and then wake up poll and have it call VOP_POLL again. 6102 * - The reading of so_state without holding so_lock does not result in 6103 * stale data that is older than the latest state change that has dropped 6104 * so_lock. This is ensured by the mutex_exit issuing the appropriate 6105 * memory barrier to force the data into the coherency domain. 6106 */ 6107 static int 6108 sotpi_poll( 6109 struct sonode *so, 6110 short events, 6111 int anyyet, 6112 short *reventsp, 6113 struct pollhead **phpp) 6114 { 6115 short origevents = events; 6116 struct vnode *vp = SOTOV(so); 6117 int error; 6118 int so_state = so->so_state; /* snapshot */ 6119 sotpi_info_t *sti = SOTOTPI(so); 6120 6121 dprintso(so, 0, ("socktpi_poll(%p): state %s err %d\n", 6122 (void *)vp, pr_state(so_state, so->so_mode), so->so_error)); 6123 6124 ASSERT(vp->v_type == VSOCK); 6125 ASSERT(vp->v_stream != NULL); 6126 6127 if (so->so_version == SOV_STREAM) { 6128 /* The imaginary "sockmod" has been popped - act as a stream */ 6129 return (strpoll(vp->v_stream, events, anyyet, 6130 reventsp, phpp)); 6131 } 6132 6133 if (!(so_state & SS_ISCONNECTED) && 6134 (so->so_mode & SM_CONNREQUIRED)) { 6135 /* Not connected yet - turn off write side events */ 6136 events &= ~(POLLOUT|POLLWRBAND); 6137 } 6138 /* 6139 * Check for errors without calling strpoll if the caller wants them. 6140 * In sockets the errors are represented as input/output events 6141 * and there is no need to ask the stream head for this information. 6142 */ 6143 if (so->so_error != 0 && 6144 ((POLLIN|POLLRDNORM|POLLOUT) & origevents) != 0) { 6145 *reventsp = (POLLIN|POLLRDNORM|POLLOUT) & origevents; 6146 return (0); 6147 } 6148 /* 6149 * Ignore M_PROTO only messages such as the T_EXDATA_IND messages. 6150 * These message with only an M_PROTO/M_PCPROTO part and no M_DATA 6151 * will not trigger a POLLIN event with POLLRDDATA set. 6152 * The handling of urgent data (causing POLLRDBAND) is done by 6153 * inspecting SS_OOBPEND below. 6154 */ 6155 events |= POLLRDDATA; 6156 6157 /* 6158 * After shutdown(output) a stream head write error is set. 6159 * However, we should not return output events. 6160 */ 6161 events |= POLLNOERR; 6162 error = strpoll(vp->v_stream, events, anyyet, 6163 reventsp, phpp); 6164 if (error) 6165 return (error); 6166 6167 ASSERT(!(*reventsp & POLLERR)); 6168 6169 /* 6170 * Notes on T_CONN_IND handling for sockets. 6171 * 6172 * If strpoll() returned without events, SR_POLLIN is guaranteed 6173 * to be set, ensuring any subsequent strrput() runs pollwakeup(). 6174 * 6175 * Since the so_lock is not held, soqueueconnind() may have run 6176 * and a T_CONN_IND may be waiting. We now check for any queued 6177 * T_CONN_IND msgs on sti_conn_ind_head and set appropriate events 6178 * to ensure poll returns. 6179 * 6180 * However: 6181 * If the T_CONN_IND hasn't arrived by the time strpoll() returns, 6182 * when strrput() does run for an arriving M_PROTO with T_CONN_IND 6183 * the following actions will occur; taken together they ensure the 6184 * syscall will return. 6185 * 6186 * 1. If a socket, soqueueconnind() will queue the T_CONN_IND but if 6187 * the accept() was run on a non-blocking socket sowaitconnind() 6188 * may have already returned EWOULDBLOCK, so not be waiting to 6189 * process the message. Additionally socktpi_poll() has probably 6190 * proceeded past the sti_conn_ind_head check below. 6191 * 2. strrput() runs pollwakeup()->pollnotify()->cv_signal() to wake 6192 * this thread, however that could occur before poll_common() 6193 * has entered cv_wait. 6194 * 3. pollnotify() sets T_POLLWAKE, while holding the pc_lock. 6195 * 6196 * Before proceeding to cv_wait() in poll_common() for an event, 6197 * poll_common() atomically checks for T_POLLWAKE under the pc_lock, 6198 * and if set, re-calls strpoll() to ensure the late arriving 6199 * T_CONN_IND is recognized, and pollsys() returns. 6200 */ 6201 6202 if (sti->sti_conn_ind_head != NULL) 6203 *reventsp |= (POLLIN|POLLRDNORM) & events; 6204 6205 if (so->so_state & SS_CANTRCVMORE) { 6206 *reventsp |= POLLRDHUP & events; 6207 6208 if (so->so_state & SS_CANTSENDMORE) 6209 *reventsp |= POLLHUP; 6210 } 6211 6212 if (so->so_state & SS_OOBPEND) 6213 *reventsp |= POLLRDBAND & events; 6214 6215 return (0); 6216 } 6217 6218 /*ARGSUSED*/ 6219 static int 6220 socktpi_constructor(void *buf, void *cdrarg, int kmflags) 6221 { 6222 sotpi_sonode_t *st = (sotpi_sonode_t *)buf; 6223 int error = 0; 6224 6225 error = sonode_constructor(buf, cdrarg, kmflags); 6226 if (error != 0) 6227 return (error); 6228 6229 error = i_sotpi_info_constructor(&st->st_info); 6230 if (error != 0) 6231 sonode_destructor(buf, cdrarg); 6232 6233 st->st_sonode.so_priv = &st->st_info; 6234 6235 return (error); 6236 } 6237 6238 /*ARGSUSED1*/ 6239 static void 6240 socktpi_destructor(void *buf, void *cdrarg) 6241 { 6242 sotpi_sonode_t *st = (sotpi_sonode_t *)buf; 6243 6244 ASSERT(st->st_sonode.so_priv == &st->st_info); 6245 st->st_sonode.so_priv = NULL; 6246 6247 i_sotpi_info_destructor(&st->st_info); 6248 sonode_destructor(buf, cdrarg); 6249 } 6250 6251 static int 6252 socktpi_unix_constructor(void *buf, void *cdrarg, int kmflags) 6253 { 6254 int retval; 6255 6256 if ((retval = socktpi_constructor(buf, cdrarg, kmflags)) == 0) { 6257 struct sonode *so = (struct sonode *)buf; 6258 sotpi_info_t *sti = SOTOTPI(so); 6259 6260 mutex_enter(&socklist.sl_lock); 6261 6262 sti->sti_next_so = socklist.sl_list; 6263 sti->sti_prev_so = NULL; 6264 if (sti->sti_next_so != NULL) 6265 SOTOTPI(sti->sti_next_so)->sti_prev_so = so; 6266 socklist.sl_list = so; 6267 6268 mutex_exit(&socklist.sl_lock); 6269 6270 } 6271 return (retval); 6272 } 6273 6274 static void 6275 socktpi_unix_destructor(void *buf, void *cdrarg) 6276 { 6277 struct sonode *so = (struct sonode *)buf; 6278 sotpi_info_t *sti = SOTOTPI(so); 6279 6280 mutex_enter(&socklist.sl_lock); 6281 6282 if (sti->sti_next_so != NULL) 6283 SOTOTPI(sti->sti_next_so)->sti_prev_so = sti->sti_prev_so; 6284 if (sti->sti_prev_so != NULL) 6285 SOTOTPI(sti->sti_prev_so)->sti_next_so = sti->sti_next_so; 6286 else 6287 socklist.sl_list = sti->sti_next_so; 6288 6289 mutex_exit(&socklist.sl_lock); 6290 6291 socktpi_destructor(buf, cdrarg); 6292 } 6293 6294 int 6295 socktpi_init(void) 6296 { 6297 /* 6298 * Create sonode caches. We create a special one for AF_UNIX so 6299 * that we can track them for netstat(8). 6300 */ 6301 socktpi_cache = kmem_cache_create("socktpi_cache", 6302 sizeof (struct sotpi_sonode), 0, socktpi_constructor, 6303 socktpi_destructor, NULL, NULL, NULL, 0); 6304 6305 socktpi_unix_cache = kmem_cache_create("socktpi_unix_cache", 6306 sizeof (struct sotpi_sonode), 0, socktpi_unix_constructor, 6307 socktpi_unix_destructor, NULL, NULL, NULL, 0); 6308 6309 return (0); 6310 } 6311 6312 /* 6313 * Given a non-TPI sonode, allocate and prep it to be ready for TPI. 6314 * 6315 * Caller must still update state and mode using sotpi_update_state(). 6316 */ 6317 int 6318 sotpi_convert_sonode(struct sonode *so, struct sockparams *newsp, 6319 boolean_t *direct, queue_t **qp, struct cred *cr) 6320 { 6321 sotpi_info_t *sti; 6322 struct sockparams *origsp = so->so_sockparams; 6323 sock_lower_handle_t handle = so->so_proto_handle; 6324 struct stdata *stp; 6325 struct vnode *vp; 6326 queue_t *q; 6327 int error = 0; 6328 6329 ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) == 6330 SS_FALLBACK_PENDING); 6331 ASSERT(SOCK_IS_NONSTR(so)); 6332 6333 *qp = NULL; 6334 *direct = B_FALSE; 6335 so->so_sockparams = newsp; 6336 /* 6337 * Allocate and initalize fields required by TPI. 6338 */ 6339 (void) sotpi_info_create(so, KM_SLEEP); 6340 sotpi_info_init(so); 6341 6342 if ((error = sotpi_init(so, NULL, cr, SO_FALLBACK)) != 0) { 6343 sotpi_info_fini(so); 6344 sotpi_info_destroy(so); 6345 return (error); 6346 } 6347 ASSERT(handle == so->so_proto_handle); 6348 sti = SOTOTPI(so); 6349 if (sti->sti_direct != 0) 6350 *direct = B_TRUE; 6351 6352 /* 6353 * Keep the original sp around so we can properly dispose of the 6354 * sonode when the socket is being closed. 6355 */ 6356 sti->sti_orig_sp = origsp; 6357 6358 so_basic_strinit(so); /* skips the T_CAPABILITY_REQ */ 6359 so_alloc_addr(so, so->so_max_addr_len); 6360 6361 /* 6362 * If the application has done a SIOCSPGRP, make sure the 6363 * STREAM head is aware. This needs to take place before 6364 * the protocol start sending up messages. Otherwise we 6365 * might miss to generate SIGPOLL. 6366 * 6367 * It is possible that the application will receive duplicate 6368 * signals if some were already generated for either data or 6369 * connection indications. 6370 */ 6371 if (so->so_pgrp != 0) { 6372 if (so_set_events(so, so->so_vnode, cr) != 0) 6373 so->so_pgrp = 0; 6374 } 6375 6376 /* 6377 * Determine which queue to use. 6378 */ 6379 vp = SOTOV(so); 6380 stp = vp->v_stream; 6381 ASSERT(stp != NULL); 6382 q = stp->sd_wrq->q_next; 6383 6384 /* 6385 * Skip any modules that may have been auto pushed when the device 6386 * was opened 6387 */ 6388 while (q->q_next != NULL) 6389 q = q->q_next; 6390 *qp = _RD(q); 6391 6392 /* This is now a STREAMS sockets */ 6393 so->so_not_str = B_FALSE; 6394 6395 return (error); 6396 } 6397 6398 /* 6399 * Revert a TPI sonode. It is only allowed to revert the sonode during 6400 * the fallback process. 6401 */ 6402 void 6403 sotpi_revert_sonode(struct sonode *so, struct cred *cr) 6404 { 6405 vnode_t *vp = SOTOV(so); 6406 6407 ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) == 6408 SS_FALLBACK_PENDING); 6409 ASSERT(!SOCK_IS_NONSTR(so)); 6410 ASSERT(vp->v_stream != NULL); 6411 6412 strclean(vp); 6413 (void) strclose(vp, FREAD|FWRITE|SO_FALLBACK, cr); 6414 6415 /* 6416 * Restore the original sockparams. The caller is responsible for 6417 * dropping the ref to the new sp. 6418 */ 6419 so->so_sockparams = SOTOTPI(so)->sti_orig_sp; 6420 6421 sotpi_info_fini(so); 6422 sotpi_info_destroy(so); 6423 6424 /* This is no longer a STREAMS sockets */ 6425 so->so_not_str = B_TRUE; 6426 } 6427 6428 void 6429 sotpi_update_state(struct sonode *so, struct T_capability_ack *tcap, 6430 struct sockaddr *laddr, socklen_t laddrlen, struct sockaddr *faddr, 6431 socklen_t faddrlen, short opts) 6432 { 6433 sotpi_info_t *sti = SOTOTPI(so); 6434 6435 so_proc_tcapability_ack(so, tcap); 6436 6437 so->so_options |= opts; 6438 6439 /* 6440 * Determine whether the foreign and local address are valid 6441 */ 6442 if (laddrlen != 0) { 6443 ASSERT(laddrlen <= sti->sti_laddr_maxlen); 6444 sti->sti_laddr_len = laddrlen; 6445 bcopy(laddr, sti->sti_laddr_sa, laddrlen); 6446 sti->sti_laddr_valid = (so->so_state & SS_ISBOUND); 6447 } 6448 6449 if (faddrlen != 0) { 6450 ASSERT(faddrlen <= sti->sti_faddr_maxlen); 6451 sti->sti_faddr_len = faddrlen; 6452 bcopy(faddr, sti->sti_faddr_sa, faddrlen); 6453 sti->sti_faddr_valid = (so->so_state & SS_ISCONNECTED); 6454 } 6455 6456 } 6457 6458 /* 6459 * Allocate enough space to cache the local and foreign addresses. 6460 */ 6461 void 6462 so_alloc_addr(struct sonode *so, t_uscalar_t maxlen) 6463 { 6464 sotpi_info_t *sti = SOTOTPI(so); 6465 6466 ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL); 6467 ASSERT(sti->sti_laddr_len == 0 && sti->sti_faddr_len == 0); 6468 sti->sti_laddr_maxlen = sti->sti_faddr_maxlen = 6469 P2ROUNDUP(maxlen, KMEM_ALIGN); 6470 so->so_max_addr_len = sti->sti_laddr_maxlen; 6471 sti->sti_laddr_sa = kmem_alloc(sti->sti_laddr_maxlen * 2, KM_SLEEP); 6472 sti->sti_faddr_sa = (struct sockaddr *)((caddr_t)sti->sti_laddr_sa 6473 + sti->sti_laddr_maxlen); 6474 6475 if (so->so_family == AF_UNIX) { 6476 /* 6477 * Initialize AF_UNIX related fields. 6478 */ 6479 bzero(&sti->sti_ux_laddr, sizeof (sti->sti_ux_laddr)); 6480 bzero(&sti->sti_ux_faddr, sizeof (sti->sti_ux_faddr)); 6481 } 6482 } 6483 6484 6485 sotpi_info_t * 6486 sotpi_sototpi(struct sonode *so) 6487 { 6488 sotpi_info_t *sti; 6489 6490 ASSERT(so != NULL); 6491 6492 sti = (sotpi_info_t *)so->so_priv; 6493 6494 ASSERT(sti != NULL); 6495 ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC); 6496 6497 return (sti); 6498 } 6499 6500 static int 6501 i_sotpi_info_constructor(sotpi_info_t *sti) 6502 { 6503 sti->sti_magic = SOTPI_INFO_MAGIC; 6504 sti->sti_ack_mp = NULL; 6505 sti->sti_discon_ind_mp = NULL; 6506 sti->sti_ux_bound_vp = NULL; 6507 sti->sti_unbind_mp = NULL; 6508 6509 sti->sti_conn_ind_head = NULL; 6510 sti->sti_conn_ind_tail = NULL; 6511 6512 sti->sti_laddr_sa = NULL; 6513 sti->sti_faddr_sa = NULL; 6514 6515 mutex_init(&sti->sti_plumb_lock, NULL, MUTEX_DEFAULT, NULL); 6516 cv_init(&sti->sti_ack_cv, NULL, CV_DEFAULT, NULL); 6517 6518 return (0); 6519 } 6520 6521 static void 6522 i_sotpi_info_destructor(sotpi_info_t *sti) 6523 { 6524 ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC); 6525 ASSERT(sti->sti_ack_mp == NULL); 6526 ASSERT(sti->sti_discon_ind_mp == NULL); 6527 ASSERT(sti->sti_ux_bound_vp == NULL); 6528 ASSERT(sti->sti_unbind_mp == NULL); 6529 6530 ASSERT(sti->sti_conn_ind_head == NULL); 6531 ASSERT(sti->sti_conn_ind_tail == NULL); 6532 6533 ASSERT(sti->sti_laddr_sa == NULL); 6534 ASSERT(sti->sti_faddr_sa == NULL); 6535 6536 mutex_destroy(&sti->sti_plumb_lock); 6537 cv_destroy(&sti->sti_ack_cv); 6538 } 6539 6540 /* 6541 * Creates and attaches TPI information to the given sonode 6542 */ 6543 static boolean_t 6544 sotpi_info_create(struct sonode *so, int kmflags) 6545 { 6546 sotpi_info_t *sti; 6547 6548 ASSERT(so->so_priv == NULL); 6549 6550 if ((sti = kmem_zalloc(sizeof (*sti), kmflags)) == NULL) 6551 return (B_FALSE); 6552 6553 if (i_sotpi_info_constructor(sti) != 0) { 6554 kmem_free(sti, sizeof (*sti)); 6555 return (B_FALSE); 6556 } 6557 6558 so->so_priv = (void *)sti; 6559 return (B_TRUE); 6560 } 6561 6562 /* 6563 * Initializes the TPI information. 6564 */ 6565 static void 6566 sotpi_info_init(struct sonode *so) 6567 { 6568 struct vnode *vp = SOTOV(so); 6569 sotpi_info_t *sti = SOTOTPI(so); 6570 time_t now; 6571 6572 sti->sti_dev = so->so_sockparams->sp_sdev_info.sd_vnode->v_rdev; 6573 vp->v_rdev = sti->sti_dev; 6574 6575 sti->sti_orig_sp = NULL; 6576 6577 sti->sti_pushcnt = 0; 6578 6579 now = gethrestime_sec(); 6580 sti->sti_atime = now; 6581 sti->sti_mtime = now; 6582 sti->sti_ctime = now; 6583 6584 sti->sti_eaddr_mp = NULL; 6585 sti->sti_delayed_error = 0; 6586 6587 sti->sti_provinfo = NULL; 6588 6589 sti->sti_oobcnt = 0; 6590 sti->sti_oobsigcnt = 0; 6591 6592 ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL); 6593 6594 sti->sti_laddr_sa = 0; 6595 sti->sti_faddr_sa = 0; 6596 sti->sti_laddr_maxlen = sti->sti_faddr_maxlen = 0; 6597 sti->sti_laddr_len = sti->sti_faddr_len = 0; 6598 6599 sti->sti_laddr_valid = 0; 6600 sti->sti_faddr_valid = 0; 6601 sti->sti_faddr_noxlate = 0; 6602 6603 sti->sti_direct = 0; 6604 6605 ASSERT(sti->sti_ack_mp == NULL); 6606 ASSERT(sti->sti_ux_bound_vp == NULL); 6607 ASSERT(sti->sti_unbind_mp == NULL); 6608 6609 ASSERT(sti->sti_conn_ind_head == NULL); 6610 ASSERT(sti->sti_conn_ind_tail == NULL); 6611 } 6612 6613 /* 6614 * Given a sonode, grab the TPI info and free any data. 6615 */ 6616 static void 6617 sotpi_info_fini(struct sonode *so) 6618 { 6619 sotpi_info_t *sti = SOTOTPI(so); 6620 mblk_t *mp; 6621 6622 ASSERT(sti->sti_discon_ind_mp == NULL); 6623 6624 if ((mp = sti->sti_conn_ind_head) != NULL) { 6625 mblk_t *mp1; 6626 6627 while (mp) { 6628 mp1 = mp->b_next; 6629 mp->b_next = NULL; 6630 freemsg(mp); 6631 mp = mp1; 6632 } 6633 sti->sti_conn_ind_head = sti->sti_conn_ind_tail = NULL; 6634 } 6635 6636 /* 6637 * Protect so->so_[lf]addr_sa so that sockfs_snapshot() can safely 6638 * indirect them. It also uses so_count as a validity test. 6639 */ 6640 mutex_enter(&so->so_lock); 6641 6642 if (sti->sti_laddr_sa) { 6643 ASSERT((caddr_t)sti->sti_faddr_sa == 6644 (caddr_t)sti->sti_laddr_sa + sti->sti_laddr_maxlen); 6645 ASSERT(sti->sti_faddr_maxlen == sti->sti_laddr_maxlen); 6646 sti->sti_laddr_valid = 0; 6647 sti->sti_faddr_valid = 0; 6648 kmem_free(sti->sti_laddr_sa, sti->sti_laddr_maxlen * 2); 6649 sti->sti_laddr_sa = NULL; 6650 sti->sti_laddr_len = sti->sti_laddr_maxlen = 0; 6651 sti->sti_faddr_sa = NULL; 6652 sti->sti_faddr_len = sti->sti_faddr_maxlen = 0; 6653 } 6654 6655 mutex_exit(&so->so_lock); 6656 6657 if ((mp = sti->sti_eaddr_mp) != NULL) { 6658 freemsg(mp); 6659 sti->sti_eaddr_mp = NULL; 6660 sti->sti_delayed_error = 0; 6661 } 6662 6663 if ((mp = sti->sti_ack_mp) != NULL) { 6664 freemsg(mp); 6665 sti->sti_ack_mp = NULL; 6666 } 6667 6668 ASSERT(sti->sti_ux_bound_vp == NULL); 6669 if ((mp = sti->sti_unbind_mp) != NULL) { 6670 freemsg(mp); 6671 sti->sti_unbind_mp = NULL; 6672 } 6673 } 6674 6675 /* 6676 * Destroys the TPI information attached to a sonode. 6677 */ 6678 static void 6679 sotpi_info_destroy(struct sonode *so) 6680 { 6681 sotpi_info_t *sti = SOTOTPI(so); 6682 6683 i_sotpi_info_destructor(sti); 6684 kmem_free(sti, sizeof (*sti)); 6685 6686 so->so_priv = NULL; 6687 } 6688 6689 /* 6690 * Create the global sotpi socket module entry. It will never be freed. 6691 */ 6692 smod_info_t * 6693 sotpi_smod_create(void) 6694 { 6695 smod_info_t *smodp; 6696 6697 smodp = kmem_zalloc(sizeof (*smodp), KM_SLEEP); 6698 smodp->smod_name = kmem_alloc(sizeof (SOTPI_SMOD_NAME), KM_SLEEP); 6699 (void) strcpy(smodp->smod_name, SOTPI_SMOD_NAME); 6700 /* 6701 * Initialize the smod_refcnt to 1 so it will never be freed. 6702 */ 6703 smodp->smod_refcnt = 1; 6704 smodp->smod_uc_version = SOCK_UC_VERSION; 6705 smodp->smod_dc_version = SOCK_DC_VERSION; 6706 smodp->smod_sock_create_func = &sotpi_create; 6707 smodp->smod_sock_destroy_func = &sotpi_destroy; 6708 return (smodp); 6709 } 6710