2013-10-17 03:18:36 +02:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2013 Tom Gundersen <teg@jklm.no>
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
|
|
|
|
#include "networkd.h"
|
|
|
|
|
|
|
|
#include "utf8.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "conf-parser.h"
|
2014-03-21 21:38:14 +01:00
|
|
|
#include "network-internal.h"
|
2013-10-17 03:18:36 +02:00
|
|
|
|
2014-04-02 21:31:12 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-01 15:16:10 +01:00
|
|
|
int address_new_static(Network *network, unsigned section, Address **ret) {
|
2013-10-17 03:18:36 +02:00
|
|
|
_cleanup_address_free_ Address *address = NULL;
|
|
|
|
|
2013-11-19 16:54:42 +01:00
|
|
|
if (section) {
|
|
|
|
uint64_t key = section;
|
|
|
|
address = hashmap_get(network->addresses_by_section, &key);
|
|
|
|
if (address) {
|
|
|
|
*ret = address;
|
|
|
|
address = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-17 03:18:36 +02:00
|
|
|
address = new0(Address, 1);
|
|
|
|
if (!address)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-04-02 21:31:12 +02:00
|
|
|
address_init(address);
|
2014-01-28 20:00:47 +01:00
|
|
|
|
2013-10-17 03:18:36 +02:00
|
|
|
address->network = network;
|
|
|
|
|
2014-05-15 20:10:33 +02:00
|
|
|
LIST_PREPEND(addresses, network->static_addresses, address);
|
2013-10-17 03:18:36 +02:00
|
|
|
|
2013-11-19 16:54:42 +01:00
|
|
|
if (section) {
|
|
|
|
address->section = section;
|
|
|
|
hashmap_put(network->addresses_by_section, &address->section, address);
|
|
|
|
}
|
|
|
|
|
2013-10-17 03:18:36 +02:00
|
|
|
*ret = address;
|
|
|
|
address = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-01 15:16:10 +01:00
|
|
|
int address_new_dynamic(Address **ret) {
|
|
|
|
_cleanup_address_free_ Address *address = NULL;
|
|
|
|
|
|
|
|
address = new0(Address, 1);
|
|
|
|
if (!address)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-04-02 21:31:12 +02:00
|
|
|
address_init(address);
|
2014-01-28 20:00:47 +01:00
|
|
|
|
2014-01-01 15:16:10 +01:00
|
|
|
*ret = address;
|
|
|
|
address = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-17 03:18:36 +02:00
|
|
|
void address_free(Address *address) {
|
|
|
|
if (!address)
|
|
|
|
return;
|
|
|
|
|
2014-01-01 15:16:10 +01:00
|
|
|
if (address->network) {
|
2014-05-15 20:10:33 +02:00
|
|
|
LIST_REMOVE(addresses, address->network->static_addresses, address);
|
2013-10-17 03:18:36 +02:00
|
|
|
|
2014-01-01 15:16:10 +01:00
|
|
|
if (address->section)
|
|
|
|
hashmap_remove(address->network->addresses_by_section,
|
|
|
|
&address->section);
|
|
|
|
}
|
2013-11-19 16:54:42 +01:00
|
|
|
|
2013-10-17 03:18:36 +02:00
|
|
|
free(address);
|
|
|
|
}
|
|
|
|
|
2013-12-31 17:31:50 +01:00
|
|
|
int address_drop(Address *address, Link *link,
|
|
|
|
sd_rtnl_message_handler_t callback) {
|
2014-02-13 03:44:14 +01:00
|
|
|
_cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
|
2013-12-31 17:31:50 +01:00
|
|
|
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);
|
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR,
|
|
|
|
link->ifindex, address->family);
|
2013-12-31 17:31:50 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_error("Could not allocate RTM_DELADDR message: %s",
|
|
|
|
strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-01-21 15:55:57 +01:00
|
|
|
r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Could not set prefixlen: %s", strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-12-31 17:31:50 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:31:12 +02:00
|
|
|
int address_update(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->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_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));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-18 18:22:14 +02:00
|
|
|
static int address_acquire(Link *link, Address *original, Address **ret) {
|
|
|
|
union in_addr_union in_addr = {};
|
|
|
|
struct in_addr broadcast = {};
|
|
|
|
Address *na = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(original);
|
|
|
|
assert(ret);
|
|
|
|
|
|
|
|
/* Something useful was configured? just use it */
|
|
|
|
if (in_addr_null(original->family, &original->in_addr) <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* The address is configured to be 0.0.0.0 or [::] by the user?
|
|
|
|
* Then let's acquire something more useful from the pool. */
|
|
|
|
r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_link(link, "Failed to acquire address from pool: %s", strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (r == 0) {
|
|
|
|
log_error_link(link, "Couldn't find free address for interface, all taken.");
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (original->family == AF_INET) {
|
|
|
|
/* Pick first address in range for ourselves ...*/
|
|
|
|
in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1);
|
|
|
|
|
|
|
|
/* .. and use last as broadcast address */
|
|
|
|
broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen);
|
|
|
|
} else if (original->family == AF_INET6)
|
|
|
|
in_addr.in6.s6_addr[15] |= 1;
|
|
|
|
|
|
|
|
r = address_new_dynamic(&na);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
na->family = original->family;
|
|
|
|
na->prefixlen = original->prefixlen;
|
|
|
|
na->scope = original->scope;
|
|
|
|
na->cinfo = original->cinfo;
|
|
|
|
|
|
|
|
if (original->label) {
|
|
|
|
na->label = strdup(original->label);
|
|
|
|
|
|
|
|
if (!na->label) {
|
|
|
|
free(na);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
na->broadcast = broadcast;
|
|
|
|
na->in_addr = in_addr;
|
|
|
|
|
|
|
|
LIST_PREPEND(addresses, link->pool_addresses, na);
|
|
|
|
|
|
|
|
*ret = na;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-14 16:22:51 +01:00
|
|
|
int address_configure(Address *address, Link *link,
|
|
|
|
sd_rtnl_message_handler_t callback) {
|
2014-02-13 03:44:14 +01:00
|
|
|
_cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
|
2013-10-17 03:18:36 +02:00
|
|
|
int r;
|
|
|
|
|
2013-11-24 23:36:58 +01:00
|
|
|
assert(address);
|
|
|
|
assert(address->family == AF_INET || address->family == AF_INET6);
|
|
|
|
assert(link);
|
|
|
|
assert(link->ifindex > 0);
|
2013-11-14 16:22:51 +01:00
|
|
|
assert(link->manager);
|
2013-11-24 23:36:58 +01:00
|
|
|
assert(link->manager->rtnl);
|
2013-11-14 16:22:51 +01:00
|
|
|
|
2014-06-18 18:22:14 +02:00
|
|
|
r = address_acquire(link, address, &address);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR,
|
|
|
|
link->ifindex, address->family);
|
2013-10-17 03:18:36 +02:00
|
|
|
if (r < 0) {
|
|
|
|
log_error("Could not allocate RTM_NEWADDR message: %s",
|
|
|
|
strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-01-21 15:55:57 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-28 16:10:20 +01:00
|
|
|
r = sd_rtnl_message_addr_set_scope(req, address->scope);
|
2014-01-21 15:55:57 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_error("Could not set scope: %s", strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-12-15 14:00:20 +01:00
|
|
|
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);
|
2013-10-17 03:18:36 +02:00
|
|
|
if (r < 0) {
|
|
|
|
log_error("Could not append IFA_LOCAL attribute: %s",
|
|
|
|
strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address->family == AF_INET) {
|
2014-01-28 20:01:37 +01:00
|
|
|
r = sd_rtnl_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast);
|
2013-10-17 03:18:36 +02:00
|
|
|
if (r < 0) {
|
|
|
|
log_error("Could not append IFA_BROADCAST attribute: %s",
|
|
|
|
strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address->label) {
|
2013-12-15 14:00:20 +01:00
|
|
|
r = sd_rtnl_message_append_string(req, IFA_LABEL, address->label);
|
2013-10-17 03:18:36 +02:00
|
|
|
if (r < 0) {
|
|
|
|
log_error("Could not append IFA_LABEL attribute: %s",
|
|
|
|
strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 16:22:51 +01:00
|
|
|
r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
|
2013-10-17 03:18:36 +02:00
|
|
|
if (r < 0) {
|
2013-11-14 16:22:51 +01:00
|
|
|
log_error("Could not send rtnetlink message: %s", strerror(-r));
|
|
|
|
return r;
|
2013-10-17 03:18:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-05 23:01:10 +01:00
|
|
|
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) {
|
2014-05-16 19:43:12 +02:00
|
|
|
Network *network = userdata;
|
|
|
|
Address *tail;
|
2014-01-05 23:01:10 +01:00
|
|
|
_cleanup_address_free_ Address *n = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(filename);
|
|
|
|
assert(section);
|
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
2014-05-16 19:43:12 +02:00
|
|
|
assert(network);
|
2014-01-05 23:01:10 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-19 18:42:14 +02:00
|
|
|
if (streq(lvalue, "DNS")) {
|
|
|
|
LIST_FIND_TAIL(addresses, network->dns, tail);
|
|
|
|
LIST_INSERT_AFTER(addresses, network->dns, tail, n);
|
|
|
|
} else if (streq(lvalue, "NTP")) {
|
|
|
|
LIST_FIND_TAIL(addresses, network->ntp, tail);
|
|
|
|
LIST_INSERT_AFTER(addresses, network->ntp, tail, n);
|
|
|
|
} else {
|
|
|
|
log_syntax(unit, LOG_ERR, filename, line, EINVAL,
|
|
|
|
"Key is invalid, ignoring assignment: %s=%s", lvalue, rvalue);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-05 23:01:10 +01:00
|
|
|
n = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-28 20:01:37 +01:00
|
|
|
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;
|
|
|
|
|
2014-02-18 15:48:30 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-28 20:01:37 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-17 03:18:36 +02:00
|
|
|
int config_parse_address(const char *unit,
|
|
|
|
const char *filename,
|
|
|
|
unsigned line,
|
|
|
|
const char *section,
|
2013-11-19 16:17:55 +01:00
|
|
|
unsigned section_line,
|
2013-10-17 03:18:36 +02:00
|
|
|
const char *lvalue,
|
|
|
|
int ltype,
|
|
|
|
const char *rvalue,
|
|
|
|
void *data,
|
|
|
|
void *userdata) {
|
2013-11-19 16:54:42 +01:00
|
|
|
Network *network = userdata;
|
2013-10-17 03:18:36 +02:00
|
|
|
_cleanup_address_free_ Address *n = NULL;
|
|
|
|
_cleanup_free_ char *address = NULL;
|
|
|
|
const char *e;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(filename);
|
2013-11-19 16:54:42 +01:00
|
|
|
assert(section);
|
2013-10-17 03:18:36 +02:00
|
|
|
assert(lvalue);
|
|
|
|
assert(rvalue);
|
|
|
|
assert(data);
|
|
|
|
|
2013-11-28 17:17:04 +01:00
|
|
|
if (streq(section, "Network")) {
|
|
|
|
/* we are not in an Address section, so treat
|
|
|
|
* this as the special '0' section */
|
|
|
|
section_line = 0;
|
|
|
|
}
|
|
|
|
|
2014-01-01 15:16:10 +01:00
|
|
|
r = address_new_static(network, section_line, &n);
|
2013-10-17 03:18:36 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* Address=address/prefixlen */
|
|
|
|
|
|
|
|
/* prefixlen */
|
|
|
|
e = strchr(rvalue, '/');
|
|
|
|
if (e) {
|
|
|
|
unsigned i;
|
|
|
|
r = safe_atou(e + 1, &i);
|
|
|
|
if (r < 0) {
|
|
|
|
log_syntax(unit, LOG_ERR, filename, line, EINVAL,
|
|
|
|
"Interface prefix length is invalid, "
|
|
|
|
"ignoring assignment: %s", e + 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
n->prefixlen = (unsigned char) i;
|
2013-11-17 21:01:20 +01:00
|
|
|
|
2013-10-17 03:18:36 +02:00
|
|
|
address = strndup(rvalue, e - rvalue);
|
|
|
|
if (!address)
|
|
|
|
return log_oom();
|
|
|
|
} else {
|
|
|
|
address = strdup(rvalue);
|
|
|
|
if (!address)
|
|
|
|
return log_oom();
|
|
|
|
}
|
|
|
|
|
|
|
|
r = net_parse_inaddr(address, &n->family, &n->in_addr);
|
|
|
|
if (r < 0) {
|
|
|
|
log_syntax(unit, LOG_ERR, filename, line, EINVAL,
|
|
|
|
"Address is invalid, ignoring assignment: %s", address);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-28 20:01:37 +01:00
|
|
|
if (n->family == AF_INET && !n->broadcast.s_addr)
|
|
|
|
n->broadcast.s_addr = n->in_addr.in.s_addr |
|
|
|
|
htonl(0xfffffffflu >> n->prefixlen);
|
|
|
|
|
2013-10-17 03:18:36 +02:00
|
|
|
n = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-19 16:54:42 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-01-01 15:16:10 +01:00
|
|
|
r = address_new_static(network, section_line, &n);
|
2013-11-19 16:54:42 +01:00
|
|
|
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;
|
|
|
|
}
|
2014-05-16 00:27:56 +02:00
|
|
|
|
|
|
|
bool address_equal(Address *a1, Address *a2) {
|
|
|
|
/* same object */
|
|
|
|
if (a1 == a2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* one, but not both, is NULL */
|
|
|
|
if (!a1 || !a2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (a1->family != a2->family)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (a1->family) {
|
|
|
|
/* use the same notion of equality as the kernel does */
|
|
|
|
case AF_UNSPEC:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case AF_INET:
|
|
|
|
if (a1->prefixlen != a2->prefixlen)
|
|
|
|
return false;
|
|
|
|
else {
|
|
|
|
uint32_t b1, b2;
|
|
|
|
|
|
|
|
b1 = be32toh(a1->in_addr.in.s_addr);
|
|
|
|
b2 = be32toh(a2->in_addr.in.s_addr);
|
|
|
|
|
|
|
|
return (b1 >> (32 - a1->prefixlen)) == (b2 >> (32 - a1->prefixlen));
|
|
|
|
}
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
uint64_t *b1, *b2;
|
|
|
|
|
|
|
|
b1 = (uint64_t*)&a1->in_addr.in6;
|
|
|
|
b2 = (uint64_t*)&a2->in_addr.in6;
|
|
|
|
|
|
|
|
return (((b1[0] ^ b2[0]) | (b1[1] ^ b2[1])) == 0UL);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert_not_reached("Invalid address family");
|
|
|
|
}
|
|
|
|
}
|