165475bc8SDavid E. O'Brien /*- 24cf49a43SJulian Elischer * Copyright (c) 1996-1999 Whistle Communications, Inc. 34cf49a43SJulian Elischer * All rights reserved. 44cf49a43SJulian Elischer * 54cf49a43SJulian Elischer * Subject to the following obligations and disclaimer of warranty, use and 64cf49a43SJulian Elischer * redistribution of this software, in source or object code forms, with or 74cf49a43SJulian Elischer * without modifications are expressly permitted by Whistle Communications; 84cf49a43SJulian Elischer * provided, however, that: 94cf49a43SJulian Elischer * 1. Any and all reproductions of the source or object code must include the 104cf49a43SJulian Elischer * copyright notice above and the following disclaimer of warranties; and 114cf49a43SJulian Elischer * 2. No rights are granted, in any manner or form, to use Whistle 124cf49a43SJulian Elischer * Communications, Inc. trademarks, including the mark "WHISTLE 134cf49a43SJulian Elischer * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 144cf49a43SJulian Elischer * such appears in the above copyright notice or in the software. 154cf49a43SJulian Elischer * 164cf49a43SJulian Elischer * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 174cf49a43SJulian Elischer * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 184cf49a43SJulian Elischer * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 194cf49a43SJulian Elischer * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 204cf49a43SJulian Elischer * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 214cf49a43SJulian Elischer * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 224cf49a43SJulian Elischer * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 234cf49a43SJulian Elischer * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 244cf49a43SJulian Elischer * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 254cf49a43SJulian Elischer * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 264cf49a43SJulian Elischer * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 274cf49a43SJulian Elischer * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 284cf49a43SJulian Elischer * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 294cf49a43SJulian Elischer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 304cf49a43SJulian Elischer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 314cf49a43SJulian Elischer * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 324cf49a43SJulian Elischer * OF SUCH DAMAGE. 334cf49a43SJulian Elischer */ 344cf49a43SJulian Elischer 356cc6f122SPhilippe Charnier #include <sys/cdefs.h> 366cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$"); 374cf49a43SJulian Elischer 384cf49a43SJulian Elischer #include <sys/param.h> 394cf49a43SJulian Elischer #include <sys/queue.h> 404cf49a43SJulian Elischer #include <sys/socket.h> 41*0e229f34SGleb Smirnoff #define _WANT_SOCKET 424cf49a43SJulian Elischer #include <sys/socketvar.h> 434cf49a43SJulian Elischer #include <sys/protosw.h> 444cf49a43SJulian Elischer #include <sys/linker.h> 454cf49a43SJulian Elischer 464cf49a43SJulian Elischer #include <net/route.h> 474cf49a43SJulian Elischer 48ebdb21f4SMatthew Dillon #include <netgraph.h> 494cf49a43SJulian Elischer #include <netgraph/ng_message.h> 504cf49a43SJulian Elischer #include <netgraph/ng_socket.h> 514cf49a43SJulian Elischer #include <netgraph/ng_socketvar.h> 524cf49a43SJulian Elischer 53821df508SXin LI #include <errno.h> 547b95a1ebSYaroslav Tykhiy #include <stdint.h> 554cf49a43SJulian Elischer #include <stdio.h> 56ade9ccfeSMarcel Moolenaar #include <stdbool.h> 574cf49a43SJulian Elischer #include <string.h> 58821df508SXin LI #include <unistd.h> 594cf49a43SJulian Elischer #include <err.h> 60ade9ccfeSMarcel Moolenaar #include <libxo/xo.h> 614cf49a43SJulian Elischer #include "netstat.h" 624cf49a43SJulian Elischer 634cf49a43SJulian Elischer static int first = 1; 644cf49a43SJulian Elischer static int csock = -1; 654cf49a43SJulian Elischer 664cf49a43SJulian Elischer void 67feda1a43SJohn Baldwin netgraphprotopr(u_long off, const char *name, int af1 __unused, 68feda1a43SJohn Baldwin int proto __unused) 694cf49a43SJulian Elischer { 704cf49a43SJulian Elischer struct ngpcb *this, *next; 714cf49a43SJulian Elischer struct ngpcb ngpcb; 724cf49a43SJulian Elischer struct socket sockb; 734cf49a43SJulian Elischer int debug = 1; 744cf49a43SJulian Elischer 754cf49a43SJulian Elischer /* If symbol not found, try looking in the KLD module */ 764cf49a43SJulian Elischer if (off == 0) { 774cf49a43SJulian Elischer if (debug) 78ade9ccfeSMarcel Moolenaar xo_warnx("Error reading symbols from ng_socket.ko"); 794cf49a43SJulian Elischer return; 804cf49a43SJulian Elischer } 814cf49a43SJulian Elischer 824cf49a43SJulian Elischer /* Get pointer to first socket */ 834cf49a43SJulian Elischer kread(off, (char *)&this, sizeof(this)); 844cf49a43SJulian Elischer 854cf49a43SJulian Elischer /* Get my own socket node */ 864cf49a43SJulian Elischer if (csock == -1) 874cf49a43SJulian Elischer NgMkSockNode(NULL, &csock, NULL); 884cf49a43SJulian Elischer 894cf49a43SJulian Elischer for (; this != NULL; this = next) { 904cf49a43SJulian Elischer u_char rbuf[sizeof(struct ng_mesg) + sizeof(struct nodeinfo)]; 914cf49a43SJulian Elischer struct ng_mesg *resp = (struct ng_mesg *) rbuf; 924cf49a43SJulian Elischer struct nodeinfo *ni = (struct nodeinfo *) resp->data; 934cf49a43SJulian Elischer char path[64]; 944cf49a43SJulian Elischer 954cf49a43SJulian Elischer /* Read in ngpcb structure */ 964cf49a43SJulian Elischer kread((u_long)this, (char *)&ngpcb, sizeof(ngpcb)); 974d51ef63SPoul-Henning Kamp next = LIST_NEXT(&ngpcb, socks); 984cf49a43SJulian Elischer 994cf49a43SJulian Elischer /* Read in socket structure */ 1004cf49a43SJulian Elischer kread((u_long)ngpcb.ng_socket, (char *)&sockb, sizeof(sockb)); 1014cf49a43SJulian Elischer 1024cf49a43SJulian Elischer /* Check type of socket */ 1034cf49a43SJulian Elischer if (strcmp(name, "ctrl") == 0 && ngpcb.type != NG_CONTROL) 1044cf49a43SJulian Elischer continue; 1054cf49a43SJulian Elischer if (strcmp(name, "data") == 0 && ngpcb.type != NG_DATA) 1064cf49a43SJulian Elischer continue; 1074cf49a43SJulian Elischer 1084cf49a43SJulian Elischer /* Do headline */ 1094cf49a43SJulian Elischer if (first) { 110ade9ccfeSMarcel Moolenaar xo_emit("{T:Netgraph sockets}\n"); 1114cf49a43SJulian Elischer if (Aflag) 112ade9ccfeSMarcel Moolenaar xo_emit("{T:/%-8.8s} ", "PCB"); 113ade9ccfeSMarcel Moolenaar xo_emit("{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} " 114ade9ccfeSMarcel Moolenaar "{T:/%-14.14s} {T:/%s}\n", 115ade9ccfeSMarcel Moolenaar "Type", "Recv-Q", "Send-Q", "Node Address", 116ade9ccfeSMarcel Moolenaar "#Hooks"); 1174cf49a43SJulian Elischer first = 0; 1184cf49a43SJulian Elischer } 1194cf49a43SJulian Elischer 1204cf49a43SJulian Elischer /* Show socket */ 1214cf49a43SJulian Elischer if (Aflag) 122ade9ccfeSMarcel Moolenaar xo_emit("{:address/%8lx} ", (u_long) this); 123ade9ccfeSMarcel Moolenaar xo_emit("{t:name/%-5.5s} {:receive-bytes-waiting/%6u} " 124ade9ccfeSMarcel Moolenaar "{:send-byte-waiting/%6u} ", 1250f9d0a73SGleb Smirnoff name, sockb.so_rcv.sb_ccc, sockb.so_snd.sb_ccc); 1264cf49a43SJulian Elischer 1274cf49a43SJulian Elischer /* Get info on associated node */ 1284b2b8a37SGleb Smirnoff if (ngpcb.node_id == 0 || csock == -1) 1294cf49a43SJulian Elischer goto finish; 1304b2b8a37SGleb Smirnoff snprintf(path, sizeof(path), "[%x]:", ngpcb.node_id); 1314cf49a43SJulian Elischer if (NgSendMsg(csock, path, 1324cf49a43SJulian Elischer NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0) 1334cf49a43SJulian Elischer goto finish; 1344cf49a43SJulian Elischer if (NgRecvMsg(csock, resp, sizeof(rbuf), NULL) < 0) 1354cf49a43SJulian Elischer goto finish; 1364cf49a43SJulian Elischer 1374cf49a43SJulian Elischer /* Display associated node info */ 1384cf49a43SJulian Elischer if (*ni->name != '\0') 1394cf49a43SJulian Elischer snprintf(path, sizeof(path), "%s:", ni->name); 140ade9ccfeSMarcel Moolenaar xo_emit("{t:path/%-14.14s} {:hooks/%4d}", path, ni->hooks); 1414cf49a43SJulian Elischer finish: 142ade9ccfeSMarcel Moolenaar xo_emit("\n"); 1434cf49a43SJulian Elischer } 1444cf49a43SJulian Elischer } 1454cf49a43SJulian Elischer 146