X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fnetwork%2Fnetworkd-address.c;h=aa7927ff78b85e10092135fb9a0d4c08d35232fd;hp=0555d317a40549f86cbf501a11601ac1705be2c5;hb=eb0ea358b688a6f83ff305c6b825c61f12b6dcb8;hpb=c166a070553511e402de5ad216d3fb62b49bdacb diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c index 0555d317a..aa7927ff7 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) { @@ -46,9 +46,11 @@ int address_new(Network *network, unsigned section, Address **ret) { if (!address) return -ENOMEM; + address->family = AF_UNSPEC; + address->network = network; - LIST_PREPEND(addresses, network->addresses, address); + LIST_PREPEND(static_addresses, network->static_addresses, address); if (section) { address->section = section; @@ -61,19 +63,80 @@ 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; + + address->family = AF_UNSPEC; + + *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); } +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, &req); + 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_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback) { _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL; @@ -87,15 +150,35 @@ int address_configure(Address *address, Link *link, assert(link->manager->rtnl); r = sd_rtnl_message_addr_new(RTM_NEWADDR, link->ifindex, - address->family, address->prefixlen, - IFA_F_PERMANENT, RT_SCOPE_UNIVERSE, &req); + address->family, &req); 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, RT_SCOPE_UNIVERSE); + 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)); @@ -103,11 +186,7 @@ int address_configure(Address *address, Link *link, } if (address->family == AF_INET) { - struct in_addr broadcast; - - 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, &address->broadcast); if (r < 0) { log_error("Could not append IFA_BROADCAST attribute: %s", strerror(-r)); @@ -116,7 +195,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)); @@ -133,6 +212,80 @@ int address_configure(Address *address, Link *link, 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) { + Address **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_syntax(unit, LOG_ERR, filename, line, EINVAL, + "DNS address is invalid, ignoring assignment: %s", rvalue); + return 0; + } + + *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; + + 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; +} + int config_parse_address(const char *unit, const char *filename, unsigned line, @@ -155,7 +308,13 @@ int config_parse_address(const char *unit, assert(rvalue); assert(data); - r = address_new(network, section_line, &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; @@ -174,7 +333,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) @@ -192,6 +350,10 @@ 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; @@ -218,7 +380,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;