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