1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 
  26 #include <sys/types.h>
  27 #include <sys/t_lock.h>
  28 #include <sys/param.h>
  29 #include <sys/systm.h>
  30 #include <sys/buf.h>
  31 #include <sys/vfs.h>
  32 #include <sys/vnode.h>
  33 #include <sys/fcntl.h>
  34 #include <sys/debug.h>
  35 #include <sys/errno.h>
  36 #include <sys/stropts.h>
  37 #include <sys/cmn_err.h>
  38 #include <sys/sysmacros.h>
  39 #include <sys/filio.h>
  40 #include <sys/policy.h>
  41 
  42 #include <sys/project.h>
  43 #include <sys/tihdr.h>
  44 #include <sys/strsubr.h>
  45 #include <sys/esunddi.h>
  46 #include <sys/ddi.h>
  47 
  48 #include <sys/sockio.h>
  49 #include <sys/socket.h>
  50 #include <sys/socketvar.h>
  51 #include <sys/strsun.h>
  52 
  53 #include <netinet/sctp.h>
  54 #include <inet/sctp_itf.h>
  55 #include <fs/sockfs/sockcommon.h>
  56 #include "socksctp.h"
  57 
  58 /*
  59  * SCTP sockfs sonode operations, 1-1 socket
  60  */
  61 static int sosctp_init(struct sonode *, struct sonode *, struct cred *, int);
  62 static int sosctp_accept(struct sonode *, int, struct cred *, struct sonode **);
  63 static int sosctp_bind(struct sonode *, struct sockaddr *, socklen_t, int,
  64     struct cred *);
  65 static int sosctp_listen(struct sonode *, int, struct cred *);
  66 static int sosctp_connect(struct sonode *, struct sockaddr *, socklen_t,
  67     int, int, struct cred *);
  68 static int sosctp_recvmsg(struct sonode *, struct nmsghdr *, struct uio *,
  69     struct cred *);
  70 static int sosctp_sendmsg(struct sonode *, struct nmsghdr *, struct uio *,
  71     struct cred *);
  72 static int sosctp_getpeername(struct sonode *, struct sockaddr *, socklen_t *,
  73     boolean_t, struct cred *);
  74 static int sosctp_getsockname(struct sonode *, struct sockaddr *, socklen_t *,
  75     struct cred *);
  76 static int sosctp_shutdown(struct sonode *, int, struct cred *);
  77 static int sosctp_getsockopt(struct sonode *, int, int, void *, socklen_t *,
  78     int, struct cred *);
  79 static int sosctp_setsockopt(struct sonode *, int, int, const void *,
  80     socklen_t, struct cred *);
  81 static int sosctp_ioctl(struct sonode *, int, intptr_t, int, struct cred *,
  82     int32_t *);
  83 static int sosctp_close(struct sonode *, int, struct cred *);
  84 void sosctp_fini(struct sonode *, struct cred *);
  85 
  86 /*
  87  * SCTP sockfs sonode operations, 1-N socket
  88  */
  89 static int sosctp_seq_connect(struct sonode *, struct sockaddr *,
  90     socklen_t, int, int, struct cred *);
  91 static int sosctp_seq_sendmsg(struct sonode *, struct nmsghdr *, struct uio *,
  92     struct cred *);
  93 
  94 /*
  95  * Socket association upcalls, 1-N socket connection
  96  */
  97 sock_upper_handle_t sctp_assoc_newconn(sock_upper_handle_t,
  98     sock_lower_handle_t, sock_downcalls_t *, struct cred *, pid_t,
  99     sock_upcalls_t **);
 100 static void sctp_assoc_connected(sock_upper_handle_t, sock_connid_t,
 101     struct cred *, pid_t);
 102 static int sctp_assoc_disconnected(sock_upper_handle_t, sock_connid_t, int);
 103 static void sctp_assoc_disconnecting(sock_upper_handle_t, sock_opctl_action_t,
 104     uintptr_t arg);
 105 static ssize_t sctp_assoc_recv(sock_upper_handle_t, mblk_t *, size_t, int,
 106     int *, boolean_t *);
 107 static void sctp_assoc_xmitted(sock_upper_handle_t, boolean_t);
 108 static void sctp_assoc_properties(sock_upper_handle_t,
 109     struct sock_proto_props *);
 110 static conn_pid_node_list_hdr_t *
 111     sctp_get_sock_pid_list(sock_upper_handle_t);
 112 
 113 sonodeops_t sosctp_sonodeops = {
 114         sosctp_init,                    /* sop_init     */
 115         sosctp_accept,                  /* sop_accept   */
 116         sosctp_bind,                    /* sop_bind     */
 117         sosctp_listen,                  /* sop_listen   */
 118         sosctp_connect,                 /* sop_connect  */
 119         sosctp_recvmsg,                 /* sop_recvmsg  */
 120         sosctp_sendmsg,                 /* sop_sendmsg  */
 121         so_sendmblk_notsupp,            /* sop_sendmblk */
 122         sosctp_getpeername,             /* sop_getpeername */
 123         sosctp_getsockname,             /* sop_getsockname */
 124         sosctp_shutdown,                /* sop_shutdown */
 125         sosctp_getsockopt,              /* sop_getsockopt */
 126         sosctp_setsockopt,              /* sop_setsockopt */
 127         sosctp_ioctl,                   /* sop_ioctl    */
 128         so_poll,                        /* sop_poll     */
 129         sosctp_close,                   /* sop_close    */
 130 };
 131 
 132 sonodeops_t sosctp_seq_sonodeops = {
 133         sosctp_init,                    /* sop_init     */
 134         so_accept_notsupp,              /* sop_accept   */
 135         sosctp_bind,                    /* sop_bind     */
 136         sosctp_listen,                  /* sop_listen   */
 137         sosctp_seq_connect,             /* sop_connect  */
 138         sosctp_recvmsg,                 /* sop_recvmsg  */
 139         sosctp_seq_sendmsg,             /* sop_sendmsg  */
 140         so_sendmblk_notsupp,            /* sop_sendmblk */
 141         so_getpeername_notsupp,         /* sop_getpeername */
 142         sosctp_getsockname,             /* sop_getsockname */
 143         so_shutdown_notsupp,            /* sop_shutdown */
 144         sosctp_getsockopt,              /* sop_getsockopt */
 145         sosctp_setsockopt,              /* sop_setsockopt */
 146         sosctp_ioctl,                   /* sop_ioctl    */
 147         so_poll,                        /* sop_poll     */
 148         sosctp_close,                   /* sop_close    */
 149 };
 150 
 151 /* All the upcalls expect the upper handle to be sonode. */
 152 sock_upcalls_t sosctp_sock_upcalls = {
 153         so_newconn,
 154         so_connected,
 155         so_disconnected,
 156         so_opctl,
 157         so_queue_msg,
 158         so_set_prop,
 159         so_txq_full,
 160         NULL,                   /* su_signal_oob */
 161 };
 162 
 163 /* All the upcalls expect the upper handle to be sctp_sonode/sctp_soassoc. */
 164 sock_upcalls_t sosctp_assoc_upcalls = {
 165         sctp_assoc_newconn,
 166         sctp_assoc_connected,
 167         sctp_assoc_disconnected,
 168         sctp_assoc_disconnecting,
 169         sctp_assoc_recv,
 170         sctp_assoc_properties,
 171         sctp_assoc_xmitted,
 172         NULL,                   /* su_recv_space */
 173         NULL,                   /* su_signal_oob */
 174         NULL,                   /* su_set_error */
 175         NULL,                   /* su_closed */
 176         sctp_get_sock_pid_list
 177 };
 178 
 179 /* ARGSUSED */
 180 static int
 181 sosctp_init(struct sonode *so, struct sonode *pso, struct cred *cr, int flags)
 182 {
 183         struct sctp_sonode *ss;
 184         struct sctp_sonode *pss;
 185         sctp_sockbuf_limits_t sbl;
 186         int err;
 187 
 188         ss = SOTOSSO(so);
 189 
 190         if (pso != NULL) {
 191                 /*
 192                  * Passive open, just inherit settings from parent. We should
 193                  * not end up here for SOCK_SEQPACKET type sockets, since no
 194                  * new sonode is created in that case.
 195                  */
 196                 ASSERT(so->so_type == SOCK_STREAM);
 197                 pss = SOTOSSO(pso);
 198 
 199                 mutex_enter(&pso->so_lock);
 200                 so->so_state |= (SS_ISBOUND | SS_ISCONNECTED |
 201                     (pso->so_state & SS_ASYNC));
 202                 sosctp_so_inherit(pss, ss);
 203                 so->so_proto_props = pso->so_proto_props;
 204                 so->so_mode = pso->so_mode;
 205                 mutex_exit(&pso->so_lock);
 206 
 207                 return (0);
 208         }
 209 
 210         if ((err = secpolicy_basic_net_access(cr)) != 0)
 211                 return (err);
 212 
 213         if (so->so_type == SOCK_STREAM) {
 214                 so->so_proto_handle = (sock_lower_handle_t)sctp_create(so,
 215                     NULL, so->so_family, so->so_type, SCTP_CAN_BLOCK,
 216                     &sosctp_sock_upcalls, &sbl, cr);
 217                 so->so_mode = SM_CONNREQUIRED;
 218         } else {
 219                 ASSERT(so->so_type == SOCK_SEQPACKET);
 220                 so->so_proto_handle = (sock_lower_handle_t)sctp_create(ss,
 221                     NULL, so->so_family, so->so_type, SCTP_CAN_BLOCK,
 222                     &sosctp_assoc_upcalls, &sbl, cr);
 223         }
 224 
 225         if (so->so_proto_handle == NULL)
 226                 return (ENOMEM);
 227 
 228         so->so_rcvbuf = sbl.sbl_rxbuf;
 229         so->so_rcvlowat = sbl.sbl_rxlowat;
 230         so->so_sndbuf = sbl.sbl_txbuf;
 231         so->so_sndlowat = sbl.sbl_txlowat;
 232 
 233         return (0);
 234 }
 235 
 236 /*
 237  * Accept incoming connection.
 238  */
 239 /*ARGSUSED*/
 240 static int
 241 sosctp_accept(struct sonode *so, int fflag, struct cred *cr,
 242     struct sonode **nsop)
 243 {
 244         int error = 0;
 245 
 246         if ((so->so_state & SS_ACCEPTCONN) == 0)
 247                 return (EINVAL);
 248 
 249         error = so_acceptq_dequeue(so, (fflag & (FNONBLOCK|FNDELAY)), nsop);
 250 
 251         return (error);
 252 }
 253 
 254 /*
 255  * Bind local endpoint.
 256  */
 257 /*ARGSUSED*/
 258 static int
 259 sosctp_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
 260     int flags, struct cred *cr)
 261 {
 262         int error;
 263 
 264         if (!(flags & _SOBIND_LOCK_HELD)) {
 265                 mutex_enter(&so->so_lock);
 266                 so_lock_single(so);     /* Set SOLOCKED */
 267         } else {
 268                 ASSERT(MUTEX_HELD(&so->so_lock));
 269         }
 270 
 271         /*
 272          * X/Open requires this check
 273          */
 274         if (so->so_state & SS_CANTSENDMORE) {
 275                 error = EINVAL;
 276                 goto done;
 277         }
 278 
 279 
 280         /*
 281          * Protocol module does address family checks.
 282          */
 283         mutex_exit(&so->so_lock);
 284 
 285         error = sctp_bind((struct sctp_s *)so->so_proto_handle, name, namelen);
 286 
 287         mutex_enter(&so->so_lock);
 288         if (error == 0) {
 289                 so->so_state |= SS_ISBOUND;
 290         } else {
 291                 eprintsoline(so, error);
 292         }
 293 done:
 294         if (!(flags & _SOBIND_LOCK_HELD)) {
 295                 so_unlock_single(so, SOLOCKED);
 296                 mutex_exit(&so->so_lock);
 297         } else {
 298                 /* If the caller held the lock don't release it here */
 299                 ASSERT(MUTEX_HELD(&so->so_lock));
 300                 ASSERT(so->so_flag & SOLOCKED);
 301         }
 302 
 303         return (error);
 304 }
 305 
 306 /*
 307  * Turn socket into a listen socket.
 308  */
 309 /* ARGSUSED */
 310 static int
 311 sosctp_listen(struct sonode *so, int backlog, struct cred *cr)
 312 {
 313         int error = 0;
 314 
 315         mutex_enter(&so->so_lock);
 316         so_lock_single(so);
 317 
 318         /*
 319          * If this socket is trying to do connect, or if it has
 320          * been connected, disallow.
 321          */
 322         if (so->so_state & (SS_ISCONNECTING | SS_ISCONNECTED |
 323             SS_ISDISCONNECTING | SS_CANTRCVMORE | SS_CANTSENDMORE)) {
 324                 error = EINVAL;
 325                 eprintsoline(so, error);
 326                 goto done;
 327         }
 328 
 329         if (backlog < 0) {
 330                 backlog = 0;
 331         }
 332 
 333         /*
 334          * If listen() is only called to change backlog, we don't
 335          * need to notify protocol module.
 336          */
 337         if (so->so_state & SS_ACCEPTCONN) {
 338                 so->so_backlog = backlog;
 339                 goto done;
 340         }
 341 
 342         mutex_exit(&so->so_lock);
 343         error = sctp_listen((struct sctp_s *)so->so_proto_handle);
 344         mutex_enter(&so->so_lock);
 345         if (error == 0) {
 346                 so->so_state |= (SS_ACCEPTCONN|SS_ISBOUND);
 347                 so->so_backlog = backlog;
 348         } else {
 349                 eprintsoline(so, error);
 350         }
 351 done:
 352         so_unlock_single(so, SOLOCKED);
 353         mutex_exit(&so->so_lock);
 354 
 355         return (error);
 356 }
 357 
 358 /*
 359  * Active open.
 360  */
 361 /*ARGSUSED*/
 362 static int
 363 sosctp_connect(struct sonode *so, struct sockaddr *name,
 364     socklen_t namelen, int fflag, int flags, struct cred *cr)
 365 {
 366         int error = 0;
 367         pid_t pid = curproc->p_pid;
 368 
 369         ASSERT(so->so_type == SOCK_STREAM);
 370 
 371         mutex_enter(&so->so_lock);
 372         so_lock_single(so);
 373 
 374         /*
 375          * Can't connect() after listen(), or if the socket is already
 376          * connected.
 377          */
 378         if (so->so_state & (SS_ACCEPTCONN|SS_ISCONNECTED|SS_ISCONNECTING)) {
 379                 if (so->so_state & SS_ISCONNECTED) {
 380                         error = EISCONN;
 381                 } else if (so->so_state & SS_ISCONNECTING) {
 382                         error = EALREADY;
 383                 } else {
 384                         error = EOPNOTSUPP;
 385                 }
 386                 eprintsoline(so, error);
 387                 goto done;
 388         }
 389 
 390         /*
 391          * Check for failure of an earlier call
 392          */
 393         if (so->so_error != 0) {
 394                 error = sogeterr(so, B_TRUE);
 395                 eprintsoline(so, error);
 396                 goto done;
 397         }
 398 
 399         /*
 400          * Connection is closing, or closed, don't allow reconnect.
 401          * TCP allows this to proceed, but the socket remains unwriteable.
 402          * BSD returns EINVAL.
 403          */
 404         if (so->so_state & (SS_ISDISCONNECTING|SS_CANTRCVMORE|
 405             SS_CANTSENDMORE)) {
 406                 error = EINVAL;
 407                 eprintsoline(so, error);
 408                 goto done;
 409         }
 410 
 411         if (name == NULL || namelen == 0) {
 412                 mutex_exit(&so->so_lock);
 413                 error = EINVAL;
 414                 eprintsoline(so, error);
 415                 goto done;
 416         }
 417 
 418         soisconnecting(so);
 419         mutex_exit(&so->so_lock);
 420 
 421         error = sctp_connect((struct sctp_s *)so->so_proto_handle,
 422             name, namelen, cr, pid);
 423 
 424         mutex_enter(&so->so_lock);
 425         if (error == 0) {
 426                 /*
 427                  * Allow other threads to access the socket
 428                  */
 429                 error = sowaitconnected(so, fflag, 0);
 430         }
 431 done:
 432         so_unlock_single(so, SOLOCKED);
 433         mutex_exit(&so->so_lock);
 434         return (error);
 435 }
 436 
 437 /*
 438  * Active open for 1-N sockets, create a new association and
 439  * call connect on that.
 440  * If there parent hasn't been bound yet (this is the first association),
 441  * make it so.
 442  */
 443 static int
 444 sosctp_seq_connect(struct sonode *so, struct sockaddr *name,
 445     socklen_t namelen, int fflag, int flags, struct cred *cr)
 446 {
 447         struct sctp_soassoc *ssa;
 448         struct sctp_sonode *ss;
 449         int error;
 450 
 451         ASSERT(so->so_type == SOCK_SEQPACKET);
 452 
 453         mutex_enter(&so->so_lock);
 454         so_lock_single(so);
 455 
 456         if (name == NULL || namelen == 0) {
 457                 error = EINVAL;
 458                 eprintsoline(so, error);
 459                 goto done;
 460         }
 461 
 462         ss = SOTOSSO(so);
 463 
 464         error = sosctp_assoc_createconn(ss, name, namelen, NULL, 0, fflag,
 465             cr, &ssa);
 466         if (error != 0) {
 467                 if ((error == EHOSTUNREACH) && (flags & _SOCONNECT_XPG4_2)) {
 468                         error = ENETUNREACH;
 469                 }
 470         }
 471         if (ssa != NULL) {
 472                 SSA_REFRELE(ss, ssa);
 473         }
 474 
 475 done:
 476         so_unlock_single(so, SOLOCKED);
 477         mutex_exit(&so->so_lock);
 478         return (error);
 479 }
 480 
 481 /*
 482  * Receive data.
 483  */
 484 /* ARGSUSED */
 485 static int
 486 sosctp_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
 487     struct cred *cr)
 488 {
 489         struct sctp_sonode *ss = SOTOSSO(so);
 490         struct sctp_soassoc *ssa = NULL;
 491         int flags, error = 0;
 492         struct T_unitdata_ind *tind;
 493         ssize_t orig_resid = uiop->uio_resid;
 494         int len, count, readcnt = 0;
 495         socklen_t controllen, namelen;
 496         void *opt;
 497         mblk_t *mp;
 498         rval_t  rval;
 499 
 500         controllen = msg->msg_controllen;
 501         namelen = msg->msg_namelen;
 502         flags = msg->msg_flags;
 503         msg->msg_flags = 0;
 504         msg->msg_controllen = 0;
 505         msg->msg_namelen = 0;
 506 
 507         if (so->so_type == SOCK_STREAM) {
 508                 if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|
 509                     SS_CANTRCVMORE))) {
 510                         return (ENOTCONN);
 511                 }
 512         } else {
 513                 /* NOTE: Will come here from vop_read() as well */
 514                 /* For 1-N socket, recv() cannot be used. */
 515                 if (namelen == 0)
 516                         return (EOPNOTSUPP);
 517                 /*
 518                  * If there are no associations, and no new connections are
 519                  * coming in, there's not going to be new messages coming
 520                  * in either.
 521                  */
 522                 if (so->so_rcv_q_head == NULL && so->so_rcv_head == NULL &&
 523                     ss->ss_assoccnt == 0 && !(so->so_state & SS_ACCEPTCONN)) {
 524                         return (ENOTCONN);
 525                 }
 526         }
 527 
 528         /*
 529          * out-of-band data not supported.
 530          */
 531         if (flags & MSG_OOB) {
 532                 return (EOPNOTSUPP);
 533         }
 534 
 535         /*
 536          * flag possibilities:
 537          *
 538          * MSG_PEEK     Don't consume data
 539          * MSG_WAITALL  Wait for full quantity of data (ignored if MSG_PEEK)
 540          * MSG_DONTWAIT Non-blocking (same as FNDELAY | FNONBLOCK)
 541          *
 542          * MSG_WAITALL can return less than the full buffer if either
 543          *
 544          * 1. we would block and we are non-blocking
 545          * 2. a full message cannot be delivered
 546          *
 547          * Given that we always get a full message from proto below,
 548          * MSG_WAITALL is not meaningful.
 549          */
 550 
 551         mutex_enter(&so->so_lock);
 552 
 553         /*
 554          * Allow just one reader at a time.
 555          */
 556         error = so_lock_read_intr(so,
 557             uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
 558         if (error) {
 559                 mutex_exit(&so->so_lock);
 560                 return (error);
 561         }
 562         mutex_exit(&so->so_lock);
 563 again:
 564         error = so_dequeue_msg(so, &mp, uiop, &rval, flags | MSG_DUPCTRL);
 565         if (mp != NULL) {
 566                 if (so->so_type == SOCK_SEQPACKET) {
 567                         ssa = *(struct sctp_soassoc **)DB_BASE(mp);
 568                 }
 569 
 570                 tind = (struct T_unitdata_ind *)mp->b_rptr;
 571 
 572                 len = tind->SRC_length;
 573 
 574                 if (namelen > 0 && len > 0) {
 575 
 576                         opt = sogetoff(mp, tind->SRC_offset, len, 1);
 577 
 578                         ASSERT(opt != NULL);
 579 
 580                         msg->msg_name = kmem_alloc(len, KM_SLEEP);
 581                         msg->msg_namelen = len;
 582 
 583                         bcopy(opt, msg->msg_name, len);
 584                 }
 585 
 586                 len = tind->OPT_length;
 587                 if (controllen == 0) {
 588                         if (len > 0) {
 589                                 msg->msg_flags |= MSG_CTRUNC;
 590                         }
 591                 } else if (len > 0) {
 592                         opt = sogetoff(mp, tind->OPT_offset, len,
 593                             __TPI_ALIGN_SIZE);
 594 
 595                         ASSERT(opt != NULL);
 596                         sosctp_pack_cmsg(opt, msg, len);
 597                 }
 598 
 599                 if (mp->b_flag & SCTP_NOTIFICATION) {
 600                         msg->msg_flags |= MSG_NOTIFICATION;
 601                 }
 602 
 603                 if (!(mp->b_flag & SCTP_PARTIAL_DATA) &&
 604                     !(rval.r_val1 & MOREDATA)) {
 605                         msg->msg_flags |= MSG_EOR;
 606                 }
 607                 freemsg(mp);
 608         }
 609 done:
 610         if (!(flags & MSG_PEEK))
 611                 readcnt = orig_resid - uiop->uio_resid;
 612         /*
 613          * Determine if we need to update SCTP about the buffer
 614          * space.  For performance reason, we cannot update SCTP
 615          * every time a message is read.  The socket buffer low
 616          * watermark is used as the threshold.
 617          */
 618         if (ssa == NULL) {
 619                 mutex_enter(&so->so_lock);
 620                 count = so->so_rcvbuf - so->so_rcv_queued;
 621 
 622                 ASSERT(so->so_rcv_q_head != NULL ||
 623                     so->so_rcv_head != NULL ||
 624                     so->so_rcv_queued == 0);
 625 
 626                 so_unlock_read(so);
 627 
 628                 /*
 629                  * so_dequeue_msg() sets r_val2 to true if flow control was
 630                  * cleared and we need to update SCTP.  so_flowctrld was
 631                  * cleared in so_dequeue_msg() via so_check_flow_control().
 632                  */
 633                 if (rval.r_val2) {
 634                         mutex_exit(&so->so_lock);
 635                         sctp_recvd((struct sctp_s *)so->so_proto_handle, count);
 636                 } else {
 637                         mutex_exit(&so->so_lock);
 638                 }
 639         } else {
 640                 /*
 641                  * Each association keeps track of how much data it has
 642                  * queued; we need to update the value here. Note that this
 643                  * is slightly different from SOCK_STREAM type sockets, which
 644                  * does not need to update the byte count, as it is already
 645                  * done in so_dequeue_msg().
 646                  */
 647                 mutex_enter(&so->so_lock);
 648                 ssa->ssa_rcv_queued -= readcnt;
 649                 count = so->so_rcvbuf - ssa->ssa_rcv_queued;
 650 
 651                 so_unlock_read(so);
 652 
 653                 if (readcnt > 0 && ssa->ssa_flowctrld &&
 654                     ssa->ssa_rcv_queued < so->so_rcvlowat) {
 655                         /*
 656                          * Need to clear ssa_flowctrld, different from 1-1
 657                          * style.
 658                          */
 659                         ssa->ssa_flowctrld = B_FALSE;
 660                         mutex_exit(&so->so_lock);
 661                         sctp_recvd(ssa->ssa_conn, count);
 662                         mutex_enter(&so->so_lock);
 663                 }
 664 
 665                 /*
 666                  * MOREDATA flag is set if all data could not be copied
 667                  */
 668                 if (!(flags & MSG_PEEK) && !(rval.r_val1 & MOREDATA)) {
 669                         SSA_REFRELE(ss, ssa);
 670                 }
 671                 mutex_exit(&so->so_lock);
 672         }
 673 
 674         return (error);
 675 }
 676 
 677 int
 678 sosctp_uiomove(mblk_t *hdr_mp, ssize_t count, ssize_t blk_size, int wroff,
 679     struct uio *uiop, int flags)
 680 {
 681         ssize_t size;
 682         int error;
 683         mblk_t *mp;
 684         dblk_t *dp;
 685 
 686         if (blk_size == INFPSZ)
 687                 blk_size = count;
 688 
 689         /*
 690          * Loop until we have all data copied into mblk's.
 691          */
 692         while (count > 0) {
 693                 size = MIN(count, blk_size);
 694 
 695                 /*
 696                  * As a message can be splitted up and sent in different
 697                  * packets, each mblk will have the extra space before
 698                  * data to accommodate what SCTP wants to put in there.
 699                  */
 700                 while ((mp = allocb(size + wroff, BPRI_MED)) == NULL) {
 701                         if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) ||
 702                             (flags & MSG_DONTWAIT)) {
 703                                 return (EAGAIN);
 704                         }
 705                         if ((error = strwaitbuf(size + wroff, BPRI_MED))) {
 706                                 return (error);
 707                         }
 708                 }
 709 
 710                 dp = mp->b_datap;
 711                 dp->db_cpid = curproc->p_pid;
 712                 ASSERT(wroff <= dp->db_lim - mp->b_wptr);
 713                 mp->b_rptr += wroff;
 714                 error = uiomove(mp->b_rptr, size, UIO_WRITE, uiop);
 715                 if (error != 0) {
 716                         freeb(mp);
 717                         return (error);
 718                 }
 719                 mp->b_wptr = mp->b_rptr + size;
 720                 count -= size;
 721                 hdr_mp->b_cont = mp;
 722                 hdr_mp = mp;
 723         }
 724         return (0);
 725 }
 726 
 727 /*
 728  * Send message.
 729  */
 730 static int
 731 sosctp_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
 732     struct cred *cr)
 733 {
 734         mblk_t *mctl;
 735         struct cmsghdr *cmsg;
 736         struct sctp_sndrcvinfo *sinfo;
 737         int optlen, flags, fflag;
 738         ssize_t count, msglen;
 739         int error;
 740 
 741         ASSERT(so->so_type == SOCK_STREAM);
 742 
 743         flags = msg->msg_flags;
 744         if (flags & MSG_OOB) {
 745                 /*
 746                  * No out-of-band data support.
 747                  */
 748                 return (EOPNOTSUPP);
 749         }
 750 
 751         if (msg->msg_controllen != 0) {
 752                 optlen = msg->msg_controllen;
 753                 cmsg = sosctp_find_cmsg(msg->msg_control, optlen, SCTP_SNDRCV);
 754                 if (cmsg != NULL) {
 755                         if (cmsg->cmsg_len <
 756                             (sizeof (*sinfo) + sizeof (*cmsg))) {
 757                                 eprintsoline(so, EINVAL);
 758                                 return (EINVAL);
 759                         }
 760                         sinfo = (struct sctp_sndrcvinfo *)(cmsg + 1);
 761 
 762                         /* Both flags should not be set together. */
 763                         if ((sinfo->sinfo_flags & MSG_EOF) &&
 764                             (sinfo->sinfo_flags & MSG_ABORT)) {
 765                                 eprintsoline(so, EINVAL);
 766                                 return (EINVAL);
 767                         }
 768 
 769                         /* Initiate a graceful shutdown. */
 770                         if (sinfo->sinfo_flags & MSG_EOF) {
 771                                 /* Can't include data in MSG_EOF message. */
 772                                 if (uiop->uio_resid != 0) {
 773                                         eprintsoline(so, EINVAL);
 774                                         return (EINVAL);
 775                                 }
 776 
 777                                 /*
 778                                  * This is the same sequence as done in
 779                                  * shutdown(SHUT_WR).
 780                                  */
 781                                 mutex_enter(&so->so_lock);
 782                                 so_lock_single(so);
 783                                 socantsendmore(so);
 784                                 cv_broadcast(&so->so_snd_cv);
 785                                 so->so_state |= SS_ISDISCONNECTING;
 786                                 mutex_exit(&so->so_lock);
 787 
 788                                 pollwakeup(&so->so_poll_list, POLLOUT);
 789                                 sctp_recvd((struct sctp_s *)so->so_proto_handle,
 790                                     so->so_rcvbuf);
 791                                 error = sctp_disconnect(
 792                                     (struct sctp_s *)so->so_proto_handle);
 793 
 794                                 mutex_enter(&so->so_lock);
 795                                 so_unlock_single(so, SOLOCKED);
 796                                 mutex_exit(&so->so_lock);
 797                                 return (error);
 798                         }
 799                 }
 800         } else {
 801                 optlen = 0;
 802         }
 803 
 804         mutex_enter(&so->so_lock);
 805         for (;;) {
 806                 if (so->so_state & SS_CANTSENDMORE) {
 807                         mutex_exit(&so->so_lock);
 808                         return (EPIPE);
 809                 }
 810 
 811                 if (so->so_error != 0) {
 812                         error = sogeterr(so, B_TRUE);
 813                         mutex_exit(&so->so_lock);
 814                         return (error);
 815                 }
 816 
 817                 if (!so->so_snd_qfull)
 818                         break;
 819 
 820                 if (so->so_state & SS_CLOSING) {
 821                         mutex_exit(&so->so_lock);
 822                         return (EINTR);
 823                 }
 824                 /*
 825                  * Xmit window full in a blocking socket.
 826                  */
 827                 if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) ||
 828                     (flags & MSG_DONTWAIT)) {
 829                         mutex_exit(&so->so_lock);
 830                         return (EAGAIN);
 831                 } else {
 832                         /*
 833                          * Wait for space to become available and try again.
 834                          */
 835                         error = cv_wait_sig(&so->so_snd_cv, &so->so_lock);
 836                         if (!error) { /* signal */
 837                                 mutex_exit(&so->so_lock);
 838                                 return (EINTR);
 839                         }
 840                 }
 841         }
 842         msglen = count = uiop->uio_resid;
 843 
 844         /* Don't allow sending a message larger than the send buffer size. */
 845         /* XXX Transport module need to enforce this */
 846         if (msglen > so->so_sndbuf) {
 847                 mutex_exit(&so->so_lock);
 848                 return (EMSGSIZE);
 849         }
 850 
 851         /*
 852          * Allow piggybacking data on handshake messages (SS_ISCONNECTING).
 853          */
 854         if (!(so->so_state & (SS_ISCONNECTING | SS_ISCONNECTED))) {
 855                 /*
 856                  * We need to check here for listener so that the
 857                  * same error will be returned as with a TCP socket.
 858                  * In this case, sosctp_connect() returns EOPNOTSUPP
 859                  * while a TCP socket returns ENOTCONN instead.  Catch it
 860                  * here to have the same behavior as a TCP socket.
 861                  *
 862                  * We also need to make sure that the peer address is
 863                  * provided before we attempt to do the connect.
 864                  */
 865                 if ((so->so_state & SS_ACCEPTCONN) ||
 866                     msg->msg_name == NULL) {
 867                         mutex_exit(&so->so_lock);
 868                         error = ENOTCONN;
 869                         goto error_nofree;
 870                 }
 871                 mutex_exit(&so->so_lock);
 872                 fflag = uiop->uio_fmode;
 873                 if (flags & MSG_DONTWAIT) {
 874                         fflag |= FNDELAY;
 875                 }
 876                 error = sosctp_connect(so, msg->msg_name, msg->msg_namelen,
 877                     fflag, (so->so_version == SOV_XPG4_2) * _SOCONNECT_XPG4_2,
 878                     cr);
 879                 if (error) {
 880                         /*
 881                          * Check for non-fatal errors, socket connected
 882                          * while the lock had been lifted.
 883                          */
 884                         if (error != EISCONN && error != EALREADY) {
 885                                 goto error_nofree;
 886                         }
 887                         error = 0;
 888                 }
 889         } else {
 890                 mutex_exit(&so->so_lock);
 891         }
 892 
 893         mctl = sctp_alloc_hdr(msg->msg_name, msg->msg_namelen,
 894             msg->msg_control, optlen, SCTP_CAN_BLOCK);
 895         if (mctl == NULL) {
 896                 error = EINTR;
 897                 goto error_nofree;
 898         }
 899 
 900         /* Copy in the message. */
 901         if ((error = sosctp_uiomove(mctl, count, so->so_proto_props.sopp_maxblk,
 902             so->so_proto_props.sopp_wroff, uiop, flags)) != 0) {
 903                 goto error_ret;
 904         }
 905         error = sctp_sendmsg((struct sctp_s *)so->so_proto_handle, mctl, 0);
 906         if (error == 0)
 907                 return (0);
 908 
 909 error_ret:
 910         freemsg(mctl);
 911 error_nofree:
 912         mutex_enter(&so->so_lock);
 913         if ((error == EPIPE) && (so->so_state & SS_CANTSENDMORE)) {
 914                 /*
 915                  * We received shutdown between the time lock was
 916                  * lifted and call to sctp_sendmsg().
 917                  */
 918                 mutex_exit(&so->so_lock);
 919                 return (EPIPE);
 920         }
 921         mutex_exit(&so->so_lock);
 922         return (error);
 923 }
 924 
 925 /*
 926  * Send message on 1-N socket. Connects automatically if there is
 927  * no association.
 928  */
 929 static int
 930 sosctp_seq_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
 931     struct cred *cr)
 932 {
 933         struct sctp_sonode *ss;
 934         struct sctp_soassoc *ssa;
 935         struct cmsghdr *cmsg;
 936         struct sctp_sndrcvinfo *sinfo;
 937         int aid = 0;
 938         mblk_t *mctl;
 939         int namelen, optlen, flags;
 940         ssize_t count, msglen;
 941         int error;
 942         uint16_t s_flags = 0;
 943 
 944         ASSERT(so->so_type == SOCK_SEQPACKET);
 945 
 946         /*
 947          * There shouldn't be problems with alignment, as the memory for
 948          * msg_control was alloced with kmem_alloc.
 949          */
 950         cmsg = sosctp_find_cmsg(msg->msg_control, msg->msg_controllen,
 951             SCTP_SNDRCV);
 952         if (cmsg != NULL) {
 953                 if (cmsg->cmsg_len < (sizeof (*sinfo) + sizeof (*cmsg))) {
 954                         eprintsoline(so, EINVAL);
 955                         return (EINVAL);
 956                 }
 957                 sinfo = (struct sctp_sndrcvinfo *)(cmsg + 1);
 958                 s_flags = sinfo->sinfo_flags;
 959                 aid = sinfo->sinfo_assoc_id;
 960         }
 961 
 962         ss = SOTOSSO(so);
 963         namelen = msg->msg_namelen;
 964 
 965         if (msg->msg_controllen > 0) {
 966                 optlen = msg->msg_controllen;
 967         } else {
 968                 optlen = 0;
 969         }
 970 
 971         mutex_enter(&so->so_lock);
 972 
 973         /*
 974          * If there is no association id, connect to address specified
 975          * in msg_name.  Otherwise look up the association using the id.
 976          */
 977         if (aid == 0) {
 978                 /*
 979                  * Connect and shutdown cannot be done together, so check for
 980                  * MSG_EOF.
 981                  */
 982                 if (msg->msg_name == NULL || namelen == 0 ||
 983                     (s_flags & MSG_EOF)) {
 984                         error = EINVAL;
 985                         eprintsoline(so, error);
 986                         goto done;
 987                 }
 988                 flags = uiop->uio_fmode;
 989                 if (msg->msg_flags & MSG_DONTWAIT) {
 990                         flags |= FNDELAY;
 991                 }
 992                 so_lock_single(so);
 993                 error = sosctp_assoc_createconn(ss, msg->msg_name, namelen,
 994                     msg->msg_control, optlen, flags, cr, &ssa);
 995                 if (error) {
 996                         if ((so->so_version == SOV_XPG4_2) &&
 997                             (error == EHOSTUNREACH)) {
 998                                 error = ENETUNREACH;
 999                         }
1000                         if (ssa == NULL) {
1001                                 /*
1002                                  * Fatal error during connect(). Bail out.
1003                                  * If ssa exists, it means that the handshake
1004                                  * is in progress.
1005                                  */
1006                                 eprintsoline(so, error);
1007                                 so_unlock_single(so, SOLOCKED);
1008                                 goto done;
1009                         }
1010                         /*
1011                          * All the errors are non-fatal ones, don't return
1012                          * e.g. EINPROGRESS from sendmsg().
1013                          */
1014                         error = 0;
1015                 }
1016                 so_unlock_single(so, SOLOCKED);
1017         } else {
1018                 if ((error = sosctp_assoc(ss, aid, &ssa)) != 0) {
1019                         eprintsoline(so, error);
1020                         goto done;
1021                 }
1022         }
1023 
1024         /*
1025          * Now we have an association.
1026          */
1027         flags = msg->msg_flags;
1028 
1029         /*
1030          * MSG_EOF initiates graceful shutdown.
1031          */
1032         if (s_flags & MSG_EOF) {
1033                 if (uiop->uio_resid) {
1034                         /*
1035                          * Can't include data in MSG_EOF message.
1036                          */
1037                         error = EINVAL;
1038                 } else {
1039                         mutex_exit(&so->so_lock);
1040                         ssa->ssa_state |= SS_ISDISCONNECTING;
1041                         sctp_recvd(ssa->ssa_conn, so->so_rcvbuf);
1042                         error = sctp_disconnect(ssa->ssa_conn);
1043                         mutex_enter(&so->so_lock);
1044                 }
1045                 goto refrele;
1046         }
1047 
1048         for (;;) {
1049                 if (ssa->ssa_state & SS_CANTSENDMORE) {
1050                         SSA_REFRELE(ss, ssa);
1051                         mutex_exit(&so->so_lock);
1052                         return (EPIPE);
1053                 }
1054                 if (ssa->ssa_error != 0) {
1055                         error = ssa->ssa_error;
1056                         ssa->ssa_error = 0;
1057                         goto refrele;
1058                 }
1059 
1060                 if (!ssa->ssa_snd_qfull)
1061                         break;
1062 
1063                 if (so->so_state & SS_CLOSING) {
1064                         error = EINTR;
1065                         goto refrele;
1066                 }
1067                 if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) ||
1068                     (flags & MSG_DONTWAIT)) {
1069                         error = EAGAIN;
1070                         goto refrele;
1071                 } else {
1072                         /*
1073                          * Wait for space to become available and try again.
1074                          */
1075                         error = cv_wait_sig(&so->so_snd_cv, &so->so_lock);
1076                         if (!error) { /* signal */
1077                                 error = EINTR;
1078                                 goto refrele;
1079                         }
1080                 }
1081         }
1082 
1083         msglen = count = uiop->uio_resid;
1084 
1085         /* Don't allow sending a message larger than the send buffer size. */
1086         if (msglen > so->so_sndbuf) {
1087                 error = EMSGSIZE;
1088                 goto refrele;
1089         }
1090 
1091         /*
1092          * Update TX buffer usage here so that we can lift the socket lock.
1093          */
1094         mutex_exit(&so->so_lock);
1095 
1096         mctl = sctp_alloc_hdr(msg->msg_name, namelen, msg->msg_control,
1097             optlen, SCTP_CAN_BLOCK);
1098         if (mctl == NULL) {
1099                 error = EINTR;
1100                 goto lock_rele;
1101         }
1102 
1103         /* Copy in the message. */
1104         if ((error = sosctp_uiomove(mctl, count, ssa->ssa_wrsize,
1105             ssa->ssa_wroff, uiop, flags)) != 0) {
1106                 goto lock_rele;
1107         }
1108         error = sctp_sendmsg((struct sctp_s *)ssa->ssa_conn, mctl, 0);
1109 lock_rele:
1110         mutex_enter(&so->so_lock);
1111         if (error != 0) {
1112                 freemsg(mctl);
1113                 if ((error == EPIPE) && (ssa->ssa_state & SS_CANTSENDMORE)) {
1114                         /*
1115                          * We received shutdown between the time lock was
1116                          * lifted and call to sctp_sendmsg().
1117                          */
1118                         SSA_REFRELE(ss, ssa);
1119                         mutex_exit(&so->so_lock);
1120                         return (EPIPE);
1121                 }
1122         }
1123 
1124 refrele:
1125         SSA_REFRELE(ss, ssa);
1126 done:
1127         mutex_exit(&so->so_lock);
1128         return (error);
1129 }
1130 
1131 /*
1132  * Get address of remote node.
1133  */
1134 /* ARGSUSED */
1135 static int
1136 sosctp_getpeername(struct sonode *so, struct sockaddr *addr, socklen_t *addrlen,
1137     boolean_t accept, struct cred *cr)
1138 {
1139         return (sctp_getpeername((struct sctp_s *)so->so_proto_handle, addr,
1140             addrlen));
1141 }
1142 
1143 /*
1144  * Get local address.
1145  */
1146 /* ARGSUSED */
1147 static int
1148 sosctp_getsockname(struct sonode *so, struct sockaddr *addr, socklen_t *addrlen,
1149     struct cred *cr)
1150 {
1151         return (sctp_getsockname((struct sctp_s *)so->so_proto_handle, addr,
1152             addrlen));
1153 }
1154 
1155 /*
1156  * Called from shutdown().
1157  */
1158 /* ARGSUSED */
1159 static int
1160 sosctp_shutdown(struct sonode *so, int how, struct cred *cr)
1161 {
1162         uint_t state_change;
1163         int wakesig = 0;
1164         int error = 0;
1165 
1166         mutex_enter(&so->so_lock);
1167         /*
1168          * Record the current state and then perform any state changes.
1169          * Then use the difference between the old and new states to
1170          * determine which needs to be done.
1171          */
1172         state_change = so->so_state;
1173 
1174         switch (how) {
1175         case SHUT_RD:
1176                 socantrcvmore(so);
1177                 break;
1178         case SHUT_WR:
1179                 socantsendmore(so);
1180                 break;
1181         case SHUT_RDWR:
1182                 socantsendmore(so);
1183                 socantrcvmore(so);
1184                 break;
1185         default:
1186                 mutex_exit(&so->so_lock);
1187                 return (EINVAL);
1188         }
1189 
1190         state_change = so->so_state & ~state_change;
1191 
1192         if (state_change & SS_CANTRCVMORE) {
1193                 if (so->so_rcv_q_head == NULL) {
1194                         cv_signal(&so->so_rcv_cv);
1195                 }
1196                 wakesig = POLLIN|POLLRDNORM;
1197 
1198                 socket_sendsig(so, SOCKETSIG_READ);
1199         }
1200         if (state_change & SS_CANTSENDMORE) {
1201                 cv_broadcast(&so->so_snd_cv);
1202                 wakesig |= POLLOUT;
1203 
1204                 so->so_state |= SS_ISDISCONNECTING;
1205         }
1206         mutex_exit(&so->so_lock);
1207 
1208         pollwakeup(&so->so_poll_list, wakesig);
1209 
1210         if (state_change & SS_CANTSENDMORE) {
1211                 sctp_recvd((struct sctp_s *)so->so_proto_handle, so->so_rcvbuf);
1212                 error = sctp_disconnect((struct sctp_s *)so->so_proto_handle);
1213         }
1214 
1215         /*
1216          * HACK: sctp_disconnect() may return EWOULDBLOCK.  But this error is
1217          * not documented in standard socket API.  Catch it here.
1218          */
1219         if (error == EWOULDBLOCK)
1220                 error = 0;
1221         return (error);
1222 }
1223 
1224 /*
1225  * Get socket options.
1226  */
1227 /*ARGSUSED5*/
1228 static int
1229 sosctp_getsockopt(struct sonode *so, int level, int option_name,
1230     void *optval, socklen_t *optlenp, int flags, struct cred *cr)
1231 {
1232         socklen_t maxlen = *optlenp;
1233         socklen_t len;
1234         socklen_t optlen;
1235         uint8_t buffer[4];
1236         void    *optbuf = &buffer;
1237         int     error = 0;
1238 
1239         if (level == SOL_SOCKET) {
1240                 switch (option_name) {
1241                 /* Not supported options */
1242                 case SO_SNDTIMEO:
1243                 case SO_RCVTIMEO:
1244                 case SO_EXCLBIND:
1245                         eprintsoline(so, ENOPROTOOPT);
1246                         return (ENOPROTOOPT);
1247                 default:
1248                         error = socket_getopt_common(so, level, option_name,
1249                             optval, optlenp, flags);
1250                         if (error >= 0)
1251                                 return (error);
1252                         /* Pass the request to the protocol */
1253                         break;
1254                 }
1255         }
1256 
1257         if (level == IPPROTO_SCTP) {
1258                 /*
1259                  * Should go through ioctl().
1260                  */
1261                 return (EINVAL);
1262         }
1263 
1264         if (maxlen > sizeof (buffer)) {
1265                 optbuf = kmem_alloc(maxlen, KM_SLEEP);
1266         }
1267         optlen = maxlen;
1268 
1269         /*
1270          * If the resulting optlen is greater than the provided maxlen, then
1271          * we sliently trucate.
1272          */
1273         error = sctp_get_opt((struct sctp_s *)so->so_proto_handle, level,
1274             option_name, optbuf, &optlen);
1275 
1276         if (error != 0) {
1277                 eprintsoline(so, error);
1278                 goto free;
1279         }
1280         len = optlen;
1281 
1282 copyout:
1283 
1284         len = MIN(len, maxlen);
1285         bcopy(optbuf, optval, len);
1286         *optlenp = optlen;
1287 free:
1288         if (optbuf != &buffer) {
1289                 kmem_free(optbuf, maxlen);
1290         }
1291 
1292         return (error);
1293 }
1294 
1295 /*
1296  * Set socket options
1297  */
1298 /* ARGSUSED */
1299 static int
1300 sosctp_setsockopt(struct sonode *so, int level, int option_name,
1301     const void *optval, t_uscalar_t optlen, struct cred *cr)
1302 {
1303         struct sctp_sonode *ss = SOTOSSO(so);
1304         struct sctp_soassoc *ssa = NULL;
1305         sctp_assoc_t id;
1306         int error, rc;
1307         void *conn = NULL;
1308 
1309         mutex_enter(&so->so_lock);
1310 
1311         /*
1312          * For some SCTP level options, one can select the association this
1313          * applies to.
1314          */
1315         if (so->so_type == SOCK_STREAM) {
1316                 conn = so->so_proto_handle;
1317         } else {
1318                 /*
1319                  * SOCK_SEQPACKET only
1320                  */
1321                 id = 0;
1322                 if (level == IPPROTO_SCTP) {
1323                         switch (option_name) {
1324                         case SCTP_RTOINFO:
1325                         case SCTP_ASSOCINFO:
1326                         case SCTP_SET_PEER_PRIMARY_ADDR:
1327                         case SCTP_PRIMARY_ADDR:
1328                         case SCTP_PEER_ADDR_PARAMS:
1329                                 /*
1330                                  * Association ID is the first element
1331                                  * params struct
1332                                  */
1333                                 if (optlen < sizeof (sctp_assoc_t)) {
1334                                         error = EINVAL;
1335                                         eprintsoline(so, error);
1336                                         goto done;
1337                                 }
1338                                 id = *(sctp_assoc_t *)optval;
1339                                 break;
1340                         case SCTP_DEFAULT_SEND_PARAM:
1341                                 if (optlen != sizeof (struct sctp_sndrcvinfo)) {
1342                                         error = EINVAL;
1343                                         eprintsoline(so, error);
1344                                         goto done;
1345                                 }
1346                                 id = ((struct sctp_sndrcvinfo *)
1347                                     optval)->sinfo_assoc_id;
1348                                 break;
1349                         case SCTP_INITMSG:
1350                                 /*
1351                                  * Only applies to future associations
1352                                  */
1353                                 conn = so->so_proto_handle;
1354                                 break;
1355                         default:
1356                                 break;
1357                         }
1358                 } else if (level == SOL_SOCKET) {
1359                         if (option_name == SO_LINGER) {
1360                                 error = EOPNOTSUPP;
1361                                 eprintsoline(so, error);
1362                                 goto done;
1363                         }
1364                         /*
1365                          * These 2 options are applied to all associations.
1366                          * The other socket level options are only applied
1367                          * to the socket (not associations).
1368                          */
1369                         if ((option_name != SO_RCVBUF) &&
1370                             (option_name != SO_SNDBUF)) {
1371                                 conn = so->so_proto_handle;
1372                         }
1373                 } else {
1374                         conn = NULL;
1375                 }
1376 
1377                 /*
1378                  * If association ID was specified, do op on that assoc.
1379                  * Otherwise set the default setting of a socket.
1380                  */
1381                 if (id != 0) {
1382                         if ((error = sosctp_assoc(ss, id, &ssa)) != 0) {
1383                                 eprintsoline(so, error);
1384                                 goto done;
1385                         }
1386                         conn = ssa->ssa_conn;
1387                 }
1388         }
1389         dprint(2, ("sosctp_setsockopt %p (%d) - conn %p %d %d id:%d\n",
1390             (void *)ss, so->so_type, (void *)conn, level, option_name, id));
1391 
1392         ASSERT(ssa == NULL || (ssa != NULL && conn != NULL));
1393         if (conn != NULL) {
1394                 mutex_exit(&so->so_lock);
1395                 error = sctp_set_opt((struct sctp_s *)conn, level, option_name,
1396                     optval, optlen);
1397                 mutex_enter(&so->so_lock);
1398                 if (ssa != NULL)
1399                         SSA_REFRELE(ss, ssa);
1400         } else {
1401                 /*
1402                  * 1-N socket, and we have to apply the operation to ALL
1403                  * associations. Like with anything of this sort, the
1404                  * problem is what to do if the operation fails.
1405                  * Just try to apply the setting to everyone, but store
1406                  * error number if someone returns such.  And since we are
1407                  * looping through all possible aids, some of them can be
1408                  * invalid.  We just ignore this kind (sosctp_assoc()) of
1409                  * errors.
1410                  */
1411                 sctp_assoc_t aid;
1412 
1413                 mutex_exit(&so->so_lock);
1414                 error = sctp_set_opt((struct sctp_s *)so->so_proto_handle,
1415                     level, option_name, optval, optlen);
1416                 mutex_enter(&so->so_lock);
1417                 for (aid = 1; aid < ss->ss_maxassoc; aid++) {
1418                         if (sosctp_assoc(ss, aid, &ssa) != 0)
1419                                 continue;
1420                         mutex_exit(&so->so_lock);
1421                         rc = sctp_set_opt((struct sctp_s *)ssa->ssa_conn, level,
1422                             option_name, optval, optlen);
1423                         mutex_enter(&so->so_lock);
1424                         SSA_REFRELE(ss, ssa);
1425                         if (error == 0) {
1426                                 error = rc;
1427                         }
1428                 }
1429         }
1430 done:
1431         mutex_exit(&so->so_lock);
1432         return (error);
1433 }
1434 
1435 /*ARGSUSED*/
1436 static int
1437 sosctp_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode,
1438     struct cred *cr, int32_t *rvalp)
1439 {
1440         struct sctp_sonode      *ss;
1441         int32_t                 value;
1442         int                     error;
1443         int                     intval;
1444         pid_t                   pid;
1445         struct sctp_soassoc     *ssa;
1446         void                    *conn;
1447         void                    *buf;
1448         STRUCT_DECL(sctpopt, opt);
1449         uint32_t                optlen;
1450         int                     buflen;
1451 
1452         ss = SOTOSSO(so);
1453 
1454         /* handle socket specific ioctls */
1455         switch (cmd) {
1456         case FIONBIO:
1457                 if (so_copyin((void *)arg, &value, sizeof (int32_t),
1458                     (mode & (int)FKIOCTL))) {
1459                         return (EFAULT);
1460                 }
1461                 mutex_enter(&so->so_lock);
1462                 if (value) {
1463                         so->so_state |= SS_NDELAY;
1464                 } else {
1465                         so->so_state &= ~SS_NDELAY;
1466                 }
1467                 mutex_exit(&so->so_lock);
1468                 return (0);
1469 
1470         case FIOASYNC:
1471                 if (so_copyin((void *)arg, &value, sizeof (int32_t),
1472                     (mode & (int)FKIOCTL))) {
1473                         return (EFAULT);
1474                 }
1475                 mutex_enter(&so->so_lock);
1476 
1477                 if (value) {
1478                         /* Turn on SIGIO */
1479                         so->so_state |= SS_ASYNC;
1480                 } else {
1481                         /* Turn off SIGIO */
1482                         so->so_state &= ~SS_ASYNC;
1483                 }
1484                 mutex_exit(&so->so_lock);
1485                 return (0);
1486 
1487         case SIOCSPGRP:
1488         case FIOSETOWN:
1489                 if (so_copyin((void *)arg, &pid, sizeof (pid_t),
1490                     (mode & (int)FKIOCTL))) {
1491                         return (EFAULT);
1492                 }
1493                 mutex_enter(&so->so_lock);
1494 
1495                 error = (pid != so->so_pgrp) ? socket_chgpgrp(so, pid) : 0;
1496                 mutex_exit(&so->so_lock);
1497                 return (error);
1498 
1499         case SIOCGPGRP:
1500         case FIOGETOWN:
1501                 if (so_copyout(&so->so_pgrp, (void *)arg,
1502                     sizeof (pid_t), (mode & (int)FKIOCTL)))
1503                         return (EFAULT);
1504                 return (0);
1505 
1506         case FIONREAD:
1507                 /* XXX: Cannot be used unless standard buffer is used */
1508                 /*
1509                  * Return number of bytes of data in all data messages
1510                  * in queue in "arg".
1511                  * For stream socket, amount of available data.
1512                  * For sock_dgram, # of available bytes + addresses.
1513                  */
1514                 intval = (so->so_state & SS_ACCEPTCONN) ? 0 :
1515                     MIN(so->so_rcv_queued, INT_MAX);
1516                 if (so_copyout(&intval, (void *)arg, sizeof (intval),
1517                     (mode & (int)FKIOCTL)))
1518                         return (EFAULT);
1519                 return (0);
1520         case SIOCATMARK:
1521                 /*
1522                  * No support for urgent data.
1523                  */
1524                 intval = 0;
1525 
1526                 if (so_copyout(&intval, (void *)arg, sizeof (int),
1527                     (mode & (int)FKIOCTL)))
1528                         return (EFAULT);
1529                 return (0);
1530         case _I_GETPEERCRED: {
1531                 int error = 0;
1532 
1533                 if ((mode & FKIOCTL) == 0)
1534                         return (EINVAL);
1535 
1536                 mutex_enter(&so->so_lock);
1537                 if ((so->so_mode & SM_CONNREQUIRED) == 0) {
1538                         error = ENOTSUP;
1539                 } else if ((so->so_state & SS_ISCONNECTED) == 0) {
1540                         error = ENOTCONN;
1541                 } else if (so->so_peercred != NULL) {
1542                         k_peercred_t *kp = (k_peercred_t *)arg;
1543                         kp->pc_cr = so->so_peercred;
1544                         kp->pc_cpid = so->so_cpid;
1545                         crhold(so->so_peercred);
1546                 } else {
1547                         error = EINVAL;
1548                 }
1549                 mutex_exit(&so->so_lock);
1550                 return (error);
1551         }
1552         case SIOCSCTPGOPT:
1553                 STRUCT_INIT(opt, mode);
1554 
1555                 if (so_copyin((void *)arg, STRUCT_BUF(opt), STRUCT_SIZE(opt),
1556                     (mode & (int)FKIOCTL))) {
1557                         return (EFAULT);
1558                 }
1559                 if ((optlen = STRUCT_FGET(opt, sopt_len)) > SO_MAXARGSIZE)
1560                         return (EINVAL);
1561 
1562                 /*
1563                  * Find the correct sctp_t based on whether it is 1-N socket
1564                  * or not.
1565                  */
1566                 intval = STRUCT_FGET(opt, sopt_aid);
1567                 mutex_enter(&so->so_lock);
1568                 if ((so->so_type == SOCK_SEQPACKET) && intval) {
1569                         if ((error = sosctp_assoc(ss, intval, &ssa)) != 0) {
1570                                 mutex_exit(&so->so_lock);
1571                                 return (error);
1572                         }
1573                         conn = ssa->ssa_conn;
1574                         ASSERT(conn != NULL);
1575                 } else {
1576                         conn = so->so_proto_handle;
1577                         ssa = NULL;
1578                 }
1579                 mutex_exit(&so->so_lock);
1580 
1581                 /* Copyin the option buffer and then call sctp_get_opt(). */
1582                 buflen = optlen;
1583                 /* Let's allocate a buffer enough to hold an int */
1584                 if (buflen < sizeof (uint32_t))
1585                         buflen = sizeof (uint32_t);
1586                 buf = kmem_alloc(buflen, KM_SLEEP);
1587                 if (so_copyin(STRUCT_FGETP(opt, sopt_val), buf, optlen,
1588                     (mode & (int)FKIOCTL))) {
1589                         if (ssa != NULL) {
1590                                 mutex_enter(&so->so_lock);
1591                                 SSA_REFRELE(ss, ssa);
1592                                 mutex_exit(&so->so_lock);
1593                         }
1594                         kmem_free(buf, buflen);
1595                         return (EFAULT);
1596                 }
1597                 /* The option level has to be IPPROTO_SCTP */
1598                 error = sctp_get_opt((struct sctp_s *)conn, IPPROTO_SCTP,
1599                     STRUCT_FGET(opt, sopt_name), buf, &optlen);
1600                 if (ssa != NULL) {
1601                         mutex_enter(&so->so_lock);
1602                         SSA_REFRELE(ss, ssa);
1603                         mutex_exit(&so->so_lock);
1604                 }
1605                 optlen = MIN(buflen, optlen);
1606                 /* No error, copyout the result with the correct buf len. */
1607                 if (error == 0) {
1608                         STRUCT_FSET(opt, sopt_len, optlen);
1609                         if (so_copyout(STRUCT_BUF(opt), (void *)arg,
1610                             STRUCT_SIZE(opt), (mode & (int)FKIOCTL))) {
1611                                 error = EFAULT;
1612                         } else if (so_copyout(buf, STRUCT_FGETP(opt, sopt_val),
1613                             optlen, (mode & (int)FKIOCTL))) {
1614                                 error = EFAULT;
1615                         }
1616                 }
1617                 kmem_free(buf, buflen);
1618                 return (error);
1619 
1620         case SIOCSCTPSOPT:
1621                 STRUCT_INIT(opt, mode);
1622 
1623                 if (so_copyin((void *)arg, STRUCT_BUF(opt), STRUCT_SIZE(opt),
1624                     (mode & (int)FKIOCTL))) {
1625                         return (EFAULT);
1626                 }
1627                 if ((optlen = STRUCT_FGET(opt, sopt_len)) > SO_MAXARGSIZE)
1628                         return (EINVAL);
1629 
1630                 /*
1631                  * Find the correct sctp_t based on whether it is 1-N socket
1632                  * or not.
1633                  */
1634                 intval = STRUCT_FGET(opt, sopt_aid);
1635                 mutex_enter(&so->so_lock);
1636                 if (intval != 0) {
1637                         if ((error = sosctp_assoc(ss, intval, &ssa)) != 0) {
1638                                 mutex_exit(&so->so_lock);
1639                                 return (error);
1640                         }
1641                         conn = ssa->ssa_conn;
1642                         ASSERT(conn != NULL);
1643                 } else {
1644                         conn = so->so_proto_handle;
1645                         ssa = NULL;
1646                 }
1647                 mutex_exit(&so->so_lock);
1648 
1649                 /* Copyin the option buffer and then call sctp_set_opt(). */
1650                 buf = kmem_alloc(optlen, KM_SLEEP);
1651                 if (so_copyin(STRUCT_FGETP(opt, sopt_val), buf, optlen,
1652                     (mode & (int)FKIOCTL))) {
1653                         if (ssa != NULL) {
1654                                 mutex_enter(&so->so_lock);
1655                                 SSA_REFRELE(ss, ssa);
1656                                 mutex_exit(&so->so_lock);
1657                         }
1658                         kmem_free(buf, intval);
1659                         return (EFAULT);
1660                 }
1661                 /* The option level has to be IPPROTO_SCTP */
1662                 error = sctp_set_opt((struct sctp_s *)conn, IPPROTO_SCTP,
1663                     STRUCT_FGET(opt, sopt_name), buf, optlen);
1664                 if (ssa) {
1665                         mutex_enter(&so->so_lock);
1666                         SSA_REFRELE(ss, ssa);
1667                         mutex_exit(&so->so_lock);
1668                 }
1669                 kmem_free(buf, optlen);
1670                 return (error);
1671 
1672         case SIOCSCTPPEELOFF: {
1673                 struct sonode *nso;
1674                 struct sctp_uc_swap us;
1675                 int nfd;
1676                 struct file *nfp;
1677                 struct vnode *nvp = NULL;
1678                 struct sockparams *sp;
1679 
1680                 dprint(2, ("sctppeeloff %p\n", (void *)ss));
1681 
1682                 if (so->so_type != SOCK_SEQPACKET) {
1683                         return (EOPNOTSUPP);
1684                 }
1685                 if (so_copyin((void *)arg, &intval, sizeof (intval),
1686                     (mode & (int)FKIOCTL))) {
1687                         return (EFAULT);
1688                 }
1689                 if (intval == 0) {
1690                         return (EINVAL);
1691                 }
1692 
1693                 /*
1694                  * Find sockparams. This is different from parent's entry,
1695                  * as the socket type is different.
1696                  */
1697                 error = solookup(so->so_family, SOCK_STREAM, so->so_protocol,
1698                     &sp);
1699                 if (error != 0)
1700                         return (error);
1701 
1702                 /*
1703                  * Allocate the user fd.
1704                  */
1705                 if ((nfd = ufalloc(0)) == -1) {
1706                         eprintsoline(so, EMFILE);
1707                         SOCKPARAMS_DEC_REF(sp);
1708                         return (EMFILE);
1709                 }
1710 
1711                 /*
1712                  * Copy the fd out.
1713                  */
1714                 if (so_copyout(&nfd, (void *)arg, sizeof (nfd),
1715                     (mode & (int)FKIOCTL))) {
1716                         error = EFAULT;
1717                         goto err;
1718                 }
1719                 mutex_enter(&so->so_lock);
1720 
1721                 /*
1722                  * Don't use sosctp_assoc() in order to peel off disconnected
1723                  * associations.
1724                  */
1725                 ssa = ((uint32_t)intval >= ss->ss_maxassoc) ? NULL :
1726                     ss->ss_assocs[intval].ssi_assoc;
1727                 if (ssa == NULL) {
1728                         mutex_exit(&so->so_lock);
1729                         error = EINVAL;
1730                         goto err;
1731                 }
1732                 SSA_REFHOLD(ssa);
1733 
1734                 nso = socksctp_create(sp, so->so_family, SOCK_STREAM,
1735                     so->so_protocol, so->so_version, SOCKET_NOSLEEP,
1736                     &error, cr);
1737                 if (nso == NULL) {
1738                         SSA_REFRELE(ss, ssa);
1739                         mutex_exit(&so->so_lock);
1740                         goto err;
1741                 }
1742                 nvp = SOTOV(nso);
1743                 so_lock_single(so);
1744                 mutex_exit(&so->so_lock);
1745 
1746                 /* cannot fail, only inheriting properties */
1747                 (void) sosctp_init(nso, so, CRED(), 0);
1748 
1749                 /*
1750                  * We have a single ref on the new socket. This is normally
1751                  * handled by socket_{create,newconn}, but since they are not
1752                  * used we have to do it here.
1753                  */
1754                 nso->so_count = 1;
1755 
1756                 us.sus_handle = nso;
1757                 us.sus_upcalls = &sosctp_sock_upcalls;
1758 
1759                 /*
1760                  * Upcalls to new socket are blocked for the duration of
1761                  * downcall.
1762                  */
1763                 mutex_enter(&nso->so_lock);
1764 
1765                 error = sctp_set_opt((struct sctp_s *)ssa->ssa_conn,
1766                     IPPROTO_SCTP, SCTP_UC_SWAP, &us, sizeof (us));
1767                 if (error) {
1768                         goto peelerr;
1769                 }
1770                 error = falloc(nvp, FWRITE|FREAD, &nfp, NULL);
1771                 if (error) {
1772                         goto peelerr;
1773                 }
1774 
1775                 /*
1776                  * fill in the entries that falloc reserved
1777                  */
1778                 nfp->f_vnode = nvp;
1779                 mutex_exit(&nfp->f_tlock);
1780                 setf(nfd, nfp);
1781 
1782                 /* add curproc to the pid list associated with that file */
1783                 if (nfp->f_vnode != NULL)
1784                         (void) VOP_IOCTL(nfp->f_vnode, F_FORKED,
1785                             (intptr_t)curproc, FKIOCTL, kcred, NULL, NULL);
1786 
1787                 mutex_enter(&so->so_lock);
1788 
1789                 sosctp_assoc_move(ss, SOTOSSO(nso), ssa);
1790 
1791                 mutex_exit(&nso->so_lock);
1792 
1793                 ssa->ssa_conn = NULL;
1794                 sosctp_assoc_free(ss, ssa);
1795 
1796                 so_unlock_single(so, SOLOCKED);
1797                 mutex_exit(&so->so_lock);
1798 
1799                 return (0);
1800 
1801 err:
1802                 SOCKPARAMS_DEC_REF(sp);
1803                 setf(nfd, NULL);
1804                 eprintsoline(so, error);
1805                 return (error);
1806 
1807 peelerr:
1808                 mutex_exit(&nso->so_lock);
1809                 mutex_enter(&so->so_lock);
1810                 ASSERT(nso->so_count == 1);
1811                 nso->so_count = 0;
1812                 so_unlock_single(so, SOLOCKED);
1813                 SSA_REFRELE(ss, ssa);
1814                 mutex_exit(&so->so_lock);
1815 
1816                 setf(nfd, NULL);
1817                 ASSERT(nvp->v_count == 1);
1818                 socket_destroy(nso);
1819                 eprintsoline(so, error);
1820                 return (error);
1821         }
1822         default:
1823                 return (EINVAL);
1824         }
1825 }
1826 
1827 /*ARGSUSED*/
1828 static int
1829 sosctp_close(struct sonode *so, int flag, struct cred *cr)
1830 {
1831         struct sctp_sonode *ss;
1832         struct sctp_sa_id *ssi;
1833         struct sctp_soassoc *ssa;
1834         int32_t i;
1835 
1836         ss = SOTOSSO(so);
1837 
1838         /*
1839          * Initiate connection shutdown.  Tell SCTP if there is any data
1840          * left unread.
1841          */
1842         sctp_recvd((struct sctp_s *)so->so_proto_handle,
1843             so->so_rcvbuf - so->so_rcv_queued);
1844         (void) sctp_disconnect((struct sctp_s *)so->so_proto_handle);
1845 
1846         /*
1847          * New associations can't come in, but old ones might get
1848          * closed in upcall. Protect against that by taking a reference
1849          * on the association.
1850          */
1851         mutex_enter(&so->so_lock);
1852         ssi = ss->ss_assocs;
1853         for (i = 0; i < ss->ss_maxassoc; i++, ssi++) {
1854                 if ((ssa = ssi->ssi_assoc) != NULL) {
1855                         SSA_REFHOLD(ssa);
1856                         sosctp_assoc_isdisconnected(ssa, 0);
1857                         mutex_exit(&so->so_lock);
1858 
1859                         sctp_recvd(ssa->ssa_conn, so->so_rcvbuf -
1860                             ssa->ssa_rcv_queued);
1861                         (void) sctp_disconnect(ssa->ssa_conn);
1862 
1863                         mutex_enter(&so->so_lock);
1864                         SSA_REFRELE(ss, ssa);
1865                 }
1866         }
1867         mutex_exit(&so->so_lock);
1868 
1869         return (0);
1870 }
1871 
1872 /*
1873  * Closes incoming connections which were never accepted, frees
1874  * resources.
1875  */
1876 /* ARGSUSED */
1877 void
1878 sosctp_fini(struct sonode *so, struct cred *cr)
1879 {
1880         struct sctp_sonode *ss;
1881         struct sctp_sa_id *ssi;
1882         struct sctp_soassoc *ssa;
1883         int32_t i;
1884 
1885         ss = SOTOSSO(so);
1886 
1887         ASSERT(so->so_ops == &sosctp_sonodeops ||
1888             so->so_ops == &sosctp_seq_sonodeops);
1889 
1890         /* We are the sole owner of so now */
1891         mutex_enter(&so->so_lock);
1892 
1893         /* Free all pending connections */
1894         so_acceptq_flush(so, B_TRUE);
1895 
1896         ssi = ss->ss_assocs;
1897         for (i = 0; i < ss->ss_maxassoc; i++, ssi++) {
1898                 if ((ssa = ssi->ssi_assoc) != NULL) {
1899                         SSA_REFHOLD(ssa);
1900                         mutex_exit(&so->so_lock);
1901 
1902                         sctp_close((struct sctp_s *)ssa->ssa_conn);
1903 
1904                         mutex_enter(&so->so_lock);
1905                         ssa->ssa_conn = NULL;
1906                         sosctp_assoc_free(ss, ssa);
1907                 }
1908         }
1909         if (ss->ss_assocs != NULL) {
1910                 ASSERT(ss->ss_assoccnt == 0);
1911                 kmem_free(ss->ss_assocs,
1912                     ss->ss_maxassoc * sizeof (struct sctp_sa_id));
1913         }
1914         mutex_exit(&so->so_lock);
1915 
1916         if (so->so_proto_handle)
1917                 sctp_close((struct sctp_s *)so->so_proto_handle);
1918         so->so_proto_handle = NULL;
1919 
1920         /*
1921          * Note until sctp_close() is called, SCTP can still send up
1922          * messages, such as event notifications.  So we should flush
1923          * the recevie buffer after calling sctp_close().
1924          */
1925         mutex_enter(&so->so_lock);
1926         so_rcv_flush(so);
1927         mutex_exit(&so->so_lock);
1928 
1929         sonode_fini(so);
1930 }
1931 
1932 /*
1933  * Upcalls from SCTP
1934  */
1935 
1936 /*
1937  * This is the upcall function for 1-N (SOCK_SEQPACKET) socket when a new
1938  * association is created.  Note that the first argument (handle) is of type
1939  * sctp_sonode *, which is the one changed to a listener for new
1940  * associations.  All the other upcalls for 1-N socket take sctp_soassoc *
1941  * as handle.  The only exception is the su_properties upcall, which
1942  * can take both types as handle.
1943  */
1944 /* ARGSUSED */
1945 sock_upper_handle_t
1946 sctp_assoc_newconn(sock_upper_handle_t parenthandle,
1947     sock_lower_handle_t connind, sock_downcalls_t *dc,
1948     struct cred *peer_cred, pid_t peer_cpid, sock_upcalls_t **ucp)
1949 {
1950         struct sctp_sonode *lss = (struct sctp_sonode *)parenthandle;
1951         struct sonode *lso = &lss->ss_so;
1952         struct sctp_soassoc *ssa;
1953         sctp_assoc_t id;
1954 
1955         ASSERT(lss->ss_type == SOSCTP_SOCKET);
1956         ASSERT(lso->so_state & SS_ACCEPTCONN);
1957         ASSERT(lso->so_proto_handle != NULL); /* closed conn */
1958         ASSERT(lso->so_type == SOCK_SEQPACKET);
1959 
1960         mutex_enter(&lso->so_lock);
1961 
1962         if ((id = sosctp_aid_get(lss)) == -1) {
1963                 /*
1964                  * Array not large enough; increase size.
1965                  */
1966                 if (sosctp_aid_grow(lss, lss->ss_maxassoc, KM_NOSLEEP) < 0) {
1967                         mutex_exit(&lso->so_lock);
1968                         return (NULL);
1969                 }
1970                 id = sosctp_aid_get(lss);
1971                 ASSERT(id != -1);
1972         }
1973 
1974         /*
1975          * Create soassoc for this connection
1976          */
1977         ssa = sosctp_assoc_create(lss, KM_NOSLEEP);
1978         if (ssa == NULL) {
1979                 mutex_exit(&lso->so_lock);
1980                 return (NULL);
1981         }
1982         sosctp_aid_reserve(lss, id, 1);
1983         lss->ss_assocs[id].ssi_assoc = ssa;
1984         ++lss->ss_assoccnt;
1985         ssa->ssa_id = id;
1986         ssa->ssa_conn = (struct sctp_s *)connind;
1987         ssa->ssa_state = (SS_ISBOUND | SS_ISCONNECTED);
1988         ssa->ssa_wroff = lss->ss_wroff;
1989         ssa->ssa_wrsize = lss->ss_wrsize;
1990 
1991         mutex_exit(&lso->so_lock);
1992 
1993         *ucp = &sosctp_assoc_upcalls;
1994 
1995         return ((sock_upper_handle_t)ssa);
1996 }
1997 
1998 /* ARGSUSED */
1999 static void
2000 sctp_assoc_connected(sock_upper_handle_t handle, sock_connid_t id,
2001     struct cred *peer_cred, pid_t peer_cpid)
2002 {
2003         struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2004         struct sonode *so = &ssa->ssa_sonode->ss_so;
2005 
2006         ASSERT(so->so_type == SOCK_SEQPACKET);
2007         ASSERT(ssa->ssa_conn);
2008 
2009         mutex_enter(&so->so_lock);
2010         sosctp_assoc_isconnected(ssa);
2011         mutex_exit(&so->so_lock);
2012 }
2013 
2014 /* ARGSUSED */
2015 static int
2016 sctp_assoc_disconnected(sock_upper_handle_t handle, sock_connid_t id, int error)
2017 {
2018         struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2019         struct sonode *so = &ssa->ssa_sonode->ss_so;
2020         int ret;
2021 
2022         ASSERT(so->so_type == SOCK_SEQPACKET);
2023         ASSERT(ssa->ssa_conn != NULL);
2024 
2025         mutex_enter(&so->so_lock);
2026         sosctp_assoc_isdisconnected(ssa, error);
2027         if (ssa->ssa_refcnt == 1) {
2028                 ret = 1;
2029                 ssa->ssa_conn = NULL;
2030         } else {
2031                 ret = 0;
2032         }
2033         SSA_REFRELE(SOTOSSO(so), ssa);
2034 
2035         cv_broadcast(&so->so_snd_cv);
2036 
2037         mutex_exit(&so->so_lock);
2038 
2039         return (ret);
2040 }
2041 
2042 /* ARGSUSED */
2043 static void
2044 sctp_assoc_disconnecting(sock_upper_handle_t handle, sock_opctl_action_t action,
2045     uintptr_t arg)
2046 {
2047         struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2048         struct sonode *so = &ssa->ssa_sonode->ss_so;
2049 
2050         ASSERT(so->so_type == SOCK_SEQPACKET);
2051         ASSERT(ssa->ssa_conn != NULL);
2052         ASSERT(action == SOCK_OPCTL_SHUT_SEND);
2053 
2054         mutex_enter(&so->so_lock);
2055         sosctp_assoc_isdisconnecting(ssa);
2056         mutex_exit(&so->so_lock);
2057 }
2058 
2059 /* ARGSUSED */
2060 static ssize_t
2061 sctp_assoc_recv(sock_upper_handle_t handle, mblk_t *mp, size_t len, int flags,
2062     int *errorp, boolean_t *forcepush)
2063 {
2064         struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2065         struct sctp_sonode *ss = ssa->ssa_sonode;
2066         struct sonode *so = &ss->ss_so;
2067         struct T_unitdata_ind *tind;
2068         mblk_t *mp2;
2069         union sctp_notification *sn;
2070         struct sctp_sndrcvinfo *sinfo;
2071         ssize_t space_available;
2072 
2073         ASSERT(ssa->ssa_type == SOSCTP_ASSOC);
2074         ASSERT(so->so_type == SOCK_SEQPACKET);
2075         ASSERT(ssa->ssa_conn != NULL); /* closed conn */
2076         ASSERT(mp != NULL);
2077 
2078         ASSERT(errorp != NULL);
2079         *errorp = 0;
2080 
2081         /*
2082          * Should be getting T_unitdata_req's only.
2083          * Must have address as part of packet.
2084          */
2085         tind = (struct T_unitdata_ind *)mp->b_rptr;
2086         ASSERT((DB_TYPE(mp) == M_PROTO) &&
2087             (tind->PRIM_type == T_UNITDATA_IND));
2088         ASSERT(tind->SRC_length);
2089 
2090         mutex_enter(&so->so_lock);
2091 
2092         /*
2093          * For notify messages, need to fill in association id.
2094          * For data messages, sndrcvinfo could be in ancillary data.
2095          */
2096         if (mp->b_flag & SCTP_NOTIFICATION) {
2097                 mp2 = mp->b_cont;
2098                 sn = (union sctp_notification *)mp2->b_rptr;
2099                 switch (sn->sn_header.sn_type) {
2100                 case SCTP_ASSOC_CHANGE:
2101                         sn->sn_assoc_change.sac_assoc_id = ssa->ssa_id;
2102                         break;
2103                 case SCTP_PEER_ADDR_CHANGE:
2104                         sn->sn_paddr_change.spc_assoc_id = ssa->ssa_id;
2105                         break;
2106                 case SCTP_REMOTE_ERROR:
2107                         sn->sn_remote_error.sre_assoc_id = ssa->ssa_id;
2108                         break;
2109                 case SCTP_SEND_FAILED:
2110                         sn->sn_send_failed.ssf_assoc_id = ssa->ssa_id;
2111                         break;
2112                 case SCTP_SHUTDOWN_EVENT:
2113                         sn->sn_shutdown_event.sse_assoc_id = ssa->ssa_id;
2114                         break;
2115                 case SCTP_ADAPTATION_INDICATION:
2116                         sn->sn_adaptation_event.sai_assoc_id = ssa->ssa_id;
2117                         break;
2118                 case SCTP_PARTIAL_DELIVERY_EVENT:
2119                         sn->sn_pdapi_event.pdapi_assoc_id = ssa->ssa_id;
2120                         break;
2121                 default:
2122                         ASSERT(0);
2123                         break;
2124                 }
2125         } else {
2126                 if (tind->OPT_length > 0) {
2127                         struct cmsghdr  *cmsg;
2128                         char            *cend;
2129 
2130                         cmsg = (struct cmsghdr *)
2131                             ((uchar_t *)mp->b_rptr + tind->OPT_offset);
2132                         cend = (char *)cmsg + tind->OPT_length;
2133                         for (;;) {
2134                                 if ((char *)(cmsg + 1) > cend ||
2135                                     ((char *)cmsg + cmsg->cmsg_len) > cend) {
2136                                         break;
2137                                 }
2138                                 if ((cmsg->cmsg_level == IPPROTO_SCTP) &&
2139                                     (cmsg->cmsg_type == SCTP_SNDRCV)) {
2140                                         sinfo = (struct sctp_sndrcvinfo *)
2141                                             (cmsg + 1);
2142                                         sinfo->sinfo_assoc_id = ssa->ssa_id;
2143                                         break;
2144                                 }
2145                                 if (cmsg->cmsg_len > 0) {
2146                                         cmsg = (struct cmsghdr *)
2147                                             ((uchar_t *)cmsg + cmsg->cmsg_len);
2148                                 } else {
2149                                         break;
2150                                 }
2151                         }
2152                 }
2153         }
2154 
2155         /*
2156          * SCTP has reserved space in the header for storing a pointer.
2157          * Put the pointer to assocation there, and queue the data.
2158          */
2159         SSA_REFHOLD(ssa);
2160         ASSERT((mp->b_rptr - DB_BASE(mp)) >= sizeof (ssa));
2161         *(struct sctp_soassoc **)DB_BASE(mp) = ssa;
2162 
2163         ssa->ssa_rcv_queued += len;
2164         space_available = so->so_rcvbuf - ssa->ssa_rcv_queued;
2165         if (space_available <= 0)
2166                 ssa->ssa_flowctrld = B_TRUE;
2167 
2168         so_enqueue_msg(so, mp, len);
2169 
2170         /* so_notify_data drops so_lock */
2171         so_notify_data(so, len);
2172 
2173         return (space_available);
2174 }
2175 
2176 static void
2177 sctp_assoc_xmitted(sock_upper_handle_t handle, boolean_t qfull)
2178 {
2179         struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2180         struct sctp_sonode *ss = ssa->ssa_sonode;
2181 
2182         ASSERT(ssa->ssa_type == SOSCTP_ASSOC);
2183         ASSERT(ss->ss_so.so_type == SOCK_SEQPACKET);
2184         ASSERT(ssa->ssa_conn != NULL);
2185 
2186         mutex_enter(&ss->ss_so.so_lock);
2187 
2188         ssa->ssa_snd_qfull = qfull;
2189 
2190         /*
2191          * Wake blocked writers.
2192          */
2193         cv_broadcast(&ss->ss_so.so_snd_cv);
2194 
2195         mutex_exit(&ss->ss_so.so_lock);
2196 }
2197 
2198 static void
2199 sctp_assoc_properties(sock_upper_handle_t handle,
2200     struct sock_proto_props *soppp)
2201 {
2202         struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2203         struct sonode *so;
2204 
2205         if (ssa->ssa_type == SOSCTP_ASSOC) {
2206                 so = &ssa->ssa_sonode->ss_so;
2207 
2208                 mutex_enter(&so->so_lock);
2209 
2210                 /* Per assoc_id properties. */
2211                 if (soppp->sopp_flags & SOCKOPT_WROFF)
2212                         ssa->ssa_wroff = soppp->sopp_wroff;
2213                 if (soppp->sopp_flags & SOCKOPT_MAXBLK)
2214                         ssa->ssa_wrsize = soppp->sopp_maxblk;
2215         } else {
2216                 so = &((struct sctp_sonode *)handle)->ss_so;
2217                 mutex_enter(&so->so_lock);
2218 
2219                 if (soppp->sopp_flags & SOCKOPT_WROFF)
2220                         so->so_proto_props.sopp_wroff = soppp->sopp_wroff;
2221                 if (soppp->sopp_flags & SOCKOPT_MAXBLK)
2222                         so->so_proto_props.sopp_maxblk = soppp->sopp_maxblk;
2223                 if (soppp->sopp_flags & SOCKOPT_RCVHIWAT) {
2224                         ssize_t lowat;
2225 
2226                         so->so_rcvbuf = soppp->sopp_rxhiwat;
2227                         /*
2228                          * The low water mark should be adjusted properly
2229                          * if the high water mark is changed.  It should
2230                          * not be bigger than 1/4 of high water mark.
2231                          */
2232                         lowat = soppp->sopp_rxhiwat >> 2;
2233                         if (so->so_rcvlowat > lowat) {
2234                                 /* Sanity check... */
2235                                 if (lowat == 0)
2236                                         so->so_rcvlowat = soppp->sopp_rxhiwat;
2237                                 else
2238                                         so->so_rcvlowat = lowat;
2239                         }
2240                 }
2241         }
2242         mutex_exit(&so->so_lock);
2243 }
2244 
2245 static conn_pid_node_list_hdr_t *
2246 sctp_get_sock_pid_list(sock_upper_handle_t handle)
2247 {
2248         struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2249         struct sonode *so;
2250 
2251         if (ssa->ssa_type == SOSCTP_ASSOC)
2252                 so = &ssa->ssa_sonode->ss_so;
2253         else
2254                 so = &((struct sctp_sonode *)handle)->ss_so;
2255 
2256         return (so_get_sock_pid_list((sock_upper_handle_t)so));
2257 }