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"
29 #include "network-internal.h"
31 static void address_init(Address *address) {
34 address->family = AF_UNSPEC;
35 address->scope = RT_SCOPE_UNIVERSE;
36 address->cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME;
37 address->cinfo.ifa_valid = CACHE_INFO_INFINITY_LIFE_TIME;
40 int address_new_static(Network *network, unsigned section, Address **ret) {
41 _cleanup_address_free_ Address *address = NULL;
44 uint64_t key = section;
45 address = hashmap_get(network->addresses_by_section, &key);
54 address = new0(Address, 1);
58 address_init(address);
60 address->network = network;
62 LIST_PREPEND(addresses, network->static_addresses, address);
65 address->section = section;
66 hashmap_put(network->addresses_by_section, &address->section, address);
75 int address_new_dynamic(Address **ret) {
76 _cleanup_address_free_ Address *address = NULL;
78 address = new0(Address, 1);
82 address_init(address);
90 void address_free(Address *address) {
94 if (address->network) {
95 LIST_REMOVE(addresses, address->network->static_addresses, address);
98 hashmap_remove(address->network->addresses_by_section,
105 int address_drop(Address *address, Link *link,
106 sd_rtnl_message_handler_t callback) {
107 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
111 assert(address->family == AF_INET || address->family == AF_INET6);
113 assert(link->ifindex > 0);
114 assert(link->manager);
115 assert(link->manager->rtnl);
117 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR,
118 link->ifindex, address->family);
120 log_error("Could not allocate RTM_DELADDR message: %s",
125 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
127 log_error("Could not set prefixlen: %s", strerror(-r));
131 if (address->family == AF_INET)
132 r = sd_rtnl_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
133 else if (address->family == AF_INET6)
134 r = sd_rtnl_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
136 log_error("Could not append IFA_LOCAL attribute: %s",
141 r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
143 log_error("Could not send rtnetlink message: %s", strerror(-r));
150 int address_update(Address *address, Link *link,
151 sd_rtnl_message_handler_t callback) {
152 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
156 assert(address->family == AF_INET || address->family == AF_INET6);
157 assert(link->ifindex > 0);
158 assert(link->manager);
159 assert(link->manager->rtnl);
161 r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req,
162 link->ifindex, address->family);
164 log_error("Could not allocate RTM_NEWADDR message: %s",
169 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
171 log_error("Could not set prefixlen: %s", strerror(-r));
175 r = sd_rtnl_message_addr_set_flags(req, IFA_F_PERMANENT);
177 log_error("Could not set flags: %s", strerror(-r));
181 r = sd_rtnl_message_addr_set_scope(req, address->scope);
183 log_error("Could not set scope: %s", strerror(-r));
187 if (address->family == AF_INET)
188 r = sd_rtnl_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
189 else if (address->family == AF_INET6)
190 r = sd_rtnl_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
192 log_error("Could not append IFA_LOCAL attribute: %s",
197 if (address->family == AF_INET) {
198 r = sd_rtnl_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast);
200 log_error("Could not append IFA_BROADCAST attribute: %s",
206 if (address->label) {
207 r = sd_rtnl_message_append_string(req, IFA_LABEL, address->label);
209 log_error("Could not append IFA_LABEL attribute: %s",
215 r = sd_rtnl_message_append_cache_info(req, IFA_CACHEINFO, &address->cinfo);
217 log_error("Could not append IFA_CACHEINFO attribute: %s",
222 r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
224 log_error("Could not send rtnetlink message: %s", strerror(-r));
231 static int address_acquire(Link *link, Address *original, Address **ret) {
232 union in_addr_union in_addr = {};
233 struct in_addr broadcast = {};
241 /* Something useful was configured? just use it */
242 if (in_addr_null(original->family, &original->in_addr) <= 0)
245 /* The address is configured to be 0.0.0.0 or [::] by the user?
246 * Then let's acquire something more useful from the pool. */
247 r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
249 log_error_link(link, "Failed to acquire address from pool: %s", strerror(-r));
253 log_error_link(link, "Couldn't find free address for interface, all taken.");
257 if (original->family == AF_INET) {
258 /* Pick first address in range for ourselves ...*/
259 in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1);
261 /* .. and use last as broadcast address */
262 broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen);
263 } else if (original->family == AF_INET6)
264 in_addr.in6.s6_addr[15] |= 1;
266 r = address_new_dynamic(&na);
270 na->family = original->family;
271 na->prefixlen = original->prefixlen;
272 na->scope = original->scope;
273 na->cinfo = original->cinfo;
275 if (original->label) {
276 na->label = strdup(original->label);
284 na->broadcast = broadcast;
285 na->in_addr = in_addr;
287 LIST_PREPEND(addresses, link->pool_addresses, na);
293 int address_configure(Address *address, Link *link,
294 sd_rtnl_message_handler_t callback) {
295 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
299 assert(address->family == AF_INET || address->family == AF_INET6);
301 assert(link->ifindex > 0);
302 assert(link->manager);
303 assert(link->manager->rtnl);
305 r = address_acquire(link, address, &address);
309 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR,
310 link->ifindex, address->family);
312 log_error("Could not allocate RTM_NEWADDR message: %s",
317 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
319 log_error("Could not set prefixlen: %s", strerror(-r));
323 r = sd_rtnl_message_addr_set_flags(req, IFA_F_PERMANENT);
325 log_error("Could not set flags: %s", strerror(-r));
329 r = sd_rtnl_message_addr_set_scope(req, address->scope);
331 log_error("Could not set scope: %s", strerror(-r));
335 if (address->family == AF_INET)
336 r = sd_rtnl_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
337 else if (address->family == AF_INET6)
338 r = sd_rtnl_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
340 log_error("Could not append IFA_LOCAL attribute: %s",
345 if (address->family == AF_INET) {
346 r = sd_rtnl_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast);
348 log_error("Could not append IFA_BROADCAST attribute: %s",
354 if (address->label) {
355 r = sd_rtnl_message_append_string(req, IFA_LABEL, address->label);
357 log_error("Could not append IFA_LABEL attribute: %s",
363 r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
365 log_error("Could not send rtnetlink message: %s", strerror(-r));
372 int config_parse_dns(const char *unit,
373 const char *filename,
376 unsigned section_line,
382 Network *network = userdata;
384 _cleanup_address_free_ Address *n = NULL;
393 r = address_new_dynamic(&n);
397 r = net_parse_inaddr(rvalue, &n->family, &n->in_addr);
399 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
400 "DNS address is invalid, ignoring assignment: %s", rvalue);
404 if (streq(lvalue, "DNS")) {
405 LIST_FIND_TAIL(addresses, network->dns, tail);
406 LIST_INSERT_AFTER(addresses, network->dns, tail, n);
407 } else if (streq(lvalue, "NTP")) {
408 LIST_FIND_TAIL(addresses, network->ntp, tail);
409 LIST_INSERT_AFTER(addresses, network->ntp, tail, n);
411 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
412 "Key is invalid, ignoring assignment: %s=%s", lvalue, rvalue);
421 int config_parse_broadcast(const char *unit,
422 const char *filename,
425 unsigned section_line,
431 Network *network = userdata;
432 _cleanup_address_free_ Address *n = NULL;
433 _cleanup_free_ char *address = NULL;
442 r = address_new_static(network, section_line, &n);
446 if (n->family == AF_INET6) {
447 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
448 "Broadcast is not valid for IPv6 addresses, "
449 "ignoring assignment: %s", address);
453 r = net_parse_inaddr(address, &n->family, &n->broadcast);
455 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
456 "Broadcast is invalid, ignoring assignment: %s", address);
465 int config_parse_address(const char *unit,
466 const char *filename,
469 unsigned section_line,
475 Network *network = userdata;
476 _cleanup_address_free_ Address *n = NULL;
477 _cleanup_free_ char *address = NULL;
487 if (streq(section, "Network")) {
488 /* we are not in an Address section, so treat
489 * this as the special '0' section */
493 r = address_new_static(network, section_line, &n);
497 /* Address=address/prefixlen */
500 e = strchr(rvalue, '/');
503 r = safe_atou(e + 1, &i);
505 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
506 "Interface prefix length is invalid, "
507 "ignoring assignment: %s", e + 1);
511 n->prefixlen = (unsigned char) i;
513 address = strndup(rvalue, e - rvalue);
517 address = strdup(rvalue);
522 r = net_parse_inaddr(address, &n->family, &n->in_addr);
524 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
525 "Address is invalid, ignoring assignment: %s", address);
529 if (n->family == AF_INET && !n->broadcast.s_addr)
530 n->broadcast.s_addr = n->in_addr.in.s_addr |
531 htonl(0xfffffffflu >> n->prefixlen);
538 int config_parse_label(const char *unit,
539 const char *filename,
542 unsigned section_line,
548 Network *network = userdata;
549 _cleanup_address_free_ Address *n = NULL;
559 r = address_new_static(network, section_line, &n);
563 label = strdup(rvalue);
567 if (!ascii_is_valid(label) || strlen(label) >= IFNAMSIZ) {
568 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
569 "Interface label is not ASCII clean or is too"
570 " long, ignoring assignment: %s", rvalue);
588 bool address_equal(Address *a1, Address *a2) {
593 /* one, but not both, is NULL */
597 if (a1->family != a2->family)
600 switch (a1->family) {
601 /* use the same notion of equality as the kernel does */
606 if (a1->prefixlen != a2->prefixlen)
611 b1 = be32toh(a1->in_addr.in.s_addr);
612 b2 = be32toh(a2->in_addr.in.s_addr);
614 return (b1 >> (32 - a1->prefixlen)) == (b2 >> (32 - a1->prefixlen));
621 b1 = (uint64_t*)&a1->in_addr.in6;
622 b2 = (uint64_t*)&a2->in_addr.in6;
624 return (((b1[0] ^ b2[0]) | (b1[1] ^ b2[1])) == 0UL);
627 assert_not_reached("Invalid address family");