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