X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fnetwork%2Fnetworkd-manager.c;h=2a0d5342daeb4672f84bc9aa3fda69e71d0cc2ea;hb=1324e3ae515719a93a94a8d8316644af6c780909;hp=4c1987daa8214fc6030185cb98732e2253caa4c9;hpb=505f8da7325591defe5f751f328bd26915267602;p=elogind.git diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index 4c1987daa..2a0d5342d 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -19,10 +19,13 @@ along with systemd; If not, see . ***/ -#include +#include +#include +#include "conf-parser.h" #include "path-util.h" #include "networkd.h" +#include "network-internal.h" #include "libudev-private.h" #include "udev-util.h" #include "rtnl-util.h" @@ -80,13 +83,18 @@ int manager_new(Manager **ret) { if (!m) return -ENOMEM; + m->state_file = strdup("/run/systemd/netif/state"); + if (!m->state_file) + return -ENOMEM; + r = sd_event_default(&m->event); if (r < 0) return r; sd_event_set_watchdog(m->event, true); - r = sd_rtnl_open(&m->rtnl, RTMGRP_LINK | RTMGRP_IPV4_IFADDR); + r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR, + RTNLGRP_IPV6_IFADDR); if (r < 0) return r; @@ -135,6 +143,8 @@ void manager_free(Manager *m) { if (!m) return; + free(m->state_file); + udev_monitor_unref(m->udev_monitor); udev_unref(m->udev); sd_bus_unref(m->bus); @@ -144,14 +154,14 @@ void manager_free(Manager *m) { sd_event_unref(m->event); while ((link = hashmap_first(m->links))) - link_free(link); + link_unref(link); hashmap_free(m->links); while ((network = m->networks)) network_free(network); while ((netdev = hashmap_first(m->netdevs))) - netdev_free(netdev); + netdev_unref(netdev); hashmap_free(m->netdevs); sd_rtnl_unref(m->rtnl); @@ -182,7 +192,7 @@ bool manager_should_reload(Manager *m) { static int manager_udev_process_link(Manager *m, struct udev_device *device) { Link *link = NULL; - int r; + int r, ifindex; assert(m); assert(device); @@ -190,12 +200,17 @@ static int manager_udev_process_link(Manager *m, struct udev_device *device) { if (!streq_ptr(udev_device_get_action(device), "add")) return 0; - r = link_get(m, udev_device_get_ifindex(device), &link); - if (r < 0) - return r; + ifindex = udev_device_get_ifindex(device); + if (ifindex <= 0) { + log_debug("ignoring udev ADD event for device with invalid ifindex"); + return 0; + } - if (!link) + r = link_get(m, ifindex, &link); + if (r == -ENODEV) return 0; + else if (r < 0) + return r; r = link_initialized(link, device); if (r < 0) @@ -203,35 +218,12 @@ static int manager_udev_process_link(Manager *m, struct udev_device *device) { return 0; } -/* - if (streq_ptr(udev_device_get_action(device), "remove")) { - log_debug("%s: link removed", udev_device_get_sysname(device)); - - if (link) - link_free(link); - } else { - if (link) { - log_debug("%s: link already exists, ignoring", - link->ifname); - return 0; - } - - r = link_add(m, device, &link); - if (r < 0) { - log_error("%s: could not handle link: %s", - udev_device_get_sysname(device), - strerror(-r)); - } else - log_debug("%s: link (with ifindex %" PRIu64") added", - link->ifname, link->ifindex); - } - return 0; -} -*/ static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) { Manager *m = userdata; Link *link = NULL; + NetDev *netdev = NULL; + uint16_t type; char *name; int r, ifindex; @@ -239,41 +231,61 @@ static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, vo assert(message); assert(m); + r = sd_rtnl_message_get_type(message, &type); + if (r < 0) { + log_warning("rtnl: could not get message type"); + return 0; + } + r = sd_rtnl_message_link_get_ifindex(message, &ifindex); if (r < 0 || ifindex <= 0) { log_warning("rtnl: received link message without valid ifindex"); return 0; - } - - link_get(m, ifindex, &link); - if (!link) { - /* link is new, so add it */ - r = link_add(m, message, &link); - if (r < 0) { - log_debug("could not add new link"); - return 0; - } - } + } else + link_get(m, ifindex, &link); r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &name); - if (r < 0) + if (r < 0 || !name) { log_warning("rtnl: received link message without valid ifname"); - else { - NetDev *netdev; + return 0; + } else + netdev_get(m, name, &netdev); + + switch (type) { + case RTM_NEWLINK: + if (!link) { + /* link is new, so add it */ + r = link_add(m, message, &link); + if (r < 0) { + log_debug("could not add new link"); + return 0; + } + } - r = netdev_get(m, name, &netdev); - if (r >= 0) { + if (netdev) { + /* netdev exists, so make sure the ifindex matches */ r = netdev_set_ifindex(netdev, message); if (r < 0) { log_debug("could not set ifindex on netdev"); return 0; } } - } - r = link_update(link, message); - if (r < 0) - return 0; + r = link_update(link, message); + if (r < 0) + return 0; + + break; + + case RTM_DELLINK: + link_drop(link); + netdev_drop(netdev); + + break; + + default: + assert_not_reached("Received invalid RTNL message type."); + } return 1; } @@ -371,6 +383,18 @@ int manager_rtnl_listen(Manager *m) { if (r < 0) return r; + r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m); + if (r < 0) + return r; + + r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, &link_rtnl_process_address, m); + if (r < 0) + return r; + + r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, &link_rtnl_process_address, m); + if (r < 0) + return r; + return 0; } @@ -389,91 +413,50 @@ int manager_bus_listen(Manager *m) { return 0; } -static void append_dns(FILE *f, struct in_addr *dns, unsigned char family, unsigned *count) { - char buf[INET6_ADDRSTRLEN]; - const char *address; - - address = inet_ntop(family, dns, buf, INET6_ADDRSTRLEN); - if (!address) { - log_warning("Invalid DNS address. Ignoring."); - return; - } - - if (*count == MAXNS) - fputs("# Too many DNS servers configured, the following entries " - "will be ignored\n", f); - - fprintf(f, "nameserver %s\n", address); - - (*count) ++; -} - -int manager_update_resolv_conf(Manager *m) { - _cleanup_free_ char *temp_path = NULL; - _cleanup_fclose_ FILE *f = NULL; +int manager_save(Manager *m) { Link *link; Iterator i; - unsigned count = 0; - const char *domainname = NULL; + _cleanup_free_ char *temp_path = NULL; + _cleanup_fclose_ FILE *f = NULL; + LinkOperationalState operstate = LINK_OPERSTATE_UNKNOWN; + const char *operstate_str; int r; assert(m); - - r = fopen_temporary("/run/systemd/network/resolv.conf", &f, &temp_path); - if (r < 0) - return r; - - fchmod(fileno(f), 0644); - - fputs("# This file is managed by systemd-networkd(8). Do not edit.\n#\n" - "# Third party programs must not access this file directly, but\n" - "# only through the symlink at /etc/resolv.conf. To manage\n" - "# resolv.conf(5) in a different way, replace the symlink by a\n" - "# static file or a different symlink.\n\n", f); + assert(m->state_file); HASHMAP_FOREACH(link, m->links, i) { - if (link->dhcp_lease) { - struct in_addr *nameservers; - size_t nameservers_size; - - if (link->network->dhcp_dns) { - r = sd_dhcp_lease_get_dns(link->dhcp_lease, &nameservers, &nameservers_size); - if (r >= 0) { - unsigned j; - - for (j = 0; j < nameservers_size; j++) - append_dns(f, &nameservers[j], AF_INET, &count); - } - } + if (link->flags & IFF_LOOPBACK) + continue; - if (link->network->dhcp_domainname && !domainname) { - r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname); - if (r >= 0) - fprintf(f, "domain %s\n", domainname); - } - } + if (link->operstate > operstate) + operstate = link->operstate; } - HASHMAP_FOREACH(link, m->links, i) { - if (link->network && link->network->dns) { - Address *address; - Iterator j; + operstate_str = link_operstate_to_string(operstate); + assert(operstate_str); - SET_FOREACH(address, link->network->dns, j) { - append_dns(f, &address->in_addr.in, - address->family, &count); - } - } - } + r = fopen_temporary(m->state_file, &f, &temp_path); + if (r < 0) + goto finish; + + fchmod(fileno(f), 0644); + + fprintf(f, + "# This is private data. Do not parse.\n" + "OPER_STATE=%s\n", operstate_str); fflush(f); - if (ferror(f) || rename(temp_path, "/run/systemd/network/resolv.conf") < 0) { + if (ferror(f) || rename(temp_path, m->state_file) < 0) { r = -errno; - unlink("/run/systemd/network/resolv.conf"); + unlink(m->state_file); unlink(temp_path); - return r; } - return 0; +finish: + if (r < 0) + log_error("Failed to save network state to %s: %s", m->state_file, strerror(-r)); + + return r; }