X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fnetwork%2Fnetworkd-address.c;h=87688a5ae60db115dbc465963a3f50f986ed33aa;hb=ecb08ec6a5c52f2d940f3b8147e2a480affd46e1;hp=75a9bae20197a411e735371b43aa6778d15bc641;hpb=8cd11a0f0f4ca05199e1166f6a07472b296f7455;p=elogind.git diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c index 75a9bae20..87688a5ae 100644 --- a/src/network/networkd-address.c +++ b/src/network/networkd-address.c @@ -26,18 +26,60 @@ #include "utf8.h" #include "util.h" #include "conf-parser.h" -#include "net-util.h" +#include "network-internal.h" -int address_new(Network *network, Address **ret) { +static void address_init(Address *address) { + assert(address); + + address->family = AF_UNSPEC; + address->scope = RT_SCOPE_UNIVERSE; + address->cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME; + address->cinfo.ifa_valid = CACHE_INFO_INFINITY_LIFE_TIME; +} + +int address_new_static(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; + address_init(address); + address->network = network; - LIST_PREPEND(addresses, network->addresses, address); + LIST_PREPEND(static_addresses, network->static_addresses, address); + + if (section) { + address->section = section; + hashmap_put(network->addresses_by_section, &address->section, address); + } + + *ret = address; + address = NULL; + + return 0; +} + +int address_new_dynamic(Address **ret) { + _cleanup_address_free_ Address *address = NULL; + + address = new0(Address, 1); + if (!address) + return -ENOMEM; + + address_init(address); *ret = address; address = NULL; @@ -49,26 +91,103 @@ 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); + } - free(address->label); free(address); } -int address_configure(Manager *manager, Address *address, Link *link) { - _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL; +int address_drop(Address *address, Link *link, + sd_rtnl_message_handler_t callback) { + _cleanup_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_new_addr(link->manager->rtnl, &req, RTM_DELADDR, + link->ifindex, address->family); + if (r < 0) { + log_error("Could not allocate RTM_DELADDR message: %s", + strerror(-r)); + return r; + } + + r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen); + if (r < 0) { + log_error("Could not set prefixlen: %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_update(Address *address, Link *link, + sd_rtnl_message_handler_t callback) { + _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; int r; - r = sd_rtnl_message_addr_new(RTM_NEWADDR, link->ifindex, - address->family, address->prefixlen, - IFA_F_PERMANENT, RT_SCOPE_UNIVERSE, &req); + assert(address); + assert(address->family == AF_INET || address->family == AF_INET6); + assert(link->ifindex > 0); + assert(link->manager); + assert(link->manager->rtnl); + + r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req, + link->ifindex, address->family); if (r < 0) { log_error("Could not allocate RTM_NEWADDR message: %s", strerror(-r)); return r; } - r = sd_rtnl_message_append(req, IFA_LOCAL, &address->in_addr); + r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen); + if (r < 0) { + log_error("Could not set prefixlen: %s", strerror(-r)); + return r; + } + + r = sd_rtnl_message_addr_set_flags(req, IFA_F_PERMANENT); + if (r < 0) { + log_error("Could not set flags: %s", strerror(-r)); + return r; + } + + r = sd_rtnl_message_addr_set_scope(req, address->scope); + if (r < 0) { + log_error("Could not set scope: %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)); @@ -76,11 +195,89 @@ int address_configure(Manager *manager, Address *address, Link *link) { } if (address->family == AF_INET) { - struct in_addr broadcast; + r = sd_rtnl_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast); + if (r < 0) { + log_error("Could not append IFA_BROADCAST attribute: %s", + strerror(-r)); + return r; + } + } + + if (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)); + return r; + } + } + + r = sd_rtnl_message_append_cache_info(req, IFA_CACHEINFO, &address->cinfo); + if (r < 0) { + log_error("Could not append IFA_CACHEINFO 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_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_new_addr(link->manager->rtnl, &req, RTM_NEWADDR, + link->ifindex, address->family); + if (r < 0) { + log_error("Could not allocate RTM_NEWADDR message: %s", + strerror(-r)); + return r; + } + + r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen); + if (r < 0) { + log_error("Could not set prefixlen: %s", strerror(-r)); + return r; + } + + r = sd_rtnl_message_addr_set_flags(req, IFA_F_PERMANENT); + if (r < 0) { + log_error("Could not set flags: %s", strerror(-r)); + return r; + } - broadcast.s_addr = address->in_addr.in.s_addr | address->netmask.s_addr; + r = sd_rtnl_message_addr_set_scope(req, address->scope); + if (r < 0) { + log_error("Could not set scope: %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_message_append(req, IFA_BROADCAST, &broadcast); + if (address->family == AF_INET) { + r = sd_rtnl_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast); if (r < 0) { log_error("Could not append IFA_BROADCAST attribute: %s", strerror(-r)); @@ -89,7 +286,7 @@ int address_configure(Manager *manager, 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)); @@ -97,13 +294,92 @@ int address_configure(Manager *manager, Address *address, Link *link) { } } - r = sd_rtnl_call(manager->rtnl, req, 0, NULL); + 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 config_parse_dns(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) { + Set **dns = data; + _cleanup_address_free_ Address *n = NULL; + int r; + + assert(filename); + assert(section); + assert(lvalue); + assert(rvalue); + assert(data); + + r = address_new_dynamic(&n); + if (r < 0) + return r; + + r = net_parse_inaddr(rvalue, &n->family, &n->in_addr); if (r < 0) { - log_error("Could not configure address: %s", strerror(-r)); - return r != -EEXIST ? r : 0; + log_syntax(unit, LOG_ERR, filename, line, EINVAL, + "DNS address is invalid, ignoring assignment: %s", rvalue); + return 0; } - log_info("Configured interface address"); + set_put(*dns, n); + n = NULL; + + return 0; +} + +int config_parse_broadcast(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; + _cleanup_free_ char *address = NULL; + int r; + + assert(filename); + assert(section); + assert(lvalue); + assert(rvalue); + assert(data); + + r = address_new_static(network, section_line, &n); + if (r < 0) + return r; + + if (n->family == AF_INET6) { + log_syntax(unit, LOG_ERR, filename, line, EINVAL, + "Broadcast is not valid for IPv6 addresses, " + "ignoring assignment: %s", address); + return 0; + } + + r = net_parse_inaddr(address, &n->family, &n->broadcast); + if (r < 0) { + log_syntax(unit, LOG_ERR, filename, line, EINVAL, + "Broadcast is invalid, ignoring assignment: %s", address); + return 0; + } + + n = NULL; return 0; } @@ -112,22 +388,31 @@ int config_parse_address(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; _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_static(network, section_line, &n); if (r < 0) return r; @@ -146,7 +431,6 @@ int config_parse_address(const char *unit, } n->prefixlen = (unsigned char) i; - n->netmask.s_addr = htonl(0xfffffffflu >> n->prefixlen); address = strndup(rvalue, e - rvalue); if (!address) @@ -164,6 +448,60 @@ int config_parse_address(const char *unit, return 0; } + if (n->family == AF_INET && !n->broadcast.s_addr) + n->broadcast.s_addr = n->in_addr.in.s_addr | + htonl(0xfffffffflu >> n->prefixlen); + + n = NULL; + + 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_static(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;