networkd-network.c revision 801bd9e859d7f3f127617172910786929776472b
369N/A/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
369N/A
369N/A/***
369N/A This file is part of systemd.
369N/A
369N/A Copyright 2013 Tom Gundersen <teg@jklm.no>
369N/A
369N/A systemd is free software; you can redistribute it and/or modify it
369N/A under the terms of the GNU Lesser General Public License as published by
369N/A the Free Software Foundation; either version 2.1 of the License, or
369N/A (at your option) any later version.
369N/A
369N/A systemd is distributed in the hope that it will be useful, but
369N/A WITHOUT ANY WARRANTY; without even the implied warranty of
369N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
369N/A Lesser General Public License for more details.
369N/A
369N/A You should have received a copy of the GNU Lesser General Public License
369N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
369N/A***/
3661N/A
369N/A#include "networkd.h"
3661N/A#include "net-util.h"
3661N/A#include "path-util.h"
3661N/A#include "conf-files.h"
3661N/A#include "conf-parser.h"
3661N/A#include "util.h"
3661N/A
3661N/Astatic int network_load_one(Manager *manager, const char *filename) {
3661N/A _cleanup_network_free_ Network *network = NULL;
3661N/A _cleanup_fclose_ FILE *file = NULL;
3661N/A Route *route;
3661N/A Address *address;
3661N/A int r;
3661N/A
3661N/A assert(manager);
3661N/A assert(filename);
369N/A
4561N/A file = fopen(filename, "re");
4561N/A if (!file) {
4561N/A if (errno == ENOENT)
4561N/A return 0;
4561N/A else
4561N/A return errno;
3996N/A }
369N/A
369N/A network = new0(Network, 1);
369N/A if (!network)
369N/A return log_oom();
1273N/A
369N/A network->manager = manager;
369N/A
618N/A LIST_HEAD_INIT(network->static_addresses);
1273N/A LIST_HEAD_INIT(network->static_routes);
618N/A
4561N/A network->addresses_by_section = hashmap_new(uint64_hash_func, uint64_compare_func);
4561N/A if (!network->addresses_by_section)
4561N/A return log_oom();
369N/A
369N/A network->routes_by_section = hashmap_new(uint64_hash_func, uint64_compare_func);
4561N/A if (!network->routes_by_section)
369N/A return log_oom();
1574N/A
1574N/A network->filename = strdup(filename);
1574N/A if (!network->filename)
1574N/A return log_oom();
1574N/A
369N/A network->dhcp_dns = true;
369N/A network->dhcp_hostname = true;
369N/A network->dhcp_domainname = true;
369N/A
369N/A r = config_parse(NULL, filename, file, "Match\0Network\0Address\0Route\0DHCPv4\0", config_item_perf_lookup,
369N/A (void*) network_gperf_lookup, false, false, network);
369N/A if (r < 0) {
369N/A log_warning("Could not parse config file %s: %s", filename, strerror(-r));
369N/A return r;
369N/A }
369N/A
369N/A LIST_PREPEND(networks, manager->networks, network);
369N/A
369N/A LIST_FOREACH(static_routes, route, network->static_routes) {
369N/A if (!route->family) {
369N/A log_warning("Route section without Gateway field configured in %s. "
369N/A "Ignoring", filename);
369N/A return 0;
369N/A }
369N/A }
369N/A
369N/A LIST_FOREACH(static_addresses, address, network->static_addresses) {
369N/A if (!address->family) {
369N/A log_warning("Address section without Address field configured in %s. "
369N/A "Ignoring", filename);
369N/A return 0;
369N/A }
369N/A }
369N/A
369N/A network = NULL;
369N/A
369N/A return 0;
369N/A}
369N/A
369N/Aint network_load(Manager *manager) {
369N/A Network *network;
3996N/A _cleanup_strv_free_ char **files = NULL;
3996N/A char **f;
3996N/A int r;
3996N/A
3996N/A assert(manager);
4994N/A
4994N/A while ((network = manager->networks))
3996N/A network_free(network);
3996N/A
r = conf_files_list_strv(&files, ".network", NULL, network_dirs);
if (r < 0) {
log_error("Failed to enumerate network files: %s", strerror(-r));
return r;
}
STRV_FOREACH_BACKWARDS(f, files) {
r = network_load_one(manager, *f);
if (r < 0)
return r;
}
return 0;
}
void network_free(Network *network) {
Route *route;
Address *address;
if (!network)
return;
free(network->filename);
free(network->match_mac);
free(network->match_path);
free(network->match_driver);
free(network->match_type);
free(network->match_name);
free(network->description);
address_free(network->dns);
while ((route = network->static_routes))
route_free(route);
while ((address = network->static_addresses))
address_free(address);
hashmap_free(network->addresses_by_section);
hashmap_free(network->routes_by_section);
if (network->manager && network->manager->networks)
LIST_REMOVE(networks, network->manager->networks, network);
free(network);
}
int network_get(Manager *manager, struct udev_device *device, Network **ret) {
Network *network;
assert(manager);
assert(device);
assert(ret);
LIST_FOREACH(networks, network, manager->networks) {
if (net_match_config(network->match_mac, network->match_path,
network->match_driver, network->match_type,
network->match_name,
udev_device_get_sysattr_value(device, "address"),
udev_device_get_property_value(device, "ID_PATH"),
udev_device_get_driver(device),
udev_device_get_devtype(device),
udev_device_get_sysname(device))) {
log_debug("%s: found matching network '%s'",
udev_device_get_sysname(device),
network->filename);
*ret = network;
return 0;
}
}
*ret = NULL;
return -ENOENT;
}
int network_apply(Manager *manager, Network *network, Link *link) {
int r;
link->network = network;
r = link_configure(link);
if (r < 0)
return r;
if (network->dns) {
r = manager_update_resolv_conf(manager);
if (r < 0)
return r;
}
return 0;
}
int config_parse_bridge(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;
Netdev *netdev;
int r;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
r = netdev_get(network->manager, rvalue, &netdev);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, EINVAL,
"Bridge is invalid, ignoring assignment: %s", rvalue);
return 0;
}
if (netdev->kind != NETDEV_KIND_BRIDGE) {
log_syntax(unit, LOG_ERR, filename, line, EINVAL,
"Netdev is not a bridge, ignoring assignment: %s", rvalue);
return 0;
}
network->bridge = netdev;
return 0;
}
int config_parse_bond(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;
Netdev *netdev;
int r;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
r = netdev_get(network->manager, rvalue, &netdev);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, EINVAL,
"Bond is invalid, ignoring assignment: %s", rvalue);
return 0;
}
if (netdev->kind != NETDEV_KIND_BOND) {
log_syntax(unit, LOG_ERR, filename, line, EINVAL,
"Netdev is not a bond, ignoring assignment: %s", rvalue);
return 0;
}
network->bond = netdev;
return 0;
}
int config_parse_vlan(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;
Netdev *netdev;
int r;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
r = netdev_get(network->manager, rvalue, &netdev);
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, EINVAL,
"VLAN is invalid, ignoring assignment: %s", rvalue);
return 0;
}
if (netdev->kind != NETDEV_KIND_VLAN) {
log_syntax(unit, LOG_ERR, filename, line, EINVAL,
"Netdev is not a VLAN, ignoring assignment: %s", rvalue);
return 0;
}
network->vlan = netdev;
return 0;
}