interface.c revision 1.17
1/* $OpenBSD: interface.c,v 1.17 2013/06/04 02:25:28 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 mtu, index and flags */ 157 iface->mtu = kif->mtu; 158 iface->ifindex = kif->ifindex; 159 iface->flags = kif->flags; 160 iface->linkstate = kif->link_state; 161 iface->media_type = kif->media_type; 162 iface->baudrate = kif->baudrate; 163 164 return (iface); 165} 166 167void 168if_del(struct iface *iface) 169{ 170 struct adj *adj; 171 struct if_addr *if_addr; 172 173 log_debug("if_del: interface %s", iface->name); 174 175 if_stop_hello_timer(iface); 176 177 while ((adj = LIST_FIRST(&iface->adj_list)) != NULL) { 178 LIST_REMOVE(adj, iface_entry); 179 adj_del(adj); 180 } 181 while ((if_addr = LIST_FIRST(&iface->addr_list)) != NULL) 182 LIST_REMOVE(if_addr, iface_entry); 183 184 free(iface); 185} 186 187void 188if_init(struct ldpd_conf *xconf, struct iface *iface) 189{ 190 /* set event handlers for interface */ 191 evtimer_set(&iface->hello_timer, if_hello_timer, iface); 192 193 iface->discovery_fd = xconf->ldp_discovery_socket; 194} 195 196struct iface * 197if_lookup(u_short ifindex) 198{ 199 struct iface *iface; 200 201 LIST_FOREACH(iface, &leconf->iface_list, entry) 202 if (iface->ifindex == ifindex) 203 return (iface); 204 205 return (NULL); 206} 207 208/* timers */ 209/* ARGSUSED */ 210void 211if_hello_timer(int fd, short event, void *arg) 212{ 213 struct iface *iface = arg; 214 struct timeval tv; 215 216 send_hello(HELLO_LINK, iface, NULL); 217 218 /* reschedule hello_timer */ 219 timerclear(&tv); 220 tv.tv_sec = iface->hello_interval; 221 if (evtimer_add(&iface->hello_timer, &tv) == -1) 222 fatal("if_hello_timer"); 223} 224 225void 226if_start_hello_timer(struct iface *iface) 227{ 228 struct timeval tv; 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 tv, now, res; 308 309 memcpy(ictl.name, iface->name, sizeof(ictl.name)); 310 ictl.rtr_id.s_addr = ldpe_router_id(); 311 ictl.ifindex = iface->ifindex; 312 ictl.state = iface->state; 313 ictl.mtu = iface->mtu; 314 ictl.baudrate = iface->baudrate; 315 ictl.holdtime = iface->hello_holdtime; 316 ictl.hello_interval = iface->hello_interval; 317 ictl.flags = iface->flags; 318 ictl.type = iface->type; 319 ictl.linkstate = iface->linkstate; 320 ictl.mediatype = iface->media_type; 321 ictl.priority = iface->priority; 322 323 gettimeofday(&now, NULL); 324 if (evtimer_pending(&iface->hello_timer, &tv)) { 325 timersub(&tv, &now, &res); 326 ictl.hello_timer = res.tv_sec; 327 } else 328 ictl.hello_timer = -1; 329 330 if (iface->state != IF_STA_DOWN && 331 iface->uptime != 0) { 332 ictl.uptime = now.tv_sec - iface->uptime; 333 } else 334 ictl.uptime = 0; 335 336 return (&ictl); 337} 338 339/* misc */ 340int 341if_set_mcast_ttl(int fd, u_int8_t ttl) 342{ 343 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, 344 (char *)&ttl, sizeof(ttl)) < 0) { 345 log_warn("if_set_mcast_ttl: error setting " 346 "IP_MULTICAST_TTL to %d", ttl); 347 return (-1); 348 } 349 350 return (0); 351} 352 353int 354if_set_tos(int fd, int tos) 355{ 356 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (int *)&tos, sizeof(tos)) < 0) { 357 log_warn("if_set_tos: error setting IP_TOS to 0x%x", tos); 358 return (-1); 359 } 360 361 return (0); 362} 363 364int 365if_set_recvif(int fd, int enable) 366{ 367 if (setsockopt(fd, IPPROTO_IP, IP_RECVIF, &enable, 368 sizeof(enable)) < 0) { 369 log_warn("if_set_recvif: error setting IP_RECVIF"); 370 return (-1); 371 } 372 return (0); 373} 374 375void 376if_set_recvbuf(int fd) 377{ 378 int bsize; 379 380 bsize = 65535; 381 while (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bsize, 382 sizeof(bsize)) == -1) 383 bsize /= 2; 384} 385 386int 387if_set_reuse(int fd, int enable) 388{ 389 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, 390 sizeof(int)) < 0) { 391 log_warn("if_set_reuse: error setting SO_REUSEADDR"); 392 return (-1); 393 } 394 395 return (0); 396} 397 398/* 399 * only one JOIN or DROP per interface and address is allowed so we need 400 * to keep track of what is added and removed. 401 */ 402struct if_group_count { 403 LIST_ENTRY(if_group_count) entry; 404 struct in_addr addr; 405 unsigned int ifindex; 406 int count; 407}; 408 409LIST_HEAD(,if_group_count) ifglist = LIST_HEAD_INITIALIZER(ifglist); 410 411int 412if_join_group(struct iface *iface, struct in_addr *addr) 413{ 414 struct ip_mreq mreq; 415 struct if_group_count *ifg; 416 struct if_addr *if_addr; 417 418 LIST_FOREACH(ifg, &ifglist, entry) 419 if (iface->ifindex == ifg->ifindex && 420 addr->s_addr == ifg->addr.s_addr) 421 break; 422 if (ifg == NULL) { 423 if ((ifg = calloc(1, sizeof(*ifg))) == NULL) 424 fatal("if_join_group"); 425 ifg->addr.s_addr = addr->s_addr; 426 ifg->ifindex = iface->ifindex; 427 LIST_INSERT_HEAD(&ifglist, ifg, entry); 428 } 429 430 if (ifg->count++ != 0) 431 /* already joined */ 432 return (0); 433 434 if_addr = LIST_FIRST(&iface->addr_list); 435 mreq.imr_multiaddr.s_addr = addr->s_addr; 436 mreq.imr_interface.s_addr = if_addr->addr.s_addr; 437 438 if (setsockopt(iface->discovery_fd, IPPROTO_IP, 439 IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) { 440 log_warn("if_join_group: error IP_ADD_MEMBERSHIP, " 441 "interface %s address %s", iface->name, 442 inet_ntoa(*addr)); 443 LIST_REMOVE(ifg, entry); 444 free(ifg); 445 return (-1); 446 } 447 return (0); 448} 449 450int 451if_leave_group(struct iface *iface, struct in_addr *addr) 452{ 453 struct ip_mreq mreq; 454 struct if_group_count *ifg; 455 struct if_addr *if_addr; 456 457 LIST_FOREACH(ifg, &ifglist, entry) 458 if (iface->ifindex == ifg->ifindex && 459 addr->s_addr == ifg->addr.s_addr) 460 break; 461 462 /* if interface is not found just try to drop membership */ 463 if (ifg) { 464 if (--ifg->count != 0) 465 /* others still joined */ 466 return (0); 467 468 LIST_REMOVE(ifg, entry); 469 free(ifg); 470 } 471 472 if_addr = LIST_FIRST(&iface->addr_list); 473 if (!if_addr) 474 return (0); 475 476 mreq.imr_multiaddr.s_addr = addr->s_addr; 477 mreq.imr_interface.s_addr = if_addr->addr.s_addr; 478 479 if (setsockopt(iface->discovery_fd, IPPROTO_IP, 480 IP_DROP_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) { 481 log_warn("if_leave_group: error IP_DROP_MEMBERSHIP, " 482 "interface %s address %s", iface->name, 483 inet_ntoa(*addr)); 484 return (-1); 485 } 486 487 return (0); 488} 489 490int 491if_set_mcast(struct iface *iface) 492{ 493 struct if_addr *if_addr; 494 495 if_addr = LIST_FIRST(&iface->addr_list); 496 497 if (setsockopt(iface->discovery_fd, IPPROTO_IP, IP_MULTICAST_IF, 498 &if_addr->addr.s_addr, sizeof(if_addr->addr.s_addr)) < 0) { 499 log_debug("if_set_mcast: error setting " 500 "IP_MULTICAST_IF, interface %s", iface->name); 501 return (-1); 502 } 503 504 return (0); 505} 506 507int 508if_set_mcast_loop(int fd) 509{ 510 u_int8_t loop = 0; 511 512 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, 513 (char *)&loop, sizeof(loop)) < 0) { 514 log_warn("if_set_mcast_loop: error setting IP_MULTICAST_LOOP"); 515 return (-1); 516 } 517 518 return (0); 519} 520