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->vlans = hashmap_new(uint64_hash_func, uint64_compare_func);
60 network->addresses_by_section = hashmap_new(uint64_hash_func, uint64_compare_func);
61 if (!network->addresses_by_section)
64 network->routes_by_section = hashmap_new(uint64_hash_func, uint64_compare_func);
65 if (!network->routes_by_section)
68 network->filename = strdup(filename);
69 if (!network->filename)
72 network->dhcp_dns = true;
73 network->dhcp_hostname = true;
74 network->dhcp_domainname = true;
76 r = config_parse(NULL, filename, file, "Match\0Network\0Address\0Route\0DHCPv4\0", config_item_perf_lookup,
77 (void*) network_gperf_lookup, false, false, network);
79 log_warning("Could not parse config file %s: %s", filename, strerror(-r));
83 LIST_PREPEND(networks, manager->networks, network);
85 LIST_FOREACH(static_routes, route, network->static_routes) {
87 log_warning("Route section without Gateway field configured in %s. "
88 "Ignoring", filename);
93 LIST_FOREACH(static_addresses, address, network->static_addresses) {
94 if (!address->family) {
95 log_warning("Address section without Address field configured in %s. "
96 "Ignoring", filename);
106 int network_load(Manager *manager) {
108 _cleanup_strv_free_ char **files = NULL;
114 while ((network = manager->networks))
115 network_free(network);
117 r = conf_files_list_strv(&files, ".network", NULL, network_dirs);
119 log_error("Failed to enumerate network files: %s", strerror(-r));
123 STRV_FOREACH_BACKWARDS(f, files) {
124 r = network_load_one(manager, *f);
132 void network_free(Network *network) {
139 free(network->filename);
141 free(network->match_mac);
142 free(network->match_path);
143 free(network->match_driver);
144 free(network->match_type);
145 free(network->match_name);
147 free(network->description);
149 address_free(network->dns);
151 hashmap_free(network->vlans);
153 while ((route = network->static_routes))
156 while ((address = network->static_addresses))
157 address_free(address);
159 hashmap_free(network->addresses_by_section);
160 hashmap_free(network->routes_by_section);
162 if (network->manager && network->manager->networks)
163 LIST_REMOVE(networks, network->manager->networks, network);
168 int network_get(Manager *manager, struct udev_device *device, Network **ret) {
175 LIST_FOREACH(networks, network, manager->networks) {
176 if (net_match_config(network->match_mac, network->match_path,
177 network->match_driver, network->match_type,
179 udev_device_get_sysattr_value(device, "address"),
180 udev_device_get_property_value(device, "ID_PATH"),
181 udev_device_get_driver(device),
182 udev_device_get_devtype(device),
183 udev_device_get_sysname(device))) {
184 log_debug("%s: found matching network '%s'",
185 udev_device_get_sysname(device),
197 int network_apply(Manager *manager, Network *network, Link *link) {
200 link->network = network;
202 r = link_configure(link);
207 r = manager_update_resolv_conf(manager);
215 int config_parse_bridge(const char *unit,
216 const char *filename,
219 unsigned section_line,
225 Network *network = userdata;
234 r = netdev_get(network->manager, rvalue, &netdev);
236 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
237 "Bridge is invalid, ignoring assignment: %s", rvalue);
241 if (netdev->kind != NETDEV_KIND_BRIDGE) {
242 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
243 "NetDev is not a bridge, ignoring assignment: %s", rvalue);
247 network->bridge = netdev;
252 int config_parse_bond(const char *unit,
253 const char *filename,
256 unsigned section_line,
262 Network *network = userdata;
271 r = netdev_get(network->manager, rvalue, &netdev);
273 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
274 "Bond is invalid, ignoring assignment: %s", rvalue);
278 if (netdev->kind != NETDEV_KIND_BOND) {
279 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
280 "NetDev is not a bond, ignoring assignment: %s", rvalue);
284 network->bond = netdev;
289 int config_parse_vlan(const char *unit,
290 const char *filename,
293 unsigned section_line,
299 Network *network = userdata;
308 r = netdev_get(network->manager, rvalue, &netdev);
310 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
311 "VLAN is invalid, ignoring assignment: %s", rvalue);
315 if (netdev->kind != NETDEV_KIND_VLAN) {
316 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
317 "NetDev is not a VLAN, ignoring assignment: %s", rvalue);
321 r = hashmap_put(network->vlans, &netdev->vlanid, netdev);
323 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
324 "Can not add VLAN to network: %s", rvalue);