xref: /freebsd/usr.bin/netstat/netgraph.c (revision 3157ba21)
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/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/protosw.h>
43 #include <sys/linker.h>
44 
45 #include <net/route.h>
46 
47 #include <netgraph.h>
48 #include <netgraph/ng_message.h>
49 #include <netgraph/ng_socket.h>
50 #include <netgraph/ng_socketvar.h>
51 
52 #include <nlist.h>
53 #include <errno.h>
54 #include <stdint.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <err.h>
59 #include "netstat.h"
60 
61 static	int first = 1;
62 static	int csock = -1;
63 
64 void
65 netgraphprotopr(u_long off, const char *name, int af1 __unused,
66     int proto __unused)
67 {
68 	struct ngpcb *this, *next;
69 	struct ngpcb ngpcb;
70 	struct ngsock info;
71 	struct socket sockb;
72 	int debug = 1;
73 
74 	/* If symbol not found, try looking in the KLD module */
75 	if (off == 0) {
76 		const char *const modname = "ng_socket.ko";
77 /* XXX We should get "mpath" from "sysctl kern.module_path" */
78 		const char *mpath[] = { "/", "/boot/", "/modules/", NULL };
79 		struct nlist sym[] = { { .n_name = "_ngsocklist" },
80 				       { .n_name = NULL } };
81 		const char **pre;
82 		struct kld_file_stat ks;
83 		int fileid;
84 
85 		/* Can't do this for core dumps. */
86 		if (!live)
87 			return;
88 
89 		/* See if module is loaded */
90 		if ((fileid = kldfind(modname)) < 0) {
91 			if (debug)
92 				warn("kldfind(%s)", modname);
93 			return;
94 		}
95 
96 		/* Get module info */
97 		memset(&ks, 0, sizeof(ks));
98 		ks.version = sizeof(struct kld_file_stat);
99 		if (kldstat(fileid, &ks) < 0) {
100 			if (debug)
101 				warn("kldstat(%d)", fileid);
102 			return;
103 		}
104 
105 		/* Get symbol table from module file */
106 		for (pre = mpath; *pre; pre++) {
107 			char path[MAXPATHLEN];
108 
109 			snprintf(path, sizeof(path), "%s%s", *pre, modname);
110 			if (nlist(path, sym) == 0)
111 				break;
112 		}
113 
114 		/* Did we find it? */
115 		if (sym[0].n_value == 0) {
116 			if (debug)
117 				warnx("%s not found", modname);
118 			return;
119 		}
120 
121 		/* Symbol found at load address plus symbol offset */
122 		off = (u_long) ks.address + sym[0].n_value;
123 	}
124 
125 	/* Get pointer to first socket */
126 	kread(off, (char *)&this, sizeof(this));
127 
128 	/* Get my own socket node */
129 	if (csock == -1)
130 		NgMkSockNode(NULL, &csock, NULL);
131 
132 	for (; this != NULL; this = next) {
133 		u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
134 		struct ng_mesg *resp = (struct ng_mesg *) rbuf;
135 		struct nodeinfo *ni = (struct nodeinfo *) resp->data;
136 		char path[64];
137 
138 		/* Read in ngpcb structure */
139 		kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb));
140 		next = LIST_NEXT(&ngpcb, socks);
141 
142 		/* Read in socket structure */
143 		kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));
144 
145 		/* Check type of socket */
146 		if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL)
147 			continue;
148 		if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA)
149 			continue;
150 
151 		/* Do headline */
152 		if (first) {
153 			printf("Netgraph sockets\n");
154 			if (Aflag)
155 				printf("%-8.8s ", "PCB");
156 			printf("%-5.5s %-6.6s %-6.6s %-14.14s %s\n",
157 			    "Type", "Recv-Q", "Send-Q",
158 			    "Node Address", "#Hooks");
159 			first = 0;
160 		}
161 
162 		/* Show socket */
163 		if (Aflag)
164 			printf("%8lx ", (u_long) this);
165 		printf("%-5.5s %6u %6u ",
166 		    name, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc);
167 
168 		/* Get ngsock structure */
169 		if (ngpcb.sockdata == NULL)	/* unconnected data socket */
170 			goto finish;
171 		kread((u_long)ngpcb.sockdata, (char *)&info, sizeof(info));
172 
173 		/* Get info on associated node */
174 		if (info.node_id == 0 || csock == -1)
175 			goto finish;
176 		snprintf(path, sizeof(path), "[%x]:", info.node_id);
177 		if (NgSendMsg(csock, path,
178 		    NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0)
179 			goto finish;
180 		if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0)
181 			goto finish;
182 
183 		/* Display associated node info */
184 		if (*ni->name != '\0')
185 			snprintf(path, sizeof(path), "%s:", ni->name);
186 		printf("%-14.14s %4d", path, ni->hooks);
187 finish:
188 		putchar('\n');
189 	}
190 }
191 
192