]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-address.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / network / networkd-address.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
f579559b
TG
2/***
3 This file is part of systemd.
4
5 Copyright 2013 Tom Gundersen <teg@jklm.no>
f579559b
TG
6***/
7
8#include <net/if.h>
9
b5efdb8a 10#include "alloc-util.h"
f579559b 11#include "conf-parser.h"
12c2884c 12#include "firewall-util.h"
fc2f9534 13#include "netlink-util.h"
6bedfcbb 14#include "networkd-address.h"
23f53b99 15#include "networkd-manager.h"
6bedfcbb 16#include "parse-util.h"
3ac8e543 17#include "set.h"
d31645ad 18#include "socket-util.h"
07630cea 19#include "string-util.h"
3ac8e543
TG
20#include "utf8.h"
21#include "util.h"
f579559b 22
1b566071 23#define ADDRESSES_PER_LINK_MAX 2048U
8c34b963
LP
24#define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U
25
f0213e37
TG
26int address_new(Address **ret) {
27 _cleanup_address_free_ Address *address = NULL;
28
29 address = new0(Address, 1);
30 if (!address)
31 return -ENOMEM;
aba496a5
UTL
32
33 address->family = AF_UNSPEC;
34 address->scope = RT_SCOPE_UNIVERSE;
35 address->cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME;
36 address->cinfo.ifa_valid = CACHE_INFO_INFINITY_LIFE_TIME;
f0213e37 37
1cc6c93a 38 *ret = TAKE_PTR(address);
f0213e37
TG
39
40 return 0;
aba496a5
UTL
41}
42
f4859fc7
SS
43int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) {
44 _cleanup_network_config_section_free_ NetworkConfigSection *n = NULL;
f579559b 45 _cleanup_address_free_ Address *address = NULL;
f0213e37 46 int r;
f579559b 47
8c34b963
LP
48 assert(network);
49 assert(ret);
48317c39 50 assert(!!filename == (section_line > 0));
8c34b963 51
48317c39 52 if (filename) {
f4859fc7
SS
53 r = network_config_section_new(filename, section_line, &n);
54 if (r < 0)
55 return r;
56
57 address = hashmap_get(network->addresses_by_section, n);
6ae115c1 58 if (address) {
1cc6c93a 59 *ret = TAKE_PTR(address);
6ae115c1
TG
60
61 return 0;
62 }
63 }
64
8c34b963
LP
65 if (network->n_static_addresses >= STATIC_ADDRESSES_PER_NETWORK_MAX)
66 return -E2BIG;
67
f0213e37
TG
68 r = address_new(&address);
69 if (r < 0)
70 return r;
801bd9e8 71
48317c39 72 if (filename) {
1cc6c93a 73 address->section = TAKE_PTR(n);
fcc48287 74
f7fe70ea
SS
75 r = hashmap_put(network->addresses_by_section, address->section, address);
76 if (r < 0)
77 return r;
6ae115c1
TG
78 }
79
5215524d
SS
80 address->network = network;
81 LIST_APPEND(addresses, network->static_addresses, address);
8c34b963 82 network->n_static_addresses++;
5215524d 83
1cc6c93a 84 *ret = TAKE_PTR(address);
f579559b
TG
85
86 return 0;
87}
88
89void address_free(Address *address) {
90 if (!address)
91 return;
92
f048a16b 93 if (address->network) {
3d3d4255 94 LIST_REMOVE(addresses, address->network->static_addresses, address);
8c34b963
LP
95 assert(address->network->n_static_addresses > 0);
96 address->network->n_static_addresses--;
f579559b 97
f4859fc7
SS
98 if (address->section) {
99 hashmap_remove(address->network->addresses_by_section, address->section);
100 network_config_section_free(address->section);
101 }
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
f579559b
TG
112 free(address);
113}
114
3ac8e543
TG
115static void address_hash_func(const void *b, struct siphash *state) {
116 const Address *a = b;
117
118 assert(a);
119
120 siphash24_compress(&a->family, sizeof(a->family), state);
121
122 switch (a->family) {
123 case AF_INET:
124 siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
125
126 /* peer prefix */
127 if (a->prefixlen != 0) {
128 uint32_t prefix;
129
130 if (a->in_addr_peer.in.s_addr != 0)
131 prefix = be32toh(a->in_addr_peer.in.s_addr) >> (32 - a->prefixlen);
132 else
133 prefix = be32toh(a->in_addr.in.s_addr) >> (32 - a->prefixlen);
134
135 siphash24_compress(&prefix, sizeof(prefix), state);
136 }
137
4831981d 138 _fallthrough_;
3ac8e543
TG
139 case AF_INET6:
140 /* local address */
141 siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state);
142
143 break;
144 default:
145 /* treat any other address family as AF_UNSPEC */
146 break;
147 }
148}
149
150static int address_compare_func(const void *c1, const void *c2) {
151 const Address *a1 = c1, *a2 = c2;
152
153 if (a1->family < a2->family)
154 return -1;
155 if (a1->family > a2->family)
156 return 1;
157
158 switch (a1->family) {
159 /* use the same notion of equality as the kernel does */
160 case AF_INET:
161 if (a1->prefixlen < a2->prefixlen)
162 return -1;
163 if (a1->prefixlen > a2->prefixlen)
164 return 1;
165
166 /* compare the peer prefixes */
167 if (a1->prefixlen != 0) {
168 /* make sure we don't try to shift by 32.
169 * See ISO/IEC 9899:TC3 § 6.5.7.3. */
170 uint32_t b1, b2;
171
172 if (a1->in_addr_peer.in.s_addr != 0)
173 b1 = be32toh(a1->in_addr_peer.in.s_addr) >> (32 - a1->prefixlen);
174 else
175 b1 = be32toh(a1->in_addr.in.s_addr) >> (32 - a1->prefixlen);
176
177 if (a2->in_addr_peer.in.s_addr != 0)
178 b2 = be32toh(a2->in_addr_peer.in.s_addr) >> (32 - a1->prefixlen);
179 else
180 b2 = be32toh(a2->in_addr.in.s_addr) >> (32 - a1->prefixlen);
181
182 if (b1 < b2)
183 return -1;
184 if (b1 > b2)
185 return 1;
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) {
054f0db4 244 _cleanup_address_free_ 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
483d099e
ZJS
431int address_remove(
432 Address *address,
433 Link *link,
434 sd_netlink_message_handler_t callback) {
435
4afd3348 436 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
407fe036
TG
437 int r;
438
439 assert(address);
4c701096 440 assert(IN_SET(address->family, AF_INET, AF_INET6));
407fe036
TG
441 assert(link);
442 assert(link->ifindex > 0);
443 assert(link->manager);
444 assert(link->manager->rtnl);
445
151b9b96
LP
446 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR,
447 link->ifindex, address->family);
eb56eb9b
MS
448 if (r < 0)
449 return log_error_errno(r, "Could not allocate RTM_DELADDR message: %m");
407fe036 450
5a723174 451 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
eb56eb9b
MS
452 if (r < 0)
453 return log_error_errno(r, "Could not set prefixlen: %m");
5a723174 454
407fe036 455 if (address->family == AF_INET)
1c4baffc 456 r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
407fe036 457 else if (address->family == AF_INET6)
1c4baffc 458 r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
eb56eb9b
MS
459 if (r < 0)
460 return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");
407fe036 461
1c4baffc 462 r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
eb56eb9b
MS
463 if (r < 0)
464 return log_error_errno(r, "Could not send rtnetlink message: %m");
407fe036 465
563c69c6
TG
466 link_ref(link);
467
407fe036
TG
468 return 0;
469}
470
11bf3cce
LP
471static int address_acquire(Link *link, Address *original, Address **ret) {
472 union in_addr_union in_addr = {};
473 struct in_addr broadcast = {};
0099bc15 474 _cleanup_address_free_ Address *na = NULL;
11bf3cce
LP
475 int r;
476
477 assert(link);
478 assert(original);
479 assert(ret);
480
481 /* Something useful was configured? just use it */
af93291c 482 if (in_addr_is_null(original->family, &original->in_addr) <= 0)
11bf3cce
LP
483 return 0;
484
485 /* The address is configured to be 0.0.0.0 or [::] by the user?
486 * Then let's acquire something more useful from the pool. */
487 r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
6a7a4e4d
LP
488 if (r < 0)
489 return log_link_error_errno(link, r, "Failed to acquire address from pool: %m");
11bf3cce 490 if (r == 0) {
79008bdd 491 log_link_error(link, "Couldn't find free address for interface, all taken.");
11bf3cce
LP
492 return -EBUSY;
493 }
494
495 if (original->family == AF_INET) {
d076c6f9 496 /* Pick first address in range for ourselves ... */
11bf3cce
LP
497 in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1);
498
499 /* .. and use last as broadcast address */
e87e2b78
SS
500 if (original->prefixlen > 30)
501 broadcast.s_addr = 0;
502 else
503 broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen);
11bf3cce
LP
504 } else if (original->family == AF_INET6)
505 in_addr.in6.s6_addr[15] |= 1;
506
f0213e37 507 r = address_new(&na);
11bf3cce
LP
508 if (r < 0)
509 return r;
510
511 na->family = original->family;
512 na->prefixlen = original->prefixlen;
513 na->scope = original->scope;
514 na->cinfo = original->cinfo;
515
516 if (original->label) {
517 na->label = strdup(original->label);
0099bc15 518 if (!na->label)
11bf3cce 519 return -ENOMEM;
11bf3cce
LP
520 }
521
522 na->broadcast = broadcast;
523 na->in_addr = in_addr;
524
525 LIST_PREPEND(addresses, link->pool_addresses, na);
526
1cc6c93a 527 *ret = TAKE_PTR(na);
0099bc15 528
11bf3cce
LP
529 return 0;
530}
531
1b566071
LP
532int address_configure(
533 Address *address,
534 Link *link,
535 sd_netlink_message_handler_t callback,
536 bool update) {
537
4afd3348 538 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
f579559b
TG
539 int r;
540
c166a070 541 assert(address);
4c701096 542 assert(IN_SET(address->family, AF_INET, AF_INET6));
c166a070
TG
543 assert(link);
544 assert(link->ifindex > 0);
f882c247 545 assert(link->manager);
c166a070 546 assert(link->manager->rtnl);
f882c247 547
1b566071
LP
548 /* If this is a new address, then refuse adding more than the limit */
549 if (address_get(link, address->family, &address->in_addr, address->prefixlen, NULL) <= 0 &&
550 set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX)
551 return -E2BIG;
552
11bf3cce
LP
553 r = address_acquire(link, address, &address);
554 if (r < 0)
555 return r;
556
66669078
TG
557 if (update)
558 r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req,
559 link->ifindex, address->family);
560 else
561 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR,
562 link->ifindex, address->family);
eb56eb9b
MS
563 if (r < 0)
564 return log_error_errno(r, "Could not allocate RTM_NEWADDR message: %m");
f579559b 565
5a723174 566 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
eb56eb9b
MS
567 if (r < 0)
568 return log_error_errno(r, "Could not set prefixlen: %m");
5a723174 569
851c9f82
PF
570 address->flags |= IFA_F_PERMANENT;
571
e63be084
SS
572 if (address->home_address)
573 address->flags |= IFA_F_HOMEADDRESS;
574
575 if (address->duplicate_address_detection)
576 address->flags |= IFA_F_NODAD;
577
578 if (address->manage_temporary_address)
579 address->flags |= IFA_F_MANAGETEMPADDR;
580
581 if (address->prefix_route)
582 address->flags |= IFA_F_NOPREFIXROUTE;
583
584 if (address->autojoin)
585 address->flags |= IFA_F_MCAUTOJOIN;
586
851c9f82 587 r = sd_rtnl_message_addr_set_flags(req, (address->flags & 0xff));
eb56eb9b
MS
588 if (r < 0)
589 return log_error_errno(r, "Could not set flags: %m");
5a723174 590
851c9f82 591 if (address->flags & ~0xff) {
1c4baffc 592 r = sd_netlink_message_append_u32(req, IFA_FLAGS, address->flags);
851c9f82
PF
593 if (r < 0)
594 return log_error_errno(r, "Could not set extended flags: %m");
595 }
596
5c1d3fc9 597 r = sd_rtnl_message_addr_set_scope(req, address->scope);
eb56eb9b
MS
598 if (r < 0)
599 return log_error_errno(r, "Could not set scope: %m");
5a723174 600
0a0dc69b 601 if (address->family == AF_INET)
1c4baffc 602 r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
0a0dc69b 603 else if (address->family == AF_INET6)
1c4baffc 604 r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
eb56eb9b
MS
605 if (r < 0)
606 return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");
f579559b 607
af93291c 608 if (!in_addr_is_null(address->family, &address->in_addr_peer)) {
c081882f 609 if (address->family == AF_INET)
1c4baffc 610 r = sd_netlink_message_append_in_addr(req, IFA_ADDRESS, &address->in_addr_peer.in);
c081882f 611 else if (address->family == AF_INET6)
1c4baffc 612 r = sd_netlink_message_append_in6_addr(req, IFA_ADDRESS, &address->in_addr_peer.in6);
eb56eb9b
MS
613 if (r < 0)
614 return log_error_errno(r, "Could not append IFA_ADDRESS attribute: %m");
c081882f
SS
615 } else {
616 if (address->family == AF_INET) {
e87e2b78
SS
617 if (address->prefixlen <= 30) {
618 r = sd_netlink_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast);
619 if (r < 0)
620 return log_error_errno(r, "Could not append IFA_BROADCAST attribute: %m");
621 }
c081882f 622 }
f579559b
TG
623 }
624
625 if (address->label) {
1c4baffc 626 r = sd_netlink_message_append_string(req, IFA_LABEL, address->label);
eb56eb9b
MS
627 if (r < 0)
628 return log_error_errno(r, "Could not append IFA_LABEL attribute: %m");
f579559b
TG
629 }
630
1c4baffc 631 r = sd_netlink_message_append_cache_info(req, IFA_CACHEINFO,
68ceb9df 632 &address->cinfo);
eb56eb9b
MS
633 if (r < 0)
634 return log_error_errno(r, "Could not append IFA_CACHEINFO attribute: %m");
68ceb9df 635
fcf50cff 636 r = address_establish(address, link);
eb56eb9b 637 if (r < 0)
fcf50cff
TG
638 return r;
639
640 r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
641 if (r < 0) {
642 address_release(address);
eb56eb9b 643 return log_error_errno(r, "Could not send rtnetlink message: %m");
fcf50cff 644 }
f579559b 645
563c69c6
TG
646 link_ref(link);
647
adda1ed9
TG
648 r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL);
649 if (r < 0) {
650 address_release(address);
651 return log_error_errno(r, "Could not add address: %m");
652 }
653
f579559b
TG
654 return 0;
655}
656
44e7b949
LP
657int config_parse_broadcast(
658 const char *unit,
eb0ea358
TG
659 const char *filename,
660 unsigned line,
661 const char *section,
662 unsigned section_line,
663 const char *lvalue,
664 int ltype,
665 const char *rvalue,
666 void *data,
667 void *userdata) {
44e7b949 668
eb0ea358
TG
669 Network *network = userdata;
670 _cleanup_address_free_ Address *n = NULL;
eb0ea358
TG
671 int r;
672
673 assert(filename);
674 assert(section);
675 assert(lvalue);
676 assert(rvalue);
677 assert(data);
678
f4859fc7 679 r = address_new_static(network, filename, section_line, &n);
eb0ea358
TG
680 if (r < 0)
681 return r;
682
482e2ac1 683 if (n->family == AF_INET6) {
12ca818f 684 log_syntax(unit, LOG_ERR, filename, line, 0, "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue);
482e2ac1
TG
685 return 0;
686 }
687
44e7b949 688 r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast);
eb0ea358 689 if (r < 0) {
12ca818f 690 log_syntax(unit, LOG_ERR, filename, line, r, "Broadcast is invalid, ignoring assignment: %s", rvalue);
eb0ea358
TG
691 return 0;
692 }
693
44e7b949 694 n->family = AF_INET;
eb0ea358
TG
695 n = NULL;
696
697 return 0;
698}
699
f579559b
TG
700int config_parse_address(const char *unit,
701 const char *filename,
702 unsigned line,
703 const char *section,
71a61510 704 unsigned section_line,
f579559b
TG
705 const char *lvalue,
706 int ltype,
707 const char *rvalue,
708 void *data,
709 void *userdata) {
44e7b949 710
6ae115c1 711 Network *network = userdata;
f579559b 712 _cleanup_address_free_ Address *n = NULL;
44e7b949
LP
713 const char *address, *e;
714 union in_addr_union buffer;
715 int r, f;
f579559b
TG
716
717 assert(filename);
6ae115c1 718 assert(section);
f579559b
TG
719 assert(lvalue);
720 assert(rvalue);
721 assert(data);
722
92fe133a
TG
723 if (streq(section, "Network")) {
724 /* we are not in an Address section, so treat
725 * this as the special '0' section */
f4859fc7
SS
726 r = address_new_static(network, NULL, 0, &n);
727 } else
728 r = address_new_static(network, filename, section_line, &n);
92fe133a 729
f579559b
TG
730 if (r < 0)
731 return r;
732
733 /* Address=address/prefixlen */
734
735 /* prefixlen */
736 e = strchr(rvalue, '/');
737 if (e) {
738 unsigned i;
12ca818f 739
f579559b
TG
740 r = safe_atou(e + 1, &i);
741 if (r < 0) {
12ca818f 742 log_syntax(unit, LOG_ERR, filename, line, r, "Prefix length is invalid, ignoring assignment: %s", e + 1);
f579559b
TG
743 return 0;
744 }
745
746 n->prefixlen = (unsigned char) i;
8cd11a0f 747
44e7b949
LP
748 address = strndupa(rvalue, e - rvalue);
749 } else
750 address = rvalue;
f579559b 751
44e7b949 752 r = in_addr_from_string_auto(address, &f, &buffer);
f579559b 753 if (r < 0) {
12ca818f 754 log_syntax(unit, LOG_ERR, filename, line, r, "Address is invalid, ignoring assignment: %s", address);
f579559b
TG
755 return 0;
756 }
757
a2a85a22 758 if (!e && f == AF_INET) {
5a941f5f 759 r = in4_addr_default_prefixlen(&buffer.in, &n->prefixlen);
a2a85a22 760 if (r < 0) {
87ac8d99 761 log_syntax(unit, LOG_ERR, filename, line, r, "Prefix length not specified, and a default one cannot be deduced for '%s', ignoring assignment", address);
a2a85a22
TG
762 return 0;
763 }
764 }
765
44e7b949 766 if (n->family != AF_UNSPEC && f != n->family) {
12ca818f 767 log_syntax(unit, LOG_ERR, filename, line, 0, "Address is incompatible, ignoring assignment: %s", address);
44e7b949
LP
768 return 0;
769 }
770
771 n->family = f;
772
773 if (streq(lvalue, "Address"))
774 n->in_addr = buffer;
775 else
776 n->in_addr_peer = buffer;
777
778 if (n->family == AF_INET && n->broadcast.s_addr == 0)
779 n->broadcast.s_addr = n->in_addr.in.s_addr | htonl(0xfffffffflu >> n->prefixlen);
eb0ea358 780
f579559b
TG
781 n = NULL;
782
783 return 0;
784}
6ae115c1 785
d31645ad
LP
786int config_parse_label(
787 const char *unit,
6ae115c1
TG
788 const char *filename,
789 unsigned line,
790 const char *section,
791 unsigned section_line,
792 const char *lvalue,
793 int ltype,
794 const char *rvalue,
795 void *data,
796 void *userdata) {
d31645ad 797
6ae115c1 798 _cleanup_address_free_ Address *n = NULL;
d31645ad 799 Network *network = userdata;
6ae115c1
TG
800 int r;
801
802 assert(filename);
803 assert(section);
804 assert(lvalue);
805 assert(rvalue);
806 assert(data);
807
f4859fc7 808 r = address_new_static(network, filename, section_line, &n);
6ae115c1
TG
809 if (r < 0)
810 return r;
811
a87d19fe
SS
812 if (!address_label_valid(rvalue)) {
813 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface label is too long or invalid, ignoring assignment: %s", rvalue);
6ae115c1
TG
814 return 0;
815 }
816
d31645ad
LP
817 r = free_and_strdup(&n->label, rvalue);
818 if (r < 0)
819 return log_oom();
6ae115c1
TG
820
821 n = NULL;
822
823 return 0;
824}
ce6c77eb 825
b5834a0b
SS
826int config_parse_lifetime(const char *unit,
827 const char *filename,
828 unsigned line,
829 const char *section,
830 unsigned section_line,
831 const char *lvalue,
832 int ltype,
833 const char *rvalue,
834 void *data,
835 void *userdata) {
836 Network *network = userdata;
837 _cleanup_address_free_ Address *n = NULL;
838 unsigned k;
839 int r;
840
841 assert(filename);
842 assert(section);
843 assert(lvalue);
844 assert(rvalue);
845 assert(data);
846
f4859fc7 847 r = address_new_static(network, filename, section_line, &n);
b5834a0b
SS
848 if (r < 0)
849 return r;
850
851 if (STR_IN_SET(rvalue, "forever", "infinity")) {
852 n->cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME;
853 n = NULL;
854
855 return 0;
856 }
857
858 r = safe_atou(rvalue, &k);
859 if (r < 0) {
860 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse PreferredLifetime, ignoring: %s", rvalue);
861 return 0;
862 }
863
864 if (k != 0)
865 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid PreferredLifetime value, ignoring: %d", k);
866 else {
867 n->cinfo.ifa_prefered = k;
868 n = NULL;
869 }
870
871 return 0;
872}
873
e63be084
SS
874int config_parse_address_flags(const char *unit,
875 const char *filename,
876 unsigned line,
877 const char *section,
878 unsigned section_line,
879 const char *lvalue,
880 int ltype,
881 const char *rvalue,
882 void *data,
883 void *userdata) {
884 Network *network = userdata;
885 _cleanup_address_free_ Address *n = NULL;
886 int r;
887
888 assert(filename);
889 assert(section);
890 assert(lvalue);
891 assert(rvalue);
892 assert(data);
893
f4859fc7 894 r = address_new_static(network, filename, section_line, &n);
e63be084
SS
895 if (r < 0)
896 return r;
897
898 r = parse_boolean(rvalue);
899 if (r < 0) {
900 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address flag, ignoring: %s", rvalue);
901 return 0;
902 }
903
904 if (streq(lvalue, "HomeAddress"))
905 n->home_address = r;
906 else if (streq(lvalue, "DuplicateAddressDetection"))
907 n->duplicate_address_detection = r;
908 else if (streq(lvalue, "ManageTemporaryAddress"))
909 n->manage_temporary_address = r;
910 else if (streq(lvalue, "PrefixRoute"))
911 n->prefix_route = r;
912 else if (streq(lvalue, "AutoJoin"))
913 n->autojoin = r;
914
915 return 0;
916}
917
2959fb07
SS
918int config_parse_address_scope(const char *unit,
919 const char *filename,
920 unsigned line,
921 const char *section,
922 unsigned section_line,
923 const char *lvalue,
924 int ltype,
925 const char *rvalue,
926 void *data,
927 void *userdata) {
928 Network *network = userdata;
929 _cleanup_address_free_ Address *n = NULL;
930 int r;
931
932 assert(filename);
933 assert(section);
934 assert(lvalue);
935 assert(rvalue);
936 assert(data);
937
938 r = address_new_static(network, filename, section_line, &n);
939 if (r < 0)
940 return r;
941
942 if (streq(rvalue, "host"))
943 n->scope = RT_SCOPE_HOST;
944 else if (streq(rvalue, "link"))
945 n->scope = RT_SCOPE_LINK;
946 else if (streq(rvalue, "global"))
947 n->scope = RT_SCOPE_UNIVERSE;
948 else {
949 r = safe_atou8(rvalue , &n->scope);
950 if (r < 0) {
951 log_syntax(unit, LOG_ERR, filename, line, r, "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue);
952 return 0;
953 }
954 }
955
956 n = NULL;
957
958 return 0;
959}
960
ce6c77eb
TG
961bool address_is_ready(const Address *a) {
962 assert(a);
963
b7ed5384
SS
964 if (a->family == AF_INET6)
965 return !(a->flags & IFA_F_TENTATIVE);
966 else
967 return !(a->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED));
ce6c77eb 968}