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