]>
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 | ||
7a08d314 | 117 | static void address_hash_func(const Address *a, struct siphash *state) { |
3ac8e543 TG |
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 | ||
4831981d | 138 | _fallthrough_; |
3ac8e543 TG |
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 | ||
7a08d314 | 150 | static int address_compare_func(const Address *a1, const Address *a2) { |
a0edd02e | 151 | int r; |
3ac8e543 | 152 | |
a0edd02e FB |
153 | r = CMP(a1->family, a2->family); |
154 | if (r != 0) | |
155 | return r; | |
3ac8e543 TG |
156 | |
157 | switch (a1->family) { | |
158 | /* use the same notion of equality as the kernel does */ | |
159 | case AF_INET: | |
a0edd02e FB |
160 | r = CMP(a1->prefixlen, a2->prefixlen); |
161 | if (r != 0) | |
162 | return r; | |
3ac8e543 TG |
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 | ||
a0edd02e FB |
180 | r = CMP(b1, b2); |
181 | if (r != 0) | |
182 | return r; | |
3ac8e543 TG |
183 | } |
184 | ||
4831981d | 185 | _fallthrough_; |
3ac8e543 TG |
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 | ||
7a08d314 | 194 | DEFINE_PRIVATE_HASH_OPS(address_hash_ops, Address, address_hash_func, address_compare_func); |
3ac8e543 TG |
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 | ||
91b5f997 TG |
206 | static int address_establish(Address *address, Link *link) { |
207 | bool masq; | |
208 | int r; | |
209 | ||
210 | assert(address); | |
211 | assert(link); | |
212 | ||
213 | masq = link->network && | |
fcf50cff TG |
214 | link->network->ip_masquerade && |
215 | address->family == AF_INET && | |
216 | address->scope < RT_SCOPE_LINK; | |
91b5f997 TG |
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 | ||
adda1ed9 TG |
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) { | |
8e766630 | 238 | _cleanup_(address_freep) Address *address = NULL; |
cf1d700d TG |
239 | int r; |
240 | ||
241 | assert(link); | |
adda1ed9 | 242 | assert(addresses); |
054f0db4 | 243 | assert(in_addr); |
054f0db4 TG |
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; | |
63bbe5c7 TG |
252 | /* Consider address tentative until we get the real flags from the kernel */ |
253 | address->flags = IFA_F_TENTATIVE; | |
cf1d700d | 254 | |
adda1ed9 | 255 | r = set_ensure_allocated(addresses, &address_hash_ops); |
cf1d700d TG |
256 | if (r < 0) |
257 | return r; | |
258 | ||
adda1ed9 | 259 | r = set_put(*addresses, address); |
cf1d700d TG |
260 | if (r < 0) |
261 | return r; | |
262 | ||
263 | address->link = link; | |
264 | ||
adda1ed9 TG |
265 | if (ret) |
266 | *ret = address; | |
267 | ||
054f0db4 TG |
268 | address = NULL; |
269 | ||
cf1d700d TG |
270 | return 0; |
271 | } | |
272 | ||
adda1ed9 TG |
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 | ||
c4a03a56 | 277 | int address_add(Link *link, int family, const union in_addr_union *in_addr, unsigned char prefixlen, Address **ret) { |
cab974b0 | 278 | Address *address; |
e7780c8d TG |
279 | int r; |
280 | ||
cab974b0 TG |
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 | |
e7780c8d TG |
302 | return r; |
303 | ||
cab974b0 TG |
304 | if (ret) |
305 | *ret = address; | |
e7780c8d TG |
306 | |
307 | return 0; | |
adda1ed9 TG |
308 | } |
309 | ||
fcf50cff | 310 | static int address_release(Address *address) { |
5a8bcb67 LP |
311 | int r; |
312 | ||
313 | assert(address); | |
fcf50cff | 314 | assert(address->link); |
5a8bcb67 | 315 | |
91b5f997 TG |
316 | /* Remove masquerading firewall entry if it was added */ |
317 | if (address->ip_masquerade_done) { | |
5a8bcb67 LP |
318 | union in_addr_union masked = address->in_addr; |
319 | in_addr_mask(address->family, &masked, address->prefixlen); | |
320 | ||
91b5f997 | 321 | r = fw_add_masquerade(false, AF_INET, 0, &masked, address->prefixlen, NULL, NULL, 0); |
5a8bcb67 | 322 | if (r < 0) |
fcf50cff | 323 | log_link_warning_errno(address->link, r, "Failed to disable IP masquerading: %m"); |
5a8bcb67 | 324 | |
91b5f997 | 325 | address->ip_masquerade_done = false; |
5a8bcb67 LP |
326 | } |
327 | ||
328 | return 0; | |
329 | } | |
330 | ||
889b550f LP |
331 | int address_update( |
332 | Address *address, | |
333 | unsigned char flags, | |
334 | unsigned char scope, | |
335 | const struct ifa_cacheinfo *cinfo) { | |
336 | ||
36c32f61 | 337 | bool ready; |
e7ab854c | 338 | int r; |
36c32f61 TG |
339 | |
340 | assert(address); | |
341 | assert(cinfo); | |
7209086d SS |
342 | assert_return(address->link, 1); |
343 | ||
344 | if (IN_SET(address->link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
345 | return 1; | |
36c32f61 TG |
346 | |
347 | ready = address_is_ready(address); | |
348 | ||
349 | address->flags = flags; | |
350 | address->scope = scope; | |
351 | address->cinfo = *cinfo; | |
352 | ||
7209086d | 353 | link_update_operstate(address->link); |
c8f7123e | 354 | link_check_ready(address->link); |
7209086d | 355 | |
c8f7123e YW |
356 | if (!ready && |
357 | address_is_ready(address) && | |
358 | 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) { | |
7209086d | 361 | |
c8f7123e YW |
362 | r = link_ipv6ll_gained(address->link, &address->in_addr.in6); |
363 | if (r < 0) | |
364 | return r; | |
a3a019e1 | 365 | } |
36c32f61 TG |
366 | |
367 | return 0; | |
368 | } | |
369 | ||
91b5f997 | 370 | int address_drop(Address *address) { |
8012cd39 TG |
371 | Link *link; |
372 | bool ready; | |
373 | ||
5a8bcb67 | 374 | assert(address); |
91b5f997 | 375 | |
8012cd39 TG |
376 | ready = address_is_ready(address); |
377 | link = address->link; | |
378 | ||
fcf50cff | 379 | address_release(address); |
91b5f997 TG |
380 | address_free(address); |
381 | ||
84de38c5 TG |
382 | link_update_operstate(link); |
383 | ||
8012cd39 TG |
384 | if (link && !ready) |
385 | link_check_ready(link); | |
386 | ||
91b5f997 TG |
387 | return 0; |
388 | } | |
389 | ||
1b566071 LP |
390 | int address_get(Link *link, |
391 | int family, | |
392 | const union in_addr_union *in_addr, | |
393 | unsigned char prefixlen, | |
394 | Address **ret) { | |
395 | ||
396 | Address address, *existing; | |
91b5f997 | 397 | |
5a8bcb67 | 398 | assert(link); |
91b5f997 | 399 | assert(in_addr); |
5a8bcb67 | 400 | |
1b566071 LP |
401 | address = (Address) { |
402 | .family = family, | |
403 | .in_addr = *in_addr, | |
404 | .prefixlen = prefixlen, | |
405 | }; | |
5a8bcb67 | 406 | |
91b5f997 | 407 | existing = set_get(link->addresses, &address); |
cab974b0 | 408 | if (existing) { |
1b566071 LP |
409 | if (ret) |
410 | *ret = existing; | |
cab974b0 | 411 | return 1; |
adda1ed9 | 412 | } |
5a8bcb67 | 413 | |
1b566071 LP |
414 | existing = set_get(link->addresses_foreign, &address); |
415 | if (existing) { | |
416 | if (ret) | |
417 | *ret = existing; | |
418 | return 0; | |
419 | } | |
5a8bcb67 | 420 | |
1b566071 | 421 | return -ENOENT; |
5a8bcb67 LP |
422 | } |
423 | ||
302a796f | 424 | static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
63ae0569 YW |
425 | int r; |
426 | ||
427 | assert(m); | |
428 | assert(link); | |
429 | assert(link->ifname); | |
430 | ||
431 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
432 | return 1; | |
433 | ||
434 | r = sd_netlink_message_get_errno(m); | |
435 | if (r < 0 && r != -EADDRNOTAVAIL) | |
436 | log_link_warning_errno(link, r, "Could not drop address: %m"); | |
437 | ||
438 | return 1; | |
439 | } | |
440 | ||
483d099e ZJS |
441 | int address_remove( |
442 | Address *address, | |
443 | Link *link, | |
302a796f | 444 | link_netlink_message_handler_t callback) { |
483d099e | 445 | |
4afd3348 | 446 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
30226d27 | 447 | _cleanup_free_ char *b = NULL; |
407fe036 TG |
448 | int r; |
449 | ||
450 | assert(address); | |
4c701096 | 451 | assert(IN_SET(address->family, AF_INET, AF_INET6)); |
407fe036 TG |
452 | assert(link); |
453 | assert(link->ifindex > 0); | |
454 | assert(link->manager); | |
455 | assert(link->manager->rtnl); | |
456 | ||
30226d27 TJ |
457 | if (DEBUG_LOGGING) { |
458 | if (in_addr_to_string(address->family, &address->in_addr, &b) >= 0) | |
459 | log_link_debug(link, "Removing address %s", b); | |
460 | } | |
461 | ||
151b9b96 LP |
462 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR, |
463 | link->ifindex, address->family); | |
eb56eb9b MS |
464 | if (r < 0) |
465 | return log_error_errno(r, "Could not allocate RTM_DELADDR message: %m"); | |
407fe036 | 466 | |
5a723174 | 467 | r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen); |
eb56eb9b MS |
468 | if (r < 0) |
469 | return log_error_errno(r, "Could not set prefixlen: %m"); | |
5a723174 | 470 | |
407fe036 | 471 | if (address->family == AF_INET) |
1c4baffc | 472 | r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in); |
407fe036 | 473 | else if (address->family == AF_INET6) |
1c4baffc | 474 | r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6); |
eb56eb9b MS |
475 | if (r < 0) |
476 | return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m"); | |
407fe036 | 477 | |
302a796f YW |
478 | r = netlink_call_async(link->manager->rtnl, NULL, req, |
479 | callback ?: address_remove_handler, | |
480 | link_netlink_destroy_callback, link); | |
eb56eb9b MS |
481 | if (r < 0) |
482 | return log_error_errno(r, "Could not send rtnetlink message: %m"); | |
407fe036 | 483 | |
563c69c6 TG |
484 | link_ref(link); |
485 | ||
407fe036 TG |
486 | return 0; |
487 | } | |
488 | ||
11bf3cce LP |
489 | static int address_acquire(Link *link, Address *original, Address **ret) { |
490 | union in_addr_union in_addr = {}; | |
491 | struct in_addr broadcast = {}; | |
8e766630 | 492 | _cleanup_(address_freep) Address *na = NULL; |
11bf3cce LP |
493 | int r; |
494 | ||
495 | assert(link); | |
496 | assert(original); | |
497 | assert(ret); | |
498 | ||
499 | /* Something useful was configured? just use it */ | |
af93291c | 500 | if (in_addr_is_null(original->family, &original->in_addr) <= 0) |
11bf3cce LP |
501 | return 0; |
502 | ||
503 | /* The address is configured to be 0.0.0.0 or [::] by the user? | |
504 | * Then let's acquire something more useful from the pool. */ | |
505 | r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr); | |
6a7a4e4d LP |
506 | if (r < 0) |
507 | return log_link_error_errno(link, r, "Failed to acquire address from pool: %m"); | |
11bf3cce | 508 | if (r == 0) { |
79008bdd | 509 | log_link_error(link, "Couldn't find free address for interface, all taken."); |
11bf3cce LP |
510 | return -EBUSY; |
511 | } | |
512 | ||
513 | if (original->family == AF_INET) { | |
d076c6f9 | 514 | /* Pick first address in range for ourselves ... */ |
11bf3cce LP |
515 | in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1); |
516 | ||
517 | /* .. and use last as broadcast address */ | |
e87e2b78 SS |
518 | if (original->prefixlen > 30) |
519 | broadcast.s_addr = 0; | |
520 | else | |
521 | broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen); | |
11bf3cce LP |
522 | } else if (original->family == AF_INET6) |
523 | in_addr.in6.s6_addr[15] |= 1; | |
524 | ||
f0213e37 | 525 | r = address_new(&na); |
11bf3cce LP |
526 | if (r < 0) |
527 | return r; | |
528 | ||
529 | na->family = original->family; | |
530 | na->prefixlen = original->prefixlen; | |
531 | na->scope = original->scope; | |
532 | na->cinfo = original->cinfo; | |
533 | ||
534 | if (original->label) { | |
535 | na->label = strdup(original->label); | |
0099bc15 | 536 | if (!na->label) |
11bf3cce | 537 | return -ENOMEM; |
11bf3cce LP |
538 | } |
539 | ||
540 | na->broadcast = broadcast; | |
541 | na->in_addr = in_addr; | |
542 | ||
543 | LIST_PREPEND(addresses, link->pool_addresses, na); | |
544 | ||
1cc6c93a | 545 | *ret = TAKE_PTR(na); |
0099bc15 | 546 | |
11bf3cce LP |
547 | return 0; |
548 | } | |
549 | ||
1b566071 LP |
550 | int address_configure( |
551 | Address *address, | |
552 | Link *link, | |
302a796f | 553 | link_netlink_message_handler_t callback, |
1b566071 LP |
554 | bool update) { |
555 | ||
4afd3348 | 556 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
f579559b TG |
557 | int r; |
558 | ||
c166a070 | 559 | assert(address); |
4c701096 | 560 | assert(IN_SET(address->family, AF_INET, AF_INET6)); |
c166a070 TG |
561 | assert(link); |
562 | assert(link->ifindex > 0); | |
f882c247 | 563 | assert(link->manager); |
c166a070 | 564 | assert(link->manager->rtnl); |
bd1175bc | 565 | assert(callback); |
f882c247 | 566 | |
1b566071 LP |
567 | /* If this is a new address, then refuse adding more than the limit */ |
568 | if (address_get(link, address->family, &address->in_addr, address->prefixlen, NULL) <= 0 && | |
569 | set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX) | |
570 | return -E2BIG; | |
571 | ||
11bf3cce LP |
572 | r = address_acquire(link, address, &address); |
573 | if (r < 0) | |
574 | return r; | |
575 | ||
66669078 TG |
576 | if (update) |
577 | r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req, | |
578 | link->ifindex, address->family); | |
579 | else | |
580 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR, | |
581 | link->ifindex, address->family); | |
eb56eb9b MS |
582 | if (r < 0) |
583 | return log_error_errno(r, "Could not allocate RTM_NEWADDR message: %m"); | |
f579559b | 584 | |
5a723174 | 585 | r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen); |
eb56eb9b MS |
586 | if (r < 0) |
587 | return log_error_errno(r, "Could not set prefixlen: %m"); | |
5a723174 | 588 | |
851c9f82 PF |
589 | address->flags |= IFA_F_PERMANENT; |
590 | ||
e63be084 SS |
591 | if (address->home_address) |
592 | address->flags |= IFA_F_HOMEADDRESS; | |
593 | ||
594 | if (address->duplicate_address_detection) | |
595 | address->flags |= IFA_F_NODAD; | |
596 | ||
597 | if (address->manage_temporary_address) | |
598 | address->flags |= IFA_F_MANAGETEMPADDR; | |
599 | ||
600 | if (address->prefix_route) | |
601 | address->flags |= IFA_F_NOPREFIXROUTE; | |
602 | ||
603 | if (address->autojoin) | |
604 | address->flags |= IFA_F_MCAUTOJOIN; | |
605 | ||
851c9f82 | 606 | r = sd_rtnl_message_addr_set_flags(req, (address->flags & 0xff)); |
eb56eb9b MS |
607 | if (r < 0) |
608 | return log_error_errno(r, "Could not set flags: %m"); | |
5a723174 | 609 | |
851c9f82 | 610 | if (address->flags & ~0xff) { |
1c4baffc | 611 | r = sd_netlink_message_append_u32(req, IFA_FLAGS, address->flags); |
851c9f82 PF |
612 | if (r < 0) |
613 | return log_error_errno(r, "Could not set extended flags: %m"); | |
614 | } | |
615 | ||
5c1d3fc9 | 616 | r = sd_rtnl_message_addr_set_scope(req, address->scope); |
eb56eb9b MS |
617 | if (r < 0) |
618 | return log_error_errno(r, "Could not set scope: %m"); | |
5a723174 | 619 | |
0a0dc69b | 620 | if (address->family == AF_INET) |
1c4baffc | 621 | r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in); |
0a0dc69b | 622 | else if (address->family == AF_INET6) |
1c4baffc | 623 | r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6); |
eb56eb9b MS |
624 | if (r < 0) |
625 | return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m"); | |
f579559b | 626 | |
af93291c | 627 | if (!in_addr_is_null(address->family, &address->in_addr_peer)) { |
c081882f | 628 | if (address->family == AF_INET) |
1c4baffc | 629 | r = sd_netlink_message_append_in_addr(req, IFA_ADDRESS, &address->in_addr_peer.in); |
c081882f | 630 | else if (address->family == AF_INET6) |
1c4baffc | 631 | r = sd_netlink_message_append_in6_addr(req, IFA_ADDRESS, &address->in_addr_peer.in6); |
eb56eb9b MS |
632 | if (r < 0) |
633 | return log_error_errno(r, "Could not append IFA_ADDRESS attribute: %m"); | |
66e0bb33 YW |
634 | } else if (address->family == AF_INET && address->prefixlen <= 30) { |
635 | r = sd_netlink_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast); | |
636 | if (r < 0) | |
637 | return log_error_errno(r, "Could not append IFA_BROADCAST attribute: %m"); | |
f579559b TG |
638 | } |
639 | ||
640 | if (address->label) { | |
1c4baffc | 641 | r = sd_netlink_message_append_string(req, IFA_LABEL, address->label); |
eb56eb9b MS |
642 | if (r < 0) |
643 | return log_error_errno(r, "Could not append IFA_LABEL attribute: %m"); | |
f579559b TG |
644 | } |
645 | ||
66e0bb33 | 646 | r = sd_netlink_message_append_cache_info(req, IFA_CACHEINFO, &address->cinfo); |
eb56eb9b MS |
647 | if (r < 0) |
648 | return log_error_errno(r, "Could not append IFA_CACHEINFO attribute: %m"); | |
68ceb9df | 649 | |
fcf50cff | 650 | r = address_establish(address, link); |
eb56eb9b | 651 | if (r < 0) |
fcf50cff TG |
652 | return r; |
653 | ||
dfef713f | 654 | r = netlink_call_async(link->manager->rtnl, NULL, req, callback, link_netlink_destroy_callback, link); |
fcf50cff TG |
655 | if (r < 0) { |
656 | address_release(address); | |
eb56eb9b | 657 | return log_error_errno(r, "Could not send rtnetlink message: %m"); |
fcf50cff | 658 | } |
f579559b | 659 | |
563c69c6 TG |
660 | link_ref(link); |
661 | ||
dfef713f SS |
662 | if (address->family == AF_INET6 && !in_addr_is_null(address->family, &address->in_addr_peer)) |
663 | r = address_add(link, address->family, &address->in_addr_peer, address->prefixlen, NULL); | |
664 | else | |
665 | r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL); | |
adda1ed9 TG |
666 | if (r < 0) { |
667 | address_release(address); | |
668 | return log_error_errno(r, "Could not add address: %m"); | |
669 | } | |
670 | ||
f579559b TG |
671 | return 0; |
672 | } | |
673 | ||
44e7b949 LP |
674 | int config_parse_broadcast( |
675 | const char *unit, | |
eb0ea358 TG |
676 | const char *filename, |
677 | unsigned line, | |
678 | const char *section, | |
679 | unsigned section_line, | |
680 | const char *lvalue, | |
681 | int ltype, | |
682 | const char *rvalue, | |
683 | void *data, | |
684 | void *userdata) { | |
44e7b949 | 685 | |
eb0ea358 | 686 | Network *network = userdata; |
8e766630 | 687 | _cleanup_(address_freep) Address *n = NULL; |
eb0ea358 TG |
688 | int r; |
689 | ||
690 | assert(filename); | |
691 | assert(section); | |
692 | assert(lvalue); | |
693 | assert(rvalue); | |
694 | assert(data); | |
695 | ||
f4859fc7 | 696 | r = address_new_static(network, filename, section_line, &n); |
eb0ea358 TG |
697 | if (r < 0) |
698 | return r; | |
699 | ||
482e2ac1 | 700 | if (n->family == AF_INET6) { |
12ca818f | 701 | log_syntax(unit, LOG_ERR, filename, line, 0, "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue); |
482e2ac1 TG |
702 | return 0; |
703 | } | |
704 | ||
44e7b949 | 705 | r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast); |
eb0ea358 | 706 | if (r < 0) { |
12ca818f | 707 | log_syntax(unit, LOG_ERR, filename, line, r, "Broadcast is invalid, ignoring assignment: %s", rvalue); |
eb0ea358 TG |
708 | return 0; |
709 | } | |
710 | ||
44e7b949 | 711 | n->family = AF_INET; |
eb0ea358 TG |
712 | n = NULL; |
713 | ||
714 | return 0; | |
715 | } | |
716 | ||
f579559b TG |
717 | int config_parse_address(const char *unit, |
718 | const char *filename, | |
719 | unsigned line, | |
720 | const char *section, | |
71a61510 | 721 | unsigned section_line, |
f579559b TG |
722 | const char *lvalue, |
723 | int ltype, | |
724 | const char *rvalue, | |
725 | void *data, | |
726 | void *userdata) { | |
44e7b949 | 727 | |
6ae115c1 | 728 | Network *network = userdata; |
8e766630 | 729 | _cleanup_(address_freep) Address *n = NULL; |
44e7b949 | 730 | union in_addr_union buffer; |
b7cb4452 | 731 | unsigned char prefixlen; |
44e7b949 | 732 | int r, f; |
f579559b TG |
733 | |
734 | assert(filename); | |
6ae115c1 | 735 | assert(section); |
f579559b TG |
736 | assert(lvalue); |
737 | assert(rvalue); | |
738 | assert(data); | |
739 | ||
92fe133a TG |
740 | if (streq(section, "Network")) { |
741 | /* we are not in an Address section, so treat | |
742 | * this as the special '0' section */ | |
f4859fc7 SS |
743 | r = address_new_static(network, NULL, 0, &n); |
744 | } else | |
745 | r = address_new_static(network, filename, section_line, &n); | |
92fe133a | 746 | |
f579559b TG |
747 | if (r < 0) |
748 | return r; | |
749 | ||
750 | /* Address=address/prefixlen */ | |
9e0fdc21 | 751 | r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_LEGACY, &f, &buffer, &prefixlen); |
f579559b | 752 | if (r < 0) { |
b7cb4452 | 753 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid address '%s', ignoring assignment: %m", rvalue); |
f579559b TG |
754 | return 0; |
755 | } | |
756 | ||
44e7b949 | 757 | if (n->family != AF_UNSPEC && f != n->family) { |
b7cb4452 | 758 | log_syntax(unit, LOG_ERR, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue); |
44e7b949 LP |
759 | return 0; |
760 | } | |
761 | ||
762 | n->family = f; | |
b7cb4452 | 763 | n->prefixlen = prefixlen; |
44e7b949 LP |
764 | |
765 | if (streq(lvalue, "Address")) | |
766 | n->in_addr = buffer; | |
767 | else | |
768 | n->in_addr_peer = buffer; | |
769 | ||
770 | if (n->family == AF_INET && n->broadcast.s_addr == 0) | |
771 | n->broadcast.s_addr = n->in_addr.in.s_addr | htonl(0xfffffffflu >> n->prefixlen); | |
eb0ea358 | 772 | |
f579559b TG |
773 | n = NULL; |
774 | ||
775 | return 0; | |
776 | } | |
6ae115c1 | 777 | |
d31645ad LP |
778 | int config_parse_label( |
779 | const char *unit, | |
6ae115c1 TG |
780 | const char *filename, |
781 | unsigned line, | |
782 | const char *section, | |
783 | unsigned section_line, | |
784 | const char *lvalue, | |
785 | int ltype, | |
786 | const char *rvalue, | |
787 | void *data, | |
788 | void *userdata) { | |
d31645ad | 789 | |
8e766630 | 790 | _cleanup_(address_freep) Address *n = NULL; |
d31645ad | 791 | Network *network = userdata; |
6ae115c1 TG |
792 | int r; |
793 | ||
794 | assert(filename); | |
795 | assert(section); | |
796 | assert(lvalue); | |
797 | assert(rvalue); | |
798 | assert(data); | |
799 | ||
f4859fc7 | 800 | r = address_new_static(network, filename, section_line, &n); |
6ae115c1 TG |
801 | if (r < 0) |
802 | return r; | |
803 | ||
a87d19fe SS |
804 | if (!address_label_valid(rvalue)) { |
805 | log_syntax(unit, LOG_ERR, filename, line, 0, "Interface label is too long or invalid, ignoring assignment: %s", rvalue); | |
6ae115c1 TG |
806 | return 0; |
807 | } | |
808 | ||
d31645ad LP |
809 | r = free_and_strdup(&n->label, rvalue); |
810 | if (r < 0) | |
811 | return log_oom(); | |
6ae115c1 TG |
812 | |
813 | n = NULL; | |
814 | ||
815 | return 0; | |
816 | } | |
ce6c77eb | 817 | |
b5834a0b SS |
818 | int config_parse_lifetime(const char *unit, |
819 | const char *filename, | |
820 | unsigned line, | |
821 | const char *section, | |
822 | unsigned section_line, | |
823 | const char *lvalue, | |
824 | int ltype, | |
825 | const char *rvalue, | |
826 | void *data, | |
827 | void *userdata) { | |
828 | Network *network = userdata; | |
8e766630 | 829 | _cleanup_(address_freep) Address *n = NULL; |
b5834a0b SS |
830 | unsigned k; |
831 | int r; | |
832 | ||
833 | assert(filename); | |
834 | assert(section); | |
835 | assert(lvalue); | |
836 | assert(rvalue); | |
837 | assert(data); | |
838 | ||
f4859fc7 | 839 | r = address_new_static(network, filename, section_line, &n); |
b5834a0b SS |
840 | if (r < 0) |
841 | return r; | |
842 | ||
843 | if (STR_IN_SET(rvalue, "forever", "infinity")) { | |
844 | n->cinfo.ifa_prefered = CACHE_INFO_INFINITY_LIFE_TIME; | |
845 | n = NULL; | |
846 | ||
847 | return 0; | |
848 | } | |
849 | ||
850 | r = safe_atou(rvalue, &k); | |
851 | if (r < 0) { | |
852 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse PreferredLifetime, ignoring: %s", rvalue); | |
853 | return 0; | |
854 | } | |
855 | ||
856 | if (k != 0) | |
857 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid PreferredLifetime value, ignoring: %d", k); | |
858 | else { | |
859 | n->cinfo.ifa_prefered = k; | |
860 | n = NULL; | |
861 | } | |
862 | ||
863 | return 0; | |
864 | } | |
865 | ||
e63be084 SS |
866 | int config_parse_address_flags(const char *unit, |
867 | const char *filename, | |
868 | unsigned line, | |
869 | const char *section, | |
870 | unsigned section_line, | |
871 | const char *lvalue, | |
872 | int ltype, | |
873 | const char *rvalue, | |
874 | void *data, | |
875 | void *userdata) { | |
876 | Network *network = userdata; | |
8e766630 | 877 | _cleanup_(address_freep) Address *n = NULL; |
e63be084 SS |
878 | int r; |
879 | ||
880 | assert(filename); | |
881 | assert(section); | |
882 | assert(lvalue); | |
883 | assert(rvalue); | |
884 | assert(data); | |
885 | ||
f4859fc7 | 886 | r = address_new_static(network, filename, section_line, &n); |
e63be084 SS |
887 | if (r < 0) |
888 | return r; | |
889 | ||
890 | r = parse_boolean(rvalue); | |
891 | if (r < 0) { | |
892 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse address flag, ignoring: %s", rvalue); | |
893 | return 0; | |
894 | } | |
895 | ||
896 | if (streq(lvalue, "HomeAddress")) | |
897 | n->home_address = r; | |
898 | else if (streq(lvalue, "DuplicateAddressDetection")) | |
899 | n->duplicate_address_detection = r; | |
900 | else if (streq(lvalue, "ManageTemporaryAddress")) | |
901 | n->manage_temporary_address = r; | |
902 | else if (streq(lvalue, "PrefixRoute")) | |
903 | n->prefix_route = r; | |
904 | else if (streq(lvalue, "AutoJoin")) | |
905 | n->autojoin = r; | |
906 | ||
907 | return 0; | |
908 | } | |
909 | ||
2959fb07 SS |
910 | int config_parse_address_scope(const char *unit, |
911 | const char *filename, | |
912 | unsigned line, | |
913 | const char *section, | |
914 | unsigned section_line, | |
915 | const char *lvalue, | |
916 | int ltype, | |
917 | const char *rvalue, | |
918 | void *data, | |
919 | void *userdata) { | |
920 | Network *network = userdata; | |
8e766630 | 921 | _cleanup_(address_freep) Address *n = NULL; |
2959fb07 SS |
922 | int r; |
923 | ||
924 | assert(filename); | |
925 | assert(section); | |
926 | assert(lvalue); | |
927 | assert(rvalue); | |
928 | assert(data); | |
929 | ||
930 | r = address_new_static(network, filename, section_line, &n); | |
931 | if (r < 0) | |
932 | return r; | |
933 | ||
934 | if (streq(rvalue, "host")) | |
935 | n->scope = RT_SCOPE_HOST; | |
936 | else if (streq(rvalue, "link")) | |
937 | n->scope = RT_SCOPE_LINK; | |
938 | else if (streq(rvalue, "global")) | |
939 | n->scope = RT_SCOPE_UNIVERSE; | |
940 | else { | |
941 | r = safe_atou8(rvalue , &n->scope); | |
942 | if (r < 0) { | |
943 | log_syntax(unit, LOG_ERR, filename, line, r, "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue); | |
944 | return 0; | |
945 | } | |
946 | } | |
947 | ||
948 | n = NULL; | |
949 | ||
950 | return 0; | |
951 | } | |
952 | ||
ce6c77eb TG |
953 | bool address_is_ready(const Address *a) { |
954 | assert(a); | |
955 | ||
b7ed5384 SS |
956 | if (a->family == AF_INET6) |
957 | return !(a->flags & IFA_F_TENTATIVE); | |
958 | else | |
959 | return !(a->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED)); | |
ce6c77eb | 960 | } |