1 /* 2 * netifd - network interface daemon 3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org> 4 * Copyright (C) 2012 Steven Barth <steven@midlink.org> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 8 * as published by the Free Software Foundation 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 */ 15 #include <string.h> 16 #include <stdlib.h> 17 #include <stdio.h> 18 #include <libgen.h> 19 #include <sys/stat.h> 20 21 #include <limits.h> 22 #include <arpa/inet.h> 23 #include <netinet/in.h> 24 25 #include "netifd.h" 26 #include "device.h" 27 #include "interface.h" 28 #include "interface-ip.h" 29 #include "proto.h" 30 #include "ubus.h" 31 #include "system.h" 32 33 enum { 34 ROUTE_INTERFACE, 35 ROUTE_TARGET, 36 ROUTE_MASK, 37 ROUTE_GATEWAY, 38 ROUTE_METRIC, 39 ROUTE_MTU, 40 ROUTE_VALID, 41 ROUTE_TABLE, 42 ROUTE_SOURCE, 43 ROUTE_ONLINK, 44 ROUTE_TYPE, 45 ROUTE_PROTO, 46 ROUTE_DISABLED, 47 __ROUTE_MAX 48 }; 49 50 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = { 51 [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING }, 52 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING }, 53 [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING }, 54 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING }, 55 [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 }, 56 [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 }, 57 [ROUTE_TABLE] = { .name = "table", .type = BLOBMSG_TYPE_STRING }, 58 [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 }, 59 [ROUTE_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_STRING }, 60 [ROUTE_ONLINK] = { .name = "onlink", .type = BLOBMSG_TYPE_BOOL }, 61 [ROUTE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING }, 62 [ROUTE_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING }, 63 [ROUTE_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL }, 64 }; 65 66 const struct uci_blob_param_list route_attr_list = { 67 .n_params = __ROUTE_MAX, 68 .params = route_attr, 69 }; 70 71 enum { 72 NEIGHBOR_INTERFACE, 73 NEIGHBOR_ADDRESS, 74 NEIGHBOR_MAC, 75 NEIGHBOR_PROXY, 76 NEIGHBOR_ROUTER, 77 __NEIGHBOR_MAX 78 }; 79 80 static const struct blobmsg_policy neighbor_attr[__NEIGHBOR_MAX]={ 81 [NEIGHBOR_INTERFACE]= { .name = "interface", .type = BLOBMSG_TYPE_STRING}, 82 [NEIGHBOR_ADDRESS]= { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING}, 83 [NEIGHBOR_MAC]= { .name = "mac", .type = BLOBMSG_TYPE_STRING}, 84 [NEIGHBOR_PROXY]= { .name = "proxy", .type = BLOBMSG_TYPE_BOOL}, 85 [NEIGHBOR_ROUTER]= {.name = "router", .type = BLOBMSG_TYPE_BOOL}, 86 }; 87 88 const struct uci_blob_param_list neighbor_attr_list = { 89 .n_params = __NEIGHBOR_MAX, 90 .params = neighbor_attr, 91 }; 92 93 94 struct list_head prefixes = LIST_HEAD_INIT(prefixes); 95 static struct device_prefix *ula_prefix = NULL; 96 static struct uloop_timeout valid_until_timeout; 97 98 99 static void 100 clear_if_addr(union if_addr *a, int mask) 101 { 102 size_t m_bytes = (mask + 7) / 8; 103 uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1; 104 uint8_t *p = (uint8_t *) a; 105 106 if (m_bytes < sizeof(*a)) 107 memset(p + m_bytes, 0, sizeof(*a) - m_bytes); 108 109 if (m_bytes) 110 p[m_bytes - 1] &= ~m_clear; 111 } 112 113 static bool 114 addr_is_offlink(struct device *dev, struct device_addr *addr) 115 { 116 /* 117 * The offlink mechanism (suppress the kernel connected route and add an 118 * unreachable prefix route as a loop guard) only makes sense on shared 119 * segments. A point-to-point link has a single peer and no loop risk, so 120 * the kernel connected route is the correct way to reach the prefix there. 121 * 122 * Query the device state live rather than caching it: this is only called 123 * while applying an address to a device that is already up, so the flag is 124 * guaranteed to be available, with none of the ordering races a cached 125 * value would introduce. 126 */ 127 return (addr->flags & DEVADDR_OFFLINK) && 128 !(dev && system_if_is_point_to_point(dev)); 129 } 130 131 static bool 132 match_if_addr(union if_addr *a1, union if_addr *a2, int mask) 133 { 134 union if_addr *p1, *p2; 135 136 p1 = alloca(sizeof(*a1)); 137 p2 = alloca(sizeof(*a2)); 138 139 memcpy(p1, a1, sizeof(*a1)); 140 clear_if_addr(p1, mask); 141 memcpy(p2, a2, sizeof(*a2)); 142 clear_if_addr(p2, mask); 143 144 return !memcmp(p1, p2, sizeof(*p1)); 145 } 146 147 static int set_ip_source_policy(bool add, bool v6, unsigned int priority, 148 const union if_addr *addr, uint8_t mask, unsigned int table, 149 struct interface *in_iface, const char *action, bool src) 150 { 151 struct iprule rule = { 152 .flags = IPRULE_PRIORITY, 153 .priority = priority 154 }; 155 156 if (addr) { 157 if (src) { 158 rule.flags |= IPRULE_SRC; 159 rule.src_addr = *addr; 160 rule.src_mask = mask; 161 } else { 162 rule.flags |= IPRULE_DEST; 163 rule.dest_addr = *addr; 164 rule.dest_mask = mask; 165 } 166 } 167 168 if (table) { 169 rule.flags |= IPRULE_LOOKUP; 170 rule.lookup = table; 171 172 if (!rule.lookup) 173 return 0; 174 } else if (action) { 175 rule.flags |= IPRULE_ACTION; 176 system_resolve_iprule_action(action, &rule.action); 177 } 178 179 if (in_iface && in_iface->l3_dev.dev) { 180 rule.flags |= IPRULE_IN; 181 strcpy(rule.in_dev, in_iface->l3_dev.dev->ifname); 182 } 183 184 rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4; 185 186 return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule); 187 } 188 189 static int set_ip_lo_policy(bool add, bool v6, struct interface *iface) 190 { 191 struct iprule rule = { 192 .flags = IPRULE_IN | IPRULE_LOOKUP | IPRULE_PRIORITY, 193 .priority = IPRULE_PRIORITY_NW + iface->l3_dev.dev->ifindex, 194 .lookup = (v6) ? iface->ip6table : iface->ip4table, 195 .in_dev = "lo" 196 }; 197 198 if (!rule.lookup) 199 return 0; 200 201 rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4; 202 203 return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule); 204 } 205 206 static bool 207 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6) 208 { 209 struct device_addr *addr; 210 211 vlist_for_each_element(&ip->addr, addr, node) { 212 if (!addr->enabled) 213 continue; 214 215 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6)) 216 continue; 217 218 if (((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4) && 219 addr->point_to_point && a->in.s_addr == addr->point_to_point) 220 return true; 221 222 /* Handle offlink addresses correctly */ 223 unsigned int mask = addr->mask; 224 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6 && 225 (addr->flags & DEVADDR_OFFLINK)) 226 mask = 128; 227 228 if (!match_if_addr(&addr->addr, a, mask)) 229 continue; 230 231 return true; 232 } 233 234 return false; 235 } 236 237 static void 238 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a, 239 bool v6, struct device_route **res) 240 { 241 struct device_route *route; 242 243 vlist_for_each_element(&ip->route, route, node) { 244 if (!route->enabled) 245 continue; 246 247 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6)) 248 continue; 249 250 if (!match_if_addr(&route->addr, a, route->mask)) 251 continue; 252 253 if (route->flags & DEVROUTE_TABLE) 254 continue; 255 256 if (!*res || route->mask > (*res)->mask || 257 ((route->mask == (*res)->mask) && (route->flags & DEVROUTE_METRIC) 258 && (route->metric < (*res)->metric))) 259 *res = route; 260 } 261 } 262 263 static bool 264 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6) 265 { 266 return __find_ip_addr_target(&iface->proto_ip, a, v6) || 267 __find_ip_addr_target(&iface->config_ip, a, v6); 268 } 269 270 static void 271 interface_ip_find_route_target(struct interface *iface, union if_addr *a, 272 bool v6, struct device_route **route) 273 { 274 __find_ip_route_target(&iface->proto_ip, a, v6, route); 275 __find_ip_route_target(&iface->config_ip, a, v6, route); 276 } 277 278 struct interface * 279 interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *iface, 280 bool exclude) 281 { 282 struct device_route *route, *r_next = NULL; 283 bool defaultroute_target = false; 284 union if_addr addr_zero; 285 int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in); 286 struct interface *exclude_iface = NULL; 287 288 if (exclude) { 289 exclude_iface = iface; 290 iface = NULL; 291 } 292 293 memset(&addr_zero, 0, sizeof(addr_zero)); 294 if (memcmp(&addr_zero, addr, addrsize) == 0) 295 defaultroute_target = true; 296 297 if (iface) { 298 /* look for locally addressable target first */ 299 if (interface_ip_find_addr_target(iface, addr, v6)) 300 return iface; 301 302 /* do not stop at the first route, let the lookup compare 303 * masks to find the best match */ 304 interface_ip_find_route_target(iface, addr, v6, &r_next); 305 } else { 306 vlist_for_each_element(&interfaces, iface, node) { 307 if (iface == exclude_iface) 308 continue; 309 310 /* look for locally addressable target first */ 311 if (interface_ip_find_addr_target(iface, addr, v6)) 312 return iface; 313 314 /* do not stop at the first route, let the lookup compare 315 * masks to find the best match */ 316 interface_ip_find_route_target(iface, addr, v6, &r_next); 317 } 318 } 319 320 if (!r_next) 321 return NULL; 322 323 iface = r_next->iface; 324 if (defaultroute_target) 325 return iface; 326 327 route = calloc(1, sizeof(*route)); 328 if (!route) 329 return NULL; 330 331 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4; 332 route->mask = v6 ? 128 : 32; 333 memcpy(&route->addr, addr, addrsize); 334 memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop)); 335 route->mtu = r_next->mtu; 336 route->metric = r_next->metric; 337 route->table = r_next->table; 338 route->iface = iface; 339 vlist_add(&iface->host_routes, &route->node, route); 340 341 return iface; 342 } 343 344 static void 345 interface_set_route_info(struct interface *iface, struct device_route *route) 346 { 347 bool v6 = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6); 348 349 if (!iface) 350 return; 351 352 if (!(route->flags & DEVROUTE_METRIC)) 353 route->metric = iface->metric; 354 355 if (!(route->flags & DEVROUTE_TABLE)) { 356 route->table = (v6) ? iface->ip6table : iface->ip4table; 357 if (route->table) 358 route->flags |= DEVROUTE_SRCTABLE; 359 } 360 } 361 362 void 363 interface_ip_add_neighbor(struct interface *iface, struct blob_attr *attr, bool v6) 364 { 365 struct interface_ip_settings *ip; 366 struct blob_attr *tb[__NEIGHBOR_MAX], *cur; 367 struct device_neighbor *neighbor; 368 int af = v6 ? AF_INET6: AF_INET; 369 struct ether_addr *ea; 370 371 blobmsg_parse_attr(neighbor_attr, __NEIGHBOR_MAX, tb, attr); 372 373 if (!iface) { 374 if ((cur = tb[NEIGHBOR_INTERFACE]) == NULL) 375 return; 376 377 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node); 378 379 if (!iface) 380 return; 381 382 ip = &iface->config_ip; 383 } else 384 ip = &iface->proto_ip; 385 386 neighbor = calloc(1,sizeof(*neighbor)); 387 if (!neighbor) 388 return; 389 390 neighbor->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4; 391 392 if ((cur = tb[NEIGHBOR_ADDRESS]) != NULL){ 393 if (!inet_pton(af, blobmsg_data(cur), &neighbor->addr)) 394 goto error; 395 } else 396 goto error; 397 398 if ((cur = tb[NEIGHBOR_MAC]) != NULL) { 399 neighbor->flags |= DEVNEIGH_MAC; 400 ea = ether_aton(blobmsg_data(cur)); 401 if (!ea) 402 goto error; 403 404 memcpy(neighbor->macaddr, ea, 6); 405 } 406 407 if ((cur = tb[NEIGHBOR_PROXY]) != NULL) 408 neighbor->proxy = blobmsg_get_bool(cur); 409 410 if ((cur = tb[NEIGHBOR_ROUTER]) != NULL) 411 neighbor->router = blobmsg_get_bool(cur); 412 413 vlist_add(&ip->neighbor, &neighbor->node, neighbor); 414 return; 415 416 error: 417 free(neighbor); 418 } 419 420 void 421 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6) 422 { 423 struct interface_ip_settings *ip; 424 struct blob_attr *tb[__ROUTE_MAX], *cur; 425 struct device_route *route; 426 int af = v6 ? AF_INET6 : AF_INET; 427 bool no_device = false; 428 429 blobmsg_parse_attr(route_attr, __ROUTE_MAX, tb, attr); 430 431 if ((cur = tb[ROUTE_DISABLED]) != NULL && blobmsg_get_bool(cur)) 432 return; 433 434 if (!iface) { 435 if ((cur = tb[ROUTE_INTERFACE]) == NULL) { 436 iface = vlist_find(&interfaces, "loopback", iface, node); 437 no_device = true; 438 } else { 439 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node); 440 } 441 442 if (!iface) 443 return; 444 445 ip = &iface->config_ip; 446 } else { 447 ip = &iface->proto_ip; 448 } 449 450 route = calloc(1, sizeof(*route)); 451 if (!route) 452 return; 453 454 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4; 455 route->mask = v6 ? 128 : 32; 456 if ((cur = tb[ROUTE_MASK]) != NULL) { 457 route->mask = parse_netmask_string(blobmsg_data(cur), v6); 458 if (route->mask > (v6 ? 128 : 32)) 459 goto error; 460 } 461 462 if ((cur = tb[ROUTE_TARGET]) != NULL) { 463 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) { 464 D(INTERFACE, "Failed to parse route target: %s", (char *) blobmsg_data(cur)); 465 goto error; 466 } 467 468 /* Mask out IPv4 host bits to avoid "Invalid prefix for given prefix length" */ 469 if (af == AF_INET && route->mask < 32) 470 clear_if_addr(&route->addr, route->mask); 471 } 472 473 if ((cur = tb[ROUTE_GATEWAY]) != NULL) { 474 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) { 475 D(INTERFACE, "Failed to parse route gateway: %s", (char *) blobmsg_data(cur)); 476 goto error; 477 } 478 } 479 480 if ((cur = tb[ROUTE_METRIC]) != NULL) { 481 route->metric = blobmsg_get_u32(cur); 482 route->flags |= DEVROUTE_METRIC; 483 } 484 485 if ((cur = tb[ROUTE_MTU]) != NULL) { 486 route->mtu = blobmsg_get_u32(cur); 487 route->flags |= DEVROUTE_MTU; 488 } 489 490 /* Use source-based routing */ 491 if ((cur = tb[ROUTE_SOURCE]) != NULL) { 492 char *saveptr, *source = alloca(blobmsg_data_len(cur)); 493 memcpy(source, blobmsg_data(cur), blobmsg_data_len(cur)); 494 495 const char *addr = strtok_r(source, "/", &saveptr); 496 const char *mask = strtok_r(NULL, "/", &saveptr); 497 498 if (!addr || inet_pton(af, addr, &route->source) < 1) { 499 D(INTERFACE, "Failed to parse route source: %s", addr ? addr : "NULL"); 500 goto error; 501 } 502 503 route->sourcemask = (mask) ? atoi(mask) : ((af == AF_INET6) ? 128 : 32); 504 } 505 506 if ((cur = tb[ROUTE_ONLINK]) != NULL && blobmsg_get_bool(cur)) 507 route->flags |= DEVROUTE_ONLINK; 508 509 if ((cur = tb[ROUTE_TABLE]) != NULL) { 510 if (!system_resolve_rt_table(blobmsg_data(cur), &route->table)) { 511 D(INTERFACE, "Failed to resolve routing table: %s", (char *) blobmsg_data(cur)); 512 goto error; 513 } 514 515 /* only set the table flag if not using the main (default) table */ 516 if (system_is_default_rt_table(route->table)) 517 route->table = 0; 518 519 if (route->table) 520 route->flags |= DEVROUTE_TABLE; 521 } 522 523 if ((cur = tb[ROUTE_VALID]) != NULL) { 524 int64_t valid = blobmsg_get_u32(cur); 525 int64_t valid_until = valid + (int64_t)system_get_rtime(); 526 if (valid_until <= LONG_MAX && valid != 0xffffffffLL) /* Catch overflow */ 527 route->valid_until = valid_until; 528 } 529 530 if ((cur = tb[ROUTE_TYPE]) != NULL) { 531 if (!system_resolve_rt_type(blobmsg_data(cur), &route->type)) { 532 D(INTERFACE, "Failed to resolve routing type: %s", (char *) blobmsg_data(cur)); 533 goto error; 534 } 535 route->flags |= DEVROUTE_TYPE; 536 } 537 538 if ((cur = tb[ROUTE_PROTO]) != NULL) { 539 if (!system_resolve_rt_proto(blobmsg_data(cur), &route->proto)) { 540 D(INTERFACE, "Failed to resolve proto type: %s", (char *) blobmsg_data(cur)); 541 goto error; 542 } 543 route->flags |= DEVROUTE_PROTO; 544 } 545 546 if (no_device) 547 route->flags |= DEVROUTE_NODEV; 548 else 549 interface_set_route_info(iface, route); 550 551 vlist_add(&ip->route, &route->node, route); 552 return; 553 554 error: 555 free(route); 556 } 557 558 static int 559 addr_cmp(const void *k1, const void *k2, void *ptr) 560 { 561 const struct device_addr *a1 = k1; 562 const struct device_addr *a2 = k2; 563 const int cmp_offset = offsetof(struct device_addr, flags); 564 const int cmp_size = sizeof(struct device_addr) - cmp_offset; 565 566 if (a1->index != a2->index) 567 return a1->index - a2->index; 568 return memcmp(k1+cmp_offset, k2+cmp_offset, cmp_size); 569 } 570 571 static int 572 neighbor_cmp(const void *k1, const void *k2, void *ptr) 573 { 574 const struct device_neighbor *n1 = k1, *n2 = k2; 575 576 return memcmp(&n1->addr, &n2->addr, sizeof(n2->addr)); 577 } 578 579 static int 580 route_cmp(const void *k1, const void *k2, void *ptr) 581 { 582 const struct device_route *r1 = k1, *r2 = k2; 583 584 if (r1->mask != r2->mask) 585 return r2->mask - r1->mask; 586 587 if (r1->metric != r2->metric) 588 return r1->metric - r2->metric; 589 590 if (r1->flags != r2->flags) 591 return r2->flags - r1->flags; 592 593 if (r1->sourcemask != r2->sourcemask) 594 return r1->sourcemask - r2->sourcemask; 595 596 if (r1->table != r2->table) 597 return r1->table - r2->table; 598 599 int maskcmp = memcmp(&r1->source, &r2->source, sizeof(r1->source)); 600 if (maskcmp) 601 return maskcmp; 602 603 return memcmp(&r1->addr, &r2->addr, sizeof(r1->addr)); 604 } 605 606 static int 607 prefix_cmp(const void *k1, const void *k2, void *ptr) 608 { 609 return memcmp(k1, k2, offsetof(struct device_prefix, pclass) - 610 offsetof(struct device_prefix, addr)); 611 } 612 613 static void 614 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add) 615 { 616 struct device *dev = iface->l3_dev.dev; 617 struct device_route *r = &addr->subnet; 618 619 if (addr_is_offlink(dev, addr)) 620 return; 621 622 if (!add) { 623 if (!addr->subnet.iface) 624 return; 625 626 system_del_route(dev, r); 627 memset(r, 0, sizeof(*r)); 628 return; 629 } 630 631 r->iface = iface; 632 r->flags = addr->flags; 633 r->mask = addr->mask; 634 memcpy(&r->addr, &addr->addr, sizeof(r->addr)); 635 clear_if_addr(&r->addr, r->mask); 636 637 if (!system_resolve_rt_proto("kernel", &r->proto)) 638 return; 639 640 r->flags |= DEVROUTE_PROTO; 641 system_del_route(dev, r); 642 643 r->flags &= ~DEVROUTE_PROTO; 644 interface_set_route_info(iface, r); 645 646 system_add_route(dev, r); 647 } 648 649 static void 650 interface_add_addr_rules(struct device_addr *addr, bool enabled) 651 { 652 bool v6 = (addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6; 653 654 set_ip_source_policy(enabled, v6, IPRULE_PRIORITY_ADDR, &addr->addr, 655 (v6) ? 128 : 32, addr->policy_table, NULL, NULL, 656 true); 657 set_ip_source_policy(enabled, v6, IPRULE_PRIORITY_ADDR_MASK, 658 &addr->addr, addr->mask, addr->policy_table, NULL, 659 NULL, false); 660 } 661 662 static void 663 interface_update_proto_addr(struct vlist_tree *tree, 664 struct vlist_node *node_new, 665 struct vlist_node *node_old) 666 { 667 struct interface_ip_settings *ip; 668 struct interface *iface; 669 struct device *dev; 670 struct device_addr *a_new = NULL, *a_old = NULL; 671 bool replace = false; 672 bool keep = false; 673 674 ip = container_of(tree, struct interface_ip_settings, addr); 675 iface = ip->iface; 676 dev = iface->l3_dev.dev; 677 678 if (!node_new || !node_old) 679 iface->updated |= IUF_ADDRESS; 680 681 if (node_new) { 682 a_new = container_of(node_new, struct device_addr, node); 683 684 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 && 685 !a_new->broadcast) { 686 687 /* /31 and /32 addressing need 255.255.255.255 688 * as broadcast address. */ 689 if (a_new->mask >= 31) { 690 a_new->broadcast = (uint32_t) ~0; 691 } else { 692 uint32_t mask = ~0; 693 uint32_t *a = (uint32_t *) &a_new->addr; 694 695 mask >>= a_new->mask; 696 a_new->broadcast = *a | htonl(mask); 697 } 698 } 699 } 700 701 if (node_old) 702 a_old = container_of(node_old, struct device_addr, node); 703 704 if (a_new && a_old) { 705 keep = true; 706 707 if (a_old->flags != a_new->flags || a_old->failed) 708 keep = false; 709 710 if (a_old->valid_until != a_new->valid_until || 711 a_old->preferred_until != a_new->preferred_until) 712 replace = true; 713 714 if (((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4) && 715 (a_new->broadcast != a_old->broadcast || 716 a_new->point_to_point != a_old->point_to_point)) 717 keep = false; 718 } 719 720 if (node_old) { 721 bool v6 = (a_old->flags & DEVADDR_FAMILY) == DEVADDR_INET6; 722 723 if (a_old->enabled && !keep) { 724 /* 725 * This is needed for source routing to work correctly. If a device 726 * has two connections to a network using the same subnet, adding 727 * only the network-rule will cause packets to be routed through the 728 * first matching network (source IP matches both masks) 729 */ 730 if (a_old->policy_table) 731 interface_add_addr_rules(a_old, false); 732 733 if (!(a_old->flags & DEVADDR_EXTERNAL)) { 734 interface_handle_subnet_route(iface, a_old, false); 735 system_del_address(dev, a_old); 736 737 if (addr_is_offlink(dev, a_old) && (a_old->mask < (v6 ? 128 : 32))) { 738 struct device_route route; 739 740 memset(&route, 0, sizeof(route)); 741 route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4; 742 route.metric = INT32_MAX; 743 route.mask = a_old->mask; 744 route.addr = a_old->addr; 745 746 clear_if_addr(&route.addr, route.mask); 747 748 /* Delete null-route */ 749 system_del_route(NULL, &route); 750 } 751 752 } 753 } 754 755 /* 756 * Keep the subnet route state so it can be deleted later. With 757 * replace set the add path rebuilds it instead, and the copied 758 * metric would poison the metric-agnostic delete of the 759 * kernel-created prefix route there 760 */ 761 if (keep && !replace) 762 a_new->subnet = a_old->subnet; 763 764 free(a_old->pclass); 765 free(a_old); 766 } 767 768 if (node_new) { 769 bool v6 = (a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET6; 770 771 a_new->enabled = true; 772 a_new->policy_table = (v6) ? iface->ip6table : iface->ip4table; 773 774 if (!keep || replace) { 775 if (!(a_new->flags & DEVADDR_EXTERNAL)) { 776 if (system_add_address(dev, a_new)) 777 a_new->failed = true; 778 779 if (iface->metric || a_new->policy_table) 780 interface_handle_subnet_route(iface, a_new, true); 781 } 782 783 if (!keep) { 784 if (!(a_new->flags & DEVADDR_EXTERNAL) && 785 addr_is_offlink(dev, a_new) && 786 (a_new->mask < (v6 ? 128 : 32))) { 787 struct device_route route; 788 789 memset(&route, 0, sizeof(route)); 790 route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4; 791 route.metric = INT32_MAX; 792 route.mask = a_new->mask; 793 route.addr = a_new->addr; 794 795 clear_if_addr(&route.addr, route.mask); 796 797 /* 798 * In case off link is specifed as address property 799 * add null-route to avoid routing loops 800 */ 801 system_add_route(NULL, &route); 802 } 803 804 if (a_new->policy_table) 805 interface_add_addr_rules(a_new, true); 806 } 807 } 808 } 809 } 810 811 static bool 812 enable_route(struct interface_ip_settings *ip, struct device_route *route) 813 { 814 if (ip->no_defaultroute && !route->mask) 815 return false; 816 817 return ip->enabled; 818 } 819 820 static void 821 interface_update_proto_neighbor(struct vlist_tree *tree, 822 struct vlist_node * node_new, 823 struct vlist_node *node_old) 824 { 825 struct device *dev; 826 struct device_neighbor *neighbor_old, *neighbor_new; 827 struct interface_ip_settings *ip; 828 bool keep = false; 829 830 ip = container_of(tree, struct interface_ip_settings, neighbor); 831 dev = ip->iface->l3_dev.dev; 832 833 neighbor_old = container_of(node_old, struct device_neighbor, node); 834 neighbor_new = container_of(node_new, struct device_neighbor, node); 835 836 if (node_old && node_new) { 837 keep = (!memcmp(neighbor_old->macaddr, neighbor_new->macaddr, sizeof(neighbor_old->macaddr)) && 838 (neighbor_old->proxy == neighbor_new->proxy) && 839 (neighbor_old->router == neighbor_new->router)); 840 } 841 842 if (node_old) { 843 if (!keep && neighbor_old->enabled) 844 system_del_neighbor(dev, neighbor_old); 845 846 free(neighbor_old); 847 } 848 849 if (node_new) { 850 if (!keep && ip->enabled) 851 if (system_add_neighbor(dev, neighbor_new)) 852 neighbor_new->failed = true; 853 854 neighbor_new->enabled = ip->enabled; 855 } 856 } 857 858 static void 859 __interface_update_route(struct interface_ip_settings *ip, 860 struct vlist_node *node_new, 861 struct vlist_node *node_old) 862 { 863 struct interface *iface = ip->iface; 864 struct device *dev; 865 struct device_route *route_old, *route_new; 866 bool keep = false; 867 868 dev = iface->l3_dev.dev; 869 870 if (!node_new || !node_old) 871 iface->updated |= IUF_ROUTE; 872 873 route_old = container_of(node_old, struct device_route, node); 874 route_new = container_of(node_new, struct device_route, node); 875 876 if (node_old && node_new) 877 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop)) && 878 (route_old->mtu == route_new->mtu) && (route_old->type == route_new->type) && 879 (route_old->proto == route_new->proto) && !route_old->failed; 880 881 if (node_old) { 882 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep) 883 system_del_route(dev, route_old); 884 885 free(route_old); 886 } 887 888 if (node_new) { 889 bool _enabled = enable_route(ip, route_new); 890 891 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled) 892 if (system_add_route(dev, route_new)) 893 route_new->failed = true; 894 895 route_new->iface = iface; 896 route_new->enabled = _enabled; 897 } 898 } 899 900 static void 901 interface_update_proto_route(struct vlist_tree *tree, 902 struct vlist_node *node_new, 903 struct vlist_node *node_old) 904 { 905 struct interface_ip_settings *ip; 906 907 ip = container_of(tree, struct interface_ip_settings, route); 908 __interface_update_route(ip, node_new, node_old); 909 } 910 911 static void 912 interface_update_host_route(struct vlist_tree *tree, 913 struct vlist_node *node_new, 914 struct vlist_node *node_old) 915 { 916 struct interface *iface; 917 918 iface = container_of(tree, struct interface, host_routes); 919 __interface_update_route(&iface->proto_ip, node_new, node_old); 920 } 921 922 static void 923 random_ifaceid(struct in6_addr *addr) 924 { 925 static bool initialized = false; 926 struct timeval t; 927 928 if (!initialized) { 929 long int seed = 0; 930 gettimeofday(&t, NULL); 931 seed = t.tv_sec ^ t.tv_usec ^ getpid(); 932 srand48(seed); 933 initialized = true; 934 } 935 addr->s6_addr32[2] = (uint32_t)mrand48(); 936 addr->s6_addr32[3] = (uint32_t)mrand48(); 937 } 938 939 static bool 940 eui64_ifaceid(struct interface *iface, struct in6_addr *addr) 941 { 942 struct device_settings st; 943 944 device_merge_settings(iface->l3_dev.dev, &st); 945 946 if (!(st.flags & DEV_OPT_MACADDR)) 947 return false; 948 949 /* get mac address */ 950 uint8_t *ifaceid = addr->s6_addr + 8; 951 memcpy(ifaceid, st.macaddr, 3); 952 memcpy(ifaceid + 5, st.macaddr + 3, 3); 953 ifaceid[3] = 0xff; 954 ifaceid[4] = 0xfe; 955 ifaceid[0] ^= 0x02; 956 957 return true; 958 } 959 960 static bool 961 generate_ifaceid(struct interface *iface, struct in6_addr *addr) 962 { 963 bool ret = true; 964 965 /* generate new iface id */ 966 switch (iface->assignment_iface_id_selection) { 967 case IFID_FIXED: 968 /* fixed */ 969 /* copy host part from assignment_fixed_iface_id */ 970 memcpy(addr->s6_addr + 8, iface->assignment_fixed_iface_id.s6_addr + 8, 8); 971 break; 972 case IFID_RANDOM: 973 /* randomize last 64 bits */ 974 random_ifaceid(addr); 975 break; 976 case IFID_EUI64: 977 /* eui64 */ 978 ret = eui64_ifaceid(iface, addr); 979 break; 980 default: 981 ret = false; 982 break; 983 } 984 return ret; 985 } 986 987 static void 988 interface_set_prefix_address(struct device_prefix_assignment *assignment, 989 const struct device_prefix *prefix, struct interface *iface, bool add) 990 { 991 const struct interface *uplink = prefix->iface; 992 if (!iface->l3_dev.dev) 993 return; 994 995 struct device *l3_downlink = iface->l3_dev.dev; 996 997 struct device_addr addr; 998 struct device_route route; 999 memset(&addr, 0, sizeof(addr)); 1000 memset(&route, 0, sizeof(route)); 1001 1002 addr.addr.in6 = assignment->addr; 1003 addr.mask = assignment->length; 1004 addr.flags = DEVADDR_INET6; 1005 addr.preferred_until = prefix->preferred_until; 1006 addr.valid_until = prefix->valid_until; 1007 1008 route.flags = DEVADDR_INET6; 1009 route.mask = addr.mask < 64 ? 64 : addr.mask; 1010 route.addr = addr.addr; 1011 1012 if (!add && assignment->enabled) { 1013 time_t now = system_get_rtime(); 1014 1015 if (addr.valid_until && addr.valid_until - 1 <= now) { 1016 addr.valid_until = 0; 1017 addr.preferred_until = 0; 1018 } else { 1019 /* Address is still valid; pass its ownership to kernel (see L-14 RFC 7084). */ 1020 addr.preferred_until = now; 1021 1022 if (!addr.valid_until || addr.valid_until > now + 7200) 1023 addr.valid_until = now + 7200; 1024 } 1025 1026 if (iface->ip6table) 1027 set_ip_source_policy(false, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr, 1028 addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false); 1029 1030 if (prefix->iface) { 1031 if (prefix->iface->ip6table) 1032 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW, &addr.addr, 1033 addr.mask, prefix->iface->ip6table, iface, NULL, true); 1034 1035 set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT, &addr.addr, 1036 addr.mask, 0, iface, "unreachable", true); 1037 } 1038 1039 clear_if_addr(&route.addr, route.mask); 1040 interface_set_route_info(iface, &route); 1041 1042 system_del_route(l3_downlink, &route); 1043 if (addr.valid_until) 1044 system_add_address(l3_downlink, &addr); 1045 else 1046 system_del_address(l3_downlink, &addr); 1047 1048 assignment->addr = in6addr_any; 1049 assignment->enabled = false; 1050 } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP)) { 1051 if (IN6_IS_ADDR_UNSPECIFIED(&addr.addr.in6)) { 1052 addr.addr.in6 = prefix->addr; 1053 addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned); 1054 if (!generate_ifaceid(iface, &addr.addr.in6)) 1055 return; 1056 1057 assignment->addr = addr.addr.in6; 1058 route.addr = addr.addr; 1059 } 1060 1061 addr.flags |= DEVADDR_OFFLINK; 1062 if (system_add_address(l3_downlink, &addr)) 1063 return; 1064 1065 if (!assignment->enabled) { 1066 if (iface->ip6table) 1067 set_ip_source_policy(true, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr, 1068 addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false); 1069 1070 if (prefix->iface) { 1071 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT, &addr.addr, 1072 addr.mask, 0, iface, "unreachable", true); 1073 1074 if (prefix->iface->ip6table) 1075 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW, &addr.addr, 1076 addr.mask, prefix->iface->ip6table, iface, NULL, true); 1077 } 1078 } 1079 1080 clear_if_addr(&route.addr, route.mask); 1081 interface_set_route_info(iface, &route); 1082 1083 system_add_route(l3_downlink, &route); 1084 1085 if (uplink && uplink->l3_dev.dev && !(l3_downlink->settings.flags & DEV_OPT_MTU6)) { 1086 int mtu = system_update_ipv6_mtu(uplink->l3_dev.dev, 0); 1087 int mtu_old = system_update_ipv6_mtu(l3_downlink, 0); 1088 1089 if (mtu > 0 && mtu_old != mtu) { 1090 if (system_update_ipv6_mtu(l3_downlink, mtu) < 0 && mtu < mtu_old) 1091 netifd_log_message(L_WARNING, "Failed to set IPv6 mtu to %d " 1092 "on interface '%s'\n", mtu, iface->name); 1093 } 1094 } 1095 1096 assignment->enabled = true; 1097 } 1098 } 1099 1100 /* 1101 * Size of a sub-prefix in /64 units, saturated to the representable 1102 * assignment space: prefixes shorter than /34 exceed the int32_t offset 1103 * range used for assignments 1104 */ 1105 static int32_t prefix_assignment_space(uint8_t length) 1106 { 1107 if (length < 34) 1108 return INT32_MAX; 1109 1110 return 1 << (64 - length); 1111 } 1112 1113 static bool interface_prefix_assign(struct list_head *list, 1114 struct device_prefix_assignment *assign) 1115 { 1116 int32_t asize = (1 << (64 - assign->length)) - 1; 1117 int64_t current = 0; 1118 struct device_prefix_assignment *c; 1119 1120 list_for_each_entry(c, list, head) { 1121 if (assign->assigned != -1) { 1122 if (assign->assigned >= current && assign->assigned + (int64_t)asize < c->assigned) { 1123 list_add_tail(&assign->head, &c->head); 1124 return true; 1125 } 1126 } else if (assign->assigned == -1) { 1127 current = (current + asize) & ~(int64_t)asize; 1128 if (current + asize < c->assigned) { 1129 assign->assigned = current; 1130 list_add_tail(&assign->head, &c->head); 1131 return true; 1132 } 1133 } 1134 current = (int64_t)c->assigned + prefix_assignment_space(c->length); 1135 } 1136 return false; 1137 } 1138 1139 /* 1140 * Sorting of assignment entries: 1141 * Primary on assignment length: smallest assignment first 1142 * Secondary on assignment weight: highest weight first 1143 * Finally alphabetical order of interface names 1144 */ 1145 static int prefix_assignment_cmp(const void *k1, const void *k2, void *ptr) 1146 { 1147 const struct device_prefix_assignment *a1 = k1, *a2 = k2; 1148 1149 if (a1->length != a2->length) 1150 return a1->length - a2->length; 1151 1152 if (a1->weight != a2->weight) 1153 return a2->weight - a1->weight; 1154 1155 return strcmp(a1->name, a2->name); 1156 } 1157 1158 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup) 1159 { 1160 struct device_prefix_assignment *c; 1161 struct interface *iface; 1162 1163 /* Delete all assignments */ 1164 while (!list_empty(&prefix->assignments)) { 1165 c = list_first_entry(&prefix->assignments, 1166 struct device_prefix_assignment, head); 1167 if ((iface = vlist_find(&interfaces, c->name, iface, node))) 1168 interface_set_prefix_address(c, prefix, iface, false); 1169 list_del(&c->head); 1170 free(c); 1171 } 1172 1173 if (!setup) 1174 return; 1175 1176 /* End-of-assignment sentinel */ 1177 c = malloc(sizeof(*c) + 1); 1178 if (!c) 1179 return; 1180 1181 c->assigned = prefix_assignment_space(prefix->length); 1182 c->length = 64; 1183 c->name[0] = 0; 1184 c->addr = in6addr_any; 1185 list_add(&c->head, &prefix->assignments); 1186 1187 /* Excluded prefix */ 1188 if (prefix->excl_length > 0) { 1189 const char name[] = "!excluded"; 1190 c = malloc(sizeof(*c) + sizeof(name)); 1191 if (c) { 1192 int32_t mask = prefix_assignment_space(prefix->length); 1193 1194 if (mask != INT32_MAX) 1195 mask -= 1; 1196 1197 c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) & mask; 1198 c->length = prefix->excl_length; 1199 c->addr = in6addr_any; 1200 memcpy(c->name, name, sizeof(name)); 1201 list_add(&c->head, &prefix->assignments); 1202 } 1203 } 1204 1205 bool assigned_any = false; 1206 struct { 1207 struct avl_node node; 1208 } *entry, *n_entry; 1209 struct avl_tree assign_later; 1210 1211 avl_init(&assign_later, prefix_assignment_cmp, false, NULL); 1212 1213 vlist_for_each_element(&interfaces, iface, node) { 1214 if (iface->assignment_length < 48 || 1215 iface->assignment_length > 64) 1216 continue; 1217 1218 /* Test whether there is a matching class */ 1219 if (!list_empty(&iface->assignment_classes)) { 1220 bool found = false; 1221 1222 struct interface_assignment_class *c; 1223 list_for_each_entry(c, &iface->assignment_classes, head) { 1224 if (!strcmp(c->name, prefix->pclass)) { 1225 found = true; 1226 break; 1227 } 1228 } 1229 1230 if (!found) 1231 continue; 1232 } 1233 1234 size_t namelen = strlen(iface->name) + 1; 1235 c = malloc(sizeof(*c) + namelen); 1236 if (!c) 1237 continue; 1238 1239 c->length = iface->assignment_length; 1240 c->assigned = iface->assignment_hint; 1241 c->weight = iface->assignment_weight; 1242 c->addr = in6addr_any; 1243 c->enabled = false; 1244 memcpy(c->name, iface->name, namelen); 1245 1246 /* First process all custom assignments, put all others in later-list */ 1247 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) { 1248 if (c->assigned != -1) { 1249 c->assigned = -1; 1250 netifd_log_message(L_WARNING, "Failed to assign requested subprefix " 1251 "of size %hhu for %s, trying other\n", c->length, c->name); 1252 } 1253 1254 entry = calloc(1, sizeof(*entry)); 1255 if (!entry) { 1256 free(c); 1257 continue; 1258 } 1259 1260 entry->node.key = c; 1261 avl_insert(&assign_later, &entry->node); 1262 } 1263 1264 if (c->assigned != -1) 1265 assigned_any = true; 1266 } 1267 1268 /* Then try to assign all other + failed custom assignments */ 1269 avl_for_each_element_safe(&assign_later, entry, node, n_entry) { 1270 bool assigned = false; 1271 1272 c = (struct device_prefix_assignment *)entry->node.key; 1273 avl_delete(&assign_later, &entry->node); 1274 1275 do { 1276 assigned = interface_prefix_assign(&prefix->assignments, c); 1277 } while (!assigned && ++c->length <= 64); 1278 1279 if (!assigned) { 1280 netifd_log_message(L_WARNING, "Failed to assign subprefix " 1281 "of size %hhu for %s\n", c->length, c->name); 1282 free(c); 1283 } else 1284 assigned_any = true; 1285 1286 free(entry); 1287 } 1288 1289 list_for_each_entry(c, &prefix->assignments, head) 1290 if ((iface = vlist_find(&interfaces, c->name, iface, node))) 1291 interface_set_prefix_address(c, prefix, iface, true); 1292 1293 if (!assigned_any) 1294 netifd_log_message(L_WARNING, "You have delegated IPv6-prefixes but haven't assigned them " 1295 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?"); 1296 } 1297 1298 1299 void interface_refresh_assignments(bool hint) 1300 { 1301 static bool refresh = false; 1302 if (!hint && refresh) { 1303 struct device_prefix *p; 1304 time_t now = system_get_rtime(); 1305 1306 list_for_each_entry(p, &prefixes, head) { 1307 bool valid = !(p->valid_until && p->valid_until - 1 <= now); 1308 1309 interface_update_prefix_assignments(p, valid); 1310 } 1311 } 1312 refresh = hint; 1313 } 1314 1315 void interface_update_prefix_delegation(struct interface_ip_settings *ip) 1316 { 1317 struct device_prefix *prefix; 1318 time_t now = system_get_rtime(); 1319 1320 vlist_for_each_element(&ip->prefix, prefix, node) { 1321 bool valid = !(prefix->valid_until && prefix->valid_until - 1 <= now); 1322 1323 interface_update_prefix_assignments(prefix, !ip->no_delegation && valid); 1324 1325 if (ip->no_delegation) { 1326 if (prefix->head.next) 1327 list_del(&prefix->head); 1328 } else 1329 list_add(&prefix->head, &prefixes); 1330 } 1331 } 1332 1333 static void 1334 interface_update_prefix(struct vlist_tree *tree, 1335 struct vlist_node *node_new, 1336 struct vlist_node *node_old) 1337 { 1338 struct device_prefix *prefix_old, *prefix_new; 1339 prefix_old = container_of(node_old, struct device_prefix, node); 1340 prefix_new = container_of(node_new, struct device_prefix, node); 1341 1342 struct interface_ip_settings *ip = container_of(tree, struct interface_ip_settings, prefix); 1343 if (tree && (!node_new || !node_old)) 1344 ip->iface->updated |= IUF_PREFIX; 1345 1346 struct device_route route; 1347 memset(&route, 0, sizeof(route)); 1348 route.flags = DEVADDR_INET6; 1349 route.metric = INT32_MAX; 1350 route.mask = (node_new) ? prefix_new->length : prefix_old->length; 1351 route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr; 1352 1353 struct device_prefix_assignment *c; 1354 struct interface *iface; 1355 bool new_valid = node_new && !(prefix_new->valid_until && prefix_new->valid_until - 1 <= system_get_rtime()); 1356 1357 if (node_old && node_new) { 1358 /* Move assignments and refresh addresses to update valid times */ 1359 list_splice(&prefix_old->assignments, &prefix_new->assignments); 1360 1361 list_for_each_entry(c, &prefix_new->assignments, head) 1362 if ((iface = vlist_find(&interfaces, c->name, iface, node))) 1363 interface_set_prefix_address(c, prefix_new, iface, new_valid); 1364 1365 if (prefix_new->preferred_until != prefix_old->preferred_until || 1366 prefix_new->valid_until != prefix_old->valid_until) 1367 ip->iface->updated |= IUF_PREFIX; 1368 } else if (node_new) { 1369 /* Set null-route to avoid routing loops */ 1370 system_add_route(NULL, &route); 1371 1372 if (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation) 1373 interface_update_prefix_assignments(prefix_new, new_valid); 1374 } else if (node_old) { 1375 /* Remove null-route */ 1376 interface_update_prefix_assignments(prefix_old, false); 1377 system_del_route(NULL, &route); 1378 } 1379 1380 if (node_old) { 1381 if (prefix_old->head.next) 1382 list_del(&prefix_old->head); 1383 free(prefix_old); 1384 } 1385 1386 if (node_new && (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation)) 1387 list_add(&prefix_new->head, &prefixes); 1388 1389 } 1390 1391 struct device_prefix* 1392 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr, 1393 uint8_t length, time_t valid_until, time_t preferred_until, 1394 struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass) 1395 { 1396 union if_addr a = { .in6 = *addr }; 1397 1398 if (!pclass) 1399 pclass = (iface) ? iface->name : "local"; 1400 1401 struct device_prefix *prefix = calloc(1, sizeof(*prefix) + strlen(pclass) + 1); 1402 if (!prefix) 1403 return NULL; 1404 1405 clear_if_addr(&a, length); 1406 1407 prefix->length = length; 1408 prefix->addr = a.in6; 1409 prefix->preferred_until = preferred_until; 1410 prefix->valid_until = valid_until; 1411 prefix->iface = iface; 1412 INIT_LIST_HEAD(&prefix->assignments); 1413 1414 if (excl_addr) { 1415 prefix->excl_addr = *excl_addr; 1416 prefix->excl_length = excl_length; 1417 } 1418 1419 strcpy(prefix->pclass, pclass); 1420 1421 if (iface) 1422 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr); 1423 else 1424 interface_update_prefix(NULL, &prefix->node, NULL); 1425 1426 return prefix; 1427 } 1428 1429 void 1430 interface_ip_set_ula_prefix(const char *prefix) 1431 { 1432 char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr; 1433 char *prefixaddr, *prefixlen; 1434 struct in6_addr addr; 1435 int length; 1436 1437 if (prefix) 1438 strncpy(buf, prefix, sizeof(buf) - 1); 1439 prefixaddr = strtok_r(buf, "/", &saveptr); 1440 1441 if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) 1442 goto invalid; 1443 1444 prefixlen = strtok_r(NULL, ",", &saveptr); 1445 if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64) 1446 goto invalid; 1447 1448 if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) || 1449 ula_prefix->length != length) { 1450 if (ula_prefix) 1451 interface_update_prefix(NULL, NULL, &ula_prefix->node); 1452 1453 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length, 1454 0, 0, NULL, 0, NULL); 1455 } 1456 1457 return; 1458 1459 invalid: 1460 if (ula_prefix) { 1461 interface_update_prefix(NULL, NULL, &ula_prefix->node); 1462 ula_prefix = NULL; 1463 } 1464 } 1465 1466 static void 1467 interface_add_dns_server(struct interface_ip_settings *ip, const char *str) 1468 { 1469 struct dns_server *s; 1470 1471 s = calloc(1, sizeof(*s)); 1472 if (!s) 1473 return; 1474 1475 s->af = AF_INET; 1476 if (inet_pton(s->af, str, &s->addr.in)) 1477 goto add; 1478 1479 s->af = AF_INET6; 1480 if (inet_pton(s->af, str, &s->addr.in6)) 1481 goto add; 1482 1483 free(s); 1484 return; 1485 1486 add: 1487 D(INTERFACE, "Add IPv%c DNS server: %s", 1488 s->af == AF_INET6 ? '6' : '4', str); 1489 vlist_simple_add(&ip->dns_servers, &s->node); 1490 } 1491 1492 void 1493 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list) 1494 { 1495 struct blob_attr *cur; 1496 size_t rem; 1497 1498 blobmsg_for_each_attr(cur, list, rem) { 1499 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) 1500 continue; 1501 1502 if (!blobmsg_check_attr(cur, false)) 1503 continue; 1504 1505 interface_add_dns_server(ip, blobmsg_data(cur)); 1506 } 1507 } 1508 1509 static void 1510 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str) 1511 { 1512 struct dns_search_domain *s; 1513 int len = strlen(str); 1514 1515 s = calloc(1, sizeof(*s) + len + 1); 1516 if (!s) 1517 return; 1518 1519 D(INTERFACE, "Add DNS search domain: %s", str); 1520 memcpy(s->name, str, len); 1521 vlist_simple_add(&ip->dns_search, &s->node); 1522 } 1523 1524 void 1525 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list) 1526 { 1527 struct blob_attr *cur; 1528 size_t rem; 1529 1530 blobmsg_for_each_attr(cur, list, rem) { 1531 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) 1532 continue; 1533 1534 if (!blobmsg_check_attr(cur, false)) 1535 continue; 1536 1537 interface_add_dns_search_domain(ip, blobmsg_data(cur)); 1538 } 1539 } 1540 1541 static void 1542 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip, const char *dev) 1543 { 1544 struct dns_server *s; 1545 struct dns_search_domain *d; 1546 const char *str; 1547 char buf[INET6_ADDRSTRLEN]; 1548 1549 vlist_simple_for_each_element(&ip->dns_servers, s, node) { 1550 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf)); 1551 if (!str) 1552 continue; 1553 1554 if (s->af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&s->addr.in6)) 1555 fprintf(f, "nameserver %s%%%s\n", str, dev); 1556 else 1557 fprintf(f, "nameserver %s\n", str); 1558 } 1559 1560 vlist_simple_for_each_element(&ip->dns_search, d, node) { 1561 fprintf(f, "search %s\n", d->name); 1562 } 1563 } 1564 1565 /* Sorting of interface resolver entries : */ 1566 /* Primary on interface dns_metric : lowest metric first */ 1567 /* Secondary on interface metric : lowest metric first */ 1568 /* Finally alphabetical order of interface names */ 1569 static int resolv_conf_iface_cmp(const void *k1, const void *k2, void *ptr) 1570 { 1571 const struct interface *iface1 = k1, *iface2 = k2; 1572 1573 if (iface1->dns_metric != iface2->dns_metric) 1574 return iface1->dns_metric - iface2->dns_metric; 1575 1576 if (iface1->metric != iface2->metric) 1577 return iface1->metric - iface2->metric; 1578 1579 return strcmp(iface1->name, iface2->name); 1580 } 1581 1582 static void 1583 __interface_write_dns_entries(FILE *f, const char *jail) 1584 { 1585 struct interface *iface; 1586 struct { 1587 struct avl_node node; 1588 } *entry, *n_entry; 1589 struct avl_tree resolv_conf_iface_entries; 1590 1591 avl_init(&resolv_conf_iface_entries, resolv_conf_iface_cmp, false, NULL); 1592 1593 vlist_for_each_element(&interfaces, iface, node) { 1594 if (iface->state != IFS_UP) 1595 continue; 1596 1597 if (jail && (!iface->jail || strcmp(jail, iface->jail))) 1598 continue; 1599 1600 if (vlist_simple_empty(&iface->proto_ip.dns_search) && 1601 vlist_simple_empty(&iface->proto_ip.dns_servers) && 1602 vlist_simple_empty(&iface->config_ip.dns_search) && 1603 vlist_simple_empty(&iface->config_ip.dns_servers)) 1604 continue; 1605 1606 entry = calloc(1, sizeof(*entry)); 1607 if (!entry) 1608 continue; 1609 1610 entry->node.key = iface; 1611 avl_insert(&resolv_conf_iface_entries, &entry->node); 1612 } 1613 1614 avl_for_each_element(&resolv_conf_iface_entries, entry, node) { 1615 iface = (struct interface *)entry->node.key; 1616 struct device *dev = iface->l3_dev.dev; 1617 1618 fprintf(f, "# Interface %s\n", iface->name); 1619 1620 write_resolv_conf_entries(f, &iface->config_ip, dev->ifname); 1621 1622 if (!iface->proto_ip.no_dns) 1623 write_resolv_conf_entries(f, &iface->proto_ip, dev->ifname); 1624 } 1625 1626 avl_remove_all_elements(&resolv_conf_iface_entries, entry, node, n_entry) 1627 free(entry); 1628 } 1629 1630 void 1631 interface_write_resolv_conf(const char *jail) 1632 { 1633 size_t plen = (jail ? strlen(jail) + 1 : 0 ) + 1634 (strlen(resolv_conf) >= strlen(DEFAULT_RESOLV_CONF) ? 1635 strlen(resolv_conf) : strlen(DEFAULT_RESOLV_CONF) ) + 1; 1636 char *path = alloca(plen); 1637 char *dpath = alloca(plen); 1638 char *tmppath = alloca(plen + 4); 1639 FILE *f; 1640 uint32_t crcold, crcnew; 1641 1642 if (jail) { 1643 sprintf(path, "/tmp/resolv.conf-%s.d/resolv.conf.auto", jail); 1644 strcpy(dpath, path); 1645 dpath = dirname(dpath); 1646 mkdir(dpath, 0755); 1647 } else { 1648 strcpy(path, resolv_conf); 1649 } 1650 1651 sprintf(tmppath, "%s.tmp", path); 1652 unlink(tmppath); 1653 f = fopen(tmppath, "w+"); 1654 if (!f) { 1655 D(INTERFACE, "Failed to open %s for writing", path); 1656 return; 1657 } 1658 1659 __interface_write_dns_entries(f, jail); 1660 1661 fflush(f); 1662 rewind(f); 1663 crcnew = crc32_file(f); 1664 fclose(f); 1665 1666 crcold = crcnew + 1; 1667 f = fopen(path, "r"); 1668 if (f) { 1669 crcold = crc32_file(f); 1670 fclose(f); 1671 } 1672 1673 if (crcold == crcnew) { 1674 unlink(tmppath); 1675 } else if (rename(tmppath, path) < 0) { 1676 D(INTERFACE, "Failed to replace %s", path); 1677 unlink(tmppath); 1678 } 1679 } 1680 1681 static void 1682 interface_ip_set_route_enabled(struct interface_ip_settings *ip, 1683 struct device_route *route, bool enabled) 1684 { 1685 struct device *dev = ip->iface->l3_dev.dev; 1686 1687 if (route->flags & DEVADDR_EXTERNAL) 1688 return; 1689 1690 if (!enable_route(ip, route)) 1691 enabled = false; 1692 1693 if (route->enabled == enabled) 1694 return; 1695 1696 if (enabled) { 1697 interface_set_route_info(ip->iface, route); 1698 1699 if (system_add_route(dev, route)) 1700 route->failed = true; 1701 } else 1702 system_del_route(dev, route); 1703 1704 route->enabled = enabled; 1705 } 1706 1707 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled) 1708 { 1709 struct device_addr *addr; 1710 struct device_route *route; 1711 struct device_neighbor *neighbor; 1712 struct device *dev; 1713 struct interface *iface; 1714 1715 ip->enabled = enabled; 1716 iface = ip->iface; 1717 dev = iface->l3_dev.dev; 1718 if (!dev) 1719 return; 1720 1721 vlist_for_each_element(&ip->addr, addr, node) { 1722 bool v6 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6) ? true : false; 1723 1724 if (addr->flags & DEVADDR_EXTERNAL) 1725 continue; 1726 1727 if (addr->enabled == enabled) 1728 continue; 1729 1730 if (enabled) { 1731 system_add_address(dev, addr); 1732 1733 addr->policy_table = (v6) ? iface->ip6table : iface->ip4table; 1734 if (iface->metric || addr->policy_table) 1735 interface_handle_subnet_route(iface, addr, true); 1736 1737 if (addr_is_offlink(dev, addr) && (addr->mask < (v6 ? 128 : 32))) { 1738 struct device_route route; 1739 1740 memset(&route, 0, sizeof(route)); 1741 route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4; 1742 route.metric = INT32_MAX; 1743 route.mask = addr->mask; 1744 route.addr = addr->addr; 1745 1746 clear_if_addr(&route.addr, route.mask); 1747 1748 /* 1749 * In case off link is specifed as address property 1750 * add null-route to avoid routing loops 1751 */ 1752 system_add_route(NULL, &route); 1753 } 1754 1755 if (addr->policy_table) 1756 interface_add_addr_rules(addr, true); 1757 } else { 1758 interface_handle_subnet_route(iface, addr, false); 1759 system_del_address(dev, addr); 1760 1761 if (addr_is_offlink(dev, addr) && (addr->mask < (v6 ? 128 : 32))) { 1762 struct device_route route; 1763 1764 memset(&route, 0, sizeof(route)); 1765 route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4; 1766 route.metric = INT32_MAX; 1767 route.mask = addr->mask; 1768 route.addr = addr->addr; 1769 1770 clear_if_addr(&route.addr, route.mask); 1771 1772 /* Delete null-route */ 1773 system_del_route(NULL, &route); 1774 } 1775 1776 if (addr->policy_table) 1777 interface_add_addr_rules(addr, false); 1778 } 1779 addr->enabled = enabled; 1780 } 1781 1782 vlist_for_each_element(&ip->route, route, node) 1783 interface_ip_set_route_enabled(ip, route, enabled); 1784 if (ip == &iface->proto_ip) 1785 vlist_for_each_element(&iface->host_routes, route, node) 1786 interface_ip_set_route_enabled(ip, route, enabled); 1787 1788 vlist_for_each_element(&ip->neighbor, neighbor, node) { 1789 if (neighbor->enabled == enabled) 1790 continue; 1791 1792 if (enabled) { 1793 if(system_add_neighbor(dev, neighbor)) 1794 neighbor->failed = true; 1795 } else 1796 system_del_neighbor(dev, neighbor); 1797 1798 neighbor->enabled = enabled; 1799 } 1800 1801 struct device_prefix *c; 1802 struct device_prefix_assignment *a; 1803 list_for_each_entry(c, &prefixes, head) 1804 list_for_each_entry(a, &c->assignments, head) 1805 if (!strcmp(a->name, ip->iface->name)) 1806 interface_set_prefix_address(a, c, ip->iface, enabled); 1807 1808 if (ip->iface->policy_rules_set != enabled && 1809 ip->iface->l3_dev.dev) { 1810 if (ip->iface->l3_dev.dev->settings.ipv6) { 1811 set_ip_lo_policy(enabled, true, ip->iface); 1812 set_ip_source_policy(enabled, true, IPRULE_PRIORITY_REJECT + ip->iface->l3_dev.dev->ifindex, 1813 NULL, 0, 0, ip->iface, "failed_policy", true); 1814 } 1815 set_ip_lo_policy(enabled, false, ip->iface); 1816 1817 ip->iface->policy_rules_set = enabled; 1818 } 1819 } 1820 1821 void 1822 interface_ip_update_start(struct interface_ip_settings *ip) 1823 { 1824 if (ip != &ip->iface->config_ip) { 1825 vlist_simple_update(&ip->dns_servers); 1826 vlist_simple_update(&ip->dns_search); 1827 } 1828 vlist_update(&ip->route); 1829 vlist_update(&ip->addr); 1830 vlist_update(&ip->prefix); 1831 vlist_update(&ip->neighbor); 1832 } 1833 1834 void 1835 interface_ip_update_complete(struct interface_ip_settings *ip) 1836 { 1837 vlist_simple_flush(&ip->dns_servers); 1838 vlist_simple_flush(&ip->dns_search); 1839 vlist_flush(&ip->route); 1840 vlist_flush(&ip->addr); 1841 vlist_flush(&ip->prefix); 1842 vlist_flush(&ip->neighbor); 1843 interface_write_resolv_conf(ip->iface->jail); 1844 } 1845 1846 void 1847 interface_ip_flush(struct interface_ip_settings *ip) 1848 { 1849 if (ip == &ip->iface->proto_ip) 1850 vlist_flush_all(&ip->iface->host_routes); 1851 vlist_simple_flush_all(&ip->dns_servers); 1852 vlist_simple_flush_all(&ip->dns_search); 1853 vlist_flush_all(&ip->route); 1854 vlist_flush_all(&ip->addr); 1855 vlist_flush_all(&ip->neighbor); 1856 vlist_flush_all(&ip->prefix); 1857 } 1858 1859 static void 1860 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface) 1861 { 1862 ip->iface = iface; 1863 ip->enabled = true; 1864 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node); 1865 vlist_simple_init(&ip->dns_servers, struct dns_server, node); 1866 vlist_init(&ip->route, route_cmp, interface_update_proto_route); 1867 vlist_init(&ip->neighbor, neighbor_cmp, interface_update_proto_neighbor); 1868 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr); 1869 vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix); 1870 } 1871 1872 void 1873 interface_ip_init(struct interface *iface) 1874 { 1875 __interface_ip_init(&iface->proto_ip, iface); 1876 __interface_ip_init(&iface->config_ip, iface); 1877 vlist_init(&iface->host_routes, route_cmp, interface_update_host_route); 1878 } 1879 1880 static void 1881 interface_ip_valid_until_handler(struct uloop_timeout *t) 1882 { 1883 time_t now = system_get_rtime(); 1884 struct interface *iface; 1885 vlist_for_each_element(&interfaces, iface, node) { 1886 if (iface->state != IFS_UP) 1887 continue; 1888 1889 struct device_addr *addr, *addrp; 1890 struct device_route *route, *routep; 1891 struct device_prefix *pref, *prefp; 1892 1893 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp) 1894 if (addr->valid_until && addr->valid_until < now) 1895 vlist_delete(&iface->proto_ip.addr, &addr->node); 1896 1897 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep) 1898 if (route->valid_until && route->valid_until < now) 1899 vlist_delete(&iface->proto_ip.route, &route->node); 1900 1901 vlist_for_each_element_safe(&iface->config_ip.route, route, node, routep) 1902 if (route->valid_until && route->valid_until < now) 1903 vlist_delete(&iface->config_ip.route, &route->node); 1904 1905 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp) 1906 if (pref->valid_until && pref->valid_until < now) 1907 vlist_delete(&iface->proto_ip.prefix, &pref->node); 1908 1909 } 1910 1911 uloop_timeout_set(t, 1000); 1912 } 1913 1914 static void __init 1915 interface_ip_init_worker(void) 1916 { 1917 valid_until_timeout.cb = interface_ip_valid_until_handler; 1918 uloop_timeout_set(&valid_until_timeout, 1000); 1919 } 1920
This page was automatically generated by LXR 0.3.1. • OpenWrt