1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "path-util.h"
25 #include "conf-files.h"
26 #include "conf-parser.h"
29 static int network_load_one(Manager *manager, const char *filename) {
30 _cleanup_network_free_ Network *network = NULL;
31 _cleanup_fclose_ FILE *file = NULL;
39 file = fopen(filename, "re");
47 network = new0(Network, 1);
51 network->manager = manager;
53 LIST_HEAD_INIT(network->static_addresses);
54 LIST_HEAD_INIT(network->static_routes);
56 network->addresses_by_section = hashmap_new(uint64_hash_func, uint64_compare_func);
57 if (!network->addresses_by_section)
60 network->routes_by_section = hashmap_new(uint64_hash_func, uint64_compare_func);
61 if (!network->routes_by_section)
64 network->filename = strdup(filename);
65 if (!network->filename)
68 r = config_parse(NULL, filename, file, "Match\0Network\0Address\0Route\0", config_item_perf_lookup,
69 (void*) network_gperf_lookup, false, false, network);
71 log_warning("Could not parse config file %s: %s", filename, strerror(-r));
75 LIST_PREPEND(networks, manager->networks, network);
77 LIST_FOREACH(static_routes, route, network->static_routes) {
79 log_warning("Route section without Gateway field configured in %s. "
80 "Ignoring", filename);
84 if (route->dst_family && route->family != route->dst_family) {
85 log_warning("Route section with conflicting Gateway and Destination address "
86 "family configured in %s. Ignoring", filename);
91 LIST_FOREACH(static_addresses, address, network->static_addresses) {
92 if (!address->family) {
93 log_warning("Address section without Address field configured in %s. "
94 "Ignoring", filename);
104 int network_load(Manager *manager) {
106 _cleanup_strv_free_ char **files = NULL;
112 while ((network = manager->networks))
113 network_free(network);
115 r = conf_files_list_strv(&files, ".network", NULL, network_dirs);
117 log_error("Failed to enumerate network files: %s", strerror(-r));
121 STRV_FOREACH_BACKWARDS(f, files) {
122 r = network_load_one(manager, *f);
130 void network_free(Network *network) {
137 free(network->filename);
139 free(network->match_mac);
140 free(network->match_path);
141 free(network->match_driver);
142 free(network->match_type);
143 free(network->match_name);
145 free(network->description);
147 address_free(network->dns);
149 while ((route = network->static_routes))
152 while ((address = network->static_addresses))
153 address_free(address);
155 hashmap_free(network->addresses_by_section);
156 hashmap_free(network->routes_by_section);
158 if (network->manager && network->manager->networks)
159 LIST_REMOVE(networks, network->manager->networks, network);
164 int network_get(Manager *manager, struct udev_device *device, Network **ret) {
171 LIST_FOREACH(networks, network, manager->networks) {
172 if (net_match_config(network->match_mac, network->match_path,
173 network->match_driver, network->match_type,
175 udev_device_get_sysattr_value(device, "address"),
176 udev_device_get_property_value(device, "ID_PATH"),
177 udev_device_get_driver(device),
178 udev_device_get_devtype(device),
179 udev_device_get_sysname(device))) {
180 log_debug("%s: found matching network '%s'",
181 udev_device_get_sysname(device),
193 int network_apply(Manager *manager, Network *network, Link *link) {
196 link->network = network;
198 r = link_configure(link);
203 r = manager_update_resolv_conf(manager);
211 int config_parse_bridge(const char *unit,
212 const char *filename,
215 unsigned section_line,
221 Network *network = userdata;
230 r = bridge_get(network->manager, rvalue, &bridge);
232 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
233 "Bridge is invalid, ignoring assignment: %s", rvalue);
237 network->bridge = bridge;