move resolv.conf.auto to /tmp/resolv.conf.d/
[oweals/netifd.git] / interface-ip.c
index 1c84d4f8afed8bbe8af9fcc868fb1472b048019d..c159e09133165aec461d34c8a42311d7f5e69bd3 100644 (file)
 #include <arpa/inet.h>
 #include <netinet/in.h>
 
+#ifdef linux
+#include <netinet/ether.h>
+#endif
+
 #include "netifd.h"
 #include "device.h"
 #include "interface.h"
@@ -64,6 +68,28 @@ const struct uci_blob_param_list route_attr_list = {
        .params = route_attr,
 };
 
+enum {
+       NEIGHBOR_INTERFACE,
+       NEIGHBOR_ADDRESS,
+       NEIGHBOR_MAC,
+       NEIGHBOR_PROXY,
+       NEIGHBOR_ROUTER,
+       __NEIGHBOR_MAX
+};
+
+static const struct blobmsg_policy neighbor_attr[__NEIGHBOR_MAX]={
+       [NEIGHBOR_INTERFACE]= { .name = "interface", .type = BLOBMSG_TYPE_STRING},
+       [NEIGHBOR_ADDRESS]= { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING},
+       [NEIGHBOR_MAC]= { .name = "mac", .type = BLOBMSG_TYPE_STRING},
+       [NEIGHBOR_PROXY]= { .name = "proxy", .type = BLOBMSG_TYPE_BOOL},
+       [NEIGHBOR_ROUTER]= {.name = "router", .type = BLOBMSG_TYPE_BOOL},
+};
+
+const struct uci_blob_param_list neighbor_attr_list = {
+       .n_params = __NEIGHBOR_MAX,
+       .params = neighbor_attr,
+};
+
 
 struct list_head prefixes = LIST_HEAD_INIT(prefixes);
 static struct device_prefix *ula_prefix = NULL;
@@ -170,7 +196,11 @@ __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v
                if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
                        continue;
 
-               // Handle offlink addresses correctly
+               if (((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4) &&
+                               addr->point_to_point && a->in.s_addr == addr->point_to_point)
+                       return true;
+
+               /* Handle offlink addresses correctly */
                unsigned int mask = addr->mask;
                if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6 &&
                                (addr->flags & DEVADDR_OFFLINK))
@@ -229,18 +259,12 @@ interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *if
 {
        struct device_route *route, *r_next = NULL;
        bool defaultroute_target = false;
+       union if_addr addr_zero;
        int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
 
-       route = calloc(1, sizeof(*route));
-       if (!route)
-               return NULL;
-
-       route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
-       route->mask = v6 ? 128 : 32;
-       if (memcmp(&route->addr, addr, addrsize) == 0)
+       memset(&addr_zero, 0, sizeof(addr_zero));
+       if (memcmp(&addr_zero, addr, addrsize) == 0)
                defaultroute_target = true;
-       else
-               memcpy(&route->addr, addr, addrsize);
 
        if (iface) {
                /* look for locally addressable target first */
@@ -262,21 +286,27 @@ interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *if
                }
        }
 
-       if (!r_next) {
-               free(route);
+       if (!r_next)
                return NULL;
-       }
 
        iface = r_next->iface;
+       if (defaultroute_target)
+               return iface;
+
+       route = calloc(1, sizeof(*route));
+       if (!route)
+               return NULL;
+
+       route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
+       route->mask = v6 ? 128 : 32;
+       memcpy(&route->addr, addr, addrsize);
        memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
        route->mtu = r_next->mtu;
        route->metric = r_next->metric;
        route->table = r_next->table;
        route->iface = iface;
-       if (defaultroute_target)
-               free(route);
-       else
-               vlist_add(&iface->host_routes, &route->node, route);
+       vlist_add(&iface->host_routes, &route->node, route);
+
        return iface;
 }
 
@@ -298,6 +328,64 @@ interface_set_route_info(struct interface *iface, struct device_route *route)
        }
 }
 
