]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-address.c
Merge pull request #2719 from evverx/add-test-to-makefile
[thirdparty/systemd.git] / src / network / networkd-address.c
CommitLineData
f579559b
TG
1/***
2 This file is part of systemd.
3
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <net/if.h>
21
b5efdb8a 22#include "alloc-util.h"
f579559b 23#include "conf-parser.h"
12c2884c 24#include "firewall-util.h"
fc2f9534 25#include "netlink-util.h"
6bedfcbb 26#include "networkd-address.h"
07630cea 27#include "networkd.h"
6bedfcbb 28#include "parse-util.h"
3ac8e543 29#include "set.h"
07630cea 30#include "string-util.h"
3ac8e543
TG
31#include "utf8.h"
32#include "util.h"
f579559b 33
f0213e37
TG
34int address_new(Address **ret) {
35 _cleanup_address_free_ Address *address = NULL;
36
37 address = new0(Address, 1);
38 if (!address)
39 return -ENOMEM;
aba496a5
UTL
40
41 address->family = AF_UNSPEC;
42 address->scope = RT_SCOPE_UNIVERSE;
43 address->cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME;
44 address->cinfo.ifa_valid = CACHE_INFO_INFINITY_LIFE_TIME;
f0213e37
TG
45
46 *ret = address;
47 address = NULL;
48
49 return 0;
aba496a5
UTL
50}
51
f048a16b 52int address_new_static(Network *network, unsigned section, Address **ret) {
f579559b 53 _cleanup_address_free_ Address *address = NULL;
f0213e37 54 int r;
f579559b 55
6ae115c1 56 if (section) {
16aa63a0 57 address = hashmap_get(network->addresses_by_section, UINT_TO_PTR(section));
6ae115c1
TG
58 if (address) {
59 *ret = address;
60 address = NULL;
61
62 return 0;
63 }
64 }
65
f0213e37
TG
66 r = address_new(&address);
67 if (r < 0)
68 return r;
801bd9e8 69
f579559b
TG
70 address->network = network;
71
1e39ff92 72 LIST_APPEND(addresses, network->static_addresses, address);
f579559b 73
6ae115c1
TG
74 if (section) {
75 address->section = section;
16aa63a0
TG
76 hashmap_put(network->addresses_by_section,
77 UINT_TO_PTR(address->section), address);
6ae115c1
TG
78 }
79
f579559b
TG
80 *ret = address;
81 address = NULL;
82
83 return 0;
84}
85
86void address_free(Address *address) {
87 if (!address)
88 return;
89
f048a16b 90 if (address->network) {
3d3d4255 91 LIST_REMOVE(addresses, address->network->static_addresses, address);
f579559b 92
f048a16b
TG
93 if (address->section)
94 hashmap_remove(address->network->addresses_by_section,
16aa63a0 95 UINT_TO_PTR(address->section));
f048a16b 96 }
6ae115c1 97
adda1ed9 98 if (address->link) {
cf1d700d 99 set_remove(address->link->addresses, address);
adda1ed9
TG
100 set_remove(address->link->addresses_foreign, address);
101 }
cf1d700d 102
f579559b
TG
103 free(address);
104}
105
3ac8e543
TG
106static void address_hash_func(const void *b, struct siphash *state) {
107 const Address *a = b;
108
109 assert(a);
110
111 siphash24_compress(&a->family, sizeof(a->family), state);
112
113 switch (a->family) {
114 case AF_INET:
115 siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state);
116
117 /* peer prefix */
118 if (a->prefixlen != 0) {
119 uint32_t prefix;
120
121 if (a->in_addr_peer.in.s_addr != 0)
122 prefix = be32toh(a->in_addr_peer.in.s_addr) >> (32 - a->prefixlen);
123 else
124 prefix = be32toh(a->in_addr.in.s_addr) >> (32 - a->prefixlen);
125
126 siphash24_compress(&prefix, sizeof(prefix), state);
127 }
128
129 /* fallthrough */
130 case AF_INET6:
131 /* local address */
132 siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state);
133
134 break;
135 default:
136 /* treat any other address family as AF_UNSPEC */
137 break;
138 }
139}
140
141static int address_compare_func(const void *c1, const void *c2) {
142 const Address *a1 = c1, *a2 = c2;
143
144 if (a1->family < a2->family)
145 return -1;
146 if (a1->family > a2->family)
147 return 1;
148
149 switch (a1->family) {
150 /* use the same notion of equality as the kernel does */
151 case AF_INET:
152 if (a1->prefixlen < a2->prefixlen)
153 return -1;
154 if (a1->prefixlen > a2->prefixlen)
155 return 1;
156
157 /* compare the peer prefixes */
158 if (a1->prefixlen != 0) {
159 /* make sure we don't try to shift by 32.
160 * See ISO/IEC 9899:TC3 § 6.5.7.3. */
161 uint32_t b1, b2;
162
163 if (a1->in_addr_peer.in.s_addr != 0)
164 b1 = be32toh(a1->in_addr_peer.in.s_addr) >> (32 - a1->prefixlen);
165 else
166 b1 = be32toh(a1->in_addr.in.s_addr) >> (32 - a1->prefixlen);
167
168 if (a2->in_addr_peer.in.s_addr != 0)
169 b2 = be32toh(a2->in_addr_peer.in.s_addr) >> (32 - a1->prefixlen);
170 else
171 b2 = be32toh(a2->in_addr.in.s_addr) >> (32 - a1->prefixlen);
172
173 if (b1 < b2)
174 return -1;
175 if (b1 > b2)
176 return 1;
177 }
178
179 /* fall-through */
180 case AF_INET6:
181 return memcmp(&a1->in_addr, &a2->in_addr, FAMILY_ADDRESS_SIZE(a1->family));
182 default:
183 /* treat any other address family as AF_UNSPEC */
184 return 0;
185 }
186}
187
188static const struct hash_ops address_hash_ops = {
189 .hash = address_hash_func,
190 .compare = address_compare_func
191};
192
193bool address_equal(Address *a1, Address *a2) {
194 if (a1 == a2)
195 return true;
196
197 if (!a1 || !a2)
198 return false;
199
200 return address_compare_func(a1, a2) == 0;
201}
202
91b5f997
TG
203static int address_establish(Address *address, Link *link) {
204 bool masq;
205 int r;
206
207 assert(address);
208 assert(link);
209
210 masq = link->network &&
fcf50cff
TG
211 link->network->ip_masquerade &&
212 address->family == AF_INET &&
213 address->scope < RT_SCOPE_LINK;
91b5f997
TG
214
215 /* Add firewall entry if this is requested */
216 if (address->ip_masquerade_done != masq) {
217 union in_addr_union masked = address->in_addr;
218 in_addr_mask(address->family, &masked, address->prefixlen);
219
220 r = fw_add_masquerade(masq, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0);
221 if (r < 0)
222 log_link_warning_errno(link, r, "Could not enable IP masquerading: %m");
223
224 address->ip_masquerade_done = masq;
225 }
226
227 return 0;
228}
229
adda1ed9
TG
230static int address_add_internal(Link *link, Set **addresses,
231 int family,
232 const union in_addr_union *in_addr,
233 unsigned char prefixlen,
234 Address **ret) {
054f0db4 235 _cleanup_address_free_ Address *address = NULL;
cf1d700d
TG
236 int r;
237
238 assert(link);
adda1ed9 239 assert(addresses);
054f0db4 240 assert(in_addr);
054f0db4
TG
241
242 r = address_new(&address);
243 if (r < 0)
244 return r;
245
246 address->family = family;
247 address->in_addr = *in_addr;
248 address->prefixlen = prefixlen;
63bbe5c7
TG
249 /* Consider address tentative until we get the real flags from the kernel */
250 address->flags = IFA_F_TENTATIVE;
cf1d700d 251
adda1ed9 252 r = set_ensure_allocated(addresses, &address_hash_ops);
cf1d700d
TG
253 if (r < 0)
254 return r;
255
adda1ed9 256 r = set_put(*addresses, address);
cf1d700d
TG
257 if (r < 0)
258 return r;
259
260 address->link = link;
261
adda1ed9
TG
262 if (ret)
263 *ret = address;
264
054f0db4
TG
265 address = NULL;
266
cf1d700d
TG
267 return 0;
268}
269
adda1ed9
TG
270int address_add_foreign(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
271 return address_add_internal(link, &link->addresses_foreign, family, in_addr, prefixlen, ret);
272}
273
c4a03a56 274int address_add(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
cab974b0 275 Address *address;
e7780c8d
TG
276 int r;
277
cab974b0
TG
278 r = address_get(link, family, in_addr, prefixlen, &address);
279 if (r == -ENOENT) {
280 /* Address does not exist, create a new one */
281 r = address_add_internal(link, &link->addresses, family, in_addr, prefixlen, &address);
282 if (r < 0)
283 return r;
284 } else if (r == 0) {
285 /* Take over a foreign address */
286 r = set_ensure_allocated(&link->addresses, &address_hash_ops);
287 if (r < 0)
288 return r;
289
290 r = set_put(link->addresses, address);
291 if (r < 0)
292 return r;
293
294 set_remove(link->addresses_foreign, address);
295 } else if (r == 1) {
296 /* Already exists, do nothing */
297 ;
298 } else
e7780c8d
TG
299 return r;
300
cab974b0
TG
301 if (ret)
302 *ret = address;
e7780c8d
TG
303
304 return 0;
adda1ed9
TG
305}
306
fcf50cff 307static int address_release(Address *address) {
5a8bcb67
LP
308 int r;
309
310 assert(address);
fcf50cff 311 assert(address->link);
5a8bcb67 312
91b5f997
TG
313 /* Remove masquerading firewall entry if it was added */
314 if (address->ip_masquerade_done) {
5a8bcb67
LP
315 union in_addr_union masked = address->in_addr;
316 in_addr_mask(address->family, &masked, address->prefixlen);
317
91b5f997 318 r = fw_add_masquerade(false, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0);
5a8bcb67 319 if (r < 0)
fcf50cff 320 log_link_warning_errno(address->link, r, "Failed to disable IP masquerading: %m");
5a8bcb67 321
91b5f997 322 address->ip_masquerade_done = false;
5a8bcb67
LP
323 }
324
325 return 0;
326}
327
36c32f61
TG
328int address_update(Address *address, unsigned char flags, unsigned char scope, struct ifa_cacheinfo *cinfo) {
329 bool ready;
e7ab854c 330 int r;
36c32f61
TG
331
332 assert(address);
333 assert(cinfo);
334
335 ready = address_is_ready(address);
336
337 address->flags = flags;
338 address->scope = scope;
339 address->cinfo = *cinfo;
340
a3a019e1
TG
341 if (address->link) {
342 link_update_operstate(address->link);
343
e7ab854c 344 if (!ready && address_is_ready(address)) {
a3a019e1 345 link_check_ready(address->link);
e7ab854c
TG
346
347 if (address->family == AF_INET6 &&
c601ebf7
TG
348 in_addr_is_link_local(AF_INET6, &address->in_addr) > 0 &&
349 in_addr_is_null(AF_INET6, (const union in_addr_union*) &address->link->ipv6ll_address) > 0) {
350 r = link_ipv6ll_gained(address->link, &address->in_addr.in6);
e7ab854c
TG
351 if (r < 0)
352 return r;
353 }
354 }
a3a019e1 355 }
36c32f61
TG
356
357 return 0;
358}
359
91b5f997 360int address_drop(Address *address) {
8012cd39
TG
361 Link *link;
362 bool ready;
363
5a8bcb67 364 assert(address);
91b5f997 365
8012cd39
TG
366 ready = address_is_ready(address);
367 link = address->link;
368
fcf50cff 369 address_release(address);
91b5f997
TG
370 address_free(address);
371
84de38c5
TG
372 link_update_operstate(link);
373
8012cd39
TG
374 if (link && !ready)
375 link_check_ready(link);
376
91b5f997
TG
377 return 0;
378}
379
380int address_get(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) {
381 Address address = {}, *existing;
382
5a8bcb67 383 assert(link);
91b5f997
TG
384 assert(in_addr);
385 assert(ret);
5a8bcb67 386
91b5f997
TG
387 address.family = family;
388 address.in_addr = *in_addr;
389 address.prefixlen = prefixlen;
5a8bcb67 390
91b5f997 391 existing = set_get(link->addresses, &address);
cab974b0
TG
392 if (existing) {
393 *ret = existing;
394
395 return 1;
396 } else {
adda1ed9
TG
397 existing = set_get(link->addresses_foreign, &address);
398 if (!existing)
399 return -ENOENT;
400 }
5a8bcb67 401
91b5f997 402 *ret = existing;
5a8bcb67
LP
403
404 return 0;
405}
406
91b5f997 407int address_remove(Address *address, Link *link,
1c4baffc 408 sd_netlink_message_handler_t callback) {
4afd3348 409 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
407fe036
TG
410 int r;
411
412 assert(address);
413 assert(address->family == AF_INET || address->family == AF_INET6);
414 assert(link);
415 assert(link->ifindex > 0);
416 assert(link->manager);
417 assert(link->manager->rtnl);
418
151b9b96
LP
419 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR,
420 link->ifindex, address->family);
eb56eb9b
MS
421 if (r < 0)
422 return log_error_errno(r, "Could not allocate RTM_DELADDR message: %m");
407fe036 423
5a723174 424 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
eb56eb9b
MS
425 if (r < 0)
426 return log_error_errno(r, "Could not set prefixlen: %m");
5a723174 427
407fe036 428 if (address->family == AF_INET)
1c4baffc 429 r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
407fe036 430 else if (address->family == AF_INET6)
1c4baffc 431 r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
eb56eb9b
MS
432 if (r < 0)
433 return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");
407fe036 434
1c4baffc 435 r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
eb56eb9b
MS
436 if (r < 0)
437 return log_error_errno(r, "Could not send rtnetlink message: %m");
407fe036 438
563c69c6
TG
439 link_ref(link);
440
407fe036
TG
441 return 0;
442}
443
11bf3cce
LP
444static int address_acquire(Link *link, Address *original, Address **ret) {
445 union in_addr_union in_addr = {};
446 struct in_addr broadcast = {};
0099bc15 447 _cleanup_address_free_ Address *na = NULL;
11bf3cce
LP
448 int r;
449
450 assert(link);
451 assert(original);
452 assert(ret);
453
454 /* Something useful was configured? just use it */
af93291c 455 if (in_addr_is_null(original->family, &original->in_addr) <= 0)
11bf3cce
LP
456 return 0;
457
458 /* The address is configured to be 0.0.0.0 or [::] by the user?
459 * Then let's acquire something more useful from the pool. */
460 r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr);
6a7a4e4d
LP
461 if (r < 0)
462 return log_link_error_errno(link, r, "Failed to acquire address from pool: %m");
11bf3cce 463 if (r == 0) {
79008bdd 464 log_link_error(link, "Couldn't find free address for interface, all taken.");
11bf3cce
LP
465 return -EBUSY;
466 }
467
468 if (original->family == AF_INET) {
d076c6f9 469 /* Pick first address in range for ourselves ... */
11bf3cce
LP
470 in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1);
471
472 /* .. and use last as broadcast address */
473 broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen);
474 } else if (original->family == AF_INET6)
475 in_addr.in6.s6_addr[15] |= 1;
476
f0213e37 477 r = address_new(&na);
11bf3cce
LP
478 if (r < 0)
479 return r;
480
481 na->family = original->family;
482 na->prefixlen = original->prefixlen;
483 na->scope = original->scope;
484 na->cinfo = original->cinfo;
485
486 if (original->label) {
487 na->label = strdup(original->label);
0099bc15 488 if (!na->label)
11bf3cce 489 return -ENOMEM;
11bf3cce
LP
490 }
491
492 na->broadcast = broadcast;
493 na->in_addr = in_addr;
494
495 LIST_PREPEND(addresses, link->pool_addresses, na);
496
497 *ret = na;
0099bc15
SS
498 na = NULL;
499
11bf3cce
LP
500 return 0;
501}
502
66669078 503int address_configure(Address *address, Link *link, sd_netlink_message_handler_t callback, bool update) {
4afd3348 504 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
f579559b
TG
505 int r;
506
c166a070
TG
507 assert(address);
508 assert(address->family == AF_INET || address->family == AF_INET6);
509 assert(link);
510 assert(link->ifindex > 0);
f882c247 511 assert(link->manager);
c166a070 512 assert(link->manager->rtnl);
f882c247 513
11bf3cce
LP
514 r = address_acquire(link, address, &address);
515 if (r < 0)
516 return r;
517
66669078
TG
518 if (update)
519 r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req,
520 link->ifindex, address->family);
521 else
522 r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR,
523 link->ifindex, address->family);
eb56eb9b
MS
524 if (r < 0)
525 return log_error_errno(r, "Could not allocate RTM_NEWADDR message: %m");
f579559b 526
5a723174 527 r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
eb56eb9b
MS
528 if (r < 0)
529 return log_error_errno(r, "Could not set prefixlen: %m");
5a723174 530
851c9f82
PF
531 address->flags |= IFA_F_PERMANENT;
532
533 r = sd_rtnl_message_addr_set_flags(req, (address->flags & 0xff));
eb56eb9b
MS
534 if (r < 0)
535 return log_error_errno(r, "Could not set flags: %m");
5a723174 536
851c9f82 537 if (address->flags & ~0xff) {
1c4baffc 538 r = sd_netlink_message_append_u32(req, IFA_FLAGS, address->flags);
851c9f82
PF
539 if (r < 0)
540 return log_error_errno(r, "Could not set extended flags: %m");
541 }
542
5c1d3fc9 543 r = sd_rtnl_message_addr_set_scope(req, address->scope);
eb56eb9b
MS
544 if (r < 0)
545 return log_error_errno(r, "Could not set scope: %m");
5a723174 546
0a0dc69b 547 if (address->family == AF_INET)
1c4baffc 548 r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
0a0dc69b 549 else if (address->family == AF_INET6)
1c4baffc 550 r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
eb56eb9b
MS
551 if (r < 0)
552 return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");
f579559b 553
af93291c 554 if (!in_addr_is_null(address->family, &address->in_addr_peer)) {
c081882f 555 if (address->family == AF_INET)
1c4baffc 556 r = sd_netlink_message_append_in_addr(req, IFA_ADDRESS, &address->in_addr_peer.in);
c081882f 557 else if (address->family == AF_INET6)
1c4baffc 558 r = sd_netlink_message_append_in6_addr(req, IFA_ADDRESS, &address->in_addr_peer.in6);
eb56eb9b
MS
559 if (r < 0)
560 return log_error_errno(r, "Could not append IFA_ADDRESS attribute: %m");
c081882f
SS
561 } else {
562 if (address->family == AF_INET) {
1c4baffc 563 r = sd_netlink_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast);
eb56eb9b
MS
564 if (r < 0)
565 return log_error_errno(r, "Could not append IFA_BROADCAST attribute: %m");
c081882f 566 }
f579559b
TG
567 }
568
569 if (address->label) {
1c4baffc 570 r = sd_netlink_message_append_string(req, IFA_LABEL, address->label);
eb56eb9b
MS
571 if (r < 0)
572 return log_error_errno(r, "Could not append IFA_LABEL attribute: %m");
f579559b
TG
573 }
574
1c4baffc 575 r = sd_netlink_message_append_cache_info(req, IFA_CACHEINFO,
68ceb9df 576 &address->cinfo);
eb56eb9b
MS
577 if (r < 0)
578 return log_error_errno(r, "Could not append IFA_CACHEINFO attribute: %m");
68ceb9df 579
fcf50cff 580 r = address_establish(address, link);
eb56eb9b 581 if (r < 0)
fcf50cff
TG
582 return r;
583
584 r = sd_netlink_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
585 if (r < 0) {
586 address_release(address);
eb56eb9b 587 return log_error_errno(r, "Could not send rtnetlink message: %m");
fcf50cff 588 }
f579559b 589
563c69c6
TG
590 link_ref(link);
591
adda1ed9
TG
592 r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL);
593 if (r < 0) {
594 address_release(address);
595 return log_error_errno(r, "Could not add address: %m");
596 }
597
f579559b
TG
598 return 0;
599}
600
44e7b949
LP
601int config_parse_broadcast(
602 const char *unit,
eb0ea358
TG
603 const char *filename,
604 unsigned line,
605 const char *section,
606 unsigned section_line,
607 const char *lvalue,
608 int ltype,
609 const char *rvalue,
610 void *data,
611 void *userdata) {
44e7b949 612
eb0ea358
TG
613 Network *network = userdata;
614 _cleanup_address_free_ Address *n = NULL;
eb0ea358
TG
615 int r;
616
617 assert(filename);
618 assert(section);
619 assert(lvalue);
620 assert(rvalue);
621 assert(data);
622
623 r = address_new_static(network, section_line, &n);
624 if (r < 0)
625 return r;
626
482e2ac1 627 if (n->family == AF_INET6) {
12ca818f 628 log_syntax(unit, LOG_ERR, filename, line, 0, "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue);
482e2ac1
TG
629 return 0;
630 }
631
44e7b949 632 r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast);
eb0ea358 633 if (r < 0) {
12ca818f 634 log_syntax(unit, LOG_ERR, filename, line, r, "Broadcast is invalid, ignoring assignment: %s", rvalue);
eb0ea358
TG
635 return 0;
636 }
637
44e7b949 638 n->family = AF_INET;
eb0ea358
TG
639 n = NULL;
640
641 return 0;
642}
643
f579559b
TG
644int config_parse_address(const char *unit,
645 const char *filename,
646 unsigned line,
647 const char *section,
71a61510 648 unsigned section_line,
f579559b
TG
649 const char *lvalue,
650 int ltype,
651 const char *rvalue,
652 void *data,
653 void *userdata) {
44e7b949 654
6ae115c1 655 Network *network = userdata;
f579559b 656 _cleanup_address_free_ Address *n = NULL;
44e7b949
LP
657 const char *address, *e;
658 union in_addr_union buffer;
659 int r, f;
f579559b
TG
660
661 assert(filename);
6ae115c1 662 assert(section);
f579559b
TG
663 assert(lvalue);
664 assert(rvalue);
665 assert(data);
666
92fe133a
TG
667 if (streq(section, "Network")) {
668 /* we are not in an Address section, so treat
669 * this as the special '0' section */
670 section_line = 0;
671 }
672
f048a16b 673 r = address_new_static(network, section_line, &n);
f579559b
TG
674 if (r < 0)
675 return r;
676
677 /* Address=address/prefixlen */
678
679 /* prefixlen */
680 e = strchr(rvalue, '/');
681 if (e) {
682 unsigned i;
12ca818f 683
f579559b
TG
684 r = safe_atou(e + 1, &i);
685 if (r < 0) {
12ca818f 686 log_syntax(unit, LOG_ERR, filename, line, r, "Prefix length is invalid, ignoring assignment: %s", e + 1);
f579559b
TG
687 return 0;
688 }
689
690 n->prefixlen = (unsigned char) i;
8cd11a0f 691
44e7b949
LP
692 address = strndupa(rvalue, e - rvalue);
693 } else
694 address = rvalue;
f579559b 695
44e7b949 696 r = in_addr_from_string_auto(address, &f, &buffer);
f579559b 697 if (r < 0) {
12ca818f 698 log_syntax(unit, LOG_ERR, filename, line, r, "Address is invalid, ignoring assignment: %s", address);
f579559b
TG
699 return 0;
700 }
701
a2a85a22
TG
702 if (!e && f == AF_INET) {
703 r = in_addr_default_prefixlen(&buffer.in, &n->prefixlen);
704 if (r < 0) {
12ca818f 705 log_syntax(unit, LOG_ERR, filename, line, r, "Prefix length not specified, and a default one can not be deduced for '%s', ignoring assignment", address);
a2a85a22
TG
706 return 0;
707 }
708 }
709
44e7b949 710 if (n->family != AF_UNSPEC && f != n->family) {
12ca818f 711 log_syntax(unit, LOG_ERR, filename, line, 0, "Address is incompatible, ignoring assignment: %s", address);
44e7b949
LP
712 return 0;
713 }
714
715 n->family = f;
716
717 if (streq(lvalue, "Address"))
718 n->in_addr = buffer;
719 else
720 n->in_addr_peer = buffer;
721
722 if (n->family == AF_INET && n->broadcast.s_addr == 0)
723 n->broadcast.s_addr = n->in_addr.in.s_addr | htonl(0xfffffffflu >> n->prefixlen);
eb0ea358 724
f579559b
TG
725 n = NULL;
726
727 return 0;
728}
6ae115c1
TG
729
730int config_parse_label(const char *unit,
731 const char *filename,
732 unsigned line,
733 const char *section,
734 unsigned section_line,
735 const char *lvalue,
736 int ltype,
737 const char *rvalue,
738 void *data,
739 void *userdata) {
740 Network *network = userdata;
741 _cleanup_address_free_ Address *n = NULL;
6ae115c1
TG
742 char *label;
743 int r;
744
745 assert(filename);
746 assert(section);
747 assert(lvalue);
748 assert(rvalue);
749 assert(data);
750
f048a16b 751 r = address_new_static(network, section_line, &n);
6ae115c1
TG
752 if (r < 0)
753 return r;
754
755 label = strdup(rvalue);
756 if (!label)
757 return log_oom();
758
759 if (!ascii_is_valid(label) || strlen(label) >= IFNAMSIZ) {
12ca818f 760 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface label is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
6ae115c1
TG
761 free(label);
762 return 0;
763 }
764
765 free(n->label);
766 if (*label)
767 n->label = label;
768 else {
769 free(label);
770 n->label = NULL;
771 }
772
773 n = NULL;
774
775 return 0;
776}
ce6c77eb
TG
777
778bool address_is_ready(const Address *a) {
779 assert(a);
780
63bbe5c7 781 return !(a->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED));
ce6c77eb 782}