1 /* $NetBSD: ip.c,v 1.16 2008/04/10 17:14:25 thorpej Exp $ */
4 * Copyright (c) 1999, 2000 Andrew Doran <ad@NetBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: ip.c,v 1.16 2008/04/10 17:14:25 thorpej Exp $");
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
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>
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)
56 uint64_t i
[IP_NSTATS
];
57 uint64_t u
[UDP_NSTATS
];
66 static enum update update
= UPDATE_TIME
;
67 static struct mystat curstat
;
68 static struct mystat oldstat
;
69 static struct mystat newstat
;
71 static struct nlist namelist
[] = {
72 { .n_name
= "_ipstat" },
73 { .n_name
= "_udpstat" },
81 return (subwin(stdscr
, -1, 0, 5, 0));
99 wmove(wnd
, 0, 0); wclrtoeol(wnd
);
101 LHD(0, "total packets received");
102 LHD(1, " passed to upper layers");
103 LHD(2, " with bad checksums");
104 LHD(3, " too short for header");
105 LHD(4, " too short for data");
106 LHD(5, " with invalid hlen");
107 LHD(6, " with invalid length");
108 LHD(7, " with invalid version");
109 LHD(8, " too large");
110 LHD(9, " option errors");
111 LHD(10, " fragments received");
112 LHD(11, " fragments dropped");
113 LHD(12, " fragments timed out");
114 LHD(13, " packets reassembled ok");
116 LHD(15, "packets forwarded");
117 LHD(16, " fast forwarded");
118 LHD(17, " unreachable dests");
119 LHD(18, " redirects generated");
121 RHD(0, "total packets sent");
122 RHD(1, " generated locally");
123 RHD(2, " output drops");
124 RHD(3, " output fragments generated");
125 RHD(4, " fragmentation failed");
126 RHD(5, " destinations unreachable");
127 RHD(6, " packets output via raw IP");
128 RHD(7, " total UDP packets sent");
130 RHD(9, "total UDP packets received");
131 RHD(10, " too short for header");
132 RHD(11, " invalid checksum");
133 RHD(12, " invalid length");
134 RHD(13, " no socket for dest port");
135 RHD(14, " no socket for broadcast");
136 RHD(15, " socket buffer full");
144 totalout
= curstat
.i
[IP_STAT_FORWARD
] + curstat
.i
[IP_STAT_LOCALOUT
];
146 SHOW(i
[IP_STAT_TOTAL
], 0, 0);
147 mvwprintw(wnd
, 0, 35, "%9llu", (unsigned long long)totalout
);
148 SHOW(i
[IP_STAT_DELIVERED
], 1, 0);
149 SHOW(i
[IP_STAT_BADSUM
], 2, 0);
150 SHOW(i
[IP_STAT_TOOSHORT
], 3, 0);
151 SHOW(i
[IP_STAT_TOOSMALL
], 4, 0);
152 SHOW(i
[IP_STAT_BADHLEN
], 5, 0);
153 SHOW(i
[IP_STAT_BADLEN
], 6, 0);
154 SHOW(i
[IP_STAT_BADVERS
], 7, 0);
155 SHOW(i
[IP_STAT_TOOLONG
], 8, 0);
156 SHOW(i
[IP_STAT_BADOPTIONS
], 9, 0);
158 SHOW(i
[IP_STAT_LOCALOUT
], 1, 35);
159 SHOW(i
[IP_STAT_ODROPPED
], 2, 35);
160 SHOW(i
[IP_STAT_OFRAGMENTS
], 3, 35);
161 SHOW(i
[IP_STAT_CANTFRAG
], 4, 35);
162 SHOW(i
[IP_STAT_NOROUTE
], 5, 35);
163 SHOW(i
[IP_STAT_RAWOUT
], 6, 35);
164 SHOW(u
[UDP_STAT_OPACKETS
], 7, 35);
166 SHOW(i
[IP_STAT_FRAGMENTS
], 10, 0);
167 SHOW(i
[IP_STAT_FRAGDROPPED
], 11, 0);
168 SHOW(i
[IP_STAT_FRAGTIMEOUT
], 12, 0);
169 SHOW(i
[IP_STAT_REASSEMBLED
], 13, 0);
171 SHOW(i
[IP_STAT_FORWARD
], 15, 0);
172 SHOW(i
[IP_STAT_FASTFORWARD
], 16, 0);
173 SHOW(i
[IP_STAT_CANTFORWARD
], 17, 0);
174 SHOW(i
[IP_STAT_REDIRECTSENT
], 18, 0);
176 SHOW(u
[UDP_STAT_IPACKETS
], 9, 35);
177 SHOW(u
[UDP_STAT_HDROPS
], 10, 35);
178 SHOW(u
[UDP_STAT_BADSUM
], 11, 35);
179 SHOW(u
[UDP_STAT_BADLEN
], 12, 35);
180 SHOW(u
[UDP_STAT_NOPORT
], 13, 35);
181 SHOW(u
[UDP_STAT_NOPORTBCAST
], 14, 35);
182 SHOW(u
[UDP_STAT_FULLSOCK
], 15, 35);
190 if (namelist
[0].n_type
== 0) {
191 if (kvm_nlist(kd
, namelist
)) {
195 if ((namelist
[0].n_type
| namelist
[1].n_type
) == 0) {
196 error("No namelist");
211 size
= sizeof(newstat
.i
);
212 if (sysctlbyname("net.inet.ip.stats", newstat
.i
, &size
,
215 size
= sizeof(newstat
.u
);
216 if (sysctlbyname("net.inet.udp.stats", newstat
.u
, &size
,
220 KREAD((void *)namelist
[0].n_value
, newstat
.i
,
222 KREAD((void *)namelist
[1].n_value
, newstat
.u
,
226 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_TOTAL
]);
227 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_DELIVERED
]);
228 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_BADSUM
]);
229 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_TOOSHORT
]);
230 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_TOOSMALL
]);
231 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_BADHLEN
]);
232 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_BADLEN
]);
233 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_BADVERS
]);
234 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_TOOLONG
]);
235 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_BADOPTIONS
]);
236 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_LOCALOUT
]);
237 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_ODROPPED
]);
238 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_OFRAGMENTS
]);
239 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_CANTFRAG
]);
240 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_NOROUTE
]);
241 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_RAWOUT
]);
242 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_FRAGMENTS
]);
243 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_FRAGDROPPED
]);
244 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_FRAGTIMEOUT
]);
245 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_REASSEMBLED
]);
246 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_FORWARD
]);
247 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_FASTFORWARD
]);
248 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_CANTFORWARD
]);
249 ADJINETCTR(curstat
, oldstat
, newstat
, i
[IP_STAT_REDIRECTSENT
]);
250 ADJINETCTR(curstat
, oldstat
, newstat
, u
[UDP_STAT_OPACKETS
]);
251 ADJINETCTR(curstat
, oldstat
, newstat
, u
[UDP_STAT_IPACKETS
]);
252 ADJINETCTR(curstat
, oldstat
, newstat
, u
[UDP_STAT_HDROPS
]);
253 ADJINETCTR(curstat
, oldstat
, newstat
, u
[UDP_STAT_BADSUM
]);
254 ADJINETCTR(curstat
, oldstat
, newstat
, u
[UDP_STAT_BADLEN
]);
255 ADJINETCTR(curstat
, oldstat
, newstat
, u
[UDP_STAT_NOPORT
]);
256 ADJINETCTR(curstat
, oldstat
, newstat
, u
[UDP_STAT_NOPORTBCAST
]);
257 ADJINETCTR(curstat
, oldstat
, newstat
, u
[UDP_STAT_FULLSOCK
]);
259 if (update
== UPDATE_TIME
)
260 memcpy(&oldstat
, &newstat
, sizeof(oldstat
));
267 memset(&oldstat
, 0, sizeof(oldstat
));
268 update
= UPDATE_BOOT
;
275 if (update
!= UPDATE_RUN
) {
276 memcpy(&oldstat
, &newstat
, sizeof(oldstat
));
285 if (update
!= UPDATE_TIME
) {
286 memcpy(&oldstat
, &newstat
, sizeof(oldstat
));
287 update
= UPDATE_TIME
;
295 if (update
== UPDATE_RUN
)
296 memcpy(&oldstat
, &newstat
, sizeof(oldstat
));