]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/bsnmp/snmp_mibII/mibII.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / bsnmp / snmp_mibII / mibII.c
1 /*
2  * Copyright (c) 2001-2003
3  *      Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4  *      All rights reserved.
5  *
6  * Author: Harti Brandt <harti@freebsd.org>
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  * 
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
27  * SUCH DAMAGE.
28  *
29  * $Begemot: mibII.c 516 2006-10-27 15:54:02Z brandt_h $
30  *
31  * Implementation of the standard interfaces and ip MIB.
32  */
33 #include "mibII.h"
34 #include "mibII_oid.h"
35 #include <net/if.h>
36 #include <net/if_types.h>
37
38
39 /*****************************/
40
41 /* our module */
42 static struct lmodule *module;
43
44 /* routing socket */
45 static int route;
46 static void *route_fd;
47
48 /* if-index allocator */
49 static uint32_t next_if_index = 1;
50
51 /* currently fetching the arp table */
52 static int in_update_arp;
53
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;
60
61 /*****************************/
62
63 /* list of all IP addresses */
64 struct mibifa_list mibifa_list = TAILQ_HEAD_INITIALIZER(mibifa_list);
65
66 /* list of all interfaces */
67 struct mibif_list mibif_list = TAILQ_HEAD_INITIALIZER(mibif_list);
68
69 /* list of dynamic interface names */
70 struct mibdynif_list mibdynif_list = SLIST_HEAD_INITIALIZER(mibdynif_list);
71
72 /* list of all interface index mappings */
73 struct mibindexmap_list mibindexmap_list = STAILQ_HEAD_INITIALIZER(mibindexmap_list);
74
75 /* list of all stacking entries */
76 struct mibifstack_list mibifstack_list = TAILQ_HEAD_INITIALIZER(mibifstack_list);
77
78 /* list of all receive addresses */
79 struct mibrcvaddr_list mibrcvaddr_list = TAILQ_HEAD_INITIALIZER(mibrcvaddr_list);
80
81 /* list of all NetToMedia entries */
82 struct mibarp_list mibarp_list = TAILQ_HEAD_INITIALIZER(mibarp_list);
83
84 /* number of interfaces */
85 int32_t mib_if_number;
86
87 /* last change of table */
88 uint64_t mib_iftable_last_change;
89
90 /* last change of stack table */
91 uint64_t mib_ifstack_last_change;
92
93 /* if this is set, one of our lists may be bad. refresh them when idle */
94 int mib_iflist_bad;
95
96 /* network socket */
97 int mib_netsock;
98
99 /* last time refreshed */
100 uint64_t mibarpticks;
101
102 /* info on system clocks */
103 struct clockinfo clockinfo;
104
105 /* list of all New if registrations */
106 static struct newifreg_list newifreg_list = TAILQ_HEAD_INITIALIZER(newifreg_list);
107
108 /* baud rate of fastest interface */
109 uint64_t mibif_maxspeed;
110
111 /* user-forced update interval */
112 u_int mibif_force_hc_update_interval;
113
114 /* current update interval */
115 u_int mibif_hc_update_interval;
116
117 /* HC update timer handle */
118 static void *hc_update_timer;
119
120 /* Idle poll timer */
121 static void *mibII_poll_timer;
122
123 /* interfaces' data poll interval */
124 u_int mibII_poll_ticks;
125
126 /* Idle poll hook */
127 static void mibII_idle(void *arg __unused);
128
129 /*****************************/
130
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;
139
140 /*****************************/
141
142 /*
143  * Find an interface
144  */
145 struct mibif *
146 mib_find_if(u_int idx)
147 {
148         struct mibif *ifp;
149
150         TAILQ_FOREACH(ifp, &mibif_list, link)
151                 if (ifp->index == idx)
152                         return (ifp);
153         return (NULL);
154 }
155
156 struct mibif *
157 mib_find_if_sys(u_int sysindex)
158 {
159         struct mibif *ifp;
160
161         TAILQ_FOREACH(ifp, &mibif_list, link)
162                 if (ifp->sysindex == sysindex)
163                         return (ifp);
164         return (NULL);
165 }
166
167 struct mibif *
168 mib_find_if_name(const char *name)
169 {
170         struct mibif *ifp;
171
172         TAILQ_FOREACH(ifp, &mibif_list, link)
173                 if (strcmp(ifp->name, name) == 0)
174                         return (ifp);
175         return (NULL);
176 }
177
178 /*
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.
181  */
182 int
183 mib_if_is_dyn(const char *name)
184 {
185         size_t len;
186         struct mibdynif *d;
187
188         for (len = 0; name[len] != '\0' && isalpha(name[len]) ; len++)
189                 ;
190         SLIST_FOREACH(d, &mibdynif_list, link)
191                 if (strlen(d->name) == len && strncmp(d->name, name, len) == 0)
192                         return (1);
193         return (0);
194 }
195
196 /* set an interface name to dynamic mode */
197 void
198 mib_if_set_dyn(const char *name)
199 {
200         struct mibdynif *d;
201
202         SLIST_FOREACH(d, &mibdynif_list, link)
203                 if (strcmp(name, d->name) == 0)
204                         return;
205         if ((d = malloc(sizeof(*d))) == NULL)
206                 err(1, NULL);
207         strcpy(d->name, name);
208         SLIST_INSERT_HEAD(&mibdynif_list, d, link);
209 }
210
211 /*
212  * register for interface creations
213  */
214 int
215 mib_register_newif(int (*func)(struct mibif *), const struct lmodule *mod)
216 {
217         struct newifreg *reg;
218
219         TAILQ_FOREACH(reg, &newifreg_list, link)
220                 if (reg->mod == mod) {
221                         reg->func = func;
222                         return (0);
223                 }
224         if ((reg = malloc(sizeof(*reg))) == NULL) {
225                 syslog(LOG_ERR, "newifreg: %m");
226                 return (-1);
227         }
228         reg->mod = mod;
229         reg->func = func;
230         TAILQ_INSERT_TAIL(&newifreg_list, reg, link);
231
232         return (0);
233 }
234
235 void
236 mib_unregister_newif(const struct lmodule *mod)
237 {
238         struct newifreg *reg;
239
240         TAILQ_FOREACH(reg, &newifreg_list, link)
241                 if (reg->mod == mod) {
242                         TAILQ_REMOVE(&newifreg_list, reg, link);
243                         free(reg);
244                         return;
245                 }
246
247 }
248
249 struct mibif *
250 mib_first_if(void)
251 {
252         return (TAILQ_FIRST(&mibif_list));
253 }
254 struct mibif *
255 mib_next_if(const struct mibif *ifp)
256 {
257         return (TAILQ_NEXT(ifp, link));
258 }
259
260 /*
261  * Change the admin status of an interface
262  */
263 int
264 mib_if_admin(struct mibif *ifp, int up)
265 {
266         struct ifreq ifr;
267
268         strncpy(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);
271                 return (-1);
272         }
273         if (up)
274                 ifr.ifr_flags |= IFF_UP;
275         else
276                 ifr.ifr_flags &= ~IFF_UP;
277         if (ioctl(mib_netsock, SIOCSIFFLAGS, &ifr) == -1) {
278                 syslog(LOG_ERR, "SIOCSIFFLAGS(%s): %m", ifp->name);
279                 return (-1);
280         }
281
282         (void)mib_fetch_ifmib(ifp);
283
284         return (0);
285 }
286
287 /*
288  * Generate a link up/down trap
289  */
290 static void
291 link_trap(struct mibif *ifp, int up)
292 {
293         struct snmp_value ifindex;
294
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;
299
300         snmp_send_trap(up ? &oid_linkUp : &oid_linkDown, &ifindex,
301             (struct snmp_value *)NULL);
302 }
303
304 /**
305  * Fetch the GENERIC IFMIB and update the HC counters
306  */
307 static int
308 fetch_generic_mib(struct mibif *ifp, const struct ifmibdata *old)
309 {
310         int name[6];
311         size_t len;
312         struct mibif_private *p = ifp->private;
313
314         name[0] = CTL_NET;
315         name[1] = PF_LINK;
316         name[2] = NETLINK_GENERIC;
317         name[3] = IFMIB_IFDATA;
318         name[4] = ifp->sysindex;
319         name[5] = IFDATA_GENERAL;
320
321         len = sizeof(ifp->mib);
322         if (sysctl(name, 6, &ifp->mib, &len, NULL, 0) == -1) {
323                 if (errno != ENOENT)
324                         syslog(LOG_WARNING, "sysctl(ifmib, %s) failed %m",
325                             ifp->name);
326                 return (-1);
327         }
328
329         /*
330          * Assume that one of the two following compounds is optimized away
331          */
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;
339
340         } else if (ULONG_MAX >= 0xffffffff) {
341
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;                         \
347                 else                                                    \
348                         p->HC += ifp->mib.ifmd_data.MIB -               \
349                             old->ifmd_data.MIB;
350
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)
357
358 #undef  UPDATE
359         } else
360                 abort();
361         return (0);
362 }
363
364 /**
365  * Update the 64-bit interface counters
366  */
367 static void
368 update_hc_counters(void *arg __unused)
369 {
370         struct mibif *ifp;
371         struct ifmibdata oldmib;
372
373         TAILQ_FOREACH(ifp, &mibif_list, link) {
374                 oldmib = ifp->mib;
375                 (void)fetch_generic_mib(ifp, &oldmib);
376         }
377 }
378
379 /**
380  * Recompute the poll timer for the HC counters
381  */
382 void
383 mibif_reset_hc_timer(void)
384 {
385         u_int ticks;
386
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 */
400                 } else {
401                         /* at 10Gbps overflow needs 3.4 seconds */
402                         ticks = 100;            /* 1 seconds */
403                 }
404         }
405
406         if (ticks == mibif_hc_update_interval)
407                 return;
408
409         if (hc_update_timer != NULL) {
410                 timer_stop(hc_update_timer);
411                 hc_update_timer = NULL;
412         }
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);
417                 return;
418         }
419         mibif_hc_update_interval = ticks;
420 }
421
422 /**
423  * Restart the idle poll timer.
424  */
425 void
426 mibif_restart_mibII_poll_timer(void)
427 {
428         if (mibII_poll_timer != NULL)
429                 timer_stop(mibII_poll_timer);
430
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);
434 }
435
436 /*
437  * Fetch new MIB data.
438  */
439 int
440 mib_fetch_ifmib(struct mibif *ifp)
441 {
442         int name[6];
443         size_t len;
444         void *newmib;
445         struct ifmibdata oldmib = ifp->mib;
446
447         if (fetch_generic_mib(ifp, &oldmib) == -1)
448                 return (-1);
449
450         /*
451          * Quoting RFC2863, 3.1.15: "... LinkUp and linkDown traps are
452          * generated just after ifOperStatus leaves, or just before it
453          * enters, the down state, respectively;"
454          */
455         if (ifp->trap_enable && ifp->mib.ifmd_data.ifi_link_state !=
456             oldmib.ifmd_data.ifi_link_state &&
457             (ifp->mib.ifmd_data.ifi_link_state == LINK_STATE_DOWN ||
458             oldmib.ifmd_data.ifi_link_state == LINK_STATE_DOWN))
459                 link_trap(ifp, ifp->mib.ifmd_data.ifi_link_state ==
460                     LINK_STATE_UP ? 1 : 0);
461
462         ifp->flags &= ~(MIBIF_HIGHSPEED | MIBIF_VERYHIGHSPEED);
463         if (ifp->mib.ifmd_data.ifi_baudrate > 20000000) {
464                 ifp->flags |= MIBIF_HIGHSPEED;
465                 if (ifp->mib.ifmd_data.ifi_baudrate > 650000000)
466                         ifp->flags |= MIBIF_VERYHIGHSPEED;
467         }
468         if (ifp->mib.ifmd_data.ifi_baudrate > mibif_maxspeed) {
469                 mibif_maxspeed = ifp->mib.ifmd_data.ifi_baudrate;
470                 mibif_reset_hc_timer();
471         }
472
473         /*
474          * linkspecific MIB
475          */
476         name[0] = CTL_NET;
477         name[1] = PF_LINK;
478         name[2] = NETLINK_GENERIC;
479         name[3] = IFMIB_IFDATA;
480         name[4] = ifp->sysindex;
481         name[5] = IFDATA_LINKSPECIFIC;
482         if (sysctl(name, 6, NULL, &len, NULL, 0) == -1) {
483                 syslog(LOG_WARNING, "sysctl linkmib estimate (%s): %m",
484                     ifp->name);
485                 if (ifp->specmib != NULL) {
486                         ifp->specmib = NULL;
487                         ifp->specmiblen = 0;
488                 }
489                 goto out;
490         }
491         if (len == 0) {
492                 if (ifp->specmib != NULL) {
493                         ifp->specmib = NULL;
494                         ifp->specmiblen = 0;
495                 }
496                 goto out;
497         }
498
499         if (ifp->specmiblen != len) {
500                 if ((newmib = realloc(ifp->specmib, len)) == NULL) {
501                         ifp->specmib = NULL;
502                         ifp->specmiblen = 0;
503                         goto out;
504                 }
505                 ifp->specmib = newmib;
506                 ifp->specmiblen = len;
507         }
508         if (sysctl(name, 6, ifp->specmib, &len, NULL, 0) == -1) {
509                 syslog(LOG_WARNING, "sysctl linkmib (%s): %m", ifp->name);
510                 if (ifp->specmib != NULL) {
511                         ifp->specmib = NULL;
512                         ifp->specmiblen = 0;
513                 }
514         }
515
516   out:
517         ifp->mibtick = get_ticks();
518         return (0);
519 }
520
521 /* find first/next address for a given interface */
522 struct mibifa *
523 mib_first_ififa(const struct mibif *ifp)
524 {
525         struct mibifa *ifa;
526
527         TAILQ_FOREACH(ifa, &mibifa_list, link)
528                 if (ifp->index == ifa->ifindex)
529                         return (ifa);
530         return (NULL);
531 }
532
533 struct mibifa *
534 mib_next_ififa(struct mibifa *ifa0)
535 {
536         struct mibifa *ifa;
537
538         ifa = ifa0;
539         while ((ifa = TAILQ_NEXT(ifa, link)) != NULL)
540                 if (ifa->ifindex == ifa0->ifindex)
541                         return (ifa);
542         return (NULL);
543 }
544
545 /*
546  * Allocate a new IFA
547  */
548 static struct mibifa *
549 alloc_ifa(u_int ifindex, struct in_addr addr)
550 {
551         struct mibifa *ifa;
552         uint32_t ha;
553
554         if ((ifa = malloc(sizeof(struct mibifa))) == NULL) {
555                 syslog(LOG_ERR, "ifa: %m");
556                 return (NULL);
557         }
558         ifa->inaddr = addr;
559         ifa->ifindex = ifindex;
560
561         ha = ntohl(ifa->inaddr.s_addr);
562         ifa->index.len = 4;
563         ifa->index.subs[0] = (ha >> 24) & 0xff;
564         ifa->index.subs[1] = (ha >> 16) & 0xff;
565         ifa->index.subs[2] = (ha >>  8) & 0xff;
566         ifa->index.subs[3] = (ha >>  0) & 0xff;
567
568         ifa->flags = 0;
569         ifa->inbcast.s_addr = 0;
570         ifa->inmask.s_addr = 0xffffffff;
571
572         INSERT_OBJECT_OID(ifa, &mibifa_list);
573
574         return (ifa);
575 }
576
577 /*
578  * Delete an interface address
579  */
580 static void
581 destroy_ifa(struct mibifa *ifa)
582 {
583         TAILQ_REMOVE(&mibifa_list, ifa, link);
584         free(ifa);
585 }
586
587
588 /*
589  * Helper routine to extract the sockaddr structures from a routing
590  * socket message.
591  */
592 void
593 mib_extract_addrs(int addrs, u_char *info, struct sockaddr **out)
594 {
595         u_int i;
596
597         for (i = 0; i < RTAX_MAX; i++) {
598                 if ((addrs & (1 << i)) != 0) {
599                         *out = (struct sockaddr *)(void *)info;
600                         info += roundup((*out)->sa_len, sizeof(long));
601                 } else
602                         *out = NULL;
603                 out++;
604         }
605 }
606
607 /*
608  * save the phys address of an interface. Handle receive address entries here.
609  */
610 static void
611 get_physaddr(struct mibif *ifp, struct sockaddr_dl *sdl, u_char *ptr)
612 {
613         u_char *np;
614         struct mibrcvaddr *rcv;
615
616         if (sdl->sdl_alen == 0) {
617                 /* no address */
618                 if (ifp->physaddrlen != 0) {
619                         if ((rcv = mib_find_rcvaddr(ifp->index, ifp->physaddr,
620                             ifp->physaddrlen)) != NULL)
621                                 mib_rcvaddr_delete(rcv);
622                         free(ifp->physaddr);
623                         ifp->physaddr = NULL;
624                         ifp->physaddrlen = 0;
625                 }
626                 return;
627         }
628
629         if (ifp->physaddrlen != sdl->sdl_alen) {
630                 /* length changed */
631                 if (ifp->physaddrlen) {
632                         /* delete olf receive address */
633                         if ((rcv = mib_find_rcvaddr(ifp->index, ifp->physaddr,
634                             ifp->physaddrlen)) != NULL)
635                                 mib_rcvaddr_delete(rcv);
636                 }
637                 if ((np = realloc(ifp->physaddr, sdl->sdl_alen)) == NULL) {
638                         free(ifp->physaddr);
639                         ifp->physaddr = NULL;
640                         ifp->physaddrlen = 0;
641                         return;
642                 }
643                 ifp->physaddr = np;
644                 ifp->physaddrlen = sdl->sdl_alen;
645
646         } else if (memcmp(ifp->physaddr, ptr, ifp->physaddrlen) == 0) {
647                 /* no change */
648                 return;
649
650         } else {
651                 /* address changed */
652
653                 /* delete olf receive address */
654                 if ((rcv = mib_find_rcvaddr(ifp->index, ifp->physaddr,
655                     ifp->physaddrlen)) != NULL)
656                         mib_rcvaddr_delete(rcv);
657         }
658
659         memcpy(ifp->physaddr, ptr, ifp->physaddrlen);
660
661         /* make new receive address */
662         if ((rcv = mib_rcvaddr_create(ifp, ifp->physaddr, ifp->physaddrlen)) != NULL)
663                 rcv->flags |= MIBRCVADDR_HW;
664 }
665
666 /*
667  * Free an interface
668  */
669 static void
670 mibif_free(struct mibif *ifp)
671 {
672         struct mibif *ifp1;
673         struct mibindexmap *map;
674         struct mibifa *ifa, *ifa1;
675         struct mibrcvaddr *rcv, *rcv1;
676         struct mibarp *at, *at1;
677
678         if (ifp->xnotify != NULL)
679                 (*ifp->xnotify)(ifp, MIBIF_NOTIFY_DESTROY, ifp->xnotify_data);
680
681         (void)mib_ifstack_delete(ifp, NULL);
682         (void)mib_ifstack_delete(NULL, ifp);
683
684         TAILQ_REMOVE(&mibif_list, ifp, link);
685
686         /* if this was the fastest interface - recompute this */
687         if (ifp->mib.ifmd_data.ifi_baudrate == mibif_maxspeed) {
688                 mibif_maxspeed = ifp->mib.ifmd_data.ifi_baudrate;
689                 TAILQ_FOREACH(ifp1, &mibif_list, link)
690                         if (ifp1->mib.ifmd_data.ifi_baudrate > mibif_maxspeed)
691                                 mibif_maxspeed =
692                                     ifp1->mib.ifmd_data.ifi_baudrate;
693                 mibif_reset_hc_timer();
694         }
695
696         free(ifp->private);
697         if (ifp->physaddr != NULL)
698                 free(ifp->physaddr);
699         if (ifp->specmib != NULL)
700                 free(ifp->specmib);
701
702         STAILQ_FOREACH(map, &mibindexmap_list, link)
703                 if (map->mibif == ifp) {
704                         map->mibif = NULL;
705                         break;
706                 }
707
708         /* purge interface addresses */
709         ifa = TAILQ_FIRST(&mibifa_list);
710         while (ifa != NULL) {
711                 ifa1 = TAILQ_NEXT(ifa, link);
712                 if (ifa->ifindex == ifp->index)
713                         destroy_ifa(ifa);
714                 ifa = ifa1;
715         }
716
717         /* purge receive addresses */
718         rcv = TAILQ_FIRST(&mibrcvaddr_list);
719         while (rcv != NULL) {
720                 rcv1 = TAILQ_NEXT(rcv, link);
721                 if (rcv->ifindex == ifp->index)
722                         mib_rcvaddr_delete(rcv);
723                 rcv = rcv1;
724         }
725
726         /* purge ARP entries */
727         at = TAILQ_FIRST(&mibarp_list);
728         while (at != NULL) {
729                 at1 = TAILQ_NEXT(at, link);
730                 if (at->index.subs[0] == ifp->index)
731                         mib_arp_delete(at);
732                 at = at1;
733         }
734
735
736         free(ifp);
737         mib_if_number--;
738         mib_iftable_last_change = this_tick;
739 }
740
741 /*
742  * Create a new interface
743  */
744 static struct mibif *
745 mibif_create(u_int sysindex, const char *name)
746 {
747         struct mibif *ifp;
748         struct mibindexmap *map;
749
750         if ((ifp = malloc(sizeof(*ifp))) == NULL) {
751                 syslog(LOG_WARNING, "%s: %m", __func__);
752                 return (NULL);
753         }
754         memset(ifp, 0, sizeof(*ifp));
755         if ((ifp->private = malloc(sizeof(struct mibif_private))) == NULL) {
756                 syslog(LOG_WARNING, "%s: %m", __func__);
757                 free(ifp);
758                 return (NULL);
759         }
760         memset(ifp->private, 0, sizeof(struct mibif_private));
761
762         ifp->sysindex = sysindex;
763         strcpy(ifp->name, name);
764         strcpy(ifp->descr, name);
765         ifp->spec_oid = oid_zeroDotZero;
766
767         map = NULL;
768         if (!mib_if_is_dyn(ifp->name)) {
769                 /* non-dynamic. look whether we know the interface */
770                 STAILQ_FOREACH(map, &mibindexmap_list, link)
771                         if (strcmp(map->name, ifp->name) == 0) {
772                                 ifp->index = map->ifindex;
773                                 map->mibif = ifp;
774                                 break;
775                         }
776                 /* assume it has a connector if it is not dynamic */
777                 ifp->has_connector = 1;
778                 ifp->trap_enable = 1;
779         }
780         if (map == NULL) {
781                 /* new interface - get new index */
782                 if (next_if_index > 0x7fffffff)
783                         errx(1, "ifindex wrap");
784
785                 if ((map = malloc(sizeof(*map))) == NULL) {
786                         syslog(LOG_ERR, "ifmap: %m");
787                         free(ifp);
788                         return (NULL);
789                 }
790                 map->ifindex = next_if_index++;
791                 map->sysindex = ifp->sysindex;
792                 strcpy(map->name, ifp->name);
793                 map->mibif = ifp;
794                 STAILQ_INSERT_TAIL(&mibindexmap_list, map, link);
795         } else {
796                 /* re-instantiate. Introduce a counter discontinuity */
797                 ifp->counter_disc = get_ticks();
798         }
799         ifp->index = map->ifindex;
800         ifp->mib.ifmd_data.ifi_link_state = LINK_STATE_UNKNOWN;
801
802         INSERT_OBJECT_INT(ifp, &mibif_list);
803         mib_if_number++;
804         mib_iftable_last_change = this_tick;
805
806         /* instantiate default ifStack entries */
807         (void)mib_ifstack_create(ifp, NULL);
808         (void)mib_ifstack_create(NULL, ifp);
809
810         return (ifp);
811 }
812
813 /*
814  * Inform all interested parties about a new interface
815  */
816 static void
817 notify_newif(struct mibif *ifp)
818 {
819         struct newifreg *reg;
820
821         TAILQ_FOREACH(reg, &newifreg_list, link)
822                 if ((*reg->func)(ifp))
823                         return;
824 }
825
826 /*
827  * This is called for new interfaces after we have fetched the interface
828  * MIB. If this is a broadcast interface try to guess the broadcast address
829  * depending on the interface type.
830  */
831 static void
832 check_llbcast(struct mibif *ifp)
833 {
834         static u_char ether_bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
835         static u_char arcnet_bcast = 0;
836         struct mibrcvaddr *rcv;
837
838         if (!(ifp->mib.ifmd_flags & IFF_BROADCAST))
839                 return;
840
841         switch (ifp->mib.ifmd_data.ifi_type) {
842
843           case IFT_ETHER:
844           case IFT_FDDI:
845           case IFT_ISO88025:
846           case IFT_L2VLAN:
847                 if (mib_find_rcvaddr(ifp->index, ether_bcast, 6) == NULL &&
848                     (rcv = mib_rcvaddr_create(ifp, ether_bcast, 6)) != NULL)
849                         rcv->flags |= MIBRCVADDR_BCAST;
850                 break;
851
852           case IFT_ARCNET:
853                 if (mib_find_rcvaddr(ifp->index, &arcnet_bcast, 1) == NULL &&
854                     (rcv = mib_rcvaddr_create(ifp, &arcnet_bcast, 1)) != NULL)
855                         rcv->flags |= MIBRCVADDR_BCAST;
856                 break;
857         }
858 }
859
860
861 /*
862  * Retrieve the current interface list from the system.
863  */
864 void
865 mib_refresh_iflist(void)
866 {
867         struct mibif *ifp, *ifp1;
868         size_t len;
869         u_short idx;
870         int name[6];
871         int count;
872         struct ifmibdata mib;
873
874         TAILQ_FOREACH(ifp, &mibif_list, link)
875                 ifp->flags &= ~MIBIF_FOUND;
876
877         len = sizeof(count);
878         if (sysctlbyname("net.link.generic.system.ifcount", &count, &len,
879             NULL, 0) == -1) {
880                 syslog(LOG_ERR, "ifcount: %m");
881                 return;
882         }
883         name[0] = CTL_NET;
884         name[1] = PF_LINK;
885         name[2] = NETLINK_GENERIC;
886         name[3] = IFMIB_IFDATA;
887         name[5] = IFDATA_GENERAL;
888         for (idx = 1; idx <= count; idx++) {
889                 name[4] = idx;
890                 len = sizeof(mib);
891                 if (sysctl(name, 6, &mib, &len, NULL, 0) == -1) {
892                         if (errno == ENOENT)
893                                 continue;
894                         syslog(LOG_ERR, "ifmib(%u): %m", idx);
895                         return;
896                 }
897                 if ((ifp = mib_find_if_sys(idx)) != NULL) {
898                         ifp->flags |= MIBIF_FOUND;
899                         continue;
900                 }
901                 /* Unknown interface - create */
902                 if ((ifp = mibif_create(idx, mib.ifmd_name)) != NULL) {
903                         ifp->flags |= MIBIF_FOUND;
904                         (void)mib_fetch_ifmib(ifp);
905                         check_llbcast(ifp);
906                         notify_newif(ifp);
907                 }
908         }
909
910         /*
911          * Purge interfaces that disappeared
912          */
913         ifp = TAILQ_FIRST(&mibif_list);
914         while (ifp != NULL) {
915                 ifp1 = TAILQ_NEXT(ifp, link);
916                 if (!(ifp->flags & MIBIF_FOUND))
917                         mibif_free(ifp);
918                 ifp = ifp1;
919         }
920 }
921
922 /*
923  * Find an interface address
924  */
925 struct mibifa *
926 mib_find_ifa(struct in_addr addr)
927 {
928         struct mibifa *ifa;
929
930         TAILQ_FOREACH(ifa, &mibifa_list, link)
931                 if (ifa->inaddr.s_addr == addr.s_addr)
932                         return (ifa);
933         return (NULL);
934 }
935
936 /*
937  * Handle a routing socket message.
938  */
939 static void
940 handle_rtmsg(struct rt_msghdr *rtm)
941 {
942         struct sockaddr *addrs[RTAX_MAX];
943         struct if_msghdr *ifm;
944         struct ifa_msghdr *ifam;
945         struct ifma_msghdr *ifmam;
946 #ifdef RTM_IFANNOUNCE
947         struct if_announcemsghdr *ifan;
948 #endif
949         struct mibif *ifp;
950         struct sockaddr_dl *sdl;
951         struct sockaddr_in *sa;
952         struct mibifa *ifa;
953         struct mibrcvaddr *rcv;
954         u_char *ptr;
955
956         if (rtm->rtm_version != RTM_VERSION) {
957                 syslog(LOG_ERR, "Bogus RTM version %u", rtm->rtm_version);
958                 return;
959         }
960
961         switch (rtm->rtm_type) {
962
963           case RTM_NEWADDR:
964                 ifam = (struct ifa_msghdr *)rtm;
965                 mib_extract_addrs(ifam->ifam_addrs, (u_char *)(ifam + 1), addrs);
966                 if (addrs[RTAX_IFA] == NULL || addrs[RTAX_NETMASK] == NULL)
967                         break;
968
969                 sa = (struct sockaddr_in *)(void *)addrs[RTAX_IFA];
970                 if ((ifa = mib_find_ifa(sa->sin_addr)) == NULL) {
971                         /* unknown address */
972                         if ((ifp = mib_find_if_sys(ifam->ifam_index)) == NULL) {
973                                 syslog(LOG_WARNING, "RTM_NEWADDR for unknown "
974                                     "interface %u", ifam->ifam_index);
975                                 break;
976                         }
977                         if ((ifa = alloc_ifa(ifp->index, sa->sin_addr)) == NULL)
978                                 break;
979                 }
980                 sa = (struct sockaddr_in *)(void *)addrs[RTAX_NETMASK];
981                 ifa->inmask = sa->sin_addr;
982
983                 if (addrs[RTAX_BRD] != NULL) {
984                         sa = (struct sockaddr_in *)(void *)addrs[RTAX_BRD];
985                         ifa->inbcast = sa->sin_addr;
986                 }
987                 ifa->flags |= MIBIFA_FOUND;
988                 break;
989
990           case RTM_DELADDR:
991                 ifam = (struct ifa_msghdr *)rtm;
992                 mib_extract_addrs(ifam->ifam_addrs, (u_char *)(ifam + 1), addrs);
993                 if (addrs[RTAX_IFA] == NULL)
994                         break;
995
996                 sa = (struct sockaddr_in *)(void *)addrs[RTAX_IFA];
997                 if ((ifa = mib_find_ifa(sa->sin_addr)) != NULL) {
998                         ifa->flags |= MIBIFA_FOUND;
999                         if (!(ifa->flags & MIBIFA_DESTROYED))
1000                                 destroy_ifa(ifa);
1001                 }
1002                 break;
1003
1004           case RTM_NEWMADDR:
1005                 ifmam = (struct ifma_msghdr *)rtm;
1006                 mib_extract_addrs(ifmam->ifmam_addrs, (u_char *)(ifmam + 1), addrs);
1007                 if (addrs[RTAX_IFA] == NULL ||
1008                     addrs[RTAX_IFA]->sa_family != AF_LINK)
1009                         break;
1010                 sdl = (struct sockaddr_dl *)(void *)addrs[RTAX_IFA];
1011                 if ((rcv = mib_find_rcvaddr(sdl->sdl_index,
1012                     sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) == NULL) {
1013                         /* unknown address */
1014                         if ((ifp = mib_find_if_sys(sdl->sdl_index)) == NULL) {
1015                                 syslog(LOG_WARNING, "RTM_NEWMADDR for unknown "
1016                                     "interface %u", sdl->sdl_index);
1017                                 break;
1018                         }
1019                         if ((rcv = mib_rcvaddr_create(ifp,
1020                             sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) == NULL)
1021                                 break;
1022                         rcv->flags |= MIBRCVADDR_VOLATILE;
1023                 }
1024                 rcv->flags |= MIBRCVADDR_FOUND;
1025                 break;
1026
1027           case RTM_DELMADDR:
1028                 ifmam = (struct ifma_msghdr *)rtm;
1029                 mib_extract_addrs(ifmam->ifmam_addrs, (u_char *)(ifmam + 1), addrs);
1030                 if (addrs[RTAX_IFA] == NULL ||
1031                     addrs[RTAX_IFA]->sa_family != AF_LINK)
1032                         break;
1033                 sdl = (struct sockaddr_dl *)(void *)addrs[RTAX_IFA];
1034                 if ((rcv = mib_find_rcvaddr(sdl->sdl_index,
1035                     sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) != NULL)
1036                         mib_rcvaddr_delete(rcv);
1037                 break;
1038
1039           case RTM_IFINFO:
1040                 ifm = (struct if_msghdr *)(void *)rtm;
1041                 mib_extract_addrs(ifm->ifm_addrs, (u_char *)(ifm + 1), addrs);
1042                 if ((ifp = mib_find_if_sys(ifm->ifm_index)) == NULL)
1043                         break;
1044                 if (addrs[RTAX_IFP] != NULL &&
1045                     addrs[RTAX_IFP]->sa_family == AF_LINK) {
1046                         sdl = (struct sockaddr_dl *)(void *)addrs[RTAX_IFP];
1047                         ptr = sdl->sdl_data + sdl->sdl_nlen;
1048                         get_physaddr(ifp, sdl, ptr);
1049                 }
1050                 (void)mib_fetch_ifmib(ifp);
1051                 break;
1052
1053 #ifdef RTM_IFANNOUNCE
1054           case RTM_IFANNOUNCE:
1055                 ifan = (struct if_announcemsghdr *)rtm;
1056                 ifp = mib_find_if_sys(ifan->ifan_index);
1057
1058                 switch (ifan->ifan_what) {
1059
1060                   case IFAN_ARRIVAL:
1061                         if (ifp == NULL && (ifp = mibif_create(ifan->ifan_index,
1062                             ifan->ifan_name)) != NULL) {
1063                                 (void)mib_fetch_ifmib(ifp);
1064                                 check_llbcast(ifp);
1065                                 notify_newif(ifp);
1066                         }
1067                         break;
1068
1069                   case IFAN_DEPARTURE:
1070                         if (ifp != NULL)
1071                                 mibif_free(ifp);
1072                         break;
1073                 }
1074                 break;
1075 #endif
1076           case RTM_GET:
1077           case RTM_ADD:
1078           case RTM_DELETE:
1079                 mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs);
1080
1081                 if (rtm->rtm_errno == 0 && (rtm->rtm_flags & RTF_UP))
1082                         mib_sroute_process(rtm, addrs[RTAX_GATEWAY],
1083                             addrs[RTAX_DST], addrs[RTAX_NETMASK]);
1084                 break;
1085         }
1086 }
1087
1088 /*
1089  * send a routing message
1090  */
1091 void
1092 mib_send_rtmsg(struct rt_msghdr *rtm, struct sockaddr *gw,
1093     struct sockaddr *dst, struct sockaddr *mask)
1094 {
1095         size_t len;
1096         struct rt_msghdr *msg;
1097         char *cp;
1098         ssize_t sent;
1099
1100         len = sizeof(*rtm) + SA_SIZE(gw) + SA_SIZE(dst) + SA_SIZE(mask);
1101         if ((msg = malloc(len)) == NULL) {
1102                 syslog(LOG_ERR, "%s: %m", __func__);
1103                 return;
1104         }
1105         cp = (char *)(msg + 1);
1106
1107         memset(msg, 0, sizeof(*msg));
1108         msg->rtm_flags = 0;
1109         msg->rtm_version = RTM_VERSION;
1110         msg->rtm_addrs = RTA_DST | RTA_GATEWAY;
1111
1112         memcpy(cp, dst, SA_SIZE(dst));
1113         cp += SA_SIZE(dst);
1114         memcpy(cp, gw, SA_SIZE(gw));
1115         cp += SA_SIZE(gw);
1116         if (mask != NULL) {
1117                 memcpy(cp, mask, SA_SIZE(mask));
1118                 cp += SA_SIZE(mask);
1119                 msg->rtm_addrs |= RTA_NETMASK;
1120         }
1121         msg->rtm_msglen = cp - (char *)msg;
1122         msg->rtm_type = RTM_GET;
1123         if ((sent = write(route, msg, msg->rtm_msglen)) == -1) {
1124                 syslog(LOG_ERR, "%s: write: %m", __func__);
1125                 free(msg);
1126                 return;
1127         }
1128         if (sent != msg->rtm_msglen) {
1129                 syslog(LOG_ERR, "%s: short write", __func__);
1130                 free(msg);
1131                 return;
1132         }
1133         free(msg);
1134 }
1135
1136 /*
1137  * Fetch the routing table via sysctl
1138  */
1139 u_char *
1140 mib_fetch_rtab(int af, int info, int arg, size_t *lenp)
1141 {
1142         int name[6];
1143         u_char *buf, *newbuf;
1144
1145         name[0] = CTL_NET;
1146         name[1] = PF_ROUTE;
1147         name[2] = 0;
1148         name[3] = af;
1149         name[4] = info;
1150         name[5] = arg;
1151
1152         *lenp = 0;
1153
1154         /* initial estimate */
1155         if (sysctl(name, 6, NULL, lenp, NULL, 0) == -1) {
1156                 syslog(LOG_ERR, "sysctl estimate (%d,%d,%d,%d,%d,%d): %m",
1157                     name[0], name[1], name[2], name[3], name[4], name[5]);
1158                 return (NULL);
1159         }
1160         if (*lenp == 0)
1161                 return (NULL);
1162
1163         buf = NULL;
1164         for (;;) {
1165                 if ((newbuf = realloc(buf, *lenp)) == NULL) {
1166                         syslog(LOG_ERR, "sysctl buffer: %m");
1167                         free(buf);
1168                         return (NULL);
1169                 }
1170                 buf = newbuf;
1171                         
1172                 if (sysctl(name, 6, buf, lenp, NULL, 0) == 0)
1173                         break;
1174
1175                 if (errno != ENOMEM) {
1176                         syslog(LOG_ERR, "sysctl get: %m");
1177                         free(buf);
1178                         return (NULL);
1179                 }
1180                 *lenp += *lenp / 8 + 1;
1181         }
1182
1183         return (buf);
1184 }
1185
1186 /*
1187  * Update the following info: interface, interface addresses, interface
1188  * receive addresses, arp-table.
1189  * This does not change the interface list itself.
1190  */
1191 static void
1192 update_ifa_info(void)
1193 {
1194         u_char *buf, *next;
1195         struct rt_msghdr *rtm;
1196         struct mibifa *ifa, *ifa1;
1197         struct mibrcvaddr *rcv, *rcv1;
1198         size_t needed;
1199         static const int infos[][3] = {
1200                 { 0, NET_RT_IFLIST, 0 },
1201 #ifdef NET_RT_IFMALIST
1202                 { AF_LINK, NET_RT_IFMALIST, 0 },
1203 #endif
1204         };
1205         u_int i;
1206
1207         TAILQ_FOREACH(ifa, &mibifa_list, link)
1208                 ifa->flags &= ~MIBIFA_FOUND;
1209         TAILQ_FOREACH(rcv, &mibrcvaddr_list, link)
1210                 rcv->flags &= ~MIBRCVADDR_FOUND;
1211
1212         for (i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
1213                 if ((buf = mib_fetch_rtab(infos[i][0], infos[i][1], infos[i][2],
1214                    &needed)) == NULL)
1215                         continue;
1216
1217                 next = buf;
1218                 while (next < buf + needed) {
1219                         rtm = (struct rt_msghdr *)(void *)next;
1220                         next += rtm->rtm_msglen;
1221                         handle_rtmsg(rtm);
1222                 }
1223                 free(buf);
1224         }
1225
1226         /*
1227          * Purge the address list of unused entries. These may happen for
1228          * interface aliases that are on the same subnet. We don't receive
1229          * routing socket messages for them.
1230          */
1231         ifa = TAILQ_FIRST(&mibifa_list);
1232         while (ifa != NULL) {
1233                 ifa1 = TAILQ_NEXT(ifa, link);
1234                 if (!(ifa->flags & MIBIFA_FOUND))
1235                         destroy_ifa(ifa);
1236                 ifa = ifa1;
1237         }
1238
1239         rcv = TAILQ_FIRST(&mibrcvaddr_list);
1240         while (rcv != NULL) {
1241                 rcv1 = TAILQ_NEXT(rcv, link);
1242                 if (!(rcv->flags & (MIBRCVADDR_FOUND | MIBRCVADDR_BCAST |
1243                     MIBRCVADDR_HW)))
1244                         mib_rcvaddr_delete(rcv);
1245                 rcv = rcv1;
1246         }
1247 }
1248
1249 /*
1250  * Update arp table
1251  *
1252 */
1253 void
1254 mib_arp_update(void)
1255 {
1256         struct mibarp *at, *at1;
1257         size_t needed;
1258         u_char *buf, *next;
1259         struct rt_msghdr *rtm;
1260
1261         if (in_update_arp)
1262                 return;         /* Aaargh */
1263         in_update_arp = 1;
1264
1265         TAILQ_FOREACH(at, &mibarp_list, link)
1266                 at->flags &= ~MIBARP_FOUND;
1267
1268         if ((buf = mib_fetch_rtab(AF_INET, NET_RT_FLAGS, 0, &needed)) == NULL) {
1269                 in_update_arp = 0;
1270                 return;
1271         }
1272         
1273         next = buf;
1274         while (next < buf + needed) {
1275                 rtm = (struct rt_msghdr *)(void *)next;
1276                 next += rtm->rtm_msglen;
1277                 handle_rtmsg(rtm);
1278         }
1279         free(buf);
1280
1281         at = TAILQ_FIRST(&mibarp_list);
1282         while (at != NULL) {
1283                 at1 = TAILQ_NEXT(at, link);
1284                 if (!(at->flags & MIBARP_FOUND))
1285                         mib_arp_delete(at);
1286                 at = at1;
1287         }
1288         mibarpticks = get_ticks();
1289         in_update_arp = 0;
1290 }
1291
1292
1293 /*
1294  * Intput on the routing socket.
1295  */
1296 static void
1297 route_input(int fd, void *udata __unused)
1298 {
1299         u_char  buf[1024 * 16];
1300         ssize_t n;
1301         struct rt_msghdr *rtm;
1302
1303         if ((n = read(fd, buf, sizeof(buf))) == -1)
1304                 err(1, "read(rt_socket)");
1305
1306         if (n == 0)
1307                 errx(1, "EOF on rt_socket");
1308
1309         rtm = (struct rt_msghdr *)(void *)buf;
1310         if ((size_t)n != rtm->rtm_msglen)
1311                 errx(1, "n=%zu, rtm_msglen=%u", (size_t)n, rtm->rtm_msglen);
1312
1313         handle_rtmsg(rtm);
1314 }
1315
1316 /*
1317  * execute and SIOCAIFADDR
1318  */
1319 static int
1320 siocaifaddr(char *ifname, struct in_addr addr, struct in_addr mask,
1321     struct in_addr bcast)
1322 {
1323         struct ifaliasreq addreq;
1324         struct sockaddr_in *sa;
1325
1326         memset(&addreq, 0, sizeof(addreq));
1327         strncpy(addreq.ifra_name, ifname, sizeof(addreq.ifra_name));
1328
1329         sa = (struct sockaddr_in *)(void *)&addreq.ifra_addr;
1330         sa->sin_family = AF_INET;
1331         sa->sin_len = sizeof(*sa);
1332         sa->sin_addr = addr;
1333
1334         sa = (struct sockaddr_in *)(void *)&addreq.ifra_mask;
1335         sa->sin_family = AF_INET;
1336         sa->sin_len = sizeof(*sa);
1337         sa->sin_addr = mask;
1338
1339         sa = (struct sockaddr_in *)(void *)&addreq.ifra_broadaddr;
1340         sa->sin_family = AF_INET;
1341         sa->sin_len = sizeof(*sa);
1342         sa->sin_addr = bcast;
1343
1344         return (ioctl(mib_netsock, SIOCAIFADDR, &addreq));
1345 }
1346
1347 /*
1348  * Exececute a SIOCDIFADDR
1349  */
1350 static int
1351 siocdifaddr(const char *ifname, struct in_addr addr)
1352 {
1353         struct ifreq delreq;
1354         struct sockaddr_in *sa;
1355
1356         memset(&delreq, 0, sizeof(delreq));
1357         strncpy(delreq.ifr_name, ifname, sizeof(delreq.ifr_name));
1358         sa = (struct sockaddr_in *)(void *)&delreq.ifr_addr;
1359         sa->sin_family = AF_INET;
1360         sa->sin_len = sizeof(*sa);
1361         sa->sin_addr = addr;
1362
1363         return (ioctl(mib_netsock, SIOCDIFADDR, &delreq));
1364 }
1365
1366 /*
1367  * Verify an interface address without fetching the entire list
1368  */
1369 static int
1370 verify_ifa(const char *name, struct mibifa *ifa)
1371 {
1372         struct ifreq req;
1373         struct sockaddr_in *sa;
1374
1375         memset(&req, 0, sizeof(req));
1376         strncpy(req.ifr_name, name, sizeof(req.ifr_name));
1377         sa = (struct sockaddr_in *)(void *)&req.ifr_addr;
1378         sa->sin_family = AF_INET;
1379         sa->sin_len = sizeof(*sa);
1380         sa->sin_addr = ifa->inaddr;
1381
1382         if (ioctl(mib_netsock, SIOCGIFADDR, &req) == -1)
1383                 return (-1);
1384         if (ifa->inaddr.s_addr != sa->sin_addr.s_addr) {
1385                 syslog(LOG_ERR, "%s: address mismatch", __func__);
1386                 return (-1);
1387         }
1388
1389         if (ioctl(mib_netsock, SIOCGIFNETMASK, &req) == -1)
1390                 return (-1);
1391         if (ifa->inmask.s_addr != sa->sin_addr.s_addr) {
1392                 syslog(LOG_ERR, "%s: netmask mismatch", __func__);
1393                 return (-1);
1394         }
1395         return (0);
1396 }
1397
1398 /*
1399  * Restore a deleted interface address. Don't wait for the routing socket
1400  * to update us.
1401  */
1402 void
1403 mib_undestroy_ifa(struct mibifa *ifa)
1404 {
1405         struct mibif *ifp;
1406
1407         if ((ifp = mib_find_if(ifa->ifindex)) == NULL)
1408                 /* keep it destroyed */
1409                 return;
1410
1411         if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast))
1412                 /* keep it destroyed */
1413                 return;
1414
1415         ifa->flags &= ~MIBIFA_DESTROYED;
1416 }
1417
1418 /*
1419  * Destroy an interface address
1420  */
1421 int
1422 mib_destroy_ifa(struct mibifa *ifa)
1423 {
1424         struct mibif *ifp;
1425
1426         if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1427                 /* ups. */
1428                 mib_iflist_bad = 1;
1429                 return (-1);
1430         }
1431         if (siocdifaddr(ifp->name, ifa->inaddr)) {
1432                 /* ups. */
1433                 syslog(LOG_ERR, "SIOCDIFADDR: %m");
1434                 mib_iflist_bad = 1;
1435                 return (-1);
1436         }
1437         ifa->flags |= MIBIFA_DESTROYED;
1438         return (0);
1439 }
1440
1441 /*
1442  * Rollback the modification of an address. Don't bother to wait for
1443  * the routing socket.
1444  */
1445 void
1446 mib_unmodify_ifa(struct mibifa *ifa)
1447 {
1448         struct mibif *ifp;
1449
1450         if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1451                 /* ups. */
1452                 mib_iflist_bad = 1;
1453                 return;
1454         }
1455
1456         if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast)) {
1457                 /* ups. */
1458                 mib_iflist_bad = 1;
1459                 return;
1460         }
1461 }
1462
1463 /*
1464  * Modify an IFA. 
1465  */
1466 int
1467 mib_modify_ifa(struct mibifa *ifa)
1468 {
1469         struct mibif *ifp;
1470
1471         if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1472                 /* ups. */
1473                 mib_iflist_bad = 1;
1474                 return (-1);
1475         }
1476
1477         if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast)) {
1478                 /* ups. */
1479                 mib_iflist_bad = 1;
1480                 return (-1);
1481         }
1482
1483         if (verify_ifa(ifp->name, ifa)) {
1484                 /* ups. */
1485                 mib_iflist_bad = 1;
1486                 return (-1);
1487         }
1488
1489         return (0);
1490 }
1491
1492 /*
1493  * Destroy a freshly created interface address. Don't bother to wait for
1494  * the routing socket.
1495  */
1496 void
1497 mib_uncreate_ifa(struct mibifa *ifa)
1498 {
1499         struct mibif *ifp;
1500
1501         if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1502                 /* ups. */
1503                 mib_iflist_bad = 1;
1504                 return;
1505         }
1506         if (siocdifaddr(ifp->name, ifa->inaddr)) {
1507                 /* ups. */
1508                 mib_iflist_bad = 1;
1509                 return;
1510         }
1511
1512         destroy_ifa(ifa);
1513 }
1514
1515 /*
1516  * Create a new ifa and verify it
1517  */
1518 struct mibifa *
1519 mib_create_ifa(u_int ifindex, struct in_addr addr, struct in_addr mask,
1520     struct in_addr bcast)
1521 {
1522         struct mibif *ifp;
1523         struct mibifa *ifa;
1524
1525         if ((ifp = mib_find_if(ifindex)) == NULL)
1526                 return (NULL);
1527         if ((ifa = alloc_ifa(ifindex, addr)) == NULL)
1528                 return (NULL);
1529         ifa->inmask = mask;
1530         ifa->inbcast = bcast;
1531
1532         if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast)) {
1533                 syslog(LOG_ERR, "%s: %m", __func__);
1534                 destroy_ifa(ifa);
1535                 return (NULL);
1536         }
1537         if (verify_ifa(ifp->name, ifa)) {
1538                 destroy_ifa(ifa);
1539                 return (NULL);
1540         }
1541         return (ifa);
1542 }
1543
1544 /*
1545  * Get all cloning interfaces and make them dynamic.
1546  * Hah! Whe should probably do this on a periodic basis (XXX).
1547  */
1548 static void
1549 get_cloners(void)
1550 {
1551         struct if_clonereq req;
1552         char *buf, *cp;
1553         int i;
1554
1555         memset(&req, 0, sizeof(req));
1556         if (ioctl(mib_netsock, SIOCIFGCLONERS, &req) == -1) {
1557                 syslog(LOG_ERR, "get cloners: %m");
1558                 return;
1559         }
1560         if ((buf = malloc(req.ifcr_total * IFNAMSIZ)) == NULL) {
1561                 syslog(LOG_ERR, "%m");
1562                 return;
1563         }
1564         req.ifcr_count = req.ifcr_total;
1565         req.ifcr_buffer = buf;
1566         if (ioctl(mib_netsock, SIOCIFGCLONERS, &req) == -1) {
1567                 syslog(LOG_ERR, "get cloners: %m");
1568                 free(buf);
1569                 return;
1570         }
1571         for (cp = buf, i = 0; i < req.ifcr_total; i++, cp += IFNAMSIZ)
1572                 mib_if_set_dyn(cp);
1573         free(buf);
1574 }
1575
1576 /*
1577  * Idle function
1578  */
1579 static void
1580 mibII_idle(void *arg __unused)
1581 {
1582         struct mibifa *ifa;
1583
1584         if (mib_iflist_bad) {
1585                 TAILQ_FOREACH(ifa, &mibifa_list, link)
1586                         ifa->flags &= ~MIBIFA_DESTROYED;
1587
1588                 /* assume, that all cloning interfaces are dynamic */
1589                 get_cloners();
1590
1591                 mib_refresh_iflist();
1592                 update_ifa_info();
1593                 mib_arp_update();
1594                 mib_iflist_bad = 0;
1595         }
1596
1597         mib_arp_update();
1598 }
1599
1600
1601 /*
1602  * Start the module
1603  */
1604 static void
1605 mibII_start(void)
1606 {
1607         if ((route_fd = fd_select(route, route_input, NULL, module)) == NULL) {
1608                 syslog(LOG_ERR, "fd_select(route): %m");
1609                 return;
1610         }
1611         mib_refresh_iflist();
1612         update_ifa_info();
1613         mib_arp_update();
1614         (void)mib_fetch_route();
1615         mib_iftable_last_change = 0;
1616         mib_ifstack_last_change = 0;
1617
1618         ifmib_reg = or_register(&oid_ifMIB,
1619             "The MIB module to describe generic objects for network interface"
1620             " sub-layers.", module);
1621
1622         ipmib_reg = or_register(&oid_ipMIB,
1623            "The MIB module for managing IP and ICMP implementations, but "
1624            "excluding their management of IP routes.", module);
1625
1626         tcpmib_reg = or_register(&oid_tcpMIB,
1627            "The MIB module for managing TCP implementations.", module);
1628
1629         udpmib_reg = or_register(&oid_udpMIB,
1630            "The MIB module for managing UDP implementations.", module);
1631
1632         ipForward_reg = or_register(&oid_ipForward,
1633            "The MIB module for the display of CIDR multipath IP Routes.",
1634            module);
1635
1636         mibII_poll_timer = NULL;
1637         mibII_poll_ticks = MIBII_POLL_TICKS;
1638         mibif_restart_mibII_poll_timer();
1639 }
1640
1641 /*
1642  * Initialize the module
1643  */
1644 static int
1645 mibII_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)
1646 {
1647         size_t len;
1648
1649         module = mod;
1650
1651         len = sizeof(clockinfo);
1652         if (sysctlbyname("kern.clockrate", &clockinfo, &len, NULL, 0) == -1) {
1653                 syslog(LOG_ERR, "kern.clockrate: %m");
1654                 return (-1);
1655         }
1656         if (len != sizeof(clockinfo)) {
1657                 syslog(LOG_ERR, "kern.clockrate: wrong size");
1658                 return (-1);
1659         }
1660
1661         if ((route = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) == -1) {
1662                 syslog(LOG_ERR, "PF_ROUTE: %m");
1663                 return (-1);
1664         }
1665
1666         if ((mib_netsock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
1667                 syslog(LOG_ERR, "PF_INET: %m");
1668                 (void)close(route);
1669                 return (-1);
1670         }
1671         (void)shutdown(mib_netsock, SHUT_RDWR);
1672
1673         /* assume, that all cloning interfaces are dynamic */
1674         get_cloners();
1675
1676         return (0);
1677 }
1678
1679 static int
1680 mibII_fini(void)
1681 {
1682         if (mibII_poll_timer != NULL ) {
1683                 timer_stop(mibII_poll_timer);
1684                 mibII_poll_timer = NULL;
1685         }
1686
1687         if (route_fd != NULL)
1688                 fd_deselect(route_fd);
1689         if (route != -1)
1690                 (void)close(route);
1691         if (mib_netsock != -1)
1692                 (void)close(mib_netsock);
1693         /* XXX free memory */
1694
1695         or_unregister(ipForward_reg);
1696         or_unregister(udpmib_reg);
1697         or_unregister(tcpmib_reg);
1698         or_unregister(ipmib_reg);
1699         or_unregister(ifmib_reg);
1700
1701         return (0);
1702 }
1703
1704 static void
1705 mibII_loading(const struct lmodule *mod, int loaded)
1706 {
1707         struct mibif *ifp;
1708
1709         if (loaded == 1)
1710                 return;
1711
1712         TAILQ_FOREACH(ifp, &mibif_list, link)
1713                 if (ifp->xnotify_mod == mod) {
1714                         ifp->xnotify_mod = NULL;
1715                         ifp->xnotify_data = NULL;
1716                         ifp->xnotify = NULL;
1717                 }
1718
1719         mib_unregister_newif(mod);
1720 }
1721
1722 const struct snmp_module config = {
1723         "This module implements the interface and ip groups.",
1724         mibII_init,
1725         mibII_fini,
1726         NULL,           /* idle */
1727         NULL,           /* dump */
1728         NULL,           /* config */
1729         mibII_start,
1730         NULL,
1731         mibII_ctree,
1732         mibII_CTREE_SIZE,
1733         mibII_loading
1734 };
1735
1736 /*
1737  * Should have a list of these attached to each interface.
1738  */
1739 void *
1740 mibif_notify(struct mibif *ifp, const struct lmodule *mod,
1741     mibif_notify_f func, void *data)
1742 {
1743         ifp->xnotify = func;
1744         ifp->xnotify_data = data;
1745         ifp->xnotify_mod = mod;
1746
1747         return (ifp);
1748 }
1749
1750 void
1751 mibif_unnotify(void *arg)
1752 {
1753         struct mibif *ifp = arg;
1754
1755         ifp->xnotify = NULL;
1756         ifp->xnotify_data = NULL;
1757         ifp->xnotify_mod = NULL;
1758 }