[systemd-commits] 3 commits - Makefile.am src/network

Tom Gundersen tomegun at kemper.freedesktop.org
Wed Jan 1 07:30:51 PST 2014


 Makefile.am                      |    2 
 src/network/networkd-address.c   |   70 +++++++++-
 src/network/networkd-gperf.gperf |    1 
 src/network/networkd-link.c      |  250 +++++++++++++++++++++++++++++++++++----
 src/network/networkd-network.c   |    8 -
 src/network/networkd-route.c     |   31 +++-
 src/network/networkd.h           |   25 ++-
 7 files changed, 338 insertions(+), 49 deletions(-)

New commits:
commit f5be560181d092c5f52a2b819aedcd48220f36ab
Author: Tom Gundersen <teg at jklm.no>
Date:   Wed Jan 1 15:16:34 2014 +0100

    networkd: add DHCPv4 support
    
    This adds basic DHCPv4 support. Link-sense is enabled unconditionally,
    but the plan is to make that configurable.
    
    I tested this in a VM with lots of NICs and over wifi in the various
    coffee shops I found this Christmas, but more testing would definitely
    be appreciated.

diff --git a/Makefile.am b/Makefile.am
index 5f31daa..44b6917 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -4056,6 +4056,7 @@ systemd_networkd_LDADD = \
 	libsystemd-bus-internal.la \
 	libsystemd-id128-internal.la \
 	libsystemd-rtnl.la \
+	libsystemd-dhcp.la \
 	libsystemd-shared.la
 
 nodist_systemunit_DATA += \
@@ -4081,6 +4082,7 @@ test_network_LDADD = \
 	libsystemd-id128-internal.la \
 	libsystemd-daemon-internal.la \
 	libsystemd-rtnl.la \
+	libsystemd-dhcp.la \
 	libsystemd-shared.la
 
 tests += \
