netgraph.c revision 279122
1200185Sed/*- 2200185Sed * Copyright (c) 1996-1999 Whistle Communications, Inc. 3200185Sed * All rights reserved. 4200185Sed * 5200185Sed * Subject to the following obligations and disclaimer of warranty, use and 6200185Sed * redistribution of this software, in source or object code forms, with or 7200185Sed * without modifications are expressly permitted by Whistle Communications; 8200185Sed * provided, however, that: 9200185Sed * 1. Any and all reproductions of the source or object code must include the 10200185Sed * copyright notice above and the following disclaimer of warranties; and 11200185Sed * 2. No rights are granted, in any manner or form, to use Whistle 12200185Sed * Communications, Inc. trademarks, including the mark "WHISTLE 13200185Sed * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 14200185Sed * such appears in the above copyright notice or in the software. 15200185Sed * 16200185Sed * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 17200185Sed * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 18200185Sed * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 19200185Sed * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 20200185Sed * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 21200185Sed * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 22200185Sed * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 23200185Sed * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 24200185Sed * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 25200185Sed * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 26200185Sed * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 27200185Sed * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 28200185Sed * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 29200185Sed * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30200185Sed * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31200185Sed * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 32200185Sed * OF SUCH DAMAGE. 33200185Sed */ 34200185Sed 35200185Sed#include <sys/cdefs.h> 36200185Sed__FBSDID("$FreeBSD: head/usr.bin/netstat/netgraph.c 279122 2015-02-21 23:47:20Z marcel $"); 37200185Sed 38200185Sed#include <sys/param.h> 39200185Sed#include <sys/queue.h> 40200185Sed#include <sys/socket.h> 41200185Sed#include <sys/socketvar.h> 42200185Sed#include <sys/protosw.h> 43200185Sed#include <sys/linker.h> 44200185Sed 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 <stdbool.h> 57#include <string.h> 58#include <unistd.h> 59#include <err.h> 60#include <libxo/xo.h> 61#include "netstat.h" 62 63static int first = 1; 64static int csock = -1; 65 66void 67netgraphprotopr(u_long off, const char *name, int af1 __unused, 68 int proto __unused) 69{ 70 struct ngpcb *this, *next; 71 struct ngpcb ngpcb; 72 struct socket sockb; 73 int debug = 1; 74 75 /* If symbol not found, try looking in the KLD module */ 76 if (off == 0) { 77 if (debug) 78 xo_warnx("Error reading symbols from ng_socket.ko"); 79 return; 80 } 81 82 /* Get pointer to first socket */ 83 kread(off, (char *)&this, sizeof(this)); 84 85 /* Get my own socket node */ 86 if (csock == -1) 87 NgMkSockNode(NULL, &csock, NULL); 88 89 for (; this != NULL; this = next) { 90 u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)]; 91 struct ng_mesg *resp = (struct ng_mesg *) rbuf; 92 struct nodeinfo *ni = (struct nodeinfo *) resp->data; 93 char path[64]; 94 95 /* Read in ngpcb structure */ 96 kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb)); 97 next = LIST_NEXT(&ngpcb, socks); 98 99 /* Read in socket structure */ 100 kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb)); 101 102 /* Check type of socket */ 103 if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL) 104 continue; 105 if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA) 106 continue; 107 108 /* Do headline */ 109 if (first) { 110 xo_emit("{T:Netgraph sockets}\n"); 111 if (Aflag) 112 xo_emit("{T:/%-8.8s} ", "PCB"); 113 xo_emit("{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} " 114 "{T:/%-14.14s} {T:/%s}\n", 115 "Type", "Recv-Q", "Send-Q", "Node Address", 116 "#Hooks"); 117 first = 0; 118 } 119 120 /* Show socket */ 121 if (Aflag) 122 xo_emit("{:address/%8lx} ", (u_long) this); 123 xo_emit("{t:name/%-5.5s} {:receive-bytes-waiting/%6u} " 124 "{:send-byte-waiting/%6u} ", 125 name, sockb.so_rcv.sb_ccc, sockb.so_snd.sb_ccc); 126 127 /* Get info on associated node */ 128 if (ngpcb.node_id == 0 || csock == -1) 129 goto finish; 130 snprintf(path, sizeof(path), "[%x]:", ngpcb.node_id); 131 if (NgSendMsg(csock, path, 132 NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0) 133 goto finish; 134 if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0) 135 goto finish; 136 137 /* Display associated node info */ 138 if (*ni->name != '\0') 139 snprintf(path, sizeof(path), "%s:", ni->name); 140 xo_emit("{t:path/%-14.14s} {:hooks/%4d}", path, ni->hooks); 141finish: 142 xo_emit("\n"); 143 } 144} 145 146