]>
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" |
0a970718 | 8 | #include "memory-util.h" |
081aea25 | 9 | #include "missing_network.h" |
fc2f9534 | 10 | #include "netlink-util.h" |
6bedfcbb | 11 | #include "networkd-address.h" |
23f53b99 | 12 | #include "networkd-manager.h" |
6bedfcbb | 13 | #include "parse-util.h" |
3ac8e543 | 14 | #include "set.h" |
d31645ad | 15 | #include "socket-util.h" |
07630cea | 16 | #include "string-util.h" |
51517f9e | 17 | #include "strv.h" |
3ac8e543 | 18 | #include "utf8.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 | ||
9560e5b3 | 42 | static 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) | |
7750b796 | 226 | return r; |
91b5f997 TG |
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) |
7750b796 | 324 | return r; |
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 | ||
959f65d3 | 354 | link_update_operstate(address->link, true); |
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; | |
7750b796 | 374 | int r; |
8012cd39 | 375 | |
5a8bcb67 | 376 | assert(address); |
91b5f997 | 377 | |
8012cd39 TG |
378 | ready = address_is_ready(address); |
379 | link = address->link; | |
380 | ||
7750b796 YW |
381 | r = address_release(address); |
382 | if (r < 0) | |
383 | log_link_warning_errno(link, r, "Failed to disable IP masquerading, ignoring: %m"); | |
384 | ||
91b5f997 TG |
385 | address_free(address); |
386 | ||
959f65d3 | 387 | link_update_operstate(link, true); |
84de38c5 | 388 | |
8012cd39 TG |
389 | if (link && !ready) |
390 | link_check_ready(link); | |
391 | ||
91b5f997 TG |
392 | return 0; |
393 | } | |
394 | ||
1b566071 LP |
395 | int address_get(Link *link, |
396 | int family, | |
397 | const union in_addr_union *in_addr, | |
398 | unsigned char prefixlen, | |
399 | Address **ret) { | |
400 | ||
401 | Address address, *existing; | |
91b5f997 | 402 | |
5a8bcb67 | 403 | assert(link); |
91b5f997 | 404 | assert(in_addr); |
5a8bcb67 | 405 | |
1b566071 LP |
406 | address = (Address) { |
407 | .family = family, | |
408 | .in_addr = *in_addr, | |
409 | .prefixlen = prefixlen, | |
410 | }; | |
5a8bcb67 | 411 | |
91b5f997 | 412 | existing = set_get(link->addresses, &address); |
cab974b0 | 413 | if (existing) { |
1b566071 LP |
414 | if (ret) |
415 | *ret = existing; | |
cab974b0 | 416 | return 1; |
adda1ed9 | 417 | } |
5a8bcb67 | 418 | |
1b566071 LP |
419 | existing = set_get(link->addresses_foreign, &address); |
420 | if (existing) { | |
421 | if (ret) | |
422 | *ret = existing; | |
423 | return 0; | |
424 | } | |
5a8bcb67 | 425 | |
1b566071 | 426 | return -ENOENT; |
5a8bcb67 LP |
427 | } |
428 | ||
302a796f | 429 | static int address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
63ae0569 YW |
430 | int r; |
431 | ||
432 | assert(m); | |
433 | assert(link); | |
434 | assert(link->ifname); | |
435 | ||
436 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
437 | return 1; | |
438 | ||
439 | r = sd_netlink_message_get_errno(m); | |
440 | if (r < 0 && r != -EADDRNOTAVAIL) | |
441 | log_link_warning_errno(link, r, "Could not drop address: %m"); | |
442 | ||
443 | return 1; | |
444 | } | |
445 | ||
483d099e ZJS |
446 | int address_remove( |
447 | Address *address, | |
448 | Link *link, | |
302a796f | 449 | link_netlink_message_handler_t callback) { |
483d099e | 450 | |
4afd3348 | 451 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
407fe036 TG |
452 | int r; |
453 | ||
454 | assert(address); | |
4c701096 | 455 | assert(IN_SET(address->family, AF_INET, AF_INET6)); |
407fe036 TG |
456 | assert(link); |
457 | assert(link->ifindex > 0); | |
458 | assert(link->manager); | |
459 | assert(link->manager->rtnl); | |
460 | ||
30226d27 | 461 | if (DEBUG_LOGGING) { |
7750b796 YW |
462 | _cleanup_free_ char *b = NULL; |
463 | ||
464 | (void) in_addr_to_string(address->family, &address->in_addr, &b); | |
465 | log_link_debug(link, "Removing address %s", strna(b)); | |
30226d27 TJ |
466 | } |
467 | ||
151b9b96 LP |
468 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_DELADDR, |
469 | link->ifindex, address->family); | |
eb56eb9b | 470 | if (r < 0) |
7750b796 | 471 | return log_link_error_errno(link, r, "Could not allocate RTM_DELADDR message: %m"); |
407fe036 | 472 | |
5a723174 | 473 | r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen); |
eb56eb9b | 474 | if (r < 0) |
7750b796 | 475 | return log_link_error_errno(link, r, "Could not set prefixlen: %m"); |
5a723174 | 476 | |
43409486 | 477 | r = netlink_message_append_in_addr_union(req, IFA_LOCAL, address->family, &address->in_addr); |
eb56eb9b | 478 | if (r < 0) |
7750b796 | 479 | return log_link_error_errno(link, r, "Could not append IFA_LOCAL attribute: %m"); |
407fe036 | 480 | |
302a796f YW |
481 | r = netlink_call_async(link->manager->rtnl, NULL, req, |
482 | callback ?: address_remove_handler, | |
483 | link_netlink_destroy_callback, link); | |
eb56eb9b | 484 | if (r < 0) |
7750b796 | 485 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); |
407fe036 | 486 | |
563c69c6 TG |
487 | link_ref(link); |
488 | ||
407fe036 TG |
489 | return 0; |
490 | } | |
491 | ||
11bf3cce | 492 | static int address_acquire(Link *link, Address *original, Address **ret) { |
67a46833 | 493 | union in_addr_union in_addr = IN_ADDR_NULL; |
11bf3cce | 494 | struct in_addr broadcast = {}; |
8e766630 | 495 | _cleanup_(address_freep) Address *na = NULL; |
11bf3cce LP |
496 | int r; |
497 | ||
498 | assert(link); | |
499 | assert(original); | |
500 | assert(ret); | |
501 | ||
502 | /* Something useful was configured? just use it */ | |
7e43ebfb YW |
503 | r = in_addr_is_null(original->family, &original->in_addr); |
504 | if (r <= 0) | |
505 | return r; | |
11bf3cce LP |
506 | |
507 | /* The address is configured to be 0.0.0.0 or [::] by the user? | |
508 | * Then let's acquire something more useful from the pool. */ | |
509 | r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr); | |
6a7a4e4d | 510 | if (r < 0) |
7750b796 YW |
511 | return r; |
512 | if (r == 0) | |
11bf3cce | 513 | return -EBUSY; |
11bf3cce LP |
514 | |
515 | if (original->family == AF_INET) { | |
d076c6f9 | 516 | /* Pick first address in range for ourselves ... */ |
11bf3cce LP |
517 | in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1); |
518 | ||
519 | /* .. and use last as broadcast address */ | |
e87e2b78 SS |
520 | if (original->prefixlen > 30) |
521 | broadcast.s_addr = 0; | |
522 | else | |
523 | broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen); | |
11bf3cce LP |
524 | } else if (original->family == AF_INET6) |
525 | in_addr.in6.s6_addr[15] |= 1; | |
526 | ||
f0213e37 | 527 | r = address_new(&na); |
11bf3cce LP |
528 | if (r < 0) |
529 | return r; | |
530 | ||
531 | na->family = original->family; | |
532 | na->prefixlen = original->prefixlen; | |
533 | na->scope = original->scope; | |
534 | na->cinfo = original->cinfo; | |
535 | ||
536 | if (original->label) { | |
537 | na->label = strdup(original->label); | |
0099bc15 | 538 | if (!na->label) |
11bf3cce | 539 | return -ENOMEM; |
11bf3cce LP |
540 | } |
541 | ||
542 | na->broadcast = broadcast; | |
543 | na->in_addr = in_addr; | |
544 | ||
545 | LIST_PREPEND(addresses, link->pool_addresses, na); | |
546 | ||
1cc6c93a | 547 | *ret = TAKE_PTR(na); |
0099bc15 | 548 | |
11bf3cce LP |
549 | return 0; |
550 | } | |
551 | ||
1b566071 LP |
552 | int address_configure( |
553 | Address *address, | |
554 | Link *link, | |
302a796f | 555 | link_netlink_message_handler_t callback, |
1b566071 LP |
556 | bool update) { |
557 | ||
4afd3348 | 558 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
f579559b TG |
559 | int r; |
560 | ||
c166a070 | 561 | assert(address); |
4c701096 | 562 | assert(IN_SET(address->family, AF_INET, AF_INET6)); |
c166a070 TG |
563 | assert(link); |
564 | assert(link->ifindex > 0); | |
f882c247 | 565 | assert(link->manager); |
c166a070 | 566 | assert(link->manager->rtnl); |
bd1175bc | 567 | assert(callback); |
f882c247 | 568 | |
bafa9641 | 569 | if (address->family == AF_INET6 && link_sysctl_ipv6_enabled(link) == 0) { |
54a1a535 YW |
570 | log_link_warning(link, "An IPv6 address is requested, but IPv6 is disabled by sysctl, ignoring."); |
571 | return 0; | |
572 | } | |
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) | |
7750b796 YW |
577 | return log_link_error_errno(link, SYNTHETIC_ERRNO(E2BIG), |
578 | "Too many addresses are configured, refusing: %m"); | |
1b566071 | 579 | |
11bf3cce LP |
580 | r = address_acquire(link, address, &address); |
581 | if (r < 0) | |
7750b796 | 582 | return log_link_error_errno(link, r, "Failed to acquire an address from pool: %m"); |
11bf3cce | 583 | |
66669078 TG |
584 | if (update) |
585 | r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req, | |
586 | link->ifindex, address->family); | |
587 | else | |
588 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR, | |
589 | link->ifindex, address->family); | |
eb56eb9b | 590 | if (r < 0) |
7750b796 | 591 | return log_link_error_errno(link, r, "Could not allocate RTM_NEWADDR message: %m"); |
f579559b | 592 | |
5a723174 | 593 | r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen); |
eb56eb9b | 594 | if (r < 0) |
7750b796 | 595 | return log_link_error_errno(link, r, "Could not set prefixlen: %m"); |
5a723174 | 596 | |
851c9f82 PF |
597 | address->flags |= IFA_F_PERMANENT; |
598 | ||
e63be084 SS |
599 | if (address->home_address) |
600 | address->flags |= IFA_F_HOMEADDRESS; | |
601 | ||
602 | if (address->duplicate_address_detection) | |
603 | address->flags |= IFA_F_NODAD; | |
604 | ||
605 | if (address->manage_temporary_address) | |
606 | address->flags |= IFA_F_MANAGETEMPADDR; | |
607 | ||
608 | if (address->prefix_route) | |
609 | address->flags |= IFA_F_NOPREFIXROUTE; | |
610 | ||
611 | if (address->autojoin) | |
612 | address->flags |= IFA_F_MCAUTOJOIN; | |
613 | ||
851c9f82 | 614 | r = sd_rtnl_message_addr_set_flags(req, (address->flags & 0xff)); |
eb56eb9b | 615 | if (r < 0) |
7750b796 | 616 | return log_link_error_errno(link, r, "Could not set flags: %m"); |
5a723174 | 617 | |
851c9f82 | 618 | if (address->flags & ~0xff) { |
1c4baffc | 619 | r = sd_netlink_message_append_u32(req, IFA_FLAGS, address->flags); |
851c9f82 | 620 | if (r < 0) |
7750b796 | 621 | return log_link_error_errno(link, r, "Could not set extended flags: %m"); |
851c9f82 PF |
622 | } |
623 | ||
5c1d3fc9 | 624 | r = sd_rtnl_message_addr_set_scope(req, address->scope); |
eb56eb9b | 625 | if (r < 0) |
7750b796 | 626 | return log_link_error_errno(link, r, "Could not set scope: %m"); |
5a723174 | 627 | |
43409486 | 628 | r = netlink_message_append_in_addr_union(req, IFA_LOCAL, address->family, &address->in_addr); |
eb56eb9b | 629 | if (r < 0) |
7750b796 | 630 | return log_link_error_errno(link, r, "Could not append IFA_LOCAL attribute: %m"); |
f579559b | 631 | |
d40b01e4 | 632 | if (in_addr_is_null(address->family, &address->in_addr_peer) == 0) { |
43409486 | 633 | r = netlink_message_append_in_addr_union(req, IFA_ADDRESS, address->family, &address->in_addr_peer); |
eb56eb9b | 634 | if (r < 0) |
7750b796 | 635 | return log_link_error_errno(link, r, "Could not append IFA_ADDRESS attribute: %m"); |
66e0bb33 YW |
636 | } else if (address->family == AF_INET && address->prefixlen <= 30) { |
637 | r = sd_netlink_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast); | |
638 | if (r < 0) | |
7750b796 | 639 | return log_link_error_errno(link, r, "Could not append IFA_BROADCAST attribute: %m"); |
f579559b TG |
640 | } |
641 | ||
642 | if (address->label) { | |
1c4baffc | 643 | r = sd_netlink_message_append_string(req, IFA_LABEL, address->label); |
eb56eb9b | 644 | if (r < 0) |
7750b796 | 645 | return log_link_error_errno(link, r, "Could not append IFA_LABEL attribute: %m"); |
f579559b TG |
646 | } |
647 | ||
66e0bb33 | 648 | r = sd_netlink_message_append_cache_info(req, IFA_CACHEINFO, &address->cinfo); |
eb56eb9b | 649 | if (r < 0) |
7750b796 | 650 | return log_link_error_errno(link, r, "Could not append IFA_CACHEINFO attribute: %m"); |
68ceb9df | 651 | |
fcf50cff | 652 | r = address_establish(address, link); |
eb56eb9b | 653 | if (r < 0) |
7750b796 | 654 | log_link_warning_errno(link, r, "Could not enable IP masquerading, ignoring: %m"); |
fcf50cff | 655 | |
dfef713f | 656 | r = netlink_call_async(link->manager->rtnl, NULL, req, callback, link_netlink_destroy_callback, link); |
fcf50cff TG |
657 | if (r < 0) { |
658 | address_release(address); | |
7750b796 | 659 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); |
fcf50cff | 660 | } |
f579559b | 661 | |
563c69c6 TG |
662 | link_ref(link); |
663 | ||
dfef713f SS |
664 | if (address->family == AF_INET6 && !in_addr_is_null(address->family, &address->in_addr_peer)) |
665 | r = address_add(link, address->family, &address->in_addr_peer, address->prefixlen, NULL); | |
666 | else | |
667 | r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL); | |
adda1ed9 TG |
668 | if (r < 0) { |
669 | address_release(address); | |
7750b796 | 670 | return log_link_error_errno(link, r, "Could not add address: %m"); |
adda1ed9 TG |
671 | } |
672 | ||
54a1a535 | 673 | return 1; |
f579559b TG |
674 | } |
675 | ||
44e7b949 LP |
676 | int config_parse_broadcast( |
677 | const char *unit, | |
eb0ea358 TG |
678 | const char *filename, |
679 | unsigned line, | |
680 | const char *section, | |
681 | unsigned section_line, | |
682 | const char *lvalue, | |
683 | int ltype, | |
684 | const char *rvalue, | |
685 | void *data, | |
686 | void *userdata) { | |
44e7b949 | 687 | |
eb0ea358 | 688 | Network *network = userdata; |
fcbf4cb7 | 689 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; |
eb0ea358 TG |
690 | int r; |
691 | ||
692 | assert(filename); | |
693 | assert(section); | |
694 | assert(lvalue); | |
695 | assert(rvalue); | |
696 | assert(data); | |
697 | ||
f4859fc7 | 698 | r = address_new_static(network, filename, section_line, &n); |
eb0ea358 TG |
699 | if (r < 0) |
700 | return r; | |
701 | ||
482e2ac1 | 702 | if (n->family == AF_INET6) { |
2850cd40 YW |
703 | log_syntax(unit, LOG_ERR, filename, line, 0, |
704 | "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue); | |
482e2ac1 TG |
705 | return 0; |
706 | } | |
707 | ||
44e7b949 | 708 | r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast); |
eb0ea358 | 709 | if (r < 0) { |
2850cd40 YW |
710 | log_syntax(unit, LOG_ERR, filename, line, r, |
711 | "Broadcast is invalid, ignoring assignment: %s", rvalue); | |
eb0ea358 TG |
712 | return 0; |
713 | } | |
714 | ||
44e7b949 | 715 | n->family = AF_INET; |
eb0ea358 TG |
716 | n = NULL; |
717 | ||
718 | return 0; | |
719 | } | |
720 | ||
f579559b TG |
721 | int config_parse_address(const char *unit, |
722 | const char *filename, | |
723 | unsigned line, | |
724 | const char *section, | |
71a61510 | 725 | unsigned section_line, |
f579559b TG |
726 | const char *lvalue, |
727 | int ltype, | |
728 | const char *rvalue, | |
729 | void *data, | |
730 | void *userdata) { | |
44e7b949 | 731 | |
6ae115c1 | 732 | Network *network = userdata; |
fcbf4cb7 | 733 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; |
44e7b949 | 734 | union in_addr_union buffer; |
b7cb4452 | 735 | unsigned char prefixlen; |
44e7b949 | 736 | int r, f; |
f579559b TG |
737 | |
738 | assert(filename); | |
6ae115c1 | 739 | assert(section); |
f579559b TG |
740 | assert(lvalue); |
741 | assert(rvalue); | |
742 | assert(data); | |
743 | ||
92fe133a TG |
744 | if (streq(section, "Network")) { |
745 | /* we are not in an Address section, so treat | |
746 | * this as the special '0' section */ | |
f4859fc7 SS |
747 | r = address_new_static(network, NULL, 0, &n); |
748 | } else | |
749 | r = address_new_static(network, filename, section_line, &n); | |
92fe133a | 750 | |
f579559b TG |
751 | if (r < 0) |
752 | return r; | |
753 | ||
754 | /* Address=address/prefixlen */ | |
0f707207 YW |
755 | r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_REFUSE, &f, &buffer, &prefixlen); |
756 | if (r == -ENOANO) { | |
757 | log_syntax(unit, LOG_ERR, filename, line, r, | |
758 | "An address '%s' is specified without prefix length. " | |
759 | "The behavior of parsing addresses without prefix length will be changed in the future release. " | |
760 | "Please specify prefix length explicitly.", rvalue); | |
761 | ||
762 | r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_LEGACY, &f, &buffer, &prefixlen); | |
763 | } | |
f579559b | 764 | if (r < 0) { |
b7cb4452 | 765 | log_syntax(unit, LOG_ERR, filename, line, r, "Invalid address '%s', ignoring assignment: %m", rvalue); |
f579559b TG |
766 | return 0; |
767 | } | |
768 | ||
44e7b949 | 769 | if (n->family != AF_UNSPEC && f != n->family) { |
b7cb4452 | 770 | log_syntax(unit, LOG_ERR, filename, line, 0, "Address is incompatible, ignoring assignment: %s", rvalue); |
44e7b949 LP |
771 | return 0; |
772 | } | |
773 | ||
c9207ff3 YW |
774 | if (in_addr_is_null(f, &buffer)) { |
775 | /* Will use address from address pool. Note that for ipv6 case, prefix of the address | |
776 | * pool is 8, but 40 bit is used by the global ID and 16 bit by the subnet ID. So, | |
777 | * let's limit the prefix length to 64 or larger. See RFC4193. */ | |
778 | if ((f == AF_INET && prefixlen < 8) || | |
779 | (f == AF_INET6 && prefixlen < 64)) { | |
780 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
781 | "Null address with invalid prefixlen='%u', ignoring assignment: %s", | |
782 | prefixlen, rvalue); | |
783 | return 0; | |
784 | } | |
785 | } | |
786 | ||
44e7b949 | 787 | n->family = f; |
b7cb4452 | 788 | n->prefixlen = prefixlen; |
44e7b949 LP |
789 | |
790 | if (streq(lvalue, "Address")) | |
791 | n->in_addr = buffer; | |
792 | else | |
793 | n->in_addr_peer = buffer; | |
794 | ||
3681d639 | 795 | if (n->family == AF_INET && n->broadcast.s_addr == 0 && n->prefixlen <= 30) |
44e7b949 | 796 | n->broadcast.s_addr = n->in_addr.in.s_addr | htonl(0xfffffffflu >> n->prefixlen); |
eb0ea358 | 797 | |
f579559b TG |
798 | n = NULL; |
799 | ||
800 | return 0; | |
801 | } | |
6ae115c1 | 802 | |
d31645ad LP |
803 | int config_parse_label( |
804 | const char *unit, | |
6ae115c1 TG |
805 | const char *filename, |
806 | unsigned line, | |
807 | const char *section, | |
808 | unsigned section_line, | |
809 | const char *lvalue, | |
810 | int ltype, | |
811 | const char *rvalue, | |
812 | void *data, | |
813 | void *userdata) { | |
d31645ad | 814 | |
fcbf4cb7 | 815 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; |
d31645ad | 816 | Network *network = userdata; |
6ae115c1 TG |
817 | int r; |
818 | ||
819 | assert(filename); | |
820 | assert(section); | |
821 | assert(lvalue); | |
822 | assert(rvalue); | |
823 | assert(data); | |
824 | ||
f4859fc7 | 825 | r = address_new_static(network, filename, section_line, &n); |
6ae115c1 TG |
826 | if (r < 0) |
827 | return r; | |
828 | ||
a87d19fe | 829 | if (!address_label_valid(rvalue)) { |
2850cd40 YW |
830 | log_syntax(unit, LOG_ERR, filename, line, 0, |
831 | "Interface label is too long or invalid, ignoring assignment: %s", rvalue); | |
6ae115c1 TG |
832 | return 0; |
833 | } | |
834 | ||
d31645ad LP |
835 | r = free_and_strdup(&n->label, rvalue); |
836 | if (r < 0) | |
837 | return log_oom(); | |
6ae115c1 TG |
838 | |
839 | n = NULL; | |
6ae115c1 TG |
840 | return 0; |
841 | } | |
ce6c77eb | 842 | |
b5834a0b SS |
843 | int config_parse_lifetime(const char *unit, |
844 | const char *filename, | |
845 | unsigned line, | |
846 | const char *section, | |
847 | unsigned section_line, | |
848 | const char *lvalue, | |
849 | int ltype, | |
850 | const char *rvalue, | |
851 | void *data, | |
852 | void *userdata) { | |
853 | Network *network = userdata; | |
fcbf4cb7 | 854 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; |
b5834a0b SS |
855 | unsigned k; |
856 | int r; | |
857 | ||
858 | assert(filename); | |
859 | assert(section); | |
860 | assert(lvalue); | |
861 | assert(rvalue); | |
862 | assert(data); | |
863 | ||
f4859fc7 | 864 | r = address_new_static(network, filename, section_line, &n); |
b5834a0b SS |
865 | if (r < 0) |
866 | return r; | |
867 | ||
33680b0a YW |
868 | /* We accept only "forever", "infinity", or "0". */ |
869 | if (STR_IN_SET(rvalue, "forever", "infinity")) | |
870 | k = CACHE_INFO_INFINITY_LIFE_TIME; | |
871 | else if (streq(rvalue, "0")) | |
872 | k = 0; | |
873 | else { | |
874 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
875 | "Invalid PreferredLifetime= value, ignoring: %s", rvalue); | |
b5834a0b SS |
876 | return 0; |
877 | } | |
878 | ||
33680b0a YW |
879 | n->cinfo.ifa_prefered = k; |
880 | n = NULL; | |
b5834a0b SS |
881 | |
882 | return 0; | |
883 | } | |
884 | ||
e63be084 SS |
885 | int config_parse_address_flags(const char *unit, |
886 | const char *filename, | |
887 | unsigned line, | |
888 | const char *section, | |
889 | unsigned section_line, | |
890 | const char *lvalue, | |
891 | int ltype, | |
892 | const char *rvalue, | |
893 | void *data, | |
894 | void *userdata) { | |
895 | Network *network = userdata; | |
fcbf4cb7 | 896 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; |
e63be084 SS |
897 | int r; |
898 | ||
899 | assert(filename); | |
900 | assert(section); | |
901 | assert(lvalue); | |
902 | assert(rvalue); | |
903 | assert(data); | |
904 | ||
f4859fc7 | 905 | r = address_new_static(network, filename, section_line, &n); |
e63be084 SS |
906 | if (r < 0) |
907 | return r; | |
908 | ||
909 | r = parse_boolean(rvalue); | |
910 | if (r < 0) { | |
2850cd40 YW |
911 | log_syntax(unit, LOG_ERR, filename, line, r, |
912 | "Failed to parse address flag, ignoring: %s", rvalue); | |
e63be084 SS |
913 | return 0; |
914 | } | |
915 | ||
916 | if (streq(lvalue, "HomeAddress")) | |
917 | n->home_address = r; | |
918 | else if (streq(lvalue, "DuplicateAddressDetection")) | |
919 | n->duplicate_address_detection = r; | |
920 | else if (streq(lvalue, "ManageTemporaryAddress")) | |
921 | n->manage_temporary_address = r; | |
922 | else if (streq(lvalue, "PrefixRoute")) | |
923 | n->prefix_route = r; | |
924 | else if (streq(lvalue, "AutoJoin")) | |
925 | n->autojoin = r; | |
2850cd40 YW |
926 | else |
927 | assert_not_reached("Invalid address flag type."); | |
e63be084 | 928 | |
4aa4c4b0 | 929 | n = NULL; |
e63be084 SS |
930 | return 0; |
931 | } | |
932 | ||
2959fb07 SS |
933 | int config_parse_address_scope(const char *unit, |
934 | const char *filename, | |
935 | unsigned line, | |
936 | const char *section, | |
937 | unsigned section_line, | |
938 | const char *lvalue, | |
939 | int ltype, | |
940 | const char *rvalue, | |
941 | void *data, | |
942 | void *userdata) { | |
943 | Network *network = userdata; | |
fcbf4cb7 | 944 | _cleanup_(address_free_or_set_invalidp) Address *n = NULL; |
2959fb07 SS |
945 | int r; |
946 | ||
947 | assert(filename); | |
948 | assert(section); | |
949 | assert(lvalue); | |
950 | assert(rvalue); | |
951 | assert(data); | |
952 | ||
953 | r = address_new_static(network, filename, section_line, &n); | |
954 | if (r < 0) | |
955 | return r; | |
956 | ||
957 | if (streq(rvalue, "host")) | |
958 | n->scope = RT_SCOPE_HOST; | |
959 | else if (streq(rvalue, "link")) | |
960 | n->scope = RT_SCOPE_LINK; | |
961 | else if (streq(rvalue, "global")) | |
962 | n->scope = RT_SCOPE_UNIVERSE; | |
963 | else { | |
964 | r = safe_atou8(rvalue , &n->scope); | |
965 | if (r < 0) { | |
2850cd40 YW |
966 | log_syntax(unit, LOG_ERR, filename, line, r, |
967 | "Could not parse address scope \"%s\", ignoring assignment: %m", rvalue); | |
2959fb07 SS |
968 | return 0; |
969 | } | |
970 | } | |
971 | ||
972 | n = NULL; | |
2959fb07 SS |
973 | return 0; |
974 | } | |
975 | ||
ce6c77eb TG |
976 | bool address_is_ready(const Address *a) { |
977 | assert(a); | |
978 | ||
b7ed5384 SS |
979 | if (a->family == AF_INET6) |
980 | return !(a->flags & IFA_F_TENTATIVE); | |
981 | else | |
982 | return !(a->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED)); | |
ce6c77eb | 983 | } |
fcbf4cb7 YW |
984 | |
985 | int address_section_verify(Address *address) { | |
986 | if (section_is_invalid(address->section)) | |
987 | return -EINVAL; | |
988 | ||
989 | if (address->family == AF_UNSPEC) { | |
990 | assert(address->section); | |
991 | ||
992 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), | |
993 | "%s: Address section without Address= field configured. " | |
994 | "Ignoring [Address] section from line %u.", | |
995 | address->section->filename, address->section->line); | |
996 | } | |
997 | ||
998 | return 0; | |
999 | } |