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