Debian Codegraph @

agent/mibgroup/kernel_sunos5.c

raw
1 /* Portions of this file are subject to the following copyright(s). See
2 * the Net-SNMP's COPYING file for more details and other copyrights
3 * that may apply:
4 */
5 /*
6 * Portions of this file are copyrighted by:
7 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
8 * Use is subject to license terms specified in the COPYING file
9 * distributed with the Net-SNMP package.
10 */
11
12 /*- This is a -*- C -*- compatible code file
13 *
14 * Code for SUNOS5_INSTRUMENTATION
15 *
16 * This file contains includes of standard and local system header files,
17 * includes of other application header files, global variable definitions,
18 * static variable definitions, static function prototypes, and function
19 * definitions.
20 *
21 * This file contains function to obtain statistics from SunOS 5.x kernel
22 *
23 */
24
25 #include <net-snmp/net-snmp-config.h>
26 #ifdef solaris2
27 /*-
28 * Includes of standard ANSI C header files
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35
36 /*-
37 * Includes of system header files (wrapped in duplicate include prevention)
38 */
39
40 #include <fcntl.h>
41 #include <stropts.h>
42 #include <sys/types.h>
43 #include <kvm.h>
44 #include <sys/fcntl.h>
45 #include <kstat.h>
46 #include <errno.h>
47 #include <time.h>
48 #include <ctype.h>
49
50 #include <sys/sockio.h>
51 #include <sys/socket.h>
52 #include <sys/stream.h>
53 #include <sys/stropts.h>
54 #include <sys/tihdr.h>
55 #include <sys/tiuser.h>
56 #include <sys/dlpi.h>
57 #include <inet/common.h>
58 #include <inet/mib2.h>
59 #include <inet/ip.h>
60 #include <net/if.h>
61 #include <netinet/in.h>
62
63 /*-
64 * Includes of local application header files
65 */
66
67 #include <net-snmp/net-snmp-includes.h>
68 #include <net-snmp/agent/net-snmp-agent-includes.h>
69
70 #include "kernel_sunos5.h"
71
72 kstat_ctl_t *kstat_fd = 0;
73
74 /*-
75 * Global variable definitions (with initialization)
76 */
77
78 /*-
79 * Static variable definitions (with initialization)
80 */
81
82 static
83 mibcache Mibcache[MIBCACHE_SIZE+1] = {
84 {MIB_SYSTEM, 0, (void *) -1, 0, 0, 0, 0},
85 {MIB_INTERFACES, 50 * sizeof(mib2_ifEntry_t), (void *) -1, 0, 3, 0,
86 0},
87 {MIB_AT, 0, (void *) -1, 0, 0, 0, 0},
88 {MIB_IP, sizeof(mib2_ip_t), (void *) -1, 0, 60, 0, 0},
89 {MIB_IP_ADDR, 20 * sizeof(mib2_ipAddrEntry_t), (void *) -1, 0, 60, 0,
90 0},
91 {MIB_IP_ROUTE, 200 * sizeof(mib2_ipRouteEntry_t), (void *) -1, 0, 30,
92 0, 0},
93 {MIB_IP_NET, 100 * sizeof(mib2_ipNetToMediaEntry_t), (void *) -1, 0,
94 300, 0, 0},
95 {MIB_ICMP, sizeof(mib2_icmp_t), (void *) -1, 0, 60, 0, 0},
96 {MIB_TCP, sizeof(mib2_tcp_t), (void *) -1, 0, 60, 0, 0},
97 {MIB_TCP_CONN, 1000 * sizeof(mib2_tcpConnEntry_t), (void *) -1, 0, 30,
98 0, 0},
99 {MIB_UDP, sizeof(mib2_udp_t), (void *) -1, 0, 30, 0, 0},
100 {MIB_UDP_LISTEN, 1000 * sizeof(mib2_udpEntry_t), (void *) -1, 0, 30, 0,
101 0},
102 {MIB_EGP, 0, (void *) -1, 0, 0, 0, 0},
103 {MIB_CMOT, 0, (void *) -1, 0, 0, 0, 0},
104 {MIB_TRANSMISSION, 0, (void *) -1, 0, 0, 0, 0},
105 {MIB_SNMP, 0, (void *) -1, 0, 0, 0, 0},
106 #ifdef SOLARIS_HAVE_IPV6_MIB_SUPPORT
107 #ifdef SOLARIS_HAVE_RFC4293_SUPPORT
108 {MIB_IP_TRAFFIC_STATS, 20 * sizeof(mib2_ipIfStatsEntry_t), (void *)-1, 0,
109 30, 0, 0},
110 {MIB_IP6, 20 * sizeof(mib2_ipIfStatsEntry_t), (void *)-1, 0, 30, 0, 0},
111 #else
112 {MIB_IP6, 20 * sizeof(mib2_ipv6IfStatsEntry_t), (void *)-1, 0, 30, 0, 0},
113 #endif
114 {MIB_IP6_ADDR, 20 * sizeof(mib2_ipv6AddrEntry_t), (void *)-1, 0, 30, 0, 0},
115 {MIB_IP6_ROUTE, 200 * sizeof(mib2_ipv6AddrEntry_t), (void *)-1, 0, 30, 0, 0},
116 {MIB_ICMP6, 20 * sizeof(mib2_ipv6IfIcmpEntry_t), (void *)-1, 0, 30, 0, 0},
117 {MIB_TCP6_CONN, 1000 * sizeof(mib2_tcp6ConnEntry_t), (void *) -1, 0, 30,
118 0, 0},
119 {MIB_UDP6_ENDPOINT, 1000 * sizeof(mib2_udp6Entry_t), (void *) -1, 0, 30,
120 0, 0},
121 #endif
122 #ifdef MIB2_SCTP
123 {MIB_SCTP, sizeof(mib2_sctp_t), (void *)-1, 0, 60, 0, 0},
124 {MIB_SCTP_CONN, sizeof(mib2_sctpConnEntry_t), (void *)-1, 0, 60, 0, 0},
125 {MIB_SCTP_CONN_LOCAL, sizeof(mib2_sctpConnLocalEntry_t), (void *)-1, 0,
126 60, 0, 0},
127 {MIB_SCTP_CONN_REMOTE, sizeof(mib2_sctpConnRemoteEntry_t), (void *)-1, 0,
128 60, 0, 0},
129 #endif
130 {0},
131 };
132
133 static
134 mibmap Mibmap[MIBCACHE_SIZE+1] = {
135 {MIB2_SYSTEM, 0,},
136 {MIB2_INTERFACES, 0,},
137 {MIB2_AT, 0,},
138 {MIB2_IP, 0,},
139 {MIB2_IP, MIB2_IP_20,},
140 {MIB2_IP, MIB2_IP_21,},
141 {MIB2_IP, MIB2_IP_22,},
142 {MIB2_ICMP, 0,},
143 {MIB2_TCP, 0,},
144 {MIB2_TCP, MIB2_TCP_13,},
145 {MIB2_UDP, 0,},
146 {MIB2_UDP, MIB2_UDP_5},
147 {MIB2_EGP, 0,},
148 {MIB2_CMOT, 0,},
149 {MIB2_TRANSMISSION, 0,},
150 {MIB2_SNMP, 0,},
151 #ifdef SOLARIS_HAVE_IPV6_MIB_SUPPORT
152 #ifdef SOLARIS_HAVE_RFC4293_SUPPORT
153 {MIB2_IP, MIB2_IP_TRAFFIC_STATS},
154 #endif
155 {MIB2_IP6, 0},
156 {MIB2_IP6, MIB2_IP6_ADDR},
157 {MIB2_IP6, MIB2_IP6_ROUTE},
158 {MIB2_ICMP6, 0},
159 {MIB2_TCP6, MIB2_TCP6_CONN},
160 {MIB2_UDP6, MIB2_UDP6_ENTRY},
161 #endif
162 #ifdef MIB2_SCTP
163 {MIB2_SCTP, 0},
164 {MIB2_SCTP, MIB2_SCTP_CONN},
165 {MIB2_SCTP, MIB2_SCTP_CONN_LOCAL},
166 {MIB2_SCTP, MIB2_SCTP_CONN_REMOTE},
167 #endif
168 {0},
169 };
170
171 static int sd = -2; /* /dev/arp stream descriptor. */
172
173 /*-
174 * Static function prototypes (use void as argument type if there are none)
175 */
176
177 static found_e
178 getentry(req_e req_type, void *bufaddr, size_t len, size_t entrysize,
179 void *resp, int (*comp)(void *, void *), void *arg);
180
181 static int
182 getmib(int groupname, int subgroupname, void **statbuf, size_t *size,
183 size_t entrysize, req_e req_type, void *resp, size_t *length,
184 int (*comp)(void *, void *), void *arg);
185
186 static int
187 getif(mib2_ifEntry_t *ifbuf, size_t size, req_e req_type, mib2_ifEntry_t *resp,
188 size_t *length, int (*comp)(void *, void *), void *arg);
189 static void
190 set_if_info(mib2_ifEntry_t *ifp, unsigned index, char *name, uint64_t flags,
191 int mtu);
192 static int get_if_stats(mib2_ifEntry_t *ifp);
193
194 #if defined(HAVE_IF_NAMEINDEX)
195 static int _dlpi_open(const char *devname);
196 static int _dlpi_get_phys_address(int fd, char *paddr, int maxlen,
197 int *paddrlen);
198 static int _dlpi_get_iftype(int fd, unsigned int *iftype);
199 static int _dlpi_attach(int fd, int ppa);
200 static int _dlpi_parse_devname(char *devname, int *ppap);
201 #else
202 static int
203 Name_cmp(void *, void *);
204 #endif
205
206 static void
207 init_mibcache_element(mibcache * cp);
208
209 #define STREAM_DEV "/dev/arp"
210 #define BUFSIZE 40960 /* Buffer for messages (should be modulo(pagesize) */
211
212 /*-
213 * Function definitions
214 */
215
216 #ifdef _STDC_COMPAT
217 #ifdef __cplusplus
218 extern "C" {
219 #endif
220 #endif
221
222 /*
223 * I profiled snmpd using Quantify on a Solaris 7 box, and it turned out that
224 * the calls to time() in getMibstat() were taking 18% of the total execution
225 * time of snmpd when doing simple walks over the whole tree. I guess it must
226 * be difficult for Sun hardware to tell the time or something ;-). Anyway,
227 * this seemed like it was negating the point of having the cache, so I have
228 * changed the code so that it runs a periodic alarm to age the cache entries
229 * instead. The meaning of the cache_ttl and cache_time members has changed to
230 * support this. cache_ttl is now the value that cache_time gets reset to when
231 * we fetch a value from the kernel; cache_time then ticks down to zero in
232 * steps of period (see below). When it reaches zero, the cache entry is no
233 * longer valid and we fetch a new one. The effect of this is the same as the
234 * previous code, but more efficient (because it's not calling time() for every
235 * variable fetched) when you are walking the tables. jbpn, 20020226.
236 */
237
238 static void
239 kernel_sunos5_cache_age(unsigned int regnumber, void *data)
240 {
241 int i = 0, period = (int)data;
242
243 for (i = 0; i < MIBCACHE_SIZE; i++) {
244 DEBUGMSGTL(("kernel_sunos5", "cache[%d] time %ld ttl %d\n", i,
245 Mibcache[i].cache_time, (int)Mibcache[i].cache_ttl));
246 if (Mibcache[i].cache_time < period) {
247 Mibcache[i].cache_time = 0;
248 } else {
249 Mibcache[i].cache_time -= period;
250 }
251 }
252 }
253
254 void
255 init_kernel_sunos5(void)
256 {
257 static int creg = 0;
258 const int period = 3;
259 int alarm_id = 0;
260
261 if (creg == 0) {
262 alarm_id = snmp_alarm_register(period, SA_REPEAT,
263 kernel_sunos5_cache_age,
264 (void *)period);
265 DEBUGMSGTL(("kernel_sunos5", "registered alarm %d with period %ds\n",
266 alarm_id, period));
267 ++creg;
268 }
269 }
270
271 /*
272 * Get various kernel statistics using undocumented Solaris kstat interface.
273 * We need it mainly for getting network interface statistics, although it is
274 * generic enough to be used for any purpose. It knows about kstat_headers
275 * module names and by the name of the statistics it tries to figure out the
276 * rest of necessary information. Returns 0 in case of success and < 0 if
277 * there were any errors.
278
279 *
280 * NOTE: To use this function correctly you have to know the actual type of the
281 * value to be returned, so you may build the test program, figure out the type
282 * and use it. Exposing kstat data types to upper layers doesn't seem to be
283 * reasonable. In any case I'd expect more reasonable kstat interface. :-(
284 */
285
286
287 int
288 getKstatInt(const char *classname, const char *statname,
289 const char *varname, int *value)
290 {
291 kstat_ctl_t *ksc;
292 kstat_t *ks;
293 kid_t kid;
294 kstat_named_t *named;
295 int ret = -1; /* fail unless ... */
296
297 if (kstat_fd == 0) {
298 kstat_fd = kstat_open();
299 if (kstat_fd == 0) {
300 snmp_log_perror("kstat_open");
301 }
302 }
303 if ((ksc = kstat_fd) == NULL) {
304 goto Return;
305 }
306 ks = kstat_lookup(ksc, NETSNMP_REMOVE_CONST(char *, classname),
307 -1, NETSNMP_REMOVE_CONST(char *, statname));
308 if (ks == NULL) {
309 DEBUGMSGTL(("kernel_sunos5", "class %s, stat %s not found\n",
310 classname ? classname : "NULL",
311 statname ? statname : "NULL"));
312 goto Return;
313 }
314 kid = kstat_read(ksc, ks, NULL);
315 if (kid == -1) {
316 DEBUGMSGTL(("kernel_sunos5", "cannot read class %s stats %s\n",
317 classname ? classname : "NULL", statname ? statname : "NULL"));
318 goto Return;
319 }
320 named = kstat_data_lookup(ks, NETSNMP_REMOVE_CONST(char *, varname));
321 if (named == NULL) {
322 DEBUGMSGTL(("kernel_sunos5", "no var %s for class %s stat %s\n",
323 varname, classname ? classname : "NULL",
324 statname ? statname : "NULL"));
325 goto Return;
326 }
327
328 ret = 0; /* maybe successful */
329 switch (named->data_type) {
330 #ifdef KSTAT_DATA_INT32 /* Solaris 2.6 and up */
331 case KSTAT_DATA_INT32:
332 *value = named->value.i32;
333 break;
334 case KSTAT_DATA_UINT32:
335 *value = named->value.ui32;
336 break;
337 case KSTAT_DATA_INT64:
338 *value = named->value.i64;
339 break;
340 case KSTAT_DATA_UINT64:
341 *value = named->value.ui64;
342 break;
343 #else
344 case KSTAT_DATA_LONG:
345 *value = named->value.l;
346 break;
347 case KSTAT_DATA_ULONG:
348 *value = named->value.ul;
349 break;
350 case KSTAT_DATA_LONGLONG:
351 *value = named->value.ll;
352 break;
353 case KSTAT_DATA_ULONGLONG:
354 *value = named->value.ull;
355 break;
356 #endif
357 default:
358 snmp_log(LOG_ERR,
359 "non-int type in kstat data: \"%s\" \"%s\" \"%s\" %d\n",
360 classname ? classname : "NULL",
361 statname ? statname : "NULL",
362 varname ? varname : "NULL", named->data_type);
363 ret = -1; /* fail */
364 break;
365 }
366 Return:
367 return ret;
368 }
369
370 int
371 getKstat(const char *statname, const char *varname, void *value)
372 {
373 kstat_ctl_t *ksc;
374 kstat_t *ks, *kstat_data;
375 kstat_named_t *d;
376 uint_t i;
377 int instance = 0;
378 char module_name[64];
379 int ret;
380 u_longlong_t val; /* The largest value */
381 void *v;
382 static char buf[128];
383
384 if (value == NULL) { /* Pretty useless but ... */
385 v = (void *) &val;
386 } else {
387 v = value;
388 }
389
390 if (kstat_fd == 0) {
391 kstat_fd = kstat_open();
392 if (kstat_fd == 0) {
393 snmp_log_perror("kstat_open");
394 }
395 }
396 if ((ksc = kstat_fd) == NULL) {
397 ret = -10;
398 goto Return; /* kstat errors */
399 }
400 if (statname == NULL || varname == NULL) {
401 ret = -20;
402 goto Return;
403 }
404
405 /*
406 * First, get "kstat_headers" statistics. It should
407 * contain all available modules.
408 */
409
410 if ((ks = kstat_lookup(ksc, NETSNMP_REMOVE_CONST(char *, "unix"),
411 0, NETSNMP_REMOVE_CONST(char *, "kstat_headers"))) == NULL) {
412 ret = -10;
413 goto Return; /* kstat errors */
414 }
415 if (kstat_read(ksc, ks, NULL) <= 0) {
416 ret = -10;
417 goto Return; /* kstat errors */
418 }
419 kstat_data = ks->ks_data;
420
421 /*
422 * Now, look for the name of our stat in the headers buf
423 */
424 for (i = 0; i < ks->ks_ndata; i++) {
425 DEBUGMSGTL(("kernel_sunos5",
426 "module: %s instance: %d name: %s class: %s type: %d flags: %x\n",
427 kstat_data[i].ks_module, kstat_data[i].ks_instance,
428 kstat_data[i].ks_name, kstat_data[i].ks_class,
429 kstat_data[i].ks_type, kstat_data[i].ks_flags));
430 if (strcmp(statname, kstat_data[i].ks_name) == 0) {
431 strcpy(module_name, kstat_data[i].ks_module);
432 instance = kstat_data[i].ks_instance;
433 break;
434 }
435 }
436
437 if (i == ks->ks_ndata) {
438 ret = -1;
439 goto Return; /* Not found */
440 }
441
442 /*
443 * Get the named statistics
444 */
445 if ((ks = kstat_lookup(ksc, module_name, instance,
446 NETSNMP_REMOVE_CONST(char *, statname))) == NULL) {
447 ret = -10;
448 goto Return; /* kstat errors */
449 }
450
451 if (kstat_read(ksc, ks, NULL) <= 0) {
452 ret = -10;
453 goto Return; /* kstat errors */
454 }
455 /*
456 * This function expects only name/value type of statistics, so if it is
457 * not the case return an error
458 */
459 if (ks->ks_type != KSTAT_TYPE_NAMED) {
460 ret = -2;
461 goto Return; /* Invalid stat type */
462 }
463
464 for (i = 0, d = KSTAT_NAMED_PTR(ks); i < ks->ks_ndata; i++, d++) {
465 DEBUGMSGTL(("kernel_sunos5", "variable: \"%s\" (type %d)\n",
466 d->name, d->data_type));
467
468 if (strcmp(d->name, varname) == 0) {
469 switch (d->data_type) {
470 case KSTAT_DATA_CHAR:
471 DEBUGMSGTL(("kernel_sunos5", "value: %s\n", d->value.c));
472 *(char **)v = buf;
473 strlcpy(buf, d->value.c, sizeof(buf));
474 break;
475 #ifdef KSTAT_DATA_INT32 /* Solaris 2.6 and up */
476 case KSTAT_DATA_INT32:
477 *(Counter *)v = d->value.i32;
478 DEBUGMSGTL(("kernel_sunos5", "value: %d\n", d->value.i32));
479 break;
480 case KSTAT_DATA_UINT32:
481 *(Counter *)v = d->value.ui32;
482 DEBUGMSGTL(("kernel_sunos5", "value: %u\n", d->value.ui32));
483 break;
484 case KSTAT_DATA_INT64:
485 *(int64_t *)v = d->value.i64;
486 DEBUGMSGTL(("kernel_sunos5", "value: %ld\n", (long)d->value.i64));
487 break;
488 case KSTAT_DATA_UINT64:
489 *(uint64_t *)v = d->value.ui64;
490 DEBUGMSGTL(("kernel_sunos5", "value: %lu\n", (unsigned long)d->value.ui64));
491 break;
492 #else
493 case KSTAT_DATA_LONG:
494 *(Counter *)v = d->value.l;
495 DEBUGMSGTL(("kernel_sunos5", "value: %ld\n", d->value.l));
496 break;
497 case KSTAT_DATA_ULONG:
498 *(Counter *)v = d->value.ul;
499 DEBUGMSGTL(("kernel_sunos5", "value: %lu\n", d->value.ul));
500 break;
501 case KSTAT_DATA_LONGLONG:
502 *(Counter *)v = d->value.ll;
503 DEBUGMSGTL(("kernel_sunos5", "value: %lld\n",
504 (long)d->value.ll));
505 break;
506 case KSTAT_DATA_ULONGLONG:
507 *(Counter *)v = d->value.ull;
508 DEBUGMSGTL(("kernel_sunos5", "value: %llu\n",
509 (unsigned long)d->value.ull));
510 break;
511 #endif
512 case KSTAT_DATA_FLOAT:
513 *(float *)v = d->value.f;
514 DEBUGMSGTL(("kernel_sunos5", "value: %f\n", d->value.f));
515 break;
516 case KSTAT_DATA_DOUBLE:
517 *(double *)v = d->value.d;
518 DEBUGMSGTL(("kernel_sunos5", "value: %f\n", d->value.d));
519 break;
520 default:
521 DEBUGMSGTL(("kernel_sunos5",
522 "UNKNOWN TYPE %d (stat \"%s\" var \"%s\")\n",
523 d->data_type, statname, varname));
524 ret = -3;
525 goto Return; /* Invalid data type */
526 }
527 ret = 0; /* Success */
528 goto Return;
529 }
530 }
531 ret = -4; /* Name not found */
532 Return:
533 return ret;
534 }
535
536 int
537 getKstatString(const char *statname, const char *varname,
538 char *value, size_t value_len)
539 {
540 kstat_ctl_t *ksc;
541 kstat_t *ks, *kstat_data;
542 kstat_named_t *d;
543 size_t i, instance = 0;
544 char module_name[64];
545 int ret;
546
547 if (kstat_fd == 0) {
548 kstat_fd = kstat_open();
549 if (kstat_fd == 0) {
550 snmp_log_perror("kstat_open");
551 }
552 }
553 if ((ksc = kstat_fd) == NULL) {
554 ret = -10;
555 goto Return; /* kstat errors */
556 }
557 if (statname == NULL || varname == NULL) {
558 ret = -20;
559 goto Return;
560 }
561
562 /*
563 * First, get "kstat_headers" statistics. It should
564 * contain all available modules.
565 */
566
567 if ((ks = kstat_lookup(ksc, NETSNMP_REMOVE_CONST(char *, "unix"),
568 0, NETSNMP_REMOVE_CONST(char *, "kstat_headers"))) == NULL) {
569 ret = -10;
570 goto Return; /* kstat errors */
571 }
572 if (kstat_read(ksc, ks, NULL) <= 0) {
573 ret = -10;
574 goto Return; /* kstat errors */
575 }
576 kstat_data = ks->ks_data;
577
578 /*
579 * Now, look for the name of our stat in the headers buf
580 */
581 for (i = 0; i < ks->ks_ndata; i++) {
582 DEBUGMSGTL(("kernel_sunos5",
583 "module: %s instance: %d name: %s class: %s type: %d flags: %x\n",
584 kstat_data[i].ks_module, kstat_data[i].ks_instance,
585 kstat_data[i].ks_name, kstat_data[i].ks_class,
586 kstat_data[i].ks_type, kstat_data[i].ks_flags));
587 if (strcmp(statname, kstat_data[i].ks_name) == 0) {
588 strcpy(module_name, kstat_data[i].ks_module);
589 instance = kstat_data[i].ks_instance;
590 break;
591 }
592 }
593
594 if (i == ks->ks_ndata) {
595 ret = -1;
596 goto Return; /* Not found */
597 }
598
599 /*
600 * Get the named statistics
601 */
602 if ((ks = kstat_lookup(ksc, module_name, instance,
603 NETSNMP_REMOVE_CONST(char *, statname))) == NULL) {
604 ret = -10;
605 goto Return; /* kstat errors */
606 }
607
608 if (kstat_read(ksc, ks, NULL) <= 0) {
609 ret = -10;
610 goto Return; /* kstat errors */
611 }
612 /*
613 * This function expects only name/value type of statistics, so if it is
614 * not the case return an error
615 */
616 if (ks->ks_type != KSTAT_TYPE_NAMED) {
617 ret = -2;
618 goto Return; /* Invalid stat type */
619 }
620
621 for (i = 0, d = KSTAT_NAMED_PTR(ks); i < ks->ks_ndata; i++, d++) {
622 DEBUGMSGTL(("kernel_sunos5", "variable: \"%s\" (type %d)\n",
623 d->name, d->data_type));
624
625 if (strcmp(d->name, varname) == 0) {
626 switch (d->data_type) {
627 case KSTAT_DATA_CHAR:
628 strlcpy(value, d->value.c, value_len);
629 DEBUGMSGTL(("kernel_sunos5", "value: %s\n", d->value.c));
630 break;
631 default:
632 DEBUGMSGTL(("kernel_sunos5",
633 "NONSTRING TYPE %d (stat \"%s\" var \"%s\")\n",
634 d->data_type, statname, varname));
635 ret = -3;
636 goto Return; /* Invalid data type */
637 }
638 ret = 0; /* Success */
639 goto Return;
640 }
641 }
642 ret = -4; /* Name not found */
643 Return:
644 return ret;
645 }
646
647 /*
648 * get MIB-II statistics. It maintaines a simple cache which buffers the last
649 * read block of MIB statistics (which may contain the whole table). It calls
650 * *comp to compare every entry with an entry pointed by arg. *comp should
651 * return 0 if comparison is successful. Req_type may be GET_FIRST, GET_EXACT,
652 * GET_NEXT. If search is successful getMibstat returns 0, otherwise 1.
653 */
654 int
655 getMibstat(mibgroup_e grid, void *resp, size_t entrysize,
656 req_e req_type, int (*comp) (void *, void *), void *arg)
657 {
658 int ret, rc = -1, mibgr, mibtb, cache_valid;
659 size_t length;
660 mibcache *cachep;
661 found_e result = NOT_FOUND;
662 void *ep;
663
664 /*
665 * We assume that Mibcache is initialized in mibgroup_e enum order so we
666 * don't check the validity of index here.
667 */
668
669 DEBUGMSGTL(("kernel_sunos5", "getMibstat (%d, *, %d, %d, *, *)\n",
670 grid, (int)entrysize, req_type));
671 cachep = &Mibcache[grid];
672 mibgr = Mibmap[grid].group;
673 mibtb = Mibmap[grid].table;
674
675 if (cachep->cache_addr == (void *) -1) /* Hasn't been initialized yet */
676 init_mibcache_element(cachep);
677 if (cachep->cache_size == 0) { /* Memory allocation problems */
678 cachep->cache_addr = resp; /* So use caller supplied address instead of cache */
679 cachep->cache_size = entrysize;
680 cachep->cache_last_found = 0;
681 }
682 if (req_type != GET_NEXT)
683 cachep->cache_last_found = 0;
684
685 cache_valid = (cachep->cache_time > 0);
686
687 DEBUGMSGTL(("kernel_sunos5","... cache_valid %d time %ld ttl %d now %ld\n",
688 cache_valid, cachep->cache_time, (int)cachep->cache_ttl,
689 time(NULL)));
690 if (cache_valid) {
691 /*
692 * Is it really?
693 */
694 if (cachep->cache_comp != (void *)comp || cachep->cache_arg != arg) {
695 cache_valid = 0; /* Nope. */
696 }
697 }
698
699 if (cache_valid) {
700 /*
701 * Entry is valid, let's try to find a match
702 */
703
704 if (req_type == GET_NEXT) {
705 result = getentry(req_type,
706 (void *)((char *)cachep->cache_addr +
707 (cachep->cache_last_found * entrysize)),
708 cachep->cache_length -
709 (cachep->cache_last_found * entrysize),
710 entrysize, &ep, comp, arg);
711 } else {
712 result = getentry(req_type, cachep->cache_addr,
713 cachep->cache_length, entrysize, &ep, comp,
714 arg);
715 }
716 }
717
718 if ((cache_valid == 0) || (result == NOT_FOUND) ||
719 (result == NEED_NEXT && cachep->cache_flags & CACHE_MOREDATA)) {
720 /*
721 * Either the cache is old, or we haven't found anything, or need the
722 * next item which hasn't been read yet. In any case, fill the cache
723 * up and try to find our entry.
724 */
725
726 if (grid == MIB_INTERFACES) {
727 rc = getif((mib2_ifEntry_t *) cachep->cache_addr,
728 cachep->cache_size, req_type,
729 (mib2_ifEntry_t *) & ep, &length, comp, arg);
730 } else {
731 rc = getmib(mibgr, mibtb, &(cachep->cache_addr),
732 &(cachep->cache_size), entrysize, req_type, &ep,
733 &length, comp, arg);
734 }
735
736 if (rc >= 0) { /* Cache has been filled up */
737 cachep->cache_time = cachep->cache_ttl;
738 cachep->cache_length = length;
739 if (rc == 1) /* Found but there are more unread data */
740 cachep->cache_flags |= CACHE_MOREDATA;
741 else {
742 cachep->cache_flags &= ~CACHE_MOREDATA;
743 if (rc > 1) {
744 cachep->cache_time = 0;
745 }
746 }
747 cachep->cache_comp = (void *) comp;
748 cachep->cache_arg = arg;
749 } else {
750 cachep->cache_comp = NULL;
751 cachep->cache_arg = NULL;
752 }
753 }
754 DEBUGMSGTL(("kernel_sunos5", "... result %d rc %d\n", result, rc));
755
756 if (result == FOUND || rc == 0 || rc == 1) {
757 /*
758 * Entry has been found, deliver it
759 */
760 if (resp != NULL) {
761 memcpy(resp, ep, entrysize);
762 }
763 ret = 0;
764 cachep->cache_last_found =
765 ((char *)ep - (char *)cachep->cache_addr) / entrysize;
766 } else {
767 ret = 1; /* Not found */
768 }
769 DEBUGMSGTL(("kernel_sunos5", "... getMibstat returns %d\n", ret));
770 return ret;
771 }
772
773 /*
774 * Get a MIB-II entry from the buffer buffaddr, which satisfies the criterion,
775 * computed by (*comp), which gets arg as the first argument and pointer to the
776 * current position in the buffer as the second. If found entry is pointed by
777 * resp.
778 */
779
780 static found_e
781 getentry(req_e req_type, void *bufaddr, size_t len,
782 size_t entrysize, void *resp, int (*comp)(void *, void *),
783 void *arg)
784 {
785 void *bp = bufaddr, **rp = resp;
786 int previous_found = 0;
787
788 if ((len > 0) && (len % entrysize != 0)) {
789 /*
790 * The data in the cache does not make sense, the size must be a
791 * multiple of the entry. Could be caused by alignment issues etc.
792 */
793 DEBUGMSGTL(("kernel_sunos5",
794 "bad cache length %d - not multiple of entry size %d\n",
795 (int)len, (int)entrysize));
796 return NOT_FOUND;
797 }
798
799 /*
800 * Here we have to perform address arithmetic with pointer to void. Ugly...
801 */
802
803 for (; len > 0; len -= entrysize, bp = (char *) bp + entrysize) {
804 if (rp != (void *) NULL) {
805 *rp = bp;
806 }
807
808 if (req_type == GET_FIRST || (req_type == GET_NEXT && previous_found)){
809 return FOUND;
810 }
811
812 if ((*comp)(arg, bp) == 0) {
813 if (req_type == GET_EXACT) {
814 return FOUND;
815 } else { /* GET_NEXT */
816 previous_found++;
817 continue;
818 }
819 }
820 }
821
822 if (previous_found) {
823 return NEED_NEXT;
824 } else {
825 return NOT_FOUND;
826 }
827 }
828
829 /*
830 * Initialize a cache element. It allocates the memory and sets the time stamp
831 * to invalidate the element.
832 */
833 static void
834 init_mibcache_element(mibcache * cp)
835 {
836 if (cp == (mibcache *)NULL) {
837 return;
838 }
839 if (cp->cache_size) {
840 cp->cache_addr = malloc(cp->cache_size);
841 }
842 cp->cache_time = 0;
843 cp->cache_comp = NULL;
844 cp->cache_arg = NULL;
845 }
846
847 /*
848 * Get MIB-II statistics from the Solaris kernel. It uses undocumented
849 * interface to TCP/IP streams modules, which provides extended MIB-II for the
850 * following groups: ip, icmp, tcp, udp, egp.
851
852 *
853 * Usage: groupname, subgroupname are from <inet/mib2.h>,
854 * size%sizeof(statbuf) == 0,
855 * entrysize should be exact size of MIB-II entry,
856 * req_type:
857 * GET_FIRST - get the first entry in the buffer
858 * GET_EXACT - get exact match
859 * GET_NEXT - get next entry after the exact match
860 *
861 * (*comp) is a compare function, provided by the caller, which gets arg as the
862 * first argument and pointer to the current entry as th second. If compared,
863 * should return 0 and found entry will be pointed by resp.
864 *
865 * If search is successful and no more data to read, it returns 0,
866 * if successful and there is more data -- 1,
867 * if not found and end of data -- 2, any other errors -- < 0
868 * (negative error numbers are pretty random).
869 *
870 * NOTE: needs to be protected by a mutex in reentrant environment
871 */
872
873 static int
874 getmib(int groupname, int subgroupname, void **statbuf, size_t *size,
875 size_t entrysize, req_e req_type, void *resp,
876 size_t *length, int (*comp)(void *, void *), void *arg)
877 {
878 int rc, ret = 0, flags;
879 char buf[BUFSIZE];
880 struct strbuf strbuf;
881 struct T_optmgmt_req *tor = (struct T_optmgmt_req *) buf;
882 struct T_optmgmt_ack *toa = (struct T_optmgmt_ack *) buf;
883 struct T_error_ack *tea = (struct T_error_ack *) buf;
884 struct opthdr *req;
885 found_e result = FOUND;
886 size_t oldsize;
887
888 DEBUGMSGTL(("kernel_sunos5", "...... getmib (%d, %d, ...)\n",
889 groupname, subgroupname));
890
891 /*
892 * Open the stream driver and push all MIB-related modules
893 */
894
895 if (sd == -2) { /* First time */
896 if ((sd = open(STREAM_DEV, O_RDWR)) == -1) {
897 snmp_log_perror(STREAM_DEV);
898 ret = -1;
899 goto Return;
900 }
901 if (ioctl(sd, I_PUSH, "tcp") == -1) {
902 snmp_log_perror("I_PUSH tcp");
903 ret = -1;
904 goto Return;
905 }
906 if (ioctl(sd, I_PUSH, "udp") == -1) {
907 snmp_log_perror("I_PUSH udp");
908 ret = -1;
909 goto Return;
910 }
911 DEBUGMSGTL(("kernel_sunos5", "...... modules pushed OK\n"));
912 }
913 if (sd == -1) {
914 ret = -1;
915 goto Return;
916 }
917
918 /*
919 * First, use bigger buffer, to accelerate skipping unwanted messages
920 */
921
922 strbuf.buf = buf;
923 strbuf.maxlen = BUFSIZE;
924
925 tor->PRIM_type = T_OPTMGMT_REQ;
926 tor->OPT_offset = sizeof(struct T_optmgmt_req);
927 tor->OPT_length = sizeof(struct opthdr);
928 #ifdef MI_T_CURRENT
929 tor->MGMT_flags = MI_T_CURRENT; /* Solaris < 2.6 */
930 #else
931 tor->MGMT_flags = T_CURRENT; /* Solaris 2.6 */
932 #endif
933 req = (struct opthdr *)(tor + 1);
934 req->level = groupname;
935 req->name = subgroupname;
936 /*
937 * non-zero len field is used to request extended MIB statistics
938 * on Solaris 10 Update 4 and later. The LEGACY_MIB_SIZE macro is only
939 * available for S10U4+, so we use that to see what action to take.
940 */
941 #ifdef LEGACY_MIB_SIZE
942 req->len = 1; /* ask for extended MIBs */
943 #else
944 req->len = 0;
945 #endif
946 strbuf.len = tor->OPT_length + tor->OPT_offset;
947 flags = 0;
948 if ((rc = putmsg(sd, &strbuf, NULL, flags))) {
949 ret = -2;
950 goto Return;
951 }
952
953 req = (struct opthdr *) (toa + 1);
954 for (;;) {
955 flags = 0;
956 if ((rc = getmsg(sd, &strbuf, NULL, &flags)) == -1) {
957 ret = -EIO;
958 break;
959 }
960 if (rc == 0 && strbuf.len >= sizeof(struct T_optmgmt_ack) &&
961 toa->PRIM_type == T_OPTMGMT_ACK &&
962 toa->MGMT_flags == T_SUCCESS && req->len == 0) {
963 ret = 2;
964 break;
965 }
966 if (strbuf.len >= sizeof(struct T_error_ack) &&
967 tea->PRIM_type == T_ERROR_ACK) {
968 /* Protocol error */
969 ret = -((tea->TLI_error == TSYSERR) ? tea->UNIX_error : EPROTO);
970 break;
971 }
972 if (rc != MOREDATA || strbuf.len < sizeof(struct T_optmgmt_ack) ||
973 toa->PRIM_type != T_OPTMGMT_ACK ||
974 toa->MGMT_flags != T_SUCCESS) {
975 ret = -ENOMSG; /* No more messages */
976 break;
977 }
978
979 /*
980 * The order in which we get the statistics is determined by the kernel
981 * and not by the group name, so we have to loop until we get the
982 * required statistics.
983 */
984
985 if (req->level != groupname || req->name != subgroupname) {
986 strbuf.maxlen = BUFSIZE;
987 strbuf.buf = buf;
988 do {
989 rc = getmsg(sd, NULL, &strbuf, &flags);
990 } while (rc == MOREDATA);
991 continue;
992 }
993
994 /*
995 * Now when we found our stat, switch buffer to a caller-provided
996 * one. Manipulating the size of it one can control performance,
997 * reducing the number of getmsg calls
998 */
999
1000 strbuf.buf = *statbuf;
1001 strbuf.maxlen = *size;
1002 strbuf.len = 0;
1003 flags = 0;
1004 do {
1005 rc = getmsg(sd, NULL, &strbuf, &flags);
1006 switch (rc) {
1007 case -1:
1008 ret = -ENOSR;
1009 snmp_perror("getmsg");
1010 goto Return;
1011
1012 default:
1013 snmp_log(LOG_ERR, "kernel_sunos5/getmib: getmsg returned %d\n", rc);
1014 ret = -ENODATA;
1015 goto Return;
1016
1017 case MOREDATA:
1018 DEBUGMSGTL(("kernel_sunos5", "...... getmib increased buffer size\n"));
1019 oldsize = ( strbuf.buf - (char *)*statbuf) + strbuf.len;
1020 strbuf.buf = (char *)realloc(*statbuf, oldsize+4096);
1021 if(strbuf.buf != NULL) {
1022 *statbuf = strbuf.buf;
1023 *size = oldsize + 4096;
1024 strbuf.buf = (char *)*statbuf + oldsize;
1025 strbuf.maxlen = 4096;
1026 result = NOT_FOUND;
1027 break;
1028 }
1029 strbuf.buf = (char *)*statbuf + (oldsize - strbuf.len);
1030 /* fallthrough */
1031 case 0:
1032 /* fix buffer to real size & position */
1033 strbuf.len += strbuf.buf - (char*)*statbuf;
1034 strbuf.buf = *statbuf;
1035 strbuf.maxlen = *size;
1036
1037 if (req_type == GET_NEXT && result == NEED_NEXT)
1038 /*
1039 * End of buffer, so "next" is the first item in the next
1040 * buffer
1041 */
1042 req_type = GET_FIRST;
1043 result = getentry(req_type, (void *) strbuf.buf, strbuf.len,
1044 entrysize, resp, comp, arg);
1045 *length = strbuf.len; /* To use in caller for cacheing */
1046 break;
1047 }
1048 } while (rc == MOREDATA && result != FOUND);
1049
1050 DEBUGMSGTL(("kernel_sunos5", "...... getmib buffer size is %d\n", (int)*size));
1051
1052 if (result == FOUND) { /* Search is successful */
1053 if (rc != MOREDATA) {
1054 ret = 0; /* Found and no more data */
1055 } else {
1056 ret = 1; /* Found and there is another unread data block */
1057 }
1058 break;
1059 } else { /* Restore buffers, continue search */
1060 strbuf.buf = buf;
1061 strbuf.maxlen = BUFSIZE;
1062 }
1063 }
1064 Return:
1065 if (sd >= 0) ioctl(sd, I_FLUSH, FLUSHRW);
1066 DEBUGMSGTL(("kernel_sunos5", "...... getmib returns %d\n", ret));
1067 return ret;
1068 }
1069
1070 /*
1071 * Get info for interfaces group. Mimics getmib interface as much as possible
1072 * to be substituted later if SunSoft decides to extend its mib2 interface.
1073 */
1074
1075 #if defined(HAVE_IF_NAMEINDEX)
1076
1077 /*
1078 * Use DLPI to obtain information from the NIC.
1079 */
1080
1081 /*
1082 * Open a DLPI device.
1083 *
1084 * On success the file descriptor is returned.
1085 * On error -1 is returned.
1086 */
1087 static int
1088 _dlpi_open(const char *devname)
1089 {
1090 char *devstr;
1091 int fd = -1;
1092 int ppa = -1;
1093
1094 DEBUGMSGTL(("kernel_sunos5", "_dlpi_open called\n"));
1095
1096 if (devname == NULL)
1097 return (-1);
1098
1099 if ((devstr = malloc(5 + strlen(devname) + 1)) == NULL)
1100 return (-1);
1101 (void) sprintf(devstr, "/dev/%s", devname);
1102 DEBUGMSGTL(("kernel_sunos5:dlpi", "devstr(%s)\n", devstr));
1103 /*
1104 * First try opening the device using style 1, if the device does not
1105 * exist we try style 2. Modules will not be pushed, so something like
1106 * ip tunnels will not work.
1107 */
1108
1109 DEBUGMSGTL(("kernel_sunos5:dlpi", "style1 open(%s)\n", devstr));
1110 if ((fd = open(devstr, O_RDWR | O_NONBLOCK)) < 0) {
1111 DEBUGMSGTL(("kernel_sunos5:dlpi", "style1 open failed\n"));
1112 if (_dlpi_parse_devname(devstr, &ppa) == 0) {
1113 DEBUGMSGTL(("kernel_sunos5:dlpi", "style2 parse: %s, %d\n",
1114 devstr, ppa));
1115 /* try style 2 */
1116 DEBUGMSGTL(("kernel_sunos5:dlpi", "style2 open(%s)\n", devstr));
1117
1118 if ((fd = open(devstr, O_RDWR | O_NONBLOCK)) != -1) {
1119 if (_dlpi_attach(fd, ppa) == 0) {
1120 DEBUGMSGTL(("kernel_sunos5:dlpi", "attached\n"));
1121 } else {
1122 DEBUGMSGTL(("kernel_sunos5:dlpi", "attached failed\n"));
1123 close(fd);
1124 fd = -1;
1125 }
1126 } else {
1127 DEBUGMSGTL(("kernel_sunos5:dlpi", "style2 open failed\n"));
1128 }
1129 }
1130 } else {
1131 DEBUGMSGTL(("kernel_sunos5:dlpi", "style1 open succeeded\n"));
1132 }
1133
1134 /* clean up */
1135 free(devstr);
1136
1137 return (fd);
1138 }
1139
1140 /*
1141 * Obtain the physical address of the interface using DLPI
1142 */
1143 static int
1144 _dlpi_get_phys_address(int fd, char *addr, int maxlen, int *addrlen)
1145 {
1146 dl_phys_addr_req_t paddr_req;
1147 union DL_primitives *dlp;
1148 struct strbuf ctlbuf;
1149 char buf[MAX(DL_PHYS_ADDR_ACK_SIZE+64, DL_ERROR_ACK_SIZE)];
1150 int flag = 0;
1151
1152 DEBUGMSGTL(("kernel_sunos5:dlpi", "_dlpi_get_phys_address\n"));
1153
1154 paddr_req.dl_primitive = DL_PHYS_ADDR_REQ;
1155 paddr_req.dl_addr_type = DL_CURR_PHYS_ADDR;
1156 ctlbuf.buf = (char *)&paddr_req;
1157 ctlbuf.len = DL_PHYS_ADDR_REQ_SIZE;
1158 if (putmsg(fd, &ctlbuf, NULL, 0) < 0)
1159 return (-1);
1160
1161 ctlbuf.maxlen = sizeof(buf);
1162 ctlbuf.len = 0;
1163 ctlbuf.buf = buf;
1164 if (getmsg(fd, &ctlbuf, NULL, &flag) < 0)
1165 return (-1);
1166
1167 if (ctlbuf.len < sizeof(uint32_t))
1168 return (-1);
1169 dlp = (union DL_primitives *)buf;
1170 switch (dlp->dl_primitive) {
1171 case DL_PHYS_ADDR_ACK: {
1172 dl_phys_addr_ack_t *phyp = (dl_phys_addr_ack_t *)buf;
1173
1174 DEBUGMSGTL(("kernel_sunos5:dlpi", "got ACK\n"));
1175 if (ctlbuf.len < DL_PHYS_ADDR_ACK_SIZE || phyp->dl_addr_length > maxlen)
1176 return (-1);
1177 (void) memcpy(addr, buf+phyp->dl_addr_offset, phyp->dl_addr_length);
1178 *addrlen = phyp->dl_addr_length;
1179 return (0);
1180 }
1181 case DL_ERROR_ACK: {
1182 dl_error_ack_t *errp = (dl_error_ack_t *)buf;
1183
1184 DEBUGMSGTL(("kernel_sunos5:dlpi", "got ERROR ACK\n"));
1185 if (ctlbuf.len < DL_ERROR_ACK_SIZE)
1186 return (-1);
1187 return (errp->dl_errno);
1188 }
1189 default:
1190 DEBUGMSGTL(("kernel_sunos5:dlpi", "got type: %x\n", (unsigned)dlp->dl_primitive));
1191 return (-1);
1192 }
1193 }
1194
1195 /*
1196 * Query the interface about it's type.
1197 */
1198 static int
1199 _dlpi_get_iftype(int fd, unsigned int *iftype)
1200 {
1201 dl_info_req_t info_req;
1202 union DL_primitives *dlp;
1203 struct strbuf ctlbuf;
1204 char buf[MAX(DL_INFO_ACK_SIZE, DL_ERROR_ACK_SIZE)];
1205 int flag = 0;
1206
1207 DEBUGMSGTL(("kernel_sunos5:dlpi", "_dlpi_get_iftype\n"));
1208
1209 info_req.dl_primitive = DL_INFO_REQ;
1210 ctlbuf.buf = (char *)&info_req;
1211 ctlbuf.len = DL_INFO_REQ_SIZE;
1212 if (putmsg(fd, &ctlbuf, NULL, 0) < 0) {
1213 DEBUGMSGTL(("kernel_sunos5:dlpi", "putmsg failed: %d\nn", errno));
1214 return (-1);
1215 }
1216
1217 ctlbuf.maxlen = sizeof(buf);
1218 ctlbuf.len = 0;
1219 ctlbuf.buf = buf;
1220 if (getmsg(fd, &ctlbuf, NULL, &flag) < 0) {
1221 DEBUGMSGTL(("kernel_sunos5:dlpi", "getmsg failed: %d\n", errno));
1222 return (-1);
1223 }
1224
1225 if (ctlbuf.len < sizeof(uint32_t))
1226 return (-1);
1227 dlp = (union DL_primitives *)buf;
1228 switch (dlp->dl_primitive) {
1229 case DL_INFO_ACK: {
1230 dl_info_ack_t *info = (dl_info_ack_t *)buf;
1231
1232 if (ctlbuf.len < DL_INFO_ACK_SIZE)
1233 return (-1);
1234
1235 DEBUGMSGTL(("kernel_sunos5:dlpi", "dl_mac_type: %x\n",
1236 (unsigned)info->dl_mac_type));
1237 switch (info->dl_mac_type) {
1238 case DL_CSMACD:
1239 case DL_ETHER:
1240 case DL_ETH_CSMA:
1241 *iftype = 6;
1242 break;
1243 case DL_TPB: /* Token Passing Bus */
1244 *iftype = 8;
1245 break;
1246 case DL_TPR: /* Token Passing Ring */
1247 *iftype = 9;
1248 break;
1249 case DL_HDLC:
1250 *iftype = 118;
1251 break;
1252 case DL_FDDI:
1253 *iftype = 15;
1254 break;
1255 case DL_FC: /* Fibre channel */
1256 *iftype = 56;
1257 break;
1258 case DL_ATM:
1259 *iftype = 37;
1260 break;
1261 case DL_X25:
1262 case DL_ISDN:
1263 *iftype = 63;
1264 break;
1265 case DL_HIPPI:
1266 *iftype = 47;
1267 break;
1268 #ifdef DL_IB
1269 case DL_IB:
1270 *iftype = 199;
1271 break;
1272 #endif
1273 case DL_FRAME: /* Frame Relay */
1274 *iftype = 32;
1275 break;
1276 case DL_LOOP:
1277 *iftype = 24;
1278 break;
1279 #ifdef DL_WIFI
1280 case DL_WIFI:
1281 *iftype = 71;
1282 break;
1283 #endif
1284 #ifdef DL_IPV4 /* then IPv6 is also defined */
1285 case DL_IPV4: /* IPv4 Tunnel */
1286 case DL_IPV6: /* IPv6 Tunnel */
1287 *iftype = 131;
1288 break;
1289 #endif
1290 default:
1291 *iftype = 1; /* Other */
1292 break;
1293 }
1294
1295 return (0);
1296 }
1297 case DL_ERROR_ACK: {
1298 dl_error_ack_t *errp = (dl_error_ack_t *)buf;
1299
1300 DEBUGMSGTL(("kernel_sunos5:dlpi",
1301 "got DL_ERROR_ACK: dlpi %ld, error %ld\n",
1302 (long)errp->dl_errno, (long)errp->dl_unix_errno));
1303
1304 if (ctlbuf.len < DL_ERROR_ACK_SIZE)
1305 return (-1);
1306 return (errp->dl_errno);
1307 }
1308 default:
1309 DEBUGMSGTL(("kernel_sunos5:dlpi", "got type %x\n", (unsigned)dlp->dl_primitive));
1310 return (-1);
1311 }
1312 }
1313
1314 static int
1315 _dlpi_attach(int fd, int ppa)
1316 {
1317 dl_attach_req_t attach_req;
1318 struct strbuf ctlbuf;
1319 union DL_primitives *dlp;
1320 char buf[MAX(DL_OK_ACK_SIZE, DL_ERROR_ACK_SIZE)];
1321 int flag = 0;
1322
1323 attach_req.dl_primitive = DL_ATTACH_REQ;
1324 attach_req.dl_ppa = ppa;
1325 ctlbuf.buf = (char *)&attach_req;
1326 ctlbuf.len = DL_ATTACH_REQ_SIZE;
1327 if (putmsg(fd, &ctlbuf, NULL, 0) != 0)
1328 return (-1);
1329
1330 ctlbuf.buf = buf;
1331 ctlbuf.len = 0;
1332 ctlbuf.maxlen = sizeof(buf);
1333 if (getmsg(fd, &ctlbuf, NULL, &flag) != 0)
1334 return (-1);
1335
1336 if (ctlbuf.len < sizeof(uint32_t))
1337 return (-1);
1338
1339 dlp = (union DL_primitives *)buf;
1340 if (dlp->dl_primitive == DL_OK_ACK && ctlbuf.len >= DL_OK_ACK_SIZE)
1341 return (0);
1342 return (-1);
1343 }
1344
1345 static int
1346 _dlpi_parse_devname(char *devname, int *ppap)
1347 {
1348 int ppa = 0;
1349 int m = 1;
1350 int i = strlen(devname) - 1;
1351
1352 while (i >= 0 && isdigit(devname[i] & 0xFF)) {
1353 ppa += m * (devname[i] - '0');
1354 m *= 10;
1355 i--;
1356 }
1357
1358 if (m == 1) {
1359 return (-1);
1360 }
1361 *ppap = ppa;
1362 devname[i + 1] = '\0';
1363
1364 return (0);
1365 }
1366 static int
1367 getif(mib2_ifEntry_t *ifbuf, size_t size, req_e req_type,
1368 mib2_ifEntry_t *resp, size_t *length, int (*comp)(void *, void *),
1369 void *arg)
1370 {
1371 int fd, i, ret;
1372 int ifsd, ifsd6 = -1;
1373 struct lifreq lifreq, *lifrp;
1374 mib2_ifEntry_t *ifp;
1375 int nentries = size / sizeof(mib2_ifEntry_t);
1376 found_e result = NOT_FOUND;
1377 boolean_t if_isv6;
1378 uint64_t if_flags;
1379 struct if_nameindex *ifname, *ifnp;
1380
1381 lifrp = &lifreq;
1382
1383 if ((ifsd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1384 return -1;
1385 }
1386
1387 DEBUGMSGTL(("kernel_sunos5", "...... using if_nameindex\n"));
1388 if ((ifname = if_nameindex()) == NULL) {
1389 ret = -1;
1390 goto Return;
1391 }
1392
1393 /*
1394 * Gather information about each interface found. We try to handle errors
1395 * gracefully: if an error occurs while processing an interface we simply
1396 * move along to the next one. Previously, the function returned with an
1397 * error right away.
1398 *
1399 * if_nameindex() already eliminates duplicate interfaces, so no extra
1400 * checks are needed for interfaces that have both IPv4 and IPv6 plumbed
1401 */
1402 Again:
1403 for (i = 0, ifnp = ifname, ifp = (mib2_ifEntry_t *) ifbuf;
1404 ifnp->if_index != 0 && (i < nentries); ifnp++) {
1405
1406 DEBUGMSGTL(("kernel_sunos5", "...... getif %s\n", ifnp->if_name));
1407 strlcpy(lifrp->lifr_name, ifnp->if_name, LIFNAMSIZ);
1408 if_isv6 = B_FALSE;
1409
1410 if (ioctl(ifsd, SIOCGLIFFLAGS, lifrp) < 0) {
1411 if (ifsd6 == -1) {
1412 if ((ifsd6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1413 ret = -1;
1414 goto Return;
1415 }
1416 }
1417 if (ioctl(ifsd6, SIOCGLIFFLAGS, lifrp) < 0) {
1418 snmp_log(LOG_ERR, "SIOCGLIFFLAGS %s: %s\n",
1419 lifrp->lifr_name, strerror(errno));
1420 continue;
1421 }
1422 if_isv6 = B_TRUE;
1423 }
1424 if_flags = lifrp->lifr_flags;
1425
1426 if (ioctl(if_isv6?ifsd6:ifsd, SIOCGLIFMTU, lifrp) < 0) {
1427 DEBUGMSGTL(("kernel_sunos5", "...... SIOCGLIFMTU failed\n"));
1428 continue;
1429 }
1430
1431 memset(ifp, 0, sizeof(mib2_ifEntry_t));
1432
1433 if ((fd = _dlpi_open(ifnp->if_name)) != -1) {
1434 /* Could open DLPI... now try to grab some info */
1435 (void) _dlpi_get_phys_address(fd, ifp->ifPhysAddress.o_bytes,
1436 sizeof(ifp->ifPhysAddress.o_bytes),
1437 &ifp->ifPhysAddress.o_length);
1438 (void) _dlpi_get_iftype(fd, &ifp->ifType);
1439 close(fd);
1440 }
1441
1442 set_if_info(ifp, ifnp->if_index, ifnp->if_name, if_flags,
1443 lifrp->lifr_metric);
1444
1445 if (get_if_stats(ifp) < 0) {
1446 DEBUGMSGTL(("kernel_sunos5", "...... get_if_stats failed\n"));
1447 continue;
1448 }
1449
1450 /*
1451 * Once we reach here we know that all went well, so move to
1452 * the next ifEntry.
1453 */
1454 i++;
1455 ifp++;
1456 }
1457
1458 if ((req_type == GET_NEXT) && (result == NEED_NEXT)) {
1459 /*
1460 * End of buffer, so "next" is the first item in the next buffer
1461 */
1462 req_type = GET_FIRST;
1463 }
1464
1465 result = getentry(req_type, (void *) ifbuf, size, sizeof(mib2_ifEntry_t),
1466 (void *)resp, comp, arg);
1467
1468 if ((result != FOUND) && (i == nentries) && ifnp->if_index != 0) {
1469 /*
1470 * We reached the end of supplied buffer, but there is
1471 * some more stuff to read, so continue.
1472 */
1473 goto Again;
1474 }
1475
1476 if (result != FOUND) {
1477 ret = 2;
1478 } else {
1479 if (ifnp->if_index != 0) {
1480 ret = 1; /* Found and more data to fetch */
1481 } else {
1482 ret = 0; /* Found and no more data */
1483 }
1484 *length = i * sizeof(mib2_ifEntry_t); /* Actual cache length */
1485 }
1486
1487 Return:
1488 if (ifname)
1489 if_freenameindex(ifname);
1490 close(ifsd);
1491 if (ifsd6 != -1)
1492 close(ifsd6);
1493 return ret;
1494 }
1495 #else /* only rely on SIOCGIFCONF to get interface information */
1496
1497 static int
1498 getif(mib2_ifEntry_t *ifbuf, size_t size, req_e req_type,
1499 mib2_ifEntry_t *resp, size_t *length, int (*comp)(void *, void *),
1500 void *arg)
1501 {
1502 int i, ret, idx = 1;
1503 int ifsd;
1504 static char *buf = NULL;
1505 static int bufsize = 0;
1506 struct ifconf ifconf;
1507 struct ifreq *ifrp;
1508 mib2_ifEntry_t *ifp;
1509 mib2_ipNetToMediaEntry_t Media;
1510 int nentries = size / sizeof(mib2_ifEntry_t);
1511 int if_flags = 0;
1512 found_e result = NOT_FOUND;
1513
1514 if ((ifsd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1515 return -1;
1516 }
1517
1518 if (!buf) {
1519 bufsize = 10240;
1520 buf = malloc(bufsize);
1521 if (!buf) {
1522 ret = -1;
1523 goto Return;
1524 }
1525 }
1526
1527 ifconf.ifc_buf = buf;
1528 ifconf.ifc_len = bufsize;
1529 while (ioctl(ifsd, SIOCGIFCONF, &ifconf) == -1) {
1530 bufsize += 10240;
1531 free(buf);
1532 buf = malloc(bufsize);
1533 if (!buf) {
1534 ret = -1;
1535 goto Return;
1536 }
1537 ifconf.ifc_buf = buf;
1538 ifconf.ifc_len = bufsize;
1539 }
1540
1541 Again:
1542 for (i = 0, ifp = (mib2_ifEntry_t *) ifbuf, ifrp = ifconf.ifc_req;
1543 ((char *) ifrp < ((char *) ifconf.ifc_buf + ifconf.ifc_len))
1544 && (i < nentries); i++, ifp++, ifrp++, idx++) {
1545
1546 DEBUGMSGTL(("kernel_sunos5", "...... getif %s\n", ifrp->ifr_name));
1547
1548 if (ioctl(ifsd, SIOCGIFFLAGS, ifrp) < 0) {
1549 ret = -1;
1550 snmp_log(LOG_ERR, "SIOCGIFFLAGS %s: %s\n", ifrp->ifr_name,
1551 strerror(errno));
1552 goto Return;
1553 }
1554 if_flags = ifrp->ifr_flags;
1555
1556 if (ioctl(ifsd, SIOCGIFMTU, ifrp) < 0) {
1557 ret = -1;
1558 DEBUGMSGTL(("kernel_sunos5", "...... SIOCGIFMTU failed\n"));
1559 goto Return;
1560 }
1561
1562 memset(ifp, 0, sizeof(mib2_ifEntry_t));
1563 set_if_info(ifp, idx, ifrp->ifr_name, if_flags, ifrp->ifr_metric);
1564
1565 if (get_if_stats(ifp) < 0) {
1566 ret = -1;
1567 goto Return;
1568 }
1569 /*
1570 * An attempt to determine the physical address of the interface.
1571 * There should be a more elegant solution using DLPI, but "the margin
1572 * is too small to put it here ..."
1573 */
1574
1575 if (ioctl(ifsd, SIOCGIFADDR, ifrp) < 0) {
1576 ret = -1;
1577 goto Return;
1578 }
1579
1580 if (getMibstat(MIB_IP_NET, &Media, sizeof(mib2_ipNetToMediaEntry_t),
1581 GET_EXACT, &Name_cmp, ifrp) == 0) {
1582 ifp->ifPhysAddress = Media.ipNetToMediaPhysAddress;
1583 }
1584 }
1585
1586 if ((req_type == GET_NEXT) && (result == NEED_NEXT)) {
1587 /*
1588 * End of buffer, so "next" is the first item in the next buffer
1589 */
1590 req_type = GET_FIRST;
1591 }
1592
1593 result = getentry(req_type, (void *) ifbuf, size, sizeof(mib2_ifEntry_t),
1594 (void *)resp, comp, arg);
1595
1596 if ((result != FOUND) && (i == nentries) &&
1597 ((char *)ifrp < (char *)ifconf.ifc_buf + ifconf.ifc_len)) {
1598 /*
1599 * We reached the end of supplied buffer, but there is
1600 * some more stuff to read, so continue.
1601 */
1602 ifconf.ifc_len -= i * sizeof(struct ifreq);
1603 ifconf.ifc_req = ifrp;
1604 goto Again;
1605 }
1606
1607 if (result != FOUND) {
1608 ret = 2;
1609 } else {
1610 if ((char *)ifrp < (char *)ifconf.ifc_buf + ifconf.ifc_len) {
1611 ret = 1; /* Found and more data to fetch */
1612 } else {
1613 ret = 0; /* Found and no more data */
1614 }
1615 *length = i * sizeof(mib2_ifEntry_t); /* Actual cache length */
1616 }
1617
1618 Return:
1619 close(ifsd);
1620 return ret;
1621 }
1622 #endif /*defined(HAVE_IF_NAMEINDEX)*/
1623
1624 static void
1625 set_if_info(mib2_ifEntry_t *ifp, unsigned index, char *name, uint64_t flags,
1626 int mtu)
1627 {
1628 boolean_t havespeed = B_FALSE;
1629 uint64_t ifspeed = 0;
1630
1631 /*
1632 * Set basic information
1633 */
1634 ifp->ifIndex = index;
1635 ifp->ifDescr.o_length = strlen(name);
1636 strcpy(ifp->ifDescr.o_bytes, name);
1637 ifp->ifAdminStatus = (flags & IFF_UP) ? 1 : 2;
1638 ifp->ifOperStatus = ((flags & IFF_UP) && (flags & IFF_RUNNING)) ? 1 : 2;
1639 ifp->ifLastChange = 0; /* Who knows ... */
1640 ifp->flags = flags;
1641 ifp->ifMtu = mtu;
1642 ifp->ifSpeed = 0;
1643 ifp->ifHighSpeed = 0;
1644
1645 /*
1646 * Get link speed
1647 */
1648 if ((getKstat(name, "ifspeed", &ifspeed) == 0)) {
1649 /*
1650 * check for SunOS patch with half implemented ifSpeed
1651 */
1652 if (ifspeed > 0 && ifspeed < 10000) {
1653 ifspeed *= 1000000;
1654 }
1655 havespeed = B_TRUE;
1656 } else if (getKstat(name, "ifSpeed", &ifspeed) == 0) {
1657 /*
1658 * this is good
1659 */
1660 havespeed = B_TRUE;
1661 }
1662
1663 if (ifspeed > 0xffffffff) {
1664 ifp->ifSpeed = 0xffffffff;
1665 } else {
1666 ifp->ifSpeed = ifspeed;
1667 }
1668 ifp->ifHighSpeed = ifspeed / 1000000;
1669
1670 /* make ifOperStatus depend on link status if available */
1671 if (ifp->ifAdminStatus == 1) {
1672 int i_tmp;
1673 /* only UPed interfaces get correct link status - if any */
1674 if (getKstatInt(NULL, name,"link_up",&i_tmp) == 0) {
1675 ifp->ifOperStatus = i_tmp ? 1 : 2;
1676 #ifdef IFF_FAILED
1677 } else if (flags & IFF_FAILED) {
1678 /*
1679 * If IPMP is used, and if the daemon marks the interface
1680 * as 'failed', then we know for sure something is amiss.
1681 */
1682 ifp->ifOperStatus = 2;
1683 #endif
1684 } else if (havespeed == B_TRUE && ifp->ifSpeed == 0) {
1685 /* Heuristic */
1686 ifp->ifOperStatus = 2;
1687 }
1688 }
1689
1690 /*
1691 * Set link Type and Speed (if it could not be determined from kstat)
1692 */
1693 if (ifp->ifType == 24) {
1694 ifp->ifSpeed = 127000000;
1695 } else if (ifp->ifType == 1 || ifp->ifType == 0) {
1696 /*
1697 * Could not get the type from DLPI, so lets fall back to the hardcoded
1698 * values.
1699 */
1700 switch (name[0]) {
1701 case 'a': /* ath (802.11) */
1702 if (name[1] == 't' && name[2] == 'h')
1703 ifp->ifType = 71;
1704 break;
1705 case 'l': /* le / lo / lane (ATM LAN Emulation) */
1706 if (name[1] == 'o') {
1707 if (!ifp->ifSpeed)
1708 ifp->ifSpeed = 127000000;
1709 ifp->ifType = 24;
1710 } else if (name[1] == 'e') {
1711 if (!ifp->ifSpeed)
1712 ifp->ifSpeed = 10000000;
1713 ifp->ifType = 6;
1714 } else if (name[1] == 'a') {
1715 if (!ifp->ifSpeed)
1716 ifp->ifSpeed = 155000000;
1717 ifp->ifType = 37;
1718 }
1719 break;
1720
1721 case 'g': /* ge (gigabit ethernet card) */
1722 case 'c': /* ce (Cassini Gigabit-Ethernet (PCI) */
1723 if (!ifp->ifSpeed)
1724 ifp->ifSpeed = 1000000000;
1725 ifp->ifType = 6;
1726 break;
1727
1728 case 'h': /* hme (SBus card) */
1729 case 'e': /* eri (PCI card) */
1730 case 'b': /* be */
1731 case 'd': /* dmfe -- found on netra X1 */
1732 if (!ifp->ifSpeed)
1733 ifp->ifSpeed = 100000000;
1734 ifp->ifType = 6;
1735 break;
1736
1737 case 'f': /* fa (Fore ATM) */
1738 if (!ifp->ifSpeed)
1739 ifp->ifSpeed = 155000000;
1740 ifp->ifType = 37;
1741 break;
1742
1743 case 'q': /* qe (QuadEther)/qa (Fore ATM)/qfe (QuadFastEther) */
1744 if (name[1] == 'a') {
1745 if (!ifp->ifSpeed)
1746 ifp->ifSpeed = 155000000;
1747 ifp->ifType = 37;
1748 } else if (name[1] == 'e') {
1749 if (!ifp->ifSpeed)
1750 ifp->ifSpeed = 10000000;
1751 ifp->ifType = 6;
1752 } else if (name[1] == 'f') {
1753 if (!ifp->ifSpeed)
1754 ifp->ifSpeed = 100000000;
1755 ifp->ifType = 6;
1756 }
1757 break;
1758
1759 case 'i': /* ibd (Infiniband)/ip.tun (IP tunnel) */
1760 if (name[1] == 'b')
1761 ifp->ifType = 199;
1762 else if (name[1] == 'p')
1763 ifp->ifType = 131;
1764 break;
1765 }
1766 }
1767 }
1768
1769 static int
1770 get_if_stats(mib2_ifEntry_t *ifp)
1771 {
1772 int l_tmp;
1773 char *name = ifp->ifDescr.o_bytes;
1774
1775 if (strchr(name, ':'))
1776 return (0);
1777
1778 /*
1779 * First try to grab 64-bit counters; if they are not available,
1780 * fall back to 32-bit.
1781 */
1782 if (getKstat(name, "ipackets64", &ifp->ifHCInUcastPkts) != 0) {
1783 if (getKstatInt(NULL, name, "ipackets", &ifp->ifInUcastPkts) != 0) {
1784 return (-1);
1785 }
1786 } else {
1787 ifp->ifInUcastPkts = (uint32_t)(ifp->ifHCInUcastPkts & 0xffffffff);
1788 }
1789
1790 if (getKstat(name, "rbytes64", &ifp->ifHCInOctets) != 0) {
1791 if (getKstatInt(NULL, name, "rbytes", &ifp->ifInOctets) != 0) {
1792 ifp->ifInOctets = ifp->ifInUcastPkts * 308;
1793 }
1794 } else {
1795 ifp->ifInOctets = (uint32_t)(ifp->ifHCInOctets & 0xffffffff);
1796 }
1797
1798 if (getKstat(name, "opackets64", &ifp->ifHCOutUcastPkts) != 0) {
1799 if (getKstatInt(NULL, name, "opackets", &ifp->ifOutUcastPkts) != 0) {
1800 return (-1);
1801 }
1802 } else {
1803 ifp->ifOutUcastPkts = (uint32_t)(ifp->ifHCOutUcastPkts & 0xffffffff);
1804 }
1805
1806 if (getKstat(name, "obytes64", &ifp->ifHCOutOctets) != 0) {
1807 if (getKstatInt(NULL, name, "obytes", &ifp->ifOutOctets) != 0) {
1808 ifp->ifOutOctets = ifp->ifOutUcastPkts * 308; /* XXX */
1809 }
1810 } else {
1811 ifp->ifOutOctets = (uint32_t)(ifp->ifHCOutOctets & 0xffffffff);
1812 }
1813
1814 if (ifp->ifType == 24) /* Loopback */
1815 return (0);
1816
1817 /* some? VLAN interfaces don't have error counters, so ignore failure */
1818 getKstatInt(NULL, name, "ierrors", &ifp->ifInErrors);
1819 getKstatInt(NULL, name, "oerrors", &ifp->ifOutErrors);
1820
1821 /* Try to grab some additional information */
1822 getKstatInt(NULL, name, "collisions", &ifp->ifCollisions);
1823 getKstatInt(NULL, name, "unknowns", &ifp->ifInUnknownProtos);
1824
1825
1826 /*
1827 * TODO some NICs maintain 64-bit counters for multi/broadcast
1828 * packets; should try to get that information.
1829 */
1830 if (getKstatInt(NULL, name, "brdcstrcv", &l_tmp) == 0)
1831 ifp->ifHCInBroadcastPkts = l_tmp;
1832
1833 if (getKstatInt(NULL, name, "multircv", &l_tmp) == 0)
1834 ifp->ifHCInMulticastPkts = l_tmp;
1835
1836 ifp->ifInNUcastPkts = (uint32_t)(ifp->ifHCInBroadcastPkts +
1837 ifp->ifHCInMulticastPkts);
1838
1839 if (getKstatInt(NULL, name, "brdcstxmt", &l_tmp) == 0)
1840 ifp->ifHCOutBroadcastPkts = l_tmp;
1841
1842 if (getKstatInt(NULL, name, "multixmt", &l_tmp) == 0)
1843 ifp->ifHCOutMulticastPkts = l_tmp;
1844
1845 ifp->ifOutNUcastPkts = (uint32_t)(ifp->ifHCOutBroadcastPkts +
1846 ifp->ifHCOutMulticastPkts);
1847 return(0);
1848 }
1849 /*
1850 * Always TRUE. May be used as a comparison function in getMibstat
1851 * to obtain the whole table (GET_FIRST should be used)
1852 */
1853 int
1854 Get_everything(void *x, void *y)
1855 {
1856 return 0; /* Always TRUE */
1857 }
1858
1859 #if !defined(HAVE_IF_NAMEINDEX)
1860 /*
1861 * Compare name and IP address of the interface to ARP table entry.
1862 * Needed to obtain the physical address of the interface in getif.
1863 */
1864 static int
1865 Name_cmp(void *ifrp, void *ep)
1866 {
1867 struct sockaddr_in *s = (struct sockaddr_in *)
1868 &(((struct ifreq *)ifrp)->ifr_addr);
1869 mib2_ipNetToMediaEntry_t *Ep = (mib2_ipNetToMediaEntry_t *)ep;
1870
1871 if ((strncmp(Ep->ipNetToMediaIfIndex.o_bytes,
1872 ((struct ifreq *)ifrp)->ifr_name,
1873 Ep->ipNetToMediaIfIndex.o_length) == 0) &&
1874 (s->sin_addr.s_addr == Ep->ipNetToMediaNetAddress)) {
1875 return 0;
1876 } else {
1877 return 1;
1878 }
1879 }
1880 #endif
1881
1882 /*
1883 * Try to determine the index of a particular interface. If mfd-rewrites is
1884 * specified, then this function would only be used when the system does not
1885 * have if_nametoindex(3SOCKET).
1886 */
1887 int
1888 solaris2_if_nametoindex(const char *Name, int Len)
1889 {
1890 int i, sd, lastlen = 0, interfaces = 0;
1891 struct ifconf ifc;
1892 struct ifreq *ifrp = NULL;
1893 char *buf = NULL;
1894
1895 if (Name == 0) {
1896 return 0;
1897 }
1898 if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1899 return 0;
1900 }
1901
1902 /*
1903 * Cope with lots of interfaces and brokenness of ioctl SIOCGIFCONF
1904 * on some platforms; see W. R. Stevens, ``Unix Network Programming
1905 * Volume I'', p.435.
1906 */
1907
1908 for (i = 8;; i += 8) {
1909 buf = calloc(i, sizeof(struct ifreq));
1910 if (buf == NULL) {
1911 close(sd);
1912 return 0;
1913 }
1914 ifc.ifc_len = i * sizeof(struct ifreq);
1915 ifc.ifc_buf = (caddr_t) buf;
1916
1917 if (ioctl(sd, SIOCGIFCONF, (char *) &ifc) < 0) {
1918 if (errno != EINVAL || lastlen != 0) {
1919 /*
1920 * Something has gone genuinely wrong.
1921 */
1922 free(buf);
1923 close(sd);
1924 return 0;
1925 }
1926 /*
1927 * Otherwise, it could just be that the buffer is too small.
1928 */
1929 } else {
1930 if (ifc.ifc_len == lastlen) {
1931 /*
1932 * The length is the same as the last time; we're done.
1933 */
1934 break;
1935 }
1936 lastlen = ifc.ifc_len;
1937 }
1938 free(buf);
1939 }
1940
1941 ifrp = ifc.ifc_req;
1942 interfaces = (ifc.ifc_len / sizeof(struct ifreq)) + 1;
1943
1944 for (i = 1; i < interfaces; i++, ifrp++) {
1945 if (strncmp(ifrp->ifr_name, Name, Len) == 0) {
1946 free(buf);
1947 close(sd);
1948 return i;
1949 }
1950 }
1951
1952 free(buf);
1953 close(sd);
1954 return 0;
1955 }
1956
1957 #ifdef _STDC_COMPAT
1958 #ifdef __cplusplus
1959 }
1960 #endif
1961 #endif
1962
1963 #ifdef _GETKSTAT_TEST
1964
1965 int
1966 main(int argc, char **argv)
1967 {
1968 int rc = 0;
1969 u_long val = 0;
1970
1971 if (argc != 3) {
1972 snmp_log(LOG_ERR, "Usage: %s stat_name var_name\n", argv[0]);
1973 exit(1);
1974 }
1975
1976 snmp_set_do_debugging(1);
1977 rc = getKstat(argv[1], argv[2], &val);
1978
1979 if (rc == 0)
1980 snmp_log(LOG_ERR, "%s = %lu\n", argv[2], val);
1981 else
1982 snmp_log(LOG_ERR, "rc =%d\n", rc);
1983 return 0;
1984 }
1985 #endif /*_GETKSTAT_TEST */
1986
1987 #ifdef _GETMIBSTAT_TEST
1988
1989 int
1990 ip20comp(void *ifname, void *ipp)
1991 {
1992 return (strncmp((char *) ifname,
1993 ((mib2_ipAddrEntry_t *) ipp)->ipAdEntIfIndex.o_bytes,
1994 ((mib2_ipAddrEntry_t *) ipp)->ipAdEntIfIndex.
1995 o_length));
1996 }
1997
1998 int
1999 ARP_Cmp_Addr(void *addr, void *ep)
2000 {
2001 DEBUGMSGTL(("kernel_sunos5", "ARP: %lx <> %lx\n",
2002 ((mib2_ipNetToMediaEntry_t *) ep)->ipNetToMediaNetAddress,
2003 *(IpAddress *) addr));
2004 if (((mib2_ipNetToMediaEntry_t *) ep)->ipNetToMediaNetAddress ==
2005 *(IpAddress *)addr) {
2006 return 0;
2007 } else {
2008 return 1;
2009 }
2010 }
2011
2012 int
2013 IF_cmp(void *addr, void *ep)
2014 {
2015 if (((mib2_ifEntry_t *)ep)->ifIndex ==((mib2_ifEntry_t *)addr)->ifIndex) {
2016 return 0;
2017 } else {
2018 return 1;
2019 }
2020 }
2021
2022 int
2023 main(int argc, char **argv)
2024 {
2025 int rc = 0, i, idx;
2026 mib2_ipAddrEntry_t ipbuf, *ipp = &ipbuf;
2027 mib2_ipNetToMediaEntry_t entry, *ep = &entry;
2028 mib2_ifEntry_t ifstat;
2029 req_e req_type;
2030 IpAddress LastAddr = 0;
2031
2032 if (argc != 3) {
2033 snmp_log(LOG_ERR,
2034 "Usage: %s if_name req_type (0 first, 1 exact, 2 next) \n",
2035 argv[0]);
2036 exit(1);
2037 }
2038
2039 switch (atoi(argv[2])) {
2040 case 0:
2041 req_type = GET_FIRST;
2042 break;
2043 case 1:
2044 req_type = GET_EXACT;
2045 break;
2046 case 2:
2047 req_type = GET_NEXT;
2048 break;
2049 };
2050
2051 snmp_set_do_debugging(0);
2052 while ((rc =
2053 getMibstat(MIB_INTERFACES, &ifstat, sizeof(mib2_ifEntry_t),
2054 req_type, &IF_cmp, &idx)) == 0) {
2055 idx = ifstat.ifIndex;
2056 DEBUGMSGTL(("kernel_sunos5", "Ifname = %s\n",
2057 ifstat.ifDescr.o_bytes));
2058 req_type = GET_NEXT;
2059 }
2060 rc = getMibstat(MIB_IP_ADDR, &ipbuf, sizeof(mib2_ipAddrEntry_t),
2061 req_type, ip20comp, argv[1]);
2062
2063 if (rc == 0)
2064 DEBUGMSGTL(("kernel_sunos5", "mtu = %ld\n",
2065 ipp->ipAdEntInfo.ae_mtu));
2066 else
2067 DEBUGMSGTL(("kernel_sunos5", "rc =%d\n", rc));
2068
2069 while ((rc =
2070 getMibstat(MIB_IP_NET, &entry,
2071 sizeof(mib2_ipNetToMediaEntry_t), req_type,
2072 &ARP_Cmp_Addr, &LastAddr)) == 0) {
2073 LastAddr = ep->ipNetToMediaNetAddress;
2074 DEBUGMSGTL(("kernel_sunos5", "Ipaddr = %lX\n", (u_long) LastAddr));
2075 req_type = GET_NEXT;
2076 }
2077 return 0;
2078 }
2079 #endif /*_GETMIBSTAT_TEST */
2080 #endif /* SUNOS5 */