1175061Sobrien/*-
252419Sjulian * Copyright (c) 1996-1999 Whistle Communications, Inc.
352419Sjulian * All rights reserved.
4175061Sobrien *
552419Sjulian * Subject to the following obligations and disclaimer of warranty, use and
652419Sjulian * redistribution of this software, in source or object code forms, with or
752419Sjulian * without modifications are expressly permitted by Whistle Communications;
852419Sjulian * provided, however, that:
952419Sjulian * 1. Any and all reproductions of the source or object code must include the
1052419Sjulian *    copyright notice above and the following disclaimer of warranties; and
1152419Sjulian * 2. No rights are granted, in any manner or form, to use Whistle
1252419Sjulian *    Communications, Inc. trademarks, including the mark "WHISTLE
1352419Sjulian *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1452419Sjulian *    such appears in the above copyright notice or in the software.
15175061Sobrien *
1652419Sjulian * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
1752419Sjulian * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
1852419Sjulian * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
1952419Sjulian * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
2052419Sjulian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
2152419Sjulian * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2252419Sjulian * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2352419Sjulian * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2452419Sjulian * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2552419Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
2652419Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
2752419Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
2852419Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
2952419Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3052419Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3152419Sjulian * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3252419Sjulian * OF SUCH DAMAGE.
3352419Sjulian */
3452419Sjulian
35132671Scharnier#include <sys/cdefs.h>
36132671Scharnier__FBSDID("$FreeBSD: releng/10.3/usr.bin/netstat/netgraph.c 263335 2014-03-19 09:36:29Z glebius $");
3752419Sjulian
3852419Sjulian#include <sys/param.h>
3952419Sjulian#include <sys/queue.h>
4052419Sjulian#include <sys/socket.h>
4152419Sjulian#include <sys/socketvar.h>
4252419Sjulian#include <sys/protosw.h>
4352419Sjulian#include <sys/linker.h>
4452419Sjulian
4552419Sjulian#include <net/route.h>
4652419Sjulian
4752450Sdillon#include <netgraph.h>
4852419Sjulian#include <netgraph/ng_message.h>
4952419Sjulian#include <netgraph/ng_socket.h>
5052419Sjulian#include <netgraph/ng_socketvar.h>
5152419Sjulian
5252419Sjulian#include <nlist.h>
53200462Sdelphij#include <errno.h>
54160787Syar#include <stdint.h>
5552419Sjulian#include <stdio.h>
5652419Sjulian#include <string.h>
57200462Sdelphij#include <unistd.h>
5852419Sjulian#include <err.h>
5952419Sjulian#include "netstat.h"
6052419Sjulian
6152419Sjulianstatic	int first = 1;
6252419Sjulianstatic	int csock = -1;
6352419Sjulian
6452419Sjulianvoid
65171465Sjhbnetgraphprotopr(u_long off, const char *name, int af1 __unused,
66171465Sjhb    int proto __unused)
6752419Sjulian{
6852419Sjulian	struct ngpcb *this, *next;
6952419Sjulian	struct ngpcb ngpcb;
7052419Sjulian	struct socket sockb;
7152419Sjulian	int debug = 1;
7252419Sjulian
7352419Sjulian	/* If symbol not found, try looking in the KLD module */
7452419Sjulian	if (off == 0) {
75263335Sglebius		if (debug)
76263335Sglebius			fprintf(stderr,
77263335Sglebius			    "Error reading symbols from ng_socket.ko");
78263335Sglebius		return;
7952419Sjulian	}
8052419Sjulian
8152419Sjulian	/* Get pointer to first socket */
8252419Sjulian	kread(off, (char *)&this, sizeof(this));
8352419Sjulian
8452419Sjulian	/* Get my own socket node */
8552419Sjulian	if (csock == -1)
8652419Sjulian		NgMkSockNode(NULL, &csock, NULL);
8752419Sjulian
8852419Sjulian	for (; this != NULL; this = next) {
8952419Sjulian		u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)];
9052419Sjulian		struct ng_mesg *resp = (struct ng_mesg *) rbuf;
9152419Sjulian		struct nodeinfo *ni = (struct nodeinfo *) resp->data;
9252419Sjulian		char path[64];
9352419Sjulian
9452419Sjulian		/* Read in ngpcb structure */
9552419Sjulian		kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb));
9670524Sphk		next = LIST_NEXT(&ngpcb, socks);
9752419Sjulian
9852419Sjulian		/* Read in socket structure */
9952419Sjulian		kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb));
10052419Sjulian
10152419Sjulian		/* Check type of socket */
10252419Sjulian		if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL)
10352419Sjulian			continue;
10452419Sjulian		if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA)
10552419Sjulian			continue;
10652419Sjulian
10752419Sjulian		/* Do headline */
10852419Sjulian		if (first) {
10952419Sjulian			printf("Netgraph sockets\n");
11052419Sjulian			if (Aflag)
11152419Sjulian				printf("%-8.8s ", "PCB");
11252419Sjulian			printf("%-5.5s %-6.6s %-6.6s %-14.14s %s\n",
11352419Sjulian			    "Type", "Recv-Q", "Send-Q",
11452419Sjulian			    "Node Address", "#Hooks");
11552419Sjulian			first = 0;
11652419Sjulian		}
11752419Sjulian
11852419Sjulian		/* Show socket */
11952419Sjulian		if (Aflag)
12052419Sjulian			printf("%8lx ", (u_long) this);
121100591Sjdp		printf("%-5.5s %6u %6u ",
12252419Sjulian		    name, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc);
12352419Sjulian
12452419Sjulian		/* Get info on associated node */
125230481Sglebius		if (ngpcb.node_id == 0 || csock == -1)
12652419Sjulian			goto finish;
127230481Sglebius		snprintf(path, sizeof(path), "[%x]:", ngpcb.node_id);
12852419Sjulian		if (NgSendMsg(csock, path,
12952419Sjulian		    NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0)
13052419Sjulian			goto finish;
13152419Sjulian		if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0)
13252419Sjulian			goto finish;
13352419Sjulian
13452419Sjulian		/* Display associated node info */
13552419Sjulian		if (*ni->name != '\0')
13652419Sjulian			snprintf(path, sizeof(path), "%s:", ni->name);
13752419Sjulian		printf("%-14.14s %4d", path, ni->hooks);
13852419Sjulianfinish:
13952419Sjulian		putchar('\n');
14052419Sjulian	}
14152419Sjulian}
14252419Sjulian
143