]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-address.c
network: rename ndisc_route_handler() to ndisc_netlink_message_handler()
[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"
fc2f9534 8#include "netlink-util.h"
6bedfcbb 9#include "networkd-address.h"
23f53b99 10#include "networkd-manager.h"
6bedfcbb 11#include "parse-util.h"
3ac8e543 12#include "set.h"
d31645ad 13#include "socket-util.h"
07630cea 14#include "string-util.h"
51517f9e 15#include "strv.h"
3ac8e543
TG
16#include "utf8.h"
17#include "util.h"
f579559b 18
1b566071 19#define ADDRESSES_PER_LINK_MAX 2048U
8c34b963
LP
20#define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
21
f0213e37 22int address_new(Address **ret) {
8e766630 23 _cleanup_(address_freep) Address *address = NULL;
f0213e37 24
17f9c355 25 address = new(Address, 1);
f0213e37
TG
26 if (!address)
27 return -ENOMEM;
aba496a5 28
17f9c355
YW
29 *address = (Address) {
30 .family = AF_UNSPEC,
31 .scope = RT_SCOPE_UNIVERSE,
32 .cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME,
33 .cinfo.ifa_valid = CACHE_INFO_INFINITY_LIFE_TIME,
34 };
f0213e37 35
1cc6c93a 36 *ret = TAKE_PTR(address);
f0213e37
TG
37
38 return 0;
aba496a5
UTL
39}
40
f4859fc7 41int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) {
8e766630
LP
42 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
43 _cleanup_(address_freep) Address *address = NULL;
f0213e37 44 int r;
f579559b 45
8c34b963
LP
46 assert(network);
47 assert(ret);
48317c39 48 assert(!!filename == (section_line > 0));
8c34b963 49
48317c39 50 if (filename) {
f4859fc7
SS
51 r = network_config_section_new(filename, section_line, &n);
52 if (r < 0)
53 return r;
54
55 address = hashmap_get(network->addresses_by_section, n);
6ae115c1 56 if (address) {
1cc6c93a 57 *ret = TAKE_PTR(address);
6ae115c1
TG
58
59 return 0;
60 }
61 }
62
8c34b963
LP
63 if (network->n_static_addresses >= STATIC_ADDRESSES_PER_NETWORK_MAX)
64 return -E2BIG;
65
f0213e37
TG
66 r = address_new(&address);
67 if (r < 0)
68 return r;
801bd9e8 69
0f7f2769
YW
70 address->network = network;
71 LIST_APPEND(addresses, network->static_addresses, address);
72 network->n_static_addresses++;
73
48317c39 74 if (filename) {
1cc6c93a 75 address->section = TAKE_PTR(n);
fcc48287 76
3e570042
YW
77 r = hashmap_ensure_allocated(&network->addresses_by_section, &network_config_hash_ops);
78 if (r < 0)
79 return r;
80
f7fe70ea
SS
81 r = hashmap_put(network->addresses_by_section, address->section, address);
82 if (r < 0)
83 return r;
6ae115c1
TG
84 }
85
1cc6c93a 86 *ret = TAKE_PTR(address);
f579559b
TG
87
88 return 0;
89}
90
91void address_free(Address *address) {
92 if (!address)
93 return;
94
f048a16b 95 if (address->network) {
3d3d4255 96 LIST_REMOVE(addresses, address->network->static_addresses, address);
8c34b963
LP
97 assert(address->network->n_static_addresses > 0);
98 address->network->n_static_addresses--;
f579559b 99
de4224aa 100 if (address->section)
f4859fc7 101 hashmap_remove(address->network->addresses_by_section, address->section);
f048a16b 102 }
6ae115c1 103
adda1ed9 104 if (address->link) {
cf1d700d 105 set_remove(address->link->addresses, address);
adda1ed9 106 set_remove(address->link->addresses_foreign, address);
f150100a
SS
107
108 if (in_addr_equal(AF_INET6, &address->in_addr, (const union in_addr_union *) &address->link->ipv6ll_address))
109 memzero(&address->link->ipv6ll_address, sizeof(struct in6_addr));
adda1ed9 110 }
cf1d700d 111
de4224aa
YW
112 network_config_section_free(address->section);
113 free(address->label);
f579559b
TG
114 free(address);
115}
116
3ac8e543
TG
117static void address_hash_func(const void *b, struct siphash *state) {
118 const Address *a = b;
119
120 assert(a);
121
122 siphash24_compress(&a->family, sizeof(a->family), state);
123
124 switch (a->family) {
125 case AF_INET:
126 siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
127
128 /* peer prefix */
129 if (a->prefixlen != 0) {
130 uint32_t prefix;
131
132 if (a->in_addr_peer.in.s_addr != 0)
133 prefix = be32toh(a->in_addr_peer.in.s_addr) >> (32 - a->prefixlen);
134 else
135 prefix = be32toh(a->in_addr.in.s_addr) >> (32 - a->prefixlen);
136
137 siphash24_compress(&prefix, sizeof(prefix), state);
138 }
139
4831981d 140 _fallthrough_;
3ac8e543
TG
141 case AF_INET6:
142 /* local address */
143 siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state);
144
145 break;
146 default:
147 /* treat any other address family as AF_UNSPEC */
148 break;
149 }
150}
151
152static int address_compare_func(const void *c1, const void *c2) {
153 const Address *a1 = c1, *a2 = c2;
a0edd02e 154 int r;
3ac8e543 155
a0edd02e
FB
156 r = CMP(a1->family, a2->family);
157 if (r != 0)
158 return r;
3ac8e543
TG
159
160 switch (a1->family) {
161 /* use the same notion of equality as the kernel does */
162 case AF_INET:
a0edd02e
FB
163 r = CMP(a1->prefixlen, a2->prefixlen);
164 if (r != 0)
165 return r;
3ac8e543
TG
166
167 /* compare the peer prefixes */
168 if (a1->prefixlen != 0) {
169 /* make sure we don't try to shift by 32.
170 * See ISO/IEC 9899:TC3 § 6.5.7.3. */
171 uint32_t b1, b2;
172
173 if (a1->in_addr_peer.in.s_addr != 0)
174 b1 = be32toh(a1->in_addr_peer.in.s_addr) >> (32 - a1->prefixlen);
175 else
176 b1 = be32toh(a1->in_addr.in.s_addr) >> (32 - a1->prefixlen);
177
178 if (a2->in_addr_peer.in.s_addr != 0)
179 b2 = be32toh(a2->in_addr_peer.in.s_addr) >> (32 - a1->prefixlen);
180 else
181 b2 = be32toh(a2->in_addr.in.s_addr) >> (32 - a1->prefixlen);
182
a0edd02e
FB
183 r = CMP(b1, b2);
184 if (r != 0)
185 return r;
3ac8e543
TG
186 }
187
4831981d 188 _fallthrough_;
3ac8e543
TG
189 case AF_INET6:
190 return memcmp(&a1->in_addr, &a2->in_addr, FAMILY_ADDRESS_SIZE(a1->family));
191 default:
192 /* treat any other address family as AF_UNSPEC */
193 return 0;
194 }
195}
196
197static const struct hash_ops address_hash_ops = {
198 .hash = address_hash_func,
199 .compare = address_compare_func
200};
201
202bool address_equal(Address *a1, Address *a2) {
203 if (a1 == a2)
204 return true;
205
206 if (!a1 || !a2)
207 return false;
208
209 return address_compare_func(a1, a2) == 0;
210}
211
91b5f997
TG
212static int address_establish(Address *address, Link *link) {
213 bool masq;
214 int r;
215
216 assert(address);
217 assert(link);
218
219 masq = link->network &&
fcf50cff
TG
220 link->network->ip_masquerade &&
221 address->family == AF_INET &&
222 address->scope < RT_SCOPE_LINK;
91b5f997
TG
223
224 /* Add firewall entry if this is requested */
225 if (address->ip_masquerade_done != masq) {
226 union in_addr_union masked = address->in_addr;
227 in_addr_mask(address->family, &masked, address->prefixlen);
228
229 r = fw_add_masquerade(masq, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0);
230 if (r < 0)
231 log_link_warning_errno(link, r, "Could not enable IP masquerading: %m");
232
233 address->ip_masquerade_done = masq;
234 }
235
236 return 0;
237}
238
adda1ed9
TG
239static int address_add_internal(Link *link, Set **addresses,
240 int family,
241 const union in_addr_union *in_addr,
242 unsigned char prefixlen,
243 Address **ret) {
8e766630 244 _cleanup_(address_freep) Address *address = NULL;
cf1d700d
TG
245 int r;
246
247 assert(link);
adda1ed9 248 assert(addresses);
054f0db4 249 assert(in_addr);
054f0db4
TG
250
251 r = address_new(&address);
252 if (r < 0)
253 return r;
254
255 address->family = family;
256 address->in_addr = *in_addr;
257 address->prefixlen = prefixlen;
63bbe5c7
TG
258 /* Consider address tentative until we get the real flags from the kernel */
259 address->flags = IFA_F_TENTATIVE;
cf1d700d 260
adda1ed9 261 r = set_ensure_allocated(addresses, &address_hash_ops);
cf1d700d
TG
262 if (r < 0)
263 return r;
264
adda1ed9 265 r = set_put(*addresses, address);
cf1d700d
TG
266 if (r < 0)
267 return r;
268
269 address->link = link;
270
adda1ed9
TG
271 if (ret)
272 *ret = address;
273
054f0db4
TG
274 address = NULL;
275
cf1d700d
TG
276 return 0;
277}
278
adda1ed9
TG
279int address_add_foreign(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
280 return address_add_internal(link, &link->addresses_foreign, family, in_addr, prefixlen, ret);
281}
282
c4a03a56 283int address_add(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
cab974b0 284 Address *address;
e7780c8d
TG
285 int r;
286
cab974b0
TG
287 r = address_get(link, family, in_addr, prefixlen, &address);
288 if (r == -ENOENT) {
289 /* Address does not exist, create a new one */
290 r = address_add_internal(link, &link->addresses, family, in_addr, prefixlen, &address);
291 if (r < 0)
292 return r;
293 } else if (r == 0) {
294 /* Take over a foreign address */
295 r = set_ensure_allocated(&link->addresses, &address_hash_ops);
296 if (r < 0)
297 return r;
298
299 r = set_put(link->addresses, address);
300 if (r < 0)
301 return r;
302
303 set_remove(link->addresses_foreign, address);
304 } else if (r == 1) {
305 /* Already exists, do nothing */
306 ;
307 } else
e7780c8d
TG
308 return r;
309
cab974b0
TG
310 if (ret)
311 *ret = address;
e7780c8d
TG
312
313 return 0;
adda1ed9
TG
314}
315
fcf50cff 316static int address_release(Address *address) {
5a8bcb67
LP
317 int r;
318
319 assert(address);
fcf50cff 320 assert(address->link);
5a8bcb67 321
91b5f997
TG
322 /* Remove masquerading firewall entry if it was added */
323 if (address->ip_masquerade_done) {
5a8bcb67
LP
324 union in_addr_union masked = address->in_addr;
325 in_addr_mask(address->family, &masked, address->prefixlen);
326
91b5f997 327 r = fw_add_masquerade(false, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0);
5a8bcb67 328 if (r < 0)
fcf50cff 329 log_link_warning_errno(address->link, r, "Failed to disable IP masquerading: %m");
5a8bcb67 330
91b5f997 331 address->ip_masquerade_done = false;
5a8bcb67
LP
332 }
333
334 return 0;
335}
336
889b550f
LP
337int address_update(
338 Address *address,
339 unsigned char flags,
340 unsigned char scope,
341 const struct ifa_cacheinfo *cinfo) {
342
36c32f61 343 bool ready;
e7ab854c 344 int r;
36c32f61
TG
345
346 assert(address);
347 assert(cinfo);
7209086d
SS
348 assert_return(address->link, 1);
349
350 if (IN_SET(address->link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
351 return 1;
36c32f61
TG
352
353 ready = address_is_ready(address);
354
355 address->flags = flags;
356 address->scope = scope;
357 address->cinfo = *cinfo;
358
7209086d
SS
359 link_update_operstate(address->link);
360
361 if (!ready && address_is_ready(address)) {
362 link_check_ready(address->link);
363
364 if (address->family == AF_INET6 &&
365 in_addr_is_link_local(AF_INET6, &address->in_addr) > 0 &&
366 in_addr_is_null(AF_INET6, (const union in_addr_union*) &address->link->ipv6ll_address) > 0) {
367
368 r = link_ipv6ll_gained(address->link, &address->in_addr.in6);
369 if (r < 0)
370 return r;
e7ab854c 371 }
a3a019e1 372 }
36c32f61
TG
373
374 return 0;
375}
376
91b5f997 377int address_drop(Address *address) {
8012cd39
TG
378 Link *link;
379 bool ready;
380
5a8bcb67 381 assert(address);
91b5f997 382
8012cd39
TG
383 ready = address_is_ready(address);
384 link = address->link;
385
fcf50cff 386 address_release(address);
91b5f997
TG
387 address_free(address);
388
84de38c5
TG
389 link_update_operstate(link);
390
8012cd39
TG
391 if (link && !ready)
392 link_check_ready(link);
393
91b5f997
TG
394 return 0;
395}
396
1b566071
LP
397int address_get(Link *link,
398 int family,
399 const union in_addr_union *in_addr,
400 unsigned char prefixlen,
401 Address **ret) {
402
403 Address address, *existing;
91b5f997 404
5a8bcb67 405 assert(link);
91b5f997 406 assert(in_addr);
5a8bcb67 407
1b566071
LP
408 address = (Address) {
409 .family = family,
410 .in_addr = *in_addr,
411 .prefixlen = prefixlen,
412 };
5a8bcb67 413
91b5f997 414 existing = set_get(link->addresses, &address);
cab974b0 415 if (existing) {
1b566071
LP
416 if (ret)
417 *ret = existing;
cab974b0 418 return 1;
adda1ed9 419 }
5a8bcb67 420
1b566071
LP
421 existing = set_get(link->addresses_foreign, &address);
422 if (existing) {
423 if (ret)
424 *ret = existing;
425 return 0;
426 }
5a8bcb67 427
1b566071 428 return -ENOENT;
5a8bcb67
LP
429}
430
63ae0569
YW
431static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
432 Link *link = userdata;
433 int r;
434
435 assert(m);
436 assert(link);
437 assert(link->ifname);
438
439 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
440 return 1;
441
442 r = sd_netlink_message_get_errno(m);
443 if (r < 0 && r != -EADDRNOTAVAIL)
444 log_link_warning_errno(link, r, "Could not drop address: %m");
445
446 return 1;
447}
448
483d099e
ZJS
449int address_remove(
450 Address *address,
451 Link *link,
452 sd_netlink_message_handler_t callback) {
453
4afd3348 454 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
30226d27 455 _cleanup_free_ char *b = NULL;
407fe036
TG
456 int r;
457
458 assert(address);
4c701096 459 assert(IN_SET(address->family, AF_INET, AF_INET6));
407fe036
TG
460 assert(link);
461 assert(link->ifindex > 0);
462 assert(link->manager);
463 assert(link->manager->rtnl);
464
30226d27
TJ
465 if (DEBUG_LOGGING) {
466 if (in_addr_to_string(address->family, &address->in_addr, &b) >= 0)
467 log_link_debug(link, "Removing address %s", b);
468 }
469
151b9b96
LP
470 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR,
471 link->ifindex, address->family);
eb56eb9b
MS
472 if (r < 0)
473 return log_error_errno(r, "Could not allocate RTM_DELADDR message: %m");
407fe036 474
5a723174 475 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
eb56eb9b
MS
476 if (r < 0)
477 return log_error_errno(r, "Could not set prefixlen: %m");
5a723174 478
407fe036 479 if (address->family == AF_INET)
1c4baffc 480 r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
407fe036 481 else if (address->family == AF_INET6)
1c4baffc 482 r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
eb56eb9b
MS
483 if (r < 0)
484 return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");
407fe036 485
63ae0569
YW
486 r = sd_netlink_call_async(link->manager->rtnl, NULL, req,
487 callback ?: address_remove_handler,
8190a388 488 link_netlink_destroy_callback, link, 0, __func__);
eb56eb9b
MS
489 if (r < 0)
490 return log_error_errno(r, "Could not send rtnetlink message: %m");
407fe036 491
563c69c6
TG
492 link_ref(link);
493
407fe036
TG
494 return 0;
495}
496
11bf3cce
LP
497static int address_acquire(Link *link, Address *original, Address **ret) {
498 union in_addr_union in_addr = {};
499 struct in_addr broadcast = {};
8e766630 500 _cleanup_(address_freep) Address *na = NULL;
11bf3cce
LP
501 int r;
502
503 assert(link);
504 assert(original);
505 assert(ret);
506
507 /* Something useful was configured? just use it */
af93291c 508 if (in_addr_is_null(original->family, &original->in_addr) <= 0)
11bf3cce
LP
509 return 0;
510
511 /* The address is configured to be 0.0.0.0 or [::] by the user?
512 * Then let's acquire something more useful from the pool. */
513 r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
6a7a4e4d
LP
514 if (r < 0)
515 return log_link_error_errno(link, r, "Failed to acquire address from pool: %m");
11bf3cce 516 if (r == 0) {
79008bdd 517 log_link_error(link, "Couldn't find free address for interface, all taken.");
11bf3cce
LP
518 return -EBUSY;
519 }
520
521 if (original->family == AF_INET) {
d076c6f9 522 /* Pick first address in range for ourselves ... */
11bf3cce
LP
523 in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1);
524
525 /* .. and use last as broadcast address */
e87e2b78
SS
526 if (original->prefixlen > 30)
527 broadcast.s_addr = 0;
528 else
529 broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen);
11bf3cce
LP
530 } else if (original->family == AF_INET6)
531 in_addr.in6.s6_addr[15] |= 1;
532
f0213e37 533 r = address_new(&na);
11bf3cce
LP
534 if (r < 0)
535 return r;
536
537 na->family = original->family;
538 na->prefixlen = original->prefixlen;
539 na->scope = original->scope;
540 na->cinfo = original->cinfo;
541
542 if (original->label) {
543 na->label = strdup(original->label);
0099bc15 544 if (!na->label)
11bf3cce 545 return -ENOMEM;
11bf3cce
LP
546 }
547
548 na->broadcast = broadcast;
549 na->in_addr = in_addr;
550
551 LIST_PREPEND(addresses, link->pool_addresses, na);
552
1cc6c93a 553 *ret = TAKE_PTR(na);
0099bc15 554
11bf3cce
LP
555 return 0;
556}
557
1b566071
LP
558int address_configure(
559 Address *address,
560 Link *link,
561 sd_netlink_message_handler_t callback,
562 bool update) {
563
4afd3348 564 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
f579559b
TG
565 int r;
566
c166a070 567 assert(address);
4c701096 568 assert(IN_SET(address->family, AF_INET, AF_INET6));
c166a070
TG
569 assert(link);
570 assert(link->ifindex > 0);
f882c247 571 assert(link->manager);
c166a070 572 assert(link->manager->rtnl);
bd1175bc 573 assert(callback);
f882c247 574
1b566071
LP
575 /* If this is a new address, then refuse adding more than the limit */
576 if (address_get(link, address->family, &address->in_addr, address->prefixlen, NULL) <= 0 &&
577 set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX)
578 return -E2BIG;
579
11bf3cce
LP
580 r = address_acquire(link, address, &address);
581 if (r < 0)
582 return r;
583
66669078
TG
584 if (update)
585 r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req,
586 link->ifindex, address->family);
587 else
588 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR,
589 link->ifindex, address->family);
eb56eb9b
MS
590 if (r < 0)
591 return log_error_errno(r, "Could not allocate RTM_NEWADDR message: %m");
f579559b 592
5a723174 593 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
eb56eb9b
MS
594 if (r < 0)
595 return log_error_errno(r, "Could not set prefixlen: %m");
5a723174 596
851c9f82
PF
597 address->flags |= IFA_F_PERMANENT;
598
e63be084
SS
599 if (address->home_address)
600 address->flags |= IFA_F_HOMEADDRESS;
601
602 if (address->duplicate_address_detection)
603 address->flags |= IFA_F_NODAD;
604
605 if (address->manage_temporary_address)
606 address->flags |= IFA_F_MANAGETEMPADDR;
607
608 if (address->prefix_route)
609 address->flags |= IFA_F_NOPREFIXROUTE;
610
611 if (address->autojoin)
612 address->flags |= IFA_F_MCAUTOJOIN;
613
851c9f82 614 r = sd_rtnl_message_addr_set_flags(req, (address->flags & 0xff));
eb56eb9b
MS
615 if (r < 0)
616 return log_error_errno(r, "Could not set flags: %m");
5a723174 617
851c9f82 618 if (address->flags & ~0xff) {
1c4baffc 619 r = sd_netlink_message_append_u32(req, IFA_FLAGS, address->flags);
851c9f82
PF
620 if (r < 0)
621 return log_error_errno(r, "Could not set extended flags: %m");
622 }
623
5c1d3fc9 624 r = sd_rtnl_message_addr_set_scope(req, address->scope);
eb56eb9b
MS
625 if (r < 0)
626 return log_error_errno(r, "Could not set scope: %m");
5a723174 627
0a0dc69b 628 if (address->family == AF_INET)
1c4baffc 629 r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
0a0dc69b 630 else if (address->family == AF_INET6)
1c4baffc 631 r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
eb56eb9b
MS
632 if (r < 0)
633 return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");
f579559b 634
af93291c 635 if (!in_addr_is_null(address->family, &address->in_addr_peer)) {
c081882f 636 if (address->family == AF_INET)
1c4baffc 637 r = sd_netlink_message_append_in_addr(req, IFA_ADDRESS, &address->in_addr_peer.in);
c081882f 638 else if (address->family == AF_INET6)
1c4baffc 639 r = sd_netlink_message_append_in6_addr(req, IFA_ADDRESS, &address->in_addr_peer.in6);
eb56eb9b
MS
640 if (r < 0)
641 return log_error_errno(r, "Could not append IFA_ADDRESS attribute: %m");
c081882f
SS
642 } else {
643 if (address->family == AF_INET) {
e87e2b78
SS
644 if (address->prefixlen <= 30) {
645 r = sd_netlink_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast);
646 if (r < 0)
647 return log_error_errno(r, "Could not append IFA_BROADCAST attribute: %m");
648 }
c081882f 649 }
f579559b
TG
650 }
651
652 if (address->label) {
1c4baffc 653 r = sd_netlink_message_append_string(req, IFA_LABEL, address->label);
eb56eb9b
MS
654 if (r < 0)
655 return log_error_errno(r, "Could not append IFA_LABEL attribute: %m");
f579559b
TG
656 }
657
1c4baffc 658 r = sd_netlink_message_append_cache_info(req, IFA_CACHEINFO,
68ceb9df 659 &address->cinfo);
eb56eb9b
MS
660 if (r < 0)
661 return log_error_errno(r, "Could not append IFA_CACHEINFO attribute: %m");
68ceb9df 662
fcf50cff 663 r = address_establish(address, link);
eb56eb9b 664 if (r < 0)
fcf50cff
TG
665 return r;
666
ee38400b 667 r = sd_netlink_call_async(link->manager->rtnl, NULL, req, callback,
8190a388 668 link_netlink_destroy_callback, link, 0, __func__);
fcf50cff
TG
669 if (r < 0) {
670 address_release(address);
eb56eb9b 671 return log_error_errno(r, "Could not send rtnetlink message: %m");
fcf50cff 672 }
f579559b 673
563c69c6
TG
674 link_ref(link);
675
adda1ed9
TG
676 r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL);
677 if (r < 0) {
678 address_release(address);
679 return log_error_errno(r, "Could not add address: %m");
680 }
681
f579559b
TG
682 return 0;
683}
684
44e7b949
LP
685int config_parse_broadcast(
686 const char *unit,
eb0ea358
TG
687 const char *filename,
688 unsigned line,
689 const char *section,
690 unsigned section_line,
691 const char *lvalue,
692 int ltype,
693 const char *rvalue,
694 void *data,
695 void *userdata) {
44e7b949 696
eb0ea358 697 Network *network = userdata;
8e766630 698 _cleanup_(address_freep) Address *n = NULL;
eb0ea358
TG
699 int r;
700
701 assert(filename);
702 assert(section);
703 assert(lvalue);
704 assert(rvalue);
705 assert(data);
706
f4859fc7 707 r = address_new_static(network, filename, section_line, &n);
eb0ea358
TG
708 if (r < 0)
709 return r;
710
482e2ac1 711 if (n->family == AF_INET6) {
12ca818f 712 log_syntax(unit, LOG_ERR, filename, line, 0, "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue);
482e2ac1
TG
713 return 0;
714 }
715
44e7b949 716 r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast);
eb0ea358 717 if (r < 0) {
12ca818f 718 log_syntax(unit, LOG_ERR, filename, line, r, "Broadcast is invalid, ignoring assignment: %s", rvalue);
eb0ea358
TG
719 return 0;
720 }
721
44e7b949 722 n->family = AF_INET;
eb0ea358
TG
723 n = NULL;
724
725 return 0;
726}
727
f579559b
TG
728int config_parse_address(const char *unit,
729 const char *filename,
730 unsigned line,
731 const char *section,
71a61510 732 unsigned section_line,
f579559b
TG
733 const char *lvalue,
734 int ltype,
735 const char *rvalue,
736 void *data,
737 void *userdata) {
44e7b949 738
6ae115c1 739 Network *network = userdata;
8e766630 740 _cleanup_(address_freep) Address *n = NULL;
44e7b949 741 union in_addr_union buffer;
b7cb4452 742 unsigned char prefixlen;
44e7b949 743 int r, f;
f579559b
TG
744
745 assert(filename);
6ae115c1 746 assert(section);
f579559b
TG
747 assert(lvalue);
748 assert(rvalue);
749 assert(data);
750
92fe133a
TG
751 if (streq(section, "Network")) {
752 /* we are not in an Address section, so treat
753 * this as the special '0' section */
f4859fc7
SS
754 r = address_new_static(network, NULL, 0, &n);
755 } else
756 r = address_new_static(network, filename, section_line, &n);
92fe133a 757
f579559b
TG
758 if (r < 0)
759 return r;
760
761 /* Address=address/prefixlen */
b7cb4452 762 r = in_addr_default_prefix_from_string_auto(rvalue, &f, &buffer, &prefixlen);
f579559b 763 if (r < 0) {
b7cb4452 764 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid address '%s', ignoring assignment: %m", rvalue);
f579559b
TG
765 return 0;
766 }
767
44e7b949 768 if (n->family != AF_UNSPEC && f != n->family) {
b7cb4452 769 log_syntax(unit, LOG_ERR, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue);
44e7b949
LP
770 return 0;
771 }
772
773 n->family = f;
b7cb4452 774 n->prefixlen = prefixlen;
44e7b949
LP
775
776 if (streq(lvalue, "Address"))
777 n->in_addr = buffer;
778 else
779 n->in_addr_peer = buffer;
780
781 if (n->family == AF_INET && n->broadcast.s_addr == 0)
782 n->broadcast.s_addr = n->in_addr.in.s_addr | htonl(0xfffffffflu >> n->prefixlen);
eb0ea358 783
f579559b
TG
784 n = NULL;
785
786 return 0;
787}
6ae115c1 788
d31645ad
LP
789int config_parse_label(
790 const char *unit,
6ae115c1
TG
791 const char *filename,
792 unsigned line,
793 const char *section,
794 unsigned section_line,
795 const char *lvalue,
796 int ltype,
797 const char *rvalue,
798 void *data,
799 void *userdata) {
d31645ad 800
8e766630 801 _cleanup_(address_freep) Address *n = NULL;
d31645ad 802 Network *network = userdata;
6ae115c1
TG
803 int r;
804
805 assert(filename);
806 assert(section);
807 assert(lvalue);
808 assert(rvalue);
809 assert(data);
810
f4859fc7 811 r = address_new_static(network, filename, section_line, &n);
6ae115c1
TG
812 if (r < 0)
813 return r;
814
a87d19fe
SS
815 if (!address_label_valid(rvalue)) {
816 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface label is too long or invalid, ignoring assignment: %s", rvalue);
6ae115c1
TG
817 return 0;
818 }
819
d31645ad
LP
820 r = free_and_strdup(&n->label, rvalue);
821 if (r < 0)
822 return log_oom();
6ae115c1
TG
823
824 n = NULL;
825
826 return 0;
827}
ce6c77eb 828
b5834a0b
SS
829int config_parse_lifetime(const char *unit,
830 const char *filename,
831 unsigned line,
832 const char *section,
833 unsigned section_line,
834 const char *lvalue,
835 int ltype,
836 const char *rvalue,
837 void *data,
838 void *userdata) {
839 Network *network = userdata;
8e766630 840 _cleanup_(address_freep) Address *n = NULL;
b5834a0b
SS
841 unsigned k;
842 int r;
843
844 assert(filename);
845 assert(section);
846 assert(lvalue);
847 assert(rvalue);
848 assert(data);
849
f4859fc7 850 r = address_new_static(network, filename, section_line, &n);
b5834a0b
SS
851 if (r < 0)
852 return r;
853
854 if (STR_IN_SET(rvalue, "forever", "infinity")) {
855 n->cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME;
856 n = NULL;
857
858 return 0;
859 }
860
861 r = safe_atou(rvalue, &k);
862 if (r < 0) {
863 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse PreferredLifetime, ignoring: %s", rvalue);
864 return 0;
865 }
866
867 if (k != 0)
868 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid PreferredLifetime value, ignoring: %d", k);
869 else {
870 n->cinfo.ifa_prefered = k;
871 n = NULL;
872 }
873
874 return 0;
875}
876
e63be084
SS
877int config_parse_address_flags(const char *unit,
878 const char *filename,
879 unsigned line,
880 const char *section,
881 unsigned section_line,
882 const char *lvalue,
883 int ltype,
884 const char *rvalue,
885 void *data,
886 void *userdata) {
887 Network *network = userdata;
8e766630 888 _cleanup_(address_freep) Address *n = NULL;
e63be084
SS
889 int r;
890
891 assert(filename);
892 assert(section);
893 assert(lvalue);
894 assert(rvalue);
895 assert(data);
896
f4859fc7 897 r = address_new_static(network, filename, section_line, &n);
e63be084
SS
898 if (r < 0)
899 return r;
900
901 r = parse_boolean(rvalue);
902 if (r < 0) {
903 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address flag, ignoring: %s", rvalue);
904 return 0;
905 }
906
907 if (streq(lvalue, "HomeAddress"))
908 n->home_address = r;
909 else if (streq(lvalue, "DuplicateAddressDetection"))
910 n->duplicate_address_detection = r;
911 else if (streq(lvalue, "ManageTemporaryAddress"))
912 n->manage_temporary_address = r;
913 else if (streq(lvalue, "PrefixRoute"))
914 n->prefix_route = r;
915 else if (streq(lvalue, "AutoJoin"))
916 n->autojoin = r;
917
918 return 0;
919}
920
2959fb07
SS
921int config_parse_address_scope(const char *unit,
922 const char *filename,
923 unsigned line,
924 const char *section,
925 unsigned section_line,
926 const char *lvalue,
927 int ltype,
928 const char *rvalue,
929 void *data,
930 void *userdata) {
931 Network *network = userdata;
8e766630 932 _cleanup_(address_freep) Address *n = NULL;
2959fb07
SS
933 int r;
934
935 assert(filename);
936 assert(section);
937 assert(lvalue);
938 assert(rvalue);
939 assert(data);
940
941 r = address_new_static(network, filename, section_line, &n);
942 if (r < 0)
943 return r;
944
945 if (streq(rvalue, "host"))
946 n->scope = RT_SCOPE_HOST;
947 else if (streq(rvalue, "link"))
948 n->scope = RT_SCOPE_LINK;
949 else if (streq(rvalue, "global"))
950 n->scope = RT_SCOPE_UNIVERSE;
951 else {
952 r = safe_atou8(rvalue , &n->scope);
953 if (r < 0) {
954 log_syntax(unit, LOG_ERR, filename, line, r, "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue);
955 return 0;
956 }
957 }
958
959 n = NULL;
960
961 return 0;
962}
963
ce6c77eb
TG
964bool address_is_ready(const Address *a) {
965 assert(a);
966
b7ed5384
SS
967 if (a->family == AF_INET6)
968 return !(a->flags & IFA_F_TENTATIVE);
969 else
970 return !(a->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED));
ce6c77eb 971}