Sun Oct 6 00:27:50 2019 UTC ()
sprintf() -> snprintf(), and adjust a buffer size to avoid any
potential for overflow.


(mrg)
diff -r1.25 -r1.26 src/usr.bin/netstat/mroute.c

cvs diff -r1.25 -r1.26 src/usr.bin/netstat/mroute.c (switch to unified diff)

--- src/usr.bin/netstat/mroute.c 2014/11/06 21:30:09 1.25
+++ src/usr.bin/netstat/mroute.c 2019/10/06 00:27:50 1.26
@@ -1,397 +1,397 @@ @@ -1,397 +1,397 @@
1/* $NetBSD: mroute.c,v 1.25 2014/11/06 21:30:09 christos Exp $ */ 1/* $NetBSD: mroute.c,v 1.26 2019/10/06 00:27:50 mrg Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1992, 1993 4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Stephen Deering of Stanford University. 8 * Stephen Deering of Stanford University.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors 18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software 19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission. 20 * without specific prior written permission.
21 * 21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 * 33 *
34 * from: @(#)mroute.c 8.1 (Berkeley) 6/6/93 34 * from: @(#)mroute.c 8.1 (Berkeley) 6/6/93
35 */ 35 */
36 36
37/* 37/*
38 * Copyright (c) 1989 Stephen Deering 38 * Copyright (c) 1989 Stephen Deering
39 * 39 *
40 * This code is derived from software contributed to Berkeley by 40 * This code is derived from software contributed to Berkeley by
41 * Stephen Deering of Stanford University. 41 * Stephen Deering of Stanford University.
42 * 42 *
43 * Redistribution and use in source and binary forms, with or without 43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions 44 * modification, are permitted provided that the following conditions
45 * are met: 45 * are met:
46 * 1. Redistributions of source code must retain the above copyright 46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer. 47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright 48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the 49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution. 50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software 51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement: 52 * must display the following acknowledgement:
53 * This product includes software developed by the University of 53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors. 54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors 55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software 56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission. 57 * without specific prior written permission.
58 * 58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE. 69 * SUCH DAMAGE.
70 * 70 *
71 * from: @(#)mroute.c 8.1 (Berkeley) 6/6/93 71 * from: @(#)mroute.c 8.1 (Berkeley) 6/6/93
72 */ 72 */
73 73
74#include <sys/cdefs.h> 74#include <sys/cdefs.h>
75#ifndef lint 75#ifndef lint
76#if 0 76#if 0
77static char sccsid[] = "from: @(#)mroute.c 8.1 (Berkeley) 6/6/93"; 77static char sccsid[] = "from: @(#)mroute.c 8.1 (Berkeley) 6/6/93";
78#else 78#else
79__RCSID("$NetBSD: mroute.c,v 1.25 2014/11/06 21:30:09 christos Exp $"); 79__RCSID("$NetBSD: mroute.c,v 1.26 2019/10/06 00:27:50 mrg Exp $");
80#endif 80#endif
81#endif /* not lint */ 81#endif /* not lint */
82 82
83/* 83/*
84 * Print multicast routing structures and statistics. 84 * Print multicast routing structures and statistics.
85 * 85 *
86 * MROUTING 1.0 86 * MROUTING 1.0
87 */ 87 */
88 88
89#include <sys/param.h> 89#include <sys/param.h>
90#include <sys/socket.h> 90#include <sys/socket.h>
91#include <sys/socketvar.h> 91#include <sys/socketvar.h>
92#include <sys/protosw.h> 92#include <sys/protosw.h>
93 93
94#include <net/if.h> 94#include <net/if.h>
95#include <net/route.h> 95#include <net/route.h>
96#include <netinet/in.h> 96#include <netinet/in.h>
97#include <netinet/igmp.h> 97#include <netinet/igmp.h>
98#define _KERNEL 98#define _KERNEL
99#include <netinet/ip_mroute.h> 99#include <netinet/ip_mroute.h>
100#undef _KERNEL 100#undef _KERNEL
101 101
102#include <stdio.h> 102#include <stdio.h>
103#include <stdlib.h> 103#include <stdlib.h>
104#include <kvm.h> 104#include <kvm.h>
105#include "netstat.h" 105#include "netstat.h"
106#include "rtutil.h" 106#include "rtutil.h"
107 107
108static char *pktscale(u_long); 108static char *pktscale(u_long);
109static void print_bw_meter(struct bw_meter *, int *); 109static void print_bw_meter(struct bw_meter *, int *);
110 110
111static char * 111static char *
112pktscale(u_long n) 112pktscale(u_long n)
113{ 113{
114 static char buf[20]; 114 static char buf[20];
115 char t; 115 char t;
116 116
117 if (n < 1024) 117 if (n < 1024)
118 t = ' '; 118 t = ' ';
119 else if (n < 1024 * 1024) { 119 else if (n < 1024 * 1024) {
120 t = 'k'; 120 t = 'k';
121 n /= 1024; 121 n /= 1024;
122 } else { 122 } else {
123 t = 'm'; 123 t = 'm';
124 n /= 1048576; 124 n /= 1048576;
125 } 125 }
126 126
127 (void)snprintf(buf, sizeof buf, "%lu%c", n, t); 127 (void)snprintf(buf, sizeof buf, "%lu%c", n, t);
128 return (buf); 128 return (buf);
129} 129}
130 130
131void 131void
132mroutepr(u_long mrpaddr, u_long mfchashtbladdr, u_long mfchashaddr, 132mroutepr(u_long mrpaddr, u_long mfchashtbladdr, u_long mfchashaddr,
133 u_long vifaddr) 133 u_long vifaddr)
134{ 134{
135 u_int mrtproto; 135 u_int mrtproto;
136 LIST_HEAD(, mfc) *mfchashtbl; 136 LIST_HEAD(, mfc) *mfchashtbl;
137 u_long mfchash, i; 137 u_long mfchash, i;
138 struct vif viftable[MAXVIFS]; 138 struct vif viftable[MAXVIFS];
139 struct mfc *mfcp, mfc; 139 struct mfc *mfcp, mfc;
140 struct vif *v; 140 struct vif *v;
141 vifi_t vifi; 141 vifi_t vifi;
142 int banner_printed; 142 int banner_printed;
143 int saved_numeric_addr; 143 int saved_numeric_addr;
144 int numvifs; 144 int numvifs;
145 int nmfc; /* No. of cache entries */ 145 int nmfc; /* No. of cache entries */
146 146
147 if (mrpaddr == 0) { 147 if (mrpaddr == 0) {
148 printf("ip_mrtproto: symbol not in namelist\n"); 148 printf("ip_mrtproto: symbol not in namelist\n");
149 return; 149 return;
150 } 150 }
151 151
152 kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto)); 152 kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
153 switch (mrtproto) { 153 switch (mrtproto) {
154 case 0: 154 case 0:
155 printf("no multicast routing compiled into this system\n"); 155 printf("no multicast routing compiled into this system\n");
156 return; 156 return;
157 157
158 case IGMP_DVMRP: 158 case IGMP_DVMRP:
159 break; 159 break;
160 160
161 default: 161 default:
162 printf("multicast routing protocol %u, unknown\n", mrtproto); 162 printf("multicast routing protocol %u, unknown\n", mrtproto);
163 return; 163 return;
164 } 164 }
165 165
166 if (mfchashtbladdr == 0) { 166 if (mfchashtbladdr == 0) {
167 printf("mfchashtbl: symbol not in namelist\n"); 167 printf("mfchashtbl: symbol not in namelist\n");
168 return; 168 return;
169 } 169 }
170 if (mfchashaddr == 0) { 170 if (mfchashaddr == 0) {
171 printf("mfchash: symbol not in namelist\n"); 171 printf("mfchash: symbol not in namelist\n");
172 return; 172 return;
173 } 173 }
174 if (vifaddr == 0) { 174 if (vifaddr == 0) {
175 printf("viftable: symbol not in namelist\n"); 175 printf("viftable: symbol not in namelist\n");
176 return; 176 return;
177 } 177 }
178 178
179 saved_numeric_addr = numeric_addr; 179 saved_numeric_addr = numeric_addr;
180 numeric_addr = 1; 180 numeric_addr = 1;
181 181
182 kread(vifaddr, (char *)&viftable, sizeof(viftable)); 182 kread(vifaddr, (char *)&viftable, sizeof(viftable));
183 banner_printed = 0; 183 banner_printed = 0;
184 numvifs = 0; 184 numvifs = 0;
185 185
186 for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) { 186 for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
187 if (v->v_lcl_addr.s_addr == 0) 187 if (v->v_lcl_addr.s_addr == 0)
188 continue; 188 continue;
189 numvifs = vifi; 189 numvifs = vifi;
190 190
191 if (!banner_printed) { 191 if (!banner_printed) {
192 printf("\nVirtual Interface Table\n %s%s", 192 printf("\nVirtual Interface Table\n %s%s",
193 "Vif Thresh Limit Local-Address ", 193 "Vif Thresh Limit Local-Address ",
194 "Remote-Address Pkt_in Pkt_out\n"); 194 "Remote-Address Pkt_in Pkt_out\n");
195 banner_printed = 1; 195 banner_printed = 1;
196 } 196 }
197 197
198 printf(" %3u %3u %5u %-15.15s", 198 printf(" %3u %3u %5u %-15.15s",
199 vifi, v->v_threshold, v->v_rate_limit, 199 vifi, v->v_threshold, v->v_rate_limit,
200 routename4(v->v_lcl_addr.s_addr, nflag)); 200 routename4(v->v_lcl_addr.s_addr, nflag));
201 printf(" %-15.15s %6lu %7lu\n", (v->v_flags & VIFF_TUNNEL) ? 201 printf(" %-15.15s %6lu %7lu\n", (v->v_flags & VIFF_TUNNEL) ?
202 routename4(v->v_rmt_addr.s_addr, nflag) : "", 202 routename4(v->v_rmt_addr.s_addr, nflag) : "",
203 v->v_pkt_in, v->v_pkt_out); 203 v->v_pkt_in, v->v_pkt_out);
204 } 204 }
205 if (!banner_printed) 205 if (!banner_printed)
206 printf("\nVirtual Interface Table is empty\n"); 206 printf("\nVirtual Interface Table is empty\n");
207 207
208 kread(mfchashtbladdr, (char *)&mfchashtbl, sizeof(mfchashtbl)); 208 kread(mfchashtbladdr, (char *)&mfchashtbl, sizeof(mfchashtbl));
209 kread(mfchashaddr, (char *)&mfchash, sizeof(mfchash)); 209 kread(mfchashaddr, (char *)&mfchash, sizeof(mfchash));
210 banner_printed = 0; 210 banner_printed = 0;
211 nmfc = 0; 211 nmfc = 0;
212 212
213 if (mfchashtbl != 0) 213 if (mfchashtbl != 0)
214 for (i = 0; i <= mfchash; ++i) { 214 for (i = 0; i <= mfchash; ++i) {
215 kread((u_long)&mfchashtbl[i], (char *)&mfcp, sizeof(mfcp)); 215 kread((u_long)&mfchashtbl[i], (char *)&mfcp, sizeof(mfcp));
216 216
217 for (; mfcp != 0; mfcp = mfc.mfc_hash.le_next) { 217 for (; mfcp != 0; mfcp = mfc.mfc_hash.le_next) {
218 if (!banner_printed) { 218 if (!banner_printed) {
219 printf("\nMulticast Forwarding Cache\n %s%s", 219 printf("\nMulticast Forwarding Cache\n %s%s",
220 "Hash Origin Mcastgroup ", 220 "Hash Origin Mcastgroup ",
221 "Traffic In-Vif Out-Vifs/Forw-ttl\n"); 221 "Traffic In-Vif Out-Vifs/Forw-ttl\n");
222 banner_printed = 1; 222 banner_printed = 1;
223 } 223 }
224 224
225 kread((u_long)mfcp, (char *)&mfc, sizeof(mfc)); 225 kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
226 printf(" %3lu %-15.15s", 226 printf(" %3lu %-15.15s",
227 i, routename4(mfc.mfc_origin.s_addr, nflag)); 227 i, routename4(mfc.mfc_origin.s_addr, nflag));
228 printf(" %-15.15s %7s %3u ", 228 printf(" %-15.15s %7s %3u ",
229 routename4(mfc.mfc_mcastgrp.s_addr, nflag), 229 routename4(mfc.mfc_mcastgrp.s_addr, nflag),
230 pktscale(mfc.mfc_pkt_cnt), mfc.mfc_parent); 230 pktscale(mfc.mfc_pkt_cnt), mfc.mfc_parent);
231 for (vifi = 0; vifi <= numvifs; ++vifi) 231 for (vifi = 0; vifi <= numvifs; ++vifi)
232 if (mfc.mfc_ttls[vifi]) 232 if (mfc.mfc_ttls[vifi])
233 printf(" %u/%u", vifi, mfc.mfc_ttls[vifi]); 233 printf(" %u/%u", vifi, mfc.mfc_ttls[vifi]);
234 234
235 printf("\n"); 235 printf("\n");
236 236
237 /* Print the bw meter information */ 237 /* Print the bw meter information */
238 { 238 {
239 struct bw_meter bw_meter, *bwm; 239 struct bw_meter bw_meter, *bwm;
240 int banner_printed2 = 0; 240 int banner_printed2 = 0;
241 241
242 bwm = mfc.mfc_bw_meter; 242 bwm = mfc.mfc_bw_meter;
243 while (bwm) { 243 while (bwm) {
244 kread((u_long)bwm, 244 kread((u_long)bwm,
245 (char *)&bw_meter, 245 (char *)&bw_meter,
246 sizeof bw_meter); 246 sizeof bw_meter);
247 print_bw_meter(&bw_meter, 247 print_bw_meter(&bw_meter,
248 &banner_printed2); 248 &banner_printed2);
249 bwm = bw_meter.bm_mfc_next; 249 bwm = bw_meter.bm_mfc_next;
250 } 250 }
251#if 0 /* Don't ever print it? */ 251#if 0 /* Don't ever print it? */
252 if (! banner_printed2) 252 if (! banner_printed2)
253 printf("\n No Bandwidth Meters\n"); 253 printf("\n No Bandwidth Meters\n");
254#endif 254#endif
255 } 255 }
256 256
257 nmfc++; 257 nmfc++;
258 } 258 }
259 } 259 }
260 if (!banner_printed) 260 if (!banner_printed)
261 printf("\nMulticast Forwarding Cache is empty\n"); 261 printf("\nMulticast Forwarding Cache is empty\n");
262 else 262 else
263 printf("\nTotal no. of entries in cache: %d\n", nmfc); 263 printf("\nTotal no. of entries in cache: %d\n", nmfc);
264 264
265 printf("\n"); 265 printf("\n");
266 numeric_addr = saved_numeric_addr; 266 numeric_addr = saved_numeric_addr;
267} 267}
268 268
269static void 269static void
270print_bw_meter(struct bw_meter *bw_meter, int *banner_printed) 270print_bw_meter(struct bw_meter *bw_meter, int *banner_printed)
271{ 271{
272 char s0[256], s1[256], s2[256], s3[256]; 272 char s0[256*3], s1[256], s2[256], s3[256];
273 struct timeval now, end, delta; 273 struct timeval now, end, delta;
274 274
275 gettimeofday(&now, NULL); 275 gettimeofday(&now, NULL);
276 276
277 if (! *banner_printed) { 277 if (! *banner_printed) {
278 printf(" Bandwidth Meters\n"); 278 printf(" Bandwidth Meters\n");
279 printf(" %-30s", "Measured(Start|Packets|Bytes)"); 279 printf(" %-30s", "Measured(Start|Packets|Bytes)");
280 printf(" %s", "Type"); 280 printf(" %s", "Type");
281 printf(" %-30s", "Thresh(Interval|Packets|Bytes)"); 281 printf(" %-30s", "Thresh(Interval|Packets|Bytes)");
282 printf(" Remain"); 282 printf(" Remain");
283 printf("\n"); 283 printf("\n");
284 *banner_printed = 1; 284 *banner_printed = 1;
285 } 285 }
286 286
287 /* The measured values */ 287 /* The measured values */
288 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) 288 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
289 sprintf(s1, "%llu", (unsigned long long)bw_meter->bm_measured.b_packets); 289 snprintf(s1, sizeof s1, "%llu", (unsigned long long)bw_meter->bm_measured.b_packets);
290 else 290 else
291 sprintf(s1, "?"); 291 snprintf(s1, sizeof s1, "?");
292 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) 292 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
293 sprintf(s2, "%llu", (unsigned long long)bw_meter->bm_measured.b_bytes); 293 snprintf(s2, sizeof s2, "%llu", (unsigned long long)bw_meter->bm_measured.b_bytes);
294 else 294 else
295 sprintf(s2, "?"); 295 snprintf(s2, sizeof s2, "?");
296 sprintf(s0, "%lld.%ld|%s|%s", 296 snprintf(s0, sizeof s0, "%lld.%ld|%s|%s",
297 (long long)bw_meter->bm_start_time.tv_sec, 297 (long long)bw_meter->bm_start_time.tv_sec,
298 (long)bw_meter->bm_start_time.tv_usec, 298 (long)bw_meter->bm_start_time.tv_usec,
299 s1, s2); 299 s1, s2);
300 printf(" %-30s", s0); 300 printf(" %-30s", s0);
301 301
302 /* The type of entry */ 302 /* The type of entry */
303 sprintf(s0, "%s", "?"); 303 snprintf(s0, sizeof s0, "%s", "?");
304 if (bw_meter->bm_flags & BW_METER_GEQ) 304 if (bw_meter->bm_flags & BW_METER_GEQ)
305 sprintf(s0, "%s", ">="); 305 snprintf(s0, sizeof s0, "%s", ">=");
306 else if (bw_meter->bm_flags & BW_METER_LEQ) 306 else if (bw_meter->bm_flags & BW_METER_LEQ)
307 sprintf(s0, "%s", "<="); 307 snprintf(s0, sizeof s0, "%s", "<=");
308 printf(" %-3s", s0); 308 printf(" %-3s", s0);
309 309
310 /* The threshold values */ 310 /* The threshold values */
311 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) 311 if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
312 sprintf(s1, "%llu", (unsigned long long)bw_meter->bm_threshold.b_packets); 312 snprintf(s1, sizeof s1, "%llu", (unsigned long long)bw_meter->bm_threshold.b_packets);
313 else 313 else
314 sprintf(s1, "?"); 314 snprintf(s1, sizeof s1, "?");
315 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) 315 if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
316 sprintf(s2, "%llu", (unsigned long long)bw_meter->bm_threshold.b_bytes); 316 snprintf(s2, sizeof s2, "%llu", (unsigned long long)bw_meter->bm_threshold.b_bytes);
317 else 317 else
318 sprintf(s2, "?"); 318 snprintf(s2, sizeof s2, "?");
319 sprintf(s0, "%lld.%ld|%s|%s", 319 snprintf(s0, sizeof s0, "%lld.%ld|%s|%s",
320 (long long)bw_meter->bm_threshold.b_time.tv_sec, 320 (long long)bw_meter->bm_threshold.b_time.tv_sec,
321 (long)bw_meter->bm_threshold.b_time.tv_usec, 321 (long)bw_meter->bm_threshold.b_time.tv_usec,
322 s1, s2); 322 s1, s2);
323 printf(" %-30s", s0); 323 printf(" %-30s", s0);
324 324
325 /* Remaining time */ 325 /* Remaining time */
326 timeradd(&bw_meter->bm_start_time, 326 timeradd(&bw_meter->bm_start_time,
327 &bw_meter->bm_threshold.b_time, &end); 327 &bw_meter->bm_threshold.b_time, &end);
328 if (timercmp(&now, &end, <=)) { 328 if (timercmp(&now, &end, <=)) {
329 timersub(&end, &now, &delta); 329 timersub(&end, &now, &delta);
330 sprintf(s3, "%lld.%ld", 330 snprintf(s3, sizeof s3, "%lld.%ld",
331 (long long)delta.tv_sec, (long)delta.tv_usec); 331 (long long)delta.tv_sec, (long)delta.tv_usec);
332 } else { 332 } else {
333 /* Negative time */ 333 /* Negative time */
334 timersub(&now, &end, &delta); 334 timersub(&now, &end, &delta);
335 sprintf(s3, "-%lld.%ld", 335 snprintf(s3, sizeof s3, "-%lld.%ld",
336 (long long)delta.tv_sec, (long)delta.tv_usec); 336 (long long)delta.tv_sec, (long)delta.tv_usec);
337 } 337 }
338 printf(" %s", s3); 338 printf(" %s", s3);
339 339
340 printf("\n"); 340 printf("\n");
341} 341}
342 342
343void 343void
344mrt_stats(u_long mrpaddr, u_long mstaddr) 344mrt_stats(u_long mrpaddr, u_long mstaddr)
345{ 345{
346 u_int mrtproto; 346 u_int mrtproto;
347 struct mrtstat mrtstat; 347 struct mrtstat mrtstat;
348 348
349 if (mrpaddr == 0) { 349 if (mrpaddr == 0) {
350 printf("ip_mrtproto: symbol not in namelist\n"); 350 printf("ip_mrtproto: symbol not in namelist\n");
351 return; 351 return;
352 } 352 }
353 353
354 kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto)); 354 kread(mrpaddr, (char *)&mrtproto, sizeof(mrtproto));
355 switch (mrtproto) { 355 switch (mrtproto) {
356 case 0: 356 case 0:
357 printf("no multicast routing compiled into this system\n"); 357 printf("no multicast routing compiled into this system\n");
358 return; 358 return;
359 359
360 case IGMP_DVMRP: 360 case IGMP_DVMRP:
361 break; 361 break;
362 362
363 default: 363 default:
364 printf("multicast routing protocol %u, unknown\n", mrtproto); 364 printf("multicast routing protocol %u, unknown\n", mrtproto);
365 return; 365 return;
366 } 366 }
367 367
368 if (mstaddr == 0) { 368 if (mstaddr == 0) {
369 printf("mrtstat: symbol not in namelist\n"); 369 printf("mrtstat: symbol not in namelist\n");
370 return; 370 return;
371 } 371 }
372 372
373 kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat)); 373 kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
374 printf("multicast routing:\n"); 374 printf("multicast routing:\n");
375 printf("\t%lu datagram%s with no route for origin\n", 375 printf("\t%lu datagram%s with no route for origin\n",
376 mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route)); 376 mrtstat.mrts_no_route, plural(mrtstat.mrts_no_route));
377 printf("\t%lu upcall%s made to mrouted\n", 377 printf("\t%lu upcall%s made to mrouted\n",
378 mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls)); 378 mrtstat.mrts_upcalls, plural(mrtstat.mrts_upcalls));
379 printf("\t%lu datagram%s with malformed tunnel options\n", 379 printf("\t%lu datagram%s with malformed tunnel options\n",
380 mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel)); 380 mrtstat.mrts_bad_tunnel, plural(mrtstat.mrts_bad_tunnel));
381 printf("\t%lu datagram%s with no room for tunnel options\n", 381 printf("\t%lu datagram%s with no room for tunnel options\n",
382 mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel)); 382 mrtstat.mrts_cant_tunnel, plural(mrtstat.mrts_cant_tunnel));
383 printf("\t%lu datagram%s arrived on wrong interface\n", 383 printf("\t%lu datagram%s arrived on wrong interface\n",
384 mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if)); 384 mrtstat.mrts_wrong_if, plural(mrtstat.mrts_wrong_if));
385 printf("\t%lu datagram%s dropped due to upcall Q overflow\n", 385 printf("\t%lu datagram%s dropped due to upcall Q overflow\n",
386 mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw)); 386 mrtstat.mrts_upq_ovflw, plural(mrtstat.mrts_upq_ovflw));
387 printf("\t%lu datagram%s dropped due to upcall socket overflow\n", 387 printf("\t%lu datagram%s dropped due to upcall socket overflow\n",
388 mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull)); 388 mrtstat.mrts_upq_sockfull, plural(mrtstat.mrts_upq_sockfull));
389 printf("\t%lu datagram%s cleaned up by the cache\n", 389 printf("\t%lu datagram%s cleaned up by the cache\n",
390 mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups)); 390 mrtstat.mrts_cache_cleanups, plural(mrtstat.mrts_cache_cleanups));
391 printf("\t%lu datagram%s dropped selectively by ratelimiter\n", 391 printf("\t%lu datagram%s dropped selectively by ratelimiter\n",
392 mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel)); 392 mrtstat.mrts_drop_sel, plural(mrtstat.mrts_drop_sel));
393 printf("\t%lu datagram%s dropped - bucket Q overflow\n", 393 printf("\t%lu datagram%s dropped - bucket Q overflow\n",
394 mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow)); 394 mrtstat.mrts_q_overflow, plural(mrtstat.mrts_q_overflow));
395 printf("\t%lu datagram%s dropped - larger than bkt size\n", 395 printf("\t%lu datagram%s dropped - larger than bkt size\n",
396 mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large)); 396 mrtstat.mrts_pkt2large, plural(mrtstat.mrts_pkt2large));
397} 397}