1 /* $OpenBSD: interface.c,v 1.7 2007/11/27 11:29:34 claudio 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 static int vlink_cnt = 0; 72 73 const char * const if_event_names[] = { 74 "NOTHING", 75 "UP", 76 "WAITTIMER", 77 "BACKUPSEEN", 78 "NEIGHBORCHANGE", 79 "LOOP", 80 "UNLOOP", 81 "DOWN" 82 }; 83 84 const char * const if_action_names[] = { 85 "NOTHING", 86 "START", 87 "ELECT", 88 "RESET" 89 }; 90 91 int 92 if_fsm(struct iface *iface, enum iface_event event) 93 { 94 int old_state; 95 int new_state = 0; 96 int i, ret = 0; 97 98 old_state = iface->state; 99 100 for (i = 0; iface_fsm[i].state != -1; i++) 101 if ((iface_fsm[i].state & old_state) && 102 (iface_fsm[i].event == event)) { 103 new_state = iface_fsm[i].new_state; 104 break; 105 } 106 107 if (iface_fsm[i].state == -1) { 108 /* event outside of the defined fsm, ignore it. */ 109 log_debug("if_fsm: interface %s, " 110 "event %s not expected in state %s", iface->name, 111 if_event_names[event], if_state_name(old_state)); 112 return (0); 113 } 114 115 switch (iface_fsm[i].action) { 116 case IF_ACT_STRT: 117 ret = if_act_start(iface); 118 break; 119 case IF_ACT_ELECT: 120 ret = if_act_elect(iface); 121 break; 122 case IF_ACT_RST: 123 ret = if_act_reset(iface); 124 break; 125 case IF_ACT_NOTHING: 126 /* do nothing */ 127 break; 128 } 129 130 if (ret) { 131 log_debug("if_fsm: error changing state for interface %s, " 132 "event %s, state %s", iface->name, if_event_names[event], 133 if_state_name(old_state)); 134 return (-1); 135 } 136 137 if (new_state != 0) 138 iface->state = new_state; 139 140 if (iface->state != old_state) 141 orig_rtr_lsa(iface->area); 142 143 if (old_state & (IF_STA_MULTI | IF_STA_POINTTOPOINT) && 144 (iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0) 145 ospfe_demote_iface(iface, 0); 146 if ((old_state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0 && 147 iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) 148 ospfe_demote_iface(iface, 1); 149 150 log_debug("if_fsm: event %s resulted in action %s and changing " 151 "state for interface %s from %s to %s", 152 if_event_names[event], if_action_names[iface_fsm[i].action], 153 iface->name, if_state_name(old_state), if_state_name(iface->state)); 154 155 return (ret); 156 } 157 158 struct iface * 159 if_new(struct kif *kif, struct kif_addr *ka) 160 { 161 struct iface *iface; 162 163 if ((iface = calloc(1, sizeof(*iface))) == NULL) 164 err(1, "if_new: calloc"); 165 166 iface->state = IF_STA_DOWN; 167 168 LIST_INIT(&iface->nbr_list); 169 TAILQ_INIT(&iface->ls_ack_list); 170 RB_INIT(&iface->lsa_tree); 171 172 if (kif == NULL) { 173 iface->type = IF_TYPE_VIRTUALLINK; 174 snprintf(iface->name, sizeof(iface->name), "vlink%d", 175 vlink_cnt++); 176 iface->flags |= IFF_UP; 177 iface->mtu = IP_MSS; 178 return (iface); 179 } 180 181 strlcpy(iface->name, kif->ifname, sizeof(iface->name)); 182 183 /* get type */ 184 if (kif->flags & IFF_POINTOPOINT) 185 iface->type = IF_TYPE_POINTOPOINT; 186 if (kif->flags & IFF_BROADCAST && 187 kif->flags & IFF_MULTICAST) 188 iface->type = IF_TYPE_BROADCAST; 189 if (kif->flags & IFF_LOOPBACK) { 190 iface->type = IF_TYPE_POINTOPOINT; 191 iface->state = IF_STA_LOOPBACK; 192 } 193 194 /* get mtu, index and flags */ 195 iface->mtu = kif->mtu; 196 iface->ifindex = kif->ifindex; 197 iface->flags = kif->flags; 198 iface->linkstate = kif->link_state; 199 iface->media_type = kif->media_type; 200 iface->baudrate = kif->baudrate; 201 202 /* set address, mask and p2p addr */ 203 iface->addr = ka->addr; 204 if (kif->flags & IFF_POINTOPOINT) { 205 iface->dst = ka->dstbrd; 206 } 207 208 return (iface); 209 } 210 211 void 212 if_del(struct iface *iface) 213 { 214 struct nbr *nbr = NULL; 215 216 log_debug("if_del: interface %s", iface->name); 217 218 /* revert the demotion when the interface is deleted */ 219 if ((iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0) 220 ospfe_demote_iface(iface, 1); 221 222 /* clear lists etc */ 223 while ((nbr = LIST_FIRST(&iface->nbr_list)) != NULL) 224 nbr_del(nbr); 225 226 if (evtimer_pending(&iface->hello_timer, NULL)) 227 evtimer_del(&iface->hello_timer); 228 if (evtimer_pending(&iface->wait_timer, NULL)) 229 evtimer_del(&iface->wait_timer); 230 if (evtimer_pending(&iface->lsack_tx_timer, NULL)) 231 evtimer_del(&iface->lsack_tx_timer); 232 233 ls_ack_list_clr(iface); 234 free(iface); 235 } 236 237 void 238 if_init(struct ospfd_conf *xconf, struct iface *iface) 239 { 240 /* init the dummy local neighbor */ 241 iface->self = nbr_new(ospfe_router_id(), iface, 1); 242 243 /* set event handlers for interface */ 244 evtimer_set(&iface->lsack_tx_timer, ls_ack_tx_timer, iface); 245 evtimer_set(&iface->hello_timer, if_hello_timer, iface); 246 evtimer_set(&iface->wait_timer, if_wait_timer, iface); 247 248 iface->fd = xconf->ospf_socket; 249 250 ospfe_demote_iface(iface, 0); 251 } 252 253 /* timers */ 254 /* ARGSUSED */ 255 void 256 if_hello_timer(int fd, short event, void *arg) 257 { 258 struct iface *iface = arg; 259 struct timeval tv; 260 261 send_hello(iface); 262 263 /* reschedule hello_timer */ 264 timerclear(&tv); 265 tv.tv_sec = iface->hello_interval; 266 if (evtimer_add(&iface->hello_timer, &tv) == -1) 267 fatal("if_hello_timer"); 268 } 269 270 void 271 if_start_hello_timer(struct iface *iface) 272 { 273 struct timeval tv; 274 275 timerclear(&tv); 276 if (evtimer_add(&iface->hello_timer, &tv) == -1) 277 fatal("if_start_hello_timer"); 278 } 279 280 void 281 if_stop_hello_timer(struct iface *iface) 282 { 283 if (evtimer_del(&iface->hello_timer) == -1) 284 fatal("if_stop_hello_timer"); 285 } 286 287 /* ARGSUSED */ 288 void 289 if_wait_timer(int fd, short event, void *arg) 290 { 291 struct iface *iface = arg; 292 293 if_fsm(iface, IF_EVT_WTIMER); 294 } 295 296 void 297 if_start_wait_timer(struct iface *iface) 298 { 299 struct timeval tv; 300 301 timerclear(&tv); 302 tv.tv_sec = iface->dead_interval; 303 if (evtimer_add(&iface->wait_timer, &tv) == -1) 304 fatal("if_start_wait_timer"); 305 } 306 307 void 308 if_stop_wait_timer(struct iface *iface) 309 { 310 if (evtimer_del(&iface->wait_timer) == -1) 311 fatal("if_stop_wait_timer"); 312 } 313 314 /* actions */ 315 int 316 if_act_start(struct iface *iface) 317 { 318 struct in6_addr addr; 319 struct timeval now; 320 321 if (!((iface->flags & IFF_UP) && 322 (LINK_STATE_IS_UP(iface->linkstate) || 323 (iface->linkstate == LINK_STATE_UNKNOWN && 324 iface->media_type != IFT_CARP)))) { 325 log_debug("if_act_start: interface %s link down", 326 iface->name); 327 return (0); 328 } 329 330 if (iface->media_type == IFT_CARP && iface->passive == 0) { 331 /* force passive mode on carp interfaces */ 332 log_warnx("if_act_start: forcing interface %s to passive", 333 iface->name); 334 iface->passive = 1; 335 } 336 337 if (iface->passive) { 338 /* for an update of stub network entries */ 339 orig_rtr_lsa(iface->area); 340 return (0); 341 } 342 343 gettimeofday(&now, NULL); 344 iface->uptime = now.tv_sec; 345 346 switch (iface->type) { 347 case IF_TYPE_POINTOPOINT: 348 inet_pton(AF_INET6, AllSPFRouters, &addr); 349 350 if (if_join_group(iface, &addr)) 351 return (-1); 352 iface->state = IF_STA_POINTTOPOINT; 353 break; 354 case IF_TYPE_VIRTUALLINK: 355 iface->state = IF_STA_POINTTOPOINT; 356 break; 357 case IF_TYPE_POINTOMULTIPOINT: 358 case IF_TYPE_NBMA: 359 log_debug("if_act_start: type %s not supported, interface %s", 360 if_type_name(iface->type), iface->name); 361 return (-1); 362 case IF_TYPE_BROADCAST: 363 inet_pton(AF_INET6, AllSPFRouters, &addr); 364 365 if (if_join_group(iface, &addr)) 366 return (-1); 367 if (iface->priority == 0) { 368 iface->state = IF_STA_DROTHER; 369 } else { 370 iface->state = IF_STA_WAITING; 371 if_start_wait_timer(iface); 372 } 373 break; 374 default: 375 fatalx("if_act_start: unknown interface type"); 376 } 377 378 /* hello timer needs to be started in any case */ 379 if_start_hello_timer(iface); 380 return (0); 381 } 382 383 struct nbr * 384 if_elect(struct nbr *a, struct nbr *b) 385 { 386 if (a->priority > b->priority) 387 return (a); 388 if (a->priority < b->priority) 389 return (b); 390 if (ntohl(a->id.s_addr) > ntohl(b->id.s_addr)) 391 return (a); 392 return (b); 393 } 394 395 int 396 if_act_elect(struct iface *iface) 397 { 398 struct in6_addr addr; 399 struct nbr *nbr, *bdr = NULL, *dr = NULL; 400 int round = 0; 401 int changed = 0; 402 int old_state; 403 char b1[16], b2[16], b3[16], b4[16]; 404 405 start: 406 /* elect backup designated router */ 407 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 408 if (nbr->priority == 0 || nbr == dr || /* not electable */ 409 nbr->state & NBR_STA_PRELIM || /* not available */ 410 nbr->dr.s_addr == nbr->id.s_addr) /* don't elect DR */ 411 continue; 412 if (bdr != NULL) { 413 /* 414 * routers announcing themselves as BDR have higher 415 * precedence over those routers announcing a 416 * different BDR. 417 */ 418 if (nbr->bdr.s_addr == nbr->id.s_addr) { 419 if (bdr->bdr.s_addr == bdr->id.s_addr) 420 bdr = if_elect(bdr, nbr); 421 else 422 bdr = nbr; 423 } else if (bdr->bdr.s_addr != bdr->id.s_addr) 424 bdr = if_elect(bdr, nbr); 425 } else 426 bdr = nbr; 427 } 428 429 /* elect designated router */ 430 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 431 if (nbr->priority == 0 || nbr->state & NBR_STA_PRELIM || 432 (nbr != dr && nbr->dr.s_addr != nbr->id.s_addr)) 433 /* only DR may be elected check priority too */ 434 continue; 435 if (dr == NULL) 436 dr = nbr; 437 else 438 dr = if_elect(dr, nbr); 439 } 440 441 if (dr == NULL) { 442 /* no designate router found use backup DR */ 443 dr = bdr; 444 bdr = NULL; 445 } 446 447 /* 448 * if we are involved in the election (e.g. new DR or no 449 * longer BDR) redo the election 450 */ 451 if (round == 0 && 452 ((iface->self == dr && iface->self != iface->dr) || 453 (iface->self != dr && iface->self == iface->dr) || 454 (iface->self == bdr && iface->self != iface->bdr) || 455 (iface->self != bdr && iface->self == iface->bdr))) { 456 /* 457 * Reset announced DR/BDR to calculated one, so 458 * that we may get elected in the second round. 459 * This is needed to drop from a DR to a BDR. 460 */ 461 iface->self->dr.s_addr = dr->id.s_addr; 462 if (bdr) 463 iface->self->bdr.s_addr = bdr->id.s_addr; 464 round = 1; 465 goto start; 466 } 467 468 log_debug("if_act_elect: interface %s old dr %s new dr %s, " 469 "old bdr %s new bdr %s", iface->name, 470 iface->dr ? inet_ntop(AF_INET, &iface->dr->id, b1, sizeof(b1)) : 471 "none", dr ? inet_ntop(AF_INET, &dr->id, b2, sizeof(b2)) : "none", 472 iface->bdr ? inet_ntop(AF_INET, &iface->bdr->id, b3, sizeof(b3)) : 473 "none", bdr ? inet_ntop(AF_INET, &bdr->id, b4, sizeof(b4)) : 474 "none"); 475 476 /* 477 * After the second round still DR or BDR change state to DR or BDR, 478 * etc. 479 */ 480 old_state = iface->state; 481 if (dr == iface->self) 482 iface->state = IF_STA_DR; 483 else if (bdr == iface->self) 484 iface->state = IF_STA_BACKUP; 485 else 486 iface->state = IF_STA_DROTHER; 487 488 /* TODO if iface is NBMA send all non eligible neighbors event Start */ 489 490 /* 491 * if DR or BDR changed issue a AdjOK? event for all neighbors > 2-Way 492 */ 493 if (iface->dr != dr || iface->bdr != bdr) 494 changed = 1; 495 496 iface->dr = dr; 497 iface->bdr = bdr; 498 499 if (changed) { 500 inet_pton(AF_INET6, AllDRouters, &addr); 501 if (old_state & IF_STA_DRORBDR && 502 (iface->state & IF_STA_DRORBDR) == 0) { 503 if (if_leave_group(iface, &addr)) 504 return (-1); 505 } else if ((old_state & IF_STA_DRORBDR) == 0 && 506 iface->state & IF_STA_DRORBDR) { 507 if (if_join_group(iface, &addr)) 508 return (-1); 509 } 510 511 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 512 if (nbr->state & NBR_STA_BIDIR) 513 nbr_fsm(nbr, NBR_EVT_ADJ_OK); 514 } 515 516 orig_rtr_lsa(iface->area); 517 if (iface->state & IF_STA_DR || old_state & IF_STA_DR) 518 orig_net_lsa(iface); 519 } 520 521 if_start_hello_timer(iface); 522 return (0); 523 } 524 525 int 526 if_act_reset(struct iface *iface) 527 { 528 struct nbr *nbr = NULL; 529 struct in6_addr addr; 530 531 if (iface->passive) { 532 /* for an update of stub network entries */ 533 orig_rtr_lsa(iface->area); 534 return (0); 535 } 536 537 switch (iface->type) { 538 case IF_TYPE_POINTOPOINT: 539 case IF_TYPE_BROADCAST: 540 inet_pton(AF_INET6, AllSPFRouters, &addr); 541 if (if_leave_group(iface, &addr)) { 542 log_warnx("if_act_reset: error leaving group %s, " 543 "interface %s", log_in6addr(&addr), iface->name); 544 } 545 if (iface->state & IF_STA_DRORBDR) { 546 inet_pton(AF_INET6, AllDRouters, &addr); 547 if (if_leave_group(iface, &addr)) { 548 log_warnx("if_act_reset: " 549 "error leaving group %s, interface %s", 550 log_in6addr(&addr), iface->name); 551 } 552 } 553 break; 554 case IF_TYPE_VIRTUALLINK: 555 /* nothing */ 556 break; 557 case IF_TYPE_NBMA: 558 case IF_TYPE_POINTOMULTIPOINT: 559 log_debug("if_act_reset: type %s not supported, interface %s", 560 if_type_name(iface->type), iface->name); 561 return (-1); 562 default: 563 fatalx("if_act_reset: unknown interface type"); 564 } 565 566 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 567 if (nbr_fsm(nbr, NBR_EVT_KILL_NBR)) { 568 log_debug("if_act_reset: error killing neighbor %s", 569 inet_ntoa(nbr->id)); 570 } 571 } 572 573 iface->dr = NULL; 574 iface->bdr = NULL; 575 576 ls_ack_list_clr(iface); 577 stop_ls_ack_tx_timer(iface); 578 if_stop_hello_timer(iface); 579 if_stop_wait_timer(iface); 580 581 /* send empty hello to tell everybody that we are going down */ 582 send_hello(iface); 583 584 return (0); 585 } 586 587 struct ctl_iface * 588 if_to_ctl(struct iface *iface) 589 { 590 static struct ctl_iface ictl; 591 struct timeval tv, now, res; 592 struct nbr *nbr; 593 594 memcpy(ictl.name, iface->name, sizeof(ictl.name)); 595 memcpy(&ictl.addr, &iface->addr, sizeof(ictl.addr)); 596 ictl.rtr_id.s_addr = ospfe_router_id(); 597 memcpy(&ictl.area, &iface->area->id, sizeof(ictl.area)); 598 if (iface->dr) { 599 memcpy(&ictl.dr_id, &iface->dr->id, sizeof(ictl.dr_id)); 600 memcpy(&ictl.dr_addr, &iface->dr->addr, sizeof(ictl.dr_addr)); 601 } else { 602 bzero(&ictl.dr_id, sizeof(ictl.dr_id)); 603 bzero(&ictl.dr_addr, sizeof(ictl.dr_addr)); 604 } 605 if (iface->bdr) { 606 memcpy(&ictl.bdr_id, &iface->bdr->id, sizeof(ictl.bdr_id)); 607 memcpy(&ictl.bdr_addr, &iface->bdr->addr, 608 sizeof(ictl.bdr_addr)); 609 } else { 610 bzero(&ictl.bdr_id, sizeof(ictl.bdr_id)); 611 bzero(&ictl.bdr_addr, sizeof(ictl.bdr_addr)); 612 } 613 ictl.ifindex = iface->ifindex; 614 ictl.state = iface->state; 615 ictl.mtu = iface->mtu; 616 ictl.nbr_cnt = 0; 617 ictl.adj_cnt = 0; 618 ictl.baudrate = iface->baudrate; 619 ictl.dead_interval = iface->dead_interval; 620 ictl.transmit_delay = iface->transmit_delay; 621 ictl.hello_interval = iface->hello_interval; 622 ictl.flags = iface->flags; 623 ictl.metric = iface->metric; 624 ictl.rxmt_interval = iface->rxmt_interval; 625 ictl.type = iface->type; 626 ictl.linkstate = iface->linkstate; 627 ictl.mediatype = iface->media_type; 628 ictl.priority = iface->priority; 629 ictl.passive = iface->passive; 630 631 gettimeofday(&now, NULL); 632 if (evtimer_pending(&iface->hello_timer, &tv)) { 633 timersub(&tv, &now, &res); 634 ictl.hello_timer = res.tv_sec; 635 } else 636 ictl.hello_timer = -1; 637 638 if (iface->state != IF_STA_DOWN) { 639 ictl.uptime = now.tv_sec - iface->uptime; 640 } else 641 ictl.uptime = 0; 642 643 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 644 if (nbr == iface->self) 645 continue; 646 ictl.nbr_cnt++; 647 if (nbr->state & NBR_STA_ADJFORM) 648 ictl.adj_cnt++; 649 } 650 651 return (&ictl); 652 } 653 654 /* misc */ 655 void 656 if_set_recvbuf(int fd) 657 { 658 int bsize; 659 660 bsize = 65535; 661 while (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bsize, 662 sizeof(bsize)) == -1) 663 bsize /= 2; 664 } 665 666 int 667 if_join_group(struct iface *iface, struct in6_addr *addr) 668 { 669 struct ipv6_mreq mreq; 670 671 switch (iface->type) { 672 case IF_TYPE_POINTOPOINT: 673 case IF_TYPE_BROADCAST: 674 log_debug("if_join_group: interface %s addr %s", 675 iface->name, log_in6addr(addr)); 676 mreq.ipv6mr_multiaddr = *addr; 677 mreq.ipv6mr_interface = iface->ifindex; 678 679 if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, 680 &mreq, sizeof(mreq)) < 0) { 681 log_warn("if_join_group: error IPV6_JOIN_GROUP, " 682 "interface %s address %s", iface->name, 683 log_in6addr(addr)); 684 return (-1); 685 } 686 break; 687 case IF_TYPE_POINTOMULTIPOINT: 688 case IF_TYPE_VIRTUALLINK: 689 case IF_TYPE_NBMA: 690 log_debug("if_join_group: type %s not supported, interface %s", 691 if_type_name(iface->type), iface->name); 692 return (-1); 693 default: 694 fatalx("if_join_group: unknown interface type"); 695 } 696 697 return (0); 698 } 699 700 int 701 if_leave_group(struct iface *iface, struct in6_addr *addr) 702 { 703 struct ipv6_mreq mreq; 704 705 switch (iface->type) { 706 case IF_TYPE_POINTOPOINT: 707 case IF_TYPE_BROADCAST: 708 log_debug("if_leave_group: interface %s addr %s", 709 iface->name, log_in6addr(addr)); 710 mreq.ipv6mr_multiaddr = *addr; 711 mreq.ipv6mr_interface = iface->ifindex; 712 713 if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, 714 (void *)&mreq, sizeof(mreq)) < 0) { 715 log_warn("if_leave_group: error IPV6_LEAVE_GROUP, " 716 "interface %s address %s", iface->name, 717 log_in6addr(addr)); 718 return (-1); 719 } 720 break; 721 case IF_TYPE_POINTOMULTIPOINT: 722 case IF_TYPE_VIRTUALLINK: 723 case IF_TYPE_NBMA: 724 log_debug("if_leave_group: type %s not supported, interface %s", 725 if_type_name(iface->type), iface->name); 726 return (-1); 727 default: 728 fatalx("if_leave_group: unknown interface type"); 729 } 730 return (0); 731 } 732 733 int 734 if_set_mcast(struct iface *iface) 735 { 736 switch (iface->type) { 737 case IF_TYPE_POINTOPOINT: 738 case IF_TYPE_BROADCAST: 739 if (setsockopt(iface->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, 740 &iface->ifindex, sizeof(iface->ifindex)) < 0) { 741 log_debug("if_set_mcast: error setting " 742 "IP_MULTICAST_IF, interface %s", iface->name); 743 return (-1); 744 } 745 break; 746 case IF_TYPE_POINTOMULTIPOINT: 747 case IF_TYPE_VIRTUALLINK: 748 case IF_TYPE_NBMA: 749 log_debug("if_set_mcast: type %s not supported, interface %s", 750 if_type_name(iface->type), iface->name); 751 return (-1); 752 default: 753 fatalx("if_set_mcast: unknown interface type"); 754 } 755 756 return (0); 757 } 758 759 int 760 if_set_mcast_loop(int fd) 761 { 762 u_int loop = 0; 763 764 if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, 765 (u_int *)&loop, sizeof(loop)) < 0) { 766 log_warn("if_set_mcast_loop: error setting " 767 "IPV6_MULTICAST_LOOP"); 768 return (-1); 769 } 770 771 return (0); 772 } 773 774 int 775 if_set_ipv6_pktinfo(int fd, int enable) 776 { 777 if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &enable, 778 sizeof(enable)) < 0) { 779 log_warn("if_set_ipv6_pktinfo: error setting IPV6_PKTINFO"); 780 return (-1); 781 } 782 783 return (0); 784 } 785 786 int 787 if_set_ipv6_checksum(int fd) 788 { 789 int offset = offsetof(struct ospf_hdr, chksum); 790 791 log_debug("if_set_ipv6_checksum setting cksum offset to %i", offset); 792 if (setsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM, &offset, 793 sizeof(offset)) < 0) { 794 log_warn("if_set_ipv6_checksum: error setting IPV6_CHECKSUM"); 795 return (-1); 796 } 797 return (0); 798 } 799