Debian Codegraph @

apps/snmpnetstat/routex.c

raw
1 /*
2 * $OpenBSD: route.c,v 1.66 2004/11/17 01:47:20 itojun Exp $
3 */
4 /*
5 * $NetBSD: route.c,v 1.15 1996/05/07 02:55:06 thorpej Exp $
6 */
7
8 /*
9 * Copyright (c) 1983, 1988, 1993
10 * The Regents of the University of California. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <net-snmp/net-snmp-config.h>
38
39 #ifdef HAVE_SYS_TYPES_H
40 #include <sys/types.h>
41 #endif
42
43 #ifdef HAVE_NETINET_IN_H
44 #include <netinet/in.h>
45 #endif
46 #ifdef HAVE_ARPA_INET_H
47 #include <arpa/inet.h>
48 #endif
49
50 #include <net-snmp/net-snmp-includes.h>
51
52 netsnmp_feature_require(sockaddr_size);
53
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
57 #ifdef HAVE_NETDB_H
58 #include <netdb.h>
59 #endif
60
61 #ifndef INET
62 #define INET
63 #endif
64
65 #include "main.h"
66 #include "netstat.h"
67 #ifdef HAVE_WINSOCK_H
68 #include "winstub.h"
69 #endif
70
71 /*
72 * inetCidrRouteTable
73 */
74 #define SET_IFNO 0x01
75 #define SET_TYPE 0x02
76 #define SET_PRTO 0x04
77 #define SET_AGE 0x08
78 #define SET_AS 0x10
79 #define SET_MET1 0x20
80 #define SET_ALL 0x3f
81
82 /*
83 * ip6RouteTable
84 */
85 #define SET_HOP 0x40
86 #define SET_INVALID 0x80
87 #define SET_ALL6 0x67
88 /*
89 * not invalid, and only the columns that we fetch
90 */
91
92 struct route_entry {
93 int af;
94 struct sockaddr_storage dst;
95 struct sockaddr_storage hop;
96 int mask;
97 int ifNumber;
98 int type;
99 int proto;
100 int age;
101 int as;
102 int metric1;
103 int set_bits;
104 char ifname[64];
105 };
106
107
108 static void pr_rtxhdr(int af, const char *table);
109 static void p_rtnodex(struct route_entry *rp);
110
111 /*
112 * Print routing tables.
113 */
114 int
115 routexpr(int af)
116 {
117 struct route_entry route, *rp = &route;
118 oid rtcol_oid[] = { 1, 3, 6, 1, 2, 1, 4, 24, 7, 1, 0 }; /* inetCidrRouteEntry */
119 size_t rtcol_len = OID_LENGTH(rtcol_oid);
120 netsnmp_variable_list *var = NULL, *vp;
121 int hdr_af = AF_UNSPEC;
122 int printed = 0;
123
124 #define ADD_RTVAR( x ) rtcol_oid[ rtcol_len-1 ] = x; \
125 snmp_varlist_add_variable( &var, rtcol_oid, rtcol_len, ASN_NULL, NULL, 0)
126 ADD_RTVAR(7); /* inetCidrRouteIfIndex */
127 ADD_RTVAR(8); /* inetCidrRouteType */
128 ADD_RTVAR(9); /* inetCidrRouteProto */
129 ADD_RTVAR(10); /* inetCidrRouteAge */
130 ADD_RTVAR(11); /* inetCidrRouteNextHopAS */
131 ADD_RTVAR(12); /* inetCidrRouteMetric1 */
132 #undef ADD_RTVAR
133
134 /*
135 * Now walk the inetCidrRouteTable, reporting the various route entries
136 */
137 while (1) {
138 oid *op;
139 unsigned char *cp;
140 int i;
141
142 if (netsnmp_query_getnext(var, ss) != SNMP_ERR_NOERROR)
143 break;
144 rtcol_oid[rtcol_len - 1] = 7; /* ifRouteIfIndex */
145 if (snmp_oid_compare(rtcol_oid, rtcol_len,
146 var->name, rtcol_len) != 0)
147 break; /* End of Table */
148 if (var->type == SNMP_NOSUCHOBJECT ||
149 var->type == SNMP_NOSUCHINSTANCE ||
150 var->type == SNMP_ENDOFMIBVIEW)
151 break;
152 memset(&route, 0, sizeof(struct route_entry));
153 /*
154 * Extract inetCidrRouteDest, inetCidrRoutePfxLen,
155 * * inetCidrRouteNextHop from index
156 */
157 switch (var->name[rtcol_len]) {
158 case 1:
159 {
160 struct sockaddr_in *sin =
161 (struct sockaddr_in *) &route.dst;
162 int len;
163 route.af = AF_INET;
164 sin->sin_family = AF_INET;
165 op = var->name + rtcol_len + 1;
166 len = *op++;
167 cp = (unsigned char *) &sin->sin_addr;
168 for (i = 0; i < len; i++)
169 *cp++ = *op++;
170 route.mask = *op++;
171 op += *op + 1;
172 op++; /* addrType */
173 op++; /* addrLen */
174 sin = (struct sockaddr_in *) &route.hop;
175 sin->sin_family = AF_INET;
176 cp = (unsigned char *) &sin->sin_addr;
177 for (i = 0; i < len; i++)
178 *cp++ = *op++;
179 break;
180 }
181 case 2:
182 {
183 struct sockaddr_in6 *sin6 =
184 (struct sockaddr_in6 *) &route.dst;
185 int len;
186 route.af = AF_INET6;
187 sin6->sin6_family = AF_INET6;
188 op = var->name + rtcol_len + 1;
189 len = *op++;
190 cp = (unsigned char *) &sin6->sin6_addr;
191 for (i = 0; i < len; i++)
192 *cp++ = *op++;
193 route.mask = *op++;
194 op += *op + 1;
195 op++; /* addrType */
196 op++; /* addrLen */
197 sin6 = (struct sockaddr_in6 *) &route.hop;
198 sin6->sin6_family = AF_INET6;
199 cp = (unsigned char *) &sin6->sin6_addr;
200 for (i = 0; i < len; i++)
201 *cp++ = *op++;
202 break;
203 }
204 default:
205 fprintf(stderr, "Bad address type: %d\n",
206 (int) var->name[rtcol_len]);
207 exit(1);
208 }
209 /*
210 * Extract ipRouteDest index value
211 */
212
213 for (vp = var; vp; vp = vp->next_variable) {
214 switch (vp->name[rtcol_len - 1]) {
215 case 7: /* inetCidrRouteIfIndex */
216 rp->ifNumber = *vp->val.integer;
217 rp->set_bits |= SET_IFNO;
218 break;
219 case 8: /* inetCidrRouteType */
220 rp->type = *vp->val.integer;
221 rp->set_bits |= SET_TYPE;
222 break;
223 case 9: /* inetCidrRouteProto */
224 rp->proto = *vp->val.integer;
225 rp->set_bits |= SET_PRTO;
226 break;
227 case 10: /* inetCidrRouteAge */
228 rp->age = *vp->val.integer;
229 rp->set_bits |= SET_AGE;
230 break;
231 case 11: /* inetCidrRouteNextHopAS */
232 rp->as = *vp->val.integer;
233 rp->set_bits |= SET_AS;
234 break;
235 case 12: /* inetCidrRouteMetric1 */
236 rp->metric1 = *vp->val.integer;
237 rp->set_bits |= SET_MET1;
238 break;
239 }
240 }
241 if (rp->set_bits != SET_ALL) {
242 continue; /* Incomplete query */
243 }
244
245 if (af != AF_UNSPEC && rp->af != af)
246 continue;
247
248 if (hdr_af != rp->af) {
249 if (hdr_af != AF_UNSPEC)
250 printf("\n");
251 hdr_af = rp->af;
252 pr_rtxhdr(hdr_af, "inetCidrRouteTable");
253 }
254 p_rtnodex(rp);
255 printed++;
256 }
257 snmp_free_varbind(var);
258 return printed;
259 }
260
261 /*
262 * Backwards-compatibility for the IPV6-MIB
263 */
264 int
265 route6pr(int af)
266 {
267 struct route_entry route, *rp = &route;
268 oid rtcol_oid[] = { 1, 3, 6, 1, 2, 1, 55, 1, 11, 1, 0 }; /* ipv6RouteEntry */
269 size_t rtcol_len = OID_LENGTH(rtcol_oid);
270 netsnmp_variable_list *var = NULL, *vp;
271 int printed = 0;
272 int hdr_af = AF_UNSPEC;
273 int i;
274
275 if (af != AF_UNSPEC && af != AF_INET6)
276 return 0;
277
278 #define ADD_RTVAR( x ) rtcol_oid[ rtcol_len-1 ] = x; \
279 snmp_varlist_add_variable( &var, rtcol_oid, rtcol_len, ASN_NULL, NULL, 0)
280 ADD_RTVAR(4); /* ipv6RouteIfIndex */
281 ADD_RTVAR(5); /* ipv6RouteNextHop */
282 ADD_RTVAR(6); /* ipv6RouteType */
283 ADD_RTVAR(7); /* ipv6RouteProto */
284 ADD_RTVAR(11); /* ipv6RouteMetric */
285 ADD_RTVAR(14); /* ipv6RouteValid */
286 #undef ADD_RTVAR
287
288 /*
289 * Now walk the ipv6RouteTable, reporting the various route entries
290 */
291 while (1) {
292 oid *op;
293 unsigned char *cp, *cp1;
294 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &route.dst;
295
296 if (netsnmp_query_getnext(var, ss) != SNMP_ERR_NOERROR)
297 break;
298 rtcol_oid[rtcol_len - 1] = 4; /* ipv6RouteIfIndex */
299 if (snmp_oid_compare(rtcol_oid, rtcol_len,
300 var->name, rtcol_len) != 0)
301 break; /* End of Table */
302 if (var->type == SNMP_NOSUCHOBJECT ||
303 var->type == SNMP_NOSUCHINSTANCE ||
304 var->type == SNMP_ENDOFMIBVIEW)
305 break;
306 memset(&route, 0, sizeof(struct route_entry));
307 rp->af = AF_INET6;
308 sin6->sin6_family = AF_INET6;
309 op = var->name + rtcol_len;
310 cp = (unsigned char *) &sin6->sin6_addr;
311 for (i = 0; i < 16; i++)
312 *cp++ = *op++;
313 route.mask = *op++;
314
315 for (vp = var; vp; vp = vp->next_variable) {
316 switch (vp->name[rtcol_len - 1]) {
317 case 4: /* ipv6RouteIfIndex */
318 rp->ifNumber = *vp->val.integer;
319 /*
320 * This is, technically, an Ipv6IfIndex, which
321 * could maybe be different than the IfIndex
322 * for the same interface. We ignore this
323 * possibility for now, in the hopes that
324 * nobody actually allocates these numbers
325 * differently.
326 */
327 rp->set_bits |= SET_IFNO;
328 break;
329 case 5: /* ipv6RouteNextHop */
330 cp1 = (unsigned char *) vp->val.string;
331 sin6 = (struct sockaddr_in6 *) &rp->hop;
332 sin6->sin6_family = AF_INET6;
333 cp = (unsigned char *) &sin6->sin6_addr;
334 for (i = 0; i < 16; i++)
335 *cp++ = *cp1++;
336 rp->set_bits |= SET_HOP;
337 break;
338 case 6: /* ipv6RouteType */
339 rp->type = *vp->val.integer;
340 /*
341 * This enum maps to similar values in inetCidrRouteType
342 */
343 rp->set_bits |= SET_TYPE;
344 break;
345 case 7: /* ipv6RouteProtocol */
346 rp->proto = *vp->val.integer;
347 /*
348 * TODO: this does not map directly to the
349 * * inetCidrRouteProtocol values. If we use
350 * * rp->proto more, we will have to manage this.
351 */
352 rp->set_bits |= SET_PRTO;
353 break;
354 case 11: /* ipv6RouteMetric */
355 rp->metric1 = *vp->val.integer;
356 rp->set_bits |= SET_MET1;
357 break;
358 case 14: /* ipv6RouteValid */
359 if (*vp->val.integer == 2)
360 rp->set_bits |= SET_INVALID;
361 break;
362 }
363 }
364 if (rp->set_bits != SET_ALL6) {
365 continue; /* Incomplete query */
366 }
367
368 if (hdr_af != rp->af) {
369 if (hdr_af != AF_UNSPEC)
370 printf("\n");
371 hdr_af = rp->af;
372 pr_rtxhdr(AF_INET6, "ip6RouteTable");
373 }
374 p_rtnodex(rp);
375 printed++;
376 }
377 snmp_free_varbind(var);
378 return printed;
379 }
380
381
382 /*
383 * column widths; each followed by one space
384 */
385 #ifndef NETSNMP_ENABLE_IPV6
386 #define WID_DST(af) 26 /* width of destination column */
387 #define WID_GW(af) 18 /* width of gateway column */
388 #else
389 /*
390 * width of destination/gateway column
391 */
392 /*
393 * strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4
394 */
395 #define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 34 : 26) : 26)
396 #define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 39 : 26) : 26)
397 #endif /* NETSNMP_ENABLE_IPV6 */
398
399 /*
400 * Print header for routing table columns.
401 */
402 static void
403 pr_rtxhdr(int af, const char *table)
404 {
405 switch (af) {
406 case AF_INET:
407 printf("IPv4 Routing tables (inetCidrRouteTable)\n");
408 break;
409 case AF_INET6:
410 printf("IPv6 Routing tables (%s)\n", table);
411 break;
412 }
413 printf("%-*.*s ", WID_DST(af), WID_DST(af), "Destination");
414 printf("%-*.*s %-6.6s %s\n",
415 WID_GW(af), WID_GW(af), "Gateway", "Flags", "Interface");
416 }
417
418 #ifndef HAVE_INET_NTOP
419 /*
420 * MSVC and MinGW
421 */
422 #define inet_ntop netsnmp_inet_ntop
423 static const char *
424 netsnmp_inet_ntop(int af, const void *src, char *dst, size_t size)
425 {
426 DWORD out_len = size;
427
428 switch (af) {
429 case AF_INET:
430 {
431 struct sockaddr_in in;
432
433 memset(&in, 0, sizeof(in));
434 in.sin_family = af;
435 memcpy(&in.sin_addr, src, 4);
436 if (WSAAddressToString
437 ((struct sockaddr *) &in, sizeof(in), NULL, dst,
438 &out_len) == 0)
439 return dst;
440 }
441 break;
442 case AF_INET6:
443 {
444 struct sockaddr_in6 in6;
445
446 memset(&in6, 0, sizeof(in6));
447 in6.sin6_family = af;
448 memcpy(&in6.sin6_addr, src, 16);
449 if (WSAAddressToString
450 ((struct sockaddr *) &in6, sizeof(in6), NULL, dst,
451 &out_len) == 0)
452 return dst;
453 }
454 break;
455 }
456 return NULL;
457 }
458 #endif
459
460 /*
461 * Return the name of the network whose address is given.
462 * The address is assumed to be that of a net or subnet, not a host.
463 */
464 static char *
465 netxname(struct sockaddr_storage *in, int mask)
466 {
467 static char host[MAXHOSTNAMELEN];
468 static char line[MAXHOSTNAMELEN];
469 struct sockaddr_in *sin = (struct sockaddr_in *) in;
470 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) in;
471
472 switch (in->ss_family) {
473 case AF_INET:
474 inet_ntop(in->ss_family, &sin->sin_addr, host, sizeof(host));
475 if (mask == 32)
476 strlcpy(line, host, sizeof line);
477 else
478 snprintf(line, sizeof(line), "%s/%d", host, mask);
479 break;
480 case AF_INET6:
481 inet_ntop(in->ss_family, &sin6->sin6_addr, host, sizeof(host));
482 if (mask == 128)
483 strlcpy(line, host, sizeof line);
484 else
485 snprintf(line, sizeof(line), "%s/%d", host, mask);
486 break;
487 }
488 return line;
489 }
490
491 static char *
492 routexname(const struct sockaddr_storage *in)
493 {
494 char *cp;
495 static char line[MAXHOSTNAMELEN];
496 char name[MAXHOSTNAMELEN];
497 static char domain[MAXHOSTNAMELEN];
498 static int first = 1;
499 const struct sockaddr *sa = (const void *) in;
500
501 if (first) {
502 first = 0;
503 if (gethostname(line, sizeof(line)) == 0
504 && (cp = strchr(line, '.')))
505 strlcpy(domain, cp + 1, sizeof(domain));
506 else
507 domain[0] = '\0';
508 }
509 if (getnameinfo(sa, netsnmp_sockaddr_size(sa), name, sizeof(name),
510 NULL, 0, nflag ? NI_NUMERICHOST : 0) < 0)
511 strlcpy(name, "?", sizeof(name));
512 if ((cp = strchr(name, '.')) && strcmp(cp + 1, domain) == 0)
513 *cp = '\0';
514 strlcpy(line, name, sizeof(line));
515 return line;
516 }
517
518
519 static char *
520 s_rtflagsx(struct route_entry *rp)
521 {
522 static char flag_buf[10];
523 char *cp = flag_buf;
524
525 *cp++ = '<';
526 *cp++ = 'U'; /* route is in use */
527 if ((rp->af == AF_INET && rp->mask == 32) ||
528 (rp->af == AF_INET6 && rp->mask == 128))
529 *cp++ = 'H'; /* host */
530 if (rp->proto == 4)
531 *cp++ = 'D'; /* ICMP redirect */
532 if (rp->type == 4)
533 *cp++ = 'G'; /* remote destination/net */
534 *cp++ = '>';
535 *cp = 0;
536 return flag_buf;
537 }
538
539 static void
540 p_rtnodex(struct route_entry *rp)
541 {
542 get_ifname(rp->ifname, rp->ifNumber);
543 if (rp->af == AF_INET) {
544 struct sockaddr_in *sin = (struct sockaddr_in *) &rp->dst;
545 printf("%-*s ",
546 WID_DST(AF_INET),
547 (sin->sin_addr.s_addr == INADDR_ANY) ? "default" :
548 (rp->mask == 32 ?
549 routexname(&rp->dst) : netxname(&rp->dst, rp->mask)));
550 } else if (rp->af == AF_INET6) {
551 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&rp->dst;
552 struct in6_addr in6_addr_any;
553
554 memset(&in6_addr_any, 0, sizeof(in6_addr_any)); /*IN6ADDR_ANY_INIT */
555 printf("%-*s ",
556 WID_DST(AF_INET6),
557 memcmp(&sin6->sin6_addr, &in6_addr_any,
558 sizeof(in6_addr_any)) == 0 ? "default" : (rp->mask ==
559 128 ?
560 routexname
561 (&rp->dst)
562 :
563 netxname
564 (&rp->dst,
565 rp->mask)));
566 }
567 printf("%-*s %-6.6s %s",
568 WID_GW(rp->af),
569 1 ? routexname(&rp->hop) : "*", s_rtflagsx(rp), rp->ifname);
570 if ((rp->set_bits & SET_AS) && rp->as != 0)
571 printf(" (AS %d)", rp->as);
572 printf("\n");
573 }