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