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/>.
28 #include "conf-parser.h"
31 int address_new_static(Network *network, unsigned section, Address **ret) {
32 _cleanup_address_free_ Address *address = NULL;
35 uint64_t key = section;
36 address = hashmap_get(network->addresses_by_section, &key);
45 address = new0(Address, 1);
49 address->network = network;
51 LIST_PREPEND(static_addresses, network->static_addresses, address);
54 address->section = section;
55 hashmap_put(network->addresses_by_section, &address->section, address);
64 int address_new_dynamic(Address **ret) {
65 _cleanup_address_free_ Address *address = NULL;
67 address = new0(Address, 1);
77 void address_free(Address *address) {
81 if (address->network) {
82 LIST_REMOVE(static_addresses, address->network->static_addresses, address);
85 hashmap_remove(address->network->addresses_by_section,
92 int address_drop(Address *address, Link *link,
93 sd_rtnl_message_handler_t callback) {
94 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
98 assert(address->family == AF_INET || address->family == AF_INET6);
100 assert(link->ifindex > 0);
101 assert(link->manager);
102 assert(link->manager->rtnl);
104 r = sd_rtnl_message_addr_new(RTM_DELADDR, link->ifindex, address->family, &req);
106 log_error("Could not allocate RTM_DELADDR message: %s",
111 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
113 log_error("Could not set prefixlen: %s", strerror(-r));
117 if (address->family == AF_INET)
118 r = sd_rtnl_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
119 else if (address->family == AF_INET6)
120 r = sd_rtnl_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
122 log_error("Could not append IFA_LOCAL attribute: %s",
127 r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
129 log_error("Could not send rtnetlink message: %s", strerror(-r));
136 int address_configure(Address *address, Link *link,
137 sd_rtnl_message_handler_t callback) {
138 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
142 assert(address->family == AF_INET || address->family == AF_INET6);
144 assert(link->ifindex > 0);
145 assert(link->manager);
146 assert(link->manager->rtnl);
148 r = sd_rtnl_message_addr_new(RTM_NEWADDR, link->ifindex,
149 address->family, &req);
151 log_error("Could not allocate RTM_NEWADDR message: %s",
156 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
158 log_error("Could not set prefixlen: %s", strerror(-r));
162 r = sd_rtnl_message_addr_set_flags(req, IFA_F_PERMANENT);
164 log_error("Could not set flags: %s", strerror(-r));
168 r = sd_rtnl_message_addr_set_scope(req, RT_SCOPE_UNIVERSE);
170 log_error("Could not set scope: %s", strerror(-r));
174 if (address->family == AF_INET)
175 r = sd_rtnl_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
176 else if (address->family == AF_INET6)
177 r = sd_rtnl_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
179 log_error("Could not append IFA_LOCAL attribute: %s",
184 if (address->family == AF_INET) {
185 struct in_addr broadcast;
187 broadcast.s_addr = address->in_addr.in.s_addr | address->netmask.s_addr;
189 r = sd_rtnl_message_append_in_addr(req, IFA_BROADCAST, &broadcast);
191 log_error("Could not append IFA_BROADCAST attribute: %s",
197 if (address->label) {
198 r = sd_rtnl_message_append_string(req, IFA_LABEL, address->label);
200 log_error("Could not append IFA_LABEL attribute: %s",
206 r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
208 log_error("Could not send rtnetlink message: %s", strerror(-r));
215 int config_parse_dns(const char *unit,
216 const char *filename,
219 unsigned section_line,
225 Address **dns = data;
226 _cleanup_address_free_ Address *n = NULL;
235 r = address_new_dynamic(&n);
239 r = net_parse_inaddr(rvalue, &n->family, &n->in_addr);
241 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
242 "DNS address is invalid, ignoring assignment: %s", rvalue);
252 int config_parse_address(const char *unit,
253 const char *filename,
256 unsigned section_line,
262 Network *network = userdata;
263 _cleanup_address_free_ Address *n = NULL;
264 _cleanup_free_ char *address = NULL;
274 if (streq(section, "Network")) {
275 /* we are not in an Address section, so treat
276 * this as the special '0' section */
280 r = address_new_static(network, section_line, &n);
284 /* Address=address/prefixlen */
287 e = strchr(rvalue, '/');
290 r = safe_atou(e + 1, &i);
292 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
293 "Interface prefix length is invalid, "
294 "ignoring assignment: %s", e + 1);
298 n->prefixlen = (unsigned char) i;
299 n->netmask.s_addr = htonl(0xfffffffflu >> n->prefixlen);
301 address = strndup(rvalue, e - rvalue);
305 address = strdup(rvalue);
310 r = net_parse_inaddr(address, &n->family, &n->in_addr);
312 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
313 "Address is invalid, ignoring assignment: %s", address);
322 int config_parse_label(const char *unit,
323 const char *filename,
326 unsigned section_line,
332 Network *network = userdata;
333 _cleanup_address_free_ Address *n = NULL;
343 r = address_new_static(network, section_line, &n);
347 label = strdup(rvalue);
351 if (!ascii_is_valid(label) || strlen(label) >= IFNAMSIZ) {
352 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
353 "Interface label is not ASCII clean or is too"
354 " long, ignoring assignment: %s", rvalue);