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