1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <net/if.h>
4 #include <net/if_arp.h>
5
6 #include "alloc-util.h"
7 #include "firewall-util.h"
8 #include "memory-util.h"
9 #include "netlink-util.h"
10 #include "networkd-address-pool.h"
11 #include "networkd-address.h"
12 #include "networkd-dhcp-server.h"
13 #include "networkd-ipv4acd.h"
14 #include "networkd-manager.h"
15 #include "networkd-network.h"
16 #include "networkd-queue.h"
17 #include "networkd-route-util.h"
18 #include "networkd-route.h"
19 #include "parse-util.h"
20 #include "string-util.h"
21 #include "strv.h"
22 #include "strxcpyx.h"
23
24 #define ADDRESSES_PER_LINK_MAX 2048U
25 #define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
26
address_flags_to_string_alloc(uint32_t flags,int family,char ** ret)27 int address_flags_to_string_alloc(uint32_t flags, int family, char **ret) {
28 _cleanup_free_ char *str = NULL;
29 static const char* map[] = {
30 [LOG2U(IFA_F_SECONDARY)] = "secondary", /* This is also called "temporary" for ipv6. */
31 [LOG2U(IFA_F_NODAD)] = "nodad",
32 [LOG2U(IFA_F_OPTIMISTIC)] = "optimistic",
33 [LOG2U(IFA_F_DADFAILED)] = "dadfailed",
34 [LOG2U(IFA_F_HOMEADDRESS)] = "home-address",
35 [LOG2U(IFA_F_DEPRECATED)] = "deprecated",
36 [LOG2U(IFA_F_TENTATIVE)] = "tentative",
37 [LOG2U(IFA_F_PERMANENT)] = "permanent",
38 [LOG2U(IFA_F_MANAGETEMPADDR)] = "manage-temporary-address",
39 [LOG2U(IFA_F_NOPREFIXROUTE)] = "no-prefixroute",
40 [LOG2U(IFA_F_MCAUTOJOIN)] = "auto-join",
41 [LOG2U(IFA_F_STABLE_PRIVACY)] = "stable-privacy",
42 };
43
44 assert(IN_SET(family, AF_INET, AF_INET6));
45 assert(ret);
46
47 for (size_t i = 0; i < ELEMENTSOF(map); i++)
48 if (FLAGS_SET(flags, 1 << i) && map[i])
49 if (!strextend_with_separator(
50 &str, ",",
51 family == AF_INET6 && (1 << i) == IFA_F_SECONDARY ? "temporary" : map[i]))
52 return -ENOMEM;
53
54 *ret = TAKE_PTR(str);
55 return 0;
56 }
57
address_new(Address ** ret)58 int address_new(Address **ret) {
59 _cleanup_(address_freep) Address *address = NULL;
60
61 address = new(Address, 1);
62 if (!address)
63 return -ENOMEM;
64
65 *address = (Address) {
66 .family = AF_UNSPEC,
67 .scope = RT_SCOPE_UNIVERSE,
68 .lifetime_valid_usec = USEC_INFINITY,
69 .lifetime_preferred_usec = USEC_INFINITY,
70 .set_broadcast = -1,
71 };
72
73 *ret = TAKE_PTR(address);
74
75 return 0;
76 }
77
address_new_static(Network * network,const char * filename,unsigned section_line,Address ** ret)78 static int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) {
79 _cleanup_(config_section_freep) ConfigSection *n = NULL;
80 _cleanup_(address_freep) Address *address = NULL;
81 int r;
82
83 assert(network);
84 assert(ret);
85 assert(filename);
86 assert(section_line > 0);
87
88 r = config_section_new(filename, section_line, &n);
89 if (r < 0)
90 return r;
91
92 address = ordered_hashmap_get(network->addresses_by_section, n);
93 if (address) {
94 *ret = TAKE_PTR(address);
95 return 0;
96 }
97
98 if (ordered_hashmap_size(network->addresses_by_section) >= STATIC_ADDRESSES_PER_NETWORK_MAX)
99 return -E2BIG;
100
101 r = address_new(&address);
102 if (r < 0)
103 return r;
104
105 address->network = network;
106 address->section = TAKE_PTR(n);
107 address->source = NETWORK_CONFIG_SOURCE_STATIC;
108 /* This will be adjusted in address_section_verify(). */
109 address->duplicate_address_detection = _ADDRESS_FAMILY_INVALID;
110
111 r = ordered_hashmap_ensure_put(&network->addresses_by_section, &config_section_hash_ops, address->section, address);
112 if (r < 0)
113 return r;
114
115 *ret = TAKE_PTR(address);
116 return 0;
117 }
118
address_free(Address * address)119 Address *address_free(Address *address) {
120 if (!address)
121 return NULL;
122
123 if (address->network) {
124 assert(address->section);
125 ordered_hashmap_remove(address->network->addresses_by_section, address->section);
126 }
127
128 if (address->link) {
129 set_remove(address->link->addresses, address);
130
131 if (address->family == AF_INET6 &&
132 in6_addr_equal(&address->in_addr.in6, &address->link->ipv6ll_address))
133 memzero(&address->link->ipv6ll_address, sizeof(struct in6_addr));
134 }
135
136 sd_ipv4acd_unref(address->acd);
137
138 config_section_free(address->section);
139 free(address->label);
140 return mfree(address);
141 }
142
address_is_ready(const Address * a)143 bool address_is_ready(const Address *a) {
144 assert(a);
145
146 if (FLAGS_SET(a->flags, IFA_F_TENTATIVE))
147 return false;
148
149 if (FLAGS_SET(a->state, NETWORK_CONFIG_STATE_REMOVING))
150 return false;
151
152 if (FLAGS_SET(a->state, NETWORK_CONFIG_STATE_PROBING))
153 return false;
154
155 if (!FLAGS_SET(a->state, NETWORK_CONFIG_STATE_CONFIGURED))
156 return false;
157
158 return true;
159 }
160
link_mark_addresses(Link * link,NetworkConfigSource source,const struct in6_addr * router)161 void link_mark_addresses(Link *link, NetworkConfigSource source, const struct in6_addr *router) {
162 Address *a;
163
164 assert(link);
165
166 SET_FOREACH(a, link->addresses) {
167 if (a->source != source)
168 continue;
169
170 if (source == NETWORK_CONFIG_SOURCE_NDISC &&
171 router && !in6_addr_equal(router, &a->provider.in6))
172 continue;
173
174 address_mark(a);
175 }
176 }
177
address_needs_to_set_broadcast(const Address * a,Link * link)178 static bool address_needs_to_set_broadcast(const Address *a, Link *link) {
179 assert(a);
180 assert(link);
181
182 if (a->family != AF_INET)
183 return false;
184
185 if (in4_addr_is_set(&a->in_addr_peer.in))
186 return false;
187
188 /* A /31 or /32 IPv4 address does not have a broadcast address.
189 * See https://tools.ietf.org/html/rfc3021 */
190 if (a->prefixlen > 30)
191 return false;
192
193 /* If explicitly configured, do not update the address. */
194 if (in4_addr_is_set(&a->broadcast))
195 return false;
196
197 if (a->set_broadcast >= 0)
198 return a->set_broadcast;
199
200 /* Defaults to true, except for wireguard, as typical configuration for wireguard does not set
201 * broadcast. */
202 return !streq_ptr(link->kind, "wireguard");
203 }
204
address_set_broadcast(Address * a,Link * link)205 void address_set_broadcast(Address *a, Link *link) {
206 assert(a);
207 assert(link);
208
209 if (!address_needs_to_set_broadcast(a, link))
210 return;
211
212 a->broadcast.s_addr = a->in_addr.in.s_addr | htobe32(UINT32_C(0xffffffff) >> a->prefixlen);
213 }
214
address_set_cinfo(const Address * a,struct ifa_cacheinfo * cinfo)215 static struct ifa_cacheinfo *address_set_cinfo(const Address *a, struct ifa_cacheinfo *cinfo) {
216 usec_t now_usec;
217
218 assert(a);
219 assert(cinfo);
220
221 now_usec = now(CLOCK_BOOTTIME);
222
223 *cinfo = (struct ifa_cacheinfo) {
224 .ifa_valid = MIN(usec_sub_unsigned(a->lifetime_valid_usec, now_usec) / USEC_PER_SEC, UINT32_MAX),
225 .ifa_prefered = MIN(usec_sub_unsigned(a->lifetime_preferred_usec, now_usec) / USEC_PER_SEC, UINT32_MAX),
226 };
227
228 return cinfo;
229 }
230
address_set_lifetime(Address * a,const struct ifa_cacheinfo * cinfo)231 static void address_set_lifetime(Address *a, const struct ifa_cacheinfo *cinfo) {
232 usec_t now_usec;
233
234 assert(a);
235 assert(cinfo);
236
237 now_usec = now(CLOCK_BOOTTIME);
238
239 if (cinfo->ifa_valid == UINT32_MAX)
240 a->lifetime_valid_usec = USEC_INFINITY;
241 else
242 a->lifetime_valid_usec = usec_add(cinfo->ifa_valid * USEC_PER_SEC, now_usec);
243
244 if (cinfo->ifa_prefered == UINT32_MAX)
245 a->lifetime_preferred_usec = USEC_INFINITY;
246 else
247 a->lifetime_preferred_usec = usec_add(cinfo->ifa_prefered * USEC_PER_SEC, now_usec);
248 }
249
address_prefix(const Address * a)250 static uint32_t address_prefix(const Address *a) {
251 assert(a);
252
253 /* make sure we don't try to shift by 32.
254 * See ISO/IEC 9899:TC3 § 6.5.7.3. */
255 if (a->prefixlen == 0)
256 return 0;
257
258 if (a->in_addr_peer.in.s_addr != 0)
259 return be32toh(a->in_addr_peer.in.s_addr) >> (32 - a->prefixlen);
260 else
261 return be32toh(a->in_addr.in.s_addr) >> (32 - a->prefixlen);
262 }
263
address_kernel_hash_func(const Address * a,struct siphash * state)264 static void address_kernel_hash_func(const Address *a, struct siphash *state) {
265 assert(a);
266
267 siphash24_compress(&a->family, sizeof(a->family), state);
268
269 switch (a->family) {
270 case AF_INET:
271 siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
272
273 uint32_t prefix = address_prefix(a);
274 siphash24_compress(&prefix, sizeof(prefix), state);
275
276 _fallthrough_;
277 case AF_INET6:
278 siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state);
279 break;
280 default:
281 /* treat any other address family as AF_UNSPEC */
282 break;
283 }
284 }
285
address_kernel_compare_func(const Address * a1,const Address * a2)286 static int address_kernel_compare_func(const Address *a1, const Address *a2) {
287 int r;
288
289 r = CMP(a1->family, a2->family);
290 if (r != 0)
291 return r;
292
293 switch (a1->family) {
294 case AF_INET:
295 /* See kernel's find_matching_ifa() in net/ipv4/devinet.c */
296 r = CMP(a1->prefixlen, a2->prefixlen);
297 if (r != 0)
298 return r;
299
300 r = CMP(address_prefix(a1), address_prefix(a2));
301 if (r != 0)
302 return r;
303
304 _fallthrough_;
305 case AF_INET6:
306 /* See kernel's ipv6_get_ifaddr() in net/ipv6/addrconf.c */
307 return memcmp(&a1->in_addr, &a2->in_addr, FAMILY_ADDRESS_SIZE(a1->family));
308 default:
309 /* treat any other address family as AF_UNSPEC */
310 return 0;
311 }
312 }
313
314 DEFINE_PRIVATE_HASH_OPS(
315 address_kernel_hash_ops,
316 Address,
317 address_kernel_hash_func,
318 address_kernel_compare_func);
319
address_hash_func(const Address * a,struct siphash * state)320 static void address_hash_func(const Address *a, struct siphash *state) {
321 assert(a);
322
323 siphash24_compress(&a->family, sizeof(a->family), state);
324
325 /* treat any other address family as AF_UNSPEC */
326 if (!IN_SET(a->family, AF_INET, AF_INET6))
327 return;
328
329 siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
330 siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state);
331 siphash24_compress(&a->in_addr_peer, FAMILY_ADDRESS_SIZE(a->family), state);
332
333 if (a->family == AF_INET) {
334 /* On update, the kernel ignores the address label and broadcast address, hence we need
335 * to distinguish addresses with different labels or broadcast addresses. Otherwise,
336 * the label or broadcast address change will not be applied when we reconfigure the
337 * interface. */
338 siphash24_compress_string(a->label, state);
339 siphash24_compress(&a->broadcast, sizeof(a->broadcast), state);
340 }
341 }
342
address_compare_func(const Address * a1,const Address * a2)343 int address_compare_func(const Address *a1, const Address *a2) {
344 int r;
345
346 r = CMP(a1->family, a2->family);
347 if (r != 0)
348 return r;
349
350 if (!IN_SET(a1->family, AF_INET, AF_INET6))
351 return 0;
352
353 r = CMP(a1->prefixlen, a2->prefixlen);
354 if (r != 0)
355 return r;
356
357 r = memcmp(&a1->in_addr, &a2->in_addr, FAMILY_ADDRESS_SIZE(a1->family));
358 if (r != 0)
359 return r;
360
361 r = memcmp(&a1->in_addr_peer, &a2->in_addr_peer, FAMILY_ADDRESS_SIZE(a1->family));
362 if (r != 0)
363 return r;
364
365 if (a1->family == AF_INET) {
366 r = strcmp_ptr(a1->label, a2->label);
367 if (r != 0)
368 return r;
369
370 r = CMP(a1->broadcast.s_addr, a2->broadcast.s_addr);
371 if (r != 0)
372 return r;
373 }
374
375 return 0;
376 }
377
378 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
379 address_hash_ops_free,
380 Address,
381 address_hash_func,
382 address_compare_func,
383 address_free);
384
address_dup(const Address * src,Address ** ret)385 int address_dup(const Address *src, Address **ret) {
386 _cleanup_(address_freep) Address *dest = NULL;
387 int r;
388
389 assert(src);
390 assert(ret);
391
392 dest = newdup(Address, src, 1);
393 if (!dest)
394 return -ENOMEM;
395
396 /* clear all pointers */
397 dest->network = NULL;
398 dest->section = NULL;
399 dest->link = NULL;
400 dest->label = NULL;
401 dest->acd = NULL;
402
403 if (src->family == AF_INET) {
404 r = free_and_strdup(&dest->label, src->label);
405 if (r < 0)
406 return r;
407 }
408
409 *ret = TAKE_PTR(dest);
410 return 0;
411 }
412
address_set_masquerade(Address * address,bool add)413 static int address_set_masquerade(Address *address, bool add) {
414 union in_addr_union masked;
415 int r;
416
417 assert(address);
418 assert(address->link);
419
420 if (!address->link->network)
421 return 0;
422
423 if (address->family == AF_INET &&
424 !FLAGS_SET(address->link->network->ip_masquerade, ADDRESS_FAMILY_IPV4))
425 return 0;
426
427 if (address->family == AF_INET6 &&
428 !FLAGS_SET(address->link->network->ip_masquerade, ADDRESS_FAMILY_IPV6))
429 return 0;
430
431 if (address->scope >= RT_SCOPE_LINK)
432 return 0;
433
434 if (address->ip_masquerade_done == add)
435 return 0;
436
437 masked = address->in_addr;
438 r = in_addr_mask(address->family, &masked, address->prefixlen);
439 if (r < 0)
440 return r;
441
442 r = fw_add_masquerade(&address->link->manager->fw_ctx, add, address->family, &masked, address->prefixlen);
443 if (r < 0)
444 return r;
445
446 address->ip_masquerade_done = add;
447
448 return 0;
449 }
450
address_add(Link * link,Address * address)451 static int address_add(Link *link, Address *address) {
452 int r;
453
454 assert(link);
455 assert(address);
456
457 r = set_ensure_put(&link->addresses, &address_hash_ops_free, address);
458 if (r < 0)
459 return r;
460 if (r == 0)
461 return -EEXIST;
462
463 address->link = link;
464 return 0;
465 }
466
address_update(Address * address)467 static int address_update(Address *address) {
468 Link *link;
469 int r;
470
471 assert(address);
472 assert(address->link);
473
474 link = address->link;
475
476 if (address_is_ready(address) &&
477 address->family == AF_INET6 &&
478 in6_addr_is_link_local(&address->in_addr.in6) &&
479 in6_addr_is_null(&link->ipv6ll_address)) {
480
481 link->ipv6ll_address = address->in_addr.in6;
482
483 r = link_ipv6ll_gained(link);
484 if (r < 0)
485 return r;
486 }
487
488 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
489 return 0;
490
491 r = address_set_masquerade(address, true);
492 if (r < 0)
493 return log_link_warning_errno(link, r, "Could not enable IP masquerading: %m");
494
495 if (address_is_ready(address) && address->callback) {
496 r = address->callback(address);
497 if (r < 0)
498 return r;
499 }
500
501 link_update_operstate(link, true);
502 link_check_ready(link);
503 return 0;
504 }
505
address_drop(Address * address)506 static int address_drop(Address *address) {
507 Link *link;
508 bool ready;
509 int r;
510
511 assert(address);
512 assert(address->link);
513
514 ready = address_is_ready(address);
515 link = address->link;
516
517 r = address_set_masquerade(address, false);
518 if (r < 0)
519 log_link_warning_errno(link, r, "Failed to disable IP masquerading, ignoring: %m");
520
521 if (address->state == 0)
522 address_free(address);
523
524 link_update_operstate(link, true);
525
526 if (link && !ready)
527 link_check_ready(link);
528
529 return 0;
530 }
531
address_get(Link * link,const Address * in,Address ** ret)532 int address_get(Link *link, const Address *in, Address **ret) {
533 Address *existing;
534
535 assert(link);
536 assert(in);
537
538 existing = set_get(link->addresses, in);
539 if (!existing)
540 return -ENOENT;
541
542 if (ret)
543 *ret = existing;
544 return 0;
545 }
546
link_get_address(Link * link,int family,const union in_addr_union * address,unsigned char prefixlen,Address ** ret)547 int link_get_address(Link *link, int family, const union in_addr_union *address, unsigned char prefixlen, Address **ret) {
548 Address *a;
549 int r;
550
551 assert(link);
552 assert(IN_SET(family, AF_INET, AF_INET6));
553 assert(address);
554
555 /* This find an Address object on the link which matches the given address and prefix length
556 * and does not have peer address. When the prefixlen is zero, then an Address object with an
557 * arbitrary prefixlen will be returned. */
558
559 if (prefixlen != 0) {
560 _cleanup_(address_freep) Address *tmp = NULL;
561
562 /* If prefixlen is set, then we can use address_get(). */
563
564 r = address_new(&tmp);
565 if (r < 0)
566 return r;
567
568 tmp->family = family;
569 tmp->in_addr = *address;
570 tmp->prefixlen = prefixlen;
571 address_set_broadcast(tmp, link);
572
573 if (address_get(link, tmp, &a) >= 0) {
574 if (ret)
575 *ret = a;
576
577 return 0;
578 }
579
580 if (family == AF_INET6)
581 return -ENOENT;
582
583 /* IPv4 addresses may have label and/or non-default broadcast address.
584 * Hence, we need to always fallback below. */
585 }
586
587 SET_FOREACH(a, link->addresses) {
588 if (a->family != family)
589 continue;
590
591 if (!in_addr_equal(family, &a->in_addr, address))
592 continue;
593
594 if (in_addr_is_set(family, &a->in_addr_peer))
595 continue;
596
597 if (ret)
598 *ret = a;
599
600 return 0;
601 }
602
603 return -ENOENT;
604 }
605
manager_get_address(Manager * manager,int family,const union in_addr_union * address,unsigned char prefixlen,Address ** ret)606 int manager_get_address(Manager *manager, int family, const union in_addr_union *address, unsigned char prefixlen, Address **ret) {
607 Link *link;
608
609 assert(manager);
610 assert(IN_SET(family, AF_INET, AF_INET6));
611 assert(address);
612
613 HASHMAP_FOREACH(link, manager->links_by_index) {
614 if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
615 continue;
616
617 if (link_get_address(link, family, address, prefixlen, ret) >= 0)
618 return 0;
619 }
620
621 return -ENOENT;
622 }
623
manager_has_address(Manager * manager,int family,const union in_addr_union * address,bool check_ready)624 bool manager_has_address(Manager *manager, int family, const union in_addr_union *address, bool check_ready) {
625 Address *a;
626
627 assert(manager);
628 assert(IN_SET(family, AF_INET, AF_INET6));
629 assert(address);
630
631 if (manager_get_address(manager, family, address, 0, &a) < 0)
632 return false;
633
634 return check_ready ? address_is_ready(a) : address_exists(a);
635 }
636
format_lifetime(char * buf,size_t l,usec_t lifetime_usec)637 const char* format_lifetime(char *buf, size_t l, usec_t lifetime_usec) {
638 assert(buf);
639 assert(l > 4);
640
641 if (lifetime_usec == USEC_INFINITY)
642 return "forever";
643
644 sprintf(buf, "for ");
645 /* format_timespan() never fails */
646 assert_se(format_timespan(buf + 4, l - 4, usec_sub_unsigned(lifetime_usec, now(CLOCK_BOOTTIME)), USEC_PER_SEC));
647 return buf;
648 }
649
log_address_debug(const Address * address,const char * str,const Link * link)650 static void log_address_debug(const Address *address, const char *str, const Link *link) {
651 _cleanup_free_ char *state = NULL, *addr = NULL, *peer = NULL, *flags_str = NULL, *scope_str = NULL;
652
653 assert(address);
654 assert(str);
655 assert(link);
656
657 if (!DEBUG_LOGGING)
658 return;
659
660 (void) network_config_state_to_string_alloc(address->state, &state);
661 (void) in_addr_to_string(address->family, &address->in_addr, &addr);
662 if (in_addr_is_set(address->family, &address->in_addr_peer))
663 (void) in_addr_to_string(address->family, &address->in_addr_peer, &peer);
664
665 (void) address_flags_to_string_alloc(address->flags, address->family, &flags_str);
666 (void) route_scope_to_string_alloc(address->scope, &scope_str);
667
668 log_link_debug(link, "%s %s address (%s): %s%s%s/%u (valid %s, preferred %s), flags: %s, scope: %s",
669 str, strna(network_config_source_to_string(address->source)), strna(state),
670 strnull(addr), peer ? " peer " : "", strempty(peer), address->prefixlen,
671 FORMAT_LIFETIME(address->lifetime_valid_usec),
672 FORMAT_LIFETIME(address->lifetime_preferred_usec),
673 strna(flags_str), strna(scope_str));
674 }
675
address_set_netlink_message(const Address * address,sd_netlink_message * m,Link * link)676 static int address_set_netlink_message(const Address *address, sd_netlink_message *m, Link *link) {
677 uint32_t flags;
678 int r;
679
680 assert(address);
681 assert(m);
682 assert(link);
683
684 r = sd_rtnl_message_addr_set_prefixlen(m, address->prefixlen);
685 if (r < 0)
686 return r;
687
688 /* On remove, only IFA_F_MANAGETEMPADDR flag for IPv6 addresses are used. But anyway, set all
689 * flags except tentative flag here unconditionally. Without setting the flag, the template
690 * addresses generated by kernel will not be removed automatically when the main address is
691 * removed. */
692 flags = address->flags & ~IFA_F_TENTATIVE;
693 r = sd_rtnl_message_addr_set_flags(m, flags & 0xff);
694 if (r < 0)
695 return r;
696
697 if ((flags & ~0xff) != 0) {
698 r = sd_netlink_message_append_u32(m, IFA_FLAGS, flags);
699 if (r < 0)
700 return r;
701 }
702
703 r = netlink_message_append_in_addr_union(m, IFA_LOCAL, address->family, &address->in_addr);
704 if (r < 0)
705 return r;
706
707 return 0;
708 }
709
address_remove_handler(sd_netlink * rtnl,sd_netlink_message * m,Link * link)710 static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
711 int r;
712
713 assert(m);
714 assert(link);
715
716 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
717 return 0;
718
719 r = sd_netlink_message_get_errno(m);
720 if (r < 0 && r != -EADDRNOTAVAIL)
721 log_link_message_warning_errno(link, m, r, "Could not drop address");
722
723 return 1;
724 }
725
address_remove(Address * address)726 int address_remove(Address *address) {
727 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
728 Link *link;
729 int r;
730
731 assert(address);
732 assert(IN_SET(address->family, AF_INET, AF_INET6));
733 assert(address->link);
734 assert(address->link->ifindex > 0);
735 assert(address->link->manager);
736 assert(address->link->manager->rtnl);
737
738 link = address->link;
739
740 log_address_debug(address, "Removing", link);
741
742 r = sd_rtnl_message_new_addr(link->manager->rtnl, &m, RTM_DELADDR,
743 link->ifindex, address->family);
744 if (r < 0)
745 return log_link_warning_errno(link, r, "Could not allocate RTM_DELADDR message: %m");
746
747 r = address_set_netlink_message(address, m, link);
748 if (r < 0)
749 return log_link_warning_errno(link, r, "Could not set netlink attributes: %m");
750
751 r = netlink_call_async(link->manager->rtnl, NULL, m,
752 address_remove_handler,
753 link_netlink_destroy_callback, link);
754 if (r < 0)
755 return log_link_warning_errno(link, r, "Could not send rtnetlink message: %m");
756
757 link_ref(link);
758
759 address_enter_removing(address);
760
761 /* The operational state is determined by address state and carrier state. Hence, if we remove
762 * an address, the operational state may be changed. */
763 link_update_operstate(link, true);
764 return 0;
765 }
766
link_address_is_dynamic(const Link * link,const Address * address)767 bool link_address_is_dynamic(const Link *link, const Address *address) {
768 Route *route;
769
770 assert(link);
771 assert(address);
772
773 if (address->lifetime_preferred_usec != USEC_INFINITY)
774 return true;
775
776 /* Even when the address is leased from a DHCP server, networkd assign the address
777 * without lifetime when KeepConfiguration=dhcp. So, let's check that we have
778 * corresponding routes with RTPROT_DHCP. */
779 SET_FOREACH(route, link->routes) {
780 if (route->source != NETWORK_CONFIG_SOURCE_FOREIGN)
781 continue;
782
783 /* The route is not assigned yet, or already removed. Ignoring. */
784 if (!route_exists(route))
785 continue;
786
787 if (route->protocol != RTPROT_DHCP)
788 continue;
789
790 if (address->family != route->family)
791 continue;
792
793 if (in_addr_equal(address->family, &address->in_addr, &route->prefsrc))
794 return true;
795 }
796
797 return false;
798 }
799
link_drop_ipv6ll_addresses(Link * link)800 int link_drop_ipv6ll_addresses(Link *link) {
801 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
802 int r;
803
804 assert(link);
805 assert(link->manager);
806 assert(link->manager->rtnl);
807
808 /* IPv6LL address may be in the tentative state, and in that case networkd has not received it.
809 * So, we need to dump all IPv6 addresses. */
810
811 if (link_may_have_ipv6ll(link))
812 return 0;
813
814 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_GETADDR, link->ifindex, AF_INET6);
815 if (r < 0)
816 return r;
817
818 r = sd_netlink_message_request_dump(req, true);
819 if (r < 0)
820 return r;
821
822 r = sd_netlink_call(link->manager->rtnl, req, 0, &reply);
823 if (r < 0)
824 return r;
825
826 for (sd_netlink_message *addr = reply; addr; addr = sd_netlink_message_next(addr)) {
827 _cleanup_(address_freep) Address *a = NULL;
828 unsigned char flags, prefixlen;
829 struct in6_addr address;
830 Address *existing;
831 int ifindex;
832
833 /* NETLINK_GET_STRICT_CHK socket option is supported since kernel 4.20. To support
834 * older kernels, we need to check ifindex here. */
835 r = sd_rtnl_message_addr_get_ifindex(addr, &ifindex);
836 if (r < 0) {
837 log_link_debug_errno(link, r, "rtnl: received address message without valid ifindex, ignoring: %m");
838 continue;
839 } else if (link->ifindex != ifindex)
840 continue;
841
842 r = sd_rtnl_message_addr_get_flags(addr, &flags);
843 if (r < 0) {
844 log_link_debug_errno(link, r, "rtnl: received address message without valid flags, ignoring: %m");
845 continue;
846 }
847
848 r = sd_rtnl_message_addr_get_prefixlen(addr, &prefixlen);
849 if (r < 0) {
850 log_link_debug_errno(link, r, "rtnl: received address message without prefixlen, ignoring: %m");
851 continue;
852 }
853
854 if (sd_netlink_message_read_in6_addr(addr, IFA_LOCAL, NULL) >= 0)
855 /* address with peer, ignoring. */
856 continue;
857
858 r = sd_netlink_message_read_in6_addr(addr, IFA_ADDRESS, &address);
859 if (r < 0) {
860 log_link_debug_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
861 continue;
862 }
863
864 if (!in6_addr_is_link_local(&address))
865 continue;
866
867 r = address_new(&a);
868 if (r < 0)
869 return -ENOMEM;
870
871 a->family = AF_INET6;
872 a->in_addr.in6 = address;
873 a->prefixlen = prefixlen;
874 a->flags = flags;
875
876 if (address_get(link, a, &existing) < 0) {
877 r = address_add(link, a);
878 if (r < 0)
879 return r;
880
881 existing = TAKE_PTR(a);
882 }
883
884 r = address_remove(existing);
885 if (r < 0)
886 return r;
887 }
888
889 return 0;
890 }
891
link_drop_foreign_addresses(Link * link)892 int link_drop_foreign_addresses(Link *link) {
893 Address *address;
894 int k, r = 0;
895
896 assert(link);
897 assert(link->network);
898
899 /* First, mark all addresses. */
900 SET_FOREACH(address, link->addresses) {
901 /* We consider IPv6LL addresses to be managed by the kernel, or dropped in link_drop_ipv6ll_addresses() */
902 if (address->family == AF_INET6 && in6_addr_is_link_local(&address->in_addr.in6))
903 continue;
904
905 /* Do not remove localhost address (127.0.0.1 and ::1) */
906 if (link->flags & IFF_LOOPBACK && in_addr_is_localhost_one(address->family, &address->in_addr) > 0)
907 continue;
908
909 /* Ignore addresses we configured. */
910 if (address->source != NETWORK_CONFIG_SOURCE_FOREIGN)
911 continue;
912
913 /* Ignore addresses not assigned yet or already removing. */
914 if (!address_exists(address))
915 continue;
916
917 /* link_address_is_dynamic() is slightly heavy. Let's call the function only when KeepConfiguration= is set. */
918 if (IN_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP, KEEP_CONFIGURATION_STATIC) &&
919 link_address_is_dynamic(link, address) == (link->network->keep_configuration == KEEP_CONFIGURATION_DHCP))
920 continue;
921
922 address_mark(address);
923 }
924
925 /* Then, unmark requested addresses. */
926 ORDERED_HASHMAP_FOREACH(address, link->network->addresses_by_section) {
927 Address *existing;
928
929 if (address_get(link, address, &existing) >= 0)
930 address_unmark(existing);
931 }
932
933 /* Finally, remove all marked addresses. */
934 SET_FOREACH(address, link->addresses) {
935 if (!address_is_marked(address))
936 continue;
937
938 k = address_remove(address);
939 if (k < 0 && r >= 0)
940 r = k;
941 }
942
943 return r;
944 }
945
link_drop_managed_addresses(Link * link)946 int link_drop_managed_addresses(Link *link) {
947 Address *address;
948 int k, r = 0;
949
950 assert(link);
951
952 SET_FOREACH(address, link->addresses) {
953 /* Do not touch addresses managed by kernel or other tools. */
954 if (address->source == NETWORK_CONFIG_SOURCE_FOREIGN)
955 continue;
956
957 /* Ignore addresses not assigned yet or already removing. */
958 if (!address_exists(address))
959 continue;
960
961 k = address_remove(address);
962 if (k < 0 && r >= 0) {
963 r = k;
964 continue;
965 }
966 }
967
968 return r;
969 }
970
link_foreignize_addresses(Link * link)971 void link_foreignize_addresses(Link *link) {
972 Address *address;
973
974 assert(link);
975
976 SET_FOREACH(address, link->addresses)
977 address->source = NETWORK_CONFIG_SOURCE_FOREIGN;
978 }
979
address_acquire(Link * link,const Address * original,Address ** ret)980 static int address_acquire(Link *link, const Address *original, Address **ret) {
981 _cleanup_(address_freep) Address *na = NULL;
982 union in_addr_union in_addr;
983 int r;
984
985 assert(link);
986 assert(original);
987 assert(ret);
988
989 /* Something useful was configured? just use it */
990 if (in_addr_is_set(original->family, &original->in_addr)) {
991 *ret = NULL;
992 return 0;
993 }
994
995 /* The address is configured to be 0.0.0.0 or [::] by the user?
996 * Then let's acquire something more useful from the pool. */
997 r = address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
998 if (r < 0)
999 return r;
1000 if (r == 0)
1001 return -EBUSY;
1002
1003 /* Pick first address in range for ourselves. */
1004 if (original->family == AF_INET)
1005 in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1);
1006 else if (original->family == AF_INET6)
1007 in_addr.in6.s6_addr[15] |= 1;
1008
1009 r = address_dup(original, &na);
1010 if (r < 0)
1011 return r;
1012
1013 na->in_addr = in_addr;
1014
1015 *ret = TAKE_PTR(na);
1016 return 1;
1017 }
1018
address_configure_handler_internal(sd_netlink * rtnl,sd_netlink_message * m,Link * link,const char * error_msg)1019 int address_configure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
1020 int r;
1021
1022 assert(rtnl);
1023 assert(m);
1024 assert(link);
1025 assert(error_msg);
1026
1027 r = sd_netlink_message_get_errno(m);
1028 if (r < 0 && r != -EEXIST) {
1029 log_link_message_warning_errno(link, m, r, error_msg);
1030 link_enter_failed(link);
1031 return 0;
1032 }
1033
1034 return 1;
1035 }
1036
address_configure(const Address * address,Link * link,Request * req)1037 static int address_configure(const Address *address, Link *link, Request *req) {
1038 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
1039 int r;
1040
1041 assert(address);
1042 assert(IN_SET(address->family, AF_INET, AF_INET6));
1043 assert(link);
1044 assert(link->ifindex > 0);
1045 assert(link->manager);
1046 assert(link->manager->rtnl);
1047 assert(req);
1048
1049 log_address_debug(address, "Configuring", link);
1050
1051 r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &m, link->ifindex, address->family);
1052 if (r < 0)
1053 return r;
1054
1055 r = address_set_netlink_message(address, m, link);
1056 if (r < 0)
1057 return r;
1058
1059 r = sd_rtnl_message_addr_set_scope(m, address->scope);
1060 if (r < 0)
1061 return r;
1062
1063 if (in_addr_is_set(address->family, &address->in_addr_peer)) {
1064 r = netlink_message_append_in_addr_union(m, IFA_ADDRESS, address->family, &address->in_addr_peer);
1065 if (r < 0)
1066 return r;
1067 } else if (in4_addr_is_set(&address->broadcast)) {
1068 r = sd_netlink_message_append_in_addr(m, IFA_BROADCAST, &address->broadcast);
1069 if (r < 0)
1070 return r;
1071 }
1072
1073 if (address->family == AF_INET && address->label) {
1074 r = sd_netlink_message_append_string(m, IFA_LABEL, address->label);
1075 if (r < 0)
1076 return r;
1077 }
1078
1079 r = sd_netlink_message_append_cache_info(m, IFA_CACHEINFO,
1080 address_set_cinfo(address, &(struct ifa_cacheinfo) {}));
1081 if (r < 0)
1082 return r;
1083
1084 r = sd_netlink_message_append_u32(m, IFA_RT_PRIORITY, address->route_metric);
1085 if (r < 0)
1086 return r;
1087
1088 return request_call_netlink_async(link->manager->rtnl, m, req);
1089 }
1090
address_is_ready_to_configure(Link * link,const Address * address)1091 static bool address_is_ready_to_configure(Link *link, const Address *address) {
1092 assert(link);
1093 assert(address);
1094
1095 if (!link_is_ready_to_configure(link, false))
1096 return false;
1097
1098 if (FLAGS_SET(address->state, NETWORK_CONFIG_STATE_PROBING))
1099 return false;
1100
1101 /* Refuse adding more than the limit */
1102 if (set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX)
1103 return false;
1104
1105 return true;
1106 }
1107
address_process_request(Request * req,Link * link,Address * address)1108 static int address_process_request(Request *req, Link *link, Address *address) {
1109 int r;
1110
1111 assert(req);
1112 assert(link);
1113 assert(address);
1114
1115 if (!address_is_ready_to_configure(link, address))
1116 return 0;
1117
1118 r = address_configure(address, link, req);
1119 if (r < 0)
1120 return log_link_warning_errno(link, r, "Failed to configure address: %m");
1121
1122 address_enter_configuring(address);
1123 return 1;
1124 }
1125
link_request_address(Link * link,Address * address,bool consume_object,unsigned * message_counter,address_netlink_handler_t netlink_handler,Request ** ret)1126 int link_request_address(
1127 Link *link,
1128 Address *address,
1129 bool consume_object,
1130 unsigned *message_counter,
1131 address_netlink_handler_t netlink_handler,
1132 Request **ret) {
1133
1134 Address *acquired, *existing;
1135 int r;
1136
1137 assert(link);
1138 assert(address);
1139 assert(address->source != NETWORK_CONFIG_SOURCE_FOREIGN);
1140
1141 r = address_acquire(link, address, &acquired);
1142 if (r < 0)
1143 return log_link_warning_errno(link, r, "Failed to acquire an address from pool: %m");
1144 if (r > 0) {
1145 if (consume_object)
1146 address_free(address);
1147
1148 address = acquired;
1149 consume_object = true;
1150 }
1151
1152 if (address_needs_to_set_broadcast(address, link)) {
1153 if (!consume_object) {
1154 Address *a;
1155
1156 r = address_dup(address, &a);
1157 if (r < 0)
1158 return r;
1159
1160 address = a;
1161 consume_object = true;
1162 }
1163
1164 address_set_broadcast(address, link);
1165 }
1166
1167 if (address_get(link, address, &existing) < 0) {
1168 _cleanup_(address_freep) Address *tmp = NULL;
1169
1170 if (consume_object)
1171 tmp = address;
1172 else {
1173 r = address_dup(address, &tmp);
1174 if (r < 0)
1175 return r;
1176 }
1177
1178 /* Consider address tentative until we get the real flags from the kernel */
1179 tmp->flags |= IFA_F_TENTATIVE;
1180
1181 r = address_add(link, tmp);
1182 if (r < 0)
1183 return r;
1184
1185 existing = TAKE_PTR(tmp);
1186 } else {
1187 existing->source = address->source;
1188 existing->provider = address->provider;
1189 existing->lifetime_valid_usec = address->lifetime_valid_usec;
1190 existing->lifetime_preferred_usec = address->lifetime_preferred_usec;
1191 if (consume_object)
1192 address_free(address);
1193 }
1194
1195 r = ipv4acd_configure(existing);
1196 if (r < 0)
1197 return r;
1198
1199 log_address_debug(existing, "Requesting", link);
1200 r = link_queue_request_safe(link, REQUEST_TYPE_ADDRESS,
1201 existing, NULL,
1202 address_hash_func,
1203 address_compare_func,
1204 address_process_request,
1205 message_counter, netlink_handler, ret);
1206 if (r < 0)
1207 return log_link_warning_errno(link, r, "Failed to request address: %m");
1208 if (r == 0)
1209 return 0;
1210
1211 address_enter_requesting(existing);
1212
1213 return 1;
1214 }
1215
static_address_handler(sd_netlink * rtnl,sd_netlink_message * m,Request * req,Link * link,Address * address)1216 static int static_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) {
1217 int r;
1218
1219 assert(link);
1220
1221 r = address_configure_handler_internal(rtnl, m, link, "Failed to set static address");
1222 if (r <= 0)
1223 return r;
1224
1225 if (link->static_address_messages == 0) {
1226 log_link_debug(link, "Addresses set");
1227 link->static_addresses_configured = true;
1228 link_check_ready(link);
1229 }
1230
1231 return 1;
1232 }
1233
link_request_static_address(Link * link,Address * address,bool consume)1234 int link_request_static_address(Link *link, Address *address, bool consume) {
1235 assert(link);
1236 assert(address);
1237 assert(address->source == NETWORK_CONFIG_SOURCE_STATIC);
1238
1239 return link_request_address(link, address, consume, &link->static_address_messages,
1240 static_address_handler, NULL);
1241 }
1242
link_request_static_addresses(Link * link)1243 int link_request_static_addresses(Link *link) {
1244 Address *a;
1245 int r;
1246
1247 assert(link);
1248 assert(link->network);
1249
1250 link->static_addresses_configured = false;
1251
1252 ORDERED_HASHMAP_FOREACH(a, link->network->addresses_by_section) {
1253 r = link_request_static_address(link, a, false);
1254 if (r < 0)
1255 return r;
1256 }
1257
1258 r = link_request_radv_addresses(link);
1259 if (r < 0)
1260 return r;
1261
1262 r = link_request_dhcp_server_address(link);
1263 if (r < 0)
1264 return r;
1265
1266 if (link->static_address_messages == 0) {
1267 link->static_addresses_configured = true;
1268 link_check_ready(link);
1269 } else {
1270 log_link_debug(link, "Setting addresses");
1271 link_set_state(link, LINK_STATE_CONFIGURING);
1272 }
1273
1274 return 0;
1275 }
1276
address_cancel_request(Address * address)1277 void address_cancel_request(Address *address) {
1278 Request req;
1279
1280 assert(address);
1281 assert(address->link);
1282
1283 if (!address_is_requesting(address))
1284 return;
1285
1286 req = (Request) {
1287 .link = address->link,
1288 .type = REQUEST_TYPE_ADDRESS,
1289 .userdata = address,
1290 .hash_func = (hash_func_t) address_hash_func,
1291 .compare_func = (compare_func_t) address_compare_func,
1292 };
1293
1294 request_detach(address->link->manager, &req);
1295 address_cancel_requesting(address);
1296 }
1297
manager_rtnl_process_address(sd_netlink * rtnl,sd_netlink_message * message,Manager * m)1298 int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
1299 _cleanup_(address_freep) Address *tmp = NULL;
1300 struct ifa_cacheinfo cinfo;
1301 Link *link = NULL;
1302 uint16_t type;
1303 Address *address = NULL;
1304 int ifindex, r;
1305
1306 assert(rtnl);
1307 assert(message);
1308 assert(m);
1309
1310 if (sd_netlink_message_is_error(message)) {
1311 r = sd_netlink_message_get_errno(message);
1312 if (r < 0)
1313 log_message_warning_errno(message, r, "rtnl: failed to receive address message, ignoring");
1314
1315 return 0;
1316 }
1317
1318 r = sd_netlink_message_get_type(message, &type);
1319 if (r < 0) {
1320 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
1321 return 0;
1322 } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) {
1323 log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type);
1324 return 0;
1325 }
1326
1327 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
1328 if (r < 0) {
1329 log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m");
1330 return 0;
1331 } else if (ifindex <= 0) {
1332 log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex);
1333 return 0;
1334 }
1335
1336 r = link_get_by_index(m, ifindex, &link);
1337 if (r < 0 || !link) {
1338 /* when enumerating we might be out of sync, but we will get the address again, so just
1339 * ignore it */
1340 if (!m->enumerating)
1341 log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex);
1342 return 0;
1343 }
1344
1345 r = address_new(&tmp);
1346 if (r < 0)
1347 return log_oom();
1348
1349 r = sd_rtnl_message_addr_get_family(message, &tmp->family);
1350 if (r < 0) {
1351 log_link_warning(link, "rtnl: received address message without family, ignoring.");
1352 return 0;
1353 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1354 log_link_debug(link, "rtnl: received address message with invalid family '%i', ignoring.", tmp->family);
1355 return 0;
1356 }
1357
1358 r = sd_rtnl_message_addr_get_prefixlen(message, &tmp->prefixlen);
1359 if (r < 0) {
1360 log_link_warning_errno(link, r, "rtnl: received address message without prefixlen, ignoring: %m");
1361 return 0;
1362 }
1363
1364 r = sd_rtnl_message_addr_get_scope(message, &tmp->scope);
1365 if (r < 0) {
1366 log_link_warning_errno(link, r, "rtnl: received address message without scope, ignoring: %m");
1367 return 0;
1368 }
1369
1370 r = sd_netlink_message_read_u32(message, IFA_FLAGS, &tmp->flags);
1371 if (r == -ENODATA) {
1372 unsigned char flags;
1373
1374 /* For old kernels. */
1375 r = sd_rtnl_message_addr_get_flags(message, &flags);
1376 if (r >= 0)
1377 tmp->flags = flags;
1378 }
1379 if (r < 0) {
1380 log_link_warning_errno(link, r, "rtnl: received address message without flags, ignoring: %m");
1381 return 0;
1382 }
1383
1384 switch (tmp->family) {
1385 case AF_INET:
1386 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &tmp->in_addr.in);
1387 if (r < 0) {
1388 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
1389 return 0;
1390 }
1391
1392 r = sd_netlink_message_read_in_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in);
1393 if (r < 0 && r != -ENODATA) {
1394 log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m");
1395 return 0;
1396 } else if (r >= 0) {
1397 if (in4_addr_equal(&tmp->in_addr.in, &tmp->in_addr_peer.in))
1398 tmp->in_addr_peer = IN_ADDR_NULL;
1399 }
1400
1401 r = sd_netlink_message_read_in_addr(message, IFA_BROADCAST, &tmp->broadcast);
1402 if (r < 0 && r != -ENODATA) {
1403 log_link_warning_errno(link, r, "rtnl: could not get broadcast from address message, ignoring: %m");
1404 return 0;
1405 }
1406
1407 r = sd_netlink_message_read_string_strdup(message, IFA_LABEL, &tmp->label);
1408 if (r < 0 && r != -ENODATA) {
1409 log_link_warning_errno(link, r, "rtnl: could not get label from address message, ignoring: %m");
1410 return 0;
1411 } else if (r >= 0 && streq_ptr(tmp->label, link->ifname))
1412 tmp->label = mfree(tmp->label);
1413
1414 break;
1415
1416 case AF_INET6:
1417 r = sd_netlink_message_read_in6_addr(message, IFA_LOCAL, &tmp->in_addr.in6);
1418 if (r >= 0) {
1419 /* Have peer address. */
1420 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in6);
1421 if (r < 0) {
1422 log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m");
1423 return 0;
1424 }
1425 } else if (r == -ENODATA) {
1426 /* Does not have peer address. */
1427 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr.in6);
1428 if (r < 0) {
1429 log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m");
1430 return 0;
1431 }
1432 } else {
1433 log_link_warning_errno(link, r, "rtnl: could not get local address from address message, ignoring: %m");
1434 return 0;
1435 }
1436
1437 break;
1438
1439 default:
1440 assert_not_reached();
1441 }
1442
1443 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
1444 if (r < 0 && r != -ENODATA) {
1445 log_link_warning_errno(link, r, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m");
1446 return 0;
1447 }
1448
1449 (void) address_get(link, tmp, &address);
1450
1451 switch (type) {
1452 case RTM_NEWADDR:
1453 if (address) {
1454 /* update flags and etc. */
1455 address->flags = tmp->flags;
1456 address->scope = tmp->scope;
1457 address_set_lifetime(address, &cinfo);
1458 address_enter_configured(address);
1459 log_address_debug(address, "Received updated", link);
1460 } else {
1461 address_set_lifetime(tmp, &cinfo);
1462 address_enter_configured(tmp);
1463 log_address_debug(tmp, "Received new", link);
1464
1465 r = address_add(link, tmp);
1466 if (r < 0) {
1467 _cleanup_free_ char *buf = NULL;
1468
1469 (void) in_addr_prefix_to_string(tmp->family, &tmp->in_addr, tmp->prefixlen, &buf);
1470 log_link_warning_errno(link, r, "Failed to remember foreign address %s, ignoring: %m",
1471 strnull(buf));
1472 return 0;
1473 }
1474
1475 address = TAKE_PTR(tmp);
1476 }
1477
1478 /* address_update() logs internally, so we don't need to here. */
1479 r = address_update(address);
1480 if (r < 0)
1481 link_enter_failed(link);
1482
1483 break;
1484
1485 case RTM_DELADDR:
1486 if (address) {
1487 address_enter_removed(address);
1488 log_address_debug(address, address->state == 0 ? "Forgetting" : "Removed", link);
1489 (void) address_drop(address);
1490 } else
1491 log_address_debug(tmp, "Kernel removed unknown", link);
1492
1493 break;
1494
1495 default:
1496 assert_not_reached();
1497 }
1498
1499 return 1;
1500 }
1501
config_parse_broadcast(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)1502 int config_parse_broadcast(
1503 const char *unit,
1504 const char *filename,
1505 unsigned line,
1506 const char *section,
1507 unsigned section_line,
1508 const char *lvalue,
1509 int ltype,
1510 const char *rvalue,
1511 void *data,
1512 void *userdata) {
1513
1514 Network *network = userdata;
1515 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1516 union in_addr_union u;
1517 int r;
1518
1519 assert(filename);
1520 assert(section);
1521 assert(lvalue);
1522 assert(rvalue);
1523 assert(data);
1524
1525 r = address_new_static(network, filename, section_line, &n);
1526 if (r == -ENOMEM)
1527 return log_oom();
1528 if (r < 0) {
1529 log_syntax(unit, LOG_WARNING, filename, line, r,
1530 "Failed to allocate new address, ignoring assignment: %m");
1531 return 0;
1532 }
1533
1534 if (isempty(rvalue)) {
1535 /* The broadcast address will be calculated based on Address=, and set if the link is
1536 * not a wireguard interface. Here, we do not check or set n->family. */
1537 n->broadcast = (struct in_addr) {};
1538 n->set_broadcast = -1;
1539 TAKE_PTR(n);
1540 return 0;
1541 }
1542
1543 r = parse_boolean(rvalue);
1544 if (r >= 0) {
1545 /* The broadcast address will be calculated based on Address=. Here, we do not check or
1546 * set n->family. */
1547 n->broadcast = (struct in_addr) {};
1548 n->set_broadcast = r;
1549 TAKE_PTR(n);
1550 return 0;
1551 }
1552
1553 if (n->family == AF_INET6) {
1554 log_syntax(unit, LOG_WARNING, filename, line, 0,
1555 "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue);
1556 return 0;
1557 }
1558
1559 r = in_addr_from_string(AF_INET, rvalue, &u);
1560 if (r < 0) {
1561 log_syntax(unit, LOG_WARNING, filename, line, r,
1562 "Broadcast is invalid, ignoring assignment: %s", rvalue);
1563 return 0;
1564 }
1565 if (in4_addr_is_null(&u.in)) {
1566 log_syntax(unit, LOG_WARNING, filename, line, 0,
1567 "Broadcast cannot be ANY address, ignoring assignment: %s", rvalue);
1568 return 0;
1569 }
1570
1571 n->broadcast = u.in;
1572 n->set_broadcast = true;
1573 n->family = AF_INET;
1574 TAKE_PTR(n);
1575
1576 return 0;
1577 }
1578
config_parse_address(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)1579 int config_parse_address(
1580 const char *unit,
1581 const char *filename,
1582 unsigned line,
1583 const char *section,
1584 unsigned section_line,
1585 const char *lvalue,
1586 int ltype,
1587 const char *rvalue,
1588 void *data,
1589 void *userdata) {
1590
1591 Network *network = userdata;
1592 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1593 union in_addr_union buffer;
1594 unsigned char prefixlen;
1595 int r, f;
1596
1597 assert(filename);
1598 assert(section);
1599 assert(lvalue);
1600 assert(rvalue);
1601 assert(data);
1602
1603 if (streq(section, "Network"))
1604 /* we are not in an Address section, so use line number instead. */
1605 r = address_new_static(network, filename, line, &n);
1606 else
1607 r = address_new_static(network, filename, section_line, &n);
1608 if (r == -ENOMEM)
1609 return log_oom();
1610 if (r < 0) {
1611 log_syntax(unit, LOG_WARNING, filename, line, r,
1612 "Failed to allocate new address, ignoring assignment: %m");
1613 return 0;
1614 }
1615
1616 /* Address=address/prefixlen */
1617 r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_REFUSE, &f, &buffer, &prefixlen);
1618 if (r == -ENOANO) {
1619 log_syntax(unit, LOG_WARNING, filename, line, r,
1620 "An address '%s' is specified without prefix length. "
1621 "The behavior of parsing addresses without prefix length will be changed in the future release. "
1622 "Please specify prefix length explicitly.", rvalue);
1623
1624 r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_LEGACY, &f, &buffer, &prefixlen);
1625 }
1626 if (r < 0) {
1627 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid address '%s', ignoring assignment: %m", rvalue);
1628 return 0;
1629 }
1630
1631 if (n->family != AF_UNSPEC && f != n->family) {
1632 log_syntax(unit, LOG_WARNING, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue);
1633 return 0;
1634 }
1635
1636 if (in_addr_is_null(f, &buffer)) {
1637 /* Will use address from address pool. Note that for ipv6 case, prefix of the address
1638 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
1639 * let's limit the prefix length to 64 or larger. See RFC4193. */
1640 if ((f == AF_INET && prefixlen < 8) ||
1641 (f == AF_INET6 && prefixlen < 64)) {
1642 log_syntax(unit, LOG_WARNING, filename, line, 0,
1643 "Null address with invalid prefixlen='%u', ignoring assignment: %s",
1644 prefixlen, rvalue);
1645 return 0;
1646 }
1647 }
1648
1649 n->family = f;
1650 n->prefixlen = prefixlen;
1651
1652 if (streq(lvalue, "Address"))
1653 n->in_addr = buffer;
1654 else
1655 n->in_addr_peer = buffer;
1656
1657 TAKE_PTR(n);
1658 return 0;
1659 }
1660
config_parse_label(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)1661 int config_parse_label(
1662 const char *unit,
1663 const char *filename,
1664 unsigned line,
1665 const char *section,
1666 unsigned section_line,
1667 const char *lvalue,
1668 int ltype,
1669 const char *rvalue,
1670 void *data,
1671 void *userdata) {
1672
1673 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1674 Network *network = userdata;
1675 int r;
1676
1677 assert(filename);
1678 assert(section);
1679 assert(lvalue);
1680 assert(rvalue);
1681 assert(data);
1682
1683 r = address_new_static(network, filename, section_line, &n);
1684 if (r == -ENOMEM)
1685 return log_oom();
1686 if (r < 0) {
1687 log_syntax(unit, LOG_WARNING, filename, line, r,
1688 "Failed to allocate new address, ignoring assignment: %m");
1689 return 0;
1690 }
1691
1692 if (!address_label_valid(rvalue)) {
1693 log_syntax(unit, LOG_WARNING, filename, line, 0,
1694 "Interface label is too long or invalid, ignoring assignment: %s", rvalue);
1695 return 0;
1696 }
1697
1698 r = free_and_strdup(&n->label, rvalue);
1699 if (r < 0)
1700 return log_oom();
1701
1702 TAKE_PTR(n);
1703 return 0;
1704 }
1705
config_parse_lifetime(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)1706 int config_parse_lifetime(
1707 const char *unit,
1708 const char *filename,
1709 unsigned line,
1710 const char *section,
1711 unsigned section_line,
1712 const char *lvalue,
1713 int ltype,
1714 const char *rvalue,
1715 void *data,
1716 void *userdata) {
1717
1718 Network *network = userdata;
1719 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1720 usec_t k;
1721 int r;
1722
1723 assert(filename);
1724 assert(section);
1725 assert(lvalue);
1726 assert(rvalue);
1727 assert(data);
1728
1729 r = address_new_static(network, filename, section_line, &n);
1730 if (r == -ENOMEM)
1731 return log_oom();
1732 if (r < 0) {
1733 log_syntax(unit, LOG_WARNING, filename, line, r,
1734 "Failed to allocate new address, ignoring assignment: %m");
1735 return 0;
1736 }
1737
1738 /* We accept only "forever", "infinity", empty, or "0". */
1739 if (STR_IN_SET(rvalue, "forever", "infinity", ""))
1740 k = USEC_INFINITY;
1741 else if (streq(rvalue, "0"))
1742 k = 0;
1743 else {
1744 log_syntax(unit, LOG_WARNING, filename, line, 0,
1745 "Invalid PreferredLifetime= value, ignoring: %s", rvalue);
1746 return 0;
1747 }
1748
1749 n->lifetime_preferred_usec = k;
1750 TAKE_PTR(n);
1751
1752 return 0;
1753 }
1754
config_parse_address_flags(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)1755 int config_parse_address_flags(
1756 const char *unit,
1757 const char *filename,
1758 unsigned line,
1759 const char *section,
1760 unsigned section_line,
1761 const char *lvalue,
1762 int ltype,
1763 const char *rvalue,
1764 void *data,
1765 void *userdata) {
1766
1767 Network *network = userdata;
1768 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1769 int r;
1770
1771 assert(filename);
1772 assert(section);
1773 assert(lvalue);
1774 assert(rvalue);
1775 assert(data);
1776
1777 r = address_new_static(network, filename, section_line, &n);
1778 if (r == -ENOMEM)
1779 return log_oom();
1780 if (r < 0) {
1781 log_syntax(unit, LOG_WARNING, filename, line, r,
1782 "Failed to allocate new address, ignoring assignment: %m");
1783 return 0;
1784 }
1785
1786 r = parse_boolean(rvalue);
1787 if (r < 0) {
1788 log_syntax(unit, LOG_WARNING, filename, line, r,
1789 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
1790 return 0;
1791 }
1792
1793 if (streq(lvalue, "AddPrefixRoute"))
1794 r = !r;
1795
1796 SET_FLAG(n->flags, ltype, r);
1797
1798 TAKE_PTR(n);
1799 return 0;
1800 }
1801
config_parse_address_scope(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)1802 int config_parse_address_scope(
1803 const char *unit,
1804 const char *filename,
1805 unsigned line,
1806 const char *section,
1807 unsigned section_line,
1808 const char *lvalue,
1809 int ltype,
1810 const char *rvalue,
1811 void *data,
1812 void *userdata) {
1813
1814 Network *network = userdata;
1815 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1816 int r;
1817
1818 assert(filename);
1819 assert(section);
1820 assert(lvalue);
1821 assert(rvalue);
1822 assert(data);
1823
1824 r = address_new_static(network, filename, section_line, &n);
1825 if (r == -ENOMEM)
1826 return log_oom();
1827 if (r < 0) {
1828 log_syntax(unit, LOG_WARNING, filename, line, r,
1829 "Failed to allocate new address, ignoring assignment: %m");
1830 return 0;
1831 }
1832
1833 r = route_scope_from_string(rvalue);
1834 if (r < 0) {
1835 log_syntax(unit, LOG_WARNING, filename, line, r,
1836 "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue);
1837 return 0;
1838 }
1839
1840 n->scope = r;
1841 n->scope_set = true;
1842 TAKE_PTR(n);
1843 return 0;
1844 }
1845
config_parse_address_route_metric(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)1846 int config_parse_address_route_metric(
1847 const char *unit,
1848 const char *filename,
1849 unsigned line,
1850 const char *section,
1851 unsigned section_line,
1852 const char *lvalue,
1853 int ltype,
1854 const char *rvalue,
1855 void *data,
1856 void *userdata) {
1857
1858 Network *network = userdata;
1859 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1860 int r;
1861
1862 assert(filename);
1863 assert(section);
1864 assert(lvalue);
1865 assert(rvalue);
1866 assert(data);
1867
1868 r = address_new_static(network, filename, section_line, &n);
1869 if (r == -ENOMEM)
1870 return log_oom();
1871 if (r < 0) {
1872 log_syntax(unit, LOG_WARNING, filename, line, r,
1873 "Failed to allocate new address, ignoring assignment: %m");
1874 return 0;
1875 }
1876
1877 r = safe_atou32(rvalue, &n->route_metric);
1878 if (r < 0) {
1879 log_syntax(unit, LOG_WARNING, filename, line, r,
1880 "Could not parse %s=, ignoring assignment: %s", lvalue, rvalue);
1881 return 0;
1882 }
1883
1884 TAKE_PTR(n);
1885 return 0;
1886 }
1887
config_parse_duplicate_address_detection(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)1888 int config_parse_duplicate_address_detection(
1889 const char *unit,
1890 const char *filename,
1891 unsigned line,
1892 const char *section,
1893 unsigned section_line,
1894 const char *lvalue,
1895 int ltype,
1896 const char *rvalue,
1897 void *data,
1898 void *userdata) {
1899
1900 Network *network = userdata;
1901 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1902 int r;
1903
1904 assert(filename);
1905 assert(section);
1906 assert(lvalue);
1907 assert(rvalue);
1908 assert(data);
1909
1910 r = address_new_static(network, filename, section_line, &n);
1911 if (r == -ENOMEM)
1912 return log_oom();
1913 if (r < 0) {
1914 log_syntax(unit, LOG_WARNING, filename, line, r,
1915 "Failed to allocate new address, ignoring assignment: %m");
1916 return 0;
1917 }
1918
1919 r = parse_boolean(rvalue);
1920 if (r >= 0) {
1921 log_syntax(unit, LOG_WARNING, filename, line, 0,
1922 "For historical reasons, %s=%s means %s=%s. "
1923 "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
1924 lvalue, rvalue, lvalue, r ? "none" : "both");
1925 n->duplicate_address_detection = r ? ADDRESS_FAMILY_NO : ADDRESS_FAMILY_YES;
1926 n = NULL;
1927 return 0;
1928 }
1929
1930 AddressFamily a = duplicate_address_detection_address_family_from_string(rvalue);
1931 if (a < 0) {
1932 log_syntax(unit, LOG_WARNING, filename, line, a,
1933 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
1934 return 0;
1935 }
1936 n->duplicate_address_detection = a;
1937
1938 TAKE_PTR(n);
1939 return 0;
1940 }
1941
address_section_verify(Address * address)1942 static int address_section_verify(Address *address) {
1943 if (section_is_invalid(address->section))
1944 return -EINVAL;
1945
1946 if (address->family == AF_UNSPEC) {
1947 assert(address->section);
1948
1949 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1950 "%s: Address section without Address= field configured. "
1951 "Ignoring [Address] section from line %u.",
1952 address->section->filename, address->section->line);
1953 }
1954
1955 assert(IN_SET(address->family, AF_INET, AF_INET6));
1956
1957 if (in4_addr_is_set(&address->broadcast) &&
1958 (address->family == AF_INET6 || address->prefixlen > 30 ||
1959 in_addr_is_set(address->family, &address->in_addr_peer))) {
1960 log_warning("%s: broadcast address is set for an IPv6 address, "
1961 "an IPv4 address with peer address, or with prefix length larger than 30. "
1962 "Ignoring Broadcast= setting in the [Address] section from line %u.",
1963 address->section->filename, address->section->line);
1964
1965 address->broadcast.s_addr = 0;
1966 }
1967
1968 if (address->family == AF_INET6 && address->label) {
1969 log_warning("%s: address label is set for IPv6 address in the [Address] section from line %u. "
1970 "Ignoring Label= setting.",
1971 address->section->filename, address->section->line);
1972
1973 address->label = mfree(address->label);
1974 }
1975
1976 if (!address->scope_set) {
1977 if (in_addr_is_localhost(address->family, &address->in_addr) > 0)
1978 address->scope = RT_SCOPE_HOST;
1979 else if (in_addr_is_link_local(address->family, &address->in_addr) > 0)
1980 address->scope = RT_SCOPE_LINK;
1981 }
1982
1983 if (address->duplicate_address_detection < 0) {
1984 if (address->family == AF_INET6)
1985 address->duplicate_address_detection = ADDRESS_FAMILY_IPV6;
1986 else if (in4_addr_is_link_local(&address->in_addr.in))
1987 address->duplicate_address_detection = ADDRESS_FAMILY_IPV4;
1988 else
1989 address->duplicate_address_detection = ADDRESS_FAMILY_NO;
1990 } else if (address->duplicate_address_detection == ADDRESS_FAMILY_IPV6 && address->family == AF_INET)
1991 log_warning("%s: DuplicateAddressDetection=ipv6 is specified for IPv4 address, ignoring.",
1992 address->section->filename);
1993 else if (address->duplicate_address_detection == ADDRESS_FAMILY_IPV4 && address->family == AF_INET6)
1994 log_warning("%s: DuplicateAddressDetection=ipv4 is specified for IPv6 address, ignoring.",
1995 address->section->filename);
1996
1997 if (address->family == AF_INET6 &&
1998 !FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV6))
1999 address->flags |= IFA_F_NODAD;
2000
2001 return 0;
2002 }
2003
network_drop_invalid_addresses(Network * network)2004 int network_drop_invalid_addresses(Network *network) {
2005 _cleanup_set_free_ Set *addresses = NULL;
2006 Address *address;
2007 int r;
2008
2009 assert(network);
2010
2011 ORDERED_HASHMAP_FOREACH(address, network->addresses_by_section) {
2012 Address *dup;
2013
2014 if (address_section_verify(address) < 0) {
2015 /* Drop invalid [Address] sections or Address= settings in [Network].
2016 * Note that address_free() will drop the address from addresses_by_section. */
2017 address_free(address);
2018 continue;
2019 }
2020
2021 /* Always use the setting specified later. So, remove the previously assigned setting. */
2022 dup = set_remove(addresses, address);
2023 if (dup) {
2024 _cleanup_free_ char *buf = NULL;
2025
2026 (void) in_addr_prefix_to_string(address->family, &address->in_addr, address->prefixlen, &buf);
2027 log_warning("%s: Duplicated address %s is specified at line %u and %u, "
2028 "dropping the address setting specified at line %u.",
2029 dup->section->filename, strna(buf), address->section->line,
2030 dup->section->line, dup->section->line);
2031 /* address_free() will drop the address from addresses_by_section. */
2032 address_free(dup);
2033 }
2034
2035 /* Use address_kernel_hash_ops here. The function address_kernel_compare_func() matches
2036 * how kernel compares addresses, and is more lenient than address_compare_func().
2037 * Hence, the logic of dedup here is stricter than when address_hash_ops is used. */
2038 r = set_ensure_put(&addresses, &address_kernel_hash_ops, address);
2039 if (r < 0)
2040 return log_oom();
2041 assert(r > 0);
2042 }
2043
2044 return 0;
2045 }
2046