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/stream.h> 31 #include <sys/strsubr.h> 32 #include <sys/stropts.h> 33 #include <sys/strsun.h> 34 #define _SUN_TPI_VERSION 2 35 #include <sys/tihdr.h> 36 #include <sys/ddi.h> 37 #include <sys/sunddi.h> 38 #include <sys/xti_inet.h> 39 #include <sys/cmn_err.h> 40 #include <sys/debug.h> 41 #include <sys/vtrace.h> 42 #include <sys/kmem.h> 43 #include <sys/cpuvar.h> 44 #include <sys/random.h> 45 #include <sys/priv.h> 46 #include <sys/sunldi.h> 47 48 #include <sys/errno.h> 49 #include <sys/signal.h> 50 #include <sys/socket.h> 51 #include <sys/isa_defs.h> 52 #include <netinet/in.h> 53 #include <netinet/tcp.h> 54 #include <netinet/ip6.h> 55 #include <netinet/icmp6.h> 56 #include <netinet/sctp.h> 57 #include <net/if.h> 58 59 #include <inet/common.h> 60 #include <inet/ip.h> 61 #include <inet/ip6.h> 62 #include <inet/mi.h> 63 #include <inet/mib2.h> 64 #include <inet/kstatcom.h> 65 #include <inet/nd.h> 66 #include <inet/optcom.h> 67 #include <inet/ipclassifier.h> 68 #include <inet/ipsec_impl.h> 69 #include <inet/sctp_ip.h> 70 #include <inet/sctp_crc32.h> 71 72 #include "sctp_impl.h" 73 #include "sctp_addr.h" 74 #include "sctp_asconf.h" 75 76 extern major_t SCTP6_MAJ; 77 extern major_t SCTP_MAJ; 78 79 int sctpdebug; 80 sin6_t sctp_sin6_null; /* Zero address for quick clears */ 81 82 /* 83 * Have to ensure that sctp_g_q_close is not done by an 84 * interrupt thread. 85 */ 86 static taskq_t *sctp_taskq; 87 88 static void sctp_closei_local(sctp_t *sctp); 89 static int sctp_init_values(sctp_t *, sctp_t *, int); 90 static void sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp); 91 static void sctp_process_recvq(void *); 92 static void sctp_rq_tq_init(sctp_stack_t *); 93 static void sctp_rq_tq_fini(sctp_stack_t *); 94 static void sctp_conn_cache_init(); 95 static void sctp_conn_cache_fini(); 96 static int sctp_conn_cache_constructor(); 97 static void sctp_conn_cache_destructor(); 98 void sctp_g_q_setup(sctp_stack_t *); 99 void sctp_g_q_create(sctp_stack_t *); 100 void sctp_g_q_destroy(sctp_stack_t *); 101 102 static void *sctp_stack_init(netstackid_t stackid, netstack_t *ns); 103 static void sctp_stack_shutdown(netstackid_t stackid, void *arg); 104 static void sctp_stack_fini(netstackid_t stackid, void *arg); 105 106 /* 107 * SCTP receive queue taskq 108 * 109 * At SCTP initialization time, a default taskq is created for 110 * servicing packets received when the interrupt thread cannot 111 * get a hold on the sctp_t. The number of taskq can be increased in 112 * sctp_find_next_tq() when an existing taskq cannot be dispatched. 113 * The taskqs are never removed. But the max number of taskq which 114 * can be created is controlled by sctp_recvq_tq_list_max_sz. Note 115 * that SCTP recvq taskq is not tied to any specific CPU or ill. 116 * 117 * Those taskqs are stored in an array recvq_tq_list. And they are 118 * used in a round robin fashion. The current taskq being used is 119 * determined by recvq_tq_list_cur. 120 */ 121 122 /* /etc/system variables */ 123 /* The minimum number of threads for each taskq. */ 124 int sctp_recvq_tq_thr_min = 4; 125 /* The maximum number of threads for each taskq. */ 126 int sctp_recvq_tq_thr_max = 16; 127 /* The minimum number of tasks for each taskq. */ 128 int sctp_recvq_tq_task_min = 5; 129 /* The maxiimum number of tasks for each taskq. */ 130 int sctp_recvq_tq_task_max = 50; 131 132 /* sctp_t/conn_t kmem cache */ 133 struct kmem_cache *sctp_conn_cache; 134 135 #define SCTP_CONDEMNED(sctp) \ 136 mutex_enter(&(sctp)->sctp_reflock); \ 137 ((sctp)->sctp_condemned = B_TRUE); \ 138 mutex_exit(&(sctp)->sctp_reflock); 139 140 /* Link/unlink a sctp_t to/from the global list. */ 141 #define SCTP_LINK(sctp, sctps) \ 142 mutex_enter(&(sctps)->sctps_g_lock); \ 143 list_insert_tail(&sctps->sctps_g_list, (sctp)); \ 144 mutex_exit(&(sctps)->sctps_g_lock); 145 146 #define SCTP_UNLINK(sctp, sctps) \ 147 mutex_enter(&(sctps)->sctps_g_lock); \ 148 ASSERT((sctp)->sctp_condemned); \ 149 list_remove(&(sctps)->sctps_g_list, (sctp)); \ 150 mutex_exit(&(sctps)->sctps_g_lock); 151 152 /* 153 * Hooks for Sun Cluster. On non-clustered nodes these will remain NULL. 154 * PSARC/2005/602. 155 */ 156 void (*cl_sctp_listen)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL; 157 void (*cl_sctp_unlisten)(sa_family_t, uchar_t *, uint_t, in_port_t) = NULL; 158 void (*cl_sctp_connect)(sa_family_t, uchar_t *, uint_t, in_port_t, 159 uchar_t *, uint_t, in_port_t, boolean_t, cl_sctp_handle_t) = NULL; 160 void (*cl_sctp_disconnect)(sa_family_t, cl_sctp_handle_t) = NULL; 161 void (*cl_sctp_assoc_change)(sa_family_t, uchar_t *, size_t, uint_t, 162 uchar_t *, size_t, uint_t, int, cl_sctp_handle_t) = NULL; 163 void (*cl_sctp_check_addrs)(sa_family_t, in_port_t, uchar_t **, size_t, 164 uint_t *, boolean_t) = NULL; 165 /* 166 * Return the version number of the SCTP kernel interface. 167 */ 168 int 169 sctp_itf_ver(int cl_ver) 170 { 171 if (cl_ver != SCTP_ITF_VER) 172 return (-1); 173 return (SCTP_ITF_VER); 174 } 175 176 /* 177 * Called when we need a new sctp instantiation but don't really have a 178 * new q to hang it off of. Copy the priv flag from the passed in structure. 179 */ 180 sctp_t * 181 sctp_create_eager(sctp_t *psctp) 182 { 183 sctp_t *sctp; 184 mblk_t *ack_mp, *hb_mp; 185 conn_t *connp, *pconnp; 186 cred_t *credp; 187 sctp_stack_t *sctps = psctp->sctp_sctps; 188 189 if ((connp = ipcl_conn_create(IPCL_SCTPCONN, KM_NOSLEEP, 190 sctps->sctps_netstack)) == NULL) { 191 return (NULL); 192 } 193 194 connp->conn_ulp_labeled = is_system_labeled(); 195 196 sctp = CONN2SCTP(connp); 197 sctp->sctp_sctps = sctps; 198 199 if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL || 200 (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) { 201 if (ack_mp != NULL) 202 freeb(ack_mp); 203 netstack_rele(sctps->sctps_netstack); 204 connp->conn_netstack = NULL; 205 sctp->sctp_sctps = NULL; 206 SCTP_G_Q_REFRELE(sctps); 207 kmem_cache_free(sctp_conn_cache, connp); 208 return (NULL); 209 } 210 211 sctp->sctp_ack_mp = ack_mp; 212 sctp->sctp_heartbeat_mp = hb_mp; 213 214 /* Inherit information from the "parent" */ 215 sctp->sctp_ipversion = psctp->sctp_ipversion; 216 sctp->sctp_family = psctp->sctp_family; 217 pconnp = psctp->sctp_connp; 218 connp->conn_af_isv6 = pconnp->conn_af_isv6; 219 connp->conn_pkt_isv6 = pconnp->conn_pkt_isv6; 220 connp->conn_ipv6_v6only = pconnp->conn_ipv6_v6only; 221 if (sctp_init_values(sctp, psctp, KM_NOSLEEP) != 0) { 222 freeb(ack_mp); 223 freeb(hb_mp); 224 netstack_rele(sctps->sctps_netstack); 225 connp->conn_netstack = NULL; 226 sctp->sctp_sctps = NULL; 227 SCTP_G_Q_REFRELE(sctps); 228 kmem_cache_free(sctp_conn_cache, connp); 229 return (NULL); 230 } 231 232 /* 233 * If the parent is multilevel, then we'll fix up the remote cred 234 * when we do sctp_accept_comm. 235 */ 236 if ((credp = pconnp->conn_cred) != NULL) { 237 connp->conn_cred = credp; 238 crhold(credp); 239 /* 240 * If the caller has the process-wide flag set, then default to 241 * MAC exempt mode. This allows read-down to unlabeled hosts. 242 */ 243 if (getpflags(NET_MAC_AWARE, credp) != 0) 244 connp->conn_mac_exempt = B_TRUE; 245 } 246 247 connp->conn_allzones = pconnp->conn_allzones; 248 connp->conn_zoneid = pconnp->conn_zoneid; 249 250 sctp->sctp_mss = psctp->sctp_mss; 251 sctp->sctp_detached = B_TRUE; 252 /* 253 * Link to the global as soon as possible so that this sctp_t 254 * can be found. 255 */ 256 SCTP_LINK(sctp, sctps); 257 258 return (sctp); 259 } 260 261 /* 262 * We are dying for some reason. Try to do it gracefully. 263 */ 264 void 265 sctp_clean_death(sctp_t *sctp, int err) 266 { 267 ASSERT(sctp != NULL); 268 ASSERT((sctp->sctp_family == AF_INET && 269 sctp->sctp_ipversion == IPV4_VERSION) || 270 (sctp->sctp_family == AF_INET6 && 271 (sctp->sctp_ipversion == IPV4_VERSION || 272 sctp->sctp_ipversion == IPV6_VERSION))); 273 274 dprint(3, ("sctp_clean_death %p, state %d\n", (void *)sctp, 275 sctp->sctp_state)); 276 277 sctp->sctp_client_errno = err; 278 /* 279 * Check to see if we need to notify upper layer. 280 */ 281 if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) && 282 !SCTP_IS_DETACHED(sctp)) { 283 if (sctp->sctp_xmit_head || sctp->sctp_xmit_unsent) { 284 sctp_regift_xmitlist(sctp); 285 } 286 if (sctp->sctp_ulp_disconnected(sctp->sctp_ulpd, err)) { 287 /* 288 * Socket is gone, detach. 289 */ 290 sctp->sctp_detached = B_TRUE; 291 sctp->sctp_ulpd = NULL; 292 bzero(&sctp->sctp_upcalls, sizeof (sctp_upcalls_t)); 293 } 294 } 295 296 /* Remove this sctp from all hashes. */ 297 sctp_closei_local(sctp); 298 299 /* 300 * If the sctp_t is detached, we need to finish freeing up 301 * the resources. At this point, ip_fanout_sctp() should have 302 * a hold on this sctp_t. Some thread doing snmp stuff can 303 * have a hold. And a taskq can also have a hold waiting to 304 * work. sctp_unlink() the sctp_t from the global list so 305 * that no new thread can find it. Then do a SCTP_REFRELE(). 306 * The sctp_t will be freed after all those threads are done. 307 */ 308 if (SCTP_IS_DETACHED(sctp)) { 309 SCTP_CONDEMNED(sctp); 310 SCTP_REFRELE(sctp); 311 } 312 } 313 314 /* 315 * Called by upper layer when it wants to close this association. 316 * Depending on the state of this assoication, we need to do 317 * different things. 318 * 319 * If the state is below COOKIE_ECHOED or it is COOKIE_ECHOED but with 320 * no sent data, just remove this sctp from all the hashes. This 321 * makes sure that all packets from the other end will go to the default 322 * sctp handling. The upper layer will then do a sctp_close() to clean 323 * up. 324 * 325 * Otherwise, check and see if SO_LINGER is set. If it is set, check 326 * the value. If the value is 0, consider this an abortive close. Send 327 * an ABORT message and kill the associatiion. 328 * 329 */ 330 int 331 sctp_disconnect(sctp_t *sctp) 332 { 333 int error = 0; 334 335 dprint(3, ("sctp_disconnect %p, state %d\n", (void *)sctp, 336 sctp->sctp_state)); 337 338 RUN_SCTP(sctp); 339 340 switch (sctp->sctp_state) { 341 case SCTPS_IDLE: 342 case SCTPS_BOUND: 343 case SCTPS_LISTEN: 344 break; 345 case SCTPS_COOKIE_WAIT: 346 case SCTPS_COOKIE_ECHOED: 347 /* 348 * Close during the connect 3-way handshake 349 * but here there may or may not be pending data 350 * already on queue. Process almost same as in 351 * the ESTABLISHED state. 352 */ 353 if (sctp->sctp_xmit_head == NULL && 354 sctp->sctp_xmit_unsent == NULL) { 355 break; 356 } 357 /* FALLTHRU */ 358 default: 359 /* 360 * If SO_LINGER has set a zero linger time, abort the 361 * connection with a reset. 362 */ 363 if (sctp->sctp_linger && sctp->sctp_lingertime == 0) { 364 sctp_user_abort(sctp, NULL, B_FALSE); 365 break; 366 } 367 368 /* 369 * In there is unread data, send an ABORT 370 */ 371 if (sctp->sctp_rxqueued > 0 || sctp->sctp_irwnd > 372 sctp->sctp_rwnd) { 373 sctp_user_abort(sctp, NULL, B_FALSE); 374 break; 375 } 376 /* 377 * Transmit the shutdown before detaching the sctp_t. 378 * After sctp_detach returns this queue/perimeter 379 * no longer owns the sctp_t thus others can modify it. 380 */ 381 sctp_send_shutdown(sctp, 0); 382 383 /* Pass gathered wisdom to IP for keeping */ 384 sctp_update_ire(sctp); 385 386 /* 387 * If lingering on close then wait until the shutdown 388 * is complete, or the SO_LINGER time passes, or an 389 * ABORT is sent/received. Note that sctp_disconnect() 390 * can be called more than once. Make sure that only 391 * one thread waits. 392 */ 393 if (sctp->sctp_linger && sctp->sctp_lingertime > 0 && 394 sctp->sctp_state >= SCTPS_ESTABLISHED && 395 !sctp->sctp_lingering) { 396 clock_t stoptime; /* in ticks */ 397 clock_t ret; 398 399 /* 400 * Process the sendq to send the SHUTDOWN out 401 * before waiting. 402 */ 403 sctp_process_sendq(sctp); 404 405 sctp->sctp_lingering = 1; 406 sctp->sctp_client_errno = 0; 407 stoptime = lbolt + sctp->sctp_lingertime; 408 409 mutex_enter(&sctp->sctp_lock); 410 sctp->sctp_running = B_FALSE; 411 while (sctp->sctp_state >= SCTPS_ESTABLISHED && 412 sctp->sctp_client_errno == 0) { 413 cv_broadcast(&sctp->sctp_cv); 414 ret = cv_timedwait_sig(&sctp->sctp_cv, 415 &sctp->sctp_lock, stoptime); 416 if (ret < 0) { 417 /* Stoptime has reached. */ 418 sctp->sctp_client_errno = EWOULDBLOCK; 419 break; 420 } else if (ret == 0) { 421 /* Got a signal. */ 422 break; 423 } 424 } 425 error = sctp->sctp_client_errno; 426 sctp->sctp_client_errno = 0; 427 mutex_exit(&sctp->sctp_lock); 428 } 429 430 WAKE_SCTP(sctp); 431 sctp_process_sendq(sctp); 432 return (error); 433 } 434 435 436 /* Remove this sctp from all hashes so nobody can find it. */ 437 sctp_closei_local(sctp); 438 WAKE_SCTP(sctp); 439 return (error); 440 } 441 442 void 443 sctp_close(sctp_t *sctp) 444 { 445 dprint(3, ("sctp_close %p, state %d\n", (void *)sctp, 446 sctp->sctp_state)); 447 448 RUN_SCTP(sctp); 449 sctp->sctp_detached = 1; 450 sctp->sctp_ulpd = NULL; 451 bzero(&sctp->sctp_upcalls, sizeof (sctp_upcalls_t)); 452 bzero(&sctp->sctp_events, sizeof (sctp->sctp_events)); 453 454 /* If the graceful shutdown has not been completed, just return. */ 455 if (sctp->sctp_state != SCTPS_IDLE) { 456 WAKE_SCTP(sctp); 457 return; 458 } 459 460 /* 461 * Since sctp_t is in SCTPS_IDLE state, so the only thread which 462 * can have a hold on the sctp_t is doing snmp stuff. Just do 463 * a SCTP_REFRELE() here after the SCTP_UNLINK(). It will 464 * be freed when the other thread is done. 465 */ 466 SCTP_CONDEMNED(sctp); 467 WAKE_SCTP(sctp); 468 SCTP_REFRELE(sctp); 469 } 470 471 /* 472 * Unlink from global list and do the eager close. 473 * Remove the refhold implicit in being on the global list. 474 */ 475 void 476 sctp_close_eager(sctp_t *sctp) 477 { 478 SCTP_CONDEMNED(sctp); 479 sctp_closei_local(sctp); 480 SCTP_REFRELE(sctp); 481 } 482 483 /* 484 * The sctp_t is going away. Remove it from all lists and set it 485 * to SCTPS_IDLE. The caller has to remove it from the 486 * global list. The freeing up of memory is deferred until 487 * sctp_free(). This is needed since a thread in sctp_input() might have 488 * done a SCTP_REFHOLD on this structure before it was removed from the 489 * hashes. 490 */ 491 static void 492 sctp_closei_local(sctp_t *sctp) 493 { 494 mblk_t *mp; 495 ire_t *ire = NULL; 496 conn_t *connp = sctp->sctp_connp; 497 498 /* Stop and free the timers */ 499 sctp_free_faddr_timers(sctp); 500 if ((mp = sctp->sctp_heartbeat_mp) != NULL) { 501 sctp_timer_free(mp); 502 sctp->sctp_heartbeat_mp = NULL; 503 } 504 if ((mp = sctp->sctp_ack_mp) != NULL) { 505 sctp_timer_free(mp); 506 sctp->sctp_ack_mp = NULL; 507 } 508 509 /* Set the CONN_CLOSING flag so that IP will not cache IRE again. */ 510 mutex_enter(&connp->conn_lock); 511 connp->conn_state_flags |= CONN_CLOSING; 512 ire = connp->conn_ire_cache; 513 connp->conn_ire_cache = NULL; 514 mutex_exit(&connp->conn_lock); 515 if (ire != NULL) 516 IRE_REFRELE_NOTR(ire); 517 518 /* Remove from all hashes. */ 519 sctp_bind_hash_remove(sctp); 520 sctp_conn_hash_remove(sctp); 521 sctp_listen_hash_remove(sctp); 522 sctp->sctp_state = SCTPS_IDLE; 523 524 /* 525 * Clean up the recvq as much as possible. All those packets 526 * will be silently dropped as this sctp_t is now in idle state. 527 */ 528 mutex_enter(&sctp->sctp_recvq_lock); 529 while ((mp = sctp->sctp_recvq) != NULL) { 530 mblk_t *ipsec_mp; 531 532 sctp->sctp_recvq = mp->b_next; 533 mp->b_next = NULL; 534 if ((ipsec_mp = mp->b_prev) != NULL) { 535 freeb(ipsec_mp); 536 mp->b_prev = NULL; 537 } 538 freemsg(mp); 539 } 540 mutex_exit(&sctp->sctp_recvq_lock); 541 } 542 543 /* 544 * Free memory associated with the sctp/ip header template. 545 */ 546 static void 547 sctp_headers_free(sctp_t *sctp) 548 { 549 if (sctp->sctp_iphc != NULL) { 550 kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len); 551 sctp->sctp_iphc = NULL; 552 sctp->sctp_ipha = NULL; 553 sctp->sctp_hdr_len = 0; 554 sctp->sctp_ip_hdr_len = 0; 555 sctp->sctp_iphc_len = 0; 556 sctp->sctp_sctph = NULL; 557 sctp->sctp_hdr_len = 0; 558 } 559 if (sctp->sctp_iphc6 != NULL) { 560 kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len); 561 sctp->sctp_iphc6 = NULL; 562 sctp->sctp_ip6h = NULL; 563 sctp->sctp_hdr6_len = 0; 564 sctp->sctp_ip_hdr6_len = 0; 565 sctp->sctp_iphc6_len = 0; 566 sctp->sctp_sctph6 = NULL; 567 sctp->sctp_hdr6_len = 0; 568 } 569 } 570 571 static void 572 sctp_free_xmit_data(sctp_t *sctp) 573 { 574 mblk_t *ump = NULL; 575 mblk_t *nump; 576 mblk_t *mp; 577 mblk_t *nmp; 578 579 sctp->sctp_xmit_unacked = NULL; 580 ump = sctp->sctp_xmit_head; 581 sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL; 582 free_unsent: 583 for (; ump != NULL; ump = nump) { 584 for (mp = ump->b_cont; mp != NULL; mp = nmp) { 585 nmp = mp->b_next; 586 mp->b_next = NULL; 587 mp->b_prev = NULL; 588 freemsg(mp); 589 } 590 ASSERT(DB_REF(ump) == 1); 591 nump = ump->b_next; 592 ump->b_next = NULL; 593 ump->b_prev = NULL; 594 ump->b_cont = NULL; 595 freeb(ump); 596 } 597 if ((ump = sctp->sctp_xmit_unsent) == NULL) { 598 ASSERT(sctp->sctp_xmit_unsent_tail == NULL); 599 return; 600 } 601 sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL; 602 goto free_unsent; 603 } 604 605 /* 606 * Cleanup all the messages in the stream queue and the reassembly lists. 607 * If 'free' is true, then delete the streams as well. 608 */ 609 void 610 sctp_instream_cleanup(sctp_t *sctp, boolean_t free) 611 { 612 int i; 613 mblk_t *mp; 614 mblk_t *mp1; 615 616 if (sctp->sctp_instr != NULL) { 617 /* walk thru and flush out anything remaining in the Q */ 618 for (i = 0; i < sctp->sctp_num_istr; i++) { 619 mp = sctp->sctp_instr[i].istr_msgs; 620 while (mp != NULL) { 621 mp1 = mp->b_next; 622 mp->b_next = mp->b_prev = NULL; 623 freemsg(mp); 624 mp = mp1; 625 } 626 sctp->sctp_instr[i].istr_msgs = NULL; 627 sctp->sctp_instr[i].istr_nmsgs = 0; 628 sctp_free_reass((sctp->sctp_instr) + i); 629 sctp->sctp_instr[i].nextseq = 0; 630 } 631 if (free) { 632 kmem_free(sctp->sctp_instr, 633 sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr); 634 sctp->sctp_instr = NULL; 635 sctp->sctp_num_istr = 0; 636 } 637 } 638 /* un-ordered fragments */ 639 if (sctp->sctp_uo_frags != NULL) { 640 for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) { 641 mp1 = mp->b_next; 642 mp->b_next = mp->b_prev = NULL; 643 freemsg(mp); 644 } 645 } 646 } 647 648 /* 649 * Last reference to the sctp_t is gone. Free all memory associated with it. 650 * Called from SCTP_REFRELE. Called inline in sctp_close() 651 */ 652 void 653 sctp_free(conn_t *connp) 654 { 655 sctp_t *sctp = CONN2SCTP(connp); 656 int cnt; 657 sctp_stack_t *sctps = sctp->sctp_sctps; 658 netstack_t *ns; 659 660 ASSERT(sctps != NULL); 661 /* Unlink it from the global list */ 662 SCTP_UNLINK(sctp, sctps); 663 664 ASSERT(connp->conn_ref == 0); 665 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 666 ASSERT(!MUTEX_HELD(&sctp->sctp_reflock)); 667 ASSERT(sctp->sctp_refcnt == 0); 668 669 ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL); 670 ASSERT(sctp->sctp_conn_hash_next == NULL && 671 sctp->sctp_conn_hash_prev == NULL); 672 673 674 /* Free up all the resources. */ 675 676 /* blow away sctp stream management */ 677 if (sctp->sctp_ostrcntrs != NULL) { 678 kmem_free(sctp->sctp_ostrcntrs, 679 sizeof (uint16_t) * sctp->sctp_num_ostr); 680 sctp->sctp_ostrcntrs = NULL; 681 } 682 sctp_instream_cleanup(sctp, B_TRUE); 683 684 /* Remove all data transfer resources. */ 685 sctp->sctp_istr_nmsgs = 0; 686 sctp->sctp_rxqueued = 0; 687 sctp_free_xmit_data(sctp); 688 sctp->sctp_unacked = 0; 689 sctp->sctp_unsent = 0; 690 if (sctp->sctp_cxmit_list != NULL) 691 sctp_asconf_free_cxmit(sctp, NULL); 692 693 sctp->sctp_lastdata = NULL; 694 695 /* Clear out default xmit settings */ 696 sctp->sctp_def_stream = 0; 697 sctp->sctp_def_flags = 0; 698 sctp->sctp_def_ppid = 0; 699 sctp->sctp_def_context = 0; 700 sctp->sctp_def_timetolive = 0; 701 702 if (sctp->sctp_sack_info != NULL) { 703 sctp_free_set(sctp->sctp_sack_info); 704 sctp->sctp_sack_info = NULL; 705 } 706 sctp->sctp_sack_gaps = 0; 707 708 if (sctp->sctp_cookie_mp != NULL) { 709 freemsg(sctp->sctp_cookie_mp); 710 sctp->sctp_cookie_mp = NULL; 711 } 712 713 /* Remove all the address resources. */ 714 sctp_zap_addrs(sctp); 715 for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) { 716 ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0); 717 list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list); 718 } 719 720 ip6_pkt_free(&sctp->sctp_sticky_ipp); 721 722 if (sctp->sctp_hopopts != NULL) { 723 mi_free(sctp->sctp_hopopts); 724 sctp->sctp_hopopts = NULL; 725 sctp->sctp_hopoptslen = 0; 726 } 727 ASSERT(sctp->sctp_hopoptslen == 0); 728 if (sctp->sctp_dstopts != NULL) { 729 mi_free(sctp->sctp_dstopts); 730 sctp->sctp_dstopts = NULL; 731 sctp->sctp_dstoptslen = 0; 732 } 733 ASSERT(sctp->sctp_dstoptslen == 0); 734 if (sctp->sctp_rtdstopts != NULL) { 735 mi_free(sctp->sctp_rtdstopts); 736 sctp->sctp_rtdstopts = NULL; 737 sctp->sctp_rtdstoptslen = 0; 738 } 739 ASSERT(sctp->sctp_rtdstoptslen == 0); 740 if (sctp->sctp_rthdr != NULL) { 741 mi_free(sctp->sctp_rthdr); 742 sctp->sctp_rthdr = NULL; 743 sctp->sctp_rthdrlen = 0; 744 } 745 ASSERT(sctp->sctp_rthdrlen == 0); 746 sctp_headers_free(sctp); 747 748 sctp->sctp_shutdown_faddr = NULL; 749 750 /* Clear all the bitfields. */ 751 bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits)); 752 753 /* It is time to update the global statistics. */ 754 UPDATE_MIB(&sctps->sctps_mib, sctpOutSCTPPkts, sctp->sctp_opkts); 755 UPDATE_MIB(&sctps->sctps_mib, sctpOutCtrlChunks, sctp->sctp_obchunks); 756 UPDATE_MIB(&sctps->sctps_mib, sctpOutOrderChunks, sctp->sctp_odchunks); 757 UPDATE_MIB(&sctps->sctps_mib, 758 sctpOutUnorderChunks, sctp->sctp_oudchunks); 759 UPDATE_MIB(&sctps->sctps_mib, sctpRetransChunks, sctp->sctp_rxtchunks); 760 UPDATE_MIB(&sctps->sctps_mib, sctpInSCTPPkts, sctp->sctp_ipkts); 761 UPDATE_MIB(&sctps->sctps_mib, sctpInCtrlChunks, sctp->sctp_ibchunks); 762 UPDATE_MIB(&sctps->sctps_mib, sctpInOrderChunks, sctp->sctp_idchunks); 763 UPDATE_MIB(&sctps->sctps_mib, 764 sctpInUnorderChunks, sctp->sctp_iudchunks); 765 UPDATE_MIB(&sctps->sctps_mib, sctpFragUsrMsgs, sctp->sctp_fragdmsgs); 766 UPDATE_MIB(&sctps->sctps_mib, sctpReasmUsrMsgs, sctp->sctp_reassmsgs); 767 sctp->sctp_opkts = 0; 768 sctp->sctp_obchunks = 0; 769 sctp->sctp_odchunks = 0; 770 sctp->sctp_oudchunks = 0; 771 sctp->sctp_rxtchunks = 0; 772 sctp->sctp_ipkts = 0; 773 sctp->sctp_ibchunks = 0; 774 sctp->sctp_idchunks = 0; 775 sctp->sctp_iudchunks = 0; 776 sctp->sctp_fragdmsgs = 0; 777 sctp->sctp_reassmsgs = 0; 778 779 sctp->sctp_autoclose = 0; 780 sctp->sctp_tx_adaption_code = 0; 781 782 sctp->sctp_v6label_len = 0; 783 sctp->sctp_v4label_len = 0; 784 785 /* Clean up conn_t stuff */ 786 connp->conn_policy_cached = B_FALSE; 787 if (connp->conn_latch != NULL) { 788 IPLATCH_REFRELE(connp->conn_latch, connp->conn_netstack); 789 connp->conn_latch = NULL; 790 } 791 if (connp->conn_policy != NULL) { 792 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 793 connp->conn_policy = NULL; 794 } 795 if (connp->conn_ipsec_opt_mp != NULL) { 796 freemsg(connp->conn_ipsec_opt_mp); 797 connp->conn_ipsec_opt_mp = NULL; 798 } 799 if (connp->conn_cred != NULL) { 800 crfree(connp->conn_cred); 801 connp->conn_cred = NULL; 802 } 803 804 /* Every sctp_t holds one reference on the default queue */ 805 sctp->sctp_sctps = NULL; 806 SCTP_G_Q_REFRELE(sctps); 807 808 ns = connp->conn_netstack; 809 connp->conn_netstack = NULL; 810 netstack_rele(ns); 811 kmem_cache_free(sctp_conn_cache, connp); 812 } 813 814 /* Diagnostic routine used to return a string associated with the sctp state. */ 815 char * 816 sctp_display(sctp_t *sctp, char *sup_buf) 817 { 818 char *buf; 819 char buf1[30]; 820 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 821 char *cp; 822 823 if (sctp == NULL) 824 return ("NULL_SCTP"); 825 826 buf = (sup_buf != NULL) ? sup_buf : priv_buf; 827 828 switch (sctp->sctp_state) { 829 case SCTPS_IDLE: 830 cp = "SCTP_IDLE"; 831 break; 832 case SCTPS_BOUND: 833 cp = "SCTP_BOUND"; 834 break; 835 case SCTPS_LISTEN: 836 cp = "SCTP_LISTEN"; 837 break; 838 case SCTPS_COOKIE_WAIT: 839 cp = "SCTP_COOKIE_WAIT"; 840 break; 841 case SCTPS_COOKIE_ECHOED: 842 cp = "SCTP_COOKIE_ECHOED"; 843 break; 844 case SCTPS_ESTABLISHED: 845 cp = "SCTP_ESTABLISHED"; 846 break; 847 case SCTPS_SHUTDOWN_PENDING: 848 cp = "SCTP_SHUTDOWN_PENDING"; 849 break; 850 case SCTPS_SHUTDOWN_SENT: 851 cp = "SCTPS_SHUTDOWN_SENT"; 852 break; 853 case SCTPS_SHUTDOWN_RECEIVED: 854 cp = "SCTPS_SHUTDOWN_RECEIVED"; 855 break; 856 case SCTPS_SHUTDOWN_ACK_SENT: 857 cp = "SCTPS_SHUTDOWN_ACK_SENT"; 858 break; 859 default: 860 (void) mi_sprintf(buf1, "SCTPUnkState(%d)", sctp->sctp_state); 861 cp = buf1; 862 break; 863 } 864 (void) mi_sprintf(buf, "[%u, %u] %s", 865 ntohs(sctp->sctp_lport), ntohs(sctp->sctp_fport), cp); 866 867 return (buf); 868 } 869 870 /* 871 * Initialize protocol control block. If a parent exists, inherit 872 * all values set through setsockopt(). 873 */ 874 static int 875 sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep) 876 { 877 int err; 878 int cnt; 879 sctp_stack_t *sctps = sctp->sctp_sctps; 880 conn_t *connp, *pconnp; 881 882 ASSERT((sctp->sctp_family == AF_INET && 883 sctp->sctp_ipversion == IPV4_VERSION) || 884 (sctp->sctp_family == AF_INET6 && 885 (sctp->sctp_ipversion == IPV4_VERSION || 886 sctp->sctp_ipversion == IPV6_VERSION))); 887 888 sctp->sctp_nsaddrs = 0; 889 for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) { 890 sctp->sctp_saddrs[cnt].ipif_count = 0; 891 list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list, 892 sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t, 893 saddr_ipif)); 894 } 895 sctp->sctp_ports = 0; 896 sctp->sctp_running = B_FALSE; 897 sctp->sctp_state = SCTPS_IDLE; 898 899 sctp->sctp_refcnt = 1; 900 901 sctp->sctp_strikes = 0; 902 903 sctp->sctp_last_mtu_probe = lbolt64; 904 sctp->sctp_mtu_probe_intvl = sctps->sctps_mtu_probe_interval; 905 906 sctp->sctp_sack_gaps = 0; 907 sctp->sctp_sack_toggle = 2; 908 909 if (psctp != NULL) { 910 /* 911 * Inherit from parent 912 */ 913 sctp->sctp_iphc = kmem_zalloc(psctp->sctp_iphc_len, 914 KM_NOSLEEP); 915 if (sctp->sctp_iphc == NULL) 916 return (ENOMEM); 917 sctp->sctp_iphc_len = psctp->sctp_iphc_len; 918 sctp->sctp_hdr_len = psctp->sctp_hdr_len; 919 920 sctp->sctp_iphc6 = kmem_zalloc(psctp->sctp_iphc6_len, 921 KM_NOSLEEP); 922 if (sctp->sctp_iphc6 == NULL) { 923 sctp->sctp_iphc6_len = 0; 924 return (ENOMEM); 925 } 926 sctp->sctp_iphc6_len = psctp->sctp_iphc6_len; 927 sctp->sctp_hdr6_len = psctp->sctp_hdr6_len; 928 929 sctp->sctp_ip_hdr_len = psctp->sctp_ip_hdr_len; 930 sctp->sctp_ip_hdr6_len = psctp->sctp_ip_hdr6_len; 931 932 /* 933 * Copy the IP+SCTP header templates from listener 934 */ 935 bcopy(psctp->sctp_iphc, sctp->sctp_iphc, 936 psctp->sctp_hdr_len); 937 sctp->sctp_ipha = (ipha_t *)sctp->sctp_iphc; 938 sctp->sctp_sctph = (sctp_hdr_t *)(sctp->sctp_iphc + 939 sctp->sctp_ip_hdr_len); 940 941 bcopy(psctp->sctp_iphc6, sctp->sctp_iphc6, 942 psctp->sctp_hdr6_len); 943 if (((ip6i_t *)(sctp->sctp_iphc6))->ip6i_nxt == IPPROTO_RAW) { 944 sctp->sctp_ip6h = (ip6_t *)(sctp->sctp_iphc6 + 945 sizeof (ip6i_t)); 946 } else { 947 sctp->sctp_ip6h = (ip6_t *)sctp->sctp_iphc6; 948 } 949 sctp->sctp_sctph6 = (sctp_hdr_t *)(sctp->sctp_iphc6 + 950 sctp->sctp_ip_hdr6_len); 951 952 sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime; 953 sctp->sctp_xmit_lowater = psctp->sctp_xmit_lowater; 954 sctp->sctp_xmit_hiwater = psctp->sctp_xmit_hiwater; 955 sctp->sctp_cwnd_max = psctp->sctp_cwnd_max; 956 sctp->sctp_rwnd = psctp->sctp_rwnd; 957 sctp->sctp_irwnd = psctp->sctp_rwnd; 958 sctp->sctp_pd_point = psctp->sctp_pd_point; 959 sctp->sctp_rto_max = psctp->sctp_rto_max; 960 sctp->sctp_init_rto_max = psctp->sctp_init_rto_max; 961 sctp->sctp_rto_min = psctp->sctp_rto_min; 962 sctp->sctp_rto_initial = psctp->sctp_rto_initial; 963 sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt; 964 sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt; 965 sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt; 966 967 sctp->sctp_def_stream = psctp->sctp_def_stream; 968 sctp->sctp_def_flags = psctp->sctp_def_flags; 969 sctp->sctp_def_ppid = psctp->sctp_def_ppid; 970 sctp->sctp_def_context = psctp->sctp_def_context; 971 sctp->sctp_def_timetolive = psctp->sctp_def_timetolive; 972 973 sctp->sctp_num_istr = psctp->sctp_num_istr; 974 sctp->sctp_num_ostr = psctp->sctp_num_ostr; 975 976 sctp->sctp_hb_interval = psctp->sctp_hb_interval; 977 sctp->sctp_autoclose = psctp->sctp_autoclose; 978 sctp->sctp_tx_adaption_code = psctp->sctp_tx_adaption_code; 979 980 /* xxx should be a better way to copy these flags xxx */ 981 sctp->sctp_debug = psctp->sctp_debug; 982 sctp->sctp_bound_to_all = psctp->sctp_bound_to_all; 983 sctp->sctp_cansleep = psctp->sctp_cansleep; 984 sctp->sctp_send_adaption = psctp->sctp_send_adaption; 985 sctp->sctp_ndelay = psctp->sctp_ndelay; 986 sctp->sctp_events = psctp->sctp_events; 987 sctp->sctp_ipv6_recvancillary = psctp->sctp_ipv6_recvancillary; 988 989 /* Copy IP-layer options */ 990 connp = sctp->sctp_connp; 991 pconnp = psctp->sctp_connp; 992 993 connp->conn_broadcast = pconnp->conn_broadcast; 994 connp->conn_loopback = pconnp->conn_loopback; 995 connp->conn_dontroute = pconnp->conn_dontroute; 996 connp->conn_reuseaddr = pconnp->conn_reuseaddr; 997 998 } else { 999 /* 1000 * Initialize the header template 1001 */ 1002 if ((err = sctp_header_init_ipv4(sctp, sleep)) != 0) { 1003 return (err); 1004 } 1005 if ((err = sctp_header_init_ipv6(sctp, sleep)) != 0) { 1006 return (err); 1007 } 1008 1009 /* 1010 * Set to system defaults 1011 */ 1012 sctp->sctp_cookie_lifetime = 1013 MSEC_TO_TICK(sctps->sctps_cookie_life); 1014 sctp->sctp_xmit_lowater = sctps->sctps_xmit_lowat; 1015 sctp->sctp_xmit_hiwater = sctps->sctps_xmit_hiwat; 1016 sctp->sctp_cwnd_max = sctps->sctps_cwnd_max_; 1017 sctp->sctp_rwnd = sctps->sctps_recv_hiwat; 1018 sctp->sctp_irwnd = sctp->sctp_rwnd; 1019 sctp->sctp_pd_point = sctp->sctp_rwnd; 1020 sctp->sctp_rto_max = MSEC_TO_TICK(sctps->sctps_rto_maxg); 1021 sctp->sctp_init_rto_max = sctp->sctp_rto_max; 1022 sctp->sctp_rto_min = MSEC_TO_TICK(sctps->sctps_rto_ming); 1023 sctp->sctp_rto_initial = MSEC_TO_TICK( 1024 sctps->sctps_rto_initialg); 1025 sctp->sctp_pa_max_rxt = sctps->sctps_pa_max_retr; 1026 sctp->sctp_pp_max_rxt = sctps->sctps_pp_max_retr; 1027 sctp->sctp_max_init_rxt = sctps->sctps_max_init_retr; 1028 1029 sctp->sctp_num_istr = sctps->sctps_max_in_streams; 1030 sctp->sctp_num_ostr = sctps->sctps_initial_out_streams; 1031 1032 sctp->sctp_hb_interval = 1033 MSEC_TO_TICK(sctps->sctps_heartbeat_interval); 1034 } 1035 sctp->sctp_understands_asconf = B_TRUE; 1036 sctp->sctp_understands_addip = B_TRUE; 1037 sctp->sctp_prsctp_aware = B_FALSE; 1038 1039 sctp->sctp_connp->conn_ref = 1; 1040 sctp->sctp_connp->conn_fully_bound = B_FALSE; 1041 1042 sctp->sctp_prsctpdrop = 0; 1043 sctp->sctp_msgcount = 0; 1044 1045 return (0); 1046 } 1047 1048 /* 1049 * Extracts the init tag from an INIT chunk and checks if it matches 1050 * the sctp's verification tag. Returns 0 if it doesn't match, 1 if 1051 * it does. 1052 */ 1053 static boolean_t 1054 sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp) 1055 { 1056 sctp_chunk_hdr_t *sch; 1057 uint32_t verf, *vp; 1058 1059 sch = (sctp_chunk_hdr_t *)(sh + 1); 1060 vp = (uint32_t *)(sch + 1); 1061 1062 /* Need at least the data chunk hdr and the first 4 bytes of INIT */ 1063 if ((unsigned char *)(vp + 1) > mp->b_wptr) { 1064 return (B_FALSE); 1065 } 1066 1067 bcopy(vp, &verf, sizeof (verf)); 1068 1069 if (verf == sctp->sctp_lvtag) { 1070 return (B_TRUE); 1071 } 1072 return (B_FALSE); 1073 } 1074 1075 /* 1076 * sctp_icmp_error is called by sctp_input() to process ICMP error messages 1077 * passed up by IP. The queue is the default queue. We need to find a sctp_t 1078 * that corresponds to the returned datagram. Passes the message back in on 1079 * the correct queue once it has located the connection. 1080 * Assumes that IP has pulled up everything up to and including 1081 * the ICMP header. 1082 */ 1083 void 1084 sctp_icmp_error(sctp_t *sctp, mblk_t *mp) 1085 { 1086 icmph_t *icmph; 1087 ipha_t *ipha; 1088 int iph_hdr_length; 1089 sctp_hdr_t *sctph; 1090 mblk_t *first_mp; 1091 uint32_t new_mtu; 1092 in6_addr_t dst; 1093 sctp_faddr_t *fp; 1094 sctp_stack_t *sctps = sctp->sctp_sctps; 1095 1096 dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp, 1097 (void *)mp)); 1098 1099 first_mp = mp; 1100 1101 ipha = (ipha_t *)mp->b_rptr; 1102 if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) { 1103 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION); 1104 sctp_icmp_error_ipv6(sctp, first_mp); 1105 return; 1106 } 1107 1108 iph_hdr_length = IPH_HDR_LENGTH(ipha); 1109 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 1110 ipha = (ipha_t *)&icmph[1]; 1111 iph_hdr_length = IPH_HDR_LENGTH(ipha); 1112 sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length); 1113 if ((uchar_t *)(sctph + 1) >= mp->b_wptr) { 1114 /* not enough data for SCTP header */ 1115 freemsg(first_mp); 1116 return; 1117 } 1118 1119 switch (icmph->icmph_type) { 1120 case ICMP_DEST_UNREACHABLE: 1121 switch (icmph->icmph_code) { 1122 case ICMP_FRAGMENTATION_NEEDED: 1123 /* 1124 * Reduce the MSS based on the new MTU. This will 1125 * eliminate any fragmentation locally. 1126 * N.B. There may well be some funny side-effects on 1127 * the local send policy and the remote receive policy. 1128 * Pending further research, we provide 1129 * sctp_ignore_path_mtu just in case this proves 1130 * disastrous somewhere. 1131 * 1132 * After updating the MSS, retransmit part of the 1133 * dropped segment using the new mss by calling 1134 * sctp_wput_slow(). Need to adjust all those 1135 * params to make sure sctp_wput_slow() work properly. 1136 */ 1137 if (sctps->sctps_ignore_path_mtu) 1138 break; 1139 1140 /* find the offending faddr */ 1141 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst); 1142 fp = sctp_lookup_faddr(sctp, &dst); 1143 if (fp == NULL) { 1144 break; 1145 } 1146 1147 new_mtu = ntohs(icmph->icmph_du_mtu); 1148 1149 if (new_mtu - sctp->sctp_hdr_len >= fp->sfa_pmss) 1150 break; 1151 1152 /* 1153 * Make sure that sfa_pmss is a multiple of 1154 * SCTP_ALIGN. 1155 */ 1156 fp->sfa_pmss = (new_mtu - sctp->sctp_hdr_len) & 1157 ~(SCTP_ALIGN - 1); 1158 fp->pmtu_discovered = 1; 1159 1160 break; 1161 case ICMP_PORT_UNREACHABLE: 1162 case ICMP_PROTOCOL_UNREACHABLE: 1163 switch (sctp->sctp_state) { 1164 case SCTPS_COOKIE_WAIT: 1165 case SCTPS_COOKIE_ECHOED: 1166 /* make sure the verification tag matches */ 1167 if (!sctp_icmp_verf(sctp, sctph, mp)) { 1168 break; 1169 } 1170 BUMP_MIB(&sctps->sctps_mib, sctpAborted); 1171 sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0, 1172 NULL); 1173 sctp_clean_death(sctp, ECONNREFUSED); 1174 break; 1175 } 1176 break; 1177 case ICMP_HOST_UNREACHABLE: 1178 case ICMP_NET_UNREACHABLE: 1179 /* Record the error in case we finally time out. */ 1180 sctp->sctp_client_errno = (icmph->icmph_code == 1181 ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH; 1182 break; 1183 default: 1184 break; 1185 } 1186 break; 1187 case ICMP_SOURCE_QUENCH: { 1188 /* Reduce the sending rate as if we got a retransmit timeout */ 1189 break; 1190 } 1191 } 1192 freemsg(first_mp); 1193 } 1194 1195 /* 1196 * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6 1197 * error messages passed up by IP. 1198 * Assumes that IP has pulled up all the extension headers as well 1199 * as the ICMPv6 header. 1200 */ 1201 static void 1202 sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp) 1203 { 1204 icmp6_t *icmp6; 1205 ip6_t *ip6h; 1206 uint16_t iph_hdr_length; 1207 sctp_hdr_t *sctpha; 1208 uint8_t *nexthdrp; 1209 uint32_t new_mtu; 1210 sctp_faddr_t *fp; 1211 sctp_stack_t *sctps = sctp->sctp_sctps; 1212 1213 ip6h = (ip6_t *)mp->b_rptr; 1214 iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ? 1215 ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN; 1216 1217 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 1218 ip6h = (ip6_t *)&icmp6[1]; 1219 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) { 1220 freemsg(mp); 1221 return; 1222 } 1223 ASSERT(*nexthdrp == IPPROTO_SCTP); 1224 1225 /* XXX need ifindex to find connection */ 1226 sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length); 1227 if ((uchar_t *)sctpha >= mp->b_wptr) { 1228 /* not enough data for SCTP header */ 1229 freemsg(mp); 1230 return; 1231 } 1232 switch (icmp6->icmp6_type) { 1233 case ICMP6_PACKET_TOO_BIG: 1234 /* 1235 * Reduce the MSS based on the new MTU. This will 1236 * eliminate any fragmentation locally. 1237 * N.B. There may well be some funny side-effects on 1238 * the local send policy and the remote receive policy. 1239 * Pending further research, we provide 1240 * sctp_ignore_path_mtu just in case this proves 1241 * disastrous somewhere. 1242 * 1243 * After updating the MSS, retransmit part of the 1244 * dropped segment using the new mss by calling 1245 * sctp_wput_slow(). Need to adjust all those 1246 * params to make sure sctp_wput_slow() work properly. 1247 */ 1248 if (sctps->sctps_ignore_path_mtu) 1249 break; 1250 1251 /* find the offending faddr */ 1252 fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst); 1253 if (fp == NULL) { 1254 break; 1255 } 1256 1257 new_mtu = ntohs(icmp6->icmp6_mtu); 1258 1259 if (new_mtu - sctp->sctp_hdr6_len >= fp->sfa_pmss) 1260 break; 1261 1262 /* Make sure that sfa_pmss is a multiple of SCTP_ALIGN. */ 1263 fp->sfa_pmss = (new_mtu - sctp->sctp_hdr6_len) & 1264 ~(SCTP_ALIGN - 1); 1265 fp->pmtu_discovered = 1; 1266 1267 break; 1268 1269 case ICMP6_DST_UNREACH: 1270 switch (icmp6->icmp6_code) { 1271 case ICMP6_DST_UNREACH_NOPORT: 1272 /* make sure the verification tag matches */ 1273 if (!sctp_icmp_verf(sctp, sctpha, mp)) { 1274 break; 1275 } 1276 if (sctp->sctp_state == SCTPS_COOKIE_WAIT || 1277 sctp->sctp_state == SCTPS_COOKIE_ECHOED) { 1278 BUMP_MIB(&sctps->sctps_mib, sctpAborted); 1279 sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0, 1280 NULL); 1281 sctp_clean_death(sctp, ECONNREFUSED); 1282 } 1283 break; 1284 1285 case ICMP6_DST_UNREACH_ADMIN: 1286 case ICMP6_DST_UNREACH_NOROUTE: 1287 case ICMP6_DST_UNREACH_NOTNEIGHBOR: 1288 case ICMP6_DST_UNREACH_ADDR: 1289 /* Record the error in case we finally time out. */ 1290 sctp->sctp_client_errno = EHOSTUNREACH; 1291 break; 1292 default: 1293 break; 1294 } 1295 break; 1296 1297 case ICMP6_PARAM_PROB: 1298 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 1299 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 1300 (uchar_t *)ip6h + icmp6->icmp6_pptr == 1301 (uchar_t *)nexthdrp) { 1302 /* make sure the verification tag matches */ 1303 if (!sctp_icmp_verf(sctp, sctpha, mp)) { 1304 break; 1305 } 1306 if (sctp->sctp_state == SCTPS_COOKIE_WAIT) { 1307 BUMP_MIB(&sctps->sctps_mib, sctpAborted); 1308 sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0, 1309 NULL); 1310 sctp_clean_death(sctp, ECONNREFUSED); 1311 } 1312 break; 1313 } 1314 break; 1315 1316 case ICMP6_TIME_EXCEEDED: 1317 default: 1318 break; 1319 } 1320 freemsg(mp); 1321 } 1322 1323 /* 1324 * Called by sockfs to create a new sctp instance. 1325 * 1326 * If parent pointer is passed in, inherit settings from it. 1327 */ 1328 sctp_t * 1329 sctp_create(void *sctp_ulpd, sctp_t *parent, int family, int flags, 1330 const sctp_upcalls_t *sctp_upcalls, sctp_sockbuf_limits_t *sbl, 1331 cred_t *credp) 1332 { 1333 sctp_t *sctp, *psctp; 1334 conn_t *sctp_connp; 1335 mblk_t *ack_mp, *hb_mp; 1336 int sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP; 1337 zoneid_t zoneid; 1338 sctp_stack_t *sctps; 1339 1340 /* User must supply a credential. */ 1341 if (credp == NULL) 1342 return (NULL); 1343 1344 psctp = (sctp_t *)parent; 1345 if (psctp != NULL) { 1346 sctps = psctp->sctp_sctps; 1347 /* Increase here to have common decrease at end */ 1348 netstack_hold(sctps->sctps_netstack); 1349 } else { 1350 netstack_t *ns; 1351 1352 ns = netstack_find_by_cred(credp); 1353 ASSERT(ns != NULL); 1354 sctps = ns->netstack_sctp; 1355 ASSERT(sctps != NULL); 1356 1357 /* 1358 * For exclusive stacks we set the zoneid to zero 1359 * to make SCTP operate as if in the global zone. 1360 */ 1361 if (sctps->sctps_netstack->netstack_stackid != 1362 GLOBAL_NETSTACKID) 1363 zoneid = GLOBAL_ZONEID; 1364 else 1365 zoneid = crgetzoneid(credp); 1366 1367 /* 1368 * For stackid zero this is done from strplumb.c, but 1369 * non-zero stackids are handled here. 1370 */ 1371 if (sctps->sctps_g_q == NULL && 1372 sctps->sctps_netstack->netstack_stackid != 1373 GLOBAL_NETSTACKID) { 1374 sctp_g_q_setup(sctps); 1375 } 1376 } 1377 if ((sctp_connp = ipcl_conn_create(IPCL_SCTPCONN, sleep, 1378 sctps->sctps_netstack)) == NULL) { 1379 netstack_rele(sctps->sctps_netstack); 1380 SCTP_KSTAT(sctps, sctp_conn_create); 1381 return (NULL); 1382 } 1383 /* 1384 * ipcl_conn_create did a netstack_hold. Undo the hold that was 1385 * done at top of sctp_create. 1386 */ 1387 netstack_rele(sctps->sctps_netstack); 1388 sctp = CONN2SCTP(sctp_connp); 1389 sctp->sctp_sctps = sctps; 1390 1391 sctp_connp->conn_ulp_labeled = is_system_labeled(); 1392 if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer)) == NULL || 1393 (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer)) == NULL) { 1394 if (ack_mp != NULL) 1395 freeb(ack_mp); 1396 netstack_rele(sctp_connp->conn_netstack); 1397 sctp_connp->conn_netstack = NULL; 1398 sctp->sctp_sctps = NULL; 1399 SCTP_G_Q_REFRELE(sctps); 1400 kmem_cache_free(sctp_conn_cache, sctp_connp); 1401 return (NULL); 1402 } 1403 1404 sctp->sctp_ack_mp = ack_mp; 1405 sctp->sctp_heartbeat_mp = hb_mp; 1406 1407 switch (family) { 1408 case AF_INET6: 1409 sctp_connp->conn_af_isv6 = B_TRUE; 1410 sctp->sctp_ipversion = IPV6_VERSION; 1411 sctp->sctp_family = AF_INET6; 1412 break; 1413 1414 case AF_INET: 1415 sctp_connp->conn_af_isv6 = B_FALSE; 1416 sctp_connp->conn_pkt_isv6 = B_FALSE; 1417 sctp->sctp_ipversion = IPV4_VERSION; 1418 sctp->sctp_family = AF_INET; 1419 break; 1420 default: 1421 ASSERT(0); 1422 break; 1423 } 1424 if (sctp_init_values(sctp, psctp, sleep) != 0) { 1425 freeb(ack_mp); 1426 freeb(hb_mp); 1427 netstack_rele(sctp_connp->conn_netstack); 1428 sctp_connp->conn_netstack = NULL; 1429 sctp->sctp_sctps = NULL; 1430 SCTP_G_Q_REFRELE(sctps); 1431 kmem_cache_free(sctp_conn_cache, sctp_connp); 1432 return (NULL); 1433 } 1434 sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK); 1435 1436 sctp->sctp_mss = sctps->sctps_initial_mtu - ((family == AF_INET6) ? 1437 sctp->sctp_hdr6_len : sctp->sctp_hdr_len); 1438 1439 if (psctp != NULL) { 1440 RUN_SCTP(psctp); 1441 /* 1442 * Inherit local address list, local port. Parent is either 1443 * in SCTPS_BOUND, or SCTPS_LISTEN state. 1444 */ 1445 ASSERT((psctp->sctp_state == SCTPS_BOUND) || 1446 (psctp->sctp_state == SCTPS_LISTEN)); 1447 if (sctp_dup_saddrs(psctp, sctp, sleep)) { 1448 WAKE_SCTP(psctp); 1449 freeb(ack_mp); 1450 freeb(hb_mp); 1451 sctp_headers_free(sctp); 1452 netstack_rele(sctps->sctps_netstack); 1453 sctp_connp->conn_netstack = NULL; 1454 sctp->sctp_sctps = NULL; 1455 SCTP_G_Q_REFRELE(sctps); 1456 kmem_cache_free(sctp_conn_cache, sctp_connp); 1457 return (NULL); 1458 } 1459 1460 /* 1461 * If the parent is specified, it'll be immediatelly 1462 * followed by sctp_connect(). So don't add this guy to 1463 * bind hash. 1464 */ 1465 sctp->sctp_lport = psctp->sctp_lport; 1466 sctp->sctp_state = SCTPS_BOUND; 1467 sctp->sctp_allzones = psctp->sctp_allzones; 1468 sctp->sctp_zoneid = psctp->sctp_zoneid; 1469 WAKE_SCTP(psctp); 1470 } else { 1471 sctp->sctp_zoneid = zoneid; 1472 } 1473 1474 sctp_connp->conn_cred = credp; 1475 crhold(credp); 1476 1477 /* 1478 * If the caller has the process-wide flag set, then default to MAC 1479 * exempt mode. This allows read-down to unlabeled hosts. 1480 */ 1481 if (getpflags(NET_MAC_AWARE, credp) != 0) 1482 sctp_connp->conn_mac_exempt = B_TRUE; 1483 1484 /* Initialize SCTP instance values, our verf tag must never be 0 */ 1485 (void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag, 1486 sizeof (sctp->sctp_lvtag)); 1487 if (sctp->sctp_lvtag == 0) 1488 sctp->sctp_lvtag = (uint32_t)gethrtime(); 1489 ASSERT(sctp->sctp_lvtag != 0); 1490 1491 sctp->sctp_ltsn = sctp->sctp_lvtag + 1; 1492 sctp->sctp_lcsn = sctp->sctp_ltsn; 1493 sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1; 1494 sctp->sctp_adv_pap = sctp->sctp_lastack_rxd; 1495 1496 /* Information required by upper layer */ 1497 if (sctp_ulpd != NULL) { 1498 sctp->sctp_ulpd = sctp_ulpd; 1499 1500 ASSERT(sctp_upcalls != NULL); 1501 bcopy(sctp_upcalls, &sctp->sctp_upcalls, 1502 sizeof (sctp_upcalls_t)); 1503 ASSERT(sbl != NULL); 1504 /* Fill in the socket buffer limits for sctpsockfs */ 1505 sbl->sbl_txlowat = sctp->sctp_xmit_lowater; 1506 sbl->sbl_txbuf = sctp->sctp_xmit_hiwater; 1507 sbl->sbl_rxbuf = sctp->sctp_rwnd; 1508 sbl->sbl_rxlowat = SCTP_RECV_LOWATER; 1509 } 1510 /* If no sctp_ulpd, must be creating the default sctp */ 1511 ASSERT(sctp_ulpd != NULL || sctps->sctps_gsctp == NULL); 1512 1513 /* Insert this in the global list. */ 1514 SCTP_LINK(sctp, sctps); 1515 1516 return (sctp); 1517 } 1518 1519 /* 1520 * Make sure we wait until the default queue is setup, yet allow 1521 * sctp_g_q_create() to open a SCTP stream. 1522 * We need to allow sctp_g_q_create() do do an open 1523 * of sctp, hence we compare curhread. 1524 * All others have to wait until the sctps_g_q has been 1525 * setup. 1526 */ 1527 void 1528 sctp_g_q_setup(sctp_stack_t *sctps) 1529 { 1530 mutex_enter(&sctps->sctps_g_q_lock); 1531 if (sctps->sctps_g_q != NULL) { 1532 mutex_exit(&sctps->sctps_g_q_lock); 1533 return; 1534 } 1535 if (sctps->sctps_g_q_creator == NULL) { 1536 /* This thread will set it up */ 1537 sctps->sctps_g_q_creator = curthread; 1538 mutex_exit(&sctps->sctps_g_q_lock); 1539 sctp_g_q_create(sctps); 1540 mutex_enter(&sctps->sctps_g_q_lock); 1541 ASSERT(sctps->sctps_g_q_creator == curthread); 1542 sctps->sctps_g_q_creator = NULL; 1543 cv_signal(&sctps->sctps_g_q_cv); 1544 ASSERT(sctps->sctps_g_q != NULL); 1545 mutex_exit(&sctps->sctps_g_q_lock); 1546 return; 1547 } 1548 /* Everybody but the creator has to wait */ 1549 if (sctps->sctps_g_q_creator != curthread) { 1550 while (sctps->sctps_g_q == NULL) 1551 cv_wait(&sctps->sctps_g_q_cv, &sctps->sctps_g_q_lock); 1552 } 1553 mutex_exit(&sctps->sctps_g_q_lock); 1554 } 1555 1556 major_t IP_MAJ; 1557 #define IP "ip" 1558 1559 #define SCTP6DEV "/devices/pseudo/sctp6@0:sctp6" 1560 1561 /* 1562 * Create a default sctp queue here instead of in strplumb 1563 */ 1564 void 1565 sctp_g_q_create(sctp_stack_t *sctps) 1566 { 1567 int error; 1568 ldi_handle_t lh = NULL; 1569 ldi_ident_t li = NULL; 1570 int rval; 1571 cred_t *cr; 1572 1573 #ifdef NS_DEBUG 1574 (void) printf("sctp_g_q_create()for stack %d\n", 1575 sctps->sctps_netstack->netstack_stackid); 1576 #endif 1577 1578 ASSERT(sctps->sctps_g_q_creator == curthread); 1579 1580 error = ldi_ident_from_major(IP_MAJ, &li); 1581 if (error) { 1582 #ifdef DEBUG 1583 printf("sctp_g_q_create: lyr ident get failed error %d\n", 1584 error); 1585 #endif 1586 return; 1587 } 1588 1589 cr = zone_get_kcred(netstackid_to_zoneid( 1590 sctps->sctps_netstack->netstack_stackid)); 1591 ASSERT(cr != NULL); 1592 /* 1593 * We set the sctp default queue to IPv6 because IPv4 falls 1594 * back to IPv6 when it can't find a client, but 1595 * IPv6 does not fall back to IPv4. 1596 */ 1597 error = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, cr, &lh, li); 1598 if (error) { 1599 #ifdef DEBUG 1600 printf("sctp_g_q_create: open of SCTP6DEV failed error %d\n", 1601 error); 1602 #endif 1603 goto out; 1604 } 1605 1606 /* 1607 * This ioctl causes the sctp framework to cache a pointer to 1608 * this stream, so we don't want to close the stream after 1609 * this operation. 1610 * Use the kernel credentials that are for the zone we're in. 1611 */ 1612 error = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q, 1613 (intptr_t)0, FKIOCTL, cr, &rval); 1614 if (error) { 1615 #ifdef DEBUG 1616 printf("sctp_g_q_create: ioctl SCTP_IOC_DEFAULT_Q failed " 1617 "error %d\n", error); 1618 #endif 1619 goto out; 1620 } 1621 sctps->sctps_g_q_lh = lh; /* For sctp_g_q_inactive */ 1622 lh = NULL; 1623 out: 1624 /* Close layered handles */ 1625 if (li) 1626 ldi_ident_release(li); 1627 /* Keep cred around until _inactive needs it */ 1628 sctps->sctps_g_q_cr = cr; 1629 } 1630 1631 /* 1632 * Remove the sctp_default queue so that new connections will not find it. 1633 * SCTP uses sctp_g_q for all transmission, so all sctp'ts implicitly 1634 * refer to it. Hence have each one have a reference on sctp_g_q_ref! 1635 * 1636 * We decrement the refcnt added in sctp_g_q_create. Once all the 1637 * sctp_t's which use the default go away, sctp_g_q_close will be called 1638 * and close the sctp_g_q. Once sctp_g_q is closed, sctp_close() will drop the 1639 * last reference count on the stack by calling netstack_rele(). 1640 */ 1641 void 1642 sctp_g_q_destroy(sctp_stack_t *sctps) 1643 { 1644 if (sctps->sctps_g_q == NULL) { 1645 return; /* Nothing to cleanup */ 1646 } 1647 /* 1648 * Keep sctps_g_q and sctps_gsctp until the last reference has 1649 * dropped, since the output is always done using those. 1650 * Need to decrement twice to take sctp_g_q_create and 1651 * the gsctp reference into account so that sctp_g_q_inactive is called 1652 * when all but the default queue remains. 1653 */ 1654 #ifdef NS_DEBUG 1655 (void) printf("sctp_g_q_destroy: ref %d\n", 1656 sctps->sctps_g_q_ref); 1657 #endif 1658 SCTP_G_Q_REFRELE(sctps); 1659 } 1660 1661 /* 1662 * Called when last user (could be sctp_g_q_destroy) drops reference count 1663 * using SCTP_G_Q_REFRELE. 1664 * Run by sctp_q_q_inactive using a taskq. 1665 */ 1666 static void 1667 sctp_g_q_close(void *arg) 1668 { 1669 sctp_stack_t *sctps = arg; 1670 int error; 1671 ldi_handle_t lh = NULL; 1672 ldi_ident_t li = NULL; 1673 cred_t *cr; 1674 1675 lh = sctps->sctps_g_q_lh; 1676 if (lh == NULL) 1677 return; /* Nothing to cleanup */ 1678 1679 error = ldi_ident_from_major(IP_MAJ, &li); 1680 if (error) { 1681 #ifdef NS_DEBUG 1682 printf("sctp_g_q_inactive: lyr ident get failed error %d\n", 1683 error); 1684 #endif 1685 return; 1686 } 1687 1688 cr = sctps->sctps_g_q_cr; 1689 sctps->sctps_g_q_cr = NULL; 1690 ASSERT(cr != NULL); 1691 1692 /* 1693 * Make sure we can break the recursion when sctp_close decrements 1694 * the reference count causing g_q_inactive to be called again. 1695 */ 1696 sctps->sctps_g_q_lh = NULL; 1697 1698 /* close the default queue */ 1699 (void) ldi_close(lh, FREAD|FWRITE, cr); 1700 1701 /* Close layered handles */ 1702 ldi_ident_release(li); 1703 crfree(cr); 1704 1705 ASSERT(sctps->sctps_g_q != NULL); 1706 sctps->sctps_g_q = NULL; 1707 /* 1708 * Now free sctps_gsctp. 1709 */ 1710 ASSERT(sctps->sctps_gsctp != NULL); 1711 sctp_closei_local(sctps->sctps_gsctp); 1712 SCTP_CONDEMNED(sctps->sctps_gsctp); 1713 SCTP_REFRELE(sctps->sctps_gsctp); 1714 sctps->sctps_gsctp = NULL; 1715 } 1716 1717 /* 1718 * Called when last sctp_t drops reference count using SCTP_G_Q_REFRELE. 1719 * 1720 * Have to ensure that the ldi routines are not used by an 1721 * interrupt thread by using a taskq. 1722 */ 1723 void 1724 sctp_g_q_inactive(sctp_stack_t *sctps) 1725 { 1726 if (sctps->sctps_g_q_lh == NULL) 1727 return; /* Nothing to cleanup */ 1728 1729 ASSERT(sctps->sctps_g_q_ref == 0); 1730 SCTP_G_Q_REFHOLD(sctps); /* Compensate for what g_q_destroy did */ 1731 1732 if (servicing_interrupt()) { 1733 (void) taskq_dispatch(sctp_taskq, sctp_g_q_close, 1734 (void *) sctps, TQ_SLEEP); 1735 } else { 1736 sctp_g_q_close(sctps); 1737 } 1738 } 1739 1740 /* Run at module load time */ 1741 void 1742 sctp_ddi_g_init(void) 1743 { 1744 IP_MAJ = ddi_name_to_major(IP); 1745 1746 /* Create sctp_t/conn_t cache */ 1747 sctp_conn_cache_init(); 1748 1749 /* Create the faddr cache */ 1750 sctp_faddr_init(); 1751 1752 /* Create the sets cache */ 1753 sctp_sets_init(); 1754 1755 /* Create the PR-SCTP sets cache */ 1756 sctp_ftsn_sets_init(); 1757 1758 /* Initialize tables used for CRC calculation */ 1759 sctp_crc32_init(); 1760 1761 sctp_taskq = taskq_create("sctp_taskq", 1, minclsyspri, 1, 1, 1762 TASKQ_PREPOPULATE); 1763 1764 /* 1765 * We want to be informed each time a stack is created or 1766 * destroyed in the kernel, so we can maintain the 1767 * set of sctp_stack_t's. 1768 */ 1769 netstack_register(NS_SCTP, sctp_stack_init, sctp_stack_shutdown, 1770 sctp_stack_fini); 1771 } 1772 1773 static void * 1774 sctp_stack_init(netstackid_t stackid, netstack_t *ns) 1775 { 1776 sctp_stack_t *sctps; 1777 1778 sctps = kmem_zalloc(sizeof (*sctps), KM_SLEEP); 1779 sctps->sctps_netstack = ns; 1780 1781 /* Initialize locks */ 1782 mutex_init(&sctps->sctps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 1783 cv_init(&sctps->sctps_g_q_cv, NULL, CV_DEFAULT, NULL); 1784 mutex_init(&sctps->sctps_g_lock, NULL, MUTEX_DEFAULT, NULL); 1785 mutex_init(&sctps->sctps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 1786 sctps->sctps_g_num_epriv_ports = SCTP_NUM_EPRIV_PORTS; 1787 sctps->sctps_g_epriv_ports[0] = 2049; 1788 sctps->sctps_g_epriv_ports[1] = 4045; 1789 1790 /* Initialize SCTP hash arrays. */ 1791 sctp_hash_init(sctps); 1792 1793 sctps->sctps_pad_mp = allocb(SCTP_ALIGN, BPRI_MED); 1794 bzero(sctps->sctps_pad_mp->b_rptr, SCTP_ALIGN); 1795 ASSERT(sctps->sctps_pad_mp); 1796 1797 if (!sctp_nd_init(sctps)) { 1798 sctp_nd_free(sctps); 1799 } 1800 1801 /* Initialize the recvq taskq. */ 1802 sctp_rq_tq_init(sctps); 1803 1804 /* saddr init */ 1805 sctp_saddr_init(sctps); 1806 1807 /* Global SCTP PCB list. */ 1808 list_create(&sctps->sctps_g_list, sizeof (sctp_t), 1809 offsetof(sctp_t, sctp_list)); 1810 1811 /* Initialize sctp kernel stats. */ 1812 sctps->sctps_mibkp = sctp_kstat_init(stackid); 1813 sctps->sctps_kstat = 1814 sctp_kstat2_init(stackid, &sctps->sctps_statistics); 1815 1816 return (sctps); 1817 } 1818 1819 /* 1820 * Called when the module is about to be unloaded. 1821 */ 1822 void 1823 sctp_ddi_g_destroy(void) 1824 { 1825 /* Destroy sctp_t/conn_t caches */ 1826 sctp_conn_cache_fini(); 1827 1828 /* Destroy the faddr cache */ 1829 sctp_faddr_fini(); 1830 1831 /* Destroy the sets cache */ 1832 sctp_sets_fini(); 1833 1834 /* Destroy the PR-SCTP sets cache */ 1835 sctp_ftsn_sets_fini(); 1836 1837 netstack_unregister(NS_SCTP); 1838 taskq_destroy(sctp_taskq); 1839 } 1840 1841 /* 1842 * Shut down the SCTP stack instance. 1843 */ 1844 /* ARGSUSED */ 1845 static void 1846 sctp_stack_shutdown(netstackid_t stackid, void *arg) 1847 { 1848 sctp_stack_t *sctps = (sctp_stack_t *)arg; 1849 1850 sctp_g_q_destroy(sctps); 1851 } 1852 1853 /* 1854 * Free the SCTP stack instance. 1855 */ 1856 static void 1857 sctp_stack_fini(netstackid_t stackid, void *arg) 1858 { 1859 sctp_stack_t *sctps = (sctp_stack_t *)arg; 1860 1861 sctp_nd_free(sctps); 1862 1863 /* Destroy the recvq taskqs. */ 1864 sctp_rq_tq_fini(sctps); 1865 1866 /* Destroy saddr */ 1867 sctp_saddr_fini(sctps); 1868 1869 /* Global SCTP PCB list. */ 1870 list_destroy(&sctps->sctps_g_list); 1871 1872 /* Destroy SCTP hash arrays. */ 1873 sctp_hash_destroy(sctps); 1874 1875 /* Destroy SCTP kernel stats. */ 1876 sctp_kstat2_fini(stackid, sctps->sctps_kstat); 1877 sctps->sctps_kstat = NULL; 1878 bzero(&sctps->sctps_statistics, sizeof (sctps->sctps_statistics)); 1879 1880 sctp_kstat_fini(stackid, sctps->sctps_mibkp); 1881 sctps->sctps_mibkp = NULL; 1882 1883 freeb(sctps->sctps_pad_mp); 1884 sctps->sctps_pad_mp = NULL; 1885 1886 mutex_destroy(&sctps->sctps_g_lock); 1887 mutex_destroy(&sctps->sctps_epriv_port_lock); 1888 mutex_destroy(&sctps->sctps_g_q_lock); 1889 cv_destroy(&sctps->sctps_g_q_cv); 1890 1891 kmem_free(sctps, sizeof (*sctps)); 1892 } 1893 1894 void 1895 sctp_display_all(sctp_stack_t *sctps) 1896 { 1897 sctp_t *sctp_walker; 1898 1899 mutex_enter(&sctps->sctps_g_lock); 1900 for (sctp_walker = sctps->sctps_gsctp; sctp_walker != NULL; 1901 sctp_walker = (sctp_t *)list_next(&sctps->sctps_g_list, 1902 sctp_walker)) { 1903 (void) sctp_display(sctp_walker, NULL); 1904 } 1905 mutex_exit(&sctps->sctps_g_lock); 1906 } 1907 1908 static void 1909 sctp_rq_tq_init(sctp_stack_t *sctps) 1910 { 1911 sctps->sctps_recvq_tq_list_max_sz = 16; 1912 sctps->sctps_recvq_tq_list_cur_sz = 1; 1913 /* 1914 * Initialize the recvq_tq_list and create the first recvq taskq. 1915 * What to do if it fails? 1916 */ 1917 sctps->sctps_recvq_tq_list = 1918 kmem_zalloc(sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *), 1919 KM_SLEEP); 1920 sctps->sctps_recvq_tq_list[0] = taskq_create("sctp_def_recvq_taskq", 1921 MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)), 1922 minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max, 1923 TASKQ_PREPOPULATE); 1924 mutex_init(&sctps->sctps_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL); 1925 } 1926 1927 static void 1928 sctp_rq_tq_fini(sctp_stack_t *sctps) 1929 { 1930 int i; 1931 1932 for (i = 0; i < sctps->sctps_recvq_tq_list_cur_sz; i++) { 1933 ASSERT(sctps->sctps_recvq_tq_list[i] != NULL); 1934 taskq_destroy(sctps->sctps_recvq_tq_list[i]); 1935 } 1936 kmem_free(sctps->sctps_recvq_tq_list, 1937 sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *)); 1938 sctps->sctps_recvq_tq_list = NULL; 1939 } 1940 1941 /* Add another taskq for a new ill. */ 1942 void 1943 sctp_inc_taskq(sctp_stack_t *sctps) 1944 { 1945 taskq_t *tq; 1946 char tq_name[TASKQ_NAMELEN]; 1947 1948 mutex_enter(&sctps->sctps_rq_tq_lock); 1949 if (sctps->sctps_recvq_tq_list_cur_sz + 1 > 1950 sctps->sctps_recvq_tq_list_max_sz) { 1951 mutex_exit(&sctps->sctps_rq_tq_lock); 1952 cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq"); 1953 return; 1954 } 1955 1956 (void) snprintf(tq_name, sizeof (tq_name), "sctp_recvq_taskq_%u", 1957 sctps->sctps_recvq_tq_list_cur_sz); 1958 tq = taskq_create(tq_name, 1959 MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min, ncpus)), 1960 minclsyspri, sctp_recvq_tq_task_min, sctp_recvq_tq_task_max, 1961 TASKQ_PREPOPULATE); 1962 if (tq == NULL) { 1963 mutex_exit(&sctps->sctps_rq_tq_lock); 1964 cmn_err(CE_NOTE, "SCTP recvq taskq creation failed"); 1965 return; 1966 } 1967 ASSERT(sctps->sctps_recvq_tq_list[ 1968 sctps->sctps_recvq_tq_list_cur_sz] == NULL); 1969 sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz] = tq; 1970 atomic_add_32(&sctps->sctps_recvq_tq_list_cur_sz, 1); 1971 mutex_exit(&sctps->sctps_rq_tq_lock); 1972 } 1973 1974 #ifdef DEBUG 1975 uint32_t sendq_loop_cnt = 0; 1976 uint32_t sendq_collision = 0; 1977 uint32_t sendq_empty = 0; 1978 #endif 1979 1980 void 1981 sctp_add_sendq(sctp_t *sctp, mblk_t *mp) 1982 { 1983 mutex_enter(&sctp->sctp_sendq_lock); 1984 if (sctp->sctp_sendq == NULL) { 1985 sctp->sctp_sendq = mp; 1986 sctp->sctp_sendq_tail = mp; 1987 } else { 1988 sctp->sctp_sendq_tail->b_next = mp; 1989 sctp->sctp_sendq_tail = mp; 1990 } 1991 mutex_exit(&sctp->sctp_sendq_lock); 1992 } 1993 1994 void 1995 sctp_process_sendq(sctp_t *sctp) 1996 { 1997 mblk_t *mp; 1998 #ifdef DEBUG 1999 uint32_t loop_cnt = 0; 2000 #endif 2001 2002 mutex_enter(&sctp->sctp_sendq_lock); 2003 if (sctp->sctp_sendq == NULL || sctp->sctp_sendq_sending) { 2004 #ifdef DEBUG 2005 if (sctp->sctp_sendq == NULL) 2006 sendq_empty++; 2007 else 2008 sendq_collision++; 2009 #endif 2010 mutex_exit(&sctp->sctp_sendq_lock); 2011 return; 2012 } 2013 sctp->sctp_sendq_sending = B_TRUE; 2014 2015 /* 2016 * Note that while we are in this loop, other thread can put 2017 * new packets in the receive queue. We may be looping for 2018 * quite a while. This is OK even for an interrupt thread. 2019 * The reason is that SCTP should only able to send a limited 2020 * number of packets out in a burst. So the number of times 2021 * we go through this loop should not be many. 2022 */ 2023 while ((mp = sctp->sctp_sendq) != NULL) { 2024 sctp->sctp_sendq = mp->b_next; 2025 ASSERT(sctp->sctp_connp->conn_ref > 0); 2026 mutex_exit(&sctp->sctp_sendq_lock); 2027 mp->b_next = NULL; 2028 CONN_INC_REF(sctp->sctp_connp); 2029 mp->b_flag |= MSGHASREF; 2030 /* If we don't have sctp_current, default to IPv4 */ 2031 IP_PUT(mp, sctp->sctp_connp, sctp->sctp_current == NULL ? 2032 B_TRUE : sctp->sctp_current->isv4); 2033 BUMP_LOCAL(sctp->sctp_opkts); 2034 #ifdef DEBUG 2035 loop_cnt++; 2036 #endif 2037 mutex_enter(&sctp->sctp_sendq_lock); 2038 } 2039 2040 sctp->sctp_sendq_tail = NULL; 2041 sctp->sctp_sendq_sending = B_FALSE; 2042 #ifdef DEBUG 2043 if (loop_cnt > sendq_loop_cnt) 2044 sendq_loop_cnt = loop_cnt; 2045 #endif 2046 mutex_exit(&sctp->sctp_sendq_lock); 2047 } 2048 2049 #ifdef DEBUG 2050 uint32_t recvq_loop_cnt = 0; 2051 uint32_t recvq_call = 0; 2052 #endif 2053 2054 /* 2055 * Find the next recvq_tq to use. This routine will go thru all the 2056 * taskqs until it can dispatch a job for the sctp. If this fails, 2057 * it will create a new taskq and try it. 2058 */ 2059 static boolean_t 2060 sctp_find_next_tq(sctp_t *sctp) 2061 { 2062 int next_tq, try; 2063 taskq_t *tq; 2064 sctp_stack_t *sctps = sctp->sctp_sctps; 2065 2066 /* 2067 * Note that since we don't hold a lock on sctp_rq_tq_lock for 2068 * performance reason, recvq_ta_list_cur_sz can be changed during 2069 * this loop. The problem this will create is that the loop may 2070 * not have tried all the recvq_tq. This should be OK. 2071 */ 2072 next_tq = atomic_add_32_nv(&sctps->sctps_recvq_tq_list_cur, 1) % 2073 sctps->sctps_recvq_tq_list_cur_sz; 2074 for (try = 0; try < sctps->sctps_recvq_tq_list_cur_sz; try++) { 2075 tq = sctps->sctps_recvq_tq_list[next_tq]; 2076 if (taskq_dispatch(tq, sctp_process_recvq, sctp, 2077 TQ_NOSLEEP) != NULL) { 2078 sctp->sctp_recvq_tq = tq; 2079 return (B_TRUE); 2080 } 2081 next_tq = (next_tq + 1) % sctps->sctps_recvq_tq_list_cur_sz; 2082 } 2083 2084 /* 2085 * Create one more taskq and try it. Note that sctp_inc_taskq() 2086 * may not have created another taskq if the number of recvq 2087 * taskqs is at the maximum. We are probably in a pretty bad 2088 * shape if this actually happens... 2089 */ 2090 sctp_inc_taskq(sctps); 2091 tq = sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz - 1]; 2092 if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP) != NULL) { 2093 sctp->sctp_recvq_tq = tq; 2094 return (B_TRUE); 2095 } 2096 SCTP_KSTAT(sctps, sctp_find_next_tq); 2097 return (B_FALSE); 2098 } 2099 2100 /* 2101 * To add a message to the recvq. Note that the sctp_timer_fire() 2102 * routine also uses this function to add the timer message to the 2103 * receive queue for later processing. And it should be the only 2104 * caller of sctp_add_recvq() which sets the try_harder argument 2105 * to B_TRUE. 2106 * 2107 * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq() 2108 * will try very hard to dispatch the task. Refer to the comment 2109 * for that routine on how it does that. 2110 */ 2111 boolean_t 2112 sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock) 2113 { 2114 if (!caller_hold_lock) 2115 mutex_enter(&sctp->sctp_recvq_lock); 2116 2117 /* If the taskq dispatch has not been scheduled, do it now. */ 2118 if (sctp->sctp_recvq_tq == NULL) { 2119 ASSERT(sctp->sctp_recvq == NULL); 2120 if (!sctp_find_next_tq(sctp)) { 2121 if (!caller_hold_lock) 2122 mutex_exit(&sctp->sctp_recvq_lock); 2123 return (B_FALSE); 2124 } 2125 /* Make sure the sctp_t will not go away. */ 2126 SCTP_REFHOLD(sctp); 2127 } 2128 2129 if (sctp->sctp_recvq == NULL) { 2130 sctp->sctp_recvq = mp; 2131 sctp->sctp_recvq_tail = mp; 2132 } else { 2133 sctp->sctp_recvq_tail->b_next = mp; 2134 sctp->sctp_recvq_tail = mp; 2135 } 2136 2137 if (!caller_hold_lock) 2138 mutex_exit(&sctp->sctp_recvq_lock); 2139 return (B_TRUE); 2140 } 2141 2142 static void 2143 sctp_process_recvq(void *arg) 2144 { 2145 sctp_t *sctp = (sctp_t *)arg; 2146 mblk_t *mp; 2147 mblk_t *ipsec_mp; 2148 #ifdef DEBUG 2149 uint32_t loop_cnt = 0; 2150 #endif 2151 2152 #ifdef _BIG_ENDIAN 2153 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 2154 #else 2155 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 2156 #endif 2157 2158 RUN_SCTP(sctp); 2159 mutex_enter(&sctp->sctp_recvq_lock); 2160 2161 #ifdef DEBUG 2162 recvq_call++; 2163 #endif 2164 /* 2165 * Note that while we are in this loop, other thread can put 2166 * new packets in the receive queue. We may be looping for 2167 * quite a while. 2168 */ 2169 while ((mp = sctp->sctp_recvq) != NULL) { 2170 sctp->sctp_recvq = mp->b_next; 2171 mutex_exit(&sctp->sctp_recvq_lock); 2172 mp->b_next = NULL; 2173 #ifdef DEBUG 2174 loop_cnt++; 2175 #endif 2176 ipsec_mp = mp->b_prev; 2177 mp->b_prev = NULL; 2178 sctp_input_data(sctp, mp, ipsec_mp); 2179 2180 mutex_enter(&sctp->sctp_recvq_lock); 2181 } 2182 2183 sctp->sctp_recvq_tail = NULL; 2184 sctp->sctp_recvq_tq = NULL; 2185 2186 mutex_exit(&sctp->sctp_recvq_lock); 2187 2188 WAKE_SCTP(sctp); 2189 2190 /* We may have sent something when processing the receive queue. */ 2191 sctp_process_sendq(sctp); 2192 #ifdef DEBUG 2193 if (loop_cnt > recvq_loop_cnt) 2194 recvq_loop_cnt = loop_cnt; 2195 #endif 2196 /* Now it can go away. */ 2197 SCTP_REFRELE(sctp); 2198 } 2199 2200 /* ARGSUSED */ 2201 static int 2202 sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags) 2203 { 2204 conn_t *sctp_connp = (conn_t *)buf; 2205 sctp_t *sctp = (sctp_t *)&sctp_connp[1]; 2206 2207 bzero(buf, (char *)&sctp[1] - (char *)buf); 2208 2209 sctp->sctp_connp = sctp_connp; 2210 mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL); 2211 mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL); 2212 mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL); 2213 cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL); 2214 mutex_init(&sctp->sctp_sendq_lock, NULL, MUTEX_DEFAULT, NULL); 2215 2216 sctp_connp->conn_rq = sctp_connp->conn_wq = NULL; 2217 sctp_connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 2218 sctp_connp->conn_ulp = IPPROTO_SCTP; 2219 mutex_init(&sctp_connp->conn_lock, NULL, MUTEX_DEFAULT, NULL); 2220 cv_init(&sctp_connp->conn_cv, NULL, CV_DEFAULT, NULL); 2221 2222 return (0); 2223 } 2224 2225 /* ARGSUSED */ 2226 static void 2227 sctp_conn_cache_destructor(void *buf, void *cdrarg) 2228 { 2229 conn_t *sctp_connp = (conn_t *)buf; 2230 sctp_t *sctp = (sctp_t *)&sctp_connp[1]; 2231 2232 ASSERT(!MUTEX_HELD(&sctp->sctp_lock)); 2233 ASSERT(!MUTEX_HELD(&sctp->sctp_reflock)); 2234 ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock)); 2235 ASSERT(!MUTEX_HELD(&sctp->sctp_sendq_lock)); 2236 ASSERT(!MUTEX_HELD(&sctp->sctp_connp->conn_lock)); 2237 2238 ASSERT(sctp->sctp_conn_hash_next == NULL); 2239 ASSERT(sctp->sctp_conn_hash_prev == NULL); 2240 ASSERT(sctp->sctp_listen_hash_next == NULL); 2241 ASSERT(sctp->sctp_listen_hash_prev == NULL); 2242 ASSERT(sctp->sctp_listen_tfp == NULL); 2243 ASSERT(sctp->sctp_conn_tfp == NULL); 2244 2245 ASSERT(sctp->sctp_faddrs == NULL); 2246 ASSERT(sctp->sctp_nsaddrs == 0); 2247 2248 ASSERT(sctp->sctp_ulpd == NULL); 2249 2250 ASSERT(sctp->sctp_lastfaddr == NULL); 2251 ASSERT(sctp->sctp_primary == NULL); 2252 ASSERT(sctp->sctp_current == NULL); 2253 ASSERT(sctp->sctp_lastdata == NULL); 2254 2255 ASSERT(sctp->sctp_xmit_head == NULL); 2256 ASSERT(sctp->sctp_xmit_tail == NULL); 2257 ASSERT(sctp->sctp_xmit_unsent == NULL); 2258 ASSERT(sctp->sctp_xmit_unsent_tail == NULL); 2259 2260 ASSERT(sctp->sctp_ostrcntrs == NULL); 2261 2262 ASSERT(sctp->sctp_sack_info == NULL); 2263 ASSERT(sctp->sctp_ack_mp == NULL); 2264 ASSERT(sctp->sctp_instr == NULL); 2265 2266 ASSERT(sctp->sctp_iphc == NULL); 2267 ASSERT(sctp->sctp_iphc6 == NULL); 2268 ASSERT(sctp->sctp_ipha == NULL); 2269 ASSERT(sctp->sctp_ip6h == NULL); 2270 ASSERT(sctp->sctp_sctph == NULL); 2271 ASSERT(sctp->sctp_sctph6 == NULL); 2272 2273 ASSERT(sctp->sctp_cookie_mp == NULL); 2274 2275 ASSERT(sctp->sctp_refcnt == 0); 2276 ASSERT(sctp->sctp_timer_mp == NULL); 2277 ASSERT(sctp->sctp_connp->conn_ref == 0); 2278 ASSERT(sctp->sctp_heartbeat_mp == NULL); 2279 ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL); 2280 2281 ASSERT(sctp->sctp_shutdown_faddr == NULL); 2282 2283 ASSERT(sctp->sctp_cxmit_list == NULL); 2284 2285 ASSERT(sctp->sctp_recvq == NULL); 2286 ASSERT(sctp->sctp_recvq_tail == NULL); 2287 ASSERT(sctp->sctp_recvq_tq == NULL); 2288 2289 ASSERT(sctp->sctp_sendq == NULL); 2290 ASSERT(sctp->sctp_sendq_tail == NULL); 2291 ASSERT(sctp->sctp_sendq_sending == B_FALSE); 2292 2293 ASSERT(sctp->sctp_ipp_hopopts == NULL); 2294 ASSERT(sctp->sctp_ipp_rtdstopts == NULL); 2295 ASSERT(sctp->sctp_ipp_rthdr == NULL); 2296 ASSERT(sctp->sctp_ipp_dstopts == NULL); 2297 ASSERT(sctp->sctp_ipp_pathmtu == NULL); 2298 2299 mutex_destroy(&sctp->sctp_reflock); 2300 mutex_destroy(&sctp->sctp_lock); 2301 mutex_destroy(&sctp->sctp_recvq_lock); 2302 cv_destroy(&sctp->sctp_cv); 2303 mutex_destroy(&sctp->sctp_sendq_lock); 2304 2305 mutex_destroy(&sctp_connp->conn_lock); 2306 cv_destroy(&sctp_connp->conn_cv); 2307 } 2308 2309 static void 2310 sctp_conn_cache_init() 2311 { 2312 sctp_conn_cache = kmem_cache_create("sctp_conn_cache", 2313 sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor, 2314 sctp_conn_cache_destructor, NULL, NULL, NULL, 0); 2315 } 2316 2317 static void 2318 sctp_conn_cache_fini() 2319 { 2320 kmem_cache_destroy(sctp_conn_cache); 2321 } 2322