+ 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);
+ }
+}
+
+void
+interface_write_resolv_conf(void)
+{
+ struct interface *iface;
+ 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;
+ }
+
+ 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;
+
+ fprintf(f, "# Interface %s\n", iface->name);
+ write_resolv_conf_entries(f, &iface->config_ip, iface->ifname);
+ if (!iface->proto_ip.no_dns)
+ write_resolv_conf_entries(f, &iface->proto_ip, iface->ifname);
+ }
+ 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->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 (!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 && 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);
+}
+
+void
+interface_ip_init(struct interface *iface)
+{
+ __interface_ip_init(&iface->proto_ip, iface);
+ __interface_ip_init(&iface->config_ip, iface);
+ vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
+}
+
+static void
+interface_ip_valid_until_handler(struct uloop_timeout *t)
+{
+ time_t now = system_get_rtime();
+ struct interface *iface;
+ vlist_for_each_element(&interfaces, iface, node) {
+ if (iface->state != IFS_UP)
+ continue;
+
+ struct device_addr *addr, *addrp;
+ struct device_route *route, *routep;
+ struct device_prefix *pref, *prefp;
+
+ vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
+ if (addr->valid_until && addr->valid_until < now)
+ vlist_delete(&iface->proto_ip.addr, &addr->node);
+
+ vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
+ if (route->valid_until && route->valid_until < now)
+ vlist_delete(&iface->proto_ip.route, &route->node);
+
+ vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
+ if (pref->valid_until && pref->valid_until < now)
+ vlist_delete(&iface->proto_ip.prefix, &pref->node);
+
+ }
+
+ uloop_timeout_set(t, 1000);
+}
+
+static void __init
+interface_ip_init_worker(void)
+{
+ valid_until_timeout.cb = interface_ip_valid_until_handler;
+ uloop_timeout_set(&valid_until_timeout, 1000);