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