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