xref: /freebsd/contrib/bsnmp/snmp_mibII/mibII.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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 	struct ifreq irr;
447 
448 	if (fetch_generic_mib(ifp, &oldmib) == -1)
449 		return (-1);
450 
451 	/*
452 	 * Quoting RFC2863, 3.1.15: "... LinkUp and linkDown traps are
453 	 * generated just after ifOperStatus leaves, or just before it
454 	 * enters, the down state, respectively;"
455 	 */
456 	if (ifp->trap_enable && ifp->mib.ifmd_data.ifi_link_state !=
457 	    oldmib.ifmd_data.ifi_link_state &&
458 	    (ifp->mib.ifmd_data.ifi_link_state == LINK_STATE_DOWN ||
459 	    oldmib.ifmd_data.ifi_link_state == LINK_STATE_DOWN))
460 		link_trap(ifp, ifp->mib.ifmd_data.ifi_link_state ==
461 		    LINK_STATE_UP ? 1 : 0);
462 
463 	ifp->flags &= ~(MIBIF_HIGHSPEED | MIBIF_VERYHIGHSPEED);
464 	if (ifp->mib.ifmd_data.ifi_baudrate > 20000000) {
465 		ifp->flags |= MIBIF_HIGHSPEED;
466 		if (ifp->mib.ifmd_data.ifi_baudrate > 650000000)
467 			ifp->flags |= MIBIF_VERYHIGHSPEED;
468 	}
469 	if (ifp->mib.ifmd_data.ifi_baudrate > mibif_maxspeed) {
470 		mibif_maxspeed = ifp->mib.ifmd_data.ifi_baudrate;
471 		mibif_reset_hc_timer();
472 	}
473 
474 	/*
475 	 * linkspecific MIB
476 	 */
477 	name[0] = CTL_NET;
478 	name[1] = PF_LINK;
479 	name[2] = NETLINK_GENERIC;
480 	name[3] = IFMIB_IFDATA;
481 	name[4] = ifp->sysindex;
482 	name[5] = IFDATA_LINKSPECIFIC;
483 	if (sysctl(name, 6, NULL, &len, NULL, 0) == -1) {
484 		syslog(LOG_WARNING, "sysctl linkmib estimate (%s): %m",
485 		    ifp->name);
486 		if (ifp->specmib != NULL) {
487 			ifp->specmib = NULL;
488 			ifp->specmiblen = 0;
489 		}
490 		goto out;
491 	}
492 	if (len == 0) {
493 		if (ifp->specmib != NULL) {
494 			ifp->specmib = NULL;
495 			ifp->specmiblen = 0;
496 		}
497 		goto out;
498 	}
499 
500 	if (ifp->specmiblen != len) {
501 		if ((newmib = realloc(ifp->specmib, len)) == NULL) {
502 			ifp->specmib = NULL;
503 			ifp->specmiblen = 0;
504 			goto out;
505 		}
506 		ifp->specmib = newmib;
507 		ifp->specmiblen = len;
508 	}
509 	if (sysctl(name, 6, ifp->specmib, &len, NULL, 0) == -1) {
510 		syslog(LOG_WARNING, "sysctl linkmib (%s): %m", ifp->name);
511 		if (ifp->specmib != NULL) {
512 			ifp->specmib = NULL;
513 			ifp->specmiblen = 0;
514 		}
515 	}
516 
517   out:
518 	strncpy(irr.ifr_name, ifp->name, sizeof(irr.ifr_name));
519 	irr.ifr_buffer.buffer = MIBIF_PRIV(ifp)->alias;
520 	irr.ifr_buffer.length = sizeof(MIBIF_PRIV(ifp)->alias);
521 	if (ioctl(mib_netsock, SIOCGIFDESCR, &irr) == -1) {
522 		MIBIF_PRIV(ifp)->alias[0] = 0;
523 		if (errno != ENOMSG)
524 			syslog(LOG_WARNING, "SIOCGIFDESCR (%s): %m", ifp->name);
525 	} else if (irr.ifr_buffer.buffer == NULL) {
526 		MIBIF_PRIV(ifp)->alias[0] = 0;
527 		syslog(LOG_WARNING, "SIOCGIFDESCR (%s): too long (%zu)",
528 		    ifp->name, irr.ifr_buffer.length);
529 	}
530 	ifp->mibtick = get_ticks();
531 	return (0);
532 }
533 
534 /* find first/next address for a given interface */
535 struct mibifa *
536 mib_first_ififa(const struct mibif *ifp)
537 {
538 	struct mibifa *ifa;
539 
540 	TAILQ_FOREACH(ifa, &mibifa_list, link)
541 		if (ifp->index == ifa->ifindex)
542 			return (ifa);
543 	return (NULL);
544 }
545 
546 struct mibifa *
547 mib_next_ififa(struct mibifa *ifa0)
548 {
549 	struct mibifa *ifa;
550 
551 	ifa = ifa0;
552 	while ((ifa = TAILQ_NEXT(ifa, link)) != NULL)
553 		if (ifa->ifindex == ifa0->ifindex)
554 			return (ifa);
555 	return (NULL);
556 }
557 
558 /*
559  * Allocate a new IFA
560  */
561 static struct mibifa *
562 alloc_ifa(u_int ifindex, struct in_addr addr)
563 {
564 	struct mibifa *ifa;
565 	uint32_t ha;
566 
567 	if ((ifa = malloc(sizeof(struct mibifa))) == NULL) {
568 		syslog(LOG_ERR, "ifa: %m");
569 		return (NULL);
570 	}
571 	ifa->inaddr = addr;
572 	ifa->ifindex = ifindex;
573 
574 	ha = ntohl(ifa->inaddr.s_addr);
575 	ifa->index.len = 4;
576 	ifa->index.subs[0] = (ha >> 24) & 0xff;
577 	ifa->index.subs[1] = (ha >> 16) & 0xff;
578 	ifa->index.subs[2] = (ha >>  8) & 0xff;
579 	ifa->index.subs[3] = (ha >>  0) & 0xff;
580 
581 	ifa->flags = 0;
582 	ifa->inbcast.s_addr = 0;
583 	ifa->inmask.s_addr = 0xffffffff;
584 
585 	INSERT_OBJECT_OID(ifa, &mibifa_list);
586 
587 	return (ifa);
588 }
589 
590 /*
591  * Delete an interface address
592  */
593 static void
594 destroy_ifa(struct mibifa *ifa)
595 {
596 	TAILQ_REMOVE(&mibifa_list, ifa, link);
597 	free(ifa);
598 }
599 
600 
601 /*
602  * Helper routine to extract the sockaddr structures from a routing
603  * socket message.
604  */
605 void
606 mib_extract_addrs(int addrs, u_char *info, struct sockaddr **out)
607 {
608 	u_int i;
609 
610 	for (i = 0; i < RTAX_MAX; i++) {
611 		if ((addrs & (1 << i)) != 0) {
612 			*out = (struct sockaddr *)(void *)info;
613 			info += roundup((*out)->sa_len, sizeof(long));
614 		} else
615 			*out = NULL;
616 		out++;
617 	}
618 }
619 
620 /*
621  * save the phys address of an interface. Handle receive address entries here.
622  */
623 static void
624 get_physaddr(struct mibif *ifp, struct sockaddr_dl *sdl, u_char *ptr)
625 {
626 	u_char *np;
627 	struct mibrcvaddr *rcv;
628 
629 	if (sdl->sdl_alen == 0) {
630 		/* no address */
631 		if (ifp->physaddrlen != 0) {
632 			if ((rcv = mib_find_rcvaddr(ifp->index, ifp->physaddr,
633 			    ifp->physaddrlen)) != NULL)
634 				mib_rcvaddr_delete(rcv);
635 			free(ifp->physaddr);
636 			ifp->physaddr = NULL;
637 			ifp->physaddrlen = 0;
638 		}
639 		return;
640 	}
641 
642 	if (ifp->physaddrlen != sdl->sdl_alen) {
643 		/* length changed */
644 		if (ifp->physaddrlen) {
645 			/* delete olf receive address */
646 			if ((rcv = mib_find_rcvaddr(ifp->index, ifp->physaddr,
647 			    ifp->physaddrlen)) != NULL)
648 				mib_rcvaddr_delete(rcv);
649 		}
650 		if ((np = realloc(ifp->physaddr, sdl->sdl_alen)) == NULL) {
651 			free(ifp->physaddr);
652 			ifp->physaddr = NULL;
653 			ifp->physaddrlen = 0;
654 			return;
655 		}
656 		ifp->physaddr = np;
657 		ifp->physaddrlen = sdl->sdl_alen;
658 
659 	} else if (memcmp(ifp->physaddr, ptr, ifp->physaddrlen) == 0) {
660 		/* no change */
661 		return;
662 
663 	} else {
664 		/* address changed */
665 
666 		/* delete olf receive address */
667 		if ((rcv = mib_find_rcvaddr(ifp->index, ifp->physaddr,
668 		    ifp->physaddrlen)) != NULL)
669 			mib_rcvaddr_delete(rcv);
670 	}
671 
672 	memcpy(ifp->physaddr, ptr, ifp->physaddrlen);
673 
674 	/* make new receive address */
675 	if ((rcv = mib_rcvaddr_create(ifp, ifp->physaddr, ifp->physaddrlen)) != NULL)
676 		rcv->flags |= MIBRCVADDR_HW;
677 }
678 
679 /*
680  * Free an interface
681  */
682 static void
683 mibif_free(struct mibif *ifp)
684 {
685 	struct mibif *ifp1;
686 	struct mibindexmap *map;
687 	struct mibifa *ifa, *ifa1;
688 	struct mibrcvaddr *rcv, *rcv1;
689 	struct mibarp *at, *at1;
690 
691 	if (ifp->xnotify != NULL)
692 		(*ifp->xnotify)(ifp, MIBIF_NOTIFY_DESTROY, ifp->xnotify_data);
693 
694 	(void)mib_ifstack_delete(ifp, NULL);
695 	(void)mib_ifstack_delete(NULL, ifp);
696 
697 	TAILQ_REMOVE(&mibif_list, ifp, link);
698 
699 	/* if this was the fastest interface - recompute this */
700 	if (ifp->mib.ifmd_data.ifi_baudrate == mibif_maxspeed) {
701 		mibif_maxspeed = ifp->mib.ifmd_data.ifi_baudrate;
702 		TAILQ_FOREACH(ifp1, &mibif_list, link)
703 			if (ifp1->mib.ifmd_data.ifi_baudrate > mibif_maxspeed)
704 				mibif_maxspeed =
705 				    ifp1->mib.ifmd_data.ifi_baudrate;
706 		mibif_reset_hc_timer();
707 	}
708 
709 	free(ifp->private);
710 	if (ifp->physaddr != NULL)
711 		free(ifp->physaddr);
712 	if (ifp->specmib != NULL)
713 		free(ifp->specmib);
714 
715 	STAILQ_FOREACH(map, &mibindexmap_list, link)
716 		if (map->mibif == ifp) {
717 			map->mibif = NULL;
718 			break;
719 		}
720 
721 	/* purge interface addresses */
722 	ifa = TAILQ_FIRST(&mibifa_list);
723 	while (ifa != NULL) {
724 		ifa1 = TAILQ_NEXT(ifa, link);
725 		if (ifa->ifindex == ifp->index)
726 			destroy_ifa(ifa);
727 		ifa = ifa1;
728 	}
729 
730 	/* purge receive addresses */
731 	rcv = TAILQ_FIRST(&mibrcvaddr_list);
732 	while (rcv != NULL) {
733 		rcv1 = TAILQ_NEXT(rcv, link);
734 		if (rcv->ifindex == ifp->index)
735 			mib_rcvaddr_delete(rcv);
736 		rcv = rcv1;
737 	}
738 
739 	/* purge ARP entries */
740 	at = TAILQ_FIRST(&mibarp_list);
741 	while (at != NULL) {
742 		at1 = TAILQ_NEXT(at, link);
743 		if (at->index.subs[0] == ifp->index)
744 			mib_arp_delete(at);
745 		at = at1;
746 	}
747 
748 
749 	free(ifp);
750 	mib_if_number--;
751 	mib_iftable_last_change = this_tick;
752 }
753 
754 /*
755  * Create a new interface
756  */
757 static struct mibif *
758 mibif_create(u_int sysindex, const char *name)
759 {
760 	struct mibif *ifp;
761 	struct mibindexmap *map;
762 
763 	if ((ifp = malloc(sizeof(*ifp))) == NULL) {
764 		syslog(LOG_WARNING, "%s: %m", __func__);
765 		return (NULL);
766 	}
767 	memset(ifp, 0, sizeof(*ifp));
768 	if ((ifp->private = malloc(sizeof(struct mibif_private))) == NULL) {
769 		syslog(LOG_WARNING, "%s: %m", __func__);
770 		free(ifp);
771 		return (NULL);
772 	}
773 	memset(ifp->private, 0, sizeof(struct mibif_private));
774 
775 	ifp->sysindex = sysindex;
776 	strcpy(ifp->name, name);
777 	strcpy(ifp->descr, name);
778 	ifp->spec_oid = oid_zeroDotZero;
779 
780 	map = NULL;
781 	if (!mib_if_is_dyn(ifp->name)) {
782 		/* non-dynamic. look whether we know the interface */
783 		STAILQ_FOREACH(map, &mibindexmap_list, link)
784 			if (strcmp(map->name, ifp->name) == 0) {
785 				ifp->index = map->ifindex;
786 				map->mibif = ifp;
787 				break;
788 			}
789 		/* assume it has a connector if it is not dynamic */
790 		ifp->has_connector = 1;
791 		ifp->trap_enable = 1;
792 	}
793 	if (map == NULL) {
794 		/* new interface - get new index */
795 		if (next_if_index > 0x7fffffff)
796 			errx(1, "ifindex wrap");
797 
798 		if ((map = malloc(sizeof(*map))) == NULL) {
799 			syslog(LOG_ERR, "ifmap: %m");
800 			free(ifp);
801 			return (NULL);
802 		}
803 		map->ifindex = next_if_index++;
804 		map->sysindex = ifp->sysindex;
805 		strcpy(map->name, ifp->name);
806 		map->mibif = ifp;
807 		STAILQ_INSERT_TAIL(&mibindexmap_list, map, link);
808 	} else {
809 		/* re-instantiate. Introduce a counter discontinuity */
810 		ifp->counter_disc = get_ticks();
811 	}
812 	ifp->index = map->ifindex;
813 	ifp->mib.ifmd_data.ifi_link_state = LINK_STATE_UNKNOWN;
814 
815 	INSERT_OBJECT_INT(ifp, &mibif_list);
816 	mib_if_number++;
817 	mib_iftable_last_change = this_tick;
818 
819 	/* instantiate default ifStack entries */
820 	(void)mib_ifstack_create(ifp, NULL);
821 	(void)mib_ifstack_create(NULL, ifp);
822 
823 	return (ifp);
824 }
825 
826 /*
827  * Inform all interested parties about a new interface
828  */
829 static void
830 notify_newif(struct mibif *ifp)
831 {
832 	struct newifreg *reg;
833 
834 	TAILQ_FOREACH(reg, &newifreg_list, link)
835 		if ((*reg->func)(ifp))
836 			return;
837 }
838 
839 /*
840  * This is called for new interfaces after we have fetched the interface
841  * MIB. If this is a broadcast interface try to guess the broadcast address
842  * depending on the interface type.
843  */
844 static void
845 check_llbcast(struct mibif *ifp)
846 {
847 	static u_char ether_bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
848 	static u_char arcnet_bcast = 0;
849 	struct mibrcvaddr *rcv;
850 
851 	if (!(ifp->mib.ifmd_flags & IFF_BROADCAST))
852 		return;
853 
854 	switch (ifp->mib.ifmd_data.ifi_type) {
855 
856 	  case IFT_ETHER:
857 	  case IFT_FDDI:
858 	  case IFT_ISO88025:
859 	  case IFT_L2VLAN:
860 		if (mib_find_rcvaddr(ifp->index, ether_bcast, 6) == NULL &&
861 		    (rcv = mib_rcvaddr_create(ifp, ether_bcast, 6)) != NULL)
862 			rcv->flags |= MIBRCVADDR_BCAST;
863 		break;
864 
865 	  case IFT_ARCNET:
866 		if (mib_find_rcvaddr(ifp->index, &arcnet_bcast, 1) == NULL &&
867 		    (rcv = mib_rcvaddr_create(ifp, &arcnet_bcast, 1)) != NULL)
868 			rcv->flags |= MIBRCVADDR_BCAST;
869 		break;
870 	}
871 }
872 
873 
874 /*
875  * Retrieve the current interface list from the system.
876  */
877 void
878 mib_refresh_iflist(void)
879 {
880 	struct mibif *ifp, *ifp1;
881 	size_t len;
882 	u_short idx;
883 	int name[6];
884 	int count;
885 	struct ifmibdata mib;
886 
887 	TAILQ_FOREACH(ifp, &mibif_list, link)
888 		ifp->flags &= ~MIBIF_FOUND;
889 
890 	len = sizeof(count);
891 	if (sysctlbyname("net.link.generic.system.ifcount", &count, &len,
892 	    NULL, 0) == -1) {
893 		syslog(LOG_ERR, "ifcount: %m");
894 		return;
895 	}
896 	name[0] = CTL_NET;
897 	name[1] = PF_LINK;
898 	name[2] = NETLINK_GENERIC;
899 	name[3] = IFMIB_IFDATA;
900 	name[5] = IFDATA_GENERAL;
901 	for (idx = 1; idx <= count; idx++) {
902 		name[4] = idx;
903 		len = sizeof(mib);
904 		if (sysctl(name, 6, &mib, &len, NULL, 0) == -1) {
905 			if (errno == ENOENT)
906 				continue;
907 			syslog(LOG_ERR, "ifmib(%u): %m", idx);
908 			return;
909 		}
910 		if ((ifp = mib_find_if_sys(idx)) != NULL) {
911 			ifp->flags |= MIBIF_FOUND;
912 			continue;
913 		}
914 		/* Unknown interface - create */
915 		if ((ifp = mibif_create(idx, mib.ifmd_name)) != NULL) {
916 			ifp->flags |= MIBIF_FOUND;
917 			(void)mib_fetch_ifmib(ifp);
918 			check_llbcast(ifp);
919 			notify_newif(ifp);
920 		}
921 	}
922 
923 	/*
924 	 * Purge interfaces that disappeared
925 	 */
926 	ifp = TAILQ_FIRST(&mibif_list);
927 	while (ifp != NULL) {
928 		ifp1 = TAILQ_NEXT(ifp, link);
929 		if (!(ifp->flags & MIBIF_FOUND))
930 			mibif_free(ifp);
931 		ifp = ifp1;
932 	}
933 }
934 
935 /*
936  * Find an interface address
937  */
938 struct mibifa *
939 mib_find_ifa(struct in_addr addr)
940 {
941 	struct mibifa *ifa;
942 
943 	TAILQ_FOREACH(ifa, &mibifa_list, link)
944 		if (ifa->inaddr.s_addr == addr.s_addr)
945 			return (ifa);
946 	return (NULL);
947 }
948 
949 /*
950  * Process a new ARP entry
951  */
952 static void
953 process_arp(const struct rt_msghdr *rtm, const struct sockaddr_dl *sdl,
954     const struct sockaddr_in *sa)
955 {
956 	struct mibif *ifp;
957 	struct mibarp *at;
958 
959 	/* IP arp table entry */
960 	if (sdl->sdl_alen == 0)
961 		return;
962 	if ((ifp = mib_find_if_sys(sdl->sdl_index)) == NULL)
963 		return;
964 	/* have a valid entry */
965 	if ((at = mib_find_arp(ifp, sa->sin_addr)) == NULL &&
966 	    (at = mib_arp_create(ifp, sa->sin_addr,
967 	    sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) == NULL)
968 		return;
969 
970 	if (rtm->rtm_rmx.rmx_expire == 0)
971 		at->flags |= MIBARP_PERM;
972 	else
973 		at->flags &= ~MIBARP_PERM;
974 	at->flags |= MIBARP_FOUND;
975 }
976 
977 /*
978  * Handle a routing socket message.
979  */
980 static void
981 handle_rtmsg(struct rt_msghdr *rtm)
982 {
983 	struct sockaddr *addrs[RTAX_MAX];
984 	struct if_msghdr *ifm;
985 	struct ifa_msghdr ifam;
986 	struct ifma_msghdr *ifmam;
987 #ifdef RTM_IFANNOUNCE
988 	struct if_announcemsghdr *ifan;
989 #endif
990 	struct mibif *ifp;
991 	struct sockaddr_dl *sdl;
992 	struct sockaddr_in *sa;
993 	struct mibifa *ifa;
994 	struct mibrcvaddr *rcv;
995 	u_char *ptr;
996 
997 	if (rtm->rtm_version != RTM_VERSION) {
998 		syslog(LOG_ERR, "Bogus RTM version %u", rtm->rtm_version);
999 		return;
1000 	}
1001 
1002 	switch (rtm->rtm_type) {
1003 
1004 	  case RTM_NEWADDR:
1005 		memcpy(&ifam, rtm, sizeof(ifam));
1006 		mib_extract_addrs(ifam.ifam_addrs, (u_char *)(&ifam + 1), addrs);
1007 		if (addrs[RTAX_IFA] == NULL || addrs[RTAX_NETMASK] == NULL)
1008 			break;
1009 
1010 		sa = (struct sockaddr_in *)(void *)addrs[RTAX_IFA];
1011 		if ((ifa = mib_find_ifa(sa->sin_addr)) == NULL) {
1012 			/* unknown address */
1013 		    	if ((ifp = mib_find_if_sys(ifam.ifam_index)) == NULL) {
1014 				syslog(LOG_WARNING, "RTM_NEWADDR for unknown "
1015 				    "interface %u", ifam.ifam_index);
1016 				break;
1017 			}
1018 		     	if ((ifa = alloc_ifa(ifp->index, sa->sin_addr)) == NULL)
1019 				break;
1020 		}
1021 		sa = (struct sockaddr_in *)(void *)addrs[RTAX_NETMASK];
1022 		ifa->inmask = sa->sin_addr;
1023 
1024 		if (addrs[RTAX_BRD] != NULL) {
1025 			sa = (struct sockaddr_in *)(void *)addrs[RTAX_BRD];
1026 			ifa->inbcast = sa->sin_addr;
1027 		}
1028 		ifa->flags |= MIBIFA_FOUND;
1029 		break;
1030 
1031 	  case RTM_DELADDR:
1032 		memcpy(&ifam, rtm, sizeof(ifam));
1033 		mib_extract_addrs(ifam.ifam_addrs, (u_char *)(&ifam + 1), addrs);
1034 		if (addrs[RTAX_IFA] == NULL)
1035 			break;
1036 
1037 		sa = (struct sockaddr_in *)(void *)addrs[RTAX_IFA];
1038 		if ((ifa = mib_find_ifa(sa->sin_addr)) != NULL) {
1039 			ifa->flags |= MIBIFA_FOUND;
1040 			if (!(ifa->flags & MIBIFA_DESTROYED))
1041 				destroy_ifa(ifa);
1042 		}
1043 		break;
1044 
1045 	  case RTM_NEWMADDR:
1046 		ifmam = (struct ifma_msghdr *)rtm;
1047 		mib_extract_addrs(ifmam->ifmam_addrs, (u_char *)(ifmam + 1), addrs);
1048 		if (addrs[RTAX_IFA] == NULL ||
1049 		    addrs[RTAX_IFA]->sa_family != AF_LINK)
1050 			break;
1051 		sdl = (struct sockaddr_dl *)(void *)addrs[RTAX_IFA];
1052 		if ((rcv = mib_find_rcvaddr(sdl->sdl_index,
1053 		    sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) == NULL) {
1054 			/* unknown address */
1055 		    	if ((ifp = mib_find_if_sys(sdl->sdl_index)) == NULL) {
1056 				syslog(LOG_WARNING, "RTM_NEWMADDR for unknown "
1057 				    "interface %u", sdl->sdl_index);
1058 				break;
1059 			}
1060 		     	if ((rcv = mib_rcvaddr_create(ifp,
1061 			    sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) == NULL)
1062 				break;
1063 			rcv->flags |= MIBRCVADDR_VOLATILE;
1064 		}
1065 		rcv->flags |= MIBRCVADDR_FOUND;
1066 		break;
1067 
1068 	  case RTM_DELMADDR:
1069 		ifmam = (struct ifma_msghdr *)rtm;
1070 		mib_extract_addrs(ifmam->ifmam_addrs, (u_char *)(ifmam + 1), addrs);
1071 		if (addrs[RTAX_IFA] == NULL ||
1072 		    addrs[RTAX_IFA]->sa_family != AF_LINK)
1073 			break;
1074 		sdl = (struct sockaddr_dl *)(void *)addrs[RTAX_IFA];
1075 		if ((rcv = mib_find_rcvaddr(sdl->sdl_index,
1076 		    sdl->sdl_data + sdl->sdl_nlen, sdl->sdl_alen)) != NULL)
1077 			mib_rcvaddr_delete(rcv);
1078 		break;
1079 
1080 	  case RTM_IFINFO:
1081 		ifm = (struct if_msghdr *)(void *)rtm;
1082 		mib_extract_addrs(ifm->ifm_addrs, (u_char *)(ifm + 1), addrs);
1083 		if ((ifp = mib_find_if_sys(ifm->ifm_index)) == NULL)
1084 			break;
1085 		if (addrs[RTAX_IFP] != NULL &&
1086 		    addrs[RTAX_IFP]->sa_family == AF_LINK) {
1087 			sdl = (struct sockaddr_dl *)(void *)addrs[RTAX_IFP];
1088 			ptr = sdl->sdl_data + sdl->sdl_nlen;
1089 			get_physaddr(ifp, sdl, ptr);
1090 		}
1091 		(void)mib_fetch_ifmib(ifp);
1092 		break;
1093 
1094 #ifdef RTM_IFANNOUNCE
1095 	  case RTM_IFANNOUNCE:
1096 		ifan = (struct if_announcemsghdr *)rtm;
1097 		ifp = mib_find_if_sys(ifan->ifan_index);
1098 
1099 		switch (ifan->ifan_what) {
1100 
1101 		  case IFAN_ARRIVAL:
1102 			if (ifp == NULL && (ifp = mibif_create(ifan->ifan_index,
1103 			    ifan->ifan_name)) != NULL) {
1104 				(void)mib_fetch_ifmib(ifp);
1105 				check_llbcast(ifp);
1106 				notify_newif(ifp);
1107 			}
1108 			break;
1109 
1110 		  case IFAN_DEPARTURE:
1111 			if (ifp != NULL)
1112 				mibif_free(ifp);
1113 			break;
1114 		}
1115 		break;
1116 #endif
1117 	  case RTM_GET:
1118 	  case RTM_ADD:
1119 		mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs);
1120 		if (rtm->rtm_flags & RTF_LLINFO) {
1121 			if (addrs[RTAX_DST] == NULL ||
1122 			    addrs[RTAX_GATEWAY] == NULL ||
1123 			    addrs[RTAX_DST]->sa_family != AF_INET ||
1124 			    addrs[RTAX_GATEWAY]->sa_family != AF_LINK)
1125 				break;
1126 			process_arp(rtm,
1127 			    (struct sockaddr_dl *)(void *)addrs[RTAX_GATEWAY],
1128 			    (struct sockaddr_in *)(void *)addrs[RTAX_DST]);
1129 		} else {
1130 			if (rtm->rtm_errno == 0 && (rtm->rtm_flags & RTF_UP))
1131 				mib_sroute_process(rtm, addrs[RTAX_GATEWAY],
1132 				    addrs[RTAX_DST], addrs[RTAX_NETMASK]);
1133 		}
1134 		break;
1135 
1136 	  case RTM_DELETE:
1137 		mib_extract_addrs(rtm->rtm_addrs, (u_char *)(rtm + 1), addrs);
1138 
1139 		if (rtm->rtm_errno == 0 && (rtm->rtm_flags & RTF_UP))
1140 			mib_sroute_process(rtm, addrs[RTAX_GATEWAY],
1141 			    addrs[RTAX_DST], addrs[RTAX_NETMASK]);
1142 		break;
1143 	}
1144 }
1145 
1146 /*
1147  * send a routing message
1148  */
1149 void
1150 mib_send_rtmsg(struct rt_msghdr *rtm, struct sockaddr *gw,
1151     struct sockaddr *dst, struct sockaddr *mask)
1152 {
1153 	size_t len;
1154 	struct rt_msghdr *msg;
1155 	char *cp;
1156 	ssize_t sent;
1157 
1158 	len = sizeof(*rtm) + SA_SIZE(gw) + SA_SIZE(dst) + SA_SIZE(mask);
1159 	if ((msg = malloc(len)) == NULL) {
1160 		syslog(LOG_ERR, "%s: %m", __func__);
1161 		return;
1162 	}
1163 	cp = (char *)(msg + 1);
1164 
1165 	memset(msg, 0, sizeof(*msg));
1166 	msg->rtm_flags = 0;
1167 	msg->rtm_version = RTM_VERSION;
1168 	msg->rtm_addrs = RTA_DST | RTA_GATEWAY;
1169 
1170 	memcpy(cp, dst, SA_SIZE(dst));
1171 	cp += SA_SIZE(dst);
1172 	memcpy(cp, gw, SA_SIZE(gw));
1173 	cp += SA_SIZE(gw);
1174 	if (mask != NULL) {
1175 		memcpy(cp, mask, SA_SIZE(mask));
1176 		cp += SA_SIZE(mask);
1177 		msg->rtm_addrs |= RTA_NETMASK;
1178 	}
1179 	msg->rtm_msglen = cp - (char *)msg;
1180 	msg->rtm_type = RTM_GET;
1181 	if ((sent = write(route, msg, msg->rtm_msglen)) == -1) {
1182 		syslog(LOG_ERR, "%s: write: %m", __func__);
1183 		free(msg);
1184 		return;
1185 	}
1186 	if (sent != msg->rtm_msglen) {
1187 		syslog(LOG_ERR, "%s: short write", __func__);
1188 		free(msg);
1189 		return;
1190 	}
1191 	free(msg);
1192 }
1193 
1194 /*
1195  * Fetch the routing table via sysctl
1196  */
1197 u_char *
1198 mib_fetch_rtab(int af, int info, int arg, size_t *lenp)
1199 {
1200 	int name[6];
1201 	u_char *buf, *newbuf;
1202 
1203 	name[0] = CTL_NET;
1204 	name[1] = PF_ROUTE;
1205 	name[2] = 0;
1206 	name[3] = af;
1207 	name[4] = info;
1208 	name[5] = arg;
1209 
1210 	*lenp = 0;
1211 
1212 	/* initial estimate */
1213 	if (sysctl(name, 6, NULL, lenp, NULL, 0) == -1) {
1214 		syslog(LOG_ERR, "sysctl estimate (%d,%d,%d,%d,%d,%d): %m",
1215 		    name[0], name[1], name[2], name[3], name[4], name[5]);
1216 		return (NULL);
1217 	}
1218 	if (*lenp == 0)
1219 		return (NULL);
1220 
1221 	buf = NULL;
1222 	for (;;) {
1223 		if ((newbuf = realloc(buf, *lenp)) == NULL) {
1224 			syslog(LOG_ERR, "sysctl buffer: %m");
1225 			free(buf);
1226 			return (NULL);
1227 		}
1228 		buf = newbuf;
1229 
1230 		if (sysctl(name, 6, buf, lenp, NULL, 0) == 0)
1231 			break;
1232 
1233 		if (errno != ENOMEM) {
1234 			syslog(LOG_ERR, "sysctl get: %m");
1235 			free(buf);
1236 			return (NULL);
1237 		}
1238 		*lenp += *lenp / 8 + 1;
1239 	}
1240 
1241 	return (buf);
1242 }
1243 
1244 /*
1245  * Update the following info: interface, interface addresses, interface
1246  * receive addresses, arp-table.
1247  * This does not change the interface list itself.
1248  */
1249 static void
1250 update_ifa_info(void)
1251 {
1252 	u_char *buf, *next;
1253 	struct rt_msghdr *rtm;
1254 	struct mibifa *ifa, *ifa1;
1255 	struct mibrcvaddr *rcv, *rcv1;
1256 	size_t needed;
1257 	static const int infos[][3] = {
1258 		{ 0, NET_RT_IFLIST, 0 },
1259 #ifdef NET_RT_IFMALIST
1260 		{ AF_LINK, NET_RT_IFMALIST, 0 },
1261 #endif
1262 	};
1263 	u_int i;
1264 
1265 	TAILQ_FOREACH(ifa, &mibifa_list, link)
1266 		ifa->flags &= ~MIBIFA_FOUND;
1267 	TAILQ_FOREACH(rcv, &mibrcvaddr_list, link)
1268 		rcv->flags &= ~MIBRCVADDR_FOUND;
1269 
1270 	for (i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
1271 		if ((buf = mib_fetch_rtab(infos[i][0], infos[i][1], infos[i][2],
1272 		   &needed)) == NULL)
1273 			continue;
1274 
1275 		next = buf;
1276 		while (next < buf + needed) {
1277 			rtm = (struct rt_msghdr *)(void *)next;
1278 			next += rtm->rtm_msglen;
1279 			handle_rtmsg(rtm);
1280 		}
1281 		free(buf);
1282 	}
1283 
1284 	/*
1285 	 * Purge the address list of unused entries. These may happen for
1286 	 * interface aliases that are on the same subnet. We don't receive
1287 	 * routing socket messages for them.
1288 	 */
1289 	ifa = TAILQ_FIRST(&mibifa_list);
1290 	while (ifa != NULL) {
1291 		ifa1 = TAILQ_NEXT(ifa, link);
1292 		if (!(ifa->flags & MIBIFA_FOUND))
1293 			destroy_ifa(ifa);
1294 		ifa = ifa1;
1295 	}
1296 
1297 	rcv = TAILQ_FIRST(&mibrcvaddr_list);
1298 	while (rcv != NULL) {
1299 		rcv1 = TAILQ_NEXT(rcv, link);
1300 		if (!(rcv->flags & (MIBRCVADDR_FOUND | MIBRCVADDR_BCAST |
1301 		    MIBRCVADDR_HW)))
1302 			mib_rcvaddr_delete(rcv);
1303 		rcv = rcv1;
1304 	}
1305 }
1306 
1307 /*
1308  * Update arp table
1309  *
1310 */
1311 void
1312 mib_arp_update(void)
1313 {
1314 	struct mibarp *at, *at1;
1315 	size_t needed;
1316 	u_char *buf, *next;
1317 	struct rt_msghdr *rtm;
1318 
1319 	if (in_update_arp)
1320 		return;		/* Aaargh */
1321 	in_update_arp = 1;
1322 
1323 	TAILQ_FOREACH(at, &mibarp_list, link)
1324 		at->flags &= ~MIBARP_FOUND;
1325 
1326 	if ((buf = mib_fetch_rtab(AF_INET, NET_RT_FLAGS, 0, &needed)) == NULL) {
1327 		in_update_arp = 0;
1328 		return;
1329 	}
1330 
1331 	next = buf;
1332 	while (next < buf + needed) {
1333 		rtm = (struct rt_msghdr *)(void *)next;
1334 		next += rtm->rtm_msglen;
1335 		handle_rtmsg(rtm);
1336 	}
1337 	free(buf);
1338 
1339 	at = TAILQ_FIRST(&mibarp_list);
1340 	while (at != NULL) {
1341 		at1 = TAILQ_NEXT(at, link);
1342 		if (!(at->flags & MIBARP_FOUND))
1343 			mib_arp_delete(at);
1344 		at = at1;
1345 	}
1346 	mibarpticks = get_ticks();
1347 	in_update_arp = 0;
1348 }
1349 
1350 
1351 /*
1352  * Intput on the routing socket.
1353  */
1354 static void
1355 route_input(int fd, void *udata __unused)
1356 {
1357 	u_char	buf[1024 * 16];
1358 	ssize_t n;
1359 	struct rt_msghdr *rtm;
1360 
1361 	if ((n = read(fd, buf, sizeof(buf))) == -1)
1362 		err(1, "read(rt_socket)");
1363 
1364 	if (n == 0)
1365 		errx(1, "EOF on rt_socket");
1366 
1367 	rtm = (struct rt_msghdr *)(void *)buf;
1368 	if ((size_t)n != rtm->rtm_msglen)
1369 		errx(1, "n=%zu, rtm_msglen=%u", (size_t)n, rtm->rtm_msglen);
1370 
1371 	handle_rtmsg(rtm);
1372 }
1373 
1374 /*
1375  * execute and SIOCAIFADDR
1376  */
1377 static int
1378 siocaifaddr(char *ifname, struct in_addr addr, struct in_addr mask,
1379     struct in_addr bcast)
1380 {
1381 	struct ifaliasreq addreq;
1382 	struct sockaddr_in *sa;
1383 
1384 	memset(&addreq, 0, sizeof(addreq));
1385 	strncpy(addreq.ifra_name, ifname, sizeof(addreq.ifra_name));
1386 
1387 	sa = (struct sockaddr_in *)(void *)&addreq.ifra_addr;
1388 	sa->sin_family = AF_INET;
1389 	sa->sin_len = sizeof(*sa);
1390 	sa->sin_addr = addr;
1391 
1392 	sa = (struct sockaddr_in *)(void *)&addreq.ifra_mask;
1393 	sa->sin_family = AF_INET;
1394 	sa->sin_len = sizeof(*sa);
1395 	sa->sin_addr = mask;
1396 
1397 	sa = (struct sockaddr_in *)(void *)&addreq.ifra_broadaddr;
1398 	sa->sin_family = AF_INET;
1399 	sa->sin_len = sizeof(*sa);
1400 	sa->sin_addr = bcast;
1401 
1402 	return (ioctl(mib_netsock, SIOCAIFADDR, &addreq));
1403 }
1404 
1405 /*
1406  * Exececute a SIOCDIFADDR
1407  */
1408 static int
1409 siocdifaddr(const char *ifname, struct in_addr addr)
1410 {
1411 	struct ifreq delreq;
1412 	struct sockaddr_in *sa;
1413 
1414 	memset(&delreq, 0, sizeof(delreq));
1415 	strncpy(delreq.ifr_name, ifname, sizeof(delreq.ifr_name));
1416 	sa = (struct sockaddr_in *)(void *)&delreq.ifr_addr;
1417 	sa->sin_family = AF_INET;
1418 	sa->sin_len = sizeof(*sa);
1419 	sa->sin_addr = addr;
1420 
1421 	return (ioctl(mib_netsock, SIOCDIFADDR, &delreq));
1422 }
1423 
1424 /*
1425  * Verify an interface address without fetching the entire list
1426  */
1427 static int
1428 verify_ifa(const char *name, struct mibifa *ifa)
1429 {
1430 	struct ifreq req;
1431 	struct sockaddr_in *sa;
1432 
1433 	memset(&req, 0, sizeof(req));
1434 	strncpy(req.ifr_name, name, sizeof(req.ifr_name));
1435 	sa = (struct sockaddr_in *)(void *)&req.ifr_addr;
1436 	sa->sin_family = AF_INET;
1437 	sa->sin_len = sizeof(*sa);
1438 	sa->sin_addr = ifa->inaddr;
1439 
1440 	if (ioctl(mib_netsock, SIOCGIFADDR, &req) == -1)
1441 		return (-1);
1442 	if (ifa->inaddr.s_addr != sa->sin_addr.s_addr) {
1443 		syslog(LOG_ERR, "%s: address mismatch", __func__);
1444 		return (-1);
1445 	}
1446 
1447 	if (ioctl(mib_netsock, SIOCGIFNETMASK, &req) == -1)
1448 		return (-1);
1449 	if (ifa->inmask.s_addr != sa->sin_addr.s_addr) {
1450 		syslog(LOG_ERR, "%s: netmask mismatch", __func__);
1451 		return (-1);
1452 	}
1453 	return (0);
1454 }
1455 
1456 /*
1457  * Restore a deleted interface address. Don't wait for the routing socket
1458  * to update us.
1459  */
1460 void
1461 mib_undestroy_ifa(struct mibifa *ifa)
1462 {
1463 	struct mibif *ifp;
1464 
1465 	if ((ifp = mib_find_if(ifa->ifindex)) == NULL)
1466 		/* keep it destroyed */
1467 		return;
1468 
1469 	if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast))
1470 		/* keep it destroyed */
1471 		return;
1472 
1473 	ifa->flags &= ~MIBIFA_DESTROYED;
1474 }
1475 
1476 /*
1477  * Destroy an interface address
1478  */
1479 int
1480 mib_destroy_ifa(struct mibifa *ifa)
1481 {
1482 	struct mibif *ifp;
1483 
1484 	if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1485 		/* ups. */
1486 		mib_iflist_bad = 1;
1487 		return (-1);
1488 	}
1489 	if (siocdifaddr(ifp->name, ifa->inaddr)) {
1490 		/* ups. */
1491 		syslog(LOG_ERR, "SIOCDIFADDR: %m");
1492 		mib_iflist_bad = 1;
1493 		return (-1);
1494 	}
1495 	ifa->flags |= MIBIFA_DESTROYED;
1496 	return (0);
1497 }
1498 
1499 /*
1500  * Rollback the modification of an address. Don't bother to wait for
1501  * the routing socket.
1502  */
1503 void
1504 mib_unmodify_ifa(struct mibifa *ifa)
1505 {
1506 	struct mibif *ifp;
1507 
1508 	if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1509 		/* ups. */
1510 		mib_iflist_bad = 1;
1511 		return;
1512 	}
1513 
1514 	if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast)) {
1515 		/* ups. */
1516 		mib_iflist_bad = 1;
1517 		return;
1518 	}
1519 }
1520 
1521 /*
1522  * Modify an IFA.
1523  */
1524 int
1525 mib_modify_ifa(struct mibifa *ifa)
1526 {
1527 	struct mibif *ifp;
1528 
1529 	if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1530 		/* ups. */
1531 		mib_iflist_bad = 1;
1532 		return (-1);
1533 	}
1534 
1535 	if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast)) {
1536 		/* ups. */
1537 		mib_iflist_bad = 1;
1538 		return (-1);
1539 	}
1540 
1541 	if (verify_ifa(ifp->name, ifa)) {
1542 		/* ups. */
1543 		mib_iflist_bad = 1;
1544 		return (-1);
1545 	}
1546 
1547 	return (0);
1548 }
1549 
1550 /*
1551  * Destroy a freshly created interface address. Don't bother to wait for
1552  * the routing socket.
1553  */
1554 void
1555 mib_uncreate_ifa(struct mibifa *ifa)
1556 {
1557 	struct mibif *ifp;
1558 
1559 	if ((ifp = mib_find_if(ifa->ifindex)) == NULL) {
1560 		/* ups. */
1561 		mib_iflist_bad = 1;
1562 		return;
1563 	}
1564 	if (siocdifaddr(ifp->name, ifa->inaddr)) {
1565 		/* ups. */
1566 		mib_iflist_bad = 1;
1567 		return;
1568 	}
1569 
1570 	destroy_ifa(ifa);
1571 }
1572 
1573 /*
1574  * Create a new ifa and verify it
1575  */
1576 struct mibifa *
1577 mib_create_ifa(u_int ifindex, struct in_addr addr, struct in_addr mask,
1578     struct in_addr bcast)
1579 {
1580 	struct mibif *ifp;
1581 	struct mibifa *ifa;
1582 
1583 	if ((ifp = mib_find_if(ifindex)) == NULL)
1584 		return (NULL);
1585 	if ((ifa = alloc_ifa(ifindex, addr)) == NULL)
1586 		return (NULL);
1587 	ifa->inmask = mask;
1588 	ifa->inbcast = bcast;
1589 
1590 	if (siocaifaddr(ifp->name, ifa->inaddr, ifa->inmask, ifa->inbcast)) {
1591 		syslog(LOG_ERR, "%s: %m", __func__);
1592 		destroy_ifa(ifa);
1593 		return (NULL);
1594 	}
1595 	if (verify_ifa(ifp->name, ifa)) {
1596 		destroy_ifa(ifa);
1597 		return (NULL);
1598 	}
1599 	return (ifa);
1600 }
1601 
1602 /*
1603  * Get all cloning interfaces and make them dynamic.
1604  * Hah! Whe should probably do this on a periodic basis (XXX).
1605  */
1606 static void
1607 get_cloners(void)
1608 {
1609 	struct if_clonereq req;
1610 	char *buf, *cp;
1611 	int i;
1612 
1613 	memset(&req, 0, sizeof(req));
1614 	if (ioctl(mib_netsock, SIOCIFGCLONERS, &req) == -1) {
1615 		syslog(LOG_ERR, "get cloners: %m");
1616 		return;
1617 	}
1618 	if ((buf = malloc(req.ifcr_total * IFNAMSIZ)) == NULL) {
1619 		syslog(LOG_ERR, "%m");
1620 		return;
1621 	}
1622 	req.ifcr_count = req.ifcr_total;
1623 	req.ifcr_buffer = buf;
1624 	if (ioctl(mib_netsock, SIOCIFGCLONERS, &req) == -1) {
1625 		syslog(LOG_ERR, "get cloners: %m");
1626 		free(buf);
1627 		return;
1628 	}
1629 	for (cp = buf, i = 0; i < req.ifcr_total; i++, cp += IFNAMSIZ)
1630 		mib_if_set_dyn(cp);
1631 	free(buf);
1632 }
1633 
1634 /*
1635  * Idle function
1636  */
1637 static void
1638 mibII_idle(void *arg __unused)
1639 {
1640 	struct mibifa *ifa;
1641 
1642 	if (mib_iflist_bad) {
1643 		TAILQ_FOREACH(ifa, &mibifa_list, link)
1644 			ifa->flags &= ~MIBIFA_DESTROYED;
1645 
1646 		/* assume, that all cloning interfaces are dynamic */
1647 		get_cloners();
1648 
1649 		mib_refresh_iflist();
1650 		update_ifa_info();
1651 		mib_arp_update();
1652 		mib_iflist_bad = 0;
1653 	}
1654 
1655 	mib_arp_update();
1656 }
1657 
1658 
1659 /*
1660  * Start the module
1661  */
1662 static void
1663 mibII_start(void)
1664 {
1665 	if ((route_fd = fd_select(route, route_input, NULL, module)) == NULL) {
1666 		syslog(LOG_ERR, "fd_select(route): %m");
1667 		return;
1668 	}
1669 	mib_refresh_iflist();
1670 	update_ifa_info();
1671 	mib_arp_update();
1672 	(void)mib_fetch_route();
1673 	mib_iftable_last_change = 0;
1674 	mib_ifstack_last_change = 0;
1675 
1676 	ifmib_reg = or_register(&oid_ifMIB,
1677 	    "The MIB module to describe generic objects for network interface"
1678 	    " sub-layers.", module);
1679 
1680 	ipmib_reg = or_register(&oid_ipMIB,
1681 	   "The MIB module for managing IP and ICMP implementations, but "
1682 	   "excluding their management of IP routes.", module);
1683 
1684 	tcpmib_reg = or_register(&oid_tcpMIB,
1685 	   "The MIB module for managing TCP implementations.", module);
1686 
1687 	udpmib_reg = or_register(&oid_udpMIB,
1688 	   "The MIB module for managing UDP implementations.", module);
1689 
1690 	ipForward_reg = or_register(&oid_ipForward,
1691 	   "The MIB module for the display of CIDR multipath IP Routes.",
1692 	   module);
1693 
1694 	mibII_poll_timer = NULL;
1695 	mibII_poll_ticks = MIBII_POLL_TICKS;
1696 	mibif_restart_mibII_poll_timer();
1697 }
1698 
1699 /*
1700  * Initialize the module
1701  */
1702 static int
1703 mibII_init(struct lmodule *mod, int argc __unused, char *argv[] __unused)
1704 {
1705 	size_t len;
1706 
1707 	module = mod;
1708 
1709 	len = sizeof(clockinfo);
1710 	if (sysctlbyname("kern.clockrate", &clockinfo, &len, NULL, 0) == -1) {
1711 		syslog(LOG_ERR, "kern.clockrate: %m");
1712 		return (-1);
1713 	}
1714 	if (len != sizeof(clockinfo)) {
1715 		syslog(LOG_ERR, "kern.clockrate: wrong size");
1716 		return (-1);
1717 	}
1718 
1719 	if ((route = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC)) == -1) {
1720 		syslog(LOG_ERR, "PF_ROUTE: %m");
1721 		return (-1);
1722 	}
1723 
1724 	if ((mib_netsock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
1725 		syslog(LOG_ERR, "PF_INET: %m");
1726 		(void)close(route);
1727 		return (-1);
1728 	}
1729 	(void)shutdown(mib_netsock, SHUT_RDWR);
1730 
1731 	/* assume, that all cloning interfaces are dynamic */
1732 	get_cloners();
1733 
1734 	return (0);
1735 }
1736 
1737 static int
1738 mibII_fini(void)
1739 {
1740 	if (mibII_poll_timer != NULL ) {
1741 		timer_stop(mibII_poll_timer);
1742 		mibII_poll_timer = NULL;
1743 	}
1744 
1745 	if (route_fd != NULL)
1746 		fd_deselect(route_fd);
1747 	if (route != -1)
1748 		(void)close(route);
1749 	if (mib_netsock != -1)
1750 		(void)close(mib_netsock);
1751 	/* XXX free memory */
1752 
1753 	or_unregister(ipForward_reg);
1754 	or_unregister(udpmib_reg);
1755 	or_unregister(tcpmib_reg);
1756 	or_unregister(ipmib_reg);
1757 	or_unregister(ifmib_reg);
1758 
1759 	return (0);
1760 }
1761 
1762 static void
1763 mibII_loading(const struct lmodule *mod, int loaded)
1764 {
1765 	struct mibif *ifp;
1766 
1767 	if (loaded == 1)
1768 		return;
1769 
1770 	TAILQ_FOREACH(ifp, &mibif_list, link)
1771 		if (ifp->xnotify_mod == mod) {
1772 			ifp->xnotify_mod = NULL;
1773 			ifp->xnotify_data = NULL;
1774 			ifp->xnotify = NULL;
1775 		}
1776 
1777 	mib_unregister_newif(mod);
1778 }
1779 
1780 const struct snmp_module config = {
1781 	"This module implements the interface and ip groups.",
1782 	mibII_init,
1783 	mibII_fini,
1784 	NULL,		/* idle */
1785 	NULL,		/* dump */
1786 	NULL,		/* config */
1787 	mibII_start,
1788 	NULL,
1789 	mibII_ctree,
1790 	mibII_CTREE_SIZE,
1791 	mibII_loading
1792 };
1793 
1794 /*
1795  * Should have a list of these attached to each interface.
1796  */
1797 void *
1798 mibif_notify(struct mibif *ifp, const struct lmodule *mod,
1799     mibif_notify_f func, void *data)
1800 {
1801 	ifp->xnotify = func;
1802 	ifp->xnotify_data = data;
1803 	ifp->xnotify_mod = mod;
1804 
1805 	return (ifp);
1806 }
1807 
1808 void
1809 mibif_unnotify(void *arg)
1810 {
1811 	struct mibif *ifp = arg;
1812 
1813 	ifp->xnotify = NULL;
1814 	ifp->xnotify_data = NULL;
1815 	ifp->xnotify_mod = NULL;
1816 }
1817