xref: /illumos-gate/usr/src/cmd/mdb/common/modules/sctp/sctp.c (revision d5dbd18d69de8954ab5ceb588e99d43fc9b21d46)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/stream.h>
31 #include <sys/mdb_modapi.h>
32 #include <sys/socket.h>
33 #include <sys/list.h>
34 #include <sys/strsun.h>
35 
36 #include <mdb/mdb_stdlib.h>
37 
38 #include <netinet/in.h>
39 #include <netinet/ip6.h>
40 #include <netinet/sctp.h>
41 
42 #include <inet/common.h>
43 #include <inet/ip.h>
44 #include <inet/ip6.h>
45 #include <inet/ipclassifier.h>
46 
47 #include <sctp/sctp_impl.h>
48 #include <sctp/sctp_addr.h>
49 
50 #define	MDB_SCTP_SHOW_FLAGS	0x1
51 #define	MDB_SCTP_DUMP_ADDRS	0x2
52 #define	MDB_SCTP_SHOW_HASH	0x4
53 #define	MDB_SCTP_SHOW_OUT	0x8
54 #define	MDB_SCTP_SHOW_IN	0x10
55 #define	MDB_SCTP_SHOW_MISC	0x20
56 #define	MDB_SCTP_SHOW_RTT	0x40
57 #define	MDB_SCTP_SHOW_STATS	0x80
58 #define	MDB_SCTP_SHOW_FLOW	0x100
59 #define	MDB_SCTP_SHOW_HDR	0x200
60 #define	MDB_SCTP_SHOW_PMTUD	0x400
61 #define	MDB_SCTP_SHOW_RXT	0x800
62 #define	MDB_SCTP_SHOW_CONN	0x1000
63 #define	MDB_SCTP_SHOW_CLOSE	0x2000
64 #define	MDB_SCTP_SHOW_EXT	0x4000
65 
66 #define	MDB_SCTP_SHOW_ALL	0xffffffff
67 
68 uint_t sctp_conn_hash_size;
69 static GElf_Sym sctp_list_sym;
70 static list_t sctp_list;
71 
72 /*
73  * Both the ill and ipif global arrays are small, so we just read
74  * in the whole arrays.
75  */
76 static sctp_ill_hash_t local_g_ills[SCTP_ILL_HASH];
77 static sctp_ipif_hash_t local_g_ipifs[SCTP_IPIF_HASH];
78 
79 /*
80  * Copy from usr/src/uts/common/os/list.c.  Should we have a generic
81  * mdb list walker?
82  */
83 #define	list_object(a, node) ((void *)(((char *)node) - (a)->list_offset))
84 
85 static char *
86 sctp_faddr_state(int state)
87 {
88 	char *statestr;
89 
90 	switch (state) {
91 	case SCTP_FADDRS_UNREACH:
92 		statestr = "Unreachable";
93 		break;
94 	case SCTP_FADDRS_DOWN:
95 		statestr = "Down";
96 		break;
97 	case SCTP_FADDRS_ALIVE:
98 		statestr = "Alive";
99 		break;
100 	case SCTP_FADDRS_UNCONFIRMED:
101 		statestr = "Unconfirmed";
102 		break;
103 	default:
104 		statestr = "Unknown";
105 		break;
106 	}
107 	return (statestr);
108 }
109 
110 /* ARGSUSED */
111 static int
112 sctp_faddr(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
113 {
114 	sctp_faddr_t fa[1];
115 	char *statestr;
116 
117 	if (!(flags & DCMD_ADDRSPEC))
118 		return (DCMD_USAGE);
119 
120 	if (mdb_vread(fa, sizeof (*fa), addr) == -1) {
121 		mdb_warn("cannot read fadder at %p", addr);
122 		return (DCMD_ERR);
123 	}
124 
125 	statestr = sctp_faddr_state(fa->state);
126 	mdb_printf("%<u>%p\t%<b>%N%</b>\t%s%</u>\n", addr, &fa->faddr,
127 	    statestr);
128 	mdb_printf("next\t\t%?p\tsaddr\t%N\n", fa->next, &fa->saddr);
129 	mdb_printf("rto\t\t%?d\tsrtt\t\t%?d\n", fa->rto, fa->srtt);
130 	mdb_printf("rttvar\t\t%?d\trtt_updates\t%?u\n", fa->rttvar,
131 	    fa->rtt_updates);
132 	mdb_printf("strikes\t\t%?d\tmax_retr\t%?d\n", fa->strikes,
133 	    fa->max_retr);
134 	mdb_printf("hb_expiry\t%?ld\thb_interval\t%?u\n", fa->hb_expiry,
135 	    fa->hb_interval);
136 	mdb_printf("pmss\t\t%?u\tcwnd\t\t%?u\n", fa->sfa_pmss, fa->cwnd);
137 	mdb_printf("ssthresh\t%?u\tsuna\t\t%?u\n", fa->ssthresh, fa->suna);
138 	mdb_printf("pba\t\t%?u\tacked\t\t%?u\n", fa->pba, fa->acked);
139 	mdb_printf("lastactive\t%?ld\thb_secret\t%?#lx\n", fa->lastactive,
140 	    fa->hb_secret);
141 	mdb_printf("timer_mp\t%?p\tire\t\t%?p\n", fa->timer_mp, fa->ire);
142 	mdb_printf("hb_pending\t%?d\ttimer_running\t%?d\n"
143 	    "df\t\t%?d\tpmtu_discovered\t%?d\n"
144 	    "isv4\t\t%?d\tretransmissions\t%?u\n",
145 	    fa->hb_pending, fa->timer_running, fa->df, fa->pmtu_discovered,
146 	    fa->isv4, fa->T3expire);
147 
148 	return (DCMD_OK);
149 }
150 
151 static void
152 print_set(sctp_set_t *sp)
153 {
154 	mdb_printf("\tbegin\t%<b>%?x%</b>\t\tend\t%<b>%?x%</b>\n",
155 	    sp->begin, sp->end);
156 	mdb_printf("\tnext\t%?p\tprev\t%?p\n", sp->next, sp->prev);
157 }
158 
159 /* ARGSUSED */
160 static int
161 sctp_set(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
162 {
163 	sctp_set_t sp[1];
164 
165 	if (!(flags & DCMD_ADDRSPEC))
166 		return (DCMD_USAGE);
167 
168 	if (mdb_vread(sp, sizeof (*sp), addr) == -1)
169 		return (DCMD_ERR);
170 
171 	print_set(sp);
172 
173 	return (DCMD_OK);
174 }
175 
176 static void
177 dump_sack_info(uintptr_t addr)
178 {
179 	sctp_set_t sp[1];
180 
181 	while (addr != 0) {
182 		if (mdb_vread(sp, sizeof (*sp), addr) == -1) {
183 			mdb_warn("failed to read sctp_set at %p", addr);
184 			return;
185 		}
186 
187 		addr = (uintptr_t)sp->next;
188 		print_set(sp);
189 	}
190 }
191 
192 static int
193 dump_msghdr(mblk_t *meta)
194 {
195 	sctp_msg_hdr_t smh;
196 
197 	if (mdb_vread(&smh, sizeof (smh), (uintptr_t)meta->b_rptr) == -1)
198 		return (-1);
199 
200 	mdb_printf("%<u>msg_hdr_t at \t%?p\tsentto\t%?p%</u>\n",
201 	    meta->b_rptr, SCTP_CHUNK_DEST(meta));
202 	mdb_printf("\tttl\t%?ld\ttob\t%?ld\n", smh.smh_ttl, smh.smh_tob);
203 	mdb_printf("\tsid\t%?u\tssn\t%?u\n", smh.smh_sid, smh.smh_ssn);
204 	mdb_printf("\tppid\t%?u\tflags\t%?s\n", smh.smh_ppid,
205 	    smh.smh_flags & MSG_UNORDERED ? "unordered" : " ");
206 	mdb_printf("\tcontext\t%?u\tmsglen\t%?d\n", smh.smh_context,
207 	    smh.smh_msglen);
208 
209 	return (0);
210 }
211 
212 static int
213 dump_datahdr(mblk_t *mp)
214 {
215 	sctp_data_hdr_t	sdc;
216 	uint16_t		sdh_int16;
217 	uint32_t		sdh_int32;
218 
219 	if (mdb_vread(&sdc, sizeof (sdc), (uintptr_t)mp->b_rptr) == -1)
220 		return (-1);
221 
222 	mdb_printf("%<u>data_chunk_t \t%?p\tsentto\t%?p%</u>\n",
223 	    mp->b_rptr, SCTP_CHUNK_DEST(mp));
224 	mdb_printf("\tsent\t%?d\t", SCTP_CHUNK_ISSENT(mp)?1:0);
225 	mdb_printf("retrans\t%?d\n", SCTP_CHUNK_WANT_REXMIT(mp)?1:0);
226 	mdb_printf("\tacked\t%?d\t", SCTP_CHUNK_ISACKED(mp)?1:0);
227 	mdb_printf("sackcnt\t%?u\n", SCTP_CHUNK_SACKCNT(mp));
228 
229 	mdb_nhconvert(&sdh_int16, &sdc.sdh_len, sizeof (sdc.sdh_len));
230 	mdb_printf("\tlen\t%?d\t", sdh_int16);
231 	mdb_printf("BBIT=%d", SCTP_DATA_GET_BBIT(&sdc) == 0 ? 0 : 1);
232 	mdb_printf("EBIT=%d", SCTP_DATA_GET_EBIT(&sdc) == 0 ? 0 : 1);
233 
234 	mdb_nhconvert(&sdh_int32, &sdc.sdh_tsn, sizeof (sdc.sdh_tsn));
235 	mdb_nhconvert(&sdh_int16, &sdc.sdh_sid, sizeof (sdc.sdh_sid));
236 	mdb_printf("\ttsn\t%?x\tsid\t%?hu\n", sdh_int32, sdh_int16);
237 
238 	mdb_nhconvert(&sdh_int16, &sdc.sdh_ssn, sizeof (sdc.sdh_ssn));
239 	mdb_nhconvert(&sdh_int32, &sdc.sdh_payload_id,
240 	    sizeof (sdc.sdh_payload_id));
241 	mdb_printf("\tssn\t%?hu\tppid\t%?d\n", sdh_int16, sdh_int32);
242 
243 	return (0);
244 }
245 
246 static int
247 sctp_sent_list(mblk_t *addr)
248 {
249 	mblk_t meta, mp;
250 
251 	if (!addr)
252 		return (0);
253 
254 	if (mdb_vread(&meta, sizeof (meta), (uintptr_t)addr) == -1)
255 		return (-1);
256 
257 	for (;;) {
258 		dump_msghdr(&meta);
259 
260 		if (meta.b_cont == NULL) {
261 			mdb_printf("No data chunks with message header!\n");
262 			return (-1);
263 		}
264 		if (mdb_vread(&mp, sizeof (mp),
265 			(uintptr_t)meta.b_cont) == -1) {
266 			return (-1);
267 		}
268 		for (;;) {
269 			dump_datahdr(&mp);
270 			if (!mp.b_next)
271 				break;
272 
273 			if (mdb_vread(&mp, sizeof (mp),
274 			    (uintptr_t)(mp.b_next)) == -1)
275 				return (-1);
276 		}
277 		if (meta.b_next == NULL)
278 			break;
279 		if (mdb_vread(&meta, sizeof (meta),
280 		    (uintptr_t)meta.b_next) == -1)
281 			return (-1);
282 	}
283 
284 	return (0);
285 }
286 
287 static int
288 sctp_unsent_list(mblk_t *addr)
289 {
290 	mblk_t meta;
291 
292 	if (!addr)
293 		return (0);
294 
295 	if (mdb_vread(&meta, sizeof (meta), (uintptr_t)addr) == -1)
296 		return (-1);
297 
298 	for (;;) {
299 		dump_msghdr(&meta);
300 
301 		if (meta.b_next == NULL)
302 			break;
303 
304 		if (mdb_vread(&meta, sizeof (meta),
305 		    (uintptr_t)meta.b_next) == -1)
306 			return (-1);
307 	}
308 
309 	return (0);
310 }
311 
312 /* ARGSUSED */
313 static int
314 sctp_xmit_list(uintptr_t addr, uint_t flags, int ac, const mdb_arg_t *av)
315 {
316 	sctp_t sctp;
317 
318 	if (!(flags & DCMD_ADDRSPEC))
319 		return (DCMD_USAGE);
320 
321 	if (mdb_vread(&sctp, sizeof (sctp), addr) == -1)
322 		return (DCMD_ERR);
323 
324 	mdb_printf("%<b>Chunkified TX list%</b>\n");
325 	if (sctp_sent_list(sctp.sctp_xmit_head) < 0)
326 		return (DCMD_ERR);
327 
328 	mdb_printf("%<b>Unchunkified TX list%</b>\n");
329 	if (sctp_unsent_list(sctp.sctp_xmit_unsent) < 0)
330 		return (DCMD_ERR);
331 
332 	return (DCMD_OK);
333 }
334 
335 /* ARGSUSED */
336 static int
337 sctp_mdata_chunk(uintptr_t addr, uint_t flags, int ac, const mdb_arg_t *av)
338 {
339 	sctp_data_hdr_t dc;
340 	mblk_t mp;
341 
342 	if (!(flags & DCMD_ADDRSPEC))
343 		return (DCMD_USAGE);
344 
345 	if (mdb_vread(&mp, sizeof (mp), addr) == -1)
346 		return (DCMD_ERR);
347 
348 	if (mdb_vread(&dc, sizeof (dc), (uintptr_t)mp.b_rptr) == -1)
349 		return (DCMD_ERR);
350 
351 	mdb_printf("%<b>%-?p%</b>tsn\t%?x\tsid\t%?hu\n", addr,
352 	    dc.sdh_tsn, dc.sdh_sid);
353 	mdb_printf("%-?sssn\t%?hu\tppid\t%?x\n", "", dc.sdh_ssn,
354 	    dc.sdh_payload_id);
355 
356 	return (DCMD_OK);
357 }
358 
359 /* ARGSUSED */
360 static int
361 sctp_istr_msgs(uintptr_t addr, uint_t flags, int ac, const mdb_arg_t *av)
362 {
363 	mblk_t			istrmp;
364 	mblk_t			dmp;
365 	sctp_data_hdr_t 	dp;
366 	uintptr_t		daddr;
367 	uintptr_t		chaddr;
368 	boolean_t		bbit;
369 	boolean_t		ebit;
370 
371 	if (!(flags & DCMD_ADDRSPEC))
372 		return (DCMD_USAGE);
373 
374 	do {
375 		if (mdb_vread(&istrmp, sizeof (istrmp), addr) == -1)
376 			return (DCMD_ERR);
377 
378 		mdb_printf("\tistr mblk at %p: next: %?p\n"
379 		    "\t\tprev: %?p\tcont: %?p\n", addr, istrmp.b_next,
380 		    istrmp.b_prev, istrmp.b_cont);
381 		daddr = (uintptr_t)&istrmp;
382 		do {
383 			if (mdb_vread(&dmp, sizeof (dmp), daddr) == -1)
384 				break;
385 			chaddr = (uintptr_t)dmp.b_rptr;
386 			if (mdb_vread(&dp, sizeof (dp), chaddr) == -1)
387 				break;
388 
389 			bbit = (SCTP_DATA_GET_BBIT(&dp) != 0);
390 			ebit = (SCTP_DATA_GET_EBIT(&dp) != 0);
391 
392 			mdb_printf("\t\t\ttsn: %x  bbit: %d  ebit: %d\n",
393 			    dp.sdh_tsn, bbit, ebit);
394 
395 
396 			daddr = (uintptr_t)dmp.b_cont;
397 		} while (daddr != NULL);
398 
399 		addr = (uintptr_t)istrmp.b_next;
400 	} while (addr != NULL);
401 
402 	return (DCMD_OK);
403 }
404 
405 /* ARGSUSED */
406 static int
407 sctp_reass_list(uintptr_t addr, uint_t flags, int ac, const mdb_arg_t *av)
408 {
409 	sctp_reass_t srp;
410 	mblk_t srpmp;
411 	sctp_data_hdr_t dp;
412 	mblk_t dmp;
413 	uintptr_t daddr;
414 	uintptr_t chaddr;
415 	boolean_t bbit, ebit;
416 
417 	if (!(flags & DCMD_ADDRSPEC))
418 		return (DCMD_USAGE);
419 
420 	do {
421 		if (mdb_vread(&srpmp, sizeof (srpmp), addr) == -1)
422 			return (DCMD_ERR);
423 
424 		if (mdb_vread(&srp, sizeof (srp),
425 		    (uintptr_t)srpmp.b_datap->db_base) == -1)
426 			return (DCMD_ERR);
427 
428 		mdb_printf("\treassembly mblk at %p: next: %?p\n"
429 		    "\t\tprev: %?p\tcont: %?p\n", addr, srpmp.b_next,
430 		    srpmp.b_prev, srpmp.b_cont);
431 		mdb_printf("\t\tssn: %hu\tneeded: %hu\tgot: %hu\ttail: %?p\n"
432 		    "\t\tpartial_delivered: %s\n", srp.ssn, srp.needed,
433 		    srp.got, srp.tail, srp.partial_delivered ? "TRUE" :
434 		    "FALSE");
435 
436 		/* display the contents of this ssn's reassemby list */
437 		daddr = DB_TYPE(&srpmp) == M_CTL ? (uintptr_t)srpmp.b_cont :
438 		    (uintptr_t)&srpmp;
439 		do {
440 			if (mdb_vread(&dmp, sizeof (dmp), daddr) == -1)
441 				break;
442 			chaddr = (uintptr_t)dmp.b_rptr;
443 			if (mdb_vread(&dp, sizeof (dp), chaddr) == -1)
444 				break;
445 
446 			bbit = (SCTP_DATA_GET_BBIT(&dp) != 0);
447 			ebit = (SCTP_DATA_GET_EBIT(&dp) != 0);
448 
449 			mdb_printf("\t\t\ttsn: %x  bbit: %d  ebit: %d\n",
450 			    dp.sdh_tsn, bbit, ebit);
451 
452 			daddr = (uintptr_t)dmp.b_cont;
453 		} while (daddr != NULL);
454 
455 		addr = (uintptr_t)srpmp.b_next;
456 	} while (addr != NULL);
457 
458 	return (DCMD_OK);
459 }
460 
461 /* ARGSUSED */
462 static int
463 sctp_uo_reass_list(uintptr_t addr, uint_t flags, int ac, const mdb_arg_t *av)
464 {
465 	sctp_data_hdr_t	dp;
466 	mblk_t		dmp;
467 	uintptr_t	chaddr;
468 	boolean_t	bbit;
469 	boolean_t	ebit;
470 	boolean_t	ubit;
471 
472 	if (!(flags & DCMD_ADDRSPEC))
473 		return (DCMD_USAGE);
474 
475 	do {
476 		if (mdb_vread(&dmp, sizeof (dmp), addr) == -1)
477 			return (DCMD_ERR);
478 
479 		mdb_printf("\treassembly mblk at %p: next: %?p\n"
480 		    "\t\tprev: %?p\n", addr, dmp.b_next, dmp.b_prev);
481 
482 		chaddr = (uintptr_t)dmp.b_rptr;
483 		if (mdb_vread(&dp, sizeof (dp), chaddr) == -1)
484 			break;
485 
486 		bbit = (SCTP_DATA_GET_BBIT(&dp) != 0);
487 		ebit = (SCTP_DATA_GET_EBIT(&dp) != 0);
488 		ubit = (SCTP_DATA_GET_UBIT(&dp) != 0);
489 
490 		mdb_printf("\t\t\tsid: %hu ssn: %hu tsn: %x "
491 		    "flags: %x (U=%d B=%d E=%d)\n", dp.sdh_sid, dp.sdh_ssn,
492 		    dp.sdh_tsn, dp.sdh_flags, ubit, bbit, ebit);
493 
494 		addr = (uintptr_t)dmp.b_next;
495 	} while (addr != NULL);
496 
497 	return (DCMD_OK);
498 }
499 
500 static int
501 sctp_instr(uintptr_t addr, uint_t flags, int ac, const mdb_arg_t *av)
502 {
503 	sctp_instr_t sip;
504 
505 	if (!(flags & DCMD_ADDRSPEC))
506 		return (DCMD_USAGE);
507 
508 	if (mdb_vread(&sip, sizeof (sip), addr) == -1)
509 		return (DCMD_ERR);
510 
511 	mdb_printf("%<b>%-?p%</b>\n\tmsglist\t%?p\tnmsgs\t%?d\n"
512 	    "\tnextseq\t%?d\treass\t%?p\n", addr, sip.istr_msgs,
513 	    sip.istr_nmsgs, sip.nextseq, sip.istr_reass);
514 	mdb_set_dot(addr + sizeof (sip));
515 
516 	return (sctp_reass_list((uintptr_t)sip.istr_reass, flags, ac, av));
517 }
518 
519 static const char *
520 state2str(sctp_t *sctp)
521 {
522 	switch (sctp->sctp_state) {
523 	case SCTPS_IDLE:		return ("SCTPS_IDLE");
524 	case SCTPS_BOUND:		return ("SCTPS_BOUND");
525 	case SCTPS_LISTEN:		return ("SCTPS_LISTEN");
526 	case SCTPS_COOKIE_WAIT:		return ("SCTPS_COOKIE_WAIT");
527 	case SCTPS_COOKIE_ECHOED:	return ("SCTPS_COOKIE_ECHOED");
528 	case SCTPS_ESTABLISHED:		return ("SCTPS_ESTABLISHED");
529 	case SCTPS_SHUTDOWN_PENDING:	return ("SCTPS_SHUTDOWN_PENDING");
530 	case SCTPS_SHUTDOWN_SENT:	return ("SCTPS_SHUTDOWN_SENT");
531 	case SCTPS_SHUTDOWN_RECEIVED:	return ("SCTPS_SHUTDOWN_RECEIVED");
532 	case SCTPS_SHUTDOWN_ACK_SENT:	return ("SCTPS_SHUTDOWN_ACK_SENT");
533 	default:			return ("UNKNOWN STATE");
534 	}
535 }
536 
537 static void
538 show_sctp_flags(sctp_t *sctp)
539 {
540 	mdb_printf("\tunderstands_asconf\t%d\n",
541 	    sctp->sctp_understands_asconf);
542 	mdb_printf("\tdebug\t\t\t%d\n", sctp->sctp_debug);
543 	mdb_printf("\tdontroute\t\t%d\n", sctp->sctp_dontroute);
544 	mdb_printf("\tbroadcast\t\t%d\n", sctp->sctp_broadcast);
545 
546 	mdb_printf("\tuseloopback\t\t%d\n", sctp->sctp_useloopback);
547 	mdb_printf("\tcchunk_pend\t\t%d\n", sctp->sctp_cchunk_pend);
548 	mdb_printf("\tdgram_errind\t\t%d\n", sctp->sctp_dgram_errind);
549 	mdb_printf("\treuseaddr\t\t%d\n", sctp->sctp_reuseaddr);
550 
551 	mdb_printf("\tlinger\t\t\t%d\n", sctp->sctp_linger);
552 	if (sctp->sctp_lingering)
553 		return;
554 	mdb_printf("\tlingering\t\t%d\n", sctp->sctp_lingering);
555 	mdb_printf("\tloopback\t\t%d\n", sctp->sctp_loopback);
556 	mdb_printf("\tforce_sack\t\t%d\n", sctp->sctp_force_sack);
557 
558 	mdb_printf("\tack_timer_runing\t%d\n", sctp->sctp_ack_timer_running);
559 	mdb_printf("\trecvdstaddr\t\t%d\n", sctp->sctp_recvdstaddr);
560 	mdb_printf("\thwcksum\t\t\t%d\n", sctp->sctp_hwcksum);
561 	mdb_printf("\tunderstands_addip\t%d\n", sctp->sctp_understands_addip);
562 
563 	mdb_printf("\tbound_to_all\t\t%d\n", sctp->sctp_bound_to_all);
564 	mdb_printf("\tcansleep\t\t%d\n", sctp->sctp_cansleep);
565 	mdb_printf("\tdetached\t\t%d\n", sctp->sctp_detached);
566 	mdb_printf("\tsend_adaption\t\t%d\n", sctp->sctp_send_adaption);
567 
568 	mdb_printf("\trecv_adaption\t\t%d\n", sctp->sctp_recv_adaption);
569 	mdb_printf("\tndelay\t\t\t%d\n", sctp->sctp_ndelay);
570 	mdb_printf("\tcondemned\t\t%d\n", sctp->sctp_condemned);
571 	mdb_printf("\tchk_fast_rexmit\t\t%d\n", sctp->sctp_chk_fast_rexmit);
572 	mdb_printf("\tprsctp_aware\t\t%d\n", sctp->sctp_prsctp_aware);
573 	mdb_printf("\tlinklocal\t\t%d\n", sctp->sctp_linklocal);
574 
575 	mdb_printf("\trecvsndrcvinfo\t\t%d\n", sctp->sctp_recvsndrcvinfo);
576 	mdb_printf("\trecvassocevnt\t\t%d\n", sctp->sctp_recvassocevnt);
577 	mdb_printf("\trecvpathevnt\t\t%d\n", sctp->sctp_recvpathevnt);
578 	mdb_printf("\trecvsendfailevnt\t%d\n", sctp->sctp_recvsendfailevnt);
579 
580 	mdb_printf("\trecvpeerevnt\t\t%d\n", sctp->sctp_recvpeererr);
581 	mdb_printf("\trecvchutdownevnt\t%d\n", sctp->sctp_recvshutdownevnt);
582 	mdb_printf("\trecvcpdnevnt\t\t%d\n", sctp->sctp_recvpdevnt);
583 	mdb_printf("\trecvcalevnt\t\t%d\n\n", sctp->sctp_recvalevnt);
584 }
585 
586 /*
587  * Given a sctp_saddr_ipif_t, print out its address.  This assumes
588  * that addr contains the sctp_addr_ipif_t structure already and this
589  * function does not need to read it in.
590  */
591 /* ARGSUSED */
592 static int
593 print_saddr(uintptr_t ptr, const void *addr, void *cbdata)
594 {
595 	sctp_saddr_ipif_t *saddr = (sctp_saddr_ipif_t *)addr;
596 	sctp_ipif_t ipif;
597 	char *statestr;
598 
599 	/* Read in the sctp_ipif object */
600 	if (mdb_vread(&ipif, sizeof (ipif), (uintptr_t)saddr->saddr_ipifp) ==
601 	    -1) {
602 		mdb_warn("cannot read ipif at %p", saddr->saddr_ipifp);
603 		return (WALK_ERR);
604 	}
605 
606 	switch (ipif.sctp_ipif_state) {
607 	case SCTP_IPIFS_CONDEMNED:
608 		statestr = "Condemned";
609 		break;
610 	case SCTP_IPIFS_INVALID:
611 		statestr = "Invalid";
612 		break;
613 	case SCTP_IPIFS_DOWN:
614 		statestr = "Down";
615 		break;
616 	case SCTP_IPIFS_UP:
617 		statestr = "Up";
618 		break;
619 	default:
620 		statestr = "Unknown";
621 		break;
622 	}
623 	mdb_printf("\t%p\t%N% (%s", saddr->saddr_ipifp, &ipif.sctp_ipif_saddr,
624 	    statestr);
625 	if (saddr->saddr_ipif_dontsrc == 1)
626 		mdb_printf("/Dontsrc");
627 	if (saddr->saddr_ipif_unconfirmed == 1)
628 		mdb_printf("/Unconfirmed");
629 	if (saddr->saddr_ipif_delete_pending == 1)
630 		mdb_printf("/DeletePending");
631 	mdb_printf(")\n");
632 	mdb_printf("\t\t\tMTU %d id %d zoneid %d IPIF flags %x\n",
633 	    ipif.sctp_ipif_mtu, ipif.sctp_ipif_id,
634 	    ipif.sctp_ipif_zoneid, ipif.sctp_ipif_flags);
635 	return (WALK_NEXT);
636 }
637 
638 /*
639  * Given a sctp_faddr_t, print out its address.  This assumes that
640  * addr contains the sctp_faddr_t structure already and this function
641  * does not need to read it in.
642  */
643 static int
644 print_faddr(uintptr_t ptr, const void *addr, void *cbdata)
645 {
646 	char	*statestr;
647 	sctp_faddr_t *faddr = (sctp_faddr_t *)addr;
648 	int *i = cbdata;
649 
650 	statestr = sctp_faddr_state(faddr->state);
651 
652 	mdb_printf("\t%d:\t%N\t%?p (%s)\n", (*i)++, &faddr->faddr, ptr,
653 	    statestr);
654 	return (WALK_NEXT);
655 }
656 
657 int
658 sctp(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
659 {
660 	sctp_t sctp;
661 	conn_t connp;
662 	int i;
663 	uint_t opts = 0;
664 	uint_t paddr = 0;
665 	in_port_t lport, fport;
666 
667 	if (!(flags & DCMD_ADDRSPEC))
668 		return (DCMD_USAGE);
669 
670 	if (mdb_vread(&sctp, sizeof (sctp), addr) == -1) {
671 		mdb_warn("failed to read sctp_t at: %p\n", addr);
672 		return (DCMD_ERR);
673 	}
674 	if (mdb_vread(&connp, sizeof (connp),
675 	    (uintptr_t)sctp.sctp_connp) == -1) {
676 		mdb_warn("failed to read conn_t at: %p\n", sctp.sctp_connp);
677 		return (DCMD_ERR);
678 	}
679 
680 	if (mdb_getopts(argc, argv,
681 		'a', MDB_OPT_SETBITS, MDB_SCTP_SHOW_ALL, &opts,
682 		'f', MDB_OPT_SETBITS, MDB_SCTP_SHOW_FLAGS, &opts,
683 		'h', MDB_OPT_SETBITS, MDB_SCTP_SHOW_HASH, &opts,
684 		'o', MDB_OPT_SETBITS, MDB_SCTP_SHOW_OUT, &opts,
685 		'i', MDB_OPT_SETBITS, MDB_SCTP_SHOW_IN, &opts,
686 		'm', MDB_OPT_SETBITS, MDB_SCTP_SHOW_MISC, &opts,
687 		'r', MDB_OPT_SETBITS, MDB_SCTP_SHOW_RTT, &opts,
688 		'S', MDB_OPT_SETBITS, MDB_SCTP_SHOW_STATS, &opts,
689 		'F', MDB_OPT_SETBITS, MDB_SCTP_SHOW_FLOW, &opts,
690 		'H', MDB_OPT_SETBITS, MDB_SCTP_SHOW_HDR, &opts,
691 		'p', MDB_OPT_SETBITS, MDB_SCTP_SHOW_PMTUD, &opts,
692 		'R', MDB_OPT_SETBITS, MDB_SCTP_SHOW_RXT, &opts,
693 		'C', MDB_OPT_SETBITS, MDB_SCTP_SHOW_CONN, &opts,
694 		'c', MDB_OPT_SETBITS, MDB_SCTP_SHOW_CLOSE, &opts,
695 		'e', MDB_OPT_SETBITS, MDB_SCTP_SHOW_EXT, &opts,
696 		'P', MDB_OPT_SETBITS, 1, &paddr,
697 		'd', MDB_OPT_SETBITS, MDB_SCTP_DUMP_ADDRS, &opts) != argc) {
698 		return (DCMD_USAGE);
699 	}
700 
701 	/* non-verbose faddrs, suitable for pipelines to sctp_faddr */
702 	if (paddr != 0) {
703 		sctp_faddr_t faddr, *fp;
704 		for (fp = sctp.sctp_faddrs; fp != NULL; fp = faddr.next) {
705 			if (mdb_vread(&faddr, sizeof (faddr), (uintptr_t)fp)
706 			    == -1) {
707 				mdb_warn("failed to read faddr at %p",
708 				    fp);
709 				return (DCMD_ERR);
710 			}
711 			mdb_printf("%p\n", fp);
712 		}
713 		return (DCMD_OK);
714 	}
715 
716 	mdb_nhconvert(&lport, &sctp.sctp_lport, sizeof (lport));
717 	mdb_nhconvert(&fport, &sctp.sctp_fport, sizeof (fport));
718 	mdb_printf("%<u>%p% %22s S=%-6hu D=%-6hu% ZONE=%d%</u>", addr,
719 	    state2str(&sctp), lport, fport, connp.conn_zoneid);
720 
721 	if (sctp.sctp_faddrs) {
722 		sctp_faddr_t faddr;
723 		if (mdb_vread(&faddr, sizeof (faddr),
724 		    (uintptr_t)sctp.sctp_faddrs) != -1)
725 			mdb_printf("%<u> %N%</u>", &faddr.faddr);
726 	}
727 	mdb_printf("\n");
728 
729 	if (opts & MDB_SCTP_DUMP_ADDRS) {
730 		mdb_printf("%<b>Local and Peer Addresses%</b>\n");
731 
732 		/* Display source addresses */
733 		mdb_printf("nsaddrs\t\t%?d\n", sctp.sctp_nsaddrs);
734 		(void) mdb_pwalk("sctp_walk_saddr", print_saddr, NULL, addr);
735 
736 		/* Display peer addresses */
737 		mdb_printf("faddrs\t\t%?p\n", sctp.sctp_faddrs);
738 		i = 1;
739 		(void) mdb_pwalk("sctp_walk_faddr", print_faddr, &i, addr);
740 
741 		mdb_printf("lastfaddr\t%?p\tprimary\t\t%?p\n",
742 		    sctp.sctp_lastfaddr, sctp.sctp_primary);
743 		mdb_printf("current\t\t%?p\tlastdata\t%?p\n",
744 		    sctp.sctp_current, sctp.sctp_lastdata);
745 	}
746 
747 	if (opts & MDB_SCTP_SHOW_OUT) {
748 		mdb_printf("%<b>Outbound Data%</b>\n");
749 		mdb_printf("xmit_head\t%?p\txmit_tail\t%?p\n",
750 		    sctp.sctp_xmit_head, sctp.sctp_xmit_tail);
751 		mdb_printf("xmit_unsent\t%?p\txmit_unsent_tail%?p\n",
752 		    sctp.sctp_xmit_unsent, sctp.sctp_xmit_unsent_tail);
753 		mdb_printf("xmit_unacked\t%?p\n", sctp.sctp_xmit_unacked);
754 		mdb_printf("unacked\t\t%?u\tunsent\t\t%?ld\n",
755 		    sctp.sctp_unacked, sctp.sctp_unsent);
756 		mdb_printf("ltsn\t\t%?x\tlastack_rxd\t%?x\n",
757 		    sctp.sctp_ltsn, sctp.sctp_lastack_rxd);
758 		mdb_printf("recovery_tsn\t%?x\tadv_pap\t\t%?x\n",
759 		    sctp.sctp_recovery_tsn, sctp.sctp_adv_pap);
760 		mdb_printf("num_ostr\t%?hu\tostrcntrs\t%?p\n",
761 		    sctp.sctp_num_ostr, sctp.sctp_ostrcntrs);
762 
763 		mdb_printf("%<b>Default Send Parameters%</b>\n");
764 		mdb_printf("def_stream\t%?u\tdef_flags\t%?x\n",
765 		    sctp.sctp_def_stream, sctp.sctp_def_flags);
766 		mdb_printf("def_ppid\t%?x\tdef_context\t%?x\n",
767 		    sctp.sctp_def_ppid, sctp.sctp_def_context);
768 		mdb_printf("def_timetolive\t%?u\n",
769 		    sctp.sctp_def_timetolive);
770 	}
771 
772 	if (opts & MDB_SCTP_SHOW_IN) {
773 		mdb_printf("%<b>Inbound Data%</b>\n");
774 		mdb_printf("sack_info\t%?p\tsack_gaps\t%?d\n",
775 		    sctp.sctp_sack_info, sctp.sctp_sack_gaps);
776 		dump_sack_info((uintptr_t)sctp.sctp_sack_info);
777 		mdb_printf("ftsn\t\t%?x\tlastacked\t%?x\n",
778 		    sctp.sctp_ftsn, sctp.sctp_lastacked);
779 		mdb_printf("istr_nmsgs\t%?d\tsack_toggle\t%?d\n",
780 		    sctp.sctp_istr_nmsgs, sctp.sctp_sack_toggle);
781 		mdb_printf("ack_mp\t\t%?p\n", sctp.sctp_ack_mp);
782 		mdb_printf("num_istr\t%?hu\tinstr\t\t%?p\n",
783 		    sctp.sctp_num_istr, sctp.sctp_instr);
784 		mdb_printf("unord_reass\t%?p\n", sctp.sctp_uo_frags);
785 	}
786 
787 	if (opts & MDB_SCTP_SHOW_RTT) {
788 		mdb_printf("%<b>RTT Tracking%</b>\n");
789 		mdb_printf("rtt_tsn\t\t%?x\tout_time\t%?ld\n",
790 		    sctp.sctp_rtt_tsn, sctp.sctp_out_time);
791 	}
792 
793 	if (opts & MDB_SCTP_SHOW_FLOW) {
794 		mdb_printf("%<b>Flow Control%</b>\n");
795 		mdb_printf("txmit_hiwater\t%?d\n"
796 		    "xmit_lowater\t%?d\tfrwnd\t\t%?u\n"
797 		    "rwnd\t\t%?u\trxqueued\t%?u\n"
798 		    "cwnd_max\t%?u\n", sctp.sctp_xmit_hiwater,
799 		    sctp.sctp_xmit_lowater, sctp.sctp_frwnd,
800 		    sctp.sctp_rwnd, sctp.sctp_rxqueued, sctp.sctp_cwnd_max);
801 	}
802 
803 	if (opts & MDB_SCTP_SHOW_HDR) {
804 		mdb_printf("%<b>Composite Headers%</b>\n");
805 		mdb_printf("iphc\t\t%?p\tiphc6\t\t%?p\n"
806 		    "iphc_len\t%?d\tiphc6_len\t%?d\n"
807 		    "hdr_len\t\t%?d\thdr6_len\t%?d\n"
808 		    "ipha\t\t%?p\tip6h\t\t%?p\n"
809 		    "ip_hdr_len\t%?d\tip_hdr6_len\t%?d\n"
810 		    "sctph\t\t%?p\tsctph6\t\t%?p\n"
811 		    "lvtag\t\t%?x\tfvtag\t\t%?x\n", sctp.sctp_iphc,
812 		    sctp.sctp_iphc6, sctp.sctp_iphc_len,
813 		    sctp.sctp_iphc6_len, sctp.sctp_hdr_len,
814 		    sctp.sctp_hdr6_len, sctp.sctp_ipha, sctp.sctp_ip6h,
815 		    sctp.sctp_ip_hdr_len, sctp.sctp_ip_hdr6_len,
816 		    sctp.sctp_sctph, sctp.sctp_sctph6, sctp.sctp_lvtag,
817 		    sctp.sctp_fvtag);
818 	}
819 
820 	if (opts & MDB_SCTP_SHOW_PMTUD) {
821 		mdb_printf("%<b>PMTUd%</b>\n");
822 		mdb_printf("last_mtu_probe\t%?ld\tmtu_probe_intvl\t%?ld\n"
823 		    "mss\t\t%?u\n",
824 		    sctp.sctp_last_mtu_probe, sctp.sctp_mtu_probe_intvl,
825 		    sctp.sctp_mss);
826 	}
827 
828 	if (opts & MDB_SCTP_SHOW_RXT) {
829 		mdb_printf("%<b>Retransmit Info%</b>\n");
830 		mdb_printf("cookie_mp\t%?p\tstrikes\t\t%?d\n"
831 		    "max_init_rxt\t%?d\tpa_max_rxt\t%?d\n"
832 		    "pp_max_rxt\t%?d\trto_max\t\t%?u\n"
833 		    "rto_min\t\t%?u\trto_initial\t%?u\n"
834 		    "init_rto_max\t%?u\n", sctp.sctp_cookie_mp,
835 		    sctp.sctp_strikes, sctp.sctp_max_init_rxt,
836 		    sctp.sctp_pa_max_rxt, sctp.sctp_pp_max_rxt,
837 		    sctp.sctp_rto_max, sctp.sctp_rto_min,
838 		    sctp.sctp_rto_initial, sctp.sctp_init_rto_max);
839 	}
840 
841 	if (opts & MDB_SCTP_SHOW_CONN) {
842 		mdb_printf("%<b>Connection State%</b>\n");
843 		mdb_printf("last_secret_update%?ld\n",
844 		    sctp.sctp_last_secret_update);
845 
846 		mdb_printf("secret\t\t");
847 		for (i = 0; i < SCTP_SECRET_LEN; i++) {
848 			if (i % 2 == 0)
849 				mdb_printf("0x%02x", sctp.sctp_secret[i]);
850 			else
851 				mdb_printf("%02x ", sctp.sctp_secret[i]);
852 		}
853 		mdb_printf("\n");
854 		mdb_printf("old_secret\t");
855 		for (i = 0; i < SCTP_SECRET_LEN; i++) {
856 			if (i % 2 == 0)
857 				mdb_printf("0x%02x", sctp.sctp_old_secret[i]);
858 			else
859 				mdb_printf("%02x ", sctp.sctp_old_secret[i]);
860 		}
861 		mdb_printf("\n");
862 	}
863 
864 	if (opts & MDB_SCTP_SHOW_STATS) {
865 		mdb_printf("%<b>Stats Counters%</b>\n");
866 		mdb_printf("opkts\t\t%?llu\tobchunks\t%?llu\n"
867 		    "odchunks\t%?llu\toudchunks\t%?llu\n"
868 		    "rxtchunks\t%?llu\tT1expire\t%?lu\n"
869 		    "T2expire\t%?lu\tT3expire\t%?lu\n"
870 		    "msgcount\t%?llu\tprsctpdrop\t%?llu\n"
871 		    "AssocStartTime\t%?lu\n",
872 		    sctp.sctp_opkts, sctp.sctp_obchunks,
873 		    sctp.sctp_odchunks, sctp.sctp_oudchunks,
874 		    sctp.sctp_rxtchunks, sctp.sctp_T1expire,
875 		    sctp.sctp_T2expire, sctp.sctp_T3expire,
876 		    sctp.sctp_msgcount, sctp.sctp_prsctpdrop,
877 		    sctp.sctp_assoc_start_time);
878 		mdb_printf("ipkts\t\t%?llu\tibchunks\t%?llu\n"
879 		    "idchunks\t%?llu\tiudchunks\t%?llu\n"
880 		    "fragdmsgs\t%?llu\treassmsgs\t%?llu\n",
881 		    sctp.sctp_ipkts, sctp.sctp_ibchunks,
882 		    sctp.sctp_idchunks, sctp.sctp_iudchunks,
883 		    sctp.sctp_fragdmsgs, sctp.sctp_reassmsgs);
884 	}
885 
886 	if (opts & MDB_SCTP_SHOW_HASH) {
887 		mdb_printf("%<b>Hash Tables%</b>\n");
888 		mdb_printf("conn_hash_next\t%?p\t", sctp.sctp_conn_hash_next);
889 		mdb_printf("conn_hash_prev\t%?p\n", sctp.sctp_conn_hash_prev);
890 		mdb_printf("[ conn_hash bucket\t%?d ]\n",
891 		    SCTP_CONN_HASH(sctp.sctp_ports));
892 
893 		mdb_printf("listen_hash_next%?p\t",
894 		    sctp.sctp_listen_hash_next);
895 		mdb_printf("listen_hash_prev%?p\n",
896 		    sctp.sctp_listen_hash_prev);
897 		mdb_nhconvert(&lport, &sctp.sctp_lport, sizeof (lport));
898 		mdb_printf("[ listen_hash bucket\t%?d ]\n",
899 		    SCTP_LISTEN_HASH(lport));
900 
901 		mdb_printf("conn_tfp\t%?p\t", sctp.sctp_conn_tfp);
902 		mdb_printf("listen_tfp\t%?p\n", sctp.sctp_listen_tfp);
903 
904 		mdb_printf("bind_hash\t%?p\tptpbhn\t\t%?p\n",
905 		    sctp.sctp_bind_hash, sctp.sctp_ptpbhn);
906 		mdb_printf("bind_lockp\t%?p\n",
907 		    sctp.sctp_bind_lockp);
908 		mdb_printf("[ bind_hash bucket\t%?d ]\n",
909 		    SCTP_BIND_HASH(lport));
910 	}
911 
912 	if (opts & MDB_SCTP_SHOW_CLOSE) {
913 		mdb_printf("%<b>Cleanup / Close%</b>\n");
914 		mdb_printf("shutdown_faddr\t%?p\tclient_errno\t%?d\n"
915 		    "lingertime\t%?d\trefcnt\t\t%?hu\n",
916 		    sctp.sctp_shutdown_faddr, sctp.sctp_client_errno,
917 		    sctp.sctp_lingertime, sctp.sctp_refcnt);
918 	}
919 
920 	if (opts & MDB_SCTP_SHOW_MISC) {
921 		mdb_printf("%<b>Miscellaneous%</b>\n");
922 		mdb_printf("bound_if\t%?u\theartbeat_mp\t%?p\n"
923 		    "family\t\t%?u\tipversion\t%?hu\n"
924 		    "hb_interval\t%?u\tautoclose\t%?d\n"
925 		    "active\t\t%?ld\ttx_adaption_code%?x\n"
926 		    "rx_adaption_code%?x\ttimer_mp\t%?p\n",
927 		    sctp.sctp_bound_if, sctp.sctp_heartbeat_mp,
928 		    sctp.sctp_family, sctp.sctp_ipversion,
929 		    sctp.sctp_hb_interval, sctp.sctp_autoclose,
930 		    sctp.sctp_active, sctp.sctp_tx_adaption_code,
931 		    sctp.sctp_rx_adaption_code, sctp.sctp_timer_mp);
932 	}
933 
934 	if (opts & MDB_SCTP_SHOW_EXT) {
935 		mdb_printf("%<b>Extensions and Reliable Ctl Chunks%</b>\n");
936 		mdb_printf("cxmit_list\t%?p\tlcsn\t\t%?x\n"
937 		    "fcsn\t\t%?x\n", sctp.sctp_cxmit_list, sctp.sctp_lcsn,
938 		    sctp.sctp_fcsn);
939 	}
940 
941 	if (opts & MDB_SCTP_SHOW_FLAGS) {
942 		mdb_printf("%<b>Flags%</b>\n");
943 		show_sctp_flags(&sctp);
944 	}
945 
946 	return (DCMD_OK);
947 }
948 
949 typedef struct fanout_walk_data {
950 	int index;
951 	int size;
952 	uintptr_t sctp;
953 	sctp_tf_t *fanout;
954 	uintptr_t (*getnext)(sctp_t *);
955 } fanout_walk_data_t;
956 
957 typedef struct fanout_init {
958 	const char *symname;
959 	int (*getsize)();
960 	uintptr_t (*getnext)(sctp_t *);
961 } fanout_init_t;
962 
963 static uintptr_t
964 listen_next(sctp_t *sctp)
965 {
966 	return ((uintptr_t)sctp->sctp_listen_hash_next);
967 }
968 
969 static int
970 listen_size(void)
971 {
972 	return (SCTP_LISTEN_FANOUT_SIZE);
973 }
974 
975 static uintptr_t
976 conn_next(sctp_t *sctp)
977 {
978 	return ((uintptr_t)sctp->sctp_conn_hash_next);
979 }
980 
981 static int
982 conn_size(void)
983 {
984 	GElf_Sym sym;
985 	int size;
986 
987 	if (mdb_lookup_by_name("sctp_conn_hash_size", &sym) == -1) {
988 		mdb_warn("can't read 'sctp_conn_hash_size'");
989 		return (1);
990 	}
991 	if (mdb_vread(&size, sizeof (size), sym.st_value) == -1) {
992 		mdb_warn("can't dereference 'sctp_conn_hash_size'");
993 		return (1);
994 	}
995 	return (size);
996 }
997 
998 static uintptr_t
999 bind_next(sctp_t *sctp)
1000 {
1001 	return ((uintptr_t)sctp->sctp_bind_hash);
1002 }
1003 
1004 static int
1005 bind_size(void)
1006 {
1007 	return (SCTP_BIND_FANOUT_SIZE);
1008 }
1009 
1010 static intptr_t
1011 find_next_hash_item(fanout_walk_data_t *fw)
1012 {
1013 	sctp_tf_t tf;
1014 	sctp_t sctp;
1015 
1016 	/* first try to continue down the hash chain */
1017 	if (fw->sctp != NULL) {
1018 		/* try to get next in hash chain */
1019 		if (mdb_vread(&sctp, sizeof (sctp), fw->sctp) == -1) {
1020 			mdb_warn("failed to read sctp at %p", fw->sctp);
1021 			return (NULL);
1022 		}
1023 		fw->sctp = fw->getnext(&sctp);
1024 		if (fw->sctp != NULL)
1025 			return (fw->sctp);
1026 		else
1027 			/* end of chain; go to next bucket */
1028 			fw->index++;
1029 	}
1030 
1031 	/* find a new hash chain, traversing the buckets */
1032 	for (; fw->index < fw->size; fw->index++) {
1033 		/* read the current hash line for an sctp */
1034 		if (mdb_vread(&tf, sizeof (tf),
1035 			(uintptr_t)(fw->fanout + fw->index)) == -1) {
1036 			mdb_warn("failed to read tf at %p",
1037 			    fw->fanout + fw->index);
1038 			return (NULL);
1039 		}
1040 		if (tf.tf_sctp != NULL) {
1041 			/* start of a new chain */
1042 			fw->sctp = (uintptr_t)tf.tf_sctp;
1043 			return (fw->sctp);
1044 		}
1045 	}
1046 	return (NULL);
1047 }
1048 
1049 static int
1050 fanout_walk_init(mdb_walk_state_t *wsp)
1051 {
1052 	GElf_Sym sym;
1053 	fanout_walk_data_t *lw;
1054 	fanout_init_t *fi = wsp->walk_arg;
1055 
1056 	if (mdb_lookup_by_name(fi->symname, &sym) == -1) {
1057 		mdb_warn("failed to read '%s'", fi->symname);
1058 		return (WALK_ERR);
1059 	}
1060 
1061 	lw = mdb_alloc(sizeof (*lw), UM_SLEEP);
1062 	lw->index = 0;
1063 	lw->size = fi->getsize();
1064 	lw->sctp = NULL;
1065 	lw->fanout = (sctp_tf_t *)(uintptr_t)sym.st_value;
1066 	lw->getnext = fi->getnext;
1067 
1068 	if ((wsp->walk_addr = find_next_hash_item(lw)) == NULL) {
1069 		return (WALK_DONE);
1070 	}
1071 	wsp->walk_data = lw;
1072 	return (WALK_NEXT);
1073 }
1074 
1075 static int
1076 fanout_walk_step(mdb_walk_state_t *wsp)
1077 {
1078 	fanout_walk_data_t *fw = wsp->walk_data;
1079 	uintptr_t addr = wsp->walk_addr;
1080 	sctp_t sctp;
1081 	int status;
1082 
1083 	if (mdb_vread(&sctp, sizeof (sctp), addr) == -1) {
1084 		mdb_warn("failed to read sctp at %p", addr);
1085 		return (WALK_DONE);
1086 	}
1087 
1088 	status = wsp->walk_callback(addr, &sctp, wsp->walk_cbdata);
1089 	if (status != WALK_NEXT)
1090 		return (status);
1091 
1092 	if ((wsp->walk_addr = find_next_hash_item(fw)) == NULL)
1093 		return (WALK_DONE);
1094 
1095 	return (WALK_NEXT);
1096 }
1097 
1098 static void
1099 fanout_walk_fini(mdb_walk_state_t *wsp)
1100 {
1101 	fanout_walk_data_t *fw = wsp->walk_data;
1102 
1103 	mdb_free(fw, sizeof (*fw));
1104 }
1105 
1106 static int
1107 sctp_walk_init(mdb_walk_state_t *wsp)
1108 {
1109 	wsp->walk_addr = (uintptr_t)list_object(&sctp_list,
1110 	    sctp_list.list_head.list_next);
1111 	return (WALK_NEXT);
1112 }
1113 
1114 static int
1115 sctp_walk_step(mdb_walk_state_t *wsp)
1116 {
1117 	uintptr_t psctp = wsp->walk_addr;
1118 	sctp_t sctp;
1119 	int status;
1120 
1121 	if (mdb_vread(&sctp, sizeof (sctp), psctp) == -1) {
1122 		mdb_warn("failed to read sctp at %p", psctp);
1123 		return (WALK_ERR);
1124 	}
1125 	status = wsp->walk_callback(psctp, &sctp, wsp->walk_cbdata);
1126 	if (status != WALK_NEXT)
1127 		return (status);
1128 
1129 	if ((psctp = (uintptr_t)sctp.sctp_list.list_next) ==
1130 	    sctp_list_sym.st_value + OFFSETOF(list_t, list_head)) {
1131 		return (WALK_DONE);
1132 	} else {
1133 		wsp->walk_addr = (uintptr_t)list_object(&sctp_list, psctp);
1134 		return (WALK_NEXT);
1135 	}
1136 }
1137 
1138 static int
1139 sctp_walk_faddr_init(mdb_walk_state_t *wsp)
1140 {
1141 	sctp_t sctp;
1142 
1143 	if (wsp->walk_addr == NULL)
1144 		return (WALK_ERR);
1145 
1146 	if (mdb_vread(&sctp, sizeof (sctp), wsp->walk_addr) == -1) {
1147 		mdb_warn("failed to read sctp at %p", wsp->walk_addr);
1148 		return (WALK_ERR);
1149 	}
1150 	if ((wsp->walk_addr = (uintptr_t)sctp.sctp_faddrs) != NULL)
1151 		return (WALK_NEXT);
1152 	else
1153 		return (WALK_DONE);
1154 }
1155 
1156 static int
1157 sctp_walk_faddr_step(mdb_walk_state_t *wsp)
1158 {
1159 	uintptr_t faddr_ptr = wsp->walk_addr;
1160 	sctp_faddr_t sctp_faddr;
1161 	int status;
1162 
1163 	if (mdb_vread(&sctp_faddr, sizeof (sctp_faddr_t), faddr_ptr) == -1) {
1164 		mdb_warn("failed to read sctp_faddr_t at %p", faddr_ptr);
1165 		return (WALK_ERR);
1166 	}
1167 	status = wsp->walk_callback(faddr_ptr, &sctp_faddr, wsp->walk_cbdata);
1168 	if (status != WALK_NEXT)
1169 		return (status);
1170 	if ((faddr_ptr = (uintptr_t)sctp_faddr.next) == NULL) {
1171 		return (WALK_DONE);
1172 	} else {
1173 		wsp->walk_addr = faddr_ptr;
1174 		return (WALK_NEXT);
1175 	}
1176 }
1177 
1178 /*
1179  * Helper structure for sctp_walk_saddr.  It stores the sctp_t being walked,
1180  * the current index to the sctp_saddrs[], and the current count of the
1181  * sctp_saddr_ipif_t list.
1182  */
1183 typedef struct {
1184 	sctp_t	sctp;
1185 	int	hash_index;
1186 	int	cur_cnt;
1187 } saddr_walk_t;
1188 
1189 static int
1190 sctp_walk_saddr_init(mdb_walk_state_t *wsp)
1191 {
1192 	sctp_t *sctp;
1193 	int i;
1194 	saddr_walk_t *swalker;
1195 
1196 	if (wsp->walk_addr == NULL)
1197 		return (WALK_ERR);
1198 
1199 	swalker = mdb_alloc(sizeof (saddr_walk_t), UM_SLEEP);
1200 	sctp = &swalker->sctp;
1201 	if (mdb_vread(sctp, sizeof (sctp_t), wsp->walk_addr) == -1) {
1202 		mdb_warn("failed to read sctp at %p", wsp->walk_addr);
1203 		mdb_free(swalker, sizeof (saddr_walk_t));
1204 		return (WALK_ERR);
1205 	}
1206 
1207 	/* Find the first source address. */
1208 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1209 		if (sctp->sctp_saddrs[i].ipif_count > 0) {
1210 			list_t *addr_list;
1211 
1212 			addr_list = &sctp->sctp_saddrs[i].sctp_ipif_list;
1213 			wsp->walk_addr = (uintptr_t)list_object(addr_list,
1214 			    addr_list->list_head.list_next);
1215 
1216 			/* Recode the current info */
1217 			swalker->hash_index = i;
1218 			swalker->cur_cnt = 1;
1219 			wsp->walk_data = swalker;
1220 
1221 			return (WALK_NEXT);
1222 		}
1223 	}
1224 	return (WALK_DONE);
1225 }
1226 
1227 static int
1228 sctp_walk_saddr_step(mdb_walk_state_t *wsp)
1229 {
1230 	uintptr_t saddr_ptr = wsp->walk_addr;
1231 	sctp_saddr_ipif_t saddr;
1232 	saddr_walk_t *swalker;
1233 	sctp_t *sctp;
1234 	int status;
1235 	int i, j;
1236 
1237 	if (mdb_vread(&saddr, sizeof (sctp_saddr_ipif_t), saddr_ptr) == -1) {
1238 		mdb_warn("failed to read sctp_saddr_ipif_t at %p", saddr_ptr);
1239 		return (WALK_ERR);
1240 	}
1241 	status = wsp->walk_callback(saddr_ptr, &saddr, wsp->walk_cbdata);
1242 	if (status != WALK_NEXT)
1243 		return (status);
1244 
1245 	swalker = (saddr_walk_t *)wsp->walk_data;
1246 	sctp = &swalker->sctp;
1247 	i = swalker->hash_index;
1248 	j = swalker->cur_cnt;
1249 
1250 	/*
1251 	 * If there is still a source address in the current list, return it.
1252 	 * Otherwise, go to the next list in the sctp_saddrs[].
1253 	 */
1254 	if (j++ < sctp->sctp_saddrs[i].ipif_count) {
1255 		wsp->walk_addr = (uintptr_t)saddr.saddr_ipif.list_next;
1256 		swalker->cur_cnt = j;
1257 		return (WALK_NEXT);
1258 	} else {
1259 		list_t *lst;
1260 
1261 		for (i = i + 1; i < SCTP_IPIF_HASH; i++) {
1262 			if (sctp->sctp_saddrs[i].ipif_count > 0) {
1263 				lst = &sctp->sctp_saddrs[i].sctp_ipif_list;
1264 				wsp->walk_addr = (uintptr_t)list_object(
1265 				    lst, lst->list_head.list_next);
1266 				swalker->hash_index = i;
1267 				swalker->cur_cnt = 1;
1268 				return (WALK_NEXT);
1269 			}
1270 		}
1271 	}
1272 	return (WALK_DONE);
1273 }
1274 
1275 static void
1276 sctp_walk_saddr_fini(mdb_walk_state_t *wsp)
1277 {
1278 	saddr_walk_t *swalker = (saddr_walk_t *)wsp->walk_data;
1279 
1280 	mdb_free(swalker, sizeof (saddr_walk_t));
1281 }
1282 
1283 static int
1284 sctp_walk_ill_init(mdb_walk_state_t *wsp)
1285 {
1286 	intptr_t i;
1287 
1288 	/* Find the first ill. */
1289 	for (i = 0; i < SCTP_ILL_HASH; i++) {
1290 		if (local_g_ills[i].ill_count > 0) {
1291 			wsp->walk_addr = (uintptr_t)list_object(
1292 			    &local_g_ills[i].sctp_ill_list,
1293 			    local_g_ills[i].sctp_ill_list.list_head.list_next);
1294 			wsp->walk_data = (void *)i;
1295 			wsp->walk_arg = (void *)1;
1296 			return (WALK_NEXT);
1297 		}
1298 	}
1299 	return (WALK_DONE);
1300 }
1301 
1302 static int
1303 sctp_walk_ill_step(mdb_walk_state_t *wsp)
1304 {
1305 	uintptr_t ill_ptr = wsp->walk_addr;
1306 	sctp_ill_t ill;
1307 	int status;
1308 	intptr_t i, j;
1309 
1310 	if (mdb_vread(&ill, sizeof (sctp_ill_t), ill_ptr) == -1) {
1311 		mdb_warn("failed to read sctp_ill_t at %p", ill_ptr);
1312 		return (WALK_ERR);
1313 	}
1314 	status = wsp->walk_callback(ill_ptr, &ill, wsp->walk_cbdata);
1315 	if (status != WALK_NEXT)
1316 		return (status);
1317 
1318 	i = (intptr_t)wsp->walk_data;
1319 	j = (intptr_t)wsp->walk_arg;
1320 
1321 	/*
1322 	 * If there is still an ill in the current list, return it.
1323 	 * Otherwise, go to the next list and find another one.
1324 	 */
1325 	if (j++ < local_g_ills[i].ill_count) {
1326 		wsp->walk_addr = (uintptr_t)ill.sctp_ills.list_next;
1327 		wsp->walk_data = (void *)i;
1328 		wsp->walk_arg = (void *)j;
1329 		return (WALK_NEXT);
1330 	} else {
1331 		list_t *ill_list;
1332 
1333 		for (i = i + 1; i < SCTP_ILL_HASH; i++) {
1334 			if (local_g_ills[i].ill_count > 0) {
1335 				ill_list = &local_g_ills[i].sctp_ill_list;
1336 				wsp->walk_addr = (uintptr_t)list_object(
1337 				    ill_list, ill_list->list_head.list_next);
1338 
1339 				/* Record the current position. */
1340 				wsp->walk_data = (void *)i;
1341 				wsp->walk_arg = (void *)1;
1342 				return (WALK_NEXT);
1343 			}
1344 		}
1345 	}
1346 	return (WALK_DONE);
1347 }
1348 
1349 static int
1350 sctp_walk_ipif_init(mdb_walk_state_t *wsp)
1351 {
1352 	intptr_t i;
1353 	list_t *ipif_list;
1354 
1355 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1356 		if (local_g_ipifs[i].ipif_count > 0) {
1357 			ipif_list = &local_g_ipifs[i].sctp_ipif_list;
1358 
1359 			wsp->walk_addr = (uintptr_t)list_object(ipif_list,
1360 			    ipif_list->list_head.list_next);
1361 			wsp->walk_data = (void *)i;
1362 			wsp->walk_arg = (void *)1;
1363 			return (WALK_NEXT);
1364 		}
1365 	}
1366 	return (WALK_DONE);
1367 }
1368 
1369 static int
1370 sctp_walk_ipif_step(mdb_walk_state_t *wsp)
1371 {
1372 	uintptr_t ipif_ptr = wsp->walk_addr;
1373 	sctp_ipif_t ipif;
1374 	int status;
1375 	intptr_t i, j;
1376 
1377 	if (mdb_vread(&ipif, sizeof (sctp_ipif_t), ipif_ptr) == -1) {
1378 		mdb_warn("failed to read sctp_ipif_t at %p", ipif_ptr);
1379 		return (WALK_ERR);
1380 	}
1381 	status = wsp->walk_callback(ipif_ptr, &ipif, wsp->walk_cbdata);
1382 	if (status != WALK_NEXT)
1383 		return (status);
1384 
1385 	i = (intptr_t)wsp->walk_data;
1386 	j = (intptr_t)wsp->walk_arg;
1387 
1388 	/*
1389 	 * If there is still an ipif in the current list, return it.
1390 	 * Otherwise, go to the next list and find another one.
1391 	 */
1392 	if (j++ < local_g_ipifs[i].ipif_count) {
1393 		wsp->walk_addr = (uintptr_t)ipif.sctp_ipifs.list_next;
1394 		wsp->walk_data = (void *)i;
1395 		wsp->walk_arg = (void *)j;
1396 		return (WALK_NEXT);
1397 	} else {
1398 		list_t *ipif_list;
1399 
1400 		for (i = i + 1; i < SCTP_IPIF_HASH; i++) {
1401 			if (local_g_ipifs[i].ipif_count > 0) {
1402 				ipif_list = &local_g_ipifs[i].sctp_ipif_list;
1403 				wsp->walk_addr = (uintptr_t)list_object(
1404 				    ipif_list, ipif_list->list_head.list_next);
1405 
1406 				/* Record the current position */
1407 				wsp->walk_data = (void *)i;
1408 				wsp->walk_arg = (void *)1;
1409 				return (WALK_NEXT);
1410 			}
1411 		}
1412 	}
1413 	return (WALK_DONE);
1414 }
1415 
1416 static void
1417 sctp_help(void)
1418 {
1419 	mdb_printf("Print information for a given SCTP sctp_t\n\n");
1420 	mdb_printf("Options:\n");
1421 	mdb_printf("\t-a\t All the information\n");
1422 	mdb_printf("\t-f\t Flags\n");
1423 	mdb_printf("\t-h\t Hash Tables\n");
1424 	mdb_printf("\t-o\t Outbound Data\n");
1425 	mdb_printf("\t-i\t Inbound Data\n");
1426 	mdb_printf("\t-m\t Miscellaneous Information\n");
1427 	mdb_printf("\t-r\t RTT Tracking\n");
1428 	mdb_printf("\t-S\t Stats Counters\n");
1429 	mdb_printf("\t-F\t Flow Control\n");
1430 	mdb_printf("\t-H\t Composite Headers\n");
1431 	mdb_printf("\t-p\t PMTUD\n");
1432 	mdb_printf("\t-R\t Retransmit Information\n");
1433 	mdb_printf("\t-C\t Connection State\n");
1434 	mdb_printf("\t-c\t Cleanup / Close\n");
1435 	mdb_printf("\t-e\t Extensions and Reliable Control Chunks\n");
1436 	mdb_printf("\t-d\t Local and Peer addresses\n");
1437 	mdb_printf("\t-P\t Peer addresses\n");
1438 }
1439 static const mdb_dcmd_t dcmds[] = {
1440 	{ "sctp", ":[-afhoimrSFHpRCcedP]",
1441 	    "display sctp control structure", sctp, sctp_help },
1442 	{ "sctp_set", ":", "display a SCTP set", sctp_set },
1443 	{ "sctp_faddr", ":", "display a faddr", sctp_faddr },
1444 	{ "sctp_istr_msgs", ":", "display msg list on an instream",
1445 	    sctp_istr_msgs },
1446 	{ "sctp_mdata_chunk", ":", "display a data chunk in an mblk",
1447 	    sctp_mdata_chunk },
1448 	{ "sctp_xmit_list", ":", "display sctp xmit lists", sctp_xmit_list },
1449 	{ "sctp_instr", ":", "display instr", sctp_instr },
1450 	{ "sctp_reass_list", ":", "display reass list", sctp_reass_list },
1451 	{ "sctp_uo_reass_list", ":", "display un-ordered reass list",
1452 	    sctp_uo_reass_list },
1453 	{ NULL }
1454 };
1455 
1456 static const fanout_init_t listen_fanout_init = {
1457 	"sctp_listen_fanout", listen_size, listen_next
1458 };
1459 
1460 static const fanout_init_t conn_fanout_init = {
1461 	"sctp_conn_fanout", conn_size, conn_next
1462 };
1463 
1464 static const fanout_init_t bind_fanout_init = {
1465 	"sctp_bind_fanout", bind_size, bind_next
1466 };
1467 
1468 static const mdb_walker_t walkers[] = {
1469 	{ "sctps", "walk the full chain of sctps",
1470 	    sctp_walk_init, sctp_walk_step, NULL },
1471 	{ "sctp_listen_fanout", "walk the sctp listen fanout",
1472 	    fanout_walk_init, fanout_walk_step, fanout_walk_fini,
1473 	    (void *)&listen_fanout_init },
1474 	{ "sctp_conn_fanout", "walk the sctp conn fanout",
1475 	    fanout_walk_init, fanout_walk_step, fanout_walk_fini,
1476 	    (void *)&conn_fanout_init },
1477 	{ "sctp_bind_fanout", "walk the sctp bind fanout",
1478 	    fanout_walk_init, fanout_walk_step, fanout_walk_fini,
1479 	    (void *)&bind_fanout_init },
1480 	{ "sctp_walk_faddr", "walk the peer address list of a given sctp_t",
1481 	    sctp_walk_faddr_init, sctp_walk_faddr_step, NULL },
1482 	{ "sctp_walk_saddr", "walk the local address list of a given sctp_t",
1483 	    sctp_walk_saddr_init, sctp_walk_saddr_step, sctp_walk_saddr_fini },
1484 	{ "sctp_walk_ill", "walk the sctp_g_ills list",
1485 	    sctp_walk_ill_init, sctp_walk_ill_step, NULL },
1486 	{ "sctp_walk_ipif", "walk the sctp_g_ipif list",
1487 	    sctp_walk_ipif_init, sctp_walk_ipif_step, NULL },
1488 	{ NULL }
1489 };
1490 
1491 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
1492 
1493 const mdb_modinfo_t *
1494 _mdb_init(void)
1495 {
1496 	GElf_Sym sym;
1497 	GElf_Sym ills_sym;
1498 	GElf_Sym ipifs_sym;
1499 
1500 	if (mdb_lookup_by_name("sctp_g_list", &sctp_list_sym) == -1) {
1501 		mdb_warn("failed to find 'sctp_g_list'");
1502 		return (NULL);
1503 	}
1504 	if (mdb_vread(&sctp_list, sizeof (list_t),
1505 	    (uintptr_t)sctp_list_sym.st_value) == -1) {
1506 		mdb_warn("failed to read 'sctp_g_list'");
1507 		return (NULL);
1508 	}
1509 	if (mdb_lookup_by_name("sctp_conn_hash_size", &sym) != -1) {
1510 		if (mdb_vread(&sctp_conn_hash_size,
1511 		    sizeof (sctp_conn_hash_size), sym.st_value) == -1) {
1512 			mdb_warn("failed to read 'sctp_conn_hash_size'");
1513 			return (NULL);
1514 		}
1515 	}
1516 	if (mdb_lookup_by_name("sctp_g_ills", &ills_sym) == -1) {
1517 		mdb_warn("failed to find 'sctp_g_ills'");
1518 		return (NULL);
1519 	}
1520 	if (mdb_vread(&local_g_ills, sizeof (local_g_ills),
1521 	    (uintptr_t)ills_sym.st_value) == -1) {
1522 		mdb_warn("failed to read 'sctp_g_ills'");
1523 		return (NULL);
1524 	}
1525 	if (mdb_lookup_by_name("sctp_g_ipifs", &ipifs_sym) == -1) {
1526 		mdb_warn("failed to find 'sctp_g_ipifs'");
1527 		return (NULL);
1528 	}
1529 	if (mdb_vread(&local_g_ipifs, sizeof (local_g_ipifs),
1530 	    (uintptr_t)ipifs_sym.st_value) == -1) {
1531 		mdb_warn("failed to read 'sctp_g_ipifs'");
1532 		return (NULL);
1533 	}
1534 
1535 	return (&modinfo);
1536 }
1537