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