1 /*        $NetBSD: ip.c,v 1.18 2014/06/03 22:22:41 joerg Exp $        */
2 
3 /*
4  * Copyright (c) 1999, 2000 Andrew Doran <ad@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.18 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_var.h>
42 #include <netinet/udp.h>
43 #include <netinet/udp_var.h>
44 
45 #include <string.h>
46 
47 #include "systat.h"
48 #include "extern.h"
49 
50 #define LHD(row, str)                   mvwprintw(wnd, row, 10, str)
51 #define RHD(row, str)                   mvwprintw(wnd, row, 45, str);
52 #define SHOW(stat, row, col) \
53     mvwprintw(wnd, row, col, "%9llu", (unsigned long long)curstat.stat)
54 
55 struct mystat {
56           uint64_t i[IP_NSTATS];
57           uint64_t u[UDP_NSTATS];
58 };
59 
60 enum update {
61           UPDATE_TIME,
62           UPDATE_BOOT,
63           UPDATE_RUN,
64 };
65 
66 static enum update update = UPDATE_TIME;
67 static struct mystat curstat;
68 static struct mystat oldstat;
69 static struct mystat newstat;
70 
71 WINDOW *
openip(void)72 openip(void)
73 {
74 
75           return (subwin(stdscr, -1, 0, 5, 0));
76 }
77 
78 void
closeip(WINDOW * w)79 closeip(WINDOW *w)
80 {
81 
82           if (w != NULL) {
83                     wclear(w);
84                     wrefresh(w);
85                     delwin(w);
86           }
87 }
88 
89 void
labelip(void)90 labelip(void)
91 {
92 
93           wmove(wnd, 0, 0); wclrtoeol(wnd);
94 
95           LHD(0,  "total packets received");
96           LHD(1,  "  passed to upper layers");
97           LHD(2,  "  with bad checksums");
98           LHD(3,  "  too short for header");
99           LHD(4,  "  too short for data");
100           LHD(5,  "  with invalid hlen");
101           LHD(6,  "  with invalid length");
102           LHD(7,  "  with invalid version");
103           LHD(8,  "  too large");
104           LHD(9,  "  option errors");
105           LHD(10, "  fragments received");
106           LHD(11, "  fragments dropped");
107           LHD(12, "  fragments timed out");
108           LHD(13, "  packets reassembled ok");
109 
110           LHD(15, "packets forwarded");
111           LHD(16, "  fast forwarded");
112           LHD(17, "  unreachable dests");
113           LHD(18, "  redirects generated");
114 
115           RHD(0,  "total packets sent");
116           RHD(1,  "  generated locally");
117           RHD(2,  "  output drops");
118           RHD(3,  "  output fragments generated");
119           RHD(4,  "  fragmentation failed");
120           RHD(5,  "  destinations unreachable");
121           RHD(6,  "  packets output via raw IP");
122           RHD(7,  "  total UDP packets sent");
123 
124           RHD(9, "total UDP packets received");
125           RHD(10, "  too short for header");
126           RHD(11, "  invalid checksum");
127           RHD(12, "  invalid length");
128           RHD(13, "  no socket for dest port");
129           RHD(14, "  no socket for broadcast");
130           RHD(15, "  socket buffer full");
131 }
132 
133 void
showip(void)134 showip(void)
135 {
136           u_quad_t totalout;
137 
138           totalout = curstat.i[IP_STAT_FORWARD] + curstat.i[IP_STAT_LOCALOUT];
139 
140           SHOW(i[IP_STAT_TOTAL], 0, 0);
141           mvwprintw(wnd, 0, 35, "%9llu", (unsigned long long)totalout);
142           SHOW(i[IP_STAT_DELIVERED], 1, 0);
143           SHOW(i[IP_STAT_BADSUM], 2, 0);
144           SHOW(i[IP_STAT_TOOSHORT], 3, 0);
145           SHOW(i[IP_STAT_TOOSMALL], 4, 0);
146           SHOW(i[IP_STAT_BADHLEN], 5, 0);
147           SHOW(i[IP_STAT_BADLEN], 6, 0);
148           SHOW(i[IP_STAT_BADVERS], 7, 0);
149           SHOW(i[IP_STAT_TOOLONG], 8, 0);
150           SHOW(i[IP_STAT_BADOPTIONS], 9, 0);
151 
152           SHOW(i[IP_STAT_LOCALOUT], 1, 35);
153           SHOW(i[IP_STAT_ODROPPED], 2, 35);
154           SHOW(i[IP_STAT_OFRAGMENTS], 3, 35);
155           SHOW(i[IP_STAT_CANTFRAG], 4, 35);
156           SHOW(i[IP_STAT_NOROUTE], 5, 35);
157           SHOW(i[IP_STAT_RAWOUT], 6, 35);
158           SHOW(u[UDP_STAT_OPACKETS], 7, 35);
159 
160           SHOW(i[IP_STAT_FRAGMENTS], 10, 0);
161           SHOW(i[IP_STAT_FRAGDROPPED], 11, 0);
162           SHOW(i[IP_STAT_FRAGTIMEOUT], 12, 0);
163           SHOW(i[IP_STAT_REASSEMBLED], 13, 0);
164 
165           SHOW(i[IP_STAT_FORWARD], 15, 0);
166           SHOW(i[IP_STAT_FASTFORWARD], 16, 0);
167           SHOW(i[IP_STAT_CANTFORWARD], 17, 0);
168           SHOW(i[IP_STAT_REDIRECTSENT], 18, 0);
169 
170           SHOW(u[UDP_STAT_IPACKETS], 9, 35);
171           SHOW(u[UDP_STAT_HDROPS], 10, 35);
172           SHOW(u[UDP_STAT_BADSUM], 11, 35);
173           SHOW(u[UDP_STAT_BADLEN], 12, 35);
174           SHOW(u[UDP_STAT_NOPORT], 13, 35);
175           SHOW(u[UDP_STAT_NOPORTBCAST], 14, 35);
176           SHOW(u[UDP_STAT_FULLSOCK], 15, 35);
177 }
178 
179 int
initip(void)180 initip(void)
181 {
182 
183           return 1;
184 }
185 
186 void
fetchip(void)187 fetchip(void)
188 {
189           size_t size;
190 
191           size = sizeof(newstat.i);
192           if (sysctlbyname("net.inet.ip.stats", newstat.i, &size, NULL, 0) == -1)
193                     return;
194           size = sizeof(newstat.u);
195           if (sysctlbyname("net.inet.udp.stats", newstat.u, &size, NULL, 0) == -1)
196                     return;
197 
198           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_TOTAL]);
199           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_DELIVERED]);
200           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_BADSUM]);
201           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_TOOSHORT]);
202           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_TOOSMALL]);
203           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_BADHLEN]);
204           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_BADLEN]);
205           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_BADVERS]);
206           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_TOOLONG]);
207           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_BADOPTIONS]);
208           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_LOCALOUT]);
209           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_ODROPPED]);
210           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_OFRAGMENTS]);
211           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_CANTFRAG]);
212           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_NOROUTE]);
213           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_RAWOUT]);
214           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_FRAGMENTS]);
215           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_FRAGDROPPED]);
216           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_FRAGTIMEOUT]);
217           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_REASSEMBLED]);
218           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_FORWARD]);
219           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_FASTFORWARD]);
220           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_CANTFORWARD]);
221           ADJINETCTR(curstat, oldstat, newstat, i[IP_STAT_REDIRECTSENT]);
222           ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_OPACKETS]);
223           ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_IPACKETS]);
224           ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_HDROPS]);
225           ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_BADSUM]);
226           ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_BADLEN]);
227           ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_NOPORT]);
228           ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_NOPORTBCAST]);
229           ADJINETCTR(curstat, oldstat, newstat, u[UDP_STAT_FULLSOCK]);
230 
231           if (update == UPDATE_TIME)
232                     memcpy(&oldstat, &newstat, sizeof(oldstat));
233 }
234 
235 void
ip_boot(char * args)236 ip_boot(char *args)
237 {
238 
239           memset(&oldstat, 0, sizeof(oldstat));
240           update = UPDATE_BOOT;
241 }
242 
243 void
ip_run(char * args)244 ip_run(char *args)
245 {
246 
247           if (update != UPDATE_RUN) {
248                     memcpy(&oldstat, &newstat, sizeof(oldstat));
249                     update = UPDATE_RUN;
250           }
251 }
252 
253 void
ip_time(char * args)254 ip_time(char *args)
255 {
256 
257           if (update != UPDATE_TIME) {
258                     memcpy(&oldstat, &newstat, sizeof(oldstat));
259                     update = UPDATE_TIME;
260           }
261 }
262 
263 void
ip_zero(char * args)264 ip_zero(char *args)
265 {
266 
267           if (update == UPDATE_RUN)
268                     memcpy(&oldstat, &newstat, sizeof(oldstat));
269 }
270