+void
+interface_ip_add_neighbor(struct interface *iface, struct blob_attr *attr, bool v6)
+{
+       struct interface_ip_settings *ip;
+       struct blob_attr *tb[__NEIGHBOR_MAX], *cur;
+       struct device_neighbor *neighbor;
+       int af = v6 ? AF_INET6: AF_INET;
+       struct ether_addr *ea;
+
+       blobmsg_parse(neighbor_attr, __NEIGHBOR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
+
+       if (!iface) {
+               if ((cur = tb[NEIGHBOR_INTERFACE]) == NULL)
+                       return;
+
+               iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
+
+               if (!iface)
+                       return;
+
+               ip = &iface->config_ip;
+       } else
+               ip = &iface->proto_ip;
+
+       neighbor = calloc(1,sizeof(*neighbor));
+       if (!neighbor)
+               return;
+
+       neighbor->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
+
+       if ((cur = tb[NEIGHBOR_ADDRESS]) != NULL){
+               if (!inet_pton(af, blobmsg_data(cur), &neighbor->addr))
+                       goto error;
+       } else
+               goto error;
+
+       if ((cur = tb[NEIGHBOR_MAC]) != NULL) {
+               neighbor->flags |= DEVNEIGH_MAC;
+               ea = ether_aton(blobmsg_data(cur));
+               if (!ea)
+                       goto error;
+
+               memcpy(neighbor->macaddr, ea, 6);
+       }
+
+       if ((cur = tb[NEIGHBOR_PROXY]) != NULL)
+               neighbor->proxy = blobmsg_get_bool(cur);
+
+       if ((cur = tb[NEIGHBOR_ROUTER]) != NULL)
+               neighbor->router = blobmsg_get_bool(cur);
+
+       vlist_add(&ip->neighbor, &neighbor->node, neighbor);
+       return;
+
+error:
+       free(neighbor);
+}
+
 void
 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
 {
@@ -357,7 +445,7 @@ interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
                route->flags |= DEVROUTE_MTU;
        }
 
-       // Use source-based routing
+       /* Use source-based routing */
        if ((cur = tb[ROUTE_SOURCE]) != NULL) {
                char *saveptr, *source = alloca(blobmsg_data_len(cur));
                memcpy(source, blobmsg_data(cur), blobmsg_data_len(cur));
@@ -393,7 +481,7 @@ interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
        if ((cur = tb[ROUTE_VALID]) != NULL) {
                int64_t valid = blobmsg_get_u32(cur);
                int64_t valid_until = valid + (int64_t)system_get_rtime();
-               if (valid_until <= LONG_MAX && valid != 0xffffffffLL) // Catch overflow
+               if (valid_until <= LONG_MAX && valid != 0xffffffffLL) /* Catch overflow */
                        route->valid_until = valid_until;
        }
 
@@ -428,6 +516,14 @@ addr_cmp(const void *k1, const void *k2, void *ptr)
                      offsetof(struct device_addr, flags));
 }
 
