Home | History | Annotate | Line # | Download | only in systat
ip.c revision 1.15
      1  1.15   thorpej /*	$NetBSD: ip.c,v 1.15 2008/04/07 06:31:28 thorpej Exp $	*/
      2   1.1        ad 
      3   1.1        ad /*
      4   1.8        ad  * Copyright (c) 1999, 2000 Andrew Doran <ad (at) NetBSD.org>
      5   1.1        ad  * All rights reserved.
      6   1.1        ad  *
      7   1.1        ad  * Redistribution and use in source and binary forms, with or without
      8   1.1        ad  * modification, are permitted provided that the following conditions
      9   1.1        ad  * are met:
     10   1.1        ad  * 1. Redistributions of source code must retain the above copyright
     11   1.1        ad  *    notice, this list of conditions and the following disclaimer.
     12   1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     14   1.1        ad  *    documentation and/or other materials provided with the distribution.
     15   1.1        ad  *
     16   1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17   1.1        ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18   1.1        ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.1        ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20   1.1        ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.1        ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.1        ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.1        ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.1        ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1        ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1        ad  * SUCH DAMAGE.
     27   1.1        ad  *
     28   1.1        ad  */
     29   1.1        ad 
     30   1.1        ad #include <sys/cdefs.h>
     31   1.1        ad #ifndef lint
     32  1.15   thorpej __RCSID("$NetBSD: ip.c,v 1.15 2008/04/07 06:31:28 thorpej Exp $");
     33   1.1        ad #endif /* not lint */
     34   1.1        ad 
     35   1.1        ad #include <sys/param.h>
     36   1.1        ad #include <sys/sysctl.h>
     37   1.1        ad 
     38   1.1        ad #include <netinet/in.h>
     39   1.1        ad #include <netinet/in_systm.h>
     40   1.1        ad #include <netinet/ip.h>
     41   1.1        ad #include <netinet/ip_var.h>
     42   1.1        ad #include <netinet/udp.h>
     43   1.1        ad #include <netinet/udp_var.h>
     44   1.1        ad 
     45   1.1        ad #include <string.h>
     46   1.5        ad 
     47   1.1        ad #include "systat.h"
     48   1.1        ad #include "extern.h"
     49   1.1        ad 
     50   1.1        ad #define LHD(row, str)		mvwprintw(wnd, row, 10, str)
     51   1.1        ad #define RHD(row, str)		mvwprintw(wnd, row, 45, str);
     52   1.4    itojun #define SHOW(stat, row, col) \
     53   1.4    itojun     mvwprintw(wnd, row, col, "%9llu", (unsigned long long)curstat.stat)
     54   1.1        ad 
     55   1.1        ad struct mystat {
     56  1.15   thorpej 	uint64_t i[IP_NSTATS];
     57  1.14   thorpej 	uint64_t u[UDP_NSTATS];
     58   1.1        ad };
     59   1.1        ad 
     60   1.8        ad enum update {
     61   1.8        ad 	UPDATE_TIME,
     62   1.8        ad 	UPDATE_BOOT,
     63   1.8        ad 	UPDATE_RUN,
     64   1.8        ad };
     65   1.8        ad 
     66   1.8        ad static enum update update = UPDATE_TIME;
     67   1.1        ad static struct mystat curstat;
     68   1.8        ad static struct mystat oldstat;
     69   1.8        ad static struct mystat newstat;
     70   1.1        ad 
     71   1.1        ad static struct nlist namelist[] = {
     72  1.13  christos 	{ .n_name = "_ipstat" },
     73  1.13  christos 	{ .n_name = "_udpstat" },
     74  1.13  christos 	{ .n_name = NULL }
     75   1.1        ad };
     76   1.1        ad 
     77   1.1        ad WINDOW *
     78   1.1        ad openip(void)
     79   1.1        ad {
     80   1.1        ad 
     81  1.11       dsl 	return (subwin(stdscr, -1, 0, 5, 0));
     82   1.1        ad }
     83   1.1        ad 
     84   1.1        ad void
     85   1.8        ad closeip(WINDOW *w)
     86   1.1        ad {
     87   1.1        ad 
     88   1.1        ad 	if (w != NULL) {
     89   1.1        ad 		wclear(w);
     90   1.1        ad 		wrefresh(w);
     91   1.1        ad 		delwin(w);
     92   1.1        ad 	}
     93   1.1        ad }
     94   1.1        ad 
     95   1.1        ad void
     96   1.1        ad labelip(void)
     97   1.1        ad {
     98   1.2        ad 
     99   1.1        ad 	wmove(wnd, 0, 0); wclrtoeol(wnd);
    100   1.1        ad 
    101   1.1        ad 	LHD(0,  "total packets received");
    102   1.1        ad 	LHD(1,  "  passed to upper layers");
    103   1.1        ad 	LHD(2,  "  with bad checksums");
    104   1.1        ad 	LHD(3,  "  too short for header");
    105   1.1        ad 	LHD(4,  "  too short for data");
    106   1.1        ad 	LHD(5,  "  with invalid hlen");
    107   1.1        ad 	LHD(6,  "  with invalid length");
    108   1.1        ad 	LHD(7,  "  with invalid version");
    109   1.1        ad 	LHD(8,  "  too large");
    110   1.1        ad 	LHD(9,  "  option errors");
    111   1.1        ad 	LHD(10, "  fragments received");
    112   1.1        ad 	LHD(11, "  fragments dropped");
    113   1.1        ad 	LHD(12, "  fragments timed out");
    114   1.1        ad 	LHD(13, "  packets reassembled ok");
    115   1.1        ad 
    116   1.1        ad 	LHD(15, "packets forwarded");
    117   1.1        ad 	LHD(16, "  fast forwarded");
    118   1.1        ad 	LHD(17, "  unreachable dests");
    119   1.1        ad 	LHD(18, "  redirects generated");
    120   1.1        ad 
    121   1.1        ad 	RHD(0,  "total packets sent");
    122   1.1        ad 	RHD(1,  "  generated locally");
    123   1.1        ad 	RHD(2,  "  output drops");
    124   1.1        ad 	RHD(3,  "  output fragments generated");
    125   1.1        ad 	RHD(4,  "  fragmentation failed");
    126   1.1        ad 	RHD(5,  "  destinations unreachable");
    127   1.1        ad 	RHD(6,  "  packets output via raw IP");
    128   1.1        ad 	RHD(7,  "  total UDP packets sent");
    129   1.1        ad 
    130  1.10       wiz 	RHD(9, "total UDP packets received");
    131   1.1        ad 	RHD(10, "  too short for header");
    132   1.1        ad 	RHD(11, "  invalid checksum");
    133   1.1        ad 	RHD(12, "  invalid length");
    134   1.1        ad 	RHD(13, "  no socket for dest port");
    135   1.1        ad 	RHD(14, "  no socket for broadcast");
    136   1.1        ad 	RHD(15, "  socket buffer full");
    137   1.1        ad }
    138   1.1        ad 
    139   1.1        ad void
    140   1.1        ad showip(void)
    141   1.1        ad {
    142   1.3   msaitoh 	u_quad_t totalout;
    143   1.1        ad 
    144  1.15   thorpej 	totalout = curstat.i[IP_STAT_FORWARD] + curstat.i[IP_STAT_LOCALOUT];
    145   1.1        ad 
    146  1.15   thorpej 	SHOW(i[IP_STAT_TOTAL], 0, 0);
    147   1.6   thorpej 	mvwprintw(wnd, 0, 35, "%9llu", (unsigned long long)totalout);
    148  1.15   thorpej 	SHOW(i[IP_STAT_DELIVERED], 1, 0);
    149  1.15   thorpej 	SHOW(i[IP_STAT_BADSUM], 2, 0);
    150  1.15   thorpej 	SHOW(i[IP_STAT_TOOSHORT], 3, 0);
    151  1.15   thorpej 	SHOW(i[IP_STAT_TOOSMALL], 4, 0);
    152  1.15   thorpej 	SHOW(i[IP_STAT_BADHLEN], 5, 0);
    153  1.15   thorpej 	SHOW(i[IP_STAT_BADLEN], 6, 0);
    154  1.15   thorpej 	SHOW(i[IP_STAT_BADVERS], 7, 0);
    155  1.15   thorpej 	SHOW(i[IP_STAT_TOOLONG], 8, 0);
    156  1.15   thorpej 	SHOW(i[IP_STAT_BADOPTIONS], 9, 0);
    157  1.15   thorpej 
    158  1.15   thorpej 	SHOW(i[IP_STAT_LOCALOUT], 1, 35);
    159  1.15   thorpej 	SHOW(i[IP_STAT_ODROPPED], 2, 35);
    160  1.15   thorpej 	SHOW(i[IP_STAT_OFRAGMENTS], 3, 35);
    161  1.15   thorpej 	SHOW(i[IP_STAT_CANTFRAG], 4, 35);
    162  1.15   thorpej 	SHOW(i[IP_STAT_NOROUTE], 5, 35);
    163  1.15   thorpej 	SHOW(i[IP_STAT_RAWOUT], 6, 35);
    164  1.14   thorpej 	SHOW(u[UDP_STAT_OPACKETS], 7, 35);
    165   1.1        ad 
    166  1.15   thorpej 	SHOW(i[IP_STAT_FRAGMENTS], 10, 0);
    167  1.15   thorpej 	SHOW(i[IP_STAT_FRAGDROPPED], 11, 0);
    168  1.15   thorpej 	SHOW(i[IP_STAT_FRAGTIMEOUT], 12, 0);
    169  1.15   thorpej 	SHOW(i[IP_STAT_REASSEMBLED], 13, 0);
    170  1.15   thorpej 
    171  1.15   thorpej 	SHOW(i[IP_STAT_FORWARD], 15, 0);
    172  1.15   thorpej 	SHOW(i[IP_STAT_FASTFORWARD], 16, 0);
    173  1.15   thorpej 	SHOW(i[IP_STAT_CANTFORWARD], 17, 0);
    174  1.15   thorpej 	SHOW(i[IP_STAT_REDIRECTSENT], 18, 0);
    175   1.1        ad 
    176  1.14   thorpej 	SHOW(u[UDP_STAT_IPACKETS], 9, 35);
    177  1.14   thorpej 	SHOW(u[UDP_STAT_HDROPS], 10, 35);
    178  1.14   thorpej 	SHOW(u[UDP_STAT_BADSUM], 11, 35);
    179  1.14   thorpej 	SHOW(u[UDP_STAT_BADLEN], 12, 35);
    180  1.14   thorpej 	SHOW(u[UDP_STAT_NOPORT], 13, 35);
    181  1.14   thorpej 	SHOW(u[UDP_STAT_NOPORTBCAST], 14, 35);
    182  1.14   thorpej 	SHOW(u[UDP_STAT_FULLSOCK], 15, 35);
    183   1.1        ad }
    184   1.1        ad 
    185   1.1        ad int
    186   1.1        ad initip(void)
    187   1.1        ad {
    188   1.2        ad 
    189   1.1        ad 	if (namelist[0].n_type == 0) {
    190   1.1        ad 		if (kvm_nlist(kd, namelist)) {
    191   1.1        ad 			nlisterr(namelist);
    192   1.1        ad 			return(0);
    193   1.1        ad 		}
    194   1.1        ad 		if ((namelist[0].n_type | namelist[1].n_type) == 0) {
    195   1.1        ad 			error("No namelist");
    196   1.1        ad 			return(0);
    197   1.1        ad 		}
    198   1.1        ad 	}
    199   1.1        ad 	return 1;
    200   1.1        ad }
    201   1.1        ad 
    202   1.1        ad void
    203   1.1        ad fetchip(void)
    204   1.1        ad {
    205   1.2        ad 
    206  1.15   thorpej 	KREAD((void *)namelist[0].n_value, newstat.i,
    207   1.8        ad 	    sizeof(newstat.i));
    208  1.14   thorpej 	KREAD((void *)namelist[1].n_value, newstat.u,
    209   1.8        ad 	    sizeof(newstat.u));
    210   1.8        ad 
    211  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_TOTAL]);
    212  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_DELIVERED]);
    213  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_BADSUM]);
    214  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_TOOSHORT]);
    215  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_TOOSMALL]);
    216  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_BADHLEN]);
    217  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_BADLEN]);
    218  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_BADVERS]);
    219  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_TOOLONG]);
    220  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_BADOPTIONS]);
    221  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_LOCALOUT]);
    222  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_ODROPPED]);
    223  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_OFRAGMENTS]);
    224  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_CANTFRAG]);
    225  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_NOROUTE]);
    226  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_RAWOUT]);
    227  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_FRAGMENTS]);
    228  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_FRAGDROPPED]);
    229  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_FRAGTIMEOUT]);
    230  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_REASSEMBLED]);
    231  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_FORWARD]);
    232  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_FASTFORWARD]);
    233  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_CANTFORWARD]);
    234  1.15   thorpej 	ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_REDIRECTSENT]);
    235  1.14   thorpej 	ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_OPACKETS]);
    236  1.14   thorpej 	ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_IPACKETS]);
    237  1.14   thorpej 	ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_HDROPS]);
    238  1.14   thorpej 	ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_BADSUM]);
    239  1.14   thorpej 	ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_BADLEN]);
    240  1.14   thorpej 	ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_NOPORT]);
    241  1.14   thorpej 	ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_NOPORTBCAST]);
    242  1.14   thorpej 	ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_FULLSOCK]);
    243   1.8        ad 
    244   1.8        ad 	if (update == UPDATE_TIME)
    245   1.8        ad 		memcpy(&oldstat, &newstat, sizeof(oldstat));
    246   1.8        ad }
    247   1.8        ad 
    248   1.8        ad void
    249   1.8        ad ip_boot(char *args)
    250   1.8        ad {
    251   1.8        ad 
    252   1.8        ad 	memset(&oldstat, 0, sizeof(oldstat));
    253   1.8        ad 	update = UPDATE_BOOT;
    254   1.8        ad }
    255   1.8        ad 
    256   1.8        ad void
    257   1.8        ad ip_run(char *args)
    258   1.8        ad {
    259   1.8        ad 
    260   1.8        ad 	if (update != UPDATE_RUN) {
    261   1.8        ad 		memcpy(&oldstat, &newstat, sizeof(oldstat));
    262   1.8        ad 		update = UPDATE_RUN;
    263   1.8        ad 	}
    264   1.8        ad }
    265   1.8        ad 
    266   1.8        ad void
    267   1.8        ad ip_time(char *args)
    268   1.8        ad {
    269   1.8        ad 
    270   1.8        ad 	if (update != UPDATE_TIME) {
    271   1.8        ad 		memcpy(&oldstat, &newstat, sizeof(oldstat));
    272   1.8        ad 		update = UPDATE_TIME;
    273   1.8        ad 	}
    274   1.8        ad }
    275   1.8        ad 
    276   1.8        ad void
    277   1.8        ad ip_zero(char *args)
    278   1.8        ad {
    279   1.8        ad 
    280   1.8        ad 	if (update == UPDATE_RUN)
    281   1.8        ad 		memcpy(&oldstat, &newstat, sizeof(oldstat));
    282   1.1        ad }
    283