]>
Commit | Line | Data |
---|---|---|
f579559b TG |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2013 Tom Gundersen <teg@jklm.no> | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include <netinet/ether.h> | |
21 | #include <linux/if.h> | |
4cc7a82c | 22 | #include <unistd.h> |
f579559b | 23 | |
b5efdb8a | 24 | #include "alloc-util.h" |
1346b1f0 | 25 | #include "bus-util.h" |
bd91b83e | 26 | #include "dhcp-lease-internal.h" |
3ffd4af2 | 27 | #include "fd-util.h" |
cf1d700d TG |
28 | #include "fileio.h" |
29 | #include "netlink-util.h" | |
c6f7c917 | 30 | #include "network-internal.h" |
634f0f98 | 31 | #include "networkd.h" |
8e1ad1ea | 32 | #include "networkd-lldp-tx.h" |
cf1d700d TG |
33 | #include "set.h" |
34 | #include "socket-util.h" | |
15a5e950 | 35 | #include "stdio-util.h" |
8b43440b | 36 | #include "string-table.h" |
cf1d700d TG |
37 | #include "udev-util.h" |
38 | #include "util.h" | |
39 | #include "virt.h" | |
fc2f9534 | 40 | |
b9d74c40 LP |
41 | static bool link_dhcp6_enabled(Link *link) { |
42 | assert(link); | |
43 | ||
fa709992 LP |
44 | if (!socket_ipv6_is_supported()) |
45 | return false; | |
46 | ||
78c958f8 TG |
47 | if (link->flags & IFF_LOOPBACK) |
48 | return false; | |
49 | ||
50 | if (!link->network) | |
51 | return false; | |
52 | ||
e0ee46f2 | 53 | return link->network->dhcp & ADDRESS_FAMILY_IPV6; |
78c958f8 TG |
54 | } |
55 | ||
b9d74c40 LP |
56 | static bool link_dhcp4_enabled(Link *link) { |
57 | assert(link); | |
58 | ||
78c958f8 TG |
59 | if (link->flags & IFF_LOOPBACK) |
60 | return false; | |
61 | ||
62 | if (!link->network) | |
63 | return false; | |
64 | ||
e0ee46f2 | 65 | return link->network->dhcp & ADDRESS_FAMILY_IPV4; |
78c958f8 TG |
66 | } |
67 | ||
b9d74c40 LP |
68 | static bool link_dhcp4_server_enabled(Link *link) { |
69 | assert(link); | |
70 | ||
78c958f8 TG |
71 | if (link->flags & IFF_LOOPBACK) |
72 | return false; | |
73 | ||
74 | if (!link->network) | |
75 | return false; | |
76 | ||
77 | return link->network->dhcp_server; | |
78 | } | |
79 | ||
b9d74c40 LP |
80 | static bool link_ipv4ll_enabled(Link *link) { |
81 | assert(link); | |
82 | ||
78c958f8 TG |
83 | if (link->flags & IFF_LOOPBACK) |
84 | return false; | |
85 | ||
86 | if (!link->network) | |
87 | return false; | |
88 | ||
e0ee46f2 | 89 | return link->network->link_local & ADDRESS_FAMILY_IPV4; |
d0d6a4cd TG |
90 | } |
91 | ||
b9d74c40 LP |
92 | static bool link_ipv6ll_enabled(Link *link) { |
93 | assert(link); | |
94 | ||
fa709992 LP |
95 | if (!socket_ipv6_is_supported()) |
96 | return false; | |
97 | ||
d0d6a4cd TG |
98 | if (link->flags & IFF_LOOPBACK) |
99 | return false; | |
100 | ||
101 | if (!link->network) | |
102 | return false; | |
103 | ||
e0ee46f2 | 104 | return link->network->link_local & ADDRESS_FAMILY_IPV6; |
78c958f8 TG |
105 | } |
106 | ||
439689c6 SS |
107 | static bool link_ipv6_enabled(Link *link) { |
108 | assert(link); | |
109 | ||
110 | if (!socket_ipv6_is_supported()) | |
111 | return false; | |
112 | ||
113 | return link_dhcp6_enabled(link) || link_ipv6ll_enabled(link) || network_has_static_ipv6_addresses(link->network); | |
114 | } | |
115 | ||
8e1ad1ea | 116 | static bool link_lldp_rx_enabled(Link *link) { |
b710e6b6 LP |
117 | assert(link); |
118 | ||
ce43e484 SS |
119 | if (link->flags & IFF_LOOPBACK) |
120 | return false; | |
121 | ||
b710e6b6 LP |
122 | if (link->iftype != ARPHRD_ETHER) |
123 | return false; | |
124 | ||
ce43e484 SS |
125 | if (!link->network) |
126 | return false; | |
127 | ||
5a8bcb67 | 128 | if (link->network->bridge) |
ce43e484 SS |
129 | return false; |
130 | ||
34437b4f | 131 | return link->network->lldp_mode != LLDP_MODE_NO; |
ce43e484 SS |
132 | } |
133 | ||
7272b25e | 134 | static bool link_lldp_emit_enabled(Link *link) { |
8e1ad1ea LP |
135 | assert(link); |
136 | ||
137 | if (link->flags & IFF_LOOPBACK) | |
138 | return false; | |
139 | ||
140 | if (link->iftype != ARPHRD_ETHER) | |
141 | return false; | |
142 | ||
143 | if (!link->network) | |
144 | return false; | |
145 | ||
7272b25e | 146 | return link->network->lldp_emit != LLDP_EMIT_NO; |
8e1ad1ea LP |
147 | } |
148 | ||
769d324c | 149 | static bool link_ipv4_forward_enabled(Link *link) { |
b9d74c40 LP |
150 | assert(link); |
151 | ||
5a8bcb67 LP |
152 | if (link->flags & IFF_LOOPBACK) |
153 | return false; | |
154 | ||
155 | if (!link->network) | |
156 | return false; | |
157 | ||
765afd5c LP |
158 | if (link->network->ip_forward == _ADDRESS_FAMILY_BOOLEAN_INVALID) |
159 | return false; | |
160 | ||
e0ee46f2 | 161 | return link->network->ip_forward & ADDRESS_FAMILY_IPV4; |
769d324c LP |
162 | } |
163 | ||
164 | static bool link_ipv6_forward_enabled(Link *link) { | |
b9d74c40 | 165 | assert(link); |
765afd5c LP |
166 | |
167 | if (!socket_ipv6_is_supported()) | |
168 | return false; | |
169 | ||
769d324c LP |
170 | if (link->flags & IFF_LOOPBACK) |
171 | return false; | |
172 | ||
173 | if (!link->network) | |
174 | return false; | |
175 | ||
765afd5c LP |
176 | if (link->network->ip_forward == _ADDRESS_FAMILY_BOOLEAN_INVALID) |
177 | return false; | |
178 | ||
e0ee46f2 | 179 | return link->network->ip_forward & ADDRESS_FAMILY_IPV6; |
5a8bcb67 LP |
180 | } |
181 | ||
23d8b221 SS |
182 | static bool link_proxy_arp_enabled(Link *link) { |
183 | assert(link); | |
184 | ||
185 | if (link->flags & IFF_LOOPBACK) | |
186 | return false; | |
187 | ||
188 | if (!link->network) | |
189 | return false; | |
190 | ||
191 | if (link->network->proxy_arp < 0) | |
192 | return false; | |
193 | ||
194 | return true; | |
195 | } | |
196 | ||
b9d74c40 LP |
197 | static bool link_ipv6_accept_ra_enabled(Link *link) { |
198 | assert(link); | |
199 | ||
fa709992 LP |
200 | if (!socket_ipv6_is_supported()) |
201 | return false; | |
202 | ||
f5a8c43f TG |
203 | if (link->flags & IFF_LOOPBACK) |
204 | return false; | |
205 | ||
206 | if (!link->network) | |
207 | return false; | |
208 | ||
209 | /* If unset use system default (enabled if local forwarding is disabled. | |
210 | * disabled if local forwarding is enabled). | |
211 | * If set, ignore or enforce RA independent of local forwarding state. | |
212 | */ | |
213 | if (link->network->ipv6_accept_ra < 0) | |
214 | /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */ | |
215 | return !link_ipv6_forward_enabled(link); | |
216 | else if (link->network->ipv6_accept_ra > 0) | |
217 | /* accept RA even if ip_forward is enabled */ | |
218 | return true; | |
219 | else | |
220 | /* ignore RA */ | |
221 | return false; | |
222 | } | |
223 | ||
1f0d9695 | 224 | static IPv6PrivacyExtensions link_ipv6_privacy_extensions(Link *link) { |
fa709992 | 225 | assert(link); |
d68e2e59 LP |
226 | |
227 | if (!socket_ipv6_is_supported()) | |
228 | return _IPV6_PRIVACY_EXTENSIONS_INVALID; | |
229 | ||
49092e22 | 230 | if (link->flags & IFF_LOOPBACK) |
1f0d9695 | 231 | return _IPV6_PRIVACY_EXTENSIONS_INVALID; |
49092e22 SS |
232 | |
233 | if (!link->network) | |
1f0d9695 | 234 | return _IPV6_PRIVACY_EXTENSIONS_INVALID; |
49092e22 SS |
235 | |
236 | return link->network->ipv6_privacy_extensions; | |
237 | } | |
238 | ||
439689c6 SS |
239 | static int link_enable_ipv6(Link *link) { |
240 | const char *p = NULL; | |
241 | bool disabled; | |
242 | int r; | |
243 | ||
244 | if (link->flags & IFF_LOOPBACK) | |
245 | return 0; | |
246 | ||
247 | disabled = !link_ipv6_enabled(link); | |
248 | ||
249 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/disable_ipv6"); | |
250 | ||
251 | r = write_string_file(p, one_zero(disabled), WRITE_STRING_FILE_VERIFY_ON_FAILURE); | |
252 | if (r < 0) | |
253 | log_link_warning_errno(link, r, "Cannot %s IPv6 for interface %s: %m", disabled ? "disable" : "enable", link->ifname); | |
254 | else { | |
255 | if (disabled) | |
256 | log_link_info(link, "IPv6 disabled for interface: %m"); | |
257 | else | |
258 | log_link_info(link, "IPv6 enabled for interface: %m"); | |
259 | } | |
260 | ||
261 | return 0; | |
262 | } | |
263 | ||
84de38c5 TG |
264 | void link_update_operstate(Link *link) { |
265 | LinkOperationalState operstate; | |
266 | assert(link); | |
267 | ||
268 | if (link->kernel_operstate == IF_OPER_DORMANT) | |
269 | operstate = LINK_OPERSTATE_DORMANT; | |
270 | else if (link_has_carrier(link)) { | |
271 | Address *address; | |
272 | uint8_t scope = RT_SCOPE_NOWHERE; | |
273 | Iterator i; | |
274 | ||
275 | /* if we have carrier, check what addresses we have */ | |
276 | SET_FOREACH(address, link->addresses, i) { | |
277 | if (!address_is_ready(address)) | |
278 | continue; | |
279 | ||
280 | if (address->scope < scope) | |
281 | scope = address->scope; | |
282 | } | |
283 | ||
284 | /* for operstate we also take foreign addresses into account */ | |
285 | SET_FOREACH(address, link->addresses_foreign, i) { | |
286 | if (!address_is_ready(address)) | |
287 | continue; | |
288 | ||
289 | if (address->scope < scope) | |
290 | scope = address->scope; | |
291 | } | |
292 | ||
293 | if (scope < RT_SCOPE_SITE) | |
294 | /* universally accessible addresses found */ | |
295 | operstate = LINK_OPERSTATE_ROUTABLE; | |
296 | else if (scope < RT_SCOPE_HOST) | |
297 | /* only link or site local addresses found */ | |
298 | operstate = LINK_OPERSTATE_DEGRADED; | |
299 | else | |
300 | /* no useful addresses found */ | |
301 | operstate = LINK_OPERSTATE_CARRIER; | |
302 | } else if (link->flags & IFF_UP) | |
303 | operstate = LINK_OPERSTATE_NO_CARRIER; | |
304 | else | |
305 | operstate = LINK_OPERSTATE_OFF; | |
306 | ||
307 | if (link->operstate != operstate) { | |
308 | link->operstate = operstate; | |
309 | link_send_changed(link, "OperationalState", NULL); | |
310 | link_dirty(link); | |
84de38c5 TG |
311 | } |
312 | } | |
313 | ||
51d18171 TG |
314 | #define FLAG_STRING(string, flag, old, new) \ |
315 | (((old ^ new) & flag) \ | |
316 | ? ((old & flag) ? (" -" string) : (" +" string)) \ | |
317 | : "") | |
318 | ||
1c4baffc | 319 | static int link_update_flags(Link *link, sd_netlink_message *m) { |
51d18171 TG |
320 | unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags; |
321 | uint8_t operstate; | |
322 | int r; | |
323 | ||
324 | assert(link); | |
325 | ||
326 | r = sd_rtnl_message_link_get_flags(m, &flags); | |
6a7a4e4d LP |
327 | if (r < 0) |
328 | return log_link_warning_errno(link, r, "Could not get link flags: %m"); | |
51d18171 | 329 | |
1c4baffc | 330 | r = sd_netlink_message_read_u8(m, IFLA_OPERSTATE, &operstate); |
51d18171 TG |
331 | if (r < 0) |
332 | /* if we got a message without operstate, take it to mean | |
333 | the state was unchanged */ | |
334 | operstate = link->kernel_operstate; | |
335 | ||
336 | if ((link->flags == flags) && (link->kernel_operstate == operstate)) | |
337 | return 0; | |
338 | ||
339 | if (link->flags != flags) { | |
6a7a4e4d | 340 | log_link_debug(link, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", |
51d18171 TG |
341 | FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags), |
342 | FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags), | |
343 | FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags), | |
344 | FLAG_STRING("UP", IFF_UP, link->flags, flags), | |
345 | FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags), | |
346 | FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags), | |
347 | FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags), | |
348 | FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags), | |
349 | FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags), | |
350 | FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags), | |
351 | FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags), | |
352 | FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags), | |
353 | FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags), | |
354 | FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags), | |
355 | FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags), | |
356 | FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags), | |
357 | FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags), | |
358 | FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags), | |
359 | FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags)); | |
360 | ||
361 | unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP | | |
362 | IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING | | |
363 | IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT | | |
364 | IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL | | |
365 | IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP | | |
366 | IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO); | |
367 | unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags); | |
368 | unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags); | |
369 | ||
370 | /* link flags are currently at most 18 bits, let's align to | |
371 | * printing 20 */ | |
372 | if (unknown_flags_added) | |
79008bdd | 373 | log_link_debug(link, |
6a7a4e4d | 374 | "Unknown link flags gained: %#.5x (ignoring)", |
51d18171 TG |
375 | unknown_flags_added); |
376 | ||
377 | if (unknown_flags_removed) | |
79008bdd | 378 | log_link_debug(link, |
6a7a4e4d | 379 | "Unknown link flags lost: %#.5x (ignoring)", |
51d18171 TG |
380 | unknown_flags_removed); |
381 | } | |
382 | ||
383 | link->flags = flags; | |
384 | link->kernel_operstate = operstate; | |
385 | ||
84de38c5 | 386 | link_update_operstate(link); |
51d18171 TG |
387 | |
388 | return 0; | |
389 | } | |
390 | ||
1c4baffc | 391 | static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) { |
14b746f7 | 392 | _cleanup_link_unref_ Link *link = NULL; |
505f8da7 | 393 | uint16_t type; |
ca4e095a | 394 | const char *ifname; |
505f8da7 | 395 | int r, ifindex; |
b710e6b6 | 396 | unsigned short iftype; |
f579559b | 397 | |
0c2f9b84 | 398 | assert(manager); |
505f8da7 | 399 | assert(message); |
f579559b TG |
400 | assert(ret); |
401 | ||
1c4baffc | 402 | r = sd_netlink_message_get_type(message, &type); |
505f8da7 TG |
403 | if (r < 0) |
404 | return r; | |
405 | else if (type != RTM_NEWLINK) | |
406 | return -EINVAL; | |
407 | ||
408 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); | |
409 | if (r < 0) | |
410 | return r; | |
411 | else if (ifindex <= 0) | |
412 | return -EINVAL; | |
413 | ||
b710e6b6 LP |
414 | r = sd_rtnl_message_link_get_type(message, &iftype); |
415 | if (r < 0) | |
416 | return r; | |
417 | ||
1c4baffc | 418 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname); |
505f8da7 TG |
419 | if (r < 0) |
420 | return r; | |
421 | ||
f579559b TG |
422 | link = new0(Link, 1); |
423 | if (!link) | |
424 | return -ENOMEM; | |
425 | ||
14b746f7 | 426 | link->n_ref = 1; |
5a3eb5a7 | 427 | link->manager = manager; |
8434fd5c | 428 | link->state = LINK_STATE_PENDING; |
be3a09b7 | 429 | link->rtnl_extended_attrs = true; |
505f8da7 | 430 | link->ifindex = ifindex; |
b710e6b6 | 431 | link->iftype = iftype; |
505f8da7 TG |
432 | link->ifname = strdup(ifname); |
433 | if (!link->ifname) | |
434 | return -ENOMEM; | |
f579559b | 435 | |
1c4baffc | 436 | r = sd_netlink_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac); |
512922f8 | 437 | if (r < 0) |
34437b4f | 438 | log_link_debug_errno(link, r, "MAC address not found for new device, continuing without"); |
512922f8 | 439 | |
34437b4f | 440 | if (asprintf(&link->state_file, "/run/systemd/netif/links/%d", link->ifindex) < 0) |
315db1a8 | 441 | return -ENOMEM; |
fe8db0c5 | 442 | |
34437b4f | 443 | if (asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", link->ifindex) < 0) |
68a8723c TG |
444 | return -ENOMEM; |
445 | ||
34437b4f | 446 | if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0) |
49699bac SS |
447 | return -ENOMEM; |
448 | ||
d5099efc | 449 | r = hashmap_ensure_allocated(&manager->links, NULL); |
ae06ab10 TG |
450 | if (r < 0) |
451 | return r; | |
452 | ||
453 | r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link); | |
f579559b TG |
454 | if (r < 0) |
455 | return r; | |
456 | ||
51d18171 TG |
457 | r = link_update_flags(link, message); |
458 | if (r < 0) | |
459 | return r; | |
460 | ||
f579559b TG |
461 | *ret = link; |
462 | link = NULL; | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
14b746f7 | 467 | static void link_free(Link *link) { |
428fd0a7 | 468 | Address *address; |
0d4ad91d AR |
469 | Iterator i; |
470 | Link *carrier; | |
428fd0a7 | 471 | |
f579559b TG |
472 | if (!link) |
473 | return; | |
474 | ||
cf1d700d TG |
475 | while (!set_isempty(link->addresses)) |
476 | address_free(set_first(link->addresses)); | |
477 | ||
adda1ed9 TG |
478 | while (!set_isempty(link->addresses_foreign)) |
479 | address_free(set_first(link->addresses_foreign)); | |
480 | ||
4701725c HV |
481 | link->addresses = set_free(link->addresses); |
482 | ||
483 | link->addresses_foreign = set_free(link->addresses_foreign); | |
adda1ed9 | 484 | |
11bf3cce LP |
485 | while ((address = link->pool_addresses)) { |
486 | LIST_REMOVE(addresses, link->pool_addresses, address); | |
487 | address_free(address); | |
488 | } | |
489 | ||
bfcdba8d | 490 | sd_dhcp_server_unref(link->dhcp_server); |
e5b04c8d | 491 | sd_dhcp_client_unref(link->dhcp_client); |
a6cc569e | 492 | sd_dhcp_lease_unref(link->dhcp_lease); |
f5be5601 | 493 | |
7272b25e | 494 | link_lldp_emit_stop(link); |
8e1ad1ea | 495 | |
68a8723c TG |
496 | free(link->lease_file); |
497 | ||
4afd3348 | 498 | sd_lldp_unref(link->lldp); |
49699bac SS |
499 | free(link->lldp_file); |
500 | ||
56cd007a | 501 | sd_ipv4ll_unref(link->ipv4ll); |
4138fb2c | 502 | sd_dhcp6_client_unref(link->dhcp6_client); |
de1e9928 | 503 | sd_ndisc_unref(link->ndisc_router_discovery); |
4138fb2c | 504 | |
28aeb07f | 505 | if (link->manager) |
ae06ab10 | 506 | hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)); |
f579559b | 507 | |
c166a070 | 508 | free(link->ifname); |
68a8723c | 509 | |
84de38c5 | 510 | (void)unlink(link->state_file); |
fe8db0c5 | 511 | free(link->state_file); |
c166a070 | 512 | |
b5db00e5 UTL |
513 | udev_device_unref(link->udev_device); |
514 | ||
0d4ad91d AR |
515 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) |
516 | hashmap_remove(link->bound_to_links, INT_TO_PTR(carrier->ifindex)); | |
517 | hashmap_free(link->bound_to_links); | |
518 | ||
519 | HASHMAP_FOREACH (carrier, link->bound_by_links, i) | |
520 | hashmap_remove(link->bound_by_links, INT_TO_PTR(carrier->ifindex)); | |
521 | hashmap_free(link->bound_by_links); | |
522 | ||
f579559b TG |
523 | free(link); |
524 | } | |
525 | ||
14b746f7 | 526 | Link *link_unref(Link *link) { |
957325b7 TG |
527 | if (!link) |
528 | return NULL; | |
529 | ||
530 | assert(link->n_ref > 0); | |
531 | ||
313cefa1 | 532 | link->n_ref--; |
957325b7 TG |
533 | |
534 | if (link->n_ref > 0) | |
535 | return NULL; | |
536 | ||
537 | link_free(link); | |
14b746f7 TG |
538 | |
539 | return NULL; | |
540 | } | |
541 | ||
542 | Link *link_ref(Link *link) { | |
957325b7 TG |
543 | if (!link) |
544 | return NULL; | |
545 | ||
546 | assert(link->n_ref > 0); | |
547 | ||
313cefa1 | 548 | link->n_ref++; |
14b746f7 TG |
549 | |
550 | return link; | |
551 | } | |
552 | ||
11a7f229 TG |
553 | int link_get(Manager *m, int ifindex, Link **ret) { |
554 | Link *link; | |
11a7f229 TG |
555 | |
556 | assert(m); | |
11a7f229 TG |
557 | assert(ifindex); |
558 | assert(ret); | |
559 | ||
ae06ab10 | 560 | link = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
11a7f229 TG |
561 | if (!link) |
562 | return -ENODEV; | |
563 | ||
564 | *ret = link; | |
565 | ||
566 | return 0; | |
567 | } | |
568 | ||
e331e246 TG |
569 | static void link_set_state(Link *link, LinkState state) { |
570 | assert(link); | |
571 | ||
572 | if (link->state == state) | |
573 | return; | |
574 | ||
575 | link->state = state; | |
576 | ||
577 | link_send_changed(link, "AdministrativeState", NULL); | |
e331e246 TG |
578 | } |
579 | ||
57bd6899 TG |
580 | static void link_enter_unmanaged(Link *link) { |
581 | assert(link); | |
582 | ||
6a7a4e4d | 583 | log_link_debug(link, "Unmanaged"); |
57bd6899 | 584 | |
e331e246 | 585 | link_set_state(link, LINK_STATE_UNMANAGED); |
57bd6899 | 586 | |
84de38c5 | 587 | link_dirty(link); |
57bd6899 TG |
588 | } |
589 | ||
111bb8f9 TG |
590 | static int link_stop_clients(Link *link) { |
591 | int r = 0, k; | |
592 | ||
593 | assert(link); | |
594 | assert(link->manager); | |
595 | assert(link->manager->event); | |
596 | ||
ba179154 | 597 | if (link->dhcp_client) { |
111bb8f9 | 598 | k = sd_dhcp_client_stop(link->dhcp_client); |
6a7a4e4d | 599 | if (k < 0) |
36c7d709 | 600 | r = log_link_warning_errno(link, k, "Could not stop DHCPv4 client: %m"); |
111bb8f9 TG |
601 | } |
602 | ||
ba179154 | 603 | if (link->ipv4ll) { |
111bb8f9 | 604 | k = sd_ipv4ll_stop(link->ipv4ll); |
6a7a4e4d | 605 | if (k < 0) |
36c7d709 | 606 | r = log_link_warning_errno(link, k, "Could not stop IPv4 link-local: %m"); |
dd43110f TG |
607 | } |
608 | ||
f5a8c43f TG |
609 | if (link->dhcp6_client) { |
610 | k = sd_dhcp6_client_stop(link->dhcp6_client); | |
611 | if (k < 0) | |
36c7d709 | 612 | r = log_link_warning_errno(link, k, "Could not stop DHCPv6 client: %m"); |
f5a8c43f | 613 | } |
4138fb2c | 614 | |
f5a8c43f | 615 | if (link->ndisc_router_discovery) { |
de1e9928 | 616 | k = sd_ndisc_stop(link->ndisc_router_discovery); |
6a7a4e4d | 617 | if (k < 0) |
36c7d709 | 618 | r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Discovery: %m"); |
4138fb2c PF |
619 | } |
620 | ||
7272b25e | 621 | link_lldp_emit_stop(link); |
111bb8f9 TG |
622 | return r; |
623 | } | |
624 | ||
b22d8a00 | 625 | void link_enter_failed(Link *link) { |
ef1ba606 | 626 | assert(link); |
f882c247 | 627 | |
370e9930 | 628 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
2139694e TG |
629 | return; |
630 | ||
6a7a4e4d | 631 | log_link_warning(link, "Failed"); |
449f7554 | 632 | |
e331e246 | 633 | link_set_state(link, LINK_STATE_FAILED); |
fe8db0c5 | 634 | |
111bb8f9 TG |
635 | link_stop_clients(link); |
636 | ||
84de38c5 | 637 | link_dirty(link); |
f882c247 TG |
638 | } |
639 | ||
4f434938 LP |
640 | static Address* link_find_dhcp_server_address(Link *link) { |
641 | Address *address; | |
642 | ||
643 | assert(link); | |
644 | assert(link->network); | |
645 | ||
d4cdbea5 | 646 | /* The first statically configured address if there is any */ |
4f434938 LP |
647 | LIST_FOREACH(addresses, address, link->network->static_addresses) { |
648 | ||
649 | if (address->family != AF_INET) | |
650 | continue; | |
651 | ||
af93291c | 652 | if (in_addr_is_null(address->family, &address->in_addr)) |
4f434938 LP |
653 | continue; |
654 | ||
655 | return address; | |
656 | } | |
657 | ||
658 | /* If that didn't work, find a suitable address we got from the pool */ | |
659 | LIST_FOREACH(addresses, address, link->pool_addresses) { | |
660 | if (address->family != AF_INET) | |
661 | continue; | |
662 | ||
663 | return address; | |
664 | } | |
665 | ||
666 | return NULL; | |
667 | } | |
668 | ||
dd43110f | 669 | static int link_enter_configured(Link *link) { |
dd43110f TG |
670 | assert(link); |
671 | assert(link->network); | |
672 | assert(link->state == LINK_STATE_SETTING_ROUTES); | |
673 | ||
6a7a4e4d | 674 | log_link_info(link, "Configured"); |
dd43110f | 675 | |
e331e246 | 676 | link_set_state(link, LINK_STATE_CONFIGURED); |
dd43110f | 677 | |
84de38c5 | 678 | link_dirty(link); |
dd43110f TG |
679 | |
680 | return 0; | |
681 | } | |
682 | ||
8012cd39 TG |
683 | void link_check_ready(Link *link) { |
684 | Address *a; | |
685 | Iterator i; | |
686 | ||
3c9b8860 | 687 | assert(link); |
adda1ed9 TG |
688 | |
689 | if (!link->network) | |
690 | return; | |
3c9b8860 TG |
691 | |
692 | if (!link->static_configured) | |
693 | return; | |
694 | ||
78c958f8 | 695 | if (link_ipv4ll_enabled(link)) |
3c9b8860 TG |
696 | if (!link->ipv4ll_address || |
697 | !link->ipv4ll_route) | |
698 | return; | |
699 | ||
e7ab854c | 700 | if (link_ipv6ll_enabled(link)) |
c601ebf7 | 701 | if (in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address) > 0) |
e7ab854c TG |
702 | return; |
703 | ||
3ada37a2 PF |
704 | if ((link_dhcp4_enabled(link) && !link_dhcp6_enabled(link) && |
705 | !link->dhcp4_configured) || | |
706 | (link_dhcp6_enabled(link) && !link_dhcp4_enabled(link) && | |
707 | !link->dhcp6_configured) || | |
708 | (link_dhcp4_enabled(link) && link_dhcp6_enabled(link) && | |
709 | !link->dhcp4_configured && !link->dhcp6_configured)) | |
18d29550 PF |
710 | return; |
711 | ||
fe307276 TG |
712 | if (link_ipv6_accept_ra_enabled(link) && !link->ndisc_configured) |
713 | return; | |
714 | ||
8012cd39 TG |
715 | SET_FOREACH(a, link->addresses, i) |
716 | if (!address_is_ready(a)) | |
717 | return; | |
718 | ||
9fdaa992 TG |
719 | if (link->state != LINK_STATE_CONFIGURED) |
720 | link_enter_configured(link); | |
3c9b8860 TG |
721 | |
722 | return; | |
723 | } | |
724 | ||
1c4baffc | 725 | static int route_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
5da8149f | 726 | _cleanup_link_unref_ Link *link = userdata; |
f882c247 TG |
727 | int r; |
728 | ||
3c9b8860 | 729 | assert(link->link_messages > 0); |
370e9930 TG |
730 | assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES, |
731 | LINK_STATE_SETTING_ROUTES, LINK_STATE_FAILED, | |
732 | LINK_STATE_LINGER)); | |
f882c247 | 733 | |
313cefa1 | 734 | link->link_messages--; |
f882c247 | 735 | |
77a008c0 | 736 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
737 | return 1; |
738 | ||
1c4baffc | 739 | r = sd_netlink_message_get_errno(m); |
c166a070 | 740 | if (r < 0 && r != -EEXIST) |
a2fae7bb | 741 | log_link_warning_errno(link, r, "Could not set route: %m"); |
f882c247 | 742 | |
3c9b8860 | 743 | if (link->link_messages == 0) { |
6a7a4e4d | 744 | log_link_debug(link, "Routes set"); |
3c9b8860 | 745 | link->static_configured = true; |
8012cd39 | 746 | link_check_ready(link); |
dd3efc09 | 747 | } |
f882c247 TG |
748 | |
749 | return 1; | |
750 | } | |
751 | ||
752 | static int link_enter_set_routes(Link *link) { | |
a6cc569e | 753 | Route *rt; |
f882c247 TG |
754 | int r; |
755 | ||
756 | assert(link); | |
757 | assert(link->network); | |
ef1ba606 | 758 | assert(link->state == LINK_STATE_SETTING_ADDRESSES); |
f882c247 | 759 | |
e331e246 | 760 | link_set_state(link, LINK_STATE_SETTING_ROUTES); |
f882c247 | 761 | |
3d3d4255 | 762 | LIST_FOREACH(routes, rt, link->network->static_routes) { |
483d099e | 763 | r = route_configure(rt, link, route_handler); |
dd3efc09 | 764 | if (r < 0) { |
6a7a4e4d | 765 | log_link_warning_errno(link, r, "Could not set routes: %m"); |
3c9b8860 | 766 | link_enter_failed(link); |
a6cc569e TG |
767 | return r; |
768 | } | |
769 | ||
313cefa1 | 770 | link->link_messages++; |
8ddbeaa2 | 771 | } |
f5be5601 | 772 | |
3c9b8860 TG |
773 | if (link->link_messages == 0) { |
774 | link->static_configured = true; | |
8012cd39 | 775 | link_check_ready(link); |
431ca2ce | 776 | } else |
6a7a4e4d | 777 | log_link_debug(link, "Setting routes"); |
f882c247 TG |
778 | |
779 | return 0; | |
780 | } | |
781 | ||
91b5f997 | 782 | int link_route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
5da8149f | 783 | _cleanup_link_unref_ Link *link = userdata; |
5c1d3fc9 UTL |
784 | int r; |
785 | ||
786 | assert(m); | |
787 | assert(link); | |
788 | assert(link->ifname); | |
789 | ||
5da8149f | 790 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
5c1d3fc9 UTL |
791 | return 1; |
792 | ||
1c4baffc | 793 | r = sd_netlink_message_get_errno(m); |
b90b025a | 794 | if (r < 0 && r != -ESRCH) |
a2fae7bb | 795 | log_link_warning_errno(link, r, "Could not drop route: %m"); |
5c1d3fc9 | 796 | |
5bdd314c | 797 | return 1; |
5c1d3fc9 UTL |
798 | } |
799 | ||
1c4baffc | 800 | static int address_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
5da8149f | 801 | _cleanup_link_unref_ Link *link = userdata; |
f882c247 TG |
802 | int r; |
803 | ||
4958aee4 | 804 | assert(rtnl); |
f5be5601 TG |
805 | assert(m); |
806 | assert(link); | |
807 | assert(link->ifname); | |
3c9b8860 | 808 | assert(link->link_messages > 0); |
370e9930 TG |
809 | assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES, |
810 | LINK_STATE_FAILED, LINK_STATE_LINGER)); | |
f882c247 | 811 | |
313cefa1 | 812 | link->link_messages--; |
f882c247 | 813 | |
5da8149f | 814 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
815 | return 1; |
816 | ||
1c4baffc | 817 | r = sd_netlink_message_get_errno(m); |
c166a070 | 818 | if (r < 0 && r != -EEXIST) |
a2fae7bb | 819 | log_link_warning_errno(link, r, "could not set address: %m"); |
45af44d4 | 820 | else if (r >= 0) |
200a0868 | 821 | manager_rtnl_process_address(rtnl, m, link->manager); |
f882c247 | 822 | |
3c9b8860 | 823 | if (link->link_messages == 0) { |
6a7a4e4d | 824 | log_link_debug(link, "Addresses set"); |
ef1ba606 | 825 | link_enter_set_routes(link); |
dd3efc09 | 826 | } |
f882c247 TG |
827 | |
828 | return 1; | |
829 | } | |
830 | ||
4f5f911e LP |
831 | static int link_push_dns_to_dhcp_server(Link *link, sd_dhcp_server *s) { |
832 | _cleanup_free_ struct in_addr *addresses = NULL; | |
833 | size_t n_addresses = 0, n_allocated = 0; | |
834 | char **a; | |
835 | ||
836 | log_debug("Copying DNS server information from %s", link->ifname); | |
837 | ||
838 | if (!link->network) | |
839 | return 0; | |
840 | ||
841 | STRV_FOREACH(a, link->network->dns) { | |
842 | struct in_addr ia; | |
843 | ||
844 | /* Only look for IPv4 addresses */ | |
845 | if (inet_pton(AF_INET, *a, &ia) <= 0) | |
846 | continue; | |
847 | ||
848 | if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1)) | |
849 | return log_oom(); | |
850 | ||
851 | addresses[n_addresses++] = ia; | |
852 | } | |
853 | ||
27cb34f5 | 854 | if (link->network->dhcp_use_dns && |
4f5f911e LP |
855 | link->dhcp_lease) { |
856 | const struct in_addr *da = NULL; | |
857 | int n; | |
858 | ||
859 | n = sd_dhcp_lease_get_dns(link->dhcp_lease, &da); | |
860 | if (n > 0) { | |
861 | ||
862 | if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + n)) | |
863 | return log_oom(); | |
864 | ||
865 | memcpy(addresses + n_addresses, da, n * sizeof(struct in_addr)); | |
866 | n_addresses += n; | |
867 | } | |
868 | } | |
869 | ||
870 | if (n_addresses <= 0) | |
871 | return 0; | |
872 | ||
873 | return sd_dhcp_server_set_dns(s, addresses, n_addresses); | |
874 | } | |
875 | ||
876 | static int link_push_ntp_to_dhcp_server(Link *link, sd_dhcp_server *s) { | |
877 | _cleanup_free_ struct in_addr *addresses = NULL; | |
878 | size_t n_addresses = 0, n_allocated = 0; | |
879 | char **a; | |
880 | ||
881 | if (!link->network) | |
882 | return 0; | |
883 | ||
884 | log_debug("Copying NTP server information from %s", link->ifname); | |
885 | ||
886 | STRV_FOREACH(a, link->network->ntp) { | |
887 | struct in_addr ia; | |
888 | ||
889 | /* Only look for IPv4 addresses */ | |
890 | if (inet_pton(AF_INET, *a, &ia) <= 0) | |
891 | continue; | |
892 | ||
893 | if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1)) | |
894 | return log_oom(); | |
895 | ||
896 | addresses[n_addresses++] = ia; | |
897 | } | |
898 | ||
27cb34f5 | 899 | if (link->network->dhcp_use_ntp && |
4f5f911e LP |
900 | link->dhcp_lease) { |
901 | const struct in_addr *da = NULL; | |
902 | int n; | |
903 | ||
904 | n = sd_dhcp_lease_get_ntp(link->dhcp_lease, &da); | |
905 | if (n > 0) { | |
906 | ||
907 | if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + n)) | |
908 | return log_oom(); | |
909 | ||
910 | memcpy(addresses + n_addresses, da, n * sizeof(struct in_addr)); | |
911 | n_addresses += n; | |
912 | } | |
913 | } | |
914 | ||
915 | if (n_addresses <= 0) | |
916 | return 0; | |
917 | ||
918 | return sd_dhcp_server_set_ntp(s, addresses, n_addresses); | |
919 | } | |
920 | ||
f882c247 | 921 | static int link_enter_set_addresses(Link *link) { |
a6cc569e | 922 | Address *ad; |
f882c247 TG |
923 | int r; |
924 | ||
925 | assert(link); | |
926 | assert(link->network); | |
f5be5601 | 927 | assert(link->state != _LINK_STATE_INVALID); |
f882c247 | 928 | |
e331e246 | 929 | link_set_state(link, LINK_STATE_SETTING_ADDRESSES); |
f882c247 | 930 | |
3d3d4255 | 931 | LIST_FOREACH(addresses, ad, link->network->static_addresses) { |
483d099e | 932 | r = address_configure(ad, link, address_handler, false); |
dd3efc09 | 933 | if (r < 0) { |
5a8bcb67 | 934 | log_link_warning_errno(link, r, "Could not set addresses: %m"); |
f5be5601 TG |
935 | link_enter_failed(link); |
936 | return r; | |
937 | } | |
938 | ||
313cefa1 | 939 | link->link_messages++; |
f882c247 TG |
940 | } |
941 | ||
d4cdbea5 TG |
942 | /* now that we can figure out a default address for the dhcp server, |
943 | start it */ | |
944 | if (link_dhcp4_server_enabled(link)) { | |
d4cdbea5 | 945 | Address *address; |
4f5f911e LP |
946 | Link *uplink = NULL; |
947 | bool acquired_uplink = false; | |
d4cdbea5 TG |
948 | |
949 | address = link_find_dhcp_server_address(link); | |
950 | if (!address) { | |
6a7a4e4d | 951 | log_link_warning(link, "Failed to find suitable address for DHCPv4 server instance."); |
d4cdbea5 TG |
952 | link_enter_failed(link); |
953 | return 0; | |
954 | } | |
955 | ||
61986155 | 956 | /* use the server address' subnet as the pool */ |
9b3a67c5 TG |
957 | r = sd_dhcp_server_configure_pool(link->dhcp_server, &address->in_addr.in, address->prefixlen, |
958 | link->network->dhcp_server_pool_offset, link->network->dhcp_server_pool_size); | |
d4cdbea5 TG |
959 | if (r < 0) |
960 | return r; | |
961 | ||
962 | /* TODO: | |
963 | r = sd_dhcp_server_set_router(link->dhcp_server, | |
964 | &main_address->in_addr.in); | |
965 | if (r < 0) | |
966 | return r; | |
d4cdbea5 TG |
967 | */ |
968 | ||
586ac6f7 LP |
969 | if (link->network->dhcp_server_max_lease_time_usec > 0) { |
970 | r = sd_dhcp_server_set_max_lease_time( | |
971 | link->dhcp_server, | |
972 | DIV_ROUND_UP(link->network->dhcp_server_max_lease_time_usec, USEC_PER_SEC)); | |
973 | if (r < 0) | |
974 | return r; | |
975 | } | |
976 | ||
977 | if (link->network->dhcp_server_default_lease_time_usec > 0) { | |
978 | r = sd_dhcp_server_set_default_lease_time( | |
979 | link->dhcp_server, | |
980 | DIV_ROUND_UP(link->network->dhcp_server_default_lease_time_usec, USEC_PER_SEC)); | |
981 | if (r < 0) | |
982 | return r; | |
983 | } | |
984 | ||
1a04db0f LP |
985 | if (link->network->dhcp_server_emit_dns) { |
986 | ||
4f5f911e | 987 | if (link->network->n_dhcp_server_dns > 0) |
1a04db0f | 988 | r = sd_dhcp_server_set_dns(link->dhcp_server, link->network->dhcp_server_dns, link->network->n_dhcp_server_dns); |
4f5f911e LP |
989 | else { |
990 | uplink = manager_find_uplink(link->manager, link); | |
991 | acquired_uplink = true; | |
992 | ||
993 | if (!uplink) { | |
994 | log_link_debug(link, "Not emitting DNS server information on link, couldn't find suitable uplink."); | |
995 | r = 0; | |
996 | } else | |
997 | r = link_push_dns_to_dhcp_server(uplink, link->dhcp_server); | |
998 | } | |
999 | if (r < 0) | |
1000 | log_link_warning_errno(link, r, "Failed to set DNS server for DHCP server, ignoring: %m"); | |
1a04db0f LP |
1001 | } |
1002 | ||
1003 | ||
1004 | if (link->network->dhcp_server_emit_ntp) { | |
1005 | ||
4f5f911e | 1006 | if (link->network->n_dhcp_server_ntp > 0) |
1a04db0f | 1007 | r = sd_dhcp_server_set_ntp(link->dhcp_server, link->network->dhcp_server_ntp, link->network->n_dhcp_server_ntp); |
4f5f911e LP |
1008 | else { |
1009 | if (!acquired_uplink) | |
1010 | uplink = manager_find_uplink(link->manager, link); | |
1011 | ||
1012 | if (!uplink) { | |
1013 | log_link_debug(link, "Not emitting NTP server information on link, couldn't find suitable uplink."); | |
1014 | r = 0; | |
1015 | } else | |
1016 | r = link_push_ntp_to_dhcp_server(uplink, link->dhcp_server); | |
1017 | ||
1018 | } | |
1019 | if (r < 0) | |
1020 | log_link_warning_errno(link, r, "Failed to set NTP server for DHCP server, ignoring: %m"); | |
1a04db0f LP |
1021 | } |
1022 | ||
77ff6022 CG |
1023 | r = sd_dhcp_server_set_emit_router(link->dhcp_server, link->network->dhcp_server_emit_router); |
1024 | if (r < 0) { | |
1025 | log_link_warning_errno(link, r, "Failed to set router emission for DHCP server: %m"); | |
1026 | return r; | |
1027 | } | |
1028 | ||
8eb9058d LP |
1029 | if (link->network->dhcp_server_emit_timezone) { |
1030 | _cleanup_free_ char *buffer = NULL; | |
0ab8a1b6 | 1031 | const char *tz = NULL; |
8eb9058d LP |
1032 | |
1033 | if (link->network->dhcp_server_timezone) | |
1034 | tz = link->network->dhcp_server_timezone; | |
1035 | else { | |
1036 | r = get_timezone(&buffer); | |
1037 | if (r < 0) | |
1038 | log_warning_errno(r, "Failed to determine timezone: %m"); | |
1039 | else | |
1040 | tz = buffer; | |
1041 | } | |
1042 | ||
1043 | if (tz) { | |
1044 | r = sd_dhcp_server_set_timezone(link->dhcp_server, tz); | |
1045 | if (r < 0) | |
1046 | return r; | |
1047 | } | |
1048 | } | |
1049 | ||
d4cdbea5 TG |
1050 | r = sd_dhcp_server_start(link->dhcp_server); |
1051 | if (r < 0) { | |
6a7a4e4d | 1052 | log_link_warning_errno(link, r, "Could not start DHCPv4 server instance: %m"); |
d4cdbea5 TG |
1053 | |
1054 | link_enter_failed(link); | |
1055 | ||
1056 | return 0; | |
1057 | } | |
1058 | ||
6a7a4e4d | 1059 | log_link_debug(link, "Offering DHCPv4 leases"); |
d4cdbea5 TG |
1060 | } |
1061 | ||
6a7a4e4d | 1062 | if (link->link_messages == 0) |
431ca2ce | 1063 | link_enter_set_routes(link); |
6a7a4e4d LP |
1064 | else |
1065 | log_link_debug(link, "Setting addresses"); | |
431ca2ce | 1066 | |
f882c247 TG |
1067 | return 0; |
1068 | } | |
1069 | ||
91b5f997 | 1070 | int link_address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
5da8149f | 1071 | _cleanup_link_unref_ Link *link = userdata; |
ff254138 TG |
1072 | int r; |
1073 | ||
1074 | assert(m); | |
1075 | assert(link); | |
1076 | assert(link->ifname); | |
1077 | ||
5da8149f | 1078 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
ff254138 TG |
1079 | return 1; |
1080 | ||
1c4baffc | 1081 | r = sd_netlink_message_get_errno(m); |
b90b025a | 1082 | if (r < 0 && r != -EADDRNOTAVAIL) |
a2fae7bb | 1083 | log_link_warning_errno(link, r, "Could not drop address: %m"); |
ff254138 | 1084 | |
5bdd314c | 1085 | return 1; |
ff254138 TG |
1086 | } |
1087 | ||
a245ced0 | 1088 | static int link_set_bridge_fdb(Link *const link) { |
b98b483b AR |
1089 | FdbEntry *fdb_entry; |
1090 | int r = 0; | |
1091 | ||
1092 | LIST_FOREACH(static_fdb_entries, fdb_entry, link->network->static_fdb_entries) { | |
ea6ec096 | 1093 | r = fdb_entry_configure(link, fdb_entry); |
9ed794a3 | 1094 | if (r < 0) { |
6a7a4e4d | 1095 | log_link_error_errno(link, r, "Failed to add MAC entry to static MAC table: %m"); |
b98b483b AR |
1096 | break; |
1097 | } | |
1098 | } | |
1099 | ||
1100 | return r; | |
1101 | } | |
1102 | ||
23d8b221 SS |
1103 | static int link_set_proxy_arp(Link *const link) { |
1104 | const char *p = NULL; | |
1105 | int r; | |
1106 | ||
1107 | if (!link_proxy_arp_enabled(link)) | |
1108 | return 0; | |
1109 | ||
1110 | p = strjoina("/proc/sys/net/ipv4/conf/", link->ifname, "/proxy_arp"); | |
1111 | ||
1112 | r = write_string_file(p, one_zero(link->network->proxy_arp), WRITE_STRING_FILE_VERIFY_ON_FAILURE); | |
1113 | if (r < 0) | |
1114 | log_link_warning_errno(link, r, "Cannot configure proxy ARP for interface: %m"); | |
1115 | ||
1116 | return 0; | |
1117 | } | |
1118 | ||
1c4baffc | 1119 | static int link_set_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
e1853b00 SS |
1120 | _cleanup_link_unref_ Link *link = userdata; |
1121 | int r; | |
1122 | ||
6a7a4e4d | 1123 | log_link_debug(link, "Set link"); |
e1853b00 | 1124 | |
1c4baffc | 1125 | r = sd_netlink_message_get_errno(m); |
e1853b00 | 1126 | if (r < 0 && r != -EEXIST) { |
f2341e0a | 1127 | log_link_error_errno(link, r, "Could not join netdev: %m"); |
e1853b00 SS |
1128 | link_enter_failed(link); |
1129 | return 1; | |
1130 | } | |
1131 | ||
1132 | return 0; | |
1133 | } | |
1134 | ||
19070062 | 1135 | static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
5da8149f | 1136 | _cleanup_link_unref_ Link *link = userdata; |
21b80ad1 | 1137 | const sd_bus_error *e; |
1346b1f0 | 1138 | |
19070062 | 1139 | assert(m); |
b226d99b TG |
1140 | assert(link); |
1141 | ||
5da8149f | 1142 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
370e9930 | 1143 | return 1; |
370e9930 | 1144 | |
21b80ad1 LP |
1145 | e = sd_bus_message_get_error(m); |
1146 | if (e) | |
1147 | log_link_warning_errno(link, sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message); | |
1346b1f0 TG |
1148 | |
1149 | return 1; | |
1150 | } | |
1151 | ||
3c9b8860 | 1152 | int link_set_hostname(Link *link, const char *hostname) { |
21b80ad1 | 1153 | int r; |
1346b1f0 | 1154 | |
b226d99b TG |
1155 | assert(link); |
1156 | assert(link->manager); | |
1346b1f0 | 1157 | |
dce391e7 | 1158 | log_link_debug(link, "Setting transient hostname: '%s'", strna(hostname)); |
1346b1f0 | 1159 | |
3c9b8860 TG |
1160 | if (!link->manager->bus) { |
1161 | /* TODO: replace by assert when we can rely on kdbus */ | |
6a7a4e4d | 1162 | log_link_info(link, "Not connected to system bus, ignoring transient hostname."); |
bcbca829 TG |
1163 | return 0; |
1164 | } | |
1165 | ||
9c34154a | 1166 | r = sd_bus_call_method_async( |
b226d99b | 1167 | link->manager->bus, |
9c34154a | 1168 | NULL, |
1346b1f0 TG |
1169 | "org.freedesktop.hostname1", |
1170 | "/org/freedesktop/hostname1", | |
1171 | "org.freedesktop.hostname1", | |
9c34154a UTL |
1172 | "SetHostname", |
1173 | set_hostname_handler, | |
1174 | link, | |
1175 | "sb", | |
1176 | hostname, | |
1177 | false); | |
1346b1f0 | 1178 | |
6a7a4e4d LP |
1179 | if (r < 0) |
1180 | return log_link_error_errno(link, r, "Could not set transient hostname: %m"); | |
b226d99b TG |
1181 | |
1182 | link_ref(link); | |
1346b1f0 | 1183 | |
5da8149f | 1184 | return 0; |
1346b1f0 TG |
1185 | } |
1186 | ||
21b80ad1 LP |
1187 | static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
1188 | _cleanup_link_unref_ Link *link = userdata; | |
1189 | const sd_bus_error *e; | |
1190 | ||
1191 | assert(m); | |
1192 | assert(link); | |
1193 | ||
1194 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1195 | return 1; | |
1196 | ||
1197 | e = sd_bus_message_get_error(m); | |
1198 | if (e) | |
1199 | log_link_warning_errno(link, sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message); | |
1200 | ||
1201 | return 1; | |
1202 | } | |
1203 | ||
64d6c229 | 1204 | int link_set_timezone(Link *link, const char *tz) { |
21b80ad1 LP |
1205 | int r; |
1206 | ||
1207 | assert(link); | |
1208 | assert(link->manager); | |
64d6c229 | 1209 | assert(tz); |
21b80ad1 | 1210 | |
64d6c229 | 1211 | log_link_debug(link, "Setting system timezone: '%s'", tz); |
21b80ad1 LP |
1212 | |
1213 | if (!link->manager->bus) { | |
1214 | log_link_info(link, "Not connected to system bus, ignoring timezone."); | |
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | r = sd_bus_call_method_async( | |
1219 | link->manager->bus, | |
1220 | NULL, | |
1221 | "org.freedesktop.timedate1", | |
1222 | "/org/freedesktop/timedate1", | |
1223 | "org.freedesktop.timedate1", | |
1224 | "SetTimezone", | |
1225 | set_timezone_handler, | |
1226 | link, | |
1227 | "sb", | |
64d6c229 | 1228 | tz, |
21b80ad1 LP |
1229 | false); |
1230 | if (r < 0) | |
1231 | return log_link_error_errno(link, r, "Could not set timezone: %m"); | |
1232 | ||
1233 | link_ref(link); | |
1234 | ||
1235 | return 0; | |
1236 | } | |
1237 | ||
1c4baffc | 1238 | static int set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
5da8149f | 1239 | _cleanup_link_unref_ Link *link = userdata; |
4f882b2a TG |
1240 | int r; |
1241 | ||
1242 | assert(m); | |
1243 | assert(link); | |
1244 | assert(link->ifname); | |
1245 | ||
5da8149f | 1246 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
4f882b2a TG |
1247 | return 1; |
1248 | ||
1c4baffc | 1249 | r = sd_netlink_message_get_errno(m); |
c9ccc19f | 1250 | if (r < 0) |
a2fae7bb | 1251 | log_link_warning_errno(link, r, "Could not set MTU: %m"); |
4f882b2a TG |
1252 | |
1253 | return 1; | |
1254 | } | |
1255 | ||
3c9b8860 | 1256 | int link_set_mtu(Link *link, uint32_t mtu) { |
4afd3348 | 1257 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f882b2a TG |
1258 | int r; |
1259 | ||
1260 | assert(link); | |
1261 | assert(link->manager); | |
1262 | assert(link->manager->rtnl); | |
1263 | ||
6a7a4e4d | 1264 | log_link_debug(link, "Setting MTU: %" PRIu32, mtu); |
4f882b2a | 1265 | |
6a7a4e4d LP |
1266 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1267 | if (r < 0) | |
1268 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
4f882b2a | 1269 | |
1c4baffc | 1270 | r = sd_netlink_message_append_u32(req, IFLA_MTU, mtu); |
6a7a4e4d LP |
1271 | if (r < 0) |
1272 | return log_link_error_errno(link, r, "Could not append MTU: %m"); | |
4f882b2a | 1273 | |
1c4baffc | 1274 | r = sd_netlink_call_async(link->manager->rtnl, req, set_mtu_handler, link, 0, NULL); |
6a7a4e4d LP |
1275 | if (r < 0) |
1276 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
4f882b2a | 1277 | |
ae941762 | 1278 | link_ref(link); |
b226d99b | 1279 | |
4f882b2a TG |
1280 | return 0; |
1281 | } | |
1282 | ||
e1853b00 | 1283 | static int link_set_bridge(Link *link) { |
4afd3348 | 1284 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
e1853b00 SS |
1285 | int r; |
1286 | ||
1287 | assert(link); | |
1288 | assert(link->network); | |
1289 | ||
6a7a4e4d LP |
1290 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1291 | if (r < 0) | |
1292 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
e1853b00 SS |
1293 | |
1294 | r = sd_rtnl_message_link_set_family(req, PF_BRIDGE); | |
6a7a4e4d LP |
1295 | if (r < 0) |
1296 | return log_link_error_errno(link, r, "Could not set message family: %m"); | |
e1853b00 | 1297 | |
1c4baffc | 1298 | r = sd_netlink_message_open_container(req, IFLA_PROTINFO); |
6a7a4e4d LP |
1299 | if (r < 0) |
1300 | return log_link_error_errno(link, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
e1853b00 | 1301 | |
84c34096 | 1302 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_GUARD, !link->network->use_bpdu); |
eb7ff4dd SS |
1303 | if (r < 0) |
1304 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_GUARD attribute: %m"); | |
1305 | ||
1306 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_MODE, link->network->hairpin); | |
1307 | if (r < 0) | |
1308 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_MODE attribute: %m"); | |
1309 | ||
1310 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_FAST_LEAVE, link->network->fast_leave); | |
1311 | if (r < 0) | |
1312 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m"); | |
1313 | ||
23da66bb | 1314 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_PROTECT, !link->network->allow_port_to_be_root); |
eb7ff4dd SS |
1315 | if (r < 0) |
1316 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_PROTECT attribute: %m"); | |
1317 | ||
1318 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_UNICAST_FLOOD, link->network->unicast_flood); | |
1319 | if (r < 0) | |
1320 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m"); | |
1321 | ||
34437b4f | 1322 | if (link->network->cost != 0) { |
1c4baffc | 1323 | r = sd_netlink_message_append_u32(req, IFLA_BRPORT_COST, link->network->cost); |
6a7a4e4d LP |
1324 | if (r < 0) |
1325 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_COST attribute: %m"); | |
e1853b00 SS |
1326 | } |
1327 | ||
1c4baffc | 1328 | r = sd_netlink_message_close_container(req); |
6a7a4e4d LP |
1329 | if (r < 0) |
1330 | return log_link_error_errno(link, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
e1853b00 | 1331 | |
1c4baffc | 1332 | r = sd_netlink_call_async(link->manager->rtnl, req, link_set_handler, link, 0, NULL); |
6a7a4e4d LP |
1333 | if (r < 0) |
1334 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
e1853b00 SS |
1335 | |
1336 | link_ref(link); | |
1337 | ||
1338 | return r; | |
1339 | } | |
1340 | ||
34437b4f LP |
1341 | static int link_lldp_save(Link *link) { |
1342 | _cleanup_free_ char *temp_path = NULL; | |
1343 | _cleanup_fclose_ FILE *f = NULL; | |
1344 | sd_lldp_neighbor **l = NULL; | |
1345 | int n = 0, r, i; | |
49699bac SS |
1346 | |
1347 | assert(link); | |
34437b4f LP |
1348 | assert(link->lldp_file); |
1349 | ||
1350 | if (!link->lldp) { | |
1351 | (void) unlink(link->lldp_file); | |
1352 | return 0; | |
1353 | } | |
1354 | ||
1355 | r = sd_lldp_get_neighbors(link->lldp, &l); | |
1356 | if (r < 0) | |
1357 | goto finish; | |
1358 | if (r == 0) { | |
1359 | (void) unlink(link->lldp_file); | |
1360 | goto finish; | |
1361 | } | |
1362 | ||
1363 | n = r; | |
1364 | ||
1365 | r = fopen_temporary(link->lldp_file, &f, &temp_path); | |
1366 | if (r < 0) | |
1367 | goto finish; | |
1368 | ||
1369 | fchmod(fileno(f), 0644); | |
49699bac | 1370 | |
34437b4f LP |
1371 | for (i = 0; i < n; i++) { |
1372 | const void *p; | |
1373 | le64_t u; | |
1374 | size_t sz; | |
1375 | ||
1376 | r = sd_lldp_neighbor_get_raw(l[i], &p, &sz); | |
9ef61f2e | 1377 | if (r < 0) |
34437b4f LP |
1378 | goto finish; |
1379 | ||
1380 | u = htole64(sz); | |
1381 | (void) fwrite(&u, 1, sizeof(u), f); | |
1382 | (void) fwrite(p, 1, sz, f); | |
1383 | } | |
49699bac | 1384 | |
34437b4f LP |
1385 | r = fflush_and_check(f); |
1386 | if (r < 0) | |
1387 | goto finish; | |
1388 | ||
1389 | if (rename(temp_path, link->lldp_file) < 0) { | |
1390 | r = -errno; | |
1391 | goto finish; | |
9ef61f2e | 1392 | } |
34437b4f LP |
1393 | |
1394 | finish: | |
1395 | if (r < 0) { | |
1396 | (void) unlink(link->lldp_file); | |
1397 | if (temp_path) | |
1398 | (void) unlink(temp_path); | |
1399 | ||
1400 | log_link_error_errno(link, r, "Failed to save LLDP data to %s: %m", link->lldp_file); | |
1401 | } | |
1402 | ||
1403 | if (l) { | |
1404 | for (i = 0; i < n; i++) | |
1405 | sd_lldp_neighbor_unref(l[i]); | |
1406 | free(l); | |
1407 | } | |
1408 | ||
1409 | return r; | |
1410 | } | |
1411 | ||
90dffb22 | 1412 | static void lldp_handler(sd_lldp *lldp, sd_lldp_event event, sd_lldp_neighbor *n, void *userdata) { |
34437b4f | 1413 | Link *link = userdata; |
90dffb22 | 1414 | int r; |
34437b4f LP |
1415 | |
1416 | assert(link); | |
1417 | ||
1418 | (void) link_lldp_save(link); | |
90dffb22 | 1419 | |
7272b25e | 1420 | if (link_lldp_emit_enabled(link) && event == SD_LLDP_EVENT_ADDED) { |
90dffb22 LP |
1421 | /* If we received information about a new neighbor, restart the LLDP "fast" logic */ |
1422 | ||
1423 | log_link_debug(link, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission."); | |
1424 | ||
7272b25e | 1425 | r = link_lldp_emit_start(link); |
90dffb22 LP |
1426 | if (r < 0) |
1427 | log_link_warning_errno(link, r, "Failed to restart LLDP transmission: %m"); | |
1428 | } | |
49699bac SS |
1429 | } |
1430 | ||
e7ab854c TG |
1431 | static int link_acquire_ipv6_conf(Link *link) { |
1432 | int r; | |
1433 | ||
1434 | assert(link); | |
1435 | ||
1436 | if (link_dhcp6_enabled(link)) { | |
1437 | assert(link->dhcp6_client); | |
c601ebf7 | 1438 | assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); |
e7ab854c TG |
1439 | |
1440 | log_link_debug(link, "Acquiring DHCPv6 lease"); | |
1441 | ||
c601ebf7 TG |
1442 | r = sd_dhcp6_client_set_local_address(link->dhcp6_client, &link->ipv6ll_address); |
1443 | if (r < 0 && r != -EBUSY) | |
1444 | return log_link_warning_errno(link, r, "Could not set IPv6LL address in DHCP client: %m"); | |
1445 | ||
e7ab854c | 1446 | r = sd_dhcp6_client_start(link->dhcp6_client); |
63348d13 | 1447 | if (r < 0 && r != -EBUSY) |
e7ab854c TG |
1448 | return log_link_warning_errno(link, r, "Could not acquire DHCPv6 lease: %m"); |
1449 | } | |
1450 | ||
1451 | if (link_ipv6_accept_ra_enabled(link)) { | |
1452 | assert(link->ndisc_router_discovery); | |
1453 | ||
1454 | log_link_debug(link, "Discovering IPv6 routers"); | |
1455 | ||
1456 | r = sd_ndisc_router_discovery_start(link->ndisc_router_discovery); | |
63348d13 | 1457 | if (r < 0 && r != -EBUSY) |
e7ab854c TG |
1458 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Discovery: %m"); |
1459 | } | |
1460 | ||
1461 | return 0; | |
1462 | } | |
1463 | ||
6fc25497 | 1464 | static int link_acquire_ipv4_conf(Link *link) { |
ff254138 TG |
1465 | int r; |
1466 | ||
1467 | assert(link); | |
1468 | assert(link->network); | |
ff254138 TG |
1469 | assert(link->manager); |
1470 | assert(link->manager->event); | |
1471 | ||
78c958f8 | 1472 | if (link_ipv4ll_enabled(link)) { |
eb34d4af | 1473 | assert(link->ipv4ll); |
ff254138 | 1474 | |
6a7a4e4d | 1475 | log_link_debug(link, "Acquiring IPv4 link-local address"); |
5c1d3fc9 UTL |
1476 | |
1477 | r = sd_ipv4ll_start(link->ipv4ll); | |
6a7a4e4d LP |
1478 | if (r < 0) |
1479 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
5c1d3fc9 UTL |
1480 | } |
1481 | ||
78c958f8 | 1482 | if (link_dhcp4_enabled(link)) { |
eb34d4af | 1483 | assert(link->dhcp_client); |
ff254138 | 1484 | |
6a7a4e4d | 1485 | log_link_debug(link, "Acquiring DHCPv4 lease"); |
ab47d620 | 1486 | |
5c1d3fc9 | 1487 | r = sd_dhcp_client_start(link->dhcp_client); |
6a7a4e4d LP |
1488 | if (r < 0) |
1489 | return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m"); | |
5c1d3fc9 | 1490 | } |
ff254138 | 1491 | |
6fc25497 SS |
1492 | return 0; |
1493 | } | |
1494 | ||
1495 | static int link_acquire_conf(Link *link) { | |
1496 | int r; | |
1497 | ||
1498 | assert(link); | |
1499 | ||
1500 | r = link_acquire_ipv4_conf(link); | |
1501 | if (r < 0) | |
1502 | return r; | |
1503 | ||
1504 | if (in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address) == 0) { | |
1505 | r = link_acquire_ipv6_conf(link); | |
1506 | if (r < 0) | |
1507 | return r; | |
1508 | } | |
1509 | ||
7272b25e LP |
1510 | if (link_lldp_emit_enabled(link)) { |
1511 | r = link_lldp_emit_start(link); | |
8e1ad1ea LP |
1512 | if (r < 0) |
1513 | return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m"); | |
1514 | } | |
1515 | ||
ff254138 TG |
1516 | return 0; |
1517 | } | |
1518 | ||
a61bb41c | 1519 | bool link_has_carrier(Link *link) { |
deb2e523 TG |
1520 | /* see Documentation/networking/operstates.txt in the kernel sources */ |
1521 | ||
a61bb41c | 1522 | if (link->kernel_operstate == IF_OPER_UP) |
deb2e523 TG |
1523 | return true; |
1524 | ||
a61bb41c | 1525 | if (link->kernel_operstate == IF_OPER_UNKNOWN) |
deb2e523 | 1526 | /* operstate may not be implemented, so fall back to flags */ |
a61bb41c | 1527 | if ((link->flags & IFF_LOWER_UP) && !(link->flags & IFF_DORMANT)) |
deb2e523 TG |
1528 | return true; |
1529 | ||
1530 | return false; | |
1531 | } | |
1532 | ||
1c4baffc | 1533 | static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
5da8149f | 1534 | _cleanup_link_unref_ Link *link = userdata; |
dd3efc09 TG |
1535 | int r; |
1536 | ||
1746cf2a TG |
1537 | assert(link); |
1538 | ||
5da8149f | 1539 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1746cf2a TG |
1540 | return 1; |
1541 | ||
1c4baffc | 1542 | r = sd_netlink_message_get_errno(m); |
6a7a4e4d LP |
1543 | if (r < 0) |
1544 | /* we warn but don't fail the link, as it may be | |
1545 | brought up later */ | |
a2fae7bb | 1546 | log_link_warning_errno(link, r, "Could not bring up interface: %m"); |
45ad2c13 | 1547 | |
f882c247 TG |
1548 | return 1; |
1549 | } | |
1550 | ||
1551 | static int link_up(Link *link) { | |
4afd3348 | 1552 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
5c3072ea | 1553 | uint8_t ipv6ll_mode; |
f579559b TG |
1554 | int r; |
1555 | ||
f882c247 | 1556 | assert(link); |
c106cc36 | 1557 | assert(link->network); |
f882c247 TG |
1558 | assert(link->manager); |
1559 | assert(link->manager->rtnl); | |
1560 | ||
6a7a4e4d | 1561 | log_link_debug(link, "Bringing link up"); |
449f7554 | 1562 | |
6a7a4e4d LP |
1563 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1564 | if (r < 0) | |
1565 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
f579559b | 1566 | |
5d4795f3 | 1567 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
6a7a4e4d LP |
1568 | if (r < 0) |
1569 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
fc25d7f8 | 1570 | |
c106cc36 | 1571 | if (link->network->mac) { |
1c4baffc | 1572 | r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac); |
6a7a4e4d LP |
1573 | if (r < 0) |
1574 | return log_link_error_errno(link, r, "Could not set MAC address: %m"); | |
c106cc36 TG |
1575 | } |
1576 | ||
439689c6 SS |
1577 | /* If IPv6 not configured (no static IPv6 address and neither DHCPv6 nor IPv6LL is enabled) |
1578 | for this interface then disable IPv6 else enable it. */ | |
1579 | (void) link_enable_ipv6(link); | |
1580 | ||
c106cc36 | 1581 | if (link->network->mtu) { |
439689c6 SS |
1582 | /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes |
1583 | on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */ | |
1584 | if (link_ipv6_enabled(link) && link->network->mtu < IPV6_MIN_MTU) { | |
1585 | ||
1586 | log_link_warning(link, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU) ", as " | |
1587 | "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU) " bytes: %m"); | |
1588 | ||
1589 | link->network->mtu = IPV6_MIN_MTU; | |
1590 | } | |
1591 | ||
1c4baffc | 1592 | r = sd_netlink_message_append_u32(req, IFLA_MTU, link->network->mtu); |
6a7a4e4d LP |
1593 | if (r < 0) |
1594 | return log_link_error_errno(link, r, "Could not set MTU: %m"); | |
c106cc36 TG |
1595 | } |
1596 | ||
1c4baffc | 1597 | r = sd_netlink_message_open_container(req, IFLA_AF_SPEC); |
6a7a4e4d LP |
1598 | if (r < 0) |
1599 | return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1600 | |
439689c6 | 1601 | if (link_ipv6_enabled(link)) { |
01d28f81 | 1602 | /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */ |
1c4baffc | 1603 | r = sd_netlink_message_open_container(req, AF_INET6); |
01d28f81 TG |
1604 | if (r < 0) |
1605 | return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m"); | |
d0d6a4cd | 1606 | |
01d28f81 | 1607 | ipv6ll_mode = link_ipv6ll_enabled(link) ? IN6_ADDR_GEN_MODE_EUI64 : IN6_ADDR_GEN_MODE_NONE; |
1c4baffc | 1608 | r = sd_netlink_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode); |
01d28f81 TG |
1609 | if (r < 0) |
1610 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m"); | |
d0d6a4cd | 1611 | |
01d28f81 | 1612 | if (!in_addr_is_null(AF_INET6, &link->network->ipv6_token)) { |
1c4baffc | 1613 | r = sd_netlink_message_append_in6_addr(req, IFLA_INET6_TOKEN, &link->network->ipv6_token.in6); |
01d28f81 TG |
1614 | if (r < 0) |
1615 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_TOKEN: %m"); | |
1616 | } | |
1617 | ||
1c4baffc | 1618 | r = sd_netlink_message_close_container(req); |
6a7a4e4d | 1619 | if (r < 0) |
01d28f81 | 1620 | return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m"); |
7f77697a | 1621 | } |
d0d6a4cd | 1622 | |
1c4baffc | 1623 | r = sd_netlink_message_close_container(req); |
6a7a4e4d LP |
1624 | if (r < 0) |
1625 | return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1626 | |
1c4baffc | 1627 | r = sd_netlink_call_async(link->manager->rtnl, req, link_up_handler, link, 0, NULL); |
6a7a4e4d LP |
1628 | if (r < 0) |
1629 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
f579559b | 1630 | |
b226d99b TG |
1631 | link_ref(link); |
1632 | ||
f882c247 TG |
1633 | return 0; |
1634 | } | |
1635 | ||
1c4baffc | 1636 | static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
0d4ad91d AR |
1637 | _cleanup_link_unref_ Link *link = userdata; |
1638 | int r; | |
1639 | ||
1640 | assert(link); | |
1641 | ||
1642 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1643 | return 1; | |
1644 | ||
1c4baffc | 1645 | r = sd_netlink_message_get_errno(m); |
0d4ad91d | 1646 | if (r < 0) |
a2fae7bb | 1647 | log_link_warning_errno(link, r, "Could not bring down interface: %m"); |
0d4ad91d AR |
1648 | |
1649 | return 1; | |
1650 | } | |
1651 | ||
1652 | static int link_down(Link *link) { | |
4afd3348 | 1653 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
0d4ad91d AR |
1654 | int r; |
1655 | ||
1656 | assert(link); | |
1657 | assert(link->manager); | |
1658 | assert(link->manager->rtnl); | |
1659 | ||
6a7a4e4d | 1660 | log_link_debug(link, "Bringing link down"); |
0d4ad91d AR |
1661 | |
1662 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, | |
1663 | RTM_SETLINK, link->ifindex); | |
6a7a4e4d LP |
1664 | if (r < 0) |
1665 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
0d4ad91d AR |
1666 | |
1667 | r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP); | |
6a7a4e4d LP |
1668 | if (r < 0) |
1669 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
0d4ad91d | 1670 | |
1c4baffc | 1671 | r = sd_netlink_call_async(link->manager->rtnl, req, link_down_handler, link, 0, NULL); |
6a7a4e4d LP |
1672 | if (r < 0) |
1673 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
0d4ad91d AR |
1674 | |
1675 | link_ref(link); | |
1676 | ||
1677 | return 0; | |
1678 | } | |
1679 | ||
1680 | static int link_handle_bound_to_list(Link *link) { | |
1681 | Link *l; | |
1682 | Iterator i; | |
1683 | int r; | |
1684 | bool required_up = false; | |
1685 | bool link_is_up = false; | |
1686 | ||
1687 | assert(link); | |
1688 | ||
1689 | if (hashmap_isempty(link->bound_to_links)) | |
1690 | return 0; | |
1691 | ||
1692 | if (link->flags & IFF_UP) | |
1693 | link_is_up = true; | |
1694 | ||
1695 | HASHMAP_FOREACH (l, link->bound_to_links, i) | |
1696 | if (link_has_carrier(l)) { | |
1697 | required_up = true; | |
1698 | break; | |
1699 | } | |
1700 | ||
1701 | if (!required_up && link_is_up) { | |
1702 | r = link_down(link); | |
1703 | if (r < 0) | |
1704 | return r; | |
1705 | } else if (required_up && !link_is_up) { | |
1706 | r = link_up(link); | |
1707 | if (r < 0) | |
1708 | return r; | |
1709 | } | |
1710 | ||
1711 | return 0; | |
1712 | } | |
1713 | ||
1714 | static int link_handle_bound_by_list(Link *link) { | |
1715 | Iterator i; | |
1716 | Link *l; | |
1717 | int r; | |
1718 | ||
1719 | assert(link); | |
1720 | ||
1721 | if (hashmap_isempty(link->bound_by_links)) | |
1722 | return 0; | |
1723 | ||
1724 | HASHMAP_FOREACH (l, link->bound_by_links, i) { | |
1725 | r = link_handle_bound_to_list(l); | |
1726 | if (r < 0) | |
1727 | return r; | |
1728 | } | |
1729 | ||
1730 | return 0; | |
1731 | } | |
1732 | ||
1733 | static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) { | |
1734 | int r; | |
1735 | ||
1736 | assert(link); | |
1737 | assert(carrier); | |
1738 | ||
1739 | if (link == carrier) | |
1740 | return 0; | |
1741 | ||
1742 | if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex))) | |
1743 | return 0; | |
1744 | ||
1745 | r = hashmap_ensure_allocated(h, NULL); | |
1746 | if (r < 0) | |
1747 | return r; | |
1748 | ||
1749 | r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier); | |
1750 | if (r < 0) | |
1751 | return r; | |
1752 | ||
1753 | return 0; | |
1754 | } | |
1755 | ||
1756 | static int link_new_bound_by_list(Link *link) { | |
1757 | Manager *m; | |
1758 | Link *carrier; | |
1759 | Iterator i; | |
1760 | int r; | |
1761 | bool list_updated = false; | |
1762 | ||
1763 | assert(link); | |
1764 | assert(link->manager); | |
1765 | ||
1766 | m = link->manager; | |
1767 | ||
b295beea | 1768 | HASHMAP_FOREACH(carrier, m->links, i) { |
0d4ad91d AR |
1769 | if (!carrier->network) |
1770 | continue; | |
1771 | ||
1772 | if (strv_isempty(carrier->network->bind_carrier)) | |
1773 | continue; | |
1774 | ||
1775 | if (strv_fnmatch(carrier->network->bind_carrier, link->ifname, 0)) { | |
1776 | r = link_put_carrier(link, carrier, &link->bound_by_links); | |
1777 | if (r < 0) | |
1778 | return r; | |
1779 | ||
1780 | list_updated = true; | |
1781 | } | |
1782 | } | |
1783 | ||
1784 | if (list_updated) | |
84de38c5 | 1785 | link_dirty(link); |
0d4ad91d | 1786 | |
b295beea | 1787 | HASHMAP_FOREACH(carrier, link->bound_by_links, i) { |
0d4ad91d AR |
1788 | r = link_put_carrier(carrier, link, &carrier->bound_to_links); |
1789 | if (r < 0) | |
1790 | return r; | |
1791 | ||
84de38c5 | 1792 | link_dirty(carrier); |
0d4ad91d AR |
1793 | } |
1794 | ||
1795 | return 0; | |
1796 | } | |
1797 | ||
1798 | static int link_new_bound_to_list(Link *link) { | |
1799 | Manager *m; | |
1800 | Link *carrier; | |
1801 | Iterator i; | |
1802 | int r; | |
1803 | bool list_updated = false; | |
1804 | ||
1805 | assert(link); | |
1806 | assert(link->manager); | |
1807 | ||
1808 | if (!link->network) | |
1809 | return 0; | |
1810 | ||
1811 | if (strv_isempty(link->network->bind_carrier)) | |
1812 | return 0; | |
1813 | ||
1814 | m = link->manager; | |
1815 | ||
1816 | HASHMAP_FOREACH (carrier, m->links, i) { | |
1817 | if (strv_fnmatch(link->network->bind_carrier, carrier->ifname, 0)) { | |
1818 | r = link_put_carrier(link, carrier, &link->bound_to_links); | |
1819 | if (r < 0) | |
1820 | return r; | |
1821 | ||
1822 | list_updated = true; | |
1823 | } | |
1824 | } | |
1825 | ||
1826 | if (list_updated) | |
84de38c5 | 1827 | link_dirty(link); |
0d4ad91d AR |
1828 | |
1829 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) { | |
1830 | r = link_put_carrier(carrier, link, &carrier->bound_by_links); | |
1831 | if (r < 0) | |
1832 | return r; | |
1833 | ||
84de38c5 | 1834 | link_dirty(carrier); |
0d4ad91d AR |
1835 | } |
1836 | ||
1837 | return 0; | |
1838 | } | |
1839 | ||
1840 | static int link_new_carrier_maps(Link *link) { | |
1841 | int r; | |
1842 | ||
1843 | r = link_new_bound_by_list(link); | |
1844 | if (r < 0) | |
1845 | return r; | |
1846 | ||
1847 | r = link_handle_bound_by_list(link); | |
1848 | if (r < 0) | |
1849 | return r; | |
1850 | ||
1851 | r = link_new_bound_to_list(link); | |
1852 | if (r < 0) | |
1853 | return r; | |
1854 | ||
1855 | r = link_handle_bound_to_list(link); | |
1856 | if (r < 0) | |
1857 | return r; | |
1858 | ||
1859 | return 0; | |
1860 | } | |
1861 | ||
1862 | static void link_free_bound_to_list(Link *link) { | |
1863 | Link *bound_to; | |
1864 | Iterator i; | |
1865 | ||
1866 | HASHMAP_FOREACH (bound_to, link->bound_to_links, i) { | |
1867 | hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex)); | |
1868 | ||
1869 | if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex))) | |
84de38c5 | 1870 | link_dirty(bound_to); |
0d4ad91d AR |
1871 | } |
1872 | ||
1873 | return; | |
1874 | } | |
1875 | ||
1876 | static void link_free_bound_by_list(Link *link) { | |
1877 | Link *bound_by; | |
1878 | Iterator i; | |
1879 | ||
1880 | HASHMAP_FOREACH (bound_by, link->bound_by_links, i) { | |
1881 | hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex)); | |
1882 | ||
1883 | if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) { | |
84de38c5 | 1884 | link_dirty(bound_by); |
0d4ad91d AR |
1885 | link_handle_bound_to_list(bound_by); |
1886 | } | |
1887 | } | |
1888 | ||
1889 | return; | |
1890 | } | |
1891 | ||
1892 | static void link_free_carrier_maps(Link *link) { | |
1893 | bool list_updated = false; | |
1894 | ||
1895 | assert(link); | |
1896 | ||
1897 | if (!hashmap_isempty(link->bound_to_links)) { | |
1898 | link_free_bound_to_list(link); | |
1899 | list_updated = true; | |
1900 | } | |
1901 | ||
1902 | if (!hashmap_isempty(link->bound_by_links)) { | |
1903 | link_free_bound_by_list(link); | |
1904 | list_updated = true; | |
1905 | } | |
1906 | ||
1907 | if (list_updated) | |
84de38c5 | 1908 | link_dirty(link); |
0d4ad91d AR |
1909 | |
1910 | return; | |
1911 | } | |
1912 | ||
1913 | void link_drop(Link *link) { | |
1914 | if (!link || link->state == LINK_STATE_LINGER) | |
1915 | return; | |
1916 | ||
1917 | link_set_state(link, LINK_STATE_LINGER); | |
1918 | ||
1919 | link_free_carrier_maps(link); | |
1920 | ||
6a7a4e4d | 1921 | log_link_debug(link, "Link removed"); |
0d4ad91d | 1922 | |
84de38c5 | 1923 | (void)unlink(link->state_file); |
0d4ad91d AR |
1924 | link_unref(link); |
1925 | ||
1926 | return; | |
1927 | } | |
1928 | ||
3f265037 | 1929 | static int link_joined(Link *link) { |
f882c247 TG |
1930 | int r; |
1931 | ||
ef1ba606 | 1932 | assert(link); |
f5be5601 | 1933 | assert(link->network); |
dd3efc09 | 1934 | |
0d4ad91d AR |
1935 | if (!hashmap_isempty(link->bound_to_links)) { |
1936 | r = link_handle_bound_to_list(link); | |
1937 | if (r < 0) | |
1938 | return r; | |
1939 | } else if (!(link->flags & IFF_UP)) { | |
505f8da7 TG |
1940 | r = link_up(link); |
1941 | if (r < 0) { | |
1942 | link_enter_failed(link); | |
1943 | return r; | |
1944 | } | |
ef1ba606 | 1945 | } |
f882c247 | 1946 | |
9ed794a3 | 1947 | if (link->network->bridge) { |
e1853b00 | 1948 | r = link_set_bridge(link); |
6a7a4e4d LP |
1949 | if (r < 0) |
1950 | log_link_error_errno(link, r, "Could not set bridge message: %m"); | |
e1853b00 SS |
1951 | } |
1952 | ||
fb6730c4 | 1953 | return link_enter_set_addresses(link); |
02b59d57 TG |
1954 | } |
1955 | ||
62e2d5bb | 1956 | static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
5da8149f | 1957 | _cleanup_link_unref_ Link *link = userdata; |
02b59d57 TG |
1958 | int r; |
1959 | ||
1746cf2a | 1960 | assert(link); |
ef1ba606 | 1961 | assert(link->network); |
02b59d57 | 1962 | |
313cefa1 | 1963 | link->enslaving--; |
52433f6b | 1964 | |
5da8149f | 1965 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
02b59d57 TG |
1966 | return 1; |
1967 | ||
1c4baffc | 1968 | r = sd_netlink_message_get_errno(m); |
856f962c | 1969 | if (r < 0 && r != -EEXIST) { |
a2fae7bb | 1970 | log_link_error_errno(link, r, "Could not join netdev: %m"); |
ef1ba606 TG |
1971 | link_enter_failed(link); |
1972 | return 1; | |
ba179154 | 1973 | } else |
6a7a4e4d | 1974 | log_link_debug(link, "Joined netdev"); |
02b59d57 | 1975 | |
856f962c | 1976 | if (link->enslaving <= 0) |
3f265037 | 1977 | link_joined(link); |
02b59d57 TG |
1978 | |
1979 | return 1; | |
1980 | } | |
1981 | ||
3f265037 | 1982 | static int link_enter_join_netdev(Link *link) { |
6a0a2f86 | 1983 | NetDev *netdev; |
672682a6 | 1984 | Iterator i; |
02b59d57 TG |
1985 | int r; |
1986 | ||
1987 | assert(link); | |
1988 | assert(link->network); | |
8434fd5c | 1989 | assert(link->state == LINK_STATE_PENDING); |
02b59d57 | 1990 | |
e331e246 | 1991 | link_set_state(link, LINK_STATE_ENSLAVING); |
02b59d57 | 1992 | |
84de38c5 | 1993 | link_dirty(link); |
fe8db0c5 | 1994 | |
7951dea2 SS |
1995 | if (!link->network->bridge && |
1996 | !link->network->bond && | |
6a0a2f86 | 1997 | hashmap_isempty(link->network->stacked_netdevs)) |
3f265037 | 1998 | return link_joined(link); |
02b59d57 | 1999 | |
d9c67ea1 | 2000 | if (link->network->bond) { |
f2341e0a LP |
2001 | log_struct(LOG_DEBUG, |
2002 | LOG_LINK_INTERFACE(link), | |
2003 | LOG_NETDEV_INTERFACE(link->network->bond), | |
2004 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname), | |
2005 | NULL); | |
2006 | ||
2007 | r = netdev_join(link->network->bond, link, netdev_join_handler); | |
52433f6b | 2008 | if (r < 0) { |
f2341e0a LP |
2009 | log_struct_errno(LOG_WARNING, r, |
2010 | LOG_LINK_INTERFACE(link), | |
2011 | LOG_NETDEV_INTERFACE(link->network->bond), | |
2012 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname), | |
2013 | NULL); | |
2014 | ||
52433f6b TG |
2015 | link_enter_failed(link); |
2016 | return r; | |
2017 | } | |
2018 | ||
313cefa1 | 2019 | link->enslaving++; |
0ad6148e MO |
2020 | } |
2021 | ||
d9c67ea1 | 2022 | if (link->network->bridge) { |
f2341e0a LP |
2023 | log_struct(LOG_DEBUG, |
2024 | LOG_LINK_INTERFACE(link), | |
2025 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
2026 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname), | |
2027 | NULL); | |
2028 | ||
2029 | r = netdev_join(link->network->bridge, link, netdev_join_handler); | |
0ad6148e | 2030 | if (r < 0) { |
f2341e0a LP |
2031 | log_struct_errno(LOG_WARNING, r, |
2032 | LOG_LINK_INTERFACE(link), | |
2033 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
2034 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname), | |
2035 | NULL), | |
0ad6148e MO |
2036 | link_enter_failed(link); |
2037 | return r; | |
2038 | } | |
2039 | ||
313cefa1 | 2040 | link->enslaving++; |
52433f6b TG |
2041 | } |
2042 | ||
6a0a2f86 | 2043 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) { |
7951dea2 | 2044 | |
f2341e0a LP |
2045 | log_struct(LOG_DEBUG, |
2046 | LOG_LINK_INTERFACE(link), | |
2047 | LOG_NETDEV_INTERFACE(netdev), | |
2048 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname), | |
2049 | NULL); | |
2050 | ||
2051 | r = netdev_join(netdev, link, netdev_join_handler); | |
7951dea2 | 2052 | if (r < 0) { |
f2341e0a LP |
2053 | log_struct_errno(LOG_WARNING, r, |
2054 | LOG_LINK_INTERFACE(link), | |
2055 | LOG_NETDEV_INTERFACE(netdev), | |
2056 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname), | |
2057 | NULL); | |
326cb406 SS |
2058 | link_enter_failed(link); |
2059 | return r; | |
2060 | } | |
2061 | ||
313cefa1 | 2062 | link->enslaving++; |
326cb406 SS |
2063 | } |
2064 | ||
ef1ba606 TG |
2065 | return 0; |
2066 | } | |
2067 | ||
769d324c | 2068 | static int link_set_ipv4_forward(Link *link) { |
5a8bcb67 LP |
2069 | int r; |
2070 | ||
765afd5c | 2071 | if (!link_ipv4_forward_enabled(link)) |
15dee3f0 LP |
2072 | return 0; |
2073 | ||
765afd5c LP |
2074 | /* We propagate the forwarding flag from one interface to the |
2075 | * global setting one way. This means: as long as at least one | |
2076 | * interface was configured at any time that had IP forwarding | |
2077 | * enabled the setting will stay on for good. We do this | |
2078 | * primarily to keep IPv4 and IPv6 packet forwarding behaviour | |
2079 | * somewhat in sync (see below). */ | |
15dee3f0 | 2080 | |
765afd5c | 2081 | r = write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2082 | if (r < 0) |
765afd5c | 2083 | log_link_warning_errno(link, r, "Cannot turn on IPv4 packet forwarding, ignoring: %m"); |
43c6d5ab | 2084 | |
769d324c LP |
2085 | return 0; |
2086 | } | |
2087 | ||
2088 | static int link_set_ipv6_forward(Link *link) { | |
769d324c LP |
2089 | int r; |
2090 | ||
765afd5c | 2091 | if (!link_ipv6_forward_enabled(link)) |
8add5f79 NO |
2092 | return 0; |
2093 | ||
765afd5c LP |
2094 | /* On Linux, the IPv6 stack does not not know a per-interface |
2095 | * packet forwarding setting: either packet forwarding is on | |
2096 | * for all, or off for all. We hence don't bother with a | |
2097 | * per-interface setting, but simply propagate the interface | |
2098 | * flag, if it is set, to the global flag, one-way. Note that | |
2099 | * while IPv4 would allow a per-interface flag, we expose the | |
2100 | * same behaviour there and also propagate the setting from | |
2101 | * one to all, to keep things simple (see above). */ | |
15dee3f0 | 2102 | |
765afd5c | 2103 | r = write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
eb3da901 | 2104 | if (r < 0) |
765afd5c | 2105 | log_link_warning_errno(link, r, "Cannot configure IPv6 packet forwarding, ignoring: %m"); |
5a8bcb67 LP |
2106 | |
2107 | return 0; | |
2108 | } | |
2109 | ||
49092e22 | 2110 | static int link_set_ipv6_privacy_extensions(Link *link) { |
1f0d9695 LP |
2111 | char buf[DECIMAL_STR_MAX(unsigned) + 1]; |
2112 | IPv6PrivacyExtensions s; | |
49092e22 SS |
2113 | const char *p = NULL; |
2114 | int r; | |
2115 | ||
1f0d9695 | 2116 | s = link_ipv6_privacy_extensions(link); |
66a6bd68 | 2117 | if (s < 0) |
49092e22 SS |
2118 | return 0; |
2119 | ||
2120 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/use_tempaddr"); | |
66a6bd68 | 2121 | xsprintf(buf, "%u", (unsigned) link->network->ipv6_privacy_extensions); |
49092e22 | 2122 | |
eb3da901 LP |
2123 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2124 | if (r < 0) | |
49092e22 SS |
2125 | log_link_warning_errno(link, r, "Cannot configure IPv6 privacy extension for interface: %m"); |
2126 | ||
2127 | return 0; | |
2128 | } | |
2129 | ||
4f2e437a | 2130 | static int link_set_ipv6_accept_ra(Link *link) { |
3b015d40 | 2131 | const char *p = NULL; |
4f2e437a SS |
2132 | int r; |
2133 | ||
2134 | /* Make this a NOP if IPv6 is not available */ | |
2135 | if (!socket_ipv6_is_supported()) | |
2136 | return 0; | |
2137 | ||
2138 | if (link->flags & IFF_LOOPBACK) | |
2139 | return 0; | |
2140 | ||
d68e2e59 LP |
2141 | if (!link->network) |
2142 | return 0; | |
2143 | ||
4f2e437a | 2144 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/accept_ra"); |
4f2e437a | 2145 | |
fe307276 TG |
2146 | /* We handle router advertisments ourselves, tell the kernel to GTFO */ |
2147 | r = write_string_file(p, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE); | |
eb3da901 | 2148 | if (r < 0) |
fe307276 | 2149 | log_link_warning_errno(link, r, "Cannot disable kernel IPv6 accept_ra for interface: %m"); |
4f2e437a SS |
2150 | |
2151 | return 0; | |
2152 | } | |
2153 | ||
8749cbcd | 2154 | static int link_set_ipv6_dad_transmits(Link *link) { |
66a6bd68 | 2155 | char buf[DECIMAL_STR_MAX(int) + 1]; |
8749cbcd SS |
2156 | const char *p = NULL; |
2157 | int r; | |
2158 | ||
2159 | /* Make this a NOP if IPv6 is not available */ | |
2160 | if (!socket_ipv6_is_supported()) | |
2161 | return 0; | |
2162 | ||
2163 | if (link->flags & IFF_LOOPBACK) | |
2164 | return 0; | |
2165 | ||
d68e2e59 LP |
2166 | if (!link->network) |
2167 | return 0; | |
2168 | ||
8749cbcd SS |
2169 | if (link->network->ipv6_dad_transmits < 0) |
2170 | return 0; | |
2171 | ||
2172 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/dad_transmits"); | |
66a6bd68 | 2173 | xsprintf(buf, "%i", link->network->ipv6_dad_transmits); |
8749cbcd | 2174 | |
eb3da901 LP |
2175 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2176 | if (r < 0) | |
8749cbcd | 2177 | log_link_warning_errno(link, r, "Cannot set IPv6 dad transmits for interface: %m"); |
8749cbcd SS |
2178 | |
2179 | return 0; | |
2180 | } | |
2181 | ||
b69c3180 | 2182 | static int link_set_ipv6_hop_limit(Link *link) { |
66a6bd68 | 2183 | char buf[DECIMAL_STR_MAX(int) + 1]; |
b69c3180 SS |
2184 | const char *p = NULL; |
2185 | int r; | |
2186 | ||
2187 | /* Make this a NOP if IPv6 is not available */ | |
2188 | if (!socket_ipv6_is_supported()) | |
2189 | return 0; | |
2190 | ||
2191 | if (link->flags & IFF_LOOPBACK) | |
2192 | return 0; | |
2193 | ||
d68e2e59 LP |
2194 | if (!link->network) |
2195 | return 0; | |
2196 | ||
b69c3180 SS |
2197 | if (link->network->ipv6_hop_limit < 0) |
2198 | return 0; | |
2199 | ||
2200 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/hop_limit"); | |
66a6bd68 | 2201 | xsprintf(buf, "%i", link->network->ipv6_hop_limit); |
b69c3180 | 2202 | |
eb3da901 LP |
2203 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE); |
2204 | if (r < 0) | |
b69c3180 | 2205 | log_link_warning_errno(link, r, "Cannot set IPv6 hop limit for interface: %m"); |
b69c3180 SS |
2206 | |
2207 | return 0; | |
2208 | } | |
2209 | ||
5e5b137a TG |
2210 | static int link_drop_foreign_config(Link *link) { |
2211 | Address *address; | |
2212 | Route *route; | |
2213 | Iterator i; | |
2214 | int r; | |
2215 | ||
2216 | SET_FOREACH(address, link->addresses_foreign, i) { | |
fe307276 | 2217 | /* we consider IPv6LL addresses to be managed by the kernel */ |
5e5b137a TG |
2218 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) |
2219 | continue; | |
2220 | ||
2221 | r = address_remove(address, link, link_address_remove_handler); | |
2222 | if (r < 0) | |
2223 | return r; | |
2224 | } | |
2225 | ||
2226 | SET_FOREACH(route, link->routes_foreign, i) { | |
fe307276 | 2227 | /* do not touch routes managed by the kernel */ |
5e5b137a TG |
2228 | if (route->protocol == RTPROT_KERNEL) |
2229 | continue; | |
2230 | ||
2231 | r = route_remove(route, link, link_address_remove_handler); | |
2232 | if (r < 0) | |
2233 | return r; | |
2234 | } | |
2235 | ||
2236 | return 0; | |
2237 | } | |
2238 | ||
273eec24 LP |
2239 | static int link_update_lldp(Link *link) { |
2240 | int r; | |
2241 | ||
2242 | assert(link); | |
2243 | ||
2244 | if (!link->lldp) | |
2245 | return 0; | |
2246 | ||
2247 | if (link->flags & IFF_UP) { | |
2248 | r = sd_lldp_start(link->lldp); | |
2249 | if (r > 0) | |
2250 | log_link_debug(link, "Started LLDP."); | |
2251 | } else { | |
2252 | r = sd_lldp_stop(link->lldp); | |
2253 | if (r > 0) | |
2254 | log_link_debug(link, "Stopped LLDP."); | |
2255 | } | |
2256 | ||
2257 | return r; | |
2258 | } | |
2259 | ||
a748b692 | 2260 | static int link_configure(Link *link) { |
02b59d57 TG |
2261 | int r; |
2262 | ||
ef1ba606 | 2263 | assert(link); |
b22d8a00 | 2264 | assert(link->network); |
8434fd5c | 2265 | assert(link->state == LINK_STATE_PENDING); |
a748b692 | 2266 | |
02e28621 ДГ |
2267 | /* Drop foreign config, but ignore loopback or critical devices. |
2268 | * We do not want to remove loopback address or addresses used for root NFS. */ | |
2269 | if (!(link->flags & IFF_LOOPBACK) && !(link->network->dhcp_critical)) { | |
e5d44b34 CH |
2270 | r = link_drop_foreign_config(link); |
2271 | if (r < 0) | |
2272 | return r; | |
2273 | } | |
5e5b137a | 2274 | |
b98b483b AR |
2275 | r = link_set_bridge_fdb(link); |
2276 | if (r < 0) | |
2277 | return r; | |
2278 | ||
23d8b221 SS |
2279 | r = link_set_proxy_arp(link); |
2280 | if (r < 0) | |
2281 | return r; | |
2282 | ||
769d324c LP |
2283 | r = link_set_ipv4_forward(link); |
2284 | if (r < 0) | |
2285 | return r; | |
2286 | ||
2287 | r = link_set_ipv6_forward(link); | |
5a8bcb67 LP |
2288 | if (r < 0) |
2289 | return r; | |
2290 | ||
49092e22 SS |
2291 | r = link_set_ipv6_privacy_extensions(link); |
2292 | if (r < 0) | |
2293 | return r; | |
2294 | ||
4f2e437a SS |
2295 | r = link_set_ipv6_accept_ra(link); |
2296 | if (r < 0) | |
2297 | return r; | |
2298 | ||
8749cbcd SS |
2299 | r = link_set_ipv6_dad_transmits(link); |
2300 | if (r < 0) | |
2301 | return r; | |
b69c3180 SS |
2302 | |
2303 | r = link_set_ipv6_hop_limit(link); | |
2304 | if (r < 0) | |
2305 | return r; | |
8749cbcd | 2306 | |
78c958f8 | 2307 | if (link_ipv4ll_enabled(link)) { |
b22d8a00 | 2308 | r = ipv4ll_configure(link); |
eb34d4af TG |
2309 | if (r < 0) |
2310 | return r; | |
2311 | } | |
2312 | ||
78c958f8 | 2313 | if (link_dhcp4_enabled(link)) { |
3c9b8860 | 2314 | r = dhcp4_configure(link); |
eb34d4af TG |
2315 | if (r < 0) |
2316 | return r; | |
eb34d4af TG |
2317 | } |
2318 | ||
78c958f8 | 2319 | if (link_dhcp4_server_enabled(link)) { |
dd43110f TG |
2320 | r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); |
2321 | if (r < 0) | |
2322 | return r; | |
2323 | ||
2324 | r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); | |
2325 | if (r < 0) | |
2326 | return r; | |
dd43110f TG |
2327 | } |
2328 | ||
62379e88 TG |
2329 | if (link_dhcp6_enabled(link) || |
2330 | link_ipv6_accept_ra_enabled(link)) { | |
f5a8c43f TG |
2331 | r = dhcp6_configure(link); |
2332 | if (r < 0) | |
2333 | return r; | |
2334 | } | |
2335 | ||
2336 | if (link_ipv6_accept_ra_enabled(link)) { | |
de1e9928 | 2337 | r = ndisc_configure(link); |
4138fb2c PF |
2338 | if (r < 0) |
2339 | return r; | |
2340 | } | |
2341 | ||
8e1ad1ea | 2342 | if (link_lldp_rx_enabled(link)) { |
34437b4f LP |
2343 | r = sd_lldp_new(&link->lldp, link->ifindex); |
2344 | if (r < 0) | |
2345 | return r; | |
2346 | ||
2347 | r = sd_lldp_match_capabilities(link->lldp, | |
2348 | link->network->lldp_mode == LLDP_MODE_ROUTERS_ONLY ? | |
6afa6767 BG |
2349 | SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS : |
2350 | SD_LLDP_SYSTEM_CAPABILITIES_ALL); | |
ce43e484 SS |
2351 | if (r < 0) |
2352 | return r; | |
2353 | ||
b553a6b1 LP |
2354 | r = sd_lldp_set_filter_address(link->lldp, &link->mac); |
2355 | if (r < 0) | |
2356 | return r; | |
2357 | ||
ce43e484 SS |
2358 | r = sd_lldp_attach_event(link->lldp, NULL, 0); |
2359 | if (r < 0) | |
2360 | return r; | |
49699bac | 2361 | |
032b27f5 | 2362 | r = sd_lldp_set_callback(link->lldp, lldp_handler, link); |
49699bac SS |
2363 | if (r < 0) |
2364 | return r; | |
273eec24 LP |
2365 | |
2366 | r = link_update_lldp(link); | |
2367 | if (r < 0) | |
2368 | return r; | |
ce43e484 SS |
2369 | } |
2370 | ||
a61bb41c | 2371 | if (link_has_carrier(link)) { |
1e9be60b TG |
2372 | r = link_acquire_conf(link); |
2373 | if (r < 0) | |
2374 | return r; | |
cc544d5f | 2375 | } |
1e9be60b | 2376 | |
3f265037 | 2377 | return link_enter_join_netdev(link); |
505f8da7 TG |
2378 | } |
2379 | ||
1c4baffc | 2380 | static int link_initialized_and_synced(sd_netlink *rtnl, sd_netlink_message *m, |
3c9b8860 | 2381 | void *userdata) { |
5da8149f | 2382 | _cleanup_link_unref_ Link *link = userdata; |
505f8da7 | 2383 | Network *network; |
505f8da7 TG |
2384 | int r; |
2385 | ||
2386 | assert(link); | |
2387 | assert(link->ifname); | |
2388 | assert(link->manager); | |
2389 | ||
8434fd5c | 2390 | if (link->state != LINK_STATE_PENDING) |
5da8149f | 2391 | return 1; |
505f8da7 | 2392 | |
6a7a4e4d | 2393 | log_link_debug(link, "Link state is up-to-date"); |
505f8da7 | 2394 | |
0d4ad91d AR |
2395 | r = link_new_bound_by_list(link); |
2396 | if (r < 0) | |
2397 | return r; | |
2398 | ||
2399 | r = link_handle_bound_by_list(link); | |
2400 | if (r < 0) | |
2401 | return r; | |
2402 | ||
c4a03a56 TG |
2403 | if (!link->network) { |
2404 | r = network_get(link->manager, link->udev_device, link->ifname, | |
2405 | &link->mac, &network); | |
2406 | if (r == -ENOENT) { | |
2407 | link_enter_unmanaged(link); | |
2408 | return 1; | |
2409 | } else if (r < 0) | |
2410 | return r; | |
505f8da7 | 2411 | |
c4a03a56 TG |
2412 | if (link->flags & IFF_LOOPBACK) { |
2413 | if (network->link_local != ADDRESS_FAMILY_NO) | |
2414 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); | |
78c958f8 | 2415 | |
c4a03a56 TG |
2416 | if (network->dhcp != ADDRESS_FAMILY_NO) |
2417 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); | |
78c958f8 | 2418 | |
c4a03a56 TG |
2419 | if (network->dhcp_server) |
2420 | log_link_debug(link, "Ignoring DHCP server for loopback link"); | |
2421 | } | |
bd2efe92 | 2422 | |
c4a03a56 TG |
2423 | r = network_apply(link->manager, network, link); |
2424 | if (r < 0) | |
2425 | return r; | |
2426 | } | |
505f8da7 | 2427 | |
0d4ad91d AR |
2428 | r = link_new_bound_to_list(link); |
2429 | if (r < 0) | |
2430 | return r; | |
2431 | ||
a748b692 TG |
2432 | r = link_configure(link); |
2433 | if (r < 0) | |
2434 | return r; | |
2435 | ||
5da8149f | 2436 | return 1; |
505f8da7 TG |
2437 | } |
2438 | ||
4f561e8e | 2439 | int link_initialized(Link *link, struct udev_device *device) { |
4afd3348 | 2440 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f561e8e TG |
2441 | int r; |
2442 | ||
2443 | assert(link); | |
2444 | assert(link->manager); | |
2445 | assert(link->manager->rtnl); | |
2446 | assert(device); | |
2447 | ||
8434fd5c | 2448 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
2449 | return 0; |
2450 | ||
679b3605 TG |
2451 | if (link->udev_device) |
2452 | return 0; | |
2453 | ||
79008bdd | 2454 | log_link_debug(link, "udev initialized link"); |
4f561e8e TG |
2455 | |
2456 | link->udev_device = udev_device_ref(device); | |
2457 | ||
3c9b8860 TG |
2458 | /* udev has initialized the link, but we don't know if we have yet |
2459 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
2460 | * when it returns we know that the pending NEWLINKs have already been | |
2461 | * processed and that we are up-to-date */ | |
4f561e8e | 2462 | |
3c9b8860 TG |
2463 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
2464 | link->ifindex); | |
4f561e8e TG |
2465 | if (r < 0) |
2466 | return r; | |
2467 | ||
1c4baffc | 2468 | r = sd_netlink_call_async(link->manager->rtnl, req, |
3c9b8860 | 2469 | link_initialized_and_synced, link, 0, NULL); |
4f561e8e TG |
2470 | if (r < 0) |
2471 | return r; | |
2472 | ||
5da8149f TG |
2473 | link_ref(link); |
2474 | ||
4f561e8e TG |
2475 | return 0; |
2476 | } | |
2477 | ||
c4a03a56 | 2478 | static int link_load(Link *link) { |
0bc70f1d TG |
2479 | _cleanup_free_ char *network_file = NULL, |
2480 | *addresses = NULL, | |
f703cc2c | 2481 | *routes = NULL, |
0bc70f1d TG |
2482 | *dhcp4_address = NULL, |
2483 | *ipv4ll_address = NULL; | |
2484 | union in_addr_union address; | |
f703cc2c | 2485 | union in_addr_union route_dst; |
c598ac76 | 2486 | const char *p; |
c4a03a56 TG |
2487 | int r; |
2488 | ||
2489 | assert(link); | |
2490 | ||
2491 | r = parse_env_file(link->state_file, NEWLINE, | |
2492 | "NETWORK_FILE", &network_file, | |
2493 | "ADDRESSES", &addresses, | |
f703cc2c | 2494 | "ROUTES", &routes, |
0bc70f1d TG |
2495 | "DHCP4_ADDRESS", &dhcp4_address, |
2496 | "IPV4LL_ADDRESS", &ipv4ll_address, | |
c4a03a56 TG |
2497 | NULL); |
2498 | if (r < 0 && r != -ENOENT) | |
2499 | return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file); | |
2500 | ||
2501 | if (network_file) { | |
2502 | Network *network; | |
2503 | char *suffix; | |
2504 | ||
2505 | /* drop suffix */ | |
2506 | suffix = strrchr(network_file, '.'); | |
2507 | if (!suffix) { | |
2508 | log_link_debug(link, "Failed to get network name from %s", network_file); | |
2509 | goto network_file_fail; | |
2510 | } | |
2511 | *suffix = '\0'; | |
2512 | ||
2513 | r = network_get_by_name(link->manager, basename(network_file), &network); | |
2514 | if (r < 0) { | |
2515 | log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file)); | |
2516 | goto network_file_fail; | |
2517 | } | |
2518 | ||
2519 | r = network_apply(link->manager, network, link); | |
2520 | if (r < 0) | |
2521 | return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file)); | |
2522 | } | |
2523 | ||
2524 | network_file_fail: | |
2525 | ||
2526 | if (addresses) { | |
c598ac76 | 2527 | p = addresses; |
c4a03a56 | 2528 | |
c598ac76 TG |
2529 | for (;;) { |
2530 | _cleanup_free_ char *address_str = NULL; | |
c4a03a56 TG |
2531 | char *prefixlen_str; |
2532 | int family; | |
2533 | unsigned char prefixlen; | |
c4a03a56 | 2534 | |
c598ac76 TG |
2535 | r = extract_first_word(&p, &address_str, NULL, 0); |
2536 | if (r < 0) { | |
2537 | log_link_debug_errno(link, r, "Failed to extract next address string: %m"); | |
2538 | continue; | |
928bc597 TA |
2539 | } |
2540 | if (r == 0) | |
c598ac76 TG |
2541 | break; |
2542 | ||
2543 | prefixlen_str = strchr(address_str, '/'); | |
c4a03a56 | 2544 | if (!prefixlen_str) { |
c598ac76 | 2545 | log_link_debug(link, "Failed to parse address and prefix length %s", address_str); |
c4a03a56 TG |
2546 | continue; |
2547 | } | |
2548 | ||
313cefa1 | 2549 | *prefixlen_str++ = '\0'; |
c4a03a56 TG |
2550 | |
2551 | r = sscanf(prefixlen_str, "%hhu", &prefixlen); | |
2552 | if (r != 1) { | |
2553 | log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str); | |
2554 | continue; | |
2555 | } | |
2556 | ||
c598ac76 | 2557 | r = in_addr_from_string_auto(address_str, &family, &address); |
c4a03a56 | 2558 | if (r < 0) { |
c598ac76 | 2559 | log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str); |
c4a03a56 TG |
2560 | continue; |
2561 | } | |
2562 | ||
2563 | r = address_add(link, family, &address, prefixlen, NULL); | |
2564 | if (r < 0) | |
2565 | return log_link_error_errno(link, r, "Failed to add address: %m"); | |
2566 | } | |
2567 | } | |
2568 | ||
f703cc2c | 2569 | if (routes) { |
74544b4e TA |
2570 | p = routes; |
2571 | ||
c598ac76 | 2572 | for (;;) { |
f833694d | 2573 | Route *route; |
c598ac76 | 2574 | _cleanup_free_ char *route_str = NULL; |
4afd3348 | 2575 | _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; |
f833694d | 2576 | usec_t lifetime; |
f703cc2c TG |
2577 | char *prefixlen_str; |
2578 | int family; | |
2579 | unsigned char prefixlen, tos, table; | |
2580 | uint32_t priority; | |
2581 | ||
c598ac76 TG |
2582 | r = extract_first_word(&p, &route_str, NULL, 0); |
2583 | if (r < 0) { | |
2584 | log_link_debug_errno(link, r, "Failed to extract next route string: %m"); | |
2585 | continue; | |
928bc597 TA |
2586 | } |
2587 | if (r == 0) | |
c598ac76 TG |
2588 | break; |
2589 | ||
2590 | prefixlen_str = strchr(route_str, '/'); | |
f703cc2c | 2591 | if (!prefixlen_str) { |
c598ac76 | 2592 | log_link_debug(link, "Failed to parse route %s", route_str); |
f703cc2c TG |
2593 | continue; |
2594 | } | |
2595 | ||
313cefa1 | 2596 | *prefixlen_str++ = '\0'; |
f703cc2c | 2597 | |
f833694d TG |
2598 | r = sscanf(prefixlen_str, "%hhu/%hhu/%"SCNu32"/%hhu/"USEC_FMT, &prefixlen, &tos, &priority, &table, &lifetime); |
2599 | if (r != 5) { | |
2600 | log_link_debug(link, | |
2601 | "Failed to parse destination prefix length, tos, priority, table or expiration %s", | |
2602 | prefixlen_str); | |
f703cc2c TG |
2603 | continue; |
2604 | } | |
2605 | ||
c598ac76 | 2606 | r = in_addr_from_string_auto(route_str, &family, &route_dst); |
f703cc2c | 2607 | if (r < 0) { |
c598ac76 | 2608 | log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str); |
f703cc2c TG |
2609 | continue; |
2610 | } | |
2611 | ||
f833694d | 2612 | r = route_add(link, family, &route_dst, prefixlen, tos, priority, table, &route); |
f703cc2c TG |
2613 | if (r < 0) |
2614 | return log_link_error_errno(link, r, "Failed to add route: %m"); | |
f833694d TG |
2615 | |
2616 | if (lifetime != USEC_INFINITY) { | |
2617 | r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), lifetime, | |
2618 | 0, route_expire_handler, route); | |
2619 | if (r < 0) | |
2620 | log_link_warning_errno(link, r, "Could not arm route expiration handler: %m"); | |
2621 | } | |
2622 | ||
2623 | route->lifetime = lifetime; | |
2624 | sd_event_source_unref(route->expire); | |
2625 | route->expire = expire; | |
2626 | expire = NULL; | |
f703cc2c TG |
2627 | } |
2628 | } | |
2629 | ||
0bc70f1d TG |
2630 | if (dhcp4_address) { |
2631 | r = in_addr_from_string(AF_INET, dhcp4_address, &address); | |
2632 | if (r < 0) { | |
2633 | log_link_debug_errno(link, r, "Falied to parse DHCPv4 address %s: %m", dhcp4_address); | |
2634 | goto dhcp4_address_fail; | |
2635 | } | |
2636 | ||
2637 | r = sd_dhcp_client_new(&link->dhcp_client); | |
2638 | if (r < 0) | |
2639 | return log_link_error_errno(link, r, "Falied to create DHCPv4 client: %m"); | |
2640 | ||
2641 | r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in); | |
2642 | if (r < 0) | |
2643 | return log_link_error_errno(link, r, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address); | |
2644 | } | |
2645 | ||
2646 | dhcp4_address_fail: | |
2647 | ||
2648 | if (ipv4ll_address) { | |
2649 | r = in_addr_from_string(AF_INET, ipv4ll_address, &address); | |
2650 | if (r < 0) { | |
2651 | log_link_debug_errno(link, r, "Falied to parse IPv4LL address %s: %m", ipv4ll_address); | |
2652 | goto ipv4ll_address_fail; | |
2653 | } | |
2654 | ||
2655 | r = sd_ipv4ll_new(&link->ipv4ll); | |
2656 | if (r < 0) | |
2657 | return log_link_error_errno(link, r, "Falied to create IPv4LL client: %m"); | |
2658 | ||
2659 | r = sd_ipv4ll_set_address(link->ipv4ll, &address.in); | |
2660 | if (r < 0) | |
2661 | return log_link_error_errno(link, r, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address); | |
2662 | } | |
2663 | ||
2664 | ipv4ll_address_fail: | |
2665 | ||
c4a03a56 TG |
2666 | return 0; |
2667 | } | |
2668 | ||
1c4baffc | 2669 | int link_add(Manager *m, sd_netlink_message *message, Link **ret) { |
505f8da7 TG |
2670 | Link *link; |
2671 | _cleanup_udev_device_unref_ struct udev_device *device = NULL; | |
2672 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; | |
2673 | int r; | |
2674 | ||
2675 | assert(m); | |
fbbeb65a | 2676 | assert(m->rtnl); |
505f8da7 TG |
2677 | assert(message); |
2678 | assert(ret); | |
2679 | ||
2680 | r = link_new(m, message, ret); | |
2681 | if (r < 0) | |
2682 | return r; | |
2683 | ||
2684 | link = *ret; | |
2685 | ||
6a7a4e4d | 2686 | log_link_debug(link, "Link %d added", link->ifindex); |
505f8da7 | 2687 | |
c4a03a56 TG |
2688 | r = link_load(link); |
2689 | if (r < 0) | |
2690 | return r; | |
2691 | ||
75f86906 | 2692 | if (detect_container() <= 0) { |
505f8da7 | 2693 | /* not in a container, udev will be around */ |
ae06ab10 | 2694 | sprintf(ifindex_str, "n%d", link->ifindex); |
505f8da7 | 2695 | device = udev_device_new_from_device_id(m->udev, ifindex_str); |
5c416fc4 TG |
2696 | if (!device) { |
2697 | r = log_link_warning_errno(link, errno, "Could not find udev device: %m"); | |
2698 | goto failed; | |
2699 | } | |
505f8da7 | 2700 | |
3c4cb064 | 2701 | if (udev_device_get_is_initialized(device) <= 0) { |
505f8da7 | 2702 | /* not yet ready */ |
79008bdd | 2703 | log_link_debug(link, "link pending udev initialization..."); |
505f8da7 | 2704 | return 0; |
3c4cb064 | 2705 | } |
505f8da7 | 2706 | |
4f561e8e TG |
2707 | r = link_initialized(link, device); |
2708 | if (r < 0) | |
5c416fc4 | 2709 | goto failed; |
4f561e8e | 2710 | } else { |
5da8149f TG |
2711 | /* we are calling a callback directly, so must take a ref */ |
2712 | link_ref(link); | |
2713 | ||
4f561e8e TG |
2714 | r = link_initialized_and_synced(m->rtnl, NULL, link); |
2715 | if (r < 0) | |
5c416fc4 | 2716 | goto failed; |
4f561e8e | 2717 | } |
505f8da7 | 2718 | |
a748b692 | 2719 | return 0; |
5c416fc4 TG |
2720 | failed: |
2721 | link_enter_failed(link); | |
2722 | return r; | |
a748b692 TG |
2723 | } |
2724 | ||
c601ebf7 | 2725 | int link_ipv6ll_gained(Link *link, const struct in6_addr *address) { |
e7ab854c TG |
2726 | int r; |
2727 | ||
2728 | assert(link); | |
2729 | ||
2730 | log_link_info(link, "Gained IPv6LL"); | |
2731 | ||
c601ebf7 | 2732 | link->ipv6ll_address = *address; |
e7ab854c TG |
2733 | link_check_ready(link); |
2734 | ||
201f0c91 | 2735 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
e7ab854c TG |
2736 | r = link_acquire_ipv6_conf(link); |
2737 | if (r < 0) { | |
2738 | link_enter_failed(link); | |
2739 | return r; | |
2740 | } | |
2741 | } | |
2742 | ||
2743 | return 0; | |
2744 | } | |
2745 | ||
9c0a72f9 TG |
2746 | static int link_carrier_gained(Link *link) { |
2747 | int r; | |
2748 | ||
2749 | assert(link); | |
2750 | ||
201f0c91 | 2751 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
9c0a72f9 TG |
2752 | r = link_acquire_conf(link); |
2753 | if (r < 0) { | |
2754 | link_enter_failed(link); | |
2755 | return r; | |
2756 | } | |
6fc25497 SS |
2757 | |
2758 | r = link_enter_set_addresses(link); | |
2759 | if (r < 0) | |
2760 | return r; | |
9c0a72f9 TG |
2761 | } |
2762 | ||
0d4ad91d AR |
2763 | r = link_handle_bound_by_list(link); |
2764 | if (r < 0) | |
2765 | return r; | |
2766 | ||
9c0a72f9 TG |
2767 | return 0; |
2768 | } | |
2769 | ||
2770 | static int link_carrier_lost(Link *link) { | |
2771 | int r; | |
2772 | ||
2773 | assert(link); | |
2774 | ||
2775 | r = link_stop_clients(link); | |
2776 | if (r < 0) { | |
2777 | link_enter_failed(link); | |
2778 | return r; | |
2779 | } | |
2780 | ||
0d4ad91d AR |
2781 | r = link_handle_bound_by_list(link); |
2782 | if (r < 0) | |
2783 | return r; | |
2784 | ||
9c0a72f9 TG |
2785 | return 0; |
2786 | } | |
2787 | ||
2788 | int link_carrier_reset(Link *link) { | |
2789 | int r; | |
2790 | ||
2791 | assert(link); | |
2792 | ||
2793 | if (link_has_carrier(link)) { | |
2794 | r = link_carrier_lost(link); | |
2795 | if (r < 0) | |
2796 | return r; | |
2797 | ||
2798 | r = link_carrier_gained(link); | |
2799 | if (r < 0) | |
2800 | return r; | |
2801 | ||
6a7a4e4d | 2802 | log_link_info(link, "Reset carrier"); |
9c0a72f9 TG |
2803 | } |
2804 | ||
2805 | return 0; | |
2806 | } | |
2807 | ||
1c4baffc | 2808 | int link_update(Link *link, sd_netlink_message *m) { |
c49b33ac | 2809 | struct ether_addr mac; |
ca4e095a | 2810 | const char *ifname; |
afe7fd56 | 2811 | uint32_t mtu; |
a61bb41c | 2812 | bool had_carrier, carrier_gained, carrier_lost; |
22936833 TG |
2813 | int r; |
2814 | ||
dd3efc09 | 2815 | assert(link); |
b8941f74 | 2816 | assert(link->ifname); |
22936833 TG |
2817 | assert(m); |
2818 | ||
7619683b TG |
2819 | if (link->state == LINK_STATE_LINGER) { |
2820 | link_ref(link); | |
6a7a4e4d | 2821 | log_link_info(link, "Link readded"); |
e331e246 | 2822 | link_set_state(link, LINK_STATE_ENSLAVING); |
0d4ad91d AR |
2823 | |
2824 | r = link_new_carrier_maps(link); | |
2825 | if (r < 0) | |
2826 | return r; | |
7619683b TG |
2827 | } |
2828 | ||
1c4baffc | 2829 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
b8941f74 | 2830 | if (r >= 0 && !streq(ifname, link->ifname)) { |
6a7a4e4d | 2831 | log_link_info(link, "Renamed to %s", ifname); |
b8941f74 | 2832 | |
0d4ad91d AR |
2833 | link_free_carrier_maps(link); |
2834 | ||
2fc09a9c DM |
2835 | r = free_and_strdup(&link->ifname, ifname); |
2836 | if (r < 0) | |
2837 | return r; | |
0d4ad91d AR |
2838 | |
2839 | r = link_new_carrier_maps(link); | |
2840 | if (r < 0) | |
2841 | return r; | |
b8941f74 TG |
2842 | } |
2843 | ||
1c4baffc | 2844 | r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu); |
afe7fd56 TG |
2845 | if (r >= 0 && mtu > 0) { |
2846 | link->mtu = mtu; | |
2847 | if (!link->original_mtu) { | |
2848 | link->original_mtu = mtu; | |
6a7a4e4d | 2849 | log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu); |
afe7fd56 TG |
2850 | } |
2851 | ||
2852 | if (link->dhcp_client) { | |
3c9b8860 TG |
2853 | r = sd_dhcp_client_set_mtu(link->dhcp_client, |
2854 | link->mtu); | |
afe7fd56 | 2855 | if (r < 0) { |
6a7a4e4d | 2856 | log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m"); |
afe7fd56 TG |
2857 | return r; |
2858 | } | |
2859 | } | |
9842de0d | 2860 | } |
69629de9 | 2861 | |
e9189a1f TG |
2862 | /* The kernel may broadcast NEWLINK messages without the MAC address |
2863 | set, simply ignore them. */ | |
1c4baffc | 2864 | r = sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &mac); |
e9189a1f | 2865 | if (r >= 0) { |
3c9b8860 TG |
2866 | if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, |
2867 | ETH_ALEN)) { | |
c49b33ac | 2868 | |
3c9b8860 TG |
2869 | memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, |
2870 | ETH_ALEN); | |
c49b33ac | 2871 | |
79008bdd | 2872 | log_link_debug(link, "MAC address: " |
20861203 TG |
2873 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
2874 | mac.ether_addr_octet[0], | |
2875 | mac.ether_addr_octet[1], | |
2876 | mac.ether_addr_octet[2], | |
2877 | mac.ether_addr_octet[3], | |
2878 | mac.ether_addr_octet[4], | |
2879 | mac.ether_addr_octet[5]); | |
c49b33ac | 2880 | |
20861203 TG |
2881 | if (link->ipv4ll) { |
2882 | r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac); | |
6a7a4e4d LP |
2883 | if (r < 0) |
2884 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); | |
c49b33ac | 2885 | } |
c49b33ac | 2886 | |
20861203 | 2887 | if (link->dhcp_client) { |
8341a5c3 ZJS |
2888 | const DUID *duid = link_duid(link); |
2889 | ||
3c9b8860 | 2890 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
76253e73 DW |
2891 | (const uint8_t *) &link->mac, |
2892 | sizeof (link->mac), | |
2893 | ARPHRD_ETHER); | |
6a7a4e4d LP |
2894 | if (r < 0) |
2895 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
413708d1 | 2896 | |
8341a5c3 ZJS |
2897 | r = sd_dhcp_client_set_iaid_duid(link->dhcp_client, |
2898 | link->network->iaid, | |
2899 | duid->type, | |
2900 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
2901 | duid->raw_data_len); | |
413708d1 VK |
2902 | if (r < 0) |
2903 | return log_link_warning_errno(link, r, "Could not update DUID/IAID in DHCP client: %m"); | |
c49b33ac | 2904 | } |
4138fb2c PF |
2905 | |
2906 | if (link->dhcp6_client) { | |
8341a5c3 ZJS |
2907 | const DUID* duid = link_duid(link); |
2908 | ||
4138fb2c | 2909 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, |
76253e73 DW |
2910 | (const uint8_t *) &link->mac, |
2911 | sizeof (link->mac), | |
2912 | ARPHRD_ETHER); | |
6a7a4e4d LP |
2913 | if (r < 0) |
2914 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
413708d1 VK |
2915 | |
2916 | r = sd_dhcp6_client_set_iaid(link->dhcp6_client, | |
2917 | link->network->iaid); | |
2918 | if (r < 0) | |
2919 | return log_link_warning_errno(link, r, "Could not update DHCPv6 IAID: %m"); | |
2920 | ||
8341a5c3 ZJS |
2921 | r = sd_dhcp6_client_set_duid(link->dhcp6_client, |
2922 | duid->type, | |
2923 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
2924 | duid->raw_data_len); | |
413708d1 VK |
2925 | if (r < 0) |
2926 | return log_link_warning_errno(link, r, "Could not update DHCPv6 DUID: %m"); | |
4138fb2c | 2927 | } |
c49b33ac | 2928 | } |
4f882b2a TG |
2929 | } |
2930 | ||
a61bb41c TG |
2931 | had_carrier = link_has_carrier(link); |
2932 | ||
2933 | r = link_update_flags(link, m); | |
2934 | if (r < 0) | |
2935 | return r; | |
2936 | ||
273eec24 LP |
2937 | r = link_update_lldp(link); |
2938 | if (r < 0) | |
2939 | return r; | |
2940 | ||
a61bb41c TG |
2941 | carrier_gained = !had_carrier && link_has_carrier(link); |
2942 | carrier_lost = had_carrier && !link_has_carrier(link); | |
2943 | ||
2944 | if (carrier_gained) { | |
6a7a4e4d | 2945 | log_link_info(link, "Gained carrier"); |
a61bb41c | 2946 | |
9c0a72f9 TG |
2947 | r = link_carrier_gained(link); |
2948 | if (r < 0) | |
2949 | return r; | |
a61bb41c | 2950 | } else if (carrier_lost) { |
6a7a4e4d | 2951 | log_link_info(link, "Lost carrier"); |
a61bb41c | 2952 | |
9c0a72f9 TG |
2953 | r = link_carrier_lost(link); |
2954 | if (r < 0) | |
a61bb41c | 2955 | return r; |
a61bb41c TG |
2956 | } |
2957 | ||
2958 | return 0; | |
dd3efc09 | 2959 | } |
fe8db0c5 | 2960 | |
b295beea LP |
2961 | static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) { |
2962 | bool space = false; | |
2963 | Iterator i; | |
2964 | Link *link; | |
2965 | ||
2966 | assert(f); | |
2967 | assert(prefix); | |
2968 | ||
2969 | if (hashmap_isempty(h)) | |
2970 | return; | |
2971 | ||
2972 | fputs(prefix, f); | |
2973 | HASHMAP_FOREACH(link, h, i) { | |
2974 | if (space) | |
2975 | fputc(' ', f); | |
2976 | ||
2977 | fprintf(f, "%i", link->ifindex); | |
2978 | space = true; | |
2979 | } | |
2980 | ||
2981 | fputc('\n', f); | |
2982 | } | |
2983 | ||
fe8db0c5 | 2984 | int link_save(Link *link) { |
68a8723c | 2985 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 | 2986 | _cleanup_fclose_ FILE *f = NULL; |
e375dcde | 2987 | const char *admin_state, *oper_state; |
e7780c8d | 2988 | Address *a; |
c1eb9872 | 2989 | Route *route; |
e7780c8d | 2990 | Iterator i; |
fe8db0c5 TG |
2991 | int r; |
2992 | ||
2993 | assert(link); | |
2994 | assert(link->state_file); | |
68a8723c | 2995 | assert(link->lease_file); |
bbf7c048 TG |
2996 | assert(link->manager); |
2997 | ||
370e9930 TG |
2998 | if (link->state == LINK_STATE_LINGER) { |
2999 | unlink(link->state_file); | |
3000 | return 0; | |
3001 | } | |
3002 | ||
34437b4f LP |
3003 | link_lldp_save(link); |
3004 | ||
deb2e523 TG |
3005 | admin_state = link_state_to_string(link->state); |
3006 | assert(admin_state); | |
3007 | ||
e375dcde TG |
3008 | oper_state = link_operstate_to_string(link->operstate); |
3009 | assert(oper_state); | |
deb2e523 | 3010 | |
fe8db0c5 TG |
3011 | r = fopen_temporary(link->state_file, &f, &temp_path); |
3012 | if (r < 0) | |
6a7a4e4d | 3013 | goto fail; |
fe8db0c5 TG |
3014 | |
3015 | fchmod(fileno(f), 0644); | |
3016 | ||
3017 | fprintf(f, | |
3018 | "# This is private data. Do not parse.\n" | |
deb2e523 | 3019 | "ADMIN_STATE=%s\n" |
6dcaa6f5 TG |
3020 | "OPER_STATE=%s\n", |
3021 | admin_state, oper_state); | |
fe8db0c5 | 3022 | |
bcb7a07e | 3023 | if (link->network) { |
ea352b40 | 3024 | bool space; |
07bdc70d | 3025 | sd_dhcp6_lease *dhcp6_lease = NULL; |
b2a81c0b LP |
3026 | const char *dhcp_domainname = NULL; |
3027 | char **dhcp6_domains = NULL; | |
07bdc70d PF |
3028 | |
3029 | if (link->dhcp6_client) { | |
4058d339 TG |
3030 | r = sd_dhcp6_client_get_lease(link->dhcp6_client, &dhcp6_lease); |
3031 | if (r < 0 && r != -ENOMSG) | |
07bdc70d PF |
3032 | log_link_debug(link, "No DHCPv6 lease"); |
3033 | } | |
b0e39c82 | 3034 | |
adc5b2e2 TG |
3035 | fprintf(f, "NETWORK_FILE=%s\n", link->network->filename); |
3036 | ||
b0e39c82 | 3037 | fputs("DNS=", f); |
ea352b40 | 3038 | space = false; |
3df9bec5 | 3039 | fputstrv(f, link->network->dns, NULL, &space); |
d5314fff | 3040 | |
27cb34f5 | 3041 | if (link->network->dhcp_use_dns && |
b0e39c82 TG |
3042 | link->dhcp_lease) { |
3043 | const struct in_addr *addresses; | |
3044 | ||
3045 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
3046 | if (r > 0) { | |
ea352b40 LP |
3047 | if (space) |
3048 | fputc(' ', f); | |
b0e39c82 | 3049 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3050 | space = true; |
3051 | } | |
3052 | } | |
3053 | ||
27cb34f5 | 3054 | if (link->network->dhcp_use_dns && dhcp6_lease) { |
07bdc70d PF |
3055 | struct in6_addr *in6_addrs; |
3056 | ||
3057 | r = sd_dhcp6_lease_get_dns(dhcp6_lease, &in6_addrs); | |
3058 | if (r > 0) { | |
3059 | if (space) | |
3060 | fputc(' ', f); | |
3061 | serialize_in6_addrs(f, in6_addrs, r); | |
b0e39c82 TG |
3062 | } |
3063 | } | |
3064 | ||
2ce40956 | 3065 | fputc('\n', f); |
b0e39c82 | 3066 | |
2ce40956 | 3067 | fputs("NTP=", f); |
ea352b40 | 3068 | space = false; |
3df9bec5 | 3069 | fputstrv(f, link->network->ntp, NULL, &space); |
d5314fff | 3070 | |
27cb34f5 | 3071 | if (link->network->dhcp_use_ntp && |
b0e39c82 TG |
3072 | link->dhcp_lease) { |
3073 | const struct in_addr *addresses; | |
3074 | ||
3075 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
3076 | if (r > 0) { | |
ea352b40 LP |
3077 | if (space) |
3078 | fputc(' ', f); | |
b0e39c82 | 3079 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3080 | space = true; |
3081 | } | |
3082 | } | |
3083 | ||
27cb34f5 | 3084 | if (link->network->dhcp_use_ntp && dhcp6_lease) { |
07bdc70d PF |
3085 | struct in6_addr *in6_addrs; |
3086 | char **hosts; | |
07bdc70d PF |
3087 | |
3088 | r = sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease, | |
3089 | &in6_addrs); | |
3090 | if (r > 0) { | |
3091 | if (space) | |
3092 | fputc(' ', f); | |
3093 | serialize_in6_addrs(f, in6_addrs, r); | |
3094 | space = true; | |
3095 | } | |
3096 | ||
3097 | r = sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease, &hosts); | |
3df9bec5 LP |
3098 | if (r > 0) |
3099 | fputstrv(f, hosts, NULL, &space); | |
b0e39c82 TG |
3100 | } |
3101 | ||
2ce40956 | 3102 | fputc('\n', f); |
bd8f6538 | 3103 | |
b2a81c0b LP |
3104 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
3105 | if (link->dhcp_lease) | |
3106 | (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname); | |
9b4d1882 | 3107 | |
b2a81c0b LP |
3108 | if (dhcp6_lease) |
3109 | (void) sd_dhcp6_lease_get_domains(dhcp6_lease, &dhcp6_domains); | |
07bdc70d PF |
3110 | } |
3111 | ||
b2a81c0b LP |
3112 | fputs("DOMAINS=", f); |
3113 | fputstrv(f, link->network->search_domains, NULL, &space); | |
07bdc70d | 3114 | |
d390f8ef LP |
3115 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES && dhcp_domainname) |
3116 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
9b4d1882 | 3117 | |
b2a81c0b LP |
3118 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES && dhcp6_domains) |
3119 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3120 | ||
2ce40956 | 3121 | fputc('\n', f); |
6192b846 | 3122 | |
3df9bec5 LP |
3123 | fputs("ROUTE_DOMAINS=", f); |
3124 | fputstrv(f, link->network->route_domains, NULL, NULL); | |
b2a81c0b | 3125 | |
d390f8ef LP |
3126 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE && dhcp_domainname) |
3127 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b2a81c0b LP |
3128 | |
3129 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE && dhcp6_domains) | |
3130 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3131 | ||
3df9bec5 | 3132 | fputc('\n', f); |
67272d15 | 3133 | |
3c9b8860 | 3134 | fprintf(f, "LLMNR=%s\n", |
a7e5da6e | 3135 | resolve_support_to_string(link->network->llmnr)); |
aaa297d4 LP |
3136 | fprintf(f, "MDNS=%s\n", |
3137 | resolve_support_to_string(link->network->mdns)); | |
e7780c8d | 3138 | |
ad6c0475 LP |
3139 | if (link->network->dnssec_mode != _DNSSEC_MODE_INVALID) |
3140 | fprintf(f, "DNSSEC=%s\n", | |
3141 | dnssec_mode_to_string(link->network->dnssec_mode)); | |
3142 | ||
8a516214 LP |
3143 | if (!set_isempty(link->network->dnssec_negative_trust_anchors)) { |
3144 | const char *n; | |
3145 | ||
3146 | fputs("DNSSEC_NTA=", f); | |
3147 | space = false; | |
d390f8ef LP |
3148 | SET_FOREACH(n, link->network->dnssec_negative_trust_anchors, i) |
3149 | fputs_with_space(f, n, NULL, &space); | |
8a516214 LP |
3150 | fputc('\n', f); |
3151 | } | |
3152 | ||
2ce40956 | 3153 | fputs("ADDRESSES=", f); |
e7780c8d TG |
3154 | space = false; |
3155 | SET_FOREACH(a, link->addresses, i) { | |
3156 | _cleanup_free_ char *address_str = NULL; | |
3157 | ||
3158 | r = in_addr_to_string(a->family, &a->in_addr, &address_str); | |
3159 | if (r < 0) | |
3160 | goto fail; | |
3161 | ||
e7780c8d TG |
3162 | fprintf(f, "%s%s/%u", space ? " " : "", address_str, a->prefixlen); |
3163 | space = true; | |
3164 | } | |
2ce40956 | 3165 | fputc('\n', f); |
c1eb9872 TG |
3166 | |
3167 | fputs("ROUTES=", f); | |
3168 | space = false; | |
3169 | SET_FOREACH(route, link->routes, i) { | |
3170 | _cleanup_free_ char *route_str = NULL; | |
3171 | ||
3172 | r = in_addr_to_string(route->family, &route->dst, &route_str); | |
3173 | if (r < 0) | |
3174 | goto fail; | |
3175 | ||
f833694d TG |
3176 | fprintf(f, "%s%s/%hhu/%hhu/%"PRIu32"/%hhu/"USEC_FMT, space ? " " : "", route_str, |
3177 | route->dst_prefixlen, route->tos, route->priority, route->table, route->lifetime); | |
c1eb9872 TG |
3178 | space = true; |
3179 | } | |
3180 | ||
3181 | fputc('\n', f); | |
bcb7a07e | 3182 | } |
7374f9d8 | 3183 | |
b295beea LP |
3184 | print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links); |
3185 | print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links); | |
0d4ad91d | 3186 | |
8eb9058d | 3187 | if (link->dhcp_lease) { |
0bc70f1d | 3188 | struct in_addr address; |
8eb9058d LP |
3189 | const char *tz = NULL; |
3190 | ||
0bc70f1d TG |
3191 | assert(link->network); |
3192 | ||
8eb9058d LP |
3193 | r = sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); |
3194 | if (r >= 0) | |
3195 | fprintf(f, "TIMEZONE=%s\n", tz); | |
8eb9058d | 3196 | |
0bc70f1d TG |
3197 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
3198 | if (r >= 0) { | |
3199 | fputs("DHCP4_ADDRESS=", f); | |
3200 | serialize_in_addrs(f, &address, 1); | |
3201 | fputc('\n', f); | |
3202 | } | |
d9876a52 | 3203 | |
bd91b83e | 3204 | r = dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 3205 | if (r < 0) |
c2d6bd61 | 3206 | goto fail; |
fe8db0c5 | 3207 | |
7374f9d8 | 3208 | fprintf(f, |
b0e39c82 TG |
3209 | "DHCP_LEASE=%s\n", |
3210 | link->lease_file); | |
deb2e523 | 3211 | } else |
68a8723c | 3212 | unlink(link->lease_file); |
fe8db0c5 | 3213 | |
0bc70f1d TG |
3214 | if (link->ipv4ll) { |
3215 | struct in_addr address; | |
3216 | ||
3217 | r = sd_ipv4ll_get_address(link->ipv4ll, &address); | |
3218 | if (r >= 0) { | |
3219 | fputs("IPV4LL_ADDRESS=", f); | |
3220 | serialize_in_addrs(f, &address, 1); | |
3221 | fputc('\n', f); | |
3222 | } | |
3223 | } | |
3224 | ||
c2d6bd61 LP |
3225 | r = fflush_and_check(f); |
3226 | if (r < 0) | |
3227 | goto fail; | |
fe8db0c5 | 3228 | |
c2d6bd61 | 3229 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 3230 | r = -errno; |
c2d6bd61 | 3231 | goto fail; |
fe8db0c5 TG |
3232 | } |
3233 | ||
c2d6bd61 | 3234 | return 0; |
dacd6cee | 3235 | |
c2d6bd61 | 3236 | fail: |
6a7a4e4d | 3237 | (void) unlink(link->state_file); |
6a7a4e4d LP |
3238 | if (temp_path) |
3239 | (void) unlink(temp_path); | |
3240 | ||
dacd6cee | 3241 | return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file); |
fe8db0c5 TG |
3242 | } |
3243 | ||
84de38c5 TG |
3244 | /* The serialized state in /run is no longer up-to-date. */ |
3245 | void link_dirty(Link *link) { | |
3246 | int r; | |
3247 | ||
3248 | assert(link); | |
3249 | ||
0c241a37 SS |
3250 | /* mark manager dirty as link is dirty */ |
3251 | manager_dirty(link->manager); | |
3252 | ||
84de38c5 TG |
3253 | r = set_ensure_allocated(&link->manager->dirty_links, NULL); |
3254 | if (r < 0) | |
3255 | /* allocation errors are ignored */ | |
3256 | return; | |
3257 | ||
3258 | r = set_put(link->manager->dirty_links, link); | |
0c241a37 SS |
3259 | if (r <= 0) |
3260 | /* don't take another ref if the link was already dirty */ | |
84de38c5 TG |
3261 | return; |
3262 | ||
3263 | link_ref(link); | |
3264 | } | |
3265 | ||
3266 | /* The serialized state in /run is up-to-date */ | |
3267 | void link_clean(Link *link) { | |
3268 | assert(link); | |
3269 | assert(link->manager); | |
3270 | ||
3271 | set_remove(link->manager->dirty_links, link); | |
3272 | link_unref(link); | |
3273 | } | |
3274 | ||
fe8db0c5 | 3275 | static const char* const link_state_table[_LINK_STATE_MAX] = { |
8434fd5c | 3276 | [LINK_STATE_PENDING] = "pending", |
fe8db0c5 TG |
3277 | [LINK_STATE_ENSLAVING] = "configuring", |
3278 | [LINK_STATE_SETTING_ADDRESSES] = "configuring", | |
3279 | [LINK_STATE_SETTING_ROUTES] = "configuring", | |
3280 | [LINK_STATE_CONFIGURED] = "configured", | |
57bd6899 | 3281 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 3282 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 3283 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
3284 | }; |
3285 | ||
3286 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); | |
e375dcde TG |
3287 | |
3288 | static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = { | |
d3df0e39 TG |
3289 | [LINK_OPERSTATE_OFF] = "off", |
3290 | [LINK_OPERSTATE_NO_CARRIER] = "no-carrier", | |
e375dcde TG |
3291 | [LINK_OPERSTATE_DORMANT] = "dormant", |
3292 | [LINK_OPERSTATE_CARRIER] = "carrier", | |
3293 | [LINK_OPERSTATE_DEGRADED] = "degraded", | |
3294 | [LINK_OPERSTATE_ROUTABLE] = "routable", | |
3295 | }; | |
3296 | ||
3297 | DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState); |