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