docs/how-to-build.md: use proper markup for directory names
[unleashed/tickless.git] / kernel / net / sctp / sctp.c
blob0734464a26554b562e96cacd3757fa099b50ae56
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
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>
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>
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>
70 #include <inet/sctp/sctp_impl.h>
71 #include <inet/sctp/sctp_addr.h>
72 #include <inet/sctp/sctp_asconf.h>
74 int sctpdebug;
75 sin6_t sctp_sin6_null; /* Zero address for quick clears */
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);
91 static void *sctp_stack_init(netstackid_t stackid, netstack_t *ns);
92 static void sctp_stack_fini(netstackid_t stackid, void *arg);
95 * SCTP receive queue taskq
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.
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.
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 = 48;
115 /* The mnimum number of tasks for each taskq. */
116 int sctp_recvq_tq_task_min = 8;
117 /* Default value of sctp_recvq_tq_list_max_sz. */
118 int sctp_recvq_tq_list_max = 16;
121 * SCTP tunables related declarations. Definitions are in sctp_tunables.c
123 extern mod_prop_info_t sctp_propinfo_tbl[];
124 extern int sctp_propinfo_count;
126 /* sctp_t/conn_t kmem cache */
127 struct kmem_cache *sctp_conn_cache;
129 #define SCTP_CONDEMNED(sctp) \
130 mutex_enter(&(sctp)->sctp_reflock); \
131 ((sctp)->sctp_condemned = B_TRUE); \
132 mutex_exit(&(sctp)->sctp_reflock);
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);
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);
147 * Return the version number of the SCTP kernel interface.
150 sctp_itf_ver(int cl_ver)
152 if (cl_ver != SCTP_ITF_VER)
153 return (-1);
154 return (SCTP_ITF_VER);
158 * Called when we need a new sctp instantiation but don't really have a
159 * new q to hang it off of. Copy the priv flag from the passed in structure.
161 sctp_t *
162 sctp_create_eager(sctp_t *psctp)
164 sctp_t *sctp;
165 mblk_t *ack_mp, *hb_mp;
166 conn_t *connp;
167 cred_t *credp;
168 sctp_stack_t *sctps = psctp->sctp_sctps;
170 if ((connp = ipcl_conn_create(IPCL_SCTPCONN, KM_NOSLEEP,
171 sctps->sctps_netstack)) == NULL) {
172 return (NULL);
175 sctp = CONN2SCTP(connp);
176 sctp->sctp_sctps = sctps;
178 if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer,
179 KM_NOSLEEP)) == NULL ||
180 (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer,
181 KM_NOSLEEP)) == NULL) {
182 if (ack_mp != NULL)
183 freeb(ack_mp);
184 sctp_conn_clear(connp);
185 sctp->sctp_sctps = NULL;
186 kmem_cache_free(sctp_conn_cache, connp);
187 return (NULL);
190 sctp->sctp_ack_mp = ack_mp;
191 sctp->sctp_heartbeat_mp = hb_mp;
193 if (sctp_init_values(sctp, psctp, KM_NOSLEEP) != 0) {
194 freeb(ack_mp);
195 freeb(hb_mp);
196 sctp_conn_clear(connp);
197 sctp->sctp_sctps = NULL;
198 kmem_cache_free(sctp_conn_cache, connp);
199 return (NULL);
202 if ((credp = psctp->sctp_connp->conn_cred) != NULL) {
203 connp->conn_cred = credp;
204 crhold(credp);
207 sctp->sctp_mss = psctp->sctp_mss;
208 sctp->sctp_detached = B_TRUE;
210 * Link to the global as soon as possible so that this sctp_t
211 * can be found.
213 SCTP_LINK(sctp, sctps);
215 /* If the listener has a limit, inherit the counter info. */
216 sctp->sctp_listen_cnt = psctp->sctp_listen_cnt;
218 return (sctp);
222 * We are dying for some reason. Try to do it gracefully.
224 void
225 sctp_clean_death(sctp_t *sctp, int err)
227 ASSERT(sctp != NULL);
229 dprint(3, ("sctp_clean_death %p, state %d\n", (void *)sctp,
230 sctp->sctp_state));
232 sctp->sctp_client_errno = err;
234 * Check to see if we need to notify upper layer.
236 if ((sctp->sctp_state >= SCTPS_COOKIE_WAIT) &&
237 !SCTP_IS_DETACHED(sctp)) {
238 if (sctp->sctp_xmit_head || sctp->sctp_xmit_unsent) {
239 sctp_regift_xmitlist(sctp);
241 if (sctp->sctp_ulp_disconnected(sctp->sctp_ulpd, 0, err)) {
243 * Socket is gone, detach.
245 sctp->sctp_detached = B_TRUE;
246 sctp->sctp_ulpd = NULL;
247 sctp->sctp_upcalls = NULL;
251 /* Remove this sctp from all hashes. */
252 sctp_closei_local(sctp);
255 * If the sctp_t is detached, we need to finish freeing up
256 * the resources. At this point, ip_fanout_sctp() should have
257 * a hold on this sctp_t. Some thread doing snmp stuff can
258 * have a hold. And a taskq can also have a hold waiting to
259 * work. sctp_unlink() the sctp_t from the global list so
260 * that no new thread can find it. Then do a SCTP_REFRELE().
261 * The sctp_t will be freed after all those threads are done.
263 if (SCTP_IS_DETACHED(sctp)) {
264 SCTP_CONDEMNED(sctp);
265 SCTP_REFRELE(sctp);
270 * Called by upper layer when it wants to close this association.
271 * Depending on the state of this assoication, we need to do
272 * different things.
274 * If the state is below COOKIE_ECHOED or it is COOKIE_ECHOED but with
275 * no sent data, just remove this sctp from all the hashes. This
276 * makes sure that all packets from the other end will go to the default
277 * sctp handling. The upper layer will then do a sctp_close() to clean
278 * up.
280 * Otherwise, check and see if SO_LINGER is set. If it is set, check
281 * the value. If the value is 0, consider this an abortive close. Send
282 * an ABORT message and kill the associatiion.
286 sctp_disconnect(sctp_t *sctp)
288 int error = 0;
289 conn_t *connp = sctp->sctp_connp;
291 dprint(3, ("sctp_disconnect %p, state %d\n", (void *)sctp,
292 sctp->sctp_state));
294 RUN_SCTP(sctp);
296 switch (sctp->sctp_state) {
297 case SCTPS_IDLE:
298 case SCTPS_BOUND:
299 case SCTPS_LISTEN:
300 break;
301 case SCTPS_COOKIE_WAIT:
302 case SCTPS_COOKIE_ECHOED:
304 * Close during the connect 3-way handshake
305 * but here there may or may not be pending data
306 * already on queue. Process almost same as in
307 * the ESTABLISHED state.
309 if (sctp->sctp_xmit_head == NULL &&
310 sctp->sctp_xmit_unsent == NULL) {
311 break;
313 /* FALLTHRU */
314 default:
316 * If SO_LINGER has set a zero linger time, terminate the
317 * association and send an ABORT.
319 if (connp->conn_linger && connp->conn_lingertime == 0) {
320 sctp_user_abort(sctp, NULL);
321 WAKE_SCTP(sctp);
322 return (error);
326 * If there is unread data, send an ABORT and terminate the
327 * association.
329 if (sctp->sctp_rxqueued > 0 || sctp->sctp_ulp_rxqueued > 0) {
330 sctp_user_abort(sctp, NULL);
331 WAKE_SCTP(sctp);
332 return (error);
335 * Transmit the shutdown before detaching the sctp_t.
336 * After sctp_detach returns this queue/perimeter
337 * no longer owns the sctp_t thus others can modify it.
339 sctp_send_shutdown(sctp, 0);
341 /* Pass gathered wisdom to IP for keeping */
342 sctp_update_dce(sctp);
345 * If lingering on close then wait until the shutdown
346 * is complete, or the SO_LINGER time passes, or an
347 * ABORT is sent/received. Note that sctp_disconnect()
348 * can be called more than once. Make sure that only
349 * one thread waits.
351 if (connp->conn_linger && connp->conn_lingertime > 0 &&
352 sctp->sctp_state >= SCTPS_ESTABLISHED &&
353 !sctp->sctp_lingering) {
354 clock_t stoptime; /* in ticks */
355 clock_t ret;
357 sctp->sctp_lingering = 1;
358 sctp->sctp_client_errno = 0;
359 stoptime = ddi_get_lbolt() +
360 connp->conn_lingertime * hz;
362 mutex_enter(&sctp->sctp_lock);
363 sctp->sctp_running = B_FALSE;
364 while (sctp->sctp_state >= SCTPS_ESTABLISHED &&
365 sctp->sctp_client_errno == 0) {
366 cv_signal(&sctp->sctp_cv);
367 ret = cv_timedwait_sig(&sctp->sctp_cv,
368 &sctp->sctp_lock, stoptime);
369 if (ret < 0) {
370 /* Stoptime has reached. */
371 sctp->sctp_client_errno = EWOULDBLOCK;
372 break;
373 } else if (ret == 0) {
374 /* Got a signal. */
375 break;
378 error = sctp->sctp_client_errno;
379 sctp->sctp_client_errno = 0;
380 mutex_exit(&sctp->sctp_lock);
383 WAKE_SCTP(sctp);
384 return (error);
388 /* Remove this sctp from all hashes so nobody can find it. */
389 sctp_closei_local(sctp);
390 WAKE_SCTP(sctp);
391 return (error);
394 void
395 sctp_close(sctp_t *sctp)
397 dprint(3, ("sctp_close %p, state %d\n", (void *)sctp,
398 sctp->sctp_state));
400 RUN_SCTP(sctp);
401 sctp->sctp_detached = 1;
402 sctp->sctp_ulpd = NULL;
403 sctp->sctp_upcalls = NULL;
404 bzero(&sctp->sctp_events, sizeof (sctp->sctp_events));
406 /* If the graceful shutdown has not been completed, just return. */
407 if (sctp->sctp_state != SCTPS_IDLE) {
408 WAKE_SCTP(sctp);
409 return;
413 * Since sctp_t is in SCTPS_IDLE state, so the only thread which
414 * can have a hold on the sctp_t is doing snmp stuff. Just do
415 * a SCTP_REFRELE() here after the SCTP_UNLINK(). It will
416 * be freed when the other thread is done.
418 SCTP_CONDEMNED(sctp);
419 WAKE_SCTP(sctp);
420 SCTP_REFRELE(sctp);
424 * Unlink from global list and do the eager close.
425 * Remove the refhold implicit in being on the global list.
427 void
428 sctp_close_eager(sctp_t *sctp)
430 SCTP_CONDEMNED(sctp);
431 sctp_closei_local(sctp);
432 SCTP_REFRELE(sctp);
436 * The sctp_t is going away. Remove it from all lists and set it
437 * to SCTPS_IDLE. The caller has to remove it from the
438 * global list. The freeing up of memory is deferred until
439 * sctp_free(). This is needed since a thread in sctp_input() might have
440 * done a SCTP_REFHOLD on this structure before it was removed from the
441 * hashes.
443 static void
444 sctp_closei_local(sctp_t *sctp)
446 mblk_t *mp;
447 conn_t *connp = sctp->sctp_connp;
449 /* The counter is incremented only for established associations. */
450 if (sctp->sctp_state >= SCTPS_ESTABLISHED)
451 SCTPS_ASSOC_DEC(sctp->sctp_sctps);
453 if (sctp->sctp_listen_cnt != NULL)
454 SCTP_DECR_LISTEN_CNT(sctp);
456 /* Sanity check, don't do the same thing twice. */
457 if (connp->conn_state_flags & CONN_CLOSING) {
458 ASSERT(sctp->sctp_state == SCTPS_IDLE);
459 return;
462 /* Stop and free the timers */
463 sctp_free_faddr_timers(sctp);
464 if ((mp = sctp->sctp_heartbeat_mp) != NULL) {
465 sctp_timer_free(mp);
466 sctp->sctp_heartbeat_mp = NULL;
468 if ((mp = sctp->sctp_ack_mp) != NULL) {
469 sctp_timer_free(mp);
470 sctp->sctp_ack_mp = NULL;
473 /* Set the CONN_CLOSING flag so that IP will not cache IRE again. */
474 mutex_enter(&connp->conn_lock);
475 connp->conn_state_flags |= CONN_CLOSING;
476 mutex_exit(&connp->conn_lock);
478 /* Remove from all hashes. */
479 sctp_bind_hash_remove(sctp);
480 sctp_conn_hash_remove(sctp);
481 sctp_listen_hash_remove(sctp);
482 sctp->sctp_state = SCTPS_IDLE;
485 * Clean up the recvq as much as possible. All those packets
486 * will be silently dropped as this sctp_t is now in idle state.
488 mutex_enter(&sctp->sctp_recvq_lock);
489 while ((mp = sctp->sctp_recvq) != NULL) {
490 sctp->sctp_recvq = mp->b_next;
491 mp->b_next = NULL;
493 if (ip_recv_attr_is_mblk(mp))
494 mp = ip_recv_attr_free_mblk(mp);
496 freemsg(mp);
498 mutex_exit(&sctp->sctp_recvq_lock);
502 * Free memory associated with the sctp/ip header template.
504 static void
505 sctp_headers_free(sctp_t *sctp)
507 if (sctp->sctp_iphc != NULL) {
508 kmem_free(sctp->sctp_iphc, sctp->sctp_iphc_len);
509 sctp->sctp_iphc = NULL;
510 sctp->sctp_ipha = NULL;
511 sctp->sctp_hdr_len = 0;
512 sctp->sctp_ip_hdr_len = 0;
513 sctp->sctp_iphc_len = 0;
514 sctp->sctp_sctph = NULL;
515 sctp->sctp_hdr_len = 0;
517 if (sctp->sctp_iphc6 != NULL) {
518 kmem_free(sctp->sctp_iphc6, sctp->sctp_iphc6_len);
519 sctp->sctp_iphc6 = NULL;
520 sctp->sctp_ip6h = NULL;
521 sctp->sctp_hdr6_len = 0;
522 sctp->sctp_ip_hdr6_len = 0;
523 sctp->sctp_iphc6_len = 0;
524 sctp->sctp_sctph6 = NULL;
525 sctp->sctp_hdr6_len = 0;
529 static void
530 sctp_free_xmit_data(sctp_t *sctp)
532 mblk_t *ump = NULL;
533 mblk_t *nump;
534 mblk_t *mp;
535 mblk_t *nmp;
537 sctp->sctp_xmit_unacked = NULL;
538 ump = sctp->sctp_xmit_head;
539 sctp->sctp_xmit_tail = sctp->sctp_xmit_head = NULL;
540 free_unsent:
541 for (; ump != NULL; ump = nump) {
542 for (mp = ump->b_cont; mp != NULL; mp = nmp) {
543 nmp = mp->b_next;
544 mp->b_next = NULL;
545 mp->b_prev = NULL;
546 freemsg(mp);
548 ASSERT(DB_REF(ump) == 1);
549 nump = ump->b_next;
550 ump->b_next = NULL;
551 ump->b_prev = NULL;
552 ump->b_cont = NULL;
553 freeb(ump);
555 if ((ump = sctp->sctp_xmit_unsent) == NULL) {
556 ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
557 return;
559 sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = NULL;
560 goto free_unsent;
564 * Cleanup all the messages in the stream queue and the reassembly lists.
565 * If 'free' is true, then delete the streams as well.
567 void
568 sctp_instream_cleanup(sctp_t *sctp, boolean_t free)
570 int i;
571 mblk_t *mp;
572 mblk_t *mp1;
574 if (sctp->sctp_instr != NULL) {
575 /* walk thru and flush out anything remaining in the Q */
576 for (i = 0; i < sctp->sctp_num_istr; i++) {
577 mp = sctp->sctp_instr[i].istr_msgs;
578 while (mp != NULL) {
579 mp1 = mp->b_next;
580 mp->b_next = mp->b_prev = NULL;
581 freemsg(mp);
582 mp = mp1;
584 sctp->sctp_instr[i].istr_msgs = NULL;
585 sctp->sctp_instr[i].istr_nmsgs = 0;
586 sctp_free_reass((sctp->sctp_instr) + i);
587 sctp->sctp_instr[i].nextseq = 0;
589 if (free) {
590 kmem_free(sctp->sctp_instr,
591 sizeof (*sctp->sctp_instr) * sctp->sctp_num_istr);
592 sctp->sctp_instr = NULL;
593 sctp->sctp_num_istr = 0;
596 /* un-ordered fragments */
597 if (sctp->sctp_uo_frags != NULL) {
598 for (mp = sctp->sctp_uo_frags; mp != NULL; mp = mp1) {
599 mp1 = mp->b_next;
600 mp->b_next = mp->b_prev = NULL;
601 freemsg(mp);
603 sctp->sctp_uo_frags = NULL;
608 * Last reference to the sctp_t is gone. Free all memory associated with it.
609 * Called from SCTP_REFRELE. Called inline in sctp_close()
611 void
612 sctp_free(conn_t *connp)
614 sctp_t *sctp = CONN2SCTP(connp);
615 int cnt;
616 sctp_stack_t *sctps = sctp->sctp_sctps;
618 ASSERT(sctps != NULL);
619 /* Unlink it from the global list */
620 SCTP_UNLINK(sctp, sctps);
622 ASSERT(connp->conn_ref == 0);
623 ASSERT(connp->conn_proto == IPPROTO_SCTP);
624 ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
625 ASSERT(sctp->sctp_refcnt == 0);
627 ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
628 ASSERT(sctp->sctp_conn_hash_next == NULL &&
629 sctp->sctp_conn_hash_prev == NULL);
632 /* Free up all the resources. */
634 /* blow away sctp stream management */
635 if (sctp->sctp_ostrcntrs != NULL) {
636 kmem_free(sctp->sctp_ostrcntrs,
637 sizeof (uint16_t) * sctp->sctp_num_ostr);
638 sctp->sctp_ostrcntrs = NULL;
640 sctp_instream_cleanup(sctp, B_TRUE);
642 /* Remove all data transfer resources. */
643 sctp->sctp_istr_nmsgs = 0;
644 sctp->sctp_rxqueued = 0;
645 sctp_free_xmit_data(sctp);
646 sctp->sctp_unacked = 0;
647 sctp->sctp_unsent = 0;
648 if (sctp->sctp_cxmit_list != NULL)
649 sctp_asconf_free_cxmit(sctp, NULL);
651 sctp->sctp_lastdata = NULL;
653 /* Clear out default xmit settings */
654 sctp->sctp_def_stream = 0;
655 sctp->sctp_def_flags = 0;
656 sctp->sctp_def_ppid = 0;
657 sctp->sctp_def_context = 0;
658 sctp->sctp_def_timetolive = 0;
660 if (sctp->sctp_sack_info != NULL) {
661 sctp_free_set(sctp->sctp_sack_info);
662 sctp->sctp_sack_info = NULL;
664 sctp->sctp_sack_gaps = 0;
666 if (sctp->sctp_cookie_mp != NULL) {
667 freemsg(sctp->sctp_cookie_mp);
668 sctp->sctp_cookie_mp = NULL;
671 /* Remove all the address resources. */
672 sctp_zap_addrs(sctp);
673 for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
674 ASSERT(sctp->sctp_saddrs[cnt].ipif_count == 0);
675 list_destroy(&sctp->sctp_saddrs[cnt].sctp_ipif_list);
678 if (sctp->sctp_hopopts != NULL) {
679 mi_free(sctp->sctp_hopopts);
680 sctp->sctp_hopopts = NULL;
681 sctp->sctp_hopoptslen = 0;
683 ASSERT(sctp->sctp_hopoptslen == 0);
684 if (sctp->sctp_dstopts != NULL) {
685 mi_free(sctp->sctp_dstopts);
686 sctp->sctp_dstopts = NULL;
687 sctp->sctp_dstoptslen = 0;
689 ASSERT(sctp->sctp_dstoptslen == 0);
690 if (sctp->sctp_rthdrdstopts != NULL) {
691 mi_free(sctp->sctp_rthdrdstopts);
692 sctp->sctp_rthdrdstopts = NULL;
693 sctp->sctp_rthdrdstoptslen = 0;
695 ASSERT(sctp->sctp_rthdrdstoptslen == 0);
696 if (sctp->sctp_rthdr != NULL) {
697 mi_free(sctp->sctp_rthdr);
698 sctp->sctp_rthdr = NULL;
699 sctp->sctp_rthdrlen = 0;
701 ASSERT(sctp->sctp_rthdrlen == 0);
702 sctp_headers_free(sctp);
704 sctp->sctp_shutdown_faddr = NULL;
706 if (sctp->sctp_err_chunks != NULL) {
707 freemsg(sctp->sctp_err_chunks);
708 sctp->sctp_err_chunks = NULL;
709 sctp->sctp_err_len = 0;
712 /* Clear all the bitfields. */
713 bzero(&sctp->sctp_bits, sizeof (sctp->sctp_bits));
715 /* It is time to update the global statistics. */
716 SCTPS_UPDATE_MIB(sctps, sctpOutSCTPPkts, sctp->sctp_opkts);
717 SCTPS_UPDATE_MIB(sctps, sctpOutCtrlChunks, sctp->sctp_obchunks);
718 SCTPS_UPDATE_MIB(sctps, sctpOutOrderChunks, sctp->sctp_odchunks);
719 SCTPS_UPDATE_MIB(sctps, sctpOutUnorderChunks, sctp->sctp_oudchunks);
720 SCTPS_UPDATE_MIB(sctps, sctpRetransChunks, sctp->sctp_rxtchunks);
721 SCTPS_UPDATE_MIB(sctps, sctpInSCTPPkts, sctp->sctp_ipkts);
722 SCTPS_UPDATE_MIB(sctps, sctpInCtrlChunks, sctp->sctp_ibchunks);
723 SCTPS_UPDATE_MIB(sctps, sctpInOrderChunks, sctp->sctp_idchunks);
724 SCTPS_UPDATE_MIB(sctps, sctpInUnorderChunks, sctp->sctp_iudchunks);
725 SCTPS_UPDATE_MIB(sctps, sctpFragUsrMsgs, sctp->sctp_fragdmsgs);
726 SCTPS_UPDATE_MIB(sctps, sctpReasmUsrMsgs, sctp->sctp_reassmsgs);
727 sctp->sctp_opkts = 0;
728 sctp->sctp_obchunks = 0;
729 sctp->sctp_odchunks = 0;
730 sctp->sctp_oudchunks = 0;
731 sctp->sctp_rxtchunks = 0;
732 sctp->sctp_ipkts = 0;
733 sctp->sctp_ibchunks = 0;
734 sctp->sctp_idchunks = 0;
735 sctp->sctp_iudchunks = 0;
736 sctp->sctp_fragdmsgs = 0;
737 sctp->sctp_reassmsgs = 0;
738 sctp->sctp_outseqtsns = 0;
739 sctp->sctp_osacks = 0;
740 sctp->sctp_isacks = 0;
741 sctp->sctp_idupchunks = 0;
742 sctp->sctp_gapcnt = 0;
743 sctp->sctp_cum_obchunks = 0;
744 sctp->sctp_cum_odchunks = 0;
745 sctp->sctp_cum_oudchunks = 0;
746 sctp->sctp_cum_rxtchunks = 0;
747 sctp->sctp_cum_ibchunks = 0;
748 sctp->sctp_cum_idchunks = 0;
749 sctp->sctp_cum_iudchunks = 0;
751 sctp->sctp_autoclose = 0;
752 sctp->sctp_tx_adaptation_code = 0;
754 sctp->sctp_sctps = NULL;
756 sctp_conn_clear(connp);
757 kmem_cache_free(sctp_conn_cache, connp);
761 * Initialize protocol control block. If a parent exists, inherit
762 * all values set through setsockopt().
764 static int
765 sctp_init_values(sctp_t *sctp, sctp_t *psctp, int sleep)
767 int err;
768 int cnt;
769 sctp_stack_t *sctps = sctp->sctp_sctps;
770 conn_t *connp;
772 connp = sctp->sctp_connp;
774 sctp->sctp_nsaddrs = 0;
775 for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
776 sctp->sctp_saddrs[cnt].ipif_count = 0;
777 list_create(&sctp->sctp_saddrs[cnt].sctp_ipif_list,
778 sizeof (sctp_saddr_ipif_t), offsetof(sctp_saddr_ipif_t,
779 saddr_ipif));
781 connp->conn_ports = 0;
782 sctp->sctp_running = B_FALSE;
783 sctp->sctp_state = SCTPS_IDLE;
785 sctp->sctp_refcnt = 1;
787 sctp->sctp_strikes = 0;
789 sctp->sctp_last_mtu_probe = ddi_get_lbolt64();
790 sctp->sctp_mtu_probe_intvl = sctps->sctps_mtu_probe_interval;
792 sctp->sctp_sack_gaps = 0;
793 /* So we will not delay sending the first SACK. */
794 sctp->sctp_sack_toggle = sctps->sctps_deferred_acks_max;
796 /* Only need to do the allocation if there is no "cached" one. */
797 if (sctp->sctp_pad_mp == NULL) {
798 if (sleep == KM_SLEEP) {
799 sctp->sctp_pad_mp = allocb_wait(SCTP_ALIGN, BPRI_MED,
800 STR_NOSIG, NULL);
801 } else {
802 sctp->sctp_pad_mp = allocb(SCTP_ALIGN, BPRI_MED);
803 if (sctp->sctp_pad_mp == NULL)
804 return (ENOMEM);
806 bzero(sctp->sctp_pad_mp->b_rptr, SCTP_ALIGN);
809 if (psctp != NULL) {
811 * Inherit from parent
813 * Start by inheriting from the conn_t, including conn_ixa and
814 * conn_xmit_ipp.
816 err = conn_inherit_parent(psctp->sctp_connp, connp);
817 if (err != 0)
818 goto failure;
820 sctp->sctp_upcalls = psctp->sctp_upcalls;
822 sctp->sctp_cookie_lifetime = psctp->sctp_cookie_lifetime;
824 sctp->sctp_cwnd_max = psctp->sctp_cwnd_max;
825 sctp->sctp_rwnd = psctp->sctp_rwnd;
826 sctp->sctp_arwnd = psctp->sctp_arwnd;
827 sctp->sctp_pd_point = psctp->sctp_pd_point;
828 sctp->sctp_rto_max = psctp->sctp_rto_max;
829 sctp->sctp_rto_max_init = psctp->sctp_rto_max_init;
830 sctp->sctp_rto_min = psctp->sctp_rto_min;
831 sctp->sctp_rto_initial = psctp->sctp_rto_initial;
832 sctp->sctp_pa_max_rxt = psctp->sctp_pa_max_rxt;
833 sctp->sctp_pp_max_rxt = psctp->sctp_pp_max_rxt;
834 sctp->sctp_max_init_rxt = psctp->sctp_max_init_rxt;
836 sctp->sctp_def_stream = psctp->sctp_def_stream;
837 sctp->sctp_def_flags = psctp->sctp_def_flags;
838 sctp->sctp_def_ppid = psctp->sctp_def_ppid;
839 sctp->sctp_def_context = psctp->sctp_def_context;
840 sctp->sctp_def_timetolive = psctp->sctp_def_timetolive;
842 sctp->sctp_num_istr = psctp->sctp_num_istr;
843 sctp->sctp_num_ostr = psctp->sctp_num_ostr;
845 sctp->sctp_hb_interval = psctp->sctp_hb_interval;
846 sctp->sctp_autoclose = psctp->sctp_autoclose;
847 sctp->sctp_tx_adaptation_code = psctp->sctp_tx_adaptation_code;
849 /* xxx should be a better way to copy these flags xxx */
850 sctp->sctp_bound_to_all = psctp->sctp_bound_to_all;
851 sctp->sctp_cansleep = psctp->sctp_cansleep;
852 sctp->sctp_send_adaptation = psctp->sctp_send_adaptation;
853 sctp->sctp_ndelay = psctp->sctp_ndelay;
854 sctp->sctp_events = psctp->sctp_events;
855 } else {
857 * Set to system defaults
859 sctp->sctp_cookie_lifetime =
860 MSEC_TO_TICK(sctps->sctps_cookie_life);
861 connp->conn_sndlowat = sctps->sctps_xmit_lowat;
862 connp->conn_sndbuf = sctps->sctps_xmit_hiwat;
863 connp->conn_rcvbuf = sctps->sctps_recv_hiwat;
865 sctp->sctp_cwnd_max = sctps->sctps_cwnd_max_;
866 sctp->sctp_rwnd = connp->conn_rcvbuf;
867 sctp->sctp_arwnd = connp->conn_rcvbuf;
868 sctp->sctp_pd_point = sctp->sctp_rwnd;
869 sctp->sctp_rto_max = MSEC_TO_TICK(sctps->sctps_rto_maxg);
870 sctp->sctp_rto_max_init = sctp->sctp_rto_max;
871 sctp->sctp_rto_min = MSEC_TO_TICK(sctps->sctps_rto_ming);
872 sctp->sctp_rto_initial = MSEC_TO_TICK(
873 sctps->sctps_rto_initialg);
874 sctp->sctp_pa_max_rxt = sctps->sctps_pa_max_retr;
875 sctp->sctp_pp_max_rxt = sctps->sctps_pp_max_retr;
876 sctp->sctp_max_init_rxt = sctps->sctps_max_init_retr;
878 sctp->sctp_num_istr = sctps->sctps_max_in_streams;
879 sctp->sctp_num_ostr = sctps->sctps_initial_out_streams;
881 sctp->sctp_hb_interval =
882 MSEC_TO_TICK(sctps->sctps_heartbeat_interval);
884 if (connp->conn_family == AF_INET)
885 connp->conn_default_ttl = sctps->sctps_ipv4_ttl;
886 else
887 connp->conn_default_ttl = sctps->sctps_ipv6_hoplimit;
889 connp->conn_xmit_ipp.ipp_unicast_hops =
890 connp->conn_default_ttl;
893 * Initialize the header template
895 if ((err = sctp_build_hdrs(sctp, sleep)) != 0) {
896 goto failure;
900 sctp->sctp_understands_asconf = B_TRUE;
901 sctp->sctp_understands_addip = B_TRUE;
902 sctp->sctp_prsctp_aware = B_FALSE;
904 sctp->sctp_connp->conn_ref = 1;
906 sctp->sctp_prsctpdrop = 0;
907 sctp->sctp_msgcount = 0;
909 return (0);
911 failure:
912 sctp_headers_free(sctp);
913 return (err);
917 * Extracts the init tag from an INIT chunk and checks if it matches
918 * the sctp's verification tag. Returns 0 if it doesn't match, 1 if
919 * it does.
921 static boolean_t
922 sctp_icmp_verf(sctp_t *sctp, sctp_hdr_t *sh, mblk_t *mp)
924 sctp_chunk_hdr_t *sch;
925 uint32_t verf, *vp;
927 sch = (sctp_chunk_hdr_t *)(sh + 1);
928 vp = (uint32_t *)(sch + 1);
930 /* Need at least the data chunk hdr and the first 4 bytes of INIT */
931 if ((unsigned char *)(vp + 1) > mp->b_wptr) {
932 return (B_FALSE);
935 bcopy(vp, &verf, sizeof (verf));
937 if (verf == sctp->sctp_lvtag) {
938 return (B_TRUE);
940 return (B_FALSE);
944 * Update the SCTP state according to change of PMTU.
946 * Path MTU might have changed by either increase or decrease, so need to
947 * adjust the MSS based on the value of ixa_pmtu.
949 static void
950 sctp_update_pmtu(sctp_t *sctp, sctp_faddr_t *fp, boolean_t decrease_only)
952 uint32_t pmtu;
953 int32_t mss;
954 ip_xmit_attr_t *ixa = fp->sf_ixa;
956 if (sctp->sctp_state < SCTPS_ESTABLISHED)
957 return;
960 * Always call ip_get_pmtu() to make sure that IP has updated
961 * ixa_flags properly.
963 pmtu = ip_get_pmtu(ixa);
966 * Calculate the MSS by decreasing the PMTU by sctp_hdr_len and
967 * IPsec overhead if applied. Make sure to use the most recent
968 * IPsec information.
970 mss = pmtu - conn_ipsec_length(sctp->sctp_connp);
971 if (ixa->ixa_flags & IXAF_IS_IPV4)
972 mss -= sctp->sctp_hdr_len;
973 else
974 mss -= sctp->sctp_hdr6_len;
977 * Nothing to change, so just return.
979 if (mss == fp->sf_pmss)
980 return;
983 * Currently, for ICMP errors, only PMTU decrease is handled.
985 if (mss > fp->sf_pmss && decrease_only)
986 return;
988 #ifdef DEBUG
989 (void) printf("sctp_update_pmtu mss from %d to %d\n",
990 fp->sf_pmss, mss);
991 #endif
992 DTRACE_PROBE2(sctp_update_pmtu, int32_t, fp->sf_pmss, uint32_t, mss);
995 * Update ixa_fragsize and ixa_pmtu.
997 ixa->ixa_fragsize = ixa->ixa_pmtu = pmtu;
1000 * Make sure that sfa_pmss is a multiple of
1001 * SCTP_ALIGN.
1003 fp->sf_pmss = mss & ~(SCTP_ALIGN - 1);
1004 fp->sf_pmtu_discovered = 1;
1006 #ifdef notyet
1007 if (mss < sctp->sctp_sctps->sctps_mss_min)
1008 ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
1009 #endif
1010 if (ixa->ixa_flags & IXAF_PMTU_TOO_SMALL)
1011 ixa->ixa_flags &= ~(IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
1014 * If below the min size then ip_get_pmtu cleared IXAF_PMTU_IPV4_DF.
1015 * Make sure to clear IXAF_DONTFRAG, which is used by IP to decide
1016 * whether to fragment the packet.
1018 if (ixa->ixa_flags & IXAF_IS_IPV4) {
1019 if (!(ixa->ixa_flags & IXAF_PMTU_IPV4_DF)) {
1020 fp->sf_df = B_FALSE;
1021 if (fp == sctp->sctp_current) {
1022 sctp->sctp_ipha->
1023 ipha_fragment_offset_and_flags = 0;
1030 * Notify function registered with ip_xmit_attr_t. It's called in the context
1031 * of conn_ip_output so it's safe to update the SCTP state.
1032 * Currently only used for pmtu changes.
1034 /* ARGSUSED1 */
1035 static void
1036 sctp_notify(void *arg, ip_xmit_attr_t *ixa, ixa_notify_type_t ntype,
1037 ixa_notify_arg_t narg)
1039 sctp_t *sctp = (sctp_t *)arg;
1040 sctp_faddr_t *fp;
1042 switch (ntype) {
1043 case IXAN_PMTU:
1044 /* Find the faddr based on the ip_xmit_attr_t pointer */
1045 for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->sf_next) {
1046 if (fp->sf_ixa == ixa)
1047 break;
1049 if (fp != NULL)
1050 sctp_update_pmtu(sctp, fp, B_FALSE);
1051 break;
1052 default:
1053 break;
1058 * sctp_icmp_error is called by sctp_input() to process ICMP error messages
1059 * passed up by IP. We need to find a sctp_t
1060 * that corresponds to the returned datagram. Passes the message back in on
1061 * the correct queue once it has located the connection.
1062 * Assumes that IP has pulled up everything up to and including
1063 * the ICMP header.
1065 void
1066 sctp_icmp_error(sctp_t *sctp, mblk_t *mp)
1068 icmph_t *icmph;
1069 ipha_t *ipha;
1070 int iph_hdr_length;
1071 sctp_hdr_t *sctph;
1072 in6_addr_t dst;
1073 sctp_faddr_t *fp;
1074 sctp_stack_t *sctps = sctp->sctp_sctps;
1076 dprint(1, ("sctp_icmp_error: sctp=%p, mp=%p\n", (void *)sctp,
1077 (void *)mp));
1079 ipha = (ipha_t *)mp->b_rptr;
1080 if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
1081 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
1082 sctp_icmp_error_ipv6(sctp, mp);
1083 return;
1086 /* account for the ip hdr from the icmp message */
1087 iph_hdr_length = IPH_HDR_LENGTH(ipha);
1088 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1089 /* now the ip hdr of message resulting in this icmp */
1090 ipha = (ipha_t *)&icmph[1];
1091 iph_hdr_length = IPH_HDR_LENGTH(ipha);
1092 sctph = (sctp_hdr_t *)((char *)ipha + iph_hdr_length);
1093 /* first_mp must expose the full sctp header. */
1094 if ((uchar_t *)(sctph + 1) >= mp->b_wptr) {
1095 /* not enough data for SCTP header */
1096 freemsg(mp);
1097 return;
1100 switch (icmph->icmph_type) {
1101 case ICMP_DEST_UNREACHABLE:
1102 switch (icmph->icmph_code) {
1103 case ICMP_FRAGMENTATION_NEEDED:
1105 * Reduce the MSS based on the new MTU. This will
1106 * eliminate any fragmentation locally.
1107 * N.B. There may well be some funny side-effects on
1108 * the local send policy and the remote receive policy.
1109 * Pending further research, we provide
1110 * sctp_ignore_path_mtu just in case this proves
1111 * disastrous somewhere.
1113 * After updating the MSS, retransmit part of the
1114 * dropped segment using the new mss by calling
1115 * sctp_wput_slow(). Need to adjust all those
1116 * params to make sure sctp_wput_slow() work properly.
1118 if (sctps->sctps_ignore_path_mtu)
1119 break;
1121 /* find the offending faddr */
1122 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
1123 fp = sctp_lookup_faddr(sctp, &dst);
1124 if (fp == NULL) {
1125 break;
1127 sctp_update_pmtu(sctp, fp, B_TRUE);
1129 * It is possible, even likely that a fast retransmit
1130 * attempt has been dropped by ip as a result of this
1131 * error, retransmission bundles as much as possible.
1132 * A retransmit here prevents significant delays waiting
1133 * on the timer. Analogous to behaviour of TCP after
1134 * ICMP too big.
1136 sctp_rexmit(sctp, fp);
1137 break;
1138 case ICMP_PORT_UNREACHABLE:
1139 case ICMP_PROTOCOL_UNREACHABLE:
1140 switch (sctp->sctp_state) {
1141 case SCTPS_COOKIE_WAIT:
1142 case SCTPS_COOKIE_ECHOED:
1143 /* make sure the verification tag matches */
1144 if (!sctp_icmp_verf(sctp, sctph, mp)) {
1145 break;
1147 SCTPS_BUMP_MIB(sctps, sctpAborted);
1148 sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1149 NULL);
1150 sctp_clean_death(sctp, ECONNREFUSED);
1151 break;
1153 break;
1154 case ICMP_HOST_UNREACHABLE:
1155 case ICMP_NET_UNREACHABLE:
1156 /* Record the error in case we finally time out. */
1157 sctp->sctp_client_errno = (icmph->icmph_code ==
1158 ICMP_HOST_UNREACHABLE) ? EHOSTUNREACH : ENETUNREACH;
1159 break;
1160 default:
1161 break;
1163 break;
1164 case ICMP_SOURCE_QUENCH: {
1165 /* Reduce the sending rate as if we got a retransmit timeout */
1166 break;
1169 freemsg(mp);
1173 * sctp_icmp_error_ipv6() is called by sctp_icmp_error() to process ICMPv6
1174 * error messages passed up by IP.
1175 * Assumes that IP has pulled up all the extension headers as well
1176 * as the ICMPv6 header.
1178 static void
1179 sctp_icmp_error_ipv6(sctp_t *sctp, mblk_t *mp)
1181 icmp6_t *icmp6;
1182 ip6_t *ip6h;
1183 uint16_t iph_hdr_length;
1184 sctp_hdr_t *sctpha;
1185 uint8_t *nexthdrp;
1186 sctp_faddr_t *fp;
1187 sctp_stack_t *sctps = sctp->sctp_sctps;
1189 ip6h = (ip6_t *)mp->b_rptr;
1190 iph_hdr_length = (ip6h->ip6_nxt != IPPROTO_SCTP) ?
1191 ip_hdr_length_v6(mp, ip6h) : IPV6_HDR_LEN;
1193 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
1194 ip6h = (ip6_t *)&icmp6[1];
1195 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
1196 freemsg(mp);
1197 return;
1199 ASSERT(*nexthdrp == IPPROTO_SCTP);
1201 /* XXX need ifindex to find connection */
1202 sctpha = (sctp_hdr_t *)((char *)ip6h + iph_hdr_length);
1203 if ((uchar_t *)sctpha >= mp->b_wptr) {
1204 /* not enough data for SCTP header */
1205 freemsg(mp);
1206 return;
1208 switch (icmp6->icmp6_type) {
1209 case ICMP6_PACKET_TOO_BIG:
1211 * Reduce the MSS based on the new MTU. This will
1212 * eliminate any fragmentation locally.
1213 * N.B. There may well be some funny side-effects on
1214 * the local send policy and the remote receive policy.
1215 * Pending further research, we provide
1216 * sctp_ignore_path_mtu just in case this proves
1217 * disastrous somewhere.
1219 * After updating the MSS, retransmit part of the
1220 * dropped segment using the new mss by calling
1221 * sctp_wput_slow(). Need to adjust all those
1222 * params to make sure sctp_wput_slow() work properly.
1224 if (sctps->sctps_ignore_path_mtu)
1225 break;
1227 /* find the offending faddr */
1228 fp = sctp_lookup_faddr(sctp, &ip6h->ip6_dst);
1229 if (fp == NULL) {
1230 break;
1233 sctp_update_pmtu(sctp, fp, B_TRUE);
1235 * It is possible, even likely that a fast retransmit
1236 * attempt has been dropped by ip as a result of this
1237 * error, retransmission bundles as much as possible.
1238 * A retransmit here prevents significant delays waiting
1239 * on the timer. Analogous to behaviour of TCP after
1240 * ICMP too big.
1242 sctp_rexmit(sctp, fp);
1243 break;
1245 case ICMP6_DST_UNREACH:
1246 switch (icmp6->icmp6_code) {
1247 case ICMP6_DST_UNREACH_NOPORT:
1248 /* make sure the verification tag matches */
1249 if (!sctp_icmp_verf(sctp, sctpha, mp)) {
1250 break;
1252 if (sctp->sctp_state == SCTPS_COOKIE_WAIT ||
1253 sctp->sctp_state == SCTPS_COOKIE_ECHOED) {
1254 SCTPS_BUMP_MIB(sctps, sctpAborted);
1255 sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1256 NULL);
1257 sctp_clean_death(sctp, ECONNREFUSED);
1259 break;
1261 case ICMP6_DST_UNREACH_ADMIN:
1262 case ICMP6_DST_UNREACH_NOROUTE:
1263 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
1264 case ICMP6_DST_UNREACH_ADDR:
1265 /* Record the error in case we finally time out. */
1266 sctp->sctp_client_errno = EHOSTUNREACH;
1267 break;
1268 default:
1269 break;
1271 break;
1273 case ICMP6_PARAM_PROB:
1274 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
1275 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
1276 (uchar_t *)ip6h + icmp6->icmp6_pptr ==
1277 (uchar_t *)nexthdrp) {
1278 /* make sure the verification tag matches */
1279 if (!sctp_icmp_verf(sctp, sctpha, mp)) {
1280 break;
1282 if (sctp->sctp_state == SCTPS_COOKIE_WAIT) {
1283 SCTPS_BUMP_MIB(sctps, sctpAborted);
1284 sctp_assoc_event(sctp, SCTP_CANT_STR_ASSOC, 0,
1285 NULL);
1286 sctp_clean_death(sctp, ECONNREFUSED);
1288 break;
1290 break;
1292 case ICMP6_TIME_EXCEEDED:
1293 default:
1294 break;
1296 freemsg(mp);
1300 * Called by sockfs to create a new sctp instance.
1302 * If parent pointer is passed in, inherit settings from it.
1304 sctp_t *
1305 sctp_create(void *ulpd, sctp_t *parent, int family, int type, int flags,
1306 sock_upcalls_t *upcalls, sctp_sockbuf_limits_t *sbl,
1307 cred_t *credp)
1309 sctp_t *sctp, *psctp;
1310 conn_t *connp;
1311 mblk_t *ack_mp, *hb_mp;
1312 int sleep = flags & SCTP_CAN_BLOCK ? KM_SLEEP : KM_NOSLEEP;
1313 zoneid_t zoneid;
1314 sctp_stack_t *sctps;
1316 /* User must supply a credential. */
1317 if (credp == NULL)
1318 return (NULL);
1320 psctp = (sctp_t *)parent;
1321 if (psctp != NULL) {
1322 sctps = psctp->sctp_sctps;
1323 /* Increase here to have common decrease at end */
1324 netstack_hold(sctps->sctps_netstack);
1325 ASSERT(sctps->sctps_recvq_tq_list_cur_sz > 0);
1326 } else {
1327 netstack_t *ns;
1329 ns = netstack_find_by_cred(credp);
1330 sctps = ns->netstack_sctp;
1332 * Check if the receive queue taskq for this sctp_stack_t has
1333 * been set up.
1335 if (sctps->sctps_recvq_tq_list_cur_sz == 0)
1336 sctp_rq_tq_init(sctps);
1339 * For exclusive stacks we set the zoneid to zero
1340 * to make SCTP operate as if in the global zone.
1342 if (sctps->sctps_netstack->netstack_stackid !=
1343 GLOBAL_NETSTACKID)
1344 zoneid = GLOBAL_ZONEID;
1345 else
1346 zoneid = crgetzoneid(credp);
1348 if ((connp = ipcl_conn_create(IPCL_SCTPCONN, sleep,
1349 sctps->sctps_netstack)) == NULL) {
1350 netstack_rele(sctps->sctps_netstack);
1351 SCTP_KSTAT(sctps, sctp_conn_create);
1352 return (NULL);
1355 * ipcl_conn_create did a netstack_hold. Undo the hold that was
1356 * done at top of sctp_create.
1358 netstack_rele(sctps->sctps_netstack);
1359 sctp = CONN2SCTP(connp);
1360 sctp->sctp_sctps = sctps;
1362 if ((ack_mp = sctp_timer_alloc(sctp, sctp_ack_timer, sleep)) == NULL ||
1363 (hb_mp = sctp_timer_alloc(sctp, sctp_heartbeat_timer,
1364 sleep)) == NULL) {
1365 if (ack_mp != NULL)
1366 freeb(ack_mp);
1367 sctp_conn_clear(connp);
1368 sctp->sctp_sctps = NULL;
1369 kmem_cache_free(sctp_conn_cache, connp);
1370 return (NULL);
1373 sctp->sctp_ack_mp = ack_mp;
1374 sctp->sctp_heartbeat_mp = hb_mp;
1377 * Have conn_ip_output drop packets should our outer source
1378 * go invalid, and tell us about mtu changes.
1380 connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
1381 IXAF_VERIFY_PMTU;
1382 connp->conn_family = family;
1383 connp->conn_so_type = type;
1385 if (sctp_init_values(sctp, psctp, sleep) != 0) {
1386 freeb(ack_mp);
1387 freeb(hb_mp);
1388 sctp_conn_clear(connp);
1389 sctp->sctp_sctps = NULL;
1390 kmem_cache_free(sctp_conn_cache, connp);
1391 return (NULL);
1393 sctp->sctp_cansleep = ((flags & SCTP_CAN_BLOCK) == SCTP_CAN_BLOCK);
1395 sctp->sctp_mss = sctps->sctps_initial_mtu - ((family == AF_INET6) ?
1396 sctp->sctp_hdr6_len : sctp->sctp_hdr_len);
1398 if (psctp != NULL) {
1399 conn_t *pconnp = psctp->sctp_connp;
1401 RUN_SCTP(psctp);
1403 * Inherit local address list, local port. Parent is either
1404 * in SCTPS_BOUND, or SCTPS_LISTEN state.
1406 ASSERT((psctp->sctp_state == SCTPS_BOUND) ||
1407 (psctp->sctp_state == SCTPS_LISTEN));
1408 if (sctp_dup_saddrs(psctp, sctp, sleep)) {
1409 WAKE_SCTP(psctp);
1410 freeb(ack_mp);
1411 freeb(hb_mp);
1412 sctp_headers_free(sctp);
1413 sctp_conn_clear(connp);
1414 sctp->sctp_sctps = NULL;
1415 kmem_cache_free(sctp_conn_cache, connp);
1416 return (NULL);
1420 * If the parent is specified, it'll be immediatelly
1421 * followed by sctp_connect(). So don't add this guy to
1422 * bind hash.
1424 connp->conn_lport = pconnp->conn_lport;
1425 sctp->sctp_state = SCTPS_BOUND;
1426 WAKE_SCTP(psctp);
1427 } else {
1428 ASSERT(connp->conn_cred == NULL);
1429 connp->conn_zoneid = zoneid;
1431 * conn_allzones can not be set this early, hence
1432 * no IPCL_ZONEID
1434 connp->conn_ixa->ixa_zoneid = zoneid;
1435 connp->conn_open_time = ddi_get_lbolt64();
1436 connp->conn_cred = credp;
1437 crhold(credp);
1438 connp->conn_cpid = curproc->p_pid;
1440 connp->conn_zone_is_global =
1441 (crgetzoneid(credp) == GLOBAL_ZONEID);
1444 /* Initialize SCTP instance values, our verf tag must never be 0 */
1445 (void) random_get_pseudo_bytes((uint8_t *)&sctp->sctp_lvtag,
1446 sizeof (sctp->sctp_lvtag));
1447 if (sctp->sctp_lvtag == 0)
1448 sctp->sctp_lvtag = (uint32_t)gethrtime();
1449 ASSERT(sctp->sctp_lvtag != 0);
1451 sctp->sctp_ltsn = sctp->sctp_lvtag + 1;
1452 sctp->sctp_lcsn = sctp->sctp_ltsn;
1453 sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd = sctp->sctp_ltsn - 1;
1454 sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
1456 /* Information required by upper layer */
1457 ASSERT(ulpd != NULL);
1458 sctp->sctp_ulpd = ulpd;
1460 ASSERT(upcalls != NULL);
1461 sctp->sctp_upcalls = upcalls;
1462 ASSERT(sbl != NULL);
1463 /* Fill in the socket buffer limits for sctpsockfs */
1464 sbl->sbl_txlowat = connp->conn_sndlowat;
1465 sbl->sbl_txbuf = connp->conn_sndbuf;
1466 sbl->sbl_rxbuf = sctp->sctp_rwnd;
1467 sbl->sbl_rxlowat = SCTP_RECV_LOWATER;
1469 /* Insert this in the global list. */
1470 SCTP_LINK(sctp, sctps);
1472 return (sctp);
1475 /* Run at module load time */
1476 void
1477 sctp_ddi_g_init(void)
1479 /* Create sctp_t/conn_t cache */
1480 sctp_conn_cache_init();
1482 /* Create the faddr cache */
1483 sctp_faddr_init();
1485 /* Create the sets cache */
1486 sctp_sets_init();
1488 /* Create the PR-SCTP sets cache */
1489 sctp_ftsn_sets_init();
1491 /* Initialize tables used for CRC calculation */
1492 sctp_crc32_init();
1495 * We want to be informed each time a stack is created or
1496 * destroyed in the kernel, so we can maintain the
1497 * set of sctp_stack_t's.
1499 netstack_register(NS_SCTP, sctp_stack_init, NULL, sctp_stack_fini);
1502 static void *
1503 sctp_stack_init(netstackid_t stackid, netstack_t *ns)
1505 sctp_stack_t *sctps;
1506 size_t arrsz;
1507 int i;
1509 sctps = kmem_zalloc(sizeof (*sctps), KM_SLEEP);
1510 sctps->sctps_netstack = ns;
1512 /* Initialize locks */
1513 mutex_init(&sctps->sctps_g_lock, NULL, MUTEX_DEFAULT, NULL);
1514 mutex_init(&sctps->sctps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
1515 sctps->sctps_g_num_epriv_ports = SCTP_NUM_EPRIV_PORTS;
1516 sctps->sctps_g_epriv_ports[0] = ULP_DEF_EPRIV_PORT1;
1517 sctps->sctps_g_epriv_ports[1] = ULP_DEF_EPRIV_PORT2;
1519 /* Initialize SCTP hash arrays. */
1520 sctp_hash_init(sctps);
1522 arrsz = sctp_propinfo_count * sizeof (mod_prop_info_t);
1523 sctps->sctps_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz,
1524 KM_SLEEP);
1525 bcopy(sctp_propinfo_tbl, sctps->sctps_propinfo_tbl, arrsz);
1527 /* saddr init */
1528 sctp_saddr_init(sctps);
1530 /* Global SCTP PCB list. */
1531 list_create(&sctps->sctps_g_list, sizeof (sctp_t),
1532 offsetof(sctp_t, sctp_list));
1534 /* Initialize SCTP kstats. */
1535 sctps->sctps_mibkp = sctp_kstat_init(stackid);
1536 sctps->sctps_kstat = sctp_kstat2_init(stackid);
1538 mutex_init(&sctps->sctps_reclaim_lock, NULL, MUTEX_DEFAULT, NULL);
1539 sctps->sctps_reclaim = B_FALSE;
1540 sctps->sctps_reclaim_tid = 0;
1541 sctps->sctps_reclaim_period = sctps->sctps_rto_maxg;
1543 /* Allocate the per netstack stats */
1544 mutex_enter(&cpu_lock);
1545 sctps->sctps_sc_cnt = MAX(ncpus, boot_ncpus);
1546 mutex_exit(&cpu_lock);
1547 sctps->sctps_sc = kmem_zalloc(max_ncpus * sizeof (sctp_stats_cpu_t *),
1548 KM_SLEEP);
1549 for (i = 0; i < sctps->sctps_sc_cnt; i++) {
1550 sctps->sctps_sc[i] = kmem_zalloc(sizeof (sctp_stats_cpu_t),
1551 KM_SLEEP);
1554 mutex_init(&sctps->sctps_listener_conf_lock, NULL, MUTEX_DEFAULT, NULL);
1555 list_create(&sctps->sctps_listener_conf, sizeof (sctp_listener_t),
1556 offsetof(sctp_listener_t, sl_link));
1558 return (sctps);
1562 * Called when the module is about to be unloaded.
1564 void
1565 sctp_ddi_g_destroy(void)
1567 /* Destroy sctp_t/conn_t caches */
1568 sctp_conn_cache_fini();
1570 /* Destroy the faddr cache */
1571 sctp_faddr_fini();
1573 /* Destroy the sets cache */
1574 sctp_sets_fini();
1576 /* Destroy the PR-SCTP sets cache */
1577 sctp_ftsn_sets_fini();
1579 netstack_unregister(NS_SCTP);
1583 * Free the SCTP stack instance.
1585 static void
1586 sctp_stack_fini(netstackid_t stackid, void *arg)
1588 sctp_stack_t *sctps = (sctp_stack_t *)arg;
1589 int i;
1592 * Set sctps_reclaim to false tells sctp_reclaim_timer() not to restart
1593 * the timer.
1595 mutex_enter(&sctps->sctps_reclaim_lock);
1596 sctps->sctps_reclaim = B_FALSE;
1597 mutex_exit(&sctps->sctps_reclaim_lock);
1598 if (sctps->sctps_reclaim_tid != 0)
1599 (void) untimeout(sctps->sctps_reclaim_tid);
1600 mutex_destroy(&sctps->sctps_reclaim_lock);
1602 sctp_listener_conf_cleanup(sctps);
1604 kmem_free(sctps->sctps_propinfo_tbl,
1605 sctp_propinfo_count * sizeof (mod_prop_info_t));
1606 sctps->sctps_propinfo_tbl = NULL;
1608 /* Destroy the recvq taskqs. */
1609 sctp_rq_tq_fini(sctps);
1611 /* Destroy saddr */
1612 sctp_saddr_fini(sctps);
1614 /* Global SCTP PCB list. */
1615 list_destroy(&sctps->sctps_g_list);
1617 /* Destroy SCTP hash arrays. */
1618 sctp_hash_destroy(sctps);
1620 /* Destroy SCTP kernel stats. */
1621 for (i = 0; i < sctps->sctps_sc_cnt; i++)
1622 kmem_free(sctps->sctps_sc[i], sizeof (sctp_stats_cpu_t));
1623 kmem_free(sctps->sctps_sc, max_ncpus * sizeof (sctp_stats_cpu_t *));
1625 sctp_kstat_fini(stackid, sctps->sctps_mibkp);
1626 sctps->sctps_mibkp = NULL;
1627 sctp_kstat2_fini(stackid, sctps->sctps_kstat);
1628 sctps->sctps_kstat = NULL;
1630 mutex_destroy(&sctps->sctps_g_lock);
1631 mutex_destroy(&sctps->sctps_epriv_port_lock);
1633 kmem_free(sctps, sizeof (*sctps));
1636 static void
1637 sctp_rq_tq_init(sctp_stack_t *sctps)
1639 char tq_name[TASKQ_NAMELEN];
1640 int thrs;
1641 int max_tasks;
1643 mutex_enter(&sctps->sctps_g_lock);
1644 /* Someone may have beaten us in creating the taskqs. */
1645 if (sctps->sctps_recvq_tq_list_cur_sz > 0) {
1646 mutex_exit(&sctps->sctps_g_lock);
1647 return;
1650 thrs = MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min,
1651 MAX(ncpus, boot_ncpus)));
1653 * Make sure that the maximum number of tasks is at least thrice as
1654 * large as the number of threads.
1656 max_tasks = MAX(sctp_recvq_tq_task_min, thrs) * 3;
1659 * This helps differentiate the default taskqs in different IP stacks.
1661 (void) snprintf(tq_name, sizeof (tq_name), "sctp_def_rq_taskq_%d",
1662 sctps->sctps_netstack->netstack_stackid);
1664 sctps->sctps_recvq_tq_list_max_sz = sctp_recvq_tq_list_max;
1665 sctps->sctps_recvq_tq_list_cur_sz = 1;
1668 * Initialize the recvq_tq_list and create the first recvq taskq.
1669 * What to do if it fails?
1671 sctps->sctps_recvq_tq_list =
1672 kmem_zalloc(sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *),
1673 KM_SLEEP);
1674 sctps->sctps_recvq_tq_list[0] = taskq_create(tq_name, thrs,
1675 minclsyspri, sctp_recvq_tq_task_min, max_tasks, TASKQ_PREPOPULATE);
1676 mutex_init(&sctps->sctps_rq_tq_lock, NULL, MUTEX_DEFAULT, NULL);
1678 mutex_exit(&sctps->sctps_g_lock);
1681 static void
1682 sctp_rq_tq_fini(sctp_stack_t *sctps)
1684 int i;
1686 if (sctps->sctps_recvq_tq_list_cur_sz == 0)
1687 return;
1689 for (i = 0; i < sctps->sctps_recvq_tq_list_cur_sz; i++) {
1690 ASSERT(sctps->sctps_recvq_tq_list[i] != NULL);
1691 taskq_destroy(sctps->sctps_recvq_tq_list[i]);
1693 kmem_free(sctps->sctps_recvq_tq_list,
1694 sctps->sctps_recvq_tq_list_max_sz * sizeof (taskq_t *));
1695 sctps->sctps_recvq_tq_list = NULL;
1698 /* Add another taskq for a new ill. */
1699 void
1700 sctp_inc_taskq(sctp_stack_t *sctps)
1702 taskq_t *tq;
1703 char tq_name[TASKQ_NAMELEN];
1704 int thrs;
1705 int max_tasks;
1707 thrs = MIN(sctp_recvq_tq_thr_max, MAX(sctp_recvq_tq_thr_min,
1708 MAX(ncpus, boot_ncpus)));
1710 * Make sure that the maximum number of tasks is at least thrice as
1711 * large as the number of threads.
1713 max_tasks = MAX(sctp_recvq_tq_task_min, thrs) * 3;
1715 mutex_enter(&sctps->sctps_rq_tq_lock);
1716 if (sctps->sctps_recvq_tq_list_cur_sz + 1 >
1717 sctps->sctps_recvq_tq_list_max_sz) {
1718 mutex_exit(&sctps->sctps_rq_tq_lock);
1719 cmn_err(CE_NOTE, "Cannot create more SCTP recvq taskq");
1720 return;
1723 (void) snprintf(tq_name, sizeof (tq_name), "sctp_rq_taskq_%d_%u",
1724 sctps->sctps_netstack->netstack_stackid,
1725 sctps->sctps_recvq_tq_list_cur_sz);
1726 tq = taskq_create(tq_name, thrs, minclsyspri, sctp_recvq_tq_task_min,
1727 max_tasks, TASKQ_PREPOPULATE);
1728 if (tq == NULL) {
1729 mutex_exit(&sctps->sctps_rq_tq_lock);
1730 cmn_err(CE_NOTE, "SCTP recvq taskq creation failed");
1731 return;
1733 ASSERT(sctps->sctps_recvq_tq_list[
1734 sctps->sctps_recvq_tq_list_cur_sz] == NULL);
1735 sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz] = tq;
1736 atomic_inc_32(&sctps->sctps_recvq_tq_list_cur_sz);
1737 mutex_exit(&sctps->sctps_rq_tq_lock);
1740 #ifdef DEBUG
1741 uint32_t recvq_loop_cnt = 0;
1742 uint32_t recvq_call = 0;
1743 #endif
1746 * Find the next recvq_tq to use. This routine will go thru all the
1747 * taskqs until it can dispatch a job for the sctp. If this fails,
1748 * it will create a new taskq and try it.
1750 static boolean_t
1751 sctp_find_next_tq(sctp_t *sctp)
1753 int next_tq, try;
1754 taskq_t *tq;
1755 sctp_stack_t *sctps = sctp->sctp_sctps;
1758 * Note that since we don't hold a lock on sctp_rq_tq_lock for
1759 * performance reason, recvq_ta_list_cur_sz can be changed during
1760 * this loop. The problem this will create is that the loop may
1761 * not have tried all the recvq_tq. This should be OK.
1763 next_tq = atomic_inc_32_nv(&sctps->sctps_recvq_tq_list_cur) %
1764 sctps->sctps_recvq_tq_list_cur_sz;
1765 for (try = 0; try < sctps->sctps_recvq_tq_list_cur_sz; try++) {
1766 tq = sctps->sctps_recvq_tq_list[next_tq];
1767 if (taskq_dispatch(tq, sctp_process_recvq, sctp,
1768 TQ_NOSLEEP) != (uintptr_t)NULL) {
1769 sctp->sctp_recvq_tq = tq;
1770 return (B_TRUE);
1772 next_tq = (next_tq + 1) % sctps->sctps_recvq_tq_list_cur_sz;
1776 * Create one more taskq and try it. Note that sctp_inc_taskq()
1777 * may not have created another taskq if the number of recvq
1778 * taskqs is at the maximum. We are probably in a pretty bad
1779 * shape if this actually happens...
1781 sctp_inc_taskq(sctps);
1782 tq = sctps->sctps_recvq_tq_list[sctps->sctps_recvq_tq_list_cur_sz - 1];
1783 if (taskq_dispatch(tq, sctp_process_recvq, sctp, TQ_NOSLEEP)
1784 != (uintptr_t)NULL) {
1785 sctp->sctp_recvq_tq = tq;
1786 return (B_TRUE);
1788 SCTP_KSTAT(sctps, sctp_find_next_tq);
1789 return (B_FALSE);
1793 * To add a message to the recvq. Note that the sctp_timer_fire()
1794 * routine also uses this function to add the timer message to the
1795 * receive queue for later processing. And it should be the only
1796 * caller of sctp_add_recvq() which sets the try_harder argument
1797 * to B_TRUE.
1799 * If the try_harder argument is B_TRUE, this routine sctp_find_next_tq()
1800 * will try very hard to dispatch the task. Refer to the comment
1801 * for that routine on how it does that.
1803 * On failure the message has been freed i.e., this routine always consumes the
1804 * message. It bumps ipIfStatsInDiscards and and uses ip_drop_input to drop.
1806 void
1807 sctp_add_recvq(sctp_t *sctp, mblk_t *mp, boolean_t caller_hold_lock,
1808 ip_recv_attr_t *ira)
1810 mblk_t *attrmp;
1811 ip_stack_t *ipst = sctp->sctp_sctps->sctps_netstack->netstack_ip;
1813 ASSERT(ira->ira_ill == NULL);
1815 if (!caller_hold_lock)
1816 mutex_enter(&sctp->sctp_recvq_lock);
1818 /* If the taskq dispatch has not been scheduled, do it now. */
1819 if (sctp->sctp_recvq_tq == NULL) {
1820 ASSERT(sctp->sctp_recvq == NULL);
1821 if (!sctp_find_next_tq(sctp)) {
1822 if (!caller_hold_lock)
1823 mutex_exit(&sctp->sctp_recvq_lock);
1824 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
1825 ip_drop_input("ipIfStatsInDiscards", mp, NULL);
1826 freemsg(mp);
1827 return;
1829 /* Make sure the sctp_t will not go away. */
1830 SCTP_REFHOLD(sctp);
1833 attrmp = ip_recv_attr_to_mblk(ira);
1834 if (attrmp == NULL) {
1835 if (!caller_hold_lock)
1836 mutex_exit(&sctp->sctp_recvq_lock);
1837 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
1838 ip_drop_input("ipIfStatsInDiscards", mp, NULL);
1839 freemsg(mp);
1840 return;
1842 ASSERT(attrmp->b_cont == NULL);
1843 attrmp->b_cont = mp;
1844 mp = attrmp;
1846 if (sctp->sctp_recvq == NULL) {
1847 sctp->sctp_recvq = mp;
1848 sctp->sctp_recvq_tail = mp;
1849 } else {
1850 sctp->sctp_recvq_tail->b_next = mp;
1851 sctp->sctp_recvq_tail = mp;
1854 if (!caller_hold_lock)
1855 mutex_exit(&sctp->sctp_recvq_lock);
1858 static void
1859 sctp_process_recvq(void *arg)
1861 sctp_t *sctp = (sctp_t *)arg;
1862 mblk_t *mp;
1863 #ifdef DEBUG
1864 uint32_t loop_cnt = 0;
1865 #endif
1866 ip_recv_attr_t iras;
1868 #ifdef _BIG_ENDIAN
1869 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7)
1870 #else
1871 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7)
1872 #endif
1874 RUN_SCTP(sctp);
1875 mutex_enter(&sctp->sctp_recvq_lock);
1877 #ifdef DEBUG
1878 recvq_call++;
1879 #endif
1881 * Note that while we are in this loop, other thread can put
1882 * new packets in the receive queue. We may be looping for
1883 * quite a while.
1885 while ((mp = sctp->sctp_recvq) != NULL) {
1886 mblk_t *data_mp;
1888 sctp->sctp_recvq = mp->b_next;
1889 mutex_exit(&sctp->sctp_recvq_lock);
1890 mp->b_next = NULL;
1891 #ifdef DEBUG
1892 loop_cnt++;
1893 #endif
1894 mp->b_prev = NULL;
1896 data_mp = mp->b_cont;
1897 mp->b_cont = NULL;
1898 if (!ip_recv_attr_from_mblk(mp, &iras)) {
1899 ip_drop_input("ip_recv_attr_from_mblk", mp, NULL);
1900 freemsg(mp);
1901 ira_cleanup(&iras, B_TRUE);
1902 continue;
1905 if (iras.ira_flags & IRAF_ICMP_ERROR)
1906 sctp_icmp_error(sctp, data_mp);
1907 else
1908 sctp_input_data(sctp, data_mp, &iras);
1910 ira_cleanup(&iras, B_TRUE);
1911 mutex_enter(&sctp->sctp_recvq_lock);
1914 sctp->sctp_recvq_tail = NULL;
1915 sctp->sctp_recvq_tq = NULL;
1917 mutex_exit(&sctp->sctp_recvq_lock);
1919 WAKE_SCTP(sctp);
1921 #ifdef DEBUG
1922 if (loop_cnt > recvq_loop_cnt)
1923 recvq_loop_cnt = loop_cnt;
1924 #endif
1925 /* Now it can go away. */
1926 SCTP_REFRELE(sctp);
1929 /* ARGSUSED */
1930 static int
1931 sctp_conn_cache_constructor(void *buf, void *cdrarg, int kmflags)
1933 conn_t *connp = (conn_t *)buf;
1934 sctp_t *sctp = (sctp_t *)&connp[1];
1935 int cnt;
1937 bzero(connp, sizeof (conn_t));
1938 bzero(buf, (char *)&sctp[1] - (char *)buf);
1940 mutex_init(&sctp->sctp_reflock, NULL, MUTEX_DEFAULT, NULL);
1941 mutex_init(&sctp->sctp_lock, NULL, MUTEX_DEFAULT, NULL);
1942 mutex_init(&sctp->sctp_recvq_lock, NULL, MUTEX_DEFAULT, NULL);
1943 cv_init(&sctp->sctp_cv, NULL, CV_DEFAULT, NULL);
1944 for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
1945 rw_init(&sctp->sctp_saddrs[cnt].ipif_hash_lock, NULL,
1946 RW_DEFAULT, NULL);
1949 mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
1950 cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
1951 connp->conn_flags = IPCL_SCTPCONN;
1952 connp->conn_proto = IPPROTO_SCTP;
1953 connp->conn_sctp = sctp;
1954 sctp->sctp_connp = connp;
1955 rw_init(&connp->conn_ilg_lock, NULL, RW_DEFAULT, NULL);
1957 connp->conn_ixa = kmem_zalloc(sizeof (ip_xmit_attr_t), kmflags);
1958 if (connp->conn_ixa == NULL) {
1959 return (ENOMEM);
1961 connp->conn_ixa->ixa_refcnt = 1;
1962 connp->conn_ixa->ixa_protocol = connp->conn_proto;
1963 connp->conn_ixa->ixa_xmit_hint = CONN_TO_XMIT_HINT(connp);
1964 return (0);
1967 /* ARGSUSED */
1968 static void
1969 sctp_conn_cache_destructor(void *buf, void *cdrarg)
1971 conn_t *connp = (conn_t *)buf;
1972 sctp_t *sctp = (sctp_t *)&connp[1];
1973 int cnt;
1975 ASSERT(sctp->sctp_connp == connp);
1976 ASSERT(!MUTEX_HELD(&sctp->sctp_lock));
1977 ASSERT(!MUTEX_HELD(&sctp->sctp_reflock));
1978 ASSERT(!MUTEX_HELD(&sctp->sctp_recvq_lock));
1980 ASSERT(sctp->sctp_conn_hash_next == NULL);
1981 ASSERT(sctp->sctp_conn_hash_prev == NULL);
1982 ASSERT(sctp->sctp_listen_hash_next == NULL);
1983 ASSERT(sctp->sctp_listen_hash_prev == NULL);
1984 ASSERT(sctp->sctp_listen_tfp == NULL);
1985 ASSERT(sctp->sctp_conn_tfp == NULL);
1987 ASSERT(sctp->sctp_faddrs == NULL);
1988 ASSERT(sctp->sctp_nsaddrs == 0);
1990 ASSERT(sctp->sctp_ulpd == NULL);
1992 ASSERT(sctp->sctp_lastfaddr == NULL);
1993 ASSERT(sctp->sctp_primary == NULL);
1994 ASSERT(sctp->sctp_current == NULL);
1995 ASSERT(sctp->sctp_lastdata == NULL);
1997 ASSERT(sctp->sctp_xmit_head == NULL);
1998 ASSERT(sctp->sctp_xmit_tail == NULL);
1999 ASSERT(sctp->sctp_xmit_unsent == NULL);
2000 ASSERT(sctp->sctp_xmit_unsent_tail == NULL);
2002 ASSERT(sctp->sctp_ostrcntrs == NULL);
2004 ASSERT(sctp->sctp_sack_info == NULL);
2005 ASSERT(sctp->sctp_ack_mp == NULL);
2006 ASSERT(sctp->sctp_instr == NULL);
2008 ASSERT(sctp->sctp_iphc == NULL);
2009 ASSERT(sctp->sctp_iphc6 == NULL);
2010 ASSERT(sctp->sctp_ipha == NULL);
2011 ASSERT(sctp->sctp_ip6h == NULL);
2012 ASSERT(sctp->sctp_sctph == NULL);
2013 ASSERT(sctp->sctp_sctph6 == NULL);
2015 ASSERT(sctp->sctp_cookie_mp == NULL);
2017 ASSERT(sctp->sctp_refcnt == 0);
2018 ASSERT(sctp->sctp_timer_mp == NULL);
2019 ASSERT(sctp->sctp_connp->conn_ref == 0);
2020 ASSERT(sctp->sctp_heartbeat_mp == NULL);
2021 ASSERT(sctp->sctp_ptpbhn == NULL && sctp->sctp_bind_hash == NULL);
2023 ASSERT(sctp->sctp_shutdown_faddr == NULL);
2025 ASSERT(sctp->sctp_cxmit_list == NULL);
2027 ASSERT(sctp->sctp_recvq == NULL);
2028 ASSERT(sctp->sctp_recvq_tail == NULL);
2029 ASSERT(sctp->sctp_recvq_tq == NULL);
2032 * sctp_pad_mp can be NULL if the memory allocation fails
2033 * in sctp_init_values() and the conn_t is freed.
2035 if (sctp->sctp_pad_mp != NULL) {
2036 freeb(sctp->sctp_pad_mp);
2037 sctp->sctp_pad_mp = NULL;
2040 mutex_destroy(&sctp->sctp_reflock);
2041 mutex_destroy(&sctp->sctp_lock);
2042 mutex_destroy(&sctp->sctp_recvq_lock);
2043 cv_destroy(&sctp->sctp_cv);
2044 for (cnt = 0; cnt < SCTP_IPIF_HASH; cnt++) {
2045 rw_destroy(&sctp->sctp_saddrs[cnt].ipif_hash_lock);
2048 mutex_destroy(&connp->conn_lock);
2049 cv_destroy(&connp->conn_cv);
2050 rw_destroy(&connp->conn_ilg_lock);
2052 /* Can be NULL if constructor failed */
2053 if (connp->conn_ixa != NULL) {
2054 ASSERT(connp->conn_ixa->ixa_refcnt == 1);
2055 ASSERT(connp->conn_ixa->ixa_ire == NULL);
2056 ASSERT(connp->conn_ixa->ixa_nce == NULL);
2057 ixa_refrele(connp->conn_ixa);
2061 static void
2062 sctp_conn_cache_init()
2064 sctp_conn_cache = kmem_cache_create("sctp_conn_cache",
2065 sizeof (sctp_t) + sizeof (conn_t), 0, sctp_conn_cache_constructor,
2066 sctp_conn_cache_destructor, sctp_conn_reclaim, NULL, NULL, 0);
2069 static void
2070 sctp_conn_cache_fini()
2072 kmem_cache_destroy(sctp_conn_cache);
2075 void
2076 sctp_conn_init(conn_t *connp)
2078 ASSERT(connp->conn_flags == IPCL_SCTPCONN);
2079 connp->conn_rq = connp->conn_wq = NULL;
2080 connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
2081 IXAF_VERIFY_PMTU;
2083 ASSERT(connp->conn_proto == IPPROTO_SCTP);
2084 ASSERT(connp->conn_ixa->ixa_protocol == connp->conn_proto);
2085 connp->conn_state_flags |= CONN_INCIPIENT;
2087 ASSERT(connp->conn_sctp != NULL);
2090 * Register sctp_notify to listen to capability changes detected by IP.
2091 * This upcall is made in the context of the call to conn_ip_output
2092 * thus it holds whatever locks sctp holds across conn_ip_output.
2094 connp->conn_ixa->ixa_notify = sctp_notify;
2095 connp->conn_ixa->ixa_notify_cookie = connp->conn_sctp;
2098 static void
2099 sctp_conn_clear(conn_t *connp)
2101 /* Clean up conn_t stuff */
2102 if (connp->conn_latch != NULL) {
2103 IPLATCH_REFRELE(connp->conn_latch);
2104 connp->conn_latch = NULL;
2106 if (connp->conn_latch_in_policy != NULL) {
2107 IPPOL_REFRELE(connp->conn_latch_in_policy);
2108 connp->conn_latch_in_policy = NULL;
2110 if (connp->conn_latch_in_action != NULL) {
2111 IPACT_REFRELE(connp->conn_latch_in_action);
2112 connp->conn_latch_in_action = NULL;
2114 if (connp->conn_policy != NULL) {
2115 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
2116 connp->conn_policy = NULL;
2118 if (connp->conn_ipsec_opt_mp != NULL) {
2119 freemsg(connp->conn_ipsec_opt_mp);
2120 connp->conn_ipsec_opt_mp = NULL;
2122 netstack_rele(connp->conn_netstack);
2123 connp->conn_netstack = NULL;
2125 /* Leave conn_ixa and other constructed fields in place */
2126 ipcl_conn_cleanup(connp);