1 /* $OpenBSD: interface.c,v 1.75 2012/05/14 10:17:21 sthen 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 RB_INIT(&iface->lsa_tree); 171 172 iface->crypt_seq_num = arc4random() & 0x0fffffff; 173 174 if (kif == NULL) { 175 iface->type = IF_TYPE_VIRTUALLINK; 176 snprintf(iface->name, sizeof(iface->name), "vlink%d", 177 vlink_cnt++); 178 iface->flags |= IFF_UP; 179 iface->mtu = IP_MSS; 180 return (iface); 181 } 182 183 strlcpy(iface->name, kif->ifname, sizeof(iface->name)); 184 185 /* get type */ 186 if (kif->flags & IFF_POINTOPOINT) 187 iface->type = IF_TYPE_POINTOPOINT; 188 if (kif->flags & IFF_BROADCAST && 189 kif->flags & IFF_MULTICAST) 190 iface->type = IF_TYPE_BROADCAST; 191 if (kif->flags & IFF_LOOPBACK) { 192 iface->type = IF_TYPE_POINTOPOINT; 193 iface->passive = 1; 194 } 195 196 /* get mtu, index and flags */ 197 iface->mtu = kif->mtu; 198 iface->ifindex = kif->ifindex; 199 iface->flags = kif->flags; 200 iface->linkstate = kif->link_state; 201 iface->media_type = kif->media_type; 202 iface->baudrate = kif->baudrate; 203 204 /* set address, mask and p2p addr */ 205 iface->addr = ka->addr; 206 iface->mask = ka->mask; 207 if (kif->flags & IFF_POINTOPOINT) { 208 iface->dst = ka->dstbrd; 209 } 210 211 return (iface); 212 } 213 214 void 215 if_del(struct iface *iface) 216 { 217 struct nbr *nbr = NULL; 218 219 /* revert the demotion when the interface is deleted */ 220 if ((iface->state & (IF_STA_MULTI | IF_STA_POINTTOPOINT)) == 0) 221 ospfe_demote_iface(iface, 1); 222 223 /* clear lists etc */ 224 while ((nbr = LIST_FIRST(&iface->nbr_list)) != NULL) 225 nbr_del(nbr); 226 227 if (evtimer_pending(&iface->hello_timer, NULL)) 228 evtimer_del(&iface->hello_timer); 229 if (evtimer_pending(&iface->wait_timer, NULL)) 230 evtimer_del(&iface->wait_timer); 231 if (evtimer_pending(&iface->lsack_tx_timer, NULL)) 232 evtimer_del(&iface->lsack_tx_timer); 233 234 ls_ack_list_clr(iface); 235 md_list_clr(&iface->auth_md_list); 236 free(iface); 237 } 238 239 void 240 if_init(struct ospfd_conf *xconf, struct iface *iface) 241 { 242 struct ifreq ifr; 243 u_int rdomain; 244 245 /* init the dummy local neighbor */ 246 iface->self = nbr_new(ospfe_router_id(), iface, 1); 247 248 /* set event handlers for interface */ 249 evtimer_set(&iface->lsack_tx_timer, ls_ack_tx_timer, iface); 250 evtimer_set(&iface->hello_timer, if_hello_timer, iface); 251 evtimer_set(&iface->wait_timer, if_wait_timer, iface); 252 253 iface->fd = xconf->ospf_socket; 254 255 strlcpy(ifr.ifr_name, iface->name, sizeof(ifr.ifr_name)); 256 if (ioctl(iface->fd, SIOCGIFRDOMAIN, (caddr_t)&ifr) == -1) 257 rdomain = 0; 258 else { 259 rdomain = ifr.ifr_rdomainid; 260 if (setsockopt(iface->fd, SOL_SOCKET, SO_RTABLE, 261 &rdomain, sizeof(rdomain)) == -1) 262 fatal("failed to set rdomain"); 263 } 264 if (rdomain != xconf->rdomain) 265 fatalx("interface rdomain mismatch"); 266 267 ospfe_demote_iface(iface, 0); 268 } 269 270 /* timers */ 271 /* ARGSUSED */ 272 void 273 if_hello_timer(int fd, short event, void *arg) 274 { 275 struct iface *iface = arg; 276 struct timeval tv; 277 278 send_hello(iface); 279 280 /* reschedule hello_timer */ 281 timerclear(&tv); 282 if (iface->dead_interval == FAST_RTR_DEAD_TIME) 283 tv.tv_usec = iface->fast_hello_interval * 1000; 284 else 285 tv.tv_sec = iface->hello_interval; 286 if (evtimer_add(&iface->hello_timer, &tv) == -1) 287 fatal("if_hello_timer"); 288 } 289 290 void 291 if_start_hello_timer(struct iface *iface) 292 { 293 struct timeval tv; 294 295 timerclear(&tv); 296 if (evtimer_add(&iface->hello_timer, &tv) == -1) 297 fatal("if_start_hello_timer"); 298 } 299 300 void 301 if_stop_hello_timer(struct iface *iface) 302 { 303 if (evtimer_del(&iface->hello_timer) == -1) 304 fatal("if_stop_hello_timer"); 305 } 306 307 /* ARGSUSED */ 308 void 309 if_wait_timer(int fd, short event, void *arg) 310 { 311 struct iface *iface = arg; 312 313 if_fsm(iface, IF_EVT_WTIMER); 314 } 315 316 void 317 if_start_wait_timer(struct iface *iface) 318 { 319 struct timeval tv; 320 321 timerclear(&tv); 322 tv.tv_sec = iface->dead_interval; 323 if (evtimer_add(&iface->wait_timer, &tv) == -1) 324 fatal("if_start_wait_timer"); 325 } 326 327 void 328 if_stop_wait_timer(struct iface *iface) 329 { 330 if (evtimer_del(&iface->wait_timer) == -1) 331 fatal("if_stop_wait_timer"); 332 } 333 334 /* actions */ 335 int 336 if_act_start(struct iface *iface) 337 { 338 struct in_addr addr; 339 struct timeval now; 340 341 if (!((iface->flags & IFF_UP) && 342 LINK_STATE_IS_UP(iface->linkstate))) 343 return (0); 344 345 if (iface->media_type == IFT_CARP && iface->passive == 0) { 346 /* force passive mode on carp interfaces */ 347 log_warnx("if_act_start: forcing interface %s to passive", 348 iface->name); 349 iface->passive = 1; 350 } 351 352 gettimeofday(&now, NULL); 353 iface->uptime = now.tv_sec; 354 355 /* loopback interfaces have a special state and are passive */ 356 if (iface->flags & IFF_LOOPBACK) 357 iface->state = IF_STA_LOOPBACK; 358 359 if (iface->passive) { 360 /* for an update of stub network entries */ 361 orig_rtr_lsa(iface->area); 362 return (0); 363 } 364 365 switch (iface->type) { 366 case IF_TYPE_POINTOPOINT: 367 inet_aton(AllSPFRouters, &addr); 368 if (if_join_group(iface, &addr)) 369 return (-1); 370 iface->state = IF_STA_POINTTOPOINT; 371 break; 372 case IF_TYPE_VIRTUALLINK: 373 iface->state = IF_STA_POINTTOPOINT; 374 break; 375 case IF_TYPE_POINTOMULTIPOINT: 376 case IF_TYPE_NBMA: 377 log_debug("if_act_start: type %s not supported, interface %s", 378 if_type_name(iface->type), iface->name); 379 return (-1); 380 case IF_TYPE_BROADCAST: 381 inet_aton(AllSPFRouters, &addr); 382 if (if_join_group(iface, &addr)) 383 return (-1); 384 if (iface->priority == 0) { 385 iface->state = IF_STA_DROTHER; 386 } else { 387 iface->state = IF_STA_WAITING; 388 if_start_wait_timer(iface); 389 } 390 break; 391 default: 392 fatalx("if_act_start: unknown interface type"); 393 } 394 395 /* hello timer needs to be started in any case */ 396 if_start_hello_timer(iface); 397 return (0); 398 } 399 400 struct nbr * 401 if_elect(struct nbr *a, struct nbr *b) 402 { 403 if (a->priority > b->priority) 404 return (a); 405 if (a->priority < b->priority) 406 return (b); 407 if (ntohl(a->id.s_addr) > ntohl(b->id.s_addr)) 408 return (a); 409 return (b); 410 } 411 412 int 413 if_act_elect(struct iface *iface) 414 { 415 struct in_addr addr; 416 struct nbr *nbr, *bdr = NULL, *dr = NULL; 417 int round = 0; 418 int changed = 0; 419 int old_state; 420 char b1[16], b2[16], b3[16], b4[16]; 421 422 start: 423 /* elect backup designated router */ 424 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 425 if (nbr->priority == 0 || nbr == dr || /* not electable */ 426 nbr->state & NBR_STA_PRELIM || /* not available */ 427 nbr->dr.s_addr == nbr->addr.s_addr) /* don't elect DR */ 428 continue; 429 if (bdr != NULL) { 430 /* 431 * routers announcing themselves as BDR have higher 432 * precedence over those routers announcing a 433 * different BDR. 434 */ 435 if (nbr->bdr.s_addr == nbr->addr.s_addr) { 436 if (bdr->bdr.s_addr == bdr->addr.s_addr) 437 bdr = if_elect(bdr, nbr); 438 else 439 bdr = nbr; 440 } else if (bdr->bdr.s_addr != bdr->addr.s_addr) 441 bdr = if_elect(bdr, nbr); 442 } else 443 bdr = nbr; 444 } 445 446 /* elect designated router */ 447 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 448 if (nbr->priority == 0 || nbr->state & NBR_STA_PRELIM || 449 (nbr != dr && nbr->dr.s_addr != nbr->addr.s_addr)) 450 /* only DR may be elected check priority too */ 451 continue; 452 if (dr == NULL) 453 dr = nbr; 454 else 455 dr = if_elect(dr, nbr); 456 } 457 458 if (dr == NULL) { 459 /* no designated router found use backup DR */ 460 dr = bdr; 461 bdr = NULL; 462 } 463 464 /* 465 * if we are involved in the election (e.g. new DR or no 466 * longer BDR) redo the election 467 */ 468 if (round == 0 && 469 ((iface->self == dr && iface->self != iface->dr) || 470 (iface->self != dr && iface->self == iface->dr) || 471 (iface->self == bdr && iface->self != iface->bdr) || 472 (iface->self != bdr && iface->self == iface->bdr))) { 473 /* 474 * Reset announced DR/BDR to calculated one, so 475 * that we may get elected in the second round. 476 * This is needed to drop from a DR to a BDR. 477 */ 478 iface->self->dr.s_addr = dr->addr.s_addr; 479 if (bdr) 480 iface->self->bdr.s_addr = bdr->addr.s_addr; 481 round = 1; 482 goto start; 483 } 484 485 log_debug("if_act_elect: interface %s old dr %s new dr %s, " 486 "old bdr %s new bdr %s", iface->name, 487 iface->dr ? inet_ntop(AF_INET, &iface->dr->addr, b1, sizeof(b1)) : 488 "none", dr ? inet_ntop(AF_INET, &dr->addr, b2, sizeof(b2)) : "none", 489 iface->bdr ? inet_ntop(AF_INET, &iface->bdr->addr, b3, sizeof(b3)) : 490 "none", bdr ? inet_ntop(AF_INET, &bdr->addr, b4, sizeof(b4)) : 491 "none"); 492 493 /* 494 * After the second round still DR or BDR change state to DR or BDR, 495 * etc. 496 */ 497 old_state = iface->state; 498 if (dr == iface->self) 499 iface->state = IF_STA_DR; 500 else if (bdr == iface->self) 501 iface->state = IF_STA_BACKUP; 502 else 503 iface->state = IF_STA_DROTHER; 504 505 /* TODO if iface is NBMA send all non eligible neighbors event Start */ 506 507 /* 508 * if DR or BDR changed issue a AdjOK? event for all neighbors > 2-Way 509 */ 510 if (iface->dr != dr || iface->bdr != bdr) 511 changed = 1; 512 513 iface->dr = dr; 514 iface->bdr = bdr; 515 516 if (changed) { 517 inet_aton(AllDRouters, &addr); 518 if (old_state & IF_STA_DRORBDR && 519 (iface->state & IF_STA_DRORBDR) == 0) { 520 if (if_leave_group(iface, &addr)) 521 return (-1); 522 } else if ((old_state & IF_STA_DRORBDR) == 0 && 523 iface->state & IF_STA_DRORBDR) { 524 if (if_join_group(iface, &addr)) 525 return (-1); 526 } 527 528 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 529 if (nbr->state & NBR_STA_BIDIR) 530 nbr_fsm(nbr, NBR_EVT_ADJ_OK); 531 } 532 533 orig_rtr_lsa(iface->area); 534 if (iface->state & IF_STA_DR || old_state & IF_STA_DR) 535 orig_net_lsa(iface); 536 } 537 538 if_start_hello_timer(iface); 539 return (0); 540 } 541 542 int 543 if_act_reset(struct iface *iface) 544 { 545 struct nbr *nbr = NULL; 546 struct in_addr addr; 547 548 if (iface->passive) { 549 /* for an update of stub network entries */ 550 orig_rtr_lsa(iface->area); 551 return (0); 552 } 553 554 switch (iface->type) { 555 case IF_TYPE_POINTOPOINT: 556 case IF_TYPE_BROADCAST: 557 /* try to cleanup */ 558 inet_aton(AllSPFRouters, &addr); 559 if_leave_group(iface, &addr); 560 if (iface->state & IF_STA_DRORBDR) { 561 inet_aton(AllDRouters, &addr); 562 if_leave_group(iface, &addr); 563 } 564 break; 565 case IF_TYPE_VIRTUALLINK: 566 /* nothing */ 567 break; 568 case IF_TYPE_NBMA: 569 case IF_TYPE_POINTOMULTIPOINT: 570 log_debug("if_act_reset: type %s not supported, interface %s", 571 if_type_name(iface->type), iface->name); 572 return (-1); 573 default: 574 fatalx("if_act_reset: unknown interface type"); 575 } 576 577 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 578 if (nbr_fsm(nbr, NBR_EVT_KILL_NBR)) { 579 log_debug("if_act_reset: error killing neighbor %s", 580 inet_ntoa(nbr->id)); 581 } 582 } 583 584 iface->dr = NULL; 585 iface->bdr = NULL; 586 587 ls_ack_list_clr(iface); 588 stop_ls_ack_tx_timer(iface); 589 if_stop_hello_timer(iface); 590 if_stop_wait_timer(iface); 591 592 /* send empty hello to tell everybody that we are going down */ 593 send_hello(iface); 594 595 return (0); 596 } 597 598 struct ctl_iface * 599 if_to_ctl(struct iface *iface) 600 { 601 static struct ctl_iface ictl; 602 struct timeval tv, now, res; 603 struct nbr *nbr; 604 605 memcpy(ictl.name, iface->name, sizeof(ictl.name)); 606 memcpy(&ictl.addr, &iface->addr, sizeof(ictl.addr)); 607 memcpy(&ictl.mask, &iface->mask, sizeof(ictl.mask)); 608 ictl.rtr_id.s_addr = ospfe_router_id(); 609 memcpy(&ictl.area, &iface->area->id, sizeof(ictl.area)); 610 if (iface->dr) { 611 memcpy(&ictl.dr_id, &iface->dr->id, sizeof(ictl.dr_id)); 612 memcpy(&ictl.dr_addr, &iface->dr->addr, sizeof(ictl.dr_addr)); 613 } else { 614 bzero(&ictl.dr_id, sizeof(ictl.dr_id)); 615 bzero(&ictl.dr_addr, sizeof(ictl.dr_addr)); 616 } 617 if (iface->bdr) { 618 memcpy(&ictl.bdr_id, &iface->bdr->id, sizeof(ictl.bdr_id)); 619 memcpy(&ictl.bdr_addr, &iface->bdr->addr, 620 sizeof(ictl.bdr_addr)); 621 } else { 622 bzero(&ictl.bdr_id, sizeof(ictl.bdr_id)); 623 bzero(&ictl.bdr_addr, sizeof(ictl.bdr_addr)); 624 } 625 ictl.ifindex = iface->ifindex; 626 ictl.state = iface->state; 627 ictl.mtu = iface->mtu; 628 ictl.nbr_cnt = 0; 629 ictl.adj_cnt = 0; 630 ictl.baudrate = iface->baudrate; 631 ictl.dead_interval = iface->dead_interval; 632 ictl.fast_hello_interval = iface->fast_hello_interval; 633 ictl.transmit_delay = iface->transmit_delay; 634 ictl.hello_interval = iface->hello_interval; 635 ictl.flags = iface->flags; 636 ictl.metric = iface->metric; 637 ictl.rxmt_interval = iface->rxmt_interval; 638 ictl.type = iface->type; 639 ictl.linkstate = iface->linkstate; 640 ictl.mediatype = iface->media_type; 641 ictl.priority = iface->priority; 642 ictl.passive = iface->passive; 643 ictl.auth_type = iface->auth_type; 644 ictl.auth_keyid = iface->auth_keyid; 645 646 gettimeofday(&now, NULL); 647 if (evtimer_pending(&iface->hello_timer, &tv)) { 648 timersub(&tv, &now, &res); 649 ictl.hello_timer = res; 650 } else { 651 ictl.hello_timer.tv_sec = -1; 652 } 653 654 if (iface->state != IF_STA_DOWN && 655 iface->uptime != 0) { 656 ictl.uptime = now.tv_sec - iface->uptime; 657 } else 658 ictl.uptime = 0; 659 660 LIST_FOREACH(nbr, &iface->nbr_list, entry) { 661 if (nbr == iface->self) 662 continue; 663 ictl.nbr_cnt++; 664 if (nbr->state & NBR_STA_ADJFORM) 665 ictl.adj_cnt++; 666 } 667 668 return (&ictl); 669 } 670 671 /* misc */ 672 int 673 if_set_recvif(int fd, int enable) 674 { 675 if (setsockopt(fd, IPPROTO_IP, IP_RECVIF, &enable, 676 sizeof(enable)) < 0) { 677 log_warn("if_set_recvif: error setting IP_RECVIF"); 678 return (-1); 679 } 680 return (0); 681 } 682 683 void 684 if_set_sockbuf(int fd) 685 { 686 int bsize; 687 688 bsize = 65535; 689 while (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bsize, 690 sizeof(bsize)) == -1) 691 bsize /= 2; 692 693 if (bsize != 65535) 694 log_warnx("if_set_sockbuf: recvbuf size only %d", bsize); 695 696 bsize = 65535; 697 while (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &bsize, 698 sizeof(bsize)) == -1) 699 bsize /= 2; 700 701 if (bsize != 65535) 702 log_warnx("if_set_sockbuf: sendbuf size only %d", bsize); 703 } 704 705 /* 706 * only one JOIN or DROP per interface and address is allowed so we need 707 * to keep track of what is added and removed. 708 */ 709 struct if_group_count { 710 LIST_ENTRY(if_group_count) entry; 711 struct in_addr addr; 712 unsigned int ifindex; 713 int count; 714 }; 715 716 LIST_HEAD(,if_group_count) ifglist = LIST_HEAD_INITIALIZER(ifglist); 717 718 int 719 if_join_group(struct iface *iface, struct in_addr *addr) 720 { 721 struct ip_mreq mreq; 722 struct if_group_count *ifg; 723 724 switch (iface->type) { 725 case IF_TYPE_POINTOPOINT: 726 case IF_TYPE_BROADCAST: 727 LIST_FOREACH(ifg, &ifglist, entry) 728 if (iface->ifindex == ifg->ifindex && 729 addr->s_addr == ifg->addr.s_addr) 730 break; 731 if (ifg == NULL) { 732 if ((ifg = calloc(1, sizeof(*ifg))) == NULL) 733 fatal("if_join_group"); 734 ifg->addr.s_addr = addr->s_addr; 735 ifg->ifindex = iface->ifindex; 736 LIST_INSERT_HEAD(&ifglist, ifg, entry); 737 } 738 739 if (ifg->count++ != 0) 740 /* already joined */ 741 return (0); 742 743 mreq.imr_multiaddr.s_addr = addr->s_addr; 744 mreq.imr_interface.s_addr = iface->addr.s_addr; 745 746 if (setsockopt(iface->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, 747 (void *)&mreq, sizeof(mreq)) < 0) { 748 log_warn("if_join_group: error IP_ADD_MEMBERSHIP, " 749 "interface %s address %s", iface->name, 750 inet_ntoa(*addr)); 751 return (-1); 752 } 753 break; 754 case IF_TYPE_POINTOMULTIPOINT: 755 case IF_TYPE_VIRTUALLINK: 756 case IF_TYPE_NBMA: 757 log_debug("if_join_group: type %s not supported, interface %s", 758 if_type_name(iface->type), iface->name); 759 return (-1); 760 default: 761 fatalx("if_join_group: unknown interface type"); 762 } 763 764 return (0); 765 } 766 767 int 768 if_leave_group(struct iface *iface, struct in_addr *addr) 769 { 770 struct ip_mreq mreq; 771 struct if_group_count *ifg; 772 773 switch (iface->type) { 774 case IF_TYPE_POINTOPOINT: 775 case IF_TYPE_BROADCAST: 776 LIST_FOREACH(ifg, &ifglist, entry) 777 if (iface->ifindex == ifg->ifindex && 778 addr->s_addr == ifg->addr.s_addr) 779 break; 780 781 /* if interface is not found just try to drop membership */ 782 if (ifg) { 783 if (--ifg->count != 0) 784 /* others still joined */ 785 return (0); 786 787 LIST_REMOVE(ifg, entry); 788 free(ifg); 789 } 790 791 mreq.imr_multiaddr.s_addr = addr->s_addr; 792 mreq.imr_interface.s_addr = iface->addr.s_addr; 793 794 if (setsockopt(iface->fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, 795 (void *)&mreq, sizeof(mreq)) < 0) { 796 log_warn("if_leave_group: error IP_DROP_MEMBERSHIP, " 797 "interface %s address %s", iface->name, 798 inet_ntoa(*addr)); 799 return (-1); 800 } 801 break; 802 case IF_TYPE_POINTOMULTIPOINT: 803 case IF_TYPE_VIRTUALLINK: 804 case IF_TYPE_NBMA: 805 log_debug("if_leave_group: type %s not supported, interface %s", 806 if_type_name(iface->type), iface->name); 807 return (-1); 808 default: 809 fatalx("if_leave_group: unknown interface type"); 810 } 811 812 return (0); 813 } 814 815 int 816 if_set_mcast(struct iface *iface) 817 { 818 switch (iface->type) { 819 case IF_TYPE_POINTOPOINT: 820 case IF_TYPE_BROADCAST: 821 if (setsockopt(iface->fd, IPPROTO_IP, IP_MULTICAST_IF, 822 &iface->addr.s_addr, sizeof(iface->addr.s_addr)) < 0) { 823 log_warn("if_set_mcast: error setting " 824 "IP_MULTICAST_IF, interface %s", iface->name); 825 return (-1); 826 } 827 break; 828 case IF_TYPE_POINTOMULTIPOINT: 829 case IF_TYPE_VIRTUALLINK: 830 case IF_TYPE_NBMA: 831 log_debug("if_set_mcast: type %s not supported, interface %s", 832 if_type_name(iface->type), iface->name); 833 return (-1); 834 default: 835 fatalx("if_set_mcast: unknown interface type"); 836 } 837 838 return (0); 839 } 840 841 int 842 if_set_mcast_loop(int fd) 843 { 844 u_int8_t loop = 0; 845 846 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, 847 (char *)&loop, sizeof(loop)) < 0) { 848 log_warn("if_set_mcast_loop: error setting IP_MULTICAST_LOOP"); 849 return (-1); 850 } 851 852 return (0); 853 } 854 855 int 856 if_set_ip_hdrincl(int fd) 857 { 858 int hincl = 1; 859 860 if (setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl)) < 0) { 861 log_warn("if_set_ip_hdrincl: error setting IP_HDRINCL"); 862 return (-1); 863 } 864 865 return (0); 866 } 867