interface.c revision 1.21
1/* $OpenBSD: interface.c,v 1.21 2015/03/21 18:25:08 renato 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 if_addr *if_addr; 169 170 if (iface->state == IF_STA_ACTIVE) 171 if_act_reset(iface); 172 173 log_debug("if_del: interface %s", iface->name); 174 175 while ((if_addr = LIST_FIRST(&iface->addr_list)) != NULL) 176 LIST_REMOVE(if_addr, iface_entry); 177 178 free(iface); 179} 180 181void 182if_init(struct ldpd_conf *xconf, struct iface *iface) 183{ 184 /* set event handlers for interface */ 185 evtimer_set(&iface->hello_timer, if_hello_timer, iface); 186 187 iface->discovery_fd = xconf->ldp_discovery_socket; 188} 189 190struct iface * 191if_lookup(u_short ifindex) 192{ 193 struct iface *iface; 194 195 LIST_FOREACH(iface, &leconf->iface_list, entry) 196 if (iface->ifindex == ifindex) 197 return (iface); 198 199 return (NULL); 200} 201 202/* timers */ 203/* ARGSUSED */ 204void 205if_hello_timer(int fd, short event, void *arg) 206{ 207 struct iface *iface = arg; 208 struct timeval tv; 209 210 send_hello(HELLO_LINK, iface, NULL); 211 212 /* reschedule hello_timer */ 213 timerclear(&tv); 214 tv.tv_sec = iface->hello_interval; 215 if (evtimer_add(&iface->hello_timer, &tv) == -1) 216 fatal("if_hello_timer"); 217} 218 219void 220if_start_hello_timer(struct iface *iface) 221{ 222 struct timeval tv; 223 224 send_hello(HELLO_LINK, iface, NULL); 225 226 timerclear(&tv); 227 tv.tv_sec = iface->hello_interval; 228 if (evtimer_add(&iface->hello_timer, &tv) == -1) 229 fatal("if_start_hello_timer"); 230} 231 232void 233if_stop_hello_timer(struct iface *iface) 234{ 235 if (evtimer_pending(&iface->hello_timer, NULL) && 236 evtimer_del(&iface->hello_timer) == -1) 237 fatal("if_stop_hello_timer"); 238} 239 240/* actions */ 241int 242if_act_start(struct iface *iface) 243{ 244 struct in_addr addr; 245 struct timeval now; 246 247 gettimeofday(&now, NULL); 248 iface->uptime = now.tv_sec; 249 250 inet_aton(AllRouters, &addr); 251 if (if_join_group(iface, &addr)) 252 return (-1); 253 254 /* hello timer needs to be started in any case */ 255 if_start_hello_timer(iface); 256 return (0); 257} 258 259int 260if_act_reset(struct iface *iface) 261{ 262 struct in_addr addr; 263 struct adj *adj; 264 265 while ((adj = LIST_FIRST(&iface->adj_list)) != NULL) { 266 LIST_REMOVE(adj, iface_entry); 267 adj_del(adj); 268 } 269 270 if_stop_hello_timer(iface); 271 272 /* try to cleanup */ 273 inet_aton(AllRouters, &addr); 274 if_leave_group(iface, &addr); 275 276 return (0); 277} 278 279int 280if_act_update(struct iface *iface) 281{ 282 int ret; 283 284 if (iface->state == IF_STA_DOWN) { 285 if (!((iface->flags & IFF_UP) && 286 LINK_STATE_IS_UP(iface->linkstate))) 287 return (0); 288 289 if (LIST_EMPTY(&iface->addr_list)) 290 return (0); 291 292 iface->state = IF_STA_ACTIVE; 293 ret = if_act_start(iface); 294 } else { 295 if (!LIST_EMPTY(&iface->addr_list)) 296 return (0); 297 298 iface->state = IF_STA_DOWN; 299 ret = if_act_reset(iface); 300 } 301 302 return (ret); 303} 304 305struct ctl_iface * 306if_to_ctl(struct iface *iface) 307{ 308 static struct ctl_iface ictl; 309 struct timeval now; 310 struct adj *adj; 311 312 memcpy(ictl.name, iface->name, sizeof(ictl.name)); 313 ictl.ifindex = iface->ifindex; 314 ictl.state = iface->state; 315 ictl.hello_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 322 gettimeofday(&now, NULL); 323 if (iface->state != IF_STA_DOWN && 324 iface->uptime != 0) { 325 ictl.uptime = now.tv_sec - iface->uptime; 326 } else 327 ictl.uptime = 0; 328 329 ictl.adj_cnt = 0; 330 LIST_FOREACH(adj, &iface->adj_list, iface_entry) 331 ictl.adj_cnt++; 332 333 return (&ictl); 334} 335 336/* misc */ 337int 338if_set_mcast_ttl(int fd, u_int8_t ttl) 339{ 340 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, 341 (char *)&ttl, sizeof(ttl)) < 0) { 342 log_warn("if_set_mcast_ttl: error setting " 343 "IP_MULTICAST_TTL to %d", ttl); 344 return (-1); 345 } 346 347 return (0); 348} 349 350int 351if_set_tos(int fd, int tos) 352{ 353 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (int *)&tos, sizeof(tos)) < 0) { 354 log_warn("if_set_tos: error setting IP_TOS to 0x%x", tos); 355 return (-1); 356 } 357 358 return (0); 359} 360 361int 362if_set_recvif(int fd, int enable) 363{ 364 if (setsockopt(fd, IPPROTO_IP, IP_RECVIF, &enable, 365 sizeof(enable)) < 0) { 366 log_warn("if_set_recvif: error setting IP_RECVIF"); 367 return (-1); 368 } 369 return (0); 370} 371 372void 373if_set_recvbuf(int fd) 374{ 375 int bsize; 376 377 bsize = 65535; 378 while (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bsize, 379 sizeof(bsize)) == -1) 380 bsize /= 2; 381} 382 383int 384if_set_reuse(int fd, int enable) 385{ 386 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, 387 sizeof(int)) < 0) { 388 log_warn("if_set_reuse: error setting SO_REUSEADDR"); 389 return (-1); 390 } 391 392 return (0); 393} 394 395/* 396 * only one JOIN or DROP per interface and address is allowed so we need 397 * to keep track of what is added and removed. 398 */ 399struct if_group_count { 400 LIST_ENTRY(if_group_count) entry; 401 struct in_addr addr; 402 unsigned int ifindex; 403 int count; 404}; 405 406LIST_HEAD(,if_group_count) ifglist = LIST_HEAD_INITIALIZER(ifglist); 407 408int 409if_join_group(struct iface *iface, struct in_addr *addr) 410{ 411 struct ip_mreq mreq; 412 struct if_group_count *ifg; 413 struct if_addr *if_addr; 414 415 LIST_FOREACH(ifg, &ifglist, entry) 416 if (iface->ifindex == ifg->ifindex && 417 addr->s_addr == ifg->addr.s_addr) 418 break; 419 if (ifg == NULL) { 420 if ((ifg = calloc(1, sizeof(*ifg))) == NULL) 421 fatal("if_join_group"); 422 ifg->addr.s_addr = addr->s_addr; 423 ifg->ifindex = iface->ifindex; 424 LIST_INSERT_HEAD(&ifglist, ifg, entry); 425 } 426 427 if (ifg->count++ != 0) 428 /* already joined */ 429 return (0); 430 431 if_addr = LIST_FIRST(&iface->addr_list); 432 mreq.imr_multiaddr.s_addr = addr->s_addr; 433 mreq.imr_interface.s_addr = if_addr->addr.s_addr; 434 435 if (setsockopt(iface->discovery_fd, IPPROTO_IP, 436 IP_ADD_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) { 437 log_warn("if_join_group: error IP_ADD_MEMBERSHIP, " 438 "interface %s address %s", iface->name, 439 inet_ntoa(*addr)); 440 LIST_REMOVE(ifg, entry); 441 free(ifg); 442 return (-1); 443 } 444 return (0); 445} 446 447int 448if_leave_group(struct iface *iface, struct in_addr *addr) 449{ 450 struct ip_mreq mreq; 451 struct if_group_count *ifg; 452 struct if_addr *if_addr; 453 454 LIST_FOREACH(ifg, &ifglist, entry) 455 if (iface->ifindex == ifg->ifindex && 456 addr->s_addr == ifg->addr.s_addr) 457 break; 458 459 /* if interface is not found just try to drop membership */ 460 if (ifg) { 461 if (--ifg->count != 0) 462 /* others still joined */ 463 return (0); 464 465 LIST_REMOVE(ifg, entry); 466 free(ifg); 467 } 468 469 if_addr = LIST_FIRST(&iface->addr_list); 470 if (!if_addr) 471 return (0); 472 473 mreq.imr_multiaddr.s_addr = addr->s_addr; 474 mreq.imr_interface.s_addr = if_addr->addr.s_addr; 475 476 if (setsockopt(iface->discovery_fd, IPPROTO_IP, 477 IP_DROP_MEMBERSHIP, (void *)&mreq, sizeof(mreq)) < 0) { 478 log_warn("if_leave_group: error IP_DROP_MEMBERSHIP, " 479 "interface %s address %s", iface->name, 480 inet_ntoa(*addr)); 481 return (-1); 482 } 483 484 return (0); 485} 486 487int 488if_set_mcast(struct iface *iface) 489{ 490 struct if_addr *if_addr; 491 492 if_addr = LIST_FIRST(&iface->addr_list); 493 494 if (setsockopt(iface->discovery_fd, IPPROTO_IP, IP_MULTICAST_IF, 495 &if_addr->addr.s_addr, sizeof(if_addr->addr.s_addr)) < 0) { 496 log_debug("if_set_mcast: error setting " 497 "IP_MULTICAST_IF, interface %s", iface->name); 498 return (-1); 499 } 500 501 return (0); 502} 503 504int 505if_set_mcast_loop(int fd) 506{ 507 u_int8_t loop = 0; 508 509 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, 510 (char *)&loop, sizeof(loop)) < 0) { 511 log_warn("if_set_mcast_loop: error setting IP_MULTICAST_LOOP"); 512 return (-1); 513 } 514 515 return (0); 516} 517