]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
f579559b TG |
2 | |
3 | #include <net/if.h> | |
b8162cd2 | 4 | #include <net/if_arp.h> |
f579559b | 5 | |
b5efdb8a | 6 | #include "alloc-util.h" |
12c2884c | 7 | #include "firewall-util.h" |
0a970718 | 8 | #include "memory-util.h" |
fc2f9534 | 9 | #include "netlink-util.h" |
bfbf150e | 10 | #include "networkd-address-pool.h" |
6bedfcbb | 11 | #include "networkd-address.h" |
12f7469b | 12 | #include "networkd-ipv6-proxy-ndp.h" |
23f53b99 | 13 | #include "networkd-manager.h" |
093e3533 | 14 | #include "networkd-network.h" |
6bedfcbb | 15 | #include "parse-util.h" |
07630cea | 16 | #include "string-util.h" |
51517f9e | 17 | #include "strv.h" |
f579559b | 18 | |
1b566071 | 19 | #define ADDRESSES_PER_LINK_MAX 2048U |
8c34b963 LP |
20 | #define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U |
21 | ||
b1476b52 | 22 | int generate_ipv6_eui_64_address(const Link *link, struct in6_addr *ret) { |
5ead5352 SS |
23 | assert(link); |
24 | assert(ret); | |
25 | ||
b8162cd2 TR |
26 | if (link->iftype == ARPHRD_INFINIBAND) { |
27 | /* see RFC4391 section 8 */ | |
28 | memcpy(&ret->s6_addr[8], &link->hw_addr.addr.infiniband[12], 8); | |
29 | ret->s6_addr[8] ^= 1 << 1; | |
30 | ||
31 | return 0; | |
32 | } | |
33 | ||
5ead5352 | 34 | /* see RFC4291 section 2.5.1 */ |
b8162cd2 | 35 | ret->s6_addr[8] = link->hw_addr.addr.ether.ether_addr_octet[0]; |
5ead5352 | 36 | ret->s6_addr[8] ^= 1 << 1; |
b8162cd2 TR |
37 | ret->s6_addr[9] = link->hw_addr.addr.ether.ether_addr_octet[1]; |
38 | ret->s6_addr[10] = link->hw_addr.addr.ether.ether_addr_octet[2]; | |
5ead5352 SS |
39 | ret->s6_addr[11] = 0xff; |
40 | ret->s6_addr[12] = 0xfe; | |
b8162cd2 TR |
41 | ret->s6_addr[13] = link->hw_addr.addr.ether.ether_addr_octet[3]; |
42 | ret->s6_addr[14] = link->hw_addr.addr.ether.ether_addr_octet[4]; | |
43 | ret->s6_addr[15] = link->hw_addr.addr.ether.ether_addr_octet[5]; | |
5ead5352 SS |
44 | |
45 | return 0; | |
46 | } | |
47 | ||
f0213e37 | 48 | int address_new(Address **ret) { |
8e766630 | 49 | _cleanup_(address_freep) Address *address = NULL; |
f0213e37 | 50 | |
17f9c355 | 51 | address = new(Address, 1); |
f0213e37 TG |
52 | if (!address) |
53 | return -ENOMEM; | |
aba496a5 | 54 | |
17f9c355 YW |
55 | *address = (Address) { |
56 | .family = AF_UNSPEC, | |
57 | .scope = RT_SCOPE_UNIVERSE, | |
58 | .cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME, | |
59 | .cinfo.ifa_valid = CACHE_INFO_INFINITY_LIFE_TIME, | |
051e77ca | 60 | .duplicate_address_detection = ADDRESS_FAMILY_IPV6, |
17f9c355 | 61 | }; |
f0213e37 | 62 | |
1cc6c93a | 63 | *ret = TAKE_PTR(address); |
f0213e37 TG |
64 | |
65 | return 0; | |
aba496a5 UTL |
66 | } |
67 | ||
9560e5b3 | 68 | static int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) { |
8e766630 LP |
69 | _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL; |
70 | _cleanup_(address_freep) Address *address = NULL; | |
f0213e37 | 71 | int r; |
f579559b | 72 | |
8c34b963 LP |
73 | assert(network); |
74 | assert(ret); | |
9cd9fc8f YW |
75 | assert(filename); |
76 | assert(section_line > 0); | |
f4859fc7 | 77 | |
9cd9fc8f YW |
78 | r = network_config_section_new(filename, section_line, &n); |
79 | if (r < 0) | |
80 | return r; | |
6ae115c1 | 81 | |
9cd9fc8f YW |
82 | address = ordered_hashmap_get(network->addresses_by_section, n); |
83 | if (address) { | |
84 | *ret = TAKE_PTR(address); | |
85 | return 0; | |
6ae115c1 TG |
86 | } |
87 | ||
9cd9fc8f | 88 | if (ordered_hashmap_size(network->addresses_by_section) >= STATIC_ADDRESSES_PER_NETWORK_MAX) |
8c34b963 LP |
89 | return -E2BIG; |
90 | ||
f0213e37 TG |
91 | r = address_new(&address); |
92 | if (r < 0) | |
93 | return r; | |
801bd9e8 | 94 | |
0f7f2769 | 95 | address->network = network; |
9cd9fc8f | 96 | address->section = TAKE_PTR(n); |
0f7f2769 | 97 | |
28d9587b | 98 | r = ordered_hashmap_ensure_put(&network->addresses_by_section, &network_config_hash_ops, address->section, address); |
9cd9fc8f YW |
99 | if (r < 0) |
100 | return r; | |
6ae115c1 | 101 | |
1cc6c93a | 102 | *ret = TAKE_PTR(address); |
f579559b TG |
103 | return 0; |
104 | } | |
105 | ||
7818f858 | 106 | Address *address_free(Address *address) { |
f579559b | 107 | if (!address) |
7818f858 | 108 | return NULL; |
f579559b | 109 | |
f048a16b | 110 | if (address->network) { |
9cd9fc8f YW |
111 | assert(address->section); |
112 | ordered_hashmap_remove(address->network->addresses_by_section, address->section); | |
f048a16b | 113 | } |
6ae115c1 | 114 | |
490ccbd5 | 115 | if (address->link) { |
50550722 | 116 | NDiscAddress *n; |
50550722 | 117 | |
cf1d700d | 118 | set_remove(address->link->addresses, address); |
adda1ed9 | 119 | set_remove(address->link->addresses_foreign, address); |
e5526518 | 120 | set_remove(address->link->static_addresses, address); |
6e537f62 YW |
121 | if (address->link->dhcp_address == address) |
122 | address->link->dhcp_address = NULL; | |
123 | if (address->link->dhcp_address_old == address) | |
124 | address->link->dhcp_address_old = NULL; | |
1633c457 YW |
125 | set_remove(address->link->dhcp6_addresses, address); |
126 | set_remove(address->link->dhcp6_addresses_old, address); | |
127 | set_remove(address->link->dhcp6_pd_addresses, address); | |
128 | set_remove(address->link->dhcp6_pd_addresses_old, address); | |
90e74a66 | 129 | SET_FOREACH(n, address->link->ndisc_addresses) |
50550722 YW |
130 | if (n->address == address) |
131 | free(set_remove(address->link->ndisc_addresses, n)); | |
f150100a SS |
132 | |
133 | if (in_addr_equal(AF_INET6, &address->in_addr, (const union in_addr_union *) &address->link->ipv6ll_address)) | |
134 | memzero(&address->link->ipv6ll_address, sizeof(struct in6_addr)); | |
adda1ed9 | 135 | } |
cf1d700d | 136 | |
051e77ca SS |
137 | sd_ipv4acd_unref(address->acd); |
138 | ||
de4224aa YW |
139 | network_config_section_free(address->section); |
140 | free(address->label); | |
7818f858 | 141 | return mfree(address); |
f579559b TG |
142 | } |
143 | ||
2a236f9f YW |
144 | static bool address_may_have_broadcast(const Address *a) { |
145 | assert(a); | |
146 | ||
147 | /* A /31 or /32 IPv4 address does not have a broadcast address. | |
148 | * See https://tools.ietf.org/html/rfc3021 */ | |
149 | ||
150 | return a->family == AF_INET && in4_addr_is_null(&a->in_addr_peer.in) && a->prefixlen <= 30; | |
151 | } | |
152 | ||
1d30fc5c YW |
153 | static uint32_t address_prefix(const Address *a) { |
154 | assert(a); | |
155 | ||
156 | /* make sure we don't try to shift by 32. | |
157 | * See ISO/IEC 9899:TC3 § 6.5.7.3. */ | |
158 | if (a->prefixlen == 0) | |
159 | return 0; | |
160 | ||
161 | if (a->in_addr_peer.in.s_addr != 0) | |
162 | return be32toh(a->in_addr_peer.in.s_addr) >> (32 - a->prefixlen); | |
163 | else | |
164 | return be32toh(a->in_addr.in.s_addr) >> (32 - a->prefixlen); | |
165 | } | |
166 | ||
99a28784 | 167 | void address_hash_func(const Address *a, struct siphash *state) { |
3ac8e543 TG |
168 | assert(a); |
169 | ||
170 | siphash24_compress(&a->family, sizeof(a->family), state); | |
171 | ||
1d30fc5c YW |
172 | switch (a->family) { |
173 | case AF_INET: | |
174 | siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state); | |
3ac8e543 | 175 | |
1d30fc5c YW |
176 | uint32_t prefix = address_prefix(a); |
177 | siphash24_compress(&prefix, sizeof(prefix), state); | |
3ac8e543 | 178 | |
1d30fc5c YW |
179 | _fallthrough_; |
180 | case AF_INET6: | |
181 | siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state); | |
182 | break; | |
183 | default: | |
184 | /* treat any other address family as AF_UNSPEC */ | |
185 | break; | |
186 | } | |
3ac8e543 TG |
187 | } |
188 | ||
99a28784 | 189 | int address_compare_func(const Address *a1, const Address *a2) { |
a0edd02e | 190 | int r; |
3ac8e543 | 191 | |
a0edd02e FB |
192 | r = CMP(a1->family, a2->family); |
193 | if (r != 0) | |
194 | return r; | |
3ac8e543 | 195 | |
1d30fc5c YW |
196 | switch (a1->family) { |
197 | case AF_INET: | |
198 | /* See kernel's find_matching_ifa() in net/ipv4/devinet.c */ | |
199 | r = CMP(a1->prefixlen, a2->prefixlen); | |
24be9181 ZJS |
200 | if (r != 0) |
201 | return r; | |
3ac8e543 | 202 | |
1d30fc5c YW |
203 | r = CMP(address_prefix(a1), address_prefix(a2)); |
204 | if (r != 0) | |
205 | return r; | |
2a236f9f | 206 | |
1d30fc5c YW |
207 | _fallthrough_; |
208 | case AF_INET6: | |
209 | /* See kernel's ipv6_get_ifaddr() in net/ipv6/addrconf.c */ | |
210 | return memcmp(&a1->in_addr, &a2->in_addr, FAMILY_ADDRESS_SIZE(a1->family)); | |
211 | default: | |
212 | /* treat any other address family as AF_UNSPEC */ | |
213 | return 0; | |
214 | } | |
3ac8e543 TG |
215 | } |
216 | ||
8eec0b9d | 217 | DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(address_hash_ops, Address, address_hash_func, address_compare_func, address_free); |
3ac8e543 | 218 | |
b1476b52 | 219 | bool address_equal(const Address *a1, const Address *a2) { |
3ac8e543 TG |
220 | if (a1 == a2) |
221 | return true; | |
222 | ||
223 | if (!a1 || !a2) | |
224 | return false; | |
225 | ||
226 | return address_compare_func(a1, a2) == 0; | |
227 | } | |
228 | ||
cde1f0e8 YW |
229 | static int address_copy(Address *dest, const Address *src) { |
230 | int r; | |
231 | ||
232 | assert(dest); | |
233 | assert(src); | |
234 | ||
2a236f9f YW |
235 | if (src->family == AF_INET) { |
236 | r = free_and_strdup(&dest->label, src->label); | |
237 | if (r < 0) | |
238 | return r; | |
239 | } | |
cde1f0e8 YW |
240 | |
241 | dest->family = src->family; | |
242 | dest->prefixlen = src->prefixlen; | |
243 | dest->scope = src->scope; | |
244 | dest->flags = src->flags; | |
cde1f0e8 YW |
245 | dest->cinfo = src->cinfo; |
246 | dest->in_addr = src->in_addr; | |
247 | dest->in_addr_peer = src->in_addr_peer; | |
2a236f9f YW |
248 | if (address_may_have_broadcast(src)) |
249 | dest->broadcast = src->broadcast; | |
cde1f0e8 YW |
250 | dest->duplicate_address_detection = src->duplicate_address_detection; |
251 | ||
252 | return 0; | |
253 | } | |
254 | ||
494b6b43 YW |
255 | static int address_set_masquerade(Address *address, bool add) { |
256 | union in_addr_union masked; | |
91b5f997 TG |
257 | int r; |
258 | ||
259 | assert(address); | |
494b6b43 | 260 | assert(address->link); |
91b5f997 | 261 | |
494b6b43 YW |
262 | if (!address->link->network) |
263 | return 0; | |
91b5f997 | 264 | |
494b6b43 YW |
265 | if (!address->link->network->ip_masquerade) |
266 | return 0; | |
91b5f997 | 267 | |
494b6b43 YW |
268 | if (address->family != AF_INET) |
269 | return 0; | |
91b5f997 | 270 | |
494b6b43 YW |
271 | if (address->scope >= RT_SCOPE_LINK) |
272 | return 0; | |
273 | ||
274 | if (address->ip_masquerade_done == add) | |
275 | return 0; | |
276 | ||
277 | masked = address->in_addr; | |
278 | r = in_addr_mask(address->family, &masked, address->prefixlen); | |
279 | if (r < 0) | |
280 | return r; | |
281 | ||
761cf19d | 282 | r = fw_add_masquerade(&address->link->manager->fw_ctx, add, AF_INET, &masked, address->prefixlen); |
494b6b43 YW |
283 | if (r < 0) |
284 | return r; | |
285 | ||
286 | address->ip_masquerade_done = add; | |
91b5f997 TG |
287 | |
288 | return 0; | |
289 | } | |
290 | ||
fe841414 | 291 | static int address_add_internal(Link *link, Set **addresses, const Address *in, Address **ret) { |
8e766630 | 292 | _cleanup_(address_freep) Address *address = NULL; |
cf1d700d TG |
293 | int r; |
294 | ||
295 | assert(link); | |
adda1ed9 | 296 | assert(addresses); |
fe841414 | 297 | assert(in); |
054f0db4 TG |
298 | |
299 | r = address_new(&address); | |
300 | if (r < 0) | |
301 | return r; | |
302 | ||
fe841414 YW |
303 | r = address_copy(address, in); |
304 | if (r < 0) | |
305 | return r; | |
306 | ||
63bbe5c7 TG |
307 | /* Consider address tentative until we get the real flags from the kernel */ |
308 | address->flags = IFA_F_TENTATIVE; | |
cf1d700d | 309 | |
de7fef4b | 310 | r = set_ensure_put(addresses, &address_hash_ops, address); |
cf1d700d TG |
311 | if (r < 0) |
312 | return r; | |
75a302b5 YW |
313 | if (r == 0) |
314 | return -EEXIST; | |
cf1d700d TG |
315 | |
316 | address->link = link; | |
317 | ||
adda1ed9 TG |
318 | if (ret) |
319 | *ret = address; | |
de7fef4b | 320 | TAKE_PTR(address); |
cf1d700d TG |
321 | return 0; |
322 | } | |
323 | ||
fe841414 YW |
324 | static int address_add_foreign(Link *link, const Address *in, Address **ret) { |
325 | return address_add_internal(link, &link->addresses_foreign, in, ret); | |
adda1ed9 TG |
326 | } |
327 | ||
fe841414 | 328 | static int address_add(Link *link, const Address *in, Address **ret) { |
d9eee312 | 329 | bool is_new = false; |
cab974b0 | 330 | Address *address; |
e7780c8d TG |
331 | int r; |
332 | ||
fe841414 YW |
333 | assert(link); |
334 | assert(in); | |
335 | ||
336 | r = address_get(link, in, &address); | |
cab974b0 TG |
337 | if (r == -ENOENT) { |
338 | /* Address does not exist, create a new one */ | |
fe841414 | 339 | r = address_add_internal(link, &link->addresses, in, &address); |
cab974b0 TG |
340 | if (r < 0) |
341 | return r; | |
d9eee312 | 342 | is_new = true; |
cab974b0 TG |
343 | } else if (r == 0) { |
344 | /* Take over a foreign address */ | |
de7fef4b | 345 | r = set_ensure_put(&link->addresses, &address_hash_ops, address); |
cab974b0 TG |
346 | if (r < 0) |
347 | return r; | |
348 | ||
349 | set_remove(link->addresses_foreign, address); | |
350 | } else if (r == 1) { | |
351 | /* Already exists, do nothing */ | |
352 | ; | |
353 | } else | |
e7780c8d TG |
354 | return r; |
355 | ||
cab974b0 TG |
356 | if (ret) |
357 | *ret = address; | |
d9eee312 | 358 | return is_new; |
adda1ed9 TG |
359 | } |
360 | ||
fe841414 | 361 | static int address_update(Address *address, const Address *src) { |
36c32f61 | 362 | bool ready; |
e7ab854c | 363 | int r; |
36c32f61 TG |
364 | |
365 | assert(address); | |
ea121d8f | 366 | assert(address->link); |
fe841414 | 367 | assert(src); |
36c32f61 TG |
368 | |
369 | ready = address_is_ready(address); | |
370 | ||
fe841414 YW |
371 | address->flags = src->flags; |
372 | address->scope = src->scope; | |
373 | address->cinfo = src->cinfo; | |
36c32f61 | 374 | |
ea121d8f YW |
375 | if (IN_SET(address->link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
376 | return 0; | |
377 | ||
959f65d3 | 378 | link_update_operstate(address->link, true); |
c8f7123e | 379 | link_check_ready(address->link); |
7209086d | 380 | |
97f00074 YW |
381 | if (!ready && address_is_ready(address)) { |
382 | if (address->callback) { | |
383 | r = address->callback(address); | |
384 | if (r < 0) | |
385 | return r; | |
386 | } | |
7209086d | 387 | |
97f00074 YW |
388 | if (address->family == AF_INET6 && |
389 | in_addr_is_link_local(AF_INET6, &address->in_addr) > 0 && | |
390 | IN6_IS_ADDR_UNSPECIFIED(&address->link->ipv6ll_address) > 0) { | |
391 | ||
392 | r = link_ipv6ll_gained(address->link, &address->in_addr.in6); | |
393 | if (r < 0) | |
394 | return r; | |
395 | } | |
a3a019e1 | 396 | } |
36c32f61 TG |
397 | |
398 | return 0; | |
399 | } | |
400 | ||
67a58eb3 | 401 | static int address_drop(Address *address) { |
8012cd39 TG |
402 | Link *link; |
403 | bool ready; | |
7750b796 | 404 | int r; |
8012cd39 | 405 | |
1db01a33 YW |
406 | if (!address) |
407 | return 0; | |
91b5f997 | 408 | |
8012cd39 TG |
409 | ready = address_is_ready(address); |
410 | link = address->link; | |
411 | ||
494b6b43 | 412 | r = address_set_masquerade(address, false); |
7750b796 YW |
413 | if (r < 0) |
414 | log_link_warning_errno(link, r, "Failed to disable IP masquerading, ignoring: %m"); | |
415 | ||
91b5f997 TG |
416 | address_free(address); |
417 | ||
959f65d3 | 418 | link_update_operstate(link, true); |
84de38c5 | 419 | |
8012cd39 TG |
420 | if (link && !ready) |
421 | link_check_ready(link); | |
422 | ||
91b5f997 TG |
423 | return 0; |
424 | } | |
425 | ||
fe841414 YW |
426 | int address_get(Link *link, const Address *in, Address **ret) { |
427 | Address *existing; | |
91b5f997 | 428 | |
5a8bcb67 | 429 | assert(link); |
fe841414 | 430 | assert(in); |
5a8bcb67 | 431 | |
fe841414 | 432 | existing = set_get(link->addresses, in); |
cab974b0 | 433 | if (existing) { |
1b566071 LP |
434 | if (ret) |
435 | *ret = existing; | |
cab974b0 | 436 | return 1; |
adda1ed9 | 437 | } |
5a8bcb67 | 438 | |
fe841414 | 439 | existing = set_get(link->addresses_foreign, in); |
1b566071 LP |
440 | if (existing) { |
441 | if (ret) | |
442 | *ret = existing; | |
443 | return 0; | |
444 | } | |
5a8bcb67 | 445 | |
1b566071 | 446 | return -ENOENT; |
5a8bcb67 LP |
447 | } |
448 | ||
c5a0aeb3 YW |
449 | int link_has_ipv6_address(Link *link, const struct in6_addr *address) { |
450 | _cleanup_(address_freep) Address *a = NULL; | |
451 | int r; | |
5eec0a08 | 452 | |
c5a0aeb3 YW |
453 | assert(link); |
454 | assert(address); | |
5eec0a08 | 455 | |
c5a0aeb3 YW |
456 | r = address_new(&a); |
457 | if (r < 0) | |
458 | return r; | |
5eec0a08 | 459 | |
c5a0aeb3 YW |
460 | /* address_compare_func() only compares the local address for IPv6 case. So, it is enough to |
461 | * set only family and the address. */ | |
462 | a->family = AF_INET6; | |
463 | a->in_addr.in6 = *address; | |
5eec0a08 | 464 | |
c5a0aeb3 | 465 | return address_get(link, a, NULL) >= 0; |
5eec0a08 YW |
466 | } |
467 | ||
24f50382 YW |
468 | static void log_address_debug(const Address *address, const char *str, const Link *link) { |
469 | assert(address); | |
470 | assert(str); | |
471 | assert(link); | |
472 | ||
473 | if (DEBUG_LOGGING) { | |
474 | _cleanup_free_ char *addr = NULL, *peer = NULL; | |
fe44ce8d YW |
475 | char valid_buf[FORMAT_TIMESPAN_MAX], preferred_buf[FORMAT_TIMESPAN_MAX]; |
476 | const char *valid_str = NULL, *preferred_str = NULL; | |
24f50382 YW |
477 | bool has_peer; |
478 | ||
479 | (void) in_addr_to_string(address->family, &address->in_addr, &addr); | |
480 | has_peer = in_addr_is_null(address->family, &address->in_addr_peer) == 0; | |
481 | if (has_peer) | |
482 | (void) in_addr_to_string(address->family, &address->in_addr_peer, &peer); | |
483 | ||
484 | if (address->cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME) | |
485 | valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX, | |
486 | address->cinfo.ifa_valid * USEC_PER_SEC, | |
487 | USEC_PER_SEC); | |
488 | ||
fe44ce8d YW |
489 | if (address->cinfo.ifa_prefered != CACHE_INFO_INFINITY_LIFE_TIME) |
490 | preferred_str = format_timespan(preferred_buf, FORMAT_TIMESPAN_MAX, | |
491 | address->cinfo.ifa_prefered * USEC_PER_SEC, | |
492 | USEC_PER_SEC); | |
493 | ||
494 | log_link_debug(link, "%s address: %s%s%s/%u (valid %s%s, preferred %s%s)", | |
24f50382 YW |
495 | str, strnull(addr), has_peer ? " peer " : "", |
496 | has_peer ? strnull(peer) : "", address->prefixlen, | |
fe44ce8d YW |
497 | valid_str ? "for " : "forever", strempty(valid_str), |
498 | preferred_str ? "for " : "forever", strempty(preferred_str)); | |
24f50382 YW |
499 | } |
500 | } | |
501 | ||
302a796f | 502 | static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
63ae0569 YW |
503 | int r; |
504 | ||
505 | assert(m); | |
506 | assert(link); | |
507 | assert(link->ifname); | |
508 | ||
509 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
510 | return 1; | |
511 | ||
512 | r = sd_netlink_message_get_errno(m); | |
513 | if (r < 0 && r != -EADDRNOTAVAIL) | |
5ecb131d | 514 | log_link_message_warning_errno(link, m, r, "Could not drop address"); |
fb282d4e | 515 | else if (r >= 0) |
93b0b88c | 516 | (void) manager_rtnl_process_address(rtnl, m, link->manager); |
63ae0569 YW |
517 | |
518 | return 1; | |
519 | } | |
520 | ||
a8481354 | 521 | static int address_set_netlink_message(const Address *address, sd_netlink_message *req, Link *link) { |
a8481354 YW |
522 | int r; |
523 | ||
524 | assert(address); | |
525 | assert(req); | |
526 | assert(link); | |
527 | ||
528 | r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen); | |
529 | if (r < 0) | |
530 | return log_link_error_errno(link, r, "Could not set prefixlen: %m"); | |
531 | ||
532 | /* On remove, only IFA_F_MANAGETEMPADDR flag for IPv6 addresses are used. But anyway, set all | |
533 | * flags here unconditionally. Without setting the flag, the template addresses generated by | |
534 | * kernel will not be removed automatically when the main address is removed. */ | |
53ae4762 | 535 | r = sd_rtnl_message_addr_set_flags(req, address->flags & 0xff); |
a8481354 YW |
536 | if (r < 0) |
537 | return log_link_error_errno(link, r, "Could not set flags: %m"); | |
538 | ||
53ae4762 YW |
539 | if ((address->flags & ~0xff) != 0) { |
540 | r = sd_netlink_message_append_u32(req, IFA_FLAGS, address->flags); | |
a8481354 YW |
541 | if (r < 0) |
542 | return log_link_error_errno(link, r, "Could not set extended flags: %m"); | |
543 | } | |
544 | ||
545 | r = netlink_message_append_in_addr_union(req, IFA_LOCAL, address->family, &address->in_addr); | |
546 | if (r < 0) | |
547 | return log_link_error_errno(link, r, "Could not append IFA_LOCAL attribute: %m"); | |
548 | ||
549 | return 0; | |
550 | } | |
551 | ||
483d099e | 552 | int address_remove( |
b1476b52 | 553 | const Address *address, |
483d099e | 554 | Link *link, |
302a796f | 555 | link_netlink_message_handler_t callback) { |
483d099e | 556 | |
4afd3348 | 557 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
407fe036 TG |
558 | int r; |
559 | ||
560 | assert(address); | |
4c701096 | 561 | assert(IN_SET(address->family, AF_INET, AF_INET6)); |
407fe036 TG |
562 | assert(link); |
563 | assert(link->ifindex > 0); | |
564 | assert(link->manager); | |
565 | assert(link->manager->rtnl); | |
566 | ||
24f50382 | 567 | log_address_debug(address, "Removing", link); |
30226d27 | 568 | |
151b9b96 LP |
569 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR, |
570 | link->ifindex, address->family); | |
eb56eb9b | 571 | if (r < 0) |
7750b796 | 572 | return log_link_error_errno(link, r, "Could not allocate RTM_DELADDR message: %m"); |
407fe036 | 573 | |
a8481354 | 574 | r = address_set_netlink_message(address, req, link); |
eb56eb9b | 575 | if (r < 0) |
a8481354 | 576 | return r; |
407fe036 | 577 | |
302a796f YW |
578 | r = netlink_call_async(link->manager->rtnl, NULL, req, |
579 | callback ?: address_remove_handler, | |
580 | link_netlink_destroy_callback, link); | |
eb56eb9b | 581 | if (r < 0) |
7750b796 | 582 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); |
407fe036 | 583 | |
563c69c6 TG |
584 | link_ref(link); |
585 | ||
407fe036 TG |
586 | return 0; |
587 | } | |
588 | ||
b1476b52 | 589 | static bool link_is_static_address_configured(const Link *link, const Address *address) { |
f8f2f880 YW |
590 | Address *net_address; |
591 | ||
592 | assert(link); | |
593 | assert(address); | |
594 | ||
595 | if (!link->network) | |
596 | return false; | |
597 | ||
9cd9fc8f | 598 | ORDERED_HASHMAP_FOREACH(net_address, link->network->addresses_by_section) |
f8f2f880 YW |
599 | if (address_equal(net_address, address)) |
600 | return true; | |
f8f2f880 YW |
601 | |
602 | return false; | |
603 | } | |
604 | ||
3def8850 | 605 | bool link_address_is_dynamic(const Link *link, const Address *address) { |
f8f2f880 YW |
606 | Route *route; |
607 | ||
608 | assert(link); | |
609 | assert(address); | |
610 | ||
611 | if (address->cinfo.ifa_prefered != CACHE_INFO_INFINITY_LIFE_TIME) | |
612 | return true; | |
613 | ||
614 | /* Even when the address is leased from a DHCP server, networkd assign the address | |
615 | * without lifetime when KeepConfiguration=dhcp. So, let's check that we have | |
616 | * corresponding routes with RTPROT_DHCP. */ | |
617 | SET_FOREACH(route, link->routes_foreign) { | |
618 | if (route->protocol != RTPROT_DHCP) | |
619 | continue; | |
620 | ||
621 | if (address->family != route->family) | |
622 | continue; | |
623 | ||
624 | if (in_addr_equal(address->family, &address->in_addr, &route->prefsrc)) | |
625 | return true; | |
626 | } | |
627 | ||
628 | return false; | |
629 | } | |
630 | ||
51f5dfd8 YW |
631 | static int link_enumerate_ipv6_tentative_addresses(Link *link) { |
632 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
51f5dfd8 YW |
633 | int r; |
634 | ||
635 | assert(link); | |
636 | assert(link->manager); | |
637 | assert(link->manager->rtnl); | |
638 | ||
639 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_GETADDR, 0, AF_INET6); | |
640 | if (r < 0) | |
641 | return r; | |
642 | ||
643 | r = sd_netlink_call(link->manager->rtnl, req, 0, &reply); | |
644 | if (r < 0) | |
645 | return r; | |
646 | ||
cda7fc8d | 647 | for (sd_netlink_message *addr = reply; addr; addr = sd_netlink_message_next(addr)) { |
51f5dfd8 YW |
648 | unsigned char flags; |
649 | int ifindex; | |
650 | ||
651 | r = sd_rtnl_message_addr_get_ifindex(addr, &ifindex); | |
652 | if (r < 0) { | |
653 | log_link_warning_errno(link, r, "rtnl: invalid ifindex, ignoring: %m"); | |
654 | continue; | |
655 | } else if (link->ifindex != ifindex) | |
656 | continue; | |
657 | ||
658 | r = sd_rtnl_message_addr_get_flags(addr, &flags); | |
659 | if (r < 0) { | |
660 | log_link_warning_errno(link, r, "rtnl: received address message with invalid flags, ignoring: %m"); | |
661 | continue; | |
662 | } else if (!(flags & IFA_F_TENTATIVE)) | |
663 | continue; | |
664 | ||
665 | log_link_debug(link, "Found tentative ipv6 link-local address"); | |
666 | (void) manager_rtnl_process_address(link->manager->rtnl, addr, link->manager); | |
667 | } | |
668 | ||
669 | return 0; | |
670 | } | |
671 | ||
f8f2f880 YW |
672 | int link_drop_foreign_addresses(Link *link) { |
673 | Address *address; | |
674 | int k, r = 0; | |
675 | ||
676 | assert(link); | |
677 | ||
51f5dfd8 YW |
678 | /* The kernel doesn't notify us about tentative addresses; |
679 | * so if ipv6ll is disabled, we need to enumerate them now so we can drop them below */ | |
680 | if (!link_ipv6ll_enabled(link)) { | |
681 | r = link_enumerate_ipv6_tentative_addresses(link); | |
682 | if (r < 0) | |
683 | return r; | |
684 | } | |
685 | ||
f8f2f880 YW |
686 | SET_FOREACH(address, link->addresses_foreign) { |
687 | /* we consider IPv6LL addresses to be managed by the kernel */ | |
688 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1 && link_ipv6ll_enabled(link)) | |
689 | continue; | |
690 | ||
691 | if (link_address_is_dynamic(link, address)) { | |
692 | if (link->network && FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) | |
693 | continue; | |
694 | } else if (link->network && FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC)) | |
695 | continue; | |
696 | ||
697 | if (link_is_static_address_configured(link, address)) { | |
fe841414 | 698 | k = address_add(link, address, NULL); |
f8f2f880 YW |
699 | if (k < 0) { |
700 | log_link_error_errno(link, k, "Failed to add address: %m"); | |
701 | if (r >= 0) | |
702 | r = k; | |
703 | } | |
704 | } else { | |
705 | k = address_remove(address, link, NULL); | |
706 | if (k < 0 && r >= 0) | |
707 | r = k; | |
708 | } | |
709 | } | |
710 | ||
711 | return r; | |
712 | } | |
713 | ||
d7fbb9f5 YW |
714 | static int remove_static_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
715 | int r; | |
716 | ||
717 | assert(m); | |
718 | assert(link); | |
719 | assert(link->ifname); | |
720 | assert(link->address_remove_messages > 0); | |
721 | ||
722 | link->address_remove_messages--; | |
723 | ||
724 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
725 | return 1; | |
726 | ||
727 | r = sd_netlink_message_get_errno(m); | |
728 | if (r < 0 && r != -EADDRNOTAVAIL) | |
729 | log_link_message_warning_errno(link, m, r, "Could not drop address"); | |
730 | else if (r >= 0) | |
731 | (void) manager_rtnl_process_address(rtnl, m, link->manager); | |
732 | ||
733 | if (link->address_remove_messages == 0 && link->request_static_addresses) { | |
734 | link_set_state(link, LINK_STATE_CONFIGURING); | |
735 | r = link_set_addresses(link); | |
736 | if (r < 0) | |
737 | link_enter_failed(link); | |
738 | } | |
739 | ||
740 | return 1; | |
741 | } | |
742 | ||
743 | int link_drop_addresses(Link *link) { | |
744 | Address *address, *pool_address; | |
745 | int k, r = 0; | |
746 | ||
747 | assert(link); | |
748 | ||
749 | SET_FOREACH(address, link->addresses) { | |
750 | /* we consider IPv6LL addresses to be managed by the kernel */ | |
751 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1 && link_ipv6ll_enabled(link)) | |
752 | continue; | |
753 | ||
754 | k = address_remove(address, link, remove_static_address_handler); | |
755 | if (k < 0 && r >= 0) { | |
756 | r = k; | |
757 | continue; | |
758 | } | |
759 | ||
760 | link->address_remove_messages++; | |
761 | ||
aa651e88 YW |
762 | SET_FOREACH(pool_address, link->pool_addresses) |
763 | if (address_equal(address, pool_address)) | |
764 | address_free(set_remove(link->pool_addresses, pool_address)); | |
d7fbb9f5 YW |
765 | } |
766 | ||
767 | return r; | |
768 | } | |
769 | ||
693ec5ca | 770 | static int address_acquire(Link *link, const Address *original, Address **ret) { |
67a46833 | 771 | union in_addr_union in_addr = IN_ADDR_NULL; |
11bf3cce | 772 | struct in_addr broadcast = {}; |
8e766630 | 773 | _cleanup_(address_freep) Address *na = NULL; |
11bf3cce LP |
774 | int r; |
775 | ||
776 | assert(link); | |
777 | assert(original); | |
778 | assert(ret); | |
779 | ||
780 | /* Something useful was configured? just use it */ | |
7e43ebfb | 781 | r = in_addr_is_null(original->family, &original->in_addr); |
aa651e88 | 782 | if (r < 0) |
7e43ebfb | 783 | return r; |
aa651e88 YW |
784 | if (r == 0) { |
785 | *ret = NULL; | |
786 | return 0; | |
787 | } | |
11bf3cce LP |
788 | |
789 | /* The address is configured to be 0.0.0.0 or [::] by the user? | |
790 | * Then let's acquire something more useful from the pool. */ | |
ed76f585 | 791 | r = address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr); |
6a7a4e4d | 792 | if (r < 0) |
7750b796 YW |
793 | return r; |
794 | if (r == 0) | |
11bf3cce | 795 | return -EBUSY; |
11bf3cce LP |
796 | |
797 | if (original->family == AF_INET) { | |
d076c6f9 | 798 | /* Pick first address in range for ourselves ... */ |
11bf3cce LP |
799 | in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1); |
800 | ||
801 | /* .. and use last as broadcast address */ | |
e87e2b78 SS |
802 | if (original->prefixlen > 30) |
803 | broadcast.s_addr = 0; | |
804 | else | |
805 | broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen); | |
11bf3cce LP |
806 | } else if (original->family == AF_INET6) |
807 | in_addr.in6.s6_addr[15] |= 1; | |
808 | ||
f0213e37 | 809 | r = address_new(&na); |
11bf3cce LP |
810 | if (r < 0) |
811 | return r; | |
812 | ||
cde1f0e8 YW |
813 | r = address_copy(na, original); |
814 | if (r < 0) | |
815 | return r; | |
11bf3cce LP |
816 | |
817 | na->broadcast = broadcast; | |
818 | na->in_addr = in_addr; | |
819 | ||
aa651e88 YW |
820 | r = set_ensure_put(&link->pool_addresses, &address_hash_ops, na); |
821 | if (r < 0) | |
822 | return r; | |
823 | if (r == 0) | |
824 | return -EEXIST; | |
11bf3cce | 825 | |
1cc6c93a | 826 | *ret = TAKE_PTR(na); |
aa651e88 | 827 | return 1; |
11bf3cce LP |
828 | } |
829 | ||
490ccbd5 YW |
830 | static int ipv4_dad_configure(Address *address); |
831 | ||
1b566071 | 832 | int address_configure( |
b1476b52 | 833 | const Address *address, |
1b566071 | 834 | Link *link, |
302a796f | 835 | link_netlink_message_handler_t callback, |
80b0e860 | 836 | Address **ret) { |
1b566071 | 837 | |
4afd3348 | 838 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
aa651e88 | 839 | Address *acquired_address, *a; |
37c0b601 | 840 | bool update; |
d9eee312 | 841 | int r, k; |
f579559b | 842 | |
c166a070 | 843 | assert(address); |
4c701096 | 844 | assert(IN_SET(address->family, AF_INET, AF_INET6)); |
c166a070 TG |
845 | assert(link); |
846 | assert(link->ifindex > 0); | |
f882c247 | 847 | assert(link->manager); |
c166a070 | 848 | assert(link->manager->rtnl); |
bd1175bc | 849 | assert(callback); |
f882c247 | 850 | |
1b566071 | 851 | /* If this is a new address, then refuse adding more than the limit */ |
fe841414 | 852 | if (address_get(link, address, NULL) <= 0 && |
1b566071 | 853 | set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX) |
7750b796 YW |
854 | return log_link_error_errno(link, SYNTHETIC_ERRNO(E2BIG), |
855 | "Too many addresses are configured, refusing: %m"); | |
1b566071 | 856 | |
aa651e88 | 857 | r = address_acquire(link, address, &acquired_address); |
11bf3cce | 858 | if (r < 0) |
7750b796 | 859 | return log_link_error_errno(link, r, "Failed to acquire an address from pool: %m"); |
aa651e88 YW |
860 | if (acquired_address) |
861 | address = acquired_address; | |
11bf3cce | 862 | |
37c0b601 YW |
863 | update = address_get(link, address, NULL) >= 0; |
864 | ||
24f50382 | 865 | log_address_debug(address, update ? "Updating" : "Configuring", link); |
3606ca65 | 866 | |
66669078 TG |
867 | if (update) |
868 | r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req, | |
869 | link->ifindex, address->family); | |
870 | else | |
871 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR, | |
872 | link->ifindex, address->family); | |
eb56eb9b | 873 | if (r < 0) |
7750b796 | 874 | return log_link_error_errno(link, r, "Could not allocate RTM_NEWADDR message: %m"); |
f579559b | 875 | |
a8481354 | 876 | r = address_set_netlink_message(address, req, link); |
eb56eb9b | 877 | if (r < 0) |
a8481354 | 878 | return r; |
851c9f82 | 879 | |
5c1d3fc9 | 880 | r = sd_rtnl_message_addr_set_scope(req, address->scope); |
eb56eb9b | 881 | if (r < 0) |
7750b796 | 882 | return log_link_error_errno(link, r, "Could not set scope: %m"); |
5a723174 | 883 | |
d40b01e4 | 884 | if (in_addr_is_null(address->family, &address->in_addr_peer) == 0) { |
43409486 | 885 | r = netlink_message_append_in_addr_union(req, IFA_ADDRESS, address->family, &address->in_addr_peer); |
eb56eb9b | 886 | if (r < 0) |
7750b796 | 887 | return log_link_error_errno(link, r, "Could not append IFA_ADDRESS attribute: %m"); |
2a236f9f | 888 | } else if (address_may_have_broadcast(address)) { |
66e0bb33 YW |
889 | r = sd_netlink_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast); |
890 | if (r < 0) | |
7750b796 | 891 | return log_link_error_errno(link, r, "Could not append IFA_BROADCAST attribute: %m"); |
f579559b TG |
892 | } |
893 | ||
2a236f9f | 894 | if (address->family == AF_INET && address->label) { |
1c4baffc | 895 | r = sd_netlink_message_append_string(req, IFA_LABEL, address->label); |
eb56eb9b | 896 | if (r < 0) |
7750b796 | 897 | return log_link_error_errno(link, r, "Could not append IFA_LABEL attribute: %m"); |
f579559b TG |
898 | } |
899 | ||
66e0bb33 | 900 | r = sd_netlink_message_append_cache_info(req, IFA_CACHEINFO, &address->cinfo); |
eb56eb9b | 901 | if (r < 0) |
7750b796 | 902 | return log_link_error_errno(link, r, "Could not append IFA_CACHEINFO attribute: %m"); |
68ceb9df | 903 | |
d9eee312 YW |
904 | k = address_add(link, address, &a); |
905 | if (k < 0) | |
906 | return log_link_error_errno(link, k, "Could not add address: %m"); | |
494b6b43 | 907 | |
494b6b43 | 908 | r = address_set_masquerade(a, true); |
eb56eb9b | 909 | if (r < 0) |
7750b796 | 910 | log_link_warning_errno(link, r, "Could not enable IP masquerading, ignoring: %m"); |
fcf50cff | 911 | |
dfef713f | 912 | r = netlink_call_async(link->manager->rtnl, NULL, req, callback, link_netlink_destroy_callback, link); |
fcf50cff | 913 | if (r < 0) { |
494b6b43 | 914 | (void) address_set_masquerade(a, false); |
7750b796 | 915 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); |
fcf50cff | 916 | } |
f579559b | 917 | |
563c69c6 TG |
918 | link_ref(link); |
919 | ||
490ccbd5 YW |
920 | if (FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV4)) { |
921 | r = ipv4_dad_configure(a); | |
051e77ca SS |
922 | if (r < 0) |
923 | log_link_warning_errno(link, r, "Failed to start IPv4ACD client, ignoring: %m"); | |
924 | } | |
925 | ||
80b0e860 YW |
926 | if (ret) |
927 | *ret = a; | |
928 | ||
d9eee312 | 929 | return k; |
f579559b TG |
930 | } |
931 | ||
682c65b0 YW |
932 | static int static_address_ready_callback(Address *address) { |
933 | Address *a; | |
934 | Link *link; | |
12f7469b | 935 | int r; |
682c65b0 YW |
936 | |
937 | assert(address); | |
938 | assert(address->link); | |
939 | ||
940 | link = address->link; | |
941 | ||
942 | if (!link->addresses_configured) | |
943 | return 0; | |
944 | ||
945 | SET_FOREACH(a, link->static_addresses) | |
946 | if (!address_is_ready(a)) { | |
947 | _cleanup_free_ char *str = NULL; | |
948 | ||
949 | (void) in_addr_to_string(a->family, &a->in_addr, &str); | |
950 | log_link_debug(link, "an address %s/%u is not ready", strnull(str), a->prefixlen); | |
951 | return 0; | |
952 | } | |
953 | ||
954 | /* This should not be called again */ | |
955 | SET_FOREACH(a, link->static_addresses) | |
956 | a->callback = NULL; | |
957 | ||
958 | link->addresses_ready = true; | |
959 | ||
12f7469b KF |
960 | r = link_set_ipv6_proxy_ndp_addresses(link); |
961 | if (r < 0) | |
962 | return r; | |
963 | ||
682c65b0 YW |
964 | return link_set_routes(link); |
965 | } | |
966 | ||
967 | static int address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { | |
968 | int r; | |
969 | ||
970 | assert(rtnl); | |
971 | assert(m); | |
972 | assert(link); | |
973 | assert(link->ifname); | |
974 | assert(link->address_messages > 0); | |
682c65b0 YW |
975 | |
976 | link->address_messages--; | |
977 | ||
978 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
979 | return 1; | |
980 | ||
981 | r = sd_netlink_message_get_errno(m); | |
982 | if (r < 0 && r != -EEXIST) { | |
983 | log_link_message_warning_errno(link, m, r, "Could not set address"); | |
984 | link_enter_failed(link); | |
985 | return 1; | |
986 | } else if (r >= 0) | |
987 | (void) manager_rtnl_process_address(rtnl, m, link->manager); | |
988 | ||
989 | if (link->address_messages == 0) { | |
990 | Address *a; | |
991 | ||
992 | log_link_debug(link, "Addresses set"); | |
993 | link->addresses_configured = true; | |
994 | ||
995 | /* When all static addresses are already ready, then static_address_ready_callback() | |
996 | * will not be called automatically. So, call it here. */ | |
997 | a = set_first(link->static_addresses); | |
998 | if (!a) { | |
b788a429 | 999 | log_link_debug(link, "No static address is stored. Already removed?"); |
682c65b0 YW |
1000 | return 1; |
1001 | } | |
b788a429 YW |
1002 | |
1003 | r = static_address_ready_callback(a); | |
682c65b0 YW |
1004 | if (r < 0) |
1005 | link_enter_failed(link); | |
1006 | } | |
1007 | ||
1008 | return 1; | |
1009 | } | |
1010 | ||
37c0b601 | 1011 | static int static_address_configure(const Address *address, Link *link) { |
682c65b0 YW |
1012 | Address *ret; |
1013 | int r; | |
1014 | ||
1015 | assert(address); | |
1016 | assert(link); | |
1017 | ||
37c0b601 | 1018 | r = address_configure(address, link, address_handler, &ret); |
682c65b0 YW |
1019 | if (r < 0) |
1020 | return log_link_warning_errno(link, r, "Could not configure static address: %m"); | |
1021 | ||
1022 | link->address_messages++; | |
1023 | ||
1024 | r = set_ensure_put(&link->static_addresses, &address_hash_ops, ret); | |
1025 | if (r < 0) | |
1026 | return log_link_warning_errno(link, r, "Failed to store static address: %m"); | |
1027 | ||
1028 | ret->callback = static_address_ready_callback; | |
1029 | ||
1030 | return 0; | |
1031 | } | |
1032 | ||
1033 | int link_set_addresses(Link *link) { | |
1034 | Address *ad; | |
8a08bbfc | 1035 | Prefix *p; |
682c65b0 YW |
1036 | int r; |
1037 | ||
1038 | assert(link); | |
1039 | assert(link->network); | |
1040 | ||
1041 | if (link->address_remove_messages != 0) { | |
1042 | log_link_debug(link, "Removing old addresses, new addresses will be configured later."); | |
1043 | link->request_static_addresses = true; | |
1044 | return 0; | |
1045 | } | |
1046 | ||
bd4733da YW |
1047 | if (link->address_messages != 0) { |
1048 | log_link_debug(link, "Static addresses are configuring."); | |
1049 | return 0; | |
1050 | } | |
1051 | ||
9cd9fc8f | 1052 | ORDERED_HASHMAP_FOREACH(ad, link->network->addresses_by_section) { |
37c0b601 | 1053 | r = static_address_configure(ad, link); |
682c65b0 YW |
1054 | if (r < 0) |
1055 | return r; | |
1056 | } | |
1057 | ||
8a08bbfc YW |
1058 | HASHMAP_FOREACH(p, link->network->prefixes_by_section) { |
1059 | _cleanup_(address_freep) Address *address = NULL; | |
682c65b0 | 1060 | |
8a08bbfc YW |
1061 | if (!p->assign) |
1062 | continue; | |
682c65b0 | 1063 | |
8a08bbfc YW |
1064 | r = address_new(&address); |
1065 | if (r < 0) | |
1066 | return log_oom(); | |
682c65b0 | 1067 | |
8a08bbfc YW |
1068 | r = sd_radv_prefix_get_prefix(p->radv_prefix, &address->in_addr.in6, &address->prefixlen); |
1069 | if (r < 0) | |
1070 | return log_link_warning_errno(link, r, "Could not get RA prefix: %m"); | |
682c65b0 | 1071 | |
8a08bbfc YW |
1072 | r = generate_ipv6_eui_64_address(link, &address->in_addr.in6); |
1073 | if (r < 0) | |
1074 | return log_link_warning_errno(link, r, "Could not generate EUI64 address: %m"); | |
682c65b0 | 1075 | |
8a08bbfc | 1076 | address->family = AF_INET6; |
37c0b601 | 1077 | r = static_address_configure(address, link); |
8a08bbfc YW |
1078 | if (r < 0) |
1079 | return r; | |
682c65b0 YW |
1080 | } |
1081 | ||
1082 | if (link->address_messages == 0) { | |
1083 | link->addresses_configured = true; | |
1084 | link->addresses_ready = true; | |
12f7469b KF |
1085 | |
1086 | r = link_set_ipv6_proxy_ndp_addresses(link); | |
1087 | if (r < 0) | |
1088 | return r; | |
1089 | ||
682c65b0 YW |
1090 | r = link_set_routes(link); |
1091 | if (r < 0) | |
1092 | return r; | |
1093 | } else { | |
1094 | log_link_debug(link, "Setting addresses"); | |
1095 | link_set_state(link, LINK_STATE_CONFIGURING); | |
1096 | } | |
1097 | ||
1098 | return 0; | |
1099 | } | |
1100 | ||
e1fc2c43 | 1101 | int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) { |
fe841414 | 1102 | _cleanup_(address_freep) Address *tmp = NULL; |
e1fc2c43 YW |
1103 | Link *link = NULL; |
1104 | uint16_t type; | |
fe841414 | 1105 | unsigned char flags; |
e1fc2c43 | 1106 | Address *address = NULL; |
fe841414 | 1107 | int ifindex, r; |
e1fc2c43 YW |
1108 | |
1109 | assert(rtnl); | |
1110 | assert(message); | |
1111 | assert(m); | |
1112 | ||
1113 | if (sd_netlink_message_is_error(message)) { | |
1114 | r = sd_netlink_message_get_errno(message); | |
1115 | if (r < 0) | |
1116 | log_message_warning_errno(message, r, "rtnl: failed to receive address message, ignoring"); | |
1117 | ||
1118 | return 0; | |
1119 | } | |
1120 | ||
1121 | r = sd_netlink_message_get_type(message, &type); | |
1122 | if (r < 0) { | |
1123 | log_warning_errno(r, "rtnl: could not get message type, ignoring: %m"); | |
1124 | return 0; | |
1125 | } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) { | |
1126 | log_warning("rtnl: received unexpected message type %u when processing address, ignoring.", type); | |
1127 | return 0; | |
1128 | } | |
1129 | ||
1130 | r = sd_rtnl_message_addr_get_ifindex(message, &ifindex); | |
1131 | if (r < 0) { | |
1132 | log_warning_errno(r, "rtnl: could not get ifindex from message, ignoring: %m"); | |
1133 | return 0; | |
1134 | } else if (ifindex <= 0) { | |
1135 | log_warning("rtnl: received address message with invalid ifindex %d, ignoring.", ifindex); | |
1136 | return 0; | |
1137 | } | |
1138 | ||
1139 | r = link_get(m, ifindex, &link); | |
1140 | if (r < 0 || !link) { | |
1141 | /* when enumerating we might be out of sync, but we will get the address again, so just | |
1142 | * ignore it */ | |
1143 | if (!m->enumerating) | |
1144 | log_warning("rtnl: received address for link '%d' we don't know about, ignoring.", ifindex); | |
1145 | return 0; | |
1146 | } | |
1147 | ||
fe841414 YW |
1148 | r = address_new(&tmp); |
1149 | if (r < 0) | |
1150 | return log_oom(); | |
1151 | ||
1152 | r = sd_rtnl_message_addr_get_family(message, &tmp->family); | |
e1fc2c43 YW |
1153 | if (r < 0) { |
1154 | log_link_warning(link, "rtnl: received address message without family, ignoring."); | |
1155 | return 0; | |
fe841414 YW |
1156 | } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) { |
1157 | log_link_debug(link, "rtnl: received address message with invalid family '%i', ignoring.", tmp->family); | |
e1fc2c43 YW |
1158 | return 0; |
1159 | } | |
1160 | ||
fe841414 | 1161 | r = sd_rtnl_message_addr_get_prefixlen(message, &tmp->prefixlen); |
e1fc2c43 | 1162 | if (r < 0) { |
9a0ad16b | 1163 | log_link_warning_errno(link, r, "rtnl: received address message without prefixlen, ignoring: %m"); |
e1fc2c43 YW |
1164 | return 0; |
1165 | } | |
1166 | ||
fe841414 | 1167 | r = sd_rtnl_message_addr_get_scope(message, &tmp->scope); |
e1fc2c43 | 1168 | if (r < 0) { |
9a0ad16b | 1169 | log_link_warning_errno(link, r, "rtnl: received address message without scope, ignoring: %m"); |
e1fc2c43 YW |
1170 | return 0; |
1171 | } | |
1172 | ||
1173 | r = sd_rtnl_message_addr_get_flags(message, &flags); | |
1174 | if (r < 0) { | |
9a0ad16b | 1175 | log_link_warning_errno(link, r, "rtnl: received address message without flags, ignoring: %m"); |
e1fc2c43 YW |
1176 | return 0; |
1177 | } | |
fe841414 | 1178 | tmp->flags = flags; |
e1fc2c43 | 1179 | |
fe841414 | 1180 | switch (tmp->family) { |
e1fc2c43 | 1181 | case AF_INET: |
fe841414 | 1182 | r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &tmp->in_addr.in); |
e1fc2c43 YW |
1183 | if (r < 0) { |
1184 | log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m"); | |
1185 | return 0; | |
1186 | } | |
1187 | ||
fe841414 YW |
1188 | r = sd_netlink_message_read_in_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in); |
1189 | if (r < 0 && r != -ENODATA) { | |
1190 | log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m"); | |
1191 | return 0; | |
1192 | } else if (r >= 0) { | |
1193 | if (in4_addr_equal(&tmp->in_addr.in, &tmp->in_addr_peer.in)) | |
1194 | tmp->in_addr_peer = IN_ADDR_NULL; | |
fe841414 YW |
1195 | } |
1196 | ||
1197 | r = sd_netlink_message_read_in_addr(message, IFA_BROADCAST, &tmp->broadcast); | |
1198 | if (r < 0 && r != -ENODATA) { | |
1199 | log_link_warning_errno(link, r, "rtnl: could not get broadcast from address message, ignoring: %m"); | |
1200 | return 0; | |
1201 | } | |
1202 | ||
1203 | r = sd_netlink_message_read_string_strdup(message, IFA_LABEL, &tmp->label); | |
1204 | if (r < 0 && r != -ENODATA) { | |
1205 | log_link_warning_errno(link, r, "rtnl: could not get label from address message, ignoring: %m"); | |
1206 | return 0; | |
1207 | } else if (r >= 0 && streq_ptr(tmp->label, link->ifname)) | |
1208 | tmp->label = mfree(tmp->label); | |
1209 | ||
e1fc2c43 YW |
1210 | break; |
1211 | ||
1212 | case AF_INET6: | |
fe841414 YW |
1213 | r = sd_netlink_message_read_in6_addr(message, IFA_LOCAL, &tmp->in_addr.in6); |
1214 | if (r >= 0) { | |
1215 | /* Have peer address. */ | |
1216 | r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr_peer.in6); | |
1217 | if (r < 0) { | |
1218 | log_link_warning_errno(link, r, "rtnl: could not get peer address from address message, ignoring: %m"); | |
1219 | return 0; | |
1220 | } | |
fe841414 YW |
1221 | } else if (r == -ENODATA) { |
1222 | /* Does not have peer address. */ | |
1223 | r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &tmp->in_addr.in6); | |
1224 | if (r < 0) { | |
1225 | log_link_warning_errno(link, r, "rtnl: received address message without valid address, ignoring: %m"); | |
1226 | return 0; | |
1227 | } | |
1228 | } else { | |
1229 | log_link_warning_errno(link, r, "rtnl: could not get local address from address message, ignoring: %m"); | |
e1fc2c43 YW |
1230 | return 0; |
1231 | } | |
1232 | ||
1233 | break; | |
1234 | ||
1235 | default: | |
1236 | assert_not_reached("Received unsupported address family"); | |
1237 | } | |
1238 | ||
fe841414 | 1239 | r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &tmp->cinfo); |
e1fc2c43 YW |
1240 | if (r < 0 && r != -ENODATA) { |
1241 | log_link_warning_errno(link, r, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m"); | |
1242 | return 0; | |
24f50382 | 1243 | } |
e1fc2c43 | 1244 | |
fe841414 | 1245 | (void) address_get(link, tmp, &address); |
e1fc2c43 YW |
1246 | |
1247 | switch (type) { | |
1248 | case RTM_NEWADDR: | |
24f50382 YW |
1249 | log_address_debug(tmp, address ? "Remembering updated" : "Remembering foreign", link); |
1250 | if (!address) { | |
e1fc2c43 | 1251 | /* An address appeared that we did not request */ |
fe841414 | 1252 | r = address_add_foreign(link, tmp, &address); |
e1fc2c43 | 1253 | if (r < 0) { |
24f50382 YW |
1254 | _cleanup_free_ char *buf = NULL; |
1255 | ||
1256 | (void) in_addr_to_string(tmp->family, &tmp->in_addr, &buf); | |
e1fc2c43 | 1257 | log_link_warning_errno(link, r, "Failed to remember foreign address %s/%u, ignoring: %m", |
fe841414 | 1258 | strnull(buf), tmp->prefixlen); |
e1fc2c43 | 1259 | return 0; |
24f50382 | 1260 | } |
e1fc2c43 YW |
1261 | } |
1262 | ||
1263 | /* address_update() logs internally, so we don't need to here. */ | |
fe841414 | 1264 | r = address_update(address, tmp); |
e1fc2c43 YW |
1265 | if (r < 0) |
1266 | link_enter_failed(link); | |
1267 | ||
1268 | break; | |
1269 | ||
1270 | case RTM_DELADDR: | |
24f50382 | 1271 | log_address_debug(tmp, address ? "Forgetting" : "Kernel removed unknown", link); |
1db01a33 | 1272 | (void) address_drop(address); |
e1fc2c43 YW |
1273 | |
1274 | break; | |
1275 | ||
1276 | default: | |
1277 | assert_not_reached("Received invalid RTNL message type"); | |
1278 | } | |
1279 | ||
1280 | return 1; | |
1281 | } | |
1282 | ||
051e77ca SS |
1283 | static void static_address_on_acd(sd_ipv4acd *acd, int event, void *userdata) { |
1284 | _cleanup_free_ char *pretty = NULL; | |
1285 | Address *address; | |
1286 | Link *link; | |
1287 | int r; | |
1288 | ||
1289 | assert(acd); | |
1290 | assert(userdata); | |
1291 | ||
1292 | address = (Address *) userdata; | |
1293 | link = address->link; | |
1294 | ||
1295 | (void) in_addr_to_string(address->family, &address->in_addr, &pretty); | |
1296 | switch (event) { | |
1297 | case SD_IPV4ACD_EVENT_STOP: | |
1298 | log_link_debug(link, "Stopping ACD client..."); | |
1299 | return; | |
1300 | ||
1301 | case SD_IPV4ACD_EVENT_BIND: | |
1302 | log_link_debug(link, "Successfully claimed address %s", strna(pretty)); | |
1303 | link_check_ready(link); | |
1304 | break; | |
1305 | ||
1306 | case SD_IPV4ACD_EVENT_CONFLICT: | |
1307 | log_link_warning(link, "DAD conflict. Dropping address %s", strna(pretty)); | |
1308 | r = address_remove(address, link, NULL); | |
1309 | if (r < 0) | |
1310 | log_link_error_errno(link, r, "Failed to drop DAD conflicted address %s", strna(pretty));; | |
1311 | ||
1312 | link_check_ready(link); | |
1313 | break; | |
1314 | ||
1315 | default: | |
1316 | assert_not_reached("Invalid IPv4ACD event."); | |
1317 | } | |
1318 | ||
c6a7531e | 1319 | (void) sd_ipv4acd_stop(acd); |
051e77ca SS |
1320 | |
1321 | return; | |
1322 | } | |
1323 | ||
490ccbd5 | 1324 | static int ipv4_dad_configure(Address *address) { |
051e77ca SS |
1325 | int r; |
1326 | ||
051e77ca | 1327 | assert(address); |
490ccbd5 | 1328 | assert(address->link); |
051e77ca | 1329 | |
490ccbd5 YW |
1330 | if (address->family != AF_INET) |
1331 | return 0; | |
051e77ca | 1332 | |
490ccbd5 YW |
1333 | if (DEBUG_LOGGING) { |
1334 | _cleanup_free_ char *pretty = NULL; | |
051e77ca | 1335 | |
490ccbd5 YW |
1336 | (void) in_addr_to_string(address->family, &address->in_addr, &pretty); |
1337 | log_link_debug(address->link, "Starting IPv4ACD client. Probing address %s", strna(pretty)); | |
1338 | } | |
1339 | ||
1340 | if (!address->acd) { | |
1341 | r = sd_ipv4acd_new(&address->acd); | |
1342 | if (r < 0) | |
1343 | return r; | |
1344 | ||
1345 | r = sd_ipv4acd_attach_event(address->acd, address->link->manager->event, 0); | |
1346 | if (r < 0) | |
1347 | return r; | |
1348 | } | |
051e77ca | 1349 | |
490ccbd5 | 1350 | r = sd_ipv4acd_set_ifindex(address->acd, address->link->ifindex); |
051e77ca SS |
1351 | if (r < 0) |
1352 | return r; | |
1353 | ||
b8162cd2 | 1354 | r = sd_ipv4acd_set_mac(address->acd, &address->link->hw_addr.addr.ether); |
051e77ca SS |
1355 | if (r < 0) |
1356 | return r; | |
1357 | ||
1358 | r = sd_ipv4acd_set_address(address->acd, &address->in_addr.in); | |
1359 | if (r < 0) | |
1360 | return r; | |
1361 | ||
1362 | r = sd_ipv4acd_set_callback(address->acd, static_address_on_acd, address); | |
1363 | if (r < 0) | |
1364 | return r; | |
1365 | ||
490ccbd5 | 1366 | return sd_ipv4acd_start(address->acd, true); |
051e77ca SS |
1367 | } |
1368 | ||
d93d655c YW |
1369 | static int ipv4_dad_update_mac_one(Address *address) { |
1370 | bool running; | |
1371 | int r; | |
1372 | ||
1373 | assert(address); | |
1374 | ||
1375 | if (!address->acd) | |
1376 | return 0; | |
1377 | ||
1378 | running = sd_ipv4acd_is_running(address->acd); | |
1379 | ||
a391901e YW |
1380 | r = sd_ipv4acd_stop(address->acd); |
1381 | if (r < 0) | |
1382 | return r; | |
d93d655c | 1383 | |
b8162cd2 | 1384 | r = sd_ipv4acd_set_mac(address->acd, &address->link->hw_addr.addr.ether); |
d93d655c YW |
1385 | if (r < 0) |
1386 | return r; | |
1387 | ||
1388 | if (running) { | |
1389 | r = sd_ipv4acd_start(address->acd, true); | |
1390 | if (r < 0) | |
1391 | return r; | |
1392 | } | |
1393 | ||
1394 | return 0; | |
1395 | } | |
1396 | ||
1397 | int ipv4_dad_update_mac(Link *link) { | |
1398 | Address *address; | |
1399 | int k, r = 0; | |
1400 | ||
1401 | assert(link); | |
1402 | ||
1403 | SET_FOREACH(address, link->addresses) { | |
1404 | k = ipv4_dad_update_mac_one(address); | |
1405 | if (k < 0 && r >= 0) | |
1406 | r = k; | |
1407 | } | |
1408 | ||
1409 | return r; | |
1410 | } | |
1411 | ||
490ccbd5 | 1412 | int ipv4_dad_stop(Link *link) { |
2488e4d9 YW |
1413 | Address *address; |
1414 | int k, r = 0; | |
1415 | ||
1416 | assert(link); | |
1417 | ||
490ccbd5 | 1418 | SET_FOREACH(address, link->addresses) { |
2488e4d9 YW |
1419 | k = sd_ipv4acd_stop(address->acd); |
1420 | if (k < 0 && r >= 0) | |
1421 | r = k; | |
1422 | } | |
1423 | ||
1424 | return r; | |
1425 | } | |
1426 | ||
7abe175c YW |
1427 | void ipv4_dad_unref(Link *link) { |
1428 | Address *address; | |
1429 | ||
1430 | assert(link); | |
1431 | ||
1432 | SET_FOREACH(address, link->addresses) | |
1433 | address->acd = sd_ipv4acd_unref(address->acd); | |
1434 | } | |
1435 | ||
44e7b949 LP |
1436 | int config_parse_broadcast( |
1437 | const char *unit, | |
eb0ea358 TG |
1438 | const char *filename, |
1439 | unsigned line, | |
1440 | const char *section, | |
1441 | unsigned section_line, | |
1442 | const char *lvalue, | |
1443 | int ltype, | |
1444 | const char *rvalue, | |
1445 | void *data, | |
1446 | void *userdata) { | |
44e7b949 | 1447 | |
eb0ea358 | 1448 | Network *network = userdata; |
fcbf4cb7 | 1449 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; |
eb0ea358 TG |
1450 | int r; |
1451 | ||
1452 | assert(filename); | |
1453 | assert(section); | |
1454 | assert(lvalue); | |
1455 | assert(rvalue); | |
1456 | assert(data); | |
1457 | ||
f4859fc7 | 1458 | r = address_new_static(network, filename, section_line, &n); |
d96edb2c YW |
1459 | if (r == -ENOMEM) |
1460 | return log_oom(); | |
1461 | if (r < 0) { | |
1462 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1463 | "Failed to allocate new address, ignoring assignment: %m"); | |
1464 | return 0; | |
1465 | } | |
eb0ea358 | 1466 | |
482e2ac1 | 1467 | if (n->family == AF_INET6) { |
d96edb2c | 1468 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
2850cd40 | 1469 | "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue); |
482e2ac1 TG |
1470 | return 0; |
1471 | } | |
1472 | ||
44e7b949 | 1473 | r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast); |
eb0ea358 | 1474 | if (r < 0) { |
d96edb2c | 1475 | log_syntax(unit, LOG_WARNING, filename, line, r, |
2850cd40 | 1476 | "Broadcast is invalid, ignoring assignment: %s", rvalue); |
eb0ea358 TG |
1477 | return 0; |
1478 | } | |
1479 | ||
44e7b949 | 1480 | n->family = AF_INET; |
dea161d9 | 1481 | TAKE_PTR(n); |
eb0ea358 TG |
1482 | |
1483 | return 0; | |
1484 | } | |
1485 | ||
f5ee7d74 YW |
1486 | int config_parse_address( |
1487 | const char *unit, | |
f579559b TG |
1488 | const char *filename, |
1489 | unsigned line, | |
1490 | const char *section, | |
71a61510 | 1491 | unsigned section_line, |
f579559b TG |
1492 | const char *lvalue, |
1493 | int ltype, | |
1494 | const char *rvalue, | |
1495 | void *data, | |
1496 | void *userdata) { | |
44e7b949 | 1497 | |
6ae115c1 | 1498 | Network *network = userdata; |
fcbf4cb7 | 1499 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; |
44e7b949 | 1500 | union in_addr_union buffer; |
b7cb4452 | 1501 | unsigned char prefixlen; |
44e7b949 | 1502 | int r, f; |
f579559b TG |
1503 | |
1504 | assert(filename); | |
6ae115c1 | 1505 | assert(section); |
f579559b TG |
1506 | assert(lvalue); |
1507 | assert(rvalue); | |
1508 | assert(data); | |
1509 | ||
9cd9fc8f YW |
1510 | if (streq(section, "Network")) |
1511 | /* we are not in an Address section, so use line number instead. */ | |
1512 | r = address_new_static(network, filename, line, &n); | |
1513 | else | |
f4859fc7 | 1514 | r = address_new_static(network, filename, section_line, &n); |
d96edb2c YW |
1515 | if (r == -ENOMEM) |
1516 | return log_oom(); | |
1517 | if (r < 0) { | |
1518 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1519 | "Failed to allocate new address, ignoring assignment: %m"); | |
1520 | return 0; | |
1521 | } | |
f579559b TG |
1522 | |
1523 | /* Address=address/prefixlen */ | |
0f707207 YW |
1524 | r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_REFUSE, &f, &buffer, &prefixlen); |
1525 | if (r == -ENOANO) { | |
d96edb2c | 1526 | log_syntax(unit, LOG_WARNING, filename, line, r, |
0f707207 YW |
1527 | "An address '%s' is specified without prefix length. " |
1528 | "The behavior of parsing addresses without prefix length will be changed in the future release. " | |
1529 | "Please specify prefix length explicitly.", rvalue); | |
1530 | ||
1531 | r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_LEGACY, &f, &buffer, &prefixlen); | |
1532 | } | |
f579559b | 1533 | if (r < 0) { |
d96edb2c | 1534 | log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid address '%s', ignoring assignment: %m", rvalue); |
f579559b TG |
1535 | return 0; |
1536 | } | |
1537 | ||
44e7b949 | 1538 | if (n->family != AF_UNSPEC && f != n->family) { |
d96edb2c | 1539 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue); |
44e7b949 LP |
1540 | return 0; |
1541 | } | |
1542 | ||
c9207ff3 YW |
1543 | if (in_addr_is_null(f, &buffer)) { |
1544 | /* Will use address from address pool. Note that for ipv6 case, prefix of the address | |
1545 | * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So, | |
1546 | * let's limit the prefix length to 64 or larger. See RFC4193. */ | |
1547 | if ((f == AF_INET && prefixlen < 8) || | |
1548 | (f == AF_INET6 && prefixlen < 64)) { | |
d96edb2c | 1549 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
c9207ff3 YW |
1550 | "Null address with invalid prefixlen='%u', ignoring assignment: %s", |
1551 | prefixlen, rvalue); | |
1552 | return 0; | |
1553 | } | |
1554 | } | |
1555 | ||
44e7b949 | 1556 | n->family = f; |
b7cb4452 | 1557 | n->prefixlen = prefixlen; |
44e7b949 LP |
1558 | |
1559 | if (streq(lvalue, "Address")) | |
1560 | n->in_addr = buffer; | |
1561 | else | |
1562 | n->in_addr_peer = buffer; | |
1563 | ||
dea161d9 | 1564 | TAKE_PTR(n); |
f579559b TG |
1565 | return 0; |
1566 | } | |
6ae115c1 | 1567 | |
d31645ad LP |
1568 | int config_parse_label( |
1569 | const char *unit, | |
6ae115c1 TG |
1570 | const char *filename, |
1571 | unsigned line, | |
1572 | const char *section, | |
1573 | unsigned section_line, | |
1574 | const char *lvalue, | |
1575 | int ltype, | |
1576 | const char *rvalue, | |
1577 | void *data, | |
1578 | void *userdata) { | |
d31645ad | 1579 | |
fcbf4cb7 | 1580 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; |
d31645ad | 1581 | Network *network = userdata; |
6ae115c1 TG |
1582 | int r; |
1583 | ||
1584 | assert(filename); | |
1585 | assert(section); | |
1586 | assert(lvalue); | |
1587 | assert(rvalue); | |
1588 | assert(data); | |
1589 | ||
f4859fc7 | 1590 | r = address_new_static(network, filename, section_line, &n); |
d96edb2c YW |
1591 | if (r == -ENOMEM) |
1592 | return log_oom(); | |
1593 | if (r < 0) { | |
1594 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1595 | "Failed to allocate new address, ignoring assignment: %m"); | |
1596 | return 0; | |
1597 | } | |
6ae115c1 | 1598 | |
a87d19fe | 1599 | if (!address_label_valid(rvalue)) { |
d96edb2c | 1600 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
2850cd40 | 1601 | "Interface label is too long or invalid, ignoring assignment: %s", rvalue); |
6ae115c1 TG |
1602 | return 0; |
1603 | } | |
1604 | ||
d31645ad LP |
1605 | r = free_and_strdup(&n->label, rvalue); |
1606 | if (r < 0) | |
1607 | return log_oom(); | |
6ae115c1 | 1608 | |
dea161d9 | 1609 | TAKE_PTR(n); |
6ae115c1 TG |
1610 | return 0; |
1611 | } | |
ce6c77eb | 1612 | |
f5ee7d74 YW |
1613 | int config_parse_lifetime( |
1614 | const char *unit, | |
1615 | const char *filename, | |
1616 | unsigned line, | |
1617 | const char *section, | |
1618 | unsigned section_line, | |
1619 | const char *lvalue, | |
1620 | int ltype, | |
1621 | const char *rvalue, | |
1622 | void *data, | |
1623 | void *userdata) { | |
1624 | ||
b5834a0b | 1625 | Network *network = userdata; |
fcbf4cb7 | 1626 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; |
d2735796 | 1627 | uint32_t k; |
b5834a0b SS |
1628 | int r; |
1629 | ||
1630 | assert(filename); | |
1631 | assert(section); | |
1632 | assert(lvalue); | |
1633 | assert(rvalue); | |
1634 | assert(data); | |
1635 | ||
f4859fc7 | 1636 | r = address_new_static(network, filename, section_line, &n); |
d96edb2c YW |
1637 | if (r == -ENOMEM) |
1638 | return log_oom(); | |
1639 | if (r < 0) { | |
1640 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1641 | "Failed to allocate new address, ignoring assignment: %m"); | |
1642 | return 0; | |
1643 | } | |
b5834a0b | 1644 | |
10b20e5a ZJS |
1645 | /* We accept only "forever", "infinity", empty, or "0". */ |
1646 | if (STR_IN_SET(rvalue, "forever", "infinity", "")) | |
33680b0a YW |
1647 | k = CACHE_INFO_INFINITY_LIFE_TIME; |
1648 | else if (streq(rvalue, "0")) | |
1649 | k = 0; | |
1650 | else { | |
d96edb2c | 1651 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
33680b0a | 1652 | "Invalid PreferredLifetime= value, ignoring: %s", rvalue); |
b5834a0b SS |
1653 | return 0; |
1654 | } | |
1655 | ||
33680b0a | 1656 | n->cinfo.ifa_prefered = k; |
d2735796 | 1657 | TAKE_PTR(n); |
b5834a0b SS |
1658 | |
1659 | return 0; | |
1660 | } | |
1661 | ||
f5ee7d74 YW |
1662 | int config_parse_address_flags( |
1663 | const char *unit, | |
1664 | const char *filename, | |
1665 | unsigned line, | |
1666 | const char *section, | |
1667 | unsigned section_line, | |
1668 | const char *lvalue, | |
1669 | int ltype, | |
1670 | const char *rvalue, | |
1671 | void *data, | |
1672 | void *userdata) { | |
1673 | ||
e63be084 | 1674 | Network *network = userdata; |
fcbf4cb7 | 1675 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; |
e63be084 SS |
1676 | int r; |
1677 | ||
1678 | assert(filename); | |
1679 | assert(section); | |
1680 | assert(lvalue); | |
1681 | assert(rvalue); | |
1682 | assert(data); | |
1683 | ||
f4859fc7 | 1684 | r = address_new_static(network, filename, section_line, &n); |
d96edb2c YW |
1685 | if (r == -ENOMEM) |
1686 | return log_oom(); | |
1687 | if (r < 0) { | |
1688 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1689 | "Failed to allocate new address, ignoring assignment: %m"); | |
1690 | return 0; | |
1691 | } | |
e63be084 SS |
1692 | |
1693 | r = parse_boolean(rvalue); | |
1694 | if (r < 0) { | |
d96edb2c | 1695 | log_syntax(unit, LOG_WARNING, filename, line, r, |
051e77ca | 1696 | "Failed to parse %s=, ignoring: %s", lvalue, rvalue); |
e63be084 SS |
1697 | return 0; |
1698 | } | |
1699 | ||
eaff204f YW |
1700 | if (streq(lvalue, "AddPrefixRoute")) |
1701 | r = !r; | |
1702 | ||
1703 | SET_FLAG(n->flags, ltype, r); | |
e63be084 | 1704 | |
dea161d9 | 1705 | TAKE_PTR(n); |
e63be084 SS |
1706 | return 0; |
1707 | } | |
1708 | ||
f5ee7d74 YW |
1709 | int config_parse_address_scope( |
1710 | const char *unit, | |
1711 | const char *filename, | |
1712 | unsigned line, | |
1713 | const char *section, | |
1714 | unsigned section_line, | |
1715 | const char *lvalue, | |
1716 | int ltype, | |
1717 | const char *rvalue, | |
1718 | void *data, | |
1719 | void *userdata) { | |
1720 | ||
2959fb07 | 1721 | Network *network = userdata; |
fcbf4cb7 | 1722 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; |
2959fb07 SS |
1723 | int r; |
1724 | ||
1725 | assert(filename); | |
1726 | assert(section); | |
1727 | assert(lvalue); | |
1728 | assert(rvalue); | |
1729 | assert(data); | |
1730 | ||
1731 | r = address_new_static(network, filename, section_line, &n); | |
d96edb2c YW |
1732 | if (r == -ENOMEM) |
1733 | return log_oom(); | |
1734 | if (r < 0) { | |
1735 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1736 | "Failed to allocate new address, ignoring assignment: %m"); | |
1737 | return 0; | |
1738 | } | |
2959fb07 SS |
1739 | |
1740 | if (streq(rvalue, "host")) | |
1741 | n->scope = RT_SCOPE_HOST; | |
1742 | else if (streq(rvalue, "link")) | |
1743 | n->scope = RT_SCOPE_LINK; | |
1744 | else if (streq(rvalue, "global")) | |
1745 | n->scope = RT_SCOPE_UNIVERSE; | |
1746 | else { | |
1747 | r = safe_atou8(rvalue , &n->scope); | |
1748 | if (r < 0) { | |
d96edb2c | 1749 | log_syntax(unit, LOG_WARNING, filename, line, r, |
2850cd40 | 1750 | "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue); |
2959fb07 SS |
1751 | return 0; |
1752 | } | |
1753 | } | |
1754 | ||
07336a06 | 1755 | n->scope_set = true; |
dea161d9 | 1756 | TAKE_PTR(n); |
2959fb07 SS |
1757 | return 0; |
1758 | } | |
1759 | ||
051e77ca SS |
1760 | int config_parse_duplicate_address_detection( |
1761 | const char *unit, | |
1762 | const char *filename, | |
1763 | unsigned line, | |
1764 | const char *section, | |
1765 | unsigned section_line, | |
1766 | const char *lvalue, | |
1767 | int ltype, | |
1768 | const char *rvalue, | |
1769 | void *data, | |
1770 | void *userdata) { | |
f5ee7d74 | 1771 | |
051e77ca SS |
1772 | Network *network = userdata; |
1773 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; | |
051e77ca SS |
1774 | int r; |
1775 | ||
1776 | assert(filename); | |
1777 | assert(section); | |
1778 | assert(lvalue); | |
1779 | assert(rvalue); | |
1780 | assert(data); | |
1781 | ||
1782 | r = address_new_static(network, filename, section_line, &n); | |
d96edb2c YW |
1783 | if (r == -ENOMEM) |
1784 | return log_oom(); | |
1785 | if (r < 0) { | |
1786 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1787 | "Failed to allocate new address, ignoring assignment: %m"); | |
1788 | return 0; | |
1789 | } | |
051e77ca SS |
1790 | |
1791 | r = parse_boolean(rvalue); | |
1792 | if (r >= 0) { | |
1793 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1794 | "For historical reasons, %s=%s means %s=%s. " | |
1795 | "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.", | |
1796 | lvalue, rvalue, lvalue, r ? "none" : "both"); | |
1797 | n->duplicate_address_detection = r ? ADDRESS_FAMILY_NO : ADDRESS_FAMILY_YES; | |
1798 | n = NULL; | |
1799 | return 0; | |
1800 | } | |
1801 | ||
7211c853 | 1802 | AddressFamily a = duplicate_address_detection_address_family_from_string(rvalue); |
051e77ca | 1803 | if (a < 0) { |
7211c853 | 1804 | log_syntax(unit, LOG_WARNING, filename, line, a, |
051e77ca SS |
1805 | "Failed to parse %s=, ignoring: %s", lvalue, rvalue); |
1806 | return 0; | |
1807 | } | |
051e77ca | 1808 | n->duplicate_address_detection = a; |
7211c853 | 1809 | |
dea161d9 | 1810 | TAKE_PTR(n); |
051e77ca SS |
1811 | return 0; |
1812 | } | |
1813 | ||
ce6c77eb TG |
1814 | bool address_is_ready(const Address *a) { |
1815 | assert(a); | |
1816 | ||
ce158189 | 1817 | return !(a->flags & IFA_F_TENTATIVE); |
ce6c77eb | 1818 | } |
fcbf4cb7 | 1819 | |
32400c2f | 1820 | static int address_section_verify(Address *address) { |
fcbf4cb7 YW |
1821 | if (section_is_invalid(address->section)) |
1822 | return -EINVAL; | |
1823 | ||
1824 | if (address->family == AF_UNSPEC) { | |
1825 | assert(address->section); | |
1826 | ||
1827 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), | |
1828 | "%s: Address section without Address= field configured. " | |
1829 | "Ignoring [Address] section from line %u.", | |
1830 | address->section->filename, address->section->line); | |
1831 | } | |
1832 | ||
2a236f9f | 1833 | if (address_may_have_broadcast(address)) { |
05a7023d YW |
1834 | if (address->broadcast.s_addr == 0) |
1835 | address->broadcast.s_addr = address->in_addr.in.s_addr | htobe32(0xfffffffflu >> address->prefixlen); | |
1836 | } else if (address->broadcast.s_addr != 0) { | |
fe841414 YW |
1837 | log_warning("%s: broadcast address is set for IPv6 address or IPv4 address with prefixlength larger than 30. " |
1838 | "Ignoring Broadcast= setting in the [Address] section from line %u.", | |
1839 | address->section->filename, address->section->line); | |
1840 | ||
1841 | address->broadcast.s_addr = 0; | |
1842 | } | |
1843 | ||
1844 | if (address->family == AF_INET6 && address->label) { | |
1845 | log_warning("%s: address label is set for IPv6 address in the [Address] section from line %u. " | |
1846 | "Ignoring Label= setting.", | |
1847 | address->section->filename, address->section->line); | |
1848 | ||
1849 | address->label = mfree(address->label); | |
1850 | } | |
1851 | ||
cd1caf30 YW |
1852 | if (in_addr_is_localhost(address->family, &address->in_addr) > 0 && |
1853 | (address->family == AF_INET || !address->scope_set)) { | |
1854 | /* For IPv4, scope must be always RT_SCOPE_HOST. | |
1855 | * For IPv6, use RT_SCOPE_HOST only when it is not explicitly specified. */ | |
1856 | ||
1857 | if (address->scope_set && address->scope != RT_SCOPE_HOST) | |
1858 | log_warning_errno(SYNTHETIC_ERRNO(EINVAL), | |
1859 | "%s: non-host scope is set in the [Address] section from line %u. " | |
1860 | "Ignoring Scope= setting.", | |
1861 | address->section->filename, address->section->line); | |
1862 | ||
07336a06 | 1863 | address->scope = RT_SCOPE_HOST; |
cd1caf30 | 1864 | } |
07336a06 | 1865 | |
eaff204f YW |
1866 | if (!FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV6)) |
1867 | address->flags |= IFA_F_NODAD; | |
1868 | ||
fcbf4cb7 YW |
1869 | return 0; |
1870 | } | |
32400c2f | 1871 | |
13ffa39f | 1872 | void network_drop_invalid_addresses(Network *network) { |
9cd9fc8f | 1873 | Address *address; |
32400c2f YW |
1874 | |
1875 | assert(network); | |
1876 | ||
9cd9fc8f | 1877 | ORDERED_HASHMAP_FOREACH(address, network->addresses_by_section) |
32400c2f YW |
1878 | if (address_section_verify(address) < 0) |
1879 | address_free(address); | |
1880 | } |