]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
60f067b4 JS |
2 | |
3 | #include <net/if.h> | |
4 | ||
db2df898 | 5 | #include "alloc-util.h" |
60f067b4 | 6 | #include "conf-parser.h" |
86f210e9 | 7 | #include "firewall-util.h" |
d9dfd233 | 8 | #include "netlink-util.h" |
d9dfd233 | 9 | #include "networkd-address.h" |
2897b343 | 10 | #include "networkd-manager.h" |
db2df898 MP |
11 | #include "parse-util.h" |
12 | #include "set.h" | |
aa27b158 | 13 | #include "socket-util.h" |
db2df898 | 14 | #include "string-util.h" |
6e866b33 | 15 | #include "strv.h" |
db2df898 MP |
16 | #include "utf8.h" |
17 | #include "util.h" | |
60f067b4 | 18 | |
5a920b42 MP |
19 | #define ADDRESSES_PER_LINK_MAX 2048U |
20 | #define STATIC_ADDRESSES_PER_NETWORK_MAX 1024U | |
21 | ||
db2df898 | 22 | int address_new(Address **ret) { |
b012e921 | 23 | _cleanup_(address_freep) Address *address = NULL; |
db2df898 | 24 | |
6e866b33 | 25 | address = new(Address, 1); |
db2df898 MP |
26 | if (!address) |
27 | return -ENOMEM; | |
60f067b4 | 28 | |
6e866b33 MB |
29 | *address = (Address) { |
30 | .family = AF_UNSPEC, | |
31 | .scope = RT_SCOPE_UNIVERSE, | |
32 | .cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME, | |
33 | .cinfo.ifa_valid = CACHE_INFO_INFINITY_LIFE_TIME, | |
34 | }; | |
db2df898 | 35 | |
b012e921 | 36 | *ret = TAKE_PTR(address); |
db2df898 MP |
37 | |
38 | return 0; | |
60f067b4 JS |
39 | } |
40 | ||
2897b343 | 41 | int address_new_static(Network *network, const char *filename, unsigned section_line, Address **ret) { |
b012e921 MB |
42 | _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL; |
43 | _cleanup_(address_freep) Address *address = NULL; | |
db2df898 | 44 | int r; |
60f067b4 | 45 | |
5a920b42 MP |
46 | assert(network); |
47 | assert(ret); | |
2897b343 | 48 | assert(!!filename == (section_line > 0)); |
5a920b42 | 49 | |
2897b343 MP |
50 | if (filename) { |
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); | |
60f067b4 | 56 | if (address) { |
b012e921 | 57 | *ret = TAKE_PTR(address); |
60f067b4 JS |
58 | |
59 | return 0; | |
60 | } | |
61 | } | |
62 | ||
5a920b42 MP |
63 | if (network->n_static_addresses >= STATIC_ADDRESSES_PER_NETWORK_MAX) |
64 | return -E2BIG; | |
65 | ||
db2df898 MP |
66 | r = address_new(&address); |
67 | if (r < 0) | |
68 | return r; | |
60f067b4 | 69 | |
6e866b33 MB |
70 | address->network = network; |
71 | LIST_APPEND(addresses, network->static_addresses, address); | |
72 | network->n_static_addresses++; | |
73 | ||
2897b343 | 74 | if (filename) { |
b012e921 | 75 | address->section = TAKE_PTR(n); |
2897b343 | 76 | |
6e866b33 MB |
77 | r = hashmap_ensure_allocated(&network->addresses_by_section, &network_config_hash_ops); |
78 | if (r < 0) | |
79 | return r; | |
80 | ||
2897b343 MP |
81 | r = hashmap_put(network->addresses_by_section, address->section, address); |
82 | if (r < 0) | |
83 | return r; | |
60f067b4 JS |
84 | } |
85 | ||
b012e921 | 86 | *ret = TAKE_PTR(address); |
60f067b4 JS |
87 | |
88 | return 0; | |
89 | } | |
90 | ||
60f067b4 JS |
91 | void address_free(Address *address) { |
92 | if (!address) | |
93 | return; | |
94 | ||
95 | if (address->network) { | |
96 | LIST_REMOVE(addresses, address->network->static_addresses, address); | |
5a920b42 MP |
97 | assert(address->network->n_static_addresses > 0); |
98 | address->network->n_static_addresses--; | |
60f067b4 | 99 | |
6e866b33 | 100 | if (address->section) |
2897b343 | 101 | hashmap_remove(address->network->addresses_by_section, address->section); |
60f067b4 JS |
102 | } |
103 | ||
db2df898 MP |
104 | if (address->link) { |
105 | set_remove(address->link->addresses, address); | |
106 | set_remove(address->link->addresses_foreign, address); | |
aa27b158 MP |
107 | |
108 | if (in_addr_equal(AF_INET6, &address->in_addr, (const union in_addr_union *) &address->link->ipv6ll_address)) | |
109 | memzero(&address->link->ipv6ll_address, sizeof(struct in6_addr)); | |
db2df898 MP |
110 | } |
111 | ||
6e866b33 MB |
112 | network_config_section_free(address->section); |
113 | free(address->label); | |
60f067b4 JS |
114 | free(address); |
115 | } | |
116 | ||
6e866b33 | 117 | static void address_hash_func(const Address *a, struct siphash *state) { |
db2df898 MP |
118 | assert(a); |
119 | ||
120 | siphash24_compress(&a->family, sizeof(a->family), state); | |
121 | ||
122 | switch (a->family) { | |
123 | case AF_INET: | |
124 | siphash24_compress(&a->prefixlen, sizeof(a->prefixlen), state); | |
125 | ||
126 | /* peer prefix */ | |
127 | if (a->prefixlen != 0) { | |
128 | uint32_t prefix; | |
129 | ||
130 | if (a->in_addr_peer.in.s_addr != 0) | |
131 | prefix = be32toh(a->in_addr_peer.in.s_addr) >> (32 - a->prefixlen); | |
132 | else | |
133 | prefix = be32toh(a->in_addr.in.s_addr) >> (32 - a->prefixlen); | |
134 | ||
135 | siphash24_compress(&prefix, sizeof(prefix), state); | |
136 | } | |
137 | ||
52ad194e | 138 | _fallthrough_; |
db2df898 MP |
139 | case AF_INET6: |
140 | /* local address */ | |
141 | siphash24_compress(&a->in_addr, FAMILY_ADDRESS_SIZE(a->family), state); | |
142 | ||
143 | break; | |
144 | default: | |
145 | /* treat any other address family as AF_UNSPEC */ | |
146 | break; | |
147 | } | |
148 | } | |
149 | ||
6e866b33 MB |
150 | static int address_compare_func(const Address *a1, const Address *a2) { |
151 | int r; | |
db2df898 | 152 | |
6e866b33 MB |
153 | r = CMP(a1->family, a2->family); |
154 | if (r != 0) | |
155 | return r; | |
db2df898 MP |
156 | |
157 | switch (a1->family) { | |
158 | /* use the same notion of equality as the kernel does */ | |
159 | case AF_INET: | |
6e866b33 MB |
160 | r = CMP(a1->prefixlen, a2->prefixlen); |
161 | if (r != 0) | |
162 | return r; | |
db2df898 MP |
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 | ||
6e866b33 MB |
180 | r = CMP(b1, b2); |
181 | if (r != 0) | |
182 | return r; | |
db2df898 MP |
183 | } |
184 | ||
52ad194e | 185 | _fallthrough_; |
db2df898 MP |
186 | case AF_INET6: |
187 | return memcmp(&a1->in_addr, &a2->in_addr, FAMILY_ADDRESS_SIZE(a1->family)); | |
188 | default: | |
189 | /* treat any other address family as AF_UNSPEC */ | |
190 | return 0; | |
191 | } | |
192 | } | |
193 | ||
6e866b33 | 194 | DEFINE_PRIVATE_HASH_OPS(address_hash_ops, Address, address_hash_func, address_compare_func); |
db2df898 MP |
195 | |
196 | bool 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 | ||
206 | static int address_establish(Address *address, Link *link) { | |
e735f4d4 MP |
207 | bool masq; |
208 | int r; | |
209 | ||
210 | assert(address); | |
211 | assert(link); | |
212 | ||
213 | masq = link->network && | |
db2df898 MP |
214 | link->network->ip_masquerade && |
215 | address->family == AF_INET && | |
216 | address->scope < RT_SCOPE_LINK; | |
e735f4d4 MP |
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 | ||
db2df898 MP |
233 | static 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) { | |
b012e921 | 238 | _cleanup_(address_freep) Address *address = NULL; |
e735f4d4 MP |
239 | int r; |
240 | ||
e735f4d4 | 241 | assert(link); |
db2df898 MP |
242 | assert(addresses); |
243 | assert(in_addr); | |
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; | |
252 | /* Consider address tentative until we get the real flags from the kernel */ | |
253 | address->flags = IFA_F_TENTATIVE; | |
254 | ||
255 | r = set_ensure_allocated(addresses, &address_hash_ops); | |
256 | if (r < 0) | |
257 | return r; | |
258 | ||
259 | r = set_put(*addresses, address); | |
260 | if (r < 0) | |
261 | return r; | |
262 | ||
263 | address->link = link; | |
264 | ||
265 | if (ret) | |
266 | *ret = address; | |
267 | ||
268 | address = NULL; | |
269 | ||
270 | return 0; | |
271 | } | |
272 | ||
273 | int 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 | ||
277 | int address_add(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) { | |
278 | Address *address; | |
279 | int r; | |
280 | ||
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 | |
302 | return r; | |
303 | ||
304 | if (ret) | |
305 | *ret = address; | |
306 | ||
307 | return 0; | |
308 | } | |
309 | ||
310 | static int address_release(Address *address) { | |
311 | int r; | |
312 | ||
313 | assert(address); | |
314 | assert(address->link); | |
e735f4d4 MP |
315 | |
316 | /* Remove masquerading firewall entry if it was added */ | |
317 | if (address->ip_masquerade_done) { | |
318 | union in_addr_union masked = address->in_addr; | |
319 | in_addr_mask(address->family, &masked, address->prefixlen); | |
320 | ||
321 | r = fw_add_masquerade(false, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0); | |
322 | if (r < 0) | |
db2df898 | 323 | log_link_warning_errno(address->link, r, "Failed to disable IP masquerading: %m"); |
e735f4d4 MP |
324 | |
325 | address->ip_masquerade_done = false; | |
326 | } | |
327 | ||
328 | return 0; | |
329 | } | |
330 | ||
5a920b42 MP |
331 | int address_update( |
332 | Address *address, | |
333 | unsigned char flags, | |
334 | unsigned char scope, | |
335 | const struct ifa_cacheinfo *cinfo) { | |
336 | ||
db2df898 | 337 | bool ready; |
60f067b4 JS |
338 | int r; |
339 | ||
340 | assert(address); | |
db2df898 | 341 | assert(cinfo); |
aa27b158 MP |
342 | assert_return(address->link, 1); |
343 | ||
344 | if (IN_SET(address->link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
345 | return 1; | |
60f067b4 | 346 | |
db2df898 | 347 | ready = address_is_ready(address); |
e735f4d4 | 348 | |
db2df898 MP |
349 | address->flags = flags; |
350 | address->scope = scope; | |
351 | address->cinfo = *cinfo; | |
60f067b4 | 352 | |
aa27b158 MP |
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; | |
db2df898 MP |
365 | } |
366 | } | |
60f067b4 | 367 | |
db2df898 MP |
368 | return 0; |
369 | } | |
370 | ||
371 | int address_drop(Address *address) { | |
372 | Link *link; | |
373 | bool ready; | |
374 | ||
375 | assert(address); | |
376 | ||
377 | ready = address_is_ready(address); | |
378 | link = address->link; | |
379 | ||
380 | address_release(address); | |
381 | address_free(address); | |
382 | ||
383 | link_update_operstate(link); | |
384 | ||
385 | if (link && !ready) | |
386 | link_check_ready(link); | |
e842803a | 387 | |
60f067b4 JS |
388 | return 0; |
389 | } | |
390 | ||
5a920b42 MP |
391 | int 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; | |
db2df898 MP |
398 | |
399 | assert(link); | |
400 | assert(in_addr); | |
db2df898 | 401 | |
5a920b42 MP |
402 | address = (Address) { |
403 | .family = family, | |
404 | .in_addr = *in_addr, | |
405 | .prefixlen = prefixlen, | |
406 | }; | |
db2df898 MP |
407 | |
408 | existing = set_get(link->addresses, &address); | |
409 | if (existing) { | |
5a920b42 MP |
410 | if (ret) |
411 | *ret = existing; | |
db2df898 | 412 | return 1; |
db2df898 MP |
413 | } |
414 | ||
5a920b42 MP |
415 | existing = set_get(link->addresses_foreign, &address); |
416 | if (existing) { | |
417 | if (ret) | |
418 | *ret = existing; | |
419 | return 0; | |
420 | } | |
db2df898 | 421 | |
5a920b42 | 422 | return -ENOENT; |
db2df898 MP |
423 | } |
424 | ||
6e866b33 MB |
425 | static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
426 | int r; | |
427 | ||
428 | assert(m); | |
429 | assert(link); | |
430 | assert(link->ifname); | |
431 | ||
432 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
433 | return 1; | |
434 | ||
435 | r = sd_netlink_message_get_errno(m); | |
436 | if (r < 0 && r != -EADDRNOTAVAIL) | |
437 | log_link_warning_errno(link, r, "Could not drop address: %m"); | |
438 | ||
439 | return 1; | |
440 | } | |
441 | ||
aa27b158 MP |
442 | int address_remove( |
443 | Address *address, | |
444 | Link *link, | |
6e866b33 | 445 | link_netlink_message_handler_t callback) { |
aa27b158 | 446 | |
4c89c718 | 447 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
6e866b33 | 448 | _cleanup_free_ char *b = NULL; |
60f067b4 JS |
449 | int r; |
450 | ||
451 | assert(address); | |
f5e65279 | 452 | assert(IN_SET(address->family, AF_INET, AF_INET6)); |
db2df898 | 453 | assert(link); |
60f067b4 JS |
454 | assert(link->ifindex > 0); |
455 | assert(link->manager); | |
456 | assert(link->manager->rtnl); | |
457 | ||
6e866b33 MB |
458 | if (DEBUG_LOGGING) { |
459 | if (in_addr_to_string(address->family, &address->in_addr, &b) >= 0) | |
460 | log_link_debug(link, "Removing address %s", b); | |
461 | } | |
462 | ||
db2df898 | 463 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR, |
60f067b4 | 464 | link->ifindex, address->family); |
f47781d8 | 465 | if (r < 0) |
db2df898 | 466 | return log_error_errno(r, "Could not allocate RTM_DELADDR message: %m"); |
60f067b4 JS |
467 | |
468 | r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen); | |
f47781d8 MP |
469 | if (r < 0) |
470 | return log_error_errno(r, "Could not set prefixlen: %m"); | |
60f067b4 | 471 | |
60f067b4 | 472 | if (address->family == AF_INET) |
86f210e9 | 473 | r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in); |
60f067b4 | 474 | else if (address->family == AF_INET6) |
86f210e9 | 475 | r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6); |
f47781d8 MP |
476 | if (r < 0) |
477 | return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m"); | |
60f067b4 | 478 | |
6e866b33 MB |
479 | r = netlink_call_async(link->manager->rtnl, NULL, req, |
480 | callback ?: address_remove_handler, | |
481 | link_netlink_destroy_callback, link); | |
f47781d8 MP |
482 | if (r < 0) |
483 | return log_error_errno(r, "Could not send rtnetlink message: %m"); | |
60f067b4 | 484 | |
e842803a MB |
485 | link_ref(link); |
486 | ||
487 | return 0; | |
488 | } | |
489 | ||
490 | static int address_acquire(Link *link, Address *original, Address **ret) { | |
491 | union in_addr_union in_addr = {}; | |
492 | struct in_addr broadcast = {}; | |
b012e921 | 493 | _cleanup_(address_freep) Address *na = NULL; |
e842803a MB |
494 | int r; |
495 | ||
496 | assert(link); | |
497 | assert(original); | |
498 | assert(ret); | |
499 | ||
500 | /* Something useful was configured? just use it */ | |
5eef597e | 501 | if (in_addr_is_null(original->family, &original->in_addr) <= 0) |
e842803a MB |
502 | return 0; |
503 | ||
504 | /* The address is configured to be 0.0.0.0 or [::] by the user? | |
505 | * Then let's acquire something more useful from the pool. */ | |
506 | r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr); | |
e3bff60a MP |
507 | if (r < 0) |
508 | return log_link_error_errno(link, r, "Failed to acquire address from pool: %m"); | |
e842803a | 509 | if (r == 0) { |
f47781d8 | 510 | log_link_error(link, "Couldn't find free address for interface, all taken."); |
e842803a MB |
511 | return -EBUSY; |
512 | } | |
513 | ||
514 | if (original->family == AF_INET) { | |
e735f4d4 | 515 | /* Pick first address in range for ourselves ... */ |
e842803a MB |
516 | in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1); |
517 | ||
518 | /* .. and use last as broadcast address */ | |
52ad194e MB |
519 | if (original->prefixlen > 30) |
520 | broadcast.s_addr = 0; | |
521 | else | |
522 | broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen); | |
e842803a MB |
523 | } else if (original->family == AF_INET6) |
524 | in_addr.in6.s6_addr[15] |= 1; | |
525 | ||
db2df898 | 526 | r = address_new(&na); |
e842803a MB |
527 | if (r < 0) |
528 | return r; | |
529 | ||
530 | na->family = original->family; | |
531 | na->prefixlen = original->prefixlen; | |
532 | na->scope = original->scope; | |
533 | na->cinfo = original->cinfo; | |
534 | ||
535 | if (original->label) { | |
536 | na->label = strdup(original->label); | |
537 | if (!na->label) | |
538 | return -ENOMEM; | |
539 | } | |
540 | ||
541 | na->broadcast = broadcast; | |
542 | na->in_addr = in_addr; | |
543 | ||
544 | LIST_PREPEND(addresses, link->pool_addresses, na); | |
545 | ||
b012e921 | 546 | *ret = TAKE_PTR(na); |
e842803a | 547 | |
60f067b4 JS |
548 | return 0; |
549 | } | |
550 | ||
5a920b42 MP |
551 | int address_configure( |
552 | Address *address, | |
553 | Link *link, | |
6e866b33 | 554 | link_netlink_message_handler_t callback, |
5a920b42 MP |
555 | bool update) { |
556 | ||
4c89c718 | 557 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
60f067b4 JS |
558 | int r; |
559 | ||
560 | assert(address); | |
f5e65279 | 561 | assert(IN_SET(address->family, AF_INET, AF_INET6)); |
60f067b4 JS |
562 | assert(link); |
563 | assert(link->ifindex > 0); | |
564 | assert(link->manager); | |
565 | assert(link->manager->rtnl); | |
6e866b33 | 566 | assert(callback); |
60f067b4 | 567 | |
5a920b42 MP |
568 | /* If this is a new address, then refuse adding more than the limit */ |
569 | if (address_get(link, address->family, &address->in_addr, address->prefixlen, NULL) <= 0 && | |
570 | set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX) | |
571 | return -E2BIG; | |
572 | ||
e842803a MB |
573 | r = address_acquire(link, address, &address); |
574 | if (r < 0) | |
575 | return r; | |
576 | ||
db2df898 MP |
577 | if (update) |
578 | r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req, | |
579 | link->ifindex, address->family); | |
580 | else | |
581 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR, | |
582 | link->ifindex, address->family); | |
f47781d8 MP |
583 | if (r < 0) |
584 | return log_error_errno(r, "Could not allocate RTM_NEWADDR message: %m"); | |
60f067b4 JS |
585 | |
586 | r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen); | |
f47781d8 MP |
587 | if (r < 0) |
588 | return log_error_errno(r, "Could not set prefixlen: %m"); | |
60f067b4 | 589 | |
e3bff60a MP |
590 | address->flags |= IFA_F_PERMANENT; |
591 | ||
8a584da2 MP |
592 | if (address->home_address) |
593 | address->flags |= IFA_F_HOMEADDRESS; | |
594 | ||
595 | if (address->duplicate_address_detection) | |
596 | address->flags |= IFA_F_NODAD; | |
597 | ||
598 | if (address->manage_temporary_address) | |
599 | address->flags |= IFA_F_MANAGETEMPADDR; | |
600 | ||
601 | if (address->prefix_route) | |
602 | address->flags |= IFA_F_NOPREFIXROUTE; | |
603 | ||
604 | if (address->autojoin) | |
605 | address->flags |= IFA_F_MCAUTOJOIN; | |
606 | ||
e3bff60a | 607 | r = sd_rtnl_message_addr_set_flags(req, (address->flags & 0xff)); |
f47781d8 MP |
608 | if (r < 0) |
609 | return log_error_errno(r, "Could not set flags: %m"); | |
60f067b4 | 610 | |
e3bff60a | 611 | if (address->flags & ~0xff) { |
86f210e9 | 612 | r = sd_netlink_message_append_u32(req, IFA_FLAGS, address->flags); |
e3bff60a MP |
613 | if (r < 0) |
614 | return log_error_errno(r, "Could not set extended flags: %m"); | |
615 | } | |
616 | ||
60f067b4 | 617 | r = sd_rtnl_message_addr_set_scope(req, address->scope); |
f47781d8 MP |
618 | if (r < 0) |
619 | return log_error_errno(r, "Could not set scope: %m"); | |
60f067b4 JS |
620 | |
621 | if (address->family == AF_INET) | |
86f210e9 | 622 | r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in); |
60f067b4 | 623 | else if (address->family == AF_INET6) |
86f210e9 | 624 | r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6); |
f47781d8 MP |
625 | if (r < 0) |
626 | return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m"); | |
60f067b4 | 627 | |
5eef597e MP |
628 | if (!in_addr_is_null(address->family, &address->in_addr_peer)) { |
629 | if (address->family == AF_INET) | |
86f210e9 | 630 | r = sd_netlink_message_append_in_addr(req, IFA_ADDRESS, &address->in_addr_peer.in); |
5eef597e | 631 | else if (address->family == AF_INET6) |
86f210e9 | 632 | r = sd_netlink_message_append_in6_addr(req, IFA_ADDRESS, &address->in_addr_peer.in6); |
f47781d8 MP |
633 | if (r < 0) |
634 | return log_error_errno(r, "Could not append IFA_ADDRESS attribute: %m"); | |
5eef597e MP |
635 | } else { |
636 | if (address->family == AF_INET) { | |
52ad194e MB |
637 | if (address->prefixlen <= 30) { |
638 | r = sd_netlink_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast); | |
639 | if (r < 0) | |
640 | return log_error_errno(r, "Could not append IFA_BROADCAST attribute: %m"); | |
641 | } | |
5eef597e | 642 | } |
60f067b4 JS |
643 | } |
644 | ||
645 | if (address->label) { | |
86f210e9 | 646 | r = sd_netlink_message_append_string(req, IFA_LABEL, address->label); |
f47781d8 MP |
647 | if (r < 0) |
648 | return log_error_errno(r, "Could not append IFA_LABEL attribute: %m"); | |
60f067b4 JS |
649 | } |
650 | ||
86f210e9 | 651 | r = sd_netlink_message_append_cache_info(req, IFA_CACHEINFO, |
e842803a | 652 | &address->cinfo); |
f47781d8 MP |
653 | if (r < 0) |
654 | return log_error_errno(r, "Could not append IFA_CACHEINFO attribute: %m"); | |
e842803a | 655 | |
db2df898 | 656 | r = address_establish(address, link); |
f47781d8 | 657 | if (r < 0) |
db2df898 MP |
658 | return r; |
659 | ||
6e866b33 MB |
660 | r = netlink_call_async(link->manager->rtnl, NULL, req, callback, |
661 | link_netlink_destroy_callback, link); | |
db2df898 MP |
662 | if (r < 0) { |
663 | address_release(address); | |
f47781d8 | 664 | return log_error_errno(r, "Could not send rtnetlink message: %m"); |
db2df898 | 665 | } |
60f067b4 | 666 | |
e842803a MB |
667 | link_ref(link); |
668 | ||
db2df898 MP |
669 | r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL); |
670 | if (r < 0) { | |
671 | address_release(address); | |
672 | return log_error_errno(r, "Could not add address: %m"); | |
673 | } | |
e735f4d4 | 674 | |
60f067b4 JS |
675 | return 0; |
676 | } | |
677 | ||
5eef597e MP |
678 | int config_parse_broadcast( |
679 | const char *unit, | |
60f067b4 JS |
680 | const char *filename, |
681 | unsigned line, | |
682 | const char *section, | |
683 | unsigned section_line, | |
684 | const char *lvalue, | |
685 | int ltype, | |
686 | const char *rvalue, | |
687 | void *data, | |
688 | void *userdata) { | |
60f067b4 | 689 | |
60f067b4 | 690 | Network *network = userdata; |
b012e921 | 691 | _cleanup_(address_freep) Address *n = NULL; |
60f067b4 JS |
692 | int r; |
693 | ||
694 | assert(filename); | |
695 | assert(section); | |
696 | assert(lvalue); | |
697 | assert(rvalue); | |
698 | assert(data); | |
699 | ||
2897b343 | 700 | r = address_new_static(network, filename, section_line, &n); |
60f067b4 JS |
701 | if (r < 0) |
702 | return r; | |
703 | ||
704 | if (n->family == AF_INET6) { | |
6300502b | 705 | log_syntax(unit, LOG_ERR, filename, line, 0, "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue); |
60f067b4 JS |
706 | return 0; |
707 | } | |
708 | ||
5eef597e | 709 | r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast); |
60f067b4 | 710 | if (r < 0) { |
6300502b | 711 | log_syntax(unit, LOG_ERR, filename, line, r, "Broadcast is invalid, ignoring assignment: %s", rvalue); |
60f067b4 JS |
712 | return 0; |
713 | } | |
714 | ||
5eef597e | 715 | n->family = AF_INET; |
60f067b4 JS |
716 | n = NULL; |
717 | ||
718 | return 0; | |
719 | } | |
720 | ||
721 | int config_parse_address(const char *unit, | |
722 | const char *filename, | |
723 | unsigned line, | |
724 | const char *section, | |
725 | unsigned section_line, | |
726 | const char *lvalue, | |
727 | int ltype, | |
728 | const char *rvalue, | |
729 | void *data, | |
730 | void *userdata) { | |
5eef597e | 731 | |
60f067b4 | 732 | Network *network = userdata; |
b012e921 | 733 | _cleanup_(address_freep) Address *n = NULL; |
5eef597e | 734 | union in_addr_union buffer; |
6e866b33 | 735 | unsigned char prefixlen; |
5eef597e | 736 | int r, f; |
60f067b4 JS |
737 | |
738 | assert(filename); | |
739 | assert(section); | |
740 | assert(lvalue); | |
741 | assert(rvalue); | |
742 | assert(data); | |
743 | ||
744 | if (streq(section, "Network")) { | |
745 | /* we are not in an Address section, so treat | |
746 | * this as the special '0' section */ | |
2897b343 MP |
747 | r = address_new_static(network, NULL, 0, &n); |
748 | } else | |
749 | r = address_new_static(network, filename, section_line, &n); | |
60f067b4 | 750 | |
60f067b4 JS |
751 | if (r < 0) |
752 | return r; | |
753 | ||
754 | /* Address=address/prefixlen */ | |
6e866b33 | 755 | r = in_addr_default_prefix_from_string_auto(rvalue, &f, &buffer, &prefixlen); |
60f067b4 | 756 | if (r < 0) { |
6e866b33 | 757 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid address '%s', ignoring assignment: %m", rvalue); |
60f067b4 JS |
758 | return 0; |
759 | } | |
760 | ||
5eef597e | 761 | if (n->family != AF_UNSPEC && f != n->family) { |
6e866b33 | 762 | log_syntax(unit, LOG_ERR, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue); |
5eef597e MP |
763 | return 0; |
764 | } | |
765 | ||
766 | n->family = f; | |
6e866b33 | 767 | n->prefixlen = prefixlen; |
5eef597e MP |
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); | |
60f067b4 JS |
776 | |
777 | n = NULL; | |
778 | ||
779 | return 0; | |
780 | } | |
781 | ||
aa27b158 MP |
782 | int config_parse_label( |
783 | const char *unit, | |
60f067b4 JS |
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) { | |
aa27b158 | 793 | |
b012e921 | 794 | _cleanup_(address_freep) Address *n = NULL; |
aa27b158 | 795 | Network *network = userdata; |
60f067b4 JS |
796 | int r; |
797 | ||
798 | assert(filename); | |
799 | assert(section); | |
800 | assert(lvalue); | |
801 | assert(rvalue); | |
802 | assert(data); | |
803 | ||
2897b343 | 804 | r = address_new_static(network, filename, section_line, &n); |
60f067b4 JS |
805 | if (r < 0) |
806 | return r; | |
807 | ||
2897b343 MP |
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); | |
aa27b158 MP |
810 | return 0; |
811 | } | |
812 | ||
813 | r = free_and_strdup(&n->label, rvalue); | |
814 | if (r < 0) | |
60f067b4 JS |
815 | return log_oom(); |
816 | ||
aa27b158 MP |
817 | n = NULL; |
818 | ||
819 | return 0; | |
820 | } | |
821 | ||
822 | int 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; | |
b012e921 | 833 | _cleanup_(address_freep) Address *n = NULL; |
aa27b158 MP |
834 | unsigned k; |
835 | int r; | |
836 | ||
837 | assert(filename); | |
838 | assert(section); | |
839 | assert(lvalue); | |
840 | assert(rvalue); | |
841 | assert(data); | |
842 | ||
2897b343 | 843 | r = address_new_static(network, filename, section_line, &n); |
aa27b158 MP |
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 | ||
60f067b4 JS |
851 | return 0; |
852 | } | |
853 | ||
aa27b158 MP |
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; | |
60f067b4 JS |
858 | } |
859 | ||
aa27b158 MP |
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 | } | |
60f067b4 JS |
866 | |
867 | return 0; | |
868 | } | |
869 | ||
8a584da2 MP |
870 | int 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; | |
b012e921 | 881 | _cleanup_(address_freep) Address *n = NULL; |
8a584da2 MP |
882 | int r; |
883 | ||
884 | assert(filename); | |
885 | assert(section); | |
886 | assert(lvalue); | |
887 | assert(rvalue); | |
888 | assert(data); | |
889 | ||
2897b343 | 890 | r = address_new_static(network, filename, section_line, &n); |
8a584da2 MP |
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 | ||
f5e65279 MB |
914 | int 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; | |
b012e921 | 925 | _cleanup_(address_freep) Address *n = NULL; |
f5e65279 MB |
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 | ||
db2df898 MP |
957 | bool address_is_ready(const Address *a) { |
958 | assert(a); | |
60f067b4 | 959 | |
52ad194e MB |
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)); | |
60f067b4 | 964 | } |