diff --git a/src/network/networkd-gperf.gperf b/src/network/networkd-gperf.gperf
index f710df6..f1594d5 100644
--- a/src/network/networkd-gperf.gperf
+++ b/src/network/networkd-gperf.gperf
@@ -22,6 +22,7 @@ Match.Type,              config_parse_string,           0,       offsetof(Networ
 Match.Name,              config_parse_ifname,           0,       offsetof(Network, match_name)
 Network.Description,     config_parse_string,           0,       offsetof(Network, description)
 Network.Bridge,          config_parse_bridge,           0,       offsetof(Network, bridge)
+Network.DHCP,            config_parse_bool,             0,       offsetof(Network, dhcp)
 Network.Address,         config_parse_address,          0,       0
 Network.Gateway,         config_parse_gateway,          0,       0
 Address.Address,         config_parse_address,          0,       0
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index ea94966..3d01625 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -73,6 +73,15 @@ void link_free(Link *link) {
 
         assert(link->manager);
 
+        if (link->dhcp)
+                sd_dhcp_client_free(link->dhcp);
+
+        route_free(link->dhcp_route);
+        link->dhcp_route = NULL;
+
+        address_free(link->dhcp_address);
+        link->dhcp_address = NULL;
+
         hashmap_remove(link->manager->links, &link->ifindex);
 
         free(link->ifname);
@@ -140,10 +149,12 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         Link *link = userdata;
         int r;
 
-        assert(link->rtnl_messages > 0);
-        assert(link->state == LINK_STATE_SETTING_ROUTES || link->state == LINK_STATE_FAILED);
+        assert(link->route_messages > 0);
+        assert(link->state == LINK_STATE_SETTING_ADDRESSES ||
+               link->state == LINK_STATE_SETTING_ROUTES ||
+               link->state == LINK_STATE_FAILED);
 
-        link->rtnl_messages --;
+        link->route_messages --;
 
         if (link->state == LINK_STATE_FAILED)
                 return 1;
@@ -153,7 +164,9 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
                 log_warning("Could not set route on interface '%s': %s",
                             link->ifname, strerror(-r));
 
-        if (link->rtnl_messages == 0) {
+        /* we might have received an old reply after moving back to SETTING_ADDRESSES,
+         * ignore it */
+        if (link->route_messages == 0 && link->state == LINK_STATE_SETTING_ROUTES) {
                 log_info("Routes set for link '%s'", link->ifname);
                 link_enter_configured(link);
         }
@@ -167,12 +180,11 @@ static int link_enter_set_routes(Link *link) {
 
         assert(link);
         assert(link->network);
-        assert(link->rtnl_messages == 0);
         assert(link->state == LINK_STATE_SETTING_ADDRESSES);
 
         link->state = LINK_STATE_SETTING_ROUTES;
 
-        if (!link->network->static_routes)
+        if (!link->network->static_routes && !link->dhcp_route)
                 return link_enter_configured(link);
 
         LIST_FOREACH(static_routes, route, link->network->static_routes) {
@@ -183,7 +195,18 @@ static int link_enter_set_routes(Link *link) {
                         return r;
                 }
 
-                link->rtnl_messages ++;
+                link->route_messages ++;
+        }
+
+        if (link->dhcp_route) {
+                r = route_configure(link->dhcp_route, link, &route_handler);
+                if (r < 0) {
+                        log_warning("Could not set routes for link '%s'", link->ifname);
+                        link_enter_failed(link);
+                        return r;
+                }
+
+                link->route_messages ++;
         }
 
         return 0;
@@ -193,10 +216,13 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         Link *link = userdata;
         int r;
 
-        assert(link->rtnl_messages > 0);
+        assert(m);
+        assert(link);
+        assert(link->ifname);
+        assert(link->addr_messages > 0);
         assert(link->state == LINK_STATE_SETTING_ADDRESSES || link->state == LINK_STATE_FAILED);
 
-        link->rtnl_messages --;
+        link->addr_messages --;
 
         if (link->state == LINK_STATE_FAILED)
                 return 1;
@@ -206,7 +232,7 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
                 log_warning("Could not set address on interface '%s': %s",
                             link->ifname, strerror(-r));
 
-        if (link->rtnl_messages == 0) {
+        if (link->addr_messages == 0) {
                 log_info("Addresses set for link '%s'", link->ifname);
                 link_enter_set_routes(link);
         }
@@ -220,12 +246,11 @@ static int link_enter_set_addresses(Link *link) {
 
         assert(link);
         assert(link->network);
-        assert(link->state == LINK_STATE_JOINING_BRIDGE);
-        assert(link->rtnl_messages == 0);
+        assert(link->state != _LINK_STATE_INVALID);
 
         link->state = LINK_STATE_SETTING_ADDRESSES;
 
-        if (!link->network->static_addresses)
+        if (!link->network->static_addresses && !link->dhcp_address)
                 return link_enter_set_routes(link);
 
         LIST_FOREACH(static_addresses, address, link->network->static_addresses) {
@@ -236,7 +261,18 @@ static int link_enter_set_addresses(Link *link) {
                         return r;
                 }
 
-                link->rtnl_messages ++;
+                link->addr_messages ++;
+        }
+
+        if (link->dhcp_address) {
+                r = address_configure(link->dhcp_address, link, &address_handler);
+                if (r < 0) {
+                        log_warning("Could not set addresses for link '%s'", link->ifname);
+                        link_enter_failed(link);
+                        return r;
+                }
+
+                link->addr_messages ++;
         }
 
         return 0;
@@ -290,6 +326,7 @@ static int link_bridge_joined(Link *link) {
 
         assert(link);
         assert(link->state == LINK_STATE_JOINING_BRIDGE);
+        assert(link->network);
 
         r = link_up(link);
         if (r < 0) {
@@ -297,10 +334,11 @@ static int link_bridge_joined(Link *link) {
                 return r;
         }
 
-        r = link_enter_set_addresses(link);
-        if (r < 0) {
-                link_enter_failed(link);
-                return r;
+        if (!link->network->dhcp) {
+                r = link_enter_set_addresses(link);
+                if (r < 0)
+                        link_enter_failed(link);
+                        return r;
         }
 
         return 0;
@@ -411,6 +449,157 @@ int link_configure(Link *link) {
         return 0;
 }
 
+static int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+        Link *link = userdata;
+        int r;
+
+        assert(m);
+        assert(link);
+        assert(link->ifname);
+
+        if (link->state == LINK_STATE_FAILED)
+                return 1;
+
+        r = sd_rtnl_message_get_errno(m);
+        if (r < 0 && r != -EEXIST)
+                log_warning("Could not drop address from interface '%s': %s",
+                            link->ifname, strerror(-r));
+
+        return 1;
+}
+
+static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
+        Link *link = userdata;
+        struct in_addr address;
+        struct in_addr netmask;
+        struct in_addr gateway;
+        int prefixlen;
+        int r;
+
+        if (link->state == LINK_STATE_FAILED)
+                return;
+
+        if (event < 0) {
+                log_warning("DHCP error: %s", strerror(-event));
+                link_enter_failed(link);
+                return;
+        }
+
+        if (event == DHCP_EVENT_NO_LEASE)
+                log_info("IP address in use.");
+
+        if (event == DHCP_EVENT_IP_CHANGE || event == DHCP_EVENT_EXPIRED ||
+            event == DHCP_EVENT_STOP) {
+                address_drop(link->dhcp_address, link, address_drop_handler);
+
+                address_free(link->dhcp_address);
+                link->dhcp_address = NULL;
+
+                route_free(link->dhcp_route);
+                link->dhcp_route = NULL;
+        }
+
+        r = sd_dhcp_client_get_address(client, &address);
+        if (r < 0) {
+                log_warning("DHCP error: no address");
+                link_enter_failed(link);
+                return;
+        }
+
+        r = sd_dhcp_client_get_netmask(client, &netmask);
+        if (r < 0) {
+                log_warning("DHCP error: no netmask");
+                link_enter_failed(link);
+                return;
+        }
+
+        prefixlen = sd_dhcp_client_prefixlen(&netmask);
+        if (prefixlen < 0) {
+                log_warning("DHCP error: no prefixlen");
+                link_enter_failed(link);
+                return;
+        }
+
+        r = sd_dhcp_client_get_router(client, &gateway);
+        if (r < 0) {
+                log_warning("DHCP error: no router");
+                link_enter_failed(link);
+                return;
+        }
+
+        if (event == DHCP_EVENT_IP_CHANGE || event == DHCP_EVENT_IP_ACQUIRE) {
+                _cleanup_address_free_ Address *addr = NULL;
+                _cleanup_route_free_ Route *rt = NULL;
+
+                log_info("Received config over DHCPv4");
+
+                r = address_new_dynamic(&addr);
+                if (r < 0) {
+                        log_error("Could not allocate address");
+                        link_enter_failed(link);
+                        return;
+                }
+
+                addr->family = AF_INET;
+                addr->in_addr.in = address;
+                addr->prefixlen = prefixlen;
+                addr->netmask = netmask;
+
+                r = route_new_dynamic(&rt);
+                if (r < 0) {
+                        log_error("Could not allocate route");
+                        link_enter_failed(link);
+                        return;
+                }
+
+                rt->family = AF_INET;
+                rt->in_addr.in = gateway;
+
+                link->dhcp_address = addr;
+                link->dhcp_route = rt;
+                addr = NULL;
+                rt = NULL;
+
+                link_enter_set_addresses(link);
+        }
+
+        return;
+}
+
+static int link_acquire_conf(Link *link) {
+        int r;
+
+        assert(link);
+        assert(link->network);
+        assert(link->network->dhcp);
+        assert(link->manager);
+        assert(link->manager->event);
+
+        if (!link->dhcp) {
+                link->dhcp = sd_dhcp_client_new(link->manager->event);
+                if (!link->dhcp)
+                        return -ENOMEM;
+
+                r = sd_dhcp_client_set_index(link->dhcp, link->ifindex);
+                if (r < 0)
+                        return r;
+
+                r = sd_dhcp_client_set_mac(link->dhcp, &link->mac);
+                if (r < 0)
+                        return r;
+
+                r = sd_dhcp_client_set_callback(link->dhcp, dhcp_handler, link);
+                if (r < 0)
+                        return r;
+        }
+
+        r = sd_dhcp_client_start(link->dhcp);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
 int link_update(Link *link, sd_rtnl_message *m) {
         unsigned flags;
         int r;
@@ -429,11 +618,28 @@ int link_update(Link *link, sd_rtnl_message *m) {
         else if (!(link->flags & IFF_UP) && flags & IFF_UP)
                 log_info("Interface '%s' is up", link->ifname);
 
-        if (link->flags & IFF_LOWER_UP && !(flags & IFF_LOWER_UP))
+        if (link->flags & IFF_LOWER_UP && !(flags & IFF_LOWER_UP)) {
                 log_info("Interface '%s' is disconnected", link->ifname);
-        else if (!(link->flags & IFF_LOWER_UP) && flags & IFF_LOWER_UP)
+
+                if (link->network->dhcp) {
+                        r = sd_dhcp_client_stop(link->dhcp);
+                        if (r < 0) {
+                                link_enter_failed(link);
+                                return r;
+                        }
+                }
+        } else if (!(link->flags & IFF_LOWER_UP) && flags & IFF_LOWER_UP) {
                 log_info("Interface '%s' is connected", link->ifname);
 
+                if (link->network->dhcp) {
+                        r = link_acquire_conf(link);
+                        if (r < 0) {
+                                link_enter_failed(link);
+                                return r;
+                        }
+                }
+        }
+
         link->flags = flags;
 
         return 0;
diff --git a/src/network/networkd.h b/src/network/networkd.h
index 547533f..cad81d9 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -26,6 +26,7 @@
 
 #include "sd-event.h"
 #include "sd-rtnl.h"
+#include "sd-dhcp-client.h"
 #include "udev.h"
 
 #include "rtnl-util.h"
@@ -84,6 +85,7 @@ struct Network {
 
         char *description;
         Bridge *bridge;
+        bool dhcp;
 
         LIST_HEAD(Address, static_addresses);
         LIST_HEAD(Route, static_routes);
@@ -153,9 +155,15 @@ struct Link {
 
         Network *network;
 
+        Route *dhcp_route;
+        Address *dhcp_address;
+
         LinkState state;
 
-        unsigned rtnl_messages;
+        unsigned addr_messages;
+        unsigned route_messages;
+
+        sd_dhcp_client *dhcp;
 };
 
 struct Manager {

commit f048a16b464295a4e0a4f4c1210f06343ad31231
Author: Tom Gundersen <teg at jklm.no>
Date:   Wed Jan 1 15:16:10 2014 +0100

    networkd: distinguish between static and dynamic addresses/routes
    
    Static addresses/routes are associated with a network. Dynamic
    addresses/routes are associtade with links (as the corresponding network
    may be shared by several links).

diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
index c0cc128..8a71630 100644
--- a/src/network/networkd-address.c
+++ b/src/network/networkd-address.c
@@ -28,7 +28,7 @@
 #include "conf-parser.h"
 #include "net-util.h"
 
-int address_new(Network *network, unsigned section, Address **ret) {
+int address_new_static(Network *network, unsigned section, Address **ret) {
         _cleanup_address_free_ Address *address = NULL;
 
         if (section) {
@@ -48,7 +48,7 @@ int address_new(Network *network, unsigned section, Address **ret) {
 
         address->network = network;
 
-        LIST_PREPEND(addresses, network->addresses, address);
+        LIST_PREPEND(static_addresses, network->static_addresses, address);
 
         if (section) {
                 address->section = section;
@@ -61,15 +61,30 @@ int address_new(Network *network, unsigned section, Address **ret) {
         return 0;
 }
 
+int address_new_dynamic(Address **ret) {
+        _cleanup_address_free_ Address *address = NULL;
+
+        address = new0(Address, 1);
+        if (!address)
+                return -ENOMEM;
+
+        *ret = address;
+        address = NULL;
+
+        return 0;
+}
+
 void address_free(Address *address) {
         if (!address)
                 return;
 
-        LIST_REMOVE(addresses, address->network->addresses, address);
+        if (address->network) {
+                LIST_REMOVE(static_addresses, address->network->static_addresses, address);
 
-        if (address->section)
-                hashmap_remove(address->network->addresses_by_section,
-                               &address->section);
+                if (address->section)
+                        hashmap_remove(address->network->addresses_by_section,
+                                       &address->section);
+        }
 
         free(address);
 }
@@ -203,7 +218,7 @@ int config_parse_address(const char *unit,
                 section_line = 0;
         }
 
-        r = address_new(network, section_line, &n);
+        r = address_new_static(network, section_line, &n);
         if (r < 0)
                 return r;
 
@@ -266,7 +281,7 @@ int config_parse_label(const char *unit,
         assert(rvalue);
         assert(data);
 
-        r = address_new(network, section_line, &n);
+        r = address_new_static(network, section_line, &n);
         if (r < 0)
                 return r;
 
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index cc5441b..ea94966 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -172,10 +172,10 @@ static int link_enter_set_routes(Link *link) {
 
         link->state = LINK_STATE_SETTING_ROUTES;
 
-        if (!link->network->routes)
+        if (!link->network->static_routes)
                 return link_enter_configured(link);
 
-        LIST_FOREACH(routes, route, link->network->routes) {
+        LIST_FOREACH(static_routes, route, link->network->static_routes) {
                 r = route_configure(route, link, &route_handler);
                 if (r < 0) {
                         log_warning("Could not set routes for link '%s'", link->ifname);
@@ -225,10 +225,10 @@ static int link_enter_set_addresses(Link *link) {
 
         link->state = LINK_STATE_SETTING_ADDRESSES;
 
-        if (!link->network->addresses)
+        if (!link->network->static_addresses)
                 return link_enter_set_routes(link);
 
-        LIST_FOREACH(addresses, address, link->network->addresses) {
+        LIST_FOREACH(static_addresses, address, link->network->static_addresses) {
                 r = address_configure(address, link, &address_handler);
                 if (r < 0) {
                         log_warning("Could not set addresses for link '%s'", link->ifname);
diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c
index 80c952a..56e5637 100644
--- a/src/network/networkd-network.c
+++ b/src/network/networkd-network.c
@@ -47,8 +47,8 @@ static int network_load_one(Manager *manager, const char *filename) {
 
         network->manager = manager;
 
-        LIST_HEAD_INIT(network->addresses);
-        LIST_HEAD_INIT(network->routes);
+        LIST_HEAD_INIT(network->static_addresses);
+        LIST_HEAD_INIT(network->static_routes);
 
         network->addresses_by_section = hashmap_new(uint64_hash_func, uint64_compare_func);
         if (!network->addresses_by_section)
@@ -120,10 +120,10 @@ void network_free(Network *network) {
 
         free(network->description);
 
-        while ((route = network->routes))
+        while ((route = network->static_routes))
                 route_free(route);
 
-        while ((address = network->addresses))
+        while ((address = network->static_addresses))
                 address_free(address);
 
         hashmap_free(network->addresses_by_section);
diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c
index 3eaefa2..488d3f5 100644
--- a/src/network/networkd-route.c
+++ b/src/network/networkd-route.c
@@ -28,7 +28,7 @@
 #include "conf-parser.h"
 #include "net-util.h"
 
-int route_new(Network *network, unsigned section, Route **ret) {
+int route_new_static(Network *network, unsigned section, Route **ret) {
         _cleanup_route_free_ Route *route = NULL;
 
         if (section) {
@@ -49,7 +49,7 @@ int route_new(Network *network, unsigned section, Route **ret) {
 
         route->network = network;
 
-        LIST_PREPEND(routes, network->routes, route);
+        LIST_PREPEND(static_routes, network->static_routes, route);
 
         if (section) {
                 route->section = section;
@@ -62,15 +62,30 @@ int route_new(Network *network, unsigned section, Route **ret) {
         return 0;
 }
 
+int route_new_dynamic(Route **ret) {
+        _cleanup_route_free_ Route *route = NULL;
+
+        route = new0(Route, 1);
+        if (!route)
+                return -ENOMEM;
+
+        *ret = route;
+        route = NULL;
+
+        return 0;
+}
+
 void route_free(Route *route) {
         if (!route)
                 return;
 
-        LIST_REMOVE(routes, route->network->routes, route);
+        if (route->network) {
+                LIST_REMOVE(static_routes, route->network->static_routes, route);
 
-        if (route->section)
-                hashmap_remove(route->network->routes_by_section,
-                               &route->section);
+                if (route->section)
+                        hashmap_remove(route->network->routes_by_section,
+                                       &route->section);
+        }
 
         free(route);
 }
@@ -160,7 +175,7 @@ int config_parse_gateway(const char *unit,
                 section_line = 0;
         }
 
-        r = route_new(network, section_line, &n);
+        r = route_new_static(network, section_line, &n);
         if (r < 0)
                 return r;
 
@@ -198,7 +213,7 @@ int config_parse_destination(const char *unit,
         assert(rvalue);
         assert(data);
 
-        r = route_new(network, section_line, &n);
+        r = route_new_static(network, section_line, &n);
         if (r < 0)
                 return r;
 
diff --git a/src/network/networkd.h b/src/network/networkd.h
index bb77508..547533f 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -85,8 +85,8 @@ struct Network {
         char *description;
         Bridge *bridge;
 
-        LIST_HEAD(Address, addresses);
-        LIST_HEAD(Route, routes);
+        LIST_HEAD(Address, static_addresses);
+        LIST_HEAD(Route, static_routes);
 
         Hashmap *addresses_by_section;
         Hashmap *routes_by_section;
@@ -109,7 +109,7 @@ struct Address {
                 struct in6_addr in6;
         } in_addr;
 
-        LIST_FIELDS(Address, addresses);
+        LIST_FIELDS(Address, static_addresses);
 };
 
 struct Route {
@@ -129,7 +129,7 @@ struct Route {
                 struct in6_addr in6;
         } dst_addr;
 
-        LIST_FIELDS(Route, routes);
+        LIST_FIELDS(Route, static_routes);
 };
 
 typedef enum LinkState {
@@ -223,7 +223,8 @@ int config_parse_bridge(const char *unit, const char *filename, unsigned line,
 const struct ConfigPerfItem* network_gperf_lookup(const char *key, unsigned length);
 
 /* Route */
-int route_new(Network *network, unsigned section, Route **ret);
+int route_new_static(Network *network, unsigned section, Route **ret);
+int route_new_dynamic(Route **ret);
 void route_free(Route *route);
 int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback);
 
@@ -239,7 +240,8 @@ int config_parse_destination(const char *unit, const char *filename, unsigned li
                              int ltype, const char *rvalue, void *data, void *userdata);
 
 /* Address */
-int address_new(Network *network, unsigned section, Address **ret);
+int address_new_static(Network *network, unsigned section, Address **ret);
+int address_new_dynamic(Address **ret);
 void address_free(Address *address);
 int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback);
 int address_drop(Address *address, Link *link, sd_rtnl_message_handler_t callback);

commit 407fe036a24834203aca6c1eec7d74d9ad3e9ee0
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue Dec 31 17:31:50 2013 +0100

    network: add support for dropping address

diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
index 63ba33e..c0cc128 100644
--- a/src/network/networkd-address.c
+++ b/src/network/networkd-address.c
@@ -74,6 +74,45 @@ void address_free(Address *address) {
         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;
diff --git a/src/network/networkd.h b/src/network/networkd.h
index c28c90f..bb77508 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -242,6 +242,7 @@ int config_parse_destination(const char *unit, const char *filename, unsigned li
 int address_new(Network *network, unsigned section, Address **ret);
 void address_free(Address *address);
 int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback);
+int address_drop(Address *address, Link *link, sd_rtnl_message_handler_t callback);
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
 #define _cleanup_address_free_ _cleanup_(address_freep)



More information about the systemd-commits mailing list