1 /* $OpenBSD: interface.c,v 1.15 2009/09/20 20:45:06 stsp Exp $ */ 2 3 /* 4 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> 5 * Copyright (c) 2004, 2005, 2007 Esben Norby <norby@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/types.h> 21 #include <sys/ioctl.h> 22 #include <sys/time.h> 23 #include <sys/socket.h> 24 #include <netinet/in.h> 25 #include <arpa/inet.h> 26 #include <net/if.h> 27 #include <net/if_types.h> 28 #include <ctype.h> 29 #include <err.h> 30 #include <stddef.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <unistd.h> 34 #include <string.h> 35 #include <event.h> 36 37 #include "ospf6d.h" 38 #include "ospf6.h" 39 #include "log.h" 40 #include "ospfe.h" 41 42 void if_hello_timer(int, short, void *); 43 void if_start_hello_timer(struct iface *); 44 void if_stop_hello_timer(struct iface *); 45 void if_stop_wait_timer(struct iface *); 46 void if_wait_timer(int, short, void *); 47 void if_start_wait_timer(struct iface *); 48 void if_stop_wait_timer(struct iface *); 49 struct nbr *if_elect(struct nbr *, struct nbr *); 50 51 struct { 52 int state; 53 enum iface_event event; 54 enum iface_action action; 55 int new_state; 56 } iface_fsm[] = { 57 /* current state event that happened action to take resulting state */ 58 {IF_STA_DOWN, IF_EVT_UP, IF_ACT_STRT, 0}, 59 {IF_STA_WAITING, IF_EVT_BACKUP_SEEN, IF_ACT_ELECT, 0}, 60 {IF_STA_WAITING, IF_EVT_WTIMER, IF_ACT_ELECT, 0}, 61 {IF_STA_ANY, IF_EVT_WTIMER, IF_ACT_NOTHING, 0}, 62 {IF_STA_WAITING, IF_EVT_NBR_CHNG, IF_ACT_NOTHING, 0}, 63 {IF_STA_MULTI, IF_EVT_NBR_CHNG, IF_ACT_ELECT, 0}, 64 {IF_STA_ANY, IF_EVT_NBR_CHNG, IF_ACT_NOTHING, 0}, 65 {IF_STA_ANY, IF_EVT_DOWN, IF_ACT_RST, IF_STA_DOWN}, 66 {IF_STA_ANY, IF_EVT_LOOP, IF_ACT_RST, IF_STA_LOOPBACK}, 67 {IF_STA_LOOPBACK, IF_EVT_UNLOOP, IF_ACT_NOTHING, IF_STA_DOWN}, 68 {-1, IF_EVT_NOTHING, IF_ACT_NOTHING, 0}, 69 }; 70 71 #if 0 72 /* TODO virtual links */ 73 static int vlink_cnt = 0; 74 #endif 75 76 TAILQ_HEAD(, iface) iflist; 77 78 const char * const if_event_names[] = { 79 "NOTHING", 80 "UP", 81 "WAITTIMER", 82 "BACKUPSEEN", 83 "NEIGHBORCHANGE", 84 "LOOP", 85 "UNLOOP", 86 "DOWN" 87 }; 88 89 const char * const if_action_names[] = { 90 "NOTHING", 91 "START", 92 "ELECT", 93 "RESET" 94 }; 95 96 int 97 if_fsm(struct iface *iface, enum iface_event event) 98 { 99 int old_state; 100 int new_state = 0; 101 int i, ret = 0; 102 103 old_state = iface->state; 104 105 for (i = 0; iface_fsm[i].state != -1; i++) 106 if ((iface_fsm[i].state & old_state) && 107 (iface_fsm[i].event == event)) { 108 new_state = iface_fsm[i].new_state; 109 break; 110 } 111 112 if (iface_fsm[i].state == -1) { 113 /* event outside of the defined fsm, ignore it. */ 114 log_debug("if_fsm: interface %s, " 115 "event %s not expected in state %s", iface->name, 116 if_event_names[event], if_state_name(old_state)); 117 return (0); 118 } 119 120 switch (iface_fsm[i].action) { 121 case IF_ACT_STRT: 122 ret = if_act_start(iface); 123 break; 124 case IF_ACT_ELECT: 125 ret = if_act_elect(iface); 126 break; 127 case IF_ACT_RST: 128 ret = if_act_reset(iface); 129 break; 130 case IF_ACT_NOTHING: 131 /* do nothing */ 132 break; 133 } 134 135 if (ret) { 136 log_debug("if_fsm: error changing state for interface %s, " 137 "event %s, state %s", iface->name, if_event_names[event], 138 if_state_name(old_state)); 139 return (-1); 140 } 141 142 if (new_state != 0) 143 iface->state = new_state; 144 145 if (iface->state != old_state) { 146 orig_rtr_lsa(iface); 147 orig_link_lsa(iface); 148 149 /* state change inform RDE */ 150 ospfe_imsg_compose_rde(IMSG_IFINFO, 151 iface->self->peerid, 0, iface, sizeof(struct iface)); 152 } 153 154 if (old_state & (IF_STA_MULTI | IF_STA_POINTTOPOINT) && 155 (iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0) 156 ospfe_demote_iface(iface, 0); 157 if ((old_state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0 && 158 iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) 159 ospfe_demote_iface(iface, 1); 160 161 log_debug("if_fsm: event %s resulted in action %s and changing " 162 "state for interface %s from %s to %s", 163 if_event_names[event], if_action_names[iface_fsm[i].action], 164 iface->name, if_state_name(old_state), if_state_name(iface->state)); 165 166 return (ret); 167 } 168 169 int 170 if_init(void) 171 { 172 TAILQ_INIT(&iflist); 173 174 return (fetchifs(0)); 175 } 176 177 /* XXX using a linked list should be OK for now */ 178 struct iface * 179 if_find(unsigned int ifindex) 180 { 181 struct iface *iface; 182 183 TAILQ_FOREACH(iface, &iflist, list) { 184 if (ifindex == iface->ifindex) 185 return (iface); 186 } 187 return (NULL); 188 } 189 190 struct iface * 191 if_findname(char *name) 192 { 193 struct iface *iface; 194 195 TAILQ_FOREACH(iface, &iflist, list) { 196 if (!strcmp(name, iface->name)) 197 return (iface); 198 } 199 return (NULL); 200 } 201 202 struct iface * 203 if_new(u_short ifindex, char *ifname) 204 { 205 struct iface *iface; 206 207 if ((iface = calloc(1, sizeof(*iface))) == NULL) 208 err(1, "if_new: calloc"); 209 210 iface->state = IF_STA_DOWN; 211 212 LIST_INIT(&iface->nbr_list); 213 TAILQ_INIT(&iface->ifa_list); 214 TAILQ_INIT(&iface->ls_ack_list); 215 RB_INIT(&iface->lsa_tree); 216 217 #if 0 218 /* TODO */ 219 if (virtual) { 220 iface->type = IF_TYPE_VIRTUALLINK; 221 snprintf(iface->name, sizeof(iface->name), "vlink%d", 222 vlink_cnt++); 223 iface->flags |= IFF_UP; 224 iface->mtu = IP_MSS; 225 return (iface); 226 } 227 #endif 228 strlcpy(iface->name, ifname, sizeof(iface->name)); 229 iface->ifindex = ifindex; 230 231 TAILQ_INSERT_TAIL(&iflist, iface, list); 232 233 return (iface); 234 } 235 236 void 237 if_update(struct iface *iface, int mtu, int flags, u_int8_t type, 238 u_int8_t state, u_int64_t rate) 239 { 240 iface->mtu = mtu; 241 iface->flags = flags; 242 iface->media_type = type; 243 iface->linkstate = state; 244 iface->baudrate = rate; 245 246 /* set type */ 247 if (flags & IFF_POINTOPOINT) 248 iface->type = IF_TYPE_POINTOPOINT; 249 if (flags & IFF_BROADCAST && flags & IFF_MULTICAST) 250 iface->type = IF_TYPE_BROADCAST; 251 if (flags & IFF_LOOPBACK) { 252 iface->type = IF_TYPE_POINTOPOINT; 253 iface->state = IF_STA_LOOPBACK; 254 } 255 } 256 257 void 258 if_del(struct iface *iface) 259 { 260 struct nbr *nbr = NULL; 261 262 log_debug("if_del: interface %s", iface->name); 263 264 /* revert the demotion when the interface is deleted */ 265 if ((iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0) 266 ospfe_demote_iface(iface, 1); 267 268 /* clear lists etc */ 269 while ((nbr = LIST_FIRST(&iface->nbr_list)) != NULL) 270 nbr_del(nbr); 271 272 if (evtimer_pending(&iface->hello_timer, NULL)) 273 evtimer_del(&iface->hello_timer); 274 if (evtimer_pending(&iface->wait_timer, NULL)) 275 evtimer_del(&iface->wait_timer); 276 if (evtimer_pending(&iface->lsack_tx_timer, NULL)) 277 evtimer_del(&iface->lsack_tx_timer); 278 279 ls_ack_list_clr(iface); 280 TAILQ_REMOVE(&iflist, iface, list); 281 free(iface); 282 } 283 284 void 285 if_start(struct ospfd_conf *xconf, struct iface *iface) 286 { 287 /* init the dummy local neighbor */ 288 iface->self = nbr_new(ospfe_router_id(), iface, iface->ifindex, 1, 289 NULL); 290 291 /* set event handlers for interface */ 292 evtimer_set(&iface->lsack_tx_timer, ls_ack_tx_timer, iface); 293 evtimer_set(&iface->hello_timer, if_hello_timer, iface); 294 evtimer_set(&iface->wait_timer, if_wait_timer, iface); 295 296 iface->fd = xconf->ospf_socket; 297 298 ospfe_demote_iface(iface, 0); 299 300 if (if_fsm(iface, IF_EVT_UP)) 301 log_debug("error starting interface %s", iface->name); 302 } 303 304 /* timers */ 305 /* ARGSUSED */ 306 void 307 if_hello_timer(int fd, short event, void *arg) 308 { 309 struct iface *iface = arg; 310 struct timeval tv; 311 312 send_hello(iface); 313 314 /* reschedule hello_timer */ 315 timerclear(&tv); 316 tv.tv_sec = iface->hello_interval; 317 if (evtimer_add(&iface->hello_timer, &tv) == -1) 318 fatal("if_hello_timer"); 319 } 320 321 void 322 if_start_hello_timer(struct iface *iface) 323 { 324 struct timeval tv; 325 326 timerclear(&tv); 327 if (evtimer_add(&iface->hello_timer, &tv) == -1) 328 fatal("if_start_hello_timer"); 329 } 330 331 void 332 if_stop_hello_timer(struct iface *iface) 333 { 334 if (evtimer_del(&iface->hello_timer) == -1) 335 fatal("if_stop_hello_timer"); 336 } 337 338 /* ARGSUSED */ 339 void 340 if_wait_timer(int fd, short event, void *arg) 341 { 342 struct iface *iface = arg; 343 344 if_fsm(iface, IF_EVT_WTIMER); 345 } 346 347 void 348 if_start_wait_timer(struct iface *iface) 349 { 350 struct timeval tv; 351 352 timerclear(&tv); 353 tv.tv_sec = iface->dead_interval; 354 if (evtimer_add(&iface->wait_timer, &tv) == -1) 355 fatal("if_start_wait_timer"); 356 } 357 358 void 359 if_stop_wait_timer(struct iface *iface) 360 { 361 if (evtimer_del(&iface->wait_timer) == -1) 362 fatal("if_stop_wait_timer"); 363 } 364 365 /* actions */ 366 int 367 if_act_start(struct iface *iface) 368 { 369 struct in6_addr addr; 370 struct timeval now; 371 372 if (!((iface->flags & IFF_UP) && 373 (LINK_STATE_IS_UP(iface->linkstate) || 374 (iface->linkstate == LINK_STATE_UNKNOWN && 375 iface->media_type != IFT_CARP)))) { 376 log_debug("if_act_start: interface %s link down", 377 iface->name); 378 return (0); 379 } 380 381 if (iface->media_type == IFT_CARP && 382 !(iface->cflags & F_IFACE_PASSIVE)) { 383 /* force passive mode on carp interfaces */ 384 log_warnx("if_act_start: forcing interface %s to passive", 385 iface->name); 386 iface->cflags |= F_IFACE_PASSIVE; 387 } 388 389 if (iface->cflags & F_IFACE_PASSIVE) { 390 /* for an update of stub network entries */ 391 orig_rtr_lsa(iface); 392 return (0); 393 } 394 395 gettimeofday(&now, NULL); 396 iface->uptime = now.tv_sec; 397 398 switch (iface->type) { 399 case IF_TYPE_POINTOPOINT: 400 inet_pton(AF_INET6, AllSPFRouters, &addr); 401 402 if (if_join_group(iface, &addr)) 403 return (-1); 404 iface->state = IF_STA_POINTTOPOINT; 405 break; 406 case IF_TYPE_VIRTUALLINK: 407 iface->state = IF_STA_POINTTOPOINT; 408 break; 409 case IF_TYPE_POINTOMULTIPOINT: 410 case IF_TYPE_NBMA: 411 log_debug("if_act_start: type %s not supported, interface %s", 412 if_type_name(iface->type), iface->name); 413 return (-1); 414 case IF_TYPE_BROADCAST: 415 inet_pton(AF_INET6, AllSPFRouters, &addr); 416 417 if (if_join_group(iface, &addr)) 418 return (-1); 419 if (iface->priority == 0) { 420 iface->state = IF_STA_DROTHER; 421 } else { 422 iface->state = IF_STA_WAITING; 423 if_start_wait_timer(iface); 424 } 425 break; 426 default: 427 fatalx("if_act_start: unknown interface type"); 428 } 429 430 /* hello timer needs to be started in any case */ 431 if_start_hello_timer(iface); 432 return (0); 433 } 434 435 struct nbr * 436 if_elect(struct nbr *a, struct nbr *b) 437 { 438 if (a->priority > b->priority) 439 return (a); 440 if (a->priority < b->priority) 441 return (b); 442 if (ntohl(a->id.s_addr) > ntohl(b->id.s_addr)) 443 return (a); 444 return (b); 445 } 446 447 int 448 if_act_elect(struct iface *iface) 449 { 450 struct in6_addr addr; 451 struct nbr *nbr, *bdr = NULL, *dr = NULL; 452 int round = 0; 453 int changed = 0; 454 int old_state; 455 char b1[16], b2[16], b3[16], b4[16]; 456 457 start: 458 /* elect backup designated router */ 459 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 460 if (nbr->priority == 0 || nbr == dr || /* not electable */ 461 nbr->state & NBR_STA_PRELIM || /* not available */ 462 nbr->dr.s_addr == nbr->id.s_addr) /* don't elect DR */ 463 continue; 464 if (bdr != NULL) { 465 /* 466 * routers announcing themselves as BDR have higher 467 * precedence over those routers announcing a 468 * different BDR. 469 */ 470 if (nbr->bdr.s_addr == nbr->id.s_addr) { 471 if (bdr->bdr.s_addr == bdr->id.s_addr) 472 bdr = if_elect(bdr, nbr); 473 else 474 bdr = nbr; 475 } else if (bdr->bdr.s_addr != bdr->id.s_addr) 476 bdr = if_elect(bdr, nbr); 477 } else 478 bdr = nbr; 479 } 480 481 /* elect designated router */ 482 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 483 if (nbr->priority == 0 || nbr->state & NBR_STA_PRELIM || 484 (nbr != dr && nbr->dr.s_addr != nbr->id.s_addr)) 485 /* only DR may be elected check priority too */ 486 continue; 487 if (dr == NULL) 488 dr = nbr; 489 else 490 dr = if_elect(dr, nbr); 491 } 492 493 if (dr == NULL) { 494 /* no designate router found use backup DR */ 495 dr = bdr; 496 bdr = NULL; 497 } 498 499 /* 500 * if we are involved in the election (e.g. new DR or no 501 * longer BDR) redo the election 502 */ 503 if (round == 0 && 504 ((iface->self == dr && iface->self != iface->dr) || 505 (iface->self != dr && iface->self == iface->dr) || 506 (iface->self == bdr && iface->self != iface->bdr) || 507 (iface->self != bdr && iface->self == iface->bdr))) { 508 /* 509 * Reset announced DR/BDR to calculated one, so 510 * that we may get elected in the second round. 511 * This is needed to drop from a DR to a BDR. 512 */ 513 iface->self->dr.s_addr = dr->id.s_addr; 514 if (bdr) 515 iface->self->bdr.s_addr = bdr->id.s_addr; 516 round = 1; 517 goto start; 518 } 519 520 log_debug("if_act_elect: interface %s old dr %s new dr %s, " 521 "old bdr %s new bdr %s", iface->name, 522 iface->dr ? inet_ntop(AF_INET, &iface->dr->id, b1, sizeof(b1)) : 523 "none", dr ? inet_ntop(AF_INET, &dr->id, b2, sizeof(b2)) : "none", 524 iface->bdr ? inet_ntop(AF_INET, &iface->bdr->id, b3, sizeof(b3)) : 525 "none", bdr ? inet_ntop(AF_INET, &bdr->id, b4, sizeof(b4)) : 526 "none"); 527 528 /* 529 * After the second round still DR or BDR change state to DR or BDR, 530 * etc. 531 */ 532 old_state = iface->state; 533 if (dr == iface->self) 534 iface->state = IF_STA_DR; 535 else if (bdr == iface->self) 536 iface->state = IF_STA_BACKUP; 537 else 538 iface->state = IF_STA_DROTHER; 539 540 /* TODO if iface is NBMA send all non eligible neighbors event Start */ 541 542 /* 543 * if DR or BDR changed issue a AdjOK? event for all neighbors > 2-Way 544 */ 545 if (iface->dr != dr || iface->bdr != bdr) 546 changed = 1; 547 548 iface->dr = dr; 549 iface->bdr = bdr; 550 551 if (changed) { 552 inet_pton(AF_INET6, AllDRouters, &addr); 553 if (old_state & IF_STA_DRORBDR && 554 (iface->state & IF_STA_DRORBDR) == 0) { 555 if (if_leave_group(iface, &addr)) 556 return (-1); 557 } else if ((old_state & IF_STA_DRORBDR) == 0 && 558 iface->state & IF_STA_DRORBDR) { 559 if (if_join_group(iface, &addr)) 560 return (-1); 561 } 562 563 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 564 if (nbr->state & NBR_STA_BIDIR) 565 nbr_fsm(nbr, NBR_EVT_ADJ_OK); 566 } 567 568 orig_rtr_lsa(iface); 569 if (iface->state & IF_STA_DR || old_state & IF_STA_DR) 570 orig_net_lsa(iface); 571 } 572 573 if_start_hello_timer(iface); 574 return (0); 575 } 576 577 int 578 if_act_reset(struct iface *iface) 579 { 580 struct nbr *nbr = NULL; 581 struct in6_addr addr; 582 583 if (iface->cflags & F_IFACE_PASSIVE) { 584 /* for an update of stub network entries */ 585 orig_rtr_lsa(iface); 586 return (0); 587 } 588 589 switch (iface->type) { 590 case IF_TYPE_POINTOPOINT: 591 case IF_TYPE_BROADCAST: 592 inet_pton(AF_INET6, AllSPFRouters, &addr); 593 if (if_leave_group(iface, &addr)) { 594 log_warnx("if_act_reset: error leaving group %s, " 595 "interface %s", log_in6addr(&addr), iface->name); 596 } 597 if (iface->state & IF_STA_DRORBDR) { 598 inet_pton(AF_INET6, AllDRouters, &addr); 599 if (if_leave_group(iface, &addr)) { 600 log_warnx("if_act_reset: " 601 "error leaving group %s, interface %s", 602 log_in6addr(&addr), iface->name); 603 } 604 } 605 break; 606 case IF_TYPE_VIRTUALLINK: 607 /* nothing */ 608 break; 609 case IF_TYPE_NBMA: 610 case IF_TYPE_POINTOMULTIPOINT: 611 log_debug("if_act_reset: type %s not supported, interface %s", 612 if_type_name(iface->type), iface->name); 613 return (-1); 614 default: 615 fatalx("if_act_reset: unknown interface type"); 616 } 617 618 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 619 if (nbr_fsm(nbr, NBR_EVT_KILL_NBR)) { 620 log_debug("if_act_reset: error killing neighbor %s", 621 inet_ntoa(nbr->id)); 622 } 623 } 624 625 iface->dr = NULL; 626 iface->bdr = NULL; 627 628 ls_ack_list_clr(iface); 629 stop_ls_ack_tx_timer(iface); 630 if_stop_hello_timer(iface); 631 if_stop_wait_timer(iface); 632 633 /* send empty hello to tell everybody that we are going down */ 634 send_hello(iface); 635 636 return (0); 637 } 638 639 struct ctl_iface * 640 if_to_ctl(struct iface *iface) 641 { 642 static struct ctl_iface ictl; 643 struct timeval tv, now, res; 644 struct nbr *nbr; 645 646 memcpy(ictl.name, iface->name, sizeof(ictl.name)); 647 memcpy(&ictl.addr, &iface->addr, sizeof(ictl.addr)); 648 ictl.rtr_id.s_addr = ospfe_router_id(); 649 memcpy(&ictl.area, &iface->area_id, sizeof(ictl.area)); 650 if (iface->dr) { 651 memcpy(&ictl.dr_id, &iface->dr->id, sizeof(ictl.dr_id)); 652 memcpy(&ictl.dr_addr, &iface->dr->addr, sizeof(ictl.dr_addr)); 653 } else { 654 bzero(&ictl.dr_id, sizeof(ictl.dr_id)); 655 bzero(&ictl.dr_addr, sizeof(ictl.dr_addr)); 656 } 657 if (iface->bdr) { 658 memcpy(&ictl.bdr_id, &iface->bdr->id, sizeof(ictl.bdr_id)); 659 memcpy(&ictl.bdr_addr, &iface->bdr->addr, 660 sizeof(ictl.bdr_addr)); 661 } else { 662 bzero(&ictl.bdr_id, sizeof(ictl.bdr_id)); 663 bzero(&ictl.bdr_addr, sizeof(ictl.bdr_addr)); 664 } 665 ictl.ifindex = iface->ifindex; 666 ictl.state = iface->state; 667 ictl.mtu = iface->mtu; 668 ictl.nbr_cnt = 0; 669 ictl.adj_cnt = 0; 670 ictl.baudrate = iface->baudrate; 671 ictl.dead_interval = iface->dead_interval; 672 ictl.transmit_delay = iface->transmit_delay; 673 ictl.hello_interval = iface->hello_interval; 674 ictl.flags = iface->flags; 675 ictl.metric = iface->metric; 676 ictl.rxmt_interval = iface->rxmt_interval; 677 ictl.type = iface->type; 678 ictl.linkstate = iface->linkstate; 679 ictl.mediatype = iface->media_type; 680 ictl.priority = iface->priority; 681 ictl.passive = (iface->cflags & F_IFACE_PASSIVE) == F_IFACE_PASSIVE; 682 683 gettimeofday(&now, NULL); 684 if (evtimer_pending(&iface->hello_timer, &tv)) { 685 timersub(&tv, &now, &res); 686 ictl.hello_timer = res.tv_sec; 687 } else 688 ictl.hello_timer = -1; 689 690 if (iface->state != IF_STA_DOWN) { 691 ictl.uptime = now.tv_sec - iface->uptime; 692 } else 693 ictl.uptime = 0; 694 695 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 696 if (nbr == iface->self) 697 continue; 698 ictl.nbr_cnt++; 699 if (nbr->state & NBR_STA_ADJFORM) 700 ictl.adj_cnt++; 701 } 702 703 return (&ictl); 704 } 705 706 /* misc */ 707 void 708 if_set_recvbuf(int fd) 709 { 710 int bsize; 711 712 bsize = 65535; 713 while (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bsize, 714 sizeof(bsize)) == -1) 715 bsize /= 2; 716 } 717 718 int 719 if_join_group(struct iface *iface, struct in6_addr *addr) 720 { 721 struct ipv6_mreq mreq; 722 723 switch (iface->type) { 724 case IF_TYPE_POINTOPOINT: 725 case IF_TYPE_BROADCAST: 726 log_debug("if_join_group: interface %s addr %s", 727 iface->name, log_in6addr(addr)); 728 mreq.ipv6mr_multiaddr = *addr; 729 mreq.ipv6mr_interface = iface->ifindex; 730 731 if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, 732 &mreq, sizeof(mreq)) < 0) { 733 log_warn("if_join_group: error IPV6_JOIN_GROUP, " 734 "interface %s address %s", iface->name, 735 log_in6addr(addr)); 736 return (-1); 737 } 738 break; 739 case IF_TYPE_POINTOMULTIPOINT: 740 case IF_TYPE_VIRTUALLINK: 741 case IF_TYPE_NBMA: 742 log_debug("if_join_group: type %s not supported, interface %s", 743 if_type_name(iface->type), iface->name); 744 return (-1); 745 default: 746 fatalx("if_join_group: unknown interface type"); 747 } 748 749 return (0); 750 } 751 752 int 753 if_leave_group(struct iface *iface, struct in6_addr *addr) 754 { 755 struct ipv6_mreq mreq; 756 757 switch (iface->type) { 758 case IF_TYPE_POINTOPOINT: 759 case IF_TYPE_BROADCAST: 760 log_debug("if_leave_group: interface %s addr %s", 761 iface->name, log_in6addr(addr)); 762 mreq.ipv6mr_multiaddr = *addr; 763 mreq.ipv6mr_interface = iface->ifindex; 764 765 if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, 766 (void *)&mreq, sizeof(mreq)) < 0) { 767 log_warn("if_leave_group: error IPV6_LEAVE_GROUP, " 768 "interface %s address %s", iface->name, 769 log_in6addr(addr)); 770 return (-1); 771 } 772 break; 773 case IF_TYPE_POINTOMULTIPOINT: 774 case IF_TYPE_VIRTUALLINK: 775 case IF_TYPE_NBMA: 776 log_debug("if_leave_group: type %s not supported, interface %s", 777 if_type_name(iface->type), iface->name); 778 return (-1); 779 default: 780 fatalx("if_leave_group: unknown interface type"); 781 } 782 return (0); 783 } 784 785 int 786 if_set_mcast(struct iface *iface) 787 { 788 switch (iface->type) { 789 case IF_TYPE_POINTOPOINT: 790 case IF_TYPE_BROADCAST: 791 if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, 792 &iface->ifindex, sizeof(iface->ifindex)) < 0) { 793 log_debug("if_set_mcast: error setting " 794 "IP_MULTICAST_IF, interface %s", iface->name); 795 return (-1); 796 } 797 break; 798 case IF_TYPE_POINTOMULTIPOINT: 799 case IF_TYPE_VIRTUALLINK: 800 case IF_TYPE_NBMA: 801 log_debug("if_set_mcast: type %s not supported, interface %s", 802 if_type_name(iface->type), iface->name); 803 return (-1); 804 default: 805 fatalx("if_set_mcast: unknown interface type"); 806 } 807 808 return (0); 809 } 810 811 int 812 if_set_mcast_loop(int fd) 813 { 814 u_int loop = 0; 815 816 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, 817 (u_int *)&loop, sizeof(loop)) < 0) { 818 log_warn("if_set_mcast_loop: error setting " 819 "IPV6_MULTICAST_LOOP"); 820 return (-1); 821 } 822 823 return (0); 824 } 825 826 int 827 if_set_ipv6_pktinfo(int fd, int enable) 828 { 829 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &enable, 830 sizeof(enable)) < 0) { 831 log_warn("if_set_ipv6_pktinfo: error setting IPV6_PKTINFO"); 832 return (-1); 833 } 834 835 return (0); 836 } 837 838 int 839 if_set_ipv6_checksum(int fd) 840 { 841 int offset = offsetof(struct ospf_hdr, chksum); 842 843 log_debug("if_set_ipv6_checksum setting cksum offset to %i", offset); 844 if (setsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM, &offset, 845 sizeof(offset)) < 0) { 846 log_warn("if_set_ipv6_checksum: error setting IPV6_CHECKSUM"); 847 return (-1); 848 } 849 return (0); 850 } 851