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