networkd-address.c revision 407fe036a24834203aca6c1eec7d74d9ad3e9ee0
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering This file is part of systemd.
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering Copyright 2013 Tom Gundersen <teg@jklm.no>
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering systemd is free software; you can redistribute it and/or modify it
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering under the terms of the GNU Lesser General Public License as published by
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering (at your option) any later version.
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering systemd is distributed in the hope that it will be useful, but
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering Lesser General Public License for more details.
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering You should have received a copy of the GNU Lesser General Public License
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poetteringint address_new(Network *network, unsigned section, Address **ret) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering _cleanup_address_free_ Address *address = NULL;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering address = hashmap_get(network->addresses_by_section, &key);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering LIST_PREPEND(addresses, network->addresses, address);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering hashmap_put(network->addresses_by_section, &address->section, address);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering LIST_REMOVE(addresses, address->network->addresses, address);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering hashmap_remove(address->network->addresses_by_section,
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poetteringint address_drop(Address *address, Link *link,
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering assert(address->family == AF_INET || address->family == AF_INET6);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering r = sd_rtnl_message_addr_new(RTM_DELADDR, link->ifindex,
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering address->family, address->prefixlen, 0, 0, &req);
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering log_error("Could not allocate RTM_DELADDR message: %s",
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering r = sd_rtnl_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering r = sd_rtnl_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering log_error("Could not append IFA_LOCAL attribute: %s",
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering log_error("Could not send rtnetlink message: %s", strerror(-r));
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poetteringint address_configure(Address *address, Link *link,
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering assert(address->family == AF_INET || address->family == AF_INET6);
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering r = sd_rtnl_message_addr_new(RTM_NEWADDR, link->ifindex,
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering IFA_F_PERMANENT, RT_SCOPE_UNIVERSE, &req);
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering log_error("Could not allocate RTM_NEWADDR message: %s",
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering r = sd_rtnl_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering r = sd_rtnl_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering log_error("Could not append IFA_LOCAL attribute: %s",
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering broadcast.s_addr = address->in_addr.in.s_addr | address->netmask.s_addr;
d57c365bf8f09fbcc649e00f7060ff30809f67c2Lennart Poettering r = sd_rtnl_message_append_in_addr(req, IFA_BROADCAST, &broadcast);
d57c365bf8f09fbcc649e00f7060ff30809f67c2Lennart Poettering log_error("Could not append IFA_BROADCAST attribute: %s",
d57c365bf8f09fbcc649e00f7060ff30809f67c2Lennart Poettering r = sd_rtnl_message_append_string(req, IFA_LABEL, address->label);
d57c365bf8f09fbcc649e00f7060ff30809f67c2Lennart Poettering log_error("Could not append IFA_LABEL attribute: %s",
d57c365bf8f09fbcc649e00f7060ff30809f67c2Lennart Poettering r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
d57c365bf8f09fbcc649e00f7060ff30809f67c2Lennart Poettering log_error("Could not send rtnetlink message: %s", strerror(-r));
d57c365bf8f09fbcc649e00f7060ff30809f67c2Lennart Poetteringint config_parse_address(const char *unit,
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering _cleanup_address_free_ Address *n = NULL;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering const char *e;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering /* we are not in an Address section, so treat
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering * this as the special '0' section */
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering r = address_new(network, section_line, &n);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering /* prefixlen */
d57c365bf8f09fbcc649e00f7060ff30809f67c2Lennart Poettering log_syntax(unit, LOG_ERR, filename, line, EINVAL,
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering "Interface prefix length is invalid, "
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering n->prefixlen = (unsigned char) i;
ab1525bc2d92780b4dc64bc5b9cdb52594e2df7dLennart Poettering n->netmask.s_addr = htonl(0xfffffffflu >> n->prefixlen);
d57c365bf8f09fbcc649e00f7060ff30809f67c2Lennart Poettering r = net_parse_inaddr(address, &n->family, &n->in_addr);
6d0c65ffb4f82e8c6dceb453919b3db54343fc27Lennart Poettering log_syntax(unit, LOG_ERR, filename, line, EINVAL,
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering "Address is invalid, ignoring assignment: %s", address);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering _cleanup_address_free_ Address *n = NULL;
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering r = address_new(network, section_line, &n);
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering if (!ascii_is_valid(label) || strlen(label) >= IFNAMSIZ) {
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering log_syntax(unit, LOG_ERR, filename, line, EINVAL,
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering "Interface label is not ASCII clean or is too"
ee8c45689526ca973407cbb77bce7b96a062c40bLennart Poettering " long, ignoring assignment: %s", rvalue);