X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fnetwork%2Fnetworkd-manager.c;h=ad36553f2b48e8979cce2d5c67cd46950a1281d2;hp=4c1987daa8214fc6030185cb98732e2253caa4c9;hb=2dcf7ec6ec0c28297311108acba119cd6e055e64;hpb=505f8da7325591defe5f751f328bd26915267602 diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index 4c1987daa..ad36553f2 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -20,9 +20,13 @@ ***/ #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" @@ -72,6 +76,95 @@ static int setup_signals(Manager *m) { return 0; } +static int set_fallback_dns(Manager *m, const char *string) { + char *word, *state; + size_t length; + int r; + + assert(m); + assert(string); + + FOREACH_WORD_QUOTED(word, length, string, state) { + _cleanup_address_free_ Address *address = NULL; + Address *tail; + _cleanup_free_ char *addrstr = NULL; + + r = address_new_dynamic(&address); + if (r < 0) + return r; + + addrstr = strndup(word, length); + if (!addrstr) + return -ENOMEM; + + r = net_parse_inaddr(addrstr, &address->family, &address->in_addr); + if (r < 0) { + log_debug("Ignoring invalid DNS address '%s'", addrstr); + continue; + } + + LIST_FIND_TAIL(addresses, m->fallback_dns, tail); + LIST_INSERT_AFTER(addresses, m->fallback_dns, tail, address); + address = NULL; + } + + return 0; +} + +int config_parse_dnsv( + 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) { + + Manager *m = userdata; + Address *address; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(m); + + while ((address = m->fallback_dns)) { + LIST_REMOVE(addresses, m->fallback_dns, address); + address_free(address); + } + + set_fallback_dns(m, rvalue); + + return 0; +} + +static int manager_parse_config_file(Manager *m) { + static const char fn[] = "/etc/systemd/networkd.conf"; + _cleanup_fclose_ FILE *f = NULL; + int r; + + assert(m); + + f = fopen(fn, "re"); + if (!f) { + if (errno == ENOENT) + return 0; + + log_warning("Failed to open configuration file %s: %m", fn); + return -errno; + } + + r = config_parse(NULL, fn, f, "Network\0", config_item_perf_lookup, + (void*) networkd_gperf_lookup, false, false, m); + if (r < 0) + log_warning("Failed to parse configuration file: %s", strerror(-r)); + + return r; +} + int manager_new(Manager **ret) { _cleanup_manager_free_ Manager *m = NULL; int r; @@ -80,13 +173,26 @@ int manager_new(Manager **ret) { if (!m) return -ENOMEM; + m->state_file = strdup("/run/systemd/network/state"); + if (!m->state_file) + return -ENOMEM; + + r = set_fallback_dns(m, DNS_SERVERS); + if (r < 0) + return r; + + r = manager_parse_config_file(m); + if (r < 0) + return r; + 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; @@ -111,6 +217,10 @@ int manager_new(Manager **ret) { return -ENOMEM; } + m->kmod_ctx = kmod_new(NULL, NULL); + if (!m->kmod_ctx) + return -ENOMEM; + m->links = hashmap_new(uint64_hash_func, uint64_compare_func); if (!m->links) return -ENOMEM; @@ -131,10 +241,14 @@ void manager_free(Manager *m) { Network *network; NetDev *netdev; Link *link; + Address *address; if (!m) return; + free(m->state_file); + + kmod_unref(m->kmod_ctx); udev_monitor_unref(m->udev_monitor); udev_unref(m->udev); sd_bus_unref(m->bus); @@ -143,15 +257,20 @@ void manager_free(Manager *m) { sd_event_source_unref(m->sigint_event_source); sd_event_unref(m->event); + while ((address = m->fallback_dns)) { + LIST_REMOVE(addresses, m->fallback_dns, address); + address_free(address); + } + 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 +301,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 +309,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 +327,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 +340,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 +492,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; } @@ -457,15 +590,22 @@ int manager_update_resolv_conf(Manager *m) { HASHMAP_FOREACH(link, m->links, i) { if (link->network && link->network->dns) { Address *address; - Iterator j; - SET_FOREACH(address, link->network->dns, j) { + LIST_FOREACH(addresses, address, link->network->dns) { append_dns(f, &address->in_addr.in, address->family, &count); } } } + if (!count) { + Address *address; + + LIST_FOREACH(addresses, address, m->fallback_dns) + append_dns(f, &address->in_addr.in, + address->family, &count); + } + fflush(f); if (ferror(f) || rename(temp_path, "/run/systemd/network/resolv.conf") < 0) { @@ -477,3 +617,55 @@ int manager_update_resolv_conf(Manager *m) { return 0; } + +int manager_save(Manager *m) { + Link *link; + Iterator i; + _cleanup_free_ char *temp_path = NULL; + _cleanup_fclose_ FILE *f = NULL; + const char *oper_state = "unknown"; + bool dormant = false, carrier = false; + int r; + + assert(m); + assert(m->state_file); + + HASHMAP_FOREACH(link, m->links, i) { + if (link->flags & IFF_LOOPBACK) + continue; + + if (link_has_carrier(link->flags, link->operstate)) + carrier = true; + else if (link->operstate == IF_OPER_DORMANT) + dormant = true; + } + + if (carrier) + oper_state = "carrier"; + else if (dormant) + oper_state = "dormant"; + + 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", oper_state); + + fflush(f); + + if (ferror(f) || rename(temp_path, m->state_file) < 0) { + r = -errno; + unlink(m->state_file); + unlink(temp_path); + } + +finish: + if (r < 0) + log_error("Failed to save network state to %s: %s", m->state_file, strerror(-r)); + + return r; +}