1 /* $NetBSD: ip.c,v 1.10 2001/06/12 15:17:29 wiz 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.10 2001/06/12 15:17:29 wiz 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 <kvm.h> 46 #include <string.h> 47 48 #include "systat.h" 49 #include "extern.h" 50 51 #define LHD(row, str) mvwprintw(wnd, row, 10, str) 52 #define RHD(row, str) mvwprintw(wnd, row, 45, str); 53 #define SHOW(stat, row, col) \ 54 mvwprintw(wnd, row, col, "%9llu", (unsigned long long)curstat.stat) 55 56 struct mystat { 57 struct ipstat i; 58 struct udpstat u; 59 }; 60 61 enum update { 62 UPDATE_TIME, 63 UPDATE_BOOT, 64 UPDATE_RUN, 65 }; 66 67 static enum update update = UPDATE_TIME; 68 static struct mystat curstat; 69 static struct mystat oldstat; 70 static struct mystat newstat; 71 72 static struct nlist namelist[] = { 73 { "_ipstat" }, 74 { "_udpstat" }, 75 { "" } 76 }; 77 78 WINDOW * 79 openip(void) 80 { 81 82 return (subwin(stdscr, LINES-5-1, 0, 5, 0)); 83 } 84 85 void 86 closeip(WINDOW *w) 87 { 88 89 if (w != NULL) { 90 wclear(w); 91 wrefresh(w); 92 delwin(w); 93 } 94 } 95 96 void 97 labelip(void) 98 { 99 100 wmove(wnd, 0, 0); wclrtoeol(wnd); 101 102 LHD(0, "total packets received"); 103 LHD(1, " passed to upper layers"); 104 LHD(2, " with bad checksums"); 105 LHD(3, " too short for header"); 106 LHD(4, " too short for data"); 107 LHD(5, " with invalid hlen"); 108 LHD(6, " with invalid length"); 109 LHD(7, " with invalid version"); 110 LHD(8, " too large"); 111 LHD(9, " option errors"); 112 LHD(10, " fragments received"); 113 LHD(11, " fragments dropped"); 114 LHD(12, " fragments timed out"); 115 LHD(13, " packets reassembled ok"); 116 117 LHD(15, "packets forwarded"); 118 LHD(16, " fast forwarded"); 119 LHD(17, " unreachable dests"); 120 LHD(18, " redirects generated"); 121 122 RHD(0, "total packets sent"); 123 RHD(1, " generated locally"); 124 RHD(2, " output drops"); 125 RHD(3, " output fragments generated"); 126 RHD(4, " fragmentation failed"); 127 RHD(5, " destinations unreachable"); 128 RHD(6, " packets output via raw IP"); 129 RHD(7, " total UDP packets sent"); 130 131 RHD(9, "total UDP packets received"); 132 RHD(10, " too short for header"); 133 RHD(11, " invalid checksum"); 134 RHD(12, " invalid length"); 135 RHD(13, " no socket for dest port"); 136 RHD(14, " no socket for broadcast"); 137 RHD(15, " socket buffer full"); 138 } 139 140 void 141 showip(void) 142 { 143 u_quad_t totalout; 144 145 totalout = curstat.i.ips_forward + curstat.i.ips_localout; 146 147 SHOW(i.ips_total, 0, 0); 148 mvwprintw(wnd, 0, 35, "%9llu", (unsigned long long)totalout); 149 SHOW(i.ips_delivered, 1, 0); 150 SHOW(i.ips_badsum, 2, 0); 151 SHOW(i.ips_tooshort, 3, 0); 152 SHOW(i.ips_toosmall, 4, 0); 153 SHOW(i.ips_badhlen, 5, 0); 154 SHOW(i.ips_badlen, 6, 0); 155 SHOW(i.ips_badvers, 7, 0); 156 SHOW(i.ips_toolong, 8, 0); 157 SHOW(i.ips_badoptions, 9, 0); 158 159 SHOW(i.ips_localout, 1, 35); 160 SHOW(i.ips_odropped, 2, 35); 161 SHOW(i.ips_ofragments, 3, 35); 162 SHOW(i.ips_cantfrag, 4, 35); 163 SHOW(i.ips_noroute, 5, 35); 164 SHOW(i.ips_rawout, 6, 35); 165 SHOW(u.udps_opackets, 7, 35); 166 167 SHOW(i.ips_fragments, 10, 0); 168 SHOW(i.ips_fragdropped, 11, 0); 169 SHOW(i.ips_fragtimeout, 12, 0); 170 SHOW(i.ips_reassembled, 13, 0); 171 172 SHOW(i.ips_forward, 15, 0); 173 SHOW(i.ips_fastforward, 16, 0); 174 SHOW(i.ips_cantforward, 17, 0); 175 SHOW(i.ips_redirectsent, 18, 0); 176 177 SHOW(u.udps_ipackets, 9, 35); 178 SHOW(u.udps_hdrops, 10, 35); 179 SHOW(u.udps_badsum, 11, 35); 180 SHOW(u.udps_badlen, 12, 35); 181 SHOW(u.udps_noport, 13, 35); 182 SHOW(u.udps_noportbcast, 14, 35); 183 SHOW(u.udps_fullsock, 15, 35); 184 } 185 186 int 187 initip(void) 188 { 189 190 if (namelist[0].n_type == 0) { 191 if (kvm_nlist(kd, namelist)) { 192 nlisterr(namelist); 193 return(0); 194 } 195 if ((namelist[0].n_type | namelist[1].n_type) == 0) { 196 error("No namelist"); 197 return(0); 198 } 199 } 200 return 1; 201 } 202 203 void 204 fetchip(void) 205 { 206 207 KREAD((void *)namelist[0].n_value, &newstat.i, 208 sizeof(newstat.i)); 209 KREAD((void *)namelist[1].n_value, &newstat.u, 210 sizeof(newstat.u)); 211 212 ADJINETCTR(curstat, oldstat, newstat, i.ips_total); 213 ADJINETCTR(curstat, oldstat, newstat, i.ips_delivered); 214 ADJINETCTR(curstat, oldstat, newstat, i.ips_badsum); 215 ADJINETCTR(curstat, oldstat, newstat, i.ips_tooshort); 216 ADJINETCTR(curstat, oldstat, newstat, i.ips_toosmall); 217 ADJINETCTR(curstat, oldstat, newstat, i.ips_badhlen); 218 ADJINETCTR(curstat, oldstat, newstat, i.ips_badlen); 219 ADJINETCTR(curstat, oldstat, newstat, i.ips_badvers); 220 ADJINETCTR(curstat, oldstat, newstat, i.ips_toolong); 221 ADJINETCTR(curstat, oldstat, newstat, i.ips_badoptions); 222 ADJINETCTR(curstat, oldstat, newstat, i.ips_localout); 223 ADJINETCTR(curstat, oldstat, newstat, i.ips_odropped); 224 ADJINETCTR(curstat, oldstat, newstat, i.ips_ofragments); 225 ADJINETCTR(curstat, oldstat, newstat, i.ips_cantfrag); 226 ADJINETCTR(curstat, oldstat, newstat, i.ips_noroute); 227 ADJINETCTR(curstat, oldstat, newstat, i.ips_rawout); 228 ADJINETCTR(curstat, oldstat, newstat, i.ips_fragments); 229 ADJINETCTR(curstat, oldstat, newstat, i.ips_fragdropped); 230 ADJINETCTR(curstat, oldstat, newstat, i.ips_fragtimeout); 231 ADJINETCTR(curstat, oldstat, newstat, i.ips_reassembled); 232 ADJINETCTR(curstat, oldstat, newstat, i.ips_forward); 233 ADJINETCTR(curstat, oldstat, newstat, i.ips_fastforward); 234 ADJINETCTR(curstat, oldstat, newstat, i.ips_cantforward); 235 ADJINETCTR(curstat, oldstat, newstat, i.ips_redirectsent); 236 ADJINETCTR(curstat, oldstat, newstat, u.udps_opackets); 237 ADJINETCTR(curstat, oldstat, newstat, u.udps_ipackets); 238 ADJINETCTR(curstat, oldstat, newstat, u.udps_hdrops); 239 ADJINETCTR(curstat, oldstat, newstat, u.udps_badsum); 240 ADJINETCTR(curstat, oldstat, newstat, u.udps_badlen); 241 ADJINETCTR(curstat, oldstat, newstat, u.udps_noport); 242 ADJINETCTR(curstat, oldstat, newstat, u.udps_noportbcast); 243 ADJINETCTR(curstat, oldstat, newstat, u.udps_fullsock); 244 245 if (update == UPDATE_TIME) 246 memcpy(&oldstat, &newstat, sizeof(oldstat)); 247 } 248 249 void 250 ip_boot(char *args) 251 { 252 253 memset(&oldstat, 0, sizeof(oldstat)); 254 update = UPDATE_BOOT; 255 } 256 257 void 258 ip_run(char *args) 259 { 260 261 if (update != UPDATE_RUN) { 262 memcpy(&oldstat, &newstat, sizeof(oldstat)); 263 update = UPDATE_RUN; 264 } 265 } 266 267 void 268 ip_time(char *args) 269 { 270 271 if (update != UPDATE_TIME) { 272 memcpy(&oldstat, &newstat, sizeof(oldstat)); 273 update = UPDATE_TIME; 274 } 275 } 276 277 void 278 ip_zero(char *args) 279 { 280 281 if (update == UPDATE_RUN) 282 memcpy(&oldstat, &newstat, sizeof(oldstat)); 283 } 284