2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $Begemot: mibII.c 516 2006-10-27 15:54:02Z brandt_h $
31 * Implementation of the standard interfaces and ip MIB.
34 #include "mibII_oid.h"
36 #include <net/if_types.h>
39 /*****************************/
42 static struct lmodule *module;
46 static void *route_fd;
48 /* if-index allocator */
49 static uint32_t next_if_index = 1;
51 /* currently fetching the arp table */
52 static int in_update_arp;
54 /* OR registrations */
55 static u_int ifmib_reg;
56 static u_int ipmib_reg;
57 static u_int tcpmib_reg;
58 static u_int udpmib_reg;
59 static u_int ipForward_reg;
61 /*****************************/
63 /* list of all IP addresses */
64 struct mibifa_list mibifa_list = TAILQ_HEAD_INITIALIZER(mibifa_list);
66 /* list of all interfaces */
67 struct mibif_list mibif_list = TAILQ_HEAD_INITIALIZER(mibif_list);
69 /* list of dynamic interface names */
70 struct mibdynif_list mibdynif_list = SLIST_HEAD_INITIALIZER(mibdynif_list);
72 /* list of all interface index mappings */
73 struct mibindexmap_list mibindexmap_list = STAILQ_HEAD_INITIALIZER(mibindexmap_list);
75 /* list of all stacking entries */
76 struct mibifstack_list mibifstack_list = TAILQ_HEAD_INITIALIZER(mibifstack_list);
78 /* list of all receive addresses */
79 struct mibrcvaddr_list mibrcvaddr_list = TAILQ_HEAD_INITIALIZER(mibrcvaddr_list);
81 /* list of all NetToMedia entries */
82 struct mibarp_list mibarp_list = TAILQ_HEAD_INITIALIZER(mibarp_list);
84 /* number of interfaces */
85 int32_t mib_if_number;
87 /* last change of table */
88 uint64_t mib_iftable_last_change;
90 /* last change of stack table */
91 uint64_t mib_ifstack_last_change;
93 /* if this is set, one of our lists may be bad. refresh them when idle */
99 /* last time refreshed */
100 uint64_t mibarpticks;
102 /* info on system clocks */
103 struct clockinfo clockinfo;
105 /* list of all New if registrations */
106 static struct newifreg_list newifreg_list = TAILQ_HEAD_INITIALIZER(newifreg_list);
108 /* baud rate of fastest interface */
109 uint64_t mibif_maxspeed;
111 /* user-forced update interval */
112 u_int mibif_force_hc_update_interval;
114 /* current update interval */
115 u_int mibif_hc_update_interval;
117 /* HC update timer handle */
118 static void *hc_update_timer;
120 /* Idle poll timer */
121 static void *mibII_poll_timer;
123 /* interfaces' data poll interval */
124 u_int mibII_poll_ticks;
127 static void mibII_idle(void *arg __unused);
129 /*****************************/
131 static const struct asn_oid oid_ifMIB = OIDX_ifMIB;
132 static const struct asn_oid oid_ipMIB = OIDX_ipMIB;
133 static const struct asn_oid oid_tcpMIB = OIDX_tcpMIB;
134 static const struct asn_oid oid_udpMIB = OIDX_udpMIB;
135 static const struct asn_oid oid_ipForward = OIDX_ipForward;
136 static const struct asn_oid oid_linkDown = OIDX_linkDown;
137 static const struct asn_oid oid_linkUp = OIDX_linkUp;
138 static const struct asn_oid oid_ifIndex = OIDX_ifIndex;
140 /*****************************/
146 mib_find_if(u_int idx)
150 TAILQ_FOREACH(ifp, &mibif_list, link)
151 if (ifp->index == idx)
157 mib_find_if_sys(u_int sysindex)
161 TAILQ_FOREACH(ifp, &mibif_list, link)
162 if (ifp->sysindex == sysindex)
168 mib_find_if_name(const char *name)
172 TAILQ_FOREACH(ifp, &mibif_list, link)
173 if (strcmp(ifp->name, name) == 0)
179 * Check whether an interface is dynamic. The argument may include the
180 * unit number. This assumes, that the name part does NOT contain digits.
183 mib_if_is_dyn(const char *name)
188 for (len = 0; name[len] != '\0' && isalpha(name[len]) ; len++)
190 SLIST_FOREACH(d, &mibdynif_list, link)
191 if (strlen(d->name) == len && strncmp(d->name, name, len) == 0)
196 /* set an interface name to dynamic mode */
198 mib_if_set_dyn(const char *name)
202 SLIST_FOREACH(d, &mibdynif_list, link)
203 if (strcmp(name, d->name) == 0)
205 if ((d = malloc(sizeof(*d))) == NULL)
207 strlcpy(d->name, name, sizeof(d->name));
208 SLIST_INSERT_HEAD(&mibdynif_list, d, link);
212 * register for interface creations
215 mib_register_newif(int (*func)(struct mibif *), const struct lmodule *mod)
217 struct newifreg *reg;
219 TAILQ_FOREACH(reg, &newifreg_list, link)
220 if (reg->mod == mod) {
224 if ((reg = malloc(sizeof(*reg))) == NULL) {
225 syslog(LOG_ERR, "newifreg: %m");
230 TAILQ_INSERT_TAIL(&newifreg_list, reg, link);
236 mib_unregister_newif(const struct lmodule *mod)
238 struct newifreg *reg;
240 TAILQ_FOREACH(reg, &newifreg_list, link)
241 if (reg->mod == mod) {
242 TAILQ_REMOVE(&newifreg_list, reg, link);
252 return (TAILQ_FIRST(&mibif_list));
255 mib_next_if(const struct mibif *ifp)
257 return (TAILQ_NEXT(ifp, link));
261 * Change the admin status of an interface
264 mib_if_admin(struct mibif *ifp, int up)
268 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
269 if (ioctl(mib_netsock, SIOCGIFFLAGS, &ifr) == -1) {
270 syslog(LOG_ERR, "SIOCGIFFLAGS(%s): %m", ifp->name);
274 ifr.ifr_flags |= IFF_UP;
276 ifr.ifr_flags &= ~IFF_UP;
277 if (ioctl(mib_netsock, SIOCSIFFLAGS, &ifr) == -1) {
278 syslog(LOG_ERR, "SIOCSIFFLAGS(%s): %m", ifp->name);
282 (void)mib_fetch_ifmib(ifp);
288 * Generate a link up/down trap
291 link_trap(struct mibif *ifp, int up)
293 struct snmp_value ifindex;
295 ifindex.var = oid_ifIndex;
296 ifindex.var.subs[ifindex.var.len++] = ifp->index;
297 ifindex.syntax = SNMP_SYNTAX_INTEGER;
298 ifindex.v.integer = ifp->index;
300 snmp_send_trap(up ? &oid_linkUp : &oid_linkDown, &ifindex,
301 (struct snmp_value *)NULL);
305 * Fetch the GENERIC IFMIB and update the HC counters
308 fetch_generic_mib(struct mibif *ifp, const struct ifmibdata *old)
312 struct mibif_private *p = ifp->private;
316 name[2] = NETLINK_GENERIC;
317 name[3] = IFMIB_IFDATA;
318 name[4] = ifp->sysindex;
319 name[5] = IFDATA_GENERAL;
321 len = sizeof(ifp->mib);
322 if (sysctl(name, nitems(name), &ifp->mib, &len, NULL, 0) == -1) {
324 syslog(LOG_WARNING, "sysctl(ifmib, %s) failed %m",
330 * Assume that one of the two following compounds is optimized away
332 if (ULONG_MAX >= 0xffffffffffffffffULL) {
333 p->hc_inoctets = ifp->mib.ifmd_data.ifi_ibytes;
334 p->hc_outoctets = ifp->mib.ifmd_data.ifi_obytes;
335 p->hc_omcasts = ifp->mib.ifmd_data.ifi_omcasts;
336 p->hc_opackets = ifp->mib.ifmd_data.ifi_opackets;
337 p->hc_imcasts = ifp->mib.ifmd_data.ifi_imcasts;
338 p->hc_ipackets = ifp->mib.ifmd_data.ifi_ipackets;
340 } else if (ULONG_MAX >= 0xffffffff) {
342 #define UPDATE(HC, MIB) \
343 if (old->ifmd_data.MIB > ifp->mib.ifmd_data.MIB) \
344 p->HC += (0x100000000ULL + \
345 ifp->mib.ifmd_data.MIB) - \
346 old->ifmd_data.MIB; \
348 p->HC += ifp->mib.ifmd_data.MIB - \
351 UPDATE(hc_inoctets, ifi_ibytes)
352 UPDATE(hc_outoctets, ifi_obytes)
353 UPDATE(hc_omcasts, ifi_omcasts)
354 UPDATE(hc_opackets, ifi_opackets)
355 UPDATE(hc_imcasts, ifi_imcasts)
356 UPDATE(hc_ipackets, ifi_ipackets)
365 * Update the 64-bit interface counters
368 update_hc_counters(void *arg __unused)
371 struct ifmibdata oldmib;
373 TAILQ_FOREACH(ifp, &mibif_list, link) {
375 (void)fetch_generic_mib(ifp, &oldmib);
380 * Recompute the poll timer for the HC counters
383 mibif_reset_hc_timer(void)
387 if ((ticks = mibif_force_hc_update_interval) == 0) {
388 if (mibif_maxspeed <= IF_Mbps(10)) {
389 /* at 10Mbps overflow needs 3436 seconds */
390 ticks = 3000 * 100; /* 50 minutes */
391 } else if (mibif_maxspeed <= IF_Mbps(100)) {
392 /* at 100Mbps overflow needs 343 seconds */
393 ticks = 300 * 100; /* 5 minutes */
394 } else if (mibif_maxspeed < IF_Mbps(622)) {
395 /* at 622Mbps overflow needs 53 seconds */
396 ticks = 40 * 100; /* 40 seconds */
397 } else if (mibif_maxspeed <= IF_Mbps(1000)) {
398 /* at 1Gbps overflow needs 34 seconds */
399 ticks = 20 * 100; /* 20 seconds */
401 /* at 10Gbps overflow needs 3.4 seconds */
402 ticks = 100; /* 1 seconds */
406 if (ticks == mibif_hc_update_interval)
409 if (hc_update_timer != NULL) {
410 timer_stop(hc_update_timer);
411 hc_update_timer = NULL;
413 update_hc_counters(NULL);
414 if ((hc_update_timer = timer_start_repeat(ticks * 10, ticks * 10,
415 update_hc_counters, NULL, module)) == NULL) {
416 syslog(LOG_ERR, "timer_start(%u): %m", ticks);
419 mibif_hc_update_interval = ticks;
423 * Restart the idle poll timer.
426 mibif_restart_mibII_poll_timer(void)
428 if (mibII_poll_timer != NULL)
429 timer_stop(mibII_poll_timer);
431 if ((mibII_poll_timer = timer_start_repeat(mibII_poll_ticks * 10,
432 mibII_poll_ticks * 10, mibII_idle, NULL, module)) == NULL)
433 syslog(LOG_ERR, "timer_start(%u): %m", mibII_poll_ticks);
437 * Fetch new MIB data.
440 mib_fetch_ifmib(struct mibif *ifp)
445 struct ifmibdata oldmib = ifp->mib;
448 if (fetch_generic_mib(ifp, &oldmib) == -1)
452 * Quoting RFC2863, 3.1.15: "... LinkUp and linkDown traps are
453 * generated just after ifOperStatus leaves, or just before it
454 * enters, the down state, respectively;"
456 if (ifp->trap_enable && ifp->mib.ifmd_data.ifi_link_state !=
457 oldmib.ifmd_data.ifi_link_state &&
458 (ifp->mib.ifmd_data.ifi_link_state == LINK_STATE_DOWN ||
459 oldmib.ifmd_data.ifi_link_state == LINK_STATE_DOWN))
460 link_trap(ifp, ifp->mib.ifmd_data.ifi_link_state ==
461 LINK_STATE_UP ? 1 : 0);
463 ifp->flags &= ~(MIBIF_HIGHSPEED | MIBIF_VERYHIGHSPEED);
464 if (ifp->mib.ifmd_data.ifi_baudrate > 20000000) {
465 ifp->flags |= MIBIF_HIGHSPEED;
466 if (ifp->mib.ifmd_data.ifi_baudrate > 650000000)
467 ifp->flags |= MIBIF_VERYHIGHSPEED;
469 if (ifp->mib.ifmd_data.ifi_baudrate > mibif_maxspeed) {
470 mibif_maxspeed = ifp->mib.ifmd_data.ifi_baudrate;
471 mibif_reset_hc_timer();
479 name[2] = NETLINK_GENERIC;
480 name[3] = IFMIB_IFDATA;
481 name[4] = ifp->sysindex;
482 name[5] = IFDATA_LINKSPECIFIC;
483 if (sysctl(name, nitems(name), NULL, &len, NULL, 0) == -1) {
484 syslog(LOG_WARNING, "sysctl linkmib estimate (%s): %m",
486 if (ifp->specmib != NULL) {
493 if (ifp->specmib != NULL) {
500 if (ifp->specmiblen != len) {
501 if ((newmib = realloc(ifp->specmib, len)) == NULL) {
506 ifp->specmib = newmib;
507 ifp->specmiblen = len;
509 if (sysctl(name, nitems(name), ifp->specmib, &len, NULL, 0) == -1) {
510 syslog(LOG_WARNING, "sysctl linkmib (%s): %m", ifp->name);
511 if (ifp->specmib != NULL) {
518 strlcpy(irr.ifr_name, ifp->name, sizeof(irr.ifr_name));
519 irr.ifr_buffer.buffer = MIBIF_PRIV(ifp)->alias;
520 irr.ifr_buffer.length = sizeof(MIBIF_PRIV(ifp)->alias);
521 if (ioctl(mib_netsock, SIOCGIFDESCR, &irr) == -1) {
522 MIBIF_PRIV(ifp)->alias[0] = 0;
524 syslog(LOG_WARNING, "SIOCGIFDESCR (%s): %m", ifp->name);
525 } else if (irr.ifr_buffer.buffer == NULL) {
526 MIBIF_PRIV(ifp)->alias[0] = 0;
527 syslog(LOG_WARNING, "SIOCGIFDESCR (%s): too long (%zu)",
528 ifp->name, irr.ifr_buffer.length);
530 ifp->mibtick = get_ticks();
534 /* find first/next address for a given interface */
536 mib_first_ififa(const struct mibif *ifp)
540 TAILQ_FOREACH(ifa, &mibifa_list, link)
541 if (ifp->index == ifa->ifindex)
547 mib_next_ififa(struct mibifa *ifa0)
552 while ((ifa = TAILQ_NEXT(ifa, link)) != NULL)
553 if (ifa->ifindex == ifa0->ifindex)
561 static struct mibifa *
562 alloc_ifa(u_int ifindex, struct in_addr addr)
567 if ((ifa = malloc(sizeof(struct mibifa))) == NULL) {
568 syslog(LOG_ERR, "ifa: %m");
572 ifa->ifindex = ifindex;
574 ha = ntohl(ifa->inaddr.s_addr);
576 ifa->index.subs[0] = (ha >> 24) & 0xff;
577 ifa->index.subs[1] = (ha >> 16) & 0xff;
578 ifa->index.subs[2] = (ha >> 8) & 0xff;
579 ifa->index.subs[3] = (ha >> 0) & 0xff;
582 ifa->inbcast.s_addr = 0;
583 ifa->inmask.s_addr = 0xffffffff;
585 INSERT_OBJECT_OID(ifa, &mibifa_list);
591 * Delete an interface address
594 destroy_ifa(struct mibifa *ifa)
596 TAILQ_REMOVE(&mibifa_list, ifa, link);
602 * Helper routine to extract the sockaddr structures from a routing
606 mib_extract_addrs(int addrs, u_char *info, struct sockaddr **out)
610 for (i = 0; i < RTAX_MAX; i++) {
611 if ((addrs & (1 << i)) != 0) {
612 *out = (struct sockaddr *)(void *)info;
613 info += roundup((*out)->sa_len, sizeof(long));
621 * save the phys address of an interface. Handle receive address entries here.
624 get_physaddr(struct mibif *ifp, struct sockaddr_dl *sdl, u_char *ptr)
627 struct mibrcvaddr *rcv;
629 if (sdl->sdl_alen == 0) {
631 if (ifp->physaddrlen != 0) {
632 if ((rcv = mib_find_rcvaddr(ifp->index, ifp->physaddr,
633 ifp->physaddrlen)) != NULL)
634 mib_rcvaddr_delete(rcv);
636 ifp->physaddr = NULL;
637 ifp->physaddrlen = 0;
642 if (ifp->physaddrlen != sdl->sdl_alen) {
644 if (ifp->physaddrlen) {
645 /* delete olf receive address */
646 if ((rcv = mib_find_rcvaddr(ifp->index, ifp->physaddr,
647 ifp->physaddrlen)) != NULL)
648 mib_rcvaddr_delete(rcv);
650 if ((np = realloc(ifp->physaddr, sdl->sdl_alen)) == NULL) {
652 ifp->physaddr = NULL;
653 ifp->physaddrlen = 0;
657 ifp->physaddrlen = sdl->sdl_alen;
659 } else if (memcmp(ifp->physaddr, ptr, ifp->physaddrlen) == 0) {
664 /* address changed */
666 /* delete olf receive address */
667 if ((rcv = mib_find_rcvaddr(ifp->index, ifp->physaddr,
668 ifp->physaddrlen)) != NULL)
669 mib_rcvaddr_delete(rcv);
672 memcpy(ifp->physaddr, ptr, ifp->physaddrlen);
674 /* make new receive address */
675 if ((rcv = mib_rcvaddr_create(ifp, ifp->physaddr, ifp->physaddrlen)) != NULL)
676 rcv->flags |= MIBRCVADDR_HW;
683 mibif_free(struct mibif *ifp)
686 struct mibindexmap *map;
687 struct mibifa *ifa, *ifa1;
688 struct mibrcvaddr *rcv, *rcv1;
689 struct mibarp *at, *at1;
691 if (ifp->xnotify != NULL)
692 (*ifp->xnotify)(ifp, MIBIF_NOTIFY_DESTROY, ifp->xnotify_data);
694 (void)mib_ifstack_delete(ifp, NULL);
695 (void)mib_ifstack_delete(NULL, ifp);
697 TAILQ_REMOVE(&mibif_list, ifp, link);
699 /* if this was the fastest interface - recompute this */
700 if (ifp->mib.ifmd_data.ifi_baudrate == mibif_maxspeed) {
701 mibif_maxspeed = ifp->mib.ifmd_data.ifi_baudrate;
702 TAILQ_FOREACH(ifp1, &mibif_list, link)
703 if (ifp1->mib.ifmd_data.ifi_baudrate > mibif_maxspeed)
705 ifp1->mib.ifmd_data.ifi_baudrate;
706 mibif_reset_hc_timer();
712 ifp->physaddr = NULL;
716 STAILQ_FOREACH(map, &mibindexmap_list, link)
717 if (map->mibif == ifp) {
722 /* purge interface addresses */
723 ifa = TAILQ_FIRST(&mibifa_list);
724 while (ifa != NULL) {
725 ifa1 = TAILQ_NEXT(ifa, link);
726 if (ifa->ifindex == ifp->index)
731 /* purge receive addresses */
732 rcv = TAILQ_FIRST(&mibrcvaddr_list);
733 while (rcv != NULL) {
734 rcv1 = TAILQ_NEXT(rcv, link);
735 if (rcv->ifindex == ifp->index)
736 mib_rcvaddr_delete(rcv);
740 /* purge ARP entries */
741 at = TAILQ_FIRST(&mibarp_list);
743 at1 = TAILQ_NEXT(at, link);
744 if (at->index.subs[0] == ifp->index)
752 mib_iftable_last_change = this_tick;
756 * Create a new interface
758 static struct mibif *
759 mibif_create(u_int sysindex, const char *name)
762 struct mibindexmap *map;
764 if ((ifp = malloc(sizeof(*ifp))) == NULL) {
765 syslog(LOG_WARNING, "%s: %m", __func__);
768 memset(ifp, 0, sizeof(*ifp));
769 if ((ifp->private = malloc(sizeof(struct mibif_private))) == NULL) {
770 syslog(LOG_WARNING, "%s: %m", __func__);
774 memset(ifp->private, 0, sizeof(struct mibif_private));
776 ifp->sysindex = sysindex;
777 strlcpy(ifp->name, name, sizeof(ifp->name));
778 strlcpy(ifp->descr, name, sizeof(ifp->descr));
779 ifp->spec_oid = oid_zeroDotZero;
782 if (!mib_if_is_dyn(ifp->name)) {
783 /* non-dynamic. look whether we know the interface */
784 STAILQ_FOREACH(map, &mibindexmap_list, link)
785 if (strcmp(map->name, ifp->name) == 0) {
786 ifp->index = map->ifindex;
790 /* assume it has a connector if it is not dynamic */
791 ifp->has_connector = 1;
792 ifp->trap_enable = 1;
795 /* new interface - get new index */
796 if (next_if_index > 0x7fffffff)
797 errx(1, "ifindex wrap");
799 if ((map = malloc(sizeof(*map))) == NULL) {
800 syslog(LOG_ERR, "ifmap: %m");
804 map->ifindex = next_if_index++;
805 map->sysindex = ifp->sysindex;
806 strcpy(map->name, ifp->name);
808 STAILQ_INSERT_TAIL(&mibindexmap_list, map, link);
810 /* re-instantiate. Introduce a counter discontinuity */
811 ifp->counter_disc = get_ticks();
813 ifp->index = map->ifindex;
814 ifp->mib.ifmd_data.ifi_link_state = LINK_STATE_UNKNOWN;
816 INSERT_OBJECT_INT(ifp, &mibif_list);
818 mib_iftable_last_change = this_tick;
820 /* instantiate default ifStack entries */
821 (void)mib_ifstack_create(ifp, NULL);
822 (void)mib_ifstack_create(NULL, ifp);
828 * Inform all interested parties about a new interface
831 notify_newif(struct mibif *ifp)
833 struct newifreg *reg;
835 TAILQ_FOREACH(reg, &newifreg_list, link)
836 if ((*reg->func)(ifp))
841 * This is called for new interfaces after we have fetched the interface
842 * MIB. If this is a broadcast interface try to guess the broadcast address
843 * depending on the interface type.
846 check_llbcast(struct mibif *ifp)
848 static u_char ether_bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
849 struct mibrcvaddr *rcv;
851 if (!(ifp->mib.ifmd_flags & IFF_BROADCAST))
854 switch (ifp->mib.ifmd_data.ifi_type) {
860 if (mib_find_rcvaddr(ifp->index, ether_bcast, 6) == NULL &&
861 (rcv = mib_rcvaddr_create(ifp, ether_bcast, 6)) != NULL)
862 rcv->flags |= MIBRCVADDR_BCAST;
869 * Retrieve the current interface list from the system.
872 mib_refresh_iflist(void)
874 struct mibif *ifp, *ifp1;
879 struct ifmibdata mib;
881 TAILQ_FOREACH(ifp, &mibif_list, link)
882 ifp->flags &= ~MIBIF_FOUND;
885 if (sysctlbyname("net.link.generic.system.ifcount", &count, &len,
887 syslog(LOG_ERR, "ifcount: %m");
892 name[2] = NETLINK_GENERIC;
893 name[3] = IFMIB_IFDATA;
894 name[5] = IFDATA_GENERAL;
895 for (idx = 1; idx <= count; idx++) {
898 if (sysctl(name, nitems(name), &mib, &len, NULL, 0) == -1) {
901 syslog(LOG_ERR, "ifmib(%u): %m", idx);
904 if ((ifp = mib_find_if_sys(idx)) != NULL) {
905 ifp->flags |= MIBIF_FOUND;
908 /* Unknown interface - create */
909 if ((ifp = mibif_create(idx, mib.ifmd_name)) != NULL) {
910 ifp->flags |= MIBIF_FOUND;
911 (void)mib_fetch_ifmib(ifp);
918 * Purge interfaces that disappeared
920 ifp = TAILQ_FIRST(&mibif_list);
921 while (ifp != NULL) {
922 ifp1 = TAILQ_NEXT(ifp, link);
923 if (!(ifp->flags & MIBIF_FOUND))
930 * Find an interface address
933 mib_find_ifa(struct in_addr addr)
937 TAILQ_FOREACH(ifa, &mibifa_list, link)
938 if (ifa->inaddr.s_addr == addr.s_addr)
944 * Process a new ARP entry
947 process_arp(const struct rt_msghdr *rtm, const struct sockaddr_dl *sdl,
948 const struct sockaddr_in *sa)
953 /* IP arp table entry */
954 if (sdl->sdl_alen == 0)
956 if ((ifp = mib_find_if_sys(sdl->sdl_index)) == NULL)
958 /* have a valid entry */
959 if ((at = mib_find_arp(ifp, sa->sin_addr)) == NULL &&
960 (at = mib_arp_create(ifp, sa->sin_addr,
961 sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) == NULL)
964 if (rtm->rtm_rmx.rmx_expire == 0)
965 at->flags |= MIBARP_PERM;
967 at->flags &= ~MIBARP_PERM;
968 at->flags |= MIBARP_FOUND;
972 * Handle a routing socket message.
975 handle_rtmsg(struct rt_msghdr *rtm)
977 struct sockaddr *addrs[RTAX_MAX];
978 struct if_msghdr *ifm;
979 struct ifa_msghdr ifam, *ifamp;
980 struct ifma_msghdr *ifmam;
981 #ifdef RTM_IFANNOUNCE
982 struct if_announcemsghdr *ifan;
985 struct sockaddr_dl *sdl;
986 struct sockaddr_in *sa;
988 struct mibrcvaddr *rcv;
991 if (rtm->rtm_version != RTM_VERSION) {
992 syslog(LOG_ERR, "Bogus RTM version %u", rtm->rtm_version);
996 switch (rtm->rtm_type) {
999 ifamp = (struct ifa_msghdr *)rtm;
1000 memcpy(&ifam, ifamp, sizeof(ifam));
1001 mib_extract_addrs(ifam.ifam_addrs, (u_char *)(ifamp + 1), addrs);
1002 if (addrs[RTAX_IFA] == NULL || addrs[RTAX_NETMASK] == NULL)
1005 sa = (struct sockaddr_in *)(void *)addrs[RTAX_IFA];
1006 if ((ifa = mib_find_ifa(sa->sin_addr)) == NULL) {
1007 /* unknown address */
1008 if ((ifp = mib_find_if_sys(ifam.ifam_index)) == NULL) {
1009 syslog(LOG_WARNING, "RTM_NEWADDR for unknown "
1010 "interface %u", ifam.ifam_index);
1013 if ((ifa = alloc_ifa(ifp->index, sa->sin_addr)) == NULL)
1016 sa = (struct sockaddr_in *)(void *)addrs[RTAX_NETMASK];
1017 ifa->inmask = sa->sin_addr;
1019 if (addrs[RTAX_BRD] != NULL) {
1020 sa = (struct sockaddr_in *)(void *)addrs[RTAX_BRD];
1021 ifa->inbcast = sa->sin_addr;
1023 ifa->flags |= MIBIFA_FOUND;
1027 ifamp = (struct ifa_msghdr *)rtm;
1028 memcpy(&ifam, ifamp, sizeof(ifam));
1029 mib_extract_addrs(ifam.ifam_addrs, (u_char *)(ifamp + 1), addrs);
1030 if (addrs[RTAX_IFA] == NULL)
1033 sa = (struct sockaddr_in *)(void *)addrs[RTAX_IFA];
1034 if ((ifa = mib_find_ifa(sa->sin_addr)) != NULL) {
1035 ifa->flags |= MIBIFA_FOUND;
1036 if (!(ifa->flags & MIBIFA_DESTROYED))
1042 ifmam = (struct ifma_msghdr *)rtm;
1043 mib_extract_addrs(ifmam->ifmam_addrs, (u_char *)(ifmam + 1), addrs);
1044 if (addrs[RTAX_IFA] == NULL ||
1045 addrs[RTAX_IFA]->sa_family != AF_LINK)
1047 sdl = (struct sockaddr_dl *)(void *)addrs[RTAX_IFA];
1048 if ((rcv = mib_find_rcvaddr(sdl->sdl_index,
1049 sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) == NULL) {
1050 /* unknown address */
1051 if ((ifp = mib_find_if_sys(sdl->sdl_index)) == NULL) {
1052 syslog(LOG_WARNING, "RTM_NEWMADDR for unknown "
1053 "interface %u", sdl->sdl_index);
1056 if ((rcv = mib_rcvaddr_create(ifp,
1057 sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) == NULL)
1059 rcv->flags |= MIBRCVADDR_VOLATILE;
1061 rcv->flags |= MIBRCVADDR_FOUND;
1065 ifmam = (struct ifma_msghdr *)rtm;
1066 mib_extract_addrs(ifmam->ifmam_addrs, (u_char *)(ifmam + 1), addrs);
1067 if (addrs[RTAX_IFA] == NULL ||
1068 addrs[RTAX_IFA]->sa_family != AF_LINK)
1070 sdl = (struct sockaddr_dl *)(void *)addrs[RTAX_IFA];
1071 if ((rcv = mib_find_rcvaddr(sdl->sdl_index,
1072 sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) != NULL)
1073 mib_rcvaddr_delete(rcv);
1077 ifm = (struct if_msghdr *)(void *)rtm;
1078 mib_extract_addrs(ifm->ifm_addrs, (u_char *)(ifm + 1), addrs);
1079 if ((ifp = mib_find_if_sys(ifm->ifm_index)) == NULL)
1081 if (addrs[RTAX_IFP] != NULL &&
1082 addrs[RTAX_IFP]->sa_family == AF_LINK) {
1083 sdl = (struct sockaddr_dl *)(void *)addrs[RTAX_IFP];
1084 ptr = sdl->sdl_data + sdl->sdl_nlen;
1085 get_physaddr(ifp, sdl, ptr);
1087 (void)mib_fetch_ifmib(ifp);
1090 #ifdef RTM_IFANNOUNCE
1091 case RTM_IFANNOUNCE:
1092 ifan = (struct if_announcemsghdr *)rtm;
1093 ifp = mib_find_if_sys(ifan->ifan_index);
1095 switch (ifan->ifan_what) {
1098 if (ifp == NULL && (ifp = mibif_create(ifan->ifan_index,
1099 ifan->ifan_name)) != NULL) {
1100 (void)mib_fetch_ifmib(ifp);
1106 case IFAN_DEPARTURE:
1115 mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs);
1116 if (rtm->rtm_flags & RTF_LLINFO) {
1117 if (addrs[RTAX_DST] == NULL ||
1118 addrs[RTAX_GATEWAY] == NULL ||
1119 addrs[RTAX_DST]->sa_family != AF_INET ||
1120 addrs[RTAX_GATEWAY]->sa_family != AF_LINK)
1123 (struct sockaddr_dl *)(void *)addrs[RTAX_GATEWAY],
1124 (struct sockaddr_in *)(void *)addrs[RTAX_DST]);
1126 if (rtm->rtm_errno == 0 && (rtm->rtm_flags & RTF_UP))
1127 mib_sroute_process(rtm, addrs[RTAX_GATEWAY],
1128 addrs[RTAX_DST], addrs[RTAX_NETMASK]);
1133 mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs);
1135 if (rtm->rtm_errno == 0 && (rtm->rtm_flags & RTF_UP))
1136 mib_sroute_process(rtm, addrs[RTAX_GATEWAY],
1137 addrs[RTAX_DST], addrs[RTAX_NETMASK]);
1143 * send a routing message
1146 mib_send_rtmsg(struct rt_msghdr *rtm, struct sockaddr *gw,
1147 struct sockaddr *dst, struct sockaddr *mask)
1150 struct rt_msghdr *msg;
1154 len = sizeof(*rtm) + SA_SIZE(gw) + SA_SIZE(dst) + SA_SIZE(mask);
1155 if ((msg = malloc(len)) == NULL) {
1156 syslog(LOG_ERR, "%s: %m", __func__);
1159 cp = (char *)(msg + 1);
1161 memset(msg, 0, sizeof(*msg));
1163 msg->rtm_version = RTM_VERSION;
1164 msg->rtm_addrs = RTA_DST | RTA_GATEWAY;
1166 memcpy(cp, dst, SA_SIZE(dst));
1168 memcpy(cp, gw, SA_SIZE(gw));
1171 memcpy(cp, mask, SA_SIZE(mask));
1172 cp += SA_SIZE(mask);
1173 msg->rtm_addrs |= RTA_NETMASK;
1175 msg->rtm_msglen = cp - (char *)msg;
1176 msg->rtm_type = RTM_GET;
1177 if ((sent = write(route, msg, msg->rtm_msglen)) == -1) {
1178 syslog(LOG_ERR, "%s: write: %m", __func__);
1182 if (sent != msg->rtm_msglen) {
1183 syslog(LOG_ERR, "%s: short write", __func__);
1191 * Fetch the routing table via sysctl
1194 mib_fetch_rtab(int af, int info, int arg, size_t *lenp)
1197 u_char *buf, *newbuf;
1208 /* initial estimate */
1209 if (sysctl(name, nitems(name), NULL, lenp, NULL, 0) == -1) {
1210 syslog(LOG_ERR, "sysctl estimate (%d,%d,%d,%d,%d,%d): %m",
1211 name[0], name[1], name[2], name[3], name[4], name[5]);
1219 if ((newbuf = realloc(buf, *lenp)) == NULL) {
1220 syslog(LOG_ERR, "sysctl buffer: %m");
1226 if (sysctl(name, nitems(name), buf, lenp, NULL, 0) == 0)
1229 if (errno != ENOMEM) {
1230 syslog(LOG_ERR, "sysctl get: %m");
1234 *lenp += *lenp / 8 + 1;
1241 * Update the following info: interface, interface addresses, interface
1242 * receive addresses, arp-table.
1243 * This does not change the interface list itself.
1246 update_ifa_info(void)
1249 struct rt_msghdr *rtm;
1250 struct mibifa *ifa, *ifa1;
1251 struct mibrcvaddr *rcv, *rcv1;
1253 static const int infos[][3] = {
1254 { 0, NET_RT_IFLIST, 0 },
1255 #ifdef NET_RT_IFMALIST
1256 { AF_LINK, NET_RT_IFMALIST, 0 },
1261 TAILQ_FOREACH(ifa, &mibifa_list, link)
1262 ifa->flags &= ~MIBIFA_FOUND;
1263 TAILQ_FOREACH(rcv, &mibrcvaddr_list, link)
1264 rcv->flags &= ~MIBRCVADDR_FOUND;
1266 for (i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
1267 if ((buf = mib_fetch_rtab(infos[i][0], infos[i][1], infos[i][2],
1272 while (next < buf + needed) {
1273 rtm = (struct rt_msghdr *)(void *)next;
1274 next += rtm->rtm_msglen;
1281 * Purge the address list of unused entries. These may happen for
1282 * interface aliases that are on the same subnet. We don't receive
1283 * routing socket messages for them.
1285 ifa = TAILQ_FIRST(&mibifa_list);
1286 while (ifa != NULL) {
1287 ifa1 = TAILQ_NEXT(ifa, link);
1288 if (!(ifa->flags & MIBIFA_FOUND))
1293 rcv = TAILQ_FIRST(&mibrcvaddr_list);
1294 while (rcv != NULL) {
1295 rcv1 = TAILQ_NEXT(rcv, link);
1296 if (!(rcv->flags & (MIBRCVADDR_FOUND | MIBRCVADDR_BCAST |
1298 mib_rcvaddr_delete(rcv);
1307 mib_arp_update(void)
1309 struct mibarp *at, *at1;
1312 struct rt_msghdr *rtm;
1315 return; /* Aaargh */
1318 TAILQ_FOREACH(at, &mibarp_list, link)
1319 at->flags &= ~MIBARP_FOUND;
1321 if ((buf = mib_fetch_rtab(AF_INET, NET_RT_FLAGS, 0, &needed)) == NULL) {
1327 while (next < buf + needed) {
1328 rtm = (struct rt_msghdr *)(void *)next;
1329 next += rtm->rtm_msglen;
1334 at = TAILQ_FIRST(&mibarp_list);
1335 while (at != NULL) {
1336 at1 = TAILQ_NEXT(at, link);
1337 if (!(at->flags & MIBARP_FOUND))
1341 mibarpticks = get_ticks();
1347 * Input on the routing socket.
1350 route_input(int fd, void *udata __unused)
1352 u_char buf[1024 * 16];
1354 struct rt_msghdr *rtm;
1356 if ((n = read(fd, buf, sizeof(buf))) == -1)
1357 err(1, "read(rt_socket)");
1360 errx(1, "EOF on rt_socket");
1362 rtm = (struct rt_msghdr *)(void *)buf;
1363 if ((size_t)n != rtm->rtm_msglen)
1364 errx(1, "n=%zu, rtm_msglen=%u", (size_t)n, rtm->rtm_msglen);
1370 * execute and SIOCAIFADDR
1373 siocaifaddr(char *ifname, struct in_addr addr, struct in_addr mask,
1374 struct in_addr bcast)
1376 struct ifaliasreq addreq;
1377 struct sockaddr_in *sa;
1379 memset(&addreq, 0, sizeof(addreq));
1380 strlcpy(addreq.ifra_name, ifname, sizeof(addreq.ifra_name));
1382 sa = (struct sockaddr_in *)(void *)&addreq.ifra_addr;
1383 sa->sin_family = AF_INET;
1384 sa->sin_len = sizeof(*sa);
1385 sa->sin_addr = addr;
1387 sa = (struct sockaddr_in *)(void *)&addreq.ifra_mask;
1388 sa->sin_family = AF_INET;
1389 sa->sin_len = sizeof(*sa);
1390 sa->sin_addr = mask;
1392 sa = (struct sockaddr_in *)(void *)&addreq.ifra_broadaddr;
1393 sa->sin_family = AF_INET;
1394 sa->sin_len = sizeof(*sa);
1395 sa->sin_addr = bcast;
1397 return (ioctl(mib_netsock, SIOCAIFADDR, &addreq));
1401 * Exececute a SIOCDIFADDR
1404 siocdifaddr(const char *ifname, struct in_addr addr)
1406 struct ifreq delreq;
1407 struct sockaddr_in *sa;
1409 memset(&delreq, 0, sizeof(delreq));
1410 strlcpy(delreq.ifr_name, ifname, sizeof(delreq.ifr_name));
1411 sa = (struct sockaddr_in *)(void *)&delreq.ifr_addr;
1412 sa->sin_family = AF_INET;
1413 sa->sin_len = sizeof(*sa);
1414 sa->sin_addr = addr;
1416 return (ioctl(mib_netsock, SIOCDIFADDR, &delreq));
1420 * Verify an interface address without fetching the entire list
1423 verify_ifa(const char *name, struct mibifa *ifa)
1426 struct sockaddr_in *sa;
1428 memset(&req, 0, sizeof(req));
1429 strlcpy(req.ifr_name, name, sizeof(req.ifr_name));
1430 sa = (struct sockaddr_in *)(void *)&req.ifr_addr;
1431 sa->sin_family = AF_INET;
1432 sa->sin_len = sizeof(*sa);
1433 sa->sin_addr = ifa->inaddr;
1435 if (ioctl(mib_netsock, SIOCGIFADDR, &req) == -1)
1437 if (ifa->inaddr.s_addr != sa->sin_addr.s_addr) {
1438 syslog(LOG_ERR, "%s: address mismatch", __func__);
1442 if (ioctl(mib_netsock, SIOCGIFNETMASK, &req) == -1)
1444 if (ifa->inmask.s_addr != sa->sin_addr.s_addr) {
1445 syslog(LOG_ERR, "%s: netmask mismatch", __func__);
1452 * Restore a deleted interface address. Don't wait for the routing socket
1456 mib_undestroy_ifa(struct mibifa *ifa)
1460 if ((ifp = mib_find_if(ifa->ifindex)) == NULL)
1461 /* keep it destroyed */
1464 if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast))
1465 /* keep it destroyed */
1468 ifa->flags &= ~MIBIFA_DESTROYED;
1472 * Destroy an interface address
1475 mib_destroy_ifa(struct mibifa *ifa)
1479 if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1484 if (siocdifaddr(ifp->name, ifa->inaddr)) {
1486 syslog(LOG_ERR, "SIOCDIFADDR: %m");
1490 ifa->flags |= MIBIFA_DESTROYED;
1495 * Rollback the modification of an address. Don't bother to wait for
1496 * the routing socket.
1499 mib_unmodify_ifa(struct mibifa *ifa)
1503 if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1509 if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast)) {
1520 mib_modify_ifa(struct mibifa *ifa)
1524 if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1530 if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast)) {
1536 if (verify_ifa(ifp->name, ifa)) {
1546 * Destroy a freshly created interface address. Don't bother to wait for
1547 * the routing socket.
1550 mib_uncreate_ifa(struct mibifa *ifa)
1554 if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1559 if (siocdifaddr(ifp->name, ifa->inaddr)) {
1569 * Create a new ifa and verify it
1572 mib_create_ifa(u_int ifindex, struct in_addr addr, struct in_addr mask,
1573 struct in_addr bcast)
1578 if ((ifp = mib_find_if(ifindex)) == NULL)
1580 if ((ifa = alloc_ifa(ifindex, addr)) == NULL)
1583 ifa->inbcast = bcast;
1585 if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast)) {
1586 syslog(LOG_ERR, "%s: %m", __func__);
1590 if (verify_ifa(ifp->name, ifa)) {
1598 * Get all cloning interfaces and make them dynamic.
1599 * Hah! Whe should probably do this on a periodic basis (XXX).
1604 struct if_clonereq req;
1608 memset(&req, 0, sizeof(req));
1609 if (ioctl(mib_netsock, SIOCIFGCLONERS, &req) == -1) {
1610 syslog(LOG_ERR, "get cloners: %m");
1613 if ((buf = malloc(req.ifcr_total * IFNAMSIZ)) == NULL) {
1614 syslog(LOG_ERR, "%m");
1617 req.ifcr_count = req.ifcr_total;
1618 req.ifcr_buffer = buf;
1619 if (ioctl(mib_netsock, SIOCIFGCLONERS, &req) == -1) {
1620 syslog(LOG_ERR, "get cloners: %m");
1624 for (cp = buf, i = 0; i < req.ifcr_total; i++, cp += IFNAMSIZ)
1633 mibII_idle(void *arg __unused)
1637 if (mib_iflist_bad) {
1638 TAILQ_FOREACH(ifa, &mibifa_list, link)
1639 ifa->flags &= ~MIBIFA_DESTROYED;
1641 /* assume, that all cloning interfaces are dynamic */
1644 mib_refresh_iflist();
1660 if ((route_fd = fd_select(route, route_input, NULL, module)) == NULL) {
1661 syslog(LOG_ERR, "fd_select(route): %m");
1664 mib_refresh_iflist();
1667 (void)mib_fetch_route();
1668 mib_iftable_last_change = 0;
1669 mib_ifstack_last_change = 0;
1671 ifmib_reg = or_register(&oid_ifMIB,
1672 "The MIB module to describe generic objects for network interface"
1673 " sub-layers.", module);
1675 ipmib_reg = or_register(&oid_ipMIB,
1676 "The MIB module for managing IP and ICMP implementations, but "
1677 "excluding their management of IP routes.", module);
1679 tcpmib_reg = or_register(&oid_tcpMIB,
1680 "The MIB module for managing TCP implementations.", module);
1682 udpmib_reg = or_register(&oid_udpMIB,
1683 "The MIB module for managing UDP implementations.", module);
1685 ipForward_reg = or_register(&oid_ipForward,
1686 "The MIB module for the display of CIDR multipath IP Routes.",
1689 mibII_poll_timer = NULL;
1690 mibII_poll_ticks = MIBII_POLL_TICKS;
1691 mibif_restart_mibII_poll_timer();
1695 * Initialize the module
1698 mibII_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)
1704 len = sizeof(clockinfo);
1705 if (sysctlbyname("kern.clockrate", &clockinfo, &len, NULL, 0) == -1) {
1706 syslog(LOG_ERR, "kern.clockrate: %m");
1709 if (len != sizeof(clockinfo)) {
1710 syslog(LOG_ERR, "kern.clockrate: wrong size");
1714 if ((route = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) == -1) {
1715 syslog(LOG_ERR, "PF_ROUTE: %m");
1719 if ((mib_netsock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
1720 syslog(LOG_ERR, "PF_INET: %m");
1724 (void)shutdown(mib_netsock, SHUT_RDWR);
1726 /* assume, that all cloning interfaces are dynamic */
1735 if (mibII_poll_timer != NULL ) {
1736 timer_stop(mibII_poll_timer);
1737 mibII_poll_timer = NULL;
1740 if (route_fd != NULL)
1741 fd_deselect(route_fd);
1744 if (mib_netsock != -1)
1745 (void)close(mib_netsock);
1746 /* XXX free memory */
1748 or_unregister(ipForward_reg);
1749 or_unregister(udpmib_reg);
1750 or_unregister(tcpmib_reg);
1751 or_unregister(ipmib_reg);
1752 or_unregister(ifmib_reg);
1758 mibII_loading(const struct lmodule *mod, int loaded)
1765 TAILQ_FOREACH(ifp, &mibif_list, link)
1766 if (ifp->xnotify_mod == mod) {
1767 ifp->xnotify_mod = NULL;
1768 ifp->xnotify_data = NULL;
1769 ifp->xnotify = NULL;
1772 mib_unregister_newif(mod);
1775 const struct snmp_module config = {
1776 "This module implements the interface and ip groups.",
1790 * Should have a list of these attached to each interface.
1793 mibif_notify(struct mibif *ifp, const struct lmodule *mod,
1794 mibif_notify_f func, void *data)
1796 ifp->xnotify = func;
1797 ifp->xnotify_data = data;
1798 ifp->xnotify_mod = mod;
1804 mibif_unnotify(void *arg)
1806 struct mibif *ifp = arg;
1808 ifp->xnotify = NULL;
1809 ifp->xnotify_data = NULL;
1810 ifp->xnotify_mod = NULL;