]>
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 | |
43409486 | 472 | r = netlink_message_append_in_addr_union(req, IFA_LOCAL, address->family, &address->in_addr); |
eb56eb9b MS |
473 | if (r < 0) |
474 | return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m"); | |
407fe036 | 475 | |
302a796f YW |
476 | r = netlink_call_async(link->manager->rtnl, NULL, req, |
477 | callback ?: address_remove_handler, | |
478 | link_netlink_destroy_callback, link); | |
eb56eb9b MS |
479 | if (r < 0) |
480 | return log_error_errno(r, "Could not send rtnetlink message: %m"); | |
407fe036 | 481 | |
563c69c6 TG |
482 | link_ref(link); |
483 | ||
407fe036 TG |
484 | return 0; |
485 | } | |
486 | ||
11bf3cce LP |
487 | static int address_acquire(Link *link, Address *original, Address **ret) { |
488 | union in_addr_union in_addr = {}; | |
489 | struct in_addr broadcast = {}; | |
8e766630 | 490 | _cleanup_(address_freep) Address *na = NULL; |
11bf3cce LP |
491 | int r; |
492 | ||
493 | assert(link); | |
494 | assert(original); | |
495 | assert(ret); | |
496 | ||
497 | /* Something useful was configured? just use it */ | |
af93291c | 498 | if (in_addr_is_null(original->family, &original->in_addr) <= 0) |
11bf3cce LP |
499 | return 0; |
500 | ||
501 | /* The address is configured to be 0.0.0.0 or [::] by the user? | |
502 | * Then let's acquire something more useful from the pool. */ | |
503 | r = manager_address_pool_acquire(link->manager, original->family, original->prefixlen, &in_addr); | |
6a7a4e4d LP |
504 | if (r < 0) |
505 | return log_link_error_errno(link, r, "Failed to acquire address from pool: %m"); | |
11bf3cce | 506 | if (r == 0) { |
79008bdd | 507 | log_link_error(link, "Couldn't find free address for interface, all taken."); |
11bf3cce LP |
508 | return -EBUSY; |
509 | } | |
510 | ||
511 | if (original->family == AF_INET) { | |
d076c6f9 | 512 | /* Pick first address in range for ourselves ... */ |
11bf3cce LP |
513 | in_addr.in.s_addr = in_addr.in.s_addr | htobe32(1); |
514 | ||
515 | /* .. and use last as broadcast address */ | |
e87e2b78 SS |
516 | if (original->prefixlen > 30) |
517 | broadcast.s_addr = 0; | |
518 | else | |
519 | broadcast.s_addr = in_addr.in.s_addr | htobe32(0xFFFFFFFFUL >> original->prefixlen); | |
11bf3cce LP |
520 | } else if (original->family == AF_INET6) |
521 | in_addr.in6.s6_addr[15] |= 1; | |
522 | ||
f0213e37 | 523 | r = address_new(&na); |
11bf3cce LP |
524 | if (r < 0) |
525 | return r; | |
526 | ||
527 | na->family = original->family; | |
528 | na->prefixlen = original->prefixlen; | |
529 | na->scope = original->scope; | |
530 | na->cinfo = original->cinfo; | |
531 | ||
532 | if (original->label) { | |
533 | na->label = strdup(original->label); | |
0099bc15 | 534 | if (!na->label) |
11bf3cce | 535 | return -ENOMEM; |
11bf3cce LP |
536 | } |
537 | ||
538 | na->broadcast = broadcast; | |
539 | na->in_addr = in_addr; | |
540 | ||
541 | LIST_PREPEND(addresses, link->pool_addresses, na); | |
542 | ||
1cc6c93a | 543 | *ret = TAKE_PTR(na); |
0099bc15 | 544 | |
11bf3cce LP |
545 | return 0; |
546 | } | |
547 | ||
1b566071 LP |
548 | int address_configure( |
549 | Address *address, | |
550 | Link *link, | |
302a796f | 551 | link_netlink_message_handler_t callback, |
1b566071 LP |
552 | bool update) { |
553 | ||
4afd3348 | 554 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
f579559b TG |
555 | int r; |
556 | ||
c166a070 | 557 | assert(address); |
4c701096 | 558 | assert(IN_SET(address->family, AF_INET, AF_INET6)); |
c166a070 TG |
559 | assert(link); |
560 | assert(link->ifindex > 0); | |
f882c247 | 561 | assert(link->manager); |
c166a070 | 562 | assert(link->manager->rtnl); |
bd1175bc | 563 | assert(callback); |
f882c247 | 564 | |
1b566071 LP |
565 | /* If this is a new address, then refuse adding more than the limit */ |
566 | if (address_get(link, address->family, &address->in_addr, address->prefixlen, NULL) <= 0 && | |
567 | set_size(link->addresses) >= ADDRESSES_PER_LINK_MAX) | |
568 | return -E2BIG; | |
569 | ||
11bf3cce LP |
570 | r = address_acquire(link, address, &address); |
571 | if (r < 0) | |
572 | return r; | |
573 | ||
66669078 TG |
574 | if (update) |
575 | r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req, | |
576 | link->ifindex, address->family); | |
577 | else | |
578 | r = sd_rtnl_message_new_addr(link->manager->rtnl, &req, RTM_NEWADDR, | |
579 | link->ifindex, address->family); | |
eb56eb9b MS |
580 | if (r < 0) |
581 | return log_error_errno(r, "Could not allocate RTM_NEWADDR message: %m"); | |
f579559b | 582 | |
5a723174 | 583 | r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen); |
eb56eb9b MS |
584 | if (r < 0) |
585 | return log_error_errno(r, "Could not set prefixlen: %m"); | |
5a723174 | 586 | |
851c9f82 PF |
587 | address->flags |= IFA_F_PERMANENT; |
588 | ||
e63be084 SS |
589 | if (address->home_address) |
590 | address->flags |= IFA_F_HOMEADDRESS; | |
591 | ||
592 | if (address->duplicate_address_detection) | |
593 | address->flags |= IFA_F_NODAD; | |
594 | ||
595 | if (address->manage_temporary_address) | |
596 | address->flags |= IFA_F_MANAGETEMPADDR; | |
597 | ||
598 | if (address->prefix_route) | |
599 | address->flags |= IFA_F_NOPREFIXROUTE; | |
600 | ||
601 | if (address->autojoin) | |
602 | address->flags |= IFA_F_MCAUTOJOIN; | |
603 | ||
851c9f82 | 604 | r = sd_rtnl_message_addr_set_flags(req, (address->flags & 0xff)); |
eb56eb9b MS |
605 | if (r < 0) |
606 | return log_error_errno(r, "Could not set flags: %m"); | |
5a723174 | 607 | |
851c9f82 | 608 | if (address->flags & ~0xff) { |
1c4baffc | 609 | r = sd_netlink_message_append_u32(req, IFA_FLAGS, address->flags); |
851c9f82 PF |
610 | if (r < 0) |
611 | return log_error_errno(r, "Could not set extended flags: %m"); | |
612 | } | |
613 | ||
5c1d3fc9 | 614 | r = sd_rtnl_message_addr_set_scope(req, address->scope); |
eb56eb9b MS |
615 | if (r < 0) |
616 | return log_error_errno(r, "Could not set scope: %m"); | |
5a723174 | 617 | |
43409486 | 618 | r = netlink_message_append_in_addr_union(req, IFA_LOCAL, address->family, &address->in_addr); |
eb56eb9b MS |
619 | if (r < 0) |
620 | return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m"); | |
f579559b | 621 | |
d40b01e4 | 622 | if (in_addr_is_null(address->family, &address->in_addr_peer) == 0) { |
43409486 | 623 | r = netlink_message_append_in_addr_union(req, IFA_ADDRESS, address->family, &address->in_addr_peer); |
eb56eb9b MS |
624 | if (r < 0) |
625 | return log_error_errno(r, "Could not append IFA_ADDRESS attribute: %m"); | |
66e0bb33 YW |
626 | } else if (address->family == AF_INET && address->prefixlen <= 30) { |
627 | r = sd_netlink_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast); | |
628 | if (r < 0) | |
629 | return log_error_errno(r, "Could not append IFA_BROADCAST attribute: %m"); | |
f579559b TG |
630 | } |
631 | ||
632 | if (address->label) { | |
1c4baffc | 633 | r = sd_netlink_message_append_string(req, IFA_LABEL, address->label); |
eb56eb9b MS |
634 | if (r < 0) |
635 | return log_error_errno(r, "Could not append IFA_LABEL attribute: %m"); | |
f579559b TG |
636 | } |
637 | ||
66e0bb33 | 638 | r = sd_netlink_message_append_cache_info(req, IFA_CACHEINFO, &address->cinfo); |
eb56eb9b MS |
639 | if (r < 0) |
640 | return log_error_errno(r, "Could not append IFA_CACHEINFO attribute: %m"); | |
68ceb9df | 641 | |
fcf50cff | 642 | r = address_establish(address, link); |
eb56eb9b | 643 | if (r < 0) |
fcf50cff TG |
644 | return r; |
645 | ||
dfef713f | 646 | r = netlink_call_async(link->manager->rtnl, NULL, req, callback, link_netlink_destroy_callback, link); |
fcf50cff TG |
647 | if (r < 0) { |
648 | address_release(address); | |
eb56eb9b | 649 | return log_error_errno(r, "Could not send rtnetlink message: %m"); |
fcf50cff | 650 | } |
f579559b | 651 | |
563c69c6 TG |
652 | link_ref(link); |
653 | ||
dfef713f SS |
654 | if (address->family == AF_INET6 && !in_addr_is_null(address->family, &address->in_addr_peer)) |
655 | r = address_add(link, address->family, &address->in_addr_peer, address->prefixlen, NULL); | |
656 | else | |
657 | r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL); | |
adda1ed9 TG |
658 | if (r < 0) { |
659 | address_release(address); | |
660 | return log_error_errno(r, "Could not add address: %m"); | |
661 | } | |
662 | ||
f579559b TG |
663 | return 0; |
664 | } | |
665 | ||
44e7b949 LP |
666 | int config_parse_broadcast( |
667 | const char *unit, | |
eb0ea358 TG |
668 | const char *filename, |
669 | unsigned line, | |
670 | const char *section, | |
671 | unsigned section_line, | |
672 | const char *lvalue, | |
673 | int ltype, | |
674 | const char *rvalue, | |
675 | void *data, | |
676 | void *userdata) { | |
44e7b949 | 677 | |
eb0ea358 | 678 | Network *network = userdata; |
8e766630 | 679 | _cleanup_(address_freep) Address *n = NULL; |
eb0ea358 TG |
680 | int r; |
681 | ||
682 | assert(filename); | |
683 | assert(section); | |
684 | assert(lvalue); | |
685 | assert(rvalue); | |
686 | assert(data); | |
687 | ||
f4859fc7 | 688 | r = address_new_static(network, filename, section_line, &n); |
eb0ea358 TG |
689 | if (r < 0) |
690 | return r; | |
691 | ||
482e2ac1 | 692 | if (n->family == AF_INET6) { |
12ca818f | 693 | log_syntax(unit, LOG_ERR, filename, line, 0, "Broadcast is not valid for IPv6 addresses, ignoring assignment: %s", rvalue); |
482e2ac1 TG |
694 | return 0; |
695 | } | |
696 | ||
44e7b949 | 697 | r = in_addr_from_string(AF_INET, rvalue, (union in_addr_union*) &n->broadcast); |
eb0ea358 | 698 | if (r < 0) { |
12ca818f | 699 | log_syntax(unit, LOG_ERR, filename, line, r, "Broadcast is invalid, ignoring assignment: %s", rvalue); |
eb0ea358 TG |
700 | return 0; |
701 | } | |
702 | ||
44e7b949 | 703 | n->family = AF_INET; |
eb0ea358 TG |
704 | n = NULL; |
705 | ||
706 | return 0; | |
707 | } | |
708 | ||
f579559b TG |
709 | int config_parse_address(const char *unit, |
710 | const char *filename, | |
711 | unsigned line, | |
712 | const char *section, | |
71a61510 | 713 | unsigned section_line, |
f579559b TG |
714 | const char *lvalue, |
715 | int ltype, | |
716 | const char *rvalue, | |
717 | void *data, | |
718 | void *userdata) { | |
44e7b949 | 719 | |
6ae115c1 | 720 | Network *network = userdata; |
8e766630 | 721 | _cleanup_(address_freep) Address *n = NULL; |
44e7b949 | 722 | union in_addr_union buffer; |
b7cb4452 | 723 | unsigned char prefixlen; |
44e7b949 | 724 | int r, f; |
f579559b TG |
725 | |
726 | assert(filename); | |
6ae115c1 | 727 | assert(section); |
f579559b TG |
728 | assert(lvalue); |
729 | assert(rvalue); | |
730 | assert(data); | |
731 | ||
92fe133a TG |
732 | if (streq(section, "Network")) { |
733 | /* we are not in an Address section, so treat | |
734 | * this as the special '0' section */ | |
f4859fc7 SS |
735 | r = address_new_static(network, NULL, 0, &n); |
736 | } else | |
737 | r = address_new_static(network, filename, section_line, &n); | |
92fe133a | 738 | |
f579559b TG |
739 | if (r < 0) |
740 | return r; | |
741 | ||
742 | /* Address=address/prefixlen */ | |
0f707207 YW |
743 | r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_REFUSE, &f, &buffer, &prefixlen); |
744 | if (r == -ENOANO) { | |
745 | log_syntax(unit, LOG_ERR, filename, line, r, | |
746 | "An address '%s' is specified without prefix length. " | |
747 | "The behavior of parsing addresses without prefix length will be changed in the future release. " | |
748 | "Please specify prefix length explicitly.", rvalue); | |
749 | ||
750 | r = in_addr_prefix_from_string_auto_internal(rvalue, PREFIXLEN_LEGACY, &f, &buffer, &prefixlen); | |
751 | } | |
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 | } |