+static int
+neighbor_cmp(const void *k1, const void *k2, void *ptr)
+{
+       const struct device_neighbor *n1 = k1, *n2 = k2;
+
+       return memcmp(&n1->addr, &n2->addr, sizeof(n2->addr));
+}
+
 static int
 route_cmp(const void *k1, const void *k2, void *ptr)
 {
@@ -572,10 +668,12 @@ interface_update_proto_addr(struct vlist_tree *tree,
 
        if (node_old) {
                if (a_old->enabled && !keep) {
-                       //This is needed for source routing to work correctly. If a device
-                       //has two connections to a network using the same subnet, adding
-                       //only the network-rule will cause packets to be routed through the
-                       //first matching network (source IP matches both masks).
+                       /*
+                        * This is needed for source routing to work correctly. If a device
+                        * has two connections to a network using the same subnet, adding
+                        * only the network-rule will cause packets to be routed through the
+                        * first matching network (source IP matches both masks)
+                        */
                        if (a_old->policy_table)
                                interface_add_addr_rules(a_old, false);
 
@@ -622,6 +720,44 @@ enable_route(struct interface_ip_settings *ip, struct device_route *route)
        return ip->enabled;
 }
 
+static void
+interface_update_proto_neighbor(struct vlist_tree *tree,
+                               struct vlist_node * node_new,
+                               struct vlist_node *node_old)
+{
+       struct device *dev;
+       struct device_neighbor *neighbor_old, *neighbor_new;
+       struct interface_ip_settings *ip;
+       bool keep = false;
+
+       ip = container_of(tree, struct interface_ip_settings, neighbor);
+       dev = ip->iface->l3_dev.dev;
+
+       neighbor_old = container_of(node_old, struct device_neighbor, node);
+       neighbor_new = container_of(node_new, struct device_neighbor, node);
+
+       if (node_old && node_new) {
+               keep = (!memcmp(neighbor_old->macaddr, neighbor_new->macaddr, sizeof(neighbor_old->macaddr)) &&
+                       (neighbor_old->proxy == neighbor_new->proxy) &&
+                       (neighbor_old->router == neighbor_new->router));
+       }
+
+       if (node_old) {
+               if (!keep && neighbor_old->enabled)
+                       system_del_neighbor(dev, neighbor_old);
+
+               free(neighbor_old);
+       }
+
+       if (node_new) {
+               if (!keep && ip->enabled)
+                       if (system_add_neighbor(dev, neighbor_new))
+                               neighbor_new->failed = true;
+
+               neighbor_new->enabled = ip->enabled;
+       }
+}
+
 static void
 interface_update_proto_route(struct vlist_tree *tree,
                             struct vlist_node *node_new,
@@ -721,10 +857,9 @@ eui64_ifaceid(struct interface *iface, struct in6_addr *addr)
                return false;
 
        /* get mac address */
-       uint8_t *macaddr = iface->l3_dev.dev->settings.macaddr;
        uint8_t *ifaceid = addr->s6_addr + 8;
-       memcpy(ifaceid,macaddr,3);
-       memcpy(ifaceid + 5,macaddr + 3, 3);
+       memcpy(ifaceid, st.macaddr, 3);
+       memcpy(ifaceid + 5, st.macaddr + 3, 3);
        ifaceid[3] = 0xff;
        ifaceid[4] = 0xfe;
        ifaceid[0] ^= 0x02;
@@ -850,8 +985,11 @@ interface_set_prefix_address(struct device_prefix_assignment *assignment,
                        int mtu = system_update_ipv6_mtu(uplink->l3_dev.dev, 0);
                        int mtu_old = system_update_ipv6_mtu(l3_downlink, 0);
 
-                       if (mtu > 0 && mtu_old > mtu)
-                               system_update_ipv6_mtu(l3_downlink, mtu);
+                       if (mtu > 0 && mtu_old != mtu) {
+                               if (system_update_ipv6_mtu(l3_downlink, mtu) < 0 && mtu < mtu_old)
+                                       netifd_log_message(L_WARNING, "Failed to set IPv6 mtu to %d "
+                                                       "on interface '%s'\n", mtu, iface->name);
+                       }
                }
 
                assignment->enabled = true;
@@ -906,7 +1044,7 @@ static void interface_update_prefix_assignments(struct device_prefix *prefix, bo
        struct device_prefix_assignment *c;
        struct interface *iface;
 
-       // Delete all assignments
+       /* Delete all assignments */
        while (!list_empty(&prefix->assignments)) {
                c = list_first_entry(&prefix->assignments,
                                struct device_prefix_assignment, head);
@@ -919,7 +1057,7 @@ static void interface_update_prefix_assignments(struct device_prefix *prefix, bo
        if (!setup)
                return;
 
-       // End-of-assignment sentinel
+       /* End-of-assignment sentinel */
        c = malloc(sizeof(*c) + 1);
        if (!c)
                return;
@@ -930,7 +1068,7 @@ static void interface_update_prefix_assignments(struct device_prefix *prefix, bo
        c->addr = in6addr_any;
        list_add(&c->head, &prefix->assignments);
 
-       // Excluded prefix
+       /* Excluded prefix */
        if (prefix->excl_length > 0) {
                const char name[] = "!excluded";
                c = malloc(sizeof(*c) + sizeof(name));
@@ -957,7 +1095,7 @@ static void interface_update_prefix_assignments(struct device_prefix *prefix, bo
                                iface->assignment_length > 64)
                        continue;
 
-               // Test whether there is a matching class
+               /* Test whether there is a matching class */
                if (!list_empty(&iface->assignment_classes)) {
                        bool found = false;
 
@@ -985,7 +1123,7 @@ static void interface_update_prefix_assignments(struct device_prefix *prefix, bo
                c->enabled = false;
                memcpy(c->name, iface->name, namelen);
 
-               // First process all custom assignments, put all others in later-list
+               /* First process all custom assignments, put all others in later-list */
                if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
                        if (c->assigned != -1) {
                                c->assigned = -1;
@@ -994,8 +1132,10 @@ static void interface_update_prefix_assignments(struct device_prefix *prefix, bo
                        }
 
                        entry = calloc(1, sizeof(*entry));
-                       if (!entry)
+                       if (!entry) {
+                               free(c);
                                continue;
+                       }
 
                        entry->node.key = c;
                        avl_insert(&assign_later, &entry->node);
@@ -1047,6 +1187,20 @@ void interface_refresh_assignments(bool hint)
        refresh = hint;
 }
 
+void interface_update_prefix_delegation(struct interface_ip_settings *ip)
+{
+       struct device_prefix *prefix;
+
+       vlist_for_each_element(&ip->prefix, prefix, node) {
+               interface_update_prefix_assignments(prefix, !ip->no_delegation);
+
+               if (ip->no_delegation) {
+                       if (prefix->head.next)
+                               list_del(&prefix->head);
+               } else
+                       list_add(&prefix->head, &prefixes);
+       }
+}
 
 static void
 interface_update_prefix(struct vlist_tree *tree,
@@ -1073,7 +1227,7 @@ interface_update_prefix(struct vlist_tree *tree,
        struct interface *iface;
 
        if (node_old && node_new) {
-               // Move assignments and refresh addresses to update valid times
+               /* 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)
@@ -1084,13 +1238,13 @@ interface_update_prefix(struct vlist_tree *tree,
                                prefix_new->valid_until != prefix_old->valid_until)
                        ip->iface->updated |= IUF_PREFIX;
        } else if (node_new) {
-               // Set null-route to avoid routing loops
+               /* 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
+               /* Remove null-route */
                interface_update_prefix_assignments(prefix_old, false);
                system_del_route(NULL, &route);
        }
@@ -1172,7 +1326,7 @@ interface_ip_set_ula_prefix(const char *prefix)
        }
 }
 
-void
+static void
 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
 {
        struct dns_server *s;
@@ -1208,7 +1362,7 @@ interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr
                if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
                        continue;
 
-               if (!blobmsg_check_attr(cur, NULL))
+               if (!blobmsg_check_attr(cur, false))
                        continue;
 
                interface_add_dns_server(ip, blobmsg_data(cur));
@@ -1240,7 +1394,7 @@ interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr
                if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
                        continue;
 
-               if (!blobmsg_check_attr(cur, NULL))
+               if (!blobmsg_check_attr(cur, false))
                        continue;
 
                interface_add_dns_search_domain(ip, blobmsg_data(cur));
@@ -1374,6 +1528,7 @@ void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
 {
        struct device_addr *addr;
        struct device_route *route;
+       struct device_neighbor *neighbor;
        struct device *dev;
        struct interface *iface;
 
@@ -1419,7 +1574,6 @@ void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
 
                if (!enable_route(ip, route))
                        _enabled = false;
-
                if (route->enabled == _enabled)
                        continue;
 
@@ -1433,6 +1587,19 @@ void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
                route->enabled = _enabled;
        }
 
+       vlist_for_each_element(&ip->neighbor, neighbor, node) {
+               if (neighbor->enabled == enabled)
+                       continue;
+
+               if (enabled) {
+                       if(system_add_neighbor(dev, neighbor))
+                               neighbor->failed = true;
+               } else
+                       system_del_neighbor(dev, neighbor);
+
+               neighbor->enabled = enabled;
+       }
+
        struct device_prefix *c;
        struct device_prefix_assignment *a;
        list_for_each_entry(c, &prefixes, head)
@@ -1440,7 +1607,7 @@ void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
                        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 &&
+       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);
@@ -1461,6 +1628,7 @@ interface_ip_update_start(struct interface_ip_settings *ip)
        vlist_update(&ip->route);
        vlist_update(&ip->addr);
        vlist_update(&ip->prefix);
+       vlist_update(&ip->neighbor);
 }
 
 void
@@ -1471,6 +1639,7 @@ interface_ip_update_complete(struct interface_ip_settings *ip)
        vlist_flush(&ip->route);
        vlist_flush(&ip->addr);
        vlist_flush(&ip->prefix);
+       vlist_flush(&ip->neighbor);
        interface_write_resolv_conf();
 }
 
@@ -1483,6 +1652,7 @@ interface_ip_flush(struct interface_ip_settings *ip)
        vlist_simple_flush_all(&ip->dns_search);
        vlist_flush_all(&ip->route);
        vlist_flush_all(&ip->addr);
+       vlist_flush_all(&ip->neighbor);
        vlist_flush_all(&ip->prefix);
 }
 
@@ -1494,6 +1664,7 @@ __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
        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->neighbor, neighbor_cmp, interface_update_proto_neighbor);
        vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
        vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
 }