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 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 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/sysmacros.h> 38 #include <sys/vfs.h> 39 #include <sys/vnode.h> 40 #include <sys/debug.h> 41 #include <sys/errno.h> 42 #include <sys/time.h> 43 #include <sys/file.h> 44 #include <sys/open.h> 45 #include <sys/user.h> 46 #include <sys/termios.h> 47 #include <sys/stream.h> 48 #include <sys/strsubr.h> 49 #include <sys/strsun.h> 50 #include <sys/ddi.h> 51 #include <sys/esunddi.h> 52 #include <sys/flock.h> 53 #include <sys/modctl.h> 54 #include <sys/vtrace.h> 55 #include <sys/cmn_err.h> 56 #include <sys/pathname.h> 57 58 #include <sys/socket.h> 59 #include <sys/socketvar.h> 60 #include <sys/sockio.h> 61 #include <netinet/in.h> 62 #include <sys/un.h> 63 #include <sys/strsun.h> 64 65 #include <sys/tiuser.h> 66 #define _SUN_TPI_VERSION 2 67 #include <sys/tihdr.h> 68 #include <sys/timod.h> /* TI_GETMYNAME, TI_GETPEERNAME */ 69 70 #include <c2/audit.h> 71 72 #include <inet/common.h> 73 #include <inet/ip.h> 74 #include <inet/ip6.h> 75 #include <inet/tcp.h> 76 #include <inet/udp_impl.h> 77 78 #include <sys/zone.h> 79 80 #include <fs/sockfs/nl7c.h> 81 #include <fs/sockfs/nl7curi.h> 82 83 #include <inet/kssl/ksslapi.h> 84 85 /* 86 * Possible failures when memory can't be allocated. The documented behavior: 87 * 88 * 5.5: 4.X: XNET: 89 * accept: ENOMEM/ENOSR/EINTR - (EINTR) ENOMEM/ENOBUFS/ENOSR/ 90 * EINTR 91 * (4.X does not document EINTR but returns it) 92 * bind: ENOSR - ENOBUFS/ENOSR 93 * connect: EINTR EINTR ENOBUFS/ENOSR/EINTR 94 * getpeername: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR 95 * getsockname: ENOMEM/ENOSR ENOBUFS (-) ENOBUFS/ENOSR 96 * (4.X getpeername and getsockname do not fail in practice) 97 * getsockopt: ENOMEM/ENOSR - ENOBUFS/ENOSR 98 * listen: - - ENOBUFS 99 * recv: ENOMEM/ENOSR/EINTR EINTR ENOBUFS/ENOMEM/ENOSR/ 100 * EINTR 101 * send: ENOMEM/ENOSR/EINTR ENOBUFS/EINTR ENOBUFS/ENOMEM/ENOSR/ 102 * EINTR 103 * setsockopt: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR 104 * shutdown: ENOMEM/ENOSR - ENOBUFS/ENOSR 105 * socket: ENOMEM/ENOSR ENOBUFS ENOBUFS/ENOMEM/ENOSR 106 * socketpair: ENOMEM/ENOSR - ENOBUFS/ENOMEM/ENOSR 107 * 108 * Resolution. When allocation fails: 109 * recv: return EINTR 110 * send: return EINTR 111 * connect, accept: EINTR 112 * bind, listen, shutdown (unbind, unix_close, disconnect): sleep 113 * socket, socketpair: ENOBUFS 114 * getpeername, getsockname: sleep 115 * getsockopt, setsockopt: sleep 116 */ 117 118 #ifdef SOCK_TEST 119 /* 120 * Variables that make sockfs do something other than the standard TPI 121 * for the AF_INET transports. 122 * 123 * solisten_tpi_tcp: 124 * TCP can handle a O_T_BIND_REQ with an increased backlog even though 125 * the transport is already bound. This is needed to avoid loosing the 126 * port number should listen() do a T_UNBIND_REQ followed by a 127 * O_T_BIND_REQ. 128 * 129 * soconnect_tpi_udp: 130 * UDP and ICMP can handle a T_CONN_REQ. 131 * This is needed to make the sequence of connect(), getsockname() 132 * return the local IP address used to send packets to the connected to 133 * destination. 134 * 135 * soconnect_tpi_tcp: 136 * TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ. 137 * Set this to non-zero to send TPI conformant messages to TCP in this 138 * respect. This is a performance optimization. 139 * 140 * soaccept_tpi_tcp: 141 * TCP can handle a T_CONN_REQ without the acceptor being bound. 142 * This is a performance optimization that has been picked up in XTI. 143 * 144 * soaccept_tpi_multioptions: 145 * When inheriting SOL_SOCKET options from the listener to the accepting 146 * socket send them as a single message for AF_INET{,6}. 147 */ 148 int solisten_tpi_tcp = 0; 149 int soconnect_tpi_udp = 0; 150 int soconnect_tpi_tcp = 0; 151 int soaccept_tpi_tcp = 0; 152 int soaccept_tpi_multioptions = 1; 153 #else /* SOCK_TEST */ 154 #define soconnect_tpi_tcp 0 155 #define soconnect_tpi_udp 0 156 #define solisten_tpi_tcp 0 157 #define soaccept_tpi_tcp 0 158 #define soaccept_tpi_multioptions 1 159 #endif /* SOCK_TEST */ 160 161 #ifdef SOCK_TEST 162 extern int do_useracc; 163 extern clock_t sock_test_timelimit; 164 #endif /* SOCK_TEST */ 165 166 /* 167 * Some X/Open added checks might have to be backed out to keep SunOS 4.X 168 * applications working. Turn on this flag to disable these checks. 169 */ 170 int xnet_skip_checks = 0; 171 int xnet_check_print = 0; 172 int xnet_truncate_print = 0; 173 174 extern void sigintr(k_sigset_t *, int); 175 extern void sigunintr(k_sigset_t *); 176 177 extern void *nl7c_lookup_addr(void *, t_uscalar_t); 178 extern void *nl7c_add_addr(void *, t_uscalar_t); 179 extern void nl7c_listener_addr(void *, struct sonode *); 180 181 /* Sockets acting as an in-kernel SSL proxy */ 182 extern mblk_t *strsock_kssl_input(vnode_t *, mblk_t *, strwakeup_t *, 183 strsigset_t *, strsigset_t *, strpollset_t *); 184 extern mblk_t *strsock_kssl_output(vnode_t *, mblk_t *, strwakeup_t *, 185 strsigset_t *, strsigset_t *, strpollset_t *); 186 187 static int sotpi_unbind(struct sonode *, int); 188 189 /* TPI sockfs sonode operations */ 190 static int sotpi_accept(struct sonode *, int, struct sonode **); 191 static int sotpi_bind(struct sonode *, struct sockaddr *, socklen_t, 192 int); 193 static int sotpi_connect(struct sonode *, const struct sockaddr *, 194 socklen_t, int, int); 195 static int sotpi_listen(struct sonode *, int); 196 static int sotpi_sendmsg(struct sonode *, struct nmsghdr *, 197 struct uio *); 198 static int sotpi_shutdown(struct sonode *, int); 199 static int sotpi_getsockname(struct sonode *); 200 static int sosend_dgramcmsg(struct sonode *, struct sockaddr *, socklen_t, 201 struct uio *, void *, t_uscalar_t, int); 202 static int sodgram_direct(struct sonode *, struct sockaddr *, 203 socklen_t, struct uio *, int); 204 205 sonodeops_t sotpi_sonodeops = { 206 sotpi_accept, /* sop_accept */ 207 sotpi_bind, /* sop_bind */ 208 sotpi_listen, /* sop_listen */ 209 sotpi_connect, /* sop_connect */ 210 sotpi_recvmsg, /* sop_recvmsg */ 211 sotpi_sendmsg, /* sop_sendmsg */ 212 sotpi_getpeername, /* sop_getpeername */ 213 sotpi_getsockname, /* sop_getsockname */ 214 sotpi_shutdown, /* sop_shutdown */ 215 sotpi_getsockopt, /* sop_getsockopt */ 216 sotpi_setsockopt /* sop_setsockopt */ 217 }; 218 219 /* 220 * Common create code for socket and accept. If tso is set the values 221 * from that node is used instead of issuing a T_INFO_REQ. 222 * 223 * Assumes that the caller has a VN_HOLD on accessvp. 224 * The VN_RELE will occur either when sotpi_create() fails or when 225 * the returned sonode is freed. 226 */ 227 struct sonode * 228 sotpi_create(vnode_t *accessvp, int domain, int type, int protocol, int version, 229 struct sonode *tso, int *errorp) 230 { 231 struct sonode *so; 232 vnode_t *vp; 233 int flags, error; 234 235 ASSERT(accessvp != NULL); 236 vp = makesockvp(accessvp, domain, type, protocol); 237 ASSERT(vp != NULL); 238 so = VTOSO(vp); 239 240 flags = FREAD|FWRITE; 241 242 if ((type == SOCK_STREAM || type == SOCK_DGRAM) && 243 (domain == AF_INET || domain == AF_INET6) && 244 (protocol == IPPROTO_TCP || protocol == IPPROTO_UDP || 245 protocol == IPPROTO_IP)) { 246 /* Tell tcp or udp that it's talking to sockets */ 247 flags |= SO_SOCKSTR; 248 249 /* 250 * Here we indicate to socktpi_open() our attempt to 251 * make direct calls between sockfs and transport. 252 * The final decision is left to socktpi_open(). 253 */ 254 so->so_state |= SS_DIRECT; 255 256 ASSERT(so->so_type != SOCK_DGRAM || tso == NULL); 257 if (so->so_type == SOCK_STREAM && tso != NULL) { 258 if (tso->so_state & SS_DIRECT) { 259 /* 260 * Inherit SS_DIRECT from listener and pass 261 * SO_ACCEPTOR open flag to tcp, indicating 262 * that this is an accept fast-path instance. 263 */ 264 flags |= SO_ACCEPTOR; 265 } else { 266 /* 267 * SS_DIRECT is not set on listener, meaning 268 * that the listener has been converted from 269 * a socket to a stream. Ensure that the 270 * acceptor inherits these settings. 271 */ 272 so->so_state &= ~SS_DIRECT; 273 flags &= ~SO_SOCKSTR; 274 } 275 } 276 } 277 278 /* 279 * Tell local transport that it is talking to sockets. 280 */ 281 if (so->so_family == AF_UNIX) { 282 flags |= SO_SOCKSTR; 283 } 284 285 /* Initialize the kernel SSL proxy fields */ 286 so->so_kssl_type = KSSL_NO_PROXY; 287 so->so_kssl_ent = NULL; 288 so->so_kssl_ctx = NULL; 289 290 if (error = socktpi_open(&vp, flags, CRED())) { 291 VN_RELE(vp); 292 *errorp = error; 293 return (NULL); 294 } 295 296 if (error = so_strinit(so, tso)) { 297 (void) VOP_CLOSE(vp, 0, 1, 0, CRED()); 298 VN_RELE(vp); 299 *errorp = error; 300 return (NULL); 301 } 302 303 if (version == SOV_DEFAULT) 304 version = so_default_version; 305 306 so->so_version = (short)version; 307 308 return (so); 309 } 310 311 /* 312 * Bind the socket to an unspecified address in sockfs only. 313 * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't 314 * required in all cases. 315 */ 316 static void 317 so_automatic_bind(struct sonode *so) 318 { 319 ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6); 320 321 ASSERT(MUTEX_HELD(&so->so_lock)); 322 ASSERT(!(so->so_state & SS_ISBOUND)); 323 ASSERT(so->so_unbind_mp); 324 325 ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 326 bzero(so->so_laddr_sa, so->so_laddr_len); 327 so->so_laddr_sa->sa_family = so->so_family; 328 so->so_state |= SS_ISBOUND; 329 } 330 331 332 /* 333 * bind the socket. 334 * 335 * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2 336 * are passed in we allow rebinding. Note that for backwards compatibility 337 * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind. 338 * Thus the rebinding code is currently not executed. 339 * 340 * The constraints for rebinding are: 341 * - it is a SOCK_DGRAM, or 342 * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected 343 * and no listen() has been done. 344 * This rebinding code was added based on some language in the XNET book 345 * about not returning EINVAL it the protocol allows rebinding. However, 346 * this language is not present in the Posix socket draft. Thus maybe the 347 * rebinding logic should be deleted from the source. 348 * 349 * A null "name" can be used to unbind the socket if: 350 * - it is a SOCK_DGRAM, or 351 * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected 352 * and no listen() has been done. 353 */ 354 static int 355 sotpi_bindlisten(struct sonode *so, struct sockaddr *name, 356 socklen_t namelen, int backlog, int flags) 357 { 358 struct T_bind_req bind_req; 359 struct T_bind_ack *bind_ack; 360 int error = 0; 361 mblk_t *mp; 362 void *addr; 363 t_uscalar_t addrlen; 364 int unbind_on_err = 1; 365 boolean_t clear_acceptconn_on_err = B_FALSE; 366 boolean_t restore_backlog_on_err = B_FALSE; 367 int save_so_backlog; 368 t_scalar_t PRIM_type = O_T_BIND_REQ; 369 boolean_t tcp_udp_xport; 370 void *nl7c = NULL; 371 372 dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n", 373 so, name, namelen, backlog, flags, 374 pr_state(so->so_state, so->so_mode))); 375 376 tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM; 377 378 if (!(flags & _SOBIND_LOCK_HELD)) { 379 mutex_enter(&so->so_lock); 380 so_lock_single(so); /* Set SOLOCKED */ 381 } else { 382 ASSERT(MUTEX_HELD(&so->so_lock)); 383 ASSERT(so->so_flag & SOLOCKED); 384 } 385 386 /* 387 * Make sure that there is a preallocated unbind_req message 388 * before binding. This message allocated when the socket is 389 * created but it might be have been consumed. 390 */ 391 if (so->so_unbind_mp == NULL) { 392 dprintso(so, 1, ("sobind: allocating unbind_req\n")); 393 /* NOTE: holding so_lock while sleeping */ 394 so->so_unbind_mp = 395 soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP); 396 } 397 398 if (flags & _SOBIND_REBIND) { 399 /* 400 * Called from solisten after doing an sotpi_unbind() or 401 * potentially without the unbind (latter for AF_INET{,6}). 402 */ 403 ASSERT(name == NULL && namelen == 0); 404 405 if (so->so_family == AF_UNIX) { 406 ASSERT(so->so_ux_bound_vp); 407 addr = &so->so_ux_laddr; 408 addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr); 409 dprintso(so, 1, 410 ("sobind rebind UNIX: addrlen %d, addr 0x%p, vp %p\n", 411 addrlen, 412 ((struct so_ux_addr *)addr)->soua_vp, 413 so->so_ux_bound_vp)); 414 } else { 415 addr = so->so_laddr_sa; 416 addrlen = (t_uscalar_t)so->so_laddr_len; 417 } 418 } else if (flags & _SOBIND_UNSPEC) { 419 ASSERT(name == NULL && namelen == 0); 420 421 /* 422 * The caller checked SS_ISBOUND but not necessarily 423 * under so_lock 424 */ 425 if (so->so_state & SS_ISBOUND) { 426 /* No error */ 427 goto done; 428 } 429 430 /* Set an initial local address */ 431 switch (so->so_family) { 432 case AF_UNIX: 433 /* 434 * Use an address with same size as struct sockaddr 435 * just like BSD. 436 */ 437 so->so_laddr_len = 438 (socklen_t)sizeof (struct sockaddr); 439 ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 440 bzero(so->so_laddr_sa, so->so_laddr_len); 441 so->so_laddr_sa->sa_family = so->so_family; 442 443 /* 444 * Pass down an address with the implicit bind 445 * magic number and the rest all zeros. 446 * The transport will return a unique address. 447 */ 448 so->so_ux_laddr.soua_vp = NULL; 449 so->so_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT; 450 addr = &so->so_ux_laddr; 451 addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr); 452 break; 453 454 case AF_INET: 455 case AF_INET6: 456 /* 457 * An unspecified bind in TPI has a NULL address. 458 * Set the address in sockfs to have the sa_family. 459 */ 460 so->so_laddr_len = (so->so_family == AF_INET) ? 461 (socklen_t)sizeof (sin_t) : 462 (socklen_t)sizeof (sin6_t); 463 ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 464 bzero(so->so_laddr_sa, so->so_laddr_len); 465 so->so_laddr_sa->sa_family = so->so_family; 466 addr = NULL; 467 addrlen = 0; 468 break; 469 470 default: 471 /* 472 * An unspecified bind in TPI has a NULL address. 473 * Set the address in sockfs to be zero length. 474 * 475 * Can not assume there is a sa_family for all 476 * protocol families. For example, AF_X25 does not 477 * have a family field. 478 */ 479 bzero(so->so_laddr_sa, so->so_laddr_len); 480 so->so_laddr_len = 0; /* XXX correct? */ 481 addr = NULL; 482 addrlen = 0; 483 break; 484 } 485 486 } else { 487 if (so->so_state & SS_ISBOUND) { 488 /* 489 * If it is ok to rebind the socket, first unbind 490 * with the transport. A rebind to the NULL address 491 * is interpreted as an unbind. 492 * Note that a bind to NULL in BSD does unbind the 493 * socket but it fails with EINVAL. 494 * Note that regular sockets set SOV_SOCKBSD i.e. 495 * _SOBIND_SOCKBSD gets set here hence no type of 496 * socket does currently allow rebinding. 497 * 498 * If the name is NULL just do an unbind. 499 */ 500 if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) && 501 name != NULL) { 502 error = EINVAL; 503 unbind_on_err = 0; 504 eprintsoline(so, error); 505 goto done; 506 } 507 if ((so->so_mode & SM_CONNREQUIRED) && 508 (so->so_state & SS_CANTREBIND)) { 509 error = EINVAL; 510 unbind_on_err = 0; 511 eprintsoline(so, error); 512 goto done; 513 } 514 error = sotpi_unbind(so, 0); 515 if (error) { 516 eprintsoline(so, error); 517 goto done; 518 } 519 ASSERT(!(so->so_state & SS_ISBOUND)); 520 if (name == NULL) { 521 so->so_state &= 522 ~(SS_ISCONNECTED|SS_ISCONNECTING); 523 goto done; 524 } 525 } 526 /* X/Open requires this check */ 527 if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 528 if (xnet_check_print) { 529 printf("sockfs: X/Open bind state check " 530 "caused EINVAL\n"); 531 } 532 error = EINVAL; 533 goto done; 534 } 535 536 switch (so->so_family) { 537 case AF_UNIX: 538 /* 539 * All AF_UNIX addresses are nul terminated 540 * when copied (copyin_name) in so the minimum 541 * length is 3 bytes. 542 */ 543 if (name == NULL || 544 (ssize_t)namelen <= sizeof (short) + 1) { 545 error = EISDIR; 546 eprintsoline(so, error); 547 goto done; 548 } 549 /* 550 * Verify so_family matches the bound family. 551 * BSD does not check this for AF_UNIX resulting 552 * in funny mknods. 553 */ 554 if (name->sa_family != so->so_family) { 555 error = EAFNOSUPPORT; 556 goto done; 557 } 558 break; 559 case AF_INET: 560 if (name == NULL) { 561 error = EINVAL; 562 eprintsoline(so, error); 563 goto done; 564 } 565 if ((size_t)namelen != sizeof (sin_t)) { 566 error = name->sa_family != so->so_family ? 567 EAFNOSUPPORT : EINVAL; 568 eprintsoline(so, error); 569 goto done; 570 } 571 if ((flags & _SOBIND_XPG4_2) && 572 (name->sa_family != so->so_family)) { 573 /* 574 * This check has to be made for X/Open 575 * sockets however application failures have 576 * been observed when it is applied to 577 * all sockets. 578 */ 579 error = EAFNOSUPPORT; 580 eprintsoline(so, error); 581 goto done; 582 } 583 /* 584 * Force a zero sa_family to match so_family. 585 * 586 * Some programs like inetd(1M) don't set the 587 * family field. Other programs leave 588 * sin_family set to garbage - SunOS 4.X does 589 * not check the family field on a bind. 590 * We use the family field that 591 * was passed in to the socket() call. 592 */ 593 name->sa_family = so->so_family; 594 break; 595 596 case AF_INET6: { 597 #ifdef DEBUG 598 sin6_t *sin6 = (sin6_t *)name; 599 #endif /* DEBUG */ 600 601 if (name == NULL) { 602 error = EINVAL; 603 eprintsoline(so, error); 604 goto done; 605 } 606 if ((size_t)namelen != sizeof (sin6_t)) { 607 error = name->sa_family != so->so_family ? 608 EAFNOSUPPORT : EINVAL; 609 eprintsoline(so, error); 610 goto done; 611 } 612 if (name->sa_family != so->so_family) { 613 /* 614 * With IPv6 we require the family to match 615 * unlike in IPv4. 616 */ 617 error = EAFNOSUPPORT; 618 eprintsoline(so, error); 619 goto done; 620 } 621 #ifdef DEBUG 622 /* 623 * Verify that apps don't forget to clear 624 * sin6_scope_id etc 625 */ 626 if (sin6->sin6_scope_id != 0 && 627 !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) { 628 zcmn_err(getzoneid(), CE_WARN, 629 "bind with uninitialized sin6_scope_id " 630 "(%d) on socket. Pid = %d\n", 631 (int)sin6->sin6_scope_id, 632 (int)curproc->p_pid); 633 } 634 if (sin6->__sin6_src_id != 0) { 635 zcmn_err(getzoneid(), CE_WARN, 636 "bind with uninitialized __sin6_src_id " 637 "(%d) on socket. Pid = %d\n", 638 (int)sin6->__sin6_src_id, 639 (int)curproc->p_pid); 640 } 641 #endif /* DEBUG */ 642 break; 643 } 644 default: 645 /* 646 * Don't do any length or sa_family check to allow 647 * non-sockaddr style addresses. 648 */ 649 if (name == NULL) { 650 error = EINVAL; 651 eprintsoline(so, error); 652 goto done; 653 } 654 break; 655 } 656 657 if (namelen > (t_uscalar_t)so->so_laddr_maxlen) { 658 error = ENAMETOOLONG; 659 eprintsoline(so, error); 660 goto done; 661 } 662 /* 663 * Save local address. 664 */ 665 so->so_laddr_len = (socklen_t)namelen; 666 ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 667 bcopy(name, so->so_laddr_sa, namelen); 668 669 addr = so->so_laddr_sa; 670 addrlen = (t_uscalar_t)so->so_laddr_len; 671 switch (so->so_family) { 672 case AF_INET6: 673 case AF_INET: 674 break; 675 case AF_UNIX: { 676 struct sockaddr_un *soun = 677 (struct sockaddr_un *)so->so_laddr_sa; 678 struct vnode *vp; 679 struct vattr vattr; 680 681 ASSERT(so->so_ux_bound_vp == NULL); 682 /* 683 * Create vnode for the specified path name. 684 * Keep vnode held with a reference in so_ux_bound_vp. 685 * Use the vnode pointer as the address used in the 686 * bind with the transport. 687 * 688 * Use the same mode as in BSD. In particular this does 689 * not observe the umask. 690 */ 691 /* MAXPATHLEN + soun_family + nul termination */ 692 if (so->so_laddr_len > 693 (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) { 694 error = ENAMETOOLONG; 695 eprintsoline(so, error); 696 goto done; 697 } 698 vattr.va_type = VSOCK; 699 vattr.va_mode = 0777 & ~PTOU(curproc)->u_cmask; 700 vattr.va_mask = AT_TYPE|AT_MODE; 701 /* NOTE: holding so_lock */ 702 error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr, 703 EXCL, 0, &vp, CRMKNOD, 0, 0); 704 if (error) { 705 if (error == EEXIST) 706 error = EADDRINUSE; 707 eprintsoline(so, error); 708 goto done; 709 } 710 /* 711 * Establish pointer from the underlying filesystem 712 * vnode to the socket node. 713 * so_ux_bound_vp and v_stream->sd_vnode form the 714 * cross-linkage between the underlying filesystem 715 * node and the socket node. 716 */ 717 ASSERT(SOTOV(so)->v_stream); 718 mutex_enter(&vp->v_lock); 719 vp->v_stream = SOTOV(so)->v_stream; 720 so->so_ux_bound_vp = vp; 721 mutex_exit(&vp->v_lock); 722 723 /* 724 * Use the vnode pointer value as a unique address 725 * (together with the magic number to avoid conflicts 726 * with implicit binds) in the transport provider. 727 */ 728 so->so_ux_laddr.soua_vp = (void *)so->so_ux_bound_vp; 729 so->so_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT; 730 addr = &so->so_ux_laddr; 731 addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr); 732 dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n", 733 addrlen, 734 ((struct so_ux_addr *)addr)->soua_vp)); 735 break; 736 } 737 } /* end switch (so->so_family) */ 738 } 739 740 /* 741 * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since 742 * the transport can start passing up T_CONN_IND messages 743 * as soon as it receives the bind req and strsock_proto() 744 * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs. 745 */ 746 if (flags & _SOBIND_LISTEN) { 747 if ((so->so_state & SS_ACCEPTCONN) == 0) 748 clear_acceptconn_on_err = B_TRUE; 749 save_so_backlog = so->so_backlog; 750 restore_backlog_on_err = B_TRUE; 751 so->so_state |= SS_ACCEPTCONN; 752 so->so_backlog = backlog; 753 } 754 755 /* 756 * If NL7C addr(s) have been configured check for addr/port match, 757 * or if an implicit NL7C socket via AF_NCA mark socket as NL7C. 758 * 759 * NL7C supports the TCP transport only so check AF_INET and AF_INET6 760 * family sockets only. If match mark as such. 761 */ 762 if (nl7c_enabled && ((addr != NULL && 763 (so->so_family == AF_INET || so->so_family == AF_INET6) && 764 (nl7c = nl7c_lookup_addr(addr, addrlen))) || 765 so->so_nl7c_flags == NL7C_AF_NCA)) { 766 /* 767 * NL7C is not supported in non-global zones, 768 * we enforce this restriction here. 769 */ 770 if (so->so_zoneid == GLOBAL_ZONEID) { 771 /* An NL7C socket, mark it */ 772 so->so_nl7c_flags |= NL7C_ENABLED; 773 if (nl7c == NULL) { 774 /* 775 * Was an AF_NCA bind() so add it to the 776 * addr list for reporting purposes. 777 */ 778 nl7c = nl7c_add_addr(addr, addrlen); 779 } 780 } else 781 nl7c = NULL; 782 } 783 /* 784 * We send a T_BIND_REQ for TCP/UDP since we know it supports it, 785 * for other transports we will send in a O_T_BIND_REQ. 786 */ 787 if (tcp_udp_xport && 788 (so->so_family == AF_INET || so->so_family == AF_INET6)) 789 PRIM_type = T_BIND_REQ; 790 791 bind_req.PRIM_type = PRIM_type; 792 bind_req.ADDR_length = addrlen; 793 bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req); 794 bind_req.CONIND_number = backlog; 795 /* NOTE: holding so_lock while sleeping */ 796 mp = soallocproto2(&bind_req, sizeof (bind_req), 797 addr, addrlen, 0, _ALLOC_SLEEP); 798 so->so_state &= ~SS_LADDR_VALID; 799 800 /* Done using so_laddr_sa - can drop the lock */ 801 mutex_exit(&so->so_lock); 802 803 /* 804 * Intercept the bind_req message here to check if this <address/port> 805 * was configured as an SSL proxy server, or if another endpoint was 806 * already configured to act as a proxy for us. 807 * 808 * Note, only if NL7C not enabled for this socket. 809 */ 810 if (nl7c == NULL && 811 (so->so_family == AF_INET || so->so_family == AF_INET6) && 812 so->so_type == SOCK_STREAM) { 813 814 if (so->so_kssl_ent != NULL) { 815 kssl_release_ent(so->so_kssl_ent, so, so->so_kssl_type); 816 so->so_kssl_ent = NULL; 817 } 818 819 so->so_kssl_type = kssl_check_proxy(mp, so, &so->so_kssl_ent); 820 switch (so->so_kssl_type) { 821 case KSSL_NO_PROXY: 822 break; 823 824 case KSSL_HAS_PROXY: 825 mutex_enter(&so->so_lock); 826 goto skip_transport; 827 828 case KSSL_IS_PROXY: 829 break; 830 } 831 } 832 833 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 834 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 835 if (error) { 836 eprintsoline(so, error); 837 mutex_enter(&so->so_lock); 838 goto done; 839 } 840 841 mutex_enter(&so->so_lock); 842 error = sowaitprim(so, PRIM_type, T_BIND_ACK, 843 (t_uscalar_t)sizeof (*bind_ack), &mp, 0); 844 if (error) { 845 eprintsoline(so, error); 846 goto done; 847 } 848 skip_transport: 849 ASSERT(mp); 850 /* 851 * Even if some TPI message (e.g. T_DISCON_IND) was received in 852 * strsock_proto while the lock was dropped above, the bind 853 * is allowed to complete. 854 */ 855 856 /* Mark as bound. This will be undone if we detect errors below. */ 857 if (flags & _SOBIND_NOXLATE) { 858 ASSERT(so->so_family == AF_UNIX); 859 so->so_state |= SS_FADDR_NOXLATE; 860 } 861 ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND)); 862 so->so_state |= SS_ISBOUND; 863 ASSERT(so->so_unbind_mp); 864 865 /* note that we've already set SS_ACCEPTCONN above */ 866 867 /* 868 * Recompute addrlen - an unspecied bind sent down an 869 * address of length zero but we expect the appropriate length 870 * in return. 871 */ 872 addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ? 873 sizeof (so->so_ux_laddr) : so->so_laddr_len); 874 875 bind_ack = (struct T_bind_ack *)mp->b_rptr; 876 /* 877 * The alignment restriction is really too strict but 878 * we want enough alignment to inspect the fields of 879 * a sockaddr_in. 880 */ 881 addr = sogetoff(mp, bind_ack->ADDR_offset, 882 bind_ack->ADDR_length, 883 __TPI_ALIGN_SIZE); 884 if (addr == NULL) { 885 freemsg(mp); 886 error = EPROTO; 887 eprintsoline(so, error); 888 goto done; 889 } 890 if (!(flags & _SOBIND_UNSPEC)) { 891 /* 892 * Verify that the transport didn't return something we 893 * did not want e.g. an address other than what we asked for. 894 * 895 * NOTE: These checks would go away if/when we switch to 896 * using the new TPI (in which the transport would fail 897 * the request instead of assigning a different address). 898 * 899 * NOTE2: For protocols that we don't know (i.e. any 900 * other than AF_INET6, AF_INET and AF_UNIX), we 901 * cannot know if the transport should be expected to 902 * return the same address as that requested. 903 * 904 * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send 905 * down a T_BIND_REQ. We use O_T_BIND_REQ for others. 906 * 907 * For example, in the case of netatalk it may be 908 * inappropriate for the transport to return the 909 * requested address (as it may have allocated a local 910 * port number in behaviour similar to that of an 911 * AF_INET bind request with a port number of zero). 912 * 913 * Given the definition of O_T_BIND_REQ, where the 914 * transport may bind to an address other than the 915 * requested address, it's not possible to determine 916 * whether a returned address that differs from the 917 * requested address is a reason to fail (because the 918 * requested address was not available) or succeed 919 * (because the transport allocated an appropriate 920 * address and/or port). 921 * 922 * sockfs currently requires that the transport return 923 * the requested address in the T_BIND_ACK, unless 924 * there is code here to allow for any discrepancy. 925 * Such code exists for AF_INET and AF_INET6. 926 * 927 * Netatalk chooses to return the requested address 928 * rather than the (correct) allocated address. This 929 * means that netatalk violates the TPI specification 930 * (and would not function correctly if used from a 931 * TLI application), but it does mean that it works 932 * with sockfs. 933 * 934 * As noted above, using the newer XTI bind primitive 935 * (T_BIND_REQ) in preference to O_T_BIND_REQ would 936 * allow sockfs to be more sure about whether or not 937 * the bind request had succeeded (as transports are 938 * not permitted to bind to a different address than 939 * that requested - they must return failure). 940 * Unfortunately, support for T_BIND_REQ may not be 941 * present in all transport implementations (netatalk, 942 * for example, doesn't have it), making the 943 * transition difficult. 944 */ 945 if (bind_ack->ADDR_length != addrlen) { 946 /* Assumes that the requested address was in use */ 947 freemsg(mp); 948 error = EADDRINUSE; 949 eprintsoline(so, error); 950 goto done; 951 } 952 953 switch (so->so_family) { 954 case AF_INET6: 955 case AF_INET: { 956 sin_t *rname, *aname; 957 958 rname = (sin_t *)addr; 959 aname = (sin_t *)so->so_laddr_sa; 960 961 /* 962 * Take advantage of the alignment 963 * of sin_port and sin6_port which fall 964 * in the same place in their data structures. 965 * Just use sin_port for either address family. 966 * 967 * This may become a problem if (heaven forbid) 968 * there's a separate ipv6port_reserved... :-P 969 * 970 * Binding to port 0 has the semantics of letting 971 * the transport bind to any port. 972 * 973 * If the transport is TCP or UDP since we had sent 974 * a T_BIND_REQ we would not get a port other than 975 * what we asked for. 976 */ 977 if (tcp_udp_xport) { 978 /* 979 * Pick up the new port number if we bound to 980 * port 0. 981 */ 982 if (aname->sin_port == 0) 983 aname->sin_port = rname->sin_port; 984 so->so_state |= SS_LADDR_VALID; 985 break; 986 } 987 if (aname->sin_port != 0 && 988 aname->sin_port != rname->sin_port) { 989 freemsg(mp); 990 error = EADDRINUSE; 991 eprintsoline(so, error); 992 goto done; 993 } 994 /* 995 * Pick up the new port number if we bound to port 0. 996 */ 997 aname->sin_port = rname->sin_port; 998 999 /* 1000 * Unfortunately, addresses aren't _quite_ the same. 1001 */ 1002 if (so->so_family == AF_INET) { 1003 if (aname->sin_addr.s_addr != 1004 rname->sin_addr.s_addr) { 1005 freemsg(mp); 1006 error = EADDRNOTAVAIL; 1007 eprintsoline(so, error); 1008 goto done; 1009 } 1010 } else { 1011 sin6_t *rname6 = (sin6_t *)rname; 1012 sin6_t *aname6 = (sin6_t *)aname; 1013 1014 if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr, 1015 &rname6->sin6_addr)) { 1016 freemsg(mp); 1017 error = EADDRNOTAVAIL; 1018 eprintsoline(so, error); 1019 goto done; 1020 } 1021 } 1022 break; 1023 } 1024 case AF_UNIX: 1025 if (bcmp(addr, &so->so_ux_laddr, addrlen) != 0) { 1026 freemsg(mp); 1027 error = EADDRINUSE; 1028 eprintsoline(so, error); 1029 eprintso(so, 1030 ("addrlen %d, addr 0x%x, vp %p\n", 1031 addrlen, *((int *)addr), 1032 so->so_ux_bound_vp)); 1033 goto done; 1034 } 1035 so->so_state |= SS_LADDR_VALID; 1036 break; 1037 default: 1038 /* 1039 * NOTE: This assumes that addresses can be 1040 * byte-compared for equivalence. 1041 */ 1042 if (bcmp(addr, so->so_laddr_sa, addrlen) != 0) { 1043 freemsg(mp); 1044 error = EADDRINUSE; 1045 eprintsoline(so, error); 1046 goto done; 1047 } 1048 /* 1049 * Don't mark SS_LADDR_VALID, as we cannot be 1050 * sure that the returned address is the real 1051 * bound address when talking to an unknown 1052 * transport. 1053 */ 1054 break; 1055 } 1056 } else { 1057 /* 1058 * Save for returned address for getsockname. 1059 * Needed for unspecific bind unless transport supports 1060 * the TI_GETMYNAME ioctl. 1061 * Do this for AF_INET{,6} even though they do, as 1062 * caching info here is much better performance than 1063 * a TPI/STREAMS trip to the transport for getsockname. 1064 * Any which can't for some reason _must_ _not_ set 1065 * LADDR_VALID here for the caching version of getsockname 1066 * to not break; 1067 */ 1068 switch (so->so_family) { 1069 case AF_UNIX: 1070 /* 1071 * Record the address bound with the transport 1072 * for use by socketpair. 1073 */ 1074 bcopy(addr, &so->so_ux_laddr, addrlen); 1075 so->so_state |= SS_LADDR_VALID; 1076 break; 1077 case AF_INET: 1078 case AF_INET6: 1079 ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 1080 bcopy(addr, so->so_laddr_sa, so->so_laddr_len); 1081 so->so_state |= SS_LADDR_VALID; 1082 break; 1083 default: 1084 /* 1085 * Don't mark SS_LADDR_VALID, as we cannot be 1086 * sure that the returned address is the real 1087 * bound address when talking to an unknown 1088 * transport. 1089 */ 1090 break; 1091 } 1092 } 1093 1094 if (nl7c != NULL) { 1095 /* Register listen()er sonode pointer with NL7C */ 1096 nl7c_listener_addr(nl7c, so); 1097 } 1098 1099 freemsg(mp); 1100 1101 done: 1102 if (error) { 1103 /* reset state & backlog to values held on entry */ 1104 if (clear_acceptconn_on_err == B_TRUE) 1105 so->so_state &= ~SS_ACCEPTCONN; 1106 if (restore_backlog_on_err == B_TRUE) 1107 so->so_backlog = save_so_backlog; 1108 1109 if (unbind_on_err && so->so_state & SS_ISBOUND) { 1110 int err; 1111 1112 err = sotpi_unbind(so, 0); 1113 /* LINTED - statement has no consequent: if */ 1114 if (err) { 1115 eprintsoline(so, error); 1116 } else { 1117 ASSERT(!(so->so_state & SS_ISBOUND)); 1118 } 1119 } 1120 } 1121 if (!(flags & _SOBIND_LOCK_HELD)) { 1122 so_unlock_single(so, SOLOCKED); 1123 mutex_exit(&so->so_lock); 1124 } else { 1125 /* If the caller held the lock don't release it here */ 1126 ASSERT(MUTEX_HELD(&so->so_lock)); 1127 ASSERT(so->so_flag & SOLOCKED); 1128 } 1129 return (error); 1130 } 1131 1132 /* bind the socket */ 1133 static int 1134 sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen, 1135 int flags) 1136 { 1137 if ((flags & _SOBIND_SOCKETPAIR) == 0) 1138 return (sotpi_bindlisten(so, name, namelen, 0, flags)); 1139 1140 flags &= ~_SOBIND_SOCKETPAIR; 1141 return (sotpi_bindlisten(so, name, namelen, 1, flags)); 1142 } 1143 1144 /* 1145 * Unbind a socket - used when bind() fails, when bind() specifies a NULL 1146 * address, or when listen needs to unbind and bind. 1147 * If the _SOUNBIND_REBIND flag is specified the addresses are retained 1148 * so that a sobind can pick them up. 1149 */ 1150 static int 1151 sotpi_unbind(struct sonode *so, int flags) 1152 { 1153 struct T_unbind_req unbind_req; 1154 int error = 0; 1155 mblk_t *mp; 1156 1157 dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n", 1158 so, flags, pr_state(so->so_state, so->so_mode))); 1159 1160 ASSERT(MUTEX_HELD(&so->so_lock)); 1161 ASSERT(so->so_flag & SOLOCKED); 1162 1163 if (!(so->so_state & SS_ISBOUND)) { 1164 error = EINVAL; 1165 eprintsoline(so, error); 1166 goto done; 1167 } 1168 1169 mutex_exit(&so->so_lock); 1170 1171 /* 1172 * Flush the read and write side (except stream head read queue) 1173 * and send down T_UNBIND_REQ. 1174 */ 1175 (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW); 1176 1177 unbind_req.PRIM_type = T_UNBIND_REQ; 1178 mp = soallocproto1(&unbind_req, sizeof (unbind_req), 1179 0, _ALLOC_SLEEP); 1180 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 1181 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 1182 mutex_enter(&so->so_lock); 1183 if (error) { 1184 eprintsoline(so, error); 1185 goto done; 1186 } 1187 1188 error = sowaitokack(so, T_UNBIND_REQ); 1189 if (error) { 1190 eprintsoline(so, error); 1191 goto done; 1192 } 1193 1194 /* 1195 * Even if some TPI message (e.g. T_DISCON_IND) was received in 1196 * strsock_proto while the lock was dropped above, the unbind 1197 * is allowed to complete. 1198 */ 1199 if (!(flags & _SOUNBIND_REBIND)) { 1200 /* 1201 * Clear out bound address. 1202 */ 1203 vnode_t *vp; 1204 1205 if ((vp = so->so_ux_bound_vp) != NULL) { 1206 1207 /* Undo any SSL proxy setup */ 1208 if ((so->so_family == AF_INET || 1209 so->so_family == AF_INET6) && 1210 (so->so_type == SOCK_STREAM) && 1211 (so->so_kssl_ent != NULL)) { 1212 kssl_release_ent(so->so_kssl_ent, so, 1213 so->so_kssl_type); 1214 so->so_kssl_ent = NULL; 1215 so->so_kssl_type = KSSL_NO_PROXY; 1216 } 1217 1218 so->so_ux_bound_vp = NULL; 1219 vn_rele_stream(vp); 1220 } 1221 /* Clear out address */ 1222 so->so_laddr_len = 0; 1223 } 1224 so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN|SS_LADDR_VALID); 1225 1226 done: 1227 1228 /* If the caller held the lock don't release it here */ 1229 ASSERT(MUTEX_HELD(&so->so_lock)); 1230 ASSERT(so->so_flag & SOLOCKED); 1231 1232 return (error); 1233 } 1234 1235 /* 1236 * listen on the socket. 1237 * For TPI conforming transports this has to first unbind with the transport 1238 * and then bind again using the new backlog. 1239 */ 1240 int 1241 sotpi_listen(struct sonode *so, int backlog) 1242 { 1243 int error = 0; 1244 1245 dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n", 1246 so, backlog, pr_state(so->so_state, so->so_mode))); 1247 1248 if (so->so_serv_type == T_CLTS) 1249 return (EOPNOTSUPP); 1250 1251 /* 1252 * If the socket is ready to accept connections already, then 1253 * return without doing anything. This avoids a problem where 1254 * a second listen() call fails if a connection is pending and 1255 * leaves the socket unbound. Only when we are not unbinding 1256 * with the transport can we safely increase the backlog. 1257 */ 1258 if (so->so_state & SS_ACCEPTCONN && 1259 !((so->so_family == AF_INET || so->so_family == AF_INET6) && 1260 /*CONSTCOND*/ 1261 !solisten_tpi_tcp)) 1262 return (0); 1263 1264 if (so->so_state & SS_ISCONNECTED) 1265 return (EINVAL); 1266 1267 mutex_enter(&so->so_lock); 1268 so_lock_single(so); /* Set SOLOCKED */ 1269 1270 if (backlog < 0) 1271 backlog = 0; 1272 /* 1273 * Use the same qlimit as in BSD. BSD checks the qlimit 1274 * before queuing the next connection implying that a 1275 * listen(sock, 0) allows one connection to be queued. 1276 * BSD also uses 1.5 times the requested backlog. 1277 * 1278 * XNS Issue 4 required a strict interpretation of the backlog. 1279 * This has been waived subsequently for Issue 4 and the change 1280 * incorporated in XNS Issue 5. So we aren't required to do 1281 * anything special for XPG apps. 1282 */ 1283 if (backlog >= (INT_MAX - 1) / 3) 1284 backlog = INT_MAX; 1285 else 1286 backlog = backlog * 3 / 2 + 1; 1287 1288 /* 1289 * If the listen doesn't change the backlog we do nothing. 1290 * This avoids an EPROTO error from the transport. 1291 */ 1292 if ((so->so_state & SS_ACCEPTCONN) && 1293 so->so_backlog == backlog) 1294 goto done; 1295 1296 if (!(so->so_state & SS_ISBOUND)) { 1297 /* 1298 * Must have been explicitly bound in the UNIX domain. 1299 */ 1300 if (so->so_family == AF_UNIX) { 1301 error = EINVAL; 1302 goto done; 1303 } 1304 error = sotpi_bindlisten(so, NULL, 0, backlog, 1305 _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN); 1306 } else if (backlog > 0) { 1307 /* 1308 * AF_INET{,6} hack to avoid losing the port. 1309 * Assumes that all AF_INET{,6} transports can handle a 1310 * O_T_BIND_REQ with a non-zero CONIND_number when the TPI 1311 * has already bound thus it is possible to avoid the unbind. 1312 */ 1313 if (!((so->so_family == AF_INET || so->so_family == AF_INET6) && 1314 /*CONSTCOND*/ 1315 !solisten_tpi_tcp)) { 1316 error = sotpi_unbind(so, _SOUNBIND_REBIND); 1317 if (error) 1318 goto done; 1319 } 1320 error = sotpi_bindlisten(so, NULL, 0, backlog, 1321 _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN); 1322 } else { 1323 so->so_state |= SS_ACCEPTCONN; 1324 so->so_backlog = backlog; 1325 } 1326 if (error) 1327 goto done; 1328 ASSERT(so->so_state & SS_ACCEPTCONN); 1329 done: 1330 so_unlock_single(so, SOLOCKED); 1331 mutex_exit(&so->so_lock); 1332 return (error); 1333 } 1334 1335 /* 1336 * Disconnect either a specified seqno or all (-1). 1337 * The former is used on listening sockets only. 1338 * 1339 * When seqno == -1 sodisconnect could call sotpi_unbind. However, 1340 * the current use of sodisconnect(seqno == -1) is only for shutdown 1341 * so there is no point (and potentially incorrect) to unbind. 1342 */ 1343 int 1344 sodisconnect(struct sonode *so, t_scalar_t seqno, int flags) 1345 { 1346 struct T_discon_req discon_req; 1347 int error = 0; 1348 mblk_t *mp; 1349 1350 dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n", 1351 so, seqno, flags, pr_state(so->so_state, so->so_mode))); 1352 1353 if (!(flags & _SODISCONNECT_LOCK_HELD)) { 1354 mutex_enter(&so->so_lock); 1355 so_lock_single(so); /* Set SOLOCKED */ 1356 } else { 1357 ASSERT(MUTEX_HELD(&so->so_lock)); 1358 ASSERT(so->so_flag & SOLOCKED); 1359 } 1360 1361 if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) { 1362 error = EINVAL; 1363 eprintsoline(so, error); 1364 goto done; 1365 } 1366 1367 mutex_exit(&so->so_lock); 1368 /* 1369 * Flush the write side (unless this is a listener) 1370 * and then send down a T_DISCON_REQ. 1371 * (Don't flush on listener since it could flush {O_}T_CONN_RES 1372 * and other messages.) 1373 */ 1374 if (!(so->so_state & SS_ACCEPTCONN)) 1375 (void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW); 1376 1377 discon_req.PRIM_type = T_DISCON_REQ; 1378 discon_req.SEQ_number = seqno; 1379 mp = soallocproto1(&discon_req, sizeof (discon_req), 1380 0, _ALLOC_SLEEP); 1381 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 1382 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 1383 mutex_enter(&so->so_lock); 1384 if (error) { 1385 eprintsoline(so, error); 1386 goto done; 1387 } 1388 1389 error = sowaitokack(so, T_DISCON_REQ); 1390 if (error) { 1391 eprintsoline(so, error); 1392 goto done; 1393 } 1394 /* 1395 * Even if some TPI message (e.g. T_DISCON_IND) was received in 1396 * strsock_proto while the lock was dropped above, the disconnect 1397 * is allowed to complete. However, it is not possible to 1398 * assert that SS_ISCONNECTED|SS_ISCONNECTING are set. 1399 */ 1400 so->so_state &= 1401 ~(SS_ISCONNECTED|SS_ISCONNECTING|SS_LADDR_VALID|SS_FADDR_VALID); 1402 done: 1403 if (!(flags & _SODISCONNECT_LOCK_HELD)) { 1404 so_unlock_single(so, SOLOCKED); 1405 mutex_exit(&so->so_lock); 1406 } else { 1407 /* If the caller held the lock don't release it here */ 1408 ASSERT(MUTEX_HELD(&so->so_lock)); 1409 ASSERT(so->so_flag & SOLOCKED); 1410 } 1411 return (error); 1412 } 1413 1414 int 1415 sotpi_accept(struct sonode *so, int fflag, struct sonode **nsop) 1416 { 1417 struct T_conn_ind *conn_ind; 1418 struct T_conn_res *conn_res; 1419 int error = 0; 1420 mblk_t *mp, *ctxmp, *ack_mp; 1421 struct sonode *nso; 1422 vnode_t *nvp; 1423 void *src; 1424 t_uscalar_t srclen; 1425 void *opt; 1426 t_uscalar_t optlen; 1427 t_scalar_t PRIM_type; 1428 t_scalar_t SEQ_number; 1429 size_t sinlen; 1430 1431 dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n", 1432 so, fflag, nsop, pr_state(so->so_state, so->so_mode))); 1433 1434 /* 1435 * Defer single-threading the accepting socket until 1436 * the T_CONN_IND has been received and parsed and the 1437 * new sonode has been opened. 1438 */ 1439 1440 /* Check that we are not already connected */ 1441 if ((so->so_state & SS_ACCEPTCONN) == 0) 1442 goto conn_bad; 1443 again: 1444 if ((error = sowaitconnind(so, fflag, &mp)) != 0) 1445 goto e_bad; 1446 1447 ASSERT(mp); 1448 conn_ind = (struct T_conn_ind *)mp->b_rptr; 1449 ctxmp = mp->b_cont; 1450 1451 /* 1452 * Save SEQ_number for error paths. 1453 */ 1454 SEQ_number = conn_ind->SEQ_number; 1455 1456 srclen = conn_ind->SRC_length; 1457 src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1); 1458 if (src == NULL) { 1459 error = EPROTO; 1460 freemsg(mp); 1461 eprintsoline(so, error); 1462 goto disconnect_unlocked; 1463 } 1464 optlen = conn_ind->OPT_length; 1465 switch (so->so_family) { 1466 case AF_INET: 1467 case AF_INET6: 1468 if ((optlen == sizeof (intptr_t)) && 1469 ((so->so_state & SS_DIRECT) != 0)) { 1470 bcopy(mp->b_rptr + conn_ind->OPT_offset, 1471 &opt, conn_ind->OPT_length); 1472 } else { 1473 /* 1474 * The transport (in this case TCP) hasn't sent up 1475 * a pointer to an instance for the accept fast-path. 1476 * Disable fast-path completely because the call to 1477 * sotpi_create() below would otherwise create an 1478 * incomplete TCP instance, which would lead to 1479 * problems when sockfs sends a normal T_CONN_RES 1480 * message down the new stream. 1481 */ 1482 if (so->so_state & SS_DIRECT) { 1483 int rval; 1484 /* 1485 * For consistency we inform tcp to disable 1486 * direct interface on the listener, though 1487 * we can certainly live without doing this 1488 * because no data will ever travel upstream 1489 * on the listening socket. 1490 */ 1491 so->so_state &= ~SS_DIRECT; 1492 (void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK, 1493 0, 0, K_TO_K, CRED(), &rval); 1494 } 1495 opt = NULL; 1496 optlen = 0; 1497 } 1498 break; 1499 case AF_UNIX: 1500 default: 1501 if (optlen != 0) { 1502 opt = sogetoff(mp, conn_ind->OPT_offset, optlen, 1503 __TPI_ALIGN_SIZE); 1504 if (opt == NULL) { 1505 error = EPROTO; 1506 freemsg(mp); 1507 eprintsoline(so, error); 1508 goto disconnect_unlocked; 1509 } 1510 } 1511 if (so->so_family == AF_UNIX) { 1512 if (!(so->so_state & SS_FADDR_NOXLATE)) { 1513 src = NULL; 1514 srclen = 0; 1515 } 1516 /* Extract src address from options */ 1517 if (optlen != 0) 1518 so_getopt_srcaddr(opt, optlen, &src, &srclen); 1519 } 1520 break; 1521 } 1522 1523 /* 1524 * Create the new socket. 1525 */ 1526 VN_HOLD(so->so_accessvp); 1527 nso = sotpi_create(so->so_accessvp, so->so_family, so->so_type, 1528 so->so_protocol, so->so_version, so, &error); 1529 if (nso == NULL) { 1530 ASSERT(error != 0); 1531 /* 1532 * Accept can not fail with ENOBUFS. sotpi_create 1533 * sleeps waiting for memory until a signal is caught 1534 * so return EINTR. 1535 */ 1536 freemsg(mp); 1537 if (error == ENOBUFS) 1538 error = EINTR; 1539 goto e_disc_unl; 1540 } 1541 nvp = SOTOV(nso); 1542 1543 /* 1544 * If the transport sent up an SSL connection context, then attach 1545 * it the new socket, and set the (sd_wputdatafunc)() and 1546 * (sd_rputdatafunc)() stream head hooks to intercept and process 1547 * SSL records. 1548 */ 1549 if (ctxmp != NULL) { 1550 /* 1551 * This kssl_ctx_t is already held for us by the transport. 1552 * So, we don't need to do a kssl_hold_ctx() here. 1553 */ 1554 nso->so_kssl_ctx = *((kssl_ctx_t *)ctxmp->b_rptr); 1555 freemsg(ctxmp); 1556 mp->b_cont = NULL; 1557 strsetrwputdatahooks(nvp, strsock_kssl_input, 1558 strsock_kssl_output); 1559 } 1560 #ifdef DEBUG 1561 /* 1562 * SO_DEBUG is used to trigger the dprint* and eprint* macros thus 1563 * it's inherited early to allow debugging of the accept code itself. 1564 */ 1565 nso->so_options |= so->so_options & SO_DEBUG; 1566 #endif /* DEBUG */ 1567 1568 /* 1569 * Save the SRC address from the T_CONN_IND 1570 * for getpeername to work on AF_UNIX and on transports that do not 1571 * support TI_GETPEERNAME. 1572 * 1573 * NOTE: AF_UNIX NUL termination is ensured by the sender's 1574 * copyin_name(). 1575 */ 1576 if (srclen > (t_uscalar_t)nso->so_faddr_maxlen) { 1577 error = EINVAL; 1578 freemsg(mp); 1579 eprintsoline(so, error); 1580 goto disconnect_vp_unlocked; 1581 } 1582 nso->so_faddr_len = (socklen_t)srclen; 1583 ASSERT(so->so_faddr_len <= so->so_faddr_maxlen); 1584 bcopy(src, nso->so_faddr_sa, srclen); 1585 nso->so_state |= SS_FADDR_VALID; 1586 1587 if ((DB_REF(mp) > 1) || MBLKSIZE(mp) < 1588 (sizeof (struct T_conn_res) + sizeof (intptr_t))) { 1589 cred_t *cr; 1590 1591 if ((cr = DB_CRED(mp)) != NULL) { 1592 crhold(cr); 1593 nso->so_peercred = cr; 1594 nso->so_cpid = DB_CPID(mp); 1595 } 1596 freemsg(mp); 1597 1598 mp = soallocproto1(NULL, sizeof (struct T_conn_res) + 1599 sizeof (intptr_t), 0, _ALLOC_INTR); 1600 if (mp == NULL) { 1601 /* 1602 * Accept can not fail with ENOBUFS. 1603 * A signal was caught so return EINTR. 1604 */ 1605 error = EINTR; 1606 eprintsoline(so, error); 1607 goto disconnect_vp_unlocked; 1608 } 1609 conn_res = (struct T_conn_res *)mp->b_rptr; 1610 } else { 1611 nso->so_peercred = DB_CRED(mp); 1612 nso->so_cpid = DB_CPID(mp); 1613 DB_CRED(mp) = NULL; 1614 1615 mp->b_rptr = DB_BASE(mp); 1616 conn_res = (struct T_conn_res *)mp->b_rptr; 1617 mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res); 1618 } 1619 1620 /* 1621 * New socket must be bound at least in sockfs and, except for AF_INET, 1622 * (or AF_INET6) it also has to be bound in the transport provider. 1623 * We set the local address in the sonode from the T_OK_ACK of the 1624 * T_CONN_RES. For this reason the address we bind to here isn't 1625 * important. 1626 */ 1627 if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) && 1628 /*CONSTCOND*/ 1629 nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) { 1630 /* 1631 * Optimization for AF_INET{,6} transports 1632 * that can handle a T_CONN_RES without being bound. 1633 */ 1634 mutex_enter(&nso->so_lock); 1635 so_automatic_bind(nso); 1636 mutex_exit(&nso->so_lock); 1637 } else { 1638 /* Perform NULL bind with the transport provider. */ 1639 if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC)) != 0) { 1640 ASSERT(error != ENOBUFS); 1641 freemsg(mp); 1642 eprintsoline(nso, error); 1643 goto disconnect_vp_unlocked; 1644 } 1645 } 1646 1647 /* 1648 * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES 1649 * so that any data arriving on the new socket will cause the 1650 * appropriate signals to be delivered for the new socket. 1651 * 1652 * No other thread (except strsock_proto and strsock_misc) 1653 * can access the new socket thus we relax the locking. 1654 */ 1655 nso->so_pgrp = so->so_pgrp; 1656 nso->so_state |= so->so_state & (SS_ASYNC|SS_FADDR_NOXLATE); 1657 1658 if (nso->so_pgrp != 0) { 1659 if ((error = so_set_events(nso, nvp, CRED())) != 0) { 1660 eprintsoline(nso, error); 1661 error = 0; 1662 nso->so_pgrp = 0; 1663 } 1664 } 1665 1666 /* 1667 * Make note of the socket level options. TCP and IP level options 1668 * are already inherited. We could do all this after accept is 1669 * successful but doing it here simplifies code and no harm done 1670 * for error case. 1671 */ 1672 nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE| 1673 SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK| 1674 SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER); 1675 nso->so_sndbuf = so->so_sndbuf; 1676 nso->so_rcvbuf = so->so_rcvbuf; 1677 if (nso->so_options & SO_LINGER) 1678 nso->so_linger = so->so_linger; 1679 1680 if ((so->so_state & SS_DIRECT) != 0) { 1681 1682 ASSERT(opt != NULL); 1683 1684 conn_res->OPT_length = optlen; 1685 conn_res->OPT_offset = MBLKL(mp); 1686 bcopy(&opt, mp->b_wptr, optlen); 1687 mp->b_wptr += optlen; 1688 conn_res->PRIM_type = T_CONN_RES; 1689 conn_res->ACCEPTOR_id = 0; 1690 PRIM_type = T_CONN_RES; 1691 1692 /* Send down the T_CONN_RES on acceptor STREAM */ 1693 error = kstrputmsg(SOTOV(nso), mp, NULL, 1694 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 1695 if (error) { 1696 mutex_enter(&so->so_lock); 1697 so_lock_single(so); 1698 eprintsoline(so, error); 1699 goto disconnect_vp; 1700 } 1701 mutex_enter(&nso->so_lock); 1702 error = sowaitprim(nso, T_CONN_RES, T_OK_ACK, 1703 (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0); 1704 if (error) { 1705 mutex_exit(&nso->so_lock); 1706 mutex_enter(&so->so_lock); 1707 so_lock_single(so); 1708 eprintsoline(so, error); 1709 goto disconnect_vp; 1710 } 1711 if (nso->so_family == AF_INET) { 1712 sin_t *sin; 1713 1714 sin = (sin_t *)(ack_mp->b_rptr + 1715 sizeof (struct T_ok_ack)); 1716 bcopy(sin, nso->so_laddr_sa, sizeof (sin_t)); 1717 nso->so_laddr_len = sizeof (sin_t); 1718 } else { 1719 sin6_t *sin6; 1720 1721 sin6 = (sin6_t *)(ack_mp->b_rptr + 1722 sizeof (struct T_ok_ack)); 1723 bcopy(sin6, nso->so_laddr_sa, sizeof (sin6_t)); 1724 nso->so_laddr_len = sizeof (sin6_t); 1725 } 1726 freemsg(ack_mp); 1727 1728 nso->so_state |= SS_ISCONNECTED | SS_LADDR_VALID; 1729 nso->so_priv = opt; 1730 1731 if (so->so_nl7c_flags & NL7C_ENABLED) { 1732 /* 1733 * A NL7C marked listen()er so the new socket 1734 * inherits the listen()er's NL7C state, except 1735 * for NL7C_POLLIN. 1736 * 1737 * Only call NL7C to process the new socket if 1738 * the listen socket allows blocking i/o. 1739 */ 1740 nso->so_nl7c_flags = so->so_nl7c_flags & (~NL7C_POLLIN); 1741 if (so->so_state & (SS_NONBLOCK|SS_NDELAY)) { 1742 /* 1743 * Nonblocking accept() just make it 1744 * persist to defer processing to the 1745 * read-side syscall (e.g. read). 1746 */ 1747 nso->so_nl7c_flags |= NL7C_SOPERSIST; 1748 } else if (nl7c_process(nso, B_FALSE)) { 1749 /* 1750 * NL7C has completed processing on the 1751 * socket, close the socket and back to 1752 * the top to await the next T_CONN_IND. 1753 */ 1754 mutex_exit(&nso->so_lock); 1755 (void) VOP_CLOSE(nvp, 0, 1, (offset_t)0, 1756 CRED()); 1757 VN_RELE(nvp); 1758 goto again; 1759 } 1760 /* Pass the new socket out */ 1761 } 1762 1763 mutex_exit(&nso->so_lock); 1764 1765 /* 1766 * It's possible, through the use of autopush for example, 1767 * that the acceptor stream may not support SS_DIRECT 1768 * semantics. If the new socket does not support SS_DIRECT 1769 * we issue a _SIOCSOCKFALLBACK to inform the transport 1770 * as we would in the I_PUSH case. 1771 */ 1772 if (!(nso->so_state & SS_DIRECT)) { 1773 int rval; 1774 1775 if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK, 1776 0, 0, K_TO_K, CRED(), &rval)) != 0) { 1777 mutex_enter(&so->so_lock); 1778 so_lock_single(so); 1779 eprintsoline(so, error); 1780 goto disconnect_vp; 1781 } 1782 } 1783 1784 /* 1785 * Pass out new socket. 1786 */ 1787 if (nsop != NULL) 1788 *nsop = nso; 1789 1790 return (0); 1791 } 1792 1793 /* 1794 * This is the non-performance case for sockets (e.g. AF_UNIX sockets) 1795 * which don't support the FireEngine accept fast-path. It is also 1796 * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd 1797 * again. Neither sockfs nor TCP attempt to find out if some other 1798 * random module has been inserted in between (in which case we 1799 * should follow TLI accept behaviour). We blindly assume the worst 1800 * case and revert back to old behaviour i.e. TCP will not send us 1801 * any option (eager) and the accept should happen on the listener 1802 * queue. Any queued T_conn_ind have already got their options removed 1803 * by so_sock2_stream() when "sockmod" was I_POP'd. 1804 */ 1805 /* 1806 * Fill in the {O_}T_CONN_RES before getting SOLOCKED. 1807 */ 1808 if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) { 1809 #ifdef _ILP32 1810 queue_t *q; 1811 1812 /* 1813 * Find read queue in driver 1814 * Can safely do this since we "own" nso/nvp. 1815 */ 1816 q = strvp2wq(nvp)->q_next; 1817 while (SAMESTR(q)) 1818 q = q->q_next; 1819 q = RD(q); 1820 conn_res->ACCEPTOR_id = (t_uscalar_t)q; 1821 #else 1822 conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev); 1823 #endif /* _ILP32 */ 1824 conn_res->PRIM_type = O_T_CONN_RES; 1825 PRIM_type = O_T_CONN_RES; 1826 } else { 1827 conn_res->ACCEPTOR_id = nso->so_acceptor_id; 1828 conn_res->PRIM_type = T_CONN_RES; 1829 PRIM_type = T_CONN_RES; 1830 } 1831 conn_res->SEQ_number = SEQ_number; 1832 conn_res->OPT_length = 0; 1833 conn_res->OPT_offset = 0; 1834 1835 mutex_enter(&so->so_lock); 1836 so_lock_single(so); /* Set SOLOCKED */ 1837 mutex_exit(&so->so_lock); 1838 1839 error = kstrputmsg(SOTOV(so), mp, NULL, 1840 0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 1841 mutex_enter(&so->so_lock); 1842 if (error) { 1843 eprintsoline(so, error); 1844 goto disconnect_vp; 1845 } 1846 error = sowaitprim(so, PRIM_type, T_OK_ACK, 1847 (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0); 1848 if (error) { 1849 eprintsoline(so, error); 1850 goto disconnect_vp; 1851 } 1852 /* 1853 * If there is a sin/sin6 appended onto the T_OK_ACK use 1854 * that to set the local address. If this is not present 1855 * then we zero out the address and don't set the 1856 * SS_LADDR_VALID bit. 1857 */ 1858 sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t); 1859 if ((nso->so_family == AF_INET) || (nso->so_family == AF_INET6) && 1860 MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) { 1861 ack_mp->b_rptr += sizeof (struct T_ok_ack); 1862 bcopy(ack_mp->b_rptr, nso->so_laddr_sa, sinlen); 1863 nso->so_laddr_len = sinlen; 1864 nso->so_state |= SS_LADDR_VALID; 1865 } else { 1866 nso->so_laddr_len = so->so_laddr_len; 1867 ASSERT(nso->so_laddr_len <= nso->so_laddr_maxlen); 1868 bzero(nso->so_laddr_sa, nso->so_addr_size); 1869 } 1870 freemsg(ack_mp); 1871 1872 so_unlock_single(so, SOLOCKED); 1873 mutex_exit(&so->so_lock); 1874 1875 nso->so_state |= SS_ISCONNECTED; 1876 1877 /* 1878 * Pass out new socket. 1879 */ 1880 if (nsop != NULL) 1881 *nsop = nso; 1882 1883 return (0); 1884 1885 1886 eproto_disc_unl: 1887 error = EPROTO; 1888 e_disc_unl: 1889 eprintsoline(so, error); 1890 goto disconnect_unlocked; 1891 1892 pr_disc_vp_unl: 1893 eprintsoline(so, error); 1894 disconnect_vp_unlocked: 1895 (void) VOP_CLOSE(nvp, 0, 1, 0, CRED()); 1896 VN_RELE(nvp); 1897 disconnect_unlocked: 1898 (void) sodisconnect(so, SEQ_number, 0); 1899 return (error); 1900 1901 pr_disc_vp: 1902 eprintsoline(so, error); 1903 disconnect_vp: 1904 (void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD); 1905 so_unlock_single(so, SOLOCKED); 1906 mutex_exit(&so->so_lock); 1907 (void) VOP_CLOSE(nvp, 0, 1, 0, CRED()); 1908 VN_RELE(nvp); 1909 return (error); 1910 1911 conn_bad: /* Note: SunOS 4/BSD unconditionally returns EINVAL here */ 1912 error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW) 1913 ? EOPNOTSUPP : EINVAL; 1914 e_bad: 1915 eprintsoline(so, error); 1916 return (error); 1917 } 1918 1919 /* 1920 * connect a socket. 1921 * 1922 * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to 1923 * unconnect (by specifying a null address). 1924 */ 1925 int 1926 sotpi_connect(struct sonode *so, 1927 const struct sockaddr *name, 1928 socklen_t namelen, 1929 int fflag, 1930 int flags) 1931 { 1932 struct T_conn_req conn_req; 1933 int error = 0; 1934 mblk_t *mp; 1935 void *src; 1936 socklen_t srclen; 1937 void *addr; 1938 socklen_t addrlen; 1939 boolean_t need_unlock; 1940 1941 dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n", 1942 so, name, namelen, fflag, flags, 1943 pr_state(so->so_state, so->so_mode))); 1944 1945 /* 1946 * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to 1947 * avoid sleeping for memory with SOLOCKED held. 1948 * We know that the T_CONN_REQ can't be larger than 2 * so_faddr_maxlen 1949 * + sizeof (struct T_opthdr). 1950 * (the AF_UNIX so_ux_addr_xlate() does not make the address 1951 * exceed so_faddr_maxlen). 1952 */ 1953 mp = soallocproto(sizeof (struct T_conn_req) + 1954 2 * so->so_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR); 1955 if (mp == NULL) { 1956 /* 1957 * Connect can not fail with ENOBUFS. A signal was 1958 * caught so return EINTR. 1959 */ 1960 error = EINTR; 1961 eprintsoline(so, error); 1962 return (error); 1963 } 1964 1965 mutex_enter(&so->so_lock); 1966 /* 1967 * Make sure that there is a preallocated unbind_req 1968 * message before any binding. This message allocated when 1969 * the socket is created but it might be have been 1970 * consumed. 1971 */ 1972 if (so->so_unbind_mp == NULL) { 1973 dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n")); 1974 /* NOTE: holding so_lock while sleeping */ 1975 so->so_unbind_mp = 1976 soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR); 1977 if (so->so_unbind_mp == NULL) { 1978 error = EINTR; 1979 need_unlock = B_FALSE; 1980 goto done; 1981 } 1982 } 1983 1984 so_lock_single(so); /* Set SOLOCKED */ 1985 need_unlock = B_TRUE; 1986 1987 /* 1988 * Can't have done a listen before connecting. 1989 */ 1990 if (so->so_state & SS_ACCEPTCONN) { 1991 error = EOPNOTSUPP; 1992 goto done; 1993 } 1994 1995 /* 1996 * Must be bound with the transport 1997 */ 1998 if (!(so->so_state & SS_ISBOUND)) { 1999 if ((so->so_family == AF_INET || so->so_family == AF_INET6) && 2000 /*CONSTCOND*/ 2001 so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) { 2002 /* 2003 * Optimization for AF_INET{,6} transports 2004 * that can handle a T_CONN_REQ without being bound. 2005 */ 2006 so_automatic_bind(so); 2007 } else { 2008 error = sotpi_bind(so, NULL, 0, 2009 _SOBIND_UNSPEC|_SOBIND_LOCK_HELD); 2010 if (error) 2011 goto done; 2012 } 2013 ASSERT(so->so_state & SS_ISBOUND); 2014 flags |= _SOCONNECT_DID_BIND; 2015 } 2016 2017 /* 2018 * Handle a connect to a name parameter of type AF_UNSPEC like a 2019 * connect to a null address. This is the portable method to 2020 * unconnect a socket. 2021 */ 2022 if ((namelen >= sizeof (sa_family_t)) && 2023 (name->sa_family == AF_UNSPEC)) { 2024 name = NULL; 2025 namelen = 0; 2026 } 2027 2028 /* 2029 * Check that we are not already connected. 2030 * A connection-oriented socket cannot be reconnected. 2031 * A connected connection-less socket can be 2032 * - connected to a different address by a subsequent connect 2033 * - "unconnected" by a connect to the NULL address 2034 */ 2035 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) { 2036 ASSERT(!(flags & _SOCONNECT_DID_BIND)); 2037 if (so->so_mode & SM_CONNREQUIRED) { 2038 /* Connection-oriented socket */ 2039 error = so->so_state & SS_ISCONNECTED ? 2040 EISCONN : EALREADY; 2041 goto done; 2042 } 2043 /* Connection-less socket */ 2044 if (name == NULL) { 2045 /* 2046 * Remove the connected state and clear SO_DGRAM_ERRIND 2047 * since it was set when the socket was connected. 2048 * If this is UDP also send down a T_DISCON_REQ. 2049 */ 2050 int val; 2051 2052 if ((so->so_family == AF_INET || 2053 so->so_family == AF_INET6) && 2054 (so->so_type == SOCK_DGRAM || 2055 so->so_type == SOCK_RAW) && 2056 /*CONSTCOND*/ 2057 !soconnect_tpi_udp) { 2058 /* XXX What about implicitly unbinding here? */ 2059 error = sodisconnect(so, -1, 2060 _SODISCONNECT_LOCK_HELD); 2061 } else { 2062 so->so_state &= 2063 ~(SS_ISCONNECTED | SS_ISCONNECTING | 2064 SS_FADDR_VALID); 2065 so->so_faddr_len = 0; 2066 } 2067 2068 so_unlock_single(so, SOLOCKED); 2069 mutex_exit(&so->so_lock); 2070 2071 val = 0; 2072 (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND, 2073 &val, (t_uscalar_t)sizeof (val)); 2074 2075 mutex_enter(&so->so_lock); 2076 so_lock_single(so); /* Set SOLOCKED */ 2077 goto done; 2078 } 2079 } 2080 ASSERT(so->so_state & SS_ISBOUND); 2081 2082 if (name == NULL || namelen == 0) { 2083 error = EINVAL; 2084 goto done; 2085 } 2086 /* 2087 * Mark the socket if so_faddr_sa represents the transport level 2088 * address. 2089 */ 2090 if (flags & _SOCONNECT_NOXLATE) { 2091 struct sockaddr_ux *soaddr_ux; 2092 2093 ASSERT(so->so_family == AF_UNIX); 2094 if (namelen != sizeof (struct sockaddr_ux)) { 2095 error = EINVAL; 2096 goto done; 2097 } 2098 soaddr_ux = (struct sockaddr_ux *)name; 2099 name = (struct sockaddr *)&soaddr_ux->sou_addr; 2100 namelen = sizeof (soaddr_ux->sou_addr); 2101 so->so_state |= SS_FADDR_NOXLATE; 2102 } 2103 2104 /* 2105 * Length and family checks. 2106 */ 2107 error = so_addr_verify(so, name, namelen); 2108 if (error) 2109 goto bad; 2110 2111 /* 2112 * Save foreign address. Needed for AF_UNIX as well as 2113 * transport providers that do not support TI_GETPEERNAME. 2114 * Also used for cached foreign address for TCP and UDP. 2115 */ 2116 if (namelen > (t_uscalar_t)so->so_faddr_maxlen) { 2117 error = EINVAL; 2118 goto done; 2119 } 2120 so->so_faddr_len = (socklen_t)namelen; 2121 ASSERT(so->so_faddr_len <= so->so_faddr_maxlen); 2122 bcopy(name, so->so_faddr_sa, namelen); 2123 so->so_state |= SS_FADDR_VALID; 2124 2125 if (so->so_family == AF_UNIX) { 2126 if (so->so_state & SS_FADDR_NOXLATE) { 2127 /* 2128 * Already have a transport internal address. Do not 2129 * pass any (transport internal) source address. 2130 */ 2131 addr = so->so_faddr_sa; 2132 addrlen = (t_uscalar_t)so->so_faddr_len; 2133 src = NULL; 2134 srclen = 0; 2135 } else { 2136 /* 2137 * Pass the sockaddr_un source address as an option 2138 * and translate the remote address. 2139 * Holding so_lock thus so_laddr_sa can not change. 2140 */ 2141 src = so->so_laddr_sa; 2142 srclen = (t_uscalar_t)so->so_laddr_len; 2143 dprintso(so, 1, 2144 ("sotpi_connect UNIX: srclen %d, src %p\n", 2145 srclen, src)); 2146 error = so_ux_addr_xlate(so, 2147 so->so_faddr_sa, (socklen_t)so->so_faddr_len, 2148 (flags & _SOCONNECT_XPG4_2), 2149 &addr, &addrlen); 2150 if (error) 2151 goto bad; 2152 } 2153 } else { 2154 addr = so->so_faddr_sa; 2155 addrlen = (t_uscalar_t)so->so_faddr_len; 2156 src = NULL; 2157 srclen = 0; 2158 } 2159 /* 2160 * When connecting a datagram socket we issue the SO_DGRAM_ERRIND 2161 * option which asks the transport provider to send T_UDERR_IND 2162 * messages. These T_UDERR_IND messages are used to return connected 2163 * style errors (e.g. ECONNRESET) for connected datagram sockets. 2164 * 2165 * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets) 2166 * we send down a T_CONN_REQ. This is needed to let the 2167 * transport assign a local address that is consistent with 2168 * the remote address. Applications depend on a getsockname() 2169 * after a connect() to retrieve the "source" IP address for 2170 * the connected socket. Invalidate the cached local address 2171 * to force getsockname() to enquire of the transport. 2172 */ 2173 if (!(so->so_mode & SM_CONNREQUIRED)) { 2174 /* 2175 * Datagram socket. 2176 */ 2177 int32_t val; 2178 2179 so_unlock_single(so, SOLOCKED); 2180 mutex_exit(&so->so_lock); 2181 2182 val = 1; 2183 (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND, 2184 &val, (t_uscalar_t)sizeof (val)); 2185 2186 mutex_enter(&so->so_lock); 2187 so_lock_single(so); /* Set SOLOCKED */ 2188 if ((so->so_family != AF_INET && so->so_family != AF_INET6) || 2189 (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) || 2190 soconnect_tpi_udp) { 2191 soisconnected(so); 2192 goto done; 2193 } 2194 /* 2195 * Send down T_CONN_REQ etc. 2196 * Clear fflag to avoid returning EWOULDBLOCK. 2197 */ 2198 fflag = 0; 2199 ASSERT(so->so_family != AF_UNIX); 2200 so->so_state &= ~SS_LADDR_VALID; 2201 } else if (so->so_laddr_len != 0) { 2202 /* 2203 * If the local address or port was "any" then it may be 2204 * changed by the transport as a result of the 2205 * connect. Invalidate the cached version if we have one. 2206 */ 2207 switch (so->so_family) { 2208 case AF_INET: 2209 ASSERT(so->so_laddr_len == (socklen_t)sizeof (sin_t)); 2210 if (((sin_t *)so->so_laddr_sa)->sin_addr.s_addr == 2211 INADDR_ANY || 2212 ((sin_t *)so->so_laddr_sa)->sin_port == 0) 2213 so->so_state &= ~SS_LADDR_VALID; 2214 break; 2215 2216 case AF_INET6: 2217 ASSERT(so->so_laddr_len == (socklen_t)sizeof (sin6_t)); 2218 if (IN6_IS_ADDR_UNSPECIFIED( 2219 &((sin6_t *)so->so_laddr_sa) ->sin6_addr) || 2220 IN6_IS_ADDR_V4MAPPED_ANY( 2221 &((sin6_t *)so->so_laddr_sa)->sin6_addr) || 2222 ((sin6_t *)so->so_laddr_sa)->sin6_port == 0) 2223 so->so_state &= ~SS_LADDR_VALID; 2224 break; 2225 2226 default: 2227 break; 2228 } 2229 } 2230 2231 /* 2232 * Check for failure of an earlier call 2233 */ 2234 if (so->so_error != 0) 2235 goto so_bad; 2236 2237 /* 2238 * Send down T_CONN_REQ. Message was allocated above. 2239 */ 2240 conn_req.PRIM_type = T_CONN_REQ; 2241 conn_req.DEST_length = addrlen; 2242 conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req); 2243 if (srclen == 0) { 2244 conn_req.OPT_length = 0; 2245 conn_req.OPT_offset = 0; 2246 soappendmsg(mp, &conn_req, sizeof (conn_req)); 2247 soappendmsg(mp, addr, addrlen); 2248 } else { 2249 /* 2250 * There is a AF_UNIX sockaddr_un to include as a source 2251 * address option. 2252 */ 2253 struct T_opthdr toh; 2254 2255 toh.level = SOL_SOCKET; 2256 toh.name = SO_SRCADDR; 2257 toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 2258 toh.status = 0; 2259 conn_req.OPT_length = 2260 (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen)); 2261 conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) + 2262 _TPI_ALIGN_TOPT(addrlen)); 2263 2264 soappendmsg(mp, &conn_req, sizeof (conn_req)); 2265 soappendmsg(mp, addr, addrlen); 2266 mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 2267 soappendmsg(mp, &toh, sizeof (toh)); 2268 soappendmsg(mp, src, srclen); 2269 mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 2270 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 2271 } 2272 /* 2273 * Set SS_ISCONNECTING before sending down the T_CONN_REQ 2274 * in order to have the right state when the T_CONN_CON shows up. 2275 */ 2276 soisconnecting(so); 2277 mutex_exit(&so->so_lock); 2278 2279 #ifdef C2_AUDIT 2280 if (audit_active) 2281 audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0); 2282 #endif /* C2_AUDIT */ 2283 2284 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 2285 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0); 2286 mp = NULL; 2287 mutex_enter(&so->so_lock); 2288 if (error != 0) 2289 goto bad; 2290 2291 if ((error = sowaitokack(so, T_CONN_REQ)) != 0) 2292 goto bad; 2293 2294 /* Allow other threads to access the socket */ 2295 so_unlock_single(so, SOLOCKED); 2296 need_unlock = B_FALSE; 2297 2298 /* 2299 * Wait until we get a T_CONN_CON or an error 2300 */ 2301 if ((error = sowaitconnected(so, fflag, 0)) != 0) { 2302 so_lock_single(so); /* Set SOLOCKED */ 2303 need_unlock = B_TRUE; 2304 } 2305 2306 done: 2307 freemsg(mp); 2308 switch (error) { 2309 case EINPROGRESS: 2310 case EALREADY: 2311 case EISCONN: 2312 case EINTR: 2313 /* Non-fatal errors */ 2314 so->so_state &= ~SS_LADDR_VALID; 2315 /* FALLTHRU */ 2316 case 0: 2317 break; 2318 2319 case EHOSTUNREACH: 2320 if (flags & _SOCONNECT_XPG4_2) { 2321 /* 2322 * X/Open specification contains a requirement that 2323 * ENETUNREACH be returned but does not require 2324 * EHOSTUNREACH. In order to keep the test suite 2325 * happy we mess with the errno here. 2326 */ 2327 error = ENETUNREACH; 2328 } 2329 /* FALLTHRU */ 2330 2331 default: 2332 ASSERT(need_unlock); 2333 /* 2334 * Fatal errors: clear SS_ISCONNECTING in case it was set, 2335 * and invalidate local-address cache 2336 */ 2337 so->so_state &= ~(SS_ISCONNECTING | SS_LADDR_VALID); 2338 /* A discon_ind might have already unbound us */ 2339 if ((flags & _SOCONNECT_DID_BIND) && 2340 (so->so_state & SS_ISBOUND)) { 2341 int err; 2342 2343 err = sotpi_unbind(so, 0); 2344 /* LINTED - statement has no conseq */ 2345 if (err) { 2346 eprintsoline(so, err); 2347 } 2348 } 2349 break; 2350 } 2351 if (need_unlock) 2352 so_unlock_single(so, SOLOCKED); 2353 mutex_exit(&so->so_lock); 2354 return (error); 2355 2356 so_bad: error = sogeterr(so); 2357 bad: eprintsoline(so, error); 2358 goto done; 2359 } 2360 2361 int 2362 sotpi_shutdown(struct sonode *so, int how) 2363 { 2364 struct T_ordrel_req ordrel_req; 2365 mblk_t *mp; 2366 uint_t old_state, state_change; 2367 int error = 0; 2368 2369 dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n", 2370 so, how, pr_state(so->so_state, so->so_mode))); 2371 2372 mutex_enter(&so->so_lock); 2373 so_lock_single(so); /* Set SOLOCKED */ 2374 2375 /* 2376 * SunOS 4.X has no check for datagram sockets. 2377 * 5.X checks that it is connected (ENOTCONN) 2378 * X/Open requires that we check the connected state. 2379 */ 2380 if (!(so->so_state & SS_ISCONNECTED)) { 2381 if (!xnet_skip_checks) { 2382 error = ENOTCONN; 2383 if (xnet_check_print) { 2384 printf("sockfs: X/Open shutdown check " 2385 "caused ENOTCONN\n"); 2386 } 2387 } 2388 goto done; 2389 } 2390 /* 2391 * Record the current state and then perform any state changes. 2392 * Then use the difference between the old and new states to 2393 * determine which messages need to be sent. 2394 * This prevents e.g. duplicate T_ORDREL_REQ when there are 2395 * duplicate calls to shutdown(). 2396 */ 2397 old_state = so->so_state; 2398 2399 switch (how) { 2400 case 0: 2401 socantrcvmore(so); 2402 break; 2403 case 1: 2404 socantsendmore(so); 2405 break; 2406 case 2: 2407 socantsendmore(so); 2408 socantrcvmore(so); 2409 break; 2410 default: 2411 error = EINVAL; 2412 goto done; 2413 } 2414 2415 /* 2416 * Assumes that the SS_CANT* flags are never cleared in the above code. 2417 */ 2418 state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) - 2419 (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)); 2420 ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0); 2421 2422 switch (state_change) { 2423 case 0: 2424 dprintso(so, 1, 2425 ("sotpi_shutdown: nothing to send in state 0x%x\n", 2426 so->so_state)); 2427 goto done; 2428 2429 case SS_CANTRCVMORE: 2430 mutex_exit(&so->so_lock); 2431 strseteof(SOTOV(so), 1); 2432 /* 2433 * strseteof takes care of read side wakeups, 2434 * pollwakeups, and signals. 2435 */ 2436 /* 2437 * Get the read lock before flushing data to avoid problems 2438 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg. 2439 */ 2440 mutex_enter(&so->so_lock); 2441 (void) so_lock_read(so, 0); /* Set SOREADLOCKED */ 2442 mutex_exit(&so->so_lock); 2443 2444 /* Flush read side queue */ 2445 strflushrq(SOTOV(so), FLUSHALL); 2446 2447 mutex_enter(&so->so_lock); 2448 so_unlock_read(so); /* Clear SOREADLOCKED */ 2449 break; 2450 2451 case SS_CANTSENDMORE: 2452 mutex_exit(&so->so_lock); 2453 strsetwerror(SOTOV(so), 0, 0, sogetwrerr); 2454 mutex_enter(&so->so_lock); 2455 break; 2456 2457 case SS_CANTSENDMORE|SS_CANTRCVMORE: 2458 mutex_exit(&so->so_lock); 2459 strsetwerror(SOTOV(so), 0, 0, sogetwrerr); 2460 strseteof(SOTOV(so), 1); 2461 /* 2462 * strseteof takes care of read side wakeups, 2463 * pollwakeups, and signals. 2464 */ 2465 /* 2466 * Get the read lock before flushing data to avoid problems 2467 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg. 2468 */ 2469 mutex_enter(&so->so_lock); 2470 (void) so_lock_read(so, 0); /* Set SOREADLOCKED */ 2471 mutex_exit(&so->so_lock); 2472 2473 /* Flush read side queue */ 2474 strflushrq(SOTOV(so), FLUSHALL); 2475 2476 mutex_enter(&so->so_lock); 2477 so_unlock_read(so); /* Clear SOREADLOCKED */ 2478 break; 2479 } 2480 2481 ASSERT(MUTEX_HELD(&so->so_lock)); 2482 2483 /* 2484 * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them 2485 * was set due to this call and the new state has both of them set: 2486 * Send the AF_UNIX close indication 2487 * For T_COTS send a discon_ind 2488 * 2489 * If cantsend was set due to this call: 2490 * For T_COTSORD send an ordrel_ind 2491 * 2492 * Note that for T_CLTS there is no message sent here. 2493 */ 2494 if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) == 2495 (SS_CANTRCVMORE|SS_CANTSENDMORE)) { 2496 /* 2497 * For SunOS 4.X compatibility we tell the other end 2498 * that we are unable to receive at this point. 2499 */ 2500 if (so->so_family == AF_UNIX && so->so_serv_type != T_CLTS) 2501 so_unix_close(so); 2502 2503 if (so->so_serv_type == T_COTS) 2504 error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD); 2505 } 2506 if ((state_change & SS_CANTSENDMORE) && 2507 (so->so_serv_type == T_COTS_ORD)) { 2508 /* Send an orderly release */ 2509 ordrel_req.PRIM_type = T_ORDREL_REQ; 2510 2511 mutex_exit(&so->so_lock); 2512 mp = soallocproto1(&ordrel_req, sizeof (ordrel_req), 2513 0, _ALLOC_SLEEP); 2514 /* 2515 * Send down the T_ORDREL_REQ even if there is flow control. 2516 * This prevents shutdown from blocking. 2517 * Note that there is no T_OK_ACK for ordrel_req. 2518 */ 2519 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 2520 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 2521 mutex_enter(&so->so_lock); 2522 if (error) { 2523 eprintsoline(so, error); 2524 goto done; 2525 } 2526 } 2527 2528 done: 2529 so_unlock_single(so, SOLOCKED); 2530 mutex_exit(&so->so_lock); 2531 return (error); 2532 } 2533 2534 /* 2535 * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send 2536 * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer 2537 * that we have closed. 2538 * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length 2539 * T_UNITDATA_REQ containing the same option. 2540 * 2541 * For SOCK_DGRAM half-connections (somebody connected to this end 2542 * but this end is not connect) we don't know where to send any 2543 * SO_UNIX_CLOSE. 2544 * 2545 * We have to ignore stream head errors just in case there has been 2546 * a shutdown(output). 2547 * Ignore any flow control to try to get the message more quickly to the peer. 2548 * While locally ignoring flow control solves the problem when there 2549 * is only the loopback transport on the stream it would not provide 2550 * the correct AF_UNIX socket semantics when one or more modules have 2551 * been pushed. 2552 */ 2553 void 2554 so_unix_close(struct sonode *so) 2555 { 2556 int error; 2557 struct T_opthdr toh; 2558 mblk_t *mp; 2559 2560 ASSERT(MUTEX_HELD(&so->so_lock)); 2561 2562 ASSERT(so->so_family == AF_UNIX); 2563 2564 if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) != 2565 (SS_ISCONNECTED|SS_ISBOUND)) 2566 return; 2567 2568 dprintso(so, 1, ("so_unix_close(%p) %s\n", 2569 so, pr_state(so->so_state, so->so_mode))); 2570 2571 toh.level = SOL_SOCKET; 2572 toh.name = SO_UNIX_CLOSE; 2573 2574 /* zero length + header */ 2575 toh.len = (t_uscalar_t)sizeof (struct T_opthdr); 2576 toh.status = 0; 2577 2578 if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) { 2579 struct T_optdata_req tdr; 2580 2581 tdr.PRIM_type = T_OPTDATA_REQ; 2582 tdr.DATA_flag = 0; 2583 2584 tdr.OPT_length = (t_scalar_t)sizeof (toh); 2585 tdr.OPT_offset = (t_scalar_t)sizeof (tdr); 2586 2587 /* NOTE: holding so_lock while sleeping */ 2588 mp = soallocproto2(&tdr, sizeof (tdr), 2589 &toh, sizeof (toh), 0, _ALLOC_SLEEP); 2590 } else { 2591 struct T_unitdata_req tudr; 2592 void *addr; 2593 socklen_t addrlen; 2594 void *src; 2595 socklen_t srclen; 2596 struct T_opthdr toh2; 2597 t_scalar_t size; 2598 2599 /* Connecteded DGRAM socket */ 2600 2601 /* 2602 * For AF_UNIX the destination address is translated to 2603 * an internal name and the source address is passed as 2604 * an option. 2605 */ 2606 /* 2607 * Length and family checks. 2608 */ 2609 error = so_addr_verify(so, so->so_faddr_sa, 2610 (t_uscalar_t)so->so_faddr_len); 2611 if (error) { 2612 eprintsoline(so, error); 2613 return; 2614 } 2615 if (so->so_state & SS_FADDR_NOXLATE) { 2616 /* 2617 * Already have a transport internal address. Do not 2618 * pass any (transport internal) source address. 2619 */ 2620 addr = so->so_faddr_sa; 2621 addrlen = (t_uscalar_t)so->so_faddr_len; 2622 src = NULL; 2623 srclen = 0; 2624 } else { 2625 /* 2626 * Pass the sockaddr_un source address as an option 2627 * and translate the remote address. 2628 * Holding so_lock thus so_laddr_sa can not change. 2629 */ 2630 src = so->so_laddr_sa; 2631 srclen = (socklen_t)so->so_laddr_len; 2632 dprintso(so, 1, 2633 ("so_ux_close: srclen %d, src %p\n", 2634 srclen, src)); 2635 error = so_ux_addr_xlate(so, 2636 so->so_faddr_sa, 2637 (socklen_t)so->so_faddr_len, 0, 2638 &addr, &addrlen); 2639 if (error) { 2640 eprintsoline(so, error); 2641 return; 2642 } 2643 } 2644 tudr.PRIM_type = T_UNITDATA_REQ; 2645 tudr.DEST_length = addrlen; 2646 tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 2647 if (srclen == 0) { 2648 tudr.OPT_length = (t_scalar_t)sizeof (toh); 2649 tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 2650 _TPI_ALIGN_TOPT(addrlen)); 2651 2652 size = tudr.OPT_offset + tudr.OPT_length; 2653 /* NOTE: holding so_lock while sleeping */ 2654 mp = soallocproto2(&tudr, sizeof (tudr), 2655 addr, addrlen, size, _ALLOC_SLEEP); 2656 mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen); 2657 soappendmsg(mp, &toh, sizeof (toh)); 2658 } else { 2659 /* 2660 * There is a AF_UNIX sockaddr_un to include as a 2661 * source address option. 2662 */ 2663 tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) + 2664 _TPI_ALIGN_TOPT(srclen)); 2665 tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 2666 _TPI_ALIGN_TOPT(addrlen)); 2667 2668 toh2.level = SOL_SOCKET; 2669 toh2.name = SO_SRCADDR; 2670 toh2.len = (t_uscalar_t)(srclen + 2671 sizeof (struct T_opthdr)); 2672 toh2.status = 0; 2673 2674 size = tudr.OPT_offset + tudr.OPT_length; 2675 2676 /* NOTE: holding so_lock while sleeping */ 2677 mp = soallocproto2(&tudr, sizeof (tudr), 2678 addr, addrlen, size, _ALLOC_SLEEP); 2679 mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 2680 soappendmsg(mp, &toh, sizeof (toh)); 2681 soappendmsg(mp, &toh2, sizeof (toh2)); 2682 soappendmsg(mp, src, srclen); 2683 mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 2684 } 2685 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 2686 } 2687 mutex_exit(&so->so_lock); 2688 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 2689 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 2690 mutex_enter(&so->so_lock); 2691 } 2692 2693 /* 2694 * Handle recv* calls that set MSG_OOB or MSG_OOB together with MSG_PEEK. 2695 */ 2696 int 2697 sorecvoob(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, int flags) 2698 { 2699 mblk_t *mp, *nmp; 2700 int error; 2701 2702 dprintso(so, 1, ("sorecvoob(%p, %p, 0x%x)\n", so, msg, flags)); 2703 2704 /* 2705 * There is never any oob data with addresses or control since 2706 * the T_EXDATA_IND does not carry any options. 2707 */ 2708 msg->msg_controllen = 0; 2709 msg->msg_namelen = 0; 2710 2711 mutex_enter(&so->so_lock); 2712 ASSERT(so_verify_oobstate(so)); 2713 if ((so->so_options & SO_OOBINLINE) || 2714 (so->so_state & (SS_OOBPEND|SS_HADOOBDATA)) != SS_OOBPEND) { 2715 dprintso(so, 1, ("sorecvoob: inline or data consumed\n")); 2716 mutex_exit(&so->so_lock); 2717 return (EINVAL); 2718 } 2719 if (!(so->so_state & SS_HAVEOOBDATA)) { 2720 dprintso(so, 1, ("sorecvoob: no data yet\n")); 2721 mutex_exit(&so->so_lock); 2722 return (EWOULDBLOCK); 2723 } 2724 ASSERT(so->so_oobmsg != NULL); 2725 mp = so->so_oobmsg; 2726 if (flags & MSG_PEEK) { 2727 /* 2728 * Since recv* can not return ENOBUFS we can not use dupmsg. 2729 * Instead we revert to the consolidation private 2730 * allocb_wait plus bcopy. 2731 */ 2732 mblk_t *mp1; 2733 2734 mp1 = allocb_wait(msgdsize(mp), BPRI_MED, STR_NOSIG, NULL); 2735 ASSERT(mp1); 2736 2737 while (mp != NULL) { 2738 ssize_t size; 2739 2740 size = MBLKL(mp); 2741 bcopy(mp->b_rptr, mp1->b_wptr, size); 2742 mp1->b_wptr += size; 2743 ASSERT(mp1->b_wptr <= mp1->b_datap->db_lim); 2744 mp = mp->b_cont; 2745 } 2746 mp = mp1; 2747 } else { 2748 /* 2749 * Update the state indicating that the data has been consumed. 2750 * Keep SS_OOBPEND set until data is consumed past the mark. 2751 */ 2752 so->so_oobmsg = NULL; 2753 so->so_state ^= SS_HAVEOOBDATA|SS_HADOOBDATA; 2754 } 2755 dprintso(so, 1, 2756 ("after recvoob(%p): counts %d/%d state %s\n", 2757 so, so->so_oobsigcnt, 2758 so->so_oobcnt, pr_state(so->so_state, so->so_mode))); 2759 ASSERT(so_verify_oobstate(so)); 2760 mutex_exit(&so->so_lock); 2761 2762 error = 0; 2763 nmp = mp; 2764 while (nmp != NULL && uiop->uio_resid > 0) { 2765 ssize_t n = MBLKL(nmp); 2766 2767 n = MIN(n, uiop->uio_resid); 2768 if (n > 0) 2769 error = uiomove(nmp->b_rptr, n, 2770 UIO_READ, uiop); 2771 if (error) 2772 break; 2773 nmp = nmp->b_cont; 2774 } 2775 freemsg(mp); 2776 return (error); 2777 } 2778 2779 /* 2780 * Called by sotpi_recvmsg when reading a non-zero amount of data. 2781 * In addition, the caller typically verifies that there is some 2782 * potential state to clear by checking 2783 * if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) 2784 * before calling this routine. 2785 * Note that such a check can be made without holding so_lock since 2786 * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg 2787 * decrements so_oobsigcnt. 2788 * 2789 * When data is read *after* the point that all pending 2790 * oob data has been consumed the oob indication is cleared. 2791 * 2792 * This logic keeps select/poll returning POLLRDBAND and 2793 * SIOCATMARK returning true until we have read past 2794 * the mark. 2795 */ 2796 static void 2797 sorecv_update_oobstate(struct sonode *so) 2798 { 2799 mutex_enter(&so->so_lock); 2800 ASSERT(so_verify_oobstate(so)); 2801 dprintso(so, 1, 2802 ("sorecv_update_oobstate: counts %d/%d state %s\n", 2803 so->so_oobsigcnt, 2804 so->so_oobcnt, pr_state(so->so_state, so->so_mode))); 2805 if (so->so_oobsigcnt == 0) { 2806 /* No more pending oob indications */ 2807 so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK); 2808 freemsg(so->so_oobmsg); 2809 so->so_oobmsg = NULL; 2810 } 2811 ASSERT(so_verify_oobstate(so)); 2812 mutex_exit(&so->so_lock); 2813 } 2814 2815 /* 2816 * Handle recv* calls for an so which has NL7C saved recv mblk_t(s). 2817 */ 2818 static int 2819 nl7c_sorecv(struct sonode *so, mblk_t **rmp, uio_t *uiop, rval_t *rp) 2820 { 2821 int error = 0; 2822 mblk_t *tmp = NULL; 2823 mblk_t *pmp = NULL; 2824 mblk_t *nmp = so->so_nl7c_rcv_mp; 2825 2826 ASSERT(nmp != NULL); 2827 2828 while (nmp != NULL && uiop->uio_resid > 0) { 2829 ssize_t n; 2830 2831 if (DB_TYPE(nmp) == M_DATA) { 2832 /* 2833 * We have some data, uiomove up to resid bytes. 2834 */ 2835 n = MIN(MBLKL(nmp), uiop->uio_resid); 2836 if (n > 0) 2837 error = uiomove(nmp->b_rptr, n, UIO_READ, uiop); 2838 nmp->b_rptr += n; 2839 if (nmp->b_rptr == nmp->b_wptr) { 2840 pmp = nmp; 2841 nmp = nmp->b_cont; 2842 } 2843 if (error) 2844 break; 2845 } else { 2846 /* 2847 * We only handle data, save for caller to handle. 2848 */ 2849 if (pmp != NULL) { 2850 pmp->b_cont = nmp->b_cont; 2851 } 2852 nmp->b_cont = NULL; 2853 if (*rmp == NULL) { 2854 *rmp = nmp; 2855 } else { 2856 tmp->b_cont = nmp; 2857 } 2858 nmp = nmp->b_cont; 2859 tmp = nmp; 2860 } 2861 } 2862 if (pmp != NULL) { 2863 /* Free any mblk_t(s) which we have consumed */ 2864 pmp->b_cont = NULL; 2865 freemsg(so->so_nl7c_rcv_mp); 2866 } 2867 if ((so->so_nl7c_rcv_mp = nmp) == NULL) { 2868 /* Last mblk_t so return the saved kstrgetmsg() rval/error */ 2869 if (error == 0) { 2870 rval_t *p = (rval_t *)&so->so_nl7c_rcv_rval; 2871 2872 error = p->r_v.r_v2; 2873 p->r_v.r_v2 = 0; 2874 } 2875 rp->r_vals = so->so_nl7c_rcv_rval; 2876 so->so_nl7c_rcv_rval = 0; 2877 } else { 2878 /* More mblk_t(s) to process so no rval to return */ 2879 rp->r_vals = 0; 2880 } 2881 return (error); 2882 } 2883 2884 /* 2885 * Receive the next message on the queue. 2886 * If msg_controllen is non-zero when called the caller is interested in 2887 * any received control info (options). 2888 * If msg_namelen is non-zero when called the caller is interested in 2889 * any received source address. 2890 * The routine returns with msg_control and msg_name pointing to 2891 * kmem_alloc'ed memory which the caller has to free. 2892 */ 2893 int 2894 sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop) 2895 { 2896 union T_primitives *tpr; 2897 mblk_t *mp; 2898 uchar_t pri; 2899 int pflag, opflag; 2900 void *control; 2901 t_uscalar_t controllen; 2902 t_uscalar_t namelen; 2903 int so_state = so->so_state; /* Snapshot */ 2904 ssize_t saved_resid; 2905 int error; 2906 rval_t rval; 2907 int flags; 2908 clock_t timout; 2909 int first; 2910 2911 flags = msg->msg_flags; 2912 msg->msg_flags = 0; 2913 2914 dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n", 2915 so, msg, flags, 2916 pr_state(so->so_state, so->so_mode), so->so_error)); 2917 2918 /* 2919 * If we are not connected because we have never been connected 2920 * we return ENOTCONN. If we have been connected (but are no longer 2921 * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return 2922 * the EOF. 2923 * 2924 * An alternative would be to post an ENOTCONN error in stream head 2925 * (read+write) and clear it when we're connected. However, that error 2926 * would cause incorrect poll/select behavior! 2927 */ 2928 if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 && 2929 (so->so_mode & SM_CONNREQUIRED)) { 2930 return (ENOTCONN); 2931 } 2932 2933 /* 2934 * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but 2935 * after checking that the read queue is empty) and returns zero. 2936 * This implementation will sleep (in kstrgetmsg) even if uio_resid 2937 * is zero. 2938 */ 2939 2940 if (flags & MSG_OOB) { 2941 /* Check that the transport supports OOB */ 2942 if (!(so->so_mode & SM_EXDATA)) 2943 return (EOPNOTSUPP); 2944 return (sorecvoob(so, msg, uiop, flags)); 2945 } 2946 2947 /* 2948 * Set msg_controllen and msg_namelen to zero here to make it 2949 * simpler in the cases that no control or name is returned. 2950 */ 2951 controllen = msg->msg_controllen; 2952 namelen = msg->msg_namelen; 2953 msg->msg_controllen = 0; 2954 msg->msg_namelen = 0; 2955 2956 dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n", 2957 namelen, controllen)); 2958 2959 mutex_enter(&so->so_lock); 2960 /* 2961 * If an NL7C enabled socket and not waiting for write data. 2962 */ 2963 if ((so->so_nl7c_flags & (NL7C_ENABLED | NL7C_WAITWRITE)) == 2964 NL7C_ENABLED) { 2965 if (so->so_nl7c_uri) { 2966 /* Close uri processing for a previous request */ 2967 nl7c_close(so); 2968 } 2969 if ((so_state & SS_CANTRCVMORE) && so->so_nl7c_rcv_mp == NULL) { 2970 /* Nothing to process, EOF */ 2971 mutex_exit(&so->so_lock); 2972 return (0); 2973 } else if (so->so_nl7c_flags & NL7C_SOPERSIST) { 2974 /* Persistent NL7C socket, try to process request */ 2975 boolean_t ret; 2976 2977 ret = nl7c_process(so, 2978 (so->so_state & (SS_NONBLOCK|SS_NDELAY))); 2979 rval.r_vals = so->so_nl7c_rcv_rval; 2980 error = rval.r_v.r_v2; 2981 if (error) { 2982 /* Error of some sort, return it */ 2983 mutex_exit(&so->so_lock); 2984 return (error); 2985 } 2986 if (so->so_nl7c_flags && 2987 ! (so->so_nl7c_flags & NL7C_WAITWRITE)) { 2988 /* 2989 * Still an NL7C socket and no data 2990 * to pass up to the caller. 2991 */ 2992 mutex_exit(&so->so_lock); 2993 if (ret) { 2994 /* EOF */ 2995 return (0); 2996 } else { 2997 /* Need more data */ 2998 return (EAGAIN); 2999 } 3000 } 3001 } else { 3002 /* 3003 * Not persistent so no further NL7C processing. 3004 */ 3005 so->so_nl7c_flags = 0; 3006 } 3007 } 3008 /* 3009 * Only one reader is allowed at any given time. This is needed 3010 * for T_EXDATA handling and, in the future, MSG_WAITALL. 3011 * 3012 * This is slightly different that BSD behavior in that it fails with 3013 * EWOULDBLOCK when using nonblocking io. In BSD the read queue access 3014 * is single-threaded using sblock(), which is dropped while waiting 3015 * for data to appear. The difference shows up e.g. if one 3016 * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor 3017 * does use nonblocking io and different threads are reading each 3018 * file descriptor. In BSD there would never be an EWOULDBLOCK error 3019 * in this case as long as the read queue doesn't get empty. 3020 * In this implementation the thread using nonblocking io can 3021 * get an EWOULDBLOCK error due to the blocking thread executing 3022 * e.g. in the uiomove in kstrgetmsg. 3023 * This difference is not believed to be significant. 3024 */ 3025 /* Set SOREADLOCKED */ 3026 error = so_lock_read_intr(so, 3027 uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0)); 3028 mutex_exit(&so->so_lock); 3029 if (error) 3030 return (error); 3031 3032 /* 3033 * Tell kstrgetmsg to not inspect the stream head errors until all 3034 * queued data has been consumed. 3035 * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set. 3036 * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block. 3037 * 3038 * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and 3039 * to T_OPTDATA_IND that do not contain any user-visible control msg. 3040 * Note that MSG_WAITALL set with MSG_PEEK is a noop. 3041 */ 3042 pflag = MSG_ANY | MSG_DELAYERROR; 3043 if (flags & MSG_PEEK) { 3044 pflag |= MSG_IPEEK; 3045 flags &= ~MSG_WAITALL; 3046 } 3047 if (so->so_mode & SM_ATOMIC) 3048 pflag |= MSG_DISCARDTAIL; 3049 3050 if (flags & MSG_DONTWAIT) 3051 timout = 0; 3052 else 3053 timout = -1; 3054 opflag = pflag; 3055 first = 1; 3056 3057 retry: 3058 saved_resid = uiop->uio_resid; 3059 pri = 0; 3060 mp = NULL; 3061 if (so->so_nl7c_rcv_mp != NULL) { 3062 /* Already kstrgetmsg()ed saved mblk(s) from NL7C */ 3063 error = nl7c_sorecv(so, &mp, uiop, &rval); 3064 } else { 3065 error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag, 3066 timout, &rval); 3067 } 3068 if (error) { 3069 switch (error) { 3070 case EINTR: 3071 case EWOULDBLOCK: 3072 if (!first) 3073 error = 0; 3074 break; 3075 case ETIME: 3076 /* Returned from kstrgetmsg when timeout expires */ 3077 if (!first) 3078 error = 0; 3079 else 3080 error = EWOULDBLOCK; 3081 break; 3082 default: 3083 eprintsoline(so, error); 3084 break; 3085 } 3086 mutex_enter(&so->so_lock); 3087 so_unlock_read(so); /* Clear SOREADLOCKED */ 3088 mutex_exit(&so->so_lock); 3089 return (error); 3090 } 3091 /* 3092 * For datagrams the MOREDATA flag is used to set MSG_TRUNC. 3093 * For non-datagrams MOREDATA is used to set MSG_EOR. 3094 */ 3095 ASSERT(!(rval.r_val1 & MORECTL)); 3096 if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC)) 3097 msg->msg_flags |= MSG_TRUNC; 3098 3099 if (mp == NULL) { 3100 dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n")); 3101 /* 3102 * 4.3BSD and 4.4BSD clears the mark when peeking across it. 3103 * The draft Posix socket spec states that the mark should 3104 * not be cleared when peeking. We follow the latter. 3105 */ 3106 if ((so->so_state & 3107 (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 3108 (uiop->uio_resid != saved_resid) && 3109 !(flags & MSG_PEEK)) { 3110 sorecv_update_oobstate(so); 3111 } 3112 3113 mutex_enter(&so->so_lock); 3114 /* Set MSG_EOR based on MOREDATA */ 3115 if (!(rval.r_val1 & MOREDATA)) { 3116 if (so->so_state & SS_SAVEDEOR) { 3117 msg->msg_flags |= MSG_EOR; 3118 so->so_state &= ~SS_SAVEDEOR; 3119 } 3120 } 3121 /* 3122 * If some data was received (i.e. not EOF) and the 3123 * read/recv* has not been satisfied wait for some more. 3124 */ 3125 if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 3126 uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 3127 mutex_exit(&so->so_lock); 3128 first = 0; 3129 pflag = opflag | MSG_NOMARK; 3130 goto retry; 3131 } 3132 so_unlock_read(so); /* Clear SOREADLOCKED */ 3133 mutex_exit(&so->so_lock); 3134 return (0); 3135 } 3136 3137 /* strsock_proto has already verified length and alignment */ 3138 tpr = (union T_primitives *)mp->b_rptr; 3139 dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type)); 3140 3141 switch (tpr->type) { 3142 case T_DATA_IND: { 3143 if ((so->so_state & 3144 (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 3145 (uiop->uio_resid != saved_resid) && 3146 !(flags & MSG_PEEK)) { 3147 sorecv_update_oobstate(so); 3148 } 3149 3150 /* 3151 * Set msg_flags to MSG_EOR based on 3152 * MORE_flag and MOREDATA. 3153 */ 3154 mutex_enter(&so->so_lock); 3155 so->so_state &= ~SS_SAVEDEOR; 3156 if (!(tpr->data_ind.MORE_flag & 1)) { 3157 if (!(rval.r_val1 & MOREDATA)) 3158 msg->msg_flags |= MSG_EOR; 3159 else 3160 so->so_state |= SS_SAVEDEOR; 3161 } 3162 freemsg(mp); 3163 /* 3164 * If some data was received (i.e. not EOF) and the 3165 * read/recv* has not been satisfied wait for some more. 3166 */ 3167 if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 3168 uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 3169 mutex_exit(&so->so_lock); 3170 first = 0; 3171 pflag = opflag | MSG_NOMARK; 3172 goto retry; 3173 } 3174 so_unlock_read(so); /* Clear SOREADLOCKED */ 3175 mutex_exit(&so->so_lock); 3176 return (0); 3177 } 3178 case T_UNITDATA_IND: { 3179 void *addr; 3180 t_uscalar_t addrlen; 3181 void *abuf; 3182 t_uscalar_t optlen; 3183 void *opt; 3184 3185 if ((so->so_state & 3186 (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 3187 (uiop->uio_resid != saved_resid) && 3188 !(flags & MSG_PEEK)) { 3189 sorecv_update_oobstate(so); 3190 } 3191 3192 if (namelen != 0) { 3193 /* Caller wants source address */ 3194 addrlen = tpr->unitdata_ind.SRC_length; 3195 addr = sogetoff(mp, 3196 tpr->unitdata_ind.SRC_offset, 3197 addrlen, 1); 3198 if (addr == NULL) { 3199 freemsg(mp); 3200 error = EPROTO; 3201 eprintsoline(so, error); 3202 goto err; 3203 } 3204 if (so->so_family == AF_UNIX) { 3205 /* 3206 * Can not use the transport level address. 3207 * If there is a SO_SRCADDR option carrying 3208 * the socket level address it will be 3209 * extracted below. 3210 */ 3211 addr = NULL; 3212 addrlen = 0; 3213 } 3214 } 3215 optlen = tpr->unitdata_ind.OPT_length; 3216 if (optlen != 0) { 3217 t_uscalar_t ncontrollen; 3218 3219 /* 3220 * Extract any source address option. 3221 * Determine how large cmsg buffer is needed. 3222 */ 3223 opt = sogetoff(mp, 3224 tpr->unitdata_ind.OPT_offset, 3225 optlen, __TPI_ALIGN_SIZE); 3226 3227 if (opt == NULL) { 3228 freemsg(mp); 3229 error = EPROTO; 3230 eprintsoline(so, error); 3231 goto err; 3232 } 3233 if (so->so_family == AF_UNIX) 3234 so_getopt_srcaddr(opt, optlen, &addr, &addrlen); 3235 ncontrollen = so_cmsglen(mp, opt, optlen, 3236 !(flags & MSG_XPG4_2)); 3237 if (controllen != 0) 3238 controllen = ncontrollen; 3239 else if (ncontrollen != 0) 3240 msg->msg_flags |= MSG_CTRUNC; 3241 } else { 3242 controllen = 0; 3243 } 3244 3245 if (namelen != 0) { 3246 /* 3247 * Return address to caller. 3248 * Caller handles truncation if length 3249 * exceeds msg_namelen. 3250 * NOTE: AF_UNIX NUL termination is ensured by 3251 * the sender's copyin_name(). 3252 */ 3253 abuf = kmem_alloc(addrlen, KM_SLEEP); 3254 3255 bcopy(addr, abuf, addrlen); 3256 msg->msg_name = abuf; 3257 msg->msg_namelen = addrlen; 3258 } 3259 3260 if (controllen != 0) { 3261 /* 3262 * Return control msg to caller. 3263 * Caller handles truncation if length 3264 * exceeds msg_controllen. 3265 */ 3266 control = kmem_zalloc(controllen, KM_SLEEP); 3267 3268 error = so_opt2cmsg(mp, opt, optlen, 3269 !(flags & MSG_XPG4_2), 3270 control, controllen); 3271 if (error) { 3272 freemsg(mp); 3273 if (msg->msg_namelen != 0) 3274 kmem_free(msg->msg_name, 3275 msg->msg_namelen); 3276 kmem_free(control, controllen); 3277 eprintsoline(so, error); 3278 goto err; 3279 } 3280 msg->msg_control = control; 3281 msg->msg_controllen = controllen; 3282 } 3283 3284 freemsg(mp); 3285 mutex_enter(&so->so_lock); 3286 so_unlock_read(so); /* Clear SOREADLOCKED */ 3287 mutex_exit(&so->so_lock); 3288 return (0); 3289 } 3290 case T_OPTDATA_IND: { 3291 struct T_optdata_req *tdr; 3292 void *opt; 3293 t_uscalar_t optlen; 3294 3295 if ((so->so_state & 3296 (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) && 3297 (uiop->uio_resid != saved_resid) && 3298 !(flags & MSG_PEEK)) { 3299 sorecv_update_oobstate(so); 3300 } 3301 3302 tdr = (struct T_optdata_req *)mp->b_rptr; 3303 optlen = tdr->OPT_length; 3304 if (optlen != 0) { 3305 t_uscalar_t ncontrollen; 3306 /* 3307 * Determine how large cmsg buffer is needed. 3308 */ 3309 opt = sogetoff(mp, 3310 tpr->optdata_ind.OPT_offset, 3311 optlen, __TPI_ALIGN_SIZE); 3312 3313 if (opt == NULL) { 3314 freemsg(mp); 3315 error = EPROTO; 3316 eprintsoline(so, error); 3317 goto err; 3318 } 3319 3320 ncontrollen = so_cmsglen(mp, opt, optlen, 3321 !(flags & MSG_XPG4_2)); 3322 if (controllen != 0) 3323 controllen = ncontrollen; 3324 else if (ncontrollen != 0) 3325 msg->msg_flags |= MSG_CTRUNC; 3326 } else { 3327 controllen = 0; 3328 } 3329 3330 if (controllen != 0) { 3331 /* 3332 * Return control msg to caller. 3333 * Caller handles truncation if length 3334 * exceeds msg_controllen. 3335 */ 3336 control = kmem_zalloc(controllen, KM_SLEEP); 3337 3338 error = so_opt2cmsg(mp, opt, optlen, 3339 !(flags & MSG_XPG4_2), 3340 control, controllen); 3341 if (error) { 3342 freemsg(mp); 3343 kmem_free(control, controllen); 3344 eprintsoline(so, error); 3345 goto err; 3346 } 3347 msg->msg_control = control; 3348 msg->msg_controllen = controllen; 3349 } 3350 3351 /* 3352 * Set msg_flags to MSG_EOR based on 3353 * DATA_flag and MOREDATA. 3354 */ 3355 mutex_enter(&so->so_lock); 3356 so->so_state &= ~SS_SAVEDEOR; 3357 if (!(tpr->data_ind.MORE_flag & 1)) { 3358 if (!(rval.r_val1 & MOREDATA)) 3359 msg->msg_flags |= MSG_EOR; 3360 else 3361 so->so_state |= SS_SAVEDEOR; 3362 } 3363 freemsg(mp); 3364 /* 3365 * If some data was received (i.e. not EOF) and the 3366 * read/recv* has not been satisfied wait for some more. 3367 * Not possible to wait if control info was received. 3368 */ 3369 if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) && 3370 controllen == 0 && 3371 uiop->uio_resid != saved_resid && uiop->uio_resid > 0) { 3372 mutex_exit(&so->so_lock); 3373 first = 0; 3374 pflag = opflag | MSG_NOMARK; 3375 goto retry; 3376 } 3377 so_unlock_read(so); /* Clear SOREADLOCKED */ 3378 mutex_exit(&so->so_lock); 3379 return (0); 3380 } 3381 case T_EXDATA_IND: { 3382 dprintso(so, 1, 3383 ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld " 3384 "state %s\n", 3385 so->so_oobsigcnt, so->so_oobcnt, 3386 saved_resid - uiop->uio_resid, 3387 pr_state(so->so_state, so->so_mode))); 3388 /* 3389 * kstrgetmsg handles MSGMARK so there is nothing to 3390 * inspect in the T_EXDATA_IND. 3391 * strsock_proto makes the stream head queue the T_EXDATA_IND 3392 * as a separate message with no M_DATA component. Furthermore, 3393 * the stream head does not consolidate M_DATA messages onto 3394 * an MSGMARK'ed message ensuring that the T_EXDATA_IND 3395 * remains a message by itself. This is needed since MSGMARK 3396 * marks both the whole message as well as the last byte 3397 * of the message. 3398 */ 3399 freemsg(mp); 3400 ASSERT(uiop->uio_resid == saved_resid); /* No data */ 3401 if (flags & MSG_PEEK) { 3402 /* 3403 * Even though we are peeking we consume the 3404 * T_EXDATA_IND thereby moving the mark information 3405 * to SS_RCVATMARK. Then the oob code below will 3406 * retry the peeking kstrgetmsg. 3407 * Note that the stream head read queue is 3408 * never flushed without holding SOREADLOCKED 3409 * thus the T_EXDATA_IND can not disappear 3410 * underneath us. 3411 */ 3412 dprintso(so, 1, 3413 ("sotpi_recvmsg: consume EXDATA_IND " 3414 "counts %d/%d state %s\n", 3415 so->so_oobsigcnt, 3416 so->so_oobcnt, 3417 pr_state(so->so_state, so->so_mode))); 3418 3419 pflag = MSG_ANY | MSG_DELAYERROR; 3420 if (so->so_mode & SM_ATOMIC) 3421 pflag |= MSG_DISCARDTAIL; 3422 3423 pri = 0; 3424 mp = NULL; 3425 3426 error = kstrgetmsg(SOTOV(so), &mp, uiop, 3427 &pri, &pflag, (clock_t)-1, &rval); 3428 ASSERT(uiop->uio_resid == saved_resid); 3429 3430 if (error) { 3431 #ifdef SOCK_DEBUG 3432 if (error != EWOULDBLOCK && error != EINTR) { 3433 eprintsoline(so, error); 3434 } 3435 #endif /* SOCK_DEBUG */ 3436 mutex_enter(&so->so_lock); 3437 so_unlock_read(so); /* Clear SOREADLOCKED */ 3438 mutex_exit(&so->so_lock); 3439 return (error); 3440 } 3441 ASSERT(mp); 3442 tpr = (union T_primitives *)mp->b_rptr; 3443 ASSERT(tpr->type == T_EXDATA_IND); 3444 freemsg(mp); 3445 } /* end "if (flags & MSG_PEEK)" */ 3446 3447 /* 3448 * Decrement the number of queued and pending oob. 3449 * 3450 * SS_RCVATMARK is cleared when we read past a mark. 3451 * SS_HAVEOOBDATA is cleared when we've read past the 3452 * last mark. 3453 * SS_OOBPEND is cleared if we've read past the last 3454 * mark and no (new) SIGURG has been posted. 3455 */ 3456 mutex_enter(&so->so_lock); 3457 ASSERT(so_verify_oobstate(so)); 3458 ASSERT(so->so_oobsigcnt >= so->so_oobcnt); 3459 ASSERT(so->so_oobsigcnt > 0); 3460 so->so_oobsigcnt--; 3461 ASSERT(so->so_oobcnt > 0); 3462 so->so_oobcnt--; 3463 /* 3464 * Since the T_EXDATA_IND has been removed from the stream 3465 * head, but we have not read data past the mark, 3466 * sockfs needs to track that the socket is still at the mark. 3467 * 3468 * Since no data was received call kstrgetmsg again to wait 3469 * for data. 3470 */ 3471 so->so_state |= SS_RCVATMARK; 3472 mutex_exit(&so->so_lock); 3473 dprintso(so, 1, 3474 ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n", 3475 so->so_oobsigcnt, so->so_oobcnt, 3476 pr_state(so->so_state, so->so_mode))); 3477 pflag = opflag; 3478 goto retry; 3479 } 3480 default: 3481 ASSERT(0); 3482 freemsg(mp); 3483 error = EPROTO; 3484 eprintsoline(so, error); 3485 goto err; 3486 } 3487 /* NOTREACHED */ 3488 err: 3489 mutex_enter(&so->so_lock); 3490 so_unlock_read(so); /* Clear SOREADLOCKED */ 3491 mutex_exit(&so->so_lock); 3492 return (error); 3493 } 3494 3495 /* 3496 * Sending data with options on a datagram socket. 3497 * Assumes caller has verified that SS_ISBOUND etc. are set. 3498 */ 3499 static int 3500 sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen, 3501 struct uio *uiop, void *control, t_uscalar_t controllen, int flags) 3502 { 3503 struct T_unitdata_req tudr; 3504 mblk_t *mp; 3505 int error; 3506 void *addr; 3507 socklen_t addrlen; 3508 void *src; 3509 socklen_t srclen; 3510 ssize_t len; 3511 int size; 3512 struct T_opthdr toh; 3513 struct fdbuf *fdbuf; 3514 t_uscalar_t optlen; 3515 void *fds; 3516 int fdlen; 3517 3518 ASSERT(name && namelen); 3519 ASSERT(control && controllen); 3520 3521 len = uiop->uio_resid; 3522 if (len > (ssize_t)so->so_tidu_size) { 3523 return (EMSGSIZE); 3524 } 3525 3526 /* 3527 * For AF_UNIX the destination address is translated to an internal 3528 * name and the source address is passed as an option. 3529 * Also, file descriptors are passed as file pointers in an 3530 * option. 3531 */ 3532 3533 /* 3534 * Length and family checks. 3535 */ 3536 error = so_addr_verify(so, name, namelen); 3537 if (error) { 3538 eprintsoline(so, error); 3539 return (error); 3540 } 3541 if (so->so_family == AF_UNIX) { 3542 if (so->so_state & SS_FADDR_NOXLATE) { 3543 /* 3544 * Already have a transport internal address. Do not 3545 * pass any (transport internal) source address. 3546 */ 3547 addr = name; 3548 addrlen = namelen; 3549 src = NULL; 3550 srclen = 0; 3551 } else { 3552 /* 3553 * Pass the sockaddr_un source address as an option 3554 * and translate the remote address. 3555 * 3556 * Note that this code does not prevent so_laddr_sa 3557 * from changing while it is being used. Thus 3558 * if an unbind+bind occurs concurrently with this 3559 * send the peer might see a partially new and a 3560 * partially old "from" address. 3561 */ 3562 src = so->so_laddr_sa; 3563 srclen = (t_uscalar_t)so->so_laddr_len; 3564 dprintso(so, 1, 3565 ("sosend_dgramcmsg UNIX: srclen %d, src %p\n", 3566 srclen, src)); 3567 error = so_ux_addr_xlate(so, name, namelen, 3568 (flags & MSG_XPG4_2), 3569 &addr, &addrlen); 3570 if (error) { 3571 eprintsoline(so, error); 3572 return (error); 3573 } 3574 } 3575 } else { 3576 addr = name; 3577 addrlen = namelen; 3578 src = NULL; 3579 srclen = 0; 3580 } 3581 optlen = so_optlen(control, controllen, 3582 !(flags & MSG_XPG4_2)); 3583 tudr.PRIM_type = T_UNITDATA_REQ; 3584 tudr.DEST_length = addrlen; 3585 tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 3586 if (srclen != 0) 3587 tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) + 3588 _TPI_ALIGN_TOPT(srclen)); 3589 else 3590 tudr.OPT_length = optlen; 3591 tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 3592 _TPI_ALIGN_TOPT(addrlen)); 3593 3594 size = tudr.OPT_offset + tudr.OPT_length; 3595 3596 /* 3597 * File descriptors only when SM_FDPASSING set. 3598 */ 3599 error = so_getfdopt(control, controllen, 3600 !(flags & MSG_XPG4_2), &fds, &fdlen); 3601 if (error) 3602 return (error); 3603 if (fdlen != -1) { 3604 if (!(so->so_mode & SM_FDPASSING)) 3605 return (EOPNOTSUPP); 3606 3607 error = fdbuf_create(fds, fdlen, &fdbuf); 3608 if (error) 3609 return (error); 3610 mp = fdbuf_allocmsg(size, fdbuf); 3611 } else { 3612 mp = soallocproto(size, _ALLOC_INTR); 3613 if (mp == NULL) { 3614 /* 3615 * Caught a signal waiting for memory. 3616 * Let send* return EINTR. 3617 */ 3618 return (EINTR); 3619 } 3620 } 3621 soappendmsg(mp, &tudr, sizeof (tudr)); 3622 soappendmsg(mp, addr, addrlen); 3623 mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 3624 3625 if (fdlen != -1) { 3626 ASSERT(fdbuf != NULL); 3627 toh.level = SOL_SOCKET; 3628 toh.name = SO_FILEP; 3629 toh.len = fdbuf->fd_size + 3630 (t_uscalar_t)sizeof (struct T_opthdr); 3631 toh.status = 0; 3632 soappendmsg(mp, &toh, sizeof (toh)); 3633 soappendmsg(mp, fdbuf, fdbuf->fd_size); 3634 ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 3635 } 3636 if (srclen != 0) { 3637 /* 3638 * There is a AF_UNIX sockaddr_un to include as a source 3639 * address option. 3640 */ 3641 toh.level = SOL_SOCKET; 3642 toh.name = SO_SRCADDR; 3643 toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 3644 toh.status = 0; 3645 soappendmsg(mp, &toh, sizeof (toh)); 3646 soappendmsg(mp, src, srclen); 3647 mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 3648 ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 3649 } 3650 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 3651 so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp); 3652 /* At most 3 bytes left in the message */ 3653 ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE)); 3654 ASSERT(MBLKL(mp) <= (ssize_t)size); 3655 3656 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 3657 #ifdef C2_AUDIT 3658 if (audit_active) 3659 audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 3660 #endif /* C2_AUDIT */ 3661 3662 error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 3663 #ifdef SOCK_DEBUG 3664 if (error) { 3665 eprintsoline(so, error); 3666 } 3667 #endif /* SOCK_DEBUG */ 3668 return (error); 3669 } 3670 3671 /* 3672 * Sending data with options on a connected stream socket. 3673 * Assumes caller has verified that SS_ISCONNECTED is set. 3674 */ 3675 static int 3676 sosend_svccmsg(struct sonode *so, 3677 struct uio *uiop, 3678 int more, 3679 void *control, 3680 t_uscalar_t controllen, 3681 int flags) 3682 { 3683 struct T_optdata_req tdr; 3684 mblk_t *mp; 3685 int error; 3686 ssize_t iosize; 3687 int first = 1; 3688 int size; 3689 struct fdbuf *fdbuf; 3690 t_uscalar_t optlen; 3691 void *fds; 3692 int fdlen; 3693 struct T_opthdr toh; 3694 3695 dprintso(so, 1, 3696 ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid)); 3697 3698 /* 3699 * Has to be bound and connected. However, since no locks are 3700 * held the state could have changed after sotpi_sendmsg checked it 3701 * thus it is not possible to ASSERT on the state. 3702 */ 3703 3704 /* Options on connection-oriented only when SM_OPTDATA set. */ 3705 if (!(so->so_mode & SM_OPTDATA)) 3706 return (EOPNOTSUPP); 3707 3708 do { 3709 /* 3710 * Set the MORE flag if uio_resid does not fit in this 3711 * message or if the caller passed in "more". 3712 * Error for transports with zero tidu_size. 3713 */ 3714 tdr.PRIM_type = T_OPTDATA_REQ; 3715 iosize = so->so_tidu_size; 3716 if (iosize <= 0) 3717 return (EMSGSIZE); 3718 if (uiop->uio_resid > iosize) { 3719 tdr.DATA_flag = 1; 3720 } else { 3721 if (more) 3722 tdr.DATA_flag = 1; 3723 else 3724 tdr.DATA_flag = 0; 3725 iosize = uiop->uio_resid; 3726 } 3727 dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n", 3728 tdr.DATA_flag, iosize)); 3729 3730 optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2)); 3731 tdr.OPT_length = optlen; 3732 tdr.OPT_offset = (t_scalar_t)sizeof (tdr); 3733 3734 size = (int)sizeof (tdr) + optlen; 3735 /* 3736 * File descriptors only when SM_FDPASSING set. 3737 */ 3738 error = so_getfdopt(control, controllen, 3739 !(flags & MSG_XPG4_2), &fds, &fdlen); 3740 if (error) 3741 return (error); 3742 if (fdlen != -1) { 3743 if (!(so->so_mode & SM_FDPASSING)) 3744 return (EOPNOTSUPP); 3745 3746 error = fdbuf_create(fds, fdlen, &fdbuf); 3747 if (error) 3748 return (error); 3749 mp = fdbuf_allocmsg(size, fdbuf); 3750 } else { 3751 mp = soallocproto(size, _ALLOC_INTR); 3752 if (mp == NULL) { 3753 /* 3754 * Caught a signal waiting for memory. 3755 * Let send* return EINTR. 3756 */ 3757 return (first ? EINTR : 0); 3758 } 3759 } 3760 soappendmsg(mp, &tdr, sizeof (tdr)); 3761 3762 if (fdlen != -1) { 3763 ASSERT(fdbuf != NULL); 3764 toh.level = SOL_SOCKET; 3765 toh.name = SO_FILEP; 3766 toh.len = fdbuf->fd_size + 3767 (t_uscalar_t)sizeof (struct T_opthdr); 3768 toh.status = 0; 3769 soappendmsg(mp, &toh, sizeof (toh)); 3770 soappendmsg(mp, fdbuf, fdbuf->fd_size); 3771 ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr)); 3772 } 3773 so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp); 3774 /* At most 3 bytes left in the message */ 3775 ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE)); 3776 ASSERT(MBLKL(mp) <= (ssize_t)size); 3777 3778 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 3779 3780 error = kstrputmsg(SOTOV(so), mp, uiop, iosize, 3781 0, MSG_BAND, 0); 3782 if (error) { 3783 if (!first && error == EWOULDBLOCK) 3784 return (0); 3785 eprintsoline(so, error); 3786 return (error); 3787 } 3788 control = NULL; 3789 first = 0; 3790 if (uiop->uio_resid > 0) { 3791 /* 3792 * Recheck for fatal errors. Fail write even though 3793 * some data have been written. This is consistent 3794 * with strwrite semantics and BSD sockets semantics. 3795 */ 3796 if (so->so_state & SS_CANTSENDMORE) { 3797 tsignal(curthread, SIGPIPE); 3798 eprintsoline(so, error); 3799 return (EPIPE); 3800 } 3801 if (so->so_error != 0) { 3802 mutex_enter(&so->so_lock); 3803 error = sogeterr(so); 3804 mutex_exit(&so->so_lock); 3805 if (error != 0) { 3806 eprintsoline(so, error); 3807 return (error); 3808 } 3809 } 3810 } 3811 } while (uiop->uio_resid > 0); 3812 return (0); 3813 } 3814 3815 /* 3816 * Sending data on a datagram socket. 3817 * Assumes caller has verified that SS_ISBOUND etc. are set. 3818 * 3819 * For AF_UNIX the destination address is translated to an internal 3820 * name and the source address is passed as an option. 3821 */ 3822 int 3823 sosend_dgram(struct sonode *so, struct sockaddr *name, socklen_t namelen, 3824 struct uio *uiop, int flags) 3825 { 3826 struct T_unitdata_req tudr; 3827 mblk_t *mp; 3828 int error; 3829 void *addr; 3830 socklen_t addrlen; 3831 void *src; 3832 socklen_t srclen; 3833 ssize_t len; 3834 3835 ASSERT(name != NULL && namelen != 0); 3836 3837 len = uiop->uio_resid; 3838 if (len > so->so_tidu_size) { 3839 error = EMSGSIZE; 3840 goto done; 3841 } 3842 3843 /* Length and family checks */ 3844 error = so_addr_verify(so, name, namelen); 3845 if (error != 0) 3846 goto done; 3847 3848 if (so->so_state & SS_DIRECT) 3849 return (sodgram_direct(so, name, namelen, uiop, flags)); 3850 3851 if (so->so_family == AF_UNIX) { 3852 if (so->so_state & SS_FADDR_NOXLATE) { 3853 /* 3854 * Already have a transport internal address. Do not 3855 * pass any (transport internal) source address. 3856 */ 3857 addr = name; 3858 addrlen = namelen; 3859 src = NULL; 3860 srclen = 0; 3861 } else { 3862 /* 3863 * Pass the sockaddr_un source address as an option 3864 * and translate the remote address. 3865 * 3866 * Note that this code does not prevent so_laddr_sa 3867 * from changing while it is being used. Thus 3868 * if an unbind+bind occurs concurrently with this 3869 * send the peer might see a partially new and a 3870 * partially old "from" address. 3871 */ 3872 src = so->so_laddr_sa; 3873 srclen = (socklen_t)so->so_laddr_len; 3874 dprintso(so, 1, 3875 ("sosend_dgram UNIX: srclen %d, src %p\n", 3876 srclen, src)); 3877 error = so_ux_addr_xlate(so, name, namelen, 3878 (flags & MSG_XPG4_2), 3879 &addr, &addrlen); 3880 if (error) { 3881 eprintsoline(so, error); 3882 goto done; 3883 } 3884 } 3885 } else { 3886 addr = name; 3887 addrlen = namelen; 3888 src = NULL; 3889 srclen = 0; 3890 } 3891 tudr.PRIM_type = T_UNITDATA_REQ; 3892 tudr.DEST_length = addrlen; 3893 tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 3894 if (srclen == 0) { 3895 tudr.OPT_length = 0; 3896 tudr.OPT_offset = 0; 3897 3898 mp = soallocproto2(&tudr, sizeof (tudr), 3899 addr, addrlen, 0, _ALLOC_INTR); 3900 if (mp == NULL) { 3901 /* 3902 * Caught a signal waiting for memory. 3903 * Let send* return EINTR. 3904 */ 3905 error = EINTR; 3906 goto done; 3907 } 3908 } else { 3909 /* 3910 * There is a AF_UNIX sockaddr_un to include as a source 3911 * address option. 3912 */ 3913 struct T_opthdr toh; 3914 ssize_t size; 3915 3916 tudr.OPT_length = (t_scalar_t)(sizeof (toh) + 3917 _TPI_ALIGN_TOPT(srclen)); 3918 tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) + 3919 _TPI_ALIGN_TOPT(addrlen)); 3920 3921 toh.level = SOL_SOCKET; 3922 toh.name = SO_SRCADDR; 3923 toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr)); 3924 toh.status = 0; 3925 3926 size = tudr.OPT_offset + tudr.OPT_length; 3927 mp = soallocproto2(&tudr, sizeof (tudr), 3928 addr, addrlen, size, _ALLOC_INTR); 3929 if (mp == NULL) { 3930 /* 3931 * Caught a signal waiting for memory. 3932 * Let send* return EINTR. 3933 */ 3934 error = EINTR; 3935 goto done; 3936 } 3937 mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen; 3938 soappendmsg(mp, &toh, sizeof (toh)); 3939 soappendmsg(mp, src, srclen); 3940 mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen; 3941 ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 3942 } 3943 3944 #ifdef C2_AUDIT 3945 if (audit_active) 3946 audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 3947 #endif /* C2_AUDIT */ 3948 3949 error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 3950 done: 3951 #ifdef SOCK_DEBUG 3952 if (error) { 3953 eprintsoline(so, error); 3954 } 3955 #endif /* SOCK_DEBUG */ 3956 return (error); 3957 } 3958 3959 /* 3960 * Sending data on a connected stream socket. 3961 * Assumes caller has verified that SS_ISCONNECTED is set. 3962 */ 3963 int 3964 sosend_svc(struct sonode *so, 3965 struct uio *uiop, 3966 t_scalar_t prim, 3967 int more, 3968 int sflag) 3969 { 3970 struct T_data_req tdr; 3971 mblk_t *mp; 3972 int error; 3973 ssize_t iosize; 3974 int first = 1; 3975 3976 dprintso(so, 1, 3977 ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n", 3978 so, uiop->uio_resid, prim, sflag)); 3979 3980 /* 3981 * Has to be bound and connected. However, since no locks are 3982 * held the state could have changed after sotpi_sendmsg checked it 3983 * thus it is not possible to ASSERT on the state. 3984 */ 3985 3986 do { 3987 /* 3988 * Set the MORE flag if uio_resid does not fit in this 3989 * message or if the caller passed in "more". 3990 * Error for transports with zero tidu_size. 3991 */ 3992 tdr.PRIM_type = prim; 3993 iosize = so->so_tidu_size; 3994 if (iosize <= 0) 3995 return (EMSGSIZE); 3996 if (uiop->uio_resid > iosize) { 3997 tdr.MORE_flag = 1; 3998 } else { 3999 if (more) 4000 tdr.MORE_flag = 1; 4001 else 4002 tdr.MORE_flag = 0; 4003 iosize = uiop->uio_resid; 4004 } 4005 dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n", 4006 prim, tdr.MORE_flag, iosize)); 4007 mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR); 4008 if (mp == NULL) { 4009 /* 4010 * Caught a signal waiting for memory. 4011 * Let send* return EINTR. 4012 */ 4013 if (first) 4014 return (EINTR); 4015 else 4016 return (0); 4017 } 4018 4019 error = kstrputmsg(SOTOV(so), mp, uiop, iosize, 4020 0, sflag | MSG_BAND, 0); 4021 if (error) { 4022 if (!first && error == EWOULDBLOCK) 4023 return (0); 4024 eprintsoline(so, error); 4025 return (error); 4026 } 4027 first = 0; 4028 if (uiop->uio_resid > 0) { 4029 /* 4030 * Recheck for fatal errors. Fail write even though 4031 * some data have been written. This is consistent 4032 * with strwrite semantics and BSD sockets semantics. 4033 */ 4034 if (so->so_state & SS_CANTSENDMORE) { 4035 tsignal(curthread, SIGPIPE); 4036 eprintsoline(so, error); 4037 return (EPIPE); 4038 } 4039 if (so->so_error != 0) { 4040 mutex_enter(&so->so_lock); 4041 error = sogeterr(so); 4042 mutex_exit(&so->so_lock); 4043 if (error != 0) { 4044 eprintsoline(so, error); 4045 return (error); 4046 } 4047 } 4048 } 4049 } while (uiop->uio_resid > 0); 4050 return (0); 4051 } 4052 4053 /* 4054 * Check the state for errors and call the appropriate send function. 4055 * 4056 * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set) 4057 * this function issues a setsockopt to toggle SO_DONTROUTE before and 4058 * after sending the message. 4059 */ 4060 static int 4061 sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop) 4062 { 4063 int so_state; 4064 int so_mode; 4065 int error; 4066 struct sockaddr *name; 4067 t_uscalar_t namelen; 4068 int dontroute; 4069 int flags; 4070 4071 dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n", 4072 so, msg, msg->msg_flags, 4073 pr_state(so->so_state, so->so_mode), so->so_error)); 4074 4075 mutex_enter(&so->so_lock); 4076 so_state = so->so_state; 4077 4078 if (so_state & SS_CANTSENDMORE) { 4079 mutex_exit(&so->so_lock); 4080 tsignal(curthread, SIGPIPE); 4081 return (EPIPE); 4082 } 4083 4084 if (so->so_error != 0) { 4085 error = sogeterr(so); 4086 if (error != 0) { 4087 mutex_exit(&so->so_lock); 4088 return (error); 4089 } 4090 } 4091 4092 name = (struct sockaddr *)msg->msg_name; 4093 namelen = msg->msg_namelen; 4094 4095 so_mode = so->so_mode; 4096 4097 if (name == NULL) { 4098 if (!(so_state & SS_ISCONNECTED)) { 4099 mutex_exit(&so->so_lock); 4100 if (so_mode & SM_CONNREQUIRED) 4101 return (ENOTCONN); 4102 else 4103 return (EDESTADDRREQ); 4104 } 4105 if (so_mode & SM_CONNREQUIRED) { 4106 name = NULL; 4107 namelen = 0; 4108 } else { 4109 /* 4110 * Note that this code does not prevent so_faddr_sa 4111 * from changing while it is being used. Thus 4112 * if an "unconnect"+connect occurs concurrently with 4113 * this send the datagram might be delivered to a 4114 * garbaled address. 4115 */ 4116 ASSERT(so->so_faddr_sa); 4117 name = so->so_faddr_sa; 4118 namelen = (t_uscalar_t)so->so_faddr_len; 4119 } 4120 } else { 4121 if (!(so_state & SS_ISCONNECTED) && 4122 (so_mode & SM_CONNREQUIRED)) { 4123 /* Required but not connected */ 4124 mutex_exit(&so->so_lock); 4125 return (ENOTCONN); 4126 } 4127 /* 4128 * Ignore the address on connection-oriented sockets. 4129 * Just like BSD this code does not generate an error for 4130 * TCP (a CONNREQUIRED socket) when sending to an address 4131 * passed in with sendto/sendmsg. Instead the data is 4132 * delivered on the connection as if no address had been 4133 * supplied. 4134 */ 4135 if ((so_state & SS_ISCONNECTED) && 4136 !(so_mode & SM_CONNREQUIRED)) { 4137 mutex_exit(&so->so_lock); 4138 return (EISCONN); 4139 } 4140 if (!(so_state & SS_ISBOUND)) { 4141 so_lock_single(so); /* Set SOLOCKED */ 4142 error = sotpi_bind(so, NULL, 0, 4143 _SOBIND_UNSPEC|_SOBIND_LOCK_HELD); 4144 so_unlock_single(so, SOLOCKED); 4145 if (error) { 4146 mutex_exit(&so->so_lock); 4147 eprintsoline(so, error); 4148 return (error); 4149 } 4150 } 4151 /* 4152 * Handle delayed datagram errors. These are only queued 4153 * when the application sets SO_DGRAM_ERRIND. 4154 * Return the error if we are sending to the address 4155 * that was returned in the last T_UDERROR_IND. 4156 * If sending to some other address discard the delayed 4157 * error indication. 4158 */ 4159 if (so->so_delayed_error) { 4160 struct T_uderror_ind *tudi; 4161 void *addr; 4162 t_uscalar_t addrlen; 4163 boolean_t match = B_FALSE; 4164 4165 ASSERT(so->so_eaddr_mp); 4166 error = so->so_delayed_error; 4167 so->so_delayed_error = 0; 4168 tudi = (struct T_uderror_ind *)so->so_eaddr_mp->b_rptr; 4169 addrlen = tudi->DEST_length; 4170 addr = sogetoff(so->so_eaddr_mp, 4171 tudi->DEST_offset, 4172 addrlen, 1); 4173 ASSERT(addr); /* Checked by strsock_proto */ 4174 switch (so->so_family) { 4175 case AF_INET: { 4176 /* Compare just IP address and port */ 4177 sin_t *sin1 = (sin_t *)name; 4178 sin_t *sin2 = (sin_t *)addr; 4179 4180 if (addrlen == sizeof (sin_t) && 4181 namelen == addrlen && 4182 sin1->sin_port == sin2->sin_port && 4183 sin1->sin_addr.s_addr == 4184 sin2->sin_addr.s_addr) 4185 match = B_TRUE; 4186 break; 4187 } 4188 case AF_INET6: { 4189 /* Compare just IP address and port. Not flow */ 4190 sin6_t *sin1 = (sin6_t *)name; 4191 sin6_t *sin2 = (sin6_t *)addr; 4192 4193 if (addrlen == sizeof (sin6_t) && 4194 namelen == addrlen && 4195 sin1->sin6_port == sin2->sin6_port && 4196 IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr, 4197 &sin2->sin6_addr)) 4198 match = B_TRUE; 4199 break; 4200 } 4201 case AF_UNIX: 4202 default: 4203 if (namelen == addrlen && 4204 bcmp(name, addr, namelen) == 0) 4205 match = B_TRUE; 4206 } 4207 if (match) { 4208 freemsg(so->so_eaddr_mp); 4209 so->so_eaddr_mp = NULL; 4210 mutex_exit(&so->so_lock); 4211 #ifdef DEBUG 4212 dprintso(so, 0, 4213 ("sockfs delayed error %d for %s\n", 4214 error, 4215 pr_addr(so->so_family, name, namelen))); 4216 #endif /* DEBUG */ 4217 return (error); 4218 } 4219 freemsg(so->so_eaddr_mp); 4220 so->so_eaddr_mp = NULL; 4221 } 4222 } 4223 mutex_exit(&so->so_lock); 4224 4225 flags = msg->msg_flags; 4226 dontroute = 0; 4227 if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) { 4228 uint32_t val; 4229 4230 val = 1; 4231 error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE, 4232 &val, (t_uscalar_t)sizeof (val)); 4233 if (error) 4234 return (error); 4235 dontroute = 1; 4236 } 4237 4238 if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) { 4239 error = EOPNOTSUPP; 4240 goto done; 4241 } 4242 if (msg->msg_controllen != 0) { 4243 if (!(so_mode & SM_CONNREQUIRED)) { 4244 error = sosend_dgramcmsg(so, name, namelen, uiop, 4245 msg->msg_control, msg->msg_controllen, flags); 4246 } else { 4247 if (flags & MSG_OOB) { 4248 /* Can't generate T_EXDATA_REQ with options */ 4249 error = EOPNOTSUPP; 4250 goto done; 4251 } 4252 error = sosend_svccmsg(so, uiop, 4253 !(flags & MSG_EOR), 4254 msg->msg_control, msg->msg_controllen, 4255 flags); 4256 } 4257 goto done; 4258 } 4259 4260 if (!(so_mode & SM_CONNREQUIRED)) { 4261 /* 4262 * If there is no SO_DONTROUTE to turn off return immediately 4263 * from send_dgram. This can allow tail-call optimizations. 4264 */ 4265 if (!dontroute) { 4266 return (sosend_dgram(so, name, namelen, uiop, flags)); 4267 } 4268 error = sosend_dgram(so, name, namelen, uiop, flags); 4269 } else { 4270 t_scalar_t prim; 4271 int sflag; 4272 4273 /* Ignore msg_name in the connected state */ 4274 if (flags & MSG_OOB) { 4275 prim = T_EXDATA_REQ; 4276 /* 4277 * Send down T_EXDATA_REQ even if there is flow 4278 * control for data. 4279 */ 4280 sflag = MSG_IGNFLOW; 4281 } else { 4282 if (so_mode & SM_BYTESTREAM) { 4283 /* Byte stream transport - use write */ 4284 4285 dprintso(so, 1, ("sotpi_sendmsg: write\n")); 4286 /* 4287 * If there is no SO_DONTROUTE to turn off, 4288 * SS_DIRECT is on, and there is no flow 4289 * control, we can take the fast path. 4290 */ 4291 if (!dontroute && 4292 (so_state & SS_DIRECT) && 4293 canputnext(SOTOV(so)->v_stream->sd_wrq)) { 4294 return (sostream_direct(so, uiop, 4295 NULL, CRED())); 4296 } 4297 error = strwrite(SOTOV(so), uiop, CRED()); 4298 goto done; 4299 } 4300 prim = T_DATA_REQ; 4301 sflag = 0; 4302 } 4303 /* 4304 * If there is no SO_DONTROUTE to turn off return immediately 4305 * from sosend_svc. This can allow tail-call optimizations. 4306 */ 4307 if (!dontroute) 4308 return (sosend_svc(so, uiop, prim, 4309 !(flags & MSG_EOR), sflag)); 4310 error = sosend_svc(so, uiop, prim, 4311 !(flags & MSG_EOR), sflag); 4312 } 4313 ASSERT(dontroute); 4314 done: 4315 if (dontroute) { 4316 uint32_t val; 4317 4318 val = 0; 4319 (void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE, 4320 &val, (t_uscalar_t)sizeof (val)); 4321 } 4322 return (error); 4323 } 4324 4325 /* 4326 * Sending data on a datagram socket. 4327 * Assumes caller has verified that SS_ISBOUND etc. are set. 4328 */ 4329 /* ARGSUSED */ 4330 static int 4331 sodgram_direct(struct sonode *so, struct sockaddr *name, 4332 socklen_t namelen, struct uio *uiop, int flags) 4333 { 4334 struct T_unitdata_req tudr; 4335 mblk_t *mp; 4336 int error = 0; 4337 void *addr; 4338 socklen_t addrlen; 4339 ssize_t len; 4340 struct stdata *stp = SOTOV(so)->v_stream; 4341 int so_state; 4342 queue_t *udp_wq; 4343 4344 ASSERT(name != NULL && namelen != 0); 4345 ASSERT(!(so->so_mode & SM_CONNREQUIRED)); 4346 ASSERT(!(so->so_mode & SM_EXDATA)); 4347 ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6); 4348 ASSERT(SOTOV(so)->v_type == VSOCK); 4349 4350 /* Caller checked for proper length */ 4351 len = uiop->uio_resid; 4352 ASSERT(len <= so->so_tidu_size); 4353 4354 /* Length and family checks have been done by caller */ 4355 ASSERT(name->sa_family == so->so_family); 4356 ASSERT(so->so_family == AF_INET || 4357 (namelen == (socklen_t)sizeof (struct sockaddr_in6))); 4358 ASSERT(so->so_family == AF_INET6 || 4359 (namelen == (socklen_t)sizeof (struct sockaddr_in))); 4360 4361 addr = name; 4362 addrlen = namelen; 4363 4364 if (stp->sd_sidp != NULL && 4365 (error = straccess(stp, JCWRITE)) != 0) 4366 goto done; 4367 4368 so_state = so->so_state; 4369 4370 /* 4371 * For UDP we don't break up the copyin into smaller pieces 4372 * as in the TCP case. That means if ENOMEM is returned by 4373 * mcopyinuio() then the uio vector has not been modified at 4374 * all and we fallback to either strwrite() or kstrputmsg() 4375 * below. Note also that we never generate priority messages 4376 * from here. 4377 */ 4378 udp_wq = stp->sd_wrq->q_next; 4379 if (canput(udp_wq) && 4380 (mp = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) { 4381 ASSERT(DB_TYPE(mp) == M_DATA); 4382 ASSERT(uiop->uio_resid == 0); 4383 #ifdef C2_AUDIT 4384 if (audit_active) 4385 audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 4386 #endif /* C2_AUDIT */ 4387 udp_wput_data(udp_wq, mp, addr, addrlen); 4388 return (0); 4389 } 4390 if (error != 0 && error != ENOMEM) 4391 return (error); 4392 4393 /* 4394 * For connected, let strwrite() handle the blocking case. 4395 * Otherwise we fall thru and use kstrputmsg(). 4396 */ 4397 if (so_state & SS_ISCONNECTED) 4398 return (strwrite(SOTOV(so), uiop, CRED())); 4399 4400 tudr.PRIM_type = T_UNITDATA_REQ; 4401 tudr.DEST_length = addrlen; 4402 tudr.DEST_offset = (t_scalar_t)sizeof (tudr); 4403 tudr.OPT_length = 0; 4404 tudr.OPT_offset = 0; 4405 4406 mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0, _ALLOC_INTR); 4407 if (mp == NULL) { 4408 /* 4409 * Caught a signal waiting for memory. 4410 * Let send* return EINTR. 4411 */ 4412 error = EINTR; 4413 goto done; 4414 } 4415 4416 #ifdef C2_AUDIT 4417 if (audit_active) 4418 audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0); 4419 #endif /* C2_AUDIT */ 4420 4421 error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0); 4422 done: 4423 #ifdef SOCK_DEBUG 4424 if (error != 0) { 4425 eprintsoline(so, error); 4426 } 4427 #endif /* SOCK_DEBUG */ 4428 return (error); 4429 } 4430 4431 int 4432 sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr) 4433 { 4434 struct stdata *stp = SOTOV(so)->v_stream; 4435 ssize_t iosize, rmax, maxblk; 4436 queue_t *tcp_wq = stp->sd_wrq->q_next; 4437 mblk_t *newmp; 4438 int error = 0, wflag = 0; 4439 4440 ASSERT(so->so_mode & SM_BYTESTREAM); 4441 ASSERT(SOTOV(so)->v_type == VSOCK); 4442 4443 if (stp->sd_sidp != NULL && 4444 (error = straccess(stp, JCWRITE)) != 0) 4445 return (error); 4446 4447 if (uiop == NULL) { 4448 /* 4449 * kstrwritemp() should have checked sd_flag and 4450 * flow-control before coming here. If we end up 4451 * here it means that we can simply pass down the 4452 * data to tcp. 4453 */ 4454 ASSERT(mp != NULL); 4455 if (stp->sd_wputdatafunc != NULL) { 4456 newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL, 4457 NULL, NULL, NULL); 4458 if (newmp == NULL) { 4459 /* The caller will free mp */ 4460 return (ECOMM); 4461 } 4462 mp = newmp; 4463 } 4464 tcp_wput(tcp_wq, mp); 4465 return (0); 4466 } 4467 4468 /* Fallback to strwrite() to do proper error handling */ 4469 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY)) 4470 return (strwrite(SOTOV(so), uiop, cr)); 4471 4472 rmax = stp->sd_qn_maxpsz; 4473 ASSERT(rmax >= 0 || rmax == INFPSZ); 4474 if (rmax == 0 || uiop->uio_resid <= 0) 4475 return (0); 4476 4477 if (rmax == INFPSZ) 4478 rmax = uiop->uio_resid; 4479 4480 maxblk = stp->sd_maxblk; 4481 4482 for (;;) { 4483 iosize = MIN(uiop->uio_resid, rmax); 4484 4485 mp = mcopyinuio(stp, uiop, iosize, maxblk, &error); 4486 if (mp == NULL) { 4487 /* 4488 * Fallback to strwrite() for ENOMEM; if this 4489 * is our first time in this routine and the uio 4490 * vector has not been modified, we will end up 4491 * calling strwrite() without any flag set. 4492 */ 4493 if (error == ENOMEM) 4494 goto slow_send; 4495 else 4496 return (error); 4497 } 4498 ASSERT(uiop->uio_resid >= 0); 4499 /* 4500 * If mp is non-NULL and ENOMEM is set, it means that 4501 * mcopyinuio() was able to break down some of the user 4502 * data into one or more mblks. Send the partial data 4503 * to tcp and let the rest be handled in strwrite(). 4504 */ 4505 ASSERT(error == 0 || error == ENOMEM); 4506 if (stp->sd_wputdatafunc != NULL) { 4507 newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL, 4508 NULL, NULL, NULL); 4509 if (newmp == NULL) { 4510 /* The caller will free mp */ 4511 return (ECOMM); 4512 } 4513 mp = newmp; 4514 } 4515 tcp_wput(tcp_wq, mp); 4516 4517 wflag |= NOINTR; 4518 4519 if (uiop->uio_resid == 0) { /* No more data; we're done */ 4520 ASSERT(error == 0); 4521 break; 4522 } else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag & 4523 (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) { 4524 slow_send: 4525 /* 4526 * We were able to send down partial data using 4527 * the direct call interface, but are now relying 4528 * on strwrite() to handle the non-fastpath cases. 4529 * If the socket is blocking we will sleep in 4530 * strwaitq() until write is permitted, otherwise, 4531 * we will need to return the amount of bytes 4532 * written so far back to the app. This is the 4533 * reason why we pass NOINTR flag to strwrite() 4534 * for non-blocking socket, because we don't want 4535 * to return EAGAIN when portion of the user data 4536 * has actually been sent down. 4537 */ 4538 return (strwrite_common(SOTOV(so), uiop, cr, wflag)); 4539 } 4540 } 4541 return (0); 4542 } 4543 4544 /* 4545 * Update so_faddr by asking the transport (unless AF_UNIX). 4546 */ 4547 int 4548 sotpi_getpeername(struct sonode *so) 4549 { 4550 struct strbuf strbuf; 4551 int error = 0, res; 4552 void *addr; 4553 t_uscalar_t addrlen; 4554 k_sigset_t smask; 4555 4556 dprintso(so, 1, ("sotpi_getpeername(%p) %s\n", 4557 so, pr_state(so->so_state, so->so_mode))); 4558 4559 mutex_enter(&so->so_lock); 4560 so_lock_single(so); /* Set SOLOCKED */ 4561 if (!(so->so_state & SS_ISCONNECTED)) { 4562 error = ENOTCONN; 4563 goto done; 4564 } 4565 /* Added this check for X/Open */ 4566 if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 4567 error = EINVAL; 4568 if (xnet_check_print) { 4569 printf("sockfs: X/Open getpeername check => EINVAL\n"); 4570 } 4571 goto done; 4572 } 4573 #ifdef DEBUG 4574 dprintso(so, 1, ("sotpi_getpeername (local): %s\n", 4575 pr_addr(so->so_family, so->so_faddr_sa, 4576 (t_uscalar_t)so->so_faddr_len))); 4577 #endif /* DEBUG */ 4578 4579 if (so->so_family == AF_UNIX) { 4580 /* Transport has different name space - return local info */ 4581 error = 0; 4582 goto done; 4583 } 4584 4585 ASSERT(so->so_faddr_sa); 4586 /* Allocate local buffer to use with ioctl */ 4587 addrlen = (t_uscalar_t)so->so_faddr_maxlen; 4588 mutex_exit(&so->so_lock); 4589 addr = kmem_alloc(addrlen, KM_SLEEP); 4590 4591 /* 4592 * Issue TI_GETPEERNAME with signals masked. 4593 * Put the result in so_faddr_sa so that getpeername works after 4594 * a shutdown(output). 4595 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted 4596 * back to the socket. 4597 */ 4598 strbuf.buf = addr; 4599 strbuf.maxlen = addrlen; 4600 strbuf.len = 0; 4601 4602 sigintr(&smask, 0); 4603 res = 0; 4604 ASSERT(CRED()); 4605 error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf, 4606 0, K_TO_K, CRED(), &res); 4607 sigunintr(&smask); 4608 4609 mutex_enter(&so->so_lock); 4610 /* 4611 * If there is an error record the error in so_error put don't fail 4612 * the getpeername. Instead fallback on the recorded 4613 * so->so_faddr_sa. 4614 */ 4615 if (error) { 4616 /* 4617 * Various stream head errors can be returned to the ioctl. 4618 * However, it is impossible to determine which ones of 4619 * these are really socket level errors that were incorrectly 4620 * consumed by the ioctl. Thus this code silently ignores the 4621 * error - to code explicitly does not reinstate the error 4622 * using soseterror(). 4623 * Experiments have shows that at least this set of 4624 * errors are reported and should not be reinstated on the 4625 * socket: 4626 * EINVAL E.g. if an I_LINK was in effect when 4627 * getpeername was called. 4628 * EPIPE The ioctl error semantics prefer the write 4629 * side error over the read side error. 4630 * ENOTCONN The transport just got disconnected but 4631 * sockfs had not yet seen the T_DISCON_IND 4632 * when issuing the ioctl. 4633 */ 4634 error = 0; 4635 } else if (res == 0 && strbuf.len > 0 && 4636 (so->so_state & SS_ISCONNECTED)) { 4637 ASSERT(strbuf.len <= (int)so->so_faddr_maxlen); 4638 so->so_faddr_len = (socklen_t)strbuf.len; 4639 bcopy(addr, so->so_faddr_sa, so->so_faddr_len); 4640 so->so_state |= SS_FADDR_VALID; 4641 } 4642 kmem_free(addr, addrlen); 4643 #ifdef DEBUG 4644 dprintso(so, 1, ("sotpi_getpeername (tp): %s\n", 4645 pr_addr(so->so_family, so->so_faddr_sa, 4646 (t_uscalar_t)so->so_faddr_len))); 4647 #endif /* DEBUG */ 4648 done: 4649 so_unlock_single(so, SOLOCKED); 4650 mutex_exit(&so->so_lock); 4651 return (error); 4652 } 4653 4654 /* 4655 * Update so_laddr by asking the transport (unless AF_UNIX). 4656 */ 4657 int 4658 sotpi_getsockname(struct sonode *so) 4659 { 4660 struct strbuf strbuf; 4661 int error = 0, res; 4662 void *addr; 4663 t_uscalar_t addrlen; 4664 k_sigset_t smask; 4665 4666 dprintso(so, 1, ("sotpi_getsockname(%p) %s\n", 4667 so, pr_state(so->so_state, so->so_mode))); 4668 4669 mutex_enter(&so->so_lock); 4670 so_lock_single(so); /* Set SOLOCKED */ 4671 if (!(so->so_state & SS_ISBOUND) && so->so_family != AF_UNIX) { 4672 /* Return an all zero address except for the family */ 4673 if (so->so_family == AF_INET) 4674 so->so_laddr_len = (socklen_t)sizeof (sin_t); 4675 else if (so->so_family == AF_INET6) 4676 so->so_laddr_len = (socklen_t)sizeof (sin6_t); 4677 ASSERT(so->so_laddr_len <= so->so_laddr_maxlen); 4678 bzero(so->so_laddr_sa, so->so_laddr_len); 4679 /* 4680 * Can not assume there is a sa_family for all 4681 * protocol families. 4682 */ 4683 if (so->so_family == AF_INET || so->so_family == AF_INET6) 4684 so->so_laddr_sa->sa_family = so->so_family; 4685 } 4686 #ifdef DEBUG 4687 dprintso(so, 1, ("sotpi_getsockname (local): %s\n", 4688 pr_addr(so->so_family, so->so_laddr_sa, 4689 (t_uscalar_t)so->so_laddr_len))); 4690 #endif /* DEBUG */ 4691 if (so->so_family == AF_UNIX) { 4692 /* Transport has different name space - return local info */ 4693 error = 0; 4694 goto done; 4695 } 4696 if (!(so->so_state & SS_ISBOUND)) { 4697 /* If not bound, then nothing to return. */ 4698 error = 0; 4699 goto done; 4700 } 4701 /* Allocate local buffer to use with ioctl */ 4702 addrlen = (t_uscalar_t)so->so_laddr_maxlen; 4703 mutex_exit(&so->so_lock); 4704 addr = kmem_alloc(addrlen, KM_SLEEP); 4705 4706 /* 4707 * Issue TI_GETMYNAME with signals masked. 4708 * Put the result in so_laddr_sa so that getsockname works after 4709 * a shutdown(output). 4710 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted 4711 * back to the socket. 4712 */ 4713 strbuf.buf = addr; 4714 strbuf.maxlen = addrlen; 4715 strbuf.len = 0; 4716 4717 sigintr(&smask, 0); 4718 res = 0; 4719 ASSERT(CRED()); 4720 error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf, 4721 0, K_TO_K, CRED(), &res); 4722 sigunintr(&smask); 4723 4724 mutex_enter(&so->so_lock); 4725 /* 4726 * If there is an error record the error in so_error put don't fail 4727 * the getsockname. Instead fallback on the recorded 4728 * so->so_laddr_sa. 4729 */ 4730 if (error) { 4731 /* 4732 * Various stream head errors can be returned to the ioctl. 4733 * However, it is impossible to determine which ones of 4734 * these are really socket level errors that were incorrectly 4735 * consumed by the ioctl. Thus this code silently ignores the 4736 * error - to code explicitly does not reinstate the error 4737 * using soseterror(). 4738 * Experiments have shows that at least this set of 4739 * errors are reported and should not be reinstated on the 4740 * socket: 4741 * EINVAL E.g. if an I_LINK was in effect when 4742 * getsockname was called. 4743 * EPIPE The ioctl error semantics prefer the write 4744 * side error over the read side error. 4745 */ 4746 error = 0; 4747 } else if (res == 0 && strbuf.len > 0 && 4748 (so->so_state & SS_ISBOUND)) { 4749 ASSERT(strbuf.len <= (int)so->so_laddr_maxlen); 4750 so->so_laddr_len = (socklen_t)strbuf.len; 4751 bcopy(addr, so->so_laddr_sa, so->so_laddr_len); 4752 so->so_state |= SS_LADDR_VALID; 4753 } 4754 kmem_free(addr, addrlen); 4755 #ifdef DEBUG 4756 dprintso(so, 1, ("sotpi_getsockname (tp): %s\n", 4757 pr_addr(so->so_family, so->so_laddr_sa, 4758 (t_uscalar_t)so->so_laddr_len))); 4759 #endif /* DEBUG */ 4760 done: 4761 so_unlock_single(so, SOLOCKED); 4762 mutex_exit(&so->so_lock); 4763 return (error); 4764 } 4765 4766 /* 4767 * Get socket options. For SOL_SOCKET options some options are handled 4768 * by the sockfs while others use the value recorded in the sonode as a 4769 * fallback should the T_SVR4_OPTMGMT_REQ fail. 4770 * 4771 * On the return most *optlenp bytes are copied to optval. 4772 */ 4773 int 4774 sotpi_getsockopt(struct sonode *so, int level, int option_name, 4775 void *optval, socklen_t *optlenp, int flags) 4776 { 4777 struct T_optmgmt_req optmgmt_req; 4778 struct T_optmgmt_ack *optmgmt_ack; 4779 struct opthdr oh; 4780 struct opthdr *opt_res; 4781 mblk_t *mp = NULL; 4782 int error = 0; 4783 void *option = NULL; /* Set if fallback value */ 4784 t_uscalar_t maxlen = *optlenp; 4785 t_uscalar_t len; 4786 uint32_t value; 4787 4788 dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n", 4789 so, level, option_name, optval, optlenp, 4790 pr_state(so->so_state, so->so_mode))); 4791 4792 mutex_enter(&so->so_lock); 4793 so_lock_single(so); /* Set SOLOCKED */ 4794 4795 /* 4796 * Check for SOL_SOCKET options. 4797 * Certain SOL_SOCKET options are returned directly whereas 4798 * others only provide a default (fallback) value should 4799 * the T_SVR4_OPTMGMT_REQ fail. 4800 */ 4801 if (level == SOL_SOCKET) { 4802 /* Check parameters */ 4803 switch (option_name) { 4804 case SO_TYPE: 4805 case SO_ERROR: 4806 case SO_DEBUG: 4807 case SO_ACCEPTCONN: 4808 case SO_REUSEADDR: 4809 case SO_KEEPALIVE: 4810 case SO_DONTROUTE: 4811 case SO_BROADCAST: 4812 case SO_USELOOPBACK: 4813 case SO_OOBINLINE: 4814 case SO_SNDBUF: 4815 case SO_RCVBUF: 4816 #ifdef notyet 4817 case SO_SNDLOWAT: 4818 case SO_RCVLOWAT: 4819 case SO_SNDTIMEO: 4820 case SO_RCVTIMEO: 4821 #endif /* notyet */ 4822 case SO_DOMAIN: 4823 case SO_DGRAM_ERRIND: 4824 if (maxlen < (t_uscalar_t)sizeof (int32_t)) { 4825 error = EINVAL; 4826 eprintsoline(so, error); 4827 goto done2; 4828 } 4829 break; 4830 case SO_LINGER: 4831 if (maxlen < (t_uscalar_t)sizeof (struct linger)) { 4832 error = EINVAL; 4833 eprintsoline(so, error); 4834 goto done2; 4835 } 4836 break; 4837 } 4838 4839 len = (t_uscalar_t)sizeof (uint32_t); /* Default */ 4840 4841 switch (option_name) { 4842 case SO_TYPE: 4843 value = so->so_type; 4844 option = &value; 4845 goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 4846 4847 case SO_ERROR: 4848 value = sogeterr(so); 4849 option = &value; 4850 goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 4851 4852 case SO_ACCEPTCONN: 4853 if (so->so_state & SS_ACCEPTCONN) 4854 value = SO_ACCEPTCONN; 4855 else 4856 value = 0; 4857 #ifdef DEBUG 4858 if (value) { 4859 dprintso(so, 1, 4860 ("sotpi_getsockopt: 0x%x is set\n", 4861 option_name)); 4862 } else { 4863 dprintso(so, 1, 4864 ("sotpi_getsockopt: 0x%x not set\n", 4865 option_name)); 4866 } 4867 #endif /* DEBUG */ 4868 option = &value; 4869 goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 4870 4871 case SO_DEBUG: 4872 case SO_REUSEADDR: 4873 case SO_KEEPALIVE: 4874 case SO_DONTROUTE: 4875 case SO_BROADCAST: 4876 case SO_USELOOPBACK: 4877 case SO_OOBINLINE: 4878 case SO_DGRAM_ERRIND: 4879 value = (so->so_options & option_name); 4880 #ifdef DEBUG 4881 if (value) { 4882 dprintso(so, 1, 4883 ("sotpi_getsockopt: 0x%x is set\n", 4884 option_name)); 4885 } else { 4886 dprintso(so, 1, 4887 ("sotpi_getsockopt: 0x%x not set\n", 4888 option_name)); 4889 } 4890 #endif /* DEBUG */ 4891 option = &value; 4892 goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 4893 4894 /* 4895 * The following options are only returned by sockfs when the 4896 * T_SVR4_OPTMGMT_REQ fails. 4897 */ 4898 case SO_LINGER: 4899 option = &so->so_linger; 4900 len = (t_uscalar_t)sizeof (struct linger); 4901 break; 4902 case SO_SNDBUF: { 4903 ssize_t lvalue; 4904 4905 /* 4906 * If the option has not been set then get a default 4907 * value from the read queue. This value is 4908 * returned if the transport fails 4909 * the T_SVR4_OPTMGMT_REQ. 4910 */ 4911 lvalue = so->so_sndbuf; 4912 if (lvalue == 0) { 4913 mutex_exit(&so->so_lock); 4914 (void) strqget(strvp2wq(SOTOV(so))->q_next, 4915 QHIWAT, 0, &lvalue); 4916 mutex_enter(&so->so_lock); 4917 dprintso(so, 1, 4918 ("got SO_SNDBUF %ld from q\n", lvalue)); 4919 } 4920 value = (int)lvalue; 4921 option = &value; 4922 len = (t_uscalar_t)sizeof (so->so_sndbuf); 4923 break; 4924 } 4925 case SO_RCVBUF: { 4926 ssize_t lvalue; 4927 4928 /* 4929 * If the option has not been set then get a default 4930 * value from the read queue. This value is 4931 * returned if the transport fails 4932 * the T_SVR4_OPTMGMT_REQ. 4933 * 4934 * XXX If SO_RCVBUF has been set and this is an 4935 * XPG 4.2 application then do not ask the transport 4936 * since the transport might adjust the value and not 4937 * return exactly what was set by the application. 4938 * For non-XPG 4.2 application we return the value 4939 * that the transport is actually using. 4940 */ 4941 lvalue = so->so_rcvbuf; 4942 if (lvalue == 0) { 4943 mutex_exit(&so->so_lock); 4944 (void) strqget(RD(strvp2wq(SOTOV(so))), 4945 QHIWAT, 0, &lvalue); 4946 mutex_enter(&so->so_lock); 4947 dprintso(so, 1, 4948 ("got SO_RCVBUF %ld from q\n", lvalue)); 4949 } else if (flags & _SOGETSOCKOPT_XPG4_2) { 4950 value = (int)lvalue; 4951 option = &value; 4952 goto copyout; /* skip asking transport */ 4953 } 4954 value = (int)lvalue; 4955 option = &value; 4956 len = (t_uscalar_t)sizeof (so->so_rcvbuf); 4957 break; 4958 } 4959 case SO_DOMAIN: 4960 value = so->so_family; 4961 option = &value; 4962 goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */ 4963 4964 #ifdef notyet 4965 /* 4966 * We do not implement the semantics of these options 4967 * thus we shouldn't implement the options either. 4968 */ 4969 case SO_SNDLOWAT: 4970 value = so->so_sndlowat; 4971 option = &value; 4972 break; 4973 case SO_RCVLOWAT: 4974 value = so->so_rcvlowat; 4975 option = &value; 4976 break; 4977 case SO_SNDTIMEO: 4978 value = so->so_sndtimeo; 4979 option = &value; 4980 break; 4981 case SO_RCVTIMEO: 4982 value = so->so_rcvtimeo; 4983 option = &value; 4984 break; 4985 #endif /* notyet */ 4986 } 4987 } 4988 4989 mutex_exit(&so->so_lock); 4990 4991 /* Send request */ 4992 optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ; 4993 optmgmt_req.MGMT_flags = T_CHECK; 4994 optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen); 4995 optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req); 4996 4997 oh.level = level; 4998 oh.name = option_name; 4999 oh.len = maxlen; 5000 5001 mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req), 5002 &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP); 5003 /* Let option management work in the presence of data flow control */ 5004 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 5005 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 5006 mp = NULL; 5007 mutex_enter(&so->so_lock); 5008 if (error) { 5009 eprintsoline(so, error); 5010 goto done2; 5011 } 5012 error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK, 5013 (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0); 5014 if (error) { 5015 if (option != NULL) { 5016 /* We have a fallback value */ 5017 error = 0; 5018 goto copyout; 5019 } 5020 eprintsoline(so, error); 5021 goto done2; 5022 } 5023 ASSERT(mp); 5024 optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr; 5025 opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset, 5026 optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE); 5027 if (opt_res == NULL) { 5028 if (option != NULL) { 5029 /* We have a fallback value */ 5030 error = 0; 5031 goto copyout; 5032 } 5033 error = EPROTO; 5034 eprintsoline(so, error); 5035 goto done; 5036 } 5037 option = &opt_res[1]; 5038 5039 /* check to ensure that the option is within bounds */ 5040 if (((uintptr_t)option + opt_res->len < (uintptr_t)option) || 5041 (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) { 5042 if (option != NULL) { 5043 /* We have a fallback value */ 5044 error = 0; 5045 goto copyout; 5046 } 5047 error = EPROTO; 5048 eprintsoline(so, error); 5049 goto done; 5050 } 5051 5052 len = opt_res->len; 5053 5054 copyout: { 5055 t_uscalar_t size = MIN(len, maxlen); 5056 bcopy(option, optval, size); 5057 bcopy(&size, optlenp, sizeof (size)); 5058 } 5059 done: 5060 freemsg(mp); 5061 done2: 5062 so_unlock_single(so, SOLOCKED); 5063 mutex_exit(&so->so_lock); 5064 return (error); 5065 } 5066 5067 /* 5068 * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ. 5069 * SOL_SOCKET options are also recorded in the sonode. A setsockopt for 5070 * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails - 5071 * setsockopt has to work even if the transport does not support the option. 5072 */ 5073 int 5074 sotpi_setsockopt(struct sonode *so, int level, int option_name, 5075 const void *optval, t_uscalar_t optlen) 5076 { 5077 struct T_optmgmt_req optmgmt_req; 5078 struct opthdr oh; 5079 mblk_t *mp; 5080 int error = 0; 5081 boolean_t handled = B_FALSE; 5082 5083 dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n", 5084 so, level, option_name, optval, optlen, 5085 pr_state(so->so_state, so->so_mode))); 5086 5087 5088 /* X/Open requires this check */ 5089 if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) { 5090 if (xnet_check_print) 5091 printf("sockfs: X/Open setsockopt check => EINVAL\n"); 5092 return (EINVAL); 5093 } 5094 5095 /* Caller allocates aligned optval, or passes null */ 5096 ASSERT(((uintptr_t)optval & (sizeof (t_scalar_t) - 1)) == 0); 5097 /* If optval is null optlen is 0, and vice-versa */ 5098 ASSERT(optval != NULL || optlen == 0); 5099 ASSERT(optlen != 0 || optval == NULL); 5100 5101 mutex_enter(&so->so_lock); 5102 so_lock_single(so); /* Set SOLOCKED */ 5103 mutex_exit(&so->so_lock); 5104 5105 /* 5106 * For SOCKET or TCP level options, try to set it here itself 5107 * provided socket has not been popped and we know the tcp 5108 * structure (stored in so_priv). 5109 */ 5110 if ((level == SOL_SOCKET || level == IPPROTO_TCP) && 5111 (so->so_family == AF_INET || so->so_family == AF_INET6) && 5112 (so->so_version == SOV_SOCKSTREAM) && (so->so_priv != NULL)) { 5113 tcp_t *tcp = so->so_priv; 5114 boolean_t onoff; 5115 5116 #define intvalue (*(int32_t *)optval) 5117 5118 switch (level) { 5119 case SOL_SOCKET: 5120 switch (option_name) { /* Check length param */ 5121 case SO_DEBUG: 5122 case SO_REUSEADDR: 5123 case SO_DONTROUTE: 5124 case SO_BROADCAST: 5125 case SO_USELOOPBACK: 5126 case SO_OOBINLINE: 5127 case SO_DGRAM_ERRIND: 5128 if (optlen != (t_uscalar_t)sizeof (int32_t)) { 5129 error = EINVAL; 5130 eprintsoline(so, error); 5131 mutex_enter(&so->so_lock); 5132 goto done2; 5133 } 5134 ASSERT(optval); 5135 onoff = intvalue != 0; 5136 handled = B_TRUE; 5137 break; 5138 case SO_LINGER: 5139 if (optlen != 5140 (t_uscalar_t)sizeof (struct linger)) { 5141 error = EINVAL; 5142 eprintsoline(so, error); 5143 mutex_enter(&so->so_lock); 5144 goto done2; 5145 } 5146 ASSERT(optval); 5147 handled = B_TRUE; 5148 break; 5149 } 5150 5151 switch (option_name) { /* Do actions */ 5152 case SO_LINGER: { 5153 struct linger *lgr = (struct linger *)optval; 5154 5155 if (lgr->l_onoff) { 5156 tcp->tcp_linger = 1; 5157 tcp->tcp_lingertime = lgr->l_linger; 5158 so->so_linger.l_onoff = SO_LINGER; 5159 so->so_options |= SO_LINGER; 5160 } else { 5161 tcp->tcp_linger = 0; 5162 tcp->tcp_lingertime = 0; 5163 so->so_linger.l_onoff = 0; 5164 so->so_options &= ~SO_LINGER; 5165 } 5166 so->so_linger.l_linger = lgr->l_linger; 5167 handled = B_TRUE; 5168 break; 5169 } 5170 case SO_DEBUG: 5171 tcp->tcp_debug = onoff; 5172 #ifdef SOCK_TEST 5173 if (intvalue & 2) 5174 sock_test_timelimit = 10 * hz; 5175 else 5176 sock_test_timelimit = 0; 5177 5178 if (intvalue & 4) 5179 do_useracc = 0; 5180 else 5181 do_useracc = 1; 5182 #endif /* SOCK_TEST */ 5183 break; 5184 case SO_DONTROUTE: 5185 /* 5186 * SO_DONTROUTE, SO_USELOOPBACK and 5187 * SO_BROADCAST are only of interest to IP. 5188 * We track them here only so 5189 * that we can report their current value. 5190 */ 5191 tcp->tcp_dontroute = onoff; 5192 if (onoff) 5193 so->so_options |= option_name; 5194 else 5195 so->so_options &= ~option_name; 5196 break; 5197 case SO_USELOOPBACK: 5198 tcp->tcp_useloopback = onoff; 5199 if (onoff) 5200 so->so_options |= option_name; 5201 else 5202 so->so_options &= ~option_name; 5203 break; 5204 case SO_BROADCAST: 5205 tcp->tcp_broadcast = onoff; 5206 if (onoff) 5207 so->so_options |= option_name; 5208 else 5209 so->so_options &= ~option_name; 5210 break; 5211 case SO_REUSEADDR: 5212 tcp->tcp_reuseaddr = onoff; 5213 if (onoff) 5214 so->so_options |= option_name; 5215 else 5216 so->so_options &= ~option_name; 5217 break; 5218 case SO_OOBINLINE: 5219 tcp->tcp_oobinline = onoff; 5220 if (onoff) 5221 so->so_options |= option_name; 5222 else 5223 so->so_options &= ~option_name; 5224 break; 5225 case SO_DGRAM_ERRIND: 5226 tcp->tcp_dgram_errind = onoff; 5227 if (onoff) 5228 so->so_options |= option_name; 5229 else 5230 so->so_options &= ~option_name; 5231 break; 5232 } 5233 break; 5234 case IPPROTO_TCP: 5235 switch (option_name) { 5236 case TCP_NODELAY: 5237 if (optlen != (t_uscalar_t)sizeof (int32_t)) { 5238 error = EINVAL; 5239 eprintsoline(so, error); 5240 mutex_enter(&so->so_lock); 5241 goto done2; 5242 } 5243 ASSERT(optval); 5244 tcp->tcp_naglim = intvalue ? 1 : tcp->tcp_mss; 5245 handled = B_TRUE; 5246 break; 5247 } 5248 break; 5249 default: 5250 handled = B_FALSE; 5251 break; 5252 } 5253 } 5254 5255 if (handled) { 5256 mutex_enter(&so->so_lock); 5257 goto done2; 5258 } 5259 5260 optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ; 5261 optmgmt_req.MGMT_flags = T_NEGOTIATE; 5262 optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen; 5263 optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req); 5264 5265 oh.level = level; 5266 oh.name = option_name; 5267 oh.len = optlen; 5268 5269 mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req), 5270 &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP); 5271 /* Let option management work in the presence of data flow control */ 5272 error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0, 5273 MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0); 5274 mp = NULL; 5275 mutex_enter(&so->so_lock); 5276 if (error) { 5277 eprintsoline(so, error); 5278 goto done; 5279 } 5280 error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK, 5281 (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0); 5282 if (error) { 5283 eprintsoline(so, error); 5284 goto done; 5285 } 5286 ASSERT(mp); 5287 /* No need to verify T_optmgmt_ack */ 5288 freemsg(mp); 5289 done: 5290 /* 5291 * Check for SOL_SOCKET options and record their values. 5292 * If we know about a SOL_SOCKET parameter and the transport 5293 * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or 5294 * EPROTO) we let the setsockopt succeed. 5295 */ 5296 if (level == SOL_SOCKET) { 5297 /* Check parameters */ 5298 switch (option_name) { 5299 case SO_DEBUG: 5300 case SO_REUSEADDR: 5301 case SO_KEEPALIVE: 5302 case SO_DONTROUTE: 5303 case SO_BROADCAST: 5304 case SO_USELOOPBACK: 5305 case SO_OOBINLINE: 5306 case SO_SNDBUF: 5307 case SO_RCVBUF: 5308 #ifdef notyet 5309 case SO_SNDLOWAT: 5310 case SO_RCVLOWAT: 5311 case SO_SNDTIMEO: 5312 case SO_RCVTIMEO: 5313 #endif /* notyet */ 5314 case SO_DGRAM_ERRIND: 5315 if (optlen != (t_uscalar_t)sizeof (int32_t)) { 5316 error = EINVAL; 5317 eprintsoline(so, error); 5318 goto done2; 5319 } 5320 ASSERT(optval); 5321 handled = B_TRUE; 5322 break; 5323 case SO_LINGER: 5324 if (optlen != (t_uscalar_t)sizeof (struct linger)) { 5325 error = EINVAL; 5326 eprintsoline(so, error); 5327 goto done2; 5328 } 5329 ASSERT(optval); 5330 handled = B_TRUE; 5331 break; 5332 } 5333 5334 #define intvalue (*(int32_t *)optval) 5335 5336 switch (option_name) { 5337 case SO_TYPE: 5338 case SO_ERROR: 5339 case SO_ACCEPTCONN: 5340 /* Can't be set */ 5341 error = ENOPROTOOPT; 5342 goto done2; 5343 case SO_LINGER: { 5344 struct linger *l = (struct linger *)optval; 5345 5346 so->so_linger.l_linger = l->l_linger; 5347 if (l->l_onoff) { 5348 so->so_linger.l_onoff = SO_LINGER; 5349 so->so_options |= SO_LINGER; 5350 } else { 5351 so->so_linger.l_onoff = 0; 5352 so->so_options &= ~SO_LINGER; 5353 } 5354 break; 5355 } 5356 5357 case SO_DEBUG: 5358 #ifdef SOCK_TEST 5359 if (intvalue & 2) 5360 sock_test_timelimit = 10 * hz; 5361 else 5362 sock_test_timelimit = 0; 5363 5364 if (intvalue & 4) 5365 do_useracc = 0; 5366 else 5367 do_useracc = 1; 5368 #endif /* SOCK_TEST */ 5369 /* FALLTHRU */ 5370 case SO_REUSEADDR: 5371 case SO_KEEPALIVE: 5372 case SO_DONTROUTE: 5373 case SO_BROADCAST: 5374 case SO_USELOOPBACK: 5375 case SO_OOBINLINE: 5376 case SO_DGRAM_ERRIND: 5377 if (intvalue != 0) { 5378 dprintso(so, 1, 5379 ("sotpi_setsockopt: setting 0x%x\n", 5380 option_name)); 5381 so->so_options |= option_name; 5382 } else { 5383 dprintso(so, 1, 5384 ("sotpi_setsockopt: clearing 0x%x\n", 5385 option_name)); 5386 so->so_options &= ~option_name; 5387 } 5388 break; 5389 /* 5390 * The following options are only returned by us when the 5391 * T_SVR4_OPTMGMT_REQ fails. 5392 * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs 5393 * since the transport might adjust the value and not 5394 * return exactly what was set by the application. 5395 */ 5396 case SO_SNDBUF: 5397 so->so_sndbuf = intvalue; 5398 break; 5399 case SO_RCVBUF: 5400 so->so_rcvbuf = intvalue; 5401 break; 5402 #ifdef notyet 5403 /* 5404 * We do not implement the semantics of these options 5405 * thus we shouldn't implement the options either. 5406 */ 5407 case SO_SNDLOWAT: 5408 so->so_sndlowat = intvalue; 5409 break; 5410 case SO_RCVLOWAT: 5411 so->so_rcvlowat = intvalue; 5412 break; 5413 case SO_SNDTIMEO: 5414 so->so_sndtimeo = intvalue; 5415 break; 5416 case SO_RCVTIMEO: 5417 so->so_rcvtimeo = intvalue; 5418 break; 5419 #endif /* notyet */ 5420 } 5421 #undef intvalue 5422 5423 if (error) { 5424 if ((error == ENOPROTOOPT || error == EPROTO || 5425 error == EINVAL) && handled) { 5426 dprintso(so, 1, 5427 ("setsockopt: ignoring error %d for 0x%x\n", 5428 error, option_name)); 5429 error = 0; 5430 } 5431 } 5432 } 5433 done2: 5434 ret: 5435 so_unlock_single(so, SOLOCKED); 5436 mutex_exit(&so->so_lock); 5437 return (error); 5438 } 5439