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