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