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