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