xref: /illumos-gate/usr/src/cmd/mdb/common/modules/sctp/sctp.c (revision cbab2b2687744cbfdc12fae90f8088127a0b266c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 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 
573 	mdb_printf("\tprsctp_aware\t\t%d\n", sctp->sctp_prsctp_aware);
574 	mdb_printf("\tlinklocal\t\t%d\n", sctp->sctp_linklocal);
575 	mdb_printf("\tmac_exempt\t\t%d\n", sctp->sctp_mac_exempt);
576 	mdb_printf("\trexmitting\t\t%d\n", sctp->sctp_rexmitting);
577 
578 	mdb_printf("\trecvsndrcvinfo\t\t%d\n", sctp->sctp_recvsndrcvinfo);
579 	mdb_printf("\trecvassocevnt\t\t%d\n", sctp->sctp_recvassocevnt);
580 	mdb_printf("\trecvpathevnt\t\t%d\n", sctp->sctp_recvpathevnt);
581 	mdb_printf("\trecvsendfailevnt\t%d\n", sctp->sctp_recvsendfailevnt);
582 
583 	mdb_printf("\trecvpeerevnt\t\t%d\n", sctp->sctp_recvpeererr);
584 	mdb_printf("\trecvchutdownevnt\t%d\n", sctp->sctp_recvshutdownevnt);
585 	mdb_printf("\trecvcpdnevnt\t\t%d\n", sctp->sctp_recvpdevnt);
586 	mdb_printf("\trecvcalevnt\t\t%d\n\n", sctp->sctp_recvalevnt);
587 }
588 
589 /*
590  * Given a sctp_saddr_ipif_t, print out its address.  This assumes
591  * that addr contains the sctp_addr_ipif_t structure already and this
592  * function does not need to read it in.
593  */
594 /* ARGSUSED */
595 static int
596 print_saddr(uintptr_t ptr, const void *addr, void *cbdata)
597 {
598 	sctp_saddr_ipif_t *saddr = (sctp_saddr_ipif_t *)addr;
599 	sctp_ipif_t ipif;
600 	char *statestr;
601 
602 	/* Read in the sctp_ipif object */
603 	if (mdb_vread(&ipif, sizeof (ipif), (uintptr_t)saddr->saddr_ipifp) ==
604 	    -1) {
605 		mdb_warn("cannot read ipif at %p", saddr->saddr_ipifp);
606 		return (WALK_ERR);
607 	}
608 
609 	switch (ipif.sctp_ipif_state) {
610 	case SCTP_IPIFS_CONDEMNED:
611 		statestr = "Condemned";
612 		break;
613 	case SCTP_IPIFS_INVALID:
614 		statestr = "Invalid";
615 		break;
616 	case SCTP_IPIFS_DOWN:
617 		statestr = "Down";
618 		break;
619 	case SCTP_IPIFS_UP:
620 		statestr = "Up";
621 		break;
622 	default:
623 		statestr = "Unknown";
624 		break;
625 	}
626 	mdb_printf("\t%p\t%N% (%s", saddr->saddr_ipifp, &ipif.sctp_ipif_saddr,
627 	    statestr);
628 	if (saddr->saddr_ipif_dontsrc == 1)
629 		mdb_printf("/Dontsrc");
630 	if (saddr->saddr_ipif_unconfirmed == 1)
631 		mdb_printf("/Unconfirmed");
632 	if (saddr->saddr_ipif_delete_pending == 1)
633 		mdb_printf("/DeletePending");
634 	mdb_printf(")\n");
635 	mdb_printf("\t\t\tMTU %d id %d zoneid %d IPIF flags %x\n",
636 	    ipif.sctp_ipif_mtu, ipif.sctp_ipif_id,
637 	    ipif.sctp_ipif_zoneid, ipif.sctp_ipif_flags);
638 	return (WALK_NEXT);
639 }
640 
641 /*
642  * Given a sctp_faddr_t, print out its address.  This assumes that
643  * addr contains the sctp_faddr_t structure already and this function
644  * does not need to read it in.
645  */
646 static int
647 print_faddr(uintptr_t ptr, const void *addr, void *cbdata)
648 {
649 	char	*statestr;
650 	sctp_faddr_t *faddr = (sctp_faddr_t *)addr;
651 	int *i = cbdata;
652 
653 	statestr = sctp_faddr_state(faddr->state);
654 
655 	mdb_printf("\t%d:\t%N\t%?p (%s)\n", (*i)++, &faddr->faddr, ptr,
656 	    statestr);
657 	return (WALK_NEXT);
658 }
659 
660 int
661 sctp(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
662 {
663 	sctp_t sctp;
664 	conn_t connp;
665 	int i;
666 	uint_t opts = 0;
667 	uint_t paddr = 0;
668 	in_port_t lport, fport;
669 
670 	if (!(flags & DCMD_ADDRSPEC))
671 		return (DCMD_USAGE);
672 
673 	if (mdb_vread(&sctp, sizeof (sctp), addr) == -1) {
674 		mdb_warn("failed to read sctp_t at: %p\n", addr);
675 		return (DCMD_ERR);
676 	}
677 	if (mdb_vread(&connp, sizeof (connp),
678 	    (uintptr_t)sctp.sctp_connp) == -1) {
679 		mdb_warn("failed to read conn_t at: %p\n", sctp.sctp_connp);
680 		return (DCMD_ERR);
681 	}
682 
683 	if (mdb_getopts(argc, argv,
684 		'a', MDB_OPT_SETBITS, MDB_SCTP_SHOW_ALL, &opts,
685 		'f', MDB_OPT_SETBITS, MDB_SCTP_SHOW_FLAGS, &opts,
686 		'h', MDB_OPT_SETBITS, MDB_SCTP_SHOW_HASH, &opts,
687 		'o', MDB_OPT_SETBITS, MDB_SCTP_SHOW_OUT, &opts,
688 		'i', MDB_OPT_SETBITS, MDB_SCTP_SHOW_IN, &opts,
689 		'm', MDB_OPT_SETBITS, MDB_SCTP_SHOW_MISC, &opts,
690 		'r', MDB_OPT_SETBITS, MDB_SCTP_SHOW_RTT, &opts,
691 		'S', MDB_OPT_SETBITS, MDB_SCTP_SHOW_STATS, &opts,
692 		'F', MDB_OPT_SETBITS, MDB_SCTP_SHOW_FLOW, &opts,
693 		'H', MDB_OPT_SETBITS, MDB_SCTP_SHOW_HDR, &opts,
694 		'p', MDB_OPT_SETBITS, MDB_SCTP_SHOW_PMTUD, &opts,
695 		'R', MDB_OPT_SETBITS, MDB_SCTP_SHOW_RXT, &opts,
696 		'C', MDB_OPT_SETBITS, MDB_SCTP_SHOW_CONN, &opts,
697 		'c', MDB_OPT_SETBITS, MDB_SCTP_SHOW_CLOSE, &opts,
698 		'e', MDB_OPT_SETBITS, MDB_SCTP_SHOW_EXT, &opts,
699 		'P', MDB_OPT_SETBITS, 1, &paddr,
700 		'd', MDB_OPT_SETBITS, MDB_SCTP_DUMP_ADDRS, &opts) != argc) {
701 		return (DCMD_USAGE);
702 	}
703 
704 	/* non-verbose faddrs, suitable for pipelines to sctp_faddr */
705 	if (paddr != 0) {
706 		sctp_faddr_t faddr, *fp;
707 		for (fp = sctp.sctp_faddrs; fp != NULL; fp = faddr.next) {
708 			if (mdb_vread(&faddr, sizeof (faddr), (uintptr_t)fp)
709 			    == -1) {
710 				mdb_warn("failed to read faddr at %p",
711 				    fp);
712 				return (DCMD_ERR);
713 			}
714 			mdb_printf("%p\n", fp);
715 		}
716 		return (DCMD_OK);
717 	}
718 
719 	mdb_nhconvert(&lport, &sctp.sctp_lport, sizeof (lport));
720 	mdb_nhconvert(&fport, &sctp.sctp_fport, sizeof (fport));
721 	mdb_printf("%<u>%p% %22s S=%-6hu D=%-6hu% ZONE=%d%</u>", addr,
722 	    state2str(&sctp), lport, fport, connp.conn_zoneid);
723 
724 	if (sctp.sctp_faddrs) {
725 		sctp_faddr_t faddr;
726 		if (mdb_vread(&faddr, sizeof (faddr),
727 		    (uintptr_t)sctp.sctp_faddrs) != -1)
728 			mdb_printf("%<u> %N%</u>", &faddr.faddr);
729 	}
730 	mdb_printf("\n");
731 
732 	if (opts & MDB_SCTP_DUMP_ADDRS) {
733 		mdb_printf("%<b>Local and Peer Addresses%</b>\n");
734 
735 		/* Display source addresses */
736 		mdb_printf("nsaddrs\t\t%?d\n", sctp.sctp_nsaddrs);
737 		(void) mdb_pwalk("sctp_walk_saddr", print_saddr, NULL, addr);
738 
739 		/* Display peer addresses */
740 		mdb_printf("nfaddrs\t\t%?d\n", sctp.sctp_nfaddrs);
741 		i = 1;
742 		(void) mdb_pwalk("sctp_walk_faddr", print_faddr, &i, addr);
743 
744 		mdb_printf("lastfaddr\t%?p\tprimary\t\t%?p\n",
745 		    sctp.sctp_lastfaddr, sctp.sctp_primary);
746 		mdb_printf("current\t\t%?p\tlastdata\t%?p\n",
747 		    sctp.sctp_current, sctp.sctp_lastdata);
748 	}
749 
750 	if (opts & MDB_SCTP_SHOW_OUT) {
751 		mdb_printf("%<b>Outbound Data%</b>\n");
752 		mdb_printf("xmit_head\t%?p\txmit_tail\t%?p\n",
753 		    sctp.sctp_xmit_head, sctp.sctp_xmit_tail);
754 		mdb_printf("xmit_unsent\t%?p\txmit_unsent_tail%?p\n",
755 		    sctp.sctp_xmit_unsent, sctp.sctp_xmit_unsent_tail);
756 		mdb_printf("xmit_unacked\t%?p\n", sctp.sctp_xmit_unacked);
757 		mdb_printf("unacked\t\t%?u\tunsent\t\t%?ld\n",
758 		    sctp.sctp_unacked, sctp.sctp_unsent);
759 		mdb_printf("ltsn\t\t%?x\tlastack_rxd\t%?x\n",
760 		    sctp.sctp_ltsn, sctp.sctp_lastack_rxd);
761 		mdb_printf("recovery_tsn\t%?x\tadv_pap\t\t%?x\n",
762 		    sctp.sctp_recovery_tsn, sctp.sctp_adv_pap);
763 		mdb_printf("num_ostr\t%?hu\tostrcntrs\t%?p\n",
764 		    sctp.sctp_num_ostr, sctp.sctp_ostrcntrs);
765 
766 		mdb_printf("%<b>Default Send Parameters%</b>\n");
767 		mdb_printf("def_stream\t%?u\tdef_flags\t%?x\n",
768 		    sctp.sctp_def_stream, sctp.sctp_def_flags);
769 		mdb_printf("def_ppid\t%?x\tdef_context\t%?x\n",
770 		    sctp.sctp_def_ppid, sctp.sctp_def_context);
771 		mdb_printf("def_timetolive\t%?u\n",
772 		    sctp.sctp_def_timetolive);
773 	}
774 
775 	if (opts & MDB_SCTP_SHOW_IN) {
776 		mdb_printf("%<b>Inbound Data%</b>\n");
777 		mdb_printf("sack_info\t%?p\tsack_gaps\t%?d\n",
778 		    sctp.sctp_sack_info, sctp.sctp_sack_gaps);
779 		dump_sack_info((uintptr_t)sctp.sctp_sack_info);
780 		mdb_printf("ftsn\t\t%?x\tlastacked\t%?x\n",
781 		    sctp.sctp_ftsn, sctp.sctp_lastacked);
782 		mdb_printf("istr_nmsgs\t%?d\tsack_toggle\t%?d\n",
783 		    sctp.sctp_istr_nmsgs, sctp.sctp_sack_toggle);
784 		mdb_printf("ack_mp\t\t%?p\n", sctp.sctp_ack_mp);
785 		mdb_printf("num_istr\t%?hu\tinstr\t\t%?p\n",
786 		    sctp.sctp_num_istr, sctp.sctp_instr);
787 		mdb_printf("unord_reass\t%?p\n", sctp.sctp_uo_frags);
788 	}
789 
790 	if (opts & MDB_SCTP_SHOW_RTT) {
791 		mdb_printf("%<b>RTT Tracking%</b>\n");
792 		mdb_printf("rtt_tsn\t\t%?x\tout_time\t%?ld\n",
793 		    sctp.sctp_rtt_tsn, sctp.sctp_out_time);
794 	}
795 
796 	if (opts & MDB_SCTP_SHOW_FLOW) {
797 		mdb_printf("%<b>Flow Control%</b>\n");
798 		mdb_printf("txmit_hiwater\t%?d\n"
799 		    "xmit_lowater\t%?d\tfrwnd\t\t%?u\n"
800 		    "rwnd\t\t%?u\tinitial rwnd\t%?u\n"
801 		    "rxqueued\t%?u\tcwnd_max\t%?u\n", sctp.sctp_xmit_hiwater,
802 		    sctp.sctp_xmit_lowater, sctp.sctp_frwnd,
803 		    sctp.sctp_rwnd, sctp.sctp_irwnd, sctp.sctp_rxqueued,
804 		    sctp.sctp_cwnd_max);
805 	}
806 
807 	if (opts & MDB_SCTP_SHOW_HDR) {
808 		mdb_printf("%<b>Composite Headers%</b>\n");
809 		mdb_printf("iphc\t\t%?p\tiphc6\t\t%?p\n"
810 		    "iphc_len\t%?d\tiphc6_len\t%?d\n"
811 		    "hdr_len\t\t%?d\thdr6_len\t%?d\n"
812 		    "ipha\t\t%?p\tip6h\t\t%?p\n"
813 		    "ip_hdr_len\t%?d\tip_hdr6_len\t%?d\n"
814 		    "sctph\t\t%?p\tsctph6\t\t%?p\n"
815 		    "lvtag\t\t%?x\tfvtag\t\t%?x\n", sctp.sctp_iphc,
816 		    sctp.sctp_iphc6, sctp.sctp_iphc_len,
817 		    sctp.sctp_iphc6_len, sctp.sctp_hdr_len,
818 		    sctp.sctp_hdr6_len, sctp.sctp_ipha, sctp.sctp_ip6h,
819 		    sctp.sctp_ip_hdr_len, sctp.sctp_ip_hdr6_len,
820 		    sctp.sctp_sctph, sctp.sctp_sctph6, sctp.sctp_lvtag,
821 		    sctp.sctp_fvtag);
822 	}
823 
824 	if (opts & MDB_SCTP_SHOW_PMTUD) {
825 		mdb_printf("%<b>PMTUd%</b>\n");
826 		mdb_printf("last_mtu_probe\t%?ld\tmtu_probe_intvl\t%?ld\n"
827 		    "mss\t\t%?u\n",
828 		    sctp.sctp_last_mtu_probe, sctp.sctp_mtu_probe_intvl,
829 		    sctp.sctp_mss);
830 	}
831 
832 	if (opts & MDB_SCTP_SHOW_RXT) {
833 		mdb_printf("%<b>Retransmit Info%</b>\n");
834 		mdb_printf("cookie_mp\t%?p\tstrikes\t\t%?d\n"
835 		    "max_init_rxt\t%?d\tpa_max_rxt\t%?d\n"
836 		    "pp_max_rxt\t%?d\trto_max\t\t%?u\n"
837 		    "rto_min\t\t%?u\trto_initial\t%?u\n"
838 		    "init_rto_max\t%?u\n"
839 		    "rxt_nxttsn\t%?u\trxt_maxtsn\t%?u\n", sctp.sctp_cookie_mp,
840 		    sctp.sctp_strikes, sctp.sctp_max_init_rxt,
841 		    sctp.sctp_pa_max_rxt, sctp.sctp_pp_max_rxt,
842 		    sctp.sctp_rto_max, sctp.sctp_rto_min,
843 		    sctp.sctp_rto_initial, sctp.sctp_init_rto_max,
844 		    sctp.sctp_rxt_nxttsn, sctp.sctp_rxt_maxtsn);
845 	}
846 
847 	if (opts & MDB_SCTP_SHOW_CONN) {
848 		mdb_printf("%<b>Connection State%</b>\n");
849 		mdb_printf("last_secret_update%?ld\n",
850 		    sctp.sctp_last_secret_update);
851 
852 		mdb_printf("secret\t\t");
853 		for (i = 0; i < SCTP_SECRET_LEN; i++) {
854 			if (i % 2 == 0)
855 				mdb_printf("0x%02x", sctp.sctp_secret[i]);
856 			else
857 				mdb_printf("%02x ", sctp.sctp_secret[i]);
858 		}
859 		mdb_printf("\n");
860 		mdb_printf("old_secret\t");
861 		for (i = 0; i < SCTP_SECRET_LEN; i++) {
862 			if (i % 2 == 0)
863 				mdb_printf("0x%02x", sctp.sctp_old_secret[i]);
864 			else
865 				mdb_printf("%02x ", sctp.sctp_old_secret[i]);
866 		}
867 		mdb_printf("\n");
868 	}
869 
870 	if (opts & MDB_SCTP_SHOW_STATS) {
871 		mdb_printf("%<b>Stats Counters%</b>\n");
872 		mdb_printf("opkts\t\t%?llu\tobchunks\t%?llu\n"
873 		    "odchunks\t%?llu\toudchunks\t%?llu\n"
874 		    "rxtchunks\t%?llu\tT1expire\t%?lu\n"
875 		    "T2expire\t%?lu\tT3expire\t%?lu\n"
876 		    "msgcount\t%?llu\tprsctpdrop\t%?llu\n"
877 		    "AssocStartTime\t%?lu\n",
878 		    sctp.sctp_opkts, sctp.sctp_obchunks,
879 		    sctp.sctp_odchunks, sctp.sctp_oudchunks,
880 		    sctp.sctp_rxtchunks, sctp.sctp_T1expire,
881 		    sctp.sctp_T2expire, sctp.sctp_T3expire,
882 		    sctp.sctp_msgcount, sctp.sctp_prsctpdrop,
883 		    sctp.sctp_assoc_start_time);
884 		mdb_printf("ipkts\t\t%?llu\tibchunks\t%?llu\n"
885 		    "idchunks\t%?llu\tiudchunks\t%?llu\n"
886 		    "fragdmsgs\t%?llu\treassmsgs\t%?llu\n",
887 		    sctp.sctp_ipkts, sctp.sctp_ibchunks,
888 		    sctp.sctp_idchunks, sctp.sctp_iudchunks,
889 		    sctp.sctp_fragdmsgs, sctp.sctp_reassmsgs);
890 	}
891 
892 	if (opts & MDB_SCTP_SHOW_HASH) {
893 		mdb_printf("%<b>Hash Tables%</b>\n");
894 		mdb_printf("conn_hash_next\t%?p\t", sctp.sctp_conn_hash_next);
895 		mdb_printf("conn_hash_prev\t%?p\n", sctp.sctp_conn_hash_prev);
896 		mdb_printf("[ conn_hash bucket\t%?d ]\n",
897 		    SCTP_CONN_HASH(sctp.sctp_ports));
898 
899 		mdb_printf("listen_hash_next%?p\t",
900 		    sctp.sctp_listen_hash_next);
901 		mdb_printf("listen_hash_prev%?p\n",
902 		    sctp.sctp_listen_hash_prev);
903 		mdb_nhconvert(&lport, &sctp.sctp_lport, sizeof (lport));
904 		mdb_printf("[ listen_hash bucket\t%?d ]\n",
905 		    SCTP_LISTEN_HASH(lport));
906 
907 		mdb_printf("conn_tfp\t%?p\t", sctp.sctp_conn_tfp);
908 		mdb_printf("listen_tfp\t%?p\n", sctp.sctp_listen_tfp);
909 
910 		mdb_printf("bind_hash\t%?p\tptpbhn\t\t%?p\n",
911 		    sctp.sctp_bind_hash, sctp.sctp_ptpbhn);
912 		mdb_printf("bind_lockp\t%?p\n",
913 		    sctp.sctp_bind_lockp);
914 		mdb_printf("[ bind_hash bucket\t%?d ]\n",
915 		    SCTP_BIND_HASH(lport));
916 	}
917 
918 	if (opts & MDB_SCTP_SHOW_CLOSE) {
919 		mdb_printf("%<b>Cleanup / Close%</b>\n");
920 		mdb_printf("shutdown_faddr\t%?p\tclient_errno\t%?d\n"
921 		    "lingertime\t%?d\trefcnt\t\t%?hu\n",
922 		    sctp.sctp_shutdown_faddr, sctp.sctp_client_errno,
923 		    sctp.sctp_lingertime, sctp.sctp_refcnt);
924 	}
925 
926 	if (opts & MDB_SCTP_SHOW_MISC) {
927 		mdb_printf("%<b>Miscellaneous%</b>\n");
928 		mdb_printf("bound_if\t%?u\theartbeat_mp\t%?p\n"
929 		    "family\t\t%?u\tipversion\t%?hu\n"
930 		    "hb_interval\t%?u\tautoclose\t%?d\n"
931 		    "active\t\t%?ld\ttx_adaption_code%?x\n"
932 		    "rx_adaption_code%?x\ttimer_mp\t%?p\n",
933 		    sctp.sctp_bound_if, sctp.sctp_heartbeat_mp,
934 		    sctp.sctp_family, sctp.sctp_ipversion,
935 		    sctp.sctp_hb_interval, sctp.sctp_autoclose,
936 		    sctp.sctp_active, sctp.sctp_tx_adaption_code,
937 		    sctp.sctp_rx_adaption_code, sctp.sctp_timer_mp);
938 	}
939 
940 	if (opts & MDB_SCTP_SHOW_EXT) {
941 		mdb_printf("%<b>Extensions and Reliable Ctl Chunks%</b>\n");
942 		mdb_printf("cxmit_list\t%?p\tlcsn\t\t%?x\n"
943 		    "fcsn\t\t%?x\n", sctp.sctp_cxmit_list, sctp.sctp_lcsn,
944 		    sctp.sctp_fcsn);
945 	}
946 
947 	if (opts & MDB_SCTP_SHOW_FLAGS) {
948 		mdb_printf("%<b>Flags%</b>\n");
949 		show_sctp_flags(&sctp);
950 	}
951 
952 	return (DCMD_OK);
953 }
954 
955 typedef struct fanout_walk_data {
956 	int index;
957 	int size;
958 	uintptr_t sctp;
959 	sctp_tf_t *fanout;
960 	uintptr_t (*getnext)(sctp_t *);
961 } fanout_walk_data_t;
962 
963 typedef struct fanout_init {
964 	const char *symname;
965 	int (*getsize)();
966 	uintptr_t (*getnext)(sctp_t *);
967 } fanout_init_t;
968 
969 static uintptr_t
970 listen_next(sctp_t *sctp)
971 {
972 	return ((uintptr_t)sctp->sctp_listen_hash_next);
973 }
974 
975 static int
976 listen_size(void)
977 {
978 	return (SCTP_LISTEN_FANOUT_SIZE);
979 }
980 
981 static uintptr_t
982 conn_next(sctp_t *sctp)
983 {
984 	return ((uintptr_t)sctp->sctp_conn_hash_next);
985 }
986 
987 static int
988 conn_size(void)
989 {
990 	GElf_Sym sym;
991 	int size;
992 
993 	if (mdb_lookup_by_name("sctp_conn_hash_size", &sym) == -1) {
994 		mdb_warn("can't read 'sctp_conn_hash_size'");
995 		return (1);
996 	}
997 	if (mdb_vread(&size, sizeof (size), sym.st_value) == -1) {
998 		mdb_warn("can't dereference 'sctp_conn_hash_size'");
999 		return (1);
1000 	}
1001 	return (size);
1002 }
1003 
1004 static uintptr_t
1005 bind_next(sctp_t *sctp)
1006 {
1007 	return ((uintptr_t)sctp->sctp_bind_hash);
1008 }
1009 
1010 static int
1011 bind_size(void)
1012 {
1013 	return (SCTP_BIND_FANOUT_SIZE);
1014 }
1015 
1016 static intptr_t
1017 find_next_hash_item(fanout_walk_data_t *fw)
1018 {
1019 	sctp_tf_t tf;
1020 	sctp_t sctp;
1021 
1022 	/* first try to continue down the hash chain */
1023 	if (fw->sctp != NULL) {
1024 		/* try to get next in hash chain */
1025 		if (mdb_vread(&sctp, sizeof (sctp), fw->sctp) == -1) {
1026 			mdb_warn("failed to read sctp at %p", fw->sctp);
1027 			return (NULL);
1028 		}
1029 		fw->sctp = fw->getnext(&sctp);
1030 		if (fw->sctp != NULL)
1031 			return (fw->sctp);
1032 		else
1033 			/* end of chain; go to next bucket */
1034 			fw->index++;
1035 	}
1036 
1037 	/* find a new hash chain, traversing the buckets */
1038 	for (; fw->index < fw->size; fw->index++) {
1039 		/* read the current hash line for an sctp */
1040 		if (mdb_vread(&tf, sizeof (tf),
1041 			(uintptr_t)(fw->fanout + fw->index)) == -1) {
1042 			mdb_warn("failed to read tf at %p",
1043 			    fw->fanout + fw->index);
1044 			return (NULL);
1045 		}
1046 		if (tf.tf_sctp != NULL) {
1047 			/* start of a new chain */
1048 			fw->sctp = (uintptr_t)tf.tf_sctp;
1049 			return (fw->sctp);
1050 		}
1051 	}
1052 	return (NULL);
1053 }
1054 
1055 static int
1056 fanout_walk_init(mdb_walk_state_t *wsp)
1057 {
1058 	GElf_Sym sym;
1059 	fanout_walk_data_t *lw;
1060 	fanout_init_t *fi = wsp->walk_arg;
1061 
1062 	if (mdb_lookup_by_name(fi->symname, &sym) == -1) {
1063 		mdb_warn("failed to read '%s'", fi->symname);
1064 		return (WALK_ERR);
1065 	}
1066 
1067 	lw = mdb_alloc(sizeof (*lw), UM_SLEEP);
1068 	lw->index = 0;
1069 	lw->size = fi->getsize();
1070 	lw->sctp = NULL;
1071 	lw->fanout = (sctp_tf_t *)(uintptr_t)sym.st_value;
1072 	lw->getnext = fi->getnext;
1073 
1074 	if ((wsp->walk_addr = find_next_hash_item(lw)) == NULL) {
1075 		return (WALK_DONE);
1076 	}
1077 	wsp->walk_data = lw;
1078 	return (WALK_NEXT);
1079 }
1080 
1081 static int
1082 fanout_walk_step(mdb_walk_state_t *wsp)
1083 {
1084 	fanout_walk_data_t *fw = wsp->walk_data;
1085 	uintptr_t addr = wsp->walk_addr;
1086 	sctp_t sctp;
1087 	int status;
1088 
1089 	if (mdb_vread(&sctp, sizeof (sctp), addr) == -1) {
1090 		mdb_warn("failed to read sctp at %p", addr);
1091 		return (WALK_DONE);
1092 	}
1093 
1094 	status = wsp->walk_callback(addr, &sctp, wsp->walk_cbdata);
1095 	if (status != WALK_NEXT)
1096 		return (status);
1097 
1098 	if ((wsp->walk_addr = find_next_hash_item(fw)) == NULL)
1099 		return (WALK_DONE);
1100 
1101 	return (WALK_NEXT);
1102 }
1103 
1104 static void
1105 fanout_walk_fini(mdb_walk_state_t *wsp)
1106 {
1107 	fanout_walk_data_t *fw = wsp->walk_data;
1108 
1109 	mdb_free(fw, sizeof (*fw));
1110 }
1111 
1112 static int
1113 sctp_walk_init(mdb_walk_state_t *wsp)
1114 {
1115 	wsp->walk_addr = (uintptr_t)list_object(&sctp_list,
1116 	    sctp_list.list_head.list_next);
1117 	return (WALK_NEXT);
1118 }
1119 
1120 static int
1121 sctp_walk_step(mdb_walk_state_t *wsp)
1122 {
1123 	uintptr_t psctp = wsp->walk_addr;
1124 	sctp_t sctp;
1125 	int status;
1126 
1127 	if (mdb_vread(&sctp, sizeof (sctp), psctp) == -1) {
1128 		mdb_warn("failed to read sctp at %p", psctp);
1129 		return (WALK_ERR);
1130 	}
1131 	status = wsp->walk_callback(psctp, &sctp, wsp->walk_cbdata);
1132 	if (status != WALK_NEXT)
1133 		return (status);
1134 
1135 	if ((psctp = (uintptr_t)sctp.sctp_list.list_next) ==
1136 	    sctp_list_sym.st_value + OFFSETOF(list_t, list_head)) {
1137 		return (WALK_DONE);
1138 	} else {
1139 		wsp->walk_addr = (uintptr_t)list_object(&sctp_list, psctp);
1140 		return (WALK_NEXT);
1141 	}
1142 }
1143 
1144 static int
1145 sctp_walk_faddr_init(mdb_walk_state_t *wsp)
1146 {
1147 	sctp_t sctp;
1148 
1149 	if (wsp->walk_addr == NULL)
1150 		return (WALK_ERR);
1151 
1152 	if (mdb_vread(&sctp, sizeof (sctp), wsp->walk_addr) == -1) {
1153 		mdb_warn("failed to read sctp at %p", wsp->walk_addr);
1154 		return (WALK_ERR);
1155 	}
1156 	if ((wsp->walk_addr = (uintptr_t)sctp.sctp_faddrs) != NULL)
1157 		return (WALK_NEXT);
1158 	else
1159 		return (WALK_DONE);
1160 }
1161 
1162 static int
1163 sctp_walk_faddr_step(mdb_walk_state_t *wsp)
1164 {
1165 	uintptr_t faddr_ptr = wsp->walk_addr;
1166 	sctp_faddr_t sctp_faddr;
1167 	int status;
1168 
1169 	if (mdb_vread(&sctp_faddr, sizeof (sctp_faddr_t), faddr_ptr) == -1) {
1170 		mdb_warn("failed to read sctp_faddr_t at %p", faddr_ptr);
1171 		return (WALK_ERR);
1172 	}
1173 	status = wsp->walk_callback(faddr_ptr, &sctp_faddr, wsp->walk_cbdata);
1174 	if (status != WALK_NEXT)
1175 		return (status);
1176 	if ((faddr_ptr = (uintptr_t)sctp_faddr.next) == NULL) {
1177 		return (WALK_DONE);
1178 	} else {
1179 		wsp->walk_addr = faddr_ptr;
1180 		return (WALK_NEXT);
1181 	}
1182 }
1183 
1184 /*
1185  * Helper structure for sctp_walk_saddr.  It stores the sctp_t being walked,
1186  * the current index to the sctp_saddrs[], and the current count of the
1187  * sctp_saddr_ipif_t list.
1188  */
1189 typedef struct {
1190 	sctp_t	sctp;
1191 	int	hash_index;
1192 	int	cur_cnt;
1193 } saddr_walk_t;
1194 
1195 static int
1196 sctp_walk_saddr_init(mdb_walk_state_t *wsp)
1197 {
1198 	sctp_t *sctp;
1199 	int i;
1200 	saddr_walk_t *swalker;
1201 
1202 	if (wsp->walk_addr == NULL)
1203 		return (WALK_ERR);
1204 
1205 	swalker = mdb_alloc(sizeof (saddr_walk_t), UM_SLEEP);
1206 	sctp = &swalker->sctp;
1207 	if (mdb_vread(sctp, sizeof (sctp_t), wsp->walk_addr) == -1) {
1208 		mdb_warn("failed to read sctp at %p", wsp->walk_addr);
1209 		mdb_free(swalker, sizeof (saddr_walk_t));
1210 		return (WALK_ERR);
1211 	}
1212 
1213 	/* Find the first source address. */
1214 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1215 		if (sctp->sctp_saddrs[i].ipif_count > 0) {
1216 			list_t *addr_list;
1217 
1218 			addr_list = &sctp->sctp_saddrs[i].sctp_ipif_list;
1219 			wsp->walk_addr = (uintptr_t)list_object(addr_list,
1220 			    addr_list->list_head.list_next);
1221 
1222 			/* Recode the current info */
1223 			swalker->hash_index = i;
1224 			swalker->cur_cnt = 1;
1225 			wsp->walk_data = swalker;
1226 
1227 			return (WALK_NEXT);
1228 		}
1229 	}
1230 	return (WALK_DONE);
1231 }
1232 
1233 static int
1234 sctp_walk_saddr_step(mdb_walk_state_t *wsp)
1235 {
1236 	uintptr_t saddr_ptr = wsp->walk_addr;
1237 	sctp_saddr_ipif_t saddr;
1238 	saddr_walk_t *swalker;
1239 	sctp_t *sctp;
1240 	int status;
1241 	int i, j;
1242 
1243 	if (mdb_vread(&saddr, sizeof (sctp_saddr_ipif_t), saddr_ptr) == -1) {
1244 		mdb_warn("failed to read sctp_saddr_ipif_t at %p", saddr_ptr);
1245 		return (WALK_ERR);
1246 	}
1247 	status = wsp->walk_callback(saddr_ptr, &saddr, wsp->walk_cbdata);
1248 	if (status != WALK_NEXT)
1249 		return (status);
1250 
1251 	swalker = (saddr_walk_t *)wsp->walk_data;
1252 	sctp = &swalker->sctp;
1253 	i = swalker->hash_index;
1254 	j = swalker->cur_cnt;
1255 
1256 	/*
1257 	 * If there is still a source address in the current list, return it.
1258 	 * Otherwise, go to the next list in the sctp_saddrs[].
1259 	 */
1260 	if (j++ < sctp->sctp_saddrs[i].ipif_count) {
1261 		wsp->walk_addr = (uintptr_t)saddr.saddr_ipif.list_next;
1262 		swalker->cur_cnt = j;
1263 		return (WALK_NEXT);
1264 	} else {
1265 		list_t *lst;
1266 
1267 		for (i = i + 1; i < SCTP_IPIF_HASH; i++) {
1268 			if (sctp->sctp_saddrs[i].ipif_count > 0) {
1269 				lst = &sctp->sctp_saddrs[i].sctp_ipif_list;
1270 				wsp->walk_addr = (uintptr_t)list_object(
1271 				    lst, lst->list_head.list_next);
1272 				swalker->hash_index = i;
1273 				swalker->cur_cnt = 1;
1274 				return (WALK_NEXT);
1275 			}
1276 		}
1277 	}
1278 	return (WALK_DONE);
1279 }
1280 
1281 static void
1282 sctp_walk_saddr_fini(mdb_walk_state_t *wsp)
1283 {
1284 	saddr_walk_t *swalker = (saddr_walk_t *)wsp->walk_data;
1285 
1286 	mdb_free(swalker, sizeof (saddr_walk_t));
1287 }
1288 
1289 static int
1290 sctp_walk_ill_init(mdb_walk_state_t *wsp)
1291 {
1292 	intptr_t i;
1293 
1294 	/* Find the first ill. */
1295 	for (i = 0; i < SCTP_ILL_HASH; i++) {
1296 		if (local_g_ills[i].ill_count > 0) {
1297 			wsp->walk_addr = (uintptr_t)list_object(
1298 			    &local_g_ills[i].sctp_ill_list,
1299 			    local_g_ills[i].sctp_ill_list.list_head.list_next);
1300 			wsp->walk_data = (void *)i;
1301 			wsp->walk_arg = (void *)1;
1302 			return (WALK_NEXT);
1303 		}
1304 	}
1305 	return (WALK_DONE);
1306 }
1307 
1308 static int
1309 sctp_walk_ill_step(mdb_walk_state_t *wsp)
1310 {
1311 	uintptr_t ill_ptr = wsp->walk_addr;
1312 	sctp_ill_t ill;
1313 	int status;
1314 	intptr_t i, j;
1315 
1316 	if (mdb_vread(&ill, sizeof (sctp_ill_t), ill_ptr) == -1) {
1317 		mdb_warn("failed to read sctp_ill_t at %p", ill_ptr);
1318 		return (WALK_ERR);
1319 	}
1320 	status = wsp->walk_callback(ill_ptr, &ill, wsp->walk_cbdata);
1321 	if (status != WALK_NEXT)
1322 		return (status);
1323 
1324 	i = (intptr_t)wsp->walk_data;
1325 	j = (intptr_t)wsp->walk_arg;
1326 
1327 	/*
1328 	 * If there is still an ill in the current list, return it.
1329 	 * Otherwise, go to the next list and find another one.
1330 	 */
1331 	if (j++ < local_g_ills[i].ill_count) {
1332 		wsp->walk_addr = (uintptr_t)ill.sctp_ills.list_next;
1333 		wsp->walk_data = (void *)i;
1334 		wsp->walk_arg = (void *)j;
1335 		return (WALK_NEXT);
1336 	} else {
1337 		list_t *ill_list;
1338 
1339 		for (i = i + 1; i < SCTP_ILL_HASH; i++) {
1340 			if (local_g_ills[i].ill_count > 0) {
1341 				ill_list = &local_g_ills[i].sctp_ill_list;
1342 				wsp->walk_addr = (uintptr_t)list_object(
1343 				    ill_list, ill_list->list_head.list_next);
1344 
1345 				/* Record the current position. */
1346 				wsp->walk_data = (void *)i;
1347 				wsp->walk_arg = (void *)1;
1348 				return (WALK_NEXT);
1349 			}
1350 		}
1351 	}
1352 	return (WALK_DONE);
1353 }
1354 
1355 static int
1356 sctp_walk_ipif_init(mdb_walk_state_t *wsp)
1357 {
1358 	intptr_t i;
1359 	list_t *ipif_list;
1360 
1361 	for (i = 0; i < SCTP_IPIF_HASH; i++) {
1362 		if (local_g_ipifs[i].ipif_count > 0) {
1363 			ipif_list = &local_g_ipifs[i].sctp_ipif_list;
1364 
1365 			wsp->walk_addr = (uintptr_t)list_object(ipif_list,
1366 			    ipif_list->list_head.list_next);
1367 			wsp->walk_data = (void *)i;
1368 			wsp->walk_arg = (void *)1;
1369 			return (WALK_NEXT);
1370 		}
1371 	}
1372 	return (WALK_DONE);
1373 }
1374 
1375 static int
1376 sctp_walk_ipif_step(mdb_walk_state_t *wsp)
1377 {
1378 	uintptr_t ipif_ptr = wsp->walk_addr;
1379 	sctp_ipif_t ipif;
1380 	int status;
1381 	intptr_t i, j;
1382 
1383 	if (mdb_vread(&ipif, sizeof (sctp_ipif_t), ipif_ptr) == -1) {
1384 		mdb_warn("failed to read sctp_ipif_t at %p", ipif_ptr);
1385 		return (WALK_ERR);
1386 	}
1387 	status = wsp->walk_callback(ipif_ptr, &ipif, wsp->walk_cbdata);
1388 	if (status != WALK_NEXT)
1389 		return (status);
1390 
1391 	i = (intptr_t)wsp->walk_data;
1392 	j = (intptr_t)wsp->walk_arg;
1393 
1394 	/*
1395 	 * If there is still an ipif in the current list, return it.
1396 	 * Otherwise, go to the next list and find another one.
1397 	 */
1398 	if (j++ < local_g_ipifs[i].ipif_count) {
1399 		wsp->walk_addr = (uintptr_t)ipif.sctp_ipifs.list_next;
1400 		wsp->walk_data = (void *)i;
1401 		wsp->walk_arg = (void *)j;
1402 		return (WALK_NEXT);
1403 	} else {
1404 		list_t *ipif_list;
1405 
1406 		for (i = i + 1; i < SCTP_IPIF_HASH; i++) {
1407 			if (local_g_ipifs[i].ipif_count > 0) {
1408 				ipif_list = &local_g_ipifs[i].sctp_ipif_list;
1409 				wsp->walk_addr = (uintptr_t)list_object(
1410 				    ipif_list, ipif_list->list_head.list_next);
1411 
1412 				/* Record the current position */
1413 				wsp->walk_data = (void *)i;
1414 				wsp->walk_arg = (void *)1;
1415 				return (WALK_NEXT);
1416 			}
1417 		}
1418 	}
1419 	return (WALK_DONE);
1420 }
1421 
1422 static void
1423 sctp_help(void)
1424 {
1425 	mdb_printf("Print information for a given SCTP sctp_t\n\n");
1426 	mdb_printf("Options:\n");
1427 	mdb_printf("\t-a\t All the information\n");
1428 	mdb_printf("\t-f\t Flags\n");
1429 	mdb_printf("\t-h\t Hash Tables\n");
1430 	mdb_printf("\t-o\t Outbound Data\n");
1431 	mdb_printf("\t-i\t Inbound Data\n");
1432 	mdb_printf("\t-m\t Miscellaneous Information\n");
1433 	mdb_printf("\t-r\t RTT Tracking\n");
1434 	mdb_printf("\t-S\t Stats Counters\n");
1435 	mdb_printf("\t-F\t Flow Control\n");
1436 	mdb_printf("\t-H\t Composite Headers\n");
1437 	mdb_printf("\t-p\t PMTUD\n");
1438 	mdb_printf("\t-R\t Retransmit Information\n");
1439 	mdb_printf("\t-C\t Connection State\n");
1440 	mdb_printf("\t-c\t Cleanup / Close\n");
1441 	mdb_printf("\t-e\t Extensions and Reliable Control Chunks\n");
1442 	mdb_printf("\t-d\t Local and Peer addresses\n");
1443 	mdb_printf("\t-P\t Peer addresses\n");
1444 }
1445 static const mdb_dcmd_t dcmds[] = {
1446 	{ "sctp", ":[-afhoimrSFHpRCcedP]",
1447 	    "display sctp control structure", sctp, sctp_help },
1448 	{ "sctp_set", ":", "display a SCTP set", sctp_set },
1449 	{ "sctp_faddr", ":", "display a faddr", sctp_faddr },
1450 	{ "sctp_istr_msgs", ":", "display msg list on an instream",
1451 	    sctp_istr_msgs },
1452 	{ "sctp_mdata_chunk", ":", "display a data chunk in an mblk",
1453 	    sctp_mdata_chunk },
1454 	{ "sctp_xmit_list", ":", "display sctp xmit lists", sctp_xmit_list },
1455 	{ "sctp_instr", ":", "display instr", sctp_instr },
1456 	{ "sctp_reass_list", ":", "display reass list", sctp_reass_list },
1457 	{ "sctp_uo_reass_list", ":", "display un-ordered reass list",
1458 	    sctp_uo_reass_list },
1459 	{ NULL }
1460 };
1461 
1462 static const fanout_init_t listen_fanout_init = {
1463 	"sctp_listen_fanout", listen_size, listen_next
1464 };
1465 
1466 static const fanout_init_t conn_fanout_init = {
1467 	"sctp_conn_fanout", conn_size, conn_next
1468 };
1469 
1470 static const fanout_init_t bind_fanout_init = {
1471 	"sctp_bind_fanout", bind_size, bind_next
1472 };
1473 
1474 static const mdb_walker_t walkers[] = {
1475 	{ "sctps", "walk the full chain of sctps",
1476 	    sctp_walk_init, sctp_walk_step, NULL },
1477 	{ "sctp_listen_fanout", "walk the sctp listen fanout",
1478 	    fanout_walk_init, fanout_walk_step, fanout_walk_fini,
1479 	    (void *)&listen_fanout_init },
1480 	{ "sctp_conn_fanout", "walk the sctp conn fanout",
1481 	    fanout_walk_init, fanout_walk_step, fanout_walk_fini,
1482 	    (void *)&conn_fanout_init },
1483 	{ "sctp_bind_fanout", "walk the sctp bind fanout",
1484 	    fanout_walk_init, fanout_walk_step, fanout_walk_fini,
1485 	    (void *)&bind_fanout_init },
1486 	{ "sctp_walk_faddr", "walk the peer address list of a given sctp_t",
1487 	    sctp_walk_faddr_init, sctp_walk_faddr_step, NULL },
1488 	{ "sctp_walk_saddr", "walk the local address list of a given sctp_t",
1489 	    sctp_walk_saddr_init, sctp_walk_saddr_step, sctp_walk_saddr_fini },
1490 	{ "sctp_walk_ill", "walk the sctp_g_ills list",
1491 	    sctp_walk_ill_init, sctp_walk_ill_step, NULL },
1492 	{ "sctp_walk_ipif", "walk the sctp_g_ipif list",
1493 	    sctp_walk_ipif_init, sctp_walk_ipif_step, NULL },
1494 	{ NULL }
1495 };
1496 
1497 static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };
1498 
1499 const mdb_modinfo_t *
1500 _mdb_init(void)
1501 {
1502 	GElf_Sym sym;
1503 	GElf_Sym ills_sym;
1504 	GElf_Sym ipifs_sym;
1505 
1506 	if (mdb_lookup_by_name("sctp_g_list", &sctp_list_sym) == -1) {
1507 		mdb_warn("failed to find 'sctp_g_list'");
1508 		return (NULL);
1509 	}
1510 	if (mdb_vread(&sctp_list, sizeof (list_t),
1511 	    (uintptr_t)sctp_list_sym.st_value) == -1) {
1512 		mdb_warn("failed to read 'sctp_g_list'");
1513 		return (NULL);
1514 	}
1515 	if (mdb_lookup_by_name("sctp_conn_hash_size", &sym) != -1) {
1516 		if (mdb_vread(&sctp_conn_hash_size,
1517 		    sizeof (sctp_conn_hash_size), sym.st_value) == -1) {
1518 			mdb_warn("failed to read 'sctp_conn_hash_size'");
1519 			return (NULL);
1520 		}
1521 	}
1522 	if (mdb_lookup_by_name("sctp_g_ills", &ills_sym) == -1) {
1523 		mdb_warn("failed to find 'sctp_g_ills'");
1524 		return (NULL);
1525 	}
1526 	if (mdb_vread(&local_g_ills, sizeof (local_g_ills),
1527 	    (uintptr_t)ills_sym.st_value) == -1) {
1528 		mdb_warn("failed to read 'sctp_g_ills'");
1529 		return (NULL);
1530 	}
1531 	if (mdb_lookup_by_name("sctp_g_ipifs", &ipifs_sym) == -1) {
1532 		mdb_warn("failed to find 'sctp_g_ipifs'");
1533 		return (NULL);
1534 	}
1535 	if (mdb_vread(&local_g_ipifs, sizeof (local_g_ipifs),
1536 	    (uintptr_t)ipifs_sym.st_value) == -1) {
1537 		mdb_warn("failed to read 'sctp_g_ipifs'");
1538 		return (NULL);
1539 	}
1540 
1541 	return (&modinfo);
1542 }
1543