+
+ /* Then try to assign all other + failed custom assignments */
+ avl_for_each_element_safe(&assign_later, entry, node, n_entry) {
+ bool assigned = false;
+
+ c = (struct device_prefix_assignment *)entry->node.key;
+ avl_delete(&assign_later, &entry->node);
+
+ do {
+ assigned = interface_prefix_assign(&prefix->assignments, c);
+ } while (!assigned && ++c->length <= 64);
+
+ if (!assigned) {
+ netifd_log_message(L_WARNING, "Failed to assign subprefix "
+ "of size %hhu for %s\n", c->length, c->name);
+ free(c);
+ } else
+ assigned_any = true;
+
+ free(entry);
+ }
+
+ list_for_each_entry(c, &prefix->assignments, head)
+ if ((iface = vlist_find(&interfaces, c->name, iface, node)))
+ interface_set_prefix_address(c, prefix, iface, true);
+
+ if (!assigned_any)
+ netifd_log_message(L_WARNING, "You have delegated IPv6-prefixes but haven't assigned them "
+ "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
+}
+
+
+void interface_refresh_assignments(bool hint)
+{
+ static bool refresh = false;
+ if (!hint && refresh) {
+ struct device_prefix *p;
+ list_for_each_entry(p, &prefixes, head)
+ interface_update_prefix_assignments(p, true);
+ }
+ refresh = hint;
+}
+
+
+static void
+interface_update_prefix(struct vlist_tree *tree,
+ struct vlist_node *node_new,
+ struct vlist_node *node_old)
+{
+ struct device_prefix *prefix_old, *prefix_new;
+ prefix_old = container_of(node_old, struct device_prefix, node);
+ prefix_new = container_of(node_new, struct device_prefix, node);
+
+ struct interface_ip_settings *ip = container_of(tree, struct interface_ip_settings, prefix);
+ if (tree && (!node_new || !node_old))
+ ip->iface->updated |= IUF_PREFIX;
+
+ struct device_route route;
+ memset(&route, 0, sizeof(route));
+ route.flags = DEVADDR_INET6;
+ route.metric = INT32_MAX;
+ route.mask = (node_new) ? prefix_new->length : prefix_old->length;
+ route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
+
+
+ struct device_prefix_assignment *c;
+ struct interface *iface;
+
+ if (node_old && node_new) {
+ // Move assignments and refresh addresses to update valid times
+ list_splice(&prefix_old->assignments, &prefix_new->assignments);
+
+ list_for_each_entry(c, &prefix_new->assignments, head)
+ if ((iface = vlist_find(&interfaces, c->name, iface, node)))
+ interface_set_prefix_address(c, prefix_new, iface, true);
+
+ if (prefix_new->preferred_until != prefix_old->preferred_until ||
+ prefix_new->valid_until != prefix_old->valid_until)
+ ip->iface->updated |= IUF_PREFIX;
+ } else if (node_new) {
+ // Set null-route to avoid routing loops
+ system_add_route(NULL, &route);
+
+ if (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation)
+ interface_update_prefix_assignments(prefix_new, true);
+ } else if (node_old) {
+ // Remove null-route
+ interface_update_prefix_assignments(prefix_old, false);
+ system_del_route(NULL, &route);
+ }
+
+ if (node_old) {
+ if (prefix_old->head.next)
+ list_del(&prefix_old->head);
+ free(prefix_old);
+ }
+
+ if (node_new && (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation))
+ list_add(&prefix_new->head, &prefixes);
+
+}
+
+struct device_prefix*
+interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
+ uint8_t length, time_t valid_until, time_t preferred_until,
+ struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass)
+{
+ if (!pclass)
+ pclass = (iface) ? iface->name : "local";
+
+ struct device_prefix *prefix = calloc(1, sizeof(*prefix) + strlen(pclass) + 1);
+ if (!prefix)
+ return NULL;
+
+ prefix->length = length;
+ prefix->addr = *addr;
+ prefix->preferred_until = preferred_until;
+ prefix->valid_until = valid_until;
+ prefix->iface = iface;
+ INIT_LIST_HEAD(&prefix->assignments);
+
+ if (excl_addr) {
+ prefix->excl_addr = *excl_addr;
+ prefix->excl_length = excl_length;
+ }
+
+ strcpy(prefix->pclass, pclass);
+
+ if (iface)
+ vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
+ else
+ interface_update_prefix(NULL, &prefix->node, NULL);
+
+ return prefix;
+}
+
+void
+interface_ip_set_ula_prefix(const char *prefix)
+{
+ char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
+ if (prefix)
+ strncpy(buf, prefix, sizeof(buf) - 1);
+ char *prefixaddr = strtok_r(buf, "/", &saveptr);
+
+ struct in6_addr addr;
+ if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
+ if (ula_prefix) {
+ interface_update_prefix(NULL, NULL, &ula_prefix->node);
+ ula_prefix = NULL;
+ }
+ return;
+ }
+
+ int length;
+ char *prefixlen = strtok_r(NULL, ",", &saveptr);
+ if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
+ return;
+
+ if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
+ ula_prefix->length != length) {
+ if (ula_prefix)
+ interface_update_prefix(NULL, NULL, &ula_prefix->node);
+
+ ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
+ 0, 0, NULL, 0, NULL);
+ }
+}
+
+void
+interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
+{
+ struct dns_server *s;
+
+ s = calloc(1, sizeof(*s));
+ if (!s)
+ return;
+
+ s->af = AF_INET;
+ if (inet_pton(s->af, str, &s->addr.in))
+ goto add;
+
+ s->af = AF_INET6;
+ if (inet_pton(s->af, str, &s->addr.in))
+ goto add;
+
+ free(s);
+ return;
+
+add:
+ D(INTERFACE, "Add IPv%c DNS server: %s\n",
+ s->af == AF_INET6 ? '6' : '4', str);
+ vlist_simple_add(&ip->dns_servers, &s->node);
+}
+
+void
+interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
+{
+ struct blob_attr *cur;
+ int rem;
+
+ blobmsg_for_each_attr(cur, list, rem) {
+ if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
+ continue;
+
+ if (!blobmsg_check_attr(cur, NULL))
+ continue;
+
+ interface_add_dns_server(ip, blobmsg_data(cur));
+ }
+}
+
+static void
+interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
+{
+ struct dns_search_domain *s;
+ int len = strlen(str);
+
+ s = calloc(1, sizeof(*s) + len + 1);
+ if (!s)
+ return;
+
+ D(INTERFACE, "Add DNS search domain: %s\n", str);
+ memcpy(s->name, str, len);
+ vlist_simple_add(&ip->dns_search, &s->node);
+}
+
+void
+interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
+{
+ struct blob_attr *cur;
+ int rem;
+
+ blobmsg_for_each_attr(cur, list, rem) {
+ if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
+ continue;
+
+ if (!blobmsg_check_attr(cur, NULL))
+ continue;
+
+ interface_add_dns_search_domain(ip, blobmsg_data(cur));
+ }
+}
+
+static void
+write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip, const char *dev)
+{
+ struct dns_server *s;
+ struct dns_search_domain *d;
+ const char *str;
+ char buf[INET6_ADDRSTRLEN];
+
+ vlist_simple_for_each_element(&ip->dns_servers, s, node) {
+ str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
+ if (!str)
+ continue;
+
+ if (s->af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&s->addr.in6))
+ fprintf(f, "nameserver %s%%%s\n", str, dev);
+ else
+ fprintf(f, "nameserver %s\n", str);
+ }
+
+ vlist_simple_for_each_element(&ip->dns_search, d, node) {
+ fprintf(f, "search %s\n", d->name);
+ }
+}
+
+/* Sorting of interface resolver entries : */
+/* Primary on interface dns_metric : lowest metric first */
+/* Secondary on interface metric : lowest metric first */
+/* Finally alphabetical order of interface names */
+static int resolv_conf_iface_cmp(const void *k1, const void *k2, void *ptr)
+{
+ const struct interface *iface1 = k1, *iface2 = k2;
+
+ if (iface1->dns_metric != iface2->dns_metric)
+ return iface1->dns_metric - iface2->dns_metric;
+
+ if (iface1->metric != iface2->metric)
+ return iface1->metric - iface2->metric;
+
+ return strcmp(iface1->name, iface2->name);
+}
+
+static void
+__interface_write_dns_entries(FILE *f)
+{
+ struct interface *iface;
+ struct {
+ struct avl_node node;
+ } *entry, *n_entry;
+ struct avl_tree resolv_conf_iface_entries;
+
+ avl_init(&resolv_conf_iface_entries, resolv_conf_iface_cmp, false, NULL);
+
+ vlist_for_each_element(&interfaces, iface, node) {
+ if (iface->state != IFS_UP)
+ continue;
+
+ if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
+ vlist_simple_empty(&iface->proto_ip.dns_servers) &&
+ vlist_simple_empty(&iface->config_ip.dns_search) &&
+ vlist_simple_empty(&iface->config_ip.dns_servers))
+ continue;
+
+ entry = calloc(1, sizeof(*entry));
+ if (!entry)
+ continue;
+
+ entry->node.key = iface;
+ avl_insert(&resolv_conf_iface_entries, &entry->node);
+ }
+
+ avl_for_each_element(&resolv_conf_iface_entries, entry, node) {
+ iface = (struct interface *)entry->node.key;
+ struct device *dev = iface->l3_dev.dev;
+
+ fprintf(f, "# Interface %s\n", iface->name);
+
+ write_resolv_conf_entries(f, &iface->config_ip, dev->ifname);
+
+ if (!iface->proto_ip.no_dns)
+ write_resolv_conf_entries(f, &iface->proto_ip, dev->ifname);
+ }
+
+ avl_remove_all_elements(&resolv_conf_iface_entries, entry, node, n_entry)
+ free(entry);
+}
+
+void
+interface_write_resolv_conf(void)
+{
+ char *path = alloca(strlen(resolv_conf) + 5);
+ FILE *f;
+ uint32_t crcold, crcnew;
+
+ sprintf(path, "%s.tmp", resolv_conf);
+ unlink(path);
+ f = fopen(path, "w+");
+ if (!f) {
+ D(INTERFACE, "Failed to open %s for writing\n", path);
+ return;
+ }
+
+ __interface_write_dns_entries(f);
+
+ fflush(f);
+ rewind(f);
+ crcnew = crc32_file(f);
+ fclose(f);
+
+ crcold = crcnew + 1;
+ f = fopen(resolv_conf, "r");
+ if (f) {
+ crcold = crc32_file(f);
+ fclose(f);
+ }
+
+ if (crcold == crcnew) {
+ unlink(path);
+ } else if (rename(path, resolv_conf) < 0) {
+ D(INTERFACE, "Failed to replace %s\n", resolv_conf);
+ unlink(path);
+ }
+}
+
+void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
+{
+ struct device_addr *addr;
+ struct device_route *route;
+ struct device *dev;
+ struct interface *iface;
+
+ ip->enabled = enabled;
+ iface = ip->iface;
+ dev = iface->l3_dev.dev;
+ if (!dev)
+ return;
+
+ vlist_for_each_element(&ip->addr, addr, node) {
+ bool v6 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6) ? true : false;
+
+ if (addr->flags & DEVADDR_EXTERNAL)
+ continue;
+
+ if (addr->enabled == enabled)
+ continue;
+
+ if (enabled) {
+ system_add_address(dev, addr);
+
+ addr->policy_table = (v6) ? iface->ip6table : iface->ip4table;
+ if (iface->metric || addr->policy_table)
+ interface_handle_subnet_route(iface, addr, true);
+
+ if (addr->policy_table)
+ interface_add_addr_rules(addr, true);
+ } else {
+ interface_handle_subnet_route(iface, addr, false);
+ system_del_address(dev, addr);
+
+ if (addr->policy_table)
+ interface_add_addr_rules(addr, false);
+ }
+ addr->enabled = enabled;
+ }
+
+ vlist_for_each_element(&ip->route, route, node) {
+ bool _enabled = enabled;
+
+ if (route->flags & DEVADDR_EXTERNAL)
+ continue;
+
+ if (!enable_route(ip, route))
+ _enabled = false;
+
+ if (route->enabled == _enabled)
+ continue;
+
+ if (_enabled) {
+ interface_set_route_info(ip->iface, route);
+
+ if (system_add_route(dev, route))
+ route->failed = true;
+ } else
+ system_del_route(dev, route);
+ route->enabled = _enabled;
+ }
+
+ struct device_prefix *c;
+ struct device_prefix_assignment *a;
+ list_for_each_entry(c, &prefixes, head)
+ list_for_each_entry(a, &c->assignments, head)
+ if (!strcmp(a->name, ip->iface->name))
+ interface_set_prefix_address(a, c, ip->iface, enabled);
+
+ if (ip->iface->policy_rules_set != enabled &&
+ ip->iface->l3_dev.dev) {
+ set_ip_lo_policy(enabled, true, ip->iface);
+ set_ip_lo_policy(enabled, false, ip->iface);
+
+ set_ip_source_policy(enabled, true, IPRULE_PRIORITY_REJECT + ip->iface->l3_dev.dev->ifindex,
+ NULL, 0, 0, ip->iface, "failed_policy", true);
+ ip->iface->policy_rules_set = enabled;
+ }
+}
+
+void
+interface_ip_update_start(struct interface_ip_settings *ip)
+{
+ if (ip != &ip->iface->config_ip) {
+ vlist_simple_update(&ip->dns_servers);
+ vlist_simple_update(&ip->dns_search);
+ }
+ vlist_update(&ip->route);
+ vlist_update(&ip->addr);
+ vlist_update(&ip->prefix);
+}
+
+void
+interface_ip_update_complete(struct interface_ip_settings *ip)
+{
+ vlist_simple_flush(&ip->dns_servers);
+ vlist_simple_flush(&ip->dns_search);
+ vlist_flush(&ip->route);
+ vlist_flush(&ip->addr);
+ vlist_flush(&ip->prefix);
+ interface_write_resolv_conf();
+}
+
+void
+interface_ip_flush(struct interface_ip_settings *ip)
+{
+ if (ip == &ip->iface->proto_ip)
+ vlist_flush_all(&ip->iface->host_routes);
+ vlist_simple_flush_all(&ip->dns_servers);
+ vlist_simple_flush_all(&ip->dns_search);
+ vlist_flush_all(&ip->route);
+ vlist_flush_all(&ip->addr);
+ vlist_flush_all(&ip->prefix);
+}
+
+static void
+__interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
+{
+ ip->iface = iface;
+ ip->enabled = true;
+ vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
+ vlist_simple_init(&ip->dns_servers, struct dns_server, node);
+ vlist_init(&ip->route, route_cmp, interface_update_proto_route);
+ vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
+ vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);