]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-address.c
basic/set: add set_ensure_put()
[thirdparty/systemd.git] / src / network / networkd-address.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f579559b
TG
2
3#include <net/if.h>
4
b5efdb8a 5#include "alloc-util.h"
f579559b 6#include "conf-parser.h"
12c2884c 7#include "firewall-util.h"
0a970718 8#include "memory-util.h"
081aea25 9#include "missing_network.h"
fc2f9534 10#include "netlink-util.h"
6bedfcbb 11#include "networkd-address.h"
23f53b99 12#include "networkd-manager.h"
6bedfcbb 13#include "parse-util.h"
3ac8e543 14#include "set.h"
d31645ad 15#include "socket-util.h"
07630cea 16#include "string-util.h"
51517f9e 17#include "strv.h"
3ac8e543 18#include "utf8.h"
f579559b 19
1b566071 20#define ADDRESSES_PER_LINK_MAX 2048U
8c34b963
LP
21#define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
22
5ead5352
SS
23int generate_ipv6_eui_64_address(Link *link, struct in6_addr *ret) {
24 assert(link);
25 assert(ret);
26
27 /* see RFC4291 section 2.5.1 */
28 ret->s6_addr[8] = link->mac.ether_addr_octet[0];
29 ret->s6_addr[8] ^= 1 << 1;
30 ret->s6_addr[9] = link->mac.ether_addr_octet[1];
31 ret->s6_addr[10] = link->mac.ether_addr_octet[2];
32 ret->s6_addr[11] = 0xff;
33 ret->s6_addr[12] = 0xfe;
34 ret->s6_addr[13] = link->mac.ether_addr_octet[3];
35 ret->s6_addr[14] = link->mac.ether_addr_octet[4];
36 ret->s6_addr[15] = link->mac.ether_addr_octet[5];
37
38 return 0;
39}
40
f0213e37 41int address_new(Address **ret) {
8e766630 42 _cleanup_(address_freep) Address *address = NULL;
f0213e37 43
17f9c355 44 address = new(Address, 1);
f0213e37
TG
45 if (!address)
46 return -ENOMEM;
aba496a5 47
17f9c355
YW
48 *address = (Address) {
49 .family = AF_UNSPEC,
50 .scope = RT_SCOPE_UNIVERSE,
51 .cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME,
52 .cinfo.ifa_valid = CACHE_INFO_INFINITY_LIFE_TIME,
051e77ca 53 .duplicate_address_detection = ADDRESS_FAMILY_IPV6,
de697db0 54 .prefix_route = true,
17f9c355 55 };
f0213e37 56
1cc6c93a 57 *ret = TAKE_PTR(address);
f0213e37
TG
58
59 return 0;
aba496a5
UTL
60}
61
9560e5b3 62static int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) {
8e766630
LP
63 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
64 _cleanup_(address_freep) Address *address = NULL;
f0213e37 65 int r;
f579559b 66
8c34b963
LP
67 assert(network);
68 assert(ret);
48317c39 69 assert(!!filename == (section_line > 0));
8c34b963 70
48317c39 71 if (filename) {
f4859fc7
SS
72 r = network_config_section_new(filename, section_line, &n);
73 if (r < 0)
74 return r;
75
76 address = hashmap_get(network->addresses_by_section, n);
6ae115c1 77 if (address) {
1cc6c93a 78 *ret = TAKE_PTR(address);
6ae115c1
TG
79
80 return 0;
81 }
82 }
83
8c34b963
LP
84 if (network->n_static_addresses >= STATIC_ADDRESSES_PER_NETWORK_MAX)
85 return -E2BIG;
86
f0213e37
TG
87 r = address_new(&address);
88 if (r < 0)
89 return r;
801bd9e8 90
0f7f2769
YW
91 address->network = network;
92 LIST_APPEND(addresses, network->static_addresses, address);
93 network->n_static_addresses++;
94
48317c39 95 if (filename) {
1cc6c93a 96 address->section = TAKE_PTR(n);
fcc48287 97
3e570042
YW
98 r = hashmap_ensure_allocated(&network->addresses_by_section, &network_config_hash_ops);
99 if (r < 0)
100 return r;
101
f7fe70ea
SS
102 r = hashmap_put(network->addresses_by_section, address->section, address);
103 if (r < 0)
104 return r;
6ae115c1
TG
105 }
106
1cc6c93a 107 *ret = TAKE_PTR(address);
f579559b
TG
108
109 return 0;
110}
111
112void address_free(Address *address) {
113 if (!address)
114 return;
115
f048a16b 116 if (address->network) {
3d3d4255 117 LIST_REMOVE(addresses, address->network->static_addresses, address);
8c34b963
LP
118 assert(address->network->n_static_addresses > 0);
119 address->network->n_static_addresses--;
f579559b 120
de4224aa 121 if (address->section)
f4859fc7 122 hashmap_remove(address->network->addresses_by_section, address->section);
f048a16b 123 }
6ae115c1 124
051e77ca 125 if (address->link && !address->acd) {
cf1d700d 126 set_remove(address->link->addresses, address);
adda1ed9 127 set_remove(address->link->addresses_foreign, address);
f150100a
SS
128
129 if (in_addr_equal(AF_INET6, &address->in_addr, (const union in_addr_union *) &address->link->ipv6ll_address))
130 memzero(&address->link->ipv6ll_address, sizeof(struct in6_addr));
adda1ed9 131 }
cf1d700d 132
051e77ca
SS
133 sd_ipv4acd_unref(address->acd);
134
de4224aa
YW
135 network_config_section_free(address->section);
136 free(address->label);
f579559b
TG
137 free(address);
138}
139
24be9181
ZJS
140static uint32_t address_prefix(const Address *a) {
141 assert(a);
142
143 /* make sure we don't try to shift by 32.
144 * See ISO/IEC 9899:TC3 § 6.5.7.3. */
145 if (a->prefixlen == 0)
146 return 0;
147
148 if (a->in_addr_peer.in.s_addr != 0)
149 return be32toh(a->in_addr_peer.in.s_addr) >> (32 - a->prefixlen);
150 else
151 return be32toh(a->in_addr.in.s_addr) >> (32 - a->prefixlen);
152}
153
7a08d314 154static void address_hash_func(const Address *a, struct siphash *state) {
3ac8e543
TG
155 assert(a);
156
157 siphash24_compress(&a->family, sizeof(a->family), state);
158
159 switch (a->family) {
160 case AF_INET:
161 siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
162
163 /* peer prefix */
24be9181
ZJS
164 uint32_t prefix = address_prefix(a);
165 siphash24_compress(&prefix, sizeof(prefix), state);
3ac8e543 166
4831981d 167 _fallthrough_;
3ac8e543
TG
168 case AF_INET6:
169 /* local address */
170 siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state);
171
172 break;
173 default:
174 /* treat any other address family as AF_UNSPEC */
175 break;
176 }
177}
178
7a08d314 179static int address_compare_func(const Address *a1, const Address *a2) {
a0edd02e 180 int r;
3ac8e543 181
a0edd02e
FB
182 r = CMP(a1->family, a2->family);
183 if (r != 0)
184 return r;
3ac8e543
TG
185
186 switch (a1->family) {
187 /* use the same notion of equality as the kernel does */
188 case AF_INET:
a0edd02e
FB
189 r = CMP(a1->prefixlen, a2->prefixlen);
190 if (r != 0)
191 return r;
3ac8e543 192
24be9181
ZJS
193 uint32_t prefix1 = address_prefix(a1);
194 uint32_t prefix2 = address_prefix(a2);
195 r = CMP(prefix1, prefix2);
196 if (r != 0)
197 return r;
3ac8e543 198
4831981d 199 _fallthrough_;
3ac8e543
TG
200 case AF_INET6:
201 return memcmp(&a1->in_addr, &a2->in_addr, FAMILY_ADDRESS_SIZE(a1->family));
202 default:
203 /* treat any other address family as AF_UNSPEC */
204 return 0;
205 }
206}
207
8a98f11e 208DEFINE_HASH_OPS(address_hash_ops, Address, address_hash_func, address_compare_func);
3ac8e543
TG
209
210bool address_equal(Address *a1, Address *a2) {
211 if (a1 == a2)
212 return true;
213
214 if (!a1 || !a2)
215 return false;
216
217 return address_compare_func(a1, a2) == 0;
218}
219
91b5f997
TG
220static int address_establish(Address *address, Link *link) {
221 bool masq;
222 int r;
223
224 assert(address);
225 assert(link);
226
227 masq = link->network &&
fcf50cff
TG
228 link->network->ip_masquerade &&
229 address->family == AF_INET &&
230 address->scope < RT_SCOPE_LINK;
91b5f997
TG
231
232 /* Add firewall entry if this is requested */
233 if (address->ip_masquerade_done != masq) {
234 union in_addr_union masked = address->in_addr;
235 in_addr_mask(address->family, &masked, address->prefixlen);
236
237 r = fw_add_masquerade(masq, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0);
238 if (r < 0)
7750b796 239 return r;
91b5f997
TG
240
241 address->ip_masquerade_done = masq;
242 }
243
244 return 0;
245}
246
adda1ed9
TG
247static int address_add_internal(Link *link, Set **addresses,
248 int family,
249 const union in_addr_union *in_addr,
250 unsigned char prefixlen,
251 Address **ret) {
8e766630 252 _cleanup_(address_freep) Address *address = NULL;
cf1d700d
TG
253 int r;
254
255 assert(link);
adda1ed9 256 assert(addresses);
054f0db4 257 assert(in_addr);
054f0db4
TG
258
259 r = address_new(&address);
260 if (r < 0)
261 return r;
262
263 address->family = family;
264 address->in_addr = *in_addr;
265 address->prefixlen = prefixlen;
63bbe5c7
TG
266 /* Consider address tentative until we get the real flags from the kernel */
267 address->flags = IFA_F_TENTATIVE;
cf1d700d 268
adda1ed9 269 r = set_ensure_allocated(addresses, &address_hash_ops);
cf1d700d
TG
270 if (r < 0)
271 return r;
272
adda1ed9 273 r = set_put(*addresses, address);
cf1d700d
TG
274 if (r < 0)
275 return r;
75a302b5
YW
276 if (r == 0)
277 return -EEXIST;
cf1d700d
TG
278
279 address->link = link;
280
adda1ed9
TG
281 if (ret)
282 *ret = address;
283
054f0db4
TG
284 address = NULL;
285
cf1d700d
TG
286 return 0;
287}
288
adda1ed9
TG
289int address_add_foreign(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
290 return address_add_internal(link, &link->addresses_foreign, family, in_addr, prefixlen, ret);
291}
292
c4a03a56 293int address_add(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
cab974b0 294 Address *address;
e7780c8d
TG
295 int r;
296
cab974b0
TG
297 r = address_get(link, family, in_addr, prefixlen, &address);
298 if (r == -ENOENT) {
299 /* Address does not exist, create a new one */
300 r = address_add_internal(link, &link->addresses, family, in_addr, prefixlen, &address);
301 if (r < 0)
302 return r;
303 } else if (r == 0) {
304 /* Take over a foreign address */
305 r = set_ensure_allocated(&link->addresses, &address_hash_ops);
306 if (r < 0)
307 return r;
308
309 r = set_put(link->addresses, address);
310 if (r < 0)
311 return r;
312
313 set_remove(link->addresses_foreign, address);
314 } else if (r == 1) {
315 /* Already exists, do nothing */
316 ;
317 } else
e7780c8d
TG
318 return r;
319
cab974b0
TG
320 if (ret)
321 *ret = address;
e7780c8d
TG
322
323 return 0;
adda1ed9
TG
324}
325
fcf50cff 326static int address_release(Address *address) {
5a8bcb67
LP
327 int r;
328
329 assert(address);
fcf50cff 330 assert(address->link);
5a8bcb67 331
91b5f997
TG
332 /* Remove masquerading firewall entry if it was added */
333 if (address->ip_masquerade_done) {
5a8bcb67
LP
334 union in_addr_union masked = address->in_addr;
335 in_addr_mask(address->family, &masked, address->prefixlen);
336
91b5f997 337 r = fw_add_masquerade(false, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0);
5a8bcb67 338 if (r < 0)
7750b796 339 return r;
5a8bcb67 340
91b5f997 341 address->ip_masquerade_done = false;
5a8bcb67
LP
342 }
343
344 return 0;
345}
346
889b550f
LP
347int address_update(
348 Address *address,
349 unsigned char flags,
350 unsigned char scope,
351 const struct ifa_cacheinfo *cinfo) {
352
36c32f61 353 bool ready;
e7ab854c 354 int r;
36c32f61
TG
355
356 assert(address);
357 assert(cinfo);
7209086d
SS
358 assert_return(address->link, 1);
359
360 if (IN_SET(address->link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
361 return 1;
36c32f61
TG
362
363 ready = address_is_ready(address);
364
365 address->flags = flags;
366 address->scope = scope;
367 address->cinfo = *cinfo;
368
959f65d3 369 link_update_operstate(address->link, true);
c8f7123e 370 link_check_ready(address->link);
7209086d 371
c8f7123e
YW
372 if (!ready &&
373 address_is_ready(address) &&
374 address->family == AF_INET6 &&
375 in_addr_is_link_local(AF_INET6, &address->in_addr) > 0 &&
376 in_addr_is_null(AF_INET6, (const union in_addr_union*) &address->link->ipv6ll_address) > 0) {
7209086d 377
c8f7123e
YW
378 r = link_ipv6ll_gained(address->link, &address->in_addr.in6);
379 if (r < 0)
380 return r;
a3a019e1 381 }
36c32f61
TG
382
383 return 0;
384}
385
91b5f997 386int address_drop(Address *address) {
8012cd39
TG
387 Link *link;
388 bool ready;
7750b796 389 int r;
8012cd39 390
5a8bcb67 391 assert(address);
91b5f997 392
8012cd39
TG
393 ready = address_is_ready(address);
394 link = address->link;
395
7750b796
YW
396 r = address_release(address);
397 if (r < 0)
398 log_link_warning_errno(link, r, "Failed to disable IP masquerading, ignoring: %m");
399
91b5f997
TG
400 address_free(address);
401
959f65d3 402 link_update_operstate(link, true);
84de38c5 403
8012cd39
TG
404 if (link && !ready)
405 link_check_ready(link);
406
91b5f997
TG
407 return 0;
408}
409
1b566071
LP
410int address_get(Link *link,
411 int family,
412 const union in_addr_union *in_addr,
413 unsigned char prefixlen,
414 Address **ret) {
415
416 Address address, *existing;
91b5f997 417
5a8bcb67 418 assert(link);
91b5f997 419 assert(in_addr);
5a8bcb67 420
1b566071
LP
421 address = (Address) {
422 .family = family,
423 .in_addr = *in_addr,
424 .prefixlen = prefixlen,
425 };
5a8bcb67 426
91b5f997 427 existing = set_get(link->addresses, &address);
cab974b0 428 if (existing) {
1b566071
LP
429 if (ret)
430 *ret = existing;
cab974b0 431 return 1;
adda1ed9 432 }
5a8bcb67 433
1b566071
LP
434 existing = set_get(link->addresses_foreign, &address);
435 if (existing) {
436 if (ret)
437 *ret = existing;
438 return 0;
439 }
5a8bcb67 440
1b566071 441 return -ENOENT;
5a8bcb67
LP
442}
443
302a796f 444static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
63ae0569
YW
445 int r;
446
447 assert(m);
448 assert(link);
449 assert(link->ifname);
450
451 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
452 return 1;
453
454 r = sd_netlink_message_get_errno(m);
455 if (r < 0 && r != -EADDRNOTAVAIL)
5ecb131d 456 log_link_message_warning_errno(link, m, r, "Could not drop address");
63ae0569
YW
457
458 return 1;
459}
460
483d099e
ZJS
461int address_remove(
462 Address *address,
463 Link *link,
302a796f 464 link_netlink_message_handler_t callback) {
483d099e 465
4afd3348 466 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
407fe036
TG
467 int r;
468
469 assert(address);
4c701096 470 assert(IN_SET(address->family, AF_INET, AF_INET6));
407fe036
TG
471 assert(link);
472 assert(link->ifindex > 0);
473 assert(link->manager);
474 assert(link->manager->rtnl);
475
30226d27 476 if (DEBUG_LOGGING) {
7750b796
YW
477 _cleanup_free_ char *b = NULL;
478
479 (void) in_addr_to_string(address->family, &address->in_addr, &b);
480 log_link_debug(link, "Removing address %s", strna(b));
30226d27
TJ
481 }
482
151b9b96
LP
483 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR,
484 link->ifindex, address->family);
eb56eb9b 485 if (r < 0)
7750b796 486 return log_link_error_errno(link, r, "Could not allocate RTM_DELADDR message: %m");
407fe036 487
5a723174 488 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
eb56eb9b 489 if (r < 0)
7750b796 490 return log_link_error_errno(link, r, "Could not set prefixlen: %m");
5a723174 491
43409486 492 r = netlink_message_append_in_addr_union(req, IFA_LOCAL, address->family, &address->in_addr);
eb56eb9b 493 if (r < 0)
7750b796 494 return log_link_error_errno(link, r, "Could not append IFA_LOCAL attribute: %m");
407fe036 495
302a796f
YW
496 r = netlink_call_async(link->manager->rtnl, NULL, req,
497 callback ?: address_remove_handler,
498 link_netlink_destroy_callback, link);
eb56eb9b 499 if (r < 0)
7750b796 500 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
407fe036 501
563c69c6
TG
502 link_ref(link);
503
407fe036
TG
504 return 0;
505}
506
11bf3cce 507static int address_acquire(Link *link, Address *original, Address **ret) {
67a46833 508 union in_addr_union in_addr = IN_ADDR_NULL;
11bf3cce 509 struct in_addr broadcast = {};
8e766630 510 _cleanup_(address_freep) Address *na = NULL;
11bf3cce
LP
511 int r;
512
513 assert(link);
514 assert(original);
515 assert(ret);
516
517 /* Something useful was configured? just use it */
7e43ebfb
YW
518 r = in_addr_is_null(original->family, &original->in_addr);
519 if (r <= 0)
520 return r;
11bf3cce
LP
521
522 /* The address is configured to be 0.0.0.0 or [::] by the user?
523 * Then let's acquire something more useful from the pool. */
524 r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
6a7a4e4d 525 if (r < 0)
7750b796
YW
526 return r;
527 if (r == 0)
11bf3cce 528 return -EBUSY;
11bf3cce
LP
529
530 if (original->family == AF_INET) {
d076c6f9 531 /* Pick first address in range for ourselves ... */
11bf3cce
LP
532 in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1);
533
534 /* .. and use last as broadcast address */
e87e2b78
SS
535 if (original->prefixlen > 30)
536 broadcast.s_addr = 0;
537 else
538 broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen);
11bf3cce
LP
539 } else if (original->family == AF_INET6)
540 in_addr.in6.s6_addr[15] |= 1;
541
f0213e37 542 r = address_new(&na);
11bf3cce
LP
543 if (r < 0)
544 return r;
545
546 na->family = original->family;
547 na->prefixlen = original->prefixlen;
548 na->scope = original->scope;
549 na->cinfo = original->cinfo;
550
551 if (original->label) {
552 na->label = strdup(original->label);
0099bc15 553 if (!na->label)
11bf3cce 554 return -ENOMEM;
11bf3cce
LP
555 }
556
557 na->broadcast = broadcast;
558 na->in_addr = in_addr;
559
560 LIST_PREPEND(addresses, link->pool_addresses, na);
561
1cc6c93a 562 *ret = TAKE_PTR(na);
0099bc15 563
11bf3cce
LP
564 return 0;
565}
566
1b566071
LP
567int address_configure(
568 Address *address,
569 Link *link,
302a796f 570 link_netlink_message_handler_t callback,
1b566071
LP
571 bool update) {
572
4afd3348 573 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
f579559b
TG
574 int r;
575
c166a070 576 assert(address);
4c701096 577 assert(IN_SET(address->family, AF_INET, AF_INET6));
c166a070
TG
578 assert(link);
579 assert(link->ifindex > 0);
f882c247 580 assert(link->manager);
c166a070 581 assert(link->manager->rtnl);
bd1175bc 582 assert(callback);
f882c247 583
1b566071
LP
584 /* If this is a new address, then refuse adding more than the limit */
585 if (address_get(link, address->family, &address->in_addr, address->prefixlen, NULL) <= 0 &&
586 set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX)
7750b796
YW
587 return log_link_error_errno(link, SYNTHETIC_ERRNO(E2BIG),
588 "Too many addresses are configured, refusing: %m");
1b566071 589
11bf3cce
LP
590 r = address_acquire(link, address, &address);
591 if (r < 0)
7750b796 592 return log_link_error_errno(link, r, "Failed to acquire an address from pool: %m");
11bf3cce 593
66669078
TG
594 if (update)
595 r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req,
596 link->ifindex, address->family);
597 else
598 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR,
599 link->ifindex, address->family);
eb56eb9b 600 if (r < 0)
7750b796 601 return log_link_error_errno(link, r, "Could not allocate RTM_NEWADDR message: %m");
f579559b 602
5a723174 603 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
eb56eb9b 604 if (r < 0)
7750b796 605 return log_link_error_errno(link, r, "Could not set prefixlen: %m");
5a723174 606
851c9f82
PF
607 address->flags |= IFA_F_PERMANENT;
608
e63be084
SS
609 if (address->home_address)
610 address->flags |= IFA_F_HOMEADDRESS;
611
051e77ca 612 if (!FLAGS_SET(address->duplicate_address_detection, ADDRESS_FAMILY_IPV6))
e63be084
SS
613 address->flags |= IFA_F_NODAD;
614
615 if (address->manage_temporary_address)
616 address->flags |= IFA_F_MANAGETEMPADDR;
617
de697db0 618 if (!address->prefix_route)
e63be084
SS
619 address->flags |= IFA_F_NOPREFIXROUTE;
620
621 if (address->autojoin)
622 address->flags |= IFA_F_MCAUTOJOIN;
623
851c9f82 624 r = sd_rtnl_message_addr_set_flags(req, (address->flags & 0xff));
eb56eb9b 625 if (r < 0)
7750b796 626 return log_link_error_errno(link, r, "Could not set flags: %m");
5a723174 627
851c9f82 628 if (address->flags & ~0xff) {
1c4baffc 629 r = sd_netlink_message_append_u32(req, IFA_FLAGS, address->flags);
851c9f82 630 if (r < 0)
7750b796 631 return log_link_error_errno(link, r, "Could not set extended flags: %m");
851c9f82
PF
632 }
633
5c1d3fc9 634 r = sd_rtnl_message_addr_set_scope(req, address->scope);
eb56eb9b 635 if (r < 0)
7750b796 636 return log_link_error_errno(link, r, "Could not set scope: %m");
5a723174 637
43409486 638 r = netlink_message_append_in_addr_union(req, IFA_LOCAL, address->family, &address->in_addr);
eb56eb9b 639 if (r < 0)
7750b796 640 return log_link_error_errno(link, r, "Could not append IFA_LOCAL attribute: %m");
f579559b 641
d40b01e4 642 if (in_addr_is_null(address->family, &address->in_addr_peer) == 0) {
43409486 643 r = netlink_message_append_in_addr_union(req, IFA_ADDRESS, address->family, &address->in_addr_peer);
eb56eb9b 644 if (r < 0)
7750b796 645 return log_link_error_errno(link, r, "Could not append IFA_ADDRESS attribute: %m");
66e0bb33
YW
646 } else if (address->family == AF_INET && address->prefixlen <= 30) {
647 r = sd_netlink_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast);
648 if (r < 0)
7750b796 649 return log_link_error_errno(link, r, "Could not append IFA_BROADCAST attribute: %m");
f579559b
TG
650 }
651
652 if (address->label) {
1c4baffc 653 r = sd_netlink_message_append_string(req, IFA_LABEL, address->label);
eb56eb9b 654 if (r < 0)
7750b796 655 return log_link_error_errno(link, r, "Could not append IFA_LABEL attribute: %m");
f579559b
TG
656 }
657
66e0bb33 658 r = sd_netlink_message_append_cache_info(req, IFA_CACHEINFO, &address->cinfo);
eb56eb9b 659 if (r < 0)
7750b796 660 return log_link_error_errno(link, r, "Could not append IFA_CACHEINFO attribute: %m");
68ceb9df 661
fcf50cff 662 r = address_establish(address, link);
eb56eb9b 663 if (r < 0)
7750b796 664 log_link_warning_errno(link, r, "Could not enable IP masquerading, ignoring: %m");
fcf50cff 665
dfef713f 666 r = netlink_call_async(link->manager->rtnl, NULL, req, callback, link_netlink_destroy_callback, link);
fcf50cff
TG
667 if (r < 0) {
668 address_release(address);
7750b796 669 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
fcf50cff 670 }
f579559b 671
563c69c6
TG
672 link_ref(link);
673
dfef713f
SS
674 if (address->family == AF_INET6 && !in_addr_is_null(address->family, &address->in_addr_peer))
675 r = address_add(link, address->family, &address->in_addr_peer, address->prefixlen, NULL);
676 else
677 r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL);
adda1ed9
TG
678 if (r < 0) {
679 address_release(address);
7750b796 680 return log_link_error_errno(link, r, "Could not add address: %m");
adda1ed9
TG
681 }
682
051e77ca
SS
683 if (address->acd) {
684 assert(address->family == AF_INET);
685 if (DEBUG_LOGGING) {
686 _cleanup_free_ char *pretty = NULL;
687
688 (void) in_addr_to_string(address->family, &address->in_addr, &pretty);
98b02994 689 log_link_debug(link, "Starting IPv4ACD client. Probing address %s", strna(pretty));
051e77ca
SS
690 }
691
e92b60b2 692 r = sd_ipv4acd_start(address->acd, true);
051e77ca
SS
693 if (r < 0)
694 log_link_warning_errno(link, r, "Failed to start IPv4ACD client, ignoring: %m");
695 }
696
54a1a535 697 return 1;
f579559b
TG
698}
699
051e77ca
SS
700static void static_address_on_acd(sd_ipv4acd *acd, int event, void *userdata) {
701 _cleanup_free_ char *pretty = NULL;
702 Address *address;
703 Link *link;
704 int r;
705
706 assert(acd);
707 assert(userdata);
708
709 address = (Address *) userdata;
710 link = address->link;
711
712 (void) in_addr_to_string(address->family, &address->in_addr, &pretty);
713 switch (event) {
714 case SD_IPV4ACD_EVENT_STOP:
715 log_link_debug(link, "Stopping ACD client...");
716 return;
717
718 case SD_IPV4ACD_EVENT_BIND:
719 log_link_debug(link, "Successfully claimed address %s", strna(pretty));
720 link_check_ready(link);
721 break;
722
723 case SD_IPV4ACD_EVENT_CONFLICT:
724 log_link_warning(link, "DAD conflict. Dropping address %s", strna(pretty));
725 r = address_remove(address, link, NULL);
726 if (r < 0)
727 log_link_error_errno(link, r, "Failed to drop DAD conflicted address %s", strna(pretty));;
728
729 link_check_ready(link);
730 break;
731
732 default:
733 assert_not_reached("Invalid IPv4ACD event.");
734 }
735
736 sd_ipv4acd_stop(acd);
737
738 return;
739}
740
741int configure_ipv4_duplicate_address_detection(Link *link, Address *address) {
742 int r;
743
744 assert(link);
745 assert(address);
746 assert(address->family == AF_INET);
747 assert(!address->link && address->network);
748
749 address->link = link;
750
751 r = sd_ipv4acd_new(&address->acd);
752 if (r < 0)
753 return r;
754
755 r = sd_ipv4acd_attach_event(address->acd, NULL, 0);
756 if (r < 0)
757 return r;
758
759 r = sd_ipv4acd_set_ifindex(address->acd, link->ifindex);
760 if (r < 0)
761 return r;
762
763 r = sd_ipv4acd_set_mac(address->acd, &link->mac);
764 if (r < 0)
765 return r;
766
767 r = sd_ipv4acd_set_address(address->acd, &address->in_addr.in);
768 if (r < 0)
769 return r;
770
771 r = sd_ipv4acd_set_callback(address->acd, static_address_on_acd, address);
772 if (r < 0)
773 return r;
774
775 return 0;
776}
777
44e7b949
LP
778int config_parse_broadcast(
779 const char *unit,
eb0ea358
TG
780 const char *filename,
781 unsigned line,
782 const char *section,
783 unsigned section_line,
784 const char *lvalue,
785 int ltype,
786 const char *rvalue,
787 void *data,
788 void *userdata) {
44e7b949 789
eb0ea358 790 Network *network = userdata;
fcbf4cb7 791 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
eb0ea358
TG
792 int r;
793
794 assert(filename);
795 assert(section);
796 assert(lvalue);
797 assert(rvalue);
798 assert(data);
799
f4859fc7 800 r = address_new_static(network, filename, section_line, &n);
eb0ea358
TG
801 if (r < 0)
802 return r;
803
482e2ac1 804 if (n->family == AF_INET6) {
2850cd40
YW
805 log_syntax(unit, LOG_ERR, filename, line, 0,
806 "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue);
482e2ac1
TG
807 return 0;
808 }
809
44e7b949 810 r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast);
eb0ea358 811 if (r < 0) {
2850cd40
YW
812 log_syntax(unit, LOG_ERR, filename, line, r,
813 "Broadcast is invalid, ignoring assignment: %s", rvalue);
eb0ea358
TG
814 return 0;
815 }
816
44e7b949 817 n->family = AF_INET;
eb0ea358
TG
818 n = NULL;
819
820 return 0;
821}
822
f579559b
TG
823int config_parse_address(const char *unit,
824 const char *filename,
825 unsigned line,
826 const char *section,
71a61510 827 unsigned section_line,
f579559b
TG
828 const char *lvalue,
829 int ltype,
830 const char *rvalue,
831 void *data,
832 void *userdata) {
44e7b949 833
6ae115c1 834 Network *network = userdata;
fcbf4cb7 835 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
44e7b949 836 union in_addr_union buffer;
b7cb4452 837 unsigned char prefixlen;
44e7b949 838 int r, f;
f579559b
TG
839
840 assert(filename);
6ae115c1 841 assert(section);
f579559b
TG
842 assert(lvalue);
843 assert(rvalue);
844 assert(data);
845
92fe133a
TG
846 if (streq(section, "Network")) {
847 /* we are not in an Address section, so treat
848 * this as the special '0' section */
f4859fc7
SS
849 r = address_new_static(network, NULL, 0, &n);
850 } else
851 r = address_new_static(network, filename, section_line, &n);
92fe133a 852
f579559b
TG
853 if (r < 0)
854 return r;
855
856 /* Address=address/prefixlen */
0f707207
YW
857 r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_REFUSE, &f, &buffer, &prefixlen);
858 if (r == -ENOANO) {
859 log_syntax(unit, LOG_ERR, filename, line, r,
860 "An address '%s' is specified without prefix length. "
861 "The behavior of parsing addresses without prefix length will be changed in the future release. "
862 "Please specify prefix length explicitly.", rvalue);
863
864 r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_LEGACY, &f, &buffer, &prefixlen);
865 }
f579559b 866 if (r < 0) {
b7cb4452 867 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid address '%s', ignoring assignment: %m", rvalue);
f579559b
TG
868 return 0;
869 }
870
44e7b949 871 if (n->family != AF_UNSPEC && f != n->family) {
b7cb4452 872 log_syntax(unit, LOG_ERR, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue);
44e7b949
LP
873 return 0;
874 }
875
c9207ff3
YW
876 if (in_addr_is_null(f, &buffer)) {
877 /* Will use address from address pool. Note that for ipv6 case, prefix of the address
878 * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So,
879 * let's limit the prefix length to 64 or larger. See RFC4193. */
880 if ((f == AF_INET && prefixlen < 8) ||
881 (f == AF_INET6 && prefixlen < 64)) {
882 log_syntax(unit, LOG_ERR, filename, line, 0,
883 "Null address with invalid prefixlen='%u', ignoring assignment: %s",
884 prefixlen, rvalue);
885 return 0;
886 }
887 }
888
44e7b949 889 n->family = f;
b7cb4452 890 n->prefixlen = prefixlen;
44e7b949
LP
891
892 if (streq(lvalue, "Address"))
893 n->in_addr = buffer;
894 else
895 n->in_addr_peer = buffer;
896
3681d639 897 if (n->family == AF_INET && n->broadcast.s_addr == 0 && n->prefixlen <= 30)
fe2e4b69 898 n->broadcast.s_addr = n->in_addr.in.s_addr | htobe32(0xfffffffflu >> n->prefixlen);
eb0ea358 899
f579559b
TG
900 n = NULL;
901
902 return 0;
903}
6ae115c1 904
d31645ad
LP
905int config_parse_label(
906 const char *unit,
6ae115c1
TG
907 const char *filename,
908 unsigned line,
909 const char *section,
910 unsigned section_line,
911 const char *lvalue,
912 int ltype,
913 const char *rvalue,
914 void *data,
915 void *userdata) {
d31645ad 916
fcbf4cb7 917 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
d31645ad 918 Network *network = userdata;
6ae115c1
TG
919 int r;
920
921 assert(filename);
922 assert(section);
923 assert(lvalue);
924 assert(rvalue);
925 assert(data);
926
f4859fc7 927 r = address_new_static(network, filename, section_line, &n);
6ae115c1
TG
928 if (r < 0)
929 return r;
930
a87d19fe 931 if (!address_label_valid(rvalue)) {
2850cd40
YW
932 log_syntax(unit, LOG_ERR, filename, line, 0,
933 "Interface label is too long or invalid, ignoring assignment: %s", rvalue);
6ae115c1
TG
934 return 0;
935 }
936
d31645ad
LP
937 r = free_and_strdup(&n->label, rvalue);
938 if (r < 0)
939 return log_oom();
6ae115c1
TG
940
941 n = NULL;
6ae115c1
TG
942 return 0;
943}
ce6c77eb 944
b5834a0b
SS
945int config_parse_lifetime(const char *unit,
946 const char *filename,
947 unsigned line,
948 const char *section,
949 unsigned section_line,
950 const char *lvalue,
951 int ltype,
952 const char *rvalue,
953 void *data,
954 void *userdata) {
955 Network *network = userdata;
fcbf4cb7 956 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
d2735796 957 uint32_t k;
b5834a0b
SS
958 int r;
959
960 assert(filename);
961 assert(section);
962 assert(lvalue);
963 assert(rvalue);
964 assert(data);
965
f4859fc7 966 r = address_new_static(network, filename, section_line, &n);
b5834a0b
SS
967 if (r < 0)
968 return r;
969
10b20e5a
ZJS
970 /* We accept only "forever", "infinity", empty, or "0". */
971 if (STR_IN_SET(rvalue, "forever", "infinity", ""))
33680b0a
YW
972 k = CACHE_INFO_INFINITY_LIFE_TIME;
973 else if (streq(rvalue, "0"))
974 k = 0;
975 else {
976 log_syntax(unit, LOG_ERR, filename, line, 0,
977 "Invalid PreferredLifetime= value, ignoring: %s", rvalue);
b5834a0b
SS
978 return 0;
979 }
980
33680b0a 981 n->cinfo.ifa_prefered = k;
d2735796 982 TAKE_PTR(n);
b5834a0b
SS
983
984 return 0;
985}
986
e63be084
SS
987int config_parse_address_flags(const char *unit,
988 const char *filename,
989 unsigned line,
990 const char *section,
991 unsigned section_line,
992 const char *lvalue,
993 int ltype,
994 const char *rvalue,
995 void *data,
996 void *userdata) {
997 Network *network = userdata;
fcbf4cb7 998 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
e63be084
SS
999 int r;
1000
1001 assert(filename);
1002 assert(section);
1003 assert(lvalue);
1004 assert(rvalue);
1005 assert(data);
1006
f4859fc7 1007 r = address_new_static(network, filename, section_line, &n);
e63be084
SS
1008 if (r < 0)
1009 return r;
1010
1011 r = parse_boolean(rvalue);
1012 if (r < 0) {
2850cd40 1013 log_syntax(unit, LOG_ERR, filename, line, r,
051e77ca 1014 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
e63be084
SS
1015 return 0;
1016 }
1017
1018 if (streq(lvalue, "HomeAddress"))
1019 n->home_address = r;
e63be084
SS
1020 else if (streq(lvalue, "ManageTemporaryAddress"))
1021 n->manage_temporary_address = r;
1022 else if (streq(lvalue, "PrefixRoute"))
de697db0
YW
1023 n->prefix_route = !r;
1024 else if (streq(lvalue, "AddPrefixRoute"))
e63be084
SS
1025 n->prefix_route = r;
1026 else if (streq(lvalue, "AutoJoin"))
1027 n->autojoin = r;
2850cd40
YW
1028 else
1029 assert_not_reached("Invalid address flag type.");
e63be084 1030
4aa4c4b0 1031 n = NULL;
e63be084
SS
1032 return 0;
1033}
1034
2959fb07
SS
1035int config_parse_address_scope(const char *unit,
1036 const char *filename,
1037 unsigned line,
1038 const char *section,
1039 unsigned section_line,
1040 const char *lvalue,
1041 int ltype,
1042 const char *rvalue,
1043 void *data,
1044 void *userdata) {
1045 Network *network = userdata;
fcbf4cb7 1046 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
2959fb07
SS
1047 int r;
1048
1049 assert(filename);
1050 assert(section);
1051 assert(lvalue);
1052 assert(rvalue);
1053 assert(data);
1054
1055 r = address_new_static(network, filename, section_line, &n);
1056 if (r < 0)
1057 return r;
1058
1059 if (streq(rvalue, "host"))
1060 n->scope = RT_SCOPE_HOST;
1061 else if (streq(rvalue, "link"))
1062 n->scope = RT_SCOPE_LINK;
1063 else if (streq(rvalue, "global"))
1064 n->scope = RT_SCOPE_UNIVERSE;
1065 else {
1066 r = safe_atou8(rvalue , &n->scope);
1067 if (r < 0) {
2850cd40
YW
1068 log_syntax(unit, LOG_ERR, filename, line, r,
1069 "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue);
2959fb07
SS
1070 return 0;
1071 }
1072 }
1073
07336a06 1074 n->scope_set = true;
2959fb07 1075 n = NULL;
2959fb07
SS
1076 return 0;
1077}
1078
051e77ca
SS
1079int config_parse_duplicate_address_detection(
1080 const char *unit,
1081 const char *filename,
1082 unsigned line,
1083 const char *section,
1084 unsigned section_line,
1085 const char *lvalue,
1086 int ltype,
1087 const char *rvalue,
1088 void *data,
1089 void *userdata) {
1090 Network *network = userdata;
1091 _cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1092 AddressFamily a;
1093 int r;
1094
1095 assert(filename);
1096 assert(section);
1097 assert(lvalue);
1098 assert(rvalue);
1099 assert(data);
1100
1101 r = address_new_static(network, filename, section_line, &n);
1102 if (r < 0)
1103 return r;
1104
1105 r = parse_boolean(rvalue);
1106 if (r >= 0) {
1107 log_syntax(unit, LOG_WARNING, filename, line, 0,
1108 "For historical reasons, %s=%s means %s=%s. "
1109 "Please use 'both', 'ipv4', 'ipv6' or 'none' instead.",
1110 lvalue, rvalue, lvalue, r ? "none" : "both");
1111 n->duplicate_address_detection = r ? ADDRESS_FAMILY_NO : ADDRESS_FAMILY_YES;
1112 n = NULL;
1113 return 0;
1114 }
1115
1116 a = duplicate_address_detection_address_family_from_string(rvalue);
1117 if (a < 0) {
1118 log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
1119 "Failed to parse %s=, ignoring: %s", lvalue, rvalue);
1120 return 0;
1121 }
1122
1123 n->duplicate_address_detection = a;
1124 n = NULL;
1125 return 0;
1126}
1127
ce6c77eb
TG
1128bool address_is_ready(const Address *a) {
1129 assert(a);
1130
ce158189 1131 return !(a->flags & IFA_F_TENTATIVE);
ce6c77eb 1132}
fcbf4cb7
YW
1133
1134int address_section_verify(Address *address) {
1135 if (section_is_invalid(address->section))
1136 return -EINVAL;
1137
1138 if (address->family == AF_UNSPEC) {
1139 assert(address->section);
1140
1141 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1142 "%s: Address section without Address= field configured. "
1143 "Ignoring [Address] section from line %u.",
1144 address->section->filename, address->section->line);
1145 }
1146
07336a06
YW
1147 if (!address->scope_set && in_addr_is_localhost(address->family, &address->in_addr) > 0)
1148 address->scope = RT_SCOPE_HOST;
1149
fcbf4cb7
YW
1150 return 0;
1151}