interface.c revision 1.19
1/* $OpenBSD: interface.c,v 1.19 2013/06/04 02:39:10 claudio Exp $ */ 2 3/* 4 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> 5 * Copyright (c) 2004, 2005, 2008 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 <fcntl.h> 29#include <ctype.h> 30#include <err.h> 31#include <stdio.h> 32#include <stdlib.h> 33#include <unistd.h> 34#include <string.h> 35#include <event.h> 36 37#include "ldpd.h" 38#include "ldp.h" 39#include "log.h" 40#include "ldpe.h" 41 42extern struct ldpd_conf *leconf; 43 44int if_act_start(struct iface *); 45int if_act_reset(struct iface *); 46int if_act_update(struct iface *); 47void if_hello_timer(int, short, void *); 48void if_start_hello_timer(struct iface *); 49void if_stop_hello_timer(struct iface *); 50struct nbr *if_elect(struct nbr *, struct nbr *); 51 52struct { 53 int state; 54 enum iface_event event; 55 enum iface_action action; 56 int new_state; 57} iface_fsm[] = { 58 /* current state event that happened action to take resulting state */ 59 {IF_STA_DOWN, IF_EVT_DOWN, IF_ACT_NOTHING, 0}, 60 {IF_STA_DOWN, IF_EVT_UP, IF_ACT_UPDATE, 0}, 61 {IF_STA_DOWN, IF_EVT_NEWADDR, IF_ACT_UPDATE, 0}, 62 {IF_STA_DOWN, IF_EVT_DELADDR, IF_ACT_NOTHING, 0}, 63 {IF_STA_ACTIVE, IF_EVT_DOWN, IF_ACT_RST, IF_STA_DOWN}, 64 {IF_STA_ACTIVE, IF_EVT_NEWADDR, IF_ACT_NOTHING, 0}, 65 {IF_STA_ACTIVE, IF_EVT_DELADDR, IF_ACT_UPDATE, 0}, 66 {-1, IF_EVT_NOTHING, IF_ACT_NOTHING, 0}, 67}; 68 69const char * const if_event_names[] = { 70 "NOTHING", 71 "UP", 72 "DOWN", 73 "NEWADDR", 74 "DELADDR" 75}; 76 77const char * const if_action_names[] = { 78 "NOTHING", 79 "UPDATE", 80 "RESET" 81}; 82 83int 84if_fsm(struct iface *iface, enum iface_event event) 85{ 86 int old_state; 87 int new_state = 0; 88 int i, ret = 0; 89 90 old_state = iface->state; 91 92 for (i = 0; iface_fsm[i].state != -1; i++) 93 if ((iface_fsm[i].state & old_state) && 94 (iface_fsm[i].event == event)) { 95 new_state = iface_fsm[i].new_state; 96 break; 97 } 98 99 if (iface_fsm[i].state == -1) { 100 /* event outside of the defined fsm, ignore it. */ 101 log_debug("if_fsm: interface %s, " 102 "event %s not expected in state %s", iface->name, 103 if_event_names[event], if_state_name(old_state)); 104 return (0); 105 } 106 107 switch (iface_fsm[i].action) { 108 case IF_ACT_UPDATE: 109 ret = if_act_update(iface); 110 break; 111 case IF_ACT_RST: 112 ret = if_act_reset(iface); 113 break; 114 case IF_ACT_NOTHING: 115 /* do nothing */ 116 break; 117 } 118 119 if (ret) { 120 log_debug("if_fsm: error changing state for interface %s, " 121 "event %s, state %s", iface->name, if_event_names[event], 122 if_state_name(old_state)); 123 return (-1); 124 } 125 126 if (new_state != 0) 127 iface->state = new_state; 128 129 log_debug("if_fsm: event %s resulted in action %s and changing " 130 "state for interface %s from %s to %s", 131 if_event_names[event], if_action_names[iface_fsm[i].action], 132 iface->name, if_state_name(old_state), if_state_name(iface->state)); 133 134 return (ret); 135} 136 137struct iface * 138if_new(struct kif *kif) 139{ 140 struct iface *iface; 141 142 if ((iface = calloc(1, sizeof(*iface))) == NULL) 143 err(1, "if_new: calloc"); 144 145 iface->state = IF_STA_DOWN; 146 147 strlcpy(iface->name, kif->ifname, sizeof(iface->name)); 148 149 /* get type */ 150 if (kif->flags & IFF_POINTOPOINT) 151 iface->type = IF_TYPE_POINTOPOINT; 152 if (kif->flags & IFF_BROADCAST && 153 kif->flags & IFF_MULTICAST) 154 iface->type = IF_TYPE_BROADCAST; 155 156 /* get index and flags */ 157 iface->ifindex = kif->ifindex; 158 iface->flags = kif->flags; 159 iface->linkstate = kif->link_state; 160 iface->media_type = kif->media_type; 161 162 return (iface); 163} 164 165void 166if_del(struct iface *iface) 167{ 168 struct adj *adj; 169 struct if_addr *if_addr; 170 171 log_debug("if_del: interface %s", iface->name); 172 173 if_stop_hello_timer(iface); 174 175 while ((adj = LIST_FIRST(&iface->adj_list)) != NULL) { 176 LIST_REMOVE(adj, iface_entry); 177 adj_del(adj); 178 } 179 while ((if_addr = LIST_FIRST(&iface->addr_list)) != NULL) 180 LIST_REMOVE(if_addr, iface_entry); 181 182 free(iface); 183} 184 185void 186if_init(struct ldpd_conf *xconf, struct iface *iface) 187{ 188 /* set event handlers for interface */ 189 evtimer_set(&iface->hello_timer, if_hello_timer, iface); 190 191 iface->discovery_fd = xconf->ldp_discovery_socket; 192} 193 194struct iface * 195if_lookup(u_short ifindex) 196{ 197 struct iface *iface; 198 199 LIST_FOREACH(iface, &leconf->iface_list, entry) 200 if (iface->ifindex == ifindex) 201 return (iface); 202 203 return (NULL); 204} 205 206/* timers */ 207/* ARGSUSED */ 208void 209if_hello_timer(int fd, short event, void *arg) 210{ 211 struct iface *iface = arg; 212 struct timeval tv; 213 214 send_hello(HELLO_LINK, iface, NULL); 215 216 /* reschedule hello_timer */ 217 timerclear(&tv); 218 tv.tv_sec = iface->hello_interval; 219 if (evtimer_add(&iface->hello_timer, &tv) == -1) 220 fatal("if_hello_timer"); 221} 222 223void 224if_start_hello_timer(struct iface *iface) 225{ 226 struct timeval tv; 227 228 send_hello(HELLO_LINK, iface, NULL); 229 230 timerclear(&tv); 231 tv.tv_sec = iface->hello_interval; 232 if (evtimer_add(&iface->hello_timer, &tv) == -1) 233 fatal("if_start_hello_timer"); 234} 235 236void 237if_stop_hello_timer(struct iface *iface) 238{ 239 if (evtimer_pending(&iface->hello_timer, NULL) && 240 evtimer_del(&iface->hello_timer) == -1) 241 fatal("if_stop_hello_timer"); 242} 243 244/* actions */ 245int 246if_act_start(struct iface *iface) 247{ 248 struct in_addr addr; 249 struct timeval now; 250 251 gettimeofday(&now, NULL); 252 iface->uptime = now.tv_sec; 253 254 inet_aton(AllRouters, &addr); 255 if (if_join_group(iface, &addr)) 256 return (-1); 257 258 /* hello timer needs to be started in any case */ 259 if_start_hello_timer(iface); 260 return (0); 261} 262 263int 264if_act_reset(struct iface *iface) 265{ 266 struct in_addr addr; 267 268 if_stop_hello_timer(iface); 269 270 /* try to cleanup */ 271 inet_aton(AllRouters, &addr); 272 if_leave_group(iface, &addr); 273 274 return (0); 275} 276 277int 278if_act_update(struct iface *iface) 279{ 280 int ret; 281 282 if (iface->state == IF_STA_DOWN) { 283 if (!((iface->flags & IFF_UP) && 284 LINK_STATE_IS_UP(iface->linkstate))) 285 return (0); 286 287 if (LIST_EMPTY(&iface->addr_list)) 288 return (0); 289 290 iface->state = IF_STA_ACTIVE; 291 ret = if_act_start(iface); 292 } else { 293 if (!LIST_EMPTY(&iface->addr_list)) 294 return (0); 295 296 iface->state = IF_STA_DOWN; 297 ret = if_act_reset(iface); 298 } 299 300 return (ret); 301} 302 303struct ctl_iface * 304if_to_ctl(struct iface *iface) 305{ 306 static struct ctl_iface ictl; 307 struct timeval now; 308 struct adj *adj; 309 310 memcpy(ictl.name, iface->name, sizeof(ictl.name)); 311 ictl.ifindex = iface->ifindex; 312 ictl.state = iface->state; 313 ictl.hello_holdtime = iface->hello_holdtime; 314 ictl.hello_interval = iface->hello_interval; 315 ictl.flags = iface->flags; 316 ictl.type = iface->type; 317 ictl.linkstate = iface->linkstate; 318 ictl.mediatype = iface->media_type; 319 320 gettimeofday(&now, NULL); 321 if (iface->state != IF_STA_DOWN && 322 iface->uptime != 0) { 323 ictl.uptime = now.tv_sec - iface->uptime; 324 } else 325 ictl.uptime = 0; 326 327 ictl.adj_cnt = 0; 328 LIST_FOREACH(adj, &iface->adj_list, iface_entry) 329 ictl.adj_cnt++; 330 331 return (&ictl); 332} 333 334/* misc */ 335int 336if_set_mcast_ttl(int fd, u_int8_t ttl) 337{ 338 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, 339 (char *)&ttl, sizeof(ttl)) < 0) { 340 log_warn("if_set_mcast_ttl: error setting " 341 "IP_MULTICAST_TTL to %d", ttl); 342 return (-1); 343 } 344 345 return (0); 346} 347 348int 349if_set_tos(int fd, int tos) 350{ 351 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (int *)&tos, sizeof(tos)) < 0) { 352 log_warn("if_set_tos: error setting IP_TOS to 0x%x", tos); 353 return (-1); 354 } 355 356 return (0); 357} 358 359int 360if_set_recvif(int fd, int enable) 361{ 362 if (setsockopt(fd, IPPROTO_IP, IP_RECVIF, &enable, 363 sizeof(enable)) < 0) { 364 log_warn("if_set_recvif: error setting IP_RECVIF"); 365 return (-1); 366 } 367 return (0); 368} 369 370void 371if_set_recvbuf(int fd) 372{ 373 int bsize; 374 375 bsize = 65535; 376 while (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bsize, 377 sizeof(bsize)) == -1) 378 bsize /= 2; 379} 380 381int 382if_set_reuse(int fd, int enable) 383{ 384 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, 385 sizeof(int)) < 0) { 386 log_warn("if_set_reuse: error setting SO_REUSEADDR"); 387 return (-1); 388 } 389 390 return (0); 391} 392 393/* 394 * only one JOIN or DROP per interface and address is allowed so we need 395 * to keep track of what is added and removed. 396 */ 397struct if_group_count { 398 LIST_ENTRY(if_group_count) entry; 399 struct in_addr addr; 400 unsigned int ifindex; 401 int count; 402}; 403 404LIST_HEAD(,if_group_count) ifglist = LIST_HEAD_INITIALIZER(ifglist); 405 406int 407if_join_group(struct iface *iface, struct in_addr *addr) 408{ 409 struct ip_mreq mreq; 410 struct if_group_count *ifg; 411 struct if_addr *if_addr; 412 413 LIST_FOREACH(ifg, &ifglist, entry) 414 if (iface->ifindex == ifg->ifindex && 415 addr->s_addr == ifg->addr.s_addr) 416 break; 417 if (ifg == NULL) { 418 if ((ifg = calloc(1, sizeof(*ifg))) == NULL) 419 fatal("if_join_group"); 420 ifg->addr.s_addr = addr->s_addr; 421 ifg->ifindex = iface->ifindex; 422 LIST_INSERT_HEAD(&ifglist, ifg, entry); 423 } 424 425 if (ifg->count++ != 0) 426 /* already joined */ 427 return (0); 428 429 if_addr = LIST_FIRST(&iface->addr_list); 430 mreq.imr_multiaddr.s_addr = addr->s_addr; 431 mreq.imr_interface.s_addr = if_addr->addr.s_addr; 432 433 if (setsockopt(iface->discovery_fd, IPPROTO_IP, 434 IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) { 435 log_warn("if_join_group: error IP_ADD_MEMBERSHIP, " 436 "interface %s address %s", iface->name, 437 inet_ntoa(*addr)); 438 LIST_REMOVE(ifg, entry); 439 free(ifg); 440 return (-1); 441 } 442 return (0); 443} 444 445int 446if_leave_group(struct iface *iface, struct in_addr *addr) 447{ 448 struct ip_mreq mreq; 449 struct if_group_count *ifg; 450 struct if_addr *if_addr; 451 452 LIST_FOREACH(ifg, &ifglist, entry) 453 if (iface->ifindex == ifg->ifindex && 454 addr->s_addr == ifg->addr.s_addr) 455 break; 456 457 /* if interface is not found just try to drop membership */ 458 if (ifg) { 459 if (--ifg->count != 0) 460 /* others still joined */ 461 return (0); 462 463 LIST_REMOVE(ifg, entry); 464 free(ifg); 465 } 466 467 if_addr = LIST_FIRST(&iface->addr_list); 468 if (!if_addr) 469 return (0); 470 471 mreq.imr_multiaddr.s_addr = addr->s_addr; 472 mreq.imr_interface.s_addr = if_addr->addr.s_addr; 473 474 if (setsockopt(iface->discovery_fd, IPPROTO_IP, 475 IP_DROP_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) { 476 log_warn("if_leave_group: error IP_DROP_MEMBERSHIP, " 477 "interface %s address %s", iface->name, 478 inet_ntoa(*addr)); 479 return (-1); 480 } 481 482 return (0); 483} 484 485int 486if_set_mcast(struct iface *iface) 487{ 488 struct if_addr *if_addr; 489 490 if_addr = LIST_FIRST(&iface->addr_list); 491 492 if (setsockopt(iface->discovery_fd, IPPROTO_IP, IP_MULTICAST_IF, 493 &if_addr->addr.s_addr, sizeof(if_addr->addr.s_addr)) < 0) { 494 log_debug("if_set_mcast: error setting " 495 "IP_MULTICAST_IF, interface %s", iface->name); 496 return (-1); 497 } 498 499 return (0); 500} 501 502int 503if_set_mcast_loop(int fd) 504{ 505 u_int8_t loop = 0; 506 507 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, 508 (char *)&loop, sizeof(loop)) < 0) { 509 log_warn("if_set_mcast_loop: error setting IP_MULTICAST_LOOP"); 510 return (-1); 511 } 512 513 return (0); 514} 515