chiark / gitweb /
network: add support for dropping address
[elogind.git] / src / network / networkd-address.c
index 0e582d626cebad8e2cbf6ab39e40bdc90d4ae1c6..c0cc1287aa3a48532a26cc5a2d7ee653bea33347 100644 (file)
 #include "conf-parser.h"
 #include "net-util.h"
 
-int address_new(Network *network, Address **ret) {
+int address_new(Network *network, unsigned section, Address **ret) {
         _cleanup_address_free_ Address *address = NULL;
 
+        if (section) {
+                uint64_t key = section;
+                address = hashmap_get(network->addresses_by_section, &key);
+                if (address) {
+                        *ret = address;
+                        address = NULL;
+
+                        return 0;
+                }
+        }
+
         address = new0(Address, 1);
         if (!address)
                 return -ENOMEM;
@@ -39,6 +50,11 @@ int address_new(Network *network, Address **ret) {
 
         LIST_PREPEND(addresses, network->addresses, address);
 
+        if (section) {
+                address->section = section;
+                hashmap_put(network->addresses_by_section, &address->section, address);
+        }
+
         *ret = address;
         address = NULL;
 
@@ -51,16 +67,63 @@ void address_free(Address *address) {
 
         LIST_REMOVE(addresses, address->network->addresses, address);
 
-        free(address->label);
+        if (address->section)
+                hashmap_remove(address->network->addresses_by_section,
+                               &address->section);
+
         free(address);
 }
 
+int address_drop(Address *address, Link *link,
+                 sd_rtnl_message_handler_t callback) {
+        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+        int r;
+
+        assert(address);
+        assert(address->family == AF_INET || address->family == AF_INET6);
+        assert(link);
+        assert(link->ifindex > 0);
+        assert(link->manager);
+        assert(link->manager->rtnl);
+
+        r = sd_rtnl_message_addr_new(RTM_DELADDR, link->ifindex,
+                        address->family, address->prefixlen, 0, 0, &req);
+        if (r < 0) {
+                log_error("Could not allocate RTM_DELADDR message: %s",
+                          strerror(-r));
+                return r;
+        }
+
+        if (address->family == AF_INET)
+                r = sd_rtnl_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
+        else if (address->family == AF_INET6)
+                r = sd_rtnl_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
+        if (r < 0) {
+                log_error("Could not append IFA_LOCAL attribute: %s",
+                          strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
+        if (r < 0) {
+                log_error("Could not send rtnetlink message: %s", strerror(-r));
+                return r;
+        }
+
+        return 0;
+}
+
 int address_configure(Address *address, Link *link,
                       sd_rtnl_message_handler_t callback) {
         _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
         int r;
 
+        assert(address);
+        assert(address->family == AF_INET || address->family == AF_INET6);
+        assert(link);
+        assert(link->ifindex > 0);
         assert(link->manager);
+        assert(link->manager->rtnl);
 
         r = sd_rtnl_message_addr_new(RTM_NEWADDR, link->ifindex,
                         address->family, address->prefixlen,
@@ -71,7 +134,10 @@ int address_configure(Address *address, Link *link,
                 return r;
         }
 
-        r = sd_rtnl_message_append(req, IFA_LOCAL, &address->in_addr);
+        if (address->family == AF_INET)
+                r = sd_rtnl_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
+        else if (address->family == AF_INET6)
+                r = sd_rtnl_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
         if (r < 0) {
                 log_error("Could not append IFA_LOCAL attribute: %s",
                           strerror(-r));
@@ -83,7 +149,7 @@ int address_configure(Address *address, Link *link,
 
                 broadcast.s_addr = address->in_addr.in.s_addr | address->netmask.s_addr;
 
-                r = sd_rtnl_message_append(req, IFA_BROADCAST, &broadcast);
+                r = sd_rtnl_message_append_in_addr(req, IFA_BROADCAST, &broadcast);
                 if (r < 0) {
                         log_error("Could not append IFA_BROADCAST attribute: %s",
                                   strerror(-r));
@@ -92,7 +158,7 @@ int address_configure(Address *address, Link *link,
         }
 
         if (address->label) {
-                r = sd_rtnl_message_append(req, IFA_LABEL, address->label);
+                r = sd_rtnl_message_append_string(req, IFA_LABEL, address->label);
                 if (r < 0) {
                         log_error("Could not append IFA_LABEL attribute: %s",
                                   strerror(-r));
@@ -106,8 +172,6 @@ int address_configure(Address *address, Link *link,
                 return r;
         }
 
-        link->rtnl_messages ++;
-
         return 0;
 }
 
@@ -121,17 +185,25 @@ int config_parse_address(const char *unit,
                 const char *rvalue,
                 void *data,
                 void *userdata) {
+        Network *network = userdata;
         _cleanup_address_free_ Address *n = NULL;
         _cleanup_free_ char *address = NULL;
         const char *e;
         int r;
 
         assert(filename);
+        assert(section);
         assert(lvalue);
         assert(rvalue);
         assert(data);
 
-        r = address_new(userdata, &n);
+        if (streq(section, "Network")) {
+                /* we are not in an Address section, so treat
+                 * this as the special '0' section */
+                section_line = 0;
+        }
+
+        r = address_new(network, section_line, &n);
         if (r < 0)
                 return r;
 
@@ -172,3 +244,53 @@ int config_parse_address(const char *unit,
 
         return 0;
 }
+
+int config_parse_label(const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
+                unsigned section_line,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
+        Network *network = userdata;
+        _cleanup_address_free_ Address *n = NULL;
+        char *label;
+        int r;
+
+        assert(filename);
+        assert(section);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        r = address_new(network, section_line, &n);
+        if (r < 0)
+                return r;
+
+        label = strdup(rvalue);
+        if (!label)
+                return log_oom();
+
+        if (!ascii_is_valid(label) || strlen(label) >= IFNAMSIZ) {
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+                           "Interface label is not ASCII clean or is too"
+                           " long, ignoring assignment: %s", rvalue);
+                free(label);
+                return 0;
+        }
+
+        free(n->label);
+        if (*label)
+                n->label = label;
+        else {
+                free(label);
+                n->label = NULL;
+        }
+
+        n = NULL;
+
+        return 0;
+}