interface: add neighbor config support
[project/netifd.git] / interface-ip.c
index 9cea0a6e7b21cf20a661f15eeef817c5195a1807..22c21d7a85ff41adfb6ccc2d39f72cd5b787978b 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,7 @@ __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
+               /* Handle offlink addresses correctly */
                unsigned int mask = addr->mask;
                if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6 &&
                                (addr->flags & DEVADDR_OFFLINK))
@@ -298,6 +324,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));
+       neighbor->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
+
+       if (!neighbor)
+               return;
+
+       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 +441,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 +477,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 +512,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 +664,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 +716,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,
@@ -908,7 +1040,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);
@@ -921,7 +1053,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;
@@ -932,7 +1064,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));
@@ -959,7 +1091,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;
 
@@ -987,7 +1119,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;
@@ -1051,6 +1183,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,
@@ -1077,7 +1223,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)
@@ -1088,13 +1234,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);
        }
@@ -1378,6 +1524,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;
 
@@ -1423,7 +1570,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;
 
@@ -1437,6 +1583,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)
@@ -1465,6 +1624,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
@@ -1475,6 +1635,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();
 }
 
@@ -1487,6 +1648,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);
 }
 
@@ -1498,6 +1660,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);
 }