]> git.proxmox.com Git - systemd.git/blame - src/network/networkd-address.c
Merge tag 'upstream/229'
[systemd.git] / src / network / networkd-address.c
CommitLineData
60f067b4
JS
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <net/if.h>
21
db2df898 22#include "alloc-util.h"
60f067b4 23#include "conf-parser.h"
86f210e9 24#include "firewall-util.h"
d9dfd233 25#include "netlink-util.h"
d9dfd233 26#include "networkd-address.h"
db2df898
MP
27#include "networkd.h"
28#include "parse-util.h"
29#include "set.h"
30#include "string-util.h"
31#include "utf8.h"
32#include "util.h"
60f067b4 33
db2df898
MP
34int address_new(Address **ret) {
35 _cleanup_address_free_ Address *address = NULL;
36
37 address = new0(Address, 1);
38 if (!address)
39 return -ENOMEM;
60f067b4
JS
40
41 address->family = AF_UNSPEC;
42 address->scope = RT_SCOPE_UNIVERSE;
43 address->cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME;
44 address->cinfo.ifa_valid = CACHE_INFO_INFINITY_LIFE_TIME;
db2df898
MP
45
46 *ret = address;
47 address = NULL;
48
49 return 0;
60f067b4
JS
50}
51
52int address_new_static(Network *network, unsigned section, Address **ret) {
53 _cleanup_address_free_ Address *address = NULL;
db2df898 54 int r;
60f067b4
JS
55
56 if (section) {
5eef597e 57 address = hashmap_get(network->addresses_by_section, UINT_TO_PTR(section));
60f067b4
JS
58 if (address) {
59 *ret = address;
60 address = NULL;
61
62 return 0;
63 }
64 }
65
db2df898
MP
66 r = address_new(&address);
67 if (r < 0)
68 return r;
60f067b4
JS
69
70 address->network = network;
71
e735f4d4 72 LIST_APPEND(addresses, network->static_addresses, address);
60f067b4
JS
73
74 if (section) {
75 address->section = section;
5eef597e
MP
76 hashmap_put(network->addresses_by_section,
77 UINT_TO_PTR(address->section), address);
60f067b4
JS
78 }
79
80 *ret = address;
81 address = NULL;
82
83 return 0;
84}
85
60f067b4
JS
86void address_free(Address *address) {
87 if (!address)
88 return;
89
90 if (address->network) {
91 LIST_REMOVE(addresses, address->network->static_addresses, address);
92
93 if (address->section)
94 hashmap_remove(address->network->addresses_by_section,
5eef597e 95 UINT_TO_PTR(address->section));
60f067b4
JS
96 }
97
db2df898
MP
98 if (address->link) {
99 set_remove(address->link->addresses, address);
100 set_remove(address->link->addresses_foreign, address);
101 }
102
60f067b4
JS
103 free(address);
104}
105
db2df898
MP
106static void address_hash_func(const void *b, struct siphash *state) {
107 const Address *a = b;
108
109 assert(a);
110
111 siphash24_compress(&a->family, sizeof(a->family), state);
112
113 switch (a->family) {
114 case AF_INET:
115 siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
116
117 /* peer prefix */
118 if (a->prefixlen != 0) {
119 uint32_t prefix;
120
121 if (a->in_addr_peer.in.s_addr != 0)
122 prefix = be32toh(a->in_addr_peer.in.s_addr) >> (32 - a->prefixlen);
123 else
124 prefix = be32toh(a->in_addr.in.s_addr) >> (32 - a->prefixlen);
125
126 siphash24_compress(&prefix, sizeof(prefix), state);
127 }
128
129 /* fallthrough */
130 case AF_INET6:
131 /* local address */
132 siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state);
133
134 break;
135 default:
136 /* treat any other address family as AF_UNSPEC */
137 break;
138 }
139}
140
141static int address_compare_func(const void *c1, const void *c2) {
142 const Address *a1 = c1, *a2 = c2;
143
144 if (a1->family < a2->family)
145 return -1;
146 if (a1->family > a2->family)
147 return 1;
148
149 switch (a1->family) {
150 /* use the same notion of equality as the kernel does */
151 case AF_INET:
152 if (a1->prefixlen < a2->prefixlen)
153 return -1;
154 if (a1->prefixlen > a2->prefixlen)
155 return 1;
156
157 /* compare the peer prefixes */
158 if (a1->prefixlen != 0) {
159 /* make sure we don't try to shift by 32.
160 * See ISO/IEC 9899:TC3 ยง 6.5.7.3. */
161 uint32_t b1, b2;
162
163 if (a1->in_addr_peer.in.s_addr != 0)
164 b1 = be32toh(a1->in_addr_peer.in.s_addr) >> (32 - a1->prefixlen);
165 else
166 b1 = be32toh(a1->in_addr.in.s_addr) >> (32 - a1->prefixlen);
167
168 if (a2->in_addr_peer.in.s_addr != 0)
169 b2 = be32toh(a2->in_addr_peer.in.s_addr) >> (32 - a1->prefixlen);
170 else
171 b2 = be32toh(a2->in_addr.in.s_addr) >> (32 - a1->prefixlen);
172
173 if (b1 < b2)
174 return -1;
175 if (b1 > b2)
176 return 1;
177 }
178
179 /* fall-through */
180 case AF_INET6:
181 return memcmp(&a1->in_addr, &a2->in_addr, FAMILY_ADDRESS_SIZE(a1->family));
182 default:
183 /* treat any other address family as AF_UNSPEC */
184 return 0;
185 }
186}
187
188static const struct hash_ops address_hash_ops = {
189 .hash = address_hash_func,
190 .compare = address_compare_func
191};
192
193bool address_equal(Address *a1, Address *a2) {
194 if (a1 == a2)
195 return true;
196
197 if (!a1 || !a2)
198 return false;
199
200 return address_compare_func(a1, a2) == 0;
201}
202
203static int address_establish(Address *address, Link *link) {
e735f4d4
MP
204 bool masq;
205 int r;
206
207 assert(address);
208 assert(link);
209
210 masq = link->network &&
db2df898
MP
211 link->network->ip_masquerade &&
212 address->family == AF_INET &&
213 address->scope < RT_SCOPE_LINK;
e735f4d4
MP
214
215 /* Add firewall entry if this is requested */
216 if (address->ip_masquerade_done != masq) {
217 union in_addr_union masked = address->in_addr;
218 in_addr_mask(address->family, &masked, address->prefixlen);
219
220 r = fw_add_masquerade(masq, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0);
221 if (r < 0)
222 log_link_warning_errno(link, r, "Could not enable IP masquerading: %m");
223
224 address->ip_masquerade_done = masq;
225 }
226
227 return 0;
228}
229
db2df898
MP
230static int address_add_internal(Link *link, Set **addresses,
231 int family,
232 const union in_addr_union *in_addr,
233 unsigned char prefixlen,
234 Address **ret) {
235 _cleanup_address_free_ Address *address = NULL;
e735f4d4
MP
236 int r;
237
e735f4d4 238 assert(link);
db2df898
MP
239 assert(addresses);
240 assert(in_addr);
241
242 r = address_new(&address);
243 if (r < 0)
244 return r;
245
246 address->family = family;
247 address->in_addr = *in_addr;
248 address->prefixlen = prefixlen;
249 /* Consider address tentative until we get the real flags from the kernel */
250 address->flags = IFA_F_TENTATIVE;
251
252 r = set_ensure_allocated(addresses, &address_hash_ops);
253 if (r < 0)
254 return r;
255
256 r = set_put(*addresses, address);
257 if (r < 0)
258 return r;
259
260 address->link = link;
261
262 if (ret)
263 *ret = address;
264
265 address = NULL;
266
267 return 0;
268}
269
270int address_add_foreign(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
271 return address_add_internal(link, &link->addresses_foreign, family, in_addr, prefixlen, ret);
272}
273
274int address_add(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
275 Address *address;
276 int r;
277
278 r = address_get(link, family, in_addr, prefixlen, &address);
279 if (r == -ENOENT) {
280 /* Address does not exist, create a new one */
281 r = address_add_internal(link, &link->addresses, family, in_addr, prefixlen, &address);
282 if (r < 0)
283 return r;
284 } else if (r == 0) {
285 /* Take over a foreign address */
286 r = set_ensure_allocated(&link->addresses, &address_hash_ops);
287 if (r < 0)
288 return r;
289
290 r = set_put(link->addresses, address);
291 if (r < 0)
292 return r;
293
294 set_remove(link->addresses_foreign, address);
295 } else if (r == 1) {
296 /* Already exists, do nothing */
297 ;
298 } else
299 return r;
300
301 if (ret)
302 *ret = address;
303
304 return 0;
305}
306
307static int address_release(Address *address) {
308 int r;
309
310 assert(address);
311 assert(address->link);
e735f4d4
MP
312
313 /* Remove masquerading firewall entry if it was added */
314 if (address->ip_masquerade_done) {
315 union in_addr_union masked = address->in_addr;
316 in_addr_mask(address->family, &masked, address->prefixlen);
317
318 r = fw_add_masquerade(false, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0);
319 if (r < 0)
db2df898 320 log_link_warning_errno(address->link, r, "Failed to disable IP masquerading: %m");
e735f4d4
MP
321
322 address->ip_masquerade_done = false;
323 }
324
325 return 0;
326}
327
db2df898
MP
328int address_update(Address *address, unsigned char flags, unsigned char scope, struct ifa_cacheinfo *cinfo) {
329 bool ready;
60f067b4
JS
330 int r;
331
332 assert(address);
db2df898 333 assert(cinfo);
60f067b4 334
db2df898 335 ready = address_is_ready(address);
e735f4d4 336
db2df898
MP
337 address->flags = flags;
338 address->scope = scope;
339 address->cinfo = *cinfo;
60f067b4 340
db2df898
MP
341 if (address->link) {
342 link_update_operstate(address->link);
60f067b4 343
db2df898
MP
344 if (!ready && address_is_ready(address)) {
345 link_check_ready(address->link);
60f067b4 346
db2df898
MP
347 if (address->family == AF_INET6 &&
348 in_addr_is_link_local(AF_INET6, &address->in_addr) > 0 &&
349 in_addr_is_null(AF_INET6, (const union in_addr_union*) &address->link->ipv6ll_address) > 0) {
350 r = link_ipv6ll_gained(address->link, &address->in_addr.in6);
351 if (r < 0)
352 return r;
353 }
354 }
355 }
60f067b4 356
db2df898
MP
357 return 0;
358}
359
360int address_drop(Address *address) {
361 Link *link;
362 bool ready;
363
364 assert(address);
365
366 ready = address_is_ready(address);
367 link = address->link;
368
369 address_release(address);
370 address_free(address);
371
372 link_update_operstate(link);
373
374 if (link && !ready)
375 link_check_ready(link);
e842803a 376
60f067b4
JS
377 return 0;
378}
379
db2df898
MP
380int address_get(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
381 Address address = {}, *existing;
382
383 assert(link);
384 assert(in_addr);
385 assert(ret);
386
387 address.family = family;
388 address.in_addr = *in_addr;
389 address.prefixlen = prefixlen;
390
391 existing = set_get(link->addresses, &address);
392 if (existing) {
393 *ret = existing;
394
395 return 1;
396 } else {
397 existing = set_get(link->addresses_foreign, &address);
398 if (!existing)
399 return -ENOENT;
400 }
401
402 *ret = existing;
403
404 return 0;
405}
406
407int address_remove(Address *address, Link *link,
408 sd_netlink_message_handler_t callback) {
4c89c718 409 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
60f067b4
JS
410 int r;
411
412 assert(address);
413 assert(address->family == AF_INET || address->family == AF_INET6);
db2df898 414 assert(link);
60f067b4
JS
415 assert(link->ifindex > 0);
416 assert(link->manager);
417 assert(link->manager->rtnl);
418
db2df898 419 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR,
60f067b4 420 link->ifindex, address->family);
f47781d8 421 if (r < 0)
db2df898 422 return log_error_errno(r, "Could not allocate RTM_DELADDR message: %m");
60f067b4
JS
423
424 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
f47781d8
MP
425 if (r < 0)
426 return log_error_errno(r, "Could not set prefixlen: %m");
60f067b4 427
60f067b4 428 if (address->family == AF_INET)
86f210e9 429 r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
60f067b4 430 else if (address->family == AF_INET6)
86f210e9 431 r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
f47781d8
MP
432 if (r < 0)
433 return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");
60f067b4 434
86f210e9 435 r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
f47781d8
MP
436 if (r < 0)
437 return log_error_errno(r, "Could not send rtnetlink message: %m");
60f067b4 438
e842803a
MB
439 link_ref(link);
440
441 return 0;
442}
443
444static int address_acquire(Link *link, Address *original, Address **ret) {
445 union in_addr_union in_addr = {};
446 struct in_addr broadcast = {};
447 _cleanup_address_free_ Address *na = NULL;
448 int r;
449
450 assert(link);
451 assert(original);
452 assert(ret);
453
454 /* Something useful was configured? just use it */
5eef597e 455 if (in_addr_is_null(original->family, &original->in_addr) <= 0)
e842803a
MB
456 return 0;
457
458 /* The address is configured to be 0.0.0.0 or [::] by the user?
459 * Then let's acquire something more useful from the pool. */
460 r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
e3bff60a
MP
461 if (r < 0)
462 return log_link_error_errno(link, r, "Failed to acquire address from pool: %m");
e842803a 463 if (r == 0) {
f47781d8 464 log_link_error(link, "Couldn't find free address for interface, all taken.");
e842803a
MB
465 return -EBUSY;
466 }
467
468 if (original->family == AF_INET) {
e735f4d4 469 /* Pick first address in range for ourselves ... */
e842803a
MB
470 in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1);
471
472 /* .. and use last as broadcast address */
473 broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen);
474 } else if (original->family == AF_INET6)
475 in_addr.in6.s6_addr[15] |= 1;
476
db2df898 477 r = address_new(&na);
e842803a
MB
478 if (r < 0)
479 return r;
480
481 na->family = original->family;
482 na->prefixlen = original->prefixlen;
483 na->scope = original->scope;
484 na->cinfo = original->cinfo;
485
486 if (original->label) {
487 na->label = strdup(original->label);
488 if (!na->label)
489 return -ENOMEM;
490 }
491
492 na->broadcast = broadcast;
493 na->in_addr = in_addr;
494
495 LIST_PREPEND(addresses, link->pool_addresses, na);
496
497 *ret = na;
498 na = NULL;
499
60f067b4
JS
500 return 0;
501}
502
db2df898 503int address_configure(Address *address, Link *link, sd_netlink_message_handler_t callback, bool update) {
4c89c718 504 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
60f067b4
JS
505 int r;
506
507 assert(address);
508 assert(address->family == AF_INET || address->family == AF_INET6);
509 assert(link);
510 assert(link->ifindex > 0);
511 assert(link->manager);
512 assert(link->manager->rtnl);
513
e842803a
MB
514 r = address_acquire(link, address, &address);
515 if (r < 0)
516 return r;
517
db2df898
MP
518 if (update)
519 r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req,
520 link->ifindex, address->family);
521 else
522 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR,
523 link->ifindex, address->family);
f47781d8
MP
524 if (r < 0)
525 return log_error_errno(r, "Could not allocate RTM_NEWADDR message: %m");
60f067b4
JS
526
527 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
f47781d8
MP
528 if (r < 0)
529 return log_error_errno(r, "Could not set prefixlen: %m");
60f067b4 530
e3bff60a
MP
531 address->flags |= IFA_F_PERMANENT;
532
533 r = sd_rtnl_message_addr_set_flags(req, (address->flags & 0xff));
f47781d8
MP
534 if (r < 0)
535 return log_error_errno(r, "Could not set flags: %m");
60f067b4 536
e3bff60a 537 if (address->flags & ~0xff) {
86f210e9 538 r = sd_netlink_message_append_u32(req, IFA_FLAGS, address->flags);
e3bff60a
MP
539 if (r < 0)
540 return log_error_errno(r, "Could not set extended flags: %m");
541 }
542
60f067b4 543 r = sd_rtnl_message_addr_set_scope(req, address->scope);
f47781d8
MP
544 if (r < 0)
545 return log_error_errno(r, "Could not set scope: %m");
60f067b4
JS
546
547 if (address->family == AF_INET)
86f210e9 548 r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
60f067b4 549 else if (address->family == AF_INET6)
86f210e9 550 r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
f47781d8
MP
551 if (r < 0)
552 return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");
60f067b4 553
5eef597e
MP
554 if (!in_addr_is_null(address->family, &address->in_addr_peer)) {
555 if (address->family == AF_INET)
86f210e9 556 r = sd_netlink_message_append_in_addr(req, IFA_ADDRESS, &address->in_addr_peer.in);
5eef597e 557 else if (address->family == AF_INET6)
86f210e9 558 r = sd_netlink_message_append_in6_addr(req, IFA_ADDRESS, &address->in_addr_peer.in6);
f47781d8
MP
559 if (r < 0)
560 return log_error_errno(r, "Could not append IFA_ADDRESS attribute: %m");
5eef597e
MP
561 } else {
562 if (address->family == AF_INET) {
86f210e9 563 r = sd_netlink_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast);
f47781d8
MP
564 if (r < 0)
565 return log_error_errno(r, "Could not append IFA_BROADCAST attribute: %m");
5eef597e 566 }
60f067b4
JS
567 }
568
569 if (address->label) {
86f210e9 570 r = sd_netlink_message_append_string(req, IFA_LABEL, address->label);
f47781d8
MP
571 if (r < 0)
572 return log_error_errno(r, "Could not append IFA_LABEL attribute: %m");
60f067b4
JS
573 }
574
86f210e9 575 r = sd_netlink_message_append_cache_info(req, IFA_CACHEINFO,
e842803a 576 &address->cinfo);
f47781d8
MP
577 if (r < 0)
578 return log_error_errno(r, "Could not append IFA_CACHEINFO attribute: %m");
e842803a 579
db2df898 580 r = address_establish(address, link);
f47781d8 581 if (r < 0)
db2df898
MP
582 return r;
583
584 r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
585 if (r < 0) {
586 address_release(address);
f47781d8 587 return log_error_errno(r, "Could not send rtnetlink message: %m");
db2df898 588 }
60f067b4 589
e842803a
MB
590 link_ref(link);
591
db2df898
MP
592 r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL);
593 if (r < 0) {
594 address_release(address);
595 return log_error_errno(r, "Could not add address: %m");
596 }
e735f4d4 597
60f067b4
JS
598 return 0;
599}
600
5eef597e
MP
601int config_parse_broadcast(
602 const char *unit,
60f067b4
JS
603 const char *filename,
604 unsigned line,
605 const char *section,
606 unsigned section_line,
607 const char *lvalue,
608 int ltype,
609 const char *rvalue,
610 void *data,
611 void *userdata) {
60f067b4 612
60f067b4
JS
613 Network *network = userdata;
614 _cleanup_address_free_ Address *n = NULL;
60f067b4
JS
615 int r;
616
617 assert(filename);
618 assert(section);
619 assert(lvalue);
620 assert(rvalue);
621 assert(data);
622
623 r = address_new_static(network, section_line, &n);
624 if (r < 0)
625 return r;
626
627 if (n->family == AF_INET6) {
6300502b 628 log_syntax(unit, LOG_ERR, filename, line, 0, "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue);
60f067b4
JS
629 return 0;
630 }
631
5eef597e 632 r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast);
60f067b4 633 if (r < 0) {
6300502b 634 log_syntax(unit, LOG_ERR, filename, line, r, "Broadcast is invalid, ignoring assignment: %s", rvalue);
60f067b4
JS
635 return 0;
636 }
637
5eef597e 638 n->family = AF_INET;
60f067b4
JS
639 n = NULL;
640
641 return 0;
642}
643
644int config_parse_address(const char *unit,
645 const char *filename,
646 unsigned line,
647 const char *section,
648 unsigned section_line,
649 const char *lvalue,
650 int ltype,
651 const char *rvalue,
652 void *data,
653 void *userdata) {
5eef597e 654
60f067b4
JS
655 Network *network = userdata;
656 _cleanup_address_free_ Address *n = NULL;
5eef597e
MP
657 const char *address, *e;
658 union in_addr_union buffer;
659 int r, f;
60f067b4
JS
660
661 assert(filename);
662 assert(section);
663 assert(lvalue);
664 assert(rvalue);
665 assert(data);
666
667 if (streq(section, "Network")) {
668 /* we are not in an Address section, so treat
669 * this as the special '0' section */
670 section_line = 0;
671 }
672
673 r = address_new_static(network, section_line, &n);
674 if (r < 0)
675 return r;
676
677 /* Address=address/prefixlen */
678
679 /* prefixlen */
680 e = strchr(rvalue, '/');
681 if (e) {
682 unsigned i;
6300502b 683
60f067b4
JS
684 r = safe_atou(e + 1, &i);
685 if (r < 0) {
6300502b 686 log_syntax(unit, LOG_ERR, filename, line, r, "Prefix length is invalid, ignoring assignment: %s", e + 1);
60f067b4
JS
687 return 0;
688 }
689
690 n->prefixlen = (unsigned char) i;
691
5eef597e
MP
692 address = strndupa(rvalue, e - rvalue);
693 } else
694 address = rvalue;
60f067b4 695
5eef597e 696 r = in_addr_from_string_auto(address, &f, &buffer);
60f067b4 697 if (r < 0) {
6300502b 698 log_syntax(unit, LOG_ERR, filename, line, r, "Address is invalid, ignoring assignment: %s", address);
60f067b4
JS
699 return 0;
700 }
701
5eef597e
MP
702 if (!e && f == AF_INET) {
703 r = in_addr_default_prefixlen(&buffer.in, &n->prefixlen);
704 if (r < 0) {
6300502b 705 log_syntax(unit, LOG_ERR, filename, line, r, "Prefix length not specified, and a default one can not be deduced for '%s', ignoring assignment", address);
5eef597e
MP
706 return 0;
707 }
708 }
709
710 if (n->family != AF_UNSPEC && f != n->family) {
6300502b 711 log_syntax(unit, LOG_ERR, filename, line, 0, "Address is incompatible, ignoring assignment: %s", address);
5eef597e
MP
712 return 0;
713 }
714
715 n->family = f;
716
717 if (streq(lvalue, "Address"))
718 n->in_addr = buffer;
719 else
720 n->in_addr_peer = buffer;
721
722 if (n->family == AF_INET && n->broadcast.s_addr == 0)
723 n->broadcast.s_addr = n->in_addr.in.s_addr | htonl(0xfffffffflu >> n->prefixlen);
60f067b4
JS
724
725 n = NULL;
726
727 return 0;
728}
729
730int config_parse_label(const char *unit,
731 const char *filename,
732 unsigned line,
733 const char *section,
734 unsigned section_line,
735 const char *lvalue,
736 int ltype,
737 const char *rvalue,
738 void *data,
739 void *userdata) {
740 Network *network = userdata;
741 _cleanup_address_free_ Address *n = NULL;
742 char *label;
743 int r;
744
745 assert(filename);
746 assert(section);
747 assert(lvalue);
748 assert(rvalue);
749 assert(data);
750
751 r = address_new_static(network, section_line, &n);
752 if (r < 0)
753 return r;
754
755 label = strdup(rvalue);
756 if (!label)
757 return log_oom();
758
759 if (!ascii_is_valid(label) || strlen(label) >= IFNAMSIZ) {
6300502b 760 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface label is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
60f067b4
JS
761 free(label);
762 return 0;
763 }
764
765 free(n->label);
766 if (*label)
767 n->label = label;
768 else {
769 free(label);
770 n->label = NULL;
771 }
772
773 n = NULL;
774
775 return 0;
776}
777
db2df898
MP
778bool address_is_ready(const Address *a) {
779 assert(a);
60f067b4 780
db2df898 781 return !(a->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED));
60f067b4 782}