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