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