]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-address.c
network: set key destructor in several hash_ops
[thirdparty/systemd.git] / src / network / networkd-address.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <net/if.h>
4
5 #include "alloc-util.h"
6 #include "conf-parser.h"
7 #include "firewall-util.h"
8 #include "memory-util.h"
9 #include "missing_network.h"
10 #include "netlink-util.h"
11 #include "networkd-address.h"
12 #include "networkd-manager.h"
13 #include "parse-util.h"
14 #include "set.h"
15 #include "socket-util.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "utf8.h"
19
20 #define ADDRESSES_PER_LINK_MAX 2048U
21 #define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
22
23 int generate_ipv6_eui_64_address(Link *link, struct in6_addr *ret) {
24 assert(link);
25 assert(ret);
26
27 /* see RFC4291 section 2.5.1 */
28 ret->s6_addr[8] = link->mac.ether_addr_octet[0];
29 ret->s6_addr[8] ^= 1 << 1;
30 ret->s6_addr[9] = link->mac.ether_addr_octet[1];
31 ret->s6_addr[10] = link->mac.ether_addr_octet[2];
32 ret->s6_addr[11] = 0xff;
33 ret->s6_addr[12] = 0xfe;
34 ret->s6_addr[13] = link->mac.ether_addr_octet[3];
35 ret->s6_addr[14] = link->mac.ether_addr_octet[4];
36 ret->s6_addr[15] = link->mac.ether_addr_octet[5];
37
38 return 0;
39 }
40
41 int address_new(Address **ret) {
42 _cleanup_(address_freep) Address *address = NULL;
43
44 address = new(Address, 1);
45 if (!address)
46 return -ENOMEM;
47
48 *address = (Address) {
49 .family = AF_UNSPEC,
50 .scope = RT_SCOPE_UNIVERSE,
51 .cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME,
52 .cinfo.ifa_valid = CACHE_INFO_INFINITY_LIFE_TIME,
53 .duplicate_address_detection = ADDRESS_FAMILY_IPV6,
54 .prefix_route = true,
55 };
56
57 *ret = TAKE_PTR(address);
58
59 return 0;
60 }
61
62 static int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) {
63 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
64 _cleanup_(address_freep) Address *address = NULL;
65 int r;
66
67 assert(network);
68 assert(ret);
69 assert(!!filename == (section_line > 0));
70
71 if (filename) {
72 r = network_config_section_new(filename, section_line, &n);
73 if (r < 0)
74 return r;
75
76 address = hashmap_get(network->addresses_by_section, n);
77 if (address) {
78 *ret = TAKE_PTR(address);
79
80 return 0;
81 }
82 }
83
84 if (network->n_static_addresses >= STATIC_ADDRESSES_PER_NETWORK_MAX)
85 return -E2BIG;
86
87 r = address_new(&address);
88 if (r < 0)
89 return r;
90
91 address->network = network;
92 LIST_APPEND(addresses, network->static_addresses, address);
93 network->n_static_addresses++;
94
95 if (filename) {
96 address->section = TAKE_PTR(n);
97
98 r = hashmap_ensure_allocated(&network->addresses_by_section, &network_config_hash_ops);
99 if (r < 0)
100 return r;
101
102 r = hashmap_put(network->addresses_by_section, address->section, address);
103 if (r < 0)
104 return r;
105 }
106
107 *ret = TAKE_PTR(address);
108
109 return 0;
110 }
111
112 void address_free(Address *address) {
113 if (!address)
114 return;
115
116 if (address->network) {
117 LIST_REMOVE(addresses, address->network->static_addresses, address);
118 assert(address->network->n_static_addresses > 0);
119 address->network->n_static_addresses--;
120
121 if (address->section)
122 hashmap_remove(address->network->addresses_by_section, address->section);
123 }
124
125 if (address->link && !address->acd) {
126 set_remove(address->link->addresses, address);
127 set_remove(address->link->addresses_foreign, address);
128
129 if (in_addr_equal(AF_INET6, &address->in_addr, (const union in_addr_union *) &address->link->ipv6ll_address))
130 memzero(&address->link->ipv6ll_address, sizeof(struct in6_addr));
131 }
132
133 sd_ipv4acd_unref(address->acd);
134
135 network_config_section_free(address->section);
136 free(address->label);
137 free(address);
138 }
139
140 static uint32_t address_prefix(const Address *a) {
141 assert(a);
142
143 /* make sure we don't try to shift by 32.
144 * See ISO/IEC 9899:TC3 ยง 6.5.7.3. */
145 if (a->prefixlen == 0)
146 return 0;
147
148 if (a->in_addr_peer.in.s_addr != 0)
149 return be32toh(a->in_addr_peer.in.s_addr) >> (32 - a->prefixlen);
150 else
151 return be32toh(a->in_addr.in.s_addr) >> (32 - a->prefixlen);
152 }
153
154 static void address_hash_func(const Address *a, struct siphash *state) {
155 assert(a);
156
157 siphash24_compress(&a->family, sizeof(a->family), state);
158
159 switch (a->family) {
160 case AF_INET:
161 siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
162
163 /* peer prefix */
164 uint32_t prefix = address_prefix(a);
165 siphash24_compress(&prefix, sizeof(prefix), state);
166
167 _fallthrough_;
168 case AF_INET6:
169 /* local address */
170 siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state);
171
172 break;
173 default:
174 /* treat any other address family as AF_UNSPEC */
175 break;
176 }
177 }
178
179 static int address_compare_func(const Address *a1, const Address *a2) {
180 int r;
181
182 r = CMP(a1->family, a2->family);
183 if (r != 0)
184 return r;
185
186 switch (a1->family) {
187 /* use the same notion of equality as the kernel does */
188 case AF_INET:
189 r = CMP(a1->prefixlen, a2->prefixlen);
190 if (r != 0)
191 return r;
192
193 uint32_t prefix1 = address_prefix(a1);
194 uint32_t prefix2 = address_prefix(a2);
195 r = CMP(prefix1, prefix2);
196 if (r != 0)
197 return r;
198
199 _fallthrough_;
200 case AF_INET6:
201 return memcmp(&a1->in_addr, &a2->in_addr, FAMILY_ADDRESS_SIZE(a1->family));
202 default:
203 /* treat any other address family as AF_UNSPEC */
204 return 0;
205 }
206 }
207
208 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(address_hash_ops, Address, address_hash_func, address_compare_func, address_free);
209
210 bool address_equal(Address *a1, Address *a2) {
211 if (a1 == a2)
212 return true;
213
214 if (!a1 || !a2)
215 return false;
216
217 return address_compare_func(a1, a2) == 0;
218 }
219
220 static int address_establish(Address *address, Link *link) {
221 bool masq;
222 int r;
223
224 assert(address);
225 assert(link);
226
227 masq = link->network &&
228 link->network->ip_masquerade &&
229 address->family == AF_INET &&
230 address->scope < RT_SCOPE_LINK;
231
232 /* Add firewall entry if this is requested */
233 if (address->ip_masquerade_done != masq) {
234 union in_addr_union masked = address->in_addr;
235 in_addr_mask(address->family, &masked, address->prefixlen);
236
237 r = fw_add_masquerade(masq, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0);
238 if (r < 0)
239 return r;
240
241 address->ip_masquerade_done = masq;
242 }
243
244 return 0;
245 }
246
247 static int address_add_internal(Link *link, Set **addresses,
248 int family,
249 const union in_addr_union *in_addr,
250 unsigned char prefixlen,
251 Address **ret) {
252 _cleanup_(address_freep) Address *address = NULL;
253 int r;
254
255 assert(link);
256 assert(addresses);
257 assert(in_addr);
258
259 r = address_new(&address);
260 if (r < 0)
261 return r;
262
263 address->family = family;
264 address->in_addr = *in_addr;
265 address->prefixlen = prefixlen;
266 /* Consider address tentative until we get the real flags from the kernel */
267 address->flags = IFA_F_TENTATIVE;
268
269 r = set_ensure_put(addresses, &address_hash_ops, address);
270 if (r < 0)
271 return r;
272 if (r == 0)
273 return -EEXIST;
274
275 address->link = link;
276
277 if (ret)
278 *ret = address;
279 TAKE_PTR(address);
280 return 0;
281 }
282
283 int address_add_foreign(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
284 return address_add_internal(link, &link->addresses_foreign, family, in_addr, prefixlen, ret);
285 }
286
287 int address_add(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
288 Address *address;
289 int r;
290
291 r = address_get(link, family, in_addr, prefixlen, &address);
292 if (r == -ENOENT) {
293 /* Address does not exist, create a new one */
294 r = address_add_internal(link, &link->addresses, family, in_addr, prefixlen, &address);
295 if (r < 0)
296 return r;
297 } else if (r == 0) {
298 /* Take over a foreign address */
299 r = set_ensure_put(&link->addresses, &address_hash_ops, address);
300 if (r < 0)
301 return r;
302
303 set_remove(link->addresses_foreign, address);
304 } else if (r == 1) {
305 /* Already exists, do nothing */
306 ;
307 } else
308 return r;
309
310 if (ret)
311 *ret = address;
312
313 return 0;
314 }
315
316 static int address_release(Address *address) {
317 int r;
318
319 assert(address);
320 assert(address->link);
321
322 /* Remove masquerading firewall entry if it was added */
323 if (address->ip_masquerade_done) {
324 union in_addr_union masked = address->in_addr;
325 in_addr_mask(address->family, &masked, address->prefixlen);
326
327 r = fw_add_masquerade(false, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0);
328 if (r < 0)
329 return r;
330
331 address->ip_masquerade_done = false;
332 }
333
334 return 0;
335 }
336
337 int address_update(
338 Address *address,
339 unsigned char flags,
340 unsigned char scope,
341 const struct ifa_cacheinfo *cinfo) {
342
343 bool ready;
344 int r;
345
346 assert(address);
347 assert(cinfo);
348 assert_return(address->link, 1);
349
350 if (IN_SET(address->link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
351 return 1;
352
353 ready = address_is_ready(address);
354
355 address->flags = flags;
356 address->scope = scope;
357 address->cinfo = *cinfo;
358
359 link_update_operstate(address->link, true);
360 link_check_ready(address->link);
361
362 if (!ready && address_is_ready(address)) {
363 if (address->callback) {
364 r = address->callback(address);
365 if (r < 0)
366 return r;
367 }
368
369 if (address->family == AF_INET6 &&
370 in_addr_is_link_local(AF_INET6, &address->in_addr) > 0 &&
371 IN6_IS_ADDR_UNSPECIFIED(&address->link->ipv6ll_address) > 0) {
372
373 r = link_ipv6ll_gained(address->link, &address->in_addr.in6);
374 if (r < 0)
375 return r;
376 }
377 }
378
379 return 0;
380 }
381
382 int address_drop(Address *address) {
383 Link *link;
384 bool ready;
385 int r;
386
387 assert(address);
388
389 ready = address_is_ready(address);
390 link = address->link;
391
392 r = address_release(address);
393 if (r < 0)
394 log_link_warning_errno(link, r, "Failed to disable IP masquerading, ignoring: %m");
395
396 address_free(address);
397
398 link_update_operstate(link, true);
399
400 if (link && !ready)
401 link_check_ready(link);
402
403 return 0;
404 }
405
406 int address_get(Link *link,
407 int family,
408 const union in_addr_union *in_addr,
409 unsigned char prefixlen,
410 Address **ret) {
411
412 Address address, *existing;
413
414 assert(link);
415 assert(in_addr);
416
417 address = (Address) {
418 .family = family,
419 .in_addr = *in_addr,
420 .prefixlen = prefixlen,
421 };
422
423 existing = set_get(link->addresses, &address);
424 if (existing) {
425 if (ret)
426 *ret = existing;
427 return 1;
428 }
429
430 existing = set_get(link->addresses_foreign, &address);
431 if (existing) {
432 if (ret)
433 *ret = existing;
434 return 0;
435 }
436
437 return -ENOENT;
438 }
439
440 static bool address_exists_internal(Set *addresses, int family, const union in_addr_union *in_addr) {
441 Address *address;
442 Iterator i;
443
444 SET_FOREACH(address, addresses, i) {
445 if (address->family != family)
446 continue;
447 if (in_addr_equal(address->family, &address->in_addr, in_addr))
448 return true;
449 }
450
451 return false;
452 }
453
454 bool address_exists(Link *link, int family, const union in_addr_union *in_addr) {
455 assert(link);
456 assert(IN_SET(family, AF_INET, AF_INET6));
457 assert(in_addr);
458
459 if (address_exists_internal(link->addresses, family, in_addr))
460 return true;
461 if (address_exists_internal(link->addresses_foreign, family, in_addr))
462 return true;
463 return false;
464 }
465
466 static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
467 int r;
468
469 assert(m);
470 assert(link);
471 assert(link->ifname);
472
473 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
474 return 1;
475
476 r = sd_netlink_message_get_errno(m);
477 if (r < 0 && r != -EADDRNOTAVAIL)
478 log_link_message_warning_errno(link, m, r, "Could not drop address");
479 else
480 (void) manager_rtnl_process_address(rtnl, m, link->manager);
481
482 return 1;
483 }
484
485 int address_remove(
486 Address *address,
487 Link *link,
488 link_netlink_message_handler_t callback) {
489
490 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
491 int r;
492
493 assert(address);
494 assert(IN_SET(address->family, AF_INET, AF_INET6));
495 assert(link);
496 assert(link->ifindex > 0);
497 assert(link->manager);
498 assert(link->manager->rtnl);
499
500 if (DEBUG_LOGGING) {
501 _cleanup_free_ char *b = NULL;
502
503 (void) in_addr_to_string(address->family, &address->in_addr, &b);
504 log_link_debug(link, "Removing address %s", strna(b));
505 }
506
507 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR,
508 link->ifindex, address->family);
509 if (r < 0)
510 return log_link_error_errno(link, r, "Could not allocate RTM_DELADDR message: %m");
511
512 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
513 if (r < 0)
514 return log_link_error_errno(link, r, "Could not set prefixlen: %m");
515
516 r = netlink_message_append_in_addr_union(req, IFA_LOCAL, address->family, &address->in_addr);
517 if (r < 0)
518 return log_link_error_errno(link, r, "Could not append IFA_LOCAL attribute: %m");
519
520 r = netlink_call_async(link->manager->rtnl, NULL, req,
521 callback ?: address_remove_handler,
522 link_netlink_destroy_callback, link);
523 if (r < 0)
524 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
525
526 link_ref(link);
527
528 return 0;
529 }
530
531 static int address_acquire(Link *link, Address *original, Address **ret) {
532 union in_addr_union in_addr = IN_ADDR_NULL;
533 struct in_addr broadcast = {};
534 _cleanup_(address_freep) Address *na = NULL;
535 int r;
536
537 assert(link);
538 assert(original);
539 assert(ret);
540
541 /* Something useful was configured? just use it */
542 r = in_addr_is_null(original->family, &original->in_addr);
543 if (r <= 0)
544 return r;
545
546 /* The address is configured to be 0.0.0.0 or [::] by the user?
547 * Then let's acquire something more useful from the pool. */
548 r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
549 if (r < 0)
550 return r;
551 if (r == 0)
552 return -EBUSY;
553
554 if (original->family == AF_INET) {
555 /* Pick first address in range for ourselves ... */
556 in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1);
557
558 /* .. and use last as broadcast address */
559 if (original->prefixlen > 30)
560 broadcast.s_addr = 0;
561 else
562 broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen);
563 } else if (original->family == AF_INET6)
564 in_addr.in6.s6_addr[15] |= 1;
565
566 r = address_new(&na);
567 if (r < 0)
568 return r;
569
570 na->family = original->family;
571 na->prefixlen = original->prefixlen;
572 na->scope = original->scope;
573 na->cinfo = original->cinfo;
574
575 if (original->label) {
576 na->label = strdup(original->label);
577 if (!na->label)
578 return -ENOMEM;
579 }
580
581 na->broadcast = broadcast;
582 na->in_addr = in_addr;
583
584 LIST_PREPEND(addresses, link->pool_addresses, na);
585
586 *ret = TAKE_PTR(na);
587
588 return 0;
589 }
590
591 int address_configure(
592 Address *address,
593 Link *link,
594 link_netlink_message_handler_t callback,
595 bool update) {
596
597 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
598 int r;
599
600 assert(address);
601 assert(IN_SET(address->family, AF_INET, AF_INET6));
602 assert(link);
603 assert(link->ifindex > 0);
604 assert(link->manager);
605 assert(link->manager->rtnl);
606 assert(callback);
607
608 /* If this is a new address, then refuse adding more than the limit */
609 if (address_get(link, address->family, &address->in_addr, address->prefixlen, NULL) <= 0 &&
610 set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX)
611 return log_link_error_errno(link, SYNTHETIC_ERRNO(E2BIG),
612 "Too many addresses are configured, refusing: %m");
613
614 r = address_acquire(link, address, &address);
615 if (r < 0)
616 return log_link_error_errno(link, r, "Failed to acquire an address from pool: %m");
617
618 if (DEBUG_LOGGING) {
619 _cleanup_free_ char *str = NULL;
620
621 (void) in_addr_to_string(address->family, &address->in_addr, &str);
622 log_link_debug(link, "%s address: %s", update ? "Updating" : "Configuring", strna(str));
623 }
624
625 if (update)
626 r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req,
627 link->ifindex, address->family);
628 else
629 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR,
630 link->ifindex, address->family);
631 if (r < 0)
632 return log_link_error_errno(link, r, "Could not allocate RTM_NEWADDR message: %m");
633
634 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
635 if (r < 0)
636 return log_link_error_errno(link, r, "Could not set prefixlen: %m");
637
638 address->flags |= IFA_F_PERMANENT;
639
640 if (address->home_address)
641 address->flags |= IFA_F_HOMEADDRESS;
642
643 if (!FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV6))
644 address->flags |= IFA_F_NODAD;
645
646 if (address->manage_temporary_address)
647 address->flags |= IFA_F_MANAGETEMPADDR;
648
649 if (!address->prefix_route)
650 address->flags |= IFA_F_NOPREFIXROUTE;
651
652 if (address->autojoin)
653 address->flags |= IFA_F_MCAUTOJOIN;
654
655 r = sd_rtnl_message_addr_set_flags(req, (address->flags & 0xff));
656 if (r < 0)
657 return log_link_error_errno(link, r, "Could not set flags: %m");
658
659 if (address->flags & ~0xff) {
660 r = sd_netlink_message_append_u32(req, IFA_FLAGS, address->flags);
661 if (r < 0)
662 return log_link_error_errno(link, r, "Could not set extended flags: %m");
663 }
664
665 r = sd_rtnl_message_addr_set_scope(req, address->scope);
666 if (r < 0)
667 return log_link_error_errno(link, r, "Could not set scope: %m");
668
669 r = netlink_message_append_in_addr_union(req, IFA_LOCAL, address->family, &address->in_addr);
670 if (r < 0)
671 return log_link_error_errno(link, r, "Could not append IFA_LOCAL attribute: %m");
672
673 if (in_addr_is_null(address->family, &address->in_addr_peer) == 0) {
674 r = netlink_message_append_in_addr_union(req, IFA_ADDRESS, address->family, &address->in_addr_peer);
675 if (r < 0)
676 return log_link_error_errno(link, r, "Could not append IFA_ADDRESS attribute: %m");
677 } else if (address->family == AF_INET && address->prefixlen <= 30) {
678 r = sd_netlink_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast);
679 if (r < 0)
680 return log_link_error_errno(link, r, "Could not append IFA_BROADCAST attribute: %m");
681 }
682
683 if (address->label) {
684 r = sd_netlink_message_append_string(req, IFA_LABEL, address->label);
685 if (r < 0)
686 return log_link_error_errno(link, r, "Could not append IFA_LABEL attribute: %m");
687 }
688
689 r = sd_netlink_message_append_cache_info(req, IFA_CACHEINFO, &address->cinfo);
690 if (r < 0)
691 return log_link_error_errno(link, r, "Could not append IFA_CACHEINFO attribute: %m");
692
693 r = address_establish(address, link);
694 if (r < 0)
695 log_link_warning_errno(link, r, "Could not enable IP masquerading, ignoring: %m");
696
697 r = netlink_call_async(link->manager->rtnl, NULL, req, callback, link_netlink_destroy_callback, link);
698 if (r < 0) {
699 address_release(address);
700 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
701 }
702
703 link_ref(link);
704
705 if (address->family == AF_INET6 && !in_addr_is_null(address->family, &address->in_addr_peer))
706 r = address_add(link, address->family, &address->in_addr_peer, address->prefixlen, NULL);
707 else
708 r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL);
709 if (r < 0) {
710 address_release(address);
711 return log_link_error_errno(link, r, "Could not add address: %m");
712 }
713
714 if (address->acd) {
715 assert(address->family == AF_INET);
716 if (DEBUG_LOGGING) {
717 _cleanup_free_ char *pretty = NULL;
718
719 (void) in_addr_to_string(address->family, &address->in_addr, &pretty);
720 log_link_debug(link, "Starting IPv4ACD client. Probing address %s", strna(pretty));
721 }
722
723 r = sd_ipv4acd_start(address->acd, true);
724 if (r < 0)
725 log_link_warning_errno(link, r, "Failed to start IPv4ACD client, ignoring: %m");
726 }
727
728 return 1;
729 }
730
731 static void static_address_on_acd(sd_ipv4acd *acd, int event, void *userdata) {
732 _cleanup_free_ char *pretty = NULL;
733 Address *address;
734 Link *link;
735 int r;
736
737 assert(acd);
738 assert(userdata);
739
740 address = (Address *) userdata;
741 link = address->link;
742
743 (void) in_addr_to_string(address->family, &address->in_addr, &pretty);
744 switch (event) {
745 case SD_IPV4ACD_EVENT_STOP:
746 log_link_debug(link, "Stopping ACD client...");
747 return;
748
749 case SD_IPV4ACD_EVENT_BIND:
750 log_link_debug(link, "Successfully claimed address %s", strna(pretty));
751 link_check_ready(link);
752 break;
753
754 case SD_IPV4ACD_EVENT_CONFLICT:
755 log_link_warning(link, "DAD conflict. Dropping address %s", strna(pretty));
756 r = address_remove(address, link, NULL);
757 if (r < 0)
758 log_link_error_errno(link, r, "Failed to drop DAD conflicted address %s", strna(pretty));;
759
760 link_check_ready(link);
761 break;
762
763 default:
764 assert_not_reached("Invalid IPv4ACD event.");
765 }
766
767 sd_ipv4acd_stop(acd);
768
769 return;
770 }
771
772 int configure_ipv4_duplicate_address_detection(Link *link, Address *address) {
773 int r;
774
775 assert(link);
776 assert(address);
777 assert(address->family == AF_INET);
778 assert(!address->link && address->network);
779
780 address->link = link;
781
782 r = sd_ipv4acd_new(&address->acd);
783 if (r < 0)
784 return r;
785
786 r = sd_ipv4acd_attach_event(address->acd, NULL, 0);
787 if (r < 0)
788 return r;
789
790 r = sd_ipv4acd_set_ifindex(address->acd, link->ifindex);
791 if (r < 0)
792 return r;
793
794 r = sd_ipv4acd_set_mac(address->acd, &link->mac);
795 if (r < 0)
796 return r;
797
798 r = sd_ipv4acd_set_address(address->acd, &address->in_addr.in);
799 if (r < 0)
800 return r;
801
802 r = sd_ipv4acd_set_callback(address->acd, static_address_on_acd, address);
803 if (r < 0)
804 return r;
805
806 return 0;
807 }
808
809 int config_parse_broadcast(
810 const char *unit,
811 const char *filename,
812 unsigned line,
813 const char *section,
814 unsigned section_line,
815 const char *lvalue,
816 int ltype,
817 const char *rvalue,
818 void *data,
819 void *userdata) {
820
821 Network *network = userdata;
822 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
823 int r;
824
825 assert(filename);
826 assert(section);
827 assert(lvalue);
828 assert(rvalue);
829 assert(data);
830
831 r = address_new_static(network, filename, section_line, &n);
832 if (r == -ENOMEM)
833 return log_oom();
834 if (r < 0) {
835 log_syntax(unit, LOG_WARNING, filename, line, r,
836 "Failed to allocate new address, ignoring assignment: %m");
837 return 0;
838 }
839
840 if (n->family == AF_INET6) {
841 log_syntax(unit, LOG_WARNING, filename, line, 0,
842 "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue);
843 return 0;
844 }
845
846 r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast);
847 if (r < 0) {
848 log_syntax(unit, LOG_WARNING, filename, line, r,
849 "Broadcast is invalid, ignoring assignment: %s", rvalue);
850 return 0;
851 }
852
853 n->family = AF_INET;
854 n = NULL;
855
856 return 0;
857 }
858
859 int config_parse_address(const char *unit,
860 const char *filename,
861 unsigned line,
862 const char *section,
863 unsigned section_line,
864 const char *lvalue,
865 int ltype,
866 const char *rvalue,
867 void *data,
868 void *userdata) {
869
870 Network *network = userdata;
871 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
872 union in_addr_union buffer;
873 unsigned char prefixlen;
874 int r, f;
875
876 assert(filename);
877 assert(section);
878 assert(lvalue);
879 assert(rvalue);
880 assert(data);
881
882 if (streq(section, "Network")) {
883 /* we are not in an Address section, so treat
884 * this as the special '0' section */
885 r = address_new_static(network, NULL, 0, &n);
886 } else
887 r = address_new_static(network, filename, section_line, &n);
888 if (r == -ENOMEM)
889 return log_oom();
890 if (r < 0) {
891 log_syntax(unit, LOG_WARNING, filename, line, r,
892 "Failed to allocate new address, ignoring assignment: %m");
893 return 0;
894 }
895
896 /* Address=address/prefixlen */
897 r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_REFUSE, &f, &buffer, &prefixlen);
898 if (r == -ENOANO) {
899 log_syntax(unit, LOG_WARNING, filename, line, r,
900 "An address '%s' is specified without prefix length. "
901 "The behavior of parsing addresses without prefix length will be changed in the future release. "
902 "Please specify prefix length explicitly.", rvalue);
903
904 r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_LEGACY, &f, &buffer, &prefixlen);
905 }
906 if (r < 0) {
907 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid address '%s', ignoring assignment: %m", rvalue);
908 return 0;
909 }
910
911 if (n->family != AF_UNSPEC && f != n->family) {
912 log_syntax(unit, LOG_WARNING, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue);
913 return 0;
914 }
915
916 if (in_addr_is_null(f, &buffer)) {
917 /* Will use address from address pool. Note that for ipv6 case, prefix of the address
918 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
919 * let's limit the prefix length to 64 or larger. See RFC4193. */
920 if ((f == AF_INET && prefixlen < 8) ||
921 (f == AF_INET6 && prefixlen < 64)) {
922 log_syntax(unit, LOG_WARNING, filename, line, 0,
923 "Null address with invalid prefixlen='%u', ignoring assignment: %s",
924 prefixlen, rvalue);
925 return 0;
926 }
927 }
928
929 n->family = f;
930 n->prefixlen = prefixlen;
931
932 if (streq(lvalue, "Address"))
933 n->in_addr = buffer;
934 else
935 n->in_addr_peer = buffer;
936
937 if (n->family == AF_INET && n->broadcast.s_addr == 0 && n->prefixlen <= 30)
938 n->broadcast.s_addr = n->in_addr.in.s_addr | htobe32(0xfffffffflu >> n->prefixlen);
939
940 n = NULL;
941
942 return 0;
943 }
944
945 int config_parse_label(
946 const char *unit,
947 const char *filename,
948 unsigned line,
949 const char *section,
950 unsigned section_line,
951 const char *lvalue,
952 int ltype,
953 const char *rvalue,
954 void *data,
955 void *userdata) {
956
957 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
958 Network *network = userdata;
959 int r;
960
961 assert(filename);
962 assert(section);
963 assert(lvalue);
964 assert(rvalue);
965 assert(data);
966
967 r = address_new_static(network, filename, section_line, &n);
968 if (r == -ENOMEM)
969 return log_oom();
970 if (r < 0) {
971 log_syntax(unit, LOG_WARNING, filename, line, r,
972 "Failed to allocate new address, ignoring assignment: %m");
973 return 0;
974 }
975
976 if (!address_label_valid(rvalue)) {
977 log_syntax(unit, LOG_WARNING, filename, line, 0,
978 "Interface label is too long or invalid, ignoring assignment: %s", rvalue);
979 return 0;
980 }
981
982 r = free_and_strdup(&n->label, rvalue);
983 if (r < 0)
984 return log_oom();
985
986 n = NULL;
987 return 0;
988 }
989
990 int config_parse_lifetime(const char *unit,
991 const char *filename,
992 unsigned line,
993 const char *section,
994 unsigned section_line,
995 const char *lvalue,
996 int ltype,
997 const char *rvalue,
998 void *data,
999 void *userdata) {
1000 Network *network = userdata;
1001 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1002 uint32_t k;
1003 int r;
1004
1005 assert(filename);
1006 assert(section);
1007 assert(lvalue);
1008 assert(rvalue);
1009 assert(data);
1010
1011 r = address_new_static(network, filename, section_line, &n);
1012 if (r == -ENOMEM)
1013 return log_oom();
1014 if (r < 0) {
1015 log_syntax(unit, LOG_WARNING, filename, line, r,
1016 "Failed to allocate new address, ignoring assignment: %m");
1017 return 0;
1018 }
1019
1020 /* We accept only "forever", "infinity", empty, or "0". */
1021 if (STR_IN_SET(rvalue, "forever", "infinity", ""))
1022 k = CACHE_INFO_INFINITY_LIFE_TIME;
1023 else if (streq(rvalue, "0"))
1024 k = 0;
1025 else {
1026 log_syntax(unit, LOG_WARNING, filename, line, 0,
1027 "Invalid PreferredLifetime= value, ignoring: %s", rvalue);
1028 return 0;
1029 }
1030
1031 n->cinfo.ifa_prefered = k;
1032 TAKE_PTR(n);
1033
1034 return 0;
1035 }
1036
1037 int config_parse_address_flags(const char *unit,
1038 const char *filename,
1039 unsigned line,
1040 const char *section,
1041 unsigned section_line,
1042 const char *lvalue,
1043 int ltype,
1044 const char *rvalue,
1045 void *data,
1046 void *userdata) {
1047 Network *network = userdata;
1048 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1049 int r;
1050
1051 assert(filename);
1052 assert(section);
1053 assert(lvalue);
1054 assert(rvalue);
1055 assert(data);
1056
1057 r = address_new_static(network, filename, section_line, &n);
1058 if (r == -ENOMEM)
1059 return log_oom();
1060 if (r < 0) {
1061 log_syntax(unit, LOG_WARNING, filename, line, r,
1062 "Failed to allocate new address, ignoring assignment: %m");
1063 return 0;
1064 }
1065
1066 r = parse_boolean(rvalue);
1067 if (r < 0) {
1068 log_syntax(unit, LOG_WARNING, filename, line, r,
1069 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
1070 return 0;
1071 }
1072
1073 if (streq(lvalue, "HomeAddress"))
1074 n->home_address = r;
1075 else if (streq(lvalue, "ManageTemporaryAddress"))
1076 n->manage_temporary_address = r;
1077 else if (streq(lvalue, "PrefixRoute"))
1078 n->prefix_route = !r;
1079 else if (streq(lvalue, "AddPrefixRoute"))
1080 n->prefix_route = r;
1081 else if (streq(lvalue, "AutoJoin"))
1082 n->autojoin = r;
1083 else
1084 assert_not_reached("Invalid address flag type.");
1085
1086 n = NULL;
1087 return 0;
1088 }
1089
1090 int config_parse_address_scope(const char *unit,
1091 const char *filename,
1092 unsigned line,
1093 const char *section,
1094 unsigned section_line,
1095 const char *lvalue,
1096 int ltype,
1097 const char *rvalue,
1098 void *data,
1099 void *userdata) {
1100 Network *network = userdata;
1101 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1102 int r;
1103
1104 assert(filename);
1105 assert(section);
1106 assert(lvalue);
1107 assert(rvalue);
1108 assert(data);
1109
1110 r = address_new_static(network, filename, section_line, &n);
1111 if (r == -ENOMEM)
1112 return log_oom();
1113 if (r < 0) {
1114 log_syntax(unit, LOG_WARNING, filename, line, r,
1115 "Failed to allocate new address, ignoring assignment: %m");
1116 return 0;
1117 }
1118
1119 if (streq(rvalue, "host"))
1120 n->scope = RT_SCOPE_HOST;
1121 else if (streq(rvalue, "link"))
1122 n->scope = RT_SCOPE_LINK;
1123 else if (streq(rvalue, "global"))
1124 n->scope = RT_SCOPE_UNIVERSE;
1125 else {
1126 r = safe_atou8(rvalue , &n->scope);
1127 if (r < 0) {
1128 log_syntax(unit, LOG_WARNING, filename, line, r,
1129 "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue);
1130 return 0;
1131 }
1132 }
1133
1134 n->scope_set = true;
1135 n = NULL;
1136 return 0;
1137 }
1138
1139 int config_parse_duplicate_address_detection(
1140 const char *unit,
1141 const char *filename,
1142 unsigned line,
1143 const char *section,
1144 unsigned section_line,
1145 const char *lvalue,
1146 int ltype,
1147 const char *rvalue,
1148 void *data,
1149 void *userdata) {
1150 Network *network = userdata;
1151 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1152 AddressFamily a;
1153 int r;
1154
1155 assert(filename);
1156 assert(section);
1157 assert(lvalue);
1158 assert(rvalue);
1159 assert(data);
1160
1161 r = address_new_static(network, filename, section_line, &n);
1162 if (r == -ENOMEM)
1163 return log_oom();
1164 if (r < 0) {
1165 log_syntax(unit, LOG_WARNING, filename, line, r,
1166 "Failed to allocate new address, ignoring assignment: %m");
1167 return 0;
1168 }
1169
1170 r = parse_boolean(rvalue);
1171 if (r >= 0) {
1172 log_syntax(unit, LOG_WARNING, filename, line, 0,
1173 "For historical reasons, %s=%s means %s=%s. "
1174 "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
1175 lvalue, rvalue, lvalue, r ? "none" : "both");
1176 n->duplicate_address_detection = r ? ADDRESS_FAMILY_NO : ADDRESS_FAMILY_YES;
1177 n = NULL;
1178 return 0;
1179 }
1180
1181 a = duplicate_address_detection_address_family_from_string(rvalue);
1182 if (a < 0) {
1183 log_syntax(unit, LOG_WARNING, filename, line, SYNTHETIC_ERRNO(EINVAL),
1184 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
1185 return 0;
1186 }
1187
1188 n->duplicate_address_detection = a;
1189 n = NULL;
1190 return 0;
1191 }
1192
1193 bool address_is_ready(const Address *a) {
1194 assert(a);
1195
1196 return !(a->flags & IFA_F_TENTATIVE);
1197 }
1198
1199 int address_section_verify(Address *address) {
1200 if (section_is_invalid(address->section))
1201 return -EINVAL;
1202
1203 if (address->family == AF_UNSPEC) {
1204 assert(address->section);
1205
1206 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1207 "%s: Address section without Address= field configured. "
1208 "Ignoring [Address] section from line %u.",
1209 address->section->filename, address->section->line);
1210 }
1211
1212 if (!address->scope_set && in_addr_is_localhost(address->family, &address->in_addr) > 0)
1213 address->scope = RT_SCOPE_HOST;
1214
1215 return 0;
1216 }