Home | History | Annotate | Line # | Download | only in systat
      1 /*	$NetBSD: icmp.c,v 1.13 2014/06/03 22:22:41 joerg 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: icmp.c,v 1.13 2014/06/03 22:22:41 joerg Exp $");
     33 #endif /* not lint */
     34 
     35 #include <sys/param.h>
     36 #include <sys/sysctl.h>
     37 
     38 #include <netinet/in.h>
     39 #include <netinet/in_systm.h>
     40 #include <netinet/ip.h>
     41 #include <netinet/ip_icmp.h>
     42 #include <netinet/icmp_var.h>
     43 
     44 #include <string.h>
     45 
     46 #include "systat.h"
     47 #include "extern.h"
     48 
     49 #define LHD(row, str) mvwprintw(wnd, row, 10, str)
     50 #define RHD(row, str) mvwprintw(wnd, row, 45, str);
     51 #define BD(row, str) LHD(row, str); RHD(row, str)
     52 #define SHOW(stat, row, col) \
     53     mvwprintw(wnd, row, col, "%9llu", (unsigned long long)curstat[stat])
     54 #define SHOW2(type, row) SHOW(ICMP_STAT_INHIST + type, row, 0); \
     55     SHOW(ICMP_STAT_OUTHIST + type, row, 35)
     56 
     57 enum update {
     58 	UPDATE_TIME,
     59 	UPDATE_BOOT,
     60 	UPDATE_RUN,
     61 };
     62 
     63 static enum update update = UPDATE_TIME;
     64 static uint64_t curstat[ICMP_NSTATS];
     65 static uint64_t newstat[ICMP_NSTATS];
     66 static uint64_t oldstat[ICMP_NSTATS];
     67 
     68 WINDOW *
     69 openicmp(void)
     70 {
     71 
     72 	return (subwin(stdscr, -1, 0, 5, 0));
     73 }
     74 
     75 void
     76 closeicmp(WINDOW *w)
     77 {
     78 
     79 	if (w != NULL) {
     80 		wclear(w);
     81 		wrefresh(w);
     82 		delwin(w);
     83 	}
     84 }
     85 
     86 void
     87 labelicmp(void)
     88 {
     89 
     90 	wmove(wnd, 0, 0); wclrtoeol(wnd);
     91 
     92 	mvwprintw(wnd, 1, 0,  "------------ ICMP input -----------");
     93 	mvwprintw(wnd, 1, 36, "------------- ICMP output ---------------");
     94 
     95 	mvwprintw(wnd, 8, 0,  "---------- Input histogram --------");
     96 	mvwprintw(wnd, 8, 36, "---------- Output histogram -------------");
     97 
     98 	LHD(3, "with bad code");
     99 	LHD(4, "with bad length");
    100 	LHD(5, "with bad checksum");
    101 	LHD(6, "with insufficient data");
    102 
    103 	RHD(3, "errors generated");
    104 	RHD(4, "suppressed - original too short");
    105 	RHD(5, "suppressed - original was ICMP");
    106 	RHD(6, "responses sent");
    107 
    108 	BD(2, "total messages");
    109 	BD(9, "echo response");
    110 	BD(10, "echo request");
    111 	BD(11, "destination unreachable");
    112 	BD(12, "redirect");
    113 	BD(13, "time-to-live exceeded");
    114 	BD(14, "parameter problem");
    115 	LHD(15, "router advertisement");
    116 	RHD(15, "router solicitation");
    117 }
    118 
    119 void
    120 showicmp(void)
    121 {
    122 	u_long tin, tout;
    123 	int i;
    124 
    125 	for (i = tin = tout = 0; i <= ICMP_MAXTYPE; i++) {
    126 		tin += curstat[ICMP_STAT_INHIST + i];
    127 		tout += curstat[ICMP_STAT_OUTHIST + i];
    128 	}
    129 
    130 	tin += curstat[ICMP_STAT_BADCODE] + curstat[ICMP_STAT_BADLEN] +
    131 	    curstat[ICMP_STAT_CHECKSUM] + curstat[ICMP_STAT_TOOSHORT];
    132 	mvwprintw(wnd, 2, 0, "%9lu", tin);
    133 	mvwprintw(wnd, 2, 35, "%9lu", tout);
    134 
    135 	SHOW(ICMP_STAT_BADCODE, 3, 0);
    136 	SHOW(ICMP_STAT_BADLEN, 4, 0);
    137 	SHOW(ICMP_STAT_CHECKSUM, 5, 0);
    138 	SHOW(ICMP_STAT_TOOSHORT, 6, 0);
    139 	SHOW(ICMP_STAT_ERROR, 3, 35);
    140 	SHOW(ICMP_STAT_OLDSHORT, 4, 35);
    141 	SHOW(ICMP_STAT_OLDICMP, 5, 35);
    142 	SHOW(ICMP_STAT_REFLECT, 6, 35);
    143 
    144 	SHOW2(ICMP_ECHOREPLY, 9);
    145 	SHOW2(ICMP_ECHO, 10);
    146 	SHOW2(ICMP_UNREACH, 11);
    147 	SHOW2(ICMP_REDIRECT, 12);
    148 	SHOW2(ICMP_TIMXCEED, 13);
    149 	SHOW2(ICMP_PARAMPROB, 14);
    150 	SHOW(ICMP_STAT_INHIST + ICMP_ROUTERADVERT, 15, 0);
    151 	SHOW(ICMP_STAT_OUTHIST + ICMP_ROUTERSOLICIT, 15, 35);
    152 }
    153 
    154 int
    155 initicmp(void)
    156 {
    157 
    158 	return (1);
    159 }
    160 
    161 void
    162 fetchicmp(void)
    163 {
    164 	size_t i, size = sizeof(newstat);
    165 
    166 	if (sysctlbyname("net.inet.icmp.stats", newstat, &size, NULL, 0) == -1)
    167 		return;
    168 
    169 	xADJINETCTR(curstat, oldstat, newstat, ICMP_STAT_BADCODE);
    170 	xADJINETCTR(curstat, oldstat, newstat, ICMP_STAT_BADLEN);
    171 	xADJINETCTR(curstat, oldstat, newstat, ICMP_STAT_CHECKSUM);
    172 	xADJINETCTR(curstat, oldstat, newstat, ICMP_STAT_TOOSHORT);
    173 	xADJINETCTR(curstat, oldstat, newstat, ICMP_STAT_ERROR);
    174 	xADJINETCTR(curstat, oldstat, newstat, ICMP_STAT_OLDSHORT);
    175 	xADJINETCTR(curstat, oldstat, newstat, ICMP_STAT_OLDICMP);
    176 	xADJINETCTR(curstat, oldstat, newstat, ICMP_STAT_REFLECT);
    177 
    178 	for (i = 0; i <= ICMP_MAXTYPE; i++) {
    179 		xADJINETCTR(curstat, oldstat, newstat, ICMP_STAT_INHIST + i);
    180 		xADJINETCTR(curstat, oldstat, newstat, ICMP_STAT_OUTHIST + i);
    181 	}
    182 
    183 	if (update == UPDATE_TIME)
    184 		memcpy(oldstat, newstat, sizeof(oldstat));
    185 }
    186 
    187 void
    188 icmp_boot(char *args)
    189 {
    190 
    191 	memset(oldstat, 0, sizeof(oldstat));
    192 	update = UPDATE_BOOT;
    193 }
    194 
    195 void
    196 icmp_run(char *args)
    197 {
    198 
    199 	if (update != UPDATE_RUN) {
    200 		memcpy(oldstat, newstat, sizeof(oldstat));
    201 		update = UPDATE_RUN;
    202 	}
    203 }
    204 
    205 void
    206 icmp_time(char *args)
    207 {
    208 
    209 	if (update != UPDATE_TIME) {
    210 		memcpy(oldstat, newstat, sizeof(oldstat));
    211 		update = UPDATE_TIME;
    212 	}
    213 }
    214 
    215 void
    216 icmp_zero(char *args)
    217 {
    218 
    219 	if (update == UPDATE_RUN)
    220 		memcpy(oldstat, newstat, sizeof(oldstat));
    221 }
    222