1/*-
2 * Copyright (c) 2007 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/*
28 * Debugger routines relating to sockets, protocols, etc, for use in DDB.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include "opt_ddb.h"
35
36#include <sys/param.h>
37#include <sys/domain.h>
38#include <sys/kernel.h>
39#include <sys/protosw.h>
40#include <sys/socket.h>
41#include <sys/socketvar.h>
42
43#ifdef DDB
44#include <ddb/ddb.h>
45
46static void
47db_print_sotype(short so_type)
48{
49
50	switch (so_type) {
51	case SOCK_STREAM:
52		db_printf("SOCK_STREAM");
53		break;
54
55	case SOCK_DGRAM:
56		db_printf("SOCK_DGRAM");
57		break;
58
59	case SOCK_RAW:
60		db_printf("SOCK_RAW");
61		break;
62
63	case SOCK_RDM:
64		db_printf("SOCK_RDM");
65		break;
66
67	case SOCK_SEQPACKET:
68		db_printf("SOCK_SEQPACKET");
69		break;
70
71	default:
72		db_printf("unknown");
73		break;
74	}
75}
76
77static void
78db_print_sooptions(short so_options)
79{
80	int comma;
81
82	comma = 0;
83	if (so_options & SO_DEBUG) {
84		db_printf("%sSO_DEBUG", comma ? ", " : "");
85		comma = 1;
86	}
87	if (so_options & SO_ACCEPTCONN) {
88		db_printf("%sSO_ACCEPTCONN", comma ? ", " : "");
89		comma = 1;
90	}
91	if (so_options & SO_REUSEADDR) {
92		db_printf("%sSO_REUSEADDR", comma ? ", " : "");
93		comma = 1;
94	}
95	if (so_options & SO_KEEPALIVE) {
96		db_printf("%sSO_KEEPALIVE", comma ? ", " : "");
97		comma = 1;
98	}
99	if (so_options & SO_DONTROUTE) {
100		db_printf("%sSO_DONTROUTE", comma ? ", " : "");
101		comma = 1;
102	}
103	if (so_options & SO_BROADCAST) {
104		db_printf("%sSO_BROADCAST", comma ? ", " : "");
105		comma = 1;
106	}
107	if (so_options & SO_USELOOPBACK) {
108		db_printf("%sSO_USELOOPBACK", comma ? ", " : "");
109		comma = 1;
110	}
111	if (so_options & SO_LINGER) {
112		db_printf("%sSO_LINGER", comma ? ", " : "");
113		comma = 1;
114	}
115	if (so_options & SO_OOBINLINE) {
116		db_printf("%sSO_OOBINLINE", comma ? ", " : "");
117		comma = 1;
118	}
119	if (so_options & SO_REUSEPORT) {
120		db_printf("%sSO_REUSEPORT", comma ? ", " : "");
121		comma = 1;
122	}
123	if (so_options & SO_TIMESTAMP) {
124		db_printf("%sSO_TIMESTAMP", comma ? ", " : "");
125		comma = 1;
126	}
127	if (so_options & SO_NOSIGPIPE) {
128		db_printf("%sSO_NOSIGPIPE", comma ? ", " : "");
129		comma = 1;
130	}
131	if (so_options & SO_ACCEPTFILTER) {
132		db_printf("%sSO_ACCEPTFILTER", comma ? ", " : "");
133		comma = 1;
134	}
135	if (so_options & SO_BINTIME) {
136		db_printf("%sSO_BINTIME", comma ? ", " : "");
137		comma = 1;
138	}
139	if (so_options & SO_NO_OFFLOAD) {
140		db_printf("%sSO_NO_OFFLOAD", comma ? ", " : "");
141		comma = 1;
142	}
143	if (so_options & SO_NO_DDP) {
144		db_printf("%sSO_NO_DDP", comma ? ", " : "");
145		comma = 1;
146	}
147}
148
149static void
150db_print_sostate(short so_state)
151{
152	int comma;
153
154	comma = 0;
155	if (so_state & SS_NOFDREF) {
156		db_printf("%sSS_NOFDREF", comma ? ", " : "");
157		comma = 1;
158	}
159	if (so_state & SS_ISCONNECTED) {
160		db_printf("%sSS_ISCONNECTED", comma ? ", " : "");
161		comma = 1;
162	}
163	if (so_state & SS_ISCONNECTING) {
164		db_printf("%sSS_ISCONNECTING", comma ? ", " : "");
165		comma = 1;
166	}
167	if (so_state & SS_ISDISCONNECTING) {
168		db_printf("%sSS_ISDISCONNECTING", comma ? ", " : "");
169		comma = 1;
170	}
171	if (so_state & SS_NBIO) {
172		db_printf("%sSS_NBIO", comma ? ", " : "");
173		comma = 1;
174	}
175	if (so_state & SS_ASYNC) {
176		db_printf("%sSS_ASYNC", comma ? ", " : "");
177		comma = 1;
178	}
179	if (so_state & SS_ISCONFIRMING) {
180		db_printf("%sSS_ISCONFIRMING", comma ? ", " : "");
181		comma = 1;
182	}
183	if (so_state & SS_PROTOREF) {
184		db_printf("%sSS_PROTOREF", comma ? ", " : "");
185		comma = 1;
186	}
187}
188
189static void
190db_print_soqstate(int so_qstate)
191{
192	int comma;
193
194	comma = 0;
195	if (so_qstate & SQ_INCOMP) {
196		db_printf("%sSQ_INCOMP", comma ? ", " : "");
197		comma = 1;
198	}
199	if (so_qstate & SQ_COMP) {
200		db_printf("%sSQ_COMP", comma ? ", " : "");
201		comma = 1;
202	}
203}
204
205static void
206db_print_sbstate(short sb_state)
207{
208	int comma;
209
210	comma = 0;
211	if (sb_state & SBS_CANTSENDMORE) {
212		db_printf("%sSBS_CANTSENDMORE", comma ? ", " : "");
213		comma = 1;
214	}
215	if (sb_state & SBS_CANTRCVMORE) {
216		db_printf("%sSBS_CANTRCVMORE", comma ? ", " : "");
217		comma = 1;
218	}
219	if (sb_state & SBS_RCVATMARK) {
220		db_printf("%sSBS_RCVATMARK", comma ? ", " : "");
221		comma = 1;
222	}
223}
224
225static void
226db_print_indent(int indent)
227{
228	int i;
229
230	for (i = 0; i < indent; i++)
231		db_printf(" ");
232}
233
234static void
235db_print_domain(struct domain *d, const char *domain_name, int indent)
236{
237
238	db_print_indent(indent);
239	db_printf("%s at %p\n", domain_name, d);
240
241	indent += 2;
242
243	db_print_indent(indent);
244	db_printf("dom_family: %d   ", d->dom_family);
245	db_printf("dom_name: %s\n", d->dom_name);
246
247	db_print_indent(indent);
248	db_printf("dom_init: %p   ", d->dom_init);
249	db_printf("dom_externalize: %p   ", d->dom_externalize);
250	db_printf("dom_dispose: %p\n", d->dom_dispose);
251
252	db_print_indent(indent);
253	db_printf("dom_protosw: %p   ", d->dom_protosw);
254	db_printf("dom_next: %p\n", d->dom_next);
255
256	db_print_indent(indent);
257	db_printf("dom_rtattach: %p   ", d->dom_rtattach);
258
259	db_print_indent(indent);
260	db_printf("dom_ifattach: %p   ", d->dom_ifattach);
261	db_printf("dom_ifdetach: %p\n", d->dom_ifdetach);
262}
263
264static void
265db_print_prflags(short pr_flags)
266{
267	int comma;
268
269	comma = 0;
270	if (pr_flags & PR_ATOMIC) {
271		db_printf("%sPR_ATOMIC", comma ? ", " : "");
272		comma = 1;
273	}
274	if (pr_flags & PR_ADDR) {
275		db_printf("%sPR_ADDR", comma ? ", " : "");
276		comma = 1;
277	}
278	if (pr_flags & PR_CONNREQUIRED) {
279		db_printf("%sPR_CONNREQUIRED", comma ? ", " : "");
280		comma = 1;
281	}
282	if (pr_flags & PR_WANTRCVD) {
283		db_printf("%sPR_WANTRCVD", comma ? ", " : "");
284		comma = 1;
285	}
286	if (pr_flags & PR_RIGHTS) {
287		db_printf("%sPR_RIGHTS", comma ? ", " : "");
288		comma = 1;
289	}
290	if (pr_flags & PR_IMPLOPCL) {
291		db_printf("%sPR_IMPLOPCL", comma ? ", " : "");
292		comma = 1;
293	}
294	if (pr_flags & PR_LASTHDR) {
295		db_printf("%sPR_LASTHDR", comma ? ", " : "");
296		comma = 1;
297	}
298}
299
300static void
301db_print_protosw(struct protosw *pr, const char *prname, int indent)
302{
303
304	db_print_indent(indent);
305	db_printf("%s at %p\n", prname, pr);
306
307	indent += 2;
308
309	db_print_indent(indent);
310	db_printf("pr_type: %d   ", pr->pr_type);
311	db_printf("pr_domain: %p\n", pr->pr_domain);
312	if (pr->pr_domain != NULL)
313		db_print_domain(pr->pr_domain, "pr_domain", indent);
314
315	db_print_indent(indent);
316	db_printf("pr_protocol: %d\n", pr->pr_protocol);
317
318	db_print_indent(indent);
319	db_printf("pr_flags: %d (", pr->pr_flags);
320	db_print_prflags(pr->pr_flags);
321	db_printf(")\n");
322
323	db_print_indent(indent);
324	db_printf("pr_input: %p   ", pr->pr_input);
325	db_printf("pr_output: %p   ", pr->pr_output);
326	db_printf("pr_ctlinput: %p\n", pr->pr_ctlinput);
327
328	db_print_indent(indent);
329	db_printf("pr_ctloutput: %p   ", pr->pr_ctloutput);
330	db_printf("pr_init: %p\n", pr->pr_init);
331
332	db_print_indent(indent);
333	db_printf("pr_fasttimo: %p   ", pr->pr_fasttimo);
334	db_printf("pr_slowtimo: %p   ", pr->pr_slowtimo);
335	db_printf("pr_drain: %p\n", pr->pr_drain);
336
337	db_print_indent(indent);
338}
339
340static void
341db_print_sbflags(short sb_flags)
342{
343	int comma;
344
345	comma = 0;
346	if (sb_flags & SB_WAIT) {
347		db_printf("%sSB_WAIT", comma ? ", " : "");
348		comma = 1;
349	}
350	if (sb_flags & SB_SEL) {
351		db_printf("%sSB_SEL", comma ? ", " : "");
352		comma = 1;
353	}
354	if (sb_flags & SB_ASYNC) {
355		db_printf("%sSB_ASYNC", comma ? ", " : "");
356		comma = 1;
357	}
358	if (sb_flags & SB_UPCALL) {
359		db_printf("%sSB_UPCALL", comma ? ", " : "");
360		comma = 1;
361	}
362	if (sb_flags & SB_NOINTR) {
363		db_printf("%sSB_NOINTR", comma ? ", " : "");
364		comma = 1;
365	}
366	if (sb_flags & SB_AIO) {
367		db_printf("%sSB_AIO", comma ? ", " : "");
368		comma = 1;
369	}
370	if (sb_flags & SB_KNOTE) {
371		db_printf("%sSB_KNOTE", comma ? ", " : "");
372		comma = 1;
373	}
374	if (sb_flags & SB_AUTOSIZE) {
375		db_printf("%sSB_AUTOSIZE", comma ? ", " : "");
376		comma = 1;
377	}
378}
379
380static void
381db_print_sockbuf(struct sockbuf *sb, const char *sockbufname, int indent)
382{
383
384	db_print_indent(indent);
385	db_printf("%s at %p\n", sockbufname, sb);
386
387	indent += 2;
388
389	db_print_indent(indent);
390	db_printf("sb_state: 0x%x (", sb->sb_state);
391	db_print_sbstate(sb->sb_state);
392	db_printf(")\n");
393
394	db_print_indent(indent);
395	db_printf("sb_mb: %p   ", sb->sb_mb);
396	db_printf("sb_mbtail: %p   ", sb->sb_mbtail);
397	db_printf("sb_lastrecord: %p\n", sb->sb_lastrecord);
398
399	db_print_indent(indent);
400	db_printf("sb_sndptr: %p   ", sb->sb_sndptr);
401	db_printf("sb_sndptroff: %u\n", sb->sb_sndptroff);
402
403	db_print_indent(indent);
404	db_printf("sb_acc: %u   ", sb->sb_acc);
405	db_printf("sb_ccc: %u   ", sb->sb_ccc);
406	db_printf("sb_hiwat: %u   ", sb->sb_hiwat);
407	db_printf("sb_mbcnt: %u   ", sb->sb_mbcnt);
408	db_printf("sb_mbmax: %u\n", sb->sb_mbmax);
409
410	db_print_indent(indent);
411	db_printf("sb_ctl: %u   ", sb->sb_ctl);
412	db_printf("sb_lowat: %d   ", sb->sb_lowat);
413	db_printf("sb_timeo: %jd\n", sb->sb_timeo);
414
415	db_print_indent(indent);
416	db_printf("sb_flags: 0x%x (", sb->sb_flags);
417	db_print_sbflags(sb->sb_flags);
418	db_printf(")\n");
419
420	db_print_indent(indent);
421	db_printf("sb_aiojobq first: %p\n", TAILQ_FIRST(&sb->sb_aiojobq));
422}
423
424static void
425db_print_socket(struct socket *so, const char *socketname, int indent)
426{
427
428	db_print_indent(indent);
429	db_printf("%s at %p\n", socketname, so);
430
431	indent += 2;
432
433	db_print_indent(indent);
434	db_printf("so_count: %d   ", so->so_count);
435	db_printf("so_type: %d (", so->so_type);
436	db_print_sotype(so->so_type);
437	db_printf(")\n");
438
439	db_print_indent(indent);
440	db_printf("so_options: 0x%x (", so->so_options);
441	db_print_sooptions(so->so_options);
442	db_printf(")\n");
443
444	db_print_indent(indent);
445	db_printf("so_linger: %d   ", so->so_linger);
446	db_printf("so_state: 0x%x (", so->so_state);
447	db_print_sostate(so->so_state);
448	db_printf(")\n");
449
450	db_print_indent(indent);
451	db_printf("so_qstate: 0x%x (", so->so_qstate);
452	db_print_soqstate(so->so_qstate);
453	db_printf(")   ");
454	db_printf("so_pcb: %p   ", so->so_pcb);
455	db_printf("so_proto: %p\n", so->so_proto);
456
457	if (so->so_proto != NULL)
458		db_print_protosw(so->so_proto, "so_proto", indent);
459
460	db_print_indent(indent);
461	db_printf("so_head: %p   ", so->so_head);
462	db_printf("so_incomp first: %p   ", TAILQ_FIRST(&so->so_incomp));
463	db_printf("so_comp first: %p\n", TAILQ_FIRST(&so->so_comp));
464
465	db_print_indent(indent);
466	/* so_list skipped */
467	db_printf("so_qlen: %u   ", so->so_qlen);
468	db_printf("so_incqlen: %u   ", so->so_incqlen);
469	db_printf("so_qlimit: %u   ", so->so_qlimit);
470	db_printf("so_timeo: %d   ", so->so_timeo);
471	db_printf("so_error: %d\n", so->so_error);
472
473	db_print_indent(indent);
474	db_printf("so_sigio: %p   ", so->so_sigio);
475	db_printf("so_oobmark: %lu   ", so->so_oobmark);
476
477	db_print_sockbuf(&so->so_rcv, "so_rcv", indent);
478	db_print_sockbuf(&so->so_snd, "so_snd", indent);
479}
480
481DB_SHOW_COMMAND(socket, db_show_socket)
482{
483	struct socket *so;
484
485	if (!have_addr) {
486		db_printf("usage: show socket <addr>\n");
487		return;
488	}
489	so = (struct socket *)addr;
490
491	db_print_socket(so, "socket", 0);
492}
493
494DB_SHOW_COMMAND(sockbuf, db_show_sockbuf)
495{
496	struct sockbuf *sb;
497
498	if (!have_addr) {
499		db_printf("usage: show sockbuf <addr>\n");
500		return;
501	}
502	sb = (struct sockbuf *)addr;
503
504	db_print_sockbuf(sb, "sockbuf", 0);
505}
506
507DB_SHOW_COMMAND(protosw, db_show_protosw)
508{
509	struct protosw *pr;
510
511	if (!have_addr) {
512		db_printf("usage: show protosw <addr>\n");
513		return;
514	}
515	pr = (struct protosw *)addr;
516
517	db_print_protosw(pr, "protosw", 0);
518}
519
520DB_SHOW_COMMAND(domain, db_show_domain)
521{
522	struct domain *d;
523
524	if (!have_addr) {
525		db_printf("usage: show protosw <addr>\n");
526		return;
527	}
528	d = (struct domain *)addr;
529
530	db_print_domain(d, "domain", 0);
531}
532#endif
533