xref: /freebsd/usr.bin/netstat/netgraph.c (revision 95968ea7ec8f1cd3c1d03cc56c704cb2392d0d25)
1 /*-
2  * Copyright (c) 1996-1999 Whistle Communications, Inc.
3  * All rights reserved.
4  *
5  * Subject to the following obligations and disclaimer of warranty, use and
6  * redistribution of this software, in source or object code forms, with or
7  * without modifications are expressly permitted by Whistle Communications;
8  * provided, however, that:
9  * 1. Any and all reproductions of the source or object code must include the
10  *    copyright notice above and the following disclaimer of warranties; and
11  * 2. No rights are granted, in any manner or form, to use Whistle
12  *    Communications, Inc. trademarks, including the mark "WHISTLE
13  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
14  *    such appears in the above copyright notice or in the software.
15  *
16  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
17  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
18  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
19  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
21  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
22  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
23  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
24  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
25  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
26  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
32  * OF SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/socket.h>
38 #define	_WANT_SOCKET
39 #include <sys/socketvar.h>
40 #include <sys/protosw.h>
41 #include <sys/linker.h>
42 
43 #include <net/route.h>
44 
45 #include <netgraph.h>
46 #include <netgraph/ng_message.h>
47 #include <netgraph/ng_socket.h>
48 #include <netgraph/ng_socketvar.h>
49 
50 #include <errno.h>
51 #include <stdint.h>
52 #include <stdio.h>
53 #include <stdbool.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <libxo/xo.h>
57 #include "netstat.h"
58 
59 static	int first = 1;
60 static	int csock = -1;
61 
62 void
netgraphprotopr(u_long off,const char * name,int af1 __unused,int proto __unused)63 netgraphprotopr(u_long off, const char *name, int af1 __unused,
64     int proto __unused)
65 {
66 	struct ngpcb *this, *next;
67 	struct ngpcb ngpcb;
68 	struct socket sockb;
69 	int debug = 1;
70 
71 	/* If symbol not found, try looking in the KLD module */
72 	if (off == 0) {
73 		if (debug)
74 			xo_warnx("Error reading symbols from ng_socket.ko");
75 		return;
76 	}
77 
78 	/* Get pointer to first socket */
79 	kread(off, (char *)&this, sizeof(this));
80 
81 	/* Get my own socket node */
82 	if (csock == -1)
83 		NgMkSockNode(NULL, &csock, NULL);
84 
85 	for (; this != NULL; this = next) {
86 		u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
87 		struct ng_mesg *resp = (struct ng_mesg *) rbuf;
88 		struct nodeinfo *ni = (struct nodeinfo *) resp->data;
89 		char path[64];
90 
91 		/* Read in ngpcb structure */
92 		kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb));
93 		next = LIST_NEXT(&ngpcb, socks);
94 
95 		/* Read in socket structure */
96 		kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));
97 
98 		/* Check type of socket */
99 		if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL)
100 			continue;
101 		if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA)
102 			continue;
103 
104 		/* Do headline */
105 		if (first) {
106 			xo_emit("{T:Netgraph sockets}\n");
107 			if (Aflag)
108 				xo_emit("{T:/%-8.8s} ", "PCB");
109 			xo_emit("{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} "
110 			    "{T:/%-14.14s} {T:/%s}\n",
111 			    "Type", "Recv-Q", "Send-Q", "Node Address",
112 			    "#Hooks");
113 			first = 0;
114 		}
115 
116 		/* Show socket */
117 		if (Aflag)
118 			xo_emit("{:address/%8lx} ", (u_long) this);
119 		xo_emit("{t:name/%-5.5s} {:receive-bytes-waiting/%6u} "
120 		    "{:send-byte-waiting/%6u} ",
121 		    name, sockb.so_rcv.sb_ccc, sockb.so_snd.sb_ccc);
122 
123 		/* Get info on associated node */
124 		if (ngpcb.node_id == 0 || csock == -1)
125 			goto finish;
126 		snprintf(path, sizeof(path), "[%x]:", ngpcb.node_id);
127 		if (NgSendMsg(csock, path,
128 		    NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0)
129 			goto finish;
130 		if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0)
131 			goto finish;
132 
133 		/* Display associated node info */
134 		if (*ni->name != '\0')
135 			snprintf(path, sizeof(path), "%s:", ni->name);
136 		xo_emit("{t:path/%-14.14s} {:hooks/%4d}", path, ni->hooks);
137 finish:
138 		xo_emit("\n");
139 	}
140 }
141 
142