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