Debian Codegraph @

agent/mibgroup/if-mib/data_access/interface.c

raw
1 /*
2 * Interface MIB architecture support
3 *
4 * $Id$
5 */
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-features.h>
8 #include <net-snmp/net-snmp-includes.h>
9 #include <errno.h>
10
11 #include <net-snmp/agent/net-snmp-agent-includes.h>
12 #include <net-snmp/library/snmp_enum.h>
13 #include <net-snmp/data_access/interface.h>
14
15 #include "mibII/mibII_common.h"
16 #include "if-mib/ifTable/ifTable.h"
17 #include "if-mib/data_access/interface.h"
18 #include "interface_private.h"
19 #if defined(HAVE_PCRE_H)
20 #include <pcre.h>
21 #elif defined(HAVE_REGEX_H)
22 #include <sys/types.h>
23 #include <regex.h>
24 #endif
25
26 netsnmp_feature_child_of(interface_all, libnetsnmpmibs);
27 netsnmp_feature_child_of(interface, interface_all);
28 netsnmp_feature_child_of(interface_access_entry_set_admin_status, interface_all);
29 netsnmp_feature_child_of(interface_legacy, interface_all);
30
31 #ifdef NETSNMP_FEATURE_REQUIRE_INTERFACE_ACCESS_ENTRY_SET_ADMIN_STATUS
32 netsnmp_feature_require(interface_arch_set_admin_status);
33 #endif /* NETSNMP_FEATURE_REQUIRE_INTERFACE_ACCESS_ENTRY_SET_ADMIN_STATUS */
34
35 /**---------------------------------------------------------------------*/
36 /*
37 * local static vars
38 */
39 static netsnmp_conf_if_list *conf_list = NULL;
40 static int need_wrap_check = -1;
41 static int _access_interface_init = 0;
42 static netsnmp_include_if_list *include_list;
43 static int ifmib_max_num_ifaces = 0;
44
45 /*
46 * local static prototypes
47 */
48 static int _access_interface_entry_compare_name(const void *lhs,
49 const void *rhs);
50 static void _access_interface_entry_release(void *entry, void *unused);
51 static void _access_interface_entry_save_name(const char *name, oid index);
52 static void _parse_interface_config(const char *token, char *cptr);
53 static void _parse_ifmib_max_num_ifaces(const char *token, char *cptr);
54 static void _free_interface_config(void);
55 static void _parse_include_if_config(const char *token, char *cptr);
56 static void _free_include_if_config(void);
57
58 /*
59 * This function is called after the snmpd configuration has been read
60 * and loads the interface list if it has not yet been loaded.
61 */
62 static int
63 _load_if_list(int majorID, int minorID, void *serverargs, void *clientarg)
64 {
65 netsnmp_access_interface_init();
66 return 0;
67 }
68
69 /**
70 * initialization
71 */
72 void
73 init_interface(void)
74 {
75 snmpd_register_config_handler("interface", _parse_interface_config,
76 _free_interface_config,
77 "name type speed");
78
79 snmpd_register_config_handler("ifmib_max_num_ifaces",
80 _parse_ifmib_max_num_ifaces,
81 NULL,
82 "IF-MIB MAX Number of ifaces");
83
84 snmpd_register_config_handler("include_ifmib_iface_prefix",
85 _parse_include_if_config,
86 _free_include_if_config,
87 "IF-MIB iface names included");
88
89 snmp_register_callback(SNMP_CALLBACK_LIBRARY,
90 SNMP_CALLBACK_POST_READ_CONFIG,
91 _load_if_list, NULL);
92 }
93
94 /* May be called multiple times. */
95 void
96 netsnmp_access_interface_init(void)
97 {
98 if (1 == _access_interface_init)
99 return;
100
101 _access_interface_init = 1;
102
103 {
104 netsnmp_container * ifcontainer;
105
106 netsnmp_arch_interface_init();
107
108 /*
109 * load once to set up ifIndexes
110 */
111 ifcontainer = netsnmp_access_interface_container_load(NULL, 0);
112 if(NULL != ifcontainer)
113 netsnmp_access_interface_container_free(ifcontainer, 0);
114 }
115 }
116
117 /**---------------------------------------------------------------------*/
118 /*
119 * container functions
120 */
121 /**
122 * initialize interface container
123 */
124 netsnmp_container *
125 netsnmp_access_interface_container_init(u_int flags)
126 {
127 netsnmp_container *container1;
128
129 DEBUGMSGTL(("access:interface:container", "init\n"));
130
131 /*
132 * create the containers. one indexed by ifIndex, the other
133 * indexed by ifName.
134 */
135 container1 = netsnmp_container_find("access_interface:table_container");
136 if (NULL == container1)
137 return NULL;
138
139 container1->container_name = strdup("interface container");
140 if (flags & NETSNMP_ACCESS_INTERFACE_INIT_ADDL_IDX_BY_NAME) {
141 netsnmp_container *container2 =
142 netsnmp_container_find("access_interface_by_name:access_interface:table_container");
143 if (NULL == container2)
144 return NULL;
145
146 container2->container_name = strdup("interface name container");
147 container2->compare = _access_interface_entry_compare_name;
148
149 netsnmp_container_add_index(container1, container2);
150 }
151
152 return container1;
153 }
154
155 /**
156 * load interface information in specified container
157 *
158 * @param container empty container, or NULL to have one created for you
159 * @param load_flags flags to modify behaviour. Examples:
160 * NETSNMP_ACCESS_INTERFACE_INIT_ADDL_IDX_BY_NAME
161 *
162 * @retval NULL error
163 * @retval !NULL pointer to container
164 */
165 netsnmp_container*
166 netsnmp_access_interface_container_load(netsnmp_container* container, u_int load_flags)
167 {
168 int rc;
169
170 DEBUGMSGTL(("access:interface:container", "load\n"));
171 netsnmp_assert(1 == _access_interface_init);
172
173 if (NULL == container)
174 container = netsnmp_access_interface_container_init(load_flags);
175 if (NULL == container) {
176 snmp_log(LOG_ERR, "no container specified/found for access_interface\n");
177 return NULL;
178 }
179
180 rc = netsnmp_arch_interface_container_load(container, load_flags);
181 if (0 != rc) {
182 netsnmp_access_interface_container_free(container,
183 NETSNMP_ACCESS_INTERFACE_FREE_NOFLAGS);
184 container = NULL;
185 }
186
187 return container;
188 }
189
190 void
191 netsnmp_access_interface_container_free(netsnmp_container *container, u_int free_flags)
192 {
193 DEBUGMSGTL(("access:interface:container", "free\n"));
194
195 if (NULL == container) {
196 snmp_log(LOG_ERR, "invalid container for netsnmp_access_interface_free\n");
197 return;
198 }
199
200 if(! (free_flags & NETSNMP_ACCESS_INTERFACE_FREE_DONT_CLEAR)) {
201 /*
202 * free all items.
203 */
204 CONTAINER_CLEAR(container, _access_interface_entry_release, NULL);
205 }
206
207 CONTAINER_FREE(container);
208 }
209
210 /**
211 * @retval 0 interface not found
212 */
213 oid
214 netsnmp_access_interface_index_find(const char *name)
215 {
216 DEBUGMSGTL(("access:interface:find", "index\n"));
217 netsnmp_assert(1 == _access_interface_init);
218
219 return netsnmp_arch_interface_index_find(name);
220 }
221
222 /**---------------------------------------------------------------------*/
223 /*
224 * ifentry functions
225 */
226 /**
227 */
228 netsnmp_interface_entry *
229 netsnmp_access_interface_entry_get_by_index(netsnmp_container *container, oid index)
230 {
231 netsnmp_index tmp;
232
233 DEBUGMSGTL(("access:interface:entry", "by_index\n"));
234 netsnmp_assert(1 == _access_interface_init);
235
236 if (NULL == container) {
237 snmp_log(LOG_ERR,
238 "invalid container for netsnmp_access_interface_entry_get_by_index\n");
239 return NULL;
240 }
241
242 tmp.len = 1;
243 tmp.oids = &index;
244
245 return (netsnmp_interface_entry *) CONTAINER_FIND(container, &tmp);
246 }
247
248 /**
249 */
250 netsnmp_interface_entry *
251 netsnmp_access_interface_entry_get_by_name(netsnmp_container *container,
252 const char *name)
253 {
254 netsnmp_interface_entry tmp;
255
256 DEBUGMSGTL(("access:interface:entry", "by_name\n"));
257 netsnmp_assert(1 == _access_interface_init);
258
259 if (NULL == container) {
260 snmp_log(LOG_ERR,
261 "invalid container for netsnmp_access_interface_entry_get_by_name\n");
262 return NULL;
263 }
264
265 if (NULL == container->next) {
266 snmp_log(LOG_ERR,
267 "secondary index missing for netsnmp_access_interface_entry_get_by_name\n");
268 return NULL;
269 }
270
271 tmp.name = NETSNMP_REMOVE_CONST(char *, name);
272 return (netsnmp_interface_entry*)CONTAINER_FIND(container->next, &tmp);
273 }
274
275 /**
276 * @retval NULL index not found
277 */
278 const char *
279 netsnmp_access_interface_name_find(oid index)
280 {
281 DEBUGMSGTL(("access:interface:find", "name\n"));
282 netsnmp_assert(1 == _access_interface_init);
283
284 return se_find_label_in_slist("interfaces", index);
285 }
286
287 /**
288 */
289 netsnmp_interface_entry *
290 netsnmp_access_interface_entry_create(const char *name, oid if_index)
291 {
292 netsnmp_interface_entry *entry =
293 SNMP_MALLOC_TYPEDEF(netsnmp_interface_entry);
294
295 DEBUGMSGTL(("access:interface:entry", "create\n"));
296 netsnmp_assert(1 == _access_interface_init);
297
298 if(NULL == entry)
299 return NULL;
300
301 if(NULL != name)
302 entry->name = strdup(name);
303
304 /*
305 * get if index, and save name for reverse lookup
306 */
307 if (0 == if_index)
308 entry->index = netsnmp_access_interface_index_find(name);
309 else
310 entry->index = if_index;
311 netsnmp_assert(entry->index != 0);
312 _access_interface_entry_save_name(name, entry->index);
313
314 if (name)
315 entry->descr = strdup(name);
316
317 /*
318 * make some assumptions
319 */
320 entry->connector_present = 1;
321
322 entry->oid_index.len = 1;
323 entry->oid_index.oids = &entry->index;
324
325 return entry;
326 }
327
328 /**
329 */
330 void
331 netsnmp_access_interface_entry_free(netsnmp_interface_entry * entry)
332 {
333 DEBUGMSGTL(("access:interface:entry", "free\n"));
334
335 if (NULL == entry)
336 return;
337
338 /*
339 * SNMP_FREE not needed, for any of these,
340 * since the whole entry is about to be freed
341 */
342
343 free(entry->old_stats);
344 free(entry->name);
345 free(entry->descr);
346 free(entry->paddr);
347 free(entry);
348 }
349
350 #ifndef NETSNMP_FEATURE_REMOVE_INTERFACE_LEGACY
351 /*
352 * Blech - backwards compatible mibII/interfaces style interface
353 * functions, so we don't have to update older modules to use
354 * the new code to get correct ifIndex values.
355 */
356 #if defined( USING_IF_MIB_IFTABLE_IFTABLE_DATA_ACCESS_MODULE ) && \
357 ! defined( NETSNMP_NO_BACKWARDS_COMPATABILITY )
358
359 static netsnmp_iterator *it = NULL;
360 static ifTable_rowreq_ctx *row = NULL;
361
362 /**
363 * Setup an iterator for scanning the interfaces using the cached entry
364 * from if-mib/ifTable.
365 */
366 void
367 Interface_Scan_Init(void)
368 {
369 netsnmp_container *cont = NULL;
370 netsnmp_cache *cache = NULL;
371
372 cache = netsnmp_cache_find_by_oid(ifTable_oid, ifTable_oid_size);
373 if (NULL != cache) {
374 netsnmp_cache_check_and_reload(cache);
375 cont = (netsnmp_container*) cache->magic;
376 }
377
378 if (NULL != cont) {
379 if (NULL != it)
380 ITERATOR_RELEASE(it);
381
382 it = CONTAINER_ITERATOR(cont);
383 }
384
385 if (NULL != it)
386 row = (ifTable_rowreq_ctx*)ITERATOR_FIRST(it);
387 }
388
389 int
390 Interface_Scan_Next(short *index, char *name, netsnmp_interface_entry **entry,
391 void *dc)
392 {
393 int returnIndex = 0;
394 int ret;
395 if (index)
396 returnIndex = *index;
397
398 ret = Interface_Scan_NextInt( &returnIndex, name, entry, dc );
399 if (index)
400 *index = (returnIndex & 0x8fff);
401 return ret;
402 }
403
404 int
405 Interface_Scan_NextInt(int *index, char *name, netsnmp_interface_entry **entry,
406 void *dc)
407 {
408 netsnmp_interface_entry* e = NULL;
409
410 if (NULL == row)
411 return 0;
412
413 e = row->data.ifentry;
414 if(index)
415 *index = e->index;
416
417 if(name)
418 strcpy(name, e->name);
419
420 if (entry)
421 *entry = e;
422
423 row = (ifTable_rowreq_ctx*) ITERATOR_NEXT(it);
424
425 return 1;
426 }
427 #endif /* NETSNMP_NO_BACKWARDS_COMPATABILITY */
428 #endif /* NETSNMP_FEATURE_REMOVE_INTERFACE_LEGACY */
429
430 #ifndef NETSNMP_FEATURE_REMOVE_INTERFACE_ACCESS_ENTRY_SET_ADMIN_STATUS
431 /**
432 *
433 * @retval 0 : success
434 * @retval < 0 : error
435 */
436 int
437 netsnmp_access_interface_entry_set_admin_status(netsnmp_interface_entry * entry,
438 int ifAdminStatus)
439 {
440 int rc;
441
442 DEBUGMSGTL(("access:interface:entry", "set_admin_status\n"));
443
444 if (NULL == entry)
445 return -1;
446
447 if ((ifAdminStatus < IFADMINSTATUS_UP) ||
448 (ifAdminStatus > IFADMINSTATUS_TESTING))
449 return -2;
450
451 rc = netsnmp_arch_set_admin_status(entry, ifAdminStatus);
452 if (0 == rc) /* success */
453 entry->admin_status = ifAdminStatus;
454
455 return rc;
456 }
457 #endif /* NETSNMP_FEATURE_REMOVE_INTERFACE_ACCESS_ENTRY_SET_ADMIN_STATUS */
458
459 /**---------------------------------------------------------------------*/
460 /*
461 * Utility routines
462 */
463
464 /**
465 */
466 static int
467 _access_interface_entry_compare_name(const void *lhs, const void *rhs)
468 {
469 return strcmp(((const netsnmp_interface_entry *) lhs)->name,
470 ((const netsnmp_interface_entry *) rhs)->name);
471 }
472
473 /**
474 */
475 static void
476 _access_interface_entry_release(void *entry, void *context)
477 {
478 netsnmp_access_interface_entry_free(entry);
479 }
480
481 /**
482 */
483 static void
484 _access_interface_entry_save_name(const char *name, oid index)
485 {
486 int tmp;
487
488 if(NULL == name)
489 return;
490
491 tmp = se_find_value_in_slist("interfaces", name);
492 if (tmp == SE_DNE) {
493 se_add_pair_to_slist("interfaces", strdup(name), index);
494 DEBUGMSGTL(("access:interface:ifIndex",
495 "saved ifIndex %" NETSNMP_PRIo "u for %s\n",
496 index, name));
497 }
498 else
499 if (index != (oid)tmp) {
500 NETSNMP_LOGONCE((LOG_ERR, "IfIndex of an interface changed. Such " \
501 "interfaces will appear multiple times in IF-MIB.\n"));
502 DEBUGMSGTL(("access:interface:ifIndex",
503 "index %" NETSNMP_PRIo "u != tmp for %s\n",
504 index, name));
505 }
506 }
507
508 /**
509 * update stats
510 *
511 * @retval 0 : success
512 * @retval -1 : error
513 */
514 int
515 netsnmp_access_interface_entry_update_stats(netsnmp_interface_entry * prev_vals,
516 netsnmp_interface_entry * new_vals)
517 {
518 DEBUGMSGTL(("access:interface", "check_wrap\n"));
519
520 /*
521 * sanity checks
522 */
523 if ((NULL == prev_vals) || (NULL == new_vals) ||
524 (NULL == prev_vals->name) || (NULL == new_vals->name) ||
525 (0 != strncmp(prev_vals->name, new_vals->name, strlen(prev_vals->name))))
526 return -1;
527
528 /*
529 * if we've determined that we have 64 bit counters, just copy them.
530 */
531 if (0 == need_wrap_check) {
532 memcpy(&prev_vals->stats, &new_vals->stats, sizeof(new_vals->stats));
533 return 0;
534 }
535
536 if (NULL == prev_vals->old_stats) {
537 /*
538 * if we don't have old stats, copy previous stats
539 */
540 prev_vals->old_stats = SNMP_MALLOC_TYPEDEF(netsnmp_interface_stats);
541 if (NULL == prev_vals->old_stats) {
542 return -2;
543 }
544 memcpy(prev_vals->old_stats, &prev_vals->stats, sizeof(prev_vals->stats));
545 }
546
547 if (0 != netsnmp_c64_check32_and_update(&prev_vals->stats.ibytes,
548 &new_vals->stats.ibytes,
549 &prev_vals->old_stats->ibytes,
550 &need_wrap_check))
551 DEBUGMSGTL(("access:interface",
552 "Error expanding ifHCInOctets to 64bits\n"));
553
554 if (new_vals->ns_flags & NETSNMP_INTERFACE_FLAGS_CALCULATE_UCAST) {
555 if (0 != netsnmp_c64_check32_and_update(&prev_vals->stats.iall,
556 &new_vals->stats.iall,
557 &prev_vals->old_stats->iall,
558 &need_wrap_check))
559 DEBUGMSGTL(("access:interface",
560 "Error expanding packet count to 64bits\n"));
561 } else {
562 if (0 != netsnmp_c64_check32_and_update(&prev_vals->stats.iucast,
563 &new_vals->stats.iucast,
564 &prev_vals->old_stats->iucast,
565 &need_wrap_check))
566 DEBUGMSGTL(("access:interface",
567 "Error expanding ifHCInUcastPkts to 64bits\n"));
568 }
569
570 if (0 != netsnmp_c64_check32_and_update(&prev_vals->stats.iucast,
571 &new_vals->stats.iucast,
572 &prev_vals->old_stats->iucast,
573 &need_wrap_check))
574 DEBUGMSGTL(("access:interface",
575 "Error expanding ifHCInUcastPkts to 64bits\n"));
576
577 if (0 != netsnmp_c64_check32_and_update(&prev_vals->stats.imcast,
578 &new_vals->stats.imcast,
579 &prev_vals->old_stats->imcast,
580 &need_wrap_check))
581 DEBUGMSGTL(("access:interface",
582 "Error expanding ifHCInMulticastPkts to 64bits\n"));
583
584 if (0 != netsnmp_c64_check32_and_update(&prev_vals->stats.ibcast,
585 &new_vals->stats.ibcast,
586 &prev_vals->old_stats->ibcast,
587 &need_wrap_check))
588 DEBUGMSGTL(("access:interface",
589 "Error expanding ifHCInBroadcastPkts to 64bits\n"));
590
591 if (0 != netsnmp_c64_check32_and_update(&prev_vals->stats.obytes,
592 &new_vals->stats.obytes,
593 &prev_vals->old_stats->obytes,
594 &need_wrap_check))
595 DEBUGMSGTL(("access:interface",
596 "Error expanding ifHCOutOctets to 64bits\n"));
597
598 if (0 != netsnmp_c64_check32_and_update(&prev_vals->stats.oucast,
599 &new_vals->stats.oucast,
600 &prev_vals->old_stats->oucast,
601 &need_wrap_check))
602 DEBUGMSGTL(("access:interface",
603 "Error expanding ifHCOutUcastPkts to 64bits\n"));
604
605 if (0 != netsnmp_c64_check32_and_update(&prev_vals->stats.omcast,
606 &new_vals->stats.omcast,
607 &prev_vals->old_stats->omcast,
608 &need_wrap_check))
609 DEBUGMSGTL(("access:interface",
610 "Error expanding ifHCOutMulticastPkts to 64bits\n"));
611
612 if (0 != netsnmp_c64_check32_and_update(&prev_vals->stats.obcast,
613 &new_vals->stats.obcast,
614 &prev_vals->old_stats->obcast,
615 &need_wrap_check))
616 DEBUGMSGTL(("access:interface",
617 "Error expanding ifHCOutBroadcastPkts to 64bits\n"));
618
619 /*
620 * Copy 32 bit counters
621 */
622 prev_vals->stats.ierrors = new_vals->stats.ierrors;
623 prev_vals->stats.idiscards = new_vals->stats.idiscards;
624 prev_vals->stats.iunknown_protos = new_vals->stats.iunknown_protos;
625 prev_vals->stats.inucast = new_vals->stats.inucast;
626 prev_vals->stats.oerrors = new_vals->stats.oerrors;
627 prev_vals->stats.odiscards = new_vals->stats.odiscards;
628 prev_vals->stats.oqlen = new_vals->stats.oqlen;
629 prev_vals->stats.collisions = new_vals->stats.collisions;
630 prev_vals->stats.onucast = new_vals->stats.onucast;
631
632 /*
633 * if we've decided we no longer need to check wraps, free old stats
634 */
635 if (0 == need_wrap_check) {
636 SNMP_FREE(prev_vals->old_stats);
637 }
638 else {
639 /*
640 * update old stats from new stats.
641 * careful - old_stats is a pointer to stats...
642 */
643 memcpy(prev_vals->old_stats, &new_vals->stats, sizeof(new_vals->stats));
644 }
645
646 return 0;
647 }
648
649 /**
650 * Calculate stats
651 *
652 * @retval 0 : success
653 * @retval -1 : error
654 */
655 int
656 netsnmp_access_interface_entry_calculate_stats(netsnmp_interface_entry *entry)
657 {
658 DEBUGMSGTL(("access:interface", "calculate_stats\n"));
659 if (entry->ns_flags & NETSNMP_INTERFACE_FLAGS_CALCULATE_UCAST) {
660 u64Subtract(&entry->stats.iall, &entry->stats.imcast,
661 &entry->stats.iucast);
662 }
663 return 0;
664 }
665
666 /**
667 * copy interface entry data (after checking for counter wraps)
668 *
669 * @retval -2 : malloc failed
670 * @retval 0 : no error
671 */
672 int
673 netsnmp_access_interface_entry_copy(netsnmp_interface_entry * lhs,
674 netsnmp_interface_entry * rhs)
675 {
676 DEBUGMSGTL(("access:interface", "copy\n"));
677
678 /*
679 * update stats
680 */
681 netsnmp_access_interface_entry_update_stats(lhs, rhs);
682 netsnmp_access_interface_entry_calculate_stats(lhs);
683
684 /*
685 * update data
686 */
687 lhs->ns_flags = rhs->ns_flags;
688 if (!lhs->descr || !rhs->descr || strcmp(lhs->descr, rhs->descr) != 0) {
689 SNMP_FREE(lhs->descr);
690 if (rhs->descr) {
691 lhs->descr = strdup(rhs->descr);
692 if (!lhs->descr)
693 return -2;
694 }
695 }
696 lhs->type = rhs->type;
697 lhs->speed = rhs->speed;
698 lhs->speed_high = rhs->speed_high;
699 lhs->retransmit_v6 = rhs->retransmit_v6;
700 lhs->retransmit_v4 = rhs->retransmit_v4;
701 lhs->reachable_time = rhs->reachable_time;
702 lhs->mtu = rhs->mtu;
703 lhs->lastchange = rhs->lastchange;
704 lhs->discontinuity = rhs->discontinuity;
705 lhs->reasm_max_v4 = rhs->reasm_max_v4;
706 lhs->reasm_max_v6 = rhs->reasm_max_v6;
707 lhs->admin_status = rhs->admin_status;
708 lhs->oper_status = rhs->oper_status;
709 lhs->promiscuous = rhs->promiscuous;
710 lhs->connector_present = rhs->connector_present;
711 lhs->forwarding_v6 = rhs->forwarding_v6;
712 lhs->os_flags = rhs->os_flags;
713 if(lhs->paddr_len == rhs->paddr_len) {
714 if(rhs->paddr_len)
715 memcpy(lhs->paddr,rhs->paddr,rhs->paddr_len);
716 } else {
717 SNMP_FREE(lhs->paddr);
718 if (rhs->paddr) {
719 lhs->paddr = (char*)malloc(rhs->paddr_len);
720 if(NULL == lhs->paddr)
721 return -2;
722 memcpy(lhs->paddr,rhs->paddr,rhs->paddr_len);
723 }
724 }
725 lhs->paddr_len = rhs->paddr_len;
726
727 return 0;
728 }
729
730 void
731 netsnmp_access_interface_entry_guess_speed(netsnmp_interface_entry *entry)
732 {
733 if (entry->type == IANAIFTYPE_ETHERNETCSMACD)
734 entry->speed = 10000000;
735 else if (entry->type == IANAIFTYPE_SOFTWARELOOPBACK)
736 entry->speed = 10000000;
737 else if (entry->type == IANAIFTYPE_ISO88025TOKENRING)
738 entry->speed = 4000000;
739 else
740 entry->speed = 0;
741 entry->speed_high = entry->speed / 1000000LL;
742 }
743
744 netsnmp_conf_if_list *
745 netsnmp_access_interface_entry_overrides_get(const char * name)
746 {
747 netsnmp_conf_if_list * if_ptr;
748
749 netsnmp_assert(1 == _access_interface_init);
750 if(NULL == name)
751 return NULL;
752
753 for (if_ptr = conf_list; if_ptr; if_ptr = if_ptr->next)
754 if (!strcmp(if_ptr->name, name))
755 break;
756
757 return if_ptr;
758 }
759
760 void
761 netsnmp_access_interface_entry_overrides(netsnmp_interface_entry *entry)
762 {
763 netsnmp_conf_if_list * if_ptr;
764
765 netsnmp_assert(1 == _access_interface_init);
766 if (NULL == entry)
767 return;
768
769 /*
770 * enforce mib size limit
771 */
772 if(entry->descr && (strlen(entry->descr) > 255))
773 entry->descr[255] = 0;
774
775 if_ptr =
776 netsnmp_access_interface_entry_overrides_get(entry->name);
777 if (if_ptr) {
778 entry->type = if_ptr->type;
779 if (if_ptr->speed > 0xffffffff) {
780 entry->speed = 0xffffffff;
781 } else {
782 entry->speed = if_ptr->speed;
783 }
784 entry->speed_high = if_ptr->speed / 1000000LL;
785 }
786 }
787
788 /*
789 * ifmib_max_num_ifaces config token
790 *
791 * Users may configure a maximum number if interfaces for
792 * the IF-MIB to include. This is useful in case there are
793 * a large number of interfaces (bridges, bonds, SVIs) that
794 * can slow things down.
795 */
796 int netsnmp_access_interface_max_reached(const char *name)
797 {
798 if (!name)
799 return FALSE;
800
801 if (ifmib_max_num_ifaces == 0)
802 /* nothing was set as a max so we include it all */
803 return FALSE;
804
805 if (netsnmp_arch_interface_index_find(name) > ifmib_max_num_ifaces)
806 /* We have gone over the max configured iface count */
807 return TRUE;
808
809 return FALSE;
810 }
811
812 /*
813 * include_ifmib_iface_prefix config token
814 *
815 * If and only if there is an iface prefix name match, we return TRUE.
816 * If there are no ifaces defined at all, return 1 so that the
817 * default behavior is to include all ifaces (include everything).
818 * (Note: including at least one iface prefix means we will only include
819 * those iface names that match the prefix and exclude all others.)
820 */
821 int netsnmp_access_interface_include(const char *name)
822 {
823 netsnmp_include_if_list *if_ptr;
824 #ifdef HAVE_PCRE_H
825 int found_ndx[3];
826 #endif
827
828 if (!name)
829 return TRUE;
830
831 if (!include_list)
832 /*
833 * If include_ifmib_iface_prefix was not configured, we should include
834 * all interfaces (which is the default).
835 */
836 return TRUE;
837
838
839 for (if_ptr = include_list; if_ptr; if_ptr = if_ptr->next) {
840 #if defined(HAVE_PCRE_H)
841 if (pcre_exec(if_ptr->regex_ptr, NULL, name, strlen(name), 0, 0,
842 found_ndx, 3) >= 0)
843 return TRUE;
844 #elif defined(HAVE_REGEX_H)
845 if (regexec(if_ptr->regex_ptr, name, 0, NULL, 0) == 0)
846 return TRUE;
847 #else
848 if (strncmp(name, if_ptr->name, strlen(if_ptr->name)) == 0)
849 return TRUE;
850 #endif
851 }
852
853 return FALSE;
854 }
855
856 /**---------------------------------------------------------------------*/
857 /*
858 * interface config token
859 */
860 /**
861 */
862 static void
863 _parse_interface_config(const char *token, char *cptr)
864 {
865 netsnmp_conf_if_list *if_ptr, *if_new;
866 char *name, *type, *speed, *ecp;
867 char *st;
868
869 name = strtok_r(cptr, " \t", &st);
870 if (!name) {
871 config_perror("Missing NAME parameter");
872 return;
873 }
874 type = strtok_r(NULL, " \t", &st);
875 if (!type) {
876 config_perror("Missing TYPE parameter");
877 return;
878 }
879 speed = strtok_r(NULL, " \t", &st);
880 if (!speed) {
881 config_perror("Missing SPEED parameter");
882 return;
883 }
884 if_ptr = conf_list;
885 while (if_ptr)
886 if (strcmp(if_ptr->name, name))
887 if_ptr = if_ptr->next;
888 else
889 break;
890 if (if_ptr)
891 config_pwarn("Duplicate interface specification");
892 if_new = SNMP_MALLOC_TYPEDEF(netsnmp_conf_if_list);
893 if (!if_new) {
894 config_perror("Out of memory");
895 return;
896 }
897 if_new->speed = strtoull(speed, &ecp, 0);
898 if (*ecp) {
899 config_perror("Bad SPEED value");
900 free(if_new);
901 return;
902 }
903 if_new->type = strtol(type, &ecp, 0);
904 if (*ecp || if_new->type < 0) {
905 config_perror("Bad TYPE");
906 free(if_new);
907 return;
908 }
909 if_new->name = strdup(name);
910 if (!if_new->name) {
911 config_perror("Out of memory");
912 free(if_new);
913 return;
914 }
915 if_new->next = conf_list;
916 conf_list = if_new;
917 }
918
919 static void
920 _free_interface_config(void)
921 {
922 netsnmp_conf_if_list *if_ptr = conf_list, *if_next;
923 while (if_ptr) {
924 if_next = if_ptr->next;
925 free(NETSNMP_REMOVE_CONST(char *, if_ptr->name));
926 free(if_ptr);
927 if_ptr = if_next;
928 }
929 conf_list = NULL;
930 }
931
932 /*
933 * Maximum number of interfaces to include in IF-MIB
934 */
935 static void
936 _parse_ifmib_max_num_ifaces(const char *token, char *cptr)
937 {
938 int temp_max;
939 char *name, *st;
940
941 errno = 0;
942 name = strtok_r(cptr, " \t", &st);
943 if (!name) {
944 config_perror("Missing NUMBER parameter");
945 return;
946 }
947 if (sscanf(cptr, "%d", &temp_max) != 1) {
948 config_perror("Error converting parameter");
949 return;
950 }
951
952 ifmib_max_num_ifaces = temp_max;
953 }
954
955
956 /*
957 * include interface config token
958 */
959 static void
960 _parse_include_if_config(const char *token, char *cptr)
961 {
962 netsnmp_include_if_list *if_ptr, *if_new;
963 char *name, *st;
964 #if defined(HAVE_PCRE_H)
965 const char *pcre_error;
966 int pcre_error_offset;
967 #elif defined(HAVE_REGEX_H)
968 int r = 0;
969 #endif
970
971 name = strtok_r(cptr, " \t", &st);
972 if (!name) {
973 config_perror("Missing NAME parameter");
974 return;
975 }
976
977 /* check for duplicate prefix configuration */
978 while (name != NULL) {
979 for (if_ptr = include_list; if_ptr; if_ptr = if_ptr->next) {
980 if (strncmp(name, if_ptr->name, strlen(if_ptr->name)) == 0) {
981 config_pwarn("Duplicate include interface prefix specification");
982 return;
983 }
984 }
985 /* now save the prefixes */
986 if_new = SNMP_MALLOC_TYPEDEF(netsnmp_include_if_list);
987 if (!if_new) {
988 config_perror("Out of memory");
989 goto err;
990 }
991 if_new->name = strdup(name);
992 if (!if_new->name) {
993 config_perror("Out of memory");
994 goto err;
995 }
996 #if defined(HAVE_PCRE_H)
997 if_new->regex_ptr = pcre_compile(if_new->name, 0, &pcre_error,
998 &pcre_error_offset, NULL);
999 if (!if_new->regex_ptr) {
1000 config_perror(pcre_error);
1001 goto err;
1002 }
1003 #elif defined(HAVE_REGEX_H)
1004 if_new->regex_ptr = malloc(sizeof(regex_t));
1005 if (!if_new->regex_ptr) {
1006 config_perror("Out of memory");
1007 goto err;
1008 }
1009 r = regcomp(if_new->regex_ptr, if_new->name, REG_NOSUB);
1010 if (r) {
1011 char buf[BUFSIZ];
1012 size_t regerror_len = 0;
1013
1014 regerror_len = regerror(r, if_new->regex_ptr, buf, BUFSIZ);
1015 if (regerror_len >= BUFSIZ)
1016 buf[BUFSIZ - 1] = '\0';
1017 else
1018 buf[regerror_len] = '\0';
1019 config_perror(buf);
1020 goto err;
1021 }
1022 #endif
1023 if_new->next = include_list;
1024 include_list = if_new;
1025 if_new = NULL;
1026 name = strtok_r(NULL, " \t", &st);
1027 }
1028 return;
1029
1030 err:
1031 if (if_new) {
1032 #if defined(HAVE_PCRE_H) || defined(HAVE_REGEX_H)
1033 free(if_new->regex_ptr);
1034 #endif
1035 free(if_new->name);
1036 }
1037 free(if_new);
1038 }
1039
1040 static void
1041 _free_include_if_config(void)
1042 {
1043 netsnmp_include_if_list *if_ptr = include_list, *if_next;
1044
1045 while (if_ptr) {
1046 if_next = if_ptr->next;
1047 #if defined(HAVE_PCRE_H)
1048 free(if_ptr->regex_ptr);
1049 #elif defined(HAVE_REGEX_H)
1050 regfree(if_ptr->regex_ptr);
1051 free(if_ptr->regex_ptr);
1052 #endif
1053 free(if_ptr->name);
1054 free(if_ptr);
1055 if_ptr = if_next;
1056 }
1057 include_list = NULL;
1058 }