]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
f579559b TG |
2 | |
3 | #include <net/if.h> | |
baa3fadf DDM |
4 | #include <stdio.h> |
5 | ||
6 | #include "sd-event.h" | |
7 | #include "sd-netlink.h" | |
f579559b | 8 | |
1cbc8d91 | 9 | #include "af-list.h" |
b5efdb8a | 10 | #include "alloc-util.h" |
1e4e5572 | 11 | #include "bitfield.h" |
baa3fadf DDM |
12 | #include "conf-parser.h" |
13 | #include "errno-util.h" | |
12c2884c | 14 | #include "firewall-util.h" |
1cbc8d91 | 15 | #include "in-addr-prefix-util.h" |
3ae6b3bf | 16 | #include "logarithm.h" |
fc2f9534 | 17 | #include "netlink-util.h" |
baa3fadf | 18 | #include "networkd-address-generation.h" |
6bedfcbb | 19 | #include "networkd-address.h" |
1cf40697 | 20 | #include "networkd-address-pool.h" |
e14679ff | 21 | #include "networkd-dhcp-prefix-delegation.h" |
3b6a3bde | 22 | #include "networkd-dhcp-server.h" |
76a86ffd | 23 | #include "networkd-ipv4acd.h" |
0ee78fc9 | 24 | #include "networkd-link.h" |
23f53b99 | 25 | #include "networkd-manager.h" |
e14679ff | 26 | #include "networkd-ndisc.h" |
4b3590c3 | 27 | #include "networkd-netlabel.h" |
093e3533 | 28 | #include "networkd-network.h" |
76c5a0f2 | 29 | #include "networkd-queue.h" |
3b6a3bde | 30 | #include "networkd-route.h" |
1cf40697 | 31 | #include "networkd-route-util.h" |
baa3fadf | 32 | #include "ordered-set.h" |
6bedfcbb | 33 | #include "parse-util.h" |
baa3fadf DDM |
34 | #include "set.h" |
35 | #include "siphash24.h" | |
36 | #include "socket-util.h" | |
07630cea | 37 | #include "string-util.h" |
51517f9e | 38 | #include "strv.h" |
f579559b | 39 | |
af7674f4 LP |
40 | #define ADDRESSES_PER_LINK_MAX 16384U |
41 | #define STATIC_ADDRESSES_PER_NETWORK_MAX 8192U | |
8c34b963 | 42 | |
b1179a5d YW |
43 | #define KNOWN_FLAGS \ |
44 | (IFA_F_SECONDARY | \ | |
45 | IFA_F_NODAD | \ | |
46 | IFA_F_OPTIMISTIC | \ | |
47 | IFA_F_DADFAILED | \ | |
48 | IFA_F_HOMEADDRESS | \ | |
49 | IFA_F_DEPRECATED | \ | |
50 | IFA_F_TENTATIVE | \ | |
51 | IFA_F_PERMANENT | \ | |
52 | IFA_F_MANAGETEMPADDR | \ | |
53 | IFA_F_NOPREFIXROUTE | \ | |
54 | IFA_F_MCAUTOJOIN | \ | |
55 | IFA_F_STABLE_PRIVACY) | |
56 | ||
57 | /* From net/ipv4/devinet.c */ | |
58 | #define IPV6ONLY_FLAGS \ | |
59 | (IFA_F_NODAD | \ | |
60 | IFA_F_OPTIMISTIC | \ | |
61 | IFA_F_DADFAILED | \ | |
62 | IFA_F_HOMEADDRESS | \ | |
63 | IFA_F_TENTATIVE | \ | |
64 | IFA_F_MANAGETEMPADDR | \ | |
65 | IFA_F_STABLE_PRIVACY) | |
66 | ||
67 | /* We do not control the following flags. */ | |
68 | #define UNMANAGED_FLAGS \ | |
69 | (IFA_F_SECONDARY | \ | |
70 | IFA_F_DADFAILED | \ | |
71 | IFA_F_DEPRECATED | \ | |
72 | IFA_F_TENTATIVE | \ | |
73 | IFA_F_PERMANENT | \ | |
74 | IFA_F_STABLE_PRIVACY) | |
75 | ||
42ceb474 | 76 | int address_flags_to_string_alloc(uint32_t flags, int family, char **ret) { |
a2362484 | 77 | _cleanup_free_ char *str = NULL; |
7396e014 ZJS |
78 | static const char* map[] = { |
79 | [LOG2U(IFA_F_SECONDARY)] = "secondary", /* This is also called "temporary" for ipv6. */ | |
80 | [LOG2U(IFA_F_NODAD)] = "nodad", | |
81 | [LOG2U(IFA_F_OPTIMISTIC)] = "optimistic", | |
82 | [LOG2U(IFA_F_DADFAILED)] = "dadfailed", | |
83 | [LOG2U(IFA_F_HOMEADDRESS)] = "home-address", | |
84 | [LOG2U(IFA_F_DEPRECATED)] = "deprecated", | |
85 | [LOG2U(IFA_F_TENTATIVE)] = "tentative", | |
86 | [LOG2U(IFA_F_PERMANENT)] = "permanent", | |
87 | [LOG2U(IFA_F_MANAGETEMPADDR)] = "manage-temporary-address", | |
88 | [LOG2U(IFA_F_NOPREFIXROUTE)] = "no-prefixroute", | |
89 | [LOG2U(IFA_F_MCAUTOJOIN)] = "auto-join", | |
90 | [LOG2U(IFA_F_STABLE_PRIVACY)] = "stable-privacy", | |
a2362484 YW |
91 | }; |
92 | ||
93 | assert(IN_SET(family, AF_INET, AF_INET6)); | |
94 | assert(ret); | |
95 | ||
0828a386 | 96 | for (size_t i = 0; i < ELEMENTSOF(map); i++) |
1e4e5572 | 97 | if (BIT_SET(flags, i) && map[i]) |
7396e014 ZJS |
98 | if (!strextend_with_separator( |
99 | &str, ",", | |
100 | family == AF_INET6 && (1 << i) == IFA_F_SECONDARY ? "temporary" : map[i])) | |
101 | return -ENOMEM; | |
a2362484 YW |
102 | |
103 | *ret = TAKE_PTR(str); | |
104 | return 0; | |
105 | } | |
106 | ||
60415c13 YW |
107 | static LinkAddressState address_state_from_scope(uint8_t scope) { |
108 | if (scope < RT_SCOPE_SITE) | |
109 | /* universally accessible addresses found */ | |
110 | return LINK_ADDRESS_STATE_ROUTABLE; | |
111 | ||
112 | if (scope < RT_SCOPE_HOST) | |
113 | /* only link or site local addresses found */ | |
114 | return LINK_ADDRESS_STATE_DEGRADED; | |
115 | ||
116 | /* no useful addresses found */ | |
117 | return LINK_ADDRESS_STATE_OFF; | |
118 | } | |
119 | ||
120 | void link_get_address_states( | |
121 | Link *link, | |
122 | LinkAddressState *ret_ipv4, | |
123 | LinkAddressState *ret_ipv6, | |
124 | LinkAddressState *ret_all) { | |
125 | ||
126 | uint8_t ipv4_scope = RT_SCOPE_NOWHERE, ipv6_scope = RT_SCOPE_NOWHERE; | |
127 | Address *address; | |
128 | ||
129 | assert(link); | |
130 | ||
131 | SET_FOREACH(address, link->addresses) { | |
132 | if (!address_is_ready(address)) | |
133 | continue; | |
134 | ||
135 | if (address->family == AF_INET) | |
136 | ipv4_scope = MIN(ipv4_scope, address->scope); | |
137 | ||
138 | if (address->family == AF_INET6) | |
139 | ipv6_scope = MIN(ipv6_scope, address->scope); | |
140 | } | |
141 | ||
142 | if (ret_ipv4) | |
143 | *ret_ipv4 = address_state_from_scope(ipv4_scope); | |
144 | if (ret_ipv6) | |
145 | *ret_ipv6 = address_state_from_scope(ipv6_scope); | |
146 | if (ret_all) | |
147 | *ret_all = address_state_from_scope(MIN(ipv4_scope, ipv6_scope)); | |
148 | } | |
149 | ||
ebd96906 YW |
150 | static void address_detach(Address *address); |
151 | ||
152 | DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR( | |
153 | address_hash_ops_detach, | |
154 | Address, | |
155 | address_hash_func, | |
156 | address_compare_func, | |
157 | address_detach); | |
158 | ||
159 | DEFINE_HASH_OPS( | |
160 | address_hash_ops, | |
161 | Address, | |
162 | address_hash_func, | |
163 | address_compare_func); | |
164 | ||
165 | DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR( | |
166 | address_section_hash_ops, | |
167 | ConfigSection, | |
168 | config_section_hash_func, | |
169 | config_section_compare_func, | |
170 | Address, | |
171 | address_detach); | |
172 | ||
f0213e37 | 173 | int address_new(Address **ret) { |
ebd96906 | 174 | _cleanup_(address_unrefp) Address *address = NULL; |
f0213e37 | 175 | |
17f9c355 | 176 | address = new(Address, 1); |
f0213e37 TG |
177 | if (!address) |
178 | return -ENOMEM; | |
aba496a5 | 179 | |
17f9c355 | 180 | *address = (Address) { |
ebd96906 | 181 | .n_ref = 1, |
17f9c355 YW |
182 | .family = AF_UNSPEC, |
183 | .scope = RT_SCOPE_UNIVERSE, | |
16bc8635 YW |
184 | .lifetime_valid_usec = USEC_INFINITY, |
185 | .lifetime_preferred_usec = USEC_INFINITY, | |
832583ad | 186 | .set_broadcast = -1, |
17f9c355 | 187 | }; |
f0213e37 | 188 | |
1cc6c93a | 189 | *ret = TAKE_PTR(address); |
f0213e37 TG |
190 | |
191 | return 0; | |
aba496a5 UTL |
192 | } |
193 | ||
5459e11d | 194 | int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) { |
307fe3cd | 195 | _cleanup_(config_section_freep) ConfigSection *n = NULL; |
ebd96906 | 196 | _cleanup_(address_unrefp) Address *address = NULL; |
f0213e37 | 197 | int r; |
f579559b | 198 | |
8c34b963 LP |
199 | assert(network); |
200 | assert(ret); | |
9cd9fc8f YW |
201 | assert(filename); |
202 | assert(section_line > 0); | |
f4859fc7 | 203 | |
307fe3cd | 204 | r = config_section_new(filename, section_line, &n); |
9cd9fc8f YW |
205 | if (r < 0) |
206 | return r; | |
6ae115c1 | 207 | |
9cd9fc8f YW |
208 | address = ordered_hashmap_get(network->addresses_by_section, n); |
209 | if (address) { | |
210 | *ret = TAKE_PTR(address); | |
211 | return 0; | |
6ae115c1 TG |
212 | } |
213 | ||
9cd9fc8f | 214 | if (ordered_hashmap_size(network->addresses_by_section) >= STATIC_ADDRESSES_PER_NETWORK_MAX) |
8c34b963 LP |
215 | return -E2BIG; |
216 | ||
f0213e37 TG |
217 | r = address_new(&address); |
218 | if (r < 0) | |
219 | return r; | |
801bd9e8 | 220 | |
0f7f2769 | 221 | address->network = network; |
9cd9fc8f | 222 | address->section = TAKE_PTR(n); |
3b6a3bde | 223 | address->source = NETWORK_CONFIG_SOURCE_STATIC; |
2859932b YW |
224 | /* This will be adjusted in address_section_verify(). */ |
225 | address->duplicate_address_detection = _ADDRESS_FAMILY_INVALID; | |
0f7f2769 | 226 | |
ebd96906 | 227 | r = ordered_hashmap_ensure_put(&network->addresses_by_section, &address_section_hash_ops, address->section, address); |
9cd9fc8f YW |
228 | if (r < 0) |
229 | return r; | |
6ae115c1 | 230 | |
1cc6c93a | 231 | *ret = TAKE_PTR(address); |
f579559b TG |
232 | return 0; |
233 | } | |
234 | ||
ebd96906 YW |
235 | static Address* address_detach_impl(Address *address) { |
236 | assert(address); | |
237 | assert(!address->link || !address->network); | |
f579559b | 238 | |
f048a16b | 239 | if (address->network) { |
9cd9fc8f YW |
240 | assert(address->section); |
241 | ordered_hashmap_remove(address->network->addresses_by_section, address->section); | |
8ae3a4f5 YW |
242 | |
243 | if (address->network->dhcp_server_address == address) | |
244 | address->network->dhcp_server_address = NULL; | |
ebd96906 YW |
245 | |
246 | address->network = NULL; | |
247 | return address; | |
f048a16b | 248 | } |
6ae115c1 | 249 | |
ebd96906 | 250 | if (address->link) { |
cf1d700d | 251 | set_remove(address->link->addresses, address); |
f150100a | 252 | |
ebd96906 YW |
253 | address->link = NULL; |
254 | return address; | |
255 | } | |
256 | ||
257 | return NULL; | |
258 | } | |
259 | ||
260 | static void address_detach(Address *address) { | |
261 | assert(address); | |
262 | ||
263 | address_unref(address_detach_impl(address)); | |
264 | } | |
265 | ||
266 | static Address* address_free(Address *address) { | |
267 | if (!address) | |
268 | return NULL; | |
269 | ||
270 | address_detach_impl(address); | |
271 | ||
307fe3cd | 272 | config_section_free(address->section); |
de4224aa | 273 | free(address->label); |
4b3590c3 | 274 | free(address->netlabel); |
e39bb291 | 275 | ipv6_token_unref(address->token); |
fc289dd0 | 276 | nft_set_context_clear(&address->nft_set_context); |
7818f858 | 277 | return mfree(address); |
f579559b TG |
278 | } |
279 | ||
ebd96906 YW |
280 | DEFINE_TRIVIAL_REF_UNREF_FUNC(Address, address, address_free); |
281 | ||
dc32de39 YW |
282 | static bool address_lifetime_is_valid(const Address *a) { |
283 | assert(a); | |
284 | ||
285 | return | |
286 | a->lifetime_valid_usec == USEC_INFINITY || | |
287 | a->lifetime_valid_usec > now(CLOCK_BOOTTIME); | |
288 | } | |
289 | ||
3b6a3bde YW |
290 | bool address_is_ready(const Address *a) { |
291 | assert(a); | |
5385e5f9 | 292 | assert(a->link); |
3b6a3bde | 293 | |
5385e5f9 | 294 | if (!ipv4acd_bound(a->link, a)) |
3b6a3bde YW |
295 | return false; |
296 | ||
e402e99e | 297 | if (FLAGS_SET(a->flags, IFA_F_TENTATIVE)) |
3b6a3bde YW |
298 | return false; |
299 | ||
e402e99e | 300 | if (FLAGS_SET(a->state, NETWORK_CONFIG_STATE_REMOVING)) |
3b6a3bde YW |
301 | return false; |
302 | ||
303 | if (!FLAGS_SET(a->state, NETWORK_CONFIG_STATE_CONFIGURED)) | |
304 | return false; | |
305 | ||
dc32de39 | 306 | return address_lifetime_is_valid(a); |
3b6a3bde YW |
307 | } |
308 | ||
5f950e56 YW |
309 | bool link_check_addresses_ready(Link *link, NetworkConfigSource source) { |
310 | Address *a; | |
311 | bool has = false; | |
312 | ||
313 | assert(link); | |
314 | ||
315 | /* Check if all addresses on the interface are ready. If there is no address, this will return false. */ | |
316 | ||
317 | SET_FOREACH(a, link->addresses) { | |
318 | if (source >= 0 && a->source != source) | |
319 | continue; | |
320 | if (address_is_marked(a)) | |
321 | continue; | |
322 | if (!address_exists(a)) | |
323 | continue; | |
324 | if (!address_is_ready(a)) | |
325 | return false; | |
326 | has = true; | |
327 | } | |
328 | ||
4c4fe8cd YW |
329 | if (has || source != NETWORK_CONFIG_SOURCE_DHCP6) |
330 | return has; | |
331 | ||
332 | /* If there is no DHCPv6 addresses, but all conflicting addresses are successfully configured, then | |
333 | * let's handle the DHCPv6 client successfully configured addresses. Otherwise, if the conflicted | |
334 | * addresses are static or foreign, and there is no other dynamic addressing protocol enabled, then | |
335 | * the link will never enter the configured state. See also link_check_ready(). */ | |
336 | SET_FOREACH(a, link->addresses) { | |
337 | if (source >= 0 && a->source == NETWORK_CONFIG_SOURCE_DHCP6) | |
338 | continue; | |
339 | if (!a->also_requested_by_dhcp6) | |
340 | continue; | |
341 | if (address_is_marked(a)) | |
342 | continue; | |
343 | if (!address_exists(a)) | |
344 | continue; | |
345 | if (!address_is_ready(a)) | |
346 | return false; | |
347 | has = true; | |
348 | } | |
349 | ||
5f950e56 YW |
350 | return has; |
351 | } | |
352 | ||
2ccada8d | 353 | void link_mark_addresses(Link *link, NetworkConfigSource source) { |
3b6a3bde YW |
354 | Address *a; |
355 | ||
356 | assert(link); | |
357 | ||
358 | SET_FOREACH(a, link->addresses) { | |
359 | if (a->source != source) | |
360 | continue; | |
361 | ||
3b6a3bde YW |
362 | address_mark(a); |
363 | } | |
364 | } | |
365 | ||
0ae8ff50 | 366 | static int address_get_broadcast(const Address *a, Link *link, struct in_addr *ret) { |
1a1f4a80 YW |
367 | struct in_addr b_addr = {}; |
368 | ||
2a236f9f | 369 | assert(a); |
e680486d | 370 | assert(link); |
2a236f9f | 371 | |
1a1f4a80 YW |
372 | /* Returns 0 when broadcast address is null, 1 when non-null broadcast address, -EAGAIN when the main |
373 | * address is null. */ | |
374 | ||
375 | /* broadcast is only for IPv4. */ | |
473680be | 376 | if (a->family != AF_INET) |
1a1f4a80 | 377 | goto finalize; |
473680be | 378 | |
1a1f4a80 | 379 | /* broadcast address cannot be used when peer address is specified. */ |
473680be | 380 | if (in4_addr_is_set(&a->in_addr_peer.in)) |
1a1f4a80 | 381 | goto finalize; |
473680be | 382 | |
2a236f9f YW |
383 | /* A /31 or /32 IPv4 address does not have a broadcast address. |
384 | * See https://tools.ietf.org/html/rfc3021 */ | |
473680be | 385 | if (a->prefixlen > 30) |
1a1f4a80 | 386 | goto finalize; |
473680be | 387 | |
1a1f4a80 YW |
388 | /* If explicitly configured, use the address as is. */ |
389 | if (in4_addr_is_set(&a->broadcast)) { | |
390 | b_addr = a->broadcast; | |
391 | goto finalize; | |
392 | } | |
473680be | 393 | |
1a1f4a80 YW |
394 | /* If explicitly disabled, then return null address. */ |
395 | if (a->set_broadcast == 0) | |
396 | goto finalize; | |
473680be | 397 | |
1a1f4a80 YW |
398 | /* For wireguard interfaces, broadcast is disabled by default. */ |
399 | if (a->set_broadcast < 0 && streq_ptr(link->kind, "wireguard")) | |
400 | goto finalize; | |
2a236f9f | 401 | |
1a1f4a80 YW |
402 | /* If the main address is null, e.g. Address=0.0.0.0/24, the broadcast address will be automatically |
403 | * determined after an address is acquired. */ | |
404 | if (!in4_addr_is_set(&a->in_addr.in)) | |
405 | return -EAGAIN; | |
406 | ||
407 | /* Otherwise, generate a broadcast address from the main address and prefix length. */ | |
408 | b_addr.s_addr = a->in_addr.in.s_addr | htobe32(UINT32_C(0xffffffff) >> a->prefixlen); | |
832583ad | 409 | |
1a1f4a80 YW |
410 | finalize: |
411 | if (ret) | |
412 | *ret = b_addr; | |
832583ad | 413 | |
1a1f4a80 | 414 | return in4_addr_is_set(&b_addr); |
832583ad YW |
415 | } |
416 | ||
0ae8ff50 YW |
417 | static void address_set_broadcast(Address *a, Link *link) { |
418 | assert(a); | |
419 | assert_se(address_get_broadcast(a, link, &a->broadcast) >= 0); | |
420 | } | |
421 | ||
b9e4ec50 | 422 | static void address_set_cinfo(Manager *m, const Address *a, struct ifa_cacheinfo *cinfo) { |
16bc8635 YW |
423 | usec_t now_usec; |
424 | ||
def4741b | 425 | assert(m); |
16bc8635 YW |
426 | assert(a); |
427 | assert(cinfo); | |
428 | ||
def4741b | 429 | assert_se(sd_event_now(m->event, CLOCK_BOOTTIME, &now_usec) >= 0); |
16bc8635 YW |
430 | |
431 | *cinfo = (struct ifa_cacheinfo) { | |
5235d739 YW |
432 | .ifa_valid = usec_to_sec(a->lifetime_valid_usec, now_usec), |
433 | .ifa_prefered = usec_to_sec(a->lifetime_preferred_usec, now_usec), | |
16bc8635 | 434 | }; |
16bc8635 YW |
435 | } |
436 | ||
def4741b | 437 | static void address_set_lifetime(Manager *m, Address *a, const struct ifa_cacheinfo *cinfo) { |
16bc8635 YW |
438 | usec_t now_usec; |
439 | ||
def4741b | 440 | assert(m); |
16bc8635 YW |
441 | assert(a); |
442 | assert(cinfo); | |
443 | ||
def4741b | 444 | assert_se(sd_event_now(m->event, CLOCK_BOOTTIME, &now_usec) >= 0); |
16bc8635 | 445 | |
5235d739 YW |
446 | a->lifetime_valid_usec = sec_to_usec(cinfo->ifa_valid, now_usec); |
447 | a->lifetime_preferred_usec = sec_to_usec(cinfo->ifa_prefered, now_usec); | |
16bc8635 YW |
448 | } |
449 | ||
d0009d92 YW |
450 | static bool address_is_static_null(const Address *address) { |
451 | assert(address); | |
452 | ||
453 | if (!address->network) | |
454 | return false; | |
455 | ||
456 | if (!address->requested_as_null) | |
457 | return false; | |
458 | ||
459 | assert(!in_addr_is_set(address->family, &address->in_addr)); | |
460 | return true; | |
461 | } | |
462 | ||
381e3cc6 YW |
463 | static int address_ipv4_prefix(const Address *a, struct in_addr *ret) { |
464 | struct in_addr p; | |
465 | int r; | |
466 | ||
1d30fc5c | 467 | assert(a); |
381e3cc6 YW |
468 | assert(a->family == AF_INET); |
469 | assert(ret); | |
1d30fc5c | 470 | |
381e3cc6 YW |
471 | p = in4_addr_is_set(&a->in_addr_peer.in) ? a->in_addr_peer.in : a->in_addr.in; |
472 | r = in4_addr_mask(&p, a->prefixlen); | |
473 | if (r < 0) | |
474 | return r; | |
1d30fc5c | 475 | |
381e3cc6 YW |
476 | *ret = p; |
477 | return 0; | |
1d30fc5c YW |
478 | } |
479 | ||
d9ef1cc4 | 480 | void address_hash_func(const Address *a, struct siphash *state) { |
3ac8e543 TG |
481 | assert(a); |
482 | ||
c01a5c05 | 483 | siphash24_compress_typesafe(a->family, state); |
3ac8e543 | 484 | |
1d30fc5c | 485 | switch (a->family) { |
381e3cc6 YW |
486 | case AF_INET: { |
487 | struct in_addr prefix; | |
488 | ||
c01a5c05 | 489 | siphash24_compress_typesafe(a->prefixlen, state); |
3ac8e543 | 490 | |
381e3cc6 | 491 | assert_se(address_ipv4_prefix(a, &prefix) >= 0); |
c01a5c05 | 492 | siphash24_compress_typesafe(prefix, state); |
3ac8e543 | 493 | |
c01a5c05 | 494 | siphash24_compress_typesafe(a->in_addr.in, state); |
52af8e5e | 495 | break; |
381e3cc6 | 496 | } |
1d30fc5c | 497 | case AF_INET6: |
c01a5c05 | 498 | siphash24_compress_typesafe(a->in_addr.in6, state); |
6174dc7d YW |
499 | |
500 | if (in6_addr_is_null(&a->in_addr.in6)) | |
c01a5c05 | 501 | siphash24_compress_typesafe(a->prefixlen, state); |
1d30fc5c | 502 | break; |
52af8e5e | 503 | |
1d30fc5c | 504 | default: |
5c9feb2d | 505 | assert_not_reached(); |
1d30fc5c | 506 | } |
3ac8e543 TG |
507 | } |
508 | ||
d9ef1cc4 | 509 | int address_compare_func(const Address *a1, const Address *a2) { |
a0edd02e | 510 | int r; |
3ac8e543 | 511 | |
a0edd02e FB |
512 | r = CMP(a1->family, a2->family); |
513 | if (r != 0) | |
514 | return r; | |
3ac8e543 | 515 | |
1d30fc5c | 516 | switch (a1->family) { |
381e3cc6 YW |
517 | case AF_INET: { |
518 | struct in_addr p1, p2; | |
519 | ||
1d30fc5c YW |
520 | /* See kernel's find_matching_ifa() in net/ipv4/devinet.c */ |
521 | r = CMP(a1->prefixlen, a2->prefixlen); | |
24be9181 ZJS |
522 | if (r != 0) |
523 | return r; | |
3ac8e543 | 524 | |
381e3cc6 YW |
525 | assert_se(address_ipv4_prefix(a1, &p1) >= 0); |
526 | assert_se(address_ipv4_prefix(a2, &p2) >= 0); | |
527 | r = memcmp(&p1, &p2, sizeof(p1)); | |
1d30fc5c YW |
528 | if (r != 0) |
529 | return r; | |
2a236f9f | 530 | |
52af8e5e | 531 | return memcmp(&a1->in_addr.in, &a2->in_addr.in, sizeof(a1->in_addr.in)); |
381e3cc6 | 532 | } |
1d30fc5c YW |
533 | case AF_INET6: |
534 | /* See kernel's ipv6_get_ifaddr() in net/ipv6/addrconf.c */ | |
6174dc7d YW |
535 | r = memcmp(&a1->in_addr.in6, &a2->in_addr.in6, sizeof(a1->in_addr.in6)); |
536 | if (r != 0) | |
537 | return r; | |
538 | ||
539 | /* To distinguish IPv6 null addresses with different prefixlen, e.g. ::48 vs ::64, let's | |
540 | * compare the prefix length. */ | |
541 | if (in6_addr_is_null(&a1->in_addr.in6)) | |
542 | r = CMP(a1->prefixlen, a2->prefixlen); | |
543 | ||
544 | return r; | |
52af8e5e | 545 | |
1d30fc5c | 546 | default: |
5c9feb2d | 547 | assert_not_reached(); |
1d30fc5c | 548 | } |
3ac8e543 TG |
549 | } |
550 | ||
77de62f9 | 551 | bool address_can_update(const Address *existing, const Address *requesting) { |
a45e1c35 YW |
552 | assert(existing); |
553 | assert(requesting); | |
b5aefc90 YW |
554 | |
555 | /* | |
556 | * property | IPv4 | IPv6 | |
557 | * ----------------------------------------- | |
558 | * family | ✗ | ✗ | |
559 | * prefixlen | ✗ | ✗ | |
560 | * address | ✗ | ✗ | |
561 | * scope | ✗ | - | |
562 | * label | ✗ | - | |
563 | * broadcast | ✗ | - | |
564 | * peer | ✗ | ✓ | |
565 | * flags | ✗ | ✓ | |
566 | * lifetime | ✓ | ✓ | |
567 | * route metric | ✓ | ✓ | |
568 | * protocol | ✓ | ✓ | |
569 | * | |
570 | * ✗ : cannot be changed | |
571 | * ✓ : can be changed | |
572 | * - : unused | |
573 | * | |
574 | * IPv4 : See inet_rtm_newaddr() in net/ipv4/devinet.c. | |
575 | * IPv6 : See inet6_addr_modify() in net/ipv6/addrconf.c. | |
576 | */ | |
577 | ||
a45e1c35 | 578 | if (existing->family != requesting->family) |
b5aefc90 YW |
579 | return false; |
580 | ||
a45e1c35 | 581 | if (existing->prefixlen != requesting->prefixlen) |
b5aefc90 YW |
582 | return false; |
583 | ||
584 | /* When a null address is requested, the address to be assigned/updated will be determined later. */ | |
a45e1c35 YW |
585 | if (!address_is_static_null(requesting) && |
586 | in_addr_equal(existing->family, &existing->in_addr, &requesting->in_addr) <= 0) | |
b5aefc90 YW |
587 | return false; |
588 | ||
a45e1c35 | 589 | switch (existing->family) { |
b5aefc90 YW |
590 | case AF_INET: { |
591 | struct in_addr bcast; | |
592 | ||
a45e1c35 | 593 | if (existing->scope != requesting->scope) |
b5aefc90 | 594 | return false; |
a45e1c35 | 595 | if (((existing->flags ^ requesting->flags) & KNOWN_FLAGS & ~IPV6ONLY_FLAGS & ~UNMANAGED_FLAGS) != 0) |
b5aefc90 | 596 | return false; |
a45e1c35 | 597 | if (!streq_ptr(existing->label, requesting->label)) |
b5aefc90 | 598 | return false; |
a45e1c35 | 599 | if (!in4_addr_equal(&existing->in_addr_peer.in, &requesting->in_addr_peer.in)) |
b5aefc90 | 600 | return false; |
a45e1c35 | 601 | if (existing->link && address_get_broadcast(requesting, existing->link, &bcast) >= 0) { |
b5aefc90 | 602 | /* If the broadcast address can be determined now, check if they match. */ |
a45e1c35 | 603 | if (!in4_addr_equal(&existing->broadcast, &bcast)) |
b5aefc90 YW |
604 | return false; |
605 | } else { | |
606 | /* When a null address is requested, then the broadcast address will be | |
607 | * automatically calculated from the acquired address, e.g. | |
608 | * 192.168.0.10/24 -> 192.168.0.255 | |
609 | * So, here let's only check if the broadcast is the last address in the range, e.g. | |
610 | * 0.0.0.0/24 -> 0.0.0.255 */ | |
a45e1c35 | 611 | if (!FLAGS_SET(existing->broadcast.s_addr, htobe32(UINT32_C(0xffffffff) >> existing->prefixlen))) |
b5aefc90 YW |
612 | return false; |
613 | } | |
614 | break; | |
615 | } | |
616 | case AF_INET6: | |
617 | break; | |
618 | ||
619 | default: | |
620 | assert_not_reached(); | |
621 | } | |
622 | ||
623 | return true; | |
624 | } | |
625 | ||
99b46669 | 626 | int address_dup(const Address *src, Address **ret) { |
ebd96906 | 627 | _cleanup_(address_unrefp) Address *dest = NULL; |
cde1f0e8 YW |
628 | int r; |
629 | ||
cde1f0e8 | 630 | assert(src); |
99b46669 YW |
631 | assert(ret); |
632 | ||
633 | dest = newdup(Address, src, 1); | |
634 | if (!dest) | |
635 | return -ENOMEM; | |
636 | ||
ebd96906 YW |
637 | /* clear the reference counter and all pointers */ |
638 | dest->n_ref = 1; | |
99b46669 YW |
639 | dest->network = NULL; |
640 | dest->section = NULL; | |
641 | dest->link = NULL; | |
642 | dest->label = NULL; | |
e39bb291 | 643 | dest->token = ipv6_token_ref(src->token); |
4b3590c3 | 644 | dest->netlabel = NULL; |
fc289dd0 TM |
645 | dest->nft_set_context.sets = NULL; |
646 | dest->nft_set_context.n_sets = 0; | |
cde1f0e8 | 647 | |
2a236f9f | 648 | if (src->family == AF_INET) { |
6a705f12 | 649 | r = strdup_to(&dest->label, src->label); |
2a236f9f YW |
650 | if (r < 0) |
651 | return r; | |
652 | } | |
cde1f0e8 | 653 | |
6a705f12 | 654 | r = strdup_to(&dest->netlabel, src->netlabel); |
4b3590c3 TM |
655 | if (r < 0) |
656 | return r; | |
657 | ||
fc289dd0 TM |
658 | r = nft_set_context_dup(&src->nft_set_context, &dest->nft_set_context); |
659 | if (r < 0) | |
660 | return r; | |
661 | ||
99b46669 | 662 | *ret = TAKE_PTR(dest); |
cde1f0e8 YW |
663 | return 0; |
664 | } | |
665 | ||
494b6b43 YW |
666 | static int address_set_masquerade(Address *address, bool add) { |
667 | union in_addr_union masked; | |
91b5f997 TG |
668 | int r; |
669 | ||
670 | assert(address); | |
494b6b43 | 671 | assert(address->link); |
91b5f997 | 672 | |
494b6b43 YW |
673 | if (!address->link->network) |
674 | return 0; | |
91b5f997 | 675 | |
59577d24 | 676 | if (!FLAGS_SET(address->link->network->ip_masquerade, AF_TO_ADDRESS_FAMILY(address->family))) |
494b6b43 | 677 | return 0; |
91b5f997 | 678 | |
494b6b43 YW |
679 | if (address->scope >= RT_SCOPE_LINK) |
680 | return 0; | |
681 | ||
da0e2bae | 682 | if (address->ip_masquerade_done == add) |
494b6b43 YW |
683 | return 0; |
684 | ||
685 | masked = address->in_addr; | |
686 | r = in_addr_mask(address->family, &masked, address->prefixlen); | |
687 | if (r < 0) | |
688 | return r; | |
689 | ||
48ed2766 | 690 | r = fw_add_masquerade(&address->link->manager->fw_ctx, add, address->family, &masked, address->prefixlen); |
494b6b43 YW |
691 | if (r < 0) |
692 | return r; | |
693 | ||
da0e2bae | 694 | address->ip_masquerade_done = add; |
91b5f997 TG |
695 | |
696 | return 0; | |
697 | } | |
698 | ||
fc289dd0 TM |
699 | static void address_modify_nft_set_context(Address *address, bool add, NFTSetContext *nft_set_context) { |
700 | int r; | |
701 | ||
702 | assert(address); | |
703 | assert(address->link); | |
704 | assert(address->link->manager); | |
705 | assert(nft_set_context); | |
706 | ||
707 | if (!address->link->manager->fw_ctx) { | |
58c6e75f | 708 | r = fw_ctx_new_full(&address->link->manager->fw_ctx, /* init_tables= */ false); |
fc289dd0 TM |
709 | if (r < 0) |
710 | return; | |
711 | } | |
712 | ||
713 | FOREACH_ARRAY(nft_set, nft_set_context->sets, nft_set_context->n_sets) { | |
714 | uint32_t ifindex; | |
715 | ||
716 | assert(nft_set); | |
717 | ||
718 | switch (nft_set->source) { | |
719 | case NFT_SET_SOURCE_ADDRESS: | |
720 | r = nft_set_element_modify_ip(address->link->manager->fw_ctx, add, nft_set->nfproto, address->family, nft_set->table, nft_set->set, | |
721 | &address->in_addr); | |
722 | break; | |
723 | case NFT_SET_SOURCE_PREFIX: | |
724 | r = nft_set_element_modify_iprange(address->link->manager->fw_ctx, add, nft_set->nfproto, address->family, nft_set->table, nft_set->set, | |
725 | &address->in_addr, address->prefixlen); | |
726 | break; | |
727 | case NFT_SET_SOURCE_IFINDEX: | |
728 | ifindex = address->link->ifindex; | |
729 | r = nft_set_element_modify_any(address->link->manager->fw_ctx, add, nft_set->nfproto, nft_set->table, nft_set->set, | |
730 | &ifindex, sizeof(ifindex)); | |
731 | break; | |
732 | default: | |
733 | assert_not_reached(); | |
734 | } | |
735 | ||
736 | if (r < 0) | |
13b8fb98 | 737 | log_warning_errno(r, "Failed to %s NFT set: family %s, table %s, set %s, IP address %s, ignoring: %m", |
4d9a30f1 | 738 | add ? "add" : "delete", |
fc289dd0 TM |
739 | nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set, |
740 | IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen)); | |
741 | else | |
742 | log_debug("%s NFT set: family %s, table %s, set %s, IP address %s", | |
743 | add ? "Added" : "Deleted", | |
744 | nfproto_to_string(nft_set->nfproto), nft_set->table, nft_set->set, | |
745 | IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen)); | |
746 | } | |
747 | } | |
748 | ||
749 | static void address_modify_nft_set(Address *address, bool add) { | |
750 | assert(address); | |
751 | assert(address->link); | |
752 | ||
753 | if (!IN_SET(address->family, AF_INET, AF_INET6)) | |
754 | return; | |
755 | ||
756 | if (!address->link->network) | |
757 | return; | |
758 | ||
759 | switch (address->source) { | |
760 | case NETWORK_CONFIG_SOURCE_DHCP4: | |
761 | return address_modify_nft_set_context(address, add, &address->link->network->dhcp_nft_set_context); | |
762 | case NETWORK_CONFIG_SOURCE_DHCP6: | |
763 | return address_modify_nft_set_context(address, add, &address->link->network->dhcp6_nft_set_context); | |
764 | case NETWORK_CONFIG_SOURCE_DHCP_PD: | |
765 | return address_modify_nft_set_context(address, add, &address->link->network->dhcp_pd_nft_set_context); | |
766 | case NETWORK_CONFIG_SOURCE_NDISC: | |
767 | return address_modify_nft_set_context(address, add, &address->link->network->ndisc_nft_set_context); | |
768 | case NETWORK_CONFIG_SOURCE_STATIC: | |
769 | return address_modify_nft_set_context(address, add, &address->nft_set_context); | |
770 | default: | |
771 | return; | |
772 | } | |
773 | } | |
774 | ||
ebd96906 | 775 | static int address_attach(Link *link, Address *address) { |
cf1d700d TG |
776 | int r; |
777 | ||
778 | assert(link); | |
3b6a3bde | 779 | assert(address); |
ebd96906 | 780 | assert(!address->link); |
cf1d700d | 781 | |
ebd96906 | 782 | r = set_ensure_put(&link->addresses, &address_hash_ops_detach, address); |
cf1d700d TG |
783 | if (r < 0) |
784 | return r; | |
75a302b5 YW |
785 | if (r == 0) |
786 | return -EEXIST; | |
cf1d700d TG |
787 | |
788 | address->link = link; | |
ebd96906 | 789 | address_ref(address); |
7b3a7581 | 790 | return 0; |
adda1ed9 TG |
791 | } |
792 | ||
f9831557 | 793 | static int address_update(Address *address) { |
4bbe559b | 794 | Link *link = ASSERT_PTR(ASSERT_PTR(address)->link); |
e7ab854c | 795 | int r; |
36c32f61 | 796 | |
7657ec3e YW |
797 | if (address_is_ready(address) && |
798 | address->family == AF_INET6 && | |
799 | in6_addr_is_link_local(&address->in_addr.in6) && | |
800 | in6_addr_is_null(&link->ipv6ll_address)) { | |
801 | ||
802 | link->ipv6ll_address = address->in_addr.in6; | |
803 | ||
804 | r = link_ipv6ll_gained(link); | |
805 | if (r < 0) | |
806 | return r; | |
807 | } | |
808 | ||
2c40a889 | 809 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
ea121d8f YW |
810 | return 0; |
811 | ||
4bbe559b | 812 | r = address_set_masquerade(address, /* add = */ true); |
7a8685ff YW |
813 | if (r < 0) |
814 | return log_link_warning_errno(link, r, "Could not enable IP masquerading: %m"); | |
815 | ||
4b3590c3 TM |
816 | address_add_netlabel(address); |
817 | ||
fc289dd0 TM |
818 | address_modify_nft_set(address, /* add = */ true); |
819 | ||
981fab5d YW |
820 | if (address_is_ready(address) && address->callback) { |
821 | r = address->callback(address); | |
822 | if (r < 0) | |
823 | return r; | |
a3a019e1 | 824 | } |
36c32f61 | 825 | |
50829577 | 826 | link_update_operstate(link, /* also_update_bond_master = */ true); |
981fab5d | 827 | link_check_ready(link); |
36c32f61 TG |
828 | return 0; |
829 | } | |
830 | ||
e14679ff YW |
831 | static int address_removed_maybe_kernel_dad(Link *link, Address *address) { |
832 | int r; | |
833 | ||
834 | assert(link); | |
835 | assert(address); | |
836 | ||
837 | if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) | |
838 | return 0; | |
839 | ||
840 | if (address->family != AF_INET6) | |
841 | return 0; | |
842 | ||
843 | if (!FLAGS_SET(address->flags, IFA_F_TENTATIVE)) | |
844 | return 0; | |
845 | ||
846 | log_link_info(link, "Address %s with tentative flag is removed, maybe a duplicated address is assigned on another node or link?", | |
847 | IN6_ADDR_TO_STRING(&address->in_addr.in6)); | |
848 | ||
849 | /* Reset the address state, as the object may be reused in the below. */ | |
850 | address->state = 0; | |
851 | ||
852 | switch (address->source) { | |
853 | case NETWORK_CONFIG_SOURCE_STATIC: | |
854 | r = link_reconfigure_radv_address(address, link); | |
855 | break; | |
856 | case NETWORK_CONFIG_SOURCE_DHCP_PD: | |
857 | r = dhcp_pd_reconfigure_address(address, link); | |
858 | break; | |
859 | case NETWORK_CONFIG_SOURCE_NDISC: | |
860 | r = ndisc_reconfigure_address(address, link); | |
861 | break; | |
862 | default: | |
863 | r = 0; | |
864 | } | |
865 | if (r < 0) | |
866 | return log_link_warning_errno(link, r, "Failed to configure an alternative address: %m"); | |
867 | ||
868 | return 0; | |
869 | } | |
870 | ||
871 | static int address_drop(Address *in, bool removed_by_us) { | |
872 | _cleanup_(address_unrefp) Address *address = address_ref(ASSERT_PTR(in)); | |
873 | Link *link = ASSERT_PTR(address->link); | |
7750b796 | 874 | int r; |
8012cd39 | 875 | |
4bbe559b | 876 | r = address_set_masquerade(address, /* add = */ false); |
7750b796 YW |
877 | if (r < 0) |
878 | log_link_warning_errno(link, r, "Failed to disable IP masquerading, ignoring: %m"); | |
879 | ||
fc289dd0 TM |
880 | address_modify_nft_set(address, /* add = */ false); |
881 | ||
4b3590c3 TM |
882 | address_del_netlabel(address); |
883 | ||
f089d3eb YW |
884 | /* FIXME: if the IPv6LL address is dropped, stop DHCPv6, NDISC, RADV. */ |
885 | if (address->family == AF_INET6 && | |
886 | in6_addr_equal(&address->in_addr.in6, &link->ipv6ll_address)) | |
887 | link->ipv6ll_address = (const struct in6_addr) {}; | |
888 | ||
889 | ipv4acd_detach(link, address); | |
890 | ||
ebd96906 | 891 | address_detach(address); |
91b5f997 | 892 | |
e14679ff YW |
893 | if (!removed_by_us) { |
894 | r = address_removed_maybe_kernel_dad(link, address); | |
895 | if (r < 0) { | |
896 | link_enter_failed(link); | |
897 | return r; | |
898 | } | |
899 | } | |
900 | ||
50829577 | 901 | link_update_operstate(link, /* also_update_bond_master = */ true); |
aaad20e0 | 902 | link_check_ready(link); |
91b5f997 TG |
903 | return 0; |
904 | } | |
905 | ||
a2532c9d YW |
906 | static bool address_match_null(const Address *a, const Address *null_address) { |
907 | assert(a); | |
908 | assert(null_address); | |
909 | ||
910 | if (!a->requested_as_null) | |
911 | return false; | |
912 | ||
913 | /* Currently, null address is supported only by static addresses. Note that static | |
914 | * address may be set as foreign during reconfiguring the interface. */ | |
915 | if (!IN_SET(a->source, NETWORK_CONFIG_SOURCE_FOREIGN, NETWORK_CONFIG_SOURCE_STATIC)) | |
916 | return false; | |
917 | ||
918 | if (a->family != null_address->family) | |
919 | return false; | |
920 | ||
921 | if (a->prefixlen != null_address->prefixlen) | |
922 | return false; | |
923 | ||
924 | return true; | |
925 | } | |
926 | ||
0a0c2672 YW |
927 | static int address_get_request(Link *link, const Address *address, Request **ret) { |
928 | Request *req; | |
929 | ||
930 | assert(link); | |
931 | assert(link->manager); | |
932 | assert(address); | |
933 | ||
934 | req = ordered_set_get( | |
935 | link->manager->request_queue, | |
936 | &(Request) { | |
937 | .link = link, | |
938 | .type = REQUEST_TYPE_ADDRESS, | |
939 | .userdata = (void*) address, | |
4da252c5 YW |
940 | .hash_func = (hash_func_t) address_hash_func, |
941 | .compare_func = (compare_func_t) address_compare_func, | |
0a0c2672 YW |
942 | }); |
943 | if (req) { | |
944 | if (ret) | |
945 | *ret = req; | |
946 | return 0; | |
947 | } | |
948 | ||
949 | if (address_is_static_null(address)) | |
950 | ORDERED_SET_FOREACH(req, link->manager->request_queue) { | |
951 | if (req->link != link) | |
952 | continue; | |
953 | if (req->type != REQUEST_TYPE_ADDRESS) | |
954 | continue; | |
955 | ||
956 | if (!address_match_null(req->userdata, address)) | |
957 | continue; | |
958 | ||
959 | if (ret) | |
960 | *ret = req; | |
961 | ||
962 | return 0; | |
963 | } | |
964 | ||
965 | return -ENOENT; | |
966 | } | |
967 | ||
fe841414 | 968 | int address_get(Link *link, const Address *in, Address **ret) { |
d0009d92 | 969 | Address *a; |
91b5f997 | 970 | |
5a8bcb67 | 971 | assert(link); |
fe841414 | 972 | assert(in); |
5a8bcb67 | 973 | |
d0009d92 YW |
974 | a = set_get(link->addresses, in); |
975 | if (a) { | |
976 | if (ret) | |
977 | *ret = a; | |
978 | return 0; | |
979 | } | |
980 | ||
981 | /* Find matching address that originally requested as null address. */ | |
982 | if (address_is_static_null(in)) | |
983 | SET_FOREACH(a, link->addresses) { | |
a2532c9d | 984 | if (!address_match_null(a, in)) |
d0009d92 YW |
985 | continue; |
986 | ||
987 | if (ret) | |
988 | *ret = a; | |
989 | return 0; | |
990 | } | |
991 | ||
992 | return -ENOENT; | |
5a8bcb67 LP |
993 | } |
994 | ||
b33dd04e YW |
995 | int address_get_harder(Link *link, const Address *in, Address **ret) { |
996 | Request *req; | |
997 | int r; | |
998 | ||
999 | assert(link); | |
1000 | assert(in); | |
1001 | ||
1002 | if (address_get(link, in, ret) >= 0) | |
1003 | return 0; | |
1004 | ||
1005 | r = address_get_request(link, in, &req); | |
1006 | if (r < 0) | |
1007 | return r; | |
1008 | ||
1009 | if (ret) | |
1010 | *ret = ASSERT_PTR(req->userdata); | |
1011 | ||
1012 | return 0; | |
1013 | } | |
1014 | ||
56f91e2d YW |
1015 | int link_get_address_full( |
1016 | Link *link, | |
1017 | int family, | |
1018 | const union in_addr_union *address, | |
1019 | const union in_addr_union *peer, /* optional, can be NULL */ | |
1020 | unsigned char prefixlen, /* optional, can be 0 */ | |
1021 | Address **ret) { | |
1022 | ||
390247d2 | 1023 | Address *a; |
c5a0aeb3 | 1024 | int r; |
5eec0a08 | 1025 | |
c5a0aeb3 | 1026 | assert(link); |
5d003031 | 1027 | assert(IN_SET(family, AF_INET, AF_INET6)); |
c5a0aeb3 | 1028 | assert(address); |
5eec0a08 | 1029 | |
56f91e2d YW |
1030 | /* This finds an Address object on the link which matches the given address, peer, and prefix length. |
1031 | * If the prefixlen is zero, then an Address object with an arbitrary prefixlen will be returned. | |
1032 | * If the peer is NULL, then an Address object with an arbitrary peer will be returned. */ | |
998545a7 | 1033 | |
56f91e2d | 1034 | if (family == AF_INET6 || (prefixlen != 0 && peer)) { |
ebd96906 | 1035 | _cleanup_(address_unrefp) Address *tmp = NULL; |
998545a7 | 1036 | |
42f8b6a8 | 1037 | /* In this case, we can use address_get(). */ |
998545a7 | 1038 | |
390247d2 | 1039 | r = address_new(&tmp); |
998545a7 YW |
1040 | if (r < 0) |
1041 | return r; | |
1042 | ||
390247d2 YW |
1043 | tmp->family = family; |
1044 | tmp->in_addr = *address; | |
56f91e2d YW |
1045 | if (peer) |
1046 | tmp->in_addr_peer = *peer; | |
390247d2 | 1047 | tmp->prefixlen = prefixlen; |
3b6a3bde | 1048 | |
42f8b6a8 YW |
1049 | r = address_get(link, tmp, &a); |
1050 | if (r < 0) | |
1051 | return r; | |
3b6a3bde | 1052 | |
42f8b6a8 YW |
1053 | if (family == AF_INET6) { |
1054 | /* IPv6 addresses are managed without peer address and prefix length. Hence, we need | |
1055 | * to check them explicitly. */ | |
56f91e2d | 1056 | if (peer && !in_addr_equal(family, &a->in_addr_peer, peer)) |
42f8b6a8 YW |
1057 | return -ENOENT; |
1058 | if (prefixlen != 0 && a->prefixlen != prefixlen) | |
1059 | return -ENOENT; | |
3b6a3bde YW |
1060 | } |
1061 | ||
42f8b6a8 YW |
1062 | if (ret) |
1063 | *ret = a; | |
390247d2 | 1064 | |
42f8b6a8 | 1065 | return 0; |
390247d2 YW |
1066 | } |
1067 | ||
1068 | SET_FOREACH(a, link->addresses) { | |
1069 | if (a->family != family) | |
1070 | continue; | |
1071 | ||
1072 | if (!in_addr_equal(family, &a->in_addr, address)) | |
1073 | continue; | |
1074 | ||
56f91e2d YW |
1075 | if (peer && !in_addr_equal(family, &a->in_addr_peer, peer)) |
1076 | continue; | |
1077 | ||
1078 | if (prefixlen != 0 && a->prefixlen != prefixlen) | |
390247d2 YW |
1079 | continue; |
1080 | ||
1081 | if (ret) | |
1082 | *ret = a; | |
1083 | ||
1084 | return 0; | |
3b6a3bde | 1085 | } |
390247d2 YW |
1086 | |
1087 | return -ENOENT; | |
998545a7 YW |
1088 | } |
1089 | ||
56f91e2d YW |
1090 | int manager_get_address_full( |
1091 | Manager *manager, | |
1092 | int family, | |
1093 | const union in_addr_union *address, | |
1094 | const union in_addr_union *peer, | |
1095 | unsigned char prefixlen, | |
1096 | Address **ret) { | |
1097 | ||
591bd5f3 | 1098 | Link *link; |
591bd5f3 YW |
1099 | |
1100 | assert(manager); | |
1101 | assert(IN_SET(family, AF_INET, AF_INET6)); | |
1102 | assert(address); | |
1103 | ||
77e9be1e YW |
1104 | HASHMAP_FOREACH(link, manager->links_by_index) { |
1105 | if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) | |
1106 | continue; | |
1107 | ||
56f91e2d | 1108 | if (link_get_address_full(link, family, address, peer, prefixlen, ret) >= 0) |
77e9be1e YW |
1109 | return 0; |
1110 | } | |
1111 | ||
1112 | return -ENOENT; | |
1113 | } | |
1114 | ||
16bc8635 | 1115 | const char* format_lifetime(char *buf, size_t l, usec_t lifetime_usec) { |
0fd97a25 | 1116 | assert(buf); |
a35f3128 | 1117 | assert(l > 4); |
0fd97a25 | 1118 | |
16bc8635 | 1119 | if (lifetime_usec == USEC_INFINITY) |
a35f3128 | 1120 | return "forever"; |
0fd97a25 | 1121 | |
a35f3128 ZJS |
1122 | sprintf(buf, "for "); |
1123 | /* format_timespan() never fails */ | |
ba4e0427 | 1124 | assert_se(format_timespan(buf + 4, l - 4, usec_sub_unsigned(lifetime_usec, now(CLOCK_BOOTTIME)), USEC_PER_SEC)); |
a35f3128 | 1125 | return buf; |
0fd97a25 YW |
1126 | } |
1127 | ||
2afd12e0 | 1128 | void log_address_debug(const Address *address, const char *str, const Link *link) { |
84dbb3fd | 1129 | _cleanup_free_ char *state = NULL, *flags_str = NULL, *scope_str = NULL; |
7653a9dc | 1130 | |
24f50382 YW |
1131 | assert(address); |
1132 | assert(str); | |
1133 | assert(link); | |
1134 | ||
7653a9dc YW |
1135 | if (!DEBUG_LOGGING) |
1136 | return; | |
1137 | ||
3b6a3bde | 1138 | (void) network_config_state_to_string_alloc(address->state, &state); |
84dbb3fd ZJS |
1139 | |
1140 | const char *peer = in_addr_is_set(address->family, &address->in_addr_peer) ? | |
1141 | IN_ADDR_TO_STRING(address->family, &address->in_addr_peer) : NULL; | |
7653a9dc | 1142 | |
14044a41 YW |
1143 | const char *broadcast = (address->family == AF_INET && in4_addr_is_set(&address->broadcast)) ? |
1144 | IN4_ADDR_TO_STRING(&address->broadcast) : NULL; | |
1145 | ||
a2362484 | 1146 | (void) address_flags_to_string_alloc(address->flags, address->family, &flags_str); |
78ef25a5 | 1147 | (void) route_scope_to_string_alloc(address->scope, &scope_str); |
a2362484 | 1148 | |
14044a41 | 1149 | log_link_debug(link, "%s %s address (%s): %s%s%s/%u%s%s (valid %s, preferred %s), flags: %s, scope: %s%s%s", |
3b6a3bde | 1150 | str, strna(network_config_source_to_string(address->source)), strna(state), |
84dbb3fd ZJS |
1151 | IN_ADDR_TO_STRING(address->family, &address->in_addr), |
1152 | peer ? " peer " : "", strempty(peer), address->prefixlen, | |
14044a41 | 1153 | broadcast ? " broadcast " : "", strempty(broadcast), |
16bc8635 YW |
1154 | FORMAT_LIFETIME(address->lifetime_valid_usec), |
1155 | FORMAT_LIFETIME(address->lifetime_preferred_usec), | |
14044a41 YW |
1156 | strna(flags_str), strna(scope_str), |
1157 | address->family == AF_INET ? ", label: " : "", | |
1158 | address->family == AF_INET ? strna(address->label) : ""); | |
24f50382 YW |
1159 | } |
1160 | ||
a4feabd8 YW |
1161 | static void address_forget(Link *link, Address *address, bool removed_by_us, const char *msg) { |
1162 | assert(link); | |
1163 | assert(address); | |
1164 | assert(msg); | |
1165 | ||
1166 | Request *req; | |
1167 | if (address_get_request(link, address, &req) >= 0) | |
1168 | address_enter_removed(req->userdata); | |
1169 | ||
1170 | if (!address->link && address_get(link, address, &address) < 0) | |
1171 | return; | |
1172 | ||
1173 | address_enter_removed(address); | |
1174 | log_address_debug(address, msg, link); | |
1175 | (void) address_drop(address, removed_by_us); | |
1176 | } | |
1177 | ||
a79a8d16 | 1178 | static int address_set_netlink_message(const Address *address, sd_netlink_message *m, Link *link) { |
a8481354 YW |
1179 | int r; |
1180 | ||
1181 | assert(address); | |
a79a8d16 | 1182 | assert(m); |
a8481354 YW |
1183 | assert(link); |
1184 | ||
a79a8d16 | 1185 | r = sd_rtnl_message_addr_set_prefixlen(m, address->prefixlen); |
a8481354 | 1186 | if (r < 0) |
a79a8d16 | 1187 | return r; |
a8481354 YW |
1188 | |
1189 | /* On remove, only IFA_F_MANAGETEMPADDR flag for IPv6 addresses are used. But anyway, set all | |
d5e3b3eb YW |
1190 | * flags except tentative flag here unconditionally. Without setting the flag, the template |
1191 | * addresses generated by kernel will not be removed automatically when the main address is | |
1192 | * removed. */ | |
29c2d4e1 YW |
1193 | uint32_t flags = address->flags & ~IFA_F_TENTATIVE; |
1194 | if (flags != 0) { | |
a79a8d16 | 1195 | r = sd_netlink_message_append_u32(m, IFA_FLAGS, flags); |
a8481354 | 1196 | if (r < 0) |
a79a8d16 | 1197 | return r; |
a8481354 YW |
1198 | } |
1199 | ||
a79a8d16 | 1200 | r = netlink_message_append_in_addr_union(m, IFA_LOCAL, address->family, &address->in_addr); |
a8481354 | 1201 | if (r < 0) |
a79a8d16 | 1202 | return r; |
a8481354 YW |
1203 | |
1204 | return 0; | |
1205 | } | |
1206 | ||
56a995fe | 1207 | static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, RemoveRequest *rreq) { |
5a07fa9d YW |
1208 | int r; |
1209 | ||
5a07fa9d | 1210 | assert(m); |
56a995fe | 1211 | assert(rreq); |
76c5a0f2 | 1212 | |
56a995fe YW |
1213 | Link *link = ASSERT_PTR(rreq->link); |
1214 | Address *address = ASSERT_PTR(rreq->userdata); | |
1215 | ||
1216 | if (link->state == LINK_STATE_LINGER) | |
5a07fa9d YW |
1217 | return 0; |
1218 | ||
1219 | r = sd_netlink_message_get_errno(m); | |
56a995fe YW |
1220 | if (r < 0) { |
1221 | log_link_message_full_errno(link, m, | |
1222 | (r == -EADDRNOTAVAIL || !address->link) ? LOG_DEBUG : LOG_WARNING, | |
1223 | r, "Could not drop address"); | |
1224 | ||
a4feabd8 YW |
1225 | /* If the address cannot be removed, then assume the address is already removed. */ |
1226 | address_forget(link, address, /* removed_by_us = */ true, "Forgetting"); | |
56a995fe | 1227 | } |
5a07fa9d YW |
1228 | |
1229 | return 1; | |
1230 | } | |
1231 | ||
7f74b00a | 1232 | int address_remove(Address *address, Link *link) { |
a79a8d16 | 1233 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
407fe036 TG |
1234 | int r; |
1235 | ||
1236 | assert(address); | |
4c701096 | 1237 | assert(IN_SET(address->family, AF_INET, AF_INET6)); |
7f74b00a YW |
1238 | assert(link); |
1239 | assert(link->ifindex > 0); | |
1240 | assert(link->manager); | |
1241 | assert(link->manager->rtnl); | |
407fe036 | 1242 | |
fe0acbf7 YW |
1243 | /* If the address is remembered, use the remembered object. */ |
1244 | (void) address_get(link, address, &address); | |
1245 | ||
24f50382 | 1246 | log_address_debug(address, "Removing", link); |
30226d27 | 1247 | |
a79a8d16 | 1248 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &m, RTM_DELADDR, |
151b9b96 | 1249 | link->ifindex, address->family); |
eb56eb9b | 1250 | if (r < 0) |
a79a8d16 | 1251 | return log_link_warning_errno(link, r, "Could not allocate RTM_DELADDR message: %m"); |
407fe036 | 1252 | |
a79a8d16 | 1253 | r = address_set_netlink_message(address, m, link); |
eb56eb9b | 1254 | if (r < 0) |
a79a8d16 | 1255 | return log_link_warning_errno(link, r, "Could not set netlink attributes: %m"); |
407fe036 | 1256 | |
56a995fe | 1257 | r = link_remove_request_add(link, address, address, link->manager->rtnl, m, address_remove_handler); |
eb56eb9b | 1258 | if (r < 0) |
56a995fe | 1259 | return log_link_warning_errno(link, r, "Could not queue rtnetlink message: %m"); |
563c69c6 | 1260 | |
3b6a3bde | 1261 | address_enter_removing(address); |
613d65d8 YW |
1262 | |
1263 | /* The operational state is determined by address state and carrier state. Hence, if we remove | |
1264 | * an address, the operational state may be changed. */ | |
1265 | link_update_operstate(link, true); | |
407fe036 TG |
1266 | return 0; |
1267 | } | |
1268 | ||
f22b586a | 1269 | int address_remove_and_cancel(Address *address, Link *link) { |
7036d472 | 1270 | _cleanup_(request_unrefp) Request *req = NULL; |
f22b586a | 1271 | bool waiting = false; |
d4b76314 | 1272 | |
f22b586a YW |
1273 | assert(address); |
1274 | assert(link); | |
1275 | assert(link->manager); | |
d4b76314 | 1276 | |
f22b586a YW |
1277 | /* If the address is remembered by the link, then use the remembered object. */ |
1278 | (void) address_get(link, address, &address); | |
d4b76314 | 1279 | |
f22b586a YW |
1280 | /* Cancel the request for the address. If the request is already called but we have not received the |
1281 | * notification about the request, then explicitly remove the address. */ | |
1282 | if (address_get_request(link, address, &req) >= 0) { | |
7036d472 | 1283 | request_ref(req); /* avoid the request freed by request_detach() */ |
f22b586a | 1284 | waiting = req->waiting_reply; |
b9b5096e | 1285 | request_detach(req); |
f22b586a YW |
1286 | address_cancel_requesting(address); |
1287 | } | |
1288 | ||
1289 | /* If we know the address will come or already exists, remove it. */ | |
1290 | if (waiting || (address->link && address_exists(address))) | |
1291 | return address_remove(address, link); | |
1292 | ||
1293 | return 0; | |
d4b76314 YW |
1294 | } |
1295 | ||
3def8850 | 1296 | bool link_address_is_dynamic(const Link *link, const Address *address) { |
f8f2f880 YW |
1297 | Route *route; |
1298 | ||
1299 | assert(link); | |
8d01e44c | 1300 | assert(link->manager); |
f8f2f880 YW |
1301 | assert(address); |
1302 | ||
16bc8635 | 1303 | if (address->lifetime_preferred_usec != USEC_INFINITY) |
f8f2f880 YW |
1304 | return true; |
1305 | ||
dc12457b YW |
1306 | /* There is no way to drtermine if the IPv6 address is dynamic when the lifetime is infinity. */ |
1307 | if (address->family != AF_INET) | |
1308 | return false; | |
1309 | ||
80a89d1a YW |
1310 | /* Even if an IPv4 address is leased from a DHCP server with a finite lifetime, networkd assign the |
1311 | * address without lifetime when KeepConfiguration=dynamic. So, let's check that we have | |
f8f2f880 | 1312 | * corresponding routes with RTPROT_DHCP. */ |
8d01e44c | 1313 | SET_FOREACH(route, link->manager->routes) { |
3b6a3bde YW |
1314 | if (route->source != NETWORK_CONFIG_SOURCE_FOREIGN) |
1315 | continue; | |
1316 | ||
dc12457b | 1317 | /* The route is not assigned yet, or already being removed. Ignoring. */ |
f974f17a YW |
1318 | if (!route_exists(route)) |
1319 | continue; | |
1320 | ||
f8f2f880 YW |
1321 | if (route->protocol != RTPROT_DHCP) |
1322 | continue; | |
1323 | ||
8d01e44c YW |
1324 | if (route->nexthop.ifindex != link->ifindex) |
1325 | continue; | |
1326 | ||
f8f2f880 YW |
1327 | if (address->family != route->family) |
1328 | continue; | |
1329 | ||
1330 | if (in_addr_equal(address->family, &address->in_addr, &route->prefsrc)) | |
1331 | return true; | |
1332 | } | |
1333 | ||
1334 | return false; | |
1335 | } | |
1336 | ||
b9bf3f22 | 1337 | int link_drop_ipv6ll_addresses(Link *link) { |
51f5dfd8 | 1338 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
51f5dfd8 YW |
1339 | int r; |
1340 | ||
1341 | assert(link); | |
1342 | assert(link->manager); | |
1343 | assert(link->manager->rtnl); | |
1344 | ||
b9bf3f22 YW |
1345 | /* IPv6LL address may be in the tentative state, and in that case networkd has not received it. |
1346 | * So, we need to dump all IPv6 addresses. */ | |
1347 | ||
bd7e0a3f | 1348 | if (link_may_have_ipv6ll(link, /* check_multicast = */ false)) |
b9bf3f22 YW |
1349 | return 0; |
1350 | ||
c31298be | 1351 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_GETADDR, link->ifindex, AF_INET6); |
51f5dfd8 YW |
1352 | if (r < 0) |
1353 | return r; | |
1354 | ||
24c0f385 | 1355 | r = sd_netlink_message_set_request_dump(req, true); |
f318f643 YW |
1356 | if (r < 0) |
1357 | return r; | |
1358 | ||
51f5dfd8 YW |
1359 | r = sd_netlink_call(link->manager->rtnl, req, 0, &reply); |
1360 | if (r < 0) | |
1361 | return r; | |
1362 | ||
cda7fc8d | 1363 | for (sd_netlink_message *addr = reply; addr; addr = sd_netlink_message_next(addr)) { |
ebd96906 | 1364 | _cleanup_(address_unrefp) Address *a = NULL; |
29c2d4e1 | 1365 | unsigned char prefixlen; |
b9bf3f22 | 1366 | struct in6_addr address; |
51f5dfd8 YW |
1367 | int ifindex; |
1368 | ||
eb381e65 YW |
1369 | /* We set ifindex in the request, and NETLINK_GET_STRICT_CHK socket option is set. Hence the |
1370 | * check below is redundant, but let's do that for safety. */ | |
51f5dfd8 YW |
1371 | r = sd_rtnl_message_addr_get_ifindex(addr, &ifindex); |
1372 | if (r < 0) { | |
b9bf3f22 | 1373 | log_link_debug_errno(link, r, "rtnl: received address message without valid ifindex, ignoring: %m"); |
51f5dfd8 YW |
1374 | continue; |
1375 | } else if (link->ifindex != ifindex) | |
1376 | continue; | |
1377 | ||
29c2d4e1 YW |
1378 | uint32_t flags; |
1379 | r = sd_netlink_message_read_u32(addr, IFA_FLAGS, &flags); | |
51f5dfd8 | 1380 | if (r < 0) { |
29c2d4e1 | 1381 | log_link_debug_errno(link, r, "rtnl: Failed to read IFA_FLAGS attribute, ignoring: %m"); |
b9bf3f22 YW |
1382 | continue; |
1383 | } | |
1384 | ||
1385 | r = sd_rtnl_message_addr_get_prefixlen(addr, &prefixlen); | |
1386 | if (r < 0) { | |
1387 | log_link_debug_errno(link, r, "rtnl: received address message without prefixlen, ignoring: %m"); | |
51f5dfd8 | 1388 | continue; |
b9bf3f22 YW |
1389 | } |
1390 | ||
1391 | if (sd_netlink_message_read_in6_addr(addr, IFA_LOCAL, NULL) >= 0) | |
1392 | /* address with peer, ignoring. */ | |
51f5dfd8 YW |
1393 | continue; |
1394 | ||
b9bf3f22 YW |
1395 | r = sd_netlink_message_read_in6_addr(addr, IFA_ADDRESS, &address); |
1396 | if (r < 0) { | |
1397 | log_link_debug_errno(link, r, "rtnl: received address message without valid address, ignoring: %m"); | |
1398 | continue; | |
1399 | } | |
1400 | ||
1401 | if (!in6_addr_is_link_local(&address)) | |
1402 | continue; | |
1403 | ||
1404 | r = address_new(&a); | |
1405 | if (r < 0) | |
1406 | return -ENOMEM; | |
1407 | ||
1408 | a->family = AF_INET6; | |
1409 | a->in_addr.in6 = address; | |
1410 | a->prefixlen = prefixlen; | |
1411 | a->flags = flags; | |
1412 | ||
f1b73dbb | 1413 | r = address_remove(a, link); |
b9bf3f22 YW |
1414 | if (r < 0) |
1415 | return r; | |
51f5dfd8 YW |
1416 | } |
1417 | ||
1418 | return 0; | |
1419 | } | |
1420 | ||
dd6d53a8 | 1421 | int link_drop_unmanaged_addresses(Link *link) { |
f8f2f880 | 1422 | Address *address; |
1339b950 | 1423 | int r = 0; |
f8f2f880 YW |
1424 | |
1425 | assert(link); | |
3b6a3bde | 1426 | assert(link->network); |
f8f2f880 | 1427 | |
3b6a3bde YW |
1428 | /* First, mark all addresses. */ |
1429 | SET_FOREACH(address, link->addresses) { | |
b9bf3f22 | 1430 | /* We consider IPv6LL addresses to be managed by the kernel, or dropped in link_drop_ipv6ll_addresses() */ |
ecedc48b | 1431 | if (address->family == AF_INET6 && in6_addr_is_link_local(&address->in_addr.in6)) |
f8f2f880 YW |
1432 | continue; |
1433 | ||
b69bfa43 YW |
1434 | /* Do not remove localhost address (127.0.0.1 and ::1) */ |
1435 | if (link->flags & IFF_LOOPBACK && in_addr_is_localhost_one(address->family, &address->in_addr) > 0) | |
1436 | continue; | |
1437 | ||
3b6a3bde YW |
1438 | /* Ignore addresses not assigned yet or already removing. */ |
1439 | if (!address_exists(address)) | |
1440 | continue; | |
1441 | ||
dd6d53a8 YW |
1442 | if (address->source == NETWORK_CONFIG_SOURCE_FOREIGN) { |
1443 | if (link->network->keep_configuration == KEEP_CONFIGURATION_YES) | |
1444 | continue; | |
1445 | ||
1446 | /* link_address_is_dynamic() is slightly heavy. Let's call the function only when | |
80a89d1a YW |
1447 | * KeepConfiguration=dynamic or static. */ |
1448 | if (IN_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC, KEEP_CONFIGURATION_STATIC) && | |
1449 | link_address_is_dynamic(link, address) == (link->network->keep_configuration == KEEP_CONFIGURATION_DYNAMIC)) | |
dd6d53a8 YW |
1450 | continue; |
1451 | ||
1452 | } else if (address->source != NETWORK_CONFIG_SOURCE_STATIC) | |
1453 | continue; /* Ignore dynamically configurad addresses. */ | |
f8f2f880 | 1454 | |
3b6a3bde YW |
1455 | address_mark(address); |
1456 | } | |
1457 | ||
1458 | /* Then, unmark requested addresses. */ | |
1459 | ORDERED_HASHMAP_FOREACH(address, link->network->addresses_by_section) { | |
1460 | Address *existing; | |
1461 | ||
d0009d92 YW |
1462 | if (address_get(link, address, &existing) < 0) |
1463 | continue; | |
1464 | ||
b5aefc90 | 1465 | if (!address_can_update(existing, address)) |
d0009d92 YW |
1466 | continue; |
1467 | ||
1468 | /* Found matching static configuration. Keep the existing address. */ | |
1469 | address_unmark(existing); | |
3b6a3bde YW |
1470 | } |
1471 | ||
1472 | /* Finally, remove all marked addresses. */ | |
1473 | SET_FOREACH(address, link->addresses) { | |
1474 | if (!address_is_marked(address)) | |
1475 | continue; | |
1476 | ||
7f74b00a | 1477 | RET_GATHER(r, address_remove(address, link)); |
f8f2f880 YW |
1478 | } |
1479 | ||
1480 | return r; | |
1481 | } | |
1482 | ||
e09b7588 | 1483 | int link_drop_static_addresses(Link *link) { |
3b6a3bde | 1484 | Address *address; |
1339b950 | 1485 | int r = 0; |
d7fbb9f5 YW |
1486 | |
1487 | assert(link); | |
1488 | ||
1489 | SET_FOREACH(address, link->addresses) { | |
e09b7588 YW |
1490 | /* Remove only static addresses here. Dynamic addresses will be removed e.g. on lease |
1491 | * expiration or stopping the DHCP client. */ | |
1492 | if (address->source != NETWORK_CONFIG_SOURCE_STATIC) | |
3b6a3bde YW |
1493 | continue; |
1494 | ||
a0e99a37 YW |
1495 | /* Ignore addresses not assigned yet or already removing. */ |
1496 | if (!address_exists(address)) | |
d7fbb9f5 YW |
1497 | continue; |
1498 | ||
7f74b00a | 1499 | RET_GATHER(r, address_remove(address, link)); |
d7fbb9f5 YW |
1500 | } |
1501 | ||
1502 | return r; | |
1503 | } | |
1504 | ||
d28746ef | 1505 | int address_configure_handler_internal(sd_netlink_message *m, Link *link, Address *address) { |
5a07fa9d YW |
1506 | int r; |
1507 | ||
5a07fa9d YW |
1508 | assert(m); |
1509 | assert(link); | |
d28746ef | 1510 | assert(address); |
5a07fa9d | 1511 | |
5a07fa9d YW |
1512 | r = sd_netlink_message_get_errno(m); |
1513 | if (r < 0 && r != -EEXIST) { | |
d28746ef YW |
1514 | log_link_message_warning_errno(link, m, r, "Failed to set %s address %s", |
1515 | network_config_source_to_string(address->source), | |
1516 | IN_ADDR_TO_STRING(address->family, &address->in_addr)); | |
5a07fa9d YW |
1517 | link_enter_failed(link); |
1518 | return 0; | |
3a1dfdb4 | 1519 | } |
5a07fa9d YW |
1520 | |
1521 | return 1; | |
1522 | } | |
1523 | ||
b9e4ec50 | 1524 | static int address_configure(const Address *address, const struct ifa_cacheinfo *c, Link *link, Request *req) { |
a79a8d16 | 1525 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
7b3a7581 | 1526 | int r; |
f579559b | 1527 | |
c166a070 | 1528 | assert(address); |
4c701096 | 1529 | assert(IN_SET(address->family, AF_INET, AF_INET6)); |
b9e4ec50 | 1530 | assert(c); |
c166a070 TG |
1531 | assert(link); |
1532 | assert(link->ifindex > 0); | |
f882c247 | 1533 | assert(link->manager); |
c166a070 | 1534 | assert(link->manager->rtnl); |
54ff39f7 | 1535 | assert(req); |
f882c247 | 1536 | |
c0c04a4b | 1537 | log_address_debug(address, "Configuring", link); |
1b566071 | 1538 | |
a79a8d16 | 1539 | r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &m, link->ifindex, address->family); |
eb56eb9b | 1540 | if (r < 0) |
a79a8d16 | 1541 | return r; |
f579559b | 1542 | |
a79a8d16 | 1543 | r = address_set_netlink_message(address, m, link); |
eb56eb9b | 1544 | if (r < 0) |
a8481354 | 1545 | return r; |
851c9f82 | 1546 | |
a79a8d16 | 1547 | r = sd_rtnl_message_addr_set_scope(m, address->scope); |
eb56eb9b | 1548 | if (r < 0) |
a79a8d16 | 1549 | return r; |
5a723174 | 1550 | |
f35aecc4 | 1551 | if (address->family == AF_INET6 || in_addr_is_set(address->family, &address->in_addr_peer)) { |
a79a8d16 | 1552 | r = netlink_message_append_in_addr_union(m, IFA_ADDRESS, address->family, &address->in_addr_peer); |
eb56eb9b | 1553 | if (r < 0) |
a79a8d16 | 1554 | return r; |
e680486d | 1555 | } else if (in4_addr_is_set(&address->broadcast)) { |
a79a8d16 | 1556 | r = sd_netlink_message_append_in_addr(m, IFA_BROADCAST, &address->broadcast); |
66e0bb33 | 1557 | if (r < 0) |
a79a8d16 | 1558 | return r; |
f579559b TG |
1559 | } |
1560 | ||
2a236f9f | 1561 | if (address->family == AF_INET && address->label) { |
a79a8d16 | 1562 | r = sd_netlink_message_append_string(m, IFA_LABEL, address->label); |
eb56eb9b | 1563 | if (r < 0) |
a79a8d16 | 1564 | return r; |
f579559b TG |
1565 | } |
1566 | ||
b9e4ec50 | 1567 | r = sd_netlink_message_append_cache_info(m, IFA_CACHEINFO, c); |
eb56eb9b | 1568 | if (r < 0) |
a79a8d16 | 1569 | return r; |
68ceb9df | 1570 | |
a79a8d16 | 1571 | r = sd_netlink_message_append_u32(m, IFA_RT_PRIORITY, address->route_metric); |
c4ff0629 | 1572 | if (r < 0) |
a79a8d16 | 1573 | return r; |
c4ff0629 | 1574 | |
80d62d4f | 1575 | return request_call_netlink_async(link->manager->rtnl, m, req); |
f579559b TG |
1576 | } |
1577 | ||
dbf66cd1 YW |
1578 | static int address_acquire(Link *link, const Address *address, union in_addr_union *ret) { |
1579 | union in_addr_union a; | |
1580 | int r; | |
1581 | ||
8bed7c55 YW |
1582 | assert(link); |
1583 | assert(address); | |
dbf66cd1 | 1584 | assert(ret); |
8bed7c55 | 1585 | |
7a89aeb1 | 1586 | r = address_acquire_from_dhcp_server_leases_file(link, address, ret); |
f50be187 | 1587 | if (!IN_SET(r, -ENOENT, -ENXIO, -EINVAL)) |
7a89aeb1 YW |
1588 | return r; |
1589 | ||
dbf66cd1 YW |
1590 | r = address_pool_acquire(link->manager, address->family, address->prefixlen, &a); |
1591 | if (r < 0) | |
1592 | return r; | |
1593 | if (r == 0) | |
1594 | return -EBUSY; | |
8bed7c55 | 1595 | |
dbf66cd1 YW |
1596 | /* Pick first address in range for ourselves. */ |
1597 | if (address->family == AF_INET) | |
1598 | a.in.s_addr |= htobe32(1); | |
1599 | else if (address->family == AF_INET6) | |
1600 | a.in6.s6_addr[15] |= 1; | |
1601 | else | |
1602 | assert_not_reached(); | |
8bed7c55 | 1603 | |
dbf66cd1 YW |
1604 | *ret = a; |
1605 | return 0; | |
1606 | } | |
8bed7c55 | 1607 | |
dbf66cd1 YW |
1608 | static int address_requeue_request(Request *req, Link *link, const Address *address) { |
1609 | int r; | |
1610 | ||
1611 | assert(req); | |
1612 | assert(link); | |
1613 | assert(link->manager); | |
1614 | assert(link->network); | |
1615 | assert(address); | |
1616 | ||
1617 | /* Something useful was configured? just use it */ | |
1618 | if (in_addr_is_set(address->family, &address->in_addr)) | |
1619 | return 0; | |
1620 | ||
1621 | /* The address is configured to be 0.0.0.0 or [::] by the user? | |
1622 | * Then let's acquire something more useful. */ | |
1623 | union in_addr_union a; | |
1624 | r = address_acquire(link, address, &a); | |
1625 | if (r < 0) | |
1626 | return r; | |
1627 | ||
1628 | _cleanup_(address_unrefp) Address *tmp = NULL; | |
1629 | r = address_dup(address, &tmp); | |
1630 | if (r < 0) | |
1631 | return r; | |
1632 | ||
1633 | tmp->in_addr = a; | |
1634 | ||
1635 | r = link_requeue_request(link, req, tmp, NULL); | |
1636 | if (r < 0) | |
1637 | return r; | |
1638 | if (r == 0) | |
1639 | return -EEXIST; /* Already queued?? Strange... */ | |
1640 | ||
1641 | TAKE_PTR(tmp); | |
1642 | return 1; /* A new request is queued. it is not necessary to process this request anymore. */ | |
8bed7c55 YW |
1643 | } |
1644 | ||
09d09207 | 1645 | static int address_process_request(Request *req, Link *link, Address *address) { |
60f4cfe4 | 1646 | Address *existing; |
b9e4ec50 | 1647 | struct ifa_cacheinfo c; |
8bed7c55 YW |
1648 | int r; |
1649 | ||
1650 | assert(req); | |
ff51134c YW |
1651 | assert(link); |
1652 | assert(address); | |
8bed7c55 | 1653 | |
dbf66cd1 YW |
1654 | if (!link_is_ready_to_configure(link, false)) |
1655 | return 0; | |
1656 | ||
1657 | /* Refuse adding more than the limit */ | |
1658 | if (set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX) | |
1659 | return 0; | |
1660 | ||
1661 | r = address_requeue_request(req, link, address); | |
1662 | if (r == -EBUSY) | |
1663 | return 0; | |
1664 | if (r != 0) | |
1665 | return r; | |
1666 | ||
1667 | address_set_broadcast(address, link); | |
1668 | ||
1669 | r = ipv4acd_configure(link, address); | |
1670 | if (r < 0) | |
1671 | return r; | |
1672 | ||
1673 | if (!ipv4acd_bound(link, address)) | |
8bed7c55 YW |
1674 | return 0; |
1675 | ||
b9e4ec50 YW |
1676 | address_set_cinfo(link->manager, address, &c); |
1677 | if (c.ifa_valid == 0) { | |
1678 | log_link_debug(link, "Refuse to configure %s address %s, as its valid lifetime is zero.", | |
1679 | network_config_source_to_string(address->source), | |
1680 | IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen)); | |
0a0c2672 | 1681 | |
b9e4ec50 | 1682 | address_cancel_requesting(address); |
0a0c2672 | 1683 | if (address_get(link, address, &existing) >= 0) |
d6b64f7a | 1684 | address_cancel_requesting(existing); |
b9e4ec50 YW |
1685 | return 1; |
1686 | } | |
1687 | ||
1688 | r = address_configure(address, &c, link, req); | |
8bed7c55 YW |
1689 | if (r < 0) |
1690 | return log_link_warning_errno(link, r, "Failed to configure address: %m"); | |
1691 | ||
1692 | address_enter_configuring(address); | |
0a0c2672 YW |
1693 | if (address_get(link, address, &existing) >= 0) |
1694 | address_enter_configuring(existing); | |
1695 | ||
8bed7c55 YW |
1696 | return 1; |
1697 | } | |
1698 | ||
76c5a0f2 YW |
1699 | int link_request_address( |
1700 | Link *link, | |
f60e6558 | 1701 | const Address *address, |
76c5a0f2 | 1702 | unsigned *message_counter, |
80d62d4f | 1703 | address_netlink_handler_t netlink_handler, |
76c5a0f2 YW |
1704 | Request **ret) { |
1705 | ||
ebd96906 | 1706 | _cleanup_(address_unrefp) Address *tmp = NULL; |
0a0c2672 | 1707 | Address *existing = NULL; |
c0c04a4b YW |
1708 | int r; |
1709 | ||
76c5a0f2 YW |
1710 | assert(link); |
1711 | assert(address); | |
3b6a3bde | 1712 | assert(address->source != NETWORK_CONFIG_SOURCE_FOREIGN); |
76c5a0f2 | 1713 | |
0a0c2672 YW |
1714 | if (address->lifetime_valid_usec == 0) |
1715 | /* The requested address is outdated. Let's ignore the request. */ | |
1716 | return 0; | |
af2aea8b | 1717 | |
dbf66cd1 YW |
1718 | if (address_get_request(link, address, NULL) >= 0) |
1719 | return 0; /* already requested, skipping. */ | |
303dfa73 | 1720 | |
dbf66cd1 YW |
1721 | r = address_dup(address, &tmp); |
1722 | if (r < 0) | |
1723 | return log_oom(); | |
9684a8de | 1724 | |
dbf66cd1 | 1725 | if (address_get(link, address, &existing) >= 0) { |
0a0c2672 YW |
1726 | /* Copy already assigned address when it is requested as a null address. */ |
1727 | if (address_is_static_null(address)) | |
1728 | tmp->in_addr = existing->in_addr; | |
9684a8de | 1729 | |
0a0c2672 YW |
1730 | /* Copy state for logging below. */ |
1731 | tmp->state = existing->state; | |
c0c04a4b YW |
1732 | } |
1733 | ||
0a0c2672 | 1734 | log_address_debug(tmp, "Requesting", link); |
09d09207 | 1735 | r = link_queue_request_safe(link, REQUEST_TYPE_ADDRESS, |
0a0c2672 | 1736 | tmp, |
ebd96906 | 1737 | address_unref, |
4da252c5 YW |
1738 | address_hash_func, |
1739 | address_compare_func, | |
09d09207 YW |
1740 | address_process_request, |
1741 | message_counter, netlink_handler, ret); | |
c0c04a4b YW |
1742 | if (r < 0) |
1743 | return log_link_warning_errno(link, r, "Failed to request address: %m"); | |
3b6a3bde YW |
1744 | if (r == 0) |
1745 | return 0; | |
1746 | ||
0a0c2672 YW |
1747 | address_enter_requesting(tmp); |
1748 | if (existing) | |
1749 | address_enter_requesting(existing); | |
3b6a3bde | 1750 | |
0a0c2672 | 1751 | TAKE_PTR(tmp); |
3b6a3bde | 1752 | return 1; |
76c5a0f2 YW |
1753 | } |
1754 | ||
80d62d4f | 1755 | static int static_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) { |
e1e4cd1e YW |
1756 | int r; |
1757 | ||
1758 | assert(link); | |
d28746ef | 1759 | assert(address); |
e1e4cd1e | 1760 | |
d28746ef | 1761 | r = address_configure_handler_internal(m, link, address); |
e1e4cd1e YW |
1762 | if (r <= 0) |
1763 | return r; | |
1764 | ||
1765 | if (link->static_address_messages == 0) { | |
1766 | log_link_debug(link, "Addresses set"); | |
1767 | link->static_addresses_configured = true; | |
1768 | link_check_ready(link); | |
1769 | } | |
1770 | ||
1771 | return 1; | |
1772 | } | |
1773 | ||
f60e6558 | 1774 | int link_request_static_address(Link *link, const Address *address) { |
8c29f484 YW |
1775 | assert(link); |
1776 | assert(address); | |
3b6a3bde | 1777 | assert(address->source == NETWORK_CONFIG_SOURCE_STATIC); |
8c29f484 | 1778 | |
f60e6558 | 1779 | return link_request_address(link, address, &link->static_address_messages, |
3b6a3bde | 1780 | static_address_handler, NULL); |
8c29f484 YW |
1781 | } |
1782 | ||
76c5a0f2 YW |
1783 | int link_request_static_addresses(Link *link) { |
1784 | Address *a; | |
682c65b0 YW |
1785 | int r; |
1786 | ||
1787 | assert(link); | |
1788 | assert(link->network); | |
1789 | ||
76c5a0f2 | 1790 | link->static_addresses_configured = false; |
682c65b0 | 1791 | |
76c5a0f2 | 1792 | ORDERED_HASHMAP_FOREACH(a, link->network->addresses_by_section) { |
f60e6558 | 1793 | r = link_request_static_address(link, a); |
682c65b0 YW |
1794 | if (r < 0) |
1795 | return r; | |
1796 | } | |
1797 | ||
3b6a3bde YW |
1798 | r = link_request_radv_addresses(link); |
1799 | if (r < 0) | |
1800 | return r; | |
0017ba31 | 1801 | |
76c5a0f2 YW |
1802 | if (link->static_address_messages == 0) { |
1803 | link->static_addresses_configured = true; | |
1804 | link_check_ready(link); | |
682c65b0 YW |
1805 | } else { |
1806 | log_link_debug(link, "Setting addresses"); | |
1807 | link_set_state(link, LINK_STATE_CONFIGURING); | |
1808 | } | |
1809 | ||
1810 | return 0; | |
1811 | } | |
1812 | ||
e1fc2c43 | 1813 | int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) { |
a4feabd8 | 1814 | int r; |
e1fc2c43 YW |
1815 | |
1816 | assert(rtnl); | |
1817 | assert(message); | |
1818 | assert(m); | |
1819 | ||
1820 | if (sd_netlink_message_is_error(message)) { | |
1821 | r = sd_netlink_message_get_errno(message); | |
1822 | if (r < 0) | |
1823 | log_message_warning_errno(message, r, "rtnl: failed to receive address message, ignoring"); | |
1824 | ||
1825 | return 0; | |
1826 | } | |
1827 | ||
a4feabd8 | 1828 | uint16_t type; |
e1fc2c43 YW |
1829 | r = sd_netlink_message_get_type(message, &type); |
1830 | if (r < 0) { | |
1831 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); | |
1832 | return 0; | |
1833 | } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) { | |
1834 | log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type); | |
1835 | return 0; | |
1836 | } | |
1837 | ||
a4feabd8 | 1838 | int ifindex; |
e1fc2c43 YW |
1839 | r = sd_rtnl_message_addr_get_ifindex(message, &ifindex); |
1840 | if (r < 0) { | |
1841 | log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m"); | |
1842 | return 0; | |
1843 | } else if (ifindex <= 0) { | |
1844 | log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex); | |
1845 | return 0; | |
1846 | } | |
1847 | ||
a4feabd8 | 1848 | Link *link; |
6eab614d | 1849 | r = link_get_by_index(m, ifindex, &link); |
a3a25d01 | 1850 | if (r < 0) { |
e1fc2c43 YW |
1851 | /* when enumerating we might be out of sync, but we will get the address again, so just |
1852 | * ignore it */ | |
1853 | if (!m->enumerating) | |
1854 | log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex); | |
1855 | return 0; | |
1856 | } | |
1857 | ||
a4feabd8 | 1858 | _cleanup_(address_unrefp) Address *tmp = NULL; |
fe841414 YW |
1859 | r = address_new(&tmp); |
1860 | if (r < 0) | |
1861 | return log_oom(); | |
1862 | ||
3c283289 YW |
1863 | /* First, read minimal information to make address_get() work below. */ |
1864 | ||
fe841414 | 1865 | r = sd_rtnl_message_addr_get_family(message, &tmp->family); |
e1fc2c43 YW |
1866 | if (r < 0) { |
1867 | log_link_warning(link, "rtnl: received address message without family, ignoring."); | |
1868 | return 0; | |
fe841414 YW |
1869 | } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) { |
1870 | log_link_debug(link, "rtnl: received address message with invalid family '%i', ignoring.", tmp->family); | |
e1fc2c43 YW |
1871 | return 0; |
1872 | } | |
1873 | ||
fe841414 | 1874 | r = sd_rtnl_message_addr_get_prefixlen(message, &tmp->prefixlen); |
e1fc2c43 | 1875 | if (r < 0) { |
9a0ad16b | 1876 | log_link_warning_errno(link, r, "rtnl: received address message without prefixlen, ignoring: %m"); |
e1fc2c43 YW |
1877 | return 0; |
1878 | } | |
1879 | ||
fe841414 | 1880 | switch (tmp->family) { |
e1fc2c43 | 1881 | case AF_INET: |
fe841414 | 1882 | r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &tmp->in_addr.in); |
e1fc2c43 YW |
1883 | if (r < 0) { |
1884 | log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m"); | |
1885 | return 0; | |
1886 | } | |
1887 | ||
fe841414 YW |
1888 | r = sd_netlink_message_read_in_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in); |
1889 | if (r < 0 && r != -ENODATA) { | |
1890 | log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m"); | |
1891 | return 0; | |
1892 | } else if (r >= 0) { | |
1893 | if (in4_addr_equal(&tmp->in_addr.in, &tmp->in_addr_peer.in)) | |
1894 | tmp->in_addr_peer = IN_ADDR_NULL; | |
fe841414 YW |
1895 | } |
1896 | ||
e1fc2c43 YW |
1897 | break; |
1898 | ||
1899 | case AF_INET6: | |
fe841414 YW |
1900 | r = sd_netlink_message_read_in6_addr(message, IFA_LOCAL, &tmp->in_addr.in6); |
1901 | if (r >= 0) { | |
1902 | /* Have peer address. */ | |
1903 | r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in6); | |
1904 | if (r < 0) { | |
1905 | log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m"); | |
1906 | return 0; | |
1907 | } | |
fe841414 YW |
1908 | } else if (r == -ENODATA) { |
1909 | /* Does not have peer address. */ | |
1910 | r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr.in6); | |
1911 | if (r < 0) { | |
1912 | log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m"); | |
1913 | return 0; | |
1914 | } | |
1915 | } else { | |
1916 | log_link_warning_errno(link, r, "rtnl: could not get local address from address message, ignoring: %m"); | |
e1fc2c43 YW |
1917 | return 0; |
1918 | } | |
1919 | ||
1920 | break; | |
1921 | ||
1922 | default: | |
04499a70 | 1923 | assert_not_reached(); |
e1fc2c43 YW |
1924 | } |
1925 | ||
a4feabd8 YW |
1926 | /* Then, find the managed Address object corresponding to the received address. */ |
1927 | Address *address = NULL; | |
fe841414 | 1928 | (void) address_get(link, tmp, &address); |
e1fc2c43 | 1929 | |
3c283289 | 1930 | if (type == RTM_DELADDR) { |
a4feabd8 YW |
1931 | if (address) |
1932 | address_forget(link, address, | |
1933 | /* removed_by_us = */ FLAGS_SET(address->state, NETWORK_CONFIG_STATE_REMOVING), | |
1934 | "Forgetting removed"); | |
1935 | else | |
3c283289 | 1936 | log_address_debug(tmp, "Kernel removed unknown", link); |
3b6a3bde | 1937 | |
fc35a9f8 | 1938 | goto finalize; |
3c283289 | 1939 | } |
3b6a3bde | 1940 | |
a4feabd8 | 1941 | bool is_new = false; |
3c283289 YW |
1942 | if (!address) { |
1943 | /* If we did not know the address, then save it. */ | |
ebd96906 | 1944 | r = address_attach(link, tmp); |
3c283289 | 1945 | if (r < 0) { |
e924dc59 | 1946 | log_link_warning_errno(link, r, "Failed to save received address %s, ignoring: %m", |
3c283289 YW |
1947 | IN_ADDR_PREFIX_TO_STRING(tmp->family, &tmp->in_addr, tmp->prefixlen)); |
1948 | return 0; | |
e1fc2c43 | 1949 | } |
ebd96906 | 1950 | address = tmp; |
e1fc2c43 | 1951 | |
3c283289 | 1952 | is_new = true; |
e1fc2c43 | 1953 | |
3c283289 YW |
1954 | } else { |
1955 | /* Otherwise, update the managed Address object with the netlink notification. */ | |
1956 | address->prefixlen = tmp->prefixlen; | |
1957 | address->in_addr_peer = tmp->in_addr_peer; | |
1958 | } | |
e1fc2c43 | 1959 | |
0a0c2672 | 1960 | /* Also update information that cannot be obtained through netlink notification. */ |
a4feabd8 YW |
1961 | Request *req = NULL; |
1962 | (void) address_get_request(link, tmp, &req); | |
0a0c2672 YW |
1963 | if (req && req->waiting_reply) { |
1964 | Address *a = ASSERT_PTR(req->userdata); | |
1965 | ||
1966 | address->source = a->source; | |
1967 | address->provider = a->provider; | |
1968 | (void) free_and_strdup_warn(&address->netlabel, a->netlabel); | |
fc289dd0 TM |
1969 | nft_set_context_clear(&address->nft_set_context); |
1970 | (void) nft_set_context_dup(&a->nft_set_context, &address->nft_set_context); | |
0a0c2672 | 1971 | address->requested_as_null = a->requested_as_null; |
4c4fe8cd | 1972 | address->also_requested_by_dhcp6 = a->also_requested_by_dhcp6; |
0a0c2672 | 1973 | address->callback = a->callback; |
e39bb291 YW |
1974 | |
1975 | ipv6_token_ref(a->token); | |
1976 | ipv6_token_unref(address->token); | |
1977 | address->token = a->token; | |
0a0c2672 YW |
1978 | } |
1979 | ||
3c283289 YW |
1980 | /* Then, update miscellaneous info. */ |
1981 | r = sd_rtnl_message_addr_get_scope(message, &address->scope); | |
1982 | if (r < 0) | |
1983 | log_link_debug_errno(link, r, "rtnl: received address message without scope, ignoring: %m"); | |
e1fc2c43 | 1984 | |
3c283289 YW |
1985 | if (address->family == AF_INET) { |
1986 | _cleanup_free_ char *label = NULL; | |
e1fc2c43 | 1987 | |
3c283289 YW |
1988 | r = sd_netlink_message_read_string_strdup(message, IFA_LABEL, &label); |
1989 | if (r >= 0) { | |
1990 | if (!streq_ptr(label, link->ifname)) | |
1991 | free_and_replace(address->label, label); | |
1992 | } else if (r != -ENODATA) | |
1993 | log_link_debug_errno(link, r, "rtnl: could not get label from address message, ignoring: %m"); | |
1994 | ||
1995 | r = sd_netlink_message_read_in_addr(message, IFA_BROADCAST, &address->broadcast); | |
1996 | if (r < 0 && r != -ENODATA) | |
1997 | log_link_debug_errno(link, r, "rtnl: could not get broadcast from address message, ignoring: %m"); | |
e1fc2c43 YW |
1998 | } |
1999 | ||
3c283289 | 2000 | r = sd_netlink_message_read_u32(message, IFA_FLAGS, &address->flags); |
29c2d4e1 | 2001 | if (r < 0) |
3c283289 YW |
2002 | log_link_debug_errno(link, r, "rtnl: failed to read IFA_FLAGS attribute, ignoring: %m"); |
2003 | ||
a4feabd8 | 2004 | struct ifa_cacheinfo cinfo; |
3c283289 YW |
2005 | r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo); |
2006 | if (r >= 0) | |
2007 | address_set_lifetime(m, address, &cinfo); | |
2008 | else if (r != -ENODATA) | |
2009 | log_link_debug_errno(link, r, "rtnl: failed to read IFA_CACHEINFO attribute, ignoring: %m"); | |
2010 | ||
7e18f9b4 YW |
2011 | r = sd_netlink_message_read_u32(message, IFA_RT_PRIORITY, &address->route_metric); |
2012 | if (r < 0 && r != -ENODATA) | |
2013 | log_link_debug_errno(link, r, "rtnl: failed to read IFA_RT_PRIORITY attribute, ignoring: %m"); | |
2014 | ||
3c283289 | 2015 | address_enter_configured(address); |
0a0c2672 YW |
2016 | if (req) |
2017 | address_enter_configured(req->userdata); | |
2018 | ||
3c283289 YW |
2019 | log_address_debug(address, is_new ? "Received new": "Received updated", link); |
2020 | ||
2021 | /* address_update() logs internally, so we don't need to here. */ | |
2022 | r = address_update(address); | |
2023 | if (r < 0) | |
2024 | link_enter_failed(link); | |
2025 | ||
fc35a9f8 | 2026 | finalize: |
a4feabd8 | 2027 | if (tmp->family == AF_INET6) { |
fc35a9f8 YW |
2028 | r = dhcp4_update_ipv6_connectivity(link); |
2029 | if (r < 0) { | |
2030 | log_link_warning_errno(link, r, "Failed to notify IPv6 connectivity to DHCPv4 client: %m"); | |
2031 | link_enter_failed(link); | |
2032 | } | |
2033 | } | |
2034 | ||
e1fc2c43 YW |
2035 | return 1; |
2036 | } | |
2037 | ||
c6f2197f | 2038 | static int config_parse_broadcast( |
44e7b949 | 2039 | const char *unit, |
eb0ea358 TG |
2040 | const char *filename, |
2041 | unsigned line, | |
2042 | const char *section, | |
2043 | unsigned section_line, | |
2044 | const char *lvalue, | |
2045 | int ltype, | |
2046 | const char *rvalue, | |
2047 | void *data, | |
2048 | void *userdata) { | |
44e7b949 | 2049 | |
c6f2197f | 2050 | Address *address = ASSERT_PTR(userdata); |
eb0ea358 TG |
2051 | int r; |
2052 | ||
6f639b25 YW |
2053 | /* Do not check or set address->family here. It will be checked later in |
2054 | * address_section_verify() -> address_section_adjust_broadcast() . */ | |
2055 | ||
832583ad YW |
2056 | if (isempty(rvalue)) { |
2057 | /* The broadcast address will be calculated based on Address=, and set if the link is | |
6f639b25 | 2058 | * not a wireguard interface. */ |
c6f2197f YW |
2059 | address->broadcast = (struct in_addr) {}; |
2060 | address->set_broadcast = -1; | |
2061 | return 1; | |
832583ad YW |
2062 | } |
2063 | ||
2064 | r = parse_boolean(rvalue); | |
2065 | if (r >= 0) { | |
6f639b25 | 2066 | /* The broadcast address will be calculated based on Address=. */ |
c6f2197f YW |
2067 | address->broadcast = (struct in_addr) {}; |
2068 | address->set_broadcast = r; | |
2069 | return 1; | |
832583ad YW |
2070 | } |
2071 | ||
81fd9280 YW |
2072 | r = config_parse_in_addr_non_null( |
2073 | unit, filename, line, section, section_line, | |
2074 | lvalue, /* ltype = */ AF_INET, rvalue, | |
2075 | &address->broadcast, /* userdata = */ NULL); | |
2076 | if (r <= 0) | |
2077 | return r; | |
eb0ea358 | 2078 | |
c6f2197f | 2079 | address->set_broadcast = true; |
c6f2197f | 2080 | return 1; |
eb0ea358 TG |
2081 | } |
2082 | ||
c6f2197f | 2083 | static int config_parse_address( |
f5ee7d74 | 2084 | const char *unit, |
f579559b TG |
2085 | const char *filename, |
2086 | unsigned line, | |
2087 | const char *section, | |
71a61510 | 2088 | unsigned section_line, |
f579559b TG |
2089 | const char *lvalue, |
2090 | int ltype, | |
2091 | const char *rvalue, | |
2092 | void *data, | |
2093 | void *userdata) { | |
44e7b949 | 2094 | |
c6f2197f | 2095 | Address *address = ASSERT_PTR(userdata); |
1cbc8d91 YW |
2096 | union in_addr_union *a = ASSERT_PTR(data); |
2097 | struct in_addr_prefix prefix; | |
2098 | int r; | |
f579559b | 2099 | |
f579559b | 2100 | assert(rvalue); |
f579559b | 2101 | |
1cbc8d91 YW |
2102 | if (isempty(rvalue)) { |
2103 | /* When an empty string is assigned, clear both Address= and Peer=. */ | |
2104 | address->family = AF_UNSPEC; | |
2105 | address->prefixlen = 0; | |
2106 | address->in_addr = IN_ADDR_NULL; | |
2107 | address->in_addr_peer = IN_ADDR_NULL; | |
2108 | return 1; | |
0f707207 | 2109 | } |
f579559b | 2110 | |
1cbc8d91 YW |
2111 | r = config_parse_in_addr_prefix(unit, filename, line, section, section_line, lvalue, /* ltype = */ true, rvalue, &prefix, /* userdata = */ NULL); |
2112 | if (r <= 0) | |
2113 | return r; | |
2114 | ||
2115 | if (address->family != AF_UNSPEC && prefix.family != address->family) { | |
d96edb2c | 2116 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue); |
44e7b949 LP |
2117 | return 0; |
2118 | } | |
2119 | ||
1cbc8d91 YW |
2120 | address->family = prefix.family; |
2121 | address->prefixlen = prefix.prefixlen; | |
2122 | *a = prefix.address; | |
c6f2197f | 2123 | return 1; |
f579559b | 2124 | } |
6ae115c1 | 2125 | |
c6f2197f | 2126 | static int config_parse_address_label( |
d31645ad | 2127 | const char *unit, |
6ae115c1 TG |
2128 | const char *filename, |
2129 | unsigned line, | |
2130 | const char *section, | |
2131 | unsigned section_line, | |
2132 | const char *lvalue, | |
2133 | int ltype, | |
2134 | const char *rvalue, | |
2135 | void *data, | |
2136 | void *userdata) { | |
d31645ad | 2137 | |
c6f2197f | 2138 | char **label = ASSERT_PTR(data); |
6ae115c1 TG |
2139 | int r; |
2140 | ||
c6f2197f | 2141 | if (!isempty(rvalue) && !address_label_valid(rvalue)) { |
d96edb2c | 2142 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
2850cd40 | 2143 | "Interface label is too long or invalid, ignoring assignment: %s", rvalue); |
6ae115c1 TG |
2144 | return 0; |
2145 | } | |
2146 | ||
c6f2197f | 2147 | r = free_and_strdup_warn(label, empty_to_null(rvalue)); |
d31645ad | 2148 | if (r < 0) |
c6f2197f | 2149 | return r; |
6ae115c1 | 2150 | |
c6f2197f | 2151 | return 1; |
6ae115c1 | 2152 | } |
ce6c77eb | 2153 | |
c6f2197f | 2154 | static int config_parse_address_lifetime( |
f5ee7d74 YW |
2155 | const char *unit, |
2156 | const char *filename, | |
2157 | unsigned line, | |
2158 | const char *section, | |
2159 | unsigned section_line, | |
2160 | const char *lvalue, | |
2161 | int ltype, | |
2162 | const char *rvalue, | |
2163 | void *data, | |
2164 | void *userdata) { | |
2165 | ||
c6f2197f | 2166 | usec_t *usec = ASSERT_PTR(data); |
b5834a0b | 2167 | |
10b20e5a | 2168 | /* We accept only "forever", "infinity", empty, or "0". */ |
c6f2197f YW |
2169 | if (isempty(rvalue) || STR_IN_SET(rvalue, "forever", "infinity")) |
2170 | *usec = USEC_INFINITY; | |
33680b0a | 2171 | else if (streq(rvalue, "0")) |
c6f2197f | 2172 | *usec = 0; |
33680b0a | 2173 | else { |
d96edb2c | 2174 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
33680b0a | 2175 | "Invalid PreferredLifetime= value, ignoring: %s", rvalue); |
b5834a0b SS |
2176 | return 0; |
2177 | } | |
2178 | ||
c6f2197f | 2179 | return 1; |
b5834a0b SS |
2180 | } |
2181 | ||
c6f2197f | 2182 | static int config_parse_address_scope( |
f5ee7d74 YW |
2183 | const char *unit, |
2184 | const char *filename, | |
2185 | unsigned line, | |
2186 | const char *section, | |
2187 | unsigned section_line, | |
2188 | const char *lvalue, | |
2189 | int ltype, | |
2190 | const char *rvalue, | |
2191 | void *data, | |
2192 | void *userdata) { | |
2193 | ||
c6f2197f | 2194 | Address *address = ASSERT_PTR(userdata); |
2959fb07 SS |
2195 | int r; |
2196 | ||
c6f2197f YW |
2197 | if (isempty(rvalue)) { |
2198 | address->scope = RT_SCOPE_UNIVERSE; | |
2199 | address->scope_set = false; | |
2200 | return 1; | |
d96edb2c | 2201 | } |
2959fb07 | 2202 | |
d8c472f2 | 2203 | r = route_scope_from_string(rvalue); |
c6f2197f YW |
2204 | if (r < 0) |
2205 | return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); | |
2959fb07 | 2206 | |
c6f2197f YW |
2207 | address->scope = r; |
2208 | address->scope_set = true; | |
2209 | return 1; | |
2959fb07 SS |
2210 | } |
2211 | ||
c6f2197f | 2212 | static int config_parse_address_dad( |
051e77ca SS |
2213 | const char *unit, |
2214 | const char *filename, | |
2215 | unsigned line, | |
2216 | const char *section, | |
2217 | unsigned section_line, | |
2218 | const char *lvalue, | |
2219 | int ltype, | |
2220 | const char *rvalue, | |
2221 | void *data, | |
2222 | void *userdata) { | |
f5ee7d74 | 2223 | |
c6f2197f | 2224 | AddressFamily *p = ASSERT_PTR(data); |
051e77ca SS |
2225 | int r; |
2226 | ||
c6f2197f YW |
2227 | if (isempty(rvalue)) { |
2228 | *p = _ADDRESS_FAMILY_INVALID; | |
2229 | return 1; | |
d96edb2c | 2230 | } |
051e77ca SS |
2231 | |
2232 | r = parse_boolean(rvalue); | |
2233 | if (r >= 0) { | |
2234 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
2235 | "For historical reasons, %s=%s means %s=%s. " | |
2236 | "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.", | |
2237 | lvalue, rvalue, lvalue, r ? "none" : "both"); | |
c6f2197f YW |
2238 | *p = r ? ADDRESS_FAMILY_NO : ADDRESS_FAMILY_YES; |
2239 | return 1; | |
051e77ca SS |
2240 | } |
2241 | ||
7211c853 | 2242 | AddressFamily a = duplicate_address_detection_address_family_from_string(rvalue); |
c6f2197f YW |
2243 | if (a < 0) |
2244 | return log_syntax_parse_error(unit, filename, line, a, lvalue, rvalue); | |
7211c853 | 2245 | |
c6f2197f YW |
2246 | *p = a; |
2247 | return 1; | |
051e77ca SS |
2248 | } |
2249 | ||
6cf41d46 | 2250 | int config_parse_address_section( |
4b3590c3 TM |
2251 | const char *unit, |
2252 | const char *filename, | |
2253 | unsigned line, | |
2254 | const char *section, | |
2255 | unsigned section_line, | |
2256 | const char *lvalue, | |
2257 | int ltype, | |
2258 | const char *rvalue, | |
2259 | void *data, | |
2260 | void *userdata) { | |
2261 | ||
6cf41d46 | 2262 | static const ConfigSectionParser table[_ADDRESS_CONF_PARSER_MAX] = { |
1cbc8d91 YW |
2263 | [ADDRESS_ADDRESS] = { .parser = config_parse_address, .ltype = 0, .offset = offsetof(Address, in_addr), }, |
2264 | [ADDRESS_PEER] = { .parser = config_parse_address, .ltype = 0, .offset = offsetof(Address, in_addr_peer), }, | |
c6f2197f YW |
2265 | [ADDRESS_BROADCAST] = { .parser = config_parse_broadcast, .ltype = 0, .offset = 0, }, |
2266 | [ADDRESS_LABEL] = { .parser = config_parse_address_label, .ltype = 0, .offset = offsetof(Address, label), }, | |
2267 | [ADDRESS_PREFERRED_LIFETIME] = { .parser = config_parse_address_lifetime, .ltype = 0, .offset = offsetof(Address, lifetime_preferred_usec), }, | |
6cf41d46 YW |
2268 | [ADDRESS_HOME_ADDRESS] = { .parser = config_parse_uint32_flag, .ltype = IFA_F_HOMEADDRESS, .offset = offsetof(Address, flags), }, |
2269 | [ADDRESS_MANAGE_TEMPORARY_ADDRESS] = { .parser = config_parse_uint32_flag, .ltype = IFA_F_MANAGETEMPADDR, .offset = offsetof(Address, flags), }, | |
2270 | [ADDRESS_PREFIX_ROUTE] = { .parser = config_parse_uint32_flag, .ltype = IFA_F_NOPREFIXROUTE, .offset = offsetof(Address, flags), }, | |
2271 | [ADDRESS_ADD_PREFIX_ROUTE] = { .parser = config_parse_uint32_invert_flag, .ltype = IFA_F_NOPREFIXROUTE, .offset = offsetof(Address, flags), }, | |
2272 | [ADDRESS_AUTO_JOIN] = { .parser = config_parse_uint32_flag, .ltype = IFA_F_MCAUTOJOIN, .offset = offsetof(Address, flags), }, | |
c6f2197f YW |
2273 | [ADDRESS_DAD] = { .parser = config_parse_address_dad, .ltype = 0, .offset = offsetof(Address, duplicate_address_detection), }, |
2274 | [ADDRESS_SCOPE] = { .parser = config_parse_address_scope, .ltype = 0, .offset = 0, }, | |
6cf41d46 YW |
2275 | [ADDRESS_ROUTE_METRIC] = { .parser = config_parse_uint32, .ltype = 0, .offset = offsetof(Address, route_metric), }, |
2276 | [ADDRESS_NET_LABEL] = { .parser = config_parse_string, .ltype = CONFIG_PARSE_STRING_SAFE, .offset = offsetof(Address, netlabel), }, | |
2277 | [ADDRESS_NFT_SET] = { .parser = config_parse_nft_set, .ltype = NFT_SET_PARSE_NETWORK, .offset = offsetof(Address, nft_set_context), }, | |
2278 | }; | |
2279 | ||
2280 | _cleanup_(address_unref_or_set_invalidp) Address *address = NULL; | |
2281 | Network *network = ASSERT_PTR(userdata); | |
4b3590c3 TM |
2282 | int r; |
2283 | ||
2284 | assert(filename); | |
c6f2197f YW |
2285 | assert(section); |
2286 | ||
2287 | if (streq(section, "Network")) { | |
2288 | assert(streq(lvalue, "Address")); | |
4b3590c3 | 2289 | |
c6f2197f YW |
2290 | if (isempty(rvalue)) { |
2291 | /* If an empty string specified in [Network] section, clear previously assigned addresses. */ | |
2292 | network->addresses_by_section = ordered_hashmap_free(network->addresses_by_section); | |
2293 | return 0; | |
2294 | } | |
2295 | ||
2296 | /* we are not in an Address section, so use line number instead. */ | |
2297 | r = address_new_static(network, filename, line, &address); | |
2298 | } else | |
2299 | r = address_new_static(network, filename, section_line, &address); | |
4b3590c3 TM |
2300 | if (r == -ENOMEM) |
2301 | return log_oom(); | |
2302 | if (r < 0) { | |
2303 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
2304 | "Failed to allocate new address, ignoring assignment: %m"); | |
2305 | return 0; | |
2306 | } | |
2307 | ||
6cf41d46 YW |
2308 | r = config_section_parse(table, ELEMENTSOF(table), |
2309 | unit, filename, line, section, section_line, lvalue, ltype, rvalue, address); | |
2310 | if (r <= 0) | |
4b3590c3 TM |
2311 | return r; |
2312 | ||
6cf41d46 | 2313 | TAKE_PTR(address); |
4b3590c3 TM |
2314 | return 0; |
2315 | } | |
2316 | ||
e58ec0dc YW |
2317 | #define log_broadcast(address, fmt, ...) \ |
2318 | ({ \ | |
2319 | const Address *_address = (address); \ | |
2320 | log_section_warning( \ | |
2321 | _address ? _address->section : NULL, \ | |
2322 | fmt " Ignoring Broadcast= setting in the [Address] section.", \ | |
2323 | ##__VA_ARGS__); \ | |
2324 | }) | |
2325 | ||
9a45125a YW |
2326 | static void address_section_adjust_broadcast(Address *address) { |
2327 | assert(address); | |
2328 | assert(address->section); | |
2329 | ||
2330 | if (!in4_addr_is_set(&address->broadcast)) | |
2331 | return; | |
2332 | ||
2333 | if (address->family == AF_INET6) | |
e58ec0dc | 2334 | log_broadcast(address, "Broadcast address is set for an IPv6 address."); |
9a45125a | 2335 | else if (address->prefixlen > 30) |
e58ec0dc | 2336 | log_broadcast(address, "Broadcast address is set for an IPv4 address with prefix length larger than 30."); |
9a45125a | 2337 | else if (in4_addr_is_set(&address->in_addr_peer.in)) |
e58ec0dc | 2338 | log_broadcast(address, "Broadcast address is set for an IPv4 address with peer address."); |
0acc6470 | 2339 | else if (!in4_addr_is_set(&address->in_addr.in)) |
e58ec0dc | 2340 | log_broadcast(address, "Broadcast address is set for an IPv4 address with null address."); |
9a45125a YW |
2341 | else |
2342 | /* Otherwise, keep the specified broadcast address. */ | |
2343 | return; | |
2344 | ||
2345 | address->broadcast.s_addr = 0; | |
2346 | } | |
2347 | ||
dd1de202 YW |
2348 | #define log_address_section(address, fmt, ...) \ |
2349 | ({ \ | |
2350 | const Address *_address = (address); \ | |
2351 | log_section_warning_errno( \ | |
2352 | _address ? _address->section : NULL, \ | |
2353 | SYNTHETIC_ERRNO(EINVAL), \ | |
2354 | fmt " Ignoring [Address] section.", \ | |
2355 | ##__VA_ARGS__); \ | |
2356 | }) | |
2357 | ||
5459e11d | 2358 | int address_section_verify(Address *address) { |
dd1de202 YW |
2359 | assert(address); |
2360 | assert(address->section); | |
2361 | ||
fcbf4cb7 YW |
2362 | if (section_is_invalid(address->section)) |
2363 | return -EINVAL; | |
2364 | ||
dd1de202 YW |
2365 | if (address->family == AF_UNSPEC) |
2366 | return log_address_section(address, "Address section without Address= field was configured."); | |
1cbc8d91 | 2367 | assert(IN_SET(address->family, AF_INET, AF_INET6)); |
fcbf4cb7 | 2368 | |
d9e2afc0 | 2369 | if (address->family == AF_INET6 && !socket_ipv6_is_supported()) |
dd1de202 | 2370 | return log_address_section(address, "An IPv6 address was configured, but the kernel does not support IPv6."); |
d9e2afc0 | 2371 | |
1cbc8d91 YW |
2372 | if (in_addr_is_null(address->family, &address->in_addr)) { |
2373 | /* Will use address from address pool. Note that for ipv6 case, prefix of the address | |
2374 | * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So, | |
2375 | * let's limit the prefix length to 64 or larger. See RFC4193. */ | |
2376 | unsigned min_prefixlen = address->family == AF_INET ? 8 : 64; | |
2377 | if (address->prefixlen < min_prefixlen) | |
2378 | return log_address_section(address, "Prefix length for %s null address must be equal or larger than %u.", | |
2379 | af_to_ipv4_ipv6(address->family), min_prefixlen); | |
2380 | ||
2381 | address->requested_as_null = !in_addr_is_set(address->family, &address->in_addr); | |
2382 | } | |
2859932b | 2383 | |
9a45125a | 2384 | address_section_adjust_broadcast(address); |
fe841414 YW |
2385 | |
2386 | if (address->family == AF_INET6 && address->label) { | |
dd1de202 | 2387 | log_section_warning(address->section, "Address label is set for IPv6 address, ignoring Label= setting."); |
fe841414 YW |
2388 | address->label = mfree(address->label); |
2389 | } | |
2390 | ||
20228b6d YW |
2391 | if (!address->scope_set) { |
2392 | if (in_addr_is_localhost(address->family, &address->in_addr) > 0) | |
2393 | address->scope = RT_SCOPE_HOST; | |
2394 | else if (in_addr_is_link_local(address->family, &address->in_addr) > 0) | |
2395 | address->scope = RT_SCOPE_LINK; | |
2396 | } | |
07336a06 | 2397 | |
2859932b YW |
2398 | if (address->duplicate_address_detection < 0) { |
2399 | if (address->family == AF_INET6) | |
2400 | address->duplicate_address_detection = ADDRESS_FAMILY_IPV6; | |
2401 | else if (in4_addr_is_link_local(&address->in_addr.in)) | |
2402 | address->duplicate_address_detection = ADDRESS_FAMILY_IPV4; | |
2403 | else | |
2404 | address->duplicate_address_detection = ADDRESS_FAMILY_NO; | |
2405 | } else if (address->duplicate_address_detection == ADDRESS_FAMILY_IPV6 && address->family == AF_INET) | |
dd1de202 | 2406 | log_section_warning(address->section, "DuplicateAddressDetection=ipv6 is specified for IPv4 address, ignoring."); |
2859932b | 2407 | else if (address->duplicate_address_detection == ADDRESS_FAMILY_IPV4 && address->family == AF_INET6) |
dd1de202 | 2408 | log_section_warning(address->section, "DuplicateAddressDetection=ipv4 is specified for IPv6 address, ignoring."); |
2859932b | 2409 | |
bd5146c6 YW |
2410 | if (address->family == AF_INET6 && |
2411 | !FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV6)) | |
eaff204f YW |
2412 | address->flags |= IFA_F_NODAD; |
2413 | ||
b1179a5d YW |
2414 | uint32_t filtered_flags = address->family == AF_INET ? |
2415 | address->flags & KNOWN_FLAGS & ~UNMANAGED_FLAGS & ~IPV6ONLY_FLAGS : | |
2416 | address->flags & KNOWN_FLAGS & ~UNMANAGED_FLAGS; | |
2417 | if (address->flags != filtered_flags) { | |
2418 | _cleanup_free_ char *str = NULL; | |
2419 | ||
b7d43592 | 2420 | (void) address_flags_to_string_alloc(address->flags ^ filtered_flags, address->family, &str); |
dd1de202 | 2421 | return log_address_section(address, "unexpected address flags \"%s\" were configured.", strna(str)); |
b1179a5d YW |
2422 | } |
2423 | ||
fcbf4cb7 YW |
2424 | return 0; |
2425 | } | |
32400c2f | 2426 | |
6a4fe38f YW |
2427 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
2428 | trivial_hash_ops_address_detach, | |
2429 | void, | |
2430 | trivial_hash_func, | |
2431 | trivial_compare_func, | |
2432 | Address, | |
2433 | address_detach); | |
2434 | ||
50783f91 | 2435 | int network_drop_invalid_addresses(Network *network) { |
6a4fe38f | 2436 | _cleanup_set_free_ Set *addresses = NULL, *duplicated_addresses = NULL; |
9cd9fc8f | 2437 | Address *address; |
50783f91 | 2438 | int r; |
32400c2f YW |
2439 | |
2440 | assert(network); | |
2441 | ||
50783f91 | 2442 | ORDERED_HASHMAP_FOREACH(address, network->addresses_by_section) { |
50783f91 YW |
2443 | if (address_section_verify(address) < 0) { |
2444 | /* Drop invalid [Address] sections or Address= settings in [Network]. | |
ebd96906 YW |
2445 | * Note that address_detach() will drop the address from addresses_by_section. */ |
2446 | address_detach(address); | |
50783f91 YW |
2447 | continue; |
2448 | } | |
2449 | ||
2450 | /* Always use the setting specified later. So, remove the previously assigned setting. */ | |
6a4fe38f | 2451 | Address *dup = set_remove(addresses, address); |
50783f91 | 2452 | if (dup) { |
50783f91 YW |
2453 | log_warning("%s: Duplicated address %s is specified at line %u and %u, " |
2454 | "dropping the address setting specified at line %u.", | |
c71384a9 ZJS |
2455 | dup->section->filename, |
2456 | IN_ADDR_PREFIX_TO_STRING(address->family, &address->in_addr, address->prefixlen), | |
2457 | address->section->line, | |
50783f91 | 2458 | dup->section->line, dup->section->line); |
ebd96906 | 2459 | |
6a4fe38f YW |
2460 | /* Do not call address_detach() for 'dup' now, as we can remove only the current |
2461 | * entry in the loop. We will drop the address from addresses_by_section later. */ | |
2462 | r = set_ensure_put(&duplicated_addresses, &trivial_hash_ops_address_detach, dup); | |
2463 | if (r < 0) | |
2464 | return log_oom(); | |
2465 | assert(r > 0); | |
50783f91 YW |
2466 | } |
2467 | ||
ebd96906 YW |
2468 | /* Use address_hash_ops, instead of address_hash_ops_detach. Otherwise, the Address objects |
2469 | * will be detached. */ | |
4da252c5 | 2470 | r = set_ensure_put(&addresses, &address_hash_ops, address); |
50783f91 YW |
2471 | if (r < 0) |
2472 | return log_oom(); | |
2473 | assert(r > 0); | |
2474 | } | |
2475 | ||
5459e11d YW |
2476 | r = network_adjust_dhcp_server(network, &addresses); |
2477 | if (r < 0) | |
2478 | return r; | |
26f88471 | 2479 | |
50783f91 | 2480 | return 0; |
32400c2f | 2481 | } |