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