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 /*****************************/
122 static const struct asn_oid oid_ifMIB = OIDX_ifMIB;
123 static const struct asn_oid oid_ipMIB = OIDX_ipMIB;
124 static const struct asn_oid oid_tcpMIB = OIDX_tcpMIB;
125 static const struct asn_oid oid_udpMIB = OIDX_udpMIB;
126 static const struct asn_oid oid_ipForward = OIDX_ipForward;
127 static const struct asn_oid oid_linkDown = OIDX_linkDown;
128 static const struct asn_oid oid_linkUp = OIDX_linkUp;
129 static const struct asn_oid oid_ifIndex = OIDX_ifIndex;
131 /*****************************/
137 mib_find_if(u_int idx)
141 TAILQ_FOREACH(ifp, &mibif_list, link)
142 if (ifp->index == idx)
148 mib_find_if_sys(u_int sysindex)
152 TAILQ_FOREACH(ifp, &mibif_list, link)
153 if (ifp->sysindex == sysindex)
159 mib_find_if_name(const char *name)
163 TAILQ_FOREACH(ifp, &mibif_list, link)
164 if (strcmp(ifp->name, name) == 0)
170 * Check whether an interface is dynamic. The argument may include the
171 * unit number. This assumes, that the name part does NOT contain digits.
174 mib_if_is_dyn(const char *name)
179 for (len = 0; name[len] != '\0' && isalpha(name[len]) ; len++)
181 SLIST_FOREACH(d, &mibdynif_list, link)
182 if (strlen(d->name) == len && strncmp(d->name, name, len) == 0)
187 /* set an interface name to dynamic mode */
189 mib_if_set_dyn(const char *name)
193 SLIST_FOREACH(d, &mibdynif_list, link)
194 if (strcmp(name, d->name) == 0)
196 if ((d = malloc(sizeof(*d))) == NULL)
198 strcpy(d->name, name);
199 SLIST_INSERT_HEAD(&mibdynif_list, d, link);
203 * register for interface creations
206 mib_register_newif(int (*func)(struct mibif *), const struct lmodule *mod)
208 struct newifreg *reg;
210 TAILQ_FOREACH(reg, &newifreg_list, link)
211 if (reg->mod == mod) {
215 if ((reg = malloc(sizeof(*reg))) == NULL) {
216 syslog(LOG_ERR, "newifreg: %m");
221 TAILQ_INSERT_TAIL(&newifreg_list, reg, link);
227 mib_unregister_newif(const struct lmodule *mod)
229 struct newifreg *reg;
231 TAILQ_FOREACH(reg, &newifreg_list, link)
232 if (reg->mod == mod) {
233 TAILQ_REMOVE(&newifreg_list, reg, link);
243 return (TAILQ_FIRST(&mibif_list));
246 mib_next_if(const struct mibif *ifp)
248 return (TAILQ_NEXT(ifp, link));
252 * Change the admin status of an interface
255 mib_if_admin(struct mibif *ifp, int up)
259 strncpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
260 if (ioctl(mib_netsock, SIOCGIFFLAGS, &ifr) == -1) {
261 syslog(LOG_ERR, "SIOCGIFFLAGS(%s): %m", ifp->name);
265 ifr.ifr_flags |= IFF_UP;
267 ifr.ifr_flags &= ~IFF_UP;
268 if (ioctl(mib_netsock, SIOCSIFFLAGS, &ifr) == -1) {
269 syslog(LOG_ERR, "SIOCSIFFLAGS(%s): %m", ifp->name);
273 (void)mib_fetch_ifmib(ifp);
279 * Generate a link up/down trap
282 link_trap(struct mibif *ifp, int up)
284 struct snmp_value ifindex;
286 ifindex.var = oid_ifIndex;
287 ifindex.var.subs[ifindex.var.len++] = ifp->index;
288 ifindex.syntax = SNMP_SYNTAX_INTEGER;
289 ifindex.v.integer = ifp->index;
291 snmp_send_trap(up ? &oid_linkUp : &oid_linkDown, &ifindex,
292 (struct snmp_value *)NULL);
296 * Fetch the GENERIC IFMIB and update the HC counters
299 fetch_generic_mib(struct mibif *ifp, const struct ifmibdata *old)
303 struct mibif_private *p = ifp->private;
307 name[2] = NETLINK_GENERIC;
308 name[3] = IFMIB_IFDATA;
309 name[4] = ifp->sysindex;
310 name[5] = IFDATA_GENERAL;
312 len = sizeof(ifp->mib);
313 if (sysctl(name, 6, &ifp->mib, &len, NULL, 0) == -1) {
315 syslog(LOG_WARNING, "sysctl(ifmib, %s) failed %m",
321 * Assume that one of the two following compounds is optimized away
323 if (ULONG_MAX >= 0xffffffffffffffffULL) {
324 p->hc_inoctets = ifp->mib.ifmd_data.ifi_ibytes;
325 p->hc_outoctets = ifp->mib.ifmd_data.ifi_obytes;
326 p->hc_omcasts = ifp->mib.ifmd_data.ifi_omcasts;
327 p->hc_opackets = ifp->mib.ifmd_data.ifi_opackets;
328 p->hc_imcasts = ifp->mib.ifmd_data.ifi_imcasts;
329 p->hc_ipackets = ifp->mib.ifmd_data.ifi_ipackets;
331 } else if (ULONG_MAX >= 0xffffffff) {
333 #define UPDATE(HC, MIB) \
334 if (old->ifmd_data.MIB > ifp->mib.ifmd_data.MIB) \
335 p->HC += (0x100000000ULL + \
336 ifp->mib.ifmd_data.MIB) - \
337 old->ifmd_data.MIB; \
339 p->HC += ifp->mib.ifmd_data.MIB - \
342 UPDATE(hc_inoctets, ifi_ibytes)
343 UPDATE(hc_outoctets, ifi_obytes)
344 UPDATE(hc_omcasts, ifi_omcasts)
345 UPDATE(hc_opackets, ifi_opackets)
346 UPDATE(hc_imcasts, ifi_imcasts)
347 UPDATE(hc_ipackets, ifi_ipackets)
356 * Update the 64-bit interface counters
359 update_hc_counters(void *arg __unused)
362 struct ifmibdata oldmib;
364 TAILQ_FOREACH(ifp, &mibif_list, link) {
366 (void)fetch_generic_mib(ifp, &oldmib);
371 * Recompute the poll timer for the HC counters
374 mibif_reset_hc_timer(void)
378 if ((ticks = mibif_force_hc_update_interval) == 0) {
379 if (mibif_maxspeed <= IF_Mbps(10)) {
380 /* at 10Mbps overflow needs 3436 seconds */
381 ticks = 3000 * 100; /* 50 minutes */
382 } else if (mibif_maxspeed <= IF_Mbps(100)) {
383 /* at 100Mbps overflow needs 343 seconds */
384 ticks = 300 * 100; /* 5 minutes */
385 } else if (mibif_maxspeed < IF_Mbps(622)) {
386 /* at 622Mbps overflow needs 53 seconds */
387 ticks = 40 * 100; /* 40 seconds */
388 } else if (mibif_maxspeed <= IF_Mbps(1000)) {
389 /* at 1Gbps overflow needs 34 seconds */
390 ticks = 20 * 100; /* 20 seconds */
392 /* at 10Gbps overflow needs 3.4 seconds */
393 ticks = 100; /* 1 seconds */
397 if (ticks == mibif_hc_update_interval)
400 if (hc_update_timer != NULL) {
401 timer_stop(hc_update_timer);
402 hc_update_timer = NULL;
404 update_hc_counters(NULL);
405 if ((hc_update_timer = timer_start_repeat(ticks * 10, ticks * 10,
406 update_hc_counters, NULL, module)) == NULL) {
407 syslog(LOG_ERR, "timer_start(%u): %m", ticks);
410 mibif_hc_update_interval = ticks;
414 * Fetch new MIB data.
417 mib_fetch_ifmib(struct mibif *ifp)
422 struct ifmibdata oldmib = ifp->mib;
424 if (fetch_generic_mib(ifp, &oldmib) == -1)
428 * Quoting RFC2863, 3.1.15: "... LinkUp and linkDown traps are
429 * generated just after ifOperStatus leaves, or just before it
430 * enters, the down state, respectively;"
432 if (ifp->trap_enable && ifp->mib.ifmd_data.ifi_link_state !=
433 oldmib.ifmd_data.ifi_link_state &&
434 (ifp->mib.ifmd_data.ifi_link_state == LINK_STATE_DOWN ||
435 oldmib.ifmd_data.ifi_link_state == LINK_STATE_DOWN))
436 link_trap(ifp, ifp->mib.ifmd_data.ifi_link_state ==
437 LINK_STATE_UP ? 1 : 0);
439 ifp->flags &= ~(MIBIF_HIGHSPEED | MIBIF_VERYHIGHSPEED);
440 if (ifp->mib.ifmd_data.ifi_baudrate > 20000000) {
441 ifp->flags |= MIBIF_HIGHSPEED;
442 if (ifp->mib.ifmd_data.ifi_baudrate > 650000000)
443 ifp->flags |= MIBIF_VERYHIGHSPEED;
445 if (ifp->mib.ifmd_data.ifi_baudrate > mibif_maxspeed) {
446 mibif_maxspeed = ifp->mib.ifmd_data.ifi_baudrate;
447 mibif_reset_hc_timer();
455 name[2] = NETLINK_GENERIC;
456 name[3] = IFMIB_IFDATA;
457 name[4] = ifp->sysindex;
458 name[5] = IFDATA_LINKSPECIFIC;
459 if (sysctl(name, 6, NULL, &len, NULL, 0) == -1) {
460 syslog(LOG_WARNING, "sysctl linkmib estimate (%s): %m",
462 if (ifp->specmib != NULL) {
469 if (ifp->specmib != NULL) {
476 if (ifp->specmiblen != len) {
477 if ((newmib = realloc(ifp->specmib, len)) == NULL) {
482 ifp->specmib = newmib;
483 ifp->specmiblen = len;
485 if (sysctl(name, 6, ifp->specmib, &len, NULL, 0) == -1) {
486 syslog(LOG_WARNING, "sysctl linkmib (%s): %m", ifp->name);
487 if (ifp->specmib != NULL) {
494 ifp->mibtick = get_ticks();
498 /* find first/next address for a given interface */
500 mib_first_ififa(const struct mibif *ifp)
504 TAILQ_FOREACH(ifa, &mibifa_list, link)
505 if (ifp->index == ifa->ifindex)
511 mib_next_ififa(struct mibifa *ifa0)
516 while ((ifa = TAILQ_NEXT(ifa, link)) != NULL)
517 if (ifa->ifindex == ifa0->ifindex)
525 static struct mibifa *
526 alloc_ifa(u_int ifindex, struct in_addr addr)
531 if ((ifa = malloc(sizeof(struct mibifa))) == NULL) {
532 syslog(LOG_ERR, "ifa: %m");
536 ifa->ifindex = ifindex;
538 ha = ntohl(ifa->inaddr.s_addr);
540 ifa->index.subs[0] = (ha >> 24) & 0xff;
541 ifa->index.subs[1] = (ha >> 16) & 0xff;
542 ifa->index.subs[2] = (ha >> 8) & 0xff;
543 ifa->index.subs[3] = (ha >> 0) & 0xff;
546 ifa->inbcast.s_addr = 0;
547 ifa->inmask.s_addr = 0xffffffff;
549 INSERT_OBJECT_OID(ifa, &mibifa_list);
555 * Delete an interface address
558 destroy_ifa(struct mibifa *ifa)
560 TAILQ_REMOVE(&mibifa_list, ifa, link);
566 * Helper routine to extract the sockaddr structures from a routing
570 mib_extract_addrs(int addrs, u_char *info, struct sockaddr **out)
574 for (i = 0; i < RTAX_MAX; i++) {
575 if ((addrs & (1 << i)) != 0) {
576 *out = (struct sockaddr *)(void *)info;
577 info += roundup((*out)->sa_len, sizeof(long));
585 * save the phys address of an interface. Handle receive address entries here.
588 get_physaddr(struct mibif *ifp, struct sockaddr_dl *sdl, u_char *ptr)
591 struct mibrcvaddr *rcv;
593 if (sdl->sdl_alen == 0) {
595 if (ifp->physaddrlen != 0) {
596 if ((rcv = mib_find_rcvaddr(ifp->index, ifp->physaddr,
597 ifp->physaddrlen)) != NULL)
598 mib_rcvaddr_delete(rcv);
600 ifp->physaddr = NULL;
601 ifp->physaddrlen = 0;
606 if (ifp->physaddrlen != sdl->sdl_alen) {
608 if (ifp->physaddrlen) {
609 /* delete olf receive address */
610 if ((rcv = mib_find_rcvaddr(ifp->index, ifp->physaddr,
611 ifp->physaddrlen)) != NULL)
612 mib_rcvaddr_delete(rcv);
614 if ((np = realloc(ifp->physaddr, sdl->sdl_alen)) == NULL) {
616 ifp->physaddr = NULL;
617 ifp->physaddrlen = 0;
621 ifp->physaddrlen = sdl->sdl_alen;
623 } else if (memcmp(ifp->physaddr, ptr, ifp->physaddrlen) == 0) {
628 /* address changed */
630 /* delete olf receive address */
631 if ((rcv = mib_find_rcvaddr(ifp->index, ifp->physaddr,
632 ifp->physaddrlen)) != NULL)
633 mib_rcvaddr_delete(rcv);
636 memcpy(ifp->physaddr, ptr, ifp->physaddrlen);
638 /* make new receive address */
639 if ((rcv = mib_rcvaddr_create(ifp, ifp->physaddr, ifp->physaddrlen)) != NULL)
640 rcv->flags |= MIBRCVADDR_HW;
647 mibif_free(struct mibif *ifp)
650 struct mibindexmap *map;
651 struct mibifa *ifa, *ifa1;
652 struct mibrcvaddr *rcv, *rcv1;
653 struct mibarp *at, *at1;
655 if (ifp->xnotify != NULL)
656 (*ifp->xnotify)(ifp, MIBIF_NOTIFY_DESTROY, ifp->xnotify_data);
658 (void)mib_ifstack_delete(ifp, NULL);
659 (void)mib_ifstack_delete(NULL, ifp);
661 TAILQ_REMOVE(&mibif_list, ifp, link);
663 /* if this was the fastest interface - recompute this */
664 if (ifp->mib.ifmd_data.ifi_baudrate == mibif_maxspeed) {
665 mibif_maxspeed = ifp->mib.ifmd_data.ifi_baudrate;
666 TAILQ_FOREACH(ifp1, &mibif_list, link)
667 if (ifp1->mib.ifmd_data.ifi_baudrate > mibif_maxspeed)
669 ifp1->mib.ifmd_data.ifi_baudrate;
670 mibif_reset_hc_timer();
674 if (ifp->physaddr != NULL)
676 if (ifp->specmib != NULL)
679 STAILQ_FOREACH(map, &mibindexmap_list, link)
680 if (map->mibif == ifp) {
685 /* purge interface addresses */
686 ifa = TAILQ_FIRST(&mibifa_list);
687 while (ifa != NULL) {
688 ifa1 = TAILQ_NEXT(ifa, link);
689 if (ifa->ifindex == ifp->index)
694 /* purge receive addresses */
695 rcv = TAILQ_FIRST(&mibrcvaddr_list);
696 while (rcv != NULL) {
697 rcv1 = TAILQ_NEXT(rcv, link);
698 if (rcv->ifindex == ifp->index)
699 mib_rcvaddr_delete(rcv);
703 /* purge ARP entries */
704 at = TAILQ_FIRST(&mibarp_list);
706 at1 = TAILQ_NEXT(at, link);
707 if (at->index.subs[0] == ifp->index)
715 mib_iftable_last_change = this_tick;
719 * Create a new interface
721 static struct mibif *
722 mibif_create(u_int sysindex, const char *name)
725 struct mibindexmap *map;
727 if ((ifp = malloc(sizeof(*ifp))) == NULL) {
728 syslog(LOG_WARNING, "%s: %m", __func__);
731 memset(ifp, 0, sizeof(*ifp));
732 if ((ifp->private = malloc(sizeof(struct mibif_private))) == NULL) {
733 syslog(LOG_WARNING, "%s: %m", __func__);
737 memset(ifp->private, 0, sizeof(struct mibif_private));
739 ifp->sysindex = sysindex;
740 strcpy(ifp->name, name);
741 strcpy(ifp->descr, name);
742 ifp->spec_oid = oid_zeroDotZero;
745 if (!mib_if_is_dyn(ifp->name)) {
746 /* non-dynamic. look whether we know the interface */
747 STAILQ_FOREACH(map, &mibindexmap_list, link)
748 if (strcmp(map->name, ifp->name) == 0) {
749 ifp->index = map->ifindex;
753 /* assume it has a connector if it is not dynamic */
754 ifp->has_connector = 1;
755 ifp->trap_enable = 1;
758 /* new interface - get new index */
759 if (next_if_index > 0x7fffffff)
760 errx(1, "ifindex wrap");
762 if ((map = malloc(sizeof(*map))) == NULL) {
763 syslog(LOG_ERR, "ifmap: %m");
767 map->ifindex = next_if_index++;
768 map->sysindex = ifp->sysindex;
769 strcpy(map->name, ifp->name);
771 STAILQ_INSERT_TAIL(&mibindexmap_list, map, link);
773 /* re-instantiate. Introduce a counter discontinuity */
774 ifp->counter_disc = get_ticks();
776 ifp->index = map->ifindex;
777 ifp->mib.ifmd_data.ifi_link_state = LINK_STATE_UNKNOWN;
779 INSERT_OBJECT_INT(ifp, &mibif_list);
781 mib_iftable_last_change = this_tick;
783 /* instantiate default ifStack entries */
784 (void)mib_ifstack_create(ifp, NULL);
785 (void)mib_ifstack_create(NULL, ifp);
791 * Inform all interested parties about a new interface
794 notify_newif(struct mibif *ifp)
796 struct newifreg *reg;
798 TAILQ_FOREACH(reg, &newifreg_list, link)
799 if ((*reg->func)(ifp))
804 * This is called for new interfaces after we have fetched the interface
805 * MIB. If this is a broadcast interface try to guess the broadcast address
806 * depending on the interface type.
809 check_llbcast(struct mibif *ifp)
811 static u_char ether_bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
812 static u_char arcnet_bcast = 0;
813 struct mibrcvaddr *rcv;
815 if (!(ifp->mib.ifmd_flags & IFF_BROADCAST))
818 switch (ifp->mib.ifmd_data.ifi_type) {
823 if (mib_find_rcvaddr(ifp->index, ether_bcast, 6) == NULL &&
824 (rcv = mib_rcvaddr_create(ifp, ether_bcast, 6)) != NULL)
825 rcv->flags |= MIBRCVADDR_BCAST;
829 if (mib_find_rcvaddr(ifp->index, &arcnet_bcast, 1) == NULL &&
830 (rcv = mib_rcvaddr_create(ifp, &arcnet_bcast, 1)) != NULL)
831 rcv->flags |= MIBRCVADDR_BCAST;
838 * Retrieve the current interface list from the system.
841 mib_refresh_iflist(void)
843 struct mibif *ifp, *ifp1;
848 struct ifmibdata mib;
850 TAILQ_FOREACH(ifp, &mibif_list, link)
851 ifp->flags &= ~MIBIF_FOUND;
854 if (sysctlbyname("net.link.generic.system.ifcount", &count, &len,
856 syslog(LOG_ERR, "ifcount: %m");
861 name[2] = NETLINK_GENERIC;
862 name[3] = IFMIB_IFDATA;
863 name[5] = IFDATA_GENERAL;
864 for (idx = 1; idx <= count; idx++) {
867 if (sysctl(name, 6, &mib, &len, NULL, 0) == -1) {
870 syslog(LOG_ERR, "ifmib(%u): %m", idx);
873 if ((ifp = mib_find_if_sys(idx)) != NULL) {
874 ifp->flags |= MIBIF_FOUND;
877 /* Unknown interface - create */
878 if ((ifp = mibif_create(idx, mib.ifmd_name)) != NULL) {
879 ifp->flags |= MIBIF_FOUND;
880 (void)mib_fetch_ifmib(ifp);
887 * Purge interfaces that disappeared
889 ifp = TAILQ_FIRST(&mibif_list);
890 while (ifp != NULL) {
891 ifp1 = TAILQ_NEXT(ifp, link);
892 if (!(ifp->flags & MIBIF_FOUND))
899 * Find an interface address
902 mib_find_ifa(struct in_addr addr)
906 TAILQ_FOREACH(ifa, &mibifa_list, link)
907 if (ifa->inaddr.s_addr == addr.s_addr)
913 * Handle a routing socket message.
916 handle_rtmsg(struct rt_msghdr *rtm)
918 struct sockaddr *addrs[RTAX_MAX];
919 struct if_msghdr *ifm;
920 struct ifa_msghdr *ifam;
921 struct ifma_msghdr *ifmam;
922 #ifdef RTM_IFANNOUNCE
923 struct if_announcemsghdr *ifan;
926 struct sockaddr_dl *sdl;
927 struct sockaddr_in *sa;
929 struct mibrcvaddr *rcv;
932 if (rtm->rtm_version != RTM_VERSION) {
933 syslog(LOG_ERR, "Bogus RTM version %u", rtm->rtm_version);
937 switch (rtm->rtm_type) {
940 ifam = (struct ifa_msghdr *)rtm;
941 mib_extract_addrs(ifam->ifam_addrs, (u_char *)(ifam + 1), addrs);
942 if (addrs[RTAX_IFA] == NULL || addrs[RTAX_NETMASK] == NULL)
945 sa = (struct sockaddr_in *)(void *)addrs[RTAX_IFA];
946 if ((ifa = mib_find_ifa(sa->sin_addr)) == NULL) {
947 /* unknown address */
948 if ((ifp = mib_find_if_sys(ifam->ifam_index)) == NULL) {
949 syslog(LOG_WARNING, "RTM_NEWADDR for unknown "
950 "interface %u", ifam->ifam_index);
953 if ((ifa = alloc_ifa(ifp->index, sa->sin_addr)) == NULL)
956 sa = (struct sockaddr_in *)(void *)addrs[RTAX_NETMASK];
957 ifa->inmask = sa->sin_addr;
959 if (addrs[RTAX_BRD] != NULL) {
960 sa = (struct sockaddr_in *)(void *)addrs[RTAX_BRD];
961 ifa->inbcast = sa->sin_addr;
963 ifa->flags |= MIBIFA_FOUND;
967 ifam = (struct ifa_msghdr *)rtm;
968 mib_extract_addrs(ifam->ifam_addrs, (u_char *)(ifam + 1), addrs);
969 if (addrs[RTAX_IFA] == NULL)
972 sa = (struct sockaddr_in *)(void *)addrs[RTAX_IFA];
973 if ((ifa = mib_find_ifa(sa->sin_addr)) != NULL) {
974 ifa->flags |= MIBIFA_FOUND;
975 if (!(ifa->flags & MIBIFA_DESTROYED))
981 ifmam = (struct ifma_msghdr *)rtm;
982 mib_extract_addrs(ifmam->ifmam_addrs, (u_char *)(ifmam + 1), addrs);
983 if (addrs[RTAX_IFA] == NULL ||
984 addrs[RTAX_IFA]->sa_family != AF_LINK)
986 sdl = (struct sockaddr_dl *)(void *)addrs[RTAX_IFA];
987 if ((rcv = mib_find_rcvaddr(sdl->sdl_index,
988 sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) == NULL) {
989 /* unknown address */
990 if ((ifp = mib_find_if_sys(sdl->sdl_index)) == NULL) {
991 syslog(LOG_WARNING, "RTM_NEWMADDR for unknown "
992 "interface %u", sdl->sdl_index);
995 if ((rcv = mib_rcvaddr_create(ifp,
996 sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) == NULL)
998 rcv->flags |= MIBRCVADDR_VOLATILE;
1000 rcv->flags |= MIBRCVADDR_FOUND;
1004 ifmam = (struct ifma_msghdr *)rtm;
1005 mib_extract_addrs(ifmam->ifmam_addrs, (u_char *)(ifmam + 1), addrs);
1006 if (addrs[RTAX_IFA] == NULL ||
1007 addrs[RTAX_IFA]->sa_family != AF_LINK)
1009 sdl = (struct sockaddr_dl *)(void *)addrs[RTAX_IFA];
1010 if ((rcv = mib_find_rcvaddr(sdl->sdl_index,
1011 sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) != NULL)
1012 mib_rcvaddr_delete(rcv);
1016 ifm = (struct if_msghdr *)(void *)rtm;
1017 mib_extract_addrs(ifm->ifm_addrs, (u_char *)(ifm + 1), addrs);
1018 if ((ifp = mib_find_if_sys(ifm->ifm_index)) == NULL)
1020 if (addrs[RTAX_IFP] != NULL &&
1021 addrs[RTAX_IFP]->sa_family == AF_LINK) {
1022 sdl = (struct sockaddr_dl *)(void *)addrs[RTAX_IFP];
1023 ptr = sdl->sdl_data + sdl->sdl_nlen;
1024 get_physaddr(ifp, sdl, ptr);
1026 (void)mib_fetch_ifmib(ifp);
1029 #ifdef RTM_IFANNOUNCE
1030 case RTM_IFANNOUNCE:
1031 ifan = (struct if_announcemsghdr *)rtm;
1032 ifp = mib_find_if_sys(ifan->ifan_index);
1034 switch (ifan->ifan_what) {
1037 if (ifp == NULL && (ifp = mibif_create(ifan->ifan_index,
1038 ifan->ifan_name)) != NULL) {
1039 (void)mib_fetch_ifmib(ifp);
1045 case IFAN_DEPARTURE:
1055 mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs);
1057 if (rtm->rtm_errno == 0 && (rtm->rtm_flags & RTF_UP))
1058 mib_sroute_process(rtm, addrs[RTAX_GATEWAY],
1059 addrs[RTAX_DST], addrs[RTAX_NETMASK]);
1065 * send a routing message
1068 mib_send_rtmsg(struct rt_msghdr *rtm, struct sockaddr *gw,
1069 struct sockaddr *dst, struct sockaddr *mask)
1072 struct rt_msghdr *msg;
1076 len = sizeof(*rtm) + SA_SIZE(gw) + SA_SIZE(dst) + SA_SIZE(mask);
1077 if ((msg = malloc(len)) == NULL) {
1078 syslog(LOG_ERR, "%s: %m", __func__);
1081 cp = (char *)(msg + 1);
1083 memset(msg, 0, sizeof(*msg));
1085 msg->rtm_version = RTM_VERSION;
1086 msg->rtm_addrs = RTA_DST | RTA_GATEWAY;
1088 memcpy(cp, dst, SA_SIZE(dst));
1090 memcpy(cp, gw, SA_SIZE(gw));
1093 memcpy(cp, mask, SA_SIZE(mask));
1094 cp += SA_SIZE(mask);
1095 msg->rtm_addrs |= RTA_NETMASK;
1097 msg->rtm_msglen = cp - (char *)msg;
1098 msg->rtm_type = RTM_GET;
1099 if ((sent = write(route, msg, msg->rtm_msglen)) == -1) {
1100 syslog(LOG_ERR, "%s: write: %m", __func__);
1104 if (sent != msg->rtm_msglen) {
1105 syslog(LOG_ERR, "%s: short write", __func__);
1113 * Fetch the routing table via sysctl
1116 mib_fetch_rtab(int af, int info, int arg, size_t *lenp)
1119 u_char *buf, *newbuf;
1130 /* initial estimate */
1131 if (sysctl(name, 6, NULL, lenp, NULL, 0) == -1) {
1132 syslog(LOG_ERR, "sysctl estimate (%d,%d,%d,%d,%d,%d): %m",
1133 name[0], name[1], name[2], name[3], name[4], name[5]);
1141 if ((newbuf = realloc(buf, *lenp)) == NULL) {
1142 syslog(LOG_ERR, "sysctl buffer: %m");
1148 if (sysctl(name, 6, buf, lenp, NULL, 0) == 0)
1151 if (errno != ENOMEM) {
1152 syslog(LOG_ERR, "sysctl get: %m");
1156 *lenp += *lenp / 8 + 1;
1163 * Update the following info: interface, interface addresses, interface
1164 * receive addresses, arp-table.
1165 * This does not change the interface list itself.
1168 update_ifa_info(void)
1171 struct rt_msghdr *rtm;
1172 struct mibifa *ifa, *ifa1;
1173 struct mibrcvaddr *rcv, *rcv1;
1175 static const int infos[][3] = {
1176 { 0, NET_RT_IFLIST, 0 },
1177 #ifdef NET_RT_IFMALIST
1178 { AF_LINK, NET_RT_IFMALIST, 0 },
1183 TAILQ_FOREACH(ifa, &mibifa_list, link)
1184 ifa->flags &= ~MIBIFA_FOUND;
1185 TAILQ_FOREACH(rcv, &mibrcvaddr_list, link)
1186 rcv->flags &= ~MIBRCVADDR_FOUND;
1188 for (i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
1189 if ((buf = mib_fetch_rtab(infos[i][0], infos[i][1], infos[i][2],
1194 while (next < buf + needed) {
1195 rtm = (struct rt_msghdr *)(void *)next;
1196 next += rtm->rtm_msglen;
1203 * Purge the address list of unused entries. These may happen for
1204 * interface aliases that are on the same subnet. We don't receive
1205 * routing socket messages for them.
1207 ifa = TAILQ_FIRST(&mibifa_list);
1208 while (ifa != NULL) {
1209 ifa1 = TAILQ_NEXT(ifa, link);
1210 if (!(ifa->flags & MIBIFA_FOUND))
1215 rcv = TAILQ_FIRST(&mibrcvaddr_list);
1216 while (rcv != NULL) {
1217 rcv1 = TAILQ_NEXT(rcv, link);
1218 if (!(rcv->flags & (MIBRCVADDR_FOUND | MIBRCVADDR_BCAST |
1220 mib_rcvaddr_delete(rcv);
1230 mib_arp_update(void)
1232 struct mibarp *at, *at1;
1235 struct rt_msghdr *rtm;
1238 return; /* Aaargh */
1241 TAILQ_FOREACH(at, &mibarp_list, link)
1242 at->flags &= ~MIBARP_FOUND;
1244 if ((buf = mib_fetch_rtab(AF_INET, NET_RT_FLAGS, 0, &needed)) == NULL) {
1250 while (next < buf + needed) {
1251 rtm = (struct rt_msghdr *)(void *)next;
1252 next += rtm->rtm_msglen;
1257 at = TAILQ_FIRST(&mibarp_list);
1258 while (at != NULL) {
1259 at1 = TAILQ_NEXT(at, link);
1260 if (!(at->flags & MIBARP_FOUND))
1264 mibarpticks = get_ticks();
1270 * Intput on the routing socket.
1273 route_input(int fd, void *udata __unused)
1275 u_char buf[1024 * 16];
1277 struct rt_msghdr *rtm;
1279 if ((n = read(fd, buf, sizeof(buf))) == -1)
1280 err(1, "read(rt_socket)");
1283 errx(1, "EOF on rt_socket");
1285 rtm = (struct rt_msghdr *)(void *)buf;
1286 if ((size_t)n != rtm->rtm_msglen)
1287 errx(1, "n=%zu, rtm_msglen=%u", (size_t)n, rtm->rtm_msglen);
1293 * execute and SIOCAIFADDR
1296 siocaifaddr(char *ifname, struct in_addr addr, struct in_addr mask,
1297 struct in_addr bcast)
1299 struct ifaliasreq addreq;
1300 struct sockaddr_in *sa;
1302 memset(&addreq, 0, sizeof(addreq));
1303 strncpy(addreq.ifra_name, ifname, sizeof(addreq.ifra_name));
1305 sa = (struct sockaddr_in *)(void *)&addreq.ifra_addr;
1306 sa->sin_family = AF_INET;
1307 sa->sin_len = sizeof(*sa);
1308 sa->sin_addr = addr;
1310 sa = (struct sockaddr_in *)(void *)&addreq.ifra_mask;
1311 sa->sin_family = AF_INET;
1312 sa->sin_len = sizeof(*sa);
1313 sa->sin_addr = mask;
1315 sa = (struct sockaddr_in *)(void *)&addreq.ifra_broadaddr;
1316 sa->sin_family = AF_INET;
1317 sa->sin_len = sizeof(*sa);
1318 sa->sin_addr = bcast;
1320 return (ioctl(mib_netsock, SIOCAIFADDR, &addreq));
1324 * Exececute a SIOCDIFADDR
1327 siocdifaddr(const char *ifname, struct in_addr addr)
1329 struct ifreq delreq;
1330 struct sockaddr_in *sa;
1332 memset(&delreq, 0, sizeof(delreq));
1333 strncpy(delreq.ifr_name, ifname, sizeof(delreq.ifr_name));
1334 sa = (struct sockaddr_in *)(void *)&delreq.ifr_addr;
1335 sa->sin_family = AF_INET;
1336 sa->sin_len = sizeof(*sa);
1337 sa->sin_addr = addr;
1339 return (ioctl(mib_netsock, SIOCDIFADDR, &delreq));
1343 * Verify an interface address without fetching the entire list
1346 verify_ifa(const char *name, struct mibifa *ifa)
1349 struct sockaddr_in *sa;
1351 memset(&req, 0, sizeof(req));
1352 strncpy(req.ifr_name, name, sizeof(req.ifr_name));
1353 sa = (struct sockaddr_in *)(void *)&req.ifr_addr;
1354 sa->sin_family = AF_INET;
1355 sa->sin_len = sizeof(*sa);
1356 sa->sin_addr = ifa->inaddr;
1358 if (ioctl(mib_netsock, SIOCGIFADDR, &req) == -1)
1360 if (ifa->inaddr.s_addr != sa->sin_addr.s_addr) {
1361 syslog(LOG_ERR, "%s: address mismatch", __func__);
1365 if (ioctl(mib_netsock, SIOCGIFNETMASK, &req) == -1)
1367 if (ifa->inmask.s_addr != sa->sin_addr.s_addr) {
1368 syslog(LOG_ERR, "%s: netmask mismatch", __func__);
1375 * Restore a deleted interface address. Don't wait for the routing socket
1379 mib_undestroy_ifa(struct mibifa *ifa)
1383 if ((ifp = mib_find_if(ifa->ifindex)) == NULL)
1384 /* keep it destroyed */
1387 if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast))
1388 /* keep it destroyed */
1391 ifa->flags &= ~MIBIFA_DESTROYED;
1395 * Destroy an interface address
1398 mib_destroy_ifa(struct mibifa *ifa)
1402 if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1407 if (siocdifaddr(ifp->name, ifa->inaddr)) {
1409 syslog(LOG_ERR, "SIOCDIFADDR: %m");
1413 ifa->flags |= MIBIFA_DESTROYED;
1418 * Rollback the modification of an address. Don't bother to wait for
1419 * the routing socket.
1422 mib_unmodify_ifa(struct mibifa *ifa)
1426 if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1432 if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast)) {
1443 mib_modify_ifa(struct mibifa *ifa)
1447 if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1453 if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast)) {
1459 if (verify_ifa(ifp->name, ifa)) {
1469 * Destroy a freshly created interface address. Don't bother to wait for
1470 * the routing socket.
1473 mib_uncreate_ifa(struct mibifa *ifa)
1477 if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1482 if (siocdifaddr(ifp->name, ifa->inaddr)) {
1492 * Create a new ifa and verify it
1495 mib_create_ifa(u_int ifindex, struct in_addr addr, struct in_addr mask,
1496 struct in_addr bcast)
1501 if ((ifp = mib_find_if(ifindex)) == NULL)
1503 if ((ifa = alloc_ifa(ifindex, addr)) == NULL)
1506 ifa->inbcast = bcast;
1508 if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast)) {
1509 syslog(LOG_ERR, "%s: %m", __func__);
1513 if (verify_ifa(ifp->name, ifa)) {
1521 * Get all cloning interfaces and make them dynamic.
1522 * Hah! Whe should probably do this on a periodic basis (XXX).
1527 struct if_clonereq req;
1531 memset(&req, 0, sizeof(req));
1532 if (ioctl(mib_netsock, SIOCIFGCLONERS, &req) == -1) {
1533 syslog(LOG_ERR, "get cloners: %m");
1536 if ((buf = malloc(req.ifcr_total * IFNAMSIZ)) == NULL) {
1537 syslog(LOG_ERR, "%m");
1540 req.ifcr_count = req.ifcr_total;
1541 req.ifcr_buffer = buf;
1542 if (ioctl(mib_netsock, SIOCIFGCLONERS, &req) == -1) {
1543 syslog(LOG_ERR, "get cloners: %m");
1547 for (cp = buf, i = 0; i < req.ifcr_total; i++, cp += IFNAMSIZ)
1560 if (mib_iflist_bad) {
1561 TAILQ_FOREACH(ifa, &mibifa_list, link)
1562 ifa->flags &= ~MIBIFA_DESTROYED;
1564 /* assume, that all cloning interfaces are dynamic */
1567 mib_refresh_iflist();
1583 if ((route_fd = fd_select(route, route_input, NULL, module)) == NULL) {
1584 syslog(LOG_ERR, "fd_select(route): %m");
1587 mib_refresh_iflist();
1590 (void)mib_fetch_route();
1591 mib_iftable_last_change = 0;
1592 mib_ifstack_last_change = 0;
1594 ifmib_reg = or_register(&oid_ifMIB,
1595 "The MIB module to describe generic objects for network interface"
1596 " sub-layers.", module);
1598 ipmib_reg = or_register(&oid_ipMIB,
1599 "The MIB module for managing IP and ICMP implementations, but "
1600 "excluding their management of IP routes.", module);
1602 tcpmib_reg = or_register(&oid_tcpMIB,
1603 "The MIB module for managing TCP implementations.", module);
1605 udpmib_reg = or_register(&oid_udpMIB,
1606 "The MIB module for managing UDP implementations.", module);
1608 ipForward_reg = or_register(&oid_ipForward,
1609 "The MIB module for the display of CIDR multipath IP Routes.",
1614 * Initialize the module
1617 mibII_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)
1623 len = sizeof(clockinfo);
1624 if (sysctlbyname("kern.clockrate", &clockinfo, &len, NULL, 0) == -1) {
1625 syslog(LOG_ERR, "kern.clockrate: %m");
1628 if (len != sizeof(clockinfo)) {
1629 syslog(LOG_ERR, "kern.clockrate: wrong size");
1633 if ((route = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) == -1) {
1634 syslog(LOG_ERR, "PF_ROUTE: %m");
1638 if ((mib_netsock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
1639 syslog(LOG_ERR, "PF_INET: %m");
1643 (void)shutdown(mib_netsock, SHUT_RDWR);
1645 /* assume, that all cloning interfaces are dynamic */
1654 if (route_fd != NULL)
1655 fd_deselect(route_fd);
1658 if (mib_netsock != -1)
1659 (void)close(mib_netsock);
1660 /* XXX free memory */
1662 or_unregister(ipForward_reg);
1663 or_unregister(udpmib_reg);
1664 or_unregister(tcpmib_reg);
1665 or_unregister(ipmib_reg);
1666 or_unregister(ifmib_reg);
1672 mibII_loading(const struct lmodule *mod, int loaded)
1679 TAILQ_FOREACH(ifp, &mibif_list, link)
1680 if (ifp->xnotify_mod == mod) {
1681 ifp->xnotify_mod = NULL;
1682 ifp->xnotify_data = NULL;
1683 ifp->xnotify = NULL;
1686 mib_unregister_newif(mod);
1689 const struct snmp_module config = {
1690 "This module implements the interface and ip groups.",
1693 mibII_idle, /* idle */
1704 * Should have a list of these attached to each interface.
1707 mibif_notify(struct mibif *ifp, const struct lmodule *mod,
1708 mibif_notify_f func, void *data)
1710 ifp->xnotify = func;
1711 ifp->xnotify_data = data;
1712 ifp->xnotify_mod = mod;
1718 mibif_unnotify(void *arg)
1720 struct mibif *ifp = arg;
1722 ifp->xnotify = NULL;
1723 ifp->xnotify_data = NULL;
1724 ifp->xnotify_mod = NULL;