Debian Codegraph @

apps/snmpnetstat/inet6.c

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * $OpenBSD: inet6.c,v 1.31 2004/11/17 01:47:20 itojun Exp $
3 */
4 /*
5 * BSDI inet.c,v 2.3 1995/10/24 02:19:29 prb Exp
6 */
7 /*
8 * Copyright (c) 1983, 1988, 1993
9 * The Regents of the University of California. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <net-snmp/net-snmp-config.h>
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41 #ifdef HAVE_WINSOCK_H
42 #include "winstub.h"
43 #endif
44 #ifdef HAVE_SYS_SOCKET_H
45 #include <sys/socket.h>
46 #endif
47 #ifdef HAVE_NETDB_H
48 #include <netdb.h>
49 #endif
50 #ifdef HAVE_SYS_TYPES_H
51 #include <sys/types.h>
52 #endif
53
54 #ifdef HAVE_NETINET_IN_H
55 #include <netinet/in.h>
56 #endif
57 #ifdef HAVE_ARPA_INET_H
58 #include <arpa/inet.h>
59 #endif
60
61 #include <net-snmp/net-snmp-includes.h>
62
63 #include "main.h"
64 #include "netstat.h"
65
66 struct stat_table {
67 unsigned int entry; /* entry number in table */
68 /*
69 * format string to printf(description, value)
70 * warning: the %d must be before the %s
71 */
72 char description[80];
73 };
74
75 static char *inet6name(const struct in6_addr *);
76
77 /*
78 * Print a summary of TCPv6 connections
79 * Listening processes are suppressed unless the
80 * -a (all) flag is specified.
81 */
82 const char *tcp6states[] = {
83 "",
84 "CLOSED",
85 "LISTEN",
86 "SYNSENT",
87 "SYNRECEIVED",
88 "ESTABLISHED",
89 "FINWAIT1",
90 "FINWAIT2",
91 "CLOSEWAIT",
92 "LASTACK",
93 "CLOSING",
94 "TIMEWAIT"
95 };
96
97 #define TCP_NSTATES 11
98
99 void
100 tcp6protopr(const char *name)
101 {
102 netsnmp_variable_list *var, *vp;
103 oid ipv6TcpConnState_oid[] =
104 { 1, 3, 6, 1, 2, 1, 6, 16, 1, 6 };
105 size_t ipv6TcpConnState_len =
106 OID_LENGTH(ipv6TcpConnState_oid);
107 int state, i;
108 unsigned char localAddr[16], remoteAddr[16];
109 int localPort, remotePort, ifIndex;
110 int first = 1;
111
112 /*
113 * Walking the v6 tcpConnState column will provide all
114 * the necessary information.
115 */
116 var = NULL;
117 snmp_varlist_add_variable(&var, ipv6TcpConnState_oid,
118 ipv6TcpConnState_len, ASN_NULL, NULL, 0);
119 if (netsnmp_query_walk(var, ss) != SNMP_ERR_NOERROR)
120 return;
121 if ((var->type & 0xF0) == 0x80) /* exception */
122 return;
123
124 for (vp = var; vp; vp = vp->next_variable) {
125 state = *vp->val.integer;
126 if (!aflag && state == MIB_TCPCONNSTATE_LISTEN)
127 continue;
128
129 if (first) {
130 printf("Active Internet Connections");
131 if (aflag)
132 printf(" (including servers)");
133 putchar('\n');
134 printf("%-5.5s %-27.27s %-27.27s %4s %s\n",
135 "Proto", "Local Address", "Remote Address", "I/F",
136 "(state)");
137 first = 0;
138 }
139
140 /*
141 * Extract the local/remote information from the index values
142 */
143 for (i = 0; i < 16; i++)
144 localAddr[i] = vp->name[10 + i];
145 localPort = vp->name[26];
146 for (i = 0; i < 16; i++)
147 remoteAddr[i] = vp->name[27 + i];
148 remotePort = vp->name[43];
149 ifIndex = vp->name[44];
150
151 printf("%-5.5s", name);
152 inet6print((struct in6_addr *) localAddr, localPort, name, 1);
153 inet6print((struct in6_addr *) remoteAddr, remotePort, name, 0);
154 if (state < 1 || state > TCP_NSTATES)
155 printf(" %4d %d\n", ifIndex, state);
156 else
157 printf(" %4d %s\n", ifIndex, tcp6states[state]);
158 }
159 snmp_free_varbind(var);
160 }
161
162 /*
163 * Print a summary of UDPv6 "connections"
164 * XXX - what about "listening" services ??
165 */
166 void
167 udp6protopr(const char *name)
168 {
169 netsnmp_variable_list *var, *vp;
170 oid ipv6UdpLocalAddress_oid[] =
171 { 1, 3, 6, 1, 2, 1, 7, 6, 1, 1 };
172 size_t ipv6UdpLocalAddress_len =
173 OID_LENGTH(ipv6UdpLocalAddress_oid);
174 int localPort, ifIndex;
175
176 /*
177 * Walking a single column of the udpTable will provide
178 * all the necessary information from the index values.
179 */
180 var = NULL;
181 snmp_varlist_add_variable(&var, ipv6UdpLocalAddress_oid,
182 ipv6UdpLocalAddress_len, ASN_NULL, NULL, 0);
183 if (netsnmp_query_walk(var, ss) != SNMP_ERR_NOERROR)
184 return;
185 if ((var->type & 0xF0) == 0x80) /* exception */
186 return;
187
188 printf("Active Internet Connections\n");
189 printf("%-5.5s %-27.27s %4s\n", "Proto", "Local Address", "I/F");
190 for (vp = var; vp; vp = vp->next_variable) {
191 printf("%-5.5s", name);
192 /*
193 * Extract the local port from the index values, but take
194 * the IP address from the varbind value, (which is why
195 * we walked udpLocalAddress rather than udpLocalPort)
196 */
197 localPort = vp->name[vp->name_length - 2];
198 ifIndex = vp->name[vp->name_length - 1];
199 inet6print((struct in6_addr *) vp->val.string, localPort, name, 1);
200 printf(" %4d\n", ifIndex);
201 }
202 snmp_free_varbind(var);
203 }
204
205
206 /*********************
207 *
208 * IPv6 statistics
209 *
210 *********************/
211
212 /*
213 * Unlike the equivalent IPv4 statistics display routine,
214 * the IPv6 version must walk the columns of a table
215 * and total the statistics for each column (rather
216 * than simply retrieving individual scalar values)
217 */
218 void
219 _dump_v6stats(const char *name, oid * oid_buf, size_t buf_len,
220 struct stat_table *stable)
221 {
222 netsnmp_variable_list *var, *vp;
223 struct stat_table *sp;
224 long *stats;
225 oid stat;
226 unsigned int max_stat = 0;
227 int active = 0;
228
229 var = NULL;
230 for (sp = stable; sp->entry; sp++) {
231 oid_buf[buf_len - 1] = sp->entry;
232 if (sp->entry > max_stat)
233 max_stat = sp->entry;
234 snmp_varlist_add_variable(&var, oid_buf, buf_len,
235 ASN_NULL, NULL, 0);
236 }
237 oid_buf[buf_len - 1] = stable[0].entry;
238 stats = (long *) calloc(max_stat + 1, sizeof(long));
239
240 /*
241 * Walk the specified column(s), and total the individual statistics
242 */
243 while (1) {
244 if (netsnmp_query_getnext(var, ss) != SNMP_ERR_NOERROR)
245 break;
246 if ((var->type & 0xF0) == 0x80) /* exception */
247 break;
248 if (snmp_oid_compare(oid_buf, buf_len, var->name, buf_len) != 0)
249 break; /* End of Table */
250
251 for (vp = var; vp; vp = vp->next_variable) {
252 stat = vp->name[buf_len - 1];
253 stats[stat] += *vp->val.integer;
254 }
255 active = 1;
256 }
257 if (!active) {
258 free(stats);
259 snmp_free_varbind(var);
260 return; /* No statistics to display */
261 }
262
263 /*
264 * Display the results
265 */
266 printf("%s:\n", name);
267 for (sp = stable; sp->entry; sp++) {
268 /*
269 * If '-Cs' was specified twice,
270 * then only display non-zero stats.
271 */
272 if (stats[sp->entry] > 0 || sflag == 1) {
273 printf(sp->description, stats[sp->entry],
274 plural(stats[sp->entry]));
275 putchar('\n');
276 }
277 }
278 free(stats);
279 snmp_free_varbind(var);
280 }
281
282
283 /*
284 * Dump IP6 statistics.
285 */
286 void
287 ip6_stats(const char *name)
288 {
289 oid ip6stats_oid[] = { 1, 3, 6, 1, 2, 1, 55, 1, 6, 1, 0 };
290 size_t ip6stats_len = OID_LENGTH(ip6stats_oid);
291 struct stat_table ip6stats_tbl[] = {
292 {1, "%14d total datagram%s received"},
293 {2, "%14d datagram%s with header errors"},
294 {3, "%14d oversized datagram%s"},
295 {4, "%14d datagram%s with no route"},
296 {5, "%14d datagram%s with an invalid destination address"},
297 {6, "%14d datagram%s with unknown protocol"},
298 {7, "%14d short datagram%s discarded"},
299 {8, "%14d datagram%s discarded"},
300 {9, "%14d datagram%s delivered"},
301 {10, "%14d datagram%s forwarded"},
302 {11, "%14d output datagram request%s"},
303 {12, "%14d output datagram%s discarded"},
304 {13, "%14d datagram%s fragmented"},
305 {14, "%14d fragmentation failure%s"},
306 {15, "%14d fragment%s created"},
307 {16, "%14d fragment%s received"},
308 {17, "%14d datagram%s reassembled"},
309 {18, "%14d reassembly failure%s"},
310 {19, "%14d multicast datagram%s received"},
311 {20, "%14d multicast datagram%s transmitted"},
312 {0, ""}
313 };
314
315 _dump_v6stats(name, ip6stats_oid, ip6stats_len, ip6stats_tbl);
316 }
317
318 /*
319 * Dump IPv6 per-interface statistics - Omitted
320 */
321
322
323 /*
324 * Dump ICMP6 statistics.
325 */
326 void
327 icmp6_stats(const char *name)
328 {
329 oid icmp6stats_oid[] =
330 { 1, 3, 6, 1, 2, 1, 56, 1, 1, 1, 0 };
331 size_t icmp6stats_len = OID_LENGTH(icmp6stats_oid);
332 struct stat_table icmp6stats_tbl[] = {
333 {1, "%14d total message%s received"},
334 {2, "%14d message%s dropped due to errors"},
•335 {18, "%14d output message request%s"},
336 {19, "%14d output message%s discarded"},
337 {0, ""}
338 };
339 struct stat_table icmp6_inhistogram[] = {
340 {3, " Destination unreachable: %d"},
341 {4, " Admin Prohibit: %d"},
342 {5, " Time Exceeded: %d"},
343 {6, " Parameter Problem: %d"},
344 {7, " Too Big: %d"},
345 {8, " Echo Request: %d"},
346 {9, " Echo Reply: %d"},
347 {10, " Router Solicit: %d"},
348 {11, " Router Advert: %d"},
349 {12, " Neighbor Solicit: %d"},
350 {13, " Neighbor Advert: %d"},
351 {14, " Redirect: %d"},
352 {15, " Group Member Request: %d"},
353 {16, " Group Member Reply: %d"},
354 {17, " Group Member Reduce: %d"},
355 {0, ""}
356 };
357 struct stat_table icmp6_outhistogram[] = {
358 {20, " Destination unreachable: %d"},
359 {21, " Admin Prohibit: %d"},
360 {22, " Time Exceeded: %d"},
361 {23, " Parameter Problem: %d"},
362 {24, " Too Big: %d"},
363 {25, " Echo Request: %d"},
364 {26, " Echo Reply: %d"},
365 {27, " Router Solicit: %d"},
366 {28, " Router Advert: %d"},
367 {29, " Neighbor Solicit: %d"},
368 {30, " Neighbor Advert: %d"},
369 {31, " Redirect: %d"},
370 {32, " Group Member Request: %d"},
371 {33, " Group Member Reply: %d"},
372 {34, " Group Member Reduce: %d"},
373 {0, ""}
374 };
375
376 _dump_v6stats(name, icmp6stats_oid, icmp6stats_len, icmp6stats_tbl);
377 _dump_v6stats(" Input Histogram",
378 icmp6stats_oid, icmp6stats_len, icmp6_inhistogram);
379 _dump_v6stats(" Output Histogram",
380 icmp6stats_oid, icmp6stats_len, icmp6_outhistogram);
381 }
382
383 /*
384 * Dump ICMPv6 per-interface statistics - Omitted
385 */
386
387
388 /*
389 * Ommitted:
390 * Dump PIM statistics
391 * Dump raw ip6 statistics
392 */
393
394
395
396 /*
397 * Pretty print an Internet address (net address + port).
398 * If the nflag was specified, use numbers instead of names.
399 */
400
401 void
402 inet6print(const struct in6_addr *in6, int port, const char *proto,
403 int local)
404 {
405
406 #define GETSERVBYPORT6(port, proto, ret) do { \
407 if (strcmp((proto), "tcp6") == 0) \
408 (ret) = getservbyport((int)(port), "tcp"); \
409 else if (strcmp((proto), "udp6") == 0) \
410 (ret) = getservbyport((int)(port), "udp"); \
411 else \
412 (ret) = getservbyport((int)(port), (proto)); \
413 } while (0)
414
415 struct servent *sp = NULL;
416 char line[80], *cp;
417 int width = 27 - 9;
418 int len = sizeof line;
419
420 if (vflag && width < strlen(inet6name(in6)))
421 width = strlen(inet6name(in6));
422 snprintf(line, len, "%.*s.", width, inet6name(in6));
423 len -= strlen(line);
424 if (len <= 0)
425 goto bail;
426
427 cp = strchr(line, '\0');
428 if (!nflag && port && local)
429 GETSERVBYPORT6(htons(port), proto, sp);
430 if (sp || port == 0)
431 snprintf(cp, len, vflag ? "%s" : "%.8s", sp ? sp->s_name : "*");
432 else
433 snprintf(cp, len, "%d", port);
434 width = 27;
435 if (vflag && width < strlen(line))
436 width = strlen(line);
437 bail:
438 printf(" %-*.*s", width, width, line);
439 }
440
441 /*
442 * Construct an Internet address representation.
443 * If the nflag has been supplied, give
444 * numeric value, otherwise try for symbolic name.
445 */
446
447 char *
448 inet6name(const struct in6_addr *in6p)
449 {
450 char *cp;
451 static char line[NI_MAXHOST];
452 static char domain[MAXHOSTNAMELEN];
453 static int first = 1;
454
455 if (first && !nflag) {
456 first = 0;
457 if (gethostname(line, sizeof(line)) == 0 &&
458 (cp = strchr(line, '.')))
459 (void) strlcpy(domain, cp + 1, sizeof domain);
460 else
461 domain[0] = '\0';
462 }
463 #ifdef NETSNMP_ENABLE_IPV6
464 if (IN6_IS_ADDR_UNSPECIFIED(in6p)) {
465 strlcpy(line, "*", sizeof(line));
466 } else {
467 struct sockaddr_in6 sin6;
468 char hbuf[NI_MAXHOST];
469
470 memset(&sin6, 0, sizeof(sin6));
471 sin6.sin6_family = AF_INET6;
472 sin6.sin6_addr = *in6p;
473 #ifdef __KAME__
474 if (IN6_IS_ADDR_LINKLOCAL(in6p) || IN6_IS_ADDR_MC_LINKLOCAL(in6p)) {
475 sin6.sin6_scope_id =
476 ntohs(*(const uint16_t *) &in6p->s6_addr[2]);
477 sin6.sin6_addr.s6_addr[2] = 0;
478 sin6.sin6_addr.s6_addr[3] = 0;
479 }
480 #endif
481 if (getnameinfo((struct sockaddr *) &sin6, sizeof(sin6),
482 hbuf, sizeof(hbuf), NULL, 0,
483 nflag ? NI_NUMERICHOST : 0) < 0)
484 strlcpy(hbuf, "?", sizeof(hbuf));
485 if ((cp = strchr(hbuf, '.')) && strcmp(cp + 1, domain) == 0)
486 *cp = 0;
487 strlcpy(line, hbuf, sizeof(line));
488 }
489 #else
490 strlcpy(line, "[[XXX - inet6 address]]", sizeof(line));
491 #endif
492 return (line);
493 }
494
495 #ifdef TCP6
496 /*
497 * Dump the contents of a TCP6 PCB - Omitted
498 */
499 #endif